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