2 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
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/errno.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/pnp.h>
15 #include <linux/slab.h>
16 #include <linux/bitmap.h>
19 DECLARE_MUTEX(pnp_res_mutex
);
21 static int pnp_assign_port(struct pnp_dev
*dev
, struct pnp_port
*rule
, int idx
)
23 unsigned long *start
, *end
, *flags
;
28 if (idx
>= PNP_MAX_PORT
) {
29 pnp_err("More than 4 ports is incompatible with pnp specifications.");
30 /* pretend we were successful so at least the manager won't try again */
34 /* check if this resource has been manually set, if so skip */
35 if (!(dev
->res
.port_resource
[idx
].flags
& IORESOURCE_AUTO
))
38 start
= &dev
->res
.port_resource
[idx
].start
;
39 end
= &dev
->res
.port_resource
[idx
].end
;
40 flags
= &dev
->res
.port_resource
[idx
].flags
;
42 /* set the initial values */
43 *flags
|= rule
->flags
| IORESOURCE_IO
;
44 *flags
&= ~IORESOURCE_UNSET
;
47 *flags
|= IORESOURCE_DISABLED
;
48 return 1; /* skip disabled resource requests */
52 *end
= *start
+ rule
->size
- 1;
54 /* run through until pnp_check_port is happy */
55 while (!pnp_check_port(dev
, idx
)) {
56 *start
+= rule
->align
;
57 *end
= *start
+ rule
->size
- 1;
58 if (*start
> rule
->max
|| !rule
->align
)
64 static int pnp_assign_mem(struct pnp_dev
*dev
, struct pnp_mem
*rule
, int idx
)
66 unsigned long *start
, *end
, *flags
;
71 if (idx
>= PNP_MAX_MEM
) {
72 pnp_err("More than 8 mems is incompatible with pnp specifications.");
73 /* pretend we were successful so at least the manager won't try again */
77 /* check if this resource has been manually set, if so skip */
78 if (!(dev
->res
.mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
81 start
= &dev
->res
.mem_resource
[idx
].start
;
82 end
= &dev
->res
.mem_resource
[idx
].end
;
83 flags
= &dev
->res
.mem_resource
[idx
].flags
;
85 /* set the initial values */
86 *flags
|= rule
->flags
| IORESOURCE_MEM
;
87 *flags
&= ~IORESOURCE_UNSET
;
89 /* convert pnp flags to standard Linux flags */
90 if (!(rule
->flags
& IORESOURCE_MEM_WRITEABLE
))
91 *flags
|= IORESOURCE_READONLY
;
92 if (rule
->flags
& IORESOURCE_MEM_CACHEABLE
)
93 *flags
|= IORESOURCE_CACHEABLE
;
94 if (rule
->flags
& IORESOURCE_MEM_RANGELENGTH
)
95 *flags
|= IORESOURCE_RANGELENGTH
;
96 if (rule
->flags
& IORESOURCE_MEM_SHADOWABLE
)
97 *flags
|= IORESOURCE_SHADOWABLE
;
100 *flags
|= IORESOURCE_DISABLED
;
101 return 1; /* skip disabled resource requests */
105 *end
= *start
+ rule
->size
-1;
107 /* run through until pnp_check_mem is happy */
108 while (!pnp_check_mem(dev
, idx
)) {
109 *start
+= rule
->align
;
110 *end
= *start
+ rule
->size
- 1;
111 if (*start
> rule
->max
|| !rule
->align
)
117 static int pnp_assign_irq(struct pnp_dev
* dev
, struct pnp_irq
*rule
, int idx
)
119 unsigned long *start
, *end
, *flags
;
122 /* IRQ priority: this table is good for i386 */
123 static unsigned short xtab
[16] = {
124 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
130 if (idx
>= PNP_MAX_IRQ
) {
131 pnp_err("More than 2 irqs is incompatible with pnp specifications.");
132 /* pretend we were successful so at least the manager won't try again */
136 /* check if this resource has been manually set, if so skip */
137 if (!(dev
->res
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
140 start
= &dev
->res
.irq_resource
[idx
].start
;
141 end
= &dev
->res
.irq_resource
[idx
].end
;
142 flags
= &dev
->res
.irq_resource
[idx
].flags
;
144 /* set the initial values */
145 *flags
|= rule
->flags
| IORESOURCE_IRQ
;
146 *flags
&= ~IORESOURCE_UNSET
;
148 if (bitmap_empty(rule
->map
, PNP_IRQ_NR
)) {
149 *flags
|= IORESOURCE_DISABLED
;
150 return 1; /* skip disabled resource requests */
153 /* TBD: need check for >16 IRQ */
154 *start
= find_next_bit(rule
->map
, PNP_IRQ_NR
, 16);
155 if (*start
< PNP_IRQ_NR
) {
159 for (i
= 0; i
< 16; i
++) {
160 if(test_bit(xtab
[i
], rule
->map
)) {
161 *start
= *end
= xtab
[i
];
162 if(pnp_check_irq(dev
, idx
))
169 static int pnp_assign_dma(struct pnp_dev
*dev
, struct pnp_dma
*rule
, int idx
)
171 unsigned long *start
, *end
, *flags
;
174 /* DMA priority: this table is good for i386 */
175 static unsigned short xtab
[8] = {
176 1, 3, 5, 6, 7, 0, 2, 4
182 if (idx
>= PNP_MAX_DMA
) {
183 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
184 /* pretend we were successful so at least the manager won't try again */
188 /* check if this resource has been manually set, if so skip */
189 if (!(dev
->res
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
192 start
= &dev
->res
.dma_resource
[idx
].start
;
193 end
= &dev
->res
.dma_resource
[idx
].end
;
194 flags
= &dev
->res
.dma_resource
[idx
].flags
;
196 /* set the initial values */
197 *flags
|= rule
->flags
| IORESOURCE_DMA
;
198 *flags
&= ~IORESOURCE_UNSET
;
201 *flags
|= IORESOURCE_DISABLED
;
202 return 1; /* skip disabled resource requests */
205 for (i
= 0; i
< 8; i
++) {
206 if(rule
->map
& (1<<xtab
[i
])) {
207 *start
= *end
= xtab
[i
];
208 if(pnp_check_dma(dev
, idx
))
216 * pnp_init_resources - Resets a resource table to default values.
217 * @table: pointer to the desired resource table
220 void pnp_init_resource_table(struct pnp_resource_table
*table
)
223 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
224 table
->irq_resource
[idx
].name
= NULL
;
225 table
->irq_resource
[idx
].start
= -1;
226 table
->irq_resource
[idx
].end
= -1;
227 table
->irq_resource
[idx
].flags
= IORESOURCE_IRQ
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
229 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
230 table
->dma_resource
[idx
].name
= NULL
;
231 table
->dma_resource
[idx
].start
= -1;
232 table
->dma_resource
[idx
].end
= -1;
233 table
->dma_resource
[idx
].flags
= IORESOURCE_DMA
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
235 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
236 table
->port_resource
[idx
].name
= NULL
;
237 table
->port_resource
[idx
].start
= 0;
238 table
->port_resource
[idx
].end
= 0;
239 table
->port_resource
[idx
].flags
= IORESOURCE_IO
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
241 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
242 table
->mem_resource
[idx
].name
= NULL
;
243 table
->mem_resource
[idx
].start
= 0;
244 table
->mem_resource
[idx
].end
= 0;
245 table
->mem_resource
[idx
].flags
= IORESOURCE_MEM
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
250 * pnp_clean_resources - clears resources that were not manually set
251 * @res: the resources to clean
254 static void pnp_clean_resource_table(struct pnp_resource_table
* res
)
257 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
258 if (!(res
->irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
260 res
->irq_resource
[idx
].start
= -1;
261 res
->irq_resource
[idx
].end
= -1;
262 res
->irq_resource
[idx
].flags
= IORESOURCE_IRQ
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
264 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
265 if (!(res
->dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
267 res
->dma_resource
[idx
].start
= -1;
268 res
->dma_resource
[idx
].end
= -1;
269 res
->dma_resource
[idx
].flags
= IORESOURCE_DMA
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
271 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
272 if (!(res
->port_resource
[idx
].flags
& IORESOURCE_AUTO
))
274 res
->port_resource
[idx
].start
= 0;
275 res
->port_resource
[idx
].end
= 0;
276 res
->port_resource
[idx
].flags
= IORESOURCE_IO
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
278 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
279 if (!(res
->mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
281 res
->mem_resource
[idx
].start
= 0;
282 res
->mem_resource
[idx
].end
= 0;
283 res
->mem_resource
[idx
].flags
= IORESOURCE_MEM
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
288 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
289 * @dev: pointer to the desired device
290 * @depnum: the dependent function number
292 * Only set depnum to 0 if the device does not have dependent options.
294 static int pnp_assign_resources(struct pnp_dev
*dev
, int depnum
)
296 struct pnp_port
*port
;
300 int nport
= 0, nmem
= 0, nirq
= 0, ndma
= 0;
302 if (!pnp_can_configure(dev
))
305 down(&pnp_res_mutex
);
306 pnp_clean_resource_table(&dev
->res
); /* start with a fresh slate */
307 if (dev
->independent
) {
308 port
= dev
->independent
->port
;
309 mem
= dev
->independent
->mem
;
310 irq
= dev
->independent
->irq
;
311 dma
= dev
->independent
->dma
;
313 if (!pnp_assign_port(dev
, port
, nport
))
319 if (!pnp_assign_mem(dev
, mem
, nmem
))
325 if (!pnp_assign_irq(dev
, irq
, nirq
))
331 if (!pnp_assign_dma(dev
, dma
, ndma
))
339 struct pnp_option
*dep
;
341 for (i
=1,dep
=dev
->dependent
; i
<depnum
; i
++, dep
=dep
->next
)
349 if (!pnp_assign_port(dev
, port
, nport
))
355 if (!pnp_assign_mem(dev
, mem
, nmem
))
361 if (!pnp_assign_irq(dev
, irq
, nirq
))
367 if (!pnp_assign_dma(dev
, dma
, ndma
))
372 } else if (dev
->dependent
)
379 pnp_clean_resource_table(&dev
->res
);
385 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
386 * @dev: pointer to the desired device
387 * @res: pointer to the new resource config
388 * @mode: 0 or PNP_CONFIG_FORCE
390 * This function can be used by drivers that want to manually set thier resources.
392 int pnp_manual_config_dev(struct pnp_dev
*dev
, struct pnp_resource_table
* res
, int mode
)
395 struct pnp_resource_table
* bak
;
398 if (!pnp_can_configure(dev
))
400 bak
= pnp_alloc(sizeof(struct pnp_resource_table
));
405 down(&pnp_res_mutex
);
407 if (!(mode
& PNP_CONFIG_FORCE
)) {
408 for (i
= 0; i
< PNP_MAX_PORT
; i
++) {
409 if(!pnp_check_port(dev
,i
))
412 for (i
= 0; i
< PNP_MAX_MEM
; i
++) {
413 if(!pnp_check_mem(dev
,i
))
416 for (i
= 0; i
< PNP_MAX_IRQ
; i
++) {
417 if(!pnp_check_irq(dev
,i
))
420 for (i
= 0; i
< PNP_MAX_DMA
; i
++) {
421 if(!pnp_check_dma(dev
,i
))
438 * pnp_auto_config_dev - automatically assigns resources to a device
439 * @dev: pointer to the desired device
442 int pnp_auto_config_dev(struct pnp_dev
*dev
)
444 struct pnp_option
*dep
;
450 if(!pnp_can_configure(dev
)) {
451 pnp_info("Device %s does not support resource configuration.", dev
->dev
.bus_id
);
455 if (!dev
->dependent
) {
456 if (pnp_assign_resources(dev
, 0))
459 dep
= dev
->dependent
;
461 if (pnp_assign_resources(dev
, i
))
468 pnp_err("Unable to assign resources to device %s.", dev
->dev
.bus_id
);
473 * pnp_activate_dev - activates a PnP device for use
474 * @dev: pointer to the desired device
476 * does not validate or set resources so be careful.
478 int pnp_activate_dev(struct pnp_dev
*dev
)
483 return 0; /* the device is already active */
486 /* ensure resources are allocated */
487 if (pnp_auto_config_dev(dev
))
490 if (!pnp_can_write(dev
)) {
491 pnp_info("Device %s does not supported activation.", dev
->dev
.bus_id
);
495 if (dev
->protocol
->set(dev
, &dev
->res
)<0) {
496 pnp_err("Failed to activate device %s.", dev
->dev
.bus_id
);
501 pnp_info("Device %s activated.", dev
->dev
.bus_id
);
507 * pnp_disable_dev - disables device
508 * @dev: pointer to the desired device
510 * inform the correct pnp protocol so that resources can be used by other devices
512 int pnp_disable_dev(struct pnp_dev
*dev
)
517 return 0; /* the device is already disabled */
520 if (!pnp_can_disable(dev
)) {
521 pnp_info("Device %s does not supported disabling.", dev
->dev
.bus_id
);
524 if (dev
->protocol
->disable(dev
)<0) {
525 pnp_err("Failed to disable device %s.", dev
->dev
.bus_id
);
530 pnp_info("Device %s disabled.", dev
->dev
.bus_id
);
532 /* release the resources so that other devices can use them */
533 down(&pnp_res_mutex
);
534 pnp_clean_resource_table(&dev
->res
);
541 * pnp_resource_change - change one resource
542 * @resource: pointer to resource to be changed
543 * @start: start of region
544 * @size: size of region
547 void pnp_resource_change(struct resource
*resource
, unsigned long start
, unsigned long size
)
549 if (resource
== NULL
)
551 resource
->flags
&= ~(IORESOURCE_AUTO
| IORESOURCE_UNSET
);
552 resource
->start
= start
;
553 resource
->end
= start
+ size
- 1;
557 EXPORT_SYMBOL(pnp_manual_config_dev
);
559 EXPORT_SYMBOL(pnp_auto_config_dev
);
561 EXPORT_SYMBOL(pnp_activate_dev
);
562 EXPORT_SYMBOL(pnp_disable_dev
);
563 EXPORT_SYMBOL(pnp_resource_change
);
564 EXPORT_SYMBOL(pnp_init_resource_table
);