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