Linux 2.6.34-rc3
[pohmelfs.git] / drivers / pnp / pnpacpi / rsparser.c
blob54514aa35b093f27d11604d4bca986e5b6c53e5c
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>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.h>
24 #include <linux/acpi.h>
25 #include <linux/pci.h>
26 #include <linux/pnp.h>
27 #include "../base.h"
28 #include "pnpacpi.h"
30 #ifdef CONFIG_IA64
31 #define valid_IRQ(i) (1)
32 #else
33 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
34 #endif
37 * Allocated Resources
39 static int irq_flags(int triggering, int polarity, int shareable)
41 int flags;
43 if (triggering == ACPI_LEVEL_SENSITIVE) {
44 if (polarity == ACPI_ACTIVE_LOW)
45 flags = IORESOURCE_IRQ_LOWLEVEL;
46 else
47 flags = IORESOURCE_IRQ_HIGHLEVEL;
48 } else {
49 if (polarity == ACPI_ACTIVE_LOW)
50 flags = IORESOURCE_IRQ_LOWEDGE;
51 else
52 flags = IORESOURCE_IRQ_HIGHEDGE;
55 if (shareable == ACPI_SHARED)
56 flags |= IORESOURCE_IRQ_SHAREABLE;
58 return flags;
61 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
62 int *polarity, int *shareable)
64 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
65 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
66 case IORESOURCE_IRQ_LOWLEVEL:
67 *triggering = ACPI_LEVEL_SENSITIVE;
68 *polarity = ACPI_ACTIVE_LOW;
69 break;
70 case IORESOURCE_IRQ_HIGHLEVEL:
71 *triggering = ACPI_LEVEL_SENSITIVE;
72 *polarity = ACPI_ACTIVE_HIGH;
73 break;
74 case IORESOURCE_IRQ_LOWEDGE:
75 *triggering = ACPI_EDGE_SENSITIVE;
76 *polarity = ACPI_ACTIVE_LOW;
77 break;
78 case IORESOURCE_IRQ_HIGHEDGE:
79 *triggering = ACPI_EDGE_SENSITIVE;
80 *polarity = ACPI_ACTIVE_HIGH;
81 break;
82 default:
83 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
84 flags);
85 *triggering = ACPI_EDGE_SENSITIVE;
86 *polarity = ACPI_ACTIVE_HIGH;
87 break;
90 if (flags & IORESOURCE_IRQ_SHAREABLE)
91 *shareable = ACPI_SHARED;
92 else
93 *shareable = ACPI_EXCLUSIVE;
96 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
97 u32 gsi, int triggering,
98 int polarity, int shareable)
100 int irq, flags;
101 int p, t;
103 if (!valid_IRQ(gsi)) {
104 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
105 return;
109 * in IO-APIC mode, use overrided attribute. Two reasons:
110 * 1. BIOS bug in DSDT
111 * 2. BIOS uses IO-APIC mode Interrupt Source Override
113 if (!acpi_get_override_irq(gsi, &t, &p)) {
114 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
115 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
117 if (triggering != t || polarity != p) {
118 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
119 gsi, t ? "edge":"level", p ? "low":"high");
120 triggering = t;
121 polarity = p;
125 flags = irq_flags(triggering, polarity, shareable);
126 irq = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
127 if (irq >= 0)
128 pcibios_penalize_isa_irq(irq, 1);
129 else
130 flags |= IORESOURCE_DISABLED;
132 pnp_add_irq_resource(dev, irq, flags);
135 static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
136 int transfer)
138 int flags = 0;
140 if (bus_master)
141 flags |= IORESOURCE_DMA_MASTER;
142 switch (type) {
143 case ACPI_COMPATIBILITY:
144 flags |= IORESOURCE_DMA_COMPATIBLE;
145 break;
146 case ACPI_TYPE_A:
147 flags |= IORESOURCE_DMA_TYPEA;
148 break;
149 case ACPI_TYPE_B:
150 flags |= IORESOURCE_DMA_TYPEB;
151 break;
152 case ACPI_TYPE_F:
153 flags |= IORESOURCE_DMA_TYPEF;
154 break;
155 default:
156 /* Set a default value ? */
157 flags |= IORESOURCE_DMA_COMPATIBLE;
158 dev_err(&dev->dev, "invalid DMA type %d\n", type);
160 switch (transfer) {
161 case ACPI_TRANSFER_8:
162 flags |= IORESOURCE_DMA_8BIT;
163 break;
164 case ACPI_TRANSFER_8_16:
165 flags |= IORESOURCE_DMA_8AND16BIT;
166 break;
167 case ACPI_TRANSFER_16:
168 flags |= IORESOURCE_DMA_16BIT;
169 break;
170 default:
171 /* Set a default value ? */
172 flags |= IORESOURCE_DMA_8AND16BIT;
173 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
176 return flags;
179 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
180 u64 len, int io_decode,
181 int window)
183 int flags = 0;
184 u64 end = start + len - 1;
186 if (io_decode == ACPI_DECODE_16)
187 flags |= IORESOURCE_IO_16BIT_ADDR;
188 if (len == 0 || end >= 0x10003)
189 flags |= IORESOURCE_DISABLED;
190 if (window)
191 flags |= IORESOURCE_WINDOW;
193 pnp_add_io_resource(dev, start, end, flags);
197 * Device CSRs that do not appear in PCI config space should be described
198 * via ACPI. This would normally be done with Address Space Descriptors
199 * marked as "consumer-only," but old versions of Windows and Linux ignore
200 * the producer/consumer flag, so HP invented a vendor-defined resource to
201 * describe the location and size of CSR space.
203 static struct acpi_vendor_uuid hp_ccsr_uuid = {
204 .subtype = 2,
205 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
206 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
209 static int vendor_resource_matches(struct pnp_dev *dev,
210 struct acpi_resource_vendor_typed *vendor,
211 struct acpi_vendor_uuid *match,
212 int expected_len)
214 int uuid_len = sizeof(vendor->uuid);
215 u8 uuid_subtype = vendor->uuid_subtype;
216 u8 *uuid = vendor->uuid;
217 int actual_len;
219 /* byte_length includes uuid_subtype and uuid */
220 actual_len = vendor->byte_length - uuid_len - 1;
222 if (uuid_subtype == match->subtype &&
223 uuid_len == sizeof(match->data) &&
224 memcmp(uuid, match->data, uuid_len) == 0) {
225 if (expected_len && expected_len != actual_len) {
226 dev_err(&dev->dev, "wrong vendor descriptor size; "
227 "expected %d, found %d bytes\n",
228 expected_len, actual_len);
229 return 0;
232 return 1;
235 return 0;
238 static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
239 struct acpi_resource_vendor_typed *vendor)
241 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
242 u64 start, length;
244 memcpy(&start, vendor->byte_data, sizeof(start));
245 memcpy(&length, vendor->byte_data + 8, sizeof(length));
247 pnp_add_mem_resource(dev, start, start + length - 1, 0);
251 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
252 u64 start, u64 len,
253 int write_protect, int window)
255 int flags = 0;
256 u64 end = start + len - 1;
258 if (len == 0)
259 flags |= IORESOURCE_DISABLED;
260 if (write_protect == ACPI_READ_WRITE_MEMORY)
261 flags |= IORESOURCE_MEM_WRITEABLE;
262 if (window)
263 flags |= IORESOURCE_WINDOW;
265 pnp_add_mem_resource(dev, start, end, flags);
268 static void pnpacpi_parse_allocated_busresource(struct pnp_dev *dev,
269 u64 start, u64 len)
271 u64 end = start + len - 1;
273 pnp_add_bus_resource(dev, start, end);
276 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
277 struct acpi_resource *res)
279 struct acpi_resource_address64 addr, *p = &addr;
280 acpi_status status;
281 int window;
283 status = acpi_resource_to_address64(res, p);
284 if (!ACPI_SUCCESS(status)) {
285 dev_warn(&dev->dev, "failed to convert resource type %d\n",
286 res->type);
287 return;
290 window = (p->producer_consumer == ACPI_PRODUCER) ? 1 : 0;
292 if (p->resource_type == ACPI_MEMORY_RANGE)
293 pnpacpi_parse_allocated_memresource(dev,
294 p->minimum, p->address_length,
295 p->info.mem.write_protect, window);
296 else if (p->resource_type == ACPI_IO_RANGE)
297 pnpacpi_parse_allocated_ioresource(dev,
298 p->minimum, p->address_length,
299 p->granularity == 0xfff ? ACPI_DECODE_10 :
300 ACPI_DECODE_16, window);
301 else if (p->resource_type == ACPI_BUS_NUMBER_RANGE)
302 pnpacpi_parse_allocated_busresource(dev, p->minimum,
303 p->address_length);
306 static void pnpacpi_parse_allocated_ext_address_space(struct pnp_dev *dev,
307 struct acpi_resource *res)
309 struct acpi_resource_extended_address64 *p = &res->data.ext_address64;
310 int window;
312 window = (p->producer_consumer == ACPI_PRODUCER) ? 1 : 0;
314 if (p->resource_type == ACPI_MEMORY_RANGE)
315 pnpacpi_parse_allocated_memresource(dev,
316 p->minimum, p->address_length,
317 p->info.mem.write_protect, window);
318 else if (p->resource_type == ACPI_IO_RANGE)
319 pnpacpi_parse_allocated_ioresource(dev,
320 p->minimum, p->address_length,
321 p->granularity == 0xfff ? ACPI_DECODE_10 :
322 ACPI_DECODE_16, window);
323 else if (p->resource_type == ACPI_BUS_NUMBER_RANGE)
324 pnpacpi_parse_allocated_busresource(dev, p->minimum,
325 p->address_length);
328 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
329 void *data)
331 struct pnp_dev *dev = data;
332 struct acpi_resource_irq *irq;
333 struct acpi_resource_dma *dma;
334 struct acpi_resource_io *io;
335 struct acpi_resource_fixed_io *fixed_io;
336 struct acpi_resource_vendor_typed *vendor_typed;
337 struct acpi_resource_memory24 *memory24;
338 struct acpi_resource_memory32 *memory32;
339 struct acpi_resource_fixed_memory32 *fixed_memory32;
340 struct acpi_resource_extended_irq *extended_irq;
341 int i, flags;
343 switch (res->type) {
344 case ACPI_RESOURCE_TYPE_IRQ:
346 * Per spec, only one interrupt per descriptor is allowed in
347 * _CRS, but some firmware violates this, so parse them all.
349 irq = &res->data.irq;
350 if (irq->interrupt_count == 0)
351 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
352 else {
353 for (i = 0; i < irq->interrupt_count; i++) {
354 pnpacpi_parse_allocated_irqresource(dev,
355 irq->interrupts[i],
356 irq->triggering,
357 irq->polarity,
358 irq->sharable);
362 * The IRQ encoder puts a single interrupt in each
363 * descriptor, so if a _CRS descriptor has more than
364 * one interrupt, we won't be able to re-encode it.
366 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
367 dev_warn(&dev->dev, "multiple interrupts in "
368 "_CRS descriptor; configuration can't "
369 "be changed\n");
370 dev->capabilities &= ~PNP_WRITE;
373 break;
375 case ACPI_RESOURCE_TYPE_DMA:
376 dma = &res->data.dma;
377 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
378 flags = dma_flags(dev, dma->type, dma->bus_master,
379 dma->transfer);
380 else
381 flags = IORESOURCE_DISABLED;
382 pnp_add_dma_resource(dev, dma->channels[0], flags);
383 break;
385 case ACPI_RESOURCE_TYPE_IO:
386 io = &res->data.io;
387 pnpacpi_parse_allocated_ioresource(dev,
388 io->minimum,
389 io->address_length,
390 io->io_decode, 0);
391 break;
393 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
394 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
395 break;
397 case ACPI_RESOURCE_TYPE_FIXED_IO:
398 fixed_io = &res->data.fixed_io;
399 pnpacpi_parse_allocated_ioresource(dev,
400 fixed_io->address,
401 fixed_io->address_length,
402 ACPI_DECODE_10, 0);
403 break;
405 case ACPI_RESOURCE_TYPE_VENDOR:
406 vendor_typed = &res->data.vendor_typed;
407 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
408 break;
410 case ACPI_RESOURCE_TYPE_END_TAG:
411 break;
413 case ACPI_RESOURCE_TYPE_MEMORY24:
414 memory24 = &res->data.memory24;
415 pnpacpi_parse_allocated_memresource(dev,
416 memory24->minimum,
417 memory24->address_length,
418 memory24->write_protect, 0);
419 break;
420 case ACPI_RESOURCE_TYPE_MEMORY32:
421 memory32 = &res->data.memory32;
422 pnpacpi_parse_allocated_memresource(dev,
423 memory32->minimum,
424 memory32->address_length,
425 memory32->write_protect, 0);
426 break;
427 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
428 fixed_memory32 = &res->data.fixed_memory32;
429 pnpacpi_parse_allocated_memresource(dev,
430 fixed_memory32->address,
431 fixed_memory32->address_length,
432 fixed_memory32->write_protect, 0);
433 break;
434 case ACPI_RESOURCE_TYPE_ADDRESS16:
435 case ACPI_RESOURCE_TYPE_ADDRESS32:
436 case ACPI_RESOURCE_TYPE_ADDRESS64:
437 pnpacpi_parse_allocated_address_space(dev, res);
438 break;
440 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
441 pnpacpi_parse_allocated_ext_address_space(dev, res);
442 break;
444 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
445 extended_irq = &res->data.extended_irq;
447 if (extended_irq->interrupt_count == 0)
448 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
449 else {
450 for (i = 0; i < extended_irq->interrupt_count; i++) {
451 pnpacpi_parse_allocated_irqresource(dev,
452 extended_irq->interrupts[i],
453 extended_irq->triggering,
454 extended_irq->polarity,
455 extended_irq->sharable);
459 * The IRQ encoder puts a single interrupt in each
460 * descriptor, so if a _CRS descriptor has more than
461 * one interrupt, we won't be able to re-encode it.
463 if (pnp_can_write(dev) &&
464 extended_irq->interrupt_count > 1) {
465 dev_warn(&dev->dev, "multiple interrupts in "
466 "_CRS descriptor; configuration can't "
467 "be changed\n");
468 dev->capabilities &= ~PNP_WRITE;
471 break;
473 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
474 break;
476 default:
477 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
478 res->type);
479 return AE_ERROR;
482 return AE_OK;
485 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
487 struct acpi_device *acpi_dev = dev->data;
488 acpi_handle handle = acpi_dev->handle;
489 acpi_status status;
491 pnp_dbg(&dev->dev, "parse allocated resources\n");
493 pnp_init_resources(dev);
495 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
496 pnpacpi_allocated_resource, dev);
498 if (ACPI_FAILURE(status)) {
499 if (status != AE_NOT_FOUND)
500 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
501 return -EPERM;
503 return 0;
506 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
507 unsigned int option_flags,
508 struct acpi_resource_dma *p)
510 int i;
511 unsigned char map = 0, flags;
513 if (p->channel_count == 0)
514 return;
516 for (i = 0; i < p->channel_count; i++)
517 map |= 1 << p->channels[i];
519 flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
520 pnp_register_dma_resource(dev, option_flags, map, flags);
523 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
524 unsigned int option_flags,
525 struct acpi_resource_irq *p)
527 int i;
528 pnp_irq_mask_t map;
529 unsigned char flags;
531 if (p->interrupt_count == 0)
532 return;
534 bitmap_zero(map.bits, PNP_IRQ_NR);
535 for (i = 0; i < p->interrupt_count; i++)
536 if (p->interrupts[i])
537 __set_bit(p->interrupts[i], map.bits);
539 flags = irq_flags(p->triggering, p->polarity, p->sharable);
540 pnp_register_irq_resource(dev, option_flags, &map, flags);
543 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
544 unsigned int option_flags,
545 struct acpi_resource_extended_irq *p)
547 int i;
548 pnp_irq_mask_t map;
549 unsigned char flags;
551 if (p->interrupt_count == 0)
552 return;
554 bitmap_zero(map.bits, PNP_IRQ_NR);
555 for (i = 0; i < p->interrupt_count; i++) {
556 if (p->interrupts[i]) {
557 if (p->interrupts[i] < PNP_IRQ_NR)
558 __set_bit(p->interrupts[i], map.bits);
559 else
560 dev_err(&dev->dev, "ignoring IRQ %d option "
561 "(too large for %d entry bitmap)\n",
562 p->interrupts[i], PNP_IRQ_NR);
566 flags = irq_flags(p->triggering, p->polarity, p->sharable);
567 pnp_register_irq_resource(dev, option_flags, &map, flags);
570 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
571 unsigned int option_flags,
572 struct acpi_resource_io *io)
574 unsigned char flags = 0;
576 if (io->address_length == 0)
577 return;
579 if (io->io_decode == ACPI_DECODE_16)
580 flags = IORESOURCE_IO_16BIT_ADDR;
581 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
582 io->alignment, io->address_length, flags);
585 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
586 unsigned int option_flags,
587 struct acpi_resource_fixed_io *io)
589 if (io->address_length == 0)
590 return;
592 pnp_register_port_resource(dev, option_flags, io->address, io->address,
593 0, io->address_length, IORESOURCE_IO_FIXED);
596 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
597 unsigned int option_flags,
598 struct acpi_resource_memory24 *p)
600 unsigned char flags = 0;
602 if (p->address_length == 0)
603 return;
605 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
606 flags = IORESOURCE_MEM_WRITEABLE;
607 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
608 p->alignment, p->address_length, flags);
611 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
612 unsigned int option_flags,
613 struct acpi_resource_memory32 *p)
615 unsigned char flags = 0;
617 if (p->address_length == 0)
618 return;
620 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
621 flags = IORESOURCE_MEM_WRITEABLE;
622 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
623 p->alignment, p->address_length, flags);
626 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
627 unsigned int option_flags,
628 struct acpi_resource_fixed_memory32 *p)
630 unsigned char flags = 0;
632 if (p->address_length == 0)
633 return;
635 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
636 flags = IORESOURCE_MEM_WRITEABLE;
637 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
638 0, p->address_length, flags);
641 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
642 unsigned int option_flags,
643 struct acpi_resource *r)
645 struct acpi_resource_address64 addr, *p = &addr;
646 acpi_status status;
647 unsigned char flags = 0;
649 status = acpi_resource_to_address64(r, p);
650 if (ACPI_FAILURE(status)) {
651 dev_warn(&dev->dev, "can't convert resource type %d\n",
652 r->type);
653 return;
656 if (p->address_length == 0)
657 return;
659 if (p->resource_type == ACPI_MEMORY_RANGE) {
660 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
661 flags = IORESOURCE_MEM_WRITEABLE;
662 pnp_register_mem_resource(dev, option_flags, p->minimum,
663 p->minimum, 0, p->address_length,
664 flags);
665 } else if (p->resource_type == ACPI_IO_RANGE)
666 pnp_register_port_resource(dev, option_flags, p->minimum,
667 p->minimum, 0, p->address_length,
668 IORESOURCE_IO_FIXED);
671 static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
672 unsigned int option_flags,
673 struct acpi_resource *r)
675 struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
676 unsigned char flags = 0;
678 if (p->address_length == 0)
679 return;
681 if (p->resource_type == ACPI_MEMORY_RANGE) {
682 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
683 flags = IORESOURCE_MEM_WRITEABLE;
684 pnp_register_mem_resource(dev, option_flags, p->minimum,
685 p->minimum, 0, p->address_length,
686 flags);
687 } else if (p->resource_type == ACPI_IO_RANGE)
688 pnp_register_port_resource(dev, option_flags, p->minimum,
689 p->minimum, 0, p->address_length,
690 IORESOURCE_IO_FIXED);
693 struct acpipnp_parse_option_s {
694 struct pnp_dev *dev;
695 unsigned int option_flags;
698 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
699 void *data)
701 int priority;
702 struct acpipnp_parse_option_s *parse_data = data;
703 struct pnp_dev *dev = parse_data->dev;
704 unsigned int option_flags = parse_data->option_flags;
706 switch (res->type) {
707 case ACPI_RESOURCE_TYPE_IRQ:
708 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
709 break;
711 case ACPI_RESOURCE_TYPE_DMA:
712 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
713 break;
715 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
716 switch (res->data.start_dpf.compatibility_priority) {
717 case ACPI_GOOD_CONFIGURATION:
718 priority = PNP_RES_PRIORITY_PREFERRED;
719 break;
721 case ACPI_ACCEPTABLE_CONFIGURATION:
722 priority = PNP_RES_PRIORITY_ACCEPTABLE;
723 break;
725 case ACPI_SUB_OPTIMAL_CONFIGURATION:
726 priority = PNP_RES_PRIORITY_FUNCTIONAL;
727 break;
728 default:
729 priority = PNP_RES_PRIORITY_INVALID;
730 break;
732 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
733 break;
735 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
736 parse_data->option_flags = 0;
737 break;
739 case ACPI_RESOURCE_TYPE_IO:
740 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
741 break;
743 case ACPI_RESOURCE_TYPE_FIXED_IO:
744 pnpacpi_parse_fixed_port_option(dev, option_flags,
745 &res->data.fixed_io);
746 break;
748 case ACPI_RESOURCE_TYPE_VENDOR:
749 case ACPI_RESOURCE_TYPE_END_TAG:
750 break;
752 case ACPI_RESOURCE_TYPE_MEMORY24:
753 pnpacpi_parse_mem24_option(dev, option_flags,
754 &res->data.memory24);
755 break;
757 case ACPI_RESOURCE_TYPE_MEMORY32:
758 pnpacpi_parse_mem32_option(dev, option_flags,
759 &res->data.memory32);
760 break;
762 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
763 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
764 &res->data.fixed_memory32);
765 break;
767 case ACPI_RESOURCE_TYPE_ADDRESS16:
768 case ACPI_RESOURCE_TYPE_ADDRESS32:
769 case ACPI_RESOURCE_TYPE_ADDRESS64:
770 pnpacpi_parse_address_option(dev, option_flags, res);
771 break;
773 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
774 pnpacpi_parse_ext_address_option(dev, option_flags, res);
775 break;
777 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
778 pnpacpi_parse_ext_irq_option(dev, option_flags,
779 &res->data.extended_irq);
780 break;
782 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
783 break;
785 default:
786 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
787 res->type);
788 return AE_ERROR;
791 return AE_OK;
794 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
796 struct acpi_device *acpi_dev = dev->data;
797 acpi_handle handle = acpi_dev->handle;
798 acpi_status status;
799 struct acpipnp_parse_option_s parse_data;
801 pnp_dbg(&dev->dev, "parse resource options\n");
803 parse_data.dev = dev;
804 parse_data.option_flags = 0;
806 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
807 pnpacpi_option_resource, &parse_data);
809 if (ACPI_FAILURE(status)) {
810 if (status != AE_NOT_FOUND)
811 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
812 return -EPERM;
814 return 0;
817 static int pnpacpi_supported_resource(struct acpi_resource *res)
819 switch (res->type) {
820 case ACPI_RESOURCE_TYPE_IRQ:
821 case ACPI_RESOURCE_TYPE_DMA:
822 case ACPI_RESOURCE_TYPE_IO:
823 case ACPI_RESOURCE_TYPE_FIXED_IO:
824 case ACPI_RESOURCE_TYPE_MEMORY24:
825 case ACPI_RESOURCE_TYPE_MEMORY32:
826 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
827 case ACPI_RESOURCE_TYPE_ADDRESS16:
828 case ACPI_RESOURCE_TYPE_ADDRESS32:
829 case ACPI_RESOURCE_TYPE_ADDRESS64:
830 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
831 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
832 return 1;
834 return 0;
838 * Set resource
840 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
841 void *data)
843 int *res_cnt = data;
845 if (pnpacpi_supported_resource(res))
846 (*res_cnt)++;
847 return AE_OK;
850 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
852 struct acpi_resource **resource = data;
854 if (pnpacpi_supported_resource(res)) {
855 (*resource)->type = res->type;
856 (*resource)->length = sizeof(struct acpi_resource);
857 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
858 (*resource)->data.irq.descriptor_length =
859 res->data.irq.descriptor_length;
860 (*resource)++;
863 return AE_OK;
866 int pnpacpi_build_resource_template(struct pnp_dev *dev,
867 struct acpi_buffer *buffer)
869 struct acpi_device *acpi_dev = dev->data;
870 acpi_handle handle = acpi_dev->handle;
871 struct acpi_resource *resource;
872 int res_cnt = 0;
873 acpi_status status;
875 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
876 pnpacpi_count_resources, &res_cnt);
877 if (ACPI_FAILURE(status)) {
878 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
879 return -EINVAL;
881 if (!res_cnt)
882 return -EINVAL;
883 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
884 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
885 if (!buffer->pointer)
886 return -ENOMEM;
888 resource = (struct acpi_resource *)buffer->pointer;
889 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
890 pnpacpi_type_resources, &resource);
891 if (ACPI_FAILURE(status)) {
892 kfree(buffer->pointer);
893 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
894 return -EINVAL;
896 /* resource will pointer the end resource now */
897 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
899 return 0;
902 static void pnpacpi_encode_irq(struct pnp_dev *dev,
903 struct acpi_resource *resource,
904 struct resource *p)
906 struct acpi_resource_irq *irq = &resource->data.irq;
907 int triggering, polarity, shareable;
909 if (!pnp_resource_enabled(p)) {
910 irq->interrupt_count = 0;
911 pnp_dbg(&dev->dev, " encode irq (%s)\n",
912 p ? "disabled" : "missing");
913 return;
916 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
917 irq->triggering = triggering;
918 irq->polarity = polarity;
919 irq->sharable = shareable;
920 irq->interrupt_count = 1;
921 irq->interrupts[0] = p->start;
923 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
924 (int) p->start,
925 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
926 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
927 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
928 irq->descriptor_length);
931 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
932 struct acpi_resource *resource,
933 struct resource *p)
935 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
936 int triggering, polarity, shareable;
938 if (!pnp_resource_enabled(p)) {
939 extended_irq->interrupt_count = 0;
940 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
941 p ? "disabled" : "missing");
942 return;
945 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
946 extended_irq->producer_consumer = ACPI_CONSUMER;
947 extended_irq->triggering = triggering;
948 extended_irq->polarity = polarity;
949 extended_irq->sharable = shareable;
950 extended_irq->interrupt_count = 1;
951 extended_irq->interrupts[0] = p->start;
953 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
954 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
955 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
956 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
959 static void pnpacpi_encode_dma(struct pnp_dev *dev,
960 struct acpi_resource *resource,
961 struct resource *p)
963 struct acpi_resource_dma *dma = &resource->data.dma;
965 if (!pnp_resource_enabled(p)) {
966 dma->channel_count = 0;
967 pnp_dbg(&dev->dev, " encode dma (%s)\n",
968 p ? "disabled" : "missing");
969 return;
972 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
973 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
974 case IORESOURCE_DMA_TYPEA:
975 dma->type = ACPI_TYPE_A;
976 break;
977 case IORESOURCE_DMA_TYPEB:
978 dma->type = ACPI_TYPE_B;
979 break;
980 case IORESOURCE_DMA_TYPEF:
981 dma->type = ACPI_TYPE_F;
982 break;
983 default:
984 dma->type = ACPI_COMPATIBILITY;
987 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
988 case IORESOURCE_DMA_8BIT:
989 dma->transfer = ACPI_TRANSFER_8;
990 break;
991 case IORESOURCE_DMA_8AND16BIT:
992 dma->transfer = ACPI_TRANSFER_8_16;
993 break;
994 default:
995 dma->transfer = ACPI_TRANSFER_16;
998 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
999 dma->channel_count = 1;
1000 dma->channels[0] = p->start;
1002 pnp_dbg(&dev->dev, " encode dma %d "
1003 "type %#x transfer %#x master %d\n",
1004 (int) p->start, dma->type, dma->transfer, dma->bus_master);
1007 static void pnpacpi_encode_io(struct pnp_dev *dev,
1008 struct acpi_resource *resource,
1009 struct resource *p)
1011 struct acpi_resource_io *io = &resource->data.io;
1013 if (pnp_resource_enabled(p)) {
1014 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
1015 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
1016 ACPI_DECODE_16 : ACPI_DECODE_10;
1017 io->minimum = p->start;
1018 io->maximum = p->end;
1019 io->alignment = 0; /* Correct? */
1020 io->address_length = p->end - p->start + 1;
1021 } else {
1022 io->minimum = 0;
1023 io->address_length = 0;
1026 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
1027 io->minimum + io->address_length - 1, io->io_decode);
1030 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
1031 struct acpi_resource *resource,
1032 struct resource *p)
1034 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
1036 if (pnp_resource_enabled(p)) {
1037 fixed_io->address = p->start;
1038 fixed_io->address_length = p->end - p->start + 1;
1039 } else {
1040 fixed_io->address = 0;
1041 fixed_io->address_length = 0;
1044 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
1045 fixed_io->address + fixed_io->address_length - 1);
1048 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
1049 struct acpi_resource *resource,
1050 struct resource *p)
1052 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
1054 if (pnp_resource_enabled(p)) {
1055 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
1056 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1057 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1058 memory24->minimum = p->start;
1059 memory24->maximum = p->end;
1060 memory24->alignment = 0;
1061 memory24->address_length = p->end - p->start + 1;
1062 } else {
1063 memory24->minimum = 0;
1064 memory24->address_length = 0;
1067 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1068 memory24->minimum,
1069 memory24->minimum + memory24->address_length - 1,
1070 memory24->write_protect);
1073 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1074 struct acpi_resource *resource,
1075 struct resource *p)
1077 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1079 if (pnp_resource_enabled(p)) {
1080 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1081 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1082 memory32->minimum = p->start;
1083 memory32->maximum = p->end;
1084 memory32->alignment = 0;
1085 memory32->address_length = p->end - p->start + 1;
1086 } else {
1087 memory32->minimum = 0;
1088 memory32->alignment = 0;
1091 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
1092 memory32->minimum,
1093 memory32->minimum + memory32->address_length - 1,
1094 memory32->write_protect);
1097 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1098 struct acpi_resource *resource,
1099 struct resource *p)
1101 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1103 if (pnp_resource_enabled(p)) {
1104 fixed_memory32->write_protect =
1105 p->flags & IORESOURCE_MEM_WRITEABLE ?
1106 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1107 fixed_memory32->address = p->start;
1108 fixed_memory32->address_length = p->end - p->start + 1;
1109 } else {
1110 fixed_memory32->address = 0;
1111 fixed_memory32->address_length = 0;
1114 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1115 fixed_memory32->address,
1116 fixed_memory32->address + fixed_memory32->address_length - 1,
1117 fixed_memory32->write_protect);
1120 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1122 int i = 0;
1123 /* pnpacpi_build_resource_template allocates extra mem */
1124 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1125 struct acpi_resource *resource = buffer->pointer;
1126 int port = 0, irq = 0, dma = 0, mem = 0;
1128 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1129 while (i < res_cnt) {
1130 switch (resource->type) {
1131 case ACPI_RESOURCE_TYPE_IRQ:
1132 pnpacpi_encode_irq(dev, resource,
1133 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1134 irq++;
1135 break;
1137 case ACPI_RESOURCE_TYPE_DMA:
1138 pnpacpi_encode_dma(dev, resource,
1139 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1140 dma++;
1141 break;
1142 case ACPI_RESOURCE_TYPE_IO:
1143 pnpacpi_encode_io(dev, resource,
1144 pnp_get_resource(dev, IORESOURCE_IO, port));
1145 port++;
1146 break;
1147 case ACPI_RESOURCE_TYPE_FIXED_IO:
1148 pnpacpi_encode_fixed_io(dev, resource,
1149 pnp_get_resource(dev, IORESOURCE_IO, port));
1150 port++;
1151 break;
1152 case ACPI_RESOURCE_TYPE_MEMORY24:
1153 pnpacpi_encode_mem24(dev, resource,
1154 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1155 mem++;
1156 break;
1157 case ACPI_RESOURCE_TYPE_MEMORY32:
1158 pnpacpi_encode_mem32(dev, resource,
1159 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1160 mem++;
1161 break;
1162 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1163 pnpacpi_encode_fixed_mem32(dev, resource,
1164 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1165 mem++;
1166 break;
1167 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1168 pnpacpi_encode_ext_irq(dev, resource,
1169 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1170 irq++;
1171 break;
1172 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1173 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1174 case ACPI_RESOURCE_TYPE_VENDOR:
1175 case ACPI_RESOURCE_TYPE_END_TAG:
1176 case ACPI_RESOURCE_TYPE_ADDRESS16:
1177 case ACPI_RESOURCE_TYPE_ADDRESS32:
1178 case ACPI_RESOURCE_TYPE_ADDRESS64:
1179 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1180 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1181 default: /* other type */
1182 dev_warn(&dev->dev, "can't encode unknown resource "
1183 "type %d\n", resource->type);
1184 return -EINVAL;
1186 resource++;
1187 i++;
1189 return 0;