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>
15 #ifdef CONFIG_PNP_DEBUG
21 #include <linux/pnp.h>
24 DECLARE_MUTEX(pnp_res_mutex
);
26 static int pnp_assign_port(struct pnp_dev
*dev
, struct pnp_port
*rule
, int idx
)
28 unsigned long *start
, *end
, *flags
;
33 if (idx
>= PNP_MAX_PORT
) {
34 pnp_err("More than 4 ports is incompatible with pnp specifications.");
35 /* pretend we were successful so at least the manager won't try again */
39 /* check if this resource has been manually set, if so skip */
40 if (!(dev
->res
.port_resource
[idx
].flags
& IORESOURCE_AUTO
))
43 start
= &dev
->res
.port_resource
[idx
].start
;
44 end
= &dev
->res
.port_resource
[idx
].end
;
45 flags
= &dev
->res
.port_resource
[idx
].flags
;
47 /* set the initial values */
49 *end
= *start
+ rule
->size
- 1;
50 *flags
= *flags
| rule
->flags
| IORESOURCE_IO
;
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 */
85 *end
= *start
+ rule
->size
-1;
86 *flags
= *flags
| rule
->flags
| IORESOURCE_MEM
;
88 /* convert pnp flags to standard Linux flags */
89 if (!(rule
->flags
& IORESOURCE_MEM_WRITEABLE
))
90 *flags
|= IORESOURCE_READONLY
;
91 if (rule
->flags
& IORESOURCE_MEM_CACHEABLE
)
92 *flags
|= IORESOURCE_CACHEABLE
;
93 if (rule
->flags
& IORESOURCE_MEM_RANGELENGTH
)
94 *flags
|= IORESOURCE_RANGELENGTH
;
95 if (rule
->flags
& IORESOURCE_MEM_SHADOWABLE
)
96 *flags
|= IORESOURCE_SHADOWABLE
;
98 /* run through until pnp_check_mem is happy */
99 while (!pnp_check_mem(dev
, idx
)) {
100 *start
+= rule
->align
;
101 *end
= *start
+ rule
->size
- 1;
102 if (*start
> rule
->max
|| !rule
->align
)
108 static int pnp_assign_irq(struct pnp_dev
* dev
, struct pnp_irq
*rule
, int idx
)
110 unsigned long *start
, *end
, *flags
;
113 /* IRQ priority: this table is good for i386 */
114 static unsigned short xtab
[16] = {
115 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
121 if (idx
>= PNP_MAX_IRQ
) {
122 pnp_err("More than 2 irqs is incompatible with pnp specifications.");
123 /* pretend we were successful so at least the manager won't try again */
127 /* check if this resource has been manually set, if so skip */
128 if (!(dev
->res
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
131 start
= &dev
->res
.irq_resource
[idx
].start
;
132 end
= &dev
->res
.irq_resource
[idx
].end
;
133 flags
= &dev
->res
.irq_resource
[idx
].flags
;
135 /* set the initial values */
136 *flags
= *flags
| rule
->flags
| IORESOURCE_IRQ
;
138 for (i
= 0; i
< 16; i
++) {
139 if(rule
->map
& (1<<xtab
[i
])) {
140 *start
= *end
= xtab
[i
];
141 if(pnp_check_irq(dev
, idx
))
148 static int pnp_assign_dma(struct pnp_dev
*dev
, struct pnp_dma
*rule
, int idx
)
150 unsigned long *start
, *end
, *flags
;
153 /* DMA priority: this table is good for i386 */
154 static unsigned short xtab
[8] = {
155 1, 3, 5, 6, 7, 0, 2, 4
161 if (idx
>= PNP_MAX_DMA
) {
162 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
163 /* pretend we were successful so at least the manager won't try again */
167 /* check if this resource has been manually set, if so skip */
168 if (!(dev
->res
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
171 start
= &dev
->res
.dma_resource
[idx
].start
;
172 end
= &dev
->res
.dma_resource
[idx
].end
;
173 flags
= &dev
->res
.dma_resource
[idx
].flags
;
175 /* set the initial values */
176 *flags
= *flags
| rule
->flags
| IORESOURCE_DMA
;
178 for (i
= 0; i
< 8; i
++) {
179 if(rule
->map
& (1<<xtab
[i
])) {
180 *start
= *end
= xtab
[i
];
181 if(pnp_check_dma(dev
, idx
))
189 * pnp_init_resources - Resets a resource table to default values.
190 * @table: pointer to the desired resource table
193 void pnp_init_resources(struct pnp_resource_table
*table
)
196 down(&pnp_res_mutex
);
197 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
198 table
->irq_resource
[idx
].name
= NULL
;
199 table
->irq_resource
[idx
].start
= -1;
200 table
->irq_resource
[idx
].end
= -1;
201 table
->irq_resource
[idx
].flags
= IORESOURCE_AUTO
;
203 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
204 table
->dma_resource
[idx
].name
= NULL
;
205 table
->dma_resource
[idx
].start
= -1;
206 table
->dma_resource
[idx
].end
= -1;
207 table
->dma_resource
[idx
].flags
= IORESOURCE_AUTO
;
209 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
210 table
->port_resource
[idx
].name
= NULL
;
211 table
->port_resource
[idx
].start
= 0;
212 table
->port_resource
[idx
].end
= 0;
213 table
->port_resource
[idx
].flags
= IORESOURCE_AUTO
;
215 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
216 table
->mem_resource
[idx
].name
= NULL
;
217 table
->mem_resource
[idx
].start
= 0;
218 table
->mem_resource
[idx
].end
= 0;
219 table
->mem_resource
[idx
].flags
= IORESOURCE_AUTO
;
225 * pnp_clean_resources - clears resources that were not manually set
226 * @res - the resources to clean
229 static void pnp_clean_resources(struct pnp_resource_table
* res
)
232 for (idx
= 0; idx
< PNP_MAX_IRQ
; idx
++) {
233 if (!(res
->irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
235 res
->irq_resource
[idx
].start
= -1;
236 res
->irq_resource
[idx
].end
= -1;
237 res
->irq_resource
[idx
].flags
= IORESOURCE_AUTO
;
239 for (idx
= 0; idx
< PNP_MAX_DMA
; idx
++) {
240 if (!(res
->dma_resource
[idx
].flags
& IORESOURCE_AUTO
))
242 res
->dma_resource
[idx
].start
= -1;
243 res
->dma_resource
[idx
].end
= -1;
244 res
->dma_resource
[idx
].flags
= IORESOURCE_AUTO
;
246 for (idx
= 0; idx
< PNP_MAX_PORT
; idx
++) {
247 if (!(res
->port_resource
[idx
].flags
& IORESOURCE_AUTO
))
249 res
->port_resource
[idx
].start
= 0;
250 res
->port_resource
[idx
].end
= 0;
251 res
->port_resource
[idx
].flags
= IORESOURCE_AUTO
;
253 for (idx
= 0; idx
< PNP_MAX_MEM
; idx
++) {
254 if (!(res
->mem_resource
[idx
].flags
& IORESOURCE_AUTO
))
256 res
->mem_resource
[idx
].start
= 0;
257 res
->mem_resource
[idx
].end
= 0;
258 res
->mem_resource
[idx
].flags
= IORESOURCE_AUTO
;
263 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
264 * @dev: pointer to the desired device
265 * @depnum: the dependent function number
267 * Only set depnum to 0 if the device does not have dependent options.
269 int pnp_assign_resources(struct pnp_dev
*dev
, int depnum
)
271 struct pnp_port
*port
;
275 int nport
= 0, nmem
= 0, nirq
= 0, ndma
= 0;
277 if (!pnp_can_configure(dev
))
280 down(&pnp_res_mutex
);
281 pnp_clean_resources(&dev
->res
); /* start with a fresh slate */
282 if (dev
->independent
) {
283 port
= dev
->independent
->port
;
284 mem
= dev
->independent
->mem
;
285 irq
= dev
->independent
->irq
;
286 dma
= dev
->independent
->dma
;
288 if (!pnp_assign_port(dev
, port
, nport
))
294 if (!pnp_assign_mem(dev
, mem
, nmem
))
300 if (!pnp_assign_irq(dev
, irq
, nirq
))
306 if (!pnp_assign_dma(dev
, dma
, ndma
))
314 struct pnp_option
*dep
;
316 for (i
=1,dep
=dev
->dependent
; i
<depnum
; i
++, dep
=dep
->next
)
324 if (!pnp_assign_port(dev
, port
, nport
))
330 if (!pnp_assign_mem(dev
, mem
, nmem
))
336 if (!pnp_assign_irq(dev
, irq
, nirq
))
342 if (!pnp_assign_dma(dev
, dma
, ndma
))
347 } else if (dev
->dependent
)
354 pnp_clean_resources(&dev
->res
);
360 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
361 * @dev: pointer to the desired device
362 * @res: pointer to the new resource config
364 * This function can be used by drivers that want to manually set thier resources.
366 int pnp_manual_config_dev(struct pnp_dev
*dev
, struct pnp_resource_table
* res
, int mode
)
369 struct pnp_resource_table
* bak
;
372 if (!pnp_can_configure(dev
))
374 bak
= pnp_alloc(sizeof(struct pnp_resource_table
));
379 down(&pnp_res_mutex
);
381 if (!(mode
& PNP_CONFIG_FORCE
)) {
382 for (i
= 0; i
< PNP_MAX_PORT
; i
++) {
383 if(pnp_check_port(dev
,i
))
386 for (i
= 0; i
< PNP_MAX_MEM
; i
++) {
387 if(pnp_check_mem(dev
,i
))
390 for (i
= 0; i
< PNP_MAX_IRQ
; i
++) {
391 if(pnp_check_irq(dev
,i
))
394 for (i
= 0; i
< PNP_MAX_DMA
; i
++) {
395 if(pnp_check_dma(dev
,i
))
401 pnp_auto_config_dev(dev
);
413 * pnp_auto_config_dev - automatically assigns resources to a device
414 * @dev: pointer to the desired device
417 int pnp_auto_config_dev(struct pnp_dev
*dev
)
419 struct pnp_option
*dep
;
425 if(!pnp_can_configure(dev
)) {
426 pnp_info("Device %s does not support resource configuration.", dev
->dev
.bus_id
);
430 if (!dev
->dependent
) {
431 if (pnp_assign_resources(dev
, 0))
437 dep
= dev
->dependent
;
439 if (pnp_assign_resources(dev
, i
))
442 /* if this dependent resource failed, try the next one */
447 pnp_err("Unable to assign resources to device %s.", dev
->dev
.bus_id
);
452 * pnp_activate_dev - activates a PnP device for use
453 * @dev: pointer to the desired device
455 * does not validate or set resources so be careful.
457 int pnp_activate_dev(struct pnp_dev
*dev
)
462 return 0; /* the device is already active */
465 /* ensure resources are allocated */
466 if (!pnp_auto_config_dev(dev
))
469 if (!pnp_can_write(dev
)) {
470 pnp_info("Device %s does not supported activation.", dev
->dev
.bus_id
);
474 if (dev
->protocol
->set(dev
, &dev
->res
)<0) {
475 pnp_err("Failed to activate device %s.", dev
->dev
.bus_id
);
480 pnp_info("Device %s activated.", dev
->dev
.bus_id
);
486 * pnp_disable_dev - disables device
487 * @dev: pointer to the desired device
489 * inform the correct pnp protocol so that resources can be used by other devices
491 int pnp_disable_dev(struct pnp_dev
*dev
)
496 return 0; /* the device is already disabled */
499 if (!pnp_can_disable(dev
)) {
500 pnp_info("Device %s does not supported disabling.", dev
->dev
.bus_id
);
503 if (dev
->protocol
->disable(dev
)<0) {
504 pnp_err("Failed to disable device %s.", dev
->dev
.bus_id
);
509 pnp_info("Device %s disabled.", dev
->dev
.bus_id
);
511 /* release the resources so that other devices can use them */
512 down(&pnp_res_mutex
);
513 pnp_clean_resources(&dev
->res
);
520 * pnp_resource_change - change one resource
521 * @resource: pointer to resource to be changed
522 * @start: start of region
523 * @size: size of region
526 void pnp_resource_change(struct resource
*resource
, unsigned long start
, unsigned long size
)
528 if (resource
== NULL
)
530 resource
->flags
&= ~IORESOURCE_AUTO
;
531 resource
->start
= start
;
532 resource
->end
= start
+ size
- 1;
536 EXPORT_SYMBOL(pnp_assign_resources
);
537 EXPORT_SYMBOL(pnp_manual_config_dev
);
538 EXPORT_SYMBOL(pnp_auto_config_dev
);
539 EXPORT_SYMBOL(pnp_activate_dev
);
540 EXPORT_SYMBOL(pnp_disable_dev
);
541 EXPORT_SYMBOL(pnp_resource_change
);
542 EXPORT_SYMBOL(pnp_init_resources
);