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