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>
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
19 #include <linux/pnp.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 */
31 static struct pnp_option
*pnp_build_option(int priority
)
33 struct pnp_option
*option
= pnp_alloc(sizeof(struct pnp_option
));
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
;
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 */
54 dev_err(&dev
->dev
, "independent resource already registered\n");
55 dev
->independent
= option
;
59 struct pnp_option
*pnp_register_dependent_option(struct pnp_dev
*dev
,
62 struct pnp_option
*option
;
64 option
= pnp_build_option(priority
);
67 struct pnp_option
*parent
= dev
->dependent
;
69 parent
= parent
->next
;
70 parent
->next
= option
;
72 dev
->dependent
= option
;
76 int pnp_register_irq_resource(struct pnp_option
*option
, struct pnp_irq
*data
)
81 while (ptr
&& ptr
->next
)
92 for (i
= 0; i
< 16; i
++)
93 if (test_bit(i
, data
->map
))
94 pcibios_penalize_isa_irq(i
, 0);
100 int pnp_register_dma_resource(struct pnp_option
*option
, struct pnp_dma
*data
)
105 while (ptr
&& ptr
->next
)
115 int pnp_register_port_resource(struct pnp_option
*option
, struct pnp_port
*data
)
117 struct pnp_port
*ptr
;
120 while (ptr
&& ptr
->next
)
130 int pnp_register_mem_resource(struct pnp_option
*option
, struct pnp_mem
*data
)
135 while (ptr
&& ptr
->next
)
144 static void pnp_free_port(struct pnp_port
*port
)
146 struct pnp_port
*next
;
155 static void pnp_free_irq(struct pnp_irq
*irq
)
157 struct pnp_irq
*next
;
166 static void pnp_free_dma(struct pnp_dma
*dma
)
168 struct pnp_dma
*next
;
177 static void pnp_free_mem(struct pnp_mem
*mem
)
179 struct pnp_mem
*next
;
188 void pnp_free_option(struct pnp_option
*option
)
190 struct pnp_option
*next
;
194 pnp_free_port(option
->port
);
195 pnp_free_irq(option
->irq
);
196 pnp_free_dma(option
->dma
);
197 pnp_free_mem(option
->mem
);
204 * resource validity checking
207 #define length(start, end) (*(end) - *(start) + 1)
209 /* Two ranges conflict if one doesn't end before the other starts */
210 #define ranged_conflict(starta, enda, startb, endb) \
211 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
213 #define cannot_compare(flags) \
214 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
216 int pnp_check_port(struct pnp_dev
*dev
, int idx
)
219 struct pnp_dev
*tdev
;
220 resource_size_t
*port
, *end
, *tport
, *tend
;
222 port
= &dev
->res
.port_resource
[idx
].start
;
223 end
= &dev
->res
.port_resource
[idx
].end
;
225 /* if the resource doesn't exist, don't complain about it */
226 if (cannot_compare(dev
->res
.port_resource
[idx
].flags
))
229 /* check if the resource is already in use, skip if the
230 * device is active because it itself may be in use */
232 if (__check_region(&ioport_resource
, *port
, length(port
, end
)))
236 /* check if the resource is reserved */
237 for (tmp
= 0; tmp
< 8; tmp
++) {
238 int rport
= pnp_reserve_io
[tmp
<< 1];
239 int rend
= pnp_reserve_io
[(tmp
<< 1) + 1] + rport
- 1;
240 if (ranged_conflict(port
, end
, &rport
, &rend
))
244 /* check for internal conflicts */
245 for (tmp
= 0; tmp
< PNP_MAX_PORT
&& tmp
!= idx
; tmp
++) {
246 if (dev
->res
.port_resource
[tmp
].flags
& IORESOURCE_IO
) {
247 tport
= &dev
->res
.port_resource
[tmp
].start
;
248 tend
= &dev
->res
.port_resource
[tmp
].end
;
249 if (ranged_conflict(port
, end
, tport
, tend
))
254 /* check for conflicts with other pnp devices */
255 pnp_for_each_dev(tdev
) {
258 for (tmp
= 0; tmp
< PNP_MAX_PORT
; tmp
++) {
259 if (tdev
->res
.port_resource
[tmp
].flags
& IORESOURCE_IO
) {
261 (tdev
->res
.port_resource
[tmp
].flags
))
263 tport
= &tdev
->res
.port_resource
[tmp
].start
;
264 tend
= &tdev
->res
.port_resource
[tmp
].end
;
265 if (ranged_conflict(port
, end
, tport
, tend
))
274 int pnp_check_mem(struct pnp_dev
*dev
, int idx
)
277 struct pnp_dev
*tdev
;
278 resource_size_t
*addr
, *end
, *taddr
, *tend
;
280 addr
= &dev
->res
.mem_resource
[idx
].start
;
281 end
= &dev
->res
.mem_resource
[idx
].end
;
283 /* if the resource doesn't exist, don't complain about it */
284 if (cannot_compare(dev
->res
.mem_resource
[idx
].flags
))
287 /* check if the resource is already in use, skip if the
288 * device is active because it itself may be in use */
290 if (check_mem_region(*addr
, length(addr
, end
)))
294 /* check if the resource is reserved */
295 for (tmp
= 0; tmp
< 8; tmp
++) {
296 int raddr
= pnp_reserve_mem
[tmp
<< 1];
297 int rend
= pnp_reserve_mem
[(tmp
<< 1) + 1] + raddr
- 1;
298 if (ranged_conflict(addr
, end
, &raddr
, &rend
))
302 /* check for internal conflicts */
303 for (tmp
= 0; tmp
< PNP_MAX_MEM
&& tmp
!= idx
; tmp
++) {
304 if (dev
->res
.mem_resource
[tmp
].flags
& IORESOURCE_MEM
) {
305 taddr
= &dev
->res
.mem_resource
[tmp
].start
;
306 tend
= &dev
->res
.mem_resource
[tmp
].end
;
307 if (ranged_conflict(addr
, end
, taddr
, tend
))
312 /* check for conflicts with other pnp devices */
313 pnp_for_each_dev(tdev
) {
316 for (tmp
= 0; tmp
< PNP_MAX_MEM
; tmp
++) {
317 if (tdev
->res
.mem_resource
[tmp
].flags
& IORESOURCE_MEM
) {
319 (tdev
->res
.mem_resource
[tmp
].flags
))
321 taddr
= &tdev
->res
.mem_resource
[tmp
].start
;
322 tend
= &tdev
->res
.mem_resource
[tmp
].end
;
323 if (ranged_conflict(addr
, end
, taddr
, tend
))
332 static irqreturn_t
pnp_test_handler(int irq
, void *dev_id
)
337 int pnp_check_irq(struct pnp_dev
*dev
, int idx
)
340 struct pnp_dev
*tdev
;
341 resource_size_t
*irq
= &dev
->res
.irq_resource
[idx
].start
;
343 /* if the resource doesn't exist, don't complain about it */
344 if (cannot_compare(dev
->res
.irq_resource
[idx
].flags
))
347 /* check if the resource is valid */
348 if (*irq
< 0 || *irq
> 15)
351 /* check if the resource is reserved */
352 for (tmp
= 0; tmp
< 16; tmp
++) {
353 if (pnp_reserve_irq
[tmp
] == *irq
)
357 /* check for internal conflicts */
358 for (tmp
= 0; tmp
< PNP_MAX_IRQ
&& tmp
!= idx
; tmp
++) {
359 if (dev
->res
.irq_resource
[tmp
].flags
& IORESOURCE_IRQ
) {
360 if (dev
->res
.irq_resource
[tmp
].start
== *irq
)
366 /* check if the resource is being used by a pci device */
368 struct pci_dev
*pci
= NULL
;
369 for_each_pci_dev(pci
) {
370 if (pci
->irq
== *irq
) {
378 /* check if the resource is already in use, skip if the
379 * device is active because it itself may be in use */
381 if (request_irq(*irq
, pnp_test_handler
,
382 IRQF_DISABLED
| IRQF_PROBE_SHARED
, "pnp", NULL
))
384 free_irq(*irq
, NULL
);
387 /* check for conflicts with other pnp devices */
388 pnp_for_each_dev(tdev
) {
391 for (tmp
= 0; tmp
< PNP_MAX_IRQ
; tmp
++) {
392 if (tdev
->res
.irq_resource
[tmp
].flags
& IORESOURCE_IRQ
) {
394 (tdev
->res
.irq_resource
[tmp
].flags
))
396 if ((tdev
->res
.irq_resource
[tmp
].start
== *irq
))
405 int pnp_check_dma(struct pnp_dev
*dev
, int idx
)
409 struct pnp_dev
*tdev
;
410 resource_size_t
*dma
= &dev
->res
.dma_resource
[idx
].start
;
412 /* if the resource doesn't exist, don't complain about it */
413 if (cannot_compare(dev
->res
.dma_resource
[idx
].flags
))
416 /* check if the resource is valid */
417 if (*dma
< 0 || *dma
== 4 || *dma
> 7)
420 /* check if the resource is reserved */
421 for (tmp
= 0; tmp
< 8; tmp
++) {
422 if (pnp_reserve_dma
[tmp
] == *dma
)
426 /* check for internal conflicts */
427 for (tmp
= 0; tmp
< PNP_MAX_DMA
&& tmp
!= idx
; tmp
++) {
428 if (dev
->res
.dma_resource
[tmp
].flags
& IORESOURCE_DMA
) {
429 if (dev
->res
.dma_resource
[tmp
].start
== *dma
)
434 /* check if the resource is already in use, skip if the
435 * device is active because it itself may be in use */
437 if (request_dma(*dma
, "pnp"))
442 /* check for conflicts with other pnp devices */
443 pnp_for_each_dev(tdev
) {
446 for (tmp
= 0; tmp
< PNP_MAX_DMA
; tmp
++) {
447 if (tdev
->res
.dma_resource
[tmp
].flags
& IORESOURCE_DMA
) {
449 (tdev
->res
.dma_resource
[tmp
].flags
))
451 if ((tdev
->res
.dma_resource
[tmp
].start
== *dma
))
459 /* IA64 does not have legacy DMA */
464 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
465 static int __init
pnp_setup_reserve_irq(char *str
)
469 for (i
= 0; i
< 16; i
++)
470 if (get_option(&str
, &pnp_reserve_irq
[i
]) != 2)
475 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq
);
477 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
478 static int __init
pnp_setup_reserve_dma(char *str
)
482 for (i
= 0; i
< 8; i
++)
483 if (get_option(&str
, &pnp_reserve_dma
[i
]) != 2)
488 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma
);
490 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
491 static int __init
pnp_setup_reserve_io(char *str
)
495 for (i
= 0; i
< 16; i
++)
496 if (get_option(&str
, &pnp_reserve_io
[i
]) != 2)
501 __setup("pnp_reserve_io=", pnp_setup_reserve_io
);
503 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
504 static int __init
pnp_setup_reserve_mem(char *str
)
508 for (i
= 0; i
< 16; i
++)
509 if (get_option(&str
, &pnp_reserve_mem
[i
]) != 2)
514 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem
);