PNP: add pnp_possible_config() -- can a device could be configured this way?
[linux-2.6/verdex.git] / drivers / pnp / resource.c
blob786fd356916d0a66f135decd11d7c51aab1fa6a5
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 */
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <asm/io.h>
13 #include <asm/dma.h>
14 #include <asm/irq.h>
15 #include <linux/pci.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
19 #include <linux/pnp.h>
20 #include "base.h"
22 static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
23 static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
24 static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
25 static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
28 * option registration
31 struct pnp_option *pnp_build_option(int priority)
33 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
35 if (!option)
36 return NULL;
38 option->priority = priority & 0xff;
39 /* make sure the priority is valid */
40 if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
41 option->priority = PNP_RES_PRIORITY_INVALID;
43 return option;
46 struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
48 struct pnp_option *option;
50 option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
52 /* this should never happen but if it does we'll try to continue */
53 if (dev->independent)
54 dev_err(&dev->dev, "independent resource already registered\n");
55 dev->independent = option;
57 dev_dbg(&dev->dev, "new independent option\n");
58 return option;
61 struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
62 int priority)
64 struct pnp_option *option;
66 option = pnp_build_option(priority);
68 if (dev->dependent) {
69 struct pnp_option *parent = dev->dependent;
70 while (parent->next)
71 parent = parent->next;
72 parent->next = option;
73 } else
74 dev->dependent = option;
76 dev_dbg(&dev->dev, "new dependent option (priority %#x)\n", priority);
77 return option;
80 int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option,
81 struct pnp_irq *data)
83 struct pnp_irq *ptr;
84 #ifdef DEBUG
85 char buf[PNP_IRQ_NR]; /* hex-encoded, so this is overkill but safe */
86 #endif
88 ptr = option->irq;
89 while (ptr && ptr->next)
90 ptr = ptr->next;
91 if (ptr)
92 ptr->next = data;
93 else
94 option->irq = data;
96 #ifdef CONFIG_PCI
98 int i;
100 for (i = 0; i < 16; i++)
101 if (test_bit(i, data->map))
102 pcibios_penalize_isa_irq(i, 0);
104 #endif
106 #ifdef DEBUG
107 bitmap_scnprintf(buf, sizeof(buf), data->map, PNP_IRQ_NR);
108 dev_dbg(&dev->dev, " irq bitmask %s flags %#x\n", buf,
109 data->flags);
110 #endif
111 return 0;
114 int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option,
115 struct pnp_dma *data)
117 struct pnp_dma *ptr;
119 ptr = option->dma;
120 while (ptr && ptr->next)
121 ptr = ptr->next;
122 if (ptr)
123 ptr->next = data;
124 else
125 option->dma = data;
127 dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map,
128 data->flags);
129 return 0;
132 int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option,
133 struct pnp_port *data)
135 struct pnp_port *ptr;
137 ptr = option->port;
138 while (ptr && ptr->next)
139 ptr = ptr->next;
140 if (ptr)
141 ptr->next = data;
142 else
143 option->port = data;
145 dev_dbg(&dev->dev, " io "
146 "min %#x max %#x align %d size %d flags %#x\n",
147 data->min, data->max, data->align, data->size, data->flags);
148 return 0;
151 int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
152 struct pnp_mem *data)
154 struct pnp_mem *ptr;
156 ptr = option->mem;
157 while (ptr && ptr->next)
158 ptr = ptr->next;
159 if (ptr)
160 ptr->next = data;
161 else
162 option->mem = data;
164 dev_dbg(&dev->dev, " mem "
165 "min %#x max %#x align %d size %d flags %#x\n",
166 data->min, data->max, data->align, data->size, data->flags);
167 return 0;
170 static void pnp_free_port(struct pnp_port *port)
172 struct pnp_port *next;
174 while (port) {
175 next = port->next;
176 kfree(port);
177 port = next;
181 static void pnp_free_irq(struct pnp_irq *irq)
183 struct pnp_irq *next;
185 while (irq) {
186 next = irq->next;
187 kfree(irq);
188 irq = next;
192 static void pnp_free_dma(struct pnp_dma *dma)
194 struct pnp_dma *next;
196 while (dma) {
197 next = dma->next;
198 kfree(dma);
199 dma = next;
203 static void pnp_free_mem(struct pnp_mem *mem)
205 struct pnp_mem *next;
207 while (mem) {
208 next = mem->next;
209 kfree(mem);
210 mem = next;
214 void pnp_free_option(struct pnp_option *option)
216 struct pnp_option *next;
218 while (option) {
219 next = option->next;
220 pnp_free_port(option->port);
221 pnp_free_irq(option->irq);
222 pnp_free_dma(option->dma);
223 pnp_free_mem(option->mem);
224 kfree(option);
225 option = next;
230 * resource validity checking
233 #define length(start, end) (*(end) - *(start) + 1)
235 /* Two ranges conflict if one doesn't end before the other starts */
236 #define ranged_conflict(starta, enda, startb, endb) \
237 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
239 #define cannot_compare(flags) \
240 ((flags) & IORESOURCE_DISABLED)
242 int pnp_check_port(struct pnp_dev *dev, struct resource *res)
244 int i;
245 struct pnp_dev *tdev;
246 struct resource *tres;
247 resource_size_t *port, *end, *tport, *tend;
249 port = &res->start;
250 end = &res->end;
252 /* if the resource doesn't exist, don't complain about it */
253 if (cannot_compare(res->flags))
254 return 1;
256 /* check if the resource is already in use, skip if the
257 * device is active because it itself may be in use */
258 if (!dev->active) {
259 if (__check_region(&ioport_resource, *port, length(port, end)))
260 return 0;
263 /* check if the resource is reserved */
264 for (i = 0; i < 8; i++) {
265 int rport = pnp_reserve_io[i << 1];
266 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
267 if (ranged_conflict(port, end, &rport, &rend))
268 return 0;
271 /* check for internal conflicts */
272 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
273 if (tres != res && tres->flags & IORESOURCE_IO) {
274 tport = &tres->start;
275 tend = &tres->end;
276 if (ranged_conflict(port, end, tport, tend))
277 return 0;
281 /* check for conflicts with other pnp devices */
282 pnp_for_each_dev(tdev) {
283 if (tdev == dev)
284 continue;
285 for (i = 0;
286 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
287 i++) {
288 if (tres->flags & IORESOURCE_IO) {
289 if (cannot_compare(tres->flags))
290 continue;
291 tport = &tres->start;
292 tend = &tres->end;
293 if (ranged_conflict(port, end, tport, tend))
294 return 0;
299 return 1;
302 int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
304 int i;
305 struct pnp_dev *tdev;
306 struct resource *tres;
307 resource_size_t *addr, *end, *taddr, *tend;
309 addr = &res->start;
310 end = &res->end;
312 /* if the resource doesn't exist, don't complain about it */
313 if (cannot_compare(res->flags))
314 return 1;
316 /* check if the resource is already in use, skip if the
317 * device is active because it itself may be in use */
318 if (!dev->active) {
319 if (check_mem_region(*addr, length(addr, end)))
320 return 0;
323 /* check if the resource is reserved */
324 for (i = 0; i < 8; i++) {
325 int raddr = pnp_reserve_mem[i << 1];
326 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
327 if (ranged_conflict(addr, end, &raddr, &rend))
328 return 0;
331 /* check for internal conflicts */
332 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
333 if (tres != res && tres->flags & IORESOURCE_MEM) {
334 taddr = &tres->start;
335 tend = &tres->end;
336 if (ranged_conflict(addr, end, taddr, tend))
337 return 0;
341 /* check for conflicts with other pnp devices */
342 pnp_for_each_dev(tdev) {
343 if (tdev == dev)
344 continue;
345 for (i = 0;
346 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
347 i++) {
348 if (tres->flags & IORESOURCE_MEM) {
349 if (cannot_compare(tres->flags))
350 continue;
351 taddr = &tres->start;
352 tend = &tres->end;
353 if (ranged_conflict(addr, end, taddr, tend))
354 return 0;
359 return 1;
362 static irqreturn_t pnp_test_handler(int irq, void *dev_id)
364 return IRQ_HANDLED;
367 int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
369 int i;
370 struct pnp_dev *tdev;
371 struct resource *tres;
372 resource_size_t *irq;
374 irq = &res->start;
376 /* if the resource doesn't exist, don't complain about it */
377 if (cannot_compare(res->flags))
378 return 1;
380 /* check if the resource is valid */
381 if (*irq < 0 || *irq > 15)
382 return 0;
384 /* check if the resource is reserved */
385 for (i = 0; i < 16; i++) {
386 if (pnp_reserve_irq[i] == *irq)
387 return 0;
390 /* check for internal conflicts */
391 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
392 if (tres != res && tres->flags & IORESOURCE_IRQ) {
393 if (tres->start == *irq)
394 return 0;
398 #ifdef CONFIG_PCI
399 /* check if the resource is being used by a pci device */
401 struct pci_dev *pci = NULL;
402 for_each_pci_dev(pci) {
403 if (pci->irq == *irq) {
404 pci_dev_put(pci);
405 return 0;
409 #endif
411 /* check if the resource is already in use, skip if the
412 * device is active because it itself may be in use */
413 if (!dev->active) {
414 if (request_irq(*irq, pnp_test_handler,
415 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
416 return 0;
417 free_irq(*irq, NULL);
420 /* check for conflicts with other pnp devices */
421 pnp_for_each_dev(tdev) {
422 if (tdev == dev)
423 continue;
424 for (i = 0;
425 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
426 i++) {
427 if (tres->flags & IORESOURCE_IRQ) {
428 if (cannot_compare(tres->flags))
429 continue;
430 if (tres->start == *irq)
431 return 0;
436 return 1;
439 int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
441 #ifndef CONFIG_IA64
442 int i;
443 struct pnp_dev *tdev;
444 struct resource *tres;
445 resource_size_t *dma;
447 dma = &res->start;
449 /* if the resource doesn't exist, don't complain about it */
450 if (cannot_compare(res->flags))
451 return 1;
453 /* check if the resource is valid */
454 if (*dma < 0 || *dma == 4 || *dma > 7)
455 return 0;
457 /* check if the resource is reserved */
458 for (i = 0; i < 8; i++) {
459 if (pnp_reserve_dma[i] == *dma)
460 return 0;
463 /* check for internal conflicts */
464 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
465 if (tres != res && tres->flags & IORESOURCE_DMA) {
466 if (tres->start == *dma)
467 return 0;
471 /* check if the resource is already in use, skip if the
472 * device is active because it itself may be in use */
473 if (!dev->active) {
474 if (request_dma(*dma, "pnp"))
475 return 0;
476 free_dma(*dma);
479 /* check for conflicts with other pnp devices */
480 pnp_for_each_dev(tdev) {
481 if (tdev == dev)
482 continue;
483 for (i = 0;
484 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
485 i++) {
486 if (tres->flags & IORESOURCE_DMA) {
487 if (cannot_compare(tres->flags))
488 continue;
489 if (tres->start == *dma)
490 return 0;
495 return 1;
496 #else
497 /* IA64 does not have legacy DMA */
498 return 0;
499 #endif
502 int pnp_resource_type(struct resource *res)
504 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
505 IORESOURCE_IRQ | IORESOURCE_DMA);
508 struct resource *pnp_get_resource(struct pnp_dev *dev,
509 unsigned int type, unsigned int num)
511 struct pnp_resource *pnp_res;
512 struct resource *res;
514 list_for_each_entry(pnp_res, &dev->resources, list) {
515 res = &pnp_res->res;
516 if (pnp_resource_type(res) == type && num-- == 0)
517 return res;
519 return NULL;
521 EXPORT_SYMBOL(pnp_get_resource);
523 static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
525 struct pnp_resource *pnp_res;
527 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
528 if (!pnp_res)
529 return NULL;
531 list_add_tail(&pnp_res->list, &dev->resources);
532 return pnp_res;
535 struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
536 int flags)
538 struct pnp_resource *pnp_res;
539 struct resource *res;
541 pnp_res = pnp_new_resource(dev);
542 if (!pnp_res) {
543 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
544 return NULL;
547 res = &pnp_res->res;
548 res->flags = IORESOURCE_IRQ | flags;
549 res->start = irq;
550 res->end = irq;
552 dev_dbg(&dev->dev, " add irq %d flags %#x\n", irq, flags);
553 return pnp_res;
556 struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
557 int flags)
559 struct pnp_resource *pnp_res;
560 struct resource *res;
562 pnp_res = pnp_new_resource(dev);
563 if (!pnp_res) {
564 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
565 return NULL;
568 res = &pnp_res->res;
569 res->flags = IORESOURCE_DMA | flags;
570 res->start = dma;
571 res->end = dma;
573 dev_dbg(&dev->dev, " add dma %d flags %#x\n", dma, flags);
574 return pnp_res;
577 struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
578 resource_size_t start,
579 resource_size_t end, int flags)
581 struct pnp_resource *pnp_res;
582 struct resource *res;
584 pnp_res = pnp_new_resource(dev);
585 if (!pnp_res) {
586 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
587 (unsigned long long) start,
588 (unsigned long long) end);
589 return NULL;
592 res = &pnp_res->res;
593 res->flags = IORESOURCE_IO | flags;
594 res->start = start;
595 res->end = end;
597 dev_dbg(&dev->dev, " add io %#llx-%#llx flags %#x\n",
598 (unsigned long long) start, (unsigned long long) end, flags);
599 return pnp_res;
602 struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
603 resource_size_t start,
604 resource_size_t end, int flags)
606 struct pnp_resource *pnp_res;
607 struct resource *res;
609 pnp_res = pnp_new_resource(dev);
610 if (!pnp_res) {
611 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
612 (unsigned long long) start,
613 (unsigned long long) end);
614 return NULL;
617 res = &pnp_res->res;
618 res->flags = IORESOURCE_MEM | flags;
619 res->start = start;
620 res->end = end;
622 dev_dbg(&dev->dev, " add mem %#llx-%#llx flags %#x\n",
623 (unsigned long long) start, (unsigned long long) end, flags);
624 return pnp_res;
627 static int pnp_possible_option(struct pnp_option *option, int type,
628 resource_size_t start, resource_size_t size)
630 struct pnp_option *tmp;
631 struct pnp_port *port;
632 struct pnp_mem *mem;
633 struct pnp_irq *irq;
634 struct pnp_dma *dma;
636 if (!option)
637 return 0;
639 for (tmp = option; tmp; tmp = tmp->next) {
640 switch (type) {
641 case IORESOURCE_IO:
642 for (port = tmp->port; port; port = port->next) {
643 if (port->min == start && port->size == size)
644 return 1;
646 break;
647 case IORESOURCE_MEM:
648 for (mem = tmp->mem; mem; mem = mem->next) {
649 if (mem->min == start && mem->size == size)
650 return 1;
652 break;
653 case IORESOURCE_IRQ:
654 for (irq = tmp->irq; irq; irq = irq->next) {
655 if (start < PNP_IRQ_NR &&
656 test_bit(start, irq->map))
657 return 1;
659 break;
660 case IORESOURCE_DMA:
661 for (dma = tmp->dma; dma; dma = dma->next) {
662 if (dma->map & (1 << start))
663 return 1;
665 break;
669 return 0;
673 * Determine whether the specified resource is a possible configuration
674 * for this device.
676 int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
677 resource_size_t size)
679 if (pnp_possible_option(dev->independent, type, start, size))
680 return 1;
682 if (pnp_possible_option(dev->dependent, type, start, size))
683 return 1;
685 return 0;
687 EXPORT_SYMBOL(pnp_possible_config);
689 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
690 static int __init pnp_setup_reserve_irq(char *str)
692 int i;
694 for (i = 0; i < 16; i++)
695 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
696 break;
697 return 1;
700 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
702 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
703 static int __init pnp_setup_reserve_dma(char *str)
705 int i;
707 for (i = 0; i < 8; i++)
708 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
709 break;
710 return 1;
713 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
715 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
716 static int __init pnp_setup_reserve_io(char *str)
718 int i;
720 for (i = 0; i < 16; i++)
721 if (get_option(&str, &pnp_reserve_io[i]) != 2)
722 break;
723 return 1;
726 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
728 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
729 static int __init pnp_setup_reserve_mem(char *str)
731 int i;
733 for (i = 0; i < 16; i++)
734 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
735 break;
736 return 1;
739 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);