PNP: add pnp_init_resources(struct pnp_dev *) interface
[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         pnp_init_resources(dev);
414
415         return acpi_walk_resources(handle, METHOD_NAME__CRS,
416                                    pnpacpi_allocated_resource, dev);
417 }
418
419 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
420                                             struct pnp_option *option,
421                                             struct acpi_resource_dma *p)
422 {
423         int i;
424         struct pnp_dma *dma;
425
426         if (p->channel_count == 0)
427                 return;
428         dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
429         if (!dma)
430                 return;
431
432         for (i = 0; i < p->channel_count; i++)
433                 dma->map |= 1 << p->channels[i];
434
435         dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
436
437         pnp_register_dma_resource(dev, option, dma);
438 }
439
440 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
441                                             struct pnp_option *option,
442                                             struct acpi_resource_irq *p)
443 {
444         int i;
445         struct pnp_irq *irq;
446
447         if (p->interrupt_count == 0)
448                 return;
449         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
450         if (!irq)
451                 return;
452
453         for (i = 0; i < p->interrupt_count; i++)
454                 if (p->interrupts[i])
455                         __set_bit(p->interrupts[i], irq->map);
456         irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
457
458         pnp_register_irq_resource(dev, option, irq);
459 }
460
461 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
462                                                 struct pnp_option *option,
463                                         struct acpi_resource_extended_irq *p)
464 {
465         int i;
466         struct pnp_irq *irq;
467
468         if (p->interrupt_count == 0)
469                 return;
470         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
471         if (!irq)
472                 return;
473
474         for (i = 0; i < p->interrupt_count; i++)
475                 if (p->interrupts[i])
476                         __set_bit(p->interrupts[i], irq->map);
477         irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
478
479         pnp_register_irq_resource(dev, option, irq);
480 }
481
482 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
483                                              struct pnp_option *option,
484                                              struct acpi_resource_io *io)
485 {
486         struct pnp_port *port;
487
488         if (io->address_length == 0)
489                 return;
490         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
491         if (!port)
492                 return;
493         port->min = io->minimum;
494         port->max = io->maximum;
495         port->align = io->alignment;
496         port->size = io->address_length;
497         port->flags = ACPI_DECODE_16 == io->io_decode ?
498             PNP_PORT_FLAG_16BITADDR : 0;
499         pnp_register_port_resource(dev, option, port);
500 }
501
502 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
503                                                    struct pnp_option *option,
504                                         struct acpi_resource_fixed_io *io)
505 {
506         struct pnp_port *port;
507
508         if (io->address_length == 0)
509                 return;
510         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
511         if (!port)
512                 return;
513         port->min = port->max = io->address;
514         port->size = io->address_length;
515         port->align = 0;
516         port->flags = PNP_PORT_FLAG_FIXED;
517         pnp_register_port_resource(dev, option, port);
518 }
519
520 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
521                                               struct pnp_option *option,
522                                               struct acpi_resource_memory24 *p)
523 {
524         struct pnp_mem *mem;
525
526         if (p->address_length == 0)
527                 return;
528         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
529         if (!mem)
530                 return;
531         mem->min = p->minimum;
532         mem->max = p->maximum;
533         mem->align = p->alignment;
534         mem->size = p->address_length;
535
536         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
537             IORESOURCE_MEM_WRITEABLE : 0;
538
539         pnp_register_mem_resource(dev, option, mem);
540 }
541
542 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
543                                               struct pnp_option *option,
544                                               struct acpi_resource_memory32 *p)
545 {
546         struct pnp_mem *mem;
547
548         if (p->address_length == 0)
549                 return;
550         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
551         if (!mem)
552                 return;
553         mem->min = p->minimum;
554         mem->max = p->maximum;
555         mem->align = p->alignment;
556         mem->size = p->address_length;
557
558         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
559             IORESOURCE_MEM_WRITEABLE : 0;
560
561         pnp_register_mem_resource(dev, option, mem);
562 }
563
564 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
565                                                     struct pnp_option *option,
566                                         struct acpi_resource_fixed_memory32 *p)
567 {
568         struct pnp_mem *mem;
569
570         if (p->address_length == 0)
571                 return;
572         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
573         if (!mem)
574                 return;
575         mem->min = mem->max = p->address;
576         mem->size = p->address_length;
577         mem->align = 0;
578
579         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
580             IORESOURCE_MEM_WRITEABLE : 0;
581
582         pnp_register_mem_resource(dev, option, mem);
583 }
584
585 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
586                                                 struct pnp_option *option,
587                                                 struct acpi_resource *r)
588 {
589         struct acpi_resource_address64 addr, *p = &addr;
590         acpi_status status;
591         struct pnp_mem *mem;
592         struct pnp_port *port;
593
594         status = acpi_resource_to_address64(r, p);
595         if (!ACPI_SUCCESS(status)) {
596                 pnp_warn("PnPACPI: failed to convert resource type %d",
597                          r->type);
598                 return;
599         }
600
601         if (p->address_length == 0)
602                 return;
603
604         if (p->resource_type == ACPI_MEMORY_RANGE) {
605                 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
606                 if (!mem)
607                         return;
608                 mem->min = mem->max = p->minimum;
609                 mem->size = p->address_length;
610                 mem->align = 0;
611                 mem->flags = (p->info.mem.write_protect ==
612                               ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
613                     : 0;
614                 pnp_register_mem_resource(dev, option, mem);
615         } else if (p->resource_type == ACPI_IO_RANGE) {
616                 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
617                 if (!port)
618                         return;
619                 port->min = port->max = p->minimum;
620                 port->size = p->address_length;
621                 port->align = 0;
622                 port->flags = PNP_PORT_FLAG_FIXED;
623                 pnp_register_port_resource(dev, option, port);
624         }
625 }
626
627 struct acpipnp_parse_option_s {
628         struct pnp_option *option;
629         struct pnp_option *option_independent;
630         struct pnp_dev *dev;
631 };
632
633 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
634                                                   void *data)
635 {
636         int priority = 0;
637         struct acpipnp_parse_option_s *parse_data = data;
638         struct pnp_dev *dev = parse_data->dev;
639         struct pnp_option *option = parse_data->option;
640
641         switch (res->type) {
642         case ACPI_RESOURCE_TYPE_IRQ:
643                 pnpacpi_parse_irq_option(dev, option, &res->data.irq);
644                 break;
645
646         case ACPI_RESOURCE_TYPE_DMA:
647                 pnpacpi_parse_dma_option(dev, option, &res->data.dma);
648                 break;
649
650         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
651                 switch (res->data.start_dpf.compatibility_priority) {
652                 case ACPI_GOOD_CONFIGURATION:
653                         priority = PNP_RES_PRIORITY_PREFERRED;
654                         break;
655
656                 case ACPI_ACCEPTABLE_CONFIGURATION:
657                         priority = PNP_RES_PRIORITY_ACCEPTABLE;
658                         break;
659
660                 case ACPI_SUB_OPTIMAL_CONFIGURATION:
661                         priority = PNP_RES_PRIORITY_FUNCTIONAL;
662                         break;
663                 default:
664                         priority = PNP_RES_PRIORITY_INVALID;
665                         break;
666                 }
667                 /* TBD: Consider performance/robustness bits */
668                 option = pnp_register_dependent_option(dev, priority);
669                 if (!option)
670                         return AE_ERROR;
671                 parse_data->option = option;
672                 break;
673
674         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
675                 /*only one EndDependentFn is allowed */
676                 if (!parse_data->option_independent) {
677                         pnp_warn("PnPACPI: more than one EndDependentFn");
678                         return AE_ERROR;
679                 }
680                 parse_data->option = parse_data->option_independent;
681                 parse_data->option_independent = NULL;
682                 dev_dbg(&dev->dev, "end dependent options\n");
683                 break;
684
685         case ACPI_RESOURCE_TYPE_IO:
686                 pnpacpi_parse_port_option(dev, option, &res->data.io);
687                 break;
688
689         case ACPI_RESOURCE_TYPE_FIXED_IO:
690                 pnpacpi_parse_fixed_port_option(dev, option,
691                                                 &res->data.fixed_io);
692                 break;
693
694         case ACPI_RESOURCE_TYPE_VENDOR:
695         case ACPI_RESOURCE_TYPE_END_TAG:
696                 break;
697
698         case ACPI_RESOURCE_TYPE_MEMORY24:
699                 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24);
700                 break;
701
702         case ACPI_RESOURCE_TYPE_MEMORY32:
703                 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32);
704                 break;
705
706         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
707                 pnpacpi_parse_fixed_mem32_option(dev, option,
708                                                  &res->data.fixed_memory32);
709                 break;
710
711         case ACPI_RESOURCE_TYPE_ADDRESS16:
712         case ACPI_RESOURCE_TYPE_ADDRESS32:
713         case ACPI_RESOURCE_TYPE_ADDRESS64:
714                 pnpacpi_parse_address_option(dev, option, res);
715                 break;
716
717         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
718                 break;
719
720         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
721                 pnpacpi_parse_ext_irq_option(dev, option,
722                                              &res->data.extended_irq);
723                 break;
724
725         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
726                 break;
727
728         default:
729                 pnp_warn("PnPACPI: unknown resource type %d", res->type);
730                 return AE_ERROR;
731         }
732
733         return AE_OK;
734 }
735
736 acpi_status __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
737 {
738         acpi_handle handle = dev->data;
739         acpi_status status;
740         struct acpipnp_parse_option_s parse_data;
741
742         dev_dbg(&dev->dev, "parse resource options\n");
743
744         parse_data.option = pnp_register_independent_option(dev);
745         if (!parse_data.option)
746                 return AE_ERROR;
747         parse_data.option_independent = parse_data.option;
748         parse_data.dev = dev;
749         status = acpi_walk_resources(handle, METHOD_NAME__PRS,
750                                      pnpacpi_option_resource, &parse_data);
751
752         return status;
753 }
754
755 static int pnpacpi_supported_resource(struct acpi_resource *res)
756 {
757         switch (res->type) {
758         case ACPI_RESOURCE_TYPE_IRQ:
759         case ACPI_RESOURCE_TYPE_DMA:
760         case ACPI_RESOURCE_TYPE_IO:
761         case ACPI_RESOURCE_TYPE_FIXED_IO:
762         case ACPI_RESOURCE_TYPE_MEMORY24:
763         case ACPI_RESOURCE_TYPE_MEMORY32:
764         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
765         case ACPI_RESOURCE_TYPE_ADDRESS16:
766         case ACPI_RESOURCE_TYPE_ADDRESS32:
767         case ACPI_RESOURCE_TYPE_ADDRESS64:
768         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
769                 return 1;
770         }
771         return 0;
772 }
773
774 /*
775  * Set resource
776  */
777 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
778                                            void *data)
779 {
780         int *res_cnt = data;
781
782         if (pnpacpi_supported_resource(res))
783                 (*res_cnt)++;
784         return AE_OK;
785 }
786
787 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
788 {
789         struct acpi_resource **resource = data;
790
791         if (pnpacpi_supported_resource(res)) {
792                 (*resource)->type = res->type;
793                 (*resource)->length = sizeof(struct acpi_resource);
794                 (*resource)++;
795         }
796
797         return AE_OK;
798 }
799
800 int pnpacpi_build_resource_template(struct pnp_dev *dev,
801                                     struct acpi_buffer *buffer)
802 {
803         acpi_handle handle = dev->data;
804         struct acpi_resource *resource;
805         int res_cnt = 0;
806         acpi_status status;
807
808         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
809                                      pnpacpi_count_resources, &res_cnt);
810         if (ACPI_FAILURE(status)) {
811                 pnp_err("Evaluate _CRS failed");
812                 return -EINVAL;
813         }
814         if (!res_cnt)
815                 return -EINVAL;
816         buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
817         buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
818         if (!buffer->pointer)
819                 return -ENOMEM;
820
821         resource = (struct acpi_resource *)buffer->pointer;
822         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
823                                      pnpacpi_type_resources, &resource);
824         if (ACPI_FAILURE(status)) {
825                 kfree(buffer->pointer);
826                 pnp_err("Evaluate _CRS failed");
827                 return -EINVAL;
828         }
829         /* resource will pointer the end resource now */
830         resource->type = ACPI_RESOURCE_TYPE_END_TAG;
831
832         return 0;
833 }
834
835 static void pnpacpi_encode_irq(struct pnp_dev *dev,
836                                struct acpi_resource *resource,
837                                struct resource *p)
838 {
839         struct acpi_resource_irq *irq = &resource->data.irq;
840         int triggering, polarity;
841
842         decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
843         irq->triggering = triggering;
844         irq->polarity = polarity;
845         if (triggering == ACPI_EDGE_SENSITIVE)
846                 irq->sharable = ACPI_EXCLUSIVE;
847         else
848                 irq->sharable = ACPI_SHARED;
849         irq->interrupt_count = 1;
850         irq->interrupts[0] = p->start;
851
852         dev_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
853                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
854                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
855                 irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
856 }
857
858 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
859                                    struct acpi_resource *resource,
860                                    struct resource *p)
861 {
862         struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
863         int triggering, polarity;
864
865         decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
866         extended_irq->producer_consumer = ACPI_CONSUMER;
867         extended_irq->triggering = triggering;
868         extended_irq->polarity = polarity;
869         if (triggering == ACPI_EDGE_SENSITIVE)
870                 extended_irq->sharable = ACPI_EXCLUSIVE;
871         else
872                 extended_irq->sharable = ACPI_SHARED;
873         extended_irq->interrupt_count = 1;
874         extended_irq->interrupts[0] = p->start;
875
876         dev_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
877                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
878                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
879                 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
880 }
881
882 static void pnpacpi_encode_dma(struct pnp_dev *dev,
883                                struct acpi_resource *resource,
884                                struct resource *p)
885 {
886         struct acpi_resource_dma *dma = &resource->data.dma;
887
888         /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
889         switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
890         case IORESOURCE_DMA_TYPEA:
891                 dma->type = ACPI_TYPE_A;
892                 break;
893         case IORESOURCE_DMA_TYPEB:
894                 dma->type = ACPI_TYPE_B;
895                 break;
896         case IORESOURCE_DMA_TYPEF:
897                 dma->type = ACPI_TYPE_F;
898                 break;
899         default:
900                 dma->type = ACPI_COMPATIBILITY;
901         }
902
903         switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
904         case IORESOURCE_DMA_8BIT:
905                 dma->transfer = ACPI_TRANSFER_8;
906                 break;
907         case IORESOURCE_DMA_8AND16BIT:
908                 dma->transfer = ACPI_TRANSFER_8_16;
909                 break;
910         default:
911                 dma->transfer = ACPI_TRANSFER_16;
912         }
913
914         dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
915         dma->channel_count = 1;
916         dma->channels[0] = p->start;
917
918         dev_dbg(&dev->dev, "  encode dma %d "
919                 "type %#x transfer %#x master %d\n",
920                 (int) p->start, dma->type, dma->transfer, dma->bus_master);
921 }
922
923 static void pnpacpi_encode_io(struct pnp_dev *dev,
924                               struct acpi_resource *resource,
925                               struct resource *p)
926 {
927         struct acpi_resource_io *io = &resource->data.io;
928
929         /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
930         io->io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
931             ACPI_DECODE_16 : ACPI_DECODE_10;
932         io->minimum = p->start;
933         io->maximum = p->end;
934         io->alignment = 0;      /* Correct? */
935         io->address_length = p->end - p->start + 1;
936
937         dev_dbg(&dev->dev, "  encode io %#llx-%#llx decode %#x\n",
938                 (unsigned long long) p->start, (unsigned long long) p->end,
939                 io->io_decode);
940 }
941
942 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
943                                     struct acpi_resource *resource,
944                                     struct resource *p)
945 {
946         struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
947
948         fixed_io->address = p->start;
949         fixed_io->address_length = p->end - p->start + 1;
950
951         dev_dbg(&dev->dev, "  encode fixed_io %#llx-%#llx\n",
952                 (unsigned long long) p->start, (unsigned long long) p->end);
953 }
954
955 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
956                                  struct acpi_resource *resource,
957                                  struct resource *p)
958 {
959         struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
960
961         /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
962         memory24->write_protect =
963             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
964             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
965         memory24->minimum = p->start;
966         memory24->maximum = p->end;
967         memory24->alignment = 0;
968         memory24->address_length = p->end - p->start + 1;
969
970         dev_dbg(&dev->dev, "  encode mem24 %#llx-%#llx write_protect %#x\n",
971                 (unsigned long long) p->start, (unsigned long long) p->end,
972                 memory24->write_protect);
973 }
974
975 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
976                                  struct acpi_resource *resource,
977                                  struct resource *p)
978 {
979         struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
980
981         memory32->write_protect =
982             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
983             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
984         memory32->minimum = p->start;
985         memory32->maximum = p->end;
986         memory32->alignment = 0;
987         memory32->address_length = p->end - p->start + 1;
988
989         dev_dbg(&dev->dev, "  encode mem32 %#llx-%#llx write_protect %#x\n",
990                 (unsigned long long) p->start, (unsigned long long) p->end,
991                 memory32->write_protect);
992 }
993
994 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
995                                        struct acpi_resource *resource,
996                                        struct resource *p)
997 {
998         struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
999
1000         fixed_memory32->write_protect =
1001             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
1002             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1003         fixed_memory32->address = p->start;
1004         fixed_memory32->address_length = p->end - p->start + 1;
1005
1006         dev_dbg(&dev->dev, "  encode fixed_mem32 %#llx-%#llx "
1007                 "write_protect %#x\n",
1008                 (unsigned long long) p->start, (unsigned long long) p->end,
1009                 fixed_memory32->write_protect);
1010 }
1011
1012 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1013 {
1014         struct pnp_resource_table *res_table = &dev->res;
1015         int i = 0;
1016         /* pnpacpi_build_resource_template allocates extra mem */
1017         int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1018         struct acpi_resource *resource = buffer->pointer;
1019         int port = 0, irq = 0, dma = 0, mem = 0;
1020
1021         dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1022         while (i < res_cnt) {
1023                 switch (resource->type) {
1024                 case ACPI_RESOURCE_TYPE_IRQ:
1025                         pnpacpi_encode_irq(dev, resource,
1026                                            &res_table->irq_resource[irq]);
1027                         irq++;
1028                         break;
1029
1030                 case ACPI_RESOURCE_TYPE_DMA:
1031                         pnpacpi_encode_dma(dev, resource,
1032                                            &res_table->dma_resource[dma]);
1033                         dma++;
1034                         break;
1035                 case ACPI_RESOURCE_TYPE_IO:
1036                         pnpacpi_encode_io(dev, resource,
1037                                           &res_table->port_resource[port]);
1038                         port++;
1039                         break;
1040                 case ACPI_RESOURCE_TYPE_FIXED_IO:
1041                         pnpacpi_encode_fixed_io(dev, resource,
1042                                                 &res_table->
1043                                                 port_resource[port]);
1044                         port++;
1045                         break;
1046                 case ACPI_RESOURCE_TYPE_MEMORY24:
1047                         pnpacpi_encode_mem24(dev, resource,
1048                                              &res_table->mem_resource[mem]);
1049                         mem++;
1050                         break;
1051                 case ACPI_RESOURCE_TYPE_MEMORY32:
1052                         pnpacpi_encode_mem32(dev, resource,
1053                                              &res_table->mem_resource[mem]);
1054                         mem++;
1055                         break;
1056                 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1057                         pnpacpi_encode_fixed_mem32(dev, resource,
1058                                                    &res_table->
1059                                                    mem_resource[mem]);
1060                         mem++;
1061                         break;
1062                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1063                         pnpacpi_encode_ext_irq(dev, resource,
1064                                                &res_table->irq_resource[irq]);
1065                         irq++;
1066                         break;
1067                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1068                 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1069                 case ACPI_RESOURCE_TYPE_VENDOR:
1070                 case ACPI_RESOURCE_TYPE_END_TAG:
1071                 case ACPI_RESOURCE_TYPE_ADDRESS16:
1072                 case ACPI_RESOURCE_TYPE_ADDRESS32:
1073                 case ACPI_RESOURCE_TYPE_ADDRESS64:
1074                 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1075                 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1076                 default:        /* other type */
1077                         pnp_warn("unknown resource type %d", resource->type);
1078                         return -EINVAL;
1079                 }
1080                 resource++;
1081                 i++;
1082         }
1083         return 0;
1084 }