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 static int pnp_reserve_irq
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
25 static int pnp_reserve_dma
[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
26 static int pnp_reserve_io
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
27 static int pnp_reserve_mem
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
34 static struct pnp_option
* pnp_build_option(int priority
)
36 struct pnp_option
*option
= pnp_alloc(sizeof(struct pnp_option
));
38 /* check if pnp_alloc ran out of memory */
42 option
->priority
= priority
& 0xff;
43 /* make sure the priority is valid */
44 if (option
->priority
> PNP_RES_PRIORITY_FUNCTIONAL
)
45 option
->priority
= PNP_RES_PRIORITY_INVALID
;
50 struct pnp_option
* pnp_register_independent_option(struct pnp_dev
*dev
)
52 struct pnp_option
*option
;
56 option
= pnp_build_option(PNP_RES_PRIORITY_PREFERRED
);
58 /* this should never happen but if it does we'll try to continue */
60 pnp_err("independent resource already registered");
61 dev
->independent
= option
;
65 struct pnp_option
* pnp_register_dependent_option(struct pnp_dev
*dev
, int priority
)
67 struct pnp_option
*option
;
71 option
= pnp_build_option(priority
);
74 struct pnp_option
*parent
= dev
->dependent
;
76 parent
= parent
->next
;
77 parent
->next
= option
;
79 dev
->dependent
= option
;
83 int pnp_register_irq_resource(struct pnp_option
*option
, struct pnp_irq
*data
)
92 while (ptr
&& ptr
->next
)
103 for (i
= 0; i
< 16; i
++)
104 if (test_bit(i
, data
->map
))
105 pcibios_penalize_isa_irq(i
);
111 int pnp_register_dma_resource(struct pnp_option
*option
, struct pnp_dma
*data
)
120 while (ptr
&& ptr
->next
)
130 int pnp_register_port_resource(struct pnp_option
*option
, struct pnp_port
*data
)
132 struct pnp_port
*ptr
;
139 while (ptr
&& ptr
->next
)
149 int pnp_register_mem_resource(struct pnp_option
*option
, struct pnp_mem
*data
)
158 while (ptr
&& ptr
->next
)
167 static void pnp_free_port(struct pnp_port
*port
)
169 struct pnp_port
*next
;
178 static void pnp_free_irq(struct pnp_irq
*irq
)
180 struct pnp_irq
*next
;
189 static void pnp_free_dma(struct pnp_dma
*dma
)
191 struct pnp_dma
*next
;
200 static void pnp_free_mem(struct pnp_mem
*mem
)
202 struct pnp_mem
*next
;
211 void pnp_free_option(struct pnp_option
*option
)
213 struct pnp_option
*next
;
217 pnp_free_port(option
->port
);
218 pnp_free_irq(option
->irq
);
219 pnp_free_dma(option
->dma
);
220 pnp_free_mem(option
->mem
);
228 * resource validity checking
231 #define length(start, end) (*(end) - *(start) + 1)
233 /* Two ranges conflict if one doesn't end before the other starts */
234 #define ranged_conflict(starta, enda, startb, endb) \
235 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
237 #define cannot_compare(flags) \
238 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
240 int pnp_check_port(struct pnp_dev
* dev
, int idx
)
243 struct pnp_dev
*tdev
;
244 unsigned long *port
, *end
, *tport
, *tend
;
245 port
= &dev
->res
.port_resource
[idx
].start
;
246 end
= &dev
->res
.port_resource
[idx
].end
;
248 /* if the resource doesn't exist, don't complain about it */
249 if (cannot_compare(dev
->res
.port_resource
[idx
].flags
))
252 /* check if the resource is already in use, skip if the
253 * device is active because it itself may be in use */
255 if (__check_region(&ioport_resource
, *port
, length(port
,end
)))
259 /* check if the resource is reserved */
260 for (tmp
= 0; tmp
< 8; tmp
++) {
261 int rport
= pnp_reserve_io
[tmp
<< 1];
262 int rend
= pnp_reserve_io
[(tmp
<< 1) + 1] + rport
- 1;
263 if (ranged_conflict(port
,end
,&rport
,&rend
))
267 /* check for internal conflicts */
268 for (tmp
= 0; tmp
< PNP_MAX_PORT
&& tmp
!= idx
; tmp
++) {
269 if (dev
->res
.port_resource
[tmp
].flags
& IORESOURCE_IO
) {
270 tport
= &dev
->res
.port_resource
[tmp
].start
;
271 tend
= &dev
->res
.port_resource
[tmp
].end
;
272 if (ranged_conflict(port
,end
,tport
,tend
))
277 /* check for conflicts with other pnp devices */
278 pnp_for_each_dev(tdev
) {
281 for (tmp
= 0; tmp
< PNP_MAX_PORT
; tmp
++) {
282 if (tdev
->res
.port_resource
[tmp
].flags
& IORESOURCE_IO
) {
283 if (cannot_compare(tdev
->res
.port_resource
[tmp
].flags
))
285 tport
= &tdev
->res
.port_resource
[tmp
].start
;
286 tend
= &tdev
->res
.port_resource
[tmp
].end
;
287 if (ranged_conflict(port
,end
,tport
,tend
))
296 int pnp_check_mem(struct pnp_dev
* dev
, int idx
)
299 struct pnp_dev
*tdev
;
300 unsigned long *addr
, *end
, *taddr
, *tend
;
301 addr
= &dev
->res
.mem_resource
[idx
].start
;
302 end
= &dev
->res
.mem_resource
[idx
].end
;
304 /* if the resource doesn't exist, don't complain about it */
305 if (cannot_compare(dev
->res
.mem_resource
[idx
].flags
))
308 /* check if the resource is already in use, skip if the
309 * device is active because it itself may be in use */
311 if (check_mem_region(*addr
, length(addr
,end
)))
315 /* check if the resource is reserved */
316 for (tmp
= 0; tmp
< 8; tmp
++) {
317 int raddr
= pnp_reserve_mem
[tmp
<< 1];
318 int rend
= pnp_reserve_mem
[(tmp
<< 1) + 1] + raddr
- 1;
319 if (ranged_conflict(addr
,end
,&raddr
,&rend
))
323 /* check for internal conflicts */
324 for (tmp
= 0; tmp
< PNP_MAX_MEM
&& tmp
!= idx
; tmp
++) {
325 if (dev
->res
.mem_resource
[tmp
].flags
& IORESOURCE_MEM
) {
326 taddr
= &dev
->res
.mem_resource
[tmp
].start
;
327 tend
= &dev
->res
.mem_resource
[tmp
].end
;
328 if (ranged_conflict(addr
,end
,taddr
,tend
))
333 /* check for conflicts with other pnp devices */
334 pnp_for_each_dev(tdev
) {
337 for (tmp
= 0; tmp
< PNP_MAX_MEM
; tmp
++) {
338 if (tdev
->res
.mem_resource
[tmp
].flags
& IORESOURCE_MEM
) {
339 if (cannot_compare(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
, struct pt_regs
*regs
)
357 int pnp_check_irq(struct pnp_dev
* dev
, int idx
)
360 struct pnp_dev
*tdev
;
361 unsigned long * 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
, SA_INTERRUPT
, "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 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
))
474 /* IA64 hasn't legacy DMA */
480 EXPORT_SYMBOL(pnp_register_dependent_option
);
481 EXPORT_SYMBOL(pnp_register_independent_option
);
482 EXPORT_SYMBOL(pnp_register_irq_resource
);
483 EXPORT_SYMBOL(pnp_register_dma_resource
);
484 EXPORT_SYMBOL(pnp_register_port_resource
);
485 EXPORT_SYMBOL(pnp_register_mem_resource
);
488 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
490 static int __init
pnp_setup_reserve_irq(char *str
)
494 for (i
= 0; i
< 16; i
++)
495 if (get_option(&str
,&pnp_reserve_irq
[i
]) != 2)
500 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq
);
502 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
504 static int __init
pnp_setup_reserve_dma(char *str
)
508 for (i
= 0; i
< 8; i
++)
509 if (get_option(&str
,&pnp_reserve_dma
[i
]) != 2)
514 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma
);
516 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
518 static int __init
pnp_setup_reserve_io(char *str
)
522 for (i
= 0; i
< 16; i
++)
523 if (get_option(&str
,&pnp_reserve_io
[i
]) != 2)
528 __setup("pnp_reserve_io=", pnp_setup_reserve_io
);
530 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
532 static int __init
pnp_setup_reserve_mem(char *str
)
536 for (i
= 0; i
< 16; i
++)
537 if (get_option(&str
,&pnp_reserve_mem
[i
]) != 2)
542 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem
);