Merge with 2.5.75.
[linux-2.6/linux-mips.git] / drivers / pnp / manager.c
blobd902dfc47164a331a5b417eb35114ed26ac5e3aa
1 /*
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>
7 */
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
16 #define DEBUG
17 #else
18 #undef DEBUG
19 #endif
21 #include <linux/pnp.h>
22 #include "base.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;
30 if (!dev || !rule)
31 return -EINVAL;
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 */
36 return 1;
39 /* check if this resource has been manually set, if so skip */
40 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
41 return 1;
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 */
48 *flags = *flags | rule->flags | IORESOURCE_IO;
50 if (!rule->size) {
51 *flags |= IORESOURCE_DISABLED;
52 return 1; /* skip disabled resource requests */
55 *start = rule->min;
56 *end = *start + rule->size - 1;
58 /* run through until pnp_check_port is happy */
59 while (!pnp_check_port(dev, idx)) {
60 *start += rule->align;
61 *end = *start + rule->size - 1;
62 if (*start > rule->max || !rule->align)
63 return 0;
65 return 1;
68 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
70 unsigned long *start, *end, *flags;
72 if (!dev || !rule)
73 return -EINVAL;
75 if (idx >= PNP_MAX_MEM) {
76 pnp_err("More than 8 mems is incompatible with pnp specifications.");
77 /* pretend we were successful so at least the manager won't try again */
78 return 1;
81 /* check if this resource has been manually set, if so skip */
82 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
83 return 1;
85 start = &dev->res.mem_resource[idx].start;
86 end = &dev->res.mem_resource[idx].end;
87 flags = &dev->res.mem_resource[idx].flags;
89 /* set the initial values */
90 *flags = *flags | rule->flags | IORESOURCE_MEM;
92 /* convert pnp flags to standard Linux flags */
93 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
94 *flags |= IORESOURCE_READONLY;
95 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
96 *flags |= IORESOURCE_CACHEABLE;
97 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
98 *flags |= IORESOURCE_RANGELENGTH;
99 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
100 *flags |= IORESOURCE_SHADOWABLE;
102 if (!rule->size) {
103 *flags |= IORESOURCE_DISABLED;
104 return 1; /* skip disabled resource requests */
107 *start = rule->min;
108 *end = *start + rule->size -1;
110 /* run through until pnp_check_mem is happy */
111 while (!pnp_check_mem(dev, idx)) {
112 *start += rule->align;
113 *end = *start + rule->size - 1;
114 if (*start > rule->max || !rule->align)
115 return 0;
117 return 1;
120 static int pnp_assign_irq(struct pnp_dev * dev, struct pnp_irq *rule, int idx)
122 unsigned long *start, *end, *flags;
123 int i;
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
130 if (!dev || !rule)
131 return -EINVAL;
133 if (idx >= PNP_MAX_IRQ) {
134 pnp_err("More than 2 irqs is incompatible with pnp specifications.");
135 /* pretend we were successful so at least the manager won't try again */
136 return 1;
139 /* check if this resource has been manually set, if so skip */
140 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
141 return 1;
143 start = &dev->res.irq_resource[idx].start;
144 end = &dev->res.irq_resource[idx].end;
145 flags = &dev->res.irq_resource[idx].flags;
147 /* set the initial values */
148 *flags = *flags | rule->flags | IORESOURCE_IRQ;
150 if (!rule->map) {
151 *flags |= IORESOURCE_DISABLED;
152 return 1; /* skip disabled resource requests */
155 for (i = 0; i < 16; i++) {
156 if(rule->map & (1<<xtab[i])) {
157 *start = *end = xtab[i];
158 if(pnp_check_irq(dev, idx))
159 return 1;
162 return 0;
165 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
167 unsigned long *start, *end, *flags;
168 int i;
170 /* DMA priority: this table is good for i386 */
171 static unsigned short xtab[8] = {
172 1, 3, 5, 6, 7, 0, 2, 4
175 if (!dev || !rule)
176 return -EINVAL;
178 if (idx >= PNP_MAX_DMA) {
179 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
180 /* pretend we were successful so at least the manager won't try again */
181 return 1;
184 /* check if this resource has been manually set, if so skip */
185 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
186 return 1;
188 start = &dev->res.dma_resource[idx].start;
189 end = &dev->res.dma_resource[idx].end;
190 flags = &dev->res.dma_resource[idx].flags;
192 /* set the initial values */
193 *flags = *flags | rule->flags | IORESOURCE_DMA;
195 if (!rule->map) {
196 *flags |= IORESOURCE_DISABLED;
197 return 1; /* skip disabled resource requests */
200 for (i = 0; i < 8; i++) {
201 if(rule->map & (1<<xtab[i])) {
202 *start = *end = xtab[i];
203 if(pnp_check_dma(dev, idx))
204 return 1;
207 return 0;
211 * pnp_init_resources - Resets a resource table to default values.
212 * @table: pointer to the desired resource table
215 void pnp_init_resource_table(struct pnp_resource_table *table)
217 int idx;
218 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
219 table->irq_resource[idx].name = NULL;
220 table->irq_resource[idx].start = -1;
221 table->irq_resource[idx].end = -1;
222 table->irq_resource[idx].flags = IORESOURCE_AUTO;
224 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
225 table->dma_resource[idx].name = NULL;
226 table->dma_resource[idx].start = -1;
227 table->dma_resource[idx].end = -1;
228 table->dma_resource[idx].flags = IORESOURCE_AUTO;
230 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
231 table->port_resource[idx].name = NULL;
232 table->port_resource[idx].start = 0;
233 table->port_resource[idx].end = 0;
234 table->port_resource[idx].flags = IORESOURCE_AUTO;
236 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
237 table->mem_resource[idx].name = NULL;
238 table->mem_resource[idx].start = 0;
239 table->mem_resource[idx].end = 0;
240 table->mem_resource[idx].flags = IORESOURCE_AUTO;
245 * pnp_clean_resources - clears resources that were not manually set
246 * @res - the resources to clean
249 static void pnp_clean_resource_table(struct pnp_resource_table * res)
251 int idx;
252 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
253 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
254 continue;
255 res->irq_resource[idx].start = -1;
256 res->irq_resource[idx].end = -1;
257 res->irq_resource[idx].flags = IORESOURCE_AUTO;
259 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
260 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
261 continue;
262 res->dma_resource[idx].start = -1;
263 res->dma_resource[idx].end = -1;
264 res->dma_resource[idx].flags = IORESOURCE_AUTO;
266 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
267 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
268 continue;
269 res->port_resource[idx].start = 0;
270 res->port_resource[idx].end = 0;
271 res->port_resource[idx].flags = IORESOURCE_AUTO;
273 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
274 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
275 continue;
276 res->mem_resource[idx].start = 0;
277 res->mem_resource[idx].end = 0;
278 res->mem_resource[idx].flags = IORESOURCE_AUTO;
283 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
284 * @dev: pointer to the desired device
285 * @depnum: the dependent function number
287 * Only set depnum to 0 if the device does not have dependent options.
289 int pnp_assign_resources(struct pnp_dev *dev, int depnum)
291 struct pnp_port *port;
292 struct pnp_mem *mem;
293 struct pnp_irq *irq;
294 struct pnp_dma *dma;
295 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
297 if (!pnp_can_configure(dev))
298 return -ENODEV;
300 down(&pnp_res_mutex);
301 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
302 if (dev->independent) {
303 port = dev->independent->port;
304 mem = dev->independent->mem;
305 irq = dev->independent->irq;
306 dma = dev->independent->dma;
307 while (port) {
308 if (!pnp_assign_port(dev, port, nport))
309 goto fail;
310 nport++;
311 port = port->next;
313 while (mem) {
314 if (!pnp_assign_mem(dev, mem, nmem))
315 goto fail;
316 nmem++;
317 mem = mem->next;
319 while (irq) {
320 if (!pnp_assign_irq(dev, irq, nirq))
321 goto fail;
322 nirq++;
323 irq = irq->next;
325 while (dma) {
326 if (!pnp_assign_dma(dev, dma, ndma))
327 goto fail;
328 ndma++;
329 dma = dma->next;
333 if (depnum) {
334 struct pnp_option *dep;
335 int i;
336 for (i=1,dep=dev->dependent; i<depnum; i++, dep=dep->next)
337 if(!dep)
338 goto fail;
339 port =dep->port;
340 mem = dep->mem;
341 irq = dep->irq;
342 dma = dep->dma;
343 while (port) {
344 if (!pnp_assign_port(dev, port, nport))
345 goto fail;
346 nport++;
347 port = port->next;
349 while (mem) {
350 if (!pnp_assign_mem(dev, mem, nmem))
351 goto fail;
352 nmem++;
353 mem = mem->next;
355 while (irq) {
356 if (!pnp_assign_irq(dev, irq, nirq))
357 goto fail;
358 nirq++;
359 irq = irq->next;
361 while (dma) {
362 if (!pnp_assign_dma(dev, dma, ndma))
363 goto fail;
364 ndma++;
365 dma = dma->next;
367 } else if (dev->dependent)
368 goto fail;
370 up(&pnp_res_mutex);
371 return 1;
373 fail:
374 pnp_clean_resource_table(&dev->res);
375 up(&pnp_res_mutex);
376 return 0;
380 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
381 * @dev: pointer to the desired device
382 * @res: pointer to the new resource config
384 * This function can be used by drivers that want to manually set thier resources.
386 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table * res, int mode)
388 int i;
389 struct pnp_resource_table * bak;
390 if (!dev || !res)
391 return -EINVAL;
392 if (!pnp_can_configure(dev))
393 return -ENODEV;
394 bak = pnp_alloc(sizeof(struct pnp_resource_table));
395 if (!bak)
396 return -ENOMEM;
397 *bak = dev->res;
399 down(&pnp_res_mutex);
400 dev->res = *res;
401 if (!(mode & PNP_CONFIG_FORCE)) {
402 for (i = 0; i < PNP_MAX_PORT; i++) {
403 if(!pnp_check_port(dev,i))
404 goto fail;
406 for (i = 0; i < PNP_MAX_MEM; i++) {
407 if(!pnp_check_mem(dev,i))
408 goto fail;
410 for (i = 0; i < PNP_MAX_IRQ; i++) {
411 if(!pnp_check_irq(dev,i))
412 goto fail;
414 for (i = 0; i < PNP_MAX_DMA; i++) {
415 if(!pnp_check_dma(dev,i))
416 goto fail;
419 up(&pnp_res_mutex);
421 kfree(bak);
422 return 0;
424 fail:
425 dev->res = *bak;
426 up(&pnp_res_mutex);
427 kfree(bak);
428 return -EINVAL;
432 * pnp_auto_config_dev - automatically assigns resources to a device
433 * @dev: pointer to the desired device
436 int pnp_auto_config_dev(struct pnp_dev *dev)
438 struct pnp_option *dep;
439 int i = 1;
441 if(!dev)
442 return -EINVAL;
444 if(!pnp_can_configure(dev)) {
445 pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
446 return -ENODEV;
449 if (!dev->dependent) {
450 if (pnp_assign_resources(dev, 0))
451 return 1;
452 else
453 return 0;
456 dep = dev->dependent;
457 do {
458 if (pnp_assign_resources(dev, i))
459 return 1;
461 /* if this dependent resource failed, try the next one */
462 dep = dep->next;
463 i++;
464 } while (dep);
466 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
467 return 0;
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)
478 if (!dev)
479 return -EINVAL;
480 if (dev->active) {
481 return 0; /* the device is already active */
484 /* ensure resources are allocated */
485 if (!pnp_auto_config_dev(dev))
486 return -EBUSY;
488 if (!pnp_can_write(dev)) {
489 pnp_info("Device %s does not supported activation.", dev->dev.bus_id);
490 return -EINVAL;
493 if (dev->protocol->set(dev, &dev->res)<0) {
494 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
495 return -EIO;
498 dev->active = 1;
499 pnp_info("Device %s activated.", dev->dev.bus_id);
501 return 1;
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)
512 if (!dev)
513 return -EINVAL;
514 if (!dev->active) {
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);
520 return -EINVAL;
522 if (dev->protocol->disable(dev)<0) {
523 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
524 return -EIO;
527 dev->active = 0;
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);
533 up(&pnp_res_mutex);
535 return 1;
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)
548 return;
549 resource->flags &= ~IORESOURCE_AUTO;
550 resource->start = start;
551 resource->end = start + size - 1;
555 EXPORT_SYMBOL(pnp_assign_resources);
556 EXPORT_SYMBOL(pnp_manual_config_dev);
557 EXPORT_SYMBOL(pnp_auto_config_dev);
558 EXPORT_SYMBOL(pnp_activate_dev);
559 EXPORT_SYMBOL(pnp_disable_dev);
560 EXPORT_SYMBOL(pnp_resource_change);
561 EXPORT_SYMBOL(pnp_init_resource_table);