Fix typos.
[linux-2.6/linux-mips.git] / drivers / pnp / manager.c
blob34d3620c8e3ae4526129bd4466e4082aee379236
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 *start = rule->min;
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)
57 return 0;
59 return 1;
62 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
64 unsigned long *start, *end, *flags;
66 if (!dev || !rule)
67 return -EINVAL;
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 */
72 return 1;
75 /* check if this resource has been manually set, if so skip */
76 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
77 return 1;
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 *start = rule->min;
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)
103 return 0;
105 return 1;
108 static int pnp_assign_irq(struct pnp_dev * dev, struct pnp_irq *rule, int idx)
110 unsigned long *start, *end, *flags;
111 int i;
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
118 if (!dev || !rule)
119 return -EINVAL;
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 */
124 return 1;
127 /* check if this resource has been manually set, if so skip */
128 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
129 return 1;
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))
142 return 1;
145 return 0;
148 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
150 unsigned long *start, *end, *flags;
151 int i;
153 /* DMA priority: this table is good for i386 */
154 static unsigned short xtab[8] = {
155 1, 3, 5, 6, 7, 0, 2, 4
158 if (!dev || !rule)
159 return -EINVAL;
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 */
164 return 1;
167 /* check if this resource has been manually set, if so skip */
168 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
169 return 1;
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))
182 return 1;
185 return 0;
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)
195 int idx;
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;
221 up(&pnp_res_mutex);
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)
231 int idx;
232 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
233 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
234 continue;
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))
241 continue;
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))
248 continue;
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))
255 continue;
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;
272 struct pnp_mem *mem;
273 struct pnp_irq *irq;
274 struct pnp_dma *dma;
275 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
277 if (!pnp_can_configure(dev))
278 return -ENODEV;
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;
287 while (port) {
288 if (!pnp_assign_port(dev, port, nport))
289 goto fail;
290 nport++;
291 port = port->next;
293 while (mem) {
294 if (!pnp_assign_mem(dev, mem, nmem))
295 goto fail;
296 nmem++;
297 mem = mem->next;
299 while (irq) {
300 if (!pnp_assign_irq(dev, irq, nirq))
301 goto fail;
302 nirq++;
303 irq = irq->next;
305 while (dma) {
306 if (!pnp_assign_dma(dev, dma, ndma))
307 goto fail;
308 ndma++;
309 dma = dma->next;
313 if (depnum) {
314 struct pnp_option *dep;
315 int i;
316 for (i=1,dep=dev->dependent; i<depnum; i++, dep=dep->next)
317 if(!dep)
318 goto fail;
319 port =dep->port;
320 mem = dep->mem;
321 irq = dep->irq;
322 dma = dep->dma;
323 while (port) {
324 if (!pnp_assign_port(dev, port, nport))
325 goto fail;
326 nport++;
327 port = port->next;
329 while (mem) {
330 if (!pnp_assign_mem(dev, mem, nmem))
331 goto fail;
332 nmem++;
333 mem = mem->next;
335 while (irq) {
336 if (!pnp_assign_irq(dev, irq, nirq))
337 goto fail;
338 nirq++;
339 irq = irq->next;
341 while (dma) {
342 if (!pnp_assign_dma(dev, dma, ndma))
343 goto fail;
344 ndma++;
345 dma = dma->next;
347 } else if (dev->dependent)
348 goto fail;
350 up(&pnp_res_mutex);
351 return 1;
353 fail:
354 pnp_clean_resources(&dev->res);
355 up(&pnp_res_mutex);
356 return 0;
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)
368 int i;
369 struct pnp_resource_table * bak;
370 if (!dev || !res)
371 return -EINVAL;
372 if (!pnp_can_configure(dev))
373 return -ENODEV;
374 bak = pnp_alloc(sizeof(struct pnp_resource_table));
375 if (!bak)
376 return -ENOMEM;
377 *bak = dev->res;
379 down(&pnp_res_mutex);
380 dev->res = *res;
381 if (!(mode & PNP_CONFIG_FORCE)) {
382 for (i = 0; i < PNP_MAX_PORT; i++) {
383 if(pnp_check_port(dev,i))
384 goto fail;
386 for (i = 0; i < PNP_MAX_MEM; i++) {
387 if(pnp_check_mem(dev,i))
388 goto fail;
390 for (i = 0; i < PNP_MAX_IRQ; i++) {
391 if(pnp_check_irq(dev,i))
392 goto fail;
394 for (i = 0; i < PNP_MAX_DMA; i++) {
395 if(pnp_check_dma(dev,i))
396 goto fail;
399 up(&pnp_res_mutex);
401 pnp_auto_config_dev(dev);
402 kfree(bak);
403 return 0;
405 fail:
406 dev->res = *bak;
407 up(&pnp_res_mutex);
408 kfree(bak);
409 return -EINVAL;
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;
420 int i = 1;
422 if(!dev)
423 return -EINVAL;
425 if(!pnp_can_configure(dev)) {
426 pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
427 return -ENODEV;
430 if (!dev->dependent) {
431 if (pnp_assign_resources(dev, 0))
432 return 1;
433 else
434 return 0;
437 dep = dev->dependent;
438 do {
439 if (pnp_assign_resources(dev, i))
440 return 1;
442 /* if this dependent resource failed, try the next one */
443 dep = dep->next;
444 i++;
445 } while (dep);
447 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
448 return 0;
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)
459 if (!dev)
460 return -EINVAL;
461 if (dev->active) {
462 return 0; /* the device is already active */
465 /* ensure resources are allocated */
466 if (!pnp_auto_config_dev(dev))
467 return -EBUSY;
469 if (!pnp_can_write(dev)) {
470 pnp_info("Device %s does not supported activation.", dev->dev.bus_id);
471 return -EINVAL;
474 if (dev->protocol->set(dev, &dev->res)<0) {
475 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
476 return -EIO;
479 dev->active = 1;
480 pnp_info("Device %s activated.", dev->dev.bus_id);
482 return 1;
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)
493 if (!dev)
494 return -EINVAL;
495 if (!dev->active) {
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);
501 return -EINVAL;
503 if (dev->protocol->disable(dev)<0) {
504 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
505 return -EIO;
508 dev->active = 0;
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);
514 up(&pnp_res_mutex);
516 return 1;
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)
529 return;
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);