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 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
.bits
))
102 pcibios_penalize_isa_irq(i
, 0);
107 bitmap_scnprintf(buf
, sizeof(buf
), data
->map
.bits
, 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 %#llx max %#llx align %lld size %lld flags %#x\n",
147 (unsigned long long) data
->min
,
148 (unsigned long long) data
->max
,
149 (unsigned long long) data
->align
,
150 (unsigned long long) data
->size
, data
->flags
);
154 int pnp_register_mem_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
155 struct pnp_mem
*data
)
160 while (ptr
&& ptr
->next
)
167 dev_dbg(&dev
->dev
, " mem "
168 "min %#llx max %#llx align %lld size %lld flags %#x\n",
169 (unsigned long long) data
->min
,
170 (unsigned long long) data
->max
,
171 (unsigned long long) data
->align
,
172 (unsigned long long) data
->size
, data
->flags
);
176 static void pnp_free_port(struct pnp_port
*port
)
178 struct pnp_port
*next
;
187 static void pnp_free_irq(struct pnp_irq
*irq
)
189 struct pnp_irq
*next
;
198 static void pnp_free_dma(struct pnp_dma
*dma
)
200 struct pnp_dma
*next
;
209 static void pnp_free_mem(struct pnp_mem
*mem
)
211 struct pnp_mem
*next
;
220 void pnp_free_option(struct pnp_option
*option
)
222 struct pnp_option
*next
;
226 pnp_free_port(option
->port
);
227 pnp_free_irq(option
->irq
);
228 pnp_free_dma(option
->dma
);
229 pnp_free_mem(option
->mem
);
236 * resource validity checking
239 #define length(start, end) (*(end) - *(start) + 1)
241 /* Two ranges conflict if one doesn't end before the other starts */
242 #define ranged_conflict(starta, enda, startb, endb) \
243 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
245 #define cannot_compare(flags) \
246 ((flags) & IORESOURCE_DISABLED)
248 int pnp_check_port(struct pnp_dev
*dev
, struct resource
*res
)
251 struct pnp_dev
*tdev
;
252 struct resource
*tres
;
253 resource_size_t
*port
, *end
, *tport
, *tend
;
258 /* if the resource doesn't exist, don't complain about it */
259 if (cannot_compare(res
->flags
))
262 /* check if the resource is already in use, skip if the
263 * device is active because it itself may be in use */
265 if (__check_region(&ioport_resource
, *port
, length(port
, end
)))
269 /* check if the resource is reserved */
270 for (i
= 0; i
< 8; i
++) {
271 int rport
= pnp_reserve_io
[i
<< 1];
272 int rend
= pnp_reserve_io
[(i
<< 1) + 1] + rport
- 1;
273 if (ranged_conflict(port
, end
, &rport
, &rend
))
277 /* check for internal conflicts */
278 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_IO
, i
)); i
++) {
279 if (tres
!= res
&& tres
->flags
& IORESOURCE_IO
) {
280 tport
= &tres
->start
;
282 if (ranged_conflict(port
, end
, tport
, tend
))
287 /* check for conflicts with other pnp devices */
288 pnp_for_each_dev(tdev
) {
292 (tres
= pnp_get_resource(tdev
, IORESOURCE_IO
, i
));
294 if (tres
->flags
& IORESOURCE_IO
) {
295 if (cannot_compare(tres
->flags
))
297 tport
= &tres
->start
;
299 if (ranged_conflict(port
, end
, tport
, tend
))
308 int pnp_check_mem(struct pnp_dev
*dev
, struct resource
*res
)
311 struct pnp_dev
*tdev
;
312 struct resource
*tres
;
313 resource_size_t
*addr
, *end
, *taddr
, *tend
;
318 /* if the resource doesn't exist, don't complain about it */
319 if (cannot_compare(res
->flags
))
322 /* check if the resource is already in use, skip if the
323 * device is active because it itself may be in use */
325 if (check_mem_region(*addr
, length(addr
, end
)))
329 /* check if the resource is reserved */
330 for (i
= 0; i
< 8; i
++) {
331 int raddr
= pnp_reserve_mem
[i
<< 1];
332 int rend
= pnp_reserve_mem
[(i
<< 1) + 1] + raddr
- 1;
333 if (ranged_conflict(addr
, end
, &raddr
, &rend
))
337 /* check for internal conflicts */
338 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_MEM
, i
)); i
++) {
339 if (tres
!= res
&& tres
->flags
& IORESOURCE_MEM
) {
340 taddr
= &tres
->start
;
342 if (ranged_conflict(addr
, end
, taddr
, tend
))
347 /* check for conflicts with other pnp devices */
348 pnp_for_each_dev(tdev
) {
352 (tres
= pnp_get_resource(tdev
, IORESOURCE_MEM
, i
));
354 if (tres
->flags
& IORESOURCE_MEM
) {
355 if (cannot_compare(tres
->flags
))
357 taddr
= &tres
->start
;
359 if (ranged_conflict(addr
, end
, taddr
, tend
))
368 static irqreturn_t
pnp_test_handler(int irq
, void *dev_id
)
373 int pnp_check_irq(struct pnp_dev
*dev
, struct resource
*res
)
376 struct pnp_dev
*tdev
;
377 struct resource
*tres
;
378 resource_size_t
*irq
;
382 /* if the resource doesn't exist, don't complain about it */
383 if (cannot_compare(res
->flags
))
386 /* check if the resource is valid */
387 if (*irq
< 0 || *irq
> 15)
390 /* check if the resource is reserved */
391 for (i
= 0; i
< 16; i
++) {
392 if (pnp_reserve_irq
[i
] == *irq
)
396 /* check for internal conflicts */
397 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_IRQ
, i
)); i
++) {
398 if (tres
!= res
&& tres
->flags
& IORESOURCE_IRQ
) {
399 if (tres
->start
== *irq
)
405 /* check if the resource is being used by a pci device */
407 struct pci_dev
*pci
= NULL
;
408 for_each_pci_dev(pci
) {
409 if (pci
->irq
== *irq
) {
417 /* check if the resource is already in use, skip if the
418 * device is active because it itself may be in use */
420 if (request_irq(*irq
, pnp_test_handler
,
421 IRQF_DISABLED
| IRQF_PROBE_SHARED
, "pnp", NULL
))
423 free_irq(*irq
, NULL
);
426 /* check for conflicts with other pnp devices */
427 pnp_for_each_dev(tdev
) {
431 (tres
= pnp_get_resource(tdev
, IORESOURCE_IRQ
, i
));
433 if (tres
->flags
& IORESOURCE_IRQ
) {
434 if (cannot_compare(tres
->flags
))
436 if (tres
->start
== *irq
)
445 int pnp_check_dma(struct pnp_dev
*dev
, struct resource
*res
)
449 struct pnp_dev
*tdev
;
450 struct resource
*tres
;
451 resource_size_t
*dma
;
455 /* if the resource doesn't exist, don't complain about it */
456 if (cannot_compare(res
->flags
))
459 /* check if the resource is valid */
460 if (*dma
< 0 || *dma
== 4 || *dma
> 7)
463 /* check if the resource is reserved */
464 for (i
= 0; i
< 8; i
++) {
465 if (pnp_reserve_dma
[i
] == *dma
)
469 /* check for internal conflicts */
470 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_DMA
, i
)); i
++) {
471 if (tres
!= res
&& tres
->flags
& IORESOURCE_DMA
) {
472 if (tres
->start
== *dma
)
477 /* check if the resource is already in use, skip if the
478 * device is active because it itself may be in use */
480 if (request_dma(*dma
, "pnp"))
485 /* check for conflicts with other pnp devices */
486 pnp_for_each_dev(tdev
) {
490 (tres
= pnp_get_resource(tdev
, IORESOURCE_DMA
, i
));
492 if (tres
->flags
& IORESOURCE_DMA
) {
493 if (cannot_compare(tres
->flags
))
495 if (tres
->start
== *dma
)
503 /* IA64 does not have legacy DMA */
508 int pnp_resource_type(struct resource
*res
)
510 return res
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
|
511 IORESOURCE_IRQ
| IORESOURCE_DMA
);
514 struct resource
*pnp_get_resource(struct pnp_dev
*dev
,
515 unsigned int type
, unsigned int num
)
517 struct pnp_resource
*pnp_res
;
518 struct resource
*res
;
520 list_for_each_entry(pnp_res
, &dev
->resources
, list
) {
522 if (pnp_resource_type(res
) == type
&& num
-- == 0)
527 EXPORT_SYMBOL(pnp_get_resource
);
529 static struct pnp_resource
*pnp_new_resource(struct pnp_dev
*dev
)
531 struct pnp_resource
*pnp_res
;
533 pnp_res
= kzalloc(sizeof(struct pnp_resource
), GFP_KERNEL
);
537 list_add_tail(&pnp_res
->list
, &dev
->resources
);
541 struct pnp_resource
*pnp_add_irq_resource(struct pnp_dev
*dev
, int irq
,
544 struct pnp_resource
*pnp_res
;
545 struct resource
*res
;
547 pnp_res
= pnp_new_resource(dev
);
549 dev_err(&dev
->dev
, "can't add resource for IRQ %d\n", irq
);
554 res
->flags
= IORESOURCE_IRQ
| flags
;
558 dev_dbg(&dev
->dev
, " add irq %d flags %#x\n", irq
, flags
);
562 struct pnp_resource
*pnp_add_dma_resource(struct pnp_dev
*dev
, int dma
,
565 struct pnp_resource
*pnp_res
;
566 struct resource
*res
;
568 pnp_res
= pnp_new_resource(dev
);
570 dev_err(&dev
->dev
, "can't add resource for DMA %d\n", dma
);
575 res
->flags
= IORESOURCE_DMA
| flags
;
579 dev_dbg(&dev
->dev
, " add dma %d flags %#x\n", dma
, flags
);
583 struct pnp_resource
*pnp_add_io_resource(struct pnp_dev
*dev
,
584 resource_size_t start
,
585 resource_size_t end
, int flags
)
587 struct pnp_resource
*pnp_res
;
588 struct resource
*res
;
590 pnp_res
= pnp_new_resource(dev
);
592 dev_err(&dev
->dev
, "can't add resource for IO %#llx-%#llx\n",
593 (unsigned long long) start
,
594 (unsigned long long) end
);
599 res
->flags
= IORESOURCE_IO
| flags
;
603 dev_dbg(&dev
->dev
, " add io %#llx-%#llx flags %#x\n",
604 (unsigned long long) start
, (unsigned long long) end
, flags
);
608 struct pnp_resource
*pnp_add_mem_resource(struct pnp_dev
*dev
,
609 resource_size_t start
,
610 resource_size_t end
, int flags
)
612 struct pnp_resource
*pnp_res
;
613 struct resource
*res
;
615 pnp_res
= pnp_new_resource(dev
);
617 dev_err(&dev
->dev
, "can't add resource for MEM %#llx-%#llx\n",
618 (unsigned long long) start
,
619 (unsigned long long) end
);
624 res
->flags
= IORESOURCE_MEM
| flags
;
628 dev_dbg(&dev
->dev
, " add mem %#llx-%#llx flags %#x\n",
629 (unsigned long long) start
, (unsigned long long) end
, flags
);
633 static int pnp_possible_option(struct pnp_option
*option
, int type
,
634 resource_size_t start
, resource_size_t size
)
636 struct pnp_option
*tmp
;
637 struct pnp_port
*port
;
645 for (tmp
= option
; tmp
; tmp
= tmp
->next
) {
648 for (port
= tmp
->port
; port
; port
= port
->next
) {
649 if (port
->min
== start
&& port
->size
== size
)
654 for (mem
= tmp
->mem
; mem
; mem
= mem
->next
) {
655 if (mem
->min
== start
&& mem
->size
== size
)
660 for (irq
= tmp
->irq
; irq
; irq
= irq
->next
) {
661 if (start
< PNP_IRQ_NR
&&
662 test_bit(start
, irq
->map
.bits
))
667 for (dma
= tmp
->dma
; dma
; dma
= dma
->next
) {
668 if (dma
->map
& (1 << start
))
679 * Determine whether the specified resource is a possible configuration
682 int pnp_possible_config(struct pnp_dev
*dev
, int type
, resource_size_t start
,
683 resource_size_t size
)
685 if (pnp_possible_option(dev
->independent
, type
, start
, size
))
688 if (pnp_possible_option(dev
->dependent
, type
, start
, size
))
693 EXPORT_SYMBOL(pnp_possible_config
);
695 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
696 static int __init
pnp_setup_reserve_irq(char *str
)
700 for (i
= 0; i
< 16; i
++)
701 if (get_option(&str
, &pnp_reserve_irq
[i
]) != 2)
706 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq
);
708 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
709 static int __init
pnp_setup_reserve_dma(char *str
)
713 for (i
= 0; i
< 8; i
++)
714 if (get_option(&str
, &pnp_reserve_dma
[i
]) != 2)
719 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma
);
721 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
722 static int __init
pnp_setup_reserve_io(char *str
)
726 for (i
= 0; i
< 16; i
++)
727 if (get_option(&str
, &pnp_reserve_io
[i
]) != 2)
732 __setup("pnp_reserve_io=", pnp_setup_reserve_io
);
734 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
735 static int __init
pnp_setup_reserve_mem(char *str
)
739 for (i
= 0; i
< 16; i
++)
740 if (get_option(&str
, &pnp_reserve_mem
[i
]) != 2)
745 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem
);