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/module.h>
10 #include <linux/errno.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/ioport.h>
18 #include <linux/init.h>
20 #include <linux/pnp.h>
23 static int pnp_reserve_irq[16] = {[0...15] = -1 }; /* reserve (don't use) some IRQ */
24 static int pnp_reserve_dma[8] = {[0...7] = -1 }; /* reserve (don't use) some DMA */
25 static int pnp_reserve_io[16] = {[0...15] = -1 }; /* reserve (don't use) some I/O region */
26 static int pnp_reserve_mem[16] = {[0...15] = -1 }; /* reserve (don't use) some memory region */
32 static struct pnp_option *pnp_build_option(int priority)
34 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
36 /* check if pnp_alloc ran out of memory */
40 option->priority = priority & 0xff;
41 /* make sure the priority is valid */
42 if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
43 option->priority = PNP_RES_PRIORITY_INVALID;
48 struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
50 struct pnp_option *option;
54 option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
56 /* this should never happen but if it does we'll try to continue */
58 pnp_err("independent resource already registered");
59 dev->independent = option;
63 struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
66 struct pnp_option *option;
70 option = pnp_build_option(priority);
73 struct pnp_option *parent = dev->dependent;
75 parent = parent->next;
76 parent->next = option;
78 dev->dependent = option;
82 int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
91 while (ptr && ptr->next)
102 for (i = 0; i < 16; i++)
103 if (test_bit(i, data->map))
104 pcibios_penalize_isa_irq(i, 0);
110 int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
119 while (ptr && ptr->next)
129 int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
131 struct pnp_port *ptr;
138 while (ptr && ptr->next)
148 int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
157 while (ptr && ptr->next)
166 static void pnp_free_port(struct pnp_port *port)
168 struct pnp_port *next;
177 static void pnp_free_irq(struct pnp_irq *irq)
179 struct pnp_irq *next;
188 static void pnp_free_dma(struct pnp_dma *dma)
190 struct pnp_dma *next;
199 static void pnp_free_mem(struct pnp_mem *mem)
201 struct pnp_mem *next;
210 void pnp_free_option(struct pnp_option *option)
212 struct pnp_option *next;
216 pnp_free_port(option->port);
217 pnp_free_irq(option->irq);
218 pnp_free_dma(option->dma);
219 pnp_free_mem(option->mem);
226 * resource validity checking
229 #define length(start, end) (*(end) - *(start) + 1)
231 /* Two ranges conflict if one doesn't end before the other starts */
232 #define ranged_conflict(starta, enda, startb, endb) \
233 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
235 #define cannot_compare(flags) \
236 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
238 int pnp_check_port(struct pnp_dev *dev, int idx)
241 struct pnp_dev *tdev;
242 resource_size_t *port, *end, *tport, *tend;
243 port = &dev->res.port_resource[idx].start;
244 end = &dev->res.port_resource[idx].end;
246 /* if the resource doesn't exist, don't complain about it */
247 if (cannot_compare(dev->res.port_resource[idx].flags))
250 /* check if the resource is already in use, skip if the
251 * device is active because it itself may be in use */
253 if (__check_region(&ioport_resource, *port, length(port, end)))
257 /* check if the resource is reserved */
258 for (tmp = 0; tmp < 8; tmp++) {
259 int rport = pnp_reserve_io[tmp << 1];
260 int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
261 if (ranged_conflict(port, end, &rport, &rend))
265 /* check for internal conflicts */
266 for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
267 if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
268 tport = &dev->res.port_resource[tmp].start;
269 tend = &dev->res.port_resource[tmp].end;
270 if (ranged_conflict(port, end, tport, tend))
275 /* check for conflicts with other pnp devices */
276 pnp_for_each_dev(tdev) {
279 for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
280 if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
282 (tdev->res.port_resource[tmp].flags))
284 tport = &tdev->res.port_resource[tmp].start;
285 tend = &tdev->res.port_resource[tmp].end;
286 if (ranged_conflict(port, end, tport, tend))
295 int pnp_check_mem(struct pnp_dev *dev, int idx)
298 struct pnp_dev *tdev;
299 resource_size_t *addr, *end, *taddr, *tend;
300 addr = &dev->res.mem_resource[idx].start;
301 end = &dev->res.mem_resource[idx].end;
303 /* if the resource doesn't exist, don't complain about it */
304 if (cannot_compare(dev->res.mem_resource[idx].flags))
307 /* check if the resource is already in use, skip if the
308 * device is active because it itself may be in use */
310 if (check_mem_region(*addr, length(addr, end)))
314 /* check if the resource is reserved */
315 for (tmp = 0; tmp < 8; tmp++) {
316 int raddr = pnp_reserve_mem[tmp << 1];
317 int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
318 if (ranged_conflict(addr, end, &raddr, &rend))
322 /* check for internal conflicts */
323 for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
324 if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
325 taddr = &dev->res.mem_resource[tmp].start;
326 tend = &dev->res.mem_resource[tmp].end;
327 if (ranged_conflict(addr, end, taddr, tend))
332 /* check for conflicts with other pnp devices */
333 pnp_for_each_dev(tdev) {
336 for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
337 if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
339 (tdev->res.mem_resource[tmp].flags))
341 taddr = &tdev->res.mem_resource[tmp].start;
342 tend = &tdev->res.mem_resource[tmp].end;
343 if (ranged_conflict(addr, end, taddr, tend))
352 static irqreturn_t pnp_test_handler(int irq, void *dev_id)
357 int pnp_check_irq(struct pnp_dev *dev, int idx)
360 struct pnp_dev *tdev;
361 resource_size_t *irq = &dev->res.irq_resource[idx].start;
363 /* if the resource doesn't exist, don't complain about it */
364 if (cannot_compare(dev->res.irq_resource[idx].flags))
367 /* check if the resource is valid */
368 if (*irq < 0 || *irq > 15)
371 /* check if the resource is reserved */
372 for (tmp = 0; tmp < 16; tmp++) {
373 if (pnp_reserve_irq[tmp] == *irq)
377 /* check for internal conflicts */
378 for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
379 if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
380 if (dev->res.irq_resource[tmp].start == *irq)
386 /* check if the resource is being used by a pci device */
388 struct pci_dev *pci = NULL;
389 for_each_pci_dev(pci) {
390 if (pci->irq == *irq)
396 /* check if the resource is already in use, skip if the
397 * device is active because it itself may be in use */
399 if (request_irq(*irq, pnp_test_handler,
400 IRQF_DISABLED | IRQF_PROBE_SHARED, "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) {
412 (tdev->res.irq_resource[tmp].flags))
414 if ((tdev->res.irq_resource[tmp].start == *irq))
423 int pnp_check_dma(struct pnp_dev *dev, int idx)
427 struct pnp_dev *tdev;
428 resource_size_t *dma = &dev->res.dma_resource[idx].start;
430 /* if the resource doesn't exist, don't complain about it */
431 if (cannot_compare(dev->res.dma_resource[idx].flags))
434 /* check if the resource is valid */
435 if (*dma < 0 || *dma == 4 || *dma > 7)
438 /* check if the resource is reserved */
439 for (tmp = 0; tmp < 8; tmp++) {
440 if (pnp_reserve_dma[tmp] == *dma)
444 /* check for internal conflicts */
445 for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
446 if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
447 if (dev->res.dma_resource[tmp].start == *dma)
452 /* check if the resource is already in use, skip if the
453 * device is active because it itself may be in use */
455 if (request_dma(*dma, "pnp"))
460 /* check for conflicts with other pnp devices */
461 pnp_for_each_dev(tdev) {
464 for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
465 if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
467 (tdev->res.dma_resource[tmp].flags))
469 if ((tdev->res.dma_resource[tmp].start == *dma))
477 /* IA64 hasn't legacy DMA */
483 EXPORT_SYMBOL(pnp_register_dependent_option);
484 EXPORT_SYMBOL(pnp_register_independent_option);
485 EXPORT_SYMBOL(pnp_register_irq_resource);
486 EXPORT_SYMBOL(pnp_register_dma_resource);
487 EXPORT_SYMBOL(pnp_register_port_resource);
488 EXPORT_SYMBOL(pnp_register_mem_resource);
491 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
493 static int __init pnp_setup_reserve_irq(char *str)
497 for (i = 0; i < 16; i++)
498 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
503 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
505 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
507 static int __init pnp_setup_reserve_dma(char *str)
511 for (i = 0; i < 8; i++)
512 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
517 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
519 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
521 static int __init pnp_setup_reserve_io(char *str)
525 for (i = 0; i < 16; i++)
526 if (get_option(&str, &pnp_reserve_io[i]) != 2)
531 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
533 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
535 static int __init pnp_setup_reserve_mem(char *str)
539 for (i = 0; i < 16; i++)
540 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
545 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);