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