Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / drivers / pnp / pnpacpi / rsparser.c
1 /*
2  * pnpacpi -- PnP ACPI driver
3  *
4  * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5  * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
6  *
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 "pnpacpi.h"
25
26 #ifdef CONFIG_IA64
27 #define valid_IRQ(i) (1)
28 #else
29 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
30 #endif
31
32 /*
33  * Allocated Resources
34  */
35 static int irq_flags(int triggering, int polarity)
36 {
37         if (triggering == ACPI_LEVEL_SENSITIVE) {
38                 if (polarity == ACPI_ACTIVE_LOW)
39                         return IORESOURCE_IRQ_LOWLEVEL;
40                 else
41                         return IORESOURCE_IRQ_HIGHLEVEL;
42         } else {
43                 if (polarity == ACPI_ACTIVE_LOW)
44                         return IORESOURCE_IRQ_LOWEDGE;
45                 else
46                         return IORESOURCE_IRQ_HIGHEDGE;
47         }
48 }
49
50 static void decode_irq_flags(int flag, int *triggering, int *polarity)
51 {
52         switch (flag) {
53         case IORESOURCE_IRQ_LOWLEVEL:
54                 *triggering = ACPI_LEVEL_SENSITIVE;
55                 *polarity = ACPI_ACTIVE_LOW;
56                 break;
57         case IORESOURCE_IRQ_HIGHLEVEL:
58                 *triggering = ACPI_LEVEL_SENSITIVE;
59                 *polarity = ACPI_ACTIVE_HIGH;
60                 break;
61         case IORESOURCE_IRQ_LOWEDGE:
62                 *triggering = ACPI_EDGE_SENSITIVE;
63                 *polarity = ACPI_ACTIVE_LOW;
64                 break;
65         case IORESOURCE_IRQ_HIGHEDGE:
66                 *triggering = ACPI_EDGE_SENSITIVE;
67                 *polarity = ACPI_ACTIVE_HIGH;
68                 break;
69         }
70 }
71
72 static void pnpacpi_parse_allocated_irqresource(struct pnp_resource_table *res,
73                                                 u32 gsi, int triggering,
74                                                 int polarity, int shareable)
75 {
76         int i = 0;
77         int irq;
78         int p, t;
79         static unsigned char warned;
80
81         if (!valid_IRQ(gsi))
82                 return;
83
84         while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
85                i < PNP_MAX_IRQ)
86                 i++;
87         if (i >= PNP_MAX_IRQ && !warned) {
88                 printk(KERN_ERR "pnpacpi: exceeded the max number of IRQ "
89                                 "resources: %d \n", PNP_MAX_IRQ);
90                 warned = 1;
91                 return;
92         }
93         /*
94          * in IO-APIC mode, use overrided attribute. Two reasons:
95          * 1. BIOS bug in DSDT
96          * 2. BIOS uses IO-APIC mode Interrupt Source Override
97          */
98         if (!acpi_get_override_irq(gsi, &t, &p)) {
99                 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
100                 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
101
102                 if (triggering != t || polarity != p) {
103                         pnp_warn("IRQ %d override to %s, %s",
104                                 gsi, t ? "edge":"level", p ? "low":"high");
105                         triggering = t;
106                         polarity = p;
107                 }
108         }
109
110         res->irq_resource[i].flags = IORESOURCE_IRQ;    // Also clears _UNSET flag
111         res->irq_resource[i].flags |= irq_flags(triggering, polarity);
112         irq = acpi_register_gsi(gsi, triggering, polarity);
113         if (irq < 0) {
114                 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
115                 return;
116         }
117
118         if (shareable)
119                 res->irq_resource[i].flags |= IORESOURCE_IRQ_SHAREABLE;
120
121         res->irq_resource[i].start = irq;
122         res->irq_resource[i].end = irq;
123         pcibios_penalize_isa_irq(irq, 1);
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_dmaresource(struct pnp_resource_table *res,
170                                                 u32 dma, int type,
171                                                 int bus_master, int transfer)
172 {
173         int i = 0;
174         static unsigned char warned;
175
176         while (i < PNP_MAX_DMA &&
177                !(res->dma_resource[i].flags & IORESOURCE_UNSET))
178                 i++;
179         if (i < PNP_MAX_DMA) {
180                 res->dma_resource[i].flags = IORESOURCE_DMA;    // Also clears _UNSET flag
181                 res->dma_resource[i].flags |=
182                     dma_flags(type, bus_master, transfer);
183                 if (dma == -1) {
184                         res->dma_resource[i].flags |= IORESOURCE_DISABLED;
185                         return;
186                 }
187                 res->dma_resource[i].start = dma;
188                 res->dma_resource[i].end = dma;
189         } else if (!warned) {
190                 printk(KERN_ERR "pnpacpi: exceeded the max number of DMA "
191                                 "resources: %d \n", PNP_MAX_DMA);
192                 warned = 1;
193         }
194 }
195
196 static void pnpacpi_parse_allocated_ioresource(struct pnp_resource_table *res,
197                                                u64 io, u64 len, int io_decode)
198 {
199         int i = 0;
200         static unsigned char warned;
201
202         while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
203                i < PNP_MAX_PORT)
204                 i++;
205         if (i < PNP_MAX_PORT) {
206                 res->port_resource[i].flags = IORESOURCE_IO;    // Also clears _UNSET flag
207                 if (io_decode == ACPI_DECODE_16)
208                         res->port_resource[i].flags |= PNP_PORT_FLAG_16BITADDR;
209                 if (len <= 0 || (io + len - 1) >= 0x10003) {
210                         res->port_resource[i].flags |= IORESOURCE_DISABLED;
211                         return;
212                 }
213                 res->port_resource[i].start = io;
214                 res->port_resource[i].end = io + len - 1;
215         } else if (!warned) {
216                 printk(KERN_ERR "pnpacpi: exceeded the max number of IO "
217                                 "resources: %d \n", PNP_MAX_PORT);
218         }
219 }
220
221 static void pnpacpi_parse_allocated_memresource(struct pnp_resource_table *res,
222                                                 u64 mem, u64 len,
223                                                 int write_protect)
224 {
225         int i = 0;
226         static unsigned char warned;
227
228         while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
229                (i < PNP_MAX_MEM))
230                 i++;
231         if (i < PNP_MAX_MEM) {
232                 res->mem_resource[i].flags = IORESOURCE_MEM;    // Also clears _UNSET flag
233                 if (len <= 0) {
234                         res->mem_resource[i].flags |= IORESOURCE_DISABLED;
235                         return;
236                 }
237                 if (write_protect == ACPI_READ_WRITE_MEMORY)
238                         res->mem_resource[i].flags |= IORESOURCE_MEM_WRITEABLE;
239
240                 res->mem_resource[i].start = mem;
241                 res->mem_resource[i].end = mem + len - 1;
242         } else if (!warned) {
243                 printk(KERN_ERR "pnpacpi: exceeded the max number of mem "
244                                 "resources: %d\n", PNP_MAX_MEM);
245         }
246 }
247
248 static void pnpacpi_parse_allocated_address_space(struct pnp_resource_table *res_table,
249                                                   struct acpi_resource *res)
250 {
251         struct acpi_resource_address64 addr, *p = &addr;
252         acpi_status status;
253
254         status = acpi_resource_to_address64(res, p);
255         if (!ACPI_SUCCESS(status)) {
256                 pnp_warn("PnPACPI: failed to convert resource type %d",
257                          res->type);
258                 return;
259         }
260
261         if (p->producer_consumer == ACPI_PRODUCER)
262                 return;
263
264         if (p->resource_type == ACPI_MEMORY_RANGE)
265                 pnpacpi_parse_allocated_memresource(res_table,
266                         p->minimum, p->address_length,
267                         p->info.mem.write_protect);
268         else if (p->resource_type == ACPI_IO_RANGE)
269                 pnpacpi_parse_allocated_ioresource(res_table,
270                         p->minimum, p->address_length,
271                         p->granularity == 0xfff ? ACPI_DECODE_10 :
272                                 ACPI_DECODE_16);
273 }
274
275 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
276                                               void *data)
277 {
278         struct pnp_resource_table *res_table = data;
279         int i;
280
281         switch (res->type) {
282         case ACPI_RESOURCE_TYPE_IRQ:
283                 /*
284                  * Per spec, only one interrupt per descriptor is allowed in
285                  * _CRS, but some firmware violates this, so parse them all.
286                  */
287                 for (i = 0; i < res->data.irq.interrupt_count; i++) {
288                         pnpacpi_parse_allocated_irqresource(res_table,
289                                 res->data.irq.interrupts[i],
290                                 res->data.irq.triggering,
291                                 res->data.irq.polarity,
292                                 res->data.irq.sharable);
293                 }
294                 break;
295
296         case ACPI_RESOURCE_TYPE_DMA:
297                 if (res->data.dma.channel_count > 0)
298                         pnpacpi_parse_allocated_dmaresource(res_table,
299                                 res->data.dma.channels[0],
300                                 res->data.dma.type,
301                                 res->data.dma.bus_master,
302                                 res->data.dma.transfer);
303                 break;
304
305         case ACPI_RESOURCE_TYPE_IO:
306                 pnpacpi_parse_allocated_ioresource(res_table,
307                         res->data.io.minimum,
308                         res->data.io.address_length,
309                         res->data.io.io_decode);
310                 break;
311
312         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
313         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
314                 break;
315
316         case ACPI_RESOURCE_TYPE_FIXED_IO:
317                 pnpacpi_parse_allocated_ioresource(res_table,
318                         res->data.fixed_io.address,
319                         res->data.fixed_io.address_length,
320                         ACPI_DECODE_10);
321                 break;
322
323         case ACPI_RESOURCE_TYPE_VENDOR:
324                 break;
325
326         case ACPI_RESOURCE_TYPE_END_TAG:
327                 break;
328
329         case ACPI_RESOURCE_TYPE_MEMORY24:
330                 pnpacpi_parse_allocated_memresource(res_table,
331                         res->data.memory24.minimum,
332                         res->data.memory24.address_length,
333                         res->data.memory24.write_protect);
334                 break;
335         case ACPI_RESOURCE_TYPE_MEMORY32:
336                 pnpacpi_parse_allocated_memresource(res_table,
337                         res->data.memory32.minimum,
338                         res->data.memory32.address_length,
339                         res->data.memory32.write_protect);
340                 break;
341         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
342                 pnpacpi_parse_allocated_memresource(res_table,
343                         res->data.fixed_memory32.address,
344                         res->data.fixed_memory32.address_length,
345                         res->data.fixed_memory32.write_protect);
346                 break;
347         case ACPI_RESOURCE_TYPE_ADDRESS16:
348         case ACPI_RESOURCE_TYPE_ADDRESS32:
349         case ACPI_RESOURCE_TYPE_ADDRESS64:
350                 pnpacpi_parse_allocated_address_space(res_table, res);
351                 break;
352
353         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
354                 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
355                         return AE_OK;
356                 break;
357
358         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
359                 if (res->data.extended_irq.producer_consumer == ACPI_PRODUCER)
360                         return AE_OK;
361
362                 for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
363                         pnpacpi_parse_allocated_irqresource(res_table,
364                                 res->data.extended_irq.interrupts[i],
365                                 res->data.extended_irq.triggering,
366                                 res->data.extended_irq.polarity,
367                                 res->data.extended_irq.sharable);
368                 }
369                 break;
370
371         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
372                 break;
373
374         default:
375                 pnp_warn("PnPACPI: unknown resource type %d", res->type);
376                 return AE_ERROR;
377         }
378
379         return AE_OK;
380 }
381
382 acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle,
383                                              struct pnp_resource_table * res)
384 {
385         /* Blank the resource table values */
386         pnp_init_resource_table(res);
387
388         return acpi_walk_resources(handle, METHOD_NAME__CRS,
389                                    pnpacpi_allocated_resource, res);
390 }
391
392 static void pnpacpi_parse_dma_option(struct pnp_option *option,
393                                      struct acpi_resource_dma *p)
394 {
395         int i;
396         struct pnp_dma *dma;
397
398         if (p->channel_count == 0)
399                 return;
400         dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
401         if (!dma)
402                 return;
403
404         for (i = 0; i < p->channel_count; i++)
405                 dma->map |= 1 << p->channels[i];
406
407         dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
408
409         pnp_register_dma_resource(option, dma);
410 }
411
412 static void pnpacpi_parse_irq_option(struct pnp_option *option,
413                                      struct acpi_resource_irq *p)
414 {
415         int i;
416         struct pnp_irq *irq;
417
418         if (p->interrupt_count == 0)
419                 return;
420         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
421         if (!irq)
422                 return;
423
424         for (i = 0; i < p->interrupt_count; i++)
425                 if (p->interrupts[i])
426                         __set_bit(p->interrupts[i], irq->map);
427         irq->flags = irq_flags(p->triggering, p->polarity);
428
429         pnp_register_irq_resource(option, irq);
430 }
431
432 static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
433                                          struct acpi_resource_extended_irq *p)
434 {
435         int i;
436         struct pnp_irq *irq;
437
438         if (p->interrupt_count == 0)
439                 return;
440         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
441         if (!irq)
442                 return;
443
444         for (i = 0; i < p->interrupt_count; i++)
445                 if (p->interrupts[i])
446                         __set_bit(p->interrupts[i], irq->map);
447         irq->flags = irq_flags(p->triggering, p->polarity);
448
449         pnp_register_irq_resource(option, irq);
450 }
451
452 static void pnpacpi_parse_port_option(struct pnp_option *option,
453                                       struct acpi_resource_io *io)
454 {
455         struct pnp_port *port;
456
457         if (io->address_length == 0)
458                 return;
459         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
460         if (!port)
461                 return;
462         port->min = io->minimum;
463         port->max = io->maximum;
464         port->align = io->alignment;
465         port->size = io->address_length;
466         port->flags = ACPI_DECODE_16 == io->io_decode ?
467             PNP_PORT_FLAG_16BITADDR : 0;
468         pnp_register_port_resource(option, port);
469 }
470
471 static void pnpacpi_parse_fixed_port_option(struct pnp_option *option,
472                                             struct acpi_resource_fixed_io *io)
473 {
474         struct pnp_port *port;
475
476         if (io->address_length == 0)
477                 return;
478         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
479         if (!port)
480                 return;
481         port->min = port->max = io->address;
482         port->size = io->address_length;
483         port->align = 0;
484         port->flags = PNP_PORT_FLAG_FIXED;
485         pnp_register_port_resource(option, port);
486 }
487
488 static void pnpacpi_parse_mem24_option(struct pnp_option *option,
489                                        struct acpi_resource_memory24 *p)
490 {
491         struct pnp_mem *mem;
492
493         if (p->address_length == 0)
494                 return;
495         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
496         if (!mem)
497                 return;
498         mem->min = p->minimum;
499         mem->max = p->maximum;
500         mem->align = p->alignment;
501         mem->size = p->address_length;
502
503         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
504             IORESOURCE_MEM_WRITEABLE : 0;
505
506         pnp_register_mem_resource(option, mem);
507 }
508
509 static void pnpacpi_parse_mem32_option(struct pnp_option *option,
510                                        struct acpi_resource_memory32 *p)
511 {
512         struct pnp_mem *mem;
513
514         if (p->address_length == 0)
515                 return;
516         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
517         if (!mem)
518                 return;
519         mem->min = p->minimum;
520         mem->max = p->maximum;
521         mem->align = p->alignment;
522         mem->size = p->address_length;
523
524         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
525             IORESOURCE_MEM_WRITEABLE : 0;
526
527         pnp_register_mem_resource(option, mem);
528 }
529
530 static void pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
531                                          struct acpi_resource_fixed_memory32 *p)
532 {
533         struct pnp_mem *mem;
534
535         if (p->address_length == 0)
536                 return;
537         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
538         if (!mem)
539                 return;
540         mem->min = mem->max = p->address;
541         mem->size = p->address_length;
542         mem->align = 0;
543
544         mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
545             IORESOURCE_MEM_WRITEABLE : 0;
546
547         pnp_register_mem_resource(option, mem);
548 }
549
550 static void pnpacpi_parse_address_option(struct pnp_option *option,
551                                          struct acpi_resource *r)
552 {
553         struct acpi_resource_address64 addr, *p = &addr;
554         acpi_status status;
555         struct pnp_mem *mem;
556         struct pnp_port *port;
557
558         status = acpi_resource_to_address64(r, p);
559         if (!ACPI_SUCCESS(status)) {
560                 pnp_warn("PnPACPI: failed to convert resource type %d",
561                          r->type);
562                 return;
563         }
564
565         if (p->address_length == 0)
566                 return;
567
568         if (p->resource_type == ACPI_MEMORY_RANGE) {
569                 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
570                 if (!mem)
571                         return;
572                 mem->min = mem->max = p->minimum;
573                 mem->size = p->address_length;
574                 mem->align = 0;
575                 mem->flags = (p->info.mem.write_protect ==
576                               ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
577                     : 0;
578                 pnp_register_mem_resource(option, mem);
579         } else if (p->resource_type == ACPI_IO_RANGE) {
580                 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
581                 if (!port)
582                         return;
583                 port->min = port->max = p->minimum;
584                 port->size = p->address_length;
585                 port->align = 0;
586                 port->flags = PNP_PORT_FLAG_FIXED;
587                 pnp_register_port_resource(option, port);
588         }
589 }
590
591 struct acpipnp_parse_option_s {
592         struct pnp_option *option;
593         struct pnp_option *option_independent;
594         struct pnp_dev *dev;
595 };
596
597 static acpi_status pnpacpi_option_resource(struct acpi_resource *res,
598                                            void *data)
599 {
600         int priority = 0;
601         struct acpipnp_parse_option_s *parse_data = data;
602         struct pnp_dev *dev = parse_data->dev;
603         struct pnp_option *option = parse_data->option;
604
605         switch (res->type) {
606         case ACPI_RESOURCE_TYPE_IRQ:
607                 pnpacpi_parse_irq_option(option, &res->data.irq);
608                 break;
609
610         case ACPI_RESOURCE_TYPE_DMA:
611                 pnpacpi_parse_dma_option(option, &res->data.dma);
612                 break;
613
614         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
615                 switch (res->data.start_dpf.compatibility_priority) {
616                 case ACPI_GOOD_CONFIGURATION:
617                         priority = PNP_RES_PRIORITY_PREFERRED;
618                         break;
619
620                 case ACPI_ACCEPTABLE_CONFIGURATION:
621                         priority = PNP_RES_PRIORITY_ACCEPTABLE;
622                         break;
623
624                 case ACPI_SUB_OPTIMAL_CONFIGURATION:
625                         priority = PNP_RES_PRIORITY_FUNCTIONAL;
626                         break;
627                 default:
628                         priority = PNP_RES_PRIORITY_INVALID;
629                         break;
630                 }
631                 /* TBD: Consider performance/robustness bits */
632                 option = pnp_register_dependent_option(dev, priority);
633                 if (!option)
634                         return AE_ERROR;
635                 parse_data->option = option;
636                 break;
637
638         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
639                 /*only one EndDependentFn is allowed */
640                 if (!parse_data->option_independent) {
641                         pnp_warn("PnPACPI: more than one EndDependentFn");
642                         return AE_ERROR;
643                 }
644                 parse_data->option = parse_data->option_independent;
645                 parse_data->option_independent = NULL;
646                 break;
647
648         case ACPI_RESOURCE_TYPE_IO:
649                 pnpacpi_parse_port_option(option, &res->data.io);
650                 break;
651
652         case ACPI_RESOURCE_TYPE_FIXED_IO:
653                 pnpacpi_parse_fixed_port_option(option, &res->data.fixed_io);
654                 break;
655
656         case ACPI_RESOURCE_TYPE_VENDOR:
657         case ACPI_RESOURCE_TYPE_END_TAG:
658                 break;
659
660         case ACPI_RESOURCE_TYPE_MEMORY24:
661                 pnpacpi_parse_mem24_option(option, &res->data.memory24);
662                 break;
663
664         case ACPI_RESOURCE_TYPE_MEMORY32:
665                 pnpacpi_parse_mem32_option(option, &res->data.memory32);
666                 break;
667
668         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
669                 pnpacpi_parse_fixed_mem32_option(option,
670                                                  &res->data.fixed_memory32);
671                 break;
672
673         case ACPI_RESOURCE_TYPE_ADDRESS16:
674         case ACPI_RESOURCE_TYPE_ADDRESS32:
675         case ACPI_RESOURCE_TYPE_ADDRESS64:
676                 pnpacpi_parse_address_option(option, res);
677                 break;
678
679         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
680                 break;
681
682         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
683                 pnpacpi_parse_ext_irq_option(option, &res->data.extended_irq);
684                 break;
685
686         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
687                 break;
688
689         default:
690                 pnp_warn("PnPACPI: unknown resource type %d", res->type);
691                 return AE_ERROR;
692         }
693
694         return AE_OK;
695 }
696
697 acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle,
698                                                struct pnp_dev * dev)
699 {
700         acpi_status status;
701         struct acpipnp_parse_option_s parse_data;
702
703         parse_data.option = pnp_register_independent_option(dev);
704         if (!parse_data.option)
705                 return AE_ERROR;
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         return status;
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(acpi_handle handle,
760                                     struct acpi_buffer *buffer)
761 {
762         struct acpi_resource *resource;
763         int res_cnt = 0;
764         acpi_status status;
765
766         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
767                                      pnpacpi_count_resources, &res_cnt);
768         if (ACPI_FAILURE(status)) {
769                 pnp_err("Evaluate _CRS failed");
770                 return -EINVAL;
771         }
772         if (!res_cnt)
773                 return -EINVAL;
774         buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
775         buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
776         if (!buffer->pointer)
777                 return -ENOMEM;
778         pnp_dbg("Res cnt %d", res_cnt);
779         resource = (struct acpi_resource *)buffer->pointer;
780         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
781                                      pnpacpi_type_resources, &resource);
782         if (ACPI_FAILURE(status)) {
783                 kfree(buffer->pointer);
784                 pnp_err("Evaluate _CRS failed");
785                 return -EINVAL;
786         }
787         /* resource will pointer the end resource now */
788         resource->type = ACPI_RESOURCE_TYPE_END_TAG;
789
790         return 0;
791 }
792
793 static void pnpacpi_encode_irq(struct acpi_resource *resource,
794                                struct resource *p)
795 {
796         int triggering, polarity;
797
798         decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
799         resource->data.irq.triggering = triggering;
800         resource->data.irq.polarity = polarity;
801         if (triggering == ACPI_EDGE_SENSITIVE)
802                 resource->data.irq.sharable = ACPI_EXCLUSIVE;
803         else
804                 resource->data.irq.sharable = ACPI_SHARED;
805         resource->data.irq.interrupt_count = 1;
806         resource->data.irq.interrupts[0] = p->start;
807 }
808
809 static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
810                                    struct resource *p)
811 {
812         int triggering, polarity;
813
814         decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
815         resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
816         resource->data.extended_irq.triggering = triggering;
817         resource->data.extended_irq.polarity = polarity;
818         if (triggering == ACPI_EDGE_SENSITIVE)
819                 resource->data.irq.sharable = ACPI_EXCLUSIVE;
820         else
821                 resource->data.irq.sharable = ACPI_SHARED;
822         resource->data.extended_irq.interrupt_count = 1;
823         resource->data.extended_irq.interrupts[0] = p->start;
824 }
825
826 static void pnpacpi_encode_dma(struct acpi_resource *resource,
827                                struct resource *p)
828 {
829         /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
830         switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
831         case IORESOURCE_DMA_TYPEA:
832                 resource->data.dma.type = ACPI_TYPE_A;
833                 break;
834         case IORESOURCE_DMA_TYPEB:
835                 resource->data.dma.type = ACPI_TYPE_B;
836                 break;
837         case IORESOURCE_DMA_TYPEF:
838                 resource->data.dma.type = ACPI_TYPE_F;
839                 break;
840         default:
841                 resource->data.dma.type = ACPI_COMPATIBILITY;
842         }
843
844         switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
845         case IORESOURCE_DMA_8BIT:
846                 resource->data.dma.transfer = ACPI_TRANSFER_8;
847                 break;
848         case IORESOURCE_DMA_8AND16BIT:
849                 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
850                 break;
851         default:
852                 resource->data.dma.transfer = ACPI_TRANSFER_16;
853         }
854
855         resource->data.dma.bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
856         resource->data.dma.channel_count = 1;
857         resource->data.dma.channels[0] = p->start;
858 }
859
860 static void pnpacpi_encode_io(struct acpi_resource *resource,
861                               struct resource *p)
862 {
863         /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
864         resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
865             ACPI_DECODE_16 : ACPI_DECODE_10;
866         resource->data.io.minimum = p->start;
867         resource->data.io.maximum = p->end;
868         resource->data.io.alignment = 0;        /* Correct? */
869         resource->data.io.address_length = p->end - p->start + 1;
870 }
871
872 static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
873                                     struct resource *p)
874 {
875         resource->data.fixed_io.address = p->start;
876         resource->data.fixed_io.address_length = p->end - p->start + 1;
877 }
878
879 static void pnpacpi_encode_mem24(struct acpi_resource *resource,
880                                  struct resource *p)
881 {
882         /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
883         resource->data.memory24.write_protect =
884             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
885             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
886         resource->data.memory24.minimum = p->start;
887         resource->data.memory24.maximum = p->end;
888         resource->data.memory24.alignment = 0;
889         resource->data.memory24.address_length = p->end - p->start + 1;
890 }
891
892 static void pnpacpi_encode_mem32(struct acpi_resource *resource,
893                                  struct resource *p)
894 {
895         resource->data.memory32.write_protect =
896             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
897             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
898         resource->data.memory32.minimum = p->start;
899         resource->data.memory32.maximum = p->end;
900         resource->data.memory32.alignment = 0;
901         resource->data.memory32.address_length = p->end - p->start + 1;
902 }
903
904 static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
905                                        struct resource *p)
906 {
907         resource->data.fixed_memory32.write_protect =
908             (p->flags & IORESOURCE_MEM_WRITEABLE) ?
909             ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
910         resource->data.fixed_memory32.address = p->start;
911         resource->data.fixed_memory32.address_length = p->end - p->start + 1;
912 }
913
914 int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
915                              struct acpi_buffer *buffer)
916 {
917         int i = 0;
918         /* pnpacpi_build_resource_template allocates extra mem */
919         int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
920         struct acpi_resource *resource = buffer->pointer;
921         int port = 0, irq = 0, dma = 0, mem = 0;
922
923         pnp_dbg("res cnt %d", res_cnt);
924         while (i < res_cnt) {
925                 switch (resource->type) {
926                 case ACPI_RESOURCE_TYPE_IRQ:
927                         pnp_dbg("Encode irq");
928                         pnpacpi_encode_irq(resource,
929                                            &res_table->irq_resource[irq]);
930                         irq++;
931                         break;
932
933                 case ACPI_RESOURCE_TYPE_DMA:
934                         pnp_dbg("Encode dma");
935                         pnpacpi_encode_dma(resource,
936                                            &res_table->dma_resource[dma]);
937                         dma++;
938                         break;
939                 case ACPI_RESOURCE_TYPE_IO:
940                         pnp_dbg("Encode io");
941                         pnpacpi_encode_io(resource,
942                                           &res_table->port_resource[port]);
943                         port++;
944                         break;
945                 case ACPI_RESOURCE_TYPE_FIXED_IO:
946                         pnp_dbg("Encode fixed io");
947                         pnpacpi_encode_fixed_io(resource,
948                                                 &res_table->
949                                                 port_resource[port]);
950                         port++;
951                         break;
952                 case ACPI_RESOURCE_TYPE_MEMORY24:
953                         pnp_dbg("Encode mem24");
954                         pnpacpi_encode_mem24(resource,
955                                              &res_table->mem_resource[mem]);
956                         mem++;
957                         break;
958                 case ACPI_RESOURCE_TYPE_MEMORY32:
959                         pnp_dbg("Encode mem32");
960                         pnpacpi_encode_mem32(resource,
961                                              &res_table->mem_resource[mem]);
962                         mem++;
963                         break;
964                 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
965                         pnp_dbg("Encode fixed mem32");
966                         pnpacpi_encode_fixed_mem32(resource,
967                                                    &res_table->
968                                                    mem_resource[mem]);
969                         mem++;
970                         break;
971                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
972                         pnp_dbg("Encode ext irq");
973                         pnpacpi_encode_ext_irq(resource,
974                                                &res_table->irq_resource[irq]);
975                         irq++;
976                         break;
977                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
978                 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
979                 case ACPI_RESOURCE_TYPE_VENDOR:
980                 case ACPI_RESOURCE_TYPE_END_TAG:
981                 case ACPI_RESOURCE_TYPE_ADDRESS16:
982                 case ACPI_RESOURCE_TYPE_ADDRESS32:
983                 case ACPI_RESOURCE_TYPE_ADDRESS64:
984                 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
985                 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
986                 default:        /* other type */
987                         pnp_warn("unknown resource type %d", resource->type);
988                         return -EINVAL;
989                 }
990                 resource++;
991                 i++;
992         }
993         return 0;
994 }