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
;
57 dev_dbg(&dev
->dev
, "new independent option\n");
61 struct pnp_option
*pnp_register_dependent_option(struct pnp_dev
*dev
,
64 struct pnp_option
*option
;
66 option
= pnp_build_option(priority
);
69 struct pnp_option
*parent
= dev
->dependent
;
71 parent
= parent
->next
;
72 parent
->next
= option
;
74 dev
->dependent
= option
;
76 dev_dbg(&dev
->dev
, "new dependent option (priority %#x)\n", priority
);
80 int pnp_register_irq_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
85 char buf
[PNP_IRQ_NR
]; /* hex-encoded, so this is overkill but safe */
89 while (ptr
&& ptr
->next
)
100 for (i
= 0; i
< 16; i
++)
101 if (test_bit(i
, data
->map
))
102 pcibios_penalize_isa_irq(i
, 0);
107 bitmap_scnprintf(buf
, sizeof(buf
), data
->map
, PNP_IRQ_NR
);
108 dev_dbg(&dev
->dev
, " irq bitmask %s flags %#x\n", buf
,
114 int pnp_register_dma_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
115 struct pnp_dma
*data
)
120 while (ptr
&& ptr
->next
)
127 dev_dbg(&dev
->dev
, " dma bitmask %#x flags %#x\n", data
->map
,
132 int pnp_register_port_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
133 struct pnp_port
*data
)
135 struct pnp_port
*ptr
;
138 while (ptr
&& ptr
->next
)
145 dev_dbg(&dev
->dev
, " io "
146 "min %#x max %#x align %d size %d flags %#x\n",
147 data
->min
, data
->max
, data
->align
, data
->size
, data
->flags
);
151 int pnp_register_mem_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
152 struct pnp_mem
*data
)
157 while (ptr
&& ptr
->next
)
164 dev_dbg(&dev
->dev
, " mem "
165 "min %#x max %#x align %d size %d flags %#x\n",
166 data
->min
, data
->max
, data
->align
, data
->size
, data
->flags
);
170 static void pnp_free_port(struct pnp_port
*port
)
172 struct pnp_port
*next
;
181 static void pnp_free_irq(struct pnp_irq
*irq
)
183 struct pnp_irq
*next
;
192 static void pnp_free_dma(struct pnp_dma
*dma
)
194 struct pnp_dma
*next
;
203 static void pnp_free_mem(struct pnp_mem
*mem
)
205 struct pnp_mem
*next
;
214 void pnp_free_option(struct pnp_option
*option
)
216 struct pnp_option
*next
;
220 pnp_free_port(option
->port
);
221 pnp_free_irq(option
->irq
);
222 pnp_free_dma(option
->dma
);
223 pnp_free_mem(option
->mem
);
230 * resource validity checking
233 #define length(start, end) (*(end) - *(start) + 1)
235 /* Two ranges conflict if one doesn't end before the other starts */
236 #define ranged_conflict(starta, enda, startb, endb) \
237 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
239 #define cannot_compare(flags) \
240 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
242 int pnp_check_port(struct pnp_dev
*dev
, int idx
)
245 struct pnp_dev
*tdev
;
246 struct resource
*res
, *tres
;
247 resource_size_t
*port
, *end
, *tport
, *tend
;
249 res
= &dev
->res
.port_resource
[idx
];
253 /* if the resource doesn't exist, don't complain about it */
254 if (cannot_compare(res
->flags
))
257 /* check if the resource is already in use, skip if the
258 * device is active because it itself may be in use */
260 if (__check_region(&ioport_resource
, *port
, length(port
, end
)))
264 /* check if the resource is reserved */
265 for (i
= 0; i
< 8; i
++) {
266 int rport
= pnp_reserve_io
[i
<< 1];
267 int rend
= pnp_reserve_io
[(i
<< 1) + 1] + rport
- 1;
268 if (ranged_conflict(port
, end
, &rport
, &rend
))
272 /* check for internal conflicts */
273 for (i
= 0; i
< PNP_MAX_PORT
; i
++) {
274 tres
= &dev
->res
.port_resource
[i
];
275 if (tres
!= res
&& tres
->flags
& IORESOURCE_IO
) {
276 tport
= &tres
->start
;
278 if (ranged_conflict(port
, end
, tport
, tend
))
283 /* check for conflicts with other pnp devices */
284 pnp_for_each_dev(tdev
) {
287 for (i
= 0; i
< PNP_MAX_PORT
; i
++) {
288 tres
= &tdev
->res
.port_resource
[i
];
289 if (tres
->flags
& IORESOURCE_IO
) {
290 if (cannot_compare(tres
->flags
))
292 tport
= &tres
->start
;
294 if (ranged_conflict(port
, end
, tport
, tend
))
303 int pnp_check_mem(struct pnp_dev
*dev
, int idx
)
306 struct pnp_dev
*tdev
;
307 struct resource
*res
, *tres
;
308 resource_size_t
*addr
, *end
, *taddr
, *tend
;
310 res
= &dev
->res
.mem_resource
[idx
];
314 /* if the resource doesn't exist, don't complain about it */
315 if (cannot_compare(res
->flags
))
318 /* check if the resource is already in use, skip if the
319 * device is active because it itself may be in use */
321 if (check_mem_region(*addr
, length(addr
, end
)))
325 /* check if the resource is reserved */
326 for (i
= 0; i
< 8; i
++) {
327 int raddr
= pnp_reserve_mem
[i
<< 1];
328 int rend
= pnp_reserve_mem
[(i
<< 1) + 1] + raddr
- 1;
329 if (ranged_conflict(addr
, end
, &raddr
, &rend
))
333 /* check for internal conflicts */
334 for (i
= 0; i
< PNP_MAX_MEM
; i
++) {
335 tres
= &dev
->res
.mem_resource
[i
];
336 if (tres
!= res
&& tres
->flags
& IORESOURCE_MEM
) {
337 taddr
= &tres
->start
;
339 if (ranged_conflict(addr
, end
, taddr
, tend
))
344 /* check for conflicts with other pnp devices */
345 pnp_for_each_dev(tdev
) {
348 for (i
= 0; i
< PNP_MAX_MEM
; i
++) {
349 tres
= &tdev
->res
.mem_resource
[i
];
350 if (tres
->flags
& IORESOURCE_MEM
) {
351 if (cannot_compare(tres
->flags
))
353 taddr
= &tres
->start
;
355 if (ranged_conflict(addr
, end
, taddr
, tend
))
364 static irqreturn_t
pnp_test_handler(int irq
, void *dev_id
)
369 int pnp_check_irq(struct pnp_dev
*dev
, int idx
)
372 struct pnp_dev
*tdev
;
373 struct resource
*res
, *tres
;
374 resource_size_t
*irq
;
376 res
= &dev
->res
.irq_resource
[idx
];
379 /* if the resource doesn't exist, don't complain about it */
380 if (cannot_compare(res
->flags
))
383 /* check if the resource is valid */
384 if (*irq
< 0 || *irq
> 15)
387 /* check if the resource is reserved */
388 for (i
= 0; i
< 16; i
++) {
389 if (pnp_reserve_irq
[i
] == *irq
)
393 /* check for internal conflicts */
394 for (i
= 0; i
< PNP_MAX_IRQ
; i
++) {
395 tres
= &dev
->res
.irq_resource
[i
];
396 if (tres
!= res
&& tres
->flags
& IORESOURCE_IRQ
) {
397 if (tres
->start
== *irq
)
403 /* check if the resource is being used by a pci device */
405 struct pci_dev
*pci
= NULL
;
406 for_each_pci_dev(pci
) {
407 if (pci
->irq
== *irq
) {
415 /* check if the resource is already in use, skip if the
416 * device is active because it itself may be in use */
418 if (request_irq(*irq
, pnp_test_handler
,
419 IRQF_DISABLED
| IRQF_PROBE_SHARED
, "pnp", NULL
))
421 free_irq(*irq
, NULL
);
424 /* check for conflicts with other pnp devices */
425 pnp_for_each_dev(tdev
) {
428 for (i
= 0; i
< PNP_MAX_IRQ
; i
++) {
429 tres
= &tdev
->res
.irq_resource
[i
];
430 if (tres
->flags
& IORESOURCE_IRQ
) {
431 if (cannot_compare(tres
->flags
))
433 if (tres
->start
== *irq
)
442 int pnp_check_dma(struct pnp_dev
*dev
, int idx
)
446 struct pnp_dev
*tdev
;
447 struct resource
*res
, *tres
;
448 resource_size_t
*dma
;
450 res
= &dev
->res
.dma_resource
[idx
];
453 /* if the resource doesn't exist, don't complain about it */
454 if (cannot_compare(res
->flags
))
457 /* check if the resource is valid */
458 if (*dma
< 0 || *dma
== 4 || *dma
> 7)
461 /* check if the resource is reserved */
462 for (i
= 0; i
< 8; i
++) {
463 if (pnp_reserve_dma
[i
] == *dma
)
467 /* check for internal conflicts */
468 for (i
= 0; i
< PNP_MAX_DMA
; i
++) {
469 tres
= &dev
->res
.dma_resource
[i
];
470 if (tres
!= res
&& tres
->flags
& IORESOURCE_DMA
) {
471 if (tres
->start
== *dma
)
476 /* check if the resource is already in use, skip if the
477 * device is active because it itself may be in use */
479 if (request_dma(*dma
, "pnp"))
484 /* check for conflicts with other pnp devices */
485 pnp_for_each_dev(tdev
) {
488 for (i
= 0; i
< PNP_MAX_DMA
; i
++) {
489 tres
= &tdev
->res
.dma_resource
[i
];
490 if (tres
->flags
& IORESOURCE_DMA
) {
491 if (cannot_compare(tres
->flags
))
493 if (tres
->start
== *dma
)
501 /* IA64 does not have legacy DMA */
506 struct resource
*pnp_get_resource(struct pnp_dev
*dev
,
507 unsigned int type
, unsigned int num
)
509 struct pnp_resource_table
*res
= &dev
->res
;
513 if (num
>= PNP_MAX_PORT
)
515 return &res
->port_resource
[num
];
517 if (num
>= PNP_MAX_MEM
)
519 return &res
->mem_resource
[num
];
521 if (num
>= PNP_MAX_IRQ
)
523 return &res
->irq_resource
[num
];
525 if (num
>= PNP_MAX_DMA
)
527 return &res
->dma_resource
[num
];
531 EXPORT_SYMBOL(pnp_get_resource
);
533 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
534 static int __init
pnp_setup_reserve_irq(char *str
)
538 for (i
= 0; i
< 16; i
++)
539 if (get_option(&str
, &pnp_reserve_irq
[i
]) != 2)
544 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq
);
546 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
547 static int __init
pnp_setup_reserve_dma(char *str
)
551 for (i
= 0; i
< 8; i
++)
552 if (get_option(&str
, &pnp_reserve_dma
[i
]) != 2)
557 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma
);
559 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
560 static int __init
pnp_setup_reserve_io(char *str
)
564 for (i
= 0; i
< 16; i
++)
565 if (get_option(&str
, &pnp_reserve_io
[i
]) != 2)
570 __setup("pnp_reserve_io=", pnp_setup_reserve_io
);
572 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
573 static int __init
pnp_setup_reserve_mem(char *str
)
577 for (i
= 0; i
< 16; i
++)
578 if (get_option(&str
, &pnp_reserve_mem
[i
]) != 2)
583 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem
);