2 * resource.c - Contains functions for registering and analyzing resource information
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@suse.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
21 #include <linux/pnp.h>
24 int pnp_skip_pci_scan; /* skip PCI resource scanning */
25 int pnp_reserve_irq[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
26 int pnp_reserve_dma[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
27 int pnp_reserve_io[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
28 int pnp_reserve_mem[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
35 static struct pnp_option * pnp_build_option(int priority)
37 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
39 /* check if pnp_alloc ran out of memory */
43 option->priority = priority & 0xff;
44 /* make sure the priority is valid */
45 if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
46 option->priority = PNP_RES_PRIORITY_INVALID;
51 struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev)
53 struct pnp_option *option;
57 option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
59 /* this should never happen but if it does we'll try to continue */
61 pnp_err("independent resource already registered");
62 dev->independent = option;
66 struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority)
68 struct pnp_option *option;
72 option = pnp_build_option(priority);
75 struct pnp_option *parent = dev->dependent;
77 parent = parent->next;
78 parent->next = option;
80 dev->dependent = option;
84 int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
93 while (ptr && ptr->next)
105 if (data->map & (1<<i))
106 pcibios_penalize_isa_irq(i);
112 int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
121 while (ptr && ptr->next)
131 int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
133 struct pnp_port *ptr;
140 while (ptr && ptr->next)
150 int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
159 while (ptr && ptr->next)
168 static void pnp_free_port(struct pnp_port *port)
170 struct pnp_port *next;
179 static void pnp_free_irq(struct pnp_irq *irq)
181 struct pnp_irq *next;
190 static void pnp_free_dma(struct pnp_dma *dma)
192 struct pnp_dma *next;
201 static void pnp_free_mem(struct pnp_mem *mem)
203 struct pnp_mem *next;
212 void pnp_free_option(struct pnp_option *option)
214 struct pnp_option *next;
218 pnp_free_port(option->port);
219 pnp_free_irq(option->irq);
220 pnp_free_dma(option->dma);
221 pnp_free_mem(option->mem);
229 * resource validity checking
232 #define length(start, end) (*(end) - *(start) + 1)
234 /* Two ranges conflict if one doesn't end before the other starts */
235 #define ranged_conflict(starta, enda, startb, endb) \
236 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
238 #define cannot_compare(flags) \
239 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
241 int pnp_check_port(struct pnp_dev * dev, int idx)
244 struct pnp_dev *tdev;
245 unsigned long *port, *end, *tport, *tend;
246 port = &dev->res.port_resource[idx].start;
247 end = &dev->res.port_resource[idx].end;
249 /* if the resource doesn't exist, don't complain about it */
250 if (cannot_compare(dev->res.port_resource[idx].flags))
253 /* check if the resource is already in use, skip if the
254 * device is active because it itself may be in use */
256 if (__check_region(&ioport_resource, *port, length(port,end)))
260 /* check if the resource is reserved */
261 for (tmp = 0; tmp < 8; tmp++) {
262 int rport = pnp_reserve_io[tmp << 1];
263 int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
264 if (ranged_conflict(port,end,&rport,&rend))
268 /* check for internal conflicts */
269 for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
270 if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
271 tport = &dev->res.port_resource[tmp].start;
272 tend = &dev->res.port_resource[tmp].end;
273 if (ranged_conflict(port,end,tport,tend))
278 /* check for conflicts with other pnp devices */
279 pnp_for_each_dev(tdev) {
282 for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
283 if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
284 if (cannot_compare(tdev->res.port_resource[tmp].flags))
286 tport = &tdev->res.port_resource[tmp].start;
287 tend = &tdev->res.port_resource[tmp].end;
288 if (ranged_conflict(port,end,tport,tend))
297 int pnp_check_mem(struct pnp_dev * dev, int idx)
300 struct pnp_dev *tdev;
301 unsigned long *addr, *end, *taddr, *tend;
302 addr = &dev->res.mem_resource[idx].start;
303 end = &dev->res.mem_resource[idx].end;
305 /* if the resource doesn't exist, don't complain about it */
306 if (cannot_compare(dev->res.mem_resource[idx].flags))
309 /* check if the resource is already in use, skip if the
310 * device is active because it itself may be in use */
312 if (check_mem_region(*addr, length(addr,end)))
316 /* check if the resource is reserved */
317 for (tmp = 0; tmp < 8; tmp++) {
318 int raddr = pnp_reserve_mem[tmp << 1];
319 int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
320 if (ranged_conflict(addr,end,&raddr,&rend))
324 /* check for internal conflicts */
325 for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
326 if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
327 taddr = &dev->res.mem_resource[tmp].start;
328 tend = &dev->res.mem_resource[tmp].end;
329 if (ranged_conflict(addr,end,taddr,tend))
334 /* check for conflicts with other pnp devices */
335 pnp_for_each_dev(tdev) {
338 for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
339 if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
340 if (cannot_compare(tdev->res.mem_resource[tmp].flags))
342 taddr = &tdev->res.mem_resource[tmp].start;
343 tend = &tdev->res.mem_resource[tmp].end;
344 if (ranged_conflict(addr,end,taddr,tend))
353 static irqreturn_t pnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
358 int pnp_check_irq(struct pnp_dev * dev, int idx)
361 struct pnp_dev *tdev;
362 unsigned long * irq = &dev->res.irq_resource[idx].start;
364 /* if the resource doesn't exist, don't complain about it */
365 if (cannot_compare(dev->res.irq_resource[idx].flags))
368 /* check if the resource is valid */
369 if (*irq < 0 || *irq > 15)
372 /* check if the resource is reserved */
373 for (tmp = 0; tmp < 16; tmp++) {
374 if (pnp_reserve_irq[tmp] == *irq)
378 /* check for internal conflicts */
379 for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
380 if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
381 if (dev->res.irq_resource[tmp].start == *irq)
387 /* check if the resource is being used by a pci device */
388 if (!pnp_skip_pci_scan) {
389 struct pci_dev * pci = NULL;
390 while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
391 if (pci->irq == *irq)
397 /* check if the resource is already in use, skip if the
398 * device is active because it itself may be in use */
400 if (request_irq(*irq, pnp_test_handler, SA_INTERRUPT, "pnp", NULL))
402 free_irq(*irq, NULL);
405 /* check for conflicts with other pnp devices */
406 pnp_for_each_dev(tdev) {
409 for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
410 if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
411 if (cannot_compare(tdev->res.irq_resource[tmp].flags))
413 if ((tdev->res.irq_resource[tmp].start == *irq))
422 int pnp_check_dma(struct pnp_dev * dev, int idx)
425 struct pnp_dev *tdev;
426 unsigned long * dma = &dev->res.dma_resource[idx].start;
428 /* if the resource doesn't exist, don't complain about it */
429 if (cannot_compare(dev->res.dma_resource[idx].flags))
432 /* check if the resource is valid */
433 if (*dma < 0 || *dma == 4 || *dma > 7)
436 /* check if the resource is reserved */
437 for (tmp = 0; tmp < 8; tmp++) {
438 if (pnp_reserve_dma[tmp] == *dma)
442 /* check for internal conflicts */
443 for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
444 if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
445 if (dev->res.dma_resource[tmp].start == *dma)
450 /* check if the resource is already in use, skip if the
451 * device is active because it itself may be in use */
453 if (request_dma(*dma, "pnp"))
458 /* check for conflicts with other pnp devices */
459 pnp_for_each_dev(tdev) {
462 for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
463 if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
464 if (cannot_compare(tdev->res.dma_resource[tmp].flags))
466 if ((tdev->res.dma_resource[tmp].start == *dma))
476 EXPORT_SYMBOL(pnp_register_dependent_option);
477 EXPORT_SYMBOL(pnp_register_independent_option);
478 EXPORT_SYMBOL(pnp_register_irq_resource);
479 EXPORT_SYMBOL(pnp_register_dma_resource);
480 EXPORT_SYMBOL(pnp_register_port_resource);
481 EXPORT_SYMBOL(pnp_register_mem_resource);
484 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
486 static int __init pnp_setup_reserve_irq(char *str)
490 for (i = 0; i < 16; i++)
491 if (get_option(&str,&pnp_reserve_irq[i]) != 2)
496 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
498 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
500 static int __init pnp_setup_reserve_dma(char *str)
504 for (i = 0; i < 8; i++)
505 if (get_option(&str,&pnp_reserve_dma[i]) != 2)
510 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
512 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
514 static int __init pnp_setup_reserve_io(char *str)
518 for (i = 0; i < 16; i++)
519 if (get_option(&str,&pnp_reserve_io[i]) != 2)
524 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
526 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
528 static int __init pnp_setup_reserve_mem(char *str)
532 for (i = 0; i < 16; i++)
533 if (get_option(&str,&pnp_reserve_mem[i]) != 2)
538 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);