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