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