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