2424bd3620dc8703712e84aaf31da59698ac4a75
[linux-2.6.git] / 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 edge_level, int active_high_low)
36 {
37         int flag;
38         if (edge_level == ACPI_LEVEL_SENSITIVE) {
39                 if(active_high_low == ACPI_ACTIVE_LOW)
40                         flag = IORESOURCE_IRQ_LOWLEVEL;
41                 else
42                         flag = IORESOURCE_IRQ_HIGHLEVEL;
43         }
44         else {
45                 if(active_high_low == ACPI_ACTIVE_LOW)
46                         flag = IORESOURCE_IRQ_LOWEDGE;
47                 else
48                         flag = IORESOURCE_IRQ_HIGHEDGE;
49         }
50         return flag;
51 }
52
53 static void decode_irq_flags(int flag, int *edge_level, int *active_high_low)
54 {
55         switch (flag) {
56         case IORESOURCE_IRQ_LOWLEVEL:
57                 *edge_level = ACPI_LEVEL_SENSITIVE;
58                 *active_high_low = ACPI_ACTIVE_LOW;
59                 break;
60         case IORESOURCE_IRQ_HIGHLEVEL:  
61                 *edge_level = ACPI_LEVEL_SENSITIVE;
62                 *active_high_low = ACPI_ACTIVE_HIGH;
63                 break;
64         case IORESOURCE_IRQ_LOWEDGE:
65                 *edge_level = ACPI_EDGE_SENSITIVE;
66                 *active_high_low = ACPI_ACTIVE_LOW;
67                 break;
68         case IORESOURCE_IRQ_HIGHEDGE:
69                 *edge_level = ACPI_EDGE_SENSITIVE;
70                 *active_high_low = ACPI_ACTIVE_HIGH;
71                 break;
72         }
73 }
74
75 static void
76 pnpacpi_parse_allocated_irqresource(struct pnp_resource_table * res, u32 gsi,
77         int edge_level, int active_high_low)
78 {
79         int i = 0;
80         int irq;
81
82         if (!valid_IRQ(gsi))
83                 return;
84
85         while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
86                         i < PNP_MAX_IRQ)
87                 i++;
88         if (i >= PNP_MAX_IRQ)
89                 return;
90
91         res->irq_resource[i].flags = IORESOURCE_IRQ;  // Also clears _UNSET flag
92         irq = acpi_register_gsi(gsi, edge_level, active_high_low);
93         if (irq < 0) {
94                 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
95                 return;
96         }
97
98         res->irq_resource[i].start = irq;
99         res->irq_resource[i].end = irq;
100         pcibios_penalize_isa_irq(irq, 1);
101 }
102
103 static void
104 pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table * res, u32 dma)
105 {
106         int i = 0;
107         while (i < PNP_MAX_DMA &&
108                         !(res->dma_resource[i].flags & IORESOURCE_UNSET))
109                 i++;
110         if (i < PNP_MAX_DMA) {
111                 res->dma_resource[i].flags = IORESOURCE_DMA;  // Also clears _UNSET flag
112                 if (dma == -1) {
113                         res->dma_resource[i].flags |= IORESOURCE_DISABLED;
114                         return;
115                 }
116                 res->dma_resource[i].start = dma;
117                 res->dma_resource[i].end = dma;
118         }
119 }
120
121 static void
122 pnpacpi_parse_allocated_ioresource(struct pnp_resource_table * res,
123         u32 io, u32 len)
124 {
125         int i = 0;
126         while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
127                         i < PNP_MAX_PORT)
128                 i++;
129         if (i < PNP_MAX_PORT) {
130                 res->port_resource[i].flags = IORESOURCE_IO;  // Also clears _UNSET flag
131                 if (len <= 0 || (io + len -1) >= 0x10003) {
132                         res->port_resource[i].flags |= IORESOURCE_DISABLED;
133                         return;
134                 }
135                 res->port_resource[i].start = io;
136                 res->port_resource[i].end = io + len - 1;
137         }
138 }
139
140 static void
141 pnpacpi_parse_allocated_memresource(struct pnp_resource_table * res,
142         u64 mem, u64 len)
143 {
144         int i = 0;
145         while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
146                         (i < PNP_MAX_MEM))
147                 i++;
148         if (i < PNP_MAX_MEM) {
149                 res->mem_resource[i].flags = IORESOURCE_MEM;  // Also clears _UNSET flag
150                 if (len <= 0) {
151                         res->mem_resource[i].flags |= IORESOURCE_DISABLED;
152                         return;
153                 }
154                 res->mem_resource[i].start = mem;
155                 res->mem_resource[i].end = mem + len - 1;
156         }
157 }
158
159
160 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
161         void *data)
162 {
163         struct pnp_resource_table * res_table = (struct pnp_resource_table *)data;
164         int i;
165
166         switch (res->id) {
167         case ACPI_RSTYPE_IRQ:
168                 /*
169                  * Per spec, only one interrupt per descriptor is allowed in
170                  * _CRS, but some firmware violates this, so parse them all.
171                  */
172                 for (i = 0; i < res->data.irq.number_of_interrupts; i++) {
173                         pnpacpi_parse_allocated_irqresource(res_table,
174                                 res->data.irq.interrupts[i],
175                                 res->data.irq.edge_level,
176                                 res->data.irq.active_high_low);
177                 }
178                 break;
179
180         case ACPI_RSTYPE_EXT_IRQ:
181                 for (i = 0; i < res->data.extended_irq.number_of_interrupts; i++) {
182                         pnpacpi_parse_allocated_irqresource(res_table,
183                                 res->data.extended_irq.interrupts[i],
184                                 res->data.extended_irq.edge_level,
185                                 res->data.extended_irq.active_high_low);
186                 }
187                 break;
188         case ACPI_RSTYPE_DMA:
189                 if (res->data.dma.number_of_channels > 0)
190                         pnpacpi_parse_allocated_dmaresource(res_table, 
191                                         res->data.dma.channels[0]);
192                 break;
193         case ACPI_RSTYPE_IO:
194                 pnpacpi_parse_allocated_ioresource(res_table, 
195                                 res->data.io.min_base_address, 
196                                 res->data.io.range_length);
197                 break;
198         case ACPI_RSTYPE_FIXED_IO:
199                 pnpacpi_parse_allocated_ioresource(res_table, 
200                                 res->data.fixed_io.base_address, 
201                                 res->data.fixed_io.range_length);
202                 break;
203         case ACPI_RSTYPE_MEM24:
204                 pnpacpi_parse_allocated_memresource(res_table, 
205                                 res->data.memory24.min_base_address, 
206                                 res->data.memory24.range_length);
207                 break;
208         case ACPI_RSTYPE_MEM32:
209                 pnpacpi_parse_allocated_memresource(res_table, 
210                                 res->data.memory32.min_base_address, 
211                                 res->data.memory32.range_length);
212                 break;
213         case ACPI_RSTYPE_FIXED_MEM32:
214                 pnpacpi_parse_allocated_memresource(res_table, 
215                                 res->data.fixed_memory32.range_base_address, 
216                                 res->data.fixed_memory32.range_length);
217                 break;
218         case ACPI_RSTYPE_ADDRESS16:
219                 pnpacpi_parse_allocated_memresource(res_table, 
220                                 res->data.address16.min_address_range, 
221                                 res->data.address16.address_length);
222                 break;
223         case ACPI_RSTYPE_ADDRESS32:
224                 pnpacpi_parse_allocated_memresource(res_table, 
225                                 res->data.address32.min_address_range, 
226                                 res->data.address32.address_length);
227                 break;
228         case ACPI_RSTYPE_ADDRESS64:
229                 pnpacpi_parse_allocated_memresource(res_table, 
230                 res->data.address64.min_address_range, 
231                 res->data.address64.address_length);
232                 break;
233         case ACPI_RSTYPE_VENDOR:
234                 break;
235         default:
236                 pnp_warn("PnPACPI: unknown resource type %d", res->id);
237                 return AE_ERROR;
238         }
239                         
240         return AE_OK;
241 }
242
243 acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle, struct pnp_resource_table * res)
244 {
245         /* Blank the resource table values */
246         pnp_init_resource_table(res);
247
248         return acpi_walk_resources(handle, METHOD_NAME__CRS, pnpacpi_allocated_resource, res);
249 }
250
251 static void pnpacpi_parse_dma_option(struct pnp_option *option, struct acpi_resource_dma *p)
252 {
253         int i;
254         struct pnp_dma * dma;
255
256         if (p->number_of_channels == 0)
257                 return;
258         dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL);
259         if (!dma)
260                 return;
261
262         for(i = 0; i < p->number_of_channels; i++)
263                 dma->map |= 1 << p->channels[i];
264         dma->flags = 0;
265         if (p->bus_master)
266                 dma->flags |= IORESOURCE_DMA_MASTER;
267         switch (p->type) {
268         case ACPI_COMPATIBILITY:
269                 dma->flags |= IORESOURCE_DMA_COMPATIBLE;
270                 break;
271         case ACPI_TYPE_A:
272                 dma->flags |= IORESOURCE_DMA_TYPEA;
273                 break;
274         case ACPI_TYPE_B:
275                 dma->flags |= IORESOURCE_DMA_TYPEB;
276                 break;
277         case ACPI_TYPE_F:
278                 dma->flags |= IORESOURCE_DMA_TYPEF;
279                 break;
280         default:
281                 /* Set a default value ? */
282                 dma->flags |= IORESOURCE_DMA_COMPATIBLE;
283                 pnp_err("Invalid DMA type");
284         }
285         switch (p->transfer) {
286         case ACPI_TRANSFER_8:
287                 dma->flags |= IORESOURCE_DMA_8BIT;
288                 break;
289         case ACPI_TRANSFER_8_16:
290                 dma->flags |= IORESOURCE_DMA_8AND16BIT;
291                 break;
292         case ACPI_TRANSFER_16:
293                 dma->flags |= IORESOURCE_DMA_16BIT;
294                 break;
295         default:
296                 /* Set a default value ? */
297                 dma->flags |= IORESOURCE_DMA_8AND16BIT;
298                 pnp_err("Invalid DMA transfer type");
299         }
300
301         pnp_register_dma_resource(option,dma);
302         return;
303 }
304
305         
306 static void pnpacpi_parse_irq_option(struct pnp_option *option,
307         struct acpi_resource_irq *p)
308 {
309         int i;
310         struct pnp_irq * irq;
311         
312         if (p->number_of_interrupts == 0)
313                 return;
314         irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
315         if (!irq)
316                 return;
317
318         for(i = 0; i < p->number_of_interrupts; i++)
319                 if (p->interrupts[i])
320                         __set_bit(p->interrupts[i], irq->map);
321         irq->flags = irq_flags(p->edge_level, p->active_high_low);
322
323         pnp_register_irq_resource(option, irq);
324         return;
325 }
326
327 static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
328         struct acpi_resource_ext_irq *p)
329 {
330         int i;
331         struct pnp_irq * irq;
332
333         if (p->number_of_interrupts == 0)
334                 return;
335         irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
336         if (!irq)
337                 return;
338
339         for(i = 0; i < p->number_of_interrupts; i++)
340                 if (p->interrupts[i])
341                         __set_bit(p->interrupts[i], irq->map);
342         irq->flags = irq_flags(p->edge_level, p->active_high_low);
343
344         pnp_register_irq_resource(option, irq);
345         return;
346 }
347
348 static void
349 pnpacpi_parse_port_option(struct pnp_option *option,
350         struct acpi_resource_io *io)
351 {
352         struct pnp_port * port;
353
354         if (io->range_length == 0)
355                 return;
356         port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
357         if (!port)
358                 return;
359         port->min = io->min_base_address;
360         port->max = io->max_base_address;
361         port->align = io->alignment;
362         port->size = io->range_length;
363         port->flags = ACPI_DECODE_16 == io->io_decode ? 
364                 PNP_PORT_FLAG_16BITADDR : 0;
365         pnp_register_port_resource(option,port);
366         return;
367 }
368
369 static void
370 pnpacpi_parse_fixed_port_option(struct pnp_option *option,
371         struct acpi_resource_fixed_io *io)
372 {
373         struct pnp_port * port;
374
375         if (io->range_length == 0)
376                 return;
377         port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
378         if (!port)
379                 return;
380         port->min = port->max = io->base_address;
381         port->size = io->range_length;
382         port->align = 0;
383         port->flags = PNP_PORT_FLAG_FIXED;
384         pnp_register_port_resource(option,port);
385         return;
386 }
387
388 static void
389 pnpacpi_parse_mem24_option(struct pnp_option *option,
390         struct acpi_resource_mem24 *p)
391 {
392         struct pnp_mem * mem;
393
394         if (p->range_length == 0)
395                 return;
396         mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
397         if (!mem)
398                 return;
399         mem->min = p->min_base_address;
400         mem->max = p->max_base_address;
401         mem->align = p->alignment;
402         mem->size = p->range_length;
403
404         mem->flags = (ACPI_READ_WRITE_MEMORY == p->read_write_attribute) ?
405                         IORESOURCE_MEM_WRITEABLE : 0;
406
407         pnp_register_mem_resource(option,mem);
408         return;
409 }
410
411 static void
412 pnpacpi_parse_mem32_option(struct pnp_option *option,
413         struct acpi_resource_mem32 *p)
414 {
415         struct pnp_mem * mem;
416
417         if (p->range_length == 0)
418                 return;
419         mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
420         if (!mem)
421                 return;
422         mem->min = p->min_base_address;
423         mem->max = p->max_base_address;
424         mem->align = p->alignment;
425         mem->size = p->range_length;
426
427         mem->flags = (ACPI_READ_WRITE_MEMORY == p->read_write_attribute) ?
428                         IORESOURCE_MEM_WRITEABLE : 0;
429
430         pnp_register_mem_resource(option,mem);
431         return;
432 }
433
434 static void
435 pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
436         struct acpi_resource_fixed_mem32 *p)
437 {
438         struct pnp_mem * mem;
439
440         if (p->range_length == 0)
441                 return;
442         mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
443         if (!mem)
444                 return;
445         mem->min = mem->max = p->range_base_address;
446         mem->size = p->range_length;
447         mem->align = 0;
448
449         mem->flags = (ACPI_READ_WRITE_MEMORY == p->read_write_attribute) ?
450                         IORESOURCE_MEM_WRITEABLE : 0;
451
452         pnp_register_mem_resource(option,mem);
453         return;
454 }
455
456 static void
457 pnpacpi_parse_address_option(struct pnp_option *option, struct acpi_resource *r)
458 {
459         struct acpi_resource_address64 addr, *p = &addr;
460         acpi_status status;
461         struct pnp_mem * mem;
462         struct pnp_port * port;
463
464         status = acpi_resource_to_address64(r, p);
465         if (!ACPI_SUCCESS(status)) {
466                 pnp_warn("PnPACPI: failed to convert resource type %d", r->id);
467                 return;
468         }
469
470         if (p->address_length == 0)
471                 return;
472
473         if (p->resource_type == ACPI_MEMORY_RANGE) {
474                 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
475                 if (!mem)
476                         return;
477                 mem->min = mem->max = p->min_address_range;
478                 mem->size = p->address_length;
479                 mem->align = 0;
480                 mem->flags = (p->attribute.memory.read_write_attribute ==
481                     ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE : 0;
482                 pnp_register_mem_resource(option,mem);
483         } else if (p->resource_type == ACPI_IO_RANGE) {
484                 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
485                 if (!port)
486                         return;
487                 port->min = port->max = p->min_address_range;
488                 port->size = p->address_length;
489                 port->align = 0;
490                 port->flags = PNP_PORT_FLAG_FIXED;
491                 pnp_register_port_resource(option,port);
492         }
493 }
494
495 struct acpipnp_parse_option_s {
496         struct pnp_option *option;
497         struct pnp_option *option_independent;
498         struct pnp_dev *dev;
499 };
500
501 static acpi_status pnpacpi_option_resource(struct acpi_resource *res, 
502         void *data)
503 {
504         int priority = 0;
505         struct acpipnp_parse_option_s *parse_data = (struct acpipnp_parse_option_s *)data;
506         struct pnp_dev *dev = parse_data->dev;
507         struct pnp_option *option = parse_data->option;
508
509         switch (res->id) {
510                 case ACPI_RSTYPE_IRQ:
511                         pnpacpi_parse_irq_option(option, &res->data.irq);
512                         break;
513                 case ACPI_RSTYPE_EXT_IRQ:
514                         pnpacpi_parse_ext_irq_option(option,
515                                 &res->data.extended_irq);
516                         break;
517                 case ACPI_RSTYPE_DMA:
518                         pnpacpi_parse_dma_option(option, &res->data.dma);       
519                         break;
520                 case ACPI_RSTYPE_IO:
521                         pnpacpi_parse_port_option(option, &res->data.io);
522                         break;
523                 case ACPI_RSTYPE_FIXED_IO:
524                         pnpacpi_parse_fixed_port_option(option,
525                                 &res->data.fixed_io);
526                         break;
527                 case ACPI_RSTYPE_MEM24:
528                         pnpacpi_parse_mem24_option(option, &res->data.memory24);
529                         break;
530                 case ACPI_RSTYPE_MEM32:
531                         pnpacpi_parse_mem32_option(option, &res->data.memory32);
532                         break;
533                 case ACPI_RSTYPE_FIXED_MEM32:
534                         pnpacpi_parse_fixed_mem32_option(option,
535                                 &res->data.fixed_memory32);
536                         break;
537                 case ACPI_RSTYPE_ADDRESS16:
538                 case ACPI_RSTYPE_ADDRESS32:
539                 case ACPI_RSTYPE_ADDRESS64:
540                         pnpacpi_parse_address_option(option, res);
541                         break;
542                 case ACPI_RSTYPE_START_DPF:
543                         switch (res->data.start_dpf.compatibility_priority) {
544                                 case ACPI_GOOD_CONFIGURATION:
545                                         priority = PNP_RES_PRIORITY_PREFERRED;
546                                         break;
547                                         
548                                 case ACPI_ACCEPTABLE_CONFIGURATION:
549                                         priority = PNP_RES_PRIORITY_ACCEPTABLE;
550                                         break;
551
552                                 case ACPI_SUB_OPTIMAL_CONFIGURATION:
553                                         priority = PNP_RES_PRIORITY_FUNCTIONAL;
554                                         break;
555                                 default:
556                                         priority = PNP_RES_PRIORITY_INVALID;
557                                         break;
558                         }
559                         /* TBD: Considering performace/robustness bits */
560                         option = pnp_register_dependent_option(dev, priority);
561                         if (!option)
562                                 return AE_ERROR;
563                         parse_data->option = option;    
564                         break;
565                 case ACPI_RSTYPE_END_DPF:
566                         /*only one EndDependentFn is allowed*/
567                         if (!parse_data->option_independent) {
568                                 pnp_warn("PnPACPI: more than one EndDependentFn");
569                                 return AE_ERROR;
570                         }
571                         parse_data->option = parse_data->option_independent;
572                         parse_data->option_independent = NULL;
573                         break;
574                 default:
575                         pnp_warn("PnPACPI: unknown resource type %d", res->id);
576                         return AE_ERROR;
577         }
578                         
579         return AE_OK;
580 }
581
582 acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle, 
583         struct pnp_dev *dev)
584 {
585         acpi_status status;
586         struct acpipnp_parse_option_s parse_data;
587
588         parse_data.option = pnp_register_independent_option(dev);
589         if (!parse_data.option)
590                 return AE_ERROR;
591         parse_data.option_independent = parse_data.option;
592         parse_data.dev = dev;
593         status = acpi_walk_resources(handle, METHOD_NAME__PRS, 
594                 pnpacpi_option_resource, &parse_data);
595
596         return status;
597 }
598
599 /*
600  * Set resource
601  */
602 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
603         void *data)
604 {
605         int *res_cnt = (int *)data;
606         switch (res->id) {
607         case ACPI_RSTYPE_IRQ:
608         case ACPI_RSTYPE_EXT_IRQ:
609         case ACPI_RSTYPE_DMA:
610         case ACPI_RSTYPE_IO:
611         case ACPI_RSTYPE_FIXED_IO:
612         case ACPI_RSTYPE_MEM24:
613         case ACPI_RSTYPE_MEM32:
614         case ACPI_RSTYPE_FIXED_MEM32:
615         case ACPI_RSTYPE_ADDRESS16:
616         case ACPI_RSTYPE_ADDRESS32:
617         case ACPI_RSTYPE_ADDRESS64:
618                 (*res_cnt) ++;
619         default:
620                 return AE_OK;
621         }
622         return AE_OK;
623 }
624
625 static acpi_status pnpacpi_type_resources(struct acpi_resource *res,
626         void *data)
627 {
628         struct acpi_resource **resource = (struct acpi_resource **)data;        
629         switch (res->id) {
630         case ACPI_RSTYPE_IRQ:
631         case ACPI_RSTYPE_EXT_IRQ:
632         case ACPI_RSTYPE_DMA:
633         case ACPI_RSTYPE_IO:
634         case ACPI_RSTYPE_FIXED_IO:
635         case ACPI_RSTYPE_MEM24:
636         case ACPI_RSTYPE_MEM32:
637         case ACPI_RSTYPE_FIXED_MEM32:
638         case ACPI_RSTYPE_ADDRESS16:
639         case ACPI_RSTYPE_ADDRESS32:
640         case ACPI_RSTYPE_ADDRESS64:
641                 (*resource)->id = res->id;
642                 (*resource)++;
643         default:
644                 return AE_OK;
645         }
646
647         return AE_OK;
648 }
649
650 int pnpacpi_build_resource_template(acpi_handle handle, 
651         struct acpi_buffer *buffer)
652 {
653         struct acpi_resource *resource;
654         int res_cnt = 0;
655         acpi_status status;
656
657         status = acpi_walk_resources(handle, METHOD_NAME__CRS, 
658                 pnpacpi_count_resources, &res_cnt);
659         if (ACPI_FAILURE(status)) {
660                 pnp_err("Evaluate _CRS failed");
661                 return -EINVAL;
662         }
663         if (!res_cnt)
664                 return -EINVAL;
665         buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
666         buffer->pointer = kcalloc(1, buffer->length - 1, GFP_KERNEL);
667         if (!buffer->pointer)
668                 return -ENOMEM;
669         pnp_dbg("Res cnt %d", res_cnt);
670         resource = (struct acpi_resource *)buffer->pointer;
671         status = acpi_walk_resources(handle, METHOD_NAME__CRS, 
672                 pnpacpi_type_resources, &resource);
673         if (ACPI_FAILURE(status)) {
674                 kfree(buffer->pointer);
675                 pnp_err("Evaluate _CRS failed");
676                 return -EINVAL;
677         }
678         /* resource will pointer the end resource now */
679         resource->id = ACPI_RSTYPE_END_TAG;
680
681         return 0;
682 }
683
684 static void pnpacpi_encode_irq(struct acpi_resource *resource, 
685         struct resource *p)
686 {
687         int edge_level, active_high_low;
688         
689         decode_irq_flags(p->flags & IORESOURCE_BITS, &edge_level, 
690                 &active_high_low);
691         resource->id = ACPI_RSTYPE_IRQ;
692         resource->length = sizeof(struct acpi_resource);
693         resource->data.irq.edge_level = edge_level;
694         resource->data.irq.active_high_low = active_high_low;
695         if (edge_level == ACPI_EDGE_SENSITIVE)
696                 resource->data.irq.shared_exclusive = ACPI_EXCLUSIVE;
697         else
698                 resource->data.irq.shared_exclusive = ACPI_SHARED;
699         resource->data.irq.number_of_interrupts = 1;
700         resource->data.irq.interrupts[0] = p->start;
701 }
702
703 static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
704         struct resource *p)
705 {
706         int edge_level, active_high_low;
707         
708         decode_irq_flags(p->flags & IORESOURCE_BITS, &edge_level, 
709                 &active_high_low);
710         resource->id = ACPI_RSTYPE_EXT_IRQ;
711         resource->length = sizeof(struct acpi_resource);
712         resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
713         resource->data.extended_irq.edge_level = edge_level;
714         resource->data.extended_irq.active_high_low = active_high_low;
715         if (edge_level == ACPI_EDGE_SENSITIVE)
716                 resource->data.irq.shared_exclusive = ACPI_EXCLUSIVE;
717         else
718                 resource->data.irq.shared_exclusive = ACPI_SHARED;
719         resource->data.extended_irq.number_of_interrupts = 1;
720         resource->data.extended_irq.interrupts[0] = p->start;
721 }
722
723 static void pnpacpi_encode_dma(struct acpi_resource *resource,
724         struct resource *p)
725 {
726         resource->id = ACPI_RSTYPE_DMA;
727         resource->length = sizeof(struct acpi_resource);
728         /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
729         if (p->flags & IORESOURCE_DMA_COMPATIBLE)
730                 resource->data.dma.type = ACPI_COMPATIBILITY;
731         else if (p->flags & IORESOURCE_DMA_TYPEA)
732                 resource->data.dma.type = ACPI_TYPE_A;
733         else if (p->flags & IORESOURCE_DMA_TYPEB)
734                 resource->data.dma.type = ACPI_TYPE_B;
735         else if (p->flags & IORESOURCE_DMA_TYPEF)
736                 resource->data.dma.type = ACPI_TYPE_F;
737         if (p->flags & IORESOURCE_DMA_8BIT)
738                 resource->data.dma.transfer = ACPI_TRANSFER_8;
739         else if (p->flags & IORESOURCE_DMA_8AND16BIT)
740                 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
741         else if (p->flags & IORESOURCE_DMA_16BIT)
742                 resource->data.dma.transfer = ACPI_TRANSFER_16;
743         resource->data.dma.bus_master = p->flags & IORESOURCE_DMA_MASTER;
744         resource->data.dma.number_of_channels = 1;
745         resource->data.dma.channels[0] = p->start;
746 }
747
748 static void pnpacpi_encode_io(struct acpi_resource *resource,
749         struct resource *p)
750 {
751         resource->id = ACPI_RSTYPE_IO;
752         resource->length = sizeof(struct acpi_resource);
753         /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
754         resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR)?
755                 ACPI_DECODE_16 : ACPI_DECODE_10; 
756         resource->data.io.min_base_address = p->start;
757         resource->data.io.max_base_address = p->end;
758         resource->data.io.alignment = 0; /* Correct? */
759         resource->data.io.range_length = p->end - p->start + 1;
760 }
761
762 static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
763         struct resource *p)
764 {
765         resource->id = ACPI_RSTYPE_FIXED_IO;
766         resource->length = sizeof(struct acpi_resource);
767         resource->data.fixed_io.base_address = p->start;
768         resource->data.fixed_io.range_length = p->end - p->start + 1;
769 }
770
771 static void pnpacpi_encode_mem24(struct acpi_resource *resource,
772         struct resource *p)
773 {
774         resource->id = ACPI_RSTYPE_MEM24;
775         resource->length = sizeof(struct acpi_resource);
776         /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
777         resource->data.memory24.read_write_attribute =
778                 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
779                 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
780         resource->data.memory24.min_base_address = p->start;
781         resource->data.memory24.max_base_address = p->end;
782         resource->data.memory24.alignment = 0;
783         resource->data.memory24.range_length = p->end - p->start + 1;
784 }
785
786 static void pnpacpi_encode_mem32(struct acpi_resource *resource,
787         struct resource *p)
788 {
789         resource->id = ACPI_RSTYPE_MEM32;
790         resource->length = sizeof(struct acpi_resource);
791         resource->data.memory32.read_write_attribute =
792                 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
793                 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
794         resource->data.memory32.min_base_address = p->start;
795         resource->data.memory32.max_base_address = p->end;
796         resource->data.memory32.alignment = 0;
797         resource->data.memory32.range_length = p->end - p->start + 1;
798 }
799
800 static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
801         struct resource *p)
802 {
803         resource->id = ACPI_RSTYPE_FIXED_MEM32;
804         resource->length = sizeof(struct acpi_resource);
805         resource->data.fixed_memory32.read_write_attribute =
806                 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
807                 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
808         resource->data.fixed_memory32.range_base_address = p->start;
809         resource->data.fixed_memory32.range_length = p->end - p->start + 1;
810 }
811
812 int pnpacpi_encode_resources(struct pnp_resource_table *res_table, 
813         struct acpi_buffer *buffer)
814 {
815         int i = 0;
816         /* pnpacpi_build_resource_template allocates extra mem */
817         int res_cnt = (buffer->length - 1)/sizeof(struct acpi_resource) - 1;
818         struct acpi_resource *resource = (struct acpi_resource*)buffer->pointer;
819         int port = 0, irq = 0, dma = 0, mem = 0;
820
821         pnp_dbg("res cnt %d", res_cnt);
822         while (i < res_cnt) {
823                 switch(resource->id) {
824                 case ACPI_RSTYPE_IRQ:
825                         pnp_dbg("Encode irq");
826                         pnpacpi_encode_irq(resource, 
827                                 &res_table->irq_resource[irq]);
828                         irq++;
829                         break;
830
831                 case ACPI_RSTYPE_EXT_IRQ:
832                         pnp_dbg("Encode ext irq");
833                         pnpacpi_encode_ext_irq(resource, 
834                                 &res_table->irq_resource[irq]);
835                         irq++;
836                         break;
837                 case ACPI_RSTYPE_DMA:
838                         pnp_dbg("Encode dma");
839                         pnpacpi_encode_dma(resource, 
840                                 &res_table->dma_resource[dma]);
841                         dma ++;
842                         break;
843                 case ACPI_RSTYPE_IO:
844                         pnp_dbg("Encode io");
845                         pnpacpi_encode_io(resource, 
846                                 &res_table->port_resource[port]);
847                         port ++;
848                         break;
849                 case ACPI_RSTYPE_FIXED_IO:
850                         pnp_dbg("Encode fixed io");
851                         pnpacpi_encode_fixed_io(resource,
852                                 &res_table->port_resource[port]);
853                         port ++;
854                         break;
855                 case ACPI_RSTYPE_MEM24:
856                         pnp_dbg("Encode mem24");
857                         pnpacpi_encode_mem24(resource,
858                                 &res_table->mem_resource[mem]);
859                         mem ++;
860                         break;
861                 case ACPI_RSTYPE_MEM32:
862                         pnp_dbg("Encode mem32");
863                         pnpacpi_encode_mem32(resource,
864                                 &res_table->mem_resource[mem]);
865                         mem ++;
866                         break;
867                 case ACPI_RSTYPE_FIXED_MEM32:
868                         pnp_dbg("Encode fixed mem32");
869                         pnpacpi_encode_fixed_mem32(resource,
870                                 &res_table->mem_resource[mem]);
871                         mem ++;
872                         break;
873                 default: /* other type */
874                         pnp_warn("unknown resource type %d", resource->id);
875                         return -EINVAL;
876                 }
877                 resource ++;
878                 i ++;
879         }
880         return 0;
881 }