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