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>
17 DECLARE_MUTEX(pnp_res_mutex
);
19 static int pnp_assign_port(struct pnp_dev
*dev
, struct pnp_port
*rule
, int idx
)
21 unsigned long *start
, *end
, *flags
;
26 if (idx
>= PNP_MAX_PORT
) {
27 pnp_err("More than 4 ports is incompatible with pnp specifications.");
28 /* pretend we were successful so at least the manager won't try again */
32 /* check if this resource has been manually set, if so skip */
33 if (!(dev
->res
.port_resource
[idx
].flags
& IORESOURCE_AUTO
))
36 start
= &dev
->res
.port_resource
[idx
].start
;
37 end
= &dev
->res
.port_resource
[idx
].end
;
38 flags
= &dev
->res
.port_resource
[idx
].flags
;
40 /* set the initial values */
41 *flags
|= rule
->flags
| IORESOURCE_IO
;
42 *flags
&= ~IORESOURCE_UNSET
;
45 *flags
|= IORESOURCE_DISABLED
;
46 return 1; /* skip disabled resource requests */
50 *end
= *start
+ rule
->size
- 1;
52 /* run through until pnp_check_port is happy */
53 while (!pnp_check_port(dev
, idx
)) {
54 *start
+= rule
->align
;
55 *end
= *start
+ rule
->size
- 1;
56 if (*start
> rule
->max
|| !rule
->align
)
62 static int pnp_assign_mem(struct pnp_dev
*dev
, struct pnp_mem
*rule
, int idx
)
64 unsigned long *start
, *end
, *flags
;
69 if (idx
>= PNP_MAX_MEM
) {
70 pnp_err("More than 8 mems is incompatible with pnp specifications.");
71 /* pretend we were successful so at least the manager won't try again */
75 /* check if this resource has been manually set, if so skip */
76 if (!(dev
->res
.mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
79 start
= &dev
->res
.mem_resource
[idx
].start
;
80 end
= &dev
->res
.mem_resource
[idx
].end
;
81 flags
= &dev
->res
.mem_resource
[idx
].flags
;
83 /* set the initial values */
84 *flags
|= rule
->flags
| IORESOURCE_MEM
;
85 *flags
&= ~IORESOURCE_UNSET
;
87 /* convert pnp flags to standard Linux flags */
88 if (!(rule
->flags
& IORESOURCE_MEM_WRITEABLE
))
89 *flags
|= IORESOURCE_READONLY
;
90 if (rule
->flags
& IORESOURCE_MEM_CACHEABLE
)
91 *flags
|= IORESOURCE_CACHEABLE
;
92 if (rule
->flags
& IORESOURCE_MEM_RANGELENGTH
)
93 *flags
|= IORESOURCE_RANGELENGTH
;
94 if (rule
->flags
& IORESOURCE_MEM_SHADOWABLE
)
95 *flags
|= IORESOURCE_SHADOWABLE
;
98 *flags
|= IORESOURCE_DISABLED
;
99 return 1; /* skip disabled resource requests */
103 *end
= *start
+ rule
->size
-1;
105 /* run through until pnp_check_mem is happy */
106 while (!pnp_check_mem(dev
, idx
)) {
107 *start
+= rule
->align
;
108 *end
= *start
+ rule
->size
- 1;
109 if (*start
> rule
->max
|| !rule
->align
)
115 static int pnp_assign_irq(struct pnp_dev
* dev
, struct pnp_irq
*rule
, int idx
)
117 unsigned long *start
, *end
, *flags
;
120 /* IRQ priority: this table is good for i386 */
121 static unsigned short xtab
[16] = {
122 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
128 if (idx
>= PNP_MAX_IRQ
) {
129 pnp_err("More than 2 irqs is incompatible with pnp specifications.");
130 /* pretend we were successful so at least the manager won't try again */
134 /* check if this resource has been manually set, if so skip */
135 if (!(dev
->res
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
138 start
= &dev
->res
.irq_resource
[idx
].start
;
139 end
= &dev
->res
.irq_resource
[idx
].end
;
140 flags
= &dev
->res
.irq_resource
[idx
].flags
;
142 /* set the initial values */
143 *flags
|= rule
->flags
| IORESOURCE_IRQ
;
144 *flags
&= ~IORESOURCE_UNSET
;
146 if (bitmap_empty(rule
->map
, PNP_IRQ_NR
)) {
147 *flags
|= IORESOURCE_DISABLED
;
148 return 1; /* skip disabled resource requests */
151 /* TBD: need check for >16 IRQ */
152 *start
= find_next_bit(rule
->map
, PNP_IRQ_NR
, 16);
153 if (*start
< PNP_IRQ_NR
) {
157 for (i
= 0; i
< 16; i
++) {
158 if(test_bit(xtab
[i
], rule
->map
)) {
159 *start
= *end
= xtab
[i
];
160 if(pnp_check_irq(dev
, idx
))
167 static int pnp_assign_dma(struct pnp_dev
*dev
, struct pnp_dma
*rule
, int idx
)
169 unsigned long *start
, *end
, *flags
;
172 /* DMA priority: this table is good for i386 */
173 static unsigned short xtab
[8] = {
174 1, 3, 5, 6, 7, 0, 2, 4
180 if (idx
>= PNP_MAX_DMA
) {
181 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
182 /* pretend we were successful so at least the manager won't try again */
186 /* check if this resource has been manually set, if so skip */
187 if (!(dev
->res
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
190 start
= &dev
->res
.dma_resource
[idx
].start
;
191 end
= &dev
->res
.dma_resource
[idx
].end
;
192 flags
= &dev
->res
.dma_resource
[idx
].flags
;
194 /* set the initial values */
195 *flags
|= rule
->flags
| IORESOURCE_DMA
;
196 *flags
&= ~IORESOURCE_UNSET
;
199 *flags
|= IORESOURCE_DISABLED
;
200 return 1; /* skip disabled resource requests */
203 for (i
= 0; i
< 8; i
++) {
204 if(rule
->map
& (1<<xtab
[i
])) {
205 *start
= *end
= xtab
[i
];
206 if(pnp_check_dma(dev
, idx
))
214 * pnp_init_resources - Resets a resource table to default values.
215 * @table: pointer to the desired resource table
218 void pnp_init_resource_table(struct pnp_resource_table
*table
)
221 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
222 table
->irq_resource
[idx
].name
= NULL
;
223 table
->irq_resource
[idx
].start
= -1;
224 table
->irq_resource
[idx
].end
= -1;
225 table
->irq_resource
[idx
].flags
= IORESOURCE_IRQ
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
227 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
228 table
->dma_resource
[idx
].name
= NULL
;
229 table
->dma_resource
[idx
].start
= -1;
230 table
->dma_resource
[idx
].end
= -1;
231 table
->dma_resource
[idx
].flags
= IORESOURCE_DMA
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
233 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
234 table
->port_resource
[idx
].name
= NULL
;
235 table
->port_resource
[idx
].start
= 0;
236 table
->port_resource
[idx
].end
= 0;
237 table
->port_resource
[idx
].flags
= IORESOURCE_IO
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
239 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
240 table
->mem_resource
[idx
].name
= NULL
;
241 table
->mem_resource
[idx
].start
= 0;
242 table
->mem_resource
[idx
].end
= 0;
243 table
->mem_resource
[idx
].flags
= IORESOURCE_MEM
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
248 * pnp_clean_resources - clears resources that were not manually set
249 * @res: the resources to clean
252 static void pnp_clean_resource_table(struct pnp_resource_table
* res
)
255 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
256 if (!(res
->irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
258 res
->irq_resource
[idx
].start
= -1;
259 res
->irq_resource
[idx
].end
= -1;
260 res
->irq_resource
[idx
].flags
= IORESOURCE_IRQ
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
262 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
263 if (!(res
->dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
265 res
->dma_resource
[idx
].start
= -1;
266 res
->dma_resource
[idx
].end
= -1;
267 res
->dma_resource
[idx
].flags
= IORESOURCE_DMA
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
269 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
270 if (!(res
->port_resource
[idx
].flags
& IORESOURCE_AUTO
))
272 res
->port_resource
[idx
].start
= 0;
273 res
->port_resource
[idx
].end
= 0;
274 res
->port_resource
[idx
].flags
= IORESOURCE_IO
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
276 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
277 if (!(res
->mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
279 res
->mem_resource
[idx
].start
= 0;
280 res
->mem_resource
[idx
].end
= 0;
281 res
->mem_resource
[idx
].flags
= IORESOURCE_MEM
| IORESOURCE_AUTO
| IORESOURCE_UNSET
;
286 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
287 * @dev: pointer to the desired device
288 * @depnum: the dependent function number
290 * Only set depnum to 0 if the device does not have dependent options.
292 static int pnp_assign_resources(struct pnp_dev
*dev
, int depnum
)
294 struct pnp_port
*port
;
298 int nport
= 0, nmem
= 0, nirq
= 0, ndma
= 0;
300 if (!pnp_can_configure(dev
))
303 down(&pnp_res_mutex
);
304 pnp_clean_resource_table(&dev
->res
); /* start with a fresh slate */
305 if (dev
->independent
) {
306 port
= dev
->independent
->port
;
307 mem
= dev
->independent
->mem
;
308 irq
= dev
->independent
->irq
;
309 dma
= dev
->independent
->dma
;
311 if (!pnp_assign_port(dev
, port
, nport
))
317 if (!pnp_assign_mem(dev
, mem
, nmem
))
323 if (!pnp_assign_irq(dev
, irq
, nirq
))
329 if (!pnp_assign_dma(dev
, dma
, ndma
))
337 struct pnp_option
*dep
;
339 for (i
=1,dep
=dev
->dependent
; i
<depnum
; i
++, dep
=dep
->next
)
347 if (!pnp_assign_port(dev
, port
, nport
))
353 if (!pnp_assign_mem(dev
, mem
, nmem
))
359 if (!pnp_assign_irq(dev
, irq
, nirq
))
365 if (!pnp_assign_dma(dev
, dma
, ndma
))
370 } else if (dev
->dependent
)
377 pnp_clean_resource_table(&dev
->res
);
383 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
384 * @dev: pointer to the desired device
385 * @res: pointer to the new resource config
386 * @mode: 0 or PNP_CONFIG_FORCE
388 * This function can be used by drivers that want to manually set thier resources.
390 int pnp_manual_config_dev(struct pnp_dev
*dev
, struct pnp_resource_table
* res
, int mode
)
393 struct pnp_resource_table
* bak
;
396 if (!pnp_can_configure(dev
))
398 bak
= pnp_alloc(sizeof(struct pnp_resource_table
));
403 down(&pnp_res_mutex
);
405 if (!(mode
& PNP_CONFIG_FORCE
)) {
406 for (i
= 0; i
< PNP_MAX_PORT
; i
++) {
407 if(!pnp_check_port(dev
,i
))
410 for (i
= 0; i
< PNP_MAX_MEM
; i
++) {
411 if(!pnp_check_mem(dev
,i
))
414 for (i
= 0; i
< PNP_MAX_IRQ
; i
++) {
415 if(!pnp_check_irq(dev
,i
))
418 for (i
= 0; i
< PNP_MAX_DMA
; i
++) {
419 if(!pnp_check_dma(dev
,i
))
436 * pnp_auto_config_dev - automatically assigns resources to a device
437 * @dev: pointer to the desired device
440 int pnp_auto_config_dev(struct pnp_dev
*dev
)
442 struct pnp_option
*dep
;
448 if(!pnp_can_configure(dev
)) {
449 pnp_info("Device %s does not support resource configuration.", dev
->dev
.bus_id
);
453 if (!dev
->dependent
) {
454 if (pnp_assign_resources(dev
, 0))
457 dep
= dev
->dependent
;
459 if (pnp_assign_resources(dev
, i
))
466 pnp_err("Unable to assign resources to device %s.", dev
->dev
.bus_id
);
471 * pnp_activate_dev - activates a PnP device for use
472 * @dev: pointer to the desired device
474 * does not validate or set resources so be careful.
476 int pnp_activate_dev(struct pnp_dev
*dev
)
481 return 0; /* the device is already active */
484 /* ensure resources are allocated */
485 if (pnp_auto_config_dev(dev
))
488 if (!pnp_can_write(dev
)) {
489 pnp_info("Device %s does not supported activation.", dev
->dev
.bus_id
);
493 if (dev
->protocol
->set(dev
, &dev
->res
)<0) {
494 pnp_err("Failed to activate device %s.", dev
->dev
.bus_id
);
499 pnp_info("Device %s activated.", dev
->dev
.bus_id
);
505 * pnp_disable_dev - disables device
506 * @dev: pointer to the desired device
508 * inform the correct pnp protocol so that resources can be used by other devices
510 int pnp_disable_dev(struct pnp_dev
*dev
)
515 return 0; /* the device is already disabled */
518 if (!pnp_can_disable(dev
)) {
519 pnp_info("Device %s does not supported disabling.", dev
->dev
.bus_id
);
522 if (dev
->protocol
->disable(dev
)<0) {
523 pnp_err("Failed to disable device %s.", dev
->dev
.bus_id
);
528 pnp_info("Device %s disabled.", dev
->dev
.bus_id
);
530 /* release the resources so that other devices can use them */
531 down(&pnp_res_mutex
);
532 pnp_clean_resource_table(&dev
->res
);
539 * pnp_resource_change - change one resource
540 * @resource: pointer to resource to be changed
541 * @start: start of region
542 * @size: size of region
545 void pnp_resource_change(struct resource
*resource
, unsigned long start
, unsigned long size
)
547 if (resource
== NULL
)
549 resource
->flags
&= ~(IORESOURCE_AUTO
| IORESOURCE_UNSET
);
550 resource
->start
= start
;
551 resource
->end
= start
+ size
- 1;
555 EXPORT_SYMBOL(pnp_manual_config_dev
);
556 EXPORT_SYMBOL(pnp_auto_config_dev
);
557 EXPORT_SYMBOL(pnp_activate_dev
);
558 EXPORT_SYMBOL(pnp_disable_dev
);
559 EXPORT_SYMBOL(pnp_resource_change
);
560 EXPORT_SYMBOL(pnp_init_resource_table
);