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
,
81 pnp_irq_mask_t
*map
, unsigned char flags
)
83 struct pnp_irq
*data
, *ptr
;
85 char buf
[PNP_IRQ_NR
]; /* hex-encoded, so this is overkill but safe */
88 data
= kzalloc(sizeof(struct pnp_irq
), GFP_KERNEL
);
96 while (ptr
&& ptr
->next
)
107 for (i
= 0; i
< 16; i
++)
108 if (test_bit(i
, data
->map
.bits
))
109 pcibios_penalize_isa_irq(i
, 0);
114 bitmap_scnprintf(buf
, sizeof(buf
), data
->map
.bits
, PNP_IRQ_NR
);
115 dev_dbg(&dev
->dev
, " irq bitmask %s flags %#x\n", buf
,
121 int pnp_register_dma_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
122 unsigned char map
, unsigned char flags
)
124 struct pnp_dma
*data
, *ptr
;
126 data
= kzalloc(sizeof(struct pnp_dma
), GFP_KERNEL
);
134 while (ptr
&& ptr
->next
)
141 dev_dbg(&dev
->dev
, " dma bitmask %#x flags %#x\n", data
->map
,
146 int pnp_register_port_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
147 resource_size_t min
, resource_size_t max
,
148 resource_size_t align
, resource_size_t size
,
151 struct pnp_port
*data
, *ptr
;
153 data
= kzalloc(sizeof(struct pnp_port
), GFP_KERNEL
);
164 while (ptr
&& ptr
->next
)
171 dev_dbg(&dev
->dev
, " io "
172 "min %#llx max %#llx align %lld size %lld flags %#x\n",
173 (unsigned long long) data
->min
,
174 (unsigned long long) data
->max
,
175 (unsigned long long) data
->align
,
176 (unsigned long long) data
->size
, data
->flags
);
180 int pnp_register_mem_resource(struct pnp_dev
*dev
, struct pnp_option
*option
,
181 resource_size_t min
, resource_size_t max
,
182 resource_size_t align
, resource_size_t size
,
185 struct pnp_mem
*data
, *ptr
;
187 data
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
198 while (ptr
&& ptr
->next
)
205 dev_dbg(&dev
->dev
, " mem "
206 "min %#llx max %#llx align %lld size %lld flags %#x\n",
207 (unsigned long long) data
->min
,
208 (unsigned long long) data
->max
,
209 (unsigned long long) data
->align
,
210 (unsigned long long) data
->size
, data
->flags
);
214 static void pnp_free_port(struct pnp_port
*port
)
216 struct pnp_port
*next
;
225 static void pnp_free_irq(struct pnp_irq
*irq
)
227 struct pnp_irq
*next
;
236 static void pnp_free_dma(struct pnp_dma
*dma
)
238 struct pnp_dma
*next
;
247 static void pnp_free_mem(struct pnp_mem
*mem
)
249 struct pnp_mem
*next
;
258 void pnp_free_option(struct pnp_option
*option
)
260 struct pnp_option
*next
;
264 pnp_free_port(option
->port
);
265 pnp_free_irq(option
->irq
);
266 pnp_free_dma(option
->dma
);
267 pnp_free_mem(option
->mem
);
274 * resource validity checking
277 #define length(start, end) (*(end) - *(start) + 1)
279 /* Two ranges conflict if one doesn't end before the other starts */
280 #define ranged_conflict(starta, enda, startb, endb) \
281 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
283 #define cannot_compare(flags) \
284 ((flags) & IORESOURCE_DISABLED)
286 int pnp_check_port(struct pnp_dev
*dev
, struct resource
*res
)
289 struct pnp_dev
*tdev
;
290 struct resource
*tres
;
291 resource_size_t
*port
, *end
, *tport
, *tend
;
296 /* if the resource doesn't exist, don't complain about it */
297 if (cannot_compare(res
->flags
))
300 /* check if the resource is already in use, skip if the
301 * device is active because it itself may be in use */
303 if (__check_region(&ioport_resource
, *port
, length(port
, end
)))
307 /* check if the resource is reserved */
308 for (i
= 0; i
< 8; i
++) {
309 int rport
= pnp_reserve_io
[i
<< 1];
310 int rend
= pnp_reserve_io
[(i
<< 1) + 1] + rport
- 1;
311 if (ranged_conflict(port
, end
, &rport
, &rend
))
315 /* check for internal conflicts */
316 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_IO
, i
)); i
++) {
317 if (tres
!= res
&& tres
->flags
& IORESOURCE_IO
) {
318 tport
= &tres
->start
;
320 if (ranged_conflict(port
, end
, tport
, tend
))
325 /* check for conflicts with other pnp devices */
326 pnp_for_each_dev(tdev
) {
330 (tres
= pnp_get_resource(tdev
, IORESOURCE_IO
, i
));
332 if (tres
->flags
& IORESOURCE_IO
) {
333 if (cannot_compare(tres
->flags
))
335 tport
= &tres
->start
;
337 if (ranged_conflict(port
, end
, tport
, tend
))
346 int pnp_check_mem(struct pnp_dev
*dev
, struct resource
*res
)
349 struct pnp_dev
*tdev
;
350 struct resource
*tres
;
351 resource_size_t
*addr
, *end
, *taddr
, *tend
;
356 /* if the resource doesn't exist, don't complain about it */
357 if (cannot_compare(res
->flags
))
360 /* check if the resource is already in use, skip if the
361 * device is active because it itself may be in use */
363 if (check_mem_region(*addr
, length(addr
, end
)))
367 /* check if the resource is reserved */
368 for (i
= 0; i
< 8; i
++) {
369 int raddr
= pnp_reserve_mem
[i
<< 1];
370 int rend
= pnp_reserve_mem
[(i
<< 1) + 1] + raddr
- 1;
371 if (ranged_conflict(addr
, end
, &raddr
, &rend
))
375 /* check for internal conflicts */
376 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_MEM
, i
)); i
++) {
377 if (tres
!= res
&& tres
->flags
& IORESOURCE_MEM
) {
378 taddr
= &tres
->start
;
380 if (ranged_conflict(addr
, end
, taddr
, tend
))
385 /* check for conflicts with other pnp devices */
386 pnp_for_each_dev(tdev
) {
390 (tres
= pnp_get_resource(tdev
, IORESOURCE_MEM
, i
));
392 if (tres
->flags
& IORESOURCE_MEM
) {
393 if (cannot_compare(tres
->flags
))
395 taddr
= &tres
->start
;
397 if (ranged_conflict(addr
, end
, taddr
, tend
))
406 static irqreturn_t
pnp_test_handler(int irq
, void *dev_id
)
411 int pnp_check_irq(struct pnp_dev
*dev
, struct resource
*res
)
414 struct pnp_dev
*tdev
;
415 struct resource
*tres
;
416 resource_size_t
*irq
;
420 /* if the resource doesn't exist, don't complain about it */
421 if (cannot_compare(res
->flags
))
424 /* check if the resource is valid */
425 if (*irq
< 0 || *irq
> 15)
428 /* check if the resource is reserved */
429 for (i
= 0; i
< 16; i
++) {
430 if (pnp_reserve_irq
[i
] == *irq
)
434 /* check for internal conflicts */
435 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_IRQ
, i
)); i
++) {
436 if (tres
!= res
&& tres
->flags
& IORESOURCE_IRQ
) {
437 if (tres
->start
== *irq
)
443 /* check if the resource is being used by a pci device */
445 struct pci_dev
*pci
= NULL
;
446 for_each_pci_dev(pci
) {
447 if (pci
->irq
== *irq
) {
455 /* check if the resource is already in use, skip if the
456 * device is active because it itself may be in use */
458 if (request_irq(*irq
, pnp_test_handler
,
459 IRQF_DISABLED
| IRQF_PROBE_SHARED
, "pnp", NULL
))
461 free_irq(*irq
, NULL
);
464 /* check for conflicts with other pnp devices */
465 pnp_for_each_dev(tdev
) {
469 (tres
= pnp_get_resource(tdev
, IORESOURCE_IRQ
, i
));
471 if (tres
->flags
& IORESOURCE_IRQ
) {
472 if (cannot_compare(tres
->flags
))
474 if (tres
->start
== *irq
)
483 int pnp_check_dma(struct pnp_dev
*dev
, struct resource
*res
)
487 struct pnp_dev
*tdev
;
488 struct resource
*tres
;
489 resource_size_t
*dma
;
493 /* if the resource doesn't exist, don't complain about it */
494 if (cannot_compare(res
->flags
))
497 /* check if the resource is valid */
498 if (*dma
< 0 || *dma
== 4 || *dma
> 7)
501 /* check if the resource is reserved */
502 for (i
= 0; i
< 8; i
++) {
503 if (pnp_reserve_dma
[i
] == *dma
)
507 /* check for internal conflicts */
508 for (i
= 0; (tres
= pnp_get_resource(dev
, IORESOURCE_DMA
, i
)); i
++) {
509 if (tres
!= res
&& tres
->flags
& IORESOURCE_DMA
) {
510 if (tres
->start
== *dma
)
515 /* check if the resource is already in use, skip if the
516 * device is active because it itself may be in use */
518 if (request_dma(*dma
, "pnp"))
523 /* check for conflicts with other pnp devices */
524 pnp_for_each_dev(tdev
) {
528 (tres
= pnp_get_resource(tdev
, IORESOURCE_DMA
, i
));
530 if (tres
->flags
& IORESOURCE_DMA
) {
531 if (cannot_compare(tres
->flags
))
533 if (tres
->start
== *dma
)
541 /* IA64 does not have legacy DMA */
546 int pnp_resource_type(struct resource
*res
)
548 return res
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
|
549 IORESOURCE_IRQ
| IORESOURCE_DMA
);
552 struct resource
*pnp_get_resource(struct pnp_dev
*dev
,
553 unsigned int type
, unsigned int num
)
555 struct pnp_resource
*pnp_res
;
556 struct resource
*res
;
558 list_for_each_entry(pnp_res
, &dev
->resources
, list
) {
560 if (pnp_resource_type(res
) == type
&& num
-- == 0)
565 EXPORT_SYMBOL(pnp_get_resource
);
567 static struct pnp_resource
*pnp_new_resource(struct pnp_dev
*dev
)
569 struct pnp_resource
*pnp_res
;
571 pnp_res
= kzalloc(sizeof(struct pnp_resource
), GFP_KERNEL
);
575 list_add_tail(&pnp_res
->list
, &dev
->resources
);
579 struct pnp_resource
*pnp_add_irq_resource(struct pnp_dev
*dev
, int irq
,
582 struct pnp_resource
*pnp_res
;
583 struct resource
*res
;
585 pnp_res
= pnp_new_resource(dev
);
587 dev_err(&dev
->dev
, "can't add resource for IRQ %d\n", irq
);
592 res
->flags
= IORESOURCE_IRQ
| flags
;
596 dev_dbg(&dev
->dev
, " add irq %d flags %#x\n", irq
, flags
);
600 struct pnp_resource
*pnp_add_dma_resource(struct pnp_dev
*dev
, int dma
,
603 struct pnp_resource
*pnp_res
;
604 struct resource
*res
;
606 pnp_res
= pnp_new_resource(dev
);
608 dev_err(&dev
->dev
, "can't add resource for DMA %d\n", dma
);
613 res
->flags
= IORESOURCE_DMA
| flags
;
617 dev_dbg(&dev
->dev
, " add dma %d flags %#x\n", dma
, flags
);
621 struct pnp_resource
*pnp_add_io_resource(struct pnp_dev
*dev
,
622 resource_size_t start
,
623 resource_size_t end
, int flags
)
625 struct pnp_resource
*pnp_res
;
626 struct resource
*res
;
628 pnp_res
= pnp_new_resource(dev
);
630 dev_err(&dev
->dev
, "can't add resource for IO %#llx-%#llx\n",
631 (unsigned long long) start
,
632 (unsigned long long) end
);
637 res
->flags
= IORESOURCE_IO
| flags
;
641 dev_dbg(&dev
->dev
, " add io %#llx-%#llx flags %#x\n",
642 (unsigned long long) start
, (unsigned long long) end
, flags
);
646 struct pnp_resource
*pnp_add_mem_resource(struct pnp_dev
*dev
,
647 resource_size_t start
,
648 resource_size_t end
, int flags
)
650 struct pnp_resource
*pnp_res
;
651 struct resource
*res
;
653 pnp_res
= pnp_new_resource(dev
);
655 dev_err(&dev
->dev
, "can't add resource for MEM %#llx-%#llx\n",
656 (unsigned long long) start
,
657 (unsigned long long) end
);
662 res
->flags
= IORESOURCE_MEM
| flags
;
666 dev_dbg(&dev
->dev
, " add mem %#llx-%#llx flags %#x\n",
667 (unsigned long long) start
, (unsigned long long) end
, flags
);
671 static int pnp_possible_option(struct pnp_option
*option
, int type
,
672 resource_size_t start
, resource_size_t size
)
674 struct pnp_option
*tmp
;
675 struct pnp_port
*port
;
683 for (tmp
= option
; tmp
; tmp
= tmp
->next
) {
686 for (port
= tmp
->port
; port
; port
= port
->next
) {
687 if (port
->min
== start
&& port
->size
== size
)
692 for (mem
= tmp
->mem
; mem
; mem
= mem
->next
) {
693 if (mem
->min
== start
&& mem
->size
== size
)
698 for (irq
= tmp
->irq
; irq
; irq
= irq
->next
) {
699 if (start
< PNP_IRQ_NR
&&
700 test_bit(start
, irq
->map
.bits
))
705 for (dma
= tmp
->dma
; dma
; dma
= dma
->next
) {
706 if (dma
->map
& (1 << start
))
717 * Determine whether the specified resource is a possible configuration
720 int pnp_possible_config(struct pnp_dev
*dev
, int type
, resource_size_t start
,
721 resource_size_t size
)
723 if (pnp_possible_option(dev
->independent
, type
, start
, size
))
726 if (pnp_possible_option(dev
->dependent
, type
, start
, size
))
731 EXPORT_SYMBOL(pnp_possible_config
);
733 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
734 static int __init
pnp_setup_reserve_irq(char *str
)
738 for (i
= 0; i
< 16; i
++)
739 if (get_option(&str
, &pnp_reserve_irq
[i
]) != 2)
744 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq
);
746 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
747 static int __init
pnp_setup_reserve_dma(char *str
)
751 for (i
= 0; i
< 8; i
++)
752 if (get_option(&str
, &pnp_reserve_dma
[i
]) != 2)
757 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma
);
759 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
760 static int __init
pnp_setup_reserve_io(char *str
)
764 for (i
= 0; i
< 16; i
++)
765 if (get_option(&str
, &pnp_reserve_io
[i
]) != 2)
770 __setup("pnp_reserve_io=", pnp_setup_reserve_io
);
772 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
773 static int __init
pnp_setup_reserve_mem(char *str
)
777 for (i
= 0; i
< 16; i
++)
778 if (get_option(&str
, &pnp_reserve_mem
[i
]) != 2)
783 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem
);