]> err.no Git - linux-2.6/blob - drivers/pnp/pnpacpi/rsparser.c
Merge branch 'pnp' into release
[linux-2.6] / drivers / pnp / pnpacpi / rsparser.c
1 /*
2  * pnpacpi -- PnP ACPI driver
3  *
4  * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5  * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <linux/kernel.h>
22 #include <linux/acpi.h>
23 #include <linux/pci.h>
24 #include <linux/pnp.h>
25 #include "../base.h"
26 #include "pnpacpi.h"
27
28 #ifdef CONFIG_IA64
29 #define valid_IRQ(i) (1)
30 #else
31 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
32 #endif
33
34 /*
35  * Allocated Resources
36  */
37 static int irq_flags(int triggering, int polarity, int shareable)
38 {
39         int flags;
40
41         if (triggering == ACPI_LEVEL_SENSITIVE) {
42                 if (polarity == ACPI_ACTIVE_LOW)
43                         flags = IORESOURCE_IRQ_LOWLEVEL;
44                 else
45                         flags = IORESOURCE_IRQ_HIGHLEVEL;
46         } else {
47                 if (polarity == ACPI_ACTIVE_LOW)
48                         flags = IORESOURCE_IRQ_LOWEDGE;
49                 else
50                         flags = IORESOURCE_IRQ_HIGHEDGE;
51         }
52
53         if (shareable)
54                 flags |= IORESOURCE_IRQ_SHAREABLE;
55
56         return flags;
57 }
58
59 static void decode_irq_flags(int flag, int *triggering, int *polarity)
60 {
61         switch (flag) {
62         case IORESOURCE_IRQ_LOWLEVEL:
63                 *triggering = ACPI_LEVEL_SENSITIVE;
64                 *polarity = ACPI_ACTIVE_LOW;
65                 break;
66         case IORESOURCE_IRQ_HIGHLEVEL:
67                 *triggering = ACPI_LEVEL_SENSITIVE;
68                 *polarity = ACPI_ACTIVE_HIGH;
69                 break;
70         case IORESOURCE_IRQ_LOWEDGE:
71                 *triggering = ACPI_EDGE_SENSITIVE;
72                 *polarity = ACPI_ACTIVE_LOW;
73                 break;
74         case IORESOURCE_IRQ_HIGHEDGE:
75                 *triggering = ACPI_EDGE_SENSITIVE;
76                 *polarity = ACPI_ACTIVE_HIGH;
77                 break;
78         }
79 }
80
81 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
82                                                 u32 gsi, int triggering,
83                                                 int polarity, int shareable)
84 {
85         int irq, flags;
86         int p, t;
87
88         if (!valid_IRQ(gsi))
89                 return;
90
91         /*
92          * in IO-APIC mode, use overrided attribute. Two reasons:
93          * 1. BIOS bug in DSDT
94          * 2. BIOS uses IO-APIC mode Interrupt Source Override
95          */
96         if (!acpi_get_override_irq(gsi, &t, &p)) {
97                 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
98                 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
99
100                 if (triggering != t || polarity != p) {
101                         dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
102                                 gsi, t ? "edge":"level", p ? "low":"high");
103                         triggering = t;
104                         polarity = p;
105                 }
106         }
107
108         flags = irq_flags(triggering, polarity, shareable);
109         irq = acpi_register_gsi(gsi, triggering, polarity);
110         if (irq >= 0)
111                 pcibios_penalize_isa_irq(irq, 1);
112         else
113                 flags |= IORESOURCE_DISABLED;
114
115         pnp_add_irq_resource(dev, irq, flags);
116 }
117
118 static int dma_flags(int type, int bus_master, int transfer)
119 {
120         int flags = 0;
121
122         if (bus_master)
123                 flags |= IORESOURCE_DMA_MASTER;
124         switch (type) {
125         case ACPI_COMPATIBILITY:
126                 flags |= IORESOURCE_DMA_COMPATIBLE;
127                 break;
128         case ACPI_TYPE_A:
129                 flags |= IORESOURCE_DMA_TYPEA;
130                 break;
131         case ACPI_TYPE_B:
132                 flags |= IORESOURCE_DMA_TYPEB;
133                 break;
134         case ACPI_TYPE_F:
135                 flags |= IORESOURCE_DMA_TYPEF;
136                 break;
137         default:
138                 /* Set a default value ? */
139                 flags |= IORESOURCE_DMA_COMPATIBLE;
140                 pnp_err("Invalid DMA type");
141         }
142         switch (transfer) {
143         case ACPI_TRANSFER_8:
144                 flags |= IORESOURCE_DMA_8BIT;
145                 break;
146         case ACPI_TRANSFER_8_16:
147                 flags |= IORESOURCE_DMA_8AND16BIT;
148                 break;
149         case ACPI_TRANSFER_16:
150                 flags |= IORESOURCE_DMA_16BIT;
151                 break;
152         default:
153                 /* Set a default value ? */
154                 flags |= IORESOURCE_DMA_8AND16BIT;
155                 pnp_err("Invalid DMA transfer type");
156         }
157
158         return flags;
159 }
160
161 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
162                                                u64 len, int io_decode)
163 {
164         int flags = 0;
165         u64 end = start + len - 1;
166
167         if (io_decode == ACPI_DECODE_16)
168                 flags |= PNP_PORT_FLAG_16BITADDR;
169         if (len == 0 || end >= 0x10003)
170                 flags |= IORESOURCE_DISABLED;
171
172         pnp_add_io_resource(dev, start, end, flags);
173 }
174
175 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
176                                                 u64 start, u64 len,
177                                                 int write_protect)
178 {
179         int flags = 0;
180         u64 end = start + len - 1;
181
182         if (len == 0)
183                 flags |= IORESOURCE_DISABLED;
184         if (write_protect == ACPI_READ_WRITE_MEMORY)
185                 flags |= IORESOURCE_MEM_WRITEABLE;
186
187         pnp_add_mem_resource(dev, start, end, flags);
188 }
189
190 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
191                                                   struct acpi_resource *res)
192 {
193         struct acpi_resource_address64 addr, *p = &addr;
194         acpi_status status;
195
196         status = acpi_resource_to_address64(res, p);
197         if (!ACPI_SUCCESS(status)) {
198                 dev_warn(&dev->dev, "failed to convert resource type %d\n",
199                          res->type);
200                 return;
201         }
202
203         if (p->producer_consumer == ACPI_PRODUCER)
204                 return;
205
206         if (p->resource_type == ACPI_MEMORY_RANGE)
207                 pnpacpi_parse_allocated_memresource(dev,
208                         p->minimum, p->address_length,
209                         p->info.mem.write_protect);
210         else if (p->resource_type == ACPI_IO_RANGE)
211                 pnpacpi_parse_allocated_ioresource(dev,
212                         p->minimum, p->address_length,
213                         p->granularity == 0xfff ? ACPI_DECODE_10 :
214                                 ACPI_DECODE_16);
215 }
216
217 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
218                                               void *data)
219 {
220         struct pnp_dev *dev = data;
221         struct acpi_resource_irq *irq;
222         struct acpi_resource_dma *dma;
223         struct acpi_resource_io *io;
224         struct acpi_resource_fixed_io *fixed_io;
225         struct acpi_resource_memory24 *memory24;
226         struct acpi_resource_memory32 *memory32;
227         struct acpi_resource_fixed_memory32 *fixed_memory32;
228         struct acpi_resource_extended_irq *extended_irq;
229         int i, flags;
230
231         switch (res->type) {
232         case ACPI_RESOURCE_TYPE_IRQ:
233                 /*
234                  * Per spec, only one interrupt per descriptor is allowed in
235                  * _CRS, but some firmware violates this, so parse them all.
236                  */
237                 irq = &res->data.irq;
238                 for (i = 0; i < irq->interrupt_count; i++) {
239                         pnpacpi_parse_allocated_irqresource(dev,
240                                 irq->interrupts[i],
241                                 irq->triggering,
242                                 irq->polarity,
243                                 irq->sharable);
244                 }
245                 break;
246
247         case ACPI_RESOURCE_TYPE_DMA:
248                 dma = &res->data.dma;
249                 if (dma->channel_count > 0) {
250                         flags = dma_flags(dma->type, dma->bus_master,
251                                           dma->transfer);
252                         if (dma->channels[0] == (u8) -1)
253                                 flags |= IORESOURCE_DISABLED;
254                         pnp_add_dma_resource(dev, dma->channels[0], flags);
255                 }
256                 break;
257
258         case ACPI_RESOURCE_TYPE_IO:
259                 io = &res->data.io;
260                 pnpacpi_parse_allocated_ioresource(dev,
261                         io->minimum,
262                         io->address_length,
263                         io->io_decode);
264                 break;
265
266         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
267         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
268                 break;
269
270         case ACPI_RESOURCE_TYPE_FIXED_IO:
271                 fixed_io = &res->data.fixed_io;
272                 pnpacpi_parse_allocated_ioresource(dev,
273                         fixed_io->address,
274                         fixed_io->address_length,
275                         ACPI_DECODE_10);
276                 break;
277
278         case ACPI_RESOURCE_TYPE_VENDOR:
279                 break;
280
281         case ACPI_RESOURCE_TYPE_END_TAG:
282                 break;
283
284         case ACPI_RESOURCE_TYPE_MEMORY24:
285                 memory24 = &res->data.memory24;
286                 pnpacpi_parse_allocated_memresource(dev,
287                         memory24->minimum,
288                         memory24->address_length,
289                         memory24->write_protect);
290                 break;
291         case ACPI_RESOURCE_TYPE_MEMORY32:
292                 memory32 = &res->data.memory32;
293                 pnpacpi_parse_allocated_memresource(dev,
294                         memory32->minimum,
295                         memory32->address_length,
296                         memory32->write_protect);
297                 break;
298         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
299                 fixed_memory32 = &res->data.fixed_memory32;
300                 pnpacpi_parse_allocated_memresource(dev,
301                         fixed_memory32->address,
302                         fixed_memory32->address_length,
303                         fixed_memory32->write_protect);
304                 break;
305         case ACPI_RESOURCE_TYPE_ADDRESS16:
306         case ACPI_RESOURCE_TYPE_ADDRESS32:
307         case ACPI_RESOURCE_TYPE_ADDRESS64:
308                 pnpacpi_parse_allocated_address_space(dev, res);
309                 break;
310
311         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
312                 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
313                         return AE_OK;
314                 break;
315
316         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
317                 extended_irq = &res->data.extended_irq;
318                 if (extended_irq->producer_consumer == ACPI_PRODUCER)
319                         return AE_OK;
320
321                 for (i = 0; i < extended_irq->interrupt_count; i++) {
322                         pnpacpi_parse_allocated_irqresource(dev,
323                                 extended_irq->interrupts[i],
324                                 extended_irq->triggering,
325                                 extended_irq->polarity,
326                                 extended_irq->sharable);
327                 }
328                 break;
329
330         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
331                 break;
332
333         default:
334                 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
335                          res->type);
336                 return AE_ERROR;
337         }
338
339         return AE_OK;
340 }
341
342 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
343 {
344         acpi_handle handle = dev->data;
345         acpi_status status;
346
347         dev_dbg(&dev->dev, "parse allocated resources\n");
348
349         pnp_init_resources(dev);
350
351         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
352                                      pnpacpi_allocated_resource, dev);
353
354         if (ACPI_FAILURE(status)) {
355                 if (status != AE_NOT_FOUND)
356                         dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
357                 return -EPERM;
358         }
359         return 0;
360 }
361
362 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
363                                             struct pnp_option *option,
364                                             struct acpi_resource_dma *p)
365 {
366         int i;
367         struct pnp_dma *dma;
368
369         if (p->channel_count == 0)
370                 return;
371         dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
372         if (!dma)
373                 return;
374
375         for (i = 0; i < p->channel_count; i++)
376                 dma->map |= 1 << p->channels[i];
377
378         dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
379
380         pnp_register_dma_resource(dev, option, dma);
381 }
382
383 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
384                                             struct pnp_option *option,
385                                             struct acpi_resource_irq *p)
386 {
387         int i;
388         struct pnp_irq *irq;
389
390         if (p->interrupt_count == 0)
391                 return;
392         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
393         if (!irq)
394                 return;
395
396         for (i = 0; i < p->interrupt_count; i++)
397                 if (p->interrupts[i])
398                         __set_bit(p->interrupts[i], irq->map);
399         irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
400
401         pnp_register_irq_resource(dev, option, irq);
402 }
403
404 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
405                                                 struct pnp_option *option,
406                                         struct acpi_resource_extended_irq *p)
407 {
408         int i;
409         struct pnp_irq *irq;
410
411         if (p->interrupt_count == 0)
412                 return;
413         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
414         if (!irq)
415                 return;
416
417         for (i = 0; i < p->interrupt_count; i++)
418                 if (p->interrupts[i])
419                         __set_bit(p->interrupts[i], irq->map);
420         irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
421
422         pnp_register_irq_resource(dev, option, irq);
423 }
424
425 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
426                                              struct pnp_option *option,
427                                              struct acpi_resource_io *io)
428 {
429         struct pnp_port *port;
430
431         if (io->address_length == 0)
432                 return;
433         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
434         if (!port)
435                 return;
436         port->min = io->minimum;
437         port->max = io->maximum;
438         port->align = io->alignment;
439         port->size = io->address_length;
440         port->flags = ACPI_DECODE_16 == io->io_decode ?
441             PNP_PORT_FLAG_16BITADDR : 0;
442         pnp_register_port_resource(dev, option, port);
443 }
444
445 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
446                                                    struct pnp_option *option,
447                                         struct acpi_resource_fixed_io *io)
448 {
449         struct pnp_port *port;
450
451         if (io->address_length == 0)
452                 return;
453         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
454         if (!port)
455                 return;
456         port->min = port->max = io->address;
457         port->size = io->address_length;
458         port->align = 0;
459         port->flags = PNP_PORT_FLAG_FIXED;
460         pnp_register_port_resource(dev, option, port);
461 }
462
463 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
464                                               struct pnp_option *option,
465                                               struct acpi_resource_memory24 *p)
466 {
467         struct pnp_mem *mem;
468
469         if (p->address_length == 0)
470                 return;
471         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
472         if (!mem)
473                 return;
474         mem->min = p->minimum;
475         mem->max = p->maximum;
476         mem->align = p->alignment;
477         mem->size = p->address_length;
478
479         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
480             IORESOURCE_MEM_WRITEABLE : 0;
481
482         pnp_register_mem_resource(dev, option, mem);
483 }
484
485 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
486                                               struct pnp_option *option,
487                                               struct acpi_resource_memory32 *p)
488 {
489         struct pnp_mem *mem;
490
491         if (p->address_length == 0)
492                 return;
493         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
494         if (!mem)
495                 return;
496         mem->min = p->minimum;
497         mem->max = p->maximum;
498         mem->align = p->alignment;
499         mem->size = p->address_length;
500
501         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
502             IORESOURCE_MEM_WRITEABLE : 0;
503
504         pnp_register_mem_resource(dev, option, mem);
505 }
506
507 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
508                                                     struct pnp_option *option,
509                                         struct acpi_resource_fixed_memory32 *p)
510 {
511         struct pnp_mem *mem;
512
513         if (p->address_length == 0)
514                 return;
515         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
516         if (!mem)
517                 return;
518         mem->min = mem->max = p->address;
519         mem->size = p->address_length;
520         mem->align = 0;
521
522         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
523             IORESOURCE_MEM_WRITEABLE : 0;
524
525         pnp_register_mem_resource(dev, option, mem);
526 }
527
528 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
529                                                 struct pnp_option *option,
530                                                 struct acpi_resource *r)
531 {
532         struct acpi_resource_address64 addr, *p = &addr;
533         acpi_status status;
534         struct pnp_mem *mem;
535         struct pnp_port *port;
536
537         status = acpi_resource_to_address64(r, p);
538         if (!ACPI_SUCCESS(status)) {
539                 pnp_warn("PnPACPI: failed to convert resource type %d",
540                          r->type);
541                 return;
542         }
543
544         if (p->address_length == 0)
545                 return;
546
547         if (p->resource_type == ACPI_MEMORY_RANGE) {
548                 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
549                 if (!mem)
550                         return;
551                 mem->min = mem->max = p->minimum;
552                 mem->size = p->address_length;
553                 mem->align = 0;
554                 mem->flags = (p->info.mem.write_protect ==
555                               ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
556                     : 0;
557                 pnp_register_mem_resource(dev, option, mem);
558         } else if (p->resource_type == ACPI_IO_RANGE) {
559                 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
560                 if (!port)
561                         return;
562                 port->min = port->max = p->minimum;
563                 port->size = p->address_length;
564                 port->align = 0;
565                 port->flags = PNP_PORT_FLAG_FIXED;
566                 pnp_register_port_resource(dev, option, port);
567         }
568 }
569
570 struct acpipnp_parse_option_s {
571         struct pnp_option *option;
572         struct pnp_option *option_independent;
573         struct pnp_dev *dev;
574 };
575
576 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
577                                                   void *data)
578 {
579         int priority = 0;
580         struct acpipnp_parse_option_s *parse_data = data;
581         struct pnp_dev *dev = parse_data->dev;
582         struct pnp_option *option = parse_data->option;
583
584         switch (res->type) {
585         case ACPI_RESOURCE_TYPE_IRQ:
586                 pnpacpi_parse_irq_option(dev, option, &res->data.irq);
587                 break;
588
589         case ACPI_RESOURCE_TYPE_DMA:
590                 pnpacpi_parse_dma_option(dev, option, &res->data.dma);
591                 break;
592
593         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
594                 switch (res->data.start_dpf.compatibility_priority) {
595                 case ACPI_GOOD_CONFIGURATION:
596                         priority = PNP_RES_PRIORITY_PREFERRED;
597                         break;
598
599                 case ACPI_ACCEPTABLE_CONFIGURATION:
600                         priority = PNP_RES_PRIORITY_ACCEPTABLE;
601                         break;
602
603                 case ACPI_SUB_OPTIMAL_CONFIGURATION:
604                         priority = PNP_RES_PRIORITY_FUNCTIONAL;
605                         break;
606                 default:
607                         priority = PNP_RES_PRIORITY_INVALID;
608                         break;
609                 }
610                 /* TBD: Consider performance/robustness bits */
611                 option = pnp_register_dependent_option(dev, priority);
612                 if (!option)
613                         return AE_ERROR;
614                 parse_data->option = option;
615                 break;
616
617         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
618                 /*only one EndDependentFn is allowed */
619                 if (!parse_data->option_independent) {
620                         dev_warn(&dev->dev, "more than one EndDependentFn "
621                                  "in _PRS\n");
622                         return AE_ERROR;
623                 }
624                 parse_data->option = parse_data->option_independent;
625                 parse_data->option_independent = NULL;
626                 dev_dbg(&dev->dev, "end dependent options\n");
627                 break;
628
629         case ACPI_RESOURCE_TYPE_IO:
630                 pnpacpi_parse_port_option(dev, option, &res->data.io);
631                 break;
632
633         case ACPI_RESOURCE_TYPE_FIXED_IO:
634                 pnpacpi_parse_fixed_port_option(dev, option,
635                                                 &res->data.fixed_io);
636                 break;
637
638         case ACPI_RESOURCE_TYPE_VENDOR:
639         case ACPI_RESOURCE_TYPE_END_TAG:
640                 break;
641
642         case ACPI_RESOURCE_TYPE_MEMORY24:
643                 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24);
644                 break;
645
646         case ACPI_RESOURCE_TYPE_MEMORY32:
647                 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32);
648                 break;
649
650         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
651                 pnpacpi_parse_fixed_mem32_option(dev, option,
652                                                  &res->data.fixed_memory32);
653                 break;
654
655         case ACPI_RESOURCE_TYPE_ADDRESS16:
656         case ACPI_RESOURCE_TYPE_ADDRESS32:
657         case ACPI_RESOURCE_TYPE_ADDRESS64:
658                 pnpacpi_parse_address_option(dev, option, res);
659                 break;
660
661         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
662                 break;
663
664         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
665                 pnpacpi_parse_ext_irq_option(dev, option,
666                                              &res->data.extended_irq);
667                 break;
668
669         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
670                 break;
671
672         default:
673                 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
674                          res->type);
675                 return AE_ERROR;
676         }
677
678         return AE_OK;
679 }
680
681 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
682 {
683         acpi_handle handle = dev->data;
684         acpi_status status;
685         struct acpipnp_parse_option_s parse_data;
686
687         dev_dbg(&dev->dev, "parse resource options\n");
688
689         parse_data.option = pnp_register_independent_option(dev);
690         if (!parse_data.option)
691                 return -ENOMEM;
692
693         parse_data.option_independent = parse_data.option;
694         parse_data.dev = dev;
695         status = acpi_walk_resources(handle, METHOD_NAME__PRS,
696                                      pnpacpi_option_resource, &parse_data);
697
698         if (ACPI_FAILURE(status)) {
699                 if (status != AE_NOT_FOUND)
700                         dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
701                 return -EPERM;
702         }
703         return 0;
704 }
705
706 static int pnpacpi_supported_resource(struct acpi_resource *res)
707 {
708         switch (res->type) {
709         case ACPI_RESOURCE_TYPE_IRQ:
710         case ACPI_RESOURCE_TYPE_DMA:
711         case ACPI_RESOURCE_TYPE_IO:
712         case ACPI_RESOURCE_TYPE_FIXED_IO:
713         case ACPI_RESOURCE_TYPE_MEMORY24:
714         case ACPI_RESOURCE_TYPE_MEMORY32:
715         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
716         case ACPI_RESOURCE_TYPE_ADDRESS16:
717         case ACPI_RESOURCE_TYPE_ADDRESS32:
718         case ACPI_RESOURCE_TYPE_ADDRESS64:
719         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
720                 return 1;
721         }
722         return 0;
723 }
724
725 /*
726  * Set resource
727  */
728 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
729                                            void *data)
730 {
731         int *res_cnt = data;
732
733         if (pnpacpi_supported_resource(res))
734                 (*res_cnt)++;
735         return AE_OK;
736 }
737
738 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
739 {
740         struct acpi_resource **resource = data;
741
742         if (pnpacpi_supported_resource(res)) {
743                 (*resource)->type = res->type;
744                 (*resource)->length = sizeof(struct acpi_resource);
745                 (*resource)++;
746         }
747
748         return AE_OK;
749 }
750
751 int pnpacpi_build_resource_template(struct pnp_dev *dev,
752                                     struct acpi_buffer *buffer)
753 {
754         acpi_handle handle = dev->data;
755         struct acpi_resource *resource;
756         int res_cnt = 0;
757         acpi_status status;
758
759         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
760                                      pnpacpi_count_resources, &res_cnt);
761         if (ACPI_FAILURE(status)) {
762                 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
763                 return -EINVAL;
764         }
765         if (!res_cnt)
766                 return -EINVAL;
767         buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
768         buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
769         if (!buffer->pointer)
770                 return -ENOMEM;
771
772         resource = (struct acpi_resource *)buffer->pointer;
773         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
774                                      pnpacpi_type_resources, &resource);
775         if (ACPI_FAILURE(status)) {
776                 kfree(buffer->pointer);
777                 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
778                 return -EINVAL;
779         }
780         /* resource will pointer the end resource now */
781         resource->type = ACPI_RESOURCE_TYPE_END_TAG;
782
783         return 0;
784 }
785
786 static void pnpacpi_encode_irq(struct pnp_dev *dev,
787                                struct acpi_resource *resource,
788                                struct resource *p)
789 {
790         struct acpi_resource_irq *irq = &resource->data.irq;
791         int triggering, polarity;
792
793         decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
794         irq->triggering = triggering;
795         irq->polarity = polarity;
796         if (triggering == ACPI_EDGE_SENSITIVE)
797                 irq->sharable = ACPI_EXCLUSIVE;
798         else
799                 irq->sharable = ACPI_SHARED;
800         irq->interrupt_count = 1;
801         irq->interrupts[0] = p->start;
802
803         dev_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
804                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
805                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
806                 irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
807 }
808
809 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
810                                    struct acpi_resource *resource,
811                                    struct resource *p)
812 {
813         struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
814         int triggering, polarity;
815
816         decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
817         extended_irq->producer_consumer = ACPI_CONSUMER;
818         extended_irq->triggering = triggering;
819         extended_irq->polarity = polarity;
820         if (triggering == ACPI_EDGE_SENSITIVE)
821                 extended_irq->sharable = ACPI_EXCLUSIVE;
822         else
823                 extended_irq->sharable = ACPI_SHARED;
824         extended_irq->interrupt_count = 1;
825         extended_irq->interrupts[0] = p->start;
826
827         dev_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
828                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
829                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
830                 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
831 }
832
833 static void pnpacpi_encode_dma(struct pnp_dev *dev,
834                                struct acpi_resource *resource,
835                                struct resource *p)
836 {
837         struct acpi_resource_dma *dma = &resource->data.dma;
838
839         /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
840         switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
841         case IORESOURCE_DMA_TYPEA:
842                 dma->type = ACPI_TYPE_A;
843                 break;
844         case IORESOURCE_DMA_TYPEB:
845                 dma->type = ACPI_TYPE_B;
846                 break;
847         case IORESOURCE_DMA_TYPEF:
848                 dma->type = ACPI_TYPE_F;
849                 break;
850         default:
851                 dma->type = ACPI_COMPATIBILITY;
852         }
853
854         switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
855         case IORESOURCE_DMA_8BIT:
856                 dma->transfer = ACPI_TRANSFER_8;
857                 break;
858         case IORESOURCE_DMA_8AND16BIT:
859                 dma->transfer = ACPI_TRANSFER_8_16;
860                 break;
861         default:
862                 dma->transfer = ACPI_TRANSFER_16;
863         }
864
865         dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
866         dma->channel_count = 1;
867         dma->channels[0] = p->start;
868
869         dev_dbg(&dev->dev, "  encode dma %d "
870                 "type %#x transfer %#x master %d\n",
871                 (int) p->start, dma->type, dma->transfer, dma->bus_master);
872 }
873
874 static void pnpacpi_encode_io(struct pnp_dev *dev,
875                               struct acpi_resource *resource,
876                               struct resource *p)
877 {
878         struct acpi_resource_io *io = &resource->data.io;
879
880         /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
881         io->io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
882             ACPI_DECODE_16 : ACPI_DECODE_10;
883         io->minimum = p->start;
884         io->maximum = p->end;
885         io->alignment = 0;      /* Correct? */
886         io->address_length = p->end - p->start + 1;
887
888         dev_dbg(&dev->dev, "  encode io %#llx-%#llx decode %#x\n",
889                 (unsigned long long) p->start, (unsigned long long) p->end,
890                 io->io_decode);
891 }
892
893 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
894                                     struct acpi_resource *resource,
895                                     struct resource *p)
896 {
897         struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
898
899         fixed_io->address = p->start;
900         fixed_io->address_length = p->end - p->start + 1;
901
902         dev_dbg(&dev->dev, "  encode fixed_io %#llx-%#llx\n",
903                 (unsigned long long) p->start, (unsigned long long) p->end);
904 }
905
906 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
907                                  struct acpi_resource *resource,
908                                  struct resource *p)
909 {
910         struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
911
912         /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
913         memory24->write_protect =
914             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
915             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
916         memory24->minimum = p->start;
917         memory24->maximum = p->end;
918         memory24->alignment = 0;
919         memory24->address_length = p->end - p->start + 1;
920
921         dev_dbg(&dev->dev, "  encode mem24 %#llx-%#llx write_protect %#x\n",
922                 (unsigned long long) p->start, (unsigned long long) p->end,
923                 memory24->write_protect);
924 }
925
926 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
927                                  struct acpi_resource *resource,
928                                  struct resource *p)
929 {
930         struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
931
932         memory32->write_protect =
933             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
934             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
935         memory32->minimum = p->start;
936         memory32->maximum = p->end;
937         memory32->alignment = 0;
938         memory32->address_length = p->end - p->start + 1;
939
940         dev_dbg(&dev->dev, "  encode mem32 %#llx-%#llx write_protect %#x\n",
941                 (unsigned long long) p->start, (unsigned long long) p->end,
942                 memory32->write_protect);
943 }
944
945 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
946                                        struct acpi_resource *resource,
947                                        struct resource *p)
948 {
949         struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
950
951         fixed_memory32->write_protect =
952             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
953             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
954         fixed_memory32->address = p->start;
955         fixed_memory32->address_length = p->end - p->start + 1;
956
957         dev_dbg(&dev->dev, "  encode fixed_mem32 %#llx-%#llx "
958                 "write_protect %#x\n",
959                 (unsigned long long) p->start, (unsigned long long) p->end,
960                 fixed_memory32->write_protect);
961 }
962
963 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
964 {
965         int i = 0;
966         /* pnpacpi_build_resource_template allocates extra mem */
967         int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
968         struct acpi_resource *resource = buffer->pointer;
969         int port = 0, irq = 0, dma = 0, mem = 0;
970
971         dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
972         while (i < res_cnt) {
973                 switch (resource->type) {
974                 case ACPI_RESOURCE_TYPE_IRQ:
975                         pnpacpi_encode_irq(dev, resource,
976                                pnp_get_resource(dev, IORESOURCE_IRQ, irq));
977                         irq++;
978                         break;
979
980                 case ACPI_RESOURCE_TYPE_DMA:
981                         pnpacpi_encode_dma(dev, resource,
982                                 pnp_get_resource(dev, IORESOURCE_DMA, dma));
983                         dma++;
984                         break;
985                 case ACPI_RESOURCE_TYPE_IO:
986                         pnpacpi_encode_io(dev, resource,
987                                 pnp_get_resource(dev, IORESOURCE_IO, port));
988                         port++;
989                         break;
990                 case ACPI_RESOURCE_TYPE_FIXED_IO:
991                         pnpacpi_encode_fixed_io(dev, resource,
992                                 pnp_get_resource(dev, IORESOURCE_IO, port));
993                         port++;
994                         break;
995                 case ACPI_RESOURCE_TYPE_MEMORY24:
996                         pnpacpi_encode_mem24(dev, resource,
997                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
998                         mem++;
999                         break;
1000                 case ACPI_RESOURCE_TYPE_MEMORY32:
1001                         pnpacpi_encode_mem32(dev, resource,
1002                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1003                         mem++;
1004                         break;
1005                 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1006                         pnpacpi_encode_fixed_mem32(dev, resource,
1007                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1008                         mem++;
1009                         break;
1010                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1011                         pnpacpi_encode_ext_irq(dev, resource,
1012                                 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1013                         irq++;
1014                         break;
1015                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1016                 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1017                 case ACPI_RESOURCE_TYPE_VENDOR:
1018                 case ACPI_RESOURCE_TYPE_END_TAG:
1019                 case ACPI_RESOURCE_TYPE_ADDRESS16:
1020                 case ACPI_RESOURCE_TYPE_ADDRESS32:
1021                 case ACPI_RESOURCE_TYPE_ADDRESS64:
1022                 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1023                 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1024                 default:        /* other type */
1025                         dev_warn(&dev->dev, "can't encode unknown resource "
1026                                  "type %d\n", resource->type);
1027                         return -EINVAL;
1028                 }
1029                 resource++;
1030                 i++;
1031         }
1032         return 0;
1033 }