2 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
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/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>
15 #include <linux/mutex.h>
18 DEFINE_MUTEX(pnp_res_mutex);
20 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
22 struct pnp_resource *pnp_res;
25 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, idx);
27 dev_err(&dev->dev, "too many I/O port resources\n");
28 /* 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 (!(res->flags & IORESOURCE_AUTO)) {
36 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
37 "flags %#lx\n", idx, (unsigned long long) res->start,
38 (unsigned long long) res->end, res->flags);
42 /* set the initial values */
44 res->flags |= rule->flags | IORESOURCE_IO;
45 res->flags &= ~IORESOURCE_UNSET;
48 res->flags |= IORESOURCE_DISABLED;
49 dev_dbg(&dev->dev, " io %d disabled\n", idx);
50 return 1; /* skip disabled resource requests */
53 res->start = rule->min;
54 res->end = res->start + rule->size - 1;
56 /* run through until pnp_check_port is happy */
57 while (!pnp_check_port(dev, res)) {
58 res->start += rule->align;
59 res->end = res->start + rule->size - 1;
60 if (res->start > rule->max || !rule->align) {
61 dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
65 dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx,
66 (unsigned long long) res->start, (unsigned long long) res->end);
70 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
72 struct pnp_resource *pnp_res;
75 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, idx);
77 dev_err(&dev->dev, "too many memory resources\n");
78 /* pretend we were successful so at least the manager won't try again */
84 /* check if this resource has been manually set, if so skip */
85 if (!(res->flags & IORESOURCE_AUTO)) {
86 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
87 "flags %#lx\n", idx, (unsigned long long) res->start,
88 (unsigned long long) res->end, res->flags);
92 /* set the initial values */
94 res->flags |= rule->flags | IORESOURCE_MEM;
95 res->flags &= ~IORESOURCE_UNSET;
97 /* convert pnp flags to standard Linux flags */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 res->flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
101 res->flags |= IORESOURCE_CACHEABLE;
102 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
103 res->flags |= IORESOURCE_RANGELENGTH;
104 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
105 res->flags |= IORESOURCE_SHADOWABLE;
108 res->flags |= IORESOURCE_DISABLED;
109 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
110 return 1; /* skip disabled resource requests */
113 res->start = rule->min;
114 res->end = res->start + rule->size - 1;
116 /* run through until pnp_check_mem is happy */
117 while (!pnp_check_mem(dev, res)) {
118 res->start += rule->align;
119 res->end = res->start + rule->size - 1;
120 if (res->start > rule->max || !rule->align) {
121 dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
125 dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx,
126 (unsigned long long) res->start, (unsigned long long) res->end);
130 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
132 struct pnp_resource *pnp_res;
133 struct resource *res;
136 /* IRQ priority: this table is good for i386 */
137 static unsigned short xtab[16] = {
138 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
141 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, idx);
143 dev_err(&dev->dev, "too many IRQ resources\n");
144 /* pretend we were successful so at least the manager won't try again */
150 /* check if this resource has been manually set, if so skip */
151 if (!(res->flags & IORESOURCE_AUTO)) {
152 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
153 idx, (int) res->start, res->flags);
157 /* set the initial values */
158 pnp_res->index = idx;
159 res->flags |= rule->flags | IORESOURCE_IRQ;
160 res->flags &= ~IORESOURCE_UNSET;
162 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
163 res->flags |= IORESOURCE_DISABLED;
164 dev_dbg(&dev->dev, " irq %d disabled\n", idx);
165 return 1; /* skip disabled resource requests */
168 /* TBD: need check for >16 IRQ */
169 res->start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
170 if (res->start < PNP_IRQ_NR) {
171 res->end = res->start;
172 dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
176 for (i = 0; i < 16; i++) {
177 if (test_bit(xtab[i], rule->map)) {
178 res->start = res->end = xtab[i];
179 if (pnp_check_irq(dev, res)) {
180 dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
186 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
190 static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
192 struct pnp_resource *pnp_res;
193 struct resource *res;
196 /* DMA priority: this table is good for i386 */
197 static unsigned short xtab[8] = {
198 1, 3, 5, 6, 7, 0, 2, 4
201 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, idx);
203 dev_err(&dev->dev, "too many DMA resources\n");
209 /* check if this resource has been manually set, if so skip */
210 if (!(res->flags & IORESOURCE_AUTO)) {
211 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
212 idx, (int) res->start, res->flags);
216 /* set the initial values */
217 pnp_res->index = idx;
218 res->flags |= rule->flags | IORESOURCE_DMA;
219 res->flags &= ~IORESOURCE_UNSET;
221 for (i = 0; i < 8; i++) {
222 if (rule->map & (1 << xtab[i])) {
223 res->start = res->end = xtab[i];
224 if (pnp_check_dma(dev, res)) {
225 dev_dbg(&dev->dev, " assign dma %d %d\n", idx,
231 #ifdef MAX_DMA_CHANNELS
232 res->start = res->end = MAX_DMA_CHANNELS;
234 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
235 dev_dbg(&dev->dev, " disable dma %d\n", idx);
238 void pnp_init_resource(struct resource *res)
242 type = res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
243 IORESOURCE_IRQ | IORESOURCE_DMA);
246 res->flags = type | IORESOURCE_AUTO | IORESOURCE_UNSET;
247 if (type == IORESOURCE_IRQ || type == IORESOURCE_DMA) {
257 * pnp_init_resources - Resets a resource table to default values.
258 * @table: pointer to the desired resource table
260 void pnp_init_resources(struct pnp_dev *dev)
262 struct resource *res;
265 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
266 res = &dev->res->irq[idx].res;
267 res->flags = IORESOURCE_IRQ;
268 pnp_init_resource(res);
270 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
271 res = &dev->res->dma[idx].res;
272 res->flags = IORESOURCE_DMA;
273 pnp_init_resource(res);
275 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
276 res = &dev->res->port[idx].res;
277 res->flags = IORESOURCE_IO;
278 pnp_init_resource(res);
280 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
281 res = &dev->res->mem[idx].res;
282 res->flags = IORESOURCE_MEM;
283 pnp_init_resource(res);
288 * pnp_clean_resources - clears resources that were not manually set
289 * @res: the resources to clean
291 static void pnp_clean_resource_table(struct pnp_dev *dev)
293 struct resource *res;
296 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
297 res = &dev->res->irq[idx].res;
298 if (res->flags & IORESOURCE_AUTO) {
299 res->flags = IORESOURCE_IRQ;
300 pnp_init_resource(res);
303 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
304 res = &dev->res->dma[idx].res;
305 if (res->flags & IORESOURCE_AUTO) {
306 res->flags = IORESOURCE_DMA;
307 pnp_init_resource(res);
310 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
311 res = &dev->res->port[idx].res;
312 if (res->flags & IORESOURCE_AUTO) {
313 res->flags = IORESOURCE_IO;
314 pnp_init_resource(res);
317 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
318 res = &dev->res->mem[idx].res;
319 if (res->flags & IORESOURCE_AUTO) {
320 res->flags = IORESOURCE_MEM;
321 pnp_init_resource(res);
327 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
328 * @dev: pointer to the desired device
329 * @depnum: the dependent function number
331 * Only set depnum to 0 if the device does not have dependent options.
333 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
335 struct pnp_port *port;
339 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
341 if (!pnp_can_configure(dev))
344 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
345 mutex_lock(&pnp_res_mutex);
346 pnp_clean_resource_table(dev);
347 if (dev->independent) {
348 dev_dbg(&dev->dev, "assigning independent options\n");
349 port = dev->independent->port;
350 mem = dev->independent->mem;
351 irq = dev->independent->irq;
352 dma = dev->independent->dma;
354 if (!pnp_assign_port(dev, port, nport))
360 if (!pnp_assign_mem(dev, mem, nmem))
366 if (!pnp_assign_irq(dev, irq, nirq))
372 pnp_assign_dma(dev, dma, ndma);
379 struct pnp_option *dep;
382 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
383 for (i = 1, dep = dev->dependent; i < depnum;
384 i++, dep = dep->next)
392 if (!pnp_assign_port(dev, port, nport))
398 if (!pnp_assign_mem(dev, mem, nmem))
404 if (!pnp_assign_irq(dev, irq, nirq))
410 pnp_assign_dma(dev, dma, ndma);
414 } else if (dev->dependent)
417 mutex_unlock(&pnp_res_mutex);
418 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
422 pnp_clean_resource_table(dev);
423 mutex_unlock(&pnp_res_mutex);
424 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
429 * pnp_auto_config_dev - automatically assigns resources to a device
430 * @dev: pointer to the desired device
432 int pnp_auto_config_dev(struct pnp_dev *dev)
434 struct pnp_option *dep;
437 if (!pnp_can_configure(dev)) {
438 dev_dbg(&dev->dev, "configuration not supported\n");
442 if (!dev->dependent) {
443 if (pnp_assign_resources(dev, 0))
446 dep = dev->dependent;
448 if (pnp_assign_resources(dev, i))
455 dev_err(&dev->dev, "unable to assign resources\n");
460 * pnp_start_dev - low-level start of the PnP device
461 * @dev: pointer to the desired device
463 * assumes that resources have already been allocated
465 int pnp_start_dev(struct pnp_dev *dev)
467 if (!pnp_can_write(dev)) {
468 dev_dbg(&dev->dev, "activation not supported\n");
472 dbg_pnp_show_resources(dev, "pnp_start_dev");
473 if (dev->protocol->set(dev) < 0) {
474 dev_err(&dev->dev, "activation failed\n");
478 dev_info(&dev->dev, "activated\n");
483 * pnp_stop_dev - low-level disable of the PnP device
484 * @dev: pointer to the desired device
486 * does not free resources
488 int pnp_stop_dev(struct pnp_dev *dev)
490 if (!pnp_can_disable(dev)) {
491 dev_dbg(&dev->dev, "disabling not supported\n");
494 if (dev->protocol->disable(dev) < 0) {
495 dev_err(&dev->dev, "disable failed\n");
499 dev_info(&dev->dev, "disabled\n");
504 * pnp_activate_dev - activates a PnP device for use
505 * @dev: pointer to the desired device
507 * does not validate or set resources so be careful.
509 int pnp_activate_dev(struct pnp_dev *dev)
516 /* ensure resources are allocated */
517 if (pnp_auto_config_dev(dev))
520 error = pnp_start_dev(dev);
529 * pnp_disable_dev - disables device
530 * @dev: pointer to the desired device
532 * inform the correct pnp protocol so that resources can be used by other devices
534 int pnp_disable_dev(struct pnp_dev *dev)
541 error = pnp_stop_dev(dev);
547 /* release the resources so that other devices can use them */
548 mutex_lock(&pnp_res_mutex);
549 pnp_clean_resource_table(dev);
550 mutex_unlock(&pnp_res_mutex);
555 EXPORT_SYMBOL(pnp_start_dev);
556 EXPORT_SYMBOL(pnp_stop_dev);
557 EXPORT_SYMBOL(pnp_activate_dev);
558 EXPORT_SYMBOL(pnp_disable_dev);