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 */
33 static struct pnp_option * pnp_build_option(int priority)
35 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
37 /* check if pnp_alloc ran out of memory */
41 option->priority = priority & 0xff;
42 /* make sure the priority is valid */
43 if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
44 option->priority = PNP_RES_PRIORITY_INVALID;
49 struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev)
51 struct pnp_option *option;
55 option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
57 /* this should never happen but if it does we'll try to continue */
59 pnp_err("independent resource already registered");
60 dev->independent = option;
64 struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority)
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);
227 * resource validity checking
230 #define length(start, end) (*(end) - *(start) + 1)
232 /* Two ranges conflict if one doesn't end before the other starts */
233 #define ranged_conflict(starta, enda, startb, endb) \
234 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
236 #define cannot_compare(flags) \
237 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
239 int pnp_check_port(struct pnp_dev * dev, int idx)
242 struct pnp_dev *tdev;
243 resource_size_t *port, *end, *tport, *tend;
244 port = &dev->res.port_resource[idx].start;
245 end = &dev->res.port_resource[idx].end;
247 /* if the resource doesn't exist, don't complain about it */
248 if (cannot_compare(dev->res.port_resource[idx].flags))
251 /* check if the resource is already in use, skip if the
252 * device is active because it itself may be in use */
254 if (__check_region(&ioport_resource, *port, length(port,end)))
258 /* check if the resource is reserved */
259 for (tmp = 0; tmp < 8; tmp++) {
260 int rport = pnp_reserve_io[tmp << 1];
261 int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
262 if (ranged_conflict(port,end,&rport,&rend))
266 /* check for internal conflicts */
267 for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
268 if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
269 tport = &dev->res.port_resource[tmp].start;
270 tend = &dev->res.port_resource[tmp].end;
271 if (ranged_conflict(port,end,tport,tend))
276 /* check for conflicts with other pnp devices */
277 pnp_for_each_dev(tdev) {
280 for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
281 if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
282 if (cannot_compare(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) {
338 if (cannot_compare(tdev->res.mem_resource[tmp].flags))
340 taddr = &tdev->res.mem_resource[tmp].start;
341 tend = &tdev->res.mem_resource[tmp].end;
342 if (ranged_conflict(addr,end,taddr,tend))
351 static irqreturn_t pnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
356 int pnp_check_irq(struct pnp_dev * dev, int idx)
359 struct pnp_dev *tdev;
360 resource_size_t * irq = &dev->res.irq_resource[idx].start;
362 /* if the resource doesn't exist, don't complain about it */
363 if (cannot_compare(dev->res.irq_resource[idx].flags))
366 /* check if the resource is valid */
367 if (*irq < 0 || *irq > 15)
370 /* check if the resource is reserved */
371 for (tmp = 0; tmp < 16; tmp++) {
372 if (pnp_reserve_irq[tmp] == *irq)
376 /* check for internal conflicts */
377 for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
378 if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
379 if (dev->res.irq_resource[tmp].start == *irq)
385 /* check if the resource is being used by a pci device */
387 struct pci_dev *pci = NULL;
388 for_each_pci_dev(pci) {
389 if (pci->irq == *irq)
395 /* check if the resource is already in use, skip if the
396 * device is active because it itself may be in use */
398 if (request_irq(*irq, pnp_test_handler,
399 IRQF_DISABLED|IRQF_PROBE_SHARED, "pnp", NULL))
401 free_irq(*irq, NULL);
404 /* check for conflicts with other pnp devices */
405 pnp_for_each_dev(tdev) {
408 for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
409 if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
410 if (cannot_compare(tdev->res.irq_resource[tmp].flags))
412 if ((tdev->res.irq_resource[tmp].start == *irq))
421 int pnp_check_dma(struct pnp_dev * dev, int idx)
425 struct pnp_dev *tdev;
426 resource_size_t * 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))
474 /* IA64 hasn't legacy DMA */
481 EXPORT_SYMBOL(pnp_register_dependent_option);
482 EXPORT_SYMBOL(pnp_register_independent_option);
483 EXPORT_SYMBOL(pnp_register_irq_resource);
484 EXPORT_SYMBOL(pnp_register_dma_resource);
485 EXPORT_SYMBOL(pnp_register_port_resource);
486 EXPORT_SYMBOL(pnp_register_mem_resource);
490 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
492 static int __init pnp_setup_reserve_irq(char *str)
496 for (i = 0; i < 16; i++)
497 if (get_option(&str,&pnp_reserve_irq[i]) != 2)
502 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
504 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
506 static int __init pnp_setup_reserve_dma(char *str)
510 for (i = 0; i < 8; i++)
511 if (get_option(&str,&pnp_reserve_dma[i]) != 2)
516 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
518 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
520 static int __init pnp_setup_reserve_io(char *str)
524 for (i = 0; i < 16; i++)
525 if (get_option(&str,&pnp_reserve_io[i]) != 2)
530 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
532 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
534 static int __init pnp_setup_reserve_mem(char *str)
538 for (i = 0; i < 16; i++)
539 if (get_option(&str,&pnp_reserve_mem[i]) != 2)
544 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);