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