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