Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / pnp / resource.c
blob2e54e6a23c72c8ff44b610412e3c1c92baac81b8
1 /*
2 * resource.c - Contains functions for registering and analyzing resource information
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
8 */
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <asm/io.h>
16 #include <asm/dma.h>
17 #include <asm/irq.h>
18 #include <linux/pci.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
22 #include <linux/pnp.h>
23 #include "base.h"
25 static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
26 static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
27 static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
28 static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
31 * option registration
34 struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
35 unsigned int option_flags)
37 struct pnp_option *option;
39 option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
40 if (!option)
41 return NULL;
43 option->flags = option_flags;
44 option->type = type;
46 list_add_tail(&option->list, &dev->options);
47 return option;
50 int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
51 pnp_irq_mask_t *map, unsigned char flags)
53 struct pnp_option *option;
54 struct pnp_irq *irq;
56 option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
57 if (!option)
58 return -ENOMEM;
60 irq = &option->u.irq;
61 irq->map = *map;
62 irq->flags = flags;
64 #ifdef CONFIG_PCI
66 int i;
68 for (i = 0; i < 16; i++)
69 if (test_bit(i, irq->map.bits))
70 pcibios_penalize_isa_irq(i, 0);
72 #endif
74 dbg_pnp_show_option(dev, option);
75 return 0;
78 int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
79 unsigned char map, unsigned char flags)
81 struct pnp_option *option;
82 struct pnp_dma *dma;
84 option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
85 if (!option)
86 return -ENOMEM;
88 dma = &option->u.dma;
89 dma->map = map;
90 dma->flags = flags;
92 dbg_pnp_show_option(dev, option);
93 return 0;
96 int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
97 resource_size_t min, resource_size_t max,
98 resource_size_t align, resource_size_t size,
99 unsigned char flags)
101 struct pnp_option *option;
102 struct pnp_port *port;
104 option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
105 if (!option)
106 return -ENOMEM;
108 port = &option->u.port;
109 port->min = min;
110 port->max = max;
111 port->align = align;
112 port->size = size;
113 port->flags = flags;
115 dbg_pnp_show_option(dev, option);
116 return 0;
119 int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
120 resource_size_t min, resource_size_t max,
121 resource_size_t align, resource_size_t size,
122 unsigned char flags)
124 struct pnp_option *option;
125 struct pnp_mem *mem;
127 option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
128 if (!option)
129 return -ENOMEM;
131 mem = &option->u.mem;
132 mem->min = min;
133 mem->max = max;
134 mem->align = align;
135 mem->size = size;
136 mem->flags = flags;
138 dbg_pnp_show_option(dev, option);
139 return 0;
142 void pnp_free_options(struct pnp_dev *dev)
144 struct pnp_option *option, *tmp;
146 list_for_each_entry_safe(option, tmp, &dev->options, list) {
147 list_del(&option->list);
148 kfree(option);
153 * resource validity checking
156 #define length(start, end) (*(end) - *(start) + 1)
158 /* Two ranges conflict if one doesn't end before the other starts */
159 #define ranged_conflict(starta, enda, startb, endb) \
160 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
162 #define cannot_compare(flags) \
163 ((flags) & IORESOURCE_DISABLED)
165 int pnp_check_port(struct pnp_dev *dev, struct resource *res)
167 int i;
168 struct pnp_dev *tdev;
169 struct resource *tres;
170 resource_size_t *port, *end, *tport, *tend;
172 port = &res->start;
173 end = &res->end;
175 /* if the resource doesn't exist, don't complain about it */
176 if (cannot_compare(res->flags))
177 return 1;
179 /* check if the resource is already in use, skip if the
180 * device is active because it itself may be in use */
181 if (!dev->active) {
182 if (__check_region(&ioport_resource, *port, length(port, end)))
183 return 0;
186 /* check if the resource is reserved */
187 for (i = 0; i < 8; i++) {
188 int rport = pnp_reserve_io[i << 1];
189 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
190 if (ranged_conflict(port, end, &rport, &rend))
191 return 0;
194 /* check for internal conflicts */
195 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
196 if (tres != res && tres->flags & IORESOURCE_IO) {
197 tport = &tres->start;
198 tend = &tres->end;
199 if (ranged_conflict(port, end, tport, tend))
200 return 0;
204 /* check for conflicts with other pnp devices */
205 pnp_for_each_dev(tdev) {
206 if (tdev == dev)
207 continue;
208 for (i = 0;
209 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
210 i++) {
211 if (tres->flags & IORESOURCE_IO) {
212 if (cannot_compare(tres->flags))
213 continue;
214 tport = &tres->start;
215 tend = &tres->end;
216 if (ranged_conflict(port, end, tport, tend))
217 return 0;
222 return 1;
225 int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
227 int i;
228 struct pnp_dev *tdev;
229 struct resource *tres;
230 resource_size_t *addr, *end, *taddr, *tend;
232 addr = &res->start;
233 end = &res->end;
235 /* if the resource doesn't exist, don't complain about it */
236 if (cannot_compare(res->flags))
237 return 1;
239 /* check if the resource is already in use, skip if the
240 * device is active because it itself may be in use */
241 if (!dev->active) {
242 if (check_mem_region(*addr, length(addr, end)))
243 return 0;
246 /* check if the resource is reserved */
247 for (i = 0; i < 8; i++) {
248 int raddr = pnp_reserve_mem[i << 1];
249 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
250 if (ranged_conflict(addr, end, &raddr, &rend))
251 return 0;
254 /* check for internal conflicts */
255 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
256 if (tres != res && tres->flags & IORESOURCE_MEM) {
257 taddr = &tres->start;
258 tend = &tres->end;
259 if (ranged_conflict(addr, end, taddr, tend))
260 return 0;
264 /* check for conflicts with other pnp devices */
265 pnp_for_each_dev(tdev) {
266 if (tdev == dev)
267 continue;
268 for (i = 0;
269 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
270 i++) {
271 if (tres->flags & IORESOURCE_MEM) {
272 if (cannot_compare(tres->flags))
273 continue;
274 taddr = &tres->start;
275 tend = &tres->end;
276 if (ranged_conflict(addr, end, taddr, tend))
277 return 0;
282 return 1;
285 static irqreturn_t pnp_test_handler(int irq, void *dev_id)
287 return IRQ_HANDLED;
290 #ifdef CONFIG_PCI
291 static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
292 unsigned int irq)
294 u32 class;
295 u8 progif;
297 if (pci->irq == irq) {
298 pnp_dbg(&pnp->dev, " device %s using irq %d\n",
299 pci_name(pci), irq);
300 return 1;
304 * See pci_setup_device() and ata_pci_sff_activate_host() for
305 * similar IDE legacy detection.
307 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
308 class >>= 8; /* discard revision ID */
309 progif = class & 0xff;
310 class >>= 8;
312 if (class == PCI_CLASS_STORAGE_IDE) {
314 * Unless both channels are native-PCI mode only,
315 * treat the compatibility IRQs as busy.
317 if ((progif & 0x5) != 0x5)
318 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
319 pci_get_legacy_ide_irq(pci, 1) == irq) {
320 pnp_dbg(&pnp->dev, " legacy IDE device %s "
321 "using irq %d\n", pci_name(pci), irq);
322 return 1;
326 return 0;
328 #endif
330 static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
332 #ifdef CONFIG_PCI
333 struct pci_dev *pci = NULL;
335 for_each_pci_dev(pci) {
336 if (pci_dev_uses_irq(pnp, pci, irq)) {
337 pci_dev_put(pci);
338 return 1;
341 #endif
342 return 0;
345 int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
347 int i;
348 struct pnp_dev *tdev;
349 struct resource *tres;
350 resource_size_t *irq;
352 irq = &res->start;
354 /* if the resource doesn't exist, don't complain about it */
355 if (cannot_compare(res->flags))
356 return 1;
358 /* check if the resource is valid */
359 if (*irq < 0 || *irq > 15)
360 return 0;
362 /* check if the resource is reserved */
363 for (i = 0; i < 16; i++) {
364 if (pnp_reserve_irq[i] == *irq)
365 return 0;
368 /* check for internal conflicts */
369 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
370 if (tres != res && tres->flags & IORESOURCE_IRQ) {
371 if (tres->start == *irq)
372 return 0;
376 /* check if the resource is being used by a pci device */
377 if (pci_uses_irq(dev, *irq))
378 return 0;
380 /* check if the resource is already in use, skip if the
381 * device is active because it itself may be in use */
382 if (!dev->active) {
383 if (request_irq(*irq, pnp_test_handler,
384 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
385 return 0;
386 free_irq(*irq, NULL);
389 /* check for conflicts with other pnp devices */
390 pnp_for_each_dev(tdev) {
391 if (tdev == dev)
392 continue;
393 for (i = 0;
394 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
395 i++) {
396 if (tres->flags & IORESOURCE_IRQ) {
397 if (cannot_compare(tres->flags))
398 continue;
399 if (tres->start == *irq)
400 return 0;
405 return 1;
408 int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
410 #ifndef CONFIG_IA64
411 int i;
412 struct pnp_dev *tdev;
413 struct resource *tres;
414 resource_size_t *dma;
416 dma = &res->start;
418 /* if the resource doesn't exist, don't complain about it */
419 if (cannot_compare(res->flags))
420 return 1;
422 /* check if the resource is valid */
423 if (*dma < 0 || *dma == 4 || *dma > 7)
424 return 0;
426 /* check if the resource is reserved */
427 for (i = 0; i < 8; i++) {
428 if (pnp_reserve_dma[i] == *dma)
429 return 0;
432 /* check for internal conflicts */
433 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
434 if (tres != res && tres->flags & IORESOURCE_DMA) {
435 if (tres->start == *dma)
436 return 0;
440 /* check if the resource is already in use, skip if the
441 * device is active because it itself may be in use */
442 if (!dev->active) {
443 if (request_dma(*dma, "pnp"))
444 return 0;
445 free_dma(*dma);
448 /* check for conflicts with other pnp devices */
449 pnp_for_each_dev(tdev) {
450 if (tdev == dev)
451 continue;
452 for (i = 0;
453 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
454 i++) {
455 if (tres->flags & IORESOURCE_DMA) {
456 if (cannot_compare(tres->flags))
457 continue;
458 if (tres->start == *dma)
459 return 0;
464 return 1;
465 #else
466 /* IA64 does not have legacy DMA */
467 return 0;
468 #endif
471 unsigned long pnp_resource_type(struct resource *res)
473 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
474 IORESOURCE_IRQ | IORESOURCE_DMA |
475 IORESOURCE_BUS);
478 struct resource *pnp_get_resource(struct pnp_dev *dev,
479 unsigned long type, unsigned int num)
481 struct pnp_resource *pnp_res;
482 struct resource *res;
484 list_for_each_entry(pnp_res, &dev->resources, list) {
485 res = &pnp_res->res;
486 if (pnp_resource_type(res) == type && num-- == 0)
487 return res;
489 return NULL;
491 EXPORT_SYMBOL(pnp_get_resource);
493 static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
495 struct pnp_resource *pnp_res;
497 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
498 if (!pnp_res)
499 return NULL;
501 list_add_tail(&pnp_res->list, &dev->resources);
502 return pnp_res;
505 struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
506 int flags)
508 struct pnp_resource *pnp_res;
509 struct resource *res;
511 pnp_res = pnp_new_resource(dev);
512 if (!pnp_res) {
513 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
514 return NULL;
517 res = &pnp_res->res;
518 res->flags = IORESOURCE_IRQ | flags;
519 res->start = irq;
520 res->end = irq;
522 pnp_dbg(&dev->dev, " add %pr\n", res);
523 return pnp_res;
526 struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
527 int flags)
529 struct pnp_resource *pnp_res;
530 struct resource *res;
532 pnp_res = pnp_new_resource(dev);
533 if (!pnp_res) {
534 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
535 return NULL;
538 res = &pnp_res->res;
539 res->flags = IORESOURCE_DMA | flags;
540 res->start = dma;
541 res->end = dma;
543 pnp_dbg(&dev->dev, " add %pr\n", res);
544 return pnp_res;
547 struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
548 resource_size_t start,
549 resource_size_t end, int flags)
551 struct pnp_resource *pnp_res;
552 struct resource *res;
554 pnp_res = pnp_new_resource(dev);
555 if (!pnp_res) {
556 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
557 (unsigned long long) start,
558 (unsigned long long) end);
559 return NULL;
562 res = &pnp_res->res;
563 res->flags = IORESOURCE_IO | flags;
564 res->start = start;
565 res->end = end;
567 pnp_dbg(&dev->dev, " add %pr\n", res);
568 return pnp_res;
571 struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
572 resource_size_t start,
573 resource_size_t end, int flags)
575 struct pnp_resource *pnp_res;
576 struct resource *res;
578 pnp_res = pnp_new_resource(dev);
579 if (!pnp_res) {
580 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
581 (unsigned long long) start,
582 (unsigned long long) end);
583 return NULL;
586 res = &pnp_res->res;
587 res->flags = IORESOURCE_MEM | flags;
588 res->start = start;
589 res->end = end;
591 pnp_dbg(&dev->dev, " add %pr\n", res);
592 return pnp_res;
595 struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
596 resource_size_t start,
597 resource_size_t end)
599 struct pnp_resource *pnp_res;
600 struct resource *res;
602 pnp_res = pnp_new_resource(dev);
603 if (!pnp_res) {
604 dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
605 (unsigned long long) start,
606 (unsigned long long) end);
607 return NULL;
610 res = &pnp_res->res;
611 res->flags = IORESOURCE_BUS;
612 res->start = start;
613 res->end = end;
615 pnp_dbg(&dev->dev, " add %pr\n", res);
616 return pnp_res;
620 * Determine whether the specified resource is a possible configuration
621 * for this device.
623 int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
624 resource_size_t size)
626 struct pnp_option *option;
627 struct pnp_port *port;
628 struct pnp_mem *mem;
629 struct pnp_irq *irq;
630 struct pnp_dma *dma;
632 list_for_each_entry(option, &dev->options, list) {
633 if (option->type != type)
634 continue;
636 switch (option->type) {
637 case IORESOURCE_IO:
638 port = &option->u.port;
639 if (port->min == start && port->size == size)
640 return 1;
641 break;
642 case IORESOURCE_MEM:
643 mem = &option->u.mem;
644 if (mem->min == start && mem->size == size)
645 return 1;
646 break;
647 case IORESOURCE_IRQ:
648 irq = &option->u.irq;
649 if (start < PNP_IRQ_NR &&
650 test_bit(start, irq->map.bits))
651 return 1;
652 break;
653 case IORESOURCE_DMA:
654 dma = &option->u.dma;
655 if (dma->map & (1 << start))
656 return 1;
657 break;
661 return 0;
663 EXPORT_SYMBOL(pnp_possible_config);
665 int pnp_range_reserved(resource_size_t start, resource_size_t end)
667 struct pnp_dev *dev;
668 struct pnp_resource *pnp_res;
669 resource_size_t *dev_start, *dev_end;
671 pnp_for_each_dev(dev) {
672 list_for_each_entry(pnp_res, &dev->resources, list) {
673 dev_start = &pnp_res->res.start;
674 dev_end = &pnp_res->res.end;
675 if (ranged_conflict(&start, &end, dev_start, dev_end))
676 return 1;
679 return 0;
681 EXPORT_SYMBOL(pnp_range_reserved);
683 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
684 static int __init pnp_setup_reserve_irq(char *str)
686 int i;
688 for (i = 0; i < 16; i++)
689 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
690 break;
691 return 1;
694 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
696 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
697 static int __init pnp_setup_reserve_dma(char *str)
699 int i;
701 for (i = 0; i < 8; i++)
702 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
703 break;
704 return 1;
707 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
709 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
710 static int __init pnp_setup_reserve_io(char *str)
712 int i;
714 for (i = 0; i < 16; i++)
715 if (get_option(&str, &pnp_reserve_io[i]) != 2)
716 break;
717 return 1;
720 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
722 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
723 static int __init pnp_setup_reserve_mem(char *str)
725 int i;
727 for (i = 0; i < 16; i++)
728 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
729 break;
730 return 1;
733 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);