PNP: make generic pnp_add_io_resource()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pnp / pnpacpi / rsparser.c
blobd3ca8e035c190f0dae18d656d4ccfbac529aecfe
1 /*
2 * pnpacpi -- PnP ACPI driver
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
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.
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.
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
21 #include <linux/kernel.h>
22 #include <linux/acpi.h>
23 #include <linux/pci.h>
24 #include <linux/pnp.h>
25 #include "../base.h"
26 #include "pnpacpi.h"
28 #ifdef CONFIG_IA64
29 #define valid_IRQ(i) (1)
30 #else
31 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
32 #endif
35 * Allocated Resources
37 static int irq_flags(int triggering, int polarity, int shareable)
39 int flags;
41 if (triggering == ACPI_LEVEL_SENSITIVE) {
42 if (polarity == ACPI_ACTIVE_LOW)
43 flags = IORESOURCE_IRQ_LOWLEVEL;
44 else
45 flags = IORESOURCE_IRQ_HIGHLEVEL;
46 } else {
47 if (polarity == ACPI_ACTIVE_LOW)
48 flags = IORESOURCE_IRQ_LOWEDGE;
49 else
50 flags = IORESOURCE_IRQ_HIGHEDGE;
53 if (shareable)
54 flags |= IORESOURCE_IRQ_SHAREABLE;
56 return flags;
59 static void decode_irq_flags(int flag, int *triggering, int *polarity)
61 switch (flag) {
62 case IORESOURCE_IRQ_LOWLEVEL:
63 *triggering = ACPI_LEVEL_SENSITIVE;
64 *polarity = ACPI_ACTIVE_LOW;
65 break;
66 case IORESOURCE_IRQ_HIGHLEVEL:
67 *triggering = ACPI_LEVEL_SENSITIVE;
68 *polarity = ACPI_ACTIVE_HIGH;
69 break;
70 case IORESOURCE_IRQ_LOWEDGE:
71 *triggering = ACPI_EDGE_SENSITIVE;
72 *polarity = ACPI_ACTIVE_LOW;
73 break;
74 case IORESOURCE_IRQ_HIGHEDGE:
75 *triggering = ACPI_EDGE_SENSITIVE;
76 *polarity = ACPI_ACTIVE_HIGH;
77 break;
81 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
82 u32 gsi, int triggering,
83 int polarity, int shareable)
85 int irq, flags;
86 int p, t;
88 if (!valid_IRQ(gsi))
89 return;
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
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;
100 if (triggering != t || polarity != p) {
101 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
102 gsi, t ? "edge":"level", p ? "low":"high");
103 triggering = t;
104 polarity = p;
108 flags = irq_flags(triggering, polarity, shareable);
109 irq = acpi_register_gsi(gsi, triggering, polarity);
110 if (irq >= 0)
111 pcibios_penalize_isa_irq(irq, 1);
112 else
113 flags |= IORESOURCE_DISABLED;
115 pnp_add_irq_resource(dev, irq, flags);
118 static int dma_flags(int type, int bus_master, int transfer)
120 int flags = 0;
122 if (bus_master)
123 flags |= IORESOURCE_DMA_MASTER;
124 switch (type) {
125 case ACPI_COMPATIBILITY:
126 flags |= IORESOURCE_DMA_COMPATIBLE;
127 break;
128 case ACPI_TYPE_A:
129 flags |= IORESOURCE_DMA_TYPEA;
130 break;
131 case ACPI_TYPE_B:
132 flags |= IORESOURCE_DMA_TYPEB;
133 break;
134 case ACPI_TYPE_F:
135 flags |= IORESOURCE_DMA_TYPEF;
136 break;
137 default:
138 /* Set a default value ? */
139 flags |= IORESOURCE_DMA_COMPATIBLE;
140 pnp_err("Invalid DMA type");
142 switch (transfer) {
143 case ACPI_TRANSFER_8:
144 flags |= IORESOURCE_DMA_8BIT;
145 break;
146 case ACPI_TRANSFER_8_16:
147 flags |= IORESOURCE_DMA_8AND16BIT;
148 break;
149 case ACPI_TRANSFER_16:
150 flags |= IORESOURCE_DMA_16BIT;
151 break;
152 default:
153 /* Set a default value ? */
154 flags |= IORESOURCE_DMA_8AND16BIT;
155 pnp_err("Invalid DMA transfer type");
158 return flags;
161 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
162 u64 len, int io_decode)
164 int flags = 0;
165 u64 end = start + len - 1;
167 if (io_decode == ACPI_DECODE_16)
168 flags |= PNP_PORT_FLAG_16BITADDR;
169 if (len == 0 || end >= 0x10003)
170 flags |= IORESOURCE_DISABLED;
172 pnp_add_io_resource(dev, start, end, flags);
175 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
176 u64 mem, u64 len,
177 int write_protect)
179 struct resource *res;
180 int i;
181 static unsigned char warned;
183 for (i = 0; i < PNP_MAX_MEM; i++) {
184 res = pnp_get_resource(dev, IORESOURCE_MEM, i);
185 if (!pnp_resource_valid(res))
186 break;
188 if (i < PNP_MAX_MEM) {
189 res->flags = IORESOURCE_MEM; // Also clears _UNSET flag
190 if (len <= 0) {
191 res->flags |= IORESOURCE_DISABLED;
192 return;
194 if (write_protect == ACPI_READ_WRITE_MEMORY)
195 res->flags |= IORESOURCE_MEM_WRITEABLE;
197 res->start = mem;
198 res->end = mem + len - 1;
199 } else if (!warned) {
200 printk(KERN_WARNING "pnpacpi: exceeded the max number of mem "
201 "resources: %d\n", PNP_MAX_MEM);
202 warned = 1;
206 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
207 struct acpi_resource *res)
209 struct acpi_resource_address64 addr, *p = &addr;
210 acpi_status status;
212 status = acpi_resource_to_address64(res, p);
213 if (!ACPI_SUCCESS(status)) {
214 dev_warn(&dev->dev, "failed to convert resource type %d\n",
215 res->type);
216 return;
219 if (p->producer_consumer == ACPI_PRODUCER)
220 return;
222 if (p->resource_type == ACPI_MEMORY_RANGE)
223 pnpacpi_parse_allocated_memresource(dev,
224 p->minimum, p->address_length,
225 p->info.mem.write_protect);
226 else if (p->resource_type == ACPI_IO_RANGE)
227 pnpacpi_parse_allocated_ioresource(dev,
228 p->minimum, p->address_length,
229 p->granularity == 0xfff ? ACPI_DECODE_10 :
230 ACPI_DECODE_16);
233 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
234 void *data)
236 struct pnp_dev *dev = data;
237 struct acpi_resource_irq *irq;
238 struct acpi_resource_dma *dma;
239 struct acpi_resource_io *io;
240 struct acpi_resource_fixed_io *fixed_io;
241 struct acpi_resource_memory24 *memory24;
242 struct acpi_resource_memory32 *memory32;
243 struct acpi_resource_fixed_memory32 *fixed_memory32;
244 struct acpi_resource_extended_irq *extended_irq;
245 int i, flags;
247 switch (res->type) {
248 case ACPI_RESOURCE_TYPE_IRQ:
250 * Per spec, only one interrupt per descriptor is allowed in
251 * _CRS, but some firmware violates this, so parse them all.
253 irq = &res->data.irq;
254 for (i = 0; i < irq->interrupt_count; i++) {
255 pnpacpi_parse_allocated_irqresource(dev,
256 irq->interrupts[i],
257 irq->triggering,
258 irq->polarity,
259 irq->sharable);
261 break;
263 case ACPI_RESOURCE_TYPE_DMA:
264 dma = &res->data.dma;
265 if (dma->channel_count > 0) {
266 flags = dma_flags(dma->type, dma->bus_master,
267 dma->transfer);
268 if (dma->channels[0] == (u8) -1)
269 flags |= IORESOURCE_DISABLED;
270 pnp_add_dma_resource(dev, dma->channels[0], flags);
272 break;
274 case ACPI_RESOURCE_TYPE_IO:
275 io = &res->data.io;
276 pnpacpi_parse_allocated_ioresource(dev,
277 io->minimum,
278 io->address_length,
279 io->io_decode);
280 break;
282 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
283 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
284 break;
286 case ACPI_RESOURCE_TYPE_FIXED_IO:
287 fixed_io = &res->data.fixed_io;
288 pnpacpi_parse_allocated_ioresource(dev,
289 fixed_io->address,
290 fixed_io->address_length,
291 ACPI_DECODE_10);
292 break;
294 case ACPI_RESOURCE_TYPE_VENDOR:
295 break;
297 case ACPI_RESOURCE_TYPE_END_TAG:
298 break;
300 case ACPI_RESOURCE_TYPE_MEMORY24:
301 memory24 = &res->data.memory24;
302 pnpacpi_parse_allocated_memresource(dev,
303 memory24->minimum,
304 memory24->address_length,
305 memory24->write_protect);
306 break;
307 case ACPI_RESOURCE_TYPE_MEMORY32:
308 memory32 = &res->data.memory32;
309 pnpacpi_parse_allocated_memresource(dev,
310 memory32->minimum,
311 memory32->address_length,
312 memory32->write_protect);
313 break;
314 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
315 fixed_memory32 = &res->data.fixed_memory32;
316 pnpacpi_parse_allocated_memresource(dev,
317 fixed_memory32->address,
318 fixed_memory32->address_length,
319 fixed_memory32->write_protect);
320 break;
321 case ACPI_RESOURCE_TYPE_ADDRESS16:
322 case ACPI_RESOURCE_TYPE_ADDRESS32:
323 case ACPI_RESOURCE_TYPE_ADDRESS64:
324 pnpacpi_parse_allocated_address_space(dev, res);
325 break;
327 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
328 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
329 return AE_OK;
330 break;
332 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
333 extended_irq = &res->data.extended_irq;
334 if (extended_irq->producer_consumer == ACPI_PRODUCER)
335 return AE_OK;
337 for (i = 0; i < extended_irq->interrupt_count; i++) {
338 pnpacpi_parse_allocated_irqresource(dev,
339 extended_irq->interrupts[i],
340 extended_irq->triggering,
341 extended_irq->polarity,
342 extended_irq->sharable);
344 break;
346 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
347 break;
349 default:
350 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
351 res->type);
352 return AE_ERROR;
355 return AE_OK;
358 acpi_status pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
360 acpi_handle handle = dev->data;
362 dev_dbg(&dev->dev, "parse allocated resources\n");
364 pnp_init_resources(dev);
366 return acpi_walk_resources(handle, METHOD_NAME__CRS,
367 pnpacpi_allocated_resource, dev);
370 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
371 struct pnp_option *option,
372 struct acpi_resource_dma *p)
374 int i;
375 struct pnp_dma *dma;
377 if (p->channel_count == 0)
378 return;
379 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
380 if (!dma)
381 return;
383 for (i = 0; i < p->channel_count; i++)
384 dma->map |= 1 << p->channels[i];
386 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
388 pnp_register_dma_resource(dev, option, dma);
391 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
392 struct pnp_option *option,
393 struct acpi_resource_irq *p)
395 int i;
396 struct pnp_irq *irq;
398 if (p->interrupt_count == 0)
399 return;
400 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
401 if (!irq)
402 return;
404 for (i = 0; i < p->interrupt_count; i++)
405 if (p->interrupts[i])
406 __set_bit(p->interrupts[i], irq->map);
407 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
409 pnp_register_irq_resource(dev, option, irq);
412 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
413 struct pnp_option *option,
414 struct acpi_resource_extended_irq *p)
416 int i;
417 struct pnp_irq *irq;
419 if (p->interrupt_count == 0)
420 return;
421 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
422 if (!irq)
423 return;
425 for (i = 0; i < p->interrupt_count; i++)
426 if (p->interrupts[i])
427 __set_bit(p->interrupts[i], irq->map);
428 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
430 pnp_register_irq_resource(dev, option, irq);
433 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
434 struct pnp_option *option,
435 struct acpi_resource_io *io)
437 struct pnp_port *port;
439 if (io->address_length == 0)
440 return;
441 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
442 if (!port)
443 return;
444 port->min = io->minimum;
445 port->max = io->maximum;
446 port->align = io->alignment;
447 port->size = io->address_length;
448 port->flags = ACPI_DECODE_16 == io->io_decode ?
449 PNP_PORT_FLAG_16BITADDR : 0;
450 pnp_register_port_resource(dev, option, port);
453 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
454 struct pnp_option *option,
455 struct acpi_resource_fixed_io *io)
457 struct pnp_port *port;
459 if (io->address_length == 0)
460 return;
461 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
462 if (!port)
463 return;
464 port->min = port->max = io->address;
465 port->size = io->address_length;
466 port->align = 0;
467 port->flags = PNP_PORT_FLAG_FIXED;
468 pnp_register_port_resource(dev, option, port);
471 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
472 struct pnp_option *option,
473 struct acpi_resource_memory24 *p)
475 struct pnp_mem *mem;
477 if (p->address_length == 0)
478 return;
479 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
480 if (!mem)
481 return;
482 mem->min = p->minimum;
483 mem->max = p->maximum;
484 mem->align = p->alignment;
485 mem->size = p->address_length;
487 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
488 IORESOURCE_MEM_WRITEABLE : 0;
490 pnp_register_mem_resource(dev, option, mem);
493 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
494 struct pnp_option *option,
495 struct acpi_resource_memory32 *p)
497 struct pnp_mem *mem;
499 if (p->address_length == 0)
500 return;
501 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
502 if (!mem)
503 return;
504 mem->min = p->minimum;
505 mem->max = p->maximum;
506 mem->align = p->alignment;
507 mem->size = p->address_length;
509 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
510 IORESOURCE_MEM_WRITEABLE : 0;
512 pnp_register_mem_resource(dev, option, mem);
515 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
516 struct pnp_option *option,
517 struct acpi_resource_fixed_memory32 *p)
519 struct pnp_mem *mem;
521 if (p->address_length == 0)
522 return;
523 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
524 if (!mem)
525 return;
526 mem->min = mem->max = p->address;
527 mem->size = p->address_length;
528 mem->align = 0;
530 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
531 IORESOURCE_MEM_WRITEABLE : 0;
533 pnp_register_mem_resource(dev, option, mem);
536 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
537 struct pnp_option *option,
538 struct acpi_resource *r)
540 struct acpi_resource_address64 addr, *p = &addr;
541 acpi_status status;
542 struct pnp_mem *mem;
543 struct pnp_port *port;
545 status = acpi_resource_to_address64(r, p);
546 if (!ACPI_SUCCESS(status)) {
547 pnp_warn("PnPACPI: failed to convert resource type %d",
548 r->type);
549 return;
552 if (p->address_length == 0)
553 return;
555 if (p->resource_type == ACPI_MEMORY_RANGE) {
556 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
557 if (!mem)
558 return;
559 mem->min = mem->max = p->minimum;
560 mem->size = p->address_length;
561 mem->align = 0;
562 mem->flags = (p->info.mem.write_protect ==
563 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
564 : 0;
565 pnp_register_mem_resource(dev, option, mem);
566 } else if (p->resource_type == ACPI_IO_RANGE) {
567 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
568 if (!port)
569 return;
570 port->min = port->max = p->minimum;
571 port->size = p->address_length;
572 port->align = 0;
573 port->flags = PNP_PORT_FLAG_FIXED;
574 pnp_register_port_resource(dev, option, port);
578 struct acpipnp_parse_option_s {
579 struct pnp_option *option;
580 struct pnp_option *option_independent;
581 struct pnp_dev *dev;
584 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
585 void *data)
587 int priority = 0;
588 struct acpipnp_parse_option_s *parse_data = data;
589 struct pnp_dev *dev = parse_data->dev;
590 struct pnp_option *option = parse_data->option;
592 switch (res->type) {
593 case ACPI_RESOURCE_TYPE_IRQ:
594 pnpacpi_parse_irq_option(dev, option, &res->data.irq);
595 break;
597 case ACPI_RESOURCE_TYPE_DMA:
598 pnpacpi_parse_dma_option(dev, option, &res->data.dma);
599 break;
601 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
602 switch (res->data.start_dpf.compatibility_priority) {
603 case ACPI_GOOD_CONFIGURATION:
604 priority = PNP_RES_PRIORITY_PREFERRED;
605 break;
607 case ACPI_ACCEPTABLE_CONFIGURATION:
608 priority = PNP_RES_PRIORITY_ACCEPTABLE;
609 break;
611 case ACPI_SUB_OPTIMAL_CONFIGURATION:
612 priority = PNP_RES_PRIORITY_FUNCTIONAL;
613 break;
614 default:
615 priority = PNP_RES_PRIORITY_INVALID;
616 break;
618 /* TBD: Consider performance/robustness bits */
619 option = pnp_register_dependent_option(dev, priority);
620 if (!option)
621 return AE_ERROR;
622 parse_data->option = option;
623 break;
625 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
626 /*only one EndDependentFn is allowed */
627 if (!parse_data->option_independent) {
628 dev_warn(&dev->dev, "more than one EndDependentFn "
629 "in _PRS\n");
630 return AE_ERROR;
632 parse_data->option = parse_data->option_independent;
633 parse_data->option_independent = NULL;
634 dev_dbg(&dev->dev, "end dependent options\n");
635 break;
637 case ACPI_RESOURCE_TYPE_IO:
638 pnpacpi_parse_port_option(dev, option, &res->data.io);
639 break;
641 case ACPI_RESOURCE_TYPE_FIXED_IO:
642 pnpacpi_parse_fixed_port_option(dev, option,
643 &res->data.fixed_io);
644 break;
646 case ACPI_RESOURCE_TYPE_VENDOR:
647 case ACPI_RESOURCE_TYPE_END_TAG:
648 break;
650 case ACPI_RESOURCE_TYPE_MEMORY24:
651 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24);
652 break;
654 case ACPI_RESOURCE_TYPE_MEMORY32:
655 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32);
656 break;
658 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
659 pnpacpi_parse_fixed_mem32_option(dev, option,
660 &res->data.fixed_memory32);
661 break;
663 case ACPI_RESOURCE_TYPE_ADDRESS16:
664 case ACPI_RESOURCE_TYPE_ADDRESS32:
665 case ACPI_RESOURCE_TYPE_ADDRESS64:
666 pnpacpi_parse_address_option(dev, option, res);
667 break;
669 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
670 break;
672 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
673 pnpacpi_parse_ext_irq_option(dev, option,
674 &res->data.extended_irq);
675 break;
677 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
678 break;
680 default:
681 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
682 res->type);
683 return AE_ERROR;
686 return AE_OK;
689 acpi_status __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
691 acpi_handle handle = dev->data;
692 acpi_status status;
693 struct acpipnp_parse_option_s parse_data;
695 dev_dbg(&dev->dev, "parse resource options\n");
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);
705 return status;
708 static int pnpacpi_supported_resource(struct acpi_resource *res)
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;
724 return 0;
728 * Set resource
730 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
731 void *data)
733 int *res_cnt = data;
735 if (pnpacpi_supported_resource(res))
736 (*res_cnt)++;
737 return AE_OK;
740 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
742 struct acpi_resource **resource = data;
744 if (pnpacpi_supported_resource(res)) {
745 (*resource)->type = res->type;
746 (*resource)->length = sizeof(struct acpi_resource);
747 (*resource)++;
750 return AE_OK;
753 int pnpacpi_build_resource_template(struct pnp_dev *dev,
754 struct acpi_buffer *buffer)
756 acpi_handle handle = dev->data;
757 struct acpi_resource *resource;
758 int res_cnt = 0;
759 acpi_status status;
761 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
762 pnpacpi_count_resources, &res_cnt);
763 if (ACPI_FAILURE(status)) {
764 dev_err(&dev->dev, "can't evaluate _CRS\n");
765 return -EINVAL;
767 if (!res_cnt)
768 return -EINVAL;
769 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
770 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
771 if (!buffer->pointer)
772 return -ENOMEM;
774 resource = (struct acpi_resource *)buffer->pointer;
775 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
776 pnpacpi_type_resources, &resource);
777 if (ACPI_FAILURE(status)) {
778 kfree(buffer->pointer);
779 dev_err(&dev->dev, "can't evaluate _CRS\n");
780 return -EINVAL;
782 /* resource will pointer the end resource now */
783 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
785 return 0;
788 static void pnpacpi_encode_irq(struct pnp_dev *dev,
789 struct acpi_resource *resource,
790 struct resource *p)
792 struct acpi_resource_irq *irq = &resource->data.irq;
793 int triggering, polarity;
795 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
796 irq->triggering = triggering;
797 irq->polarity = polarity;
798 if (triggering == ACPI_EDGE_SENSITIVE)
799 irq->sharable = ACPI_EXCLUSIVE;
800 else
801 irq->sharable = ACPI_SHARED;
802 irq->interrupt_count = 1;
803 irq->interrupts[0] = p->start;
805 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
806 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
807 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
808 irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
811 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
812 struct acpi_resource *resource,
813 struct resource *p)
815 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
816 int triggering, polarity;
818 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
819 extended_irq->producer_consumer = ACPI_CONSUMER;
820 extended_irq->triggering = triggering;
821 extended_irq->polarity = polarity;
822 if (triggering == ACPI_EDGE_SENSITIVE)
823 extended_irq->sharable = ACPI_EXCLUSIVE;
824 else
825 extended_irq->sharable = ACPI_SHARED;
826 extended_irq->interrupt_count = 1;
827 extended_irq->interrupts[0] = p->start;
829 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
830 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
831 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
832 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
835 static void pnpacpi_encode_dma(struct pnp_dev *dev,
836 struct acpi_resource *resource,
837 struct resource *p)
839 struct acpi_resource_dma *dma = &resource->data.dma;
841 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
842 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
843 case IORESOURCE_DMA_TYPEA:
844 dma->type = ACPI_TYPE_A;
845 break;
846 case IORESOURCE_DMA_TYPEB:
847 dma->type = ACPI_TYPE_B;
848 break;
849 case IORESOURCE_DMA_TYPEF:
850 dma->type = ACPI_TYPE_F;
851 break;
852 default:
853 dma->type = ACPI_COMPATIBILITY;
856 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
857 case IORESOURCE_DMA_8BIT:
858 dma->transfer = ACPI_TRANSFER_8;
859 break;
860 case IORESOURCE_DMA_8AND16BIT:
861 dma->transfer = ACPI_TRANSFER_8_16;
862 break;
863 default:
864 dma->transfer = ACPI_TRANSFER_16;
867 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
868 dma->channel_count = 1;
869 dma->channels[0] = p->start;
871 dev_dbg(&dev->dev, " encode dma %d "
872 "type %#x transfer %#x master %d\n",
873 (int) p->start, dma->type, dma->transfer, dma->bus_master);
876 static void pnpacpi_encode_io(struct pnp_dev *dev,
877 struct acpi_resource *resource,
878 struct resource *p)
880 struct acpi_resource_io *io = &resource->data.io;
882 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
883 io->io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
884 ACPI_DECODE_16 : ACPI_DECODE_10;
885 io->minimum = p->start;
886 io->maximum = p->end;
887 io->alignment = 0; /* Correct? */
888 io->address_length = p->end - p->start + 1;
890 dev_dbg(&dev->dev, " encode io %#llx-%#llx decode %#x\n",
891 (unsigned long long) p->start, (unsigned long long) p->end,
892 io->io_decode);
895 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
896 struct acpi_resource *resource,
897 struct resource *p)
899 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
901 fixed_io->address = p->start;
902 fixed_io->address_length = p->end - p->start + 1;
904 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
905 (unsigned long long) p->start, (unsigned long long) p->end);
908 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
909 struct acpi_resource *resource,
910 struct resource *p)
912 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
914 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
915 memory24->write_protect =
916 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
917 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
918 memory24->minimum = p->start;
919 memory24->maximum = p->end;
920 memory24->alignment = 0;
921 memory24->address_length = p->end - p->start + 1;
923 dev_dbg(&dev->dev, " encode mem24 %#llx-%#llx write_protect %#x\n",
924 (unsigned long long) p->start, (unsigned long long) p->end,
925 memory24->write_protect);
928 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
929 struct acpi_resource *resource,
930 struct resource *p)
932 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
934 memory32->write_protect =
935 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
936 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
937 memory32->minimum = p->start;
938 memory32->maximum = p->end;
939 memory32->alignment = 0;
940 memory32->address_length = p->end - p->start + 1;
942 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx write_protect %#x\n",
943 (unsigned long long) p->start, (unsigned long long) p->end,
944 memory32->write_protect);
947 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
948 struct acpi_resource *resource,
949 struct resource *p)
951 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
953 fixed_memory32->write_protect =
954 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
955 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
956 fixed_memory32->address = p->start;
957 fixed_memory32->address_length = p->end - p->start + 1;
959 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx "
960 "write_protect %#x\n",
961 (unsigned long long) p->start, (unsigned long long) p->end,
962 fixed_memory32->write_protect);
965 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
967 int i = 0;
968 /* pnpacpi_build_resource_template allocates extra mem */
969 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
970 struct acpi_resource *resource = buffer->pointer;
971 int port = 0, irq = 0, dma = 0, mem = 0;
973 dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
974 while (i < res_cnt) {
975 switch (resource->type) {
976 case ACPI_RESOURCE_TYPE_IRQ:
977 pnpacpi_encode_irq(dev, resource,
978 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
979 irq++;
980 break;
982 case ACPI_RESOURCE_TYPE_DMA:
983 pnpacpi_encode_dma(dev, resource,
984 pnp_get_resource(dev, IORESOURCE_DMA, dma));
985 dma++;
986 break;
987 case ACPI_RESOURCE_TYPE_IO:
988 pnpacpi_encode_io(dev, resource,
989 pnp_get_resource(dev, IORESOURCE_IO, port));
990 port++;
991 break;
992 case ACPI_RESOURCE_TYPE_FIXED_IO:
993 pnpacpi_encode_fixed_io(dev, resource,
994 pnp_get_resource(dev, IORESOURCE_IO, port));
995 port++;
996 break;
997 case ACPI_RESOURCE_TYPE_MEMORY24:
998 pnpacpi_encode_mem24(dev, resource,
999 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1000 mem++;
1001 break;
1002 case ACPI_RESOURCE_TYPE_MEMORY32:
1003 pnpacpi_encode_mem32(dev, resource,
1004 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1005 mem++;
1006 break;
1007 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1008 pnpacpi_encode_fixed_mem32(dev, resource,
1009 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1010 mem++;
1011 break;
1012 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1013 pnpacpi_encode_ext_irq(dev, resource,
1014 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1015 irq++;
1016 break;
1017 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1018 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1019 case ACPI_RESOURCE_TYPE_VENDOR:
1020 case ACPI_RESOURCE_TYPE_END_TAG:
1021 case ACPI_RESOURCE_TYPE_ADDRESS16:
1022 case ACPI_RESOURCE_TYPE_ADDRESS32:
1023 case ACPI_RESOURCE_TYPE_ADDRESS64:
1024 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1025 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1026 default: /* other type */
1027 dev_warn(&dev->dev, "can't encode unknown resource "
1028 "type %d\n", resource->type);
1029 return -EINVAL;
1031 resource++;
1032 i++;
1034 return 0;