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
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>
29 #define valid_IRQ(i) (1)
31 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
37 static int irq_flags(int triggering, int polarity, int shareable)
41 if (triggering == ACPI_LEVEL_SENSITIVE) {
42 if (polarity == ACPI_ACTIVE_LOW)
43 flags = IORESOURCE_IRQ_LOWLEVEL;
45 flags = IORESOURCE_IRQ_HIGHLEVEL;
47 if (polarity == ACPI_ACTIVE_LOW)
48 flags = IORESOURCE_IRQ_LOWEDGE;
50 flags = IORESOURCE_IRQ_HIGHEDGE;
53 if (shareable == ACPI_SHARED)
54 flags |= IORESOURCE_IRQ_SHAREABLE;
59 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
60 int *polarity, int *shareable)
62 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
63 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
64 case IORESOURCE_IRQ_LOWLEVEL:
65 *triggering = ACPI_LEVEL_SENSITIVE;
66 *polarity = ACPI_ACTIVE_LOW;
68 case IORESOURCE_IRQ_HIGHLEVEL:
69 *triggering = ACPI_LEVEL_SENSITIVE;
70 *polarity = ACPI_ACTIVE_HIGH;
72 case IORESOURCE_IRQ_LOWEDGE:
73 *triggering = ACPI_EDGE_SENSITIVE;
74 *polarity = ACPI_ACTIVE_LOW;
76 case IORESOURCE_IRQ_HIGHEDGE:
77 *triggering = ACPI_EDGE_SENSITIVE;
78 *polarity = ACPI_ACTIVE_HIGH;
81 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
83 *triggering = ACPI_EDGE_SENSITIVE;
84 *polarity = ACPI_ACTIVE_HIGH;
88 if (flags & IORESOURCE_IRQ_SHAREABLE)
89 *shareable = ACPI_SHARED;
91 *shareable = ACPI_EXCLUSIVE;
94 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
95 u32 gsi, int triggering,
96 int polarity, int shareable)
101 if (!valid_IRQ(gsi)) {
102 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
107 * in IO-APIC mode, use overrided attribute. Two reasons:
108 * 1. BIOS bug in DSDT
109 * 2. BIOS uses IO-APIC mode Interrupt Source Override
111 if (!acpi_get_override_irq(gsi, &t, &p)) {
112 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
113 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
115 if (triggering != t || polarity != p) {
116 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
117 gsi, t ? "edge":"level", p ? "low":"high");
123 flags = irq_flags(triggering, polarity, shareable);
124 irq = acpi_register_gsi(gsi, triggering, polarity);
126 pcibios_penalize_isa_irq(irq, 1);
128 flags |= IORESOURCE_DISABLED;
130 pnp_add_irq_resource(dev, irq, flags);
133 static int dma_flags(int type, int bus_master, int transfer)
138 flags |= IORESOURCE_DMA_MASTER;
140 case ACPI_COMPATIBILITY:
141 flags |= IORESOURCE_DMA_COMPATIBLE;
144 flags |= IORESOURCE_DMA_TYPEA;
147 flags |= IORESOURCE_DMA_TYPEB;
150 flags |= IORESOURCE_DMA_TYPEF;
153 /* Set a default value ? */
154 flags |= IORESOURCE_DMA_COMPATIBLE;
155 pnp_err("Invalid DMA type");
158 case ACPI_TRANSFER_8:
159 flags |= IORESOURCE_DMA_8BIT;
161 case ACPI_TRANSFER_8_16:
162 flags |= IORESOURCE_DMA_8AND16BIT;
164 case ACPI_TRANSFER_16:
165 flags |= IORESOURCE_DMA_16BIT;
168 /* Set a default value ? */
169 flags |= IORESOURCE_DMA_8AND16BIT;
170 pnp_err("Invalid DMA transfer type");
176 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
177 u64 len, int io_decode)
180 u64 end = start + len - 1;
182 if (io_decode == ACPI_DECODE_16)
183 flags |= IORESOURCE_IO_16BIT_ADDR;
184 if (len == 0 || end >= 0x10003)
185 flags |= IORESOURCE_DISABLED;
187 pnp_add_io_resource(dev, start, end, flags);
190 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
195 u64 end = start + len - 1;
198 flags |= IORESOURCE_DISABLED;
199 if (write_protect == ACPI_READ_WRITE_MEMORY)
200 flags |= IORESOURCE_MEM_WRITEABLE;
202 pnp_add_mem_resource(dev, start, end, flags);
205 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
206 struct acpi_resource *res)
208 struct acpi_resource_address64 addr, *p = &addr;
211 status = acpi_resource_to_address64(res, p);
212 if (!ACPI_SUCCESS(status)) {
213 dev_warn(&dev->dev, "failed to convert resource type %d\n",
218 if (p->producer_consumer == ACPI_PRODUCER)
221 if (p->resource_type == ACPI_MEMORY_RANGE)
222 pnpacpi_parse_allocated_memresource(dev,
223 p->minimum, p->address_length,
224 p->info.mem.write_protect);
225 else if (p->resource_type == ACPI_IO_RANGE)
226 pnpacpi_parse_allocated_ioresource(dev,
227 p->minimum, p->address_length,
228 p->granularity == 0xfff ? ACPI_DECODE_10 :
232 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
235 struct pnp_dev *dev = data;
236 struct acpi_resource_irq *irq;
237 struct acpi_resource_dma *dma;
238 struct acpi_resource_io *io;
239 struct acpi_resource_fixed_io *fixed_io;
240 struct acpi_resource_memory24 *memory24;
241 struct acpi_resource_memory32 *memory32;
242 struct acpi_resource_fixed_memory32 *fixed_memory32;
243 struct acpi_resource_extended_irq *extended_irq;
247 case ACPI_RESOURCE_TYPE_IRQ:
249 * Per spec, only one interrupt per descriptor is allowed in
250 * _CRS, but some firmware violates this, so parse them all.
252 irq = &res->data.irq;
253 if (irq->interrupt_count == 0)
254 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
256 for (i = 0; i < irq->interrupt_count; i++) {
257 pnpacpi_parse_allocated_irqresource(dev,
265 * The IRQ encoder puts a single interrupt in each
266 * descriptor, so if a _CRS descriptor has more than
267 * one interrupt, we won't be able to re-encode it.
269 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
270 dev_warn(&dev->dev, "multiple interrupts in "
271 "_CRS descriptor; configuration can't "
273 dev->capabilities &= ~PNP_WRITE;
278 case ACPI_RESOURCE_TYPE_DMA:
279 dma = &res->data.dma;
280 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
281 flags = dma_flags(dma->type, dma->bus_master,
284 flags = IORESOURCE_DISABLED;
285 pnp_add_dma_resource(dev, dma->channels[0], flags);
288 case ACPI_RESOURCE_TYPE_IO:
290 pnpacpi_parse_allocated_ioresource(dev,
296 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
297 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
300 case ACPI_RESOURCE_TYPE_FIXED_IO:
301 fixed_io = &res->data.fixed_io;
302 pnpacpi_parse_allocated_ioresource(dev,
304 fixed_io->address_length,
308 case ACPI_RESOURCE_TYPE_VENDOR:
311 case ACPI_RESOURCE_TYPE_END_TAG:
314 case ACPI_RESOURCE_TYPE_MEMORY24:
315 memory24 = &res->data.memory24;
316 pnpacpi_parse_allocated_memresource(dev,
318 memory24->address_length,
319 memory24->write_protect);
321 case ACPI_RESOURCE_TYPE_MEMORY32:
322 memory32 = &res->data.memory32;
323 pnpacpi_parse_allocated_memresource(dev,
325 memory32->address_length,
326 memory32->write_protect);
328 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
329 fixed_memory32 = &res->data.fixed_memory32;
330 pnpacpi_parse_allocated_memresource(dev,
331 fixed_memory32->address,
332 fixed_memory32->address_length,
333 fixed_memory32->write_protect);
335 case ACPI_RESOURCE_TYPE_ADDRESS16:
336 case ACPI_RESOURCE_TYPE_ADDRESS32:
337 case ACPI_RESOURCE_TYPE_ADDRESS64:
338 pnpacpi_parse_allocated_address_space(dev, res);
341 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
342 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
346 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
347 extended_irq = &res->data.extended_irq;
348 if (extended_irq->producer_consumer == ACPI_PRODUCER)
351 if (extended_irq->interrupt_count == 0)
352 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
354 for (i = 0; i < extended_irq->interrupt_count; i++) {
355 pnpacpi_parse_allocated_irqresource(dev,
356 extended_irq->interrupts[i],
357 extended_irq->triggering,
358 extended_irq->polarity,
359 extended_irq->sharable);
363 * The IRQ encoder puts a single interrupt in each
364 * descriptor, so if a _CRS descriptor has more than
365 * one interrupt, we won't be able to re-encode it.
367 if (pnp_can_write(dev) &&
368 extended_irq->interrupt_count > 1) {
369 dev_warn(&dev->dev, "multiple interrupts in "
370 "_CRS descriptor; configuration can't "
372 dev->capabilities &= ~PNP_WRITE;
377 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
381 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
389 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
391 acpi_handle handle = dev->data;
394 dev_dbg(&dev->dev, "parse allocated resources\n");
396 pnp_init_resources(dev);
398 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
399 pnpacpi_allocated_resource, dev);
401 if (ACPI_FAILURE(status)) {
402 if (status != AE_NOT_FOUND)
403 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
409 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
410 struct pnp_option *option,
411 struct acpi_resource_dma *p)
414 unsigned char map = 0, flags;
416 if (p->channel_count == 0)
419 for (i = 0; i < p->channel_count; i++)
420 map |= 1 << p->channels[i];
422 flags = dma_flags(p->type, p->bus_master, p->transfer);
423 pnp_register_dma_resource(dev, option, map, flags);
426 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
427 struct pnp_option *option,
428 struct acpi_resource_irq *p)
434 if (p->interrupt_count == 0)
437 bitmap_zero(map.bits, PNP_IRQ_NR);
438 for (i = 0; i < p->interrupt_count; i++)
439 if (p->interrupts[i])
440 __set_bit(p->interrupts[i], map.bits);
442 flags = irq_flags(p->triggering, p->polarity, p->sharable);
443 pnp_register_irq_resource(dev, option, &map, flags);
446 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
447 struct pnp_option *option,
448 struct acpi_resource_extended_irq *p)
454 if (p->interrupt_count == 0)
457 bitmap_zero(map.bits, PNP_IRQ_NR);
458 for (i = 0; i < p->interrupt_count; i++) {
459 if (p->interrupts[i]) {
460 if (p->interrupts[i] < PNP_IRQ_NR)
461 __set_bit(p->interrupts[i], map.bits);
463 dev_err(&dev->dev, "ignoring IRQ %d option "
464 "(too large for %d entry bitmap)\n",
465 p->interrupts[i], PNP_IRQ_NR);
469 flags = irq_flags(p->triggering, p->polarity, p->sharable);
470 pnp_register_irq_resource(dev, option, &map, flags);
473 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
474 struct pnp_option *option,
475 struct acpi_resource_io *io)
477 unsigned char flags = 0;
479 if (io->address_length == 0)
482 if (io->io_decode == ACPI_DECODE_16)
483 flags = IORESOURCE_IO_16BIT_ADDR;
484 pnp_register_port_resource(dev, option, io->minimum, io->maximum,
485 io->alignment, io->address_length, flags);
488 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
489 struct pnp_option *option,
490 struct acpi_resource_fixed_io *io)
492 if (io->address_length == 0)
495 pnp_register_port_resource(dev, option, io->address, io->address, 0,
496 io->address_length, IORESOURCE_IO_FIXED);
499 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
500 struct pnp_option *option,
501 struct acpi_resource_memory24 *p)
503 unsigned char flags = 0;
505 if (p->address_length == 0)
508 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
509 flags = IORESOURCE_MEM_WRITEABLE;
510 pnp_register_mem_resource(dev, option, p->minimum, p->maximum,
511 p->alignment, p->address_length, flags);
514 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
515 struct pnp_option *option,
516 struct acpi_resource_memory32 *p)
518 unsigned char flags = 0;
520 if (p->address_length == 0)
523 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
524 flags = IORESOURCE_MEM_WRITEABLE;
525 pnp_register_mem_resource(dev, option, p->minimum, p->maximum,
526 p->alignment, p->address_length, flags);
529 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
530 struct pnp_option *option,
531 struct acpi_resource_fixed_memory32 *p)
533 unsigned char flags = 0;
535 if (p->address_length == 0)
538 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
539 flags = IORESOURCE_MEM_WRITEABLE;
540 pnp_register_mem_resource(dev, option, p->address, p->address,
541 0, p->address_length, flags);
544 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
545 struct pnp_option *option,
546 struct acpi_resource *r)
548 struct acpi_resource_address64 addr, *p = &addr;
550 unsigned char flags = 0;
552 status = acpi_resource_to_address64(r, p);
553 if (!ACPI_SUCCESS(status)) {
554 pnp_warn("PnPACPI: failed to convert resource type %d",
559 if (p->address_length == 0)
562 if (p->resource_type == ACPI_MEMORY_RANGE) {
563 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
564 flags = IORESOURCE_MEM_WRITEABLE;
565 pnp_register_mem_resource(dev, option, p->minimum, p->minimum,
566 0, p->address_length, flags);
567 } else if (p->resource_type == ACPI_IO_RANGE)
568 pnp_register_port_resource(dev, option, p->minimum, p->minimum,
569 0, p->address_length,
570 IORESOURCE_IO_FIXED);
573 struct acpipnp_parse_option_s {
574 struct pnp_option *option;
575 struct pnp_option *option_independent;
579 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
583 struct acpipnp_parse_option_s *parse_data = data;
584 struct pnp_dev *dev = parse_data->dev;
585 struct pnp_option *option = parse_data->option;
588 case ACPI_RESOURCE_TYPE_IRQ:
589 pnpacpi_parse_irq_option(dev, option, &res->data.irq);
592 case ACPI_RESOURCE_TYPE_DMA:
593 pnpacpi_parse_dma_option(dev, option, &res->data.dma);
596 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
597 switch (res->data.start_dpf.compatibility_priority) {
598 case ACPI_GOOD_CONFIGURATION:
599 priority = PNP_RES_PRIORITY_PREFERRED;
602 case ACPI_ACCEPTABLE_CONFIGURATION:
603 priority = PNP_RES_PRIORITY_ACCEPTABLE;
606 case ACPI_SUB_OPTIMAL_CONFIGURATION:
607 priority = PNP_RES_PRIORITY_FUNCTIONAL;
610 priority = PNP_RES_PRIORITY_INVALID;
613 /* TBD: Consider performance/robustness bits */
614 option = pnp_register_dependent_option(dev, priority);
617 parse_data->option = option;
620 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
621 /*only one EndDependentFn is allowed */
622 if (!parse_data->option_independent) {
623 dev_warn(&dev->dev, "more than one EndDependentFn "
627 parse_data->option = parse_data->option_independent;
628 parse_data->option_independent = NULL;
629 dev_dbg(&dev->dev, "end dependent options\n");
632 case ACPI_RESOURCE_TYPE_IO:
633 pnpacpi_parse_port_option(dev, option, &res->data.io);
636 case ACPI_RESOURCE_TYPE_FIXED_IO:
637 pnpacpi_parse_fixed_port_option(dev, option,
638 &res->data.fixed_io);
641 case ACPI_RESOURCE_TYPE_VENDOR:
642 case ACPI_RESOURCE_TYPE_END_TAG:
645 case ACPI_RESOURCE_TYPE_MEMORY24:
646 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24);
649 case ACPI_RESOURCE_TYPE_MEMORY32:
650 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32);
653 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
654 pnpacpi_parse_fixed_mem32_option(dev, option,
655 &res->data.fixed_memory32);
658 case ACPI_RESOURCE_TYPE_ADDRESS16:
659 case ACPI_RESOURCE_TYPE_ADDRESS32:
660 case ACPI_RESOURCE_TYPE_ADDRESS64:
661 pnpacpi_parse_address_option(dev, option, res);
664 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
667 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
668 pnpacpi_parse_ext_irq_option(dev, option,
669 &res->data.extended_irq);
672 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
676 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
684 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
686 acpi_handle handle = dev->data;
688 struct acpipnp_parse_option_s parse_data;
690 dev_dbg(&dev->dev, "parse resource options\n");
692 parse_data.option = pnp_register_independent_option(dev);
693 if (!parse_data.option)
696 parse_data.option_independent = parse_data.option;
697 parse_data.dev = dev;
698 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
699 pnpacpi_option_resource, &parse_data);
701 if (ACPI_FAILURE(status)) {
702 if (status != AE_NOT_FOUND)
703 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
709 static int pnpacpi_supported_resource(struct acpi_resource *res)
712 case ACPI_RESOURCE_TYPE_IRQ:
713 case ACPI_RESOURCE_TYPE_DMA:
714 case ACPI_RESOURCE_TYPE_IO:
715 case ACPI_RESOURCE_TYPE_FIXED_IO:
716 case ACPI_RESOURCE_TYPE_MEMORY24:
717 case ACPI_RESOURCE_TYPE_MEMORY32:
718 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
719 case ACPI_RESOURCE_TYPE_ADDRESS16:
720 case ACPI_RESOURCE_TYPE_ADDRESS32:
721 case ACPI_RESOURCE_TYPE_ADDRESS64:
722 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
731 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
736 if (pnpacpi_supported_resource(res))
741 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
743 struct acpi_resource **resource = data;
745 if (pnpacpi_supported_resource(res)) {
746 (*resource)->type = res->type;
747 (*resource)->length = sizeof(struct acpi_resource);
748 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
749 (*resource)->data.irq.descriptor_length =
750 res->data.irq.descriptor_length;
757 int pnpacpi_build_resource_template(struct pnp_dev *dev,
758 struct acpi_buffer *buffer)
760 acpi_handle handle = dev->data;
761 struct acpi_resource *resource;
765 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
766 pnpacpi_count_resources, &res_cnt);
767 if (ACPI_FAILURE(status)) {
768 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
773 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
774 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
775 if (!buffer->pointer)
778 resource = (struct acpi_resource *)buffer->pointer;
779 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
780 pnpacpi_type_resources, &resource);
781 if (ACPI_FAILURE(status)) {
782 kfree(buffer->pointer);
783 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
786 /* resource will pointer the end resource now */
787 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
792 static void pnpacpi_encode_irq(struct pnp_dev *dev,
793 struct acpi_resource *resource,
796 struct acpi_resource_irq *irq = &resource->data.irq;
797 int triggering, polarity, shareable;
799 if (!pnp_resource_enabled(p)) {
800 irq->interrupt_count = 0;
801 dev_dbg(&dev->dev, " encode irq (%s)\n",
802 p ? "disabled" : "missing");
806 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
807 irq->triggering = triggering;
808 irq->polarity = polarity;
809 irq->sharable = shareable;
810 irq->interrupt_count = 1;
811 irq->interrupts[0] = p->start;
813 dev_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
815 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
816 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
817 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
818 irq->descriptor_length);
821 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
822 struct acpi_resource *resource,
825 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
826 int triggering, polarity, shareable;
828 if (!pnp_resource_enabled(p)) {
829 extended_irq->interrupt_count = 0;
830 dev_dbg(&dev->dev, " encode extended irq (%s)\n",
831 p ? "disabled" : "missing");
835 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
836 extended_irq->producer_consumer = ACPI_CONSUMER;
837 extended_irq->triggering = triggering;
838 extended_irq->polarity = polarity;
839 extended_irq->sharable = shareable;
840 extended_irq->interrupt_count = 1;
841 extended_irq->interrupts[0] = p->start;
843 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
844 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
845 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
846 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
849 static void pnpacpi_encode_dma(struct pnp_dev *dev,
850 struct acpi_resource *resource,
853 struct acpi_resource_dma *dma = &resource->data.dma;
855 if (!pnp_resource_enabled(p)) {
856 dma->channel_count = 0;
857 dev_dbg(&dev->dev, " encode dma (%s)\n",
858 p ? "disabled" : "missing");
862 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
863 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
864 case IORESOURCE_DMA_TYPEA:
865 dma->type = ACPI_TYPE_A;
867 case IORESOURCE_DMA_TYPEB:
868 dma->type = ACPI_TYPE_B;
870 case IORESOURCE_DMA_TYPEF:
871 dma->type = ACPI_TYPE_F;
874 dma->type = ACPI_COMPATIBILITY;
877 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
878 case IORESOURCE_DMA_8BIT:
879 dma->transfer = ACPI_TRANSFER_8;
881 case IORESOURCE_DMA_8AND16BIT:
882 dma->transfer = ACPI_TRANSFER_8_16;
885 dma->transfer = ACPI_TRANSFER_16;
888 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
889 dma->channel_count = 1;
890 dma->channels[0] = p->start;
892 dev_dbg(&dev->dev, " encode dma %d "
893 "type %#x transfer %#x master %d\n",
894 (int) p->start, dma->type, dma->transfer, dma->bus_master);
897 static void pnpacpi_encode_io(struct pnp_dev *dev,
898 struct acpi_resource *resource,
901 struct acpi_resource_io *io = &resource->data.io;
903 if (pnp_resource_enabled(p)) {
904 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
905 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
906 ACPI_DECODE_16 : ACPI_DECODE_10;
907 io->minimum = p->start;
908 io->maximum = p->end;
909 io->alignment = 0; /* Correct? */
910 io->address_length = p->end - p->start + 1;
913 io->address_length = 0;
916 dev_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
917 io->minimum + io->address_length - 1, io->io_decode);
920 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
921 struct acpi_resource *resource,
924 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
926 if (pnp_resource_enabled(p)) {
927 fixed_io->address = p->start;
928 fixed_io->address_length = p->end - p->start + 1;
930 fixed_io->address = 0;
931 fixed_io->address_length = 0;
934 dev_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
935 fixed_io->address + fixed_io->address_length - 1);
938 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
939 struct acpi_resource *resource,
942 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
944 if (pnp_resource_enabled(p)) {
945 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
946 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
947 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
948 memory24->minimum = p->start;
949 memory24->maximum = p->end;
950 memory24->alignment = 0;
951 memory24->address_length = p->end - p->start + 1;
953 memory24->minimum = 0;
954 memory24->address_length = 0;
957 dev_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
959 memory24->minimum + memory24->address_length - 1,
960 memory24->write_protect);
963 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
964 struct acpi_resource *resource,
967 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
969 if (pnp_resource_enabled(p)) {
970 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
971 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
972 memory32->minimum = p->start;
973 memory32->maximum = p->end;
974 memory32->alignment = 0;
975 memory32->address_length = p->end - p->start + 1;
977 memory32->minimum = 0;
978 memory32->alignment = 0;
981 dev_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
983 memory32->minimum + memory32->address_length - 1,
984 memory32->write_protect);
987 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
988 struct acpi_resource *resource,
991 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
993 if (pnp_resource_enabled(p)) {
994 fixed_memory32->write_protect =
995 p->flags & IORESOURCE_MEM_WRITEABLE ?
996 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
997 fixed_memory32->address = p->start;
998 fixed_memory32->address_length = p->end - p->start + 1;
1000 fixed_memory32->address = 0;
1001 fixed_memory32->address_length = 0;
1004 dev_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1005 fixed_memory32->address,
1006 fixed_memory32->address + fixed_memory32->address_length - 1,
1007 fixed_memory32->write_protect);
1010 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1013 /* pnpacpi_build_resource_template allocates extra mem */
1014 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1015 struct acpi_resource *resource = buffer->pointer;
1016 int port = 0, irq = 0, dma = 0, mem = 0;
1018 dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1019 while (i < res_cnt) {
1020 switch (resource->type) {
1021 case ACPI_RESOURCE_TYPE_IRQ:
1022 pnpacpi_encode_irq(dev, resource,
1023 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1027 case ACPI_RESOURCE_TYPE_DMA:
1028 pnpacpi_encode_dma(dev, resource,
1029 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1032 case ACPI_RESOURCE_TYPE_IO:
1033 pnpacpi_encode_io(dev, resource,
1034 pnp_get_resource(dev, IORESOURCE_IO, port));
1037 case ACPI_RESOURCE_TYPE_FIXED_IO:
1038 pnpacpi_encode_fixed_io(dev, resource,
1039 pnp_get_resource(dev, IORESOURCE_IO, port));
1042 case ACPI_RESOURCE_TYPE_MEMORY24:
1043 pnpacpi_encode_mem24(dev, resource,
1044 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1047 case ACPI_RESOURCE_TYPE_MEMORY32:
1048 pnpacpi_encode_mem32(dev, resource,
1049 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1052 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1053 pnpacpi_encode_fixed_mem32(dev, resource,
1054 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1057 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1058 pnpacpi_encode_ext_irq(dev, resource,
1059 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1062 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1063 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1064 case ACPI_RESOURCE_TYPE_VENDOR:
1065 case ACPI_RESOURCE_TYPE_END_TAG:
1066 case ACPI_RESOURCE_TYPE_ADDRESS16:
1067 case ACPI_RESOURCE_TYPE_ADDRESS32:
1068 case ACPI_RESOURCE_TYPE_ADDRESS64:
1069 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1070 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1071 default: /* other type */
1072 dev_warn(&dev->dev, "can't encode unknown resource "
1073 "type %d\n", resource->type);