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