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>
8 #include <linux/errno.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/pnp.h>
13 #include <linux/slab.h>
14 #include <linux/bitmap.h>
17 DECLARE_MUTEX(pnp_res_mutex
);
19 static int pnp_assign_port(struct pnp_dev
*dev
, struct pnp_port
*rule
, int idx
)
21 resource_size_t
*start
, *end
;
27 if (idx
>= PNP_MAX_PORT
) {
29 ("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 resource_size_t
*start
, *end
;
72 if (idx
>= PNP_MAX_MEM
) {
74 ("More than 8 mems is incompatible with pnp specifications.");
75 /* pretend we were successful so at least the manager won't try again */
79 /* check if this resource has been manually set, if so skip */
80 if (!(dev
->res
.mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
83 start
= &dev
->res
.mem_resource
[idx
].start
;
84 end
= &dev
->res
.mem_resource
[idx
].end
;
85 flags
= &dev
->res
.mem_resource
[idx
].flags
;
87 /* set the initial values */
88 *flags
|= rule
->flags
| IORESOURCE_MEM
;
89 *flags
&= ~IORESOURCE_UNSET
;
91 /* convert pnp flags to standard Linux flags */
92 if (!(rule
->flags
& IORESOURCE_MEM_WRITEABLE
))
93 *flags
|= IORESOURCE_READONLY
;
94 if (rule
->flags
& IORESOURCE_MEM_CACHEABLE
)
95 *flags
|= IORESOURCE_CACHEABLE
;
96 if (rule
->flags
& IORESOURCE_MEM_RANGELENGTH
)
97 *flags
|= IORESOURCE_RANGELENGTH
;
98 if (rule
->flags
& IORESOURCE_MEM_SHADOWABLE
)
99 *flags
|= IORESOURCE_SHADOWABLE
;
102 *flags
|= IORESOURCE_DISABLED
;
103 return 1; /* skip disabled resource requests */
107 *end
= *start
+ rule
->size
- 1;
109 /* run through until pnp_check_mem is happy */
110 while (!pnp_check_mem(dev
, idx
)) {
111 *start
+= rule
->align
;
112 *end
= *start
+ rule
->size
- 1;
113 if (*start
> rule
->max
|| !rule
->align
)
119 static int pnp_assign_irq(struct pnp_dev
*dev
, struct pnp_irq
*rule
, int idx
)
121 resource_size_t
*start
, *end
;
122 unsigned long *flags
;
125 /* IRQ priority: this table is good for i386 */
126 static unsigned short xtab
[16] = {
127 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
133 if (idx
>= PNP_MAX_IRQ
) {
135 ("More than 2 irqs is incompatible with pnp specifications.");
136 /* pretend we were successful so at least the manager won't try again */
140 /* check if this resource has been manually set, if so skip */
141 if (!(dev
->res
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
144 start
= &dev
->res
.irq_resource
[idx
].start
;
145 end
= &dev
->res
.irq_resource
[idx
].end
;
146 flags
= &dev
->res
.irq_resource
[idx
].flags
;
148 /* set the initial values */
149 *flags
|= rule
->flags
| IORESOURCE_IRQ
;
150 *flags
&= ~IORESOURCE_UNSET
;
152 if (bitmap_empty(rule
->map
, PNP_IRQ_NR
)) {
153 *flags
|= IORESOURCE_DISABLED
;
154 return 1; /* skip disabled resource requests */
157 /* TBD: need check for >16 IRQ */
158 *start
= find_next_bit(rule
->map
, PNP_IRQ_NR
, 16);
159 if (*start
< PNP_IRQ_NR
) {
163 for (i
= 0; i
< 16; i
++) {
164 if (test_bit(xtab
[i
], rule
->map
)) {
165 *start
= *end
= xtab
[i
];
166 if (pnp_check_irq(dev
, idx
))
173 static int pnp_assign_dma(struct pnp_dev
*dev
, struct pnp_dma
*rule
, int idx
)
175 resource_size_t
*start
, *end
;
176 unsigned long *flags
;
179 /* DMA priority: this table is good for i386 */
180 static unsigned short xtab
[8] = {
181 1, 3, 5, 6, 7, 0, 2, 4
187 if (idx
>= PNP_MAX_DMA
) {
189 ("More than 2 dmas is incompatible with pnp specifications.");
190 /* pretend we were successful so at least the manager won't try again */
194 /* check if this resource has been manually set, if so skip */
195 if (!(dev
->res
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
198 start
= &dev
->res
.dma_resource
[idx
].start
;
199 end
= &dev
->res
.dma_resource
[idx
].end
;
200 flags
= &dev
->res
.dma_resource
[idx
].flags
;
202 /* set the initial values */
203 *flags
|= rule
->flags
| IORESOURCE_DMA
;
204 *flags
&= ~IORESOURCE_UNSET
;
207 *flags
|= IORESOURCE_DISABLED
;
208 return 1; /* skip disabled resource requests */
211 for (i
= 0; i
< 8; i
++) {
212 if (rule
->map
& (1 << xtab
[i
])) {
213 *start
= *end
= xtab
[i
];
214 if (pnp_check_dma(dev
, idx
))
222 * pnp_init_resources - Resets a resource table to default values.
223 * @table: pointer to the desired resource table
225 void pnp_init_resource_table(struct pnp_resource_table
*table
)
229 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
230 table
->irq_resource
[idx
].name
= NULL
;
231 table
->irq_resource
[idx
].start
= -1;
232 table
->irq_resource
[idx
].end
= -1;
233 table
->irq_resource
[idx
].flags
=
234 IORESOURCE_IRQ
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
236 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
237 table
->dma_resource
[idx
].name
= NULL
;
238 table
->dma_resource
[idx
].start
= -1;
239 table
->dma_resource
[idx
].end
= -1;
240 table
->dma_resource
[idx
].flags
=
241 IORESOURCE_DMA
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
243 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
244 table
->port_resource
[idx
].name
= NULL
;
245 table
->port_resource
[idx
].start
= 0;
246 table
->port_resource
[idx
].end
= 0;
247 table
->port_resource
[idx
].flags
=
248 IORESOURCE_IO
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
250 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
251 table
->mem_resource
[idx
].name
= NULL
;
252 table
->mem_resource
[idx
].start
= 0;
253 table
->mem_resource
[idx
].end
= 0;
254 table
->mem_resource
[idx
].flags
=
255 IORESOURCE_MEM
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
260 * pnp_clean_resources - clears resources that were not manually set
261 * @res: the resources to clean
263 static void pnp_clean_resource_table(struct pnp_resource_table
*res
)
267 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
268 if (!(res
->irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
270 res
->irq_resource
[idx
].start
= -1;
271 res
->irq_resource
[idx
].end
= -1;
272 res
->irq_resource
[idx
].flags
=
273 IORESOURCE_IRQ
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
275 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
276 if (!(res
->dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
278 res
->dma_resource
[idx
].start
= -1;
279 res
->dma_resource
[idx
].end
= -1;
280 res
->dma_resource
[idx
].flags
=
281 IORESOURCE_DMA
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
283 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
284 if (!(res
->port_resource
[idx
].flags
& IORESOURCE_AUTO
))
286 res
->port_resource
[idx
].start
= 0;
287 res
->port_resource
[idx
].end
= 0;
288 res
->port_resource
[idx
].flags
=
289 IORESOURCE_IO
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
291 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
292 if (!(res
->mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
294 res
->mem_resource
[idx
].start
= 0;
295 res
->mem_resource
[idx
].end
= 0;
296 res
->mem_resource
[idx
].flags
=
297 IORESOURCE_MEM
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
302 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
303 * @dev: pointer to the desired device
304 * @depnum: the dependent function number
306 * Only set depnum to 0 if the device does not have dependent options.
308 static int pnp_assign_resources(struct pnp_dev
*dev
, int depnum
)
310 struct pnp_port
*port
;
314 int nport
= 0, nmem
= 0, nirq
= 0, ndma
= 0;
316 if (!pnp_can_configure(dev
))
319 down(&pnp_res_mutex
);
320 pnp_clean_resource_table(&dev
->res
); /* start with a fresh slate */
321 if (dev
->independent
) {
322 port
= dev
->independent
->port
;
323 mem
= dev
->independent
->mem
;
324 irq
= dev
->independent
->irq
;
325 dma
= dev
->independent
->dma
;
327 if (!pnp_assign_port(dev
, port
, nport
))
333 if (!pnp_assign_mem(dev
, mem
, nmem
))
339 if (!pnp_assign_irq(dev
, irq
, nirq
))
345 if (!pnp_assign_dma(dev
, dma
, ndma
))
353 struct pnp_option
*dep
;
355 for (i
= 1, dep
= dev
->dependent
; i
< depnum
;
356 i
++, dep
= dep
->next
)
364 if (!pnp_assign_port(dev
, port
, nport
))
370 if (!pnp_assign_mem(dev
, mem
, nmem
))
376 if (!pnp_assign_irq(dev
, irq
, nirq
))
382 if (!pnp_assign_dma(dev
, dma
, ndma
))
387 } else if (dev
->dependent
)
394 pnp_clean_resource_table(&dev
->res
);
400 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
401 * @dev: pointer to the desired device
402 * @res: pointer to the new resource config
403 * @mode: 0 or PNP_CONFIG_FORCE
405 * This function can be used by drivers that want to manually set thier resources.
407 int pnp_manual_config_dev(struct pnp_dev
*dev
, struct pnp_resource_table
*res
,
411 struct pnp_resource_table
*bak
;
415 if (!pnp_can_configure(dev
))
417 bak
= pnp_alloc(sizeof(struct pnp_resource_table
));
422 down(&pnp_res_mutex
);
424 if (!(mode
& PNP_CONFIG_FORCE
)) {
425 for (i
= 0; i
< PNP_MAX_PORT
; i
++) {
426 if (!pnp_check_port(dev
, i
))
429 for (i
= 0; i
< PNP_MAX_MEM
; i
++) {
430 if (!pnp_check_mem(dev
, i
))
433 for (i
= 0; i
< PNP_MAX_IRQ
; i
++) {
434 if (!pnp_check_irq(dev
, i
))
437 for (i
= 0; i
< PNP_MAX_DMA
; i
++) {
438 if (!pnp_check_dma(dev
, i
))
455 * pnp_auto_config_dev - automatically assigns resources to a device
456 * @dev: pointer to the desired device
458 int pnp_auto_config_dev(struct pnp_dev
*dev
)
460 struct pnp_option
*dep
;
466 if (!pnp_can_configure(dev
)) {
467 pnp_dbg("Device %s does not support resource configuration.",
472 if (!dev
->dependent
) {
473 if (pnp_assign_resources(dev
, 0))
476 dep
= dev
->dependent
;
478 if (pnp_assign_resources(dev
, i
))
485 pnp_err("Unable to assign resources to device %s.", dev
->dev
.bus_id
);
490 * pnp_start_dev - low-level start of the PnP device
491 * @dev: pointer to the desired device
493 * assumes that resources have already been allocated
495 int pnp_start_dev(struct pnp_dev
*dev
)
497 if (!pnp_can_write(dev
)) {
498 pnp_dbg("Device %s does not support activation.",
503 if (dev
->protocol
->set(dev
, &dev
->res
) < 0) {
504 pnp_err("Failed to activate device %s.", dev
->dev
.bus_id
);
508 pnp_info("Device %s activated.", dev
->dev
.bus_id
);
513 * pnp_stop_dev - low-level disable of the PnP device
514 * @dev: pointer to the desired device
516 * does not free resources
518 int pnp_stop_dev(struct pnp_dev
*dev
)
520 if (!pnp_can_disable(dev
)) {
521 pnp_dbg("Device %s does not support disabling.",
525 if (dev
->protocol
->disable(dev
) < 0) {
526 pnp_err("Failed to disable device %s.", dev
->dev
.bus_id
);
530 pnp_info("Device %s disabled.", dev
->dev
.bus_id
);
535 * pnp_activate_dev - activates a PnP device for use
536 * @dev: pointer to the desired device
538 * does not validate or set resources so be careful.
540 int pnp_activate_dev(struct pnp_dev
*dev
)
547 return 0; /* the device is already active */
549 /* ensure resources are allocated */
550 if (pnp_auto_config_dev(dev
))
553 error
= pnp_start_dev(dev
);
562 * pnp_disable_dev - disables device
563 * @dev: pointer to the desired device
565 * inform the correct pnp protocol so that resources can be used by other devices
567 int pnp_disable_dev(struct pnp_dev
*dev
)
574 return 0; /* the device is already disabled */
576 error
= pnp_stop_dev(dev
);
582 /* release the resources so that other devices can use them */
583 down(&pnp_res_mutex
);
584 pnp_clean_resource_table(&dev
->res
);
591 * pnp_resource_change - change one resource
592 * @resource: pointer to resource to be changed
593 * @start: start of region
594 * @size: size of region
596 void pnp_resource_change(struct resource
*resource
, resource_size_t start
,
597 resource_size_t size
)
599 if (resource
== NULL
)
601 resource
->flags
&= ~(IORESOURCE_AUTO
| IORESOURCE_UNSET
);
602 resource
->start
= start
;
603 resource
->end
= start
+ size
- 1;
606 EXPORT_SYMBOL(pnp_manual_config_dev
);
607 EXPORT_SYMBOL(pnp_start_dev
);
608 EXPORT_SYMBOL(pnp_stop_dev
);
609 EXPORT_SYMBOL(pnp_activate_dev
);
610 EXPORT_SYMBOL(pnp_disable_dev
);
611 EXPORT_SYMBOL(pnp_resource_change
);
612 EXPORT_SYMBOL(pnp_init_resource_table
);