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