PNP: centralize resource option allocations
[linux-2.6/sactl.git] / drivers / pnp / resource.c
blob61145491f363cd8b1d2b8c5b6962337816761c13
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 pnp_irq_mask_t *map, unsigned char flags)
83 struct pnp_irq *data, *ptr;
84 #ifdef DEBUG
85 char buf[PNP_IRQ_NR]; /* hex-encoded, so this is overkill but safe */
86 #endif
88 data = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
89 if (!data)
90 return -ENOMEM;
92 data->map = *map;
93 data->flags = flags;
95 ptr = option->irq;
96 while (ptr && ptr->next)
97 ptr = ptr->next;
98 if (ptr)
99 ptr->next = data;
100 else
101 option->irq = data;
103 #ifdef CONFIG_PCI
105 int i;
107 for (i = 0; i < 16; i++)
108 if (test_bit(i, data->map.bits))
109 pcibios_penalize_isa_irq(i, 0);
111 #endif
113 #ifdef DEBUG
114 bitmap_scnprintf(buf, sizeof(buf), data->map.bits, PNP_IRQ_NR);
115 dev_dbg(&dev->dev, " irq bitmask %s flags %#x\n", buf,
116 data->flags);
117 #endif
118 return 0;
121 int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option,
122 unsigned char map, unsigned char flags)
124 struct pnp_dma *data, *ptr;
126 data = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
127 if (!data)
128 return -ENOMEM;
130 data->map = map;
131 data->flags = flags;
133 ptr = option->dma;
134 while (ptr && ptr->next)
135 ptr = ptr->next;
136 if (ptr)
137 ptr->next = data;
138 else
139 option->dma = data;
141 dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map,
142 data->flags);
143 return 0;
146 int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option,
147 resource_size_t min, resource_size_t max,
148 resource_size_t align, resource_size_t size,
149 unsigned char flags)
151 struct pnp_port *data, *ptr;
153 data = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
157 data->min = min;
158 data->max = max;
159 data->align = align;
160 data->size = size;
161 data->flags = flags;
163 ptr = option->port;
164 while (ptr && ptr->next)
165 ptr = ptr->next;
166 if (ptr)
167 ptr->next = data;
168 else
169 option->port = data;
171 dev_dbg(&dev->dev, " io "
172 "min %#llx max %#llx align %lld size %lld flags %#x\n",
173 (unsigned long long) data->min,
174 (unsigned long long) data->max,
175 (unsigned long long) data->align,
176 (unsigned long long) data->size, data->flags);
177 return 0;
180 int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
181 resource_size_t min, resource_size_t max,
182 resource_size_t align, resource_size_t size,
183 unsigned char flags)
185 struct pnp_mem *data, *ptr;
187 data = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
188 if (!data)
189 return -ENOMEM;
191 data->min = min;
192 data->max = max;
193 data->align = align;
194 data->size = size;
195 data->flags = flags;
197 ptr = option->mem;
198 while (ptr && ptr->next)
199 ptr = ptr->next;
200 if (ptr)
201 ptr->next = data;
202 else
203 option->mem = data;
205 dev_dbg(&dev->dev, " mem "
206 "min %#llx max %#llx align %lld size %lld flags %#x\n",
207 (unsigned long long) data->min,
208 (unsigned long long) data->max,
209 (unsigned long long) data->align,
210 (unsigned long long) data->size, data->flags);
211 return 0;
214 static void pnp_free_port(struct pnp_port *port)
216 struct pnp_port *next;
218 while (port) {
219 next = port->next;
220 kfree(port);
221 port = next;
225 static void pnp_free_irq(struct pnp_irq *irq)
227 struct pnp_irq *next;
229 while (irq) {
230 next = irq->next;
231 kfree(irq);
232 irq = next;
236 static void pnp_free_dma(struct pnp_dma *dma)
238 struct pnp_dma *next;
240 while (dma) {
241 next = dma->next;
242 kfree(dma);
243 dma = next;
247 static void pnp_free_mem(struct pnp_mem *mem)
249 struct pnp_mem *next;
251 while (mem) {
252 next = mem->next;
253 kfree(mem);
254 mem = next;
258 void pnp_free_option(struct pnp_option *option)
260 struct pnp_option *next;
262 while (option) {
263 next = option->next;
264 pnp_free_port(option->port);
265 pnp_free_irq(option->irq);
266 pnp_free_dma(option->dma);
267 pnp_free_mem(option->mem);
268 kfree(option);
269 option = next;
274 * resource validity checking
277 #define length(start, end) (*(end) - *(start) + 1)
279 /* Two ranges conflict if one doesn't end before the other starts */
280 #define ranged_conflict(starta, enda, startb, endb) \
281 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
283 #define cannot_compare(flags) \
284 ((flags) & IORESOURCE_DISABLED)
286 int pnp_check_port(struct pnp_dev *dev, struct resource *res)
288 int i;
289 struct pnp_dev *tdev;
290 struct resource *tres;
291 resource_size_t *port, *end, *tport, *tend;
293 port = &res->start;
294 end = &res->end;
296 /* if the resource doesn't exist, don't complain about it */
297 if (cannot_compare(res->flags))
298 return 1;
300 /* check if the resource is already in use, skip if the
301 * device is active because it itself may be in use */
302 if (!dev->active) {
303 if (__check_region(&ioport_resource, *port, length(port, end)))
304 return 0;
307 /* check if the resource is reserved */
308 for (i = 0; i < 8; i++) {
309 int rport = pnp_reserve_io[i << 1];
310 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
311 if (ranged_conflict(port, end, &rport, &rend))
312 return 0;
315 /* check for internal conflicts */
316 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
317 if (tres != res && tres->flags & IORESOURCE_IO) {
318 tport = &tres->start;
319 tend = &tres->end;
320 if (ranged_conflict(port, end, tport, tend))
321 return 0;
325 /* check for conflicts with other pnp devices */
326 pnp_for_each_dev(tdev) {
327 if (tdev == dev)
328 continue;
329 for (i = 0;
330 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
331 i++) {
332 if (tres->flags & IORESOURCE_IO) {
333 if (cannot_compare(tres->flags))
334 continue;
335 tport = &tres->start;
336 tend = &tres->end;
337 if (ranged_conflict(port, end, tport, tend))
338 return 0;
343 return 1;
346 int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
348 int i;
349 struct pnp_dev *tdev;
350 struct resource *tres;
351 resource_size_t *addr, *end, *taddr, *tend;
353 addr = &res->start;
354 end = &res->end;
356 /* if the resource doesn't exist, don't complain about it */
357 if (cannot_compare(res->flags))
358 return 1;
360 /* check if the resource is already in use, skip if the
361 * device is active because it itself may be in use */
362 if (!dev->active) {
363 if (check_mem_region(*addr, length(addr, end)))
364 return 0;
367 /* check if the resource is reserved */
368 for (i = 0; i < 8; i++) {
369 int raddr = pnp_reserve_mem[i << 1];
370 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
371 if (ranged_conflict(addr, end, &raddr, &rend))
372 return 0;
375 /* check for internal conflicts */
376 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
377 if (tres != res && tres->flags & IORESOURCE_MEM) {
378 taddr = &tres->start;
379 tend = &tres->end;
380 if (ranged_conflict(addr, end, taddr, tend))
381 return 0;
385 /* check for conflicts with other pnp devices */
386 pnp_for_each_dev(tdev) {
387 if (tdev == dev)
388 continue;
389 for (i = 0;
390 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
391 i++) {
392 if (tres->flags & IORESOURCE_MEM) {
393 if (cannot_compare(tres->flags))
394 continue;
395 taddr = &tres->start;
396 tend = &tres->end;
397 if (ranged_conflict(addr, end, taddr, tend))
398 return 0;
403 return 1;
406 static irqreturn_t pnp_test_handler(int irq, void *dev_id)
408 return IRQ_HANDLED;
411 int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
413 int i;
414 struct pnp_dev *tdev;
415 struct resource *tres;
416 resource_size_t *irq;
418 irq = &res->start;
420 /* if the resource doesn't exist, don't complain about it */
421 if (cannot_compare(res->flags))
422 return 1;
424 /* check if the resource is valid */
425 if (*irq < 0 || *irq > 15)
426 return 0;
428 /* check if the resource is reserved */
429 for (i = 0; i < 16; i++) {
430 if (pnp_reserve_irq[i] == *irq)
431 return 0;
434 /* check for internal conflicts */
435 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
436 if (tres != res && tres->flags & IORESOURCE_IRQ) {
437 if (tres->start == *irq)
438 return 0;
442 #ifdef CONFIG_PCI
443 /* check if the resource is being used by a pci device */
445 struct pci_dev *pci = NULL;
446 for_each_pci_dev(pci) {
447 if (pci->irq == *irq) {
448 pci_dev_put(pci);
449 return 0;
453 #endif
455 /* check if the resource is already in use, skip if the
456 * device is active because it itself may be in use */
457 if (!dev->active) {
458 if (request_irq(*irq, pnp_test_handler,
459 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
460 return 0;
461 free_irq(*irq, NULL);
464 /* check for conflicts with other pnp devices */
465 pnp_for_each_dev(tdev) {
466 if (tdev == dev)
467 continue;
468 for (i = 0;
469 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
470 i++) {
471 if (tres->flags & IORESOURCE_IRQ) {
472 if (cannot_compare(tres->flags))
473 continue;
474 if (tres->start == *irq)
475 return 0;
480 return 1;
483 int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
485 #ifndef CONFIG_IA64
486 int i;
487 struct pnp_dev *tdev;
488 struct resource *tres;
489 resource_size_t *dma;
491 dma = &res->start;
493 /* if the resource doesn't exist, don't complain about it */
494 if (cannot_compare(res->flags))
495 return 1;
497 /* check if the resource is valid */
498 if (*dma < 0 || *dma == 4 || *dma > 7)
499 return 0;
501 /* check if the resource is reserved */
502 for (i = 0; i < 8; i++) {
503 if (pnp_reserve_dma[i] == *dma)
504 return 0;
507 /* check for internal conflicts */
508 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
509 if (tres != res && tres->flags & IORESOURCE_DMA) {
510 if (tres->start == *dma)
511 return 0;
515 /* check if the resource is already in use, skip if the
516 * device is active because it itself may be in use */
517 if (!dev->active) {
518 if (request_dma(*dma, "pnp"))
519 return 0;
520 free_dma(*dma);
523 /* check for conflicts with other pnp devices */
524 pnp_for_each_dev(tdev) {
525 if (tdev == dev)
526 continue;
527 for (i = 0;
528 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
529 i++) {
530 if (tres->flags & IORESOURCE_DMA) {
531 if (cannot_compare(tres->flags))
532 continue;
533 if (tres->start == *dma)
534 return 0;
539 return 1;
540 #else
541 /* IA64 does not have legacy DMA */
542 return 0;
543 #endif
546 int pnp_resource_type(struct resource *res)
548 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
549 IORESOURCE_IRQ | IORESOURCE_DMA);
552 struct resource *pnp_get_resource(struct pnp_dev *dev,
553 unsigned int type, unsigned int num)
555 struct pnp_resource *pnp_res;
556 struct resource *res;
558 list_for_each_entry(pnp_res, &dev->resources, list) {
559 res = &pnp_res->res;
560 if (pnp_resource_type(res) == type && num-- == 0)
561 return res;
563 return NULL;
565 EXPORT_SYMBOL(pnp_get_resource);
567 static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
569 struct pnp_resource *pnp_res;
571 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
572 if (!pnp_res)
573 return NULL;
575 list_add_tail(&pnp_res->list, &dev->resources);
576 return pnp_res;
579 struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
580 int flags)
582 struct pnp_resource *pnp_res;
583 struct resource *res;
585 pnp_res = pnp_new_resource(dev);
586 if (!pnp_res) {
587 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
588 return NULL;
591 res = &pnp_res->res;
592 res->flags = IORESOURCE_IRQ | flags;
593 res->start = irq;
594 res->end = irq;
596 dev_dbg(&dev->dev, " add irq %d flags %#x\n", irq, flags);
597 return pnp_res;
600 struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
601 int flags)
603 struct pnp_resource *pnp_res;
604 struct resource *res;
606 pnp_res = pnp_new_resource(dev);
607 if (!pnp_res) {
608 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
609 return NULL;
612 res = &pnp_res->res;
613 res->flags = IORESOURCE_DMA | flags;
614 res->start = dma;
615 res->end = dma;
617 dev_dbg(&dev->dev, " add dma %d flags %#x\n", dma, flags);
618 return pnp_res;
621 struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
622 resource_size_t start,
623 resource_size_t end, int flags)
625 struct pnp_resource *pnp_res;
626 struct resource *res;
628 pnp_res = pnp_new_resource(dev);
629 if (!pnp_res) {
630 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
631 (unsigned long long) start,
632 (unsigned long long) end);
633 return NULL;
636 res = &pnp_res->res;
637 res->flags = IORESOURCE_IO | flags;
638 res->start = start;
639 res->end = end;
641 dev_dbg(&dev->dev, " add io %#llx-%#llx flags %#x\n",
642 (unsigned long long) start, (unsigned long long) end, flags);
643 return pnp_res;
646 struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
647 resource_size_t start,
648 resource_size_t end, int flags)
650 struct pnp_resource *pnp_res;
651 struct resource *res;
653 pnp_res = pnp_new_resource(dev);
654 if (!pnp_res) {
655 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
656 (unsigned long long) start,
657 (unsigned long long) end);
658 return NULL;
661 res = &pnp_res->res;
662 res->flags = IORESOURCE_MEM | flags;
663 res->start = start;
664 res->end = end;
666 dev_dbg(&dev->dev, " add mem %#llx-%#llx flags %#x\n",
667 (unsigned long long) start, (unsigned long long) end, flags);
668 return pnp_res;
671 static int pnp_possible_option(struct pnp_option *option, int type,
672 resource_size_t start, resource_size_t size)
674 struct pnp_option *tmp;
675 struct pnp_port *port;
676 struct pnp_mem *mem;
677 struct pnp_irq *irq;
678 struct pnp_dma *dma;
680 if (!option)
681 return 0;
683 for (tmp = option; tmp; tmp = tmp->next) {
684 switch (type) {
685 case IORESOURCE_IO:
686 for (port = tmp->port; port; port = port->next) {
687 if (port->min == start && port->size == size)
688 return 1;
690 break;
691 case IORESOURCE_MEM:
692 for (mem = tmp->mem; mem; mem = mem->next) {
693 if (mem->min == start && mem->size == size)
694 return 1;
696 break;
697 case IORESOURCE_IRQ:
698 for (irq = tmp->irq; irq; irq = irq->next) {
699 if (start < PNP_IRQ_NR &&
700 test_bit(start, irq->map.bits))
701 return 1;
703 break;
704 case IORESOURCE_DMA:
705 for (dma = tmp->dma; dma; dma = dma->next) {
706 if (dma->map & (1 << start))
707 return 1;
709 break;
713 return 0;
717 * Determine whether the specified resource is a possible configuration
718 * for this device.
720 int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
721 resource_size_t size)
723 if (pnp_possible_option(dev->independent, type, start, size))
724 return 1;
726 if (pnp_possible_option(dev->dependent, type, start, size))
727 return 1;
729 return 0;
731 EXPORT_SYMBOL(pnp_possible_config);
733 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
734 static int __init pnp_setup_reserve_irq(char *str)
736 int i;
738 for (i = 0; i < 16; i++)
739 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
740 break;
741 return 1;
744 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
746 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
747 static int __init pnp_setup_reserve_dma(char *str)
749 int i;
751 for (i = 0; i < 8; i++)
752 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
753 break;
754 return 1;
757 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
759 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
760 static int __init pnp_setup_reserve_io(char *str)
762 int i;
764 for (i = 0; i < 16; i++)
765 if (get_option(&str, &pnp_reserve_io[i]) != 2)
766 break;
767 return 1;
770 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
772 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
773 static int __init pnp_setup_reserve_mem(char *str)
775 int i;
777 for (i = 0; i < 16; i++)
778 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
779 break;
780 return 1;
783 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);