PNP: add pnp_init_resources(struct pnp_dev *) interface
[wandboard.git] / drivers / pnp / manager.c
blobc9af87a8fb1657995e00f577fe23d2b3b02288cd
1 /*
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>
6 */
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>
16 #include "base.h"
18 DEFINE_MUTEX(pnp_res_mutex);
20 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
22 resource_size_t *start, *end;
23 unsigned long *flags;
25 if (idx >= PNP_MAX_PORT) {
26 dev_err(&dev->dev, "too many I/O port resources\n");
27 /* pretend we were successful so at least the manager won't try again */
28 return 1;
31 start = &dev->res.port_resource[idx].start;
32 end = &dev->res.port_resource[idx].end;
33 flags = &dev->res.port_resource[idx].flags;
35 /* check if this resource has been manually set, if so skip */
36 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO)) {
37 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
38 "flags %#lx\n", idx, (unsigned long long) *start,
39 (unsigned long long) *end, *flags);
40 return 1;
43 /* set the initial values */
44 *flags |= rule->flags | IORESOURCE_IO;
45 *flags &= ~IORESOURCE_UNSET;
47 if (!rule->size) {
48 *flags |= IORESOURCE_DISABLED;
49 dev_dbg(&dev->dev, " io %d disabled\n", idx);
50 return 1; /* skip disabled resource requests */
53 *start = rule->min;
54 *end = *start + rule->size - 1;
56 /* run through until pnp_check_port is happy */
57 while (!pnp_check_port(dev, idx)) {
58 *start += rule->align;
59 *end = *start + rule->size - 1;
60 if (*start > rule->max || !rule->align) {
61 dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
62 return 0;
65 dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx,
66 (unsigned long long) *start, (unsigned long long) *end);
67 return 1;
70 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
72 resource_size_t *start, *end;
73 unsigned long *flags;
75 if (idx >= PNP_MAX_MEM) {
76 dev_err(&dev->dev, "too many memory resources\n");
77 /* pretend we were successful so at least the manager won't try again */
78 return 1;
81 start = &dev->res.mem_resource[idx].start;
82 end = &dev->res.mem_resource[idx].end;
83 flags = &dev->res.mem_resource[idx].flags;
85 /* check if this resource has been manually set, if so skip */
86 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO)) {
87 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
88 "flags %#lx\n", idx, (unsigned long long) *start,
89 (unsigned long long) *end, *flags);
90 return 1;
93 /* set the initial values */
94 *flags |= rule->flags | IORESOURCE_MEM;
95 *flags &= ~IORESOURCE_UNSET;
97 /* convert pnp flags to standard Linux flags */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 *flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
101 *flags |= IORESOURCE_CACHEABLE;
102 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
103 *flags |= IORESOURCE_RANGELENGTH;
104 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
105 *flags |= IORESOURCE_SHADOWABLE;
107 if (!rule->size) {
108 *flags |= IORESOURCE_DISABLED;
109 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
110 return 1; /* skip disabled resource requests */
113 *start = rule->min;
114 *end = *start + rule->size - 1;
116 /* run through until pnp_check_mem is happy */
117 while (!pnp_check_mem(dev, idx)) {
118 *start += rule->align;
119 *end = *start + rule->size - 1;
120 if (*start > rule->max || !rule->align) {
121 dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
122 return 0;
125 dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx,
126 (unsigned long long) *start, (unsigned long long) *end);
127 return 1;
130 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
132 resource_size_t *start, *end;
133 unsigned long *flags;
134 int i;
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 if (idx >= PNP_MAX_IRQ) {
142 dev_err(&dev->dev, "too many IRQ resources\n");
143 /* pretend we were successful so at least the manager won't try again */
144 return 1;
147 start = &dev->res.irq_resource[idx].start;
148 end = &dev->res.irq_resource[idx].end;
149 flags = &dev->res.irq_resource[idx].flags;
151 /* check if this resource has been manually set, if so skip */
152 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO)) {
153 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
154 idx, (int) *start, *flags);
155 return 1;
158 /* set the initial values */
159 *flags |= rule->flags | IORESOURCE_IRQ;
160 *flags &= ~IORESOURCE_UNSET;
162 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
163 *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 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
170 if (*start < PNP_IRQ_NR) {
171 *end = *start;
172 dev_dbg(&dev->dev, " assign irq %d %d\n", idx, (int) *start);
173 return 1;
175 for (i = 0; i < 16; i++) {
176 if (test_bit(xtab[i], rule->map)) {
177 *start = *end = xtab[i];
178 if (pnp_check_irq(dev, idx)) {
179 dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
180 (int) *start);
181 return 1;
185 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
186 return 0;
189 static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
191 resource_size_t *start, *end;
192 unsigned long *flags;
193 int i;
195 /* DMA priority: this table is good for i386 */
196 static unsigned short xtab[8] = {
197 1, 3, 5, 6, 7, 0, 2, 4
200 if (idx >= PNP_MAX_DMA) {
201 dev_err(&dev->dev, "too many DMA resources\n");
202 return;
205 start = &dev->res.dma_resource[idx].start;
206 end = &dev->res.dma_resource[idx].end;
207 flags = &dev->res.dma_resource[idx].flags;
209 /* check if this resource has been manually set, if so skip */
210 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO)) {
211 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
212 idx, (int) *start, *flags);
213 return;
216 /* set the initial values */
217 *flags |= rule->flags | IORESOURCE_DMA;
218 *flags &= ~IORESOURCE_UNSET;
220 for (i = 0; i < 8; i++) {
221 if (rule->map & (1 << xtab[i])) {
222 *start = *end = xtab[i];
223 if (pnp_check_dma(dev, idx)) {
224 dev_dbg(&dev->dev, " assign dma %d %d\n", idx,
225 (int) *start);
226 return;
230 #ifdef MAX_DMA_CHANNELS
231 *start = *end = MAX_DMA_CHANNELS;
232 #endif
233 *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
234 dev_dbg(&dev->dev, " disable dma %d\n", idx);
238 * pnp_init_resources - Resets a resource table to default values.
239 * @table: pointer to the desired resource table
241 void pnp_init_resource_table(struct pnp_resource_table *table)
243 int idx;
245 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
246 table->irq_resource[idx].name = NULL;
247 table->irq_resource[idx].start = -1;
248 table->irq_resource[idx].end = -1;
249 table->irq_resource[idx].flags =
250 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
252 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
253 table->dma_resource[idx].name = NULL;
254 table->dma_resource[idx].start = -1;
255 table->dma_resource[idx].end = -1;
256 table->dma_resource[idx].flags =
257 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
259 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
260 table->port_resource[idx].name = NULL;
261 table->port_resource[idx].start = 0;
262 table->port_resource[idx].end = 0;
263 table->port_resource[idx].flags =
264 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
266 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
267 table->mem_resource[idx].name = NULL;
268 table->mem_resource[idx].start = 0;
269 table->mem_resource[idx].end = 0;
270 table->mem_resource[idx].flags =
271 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
275 void pnp_init_resources(struct pnp_dev *dev)
277 pnp_init_resource_table(&dev->res);
281 * pnp_clean_resources - clears resources that were not manually set
282 * @res: the resources to clean
284 static void pnp_clean_resource_table(struct pnp_resource_table *res)
286 int idx;
288 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
289 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
290 continue;
291 res->irq_resource[idx].start = -1;
292 res->irq_resource[idx].end = -1;
293 res->irq_resource[idx].flags =
294 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
296 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
297 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
298 continue;
299 res->dma_resource[idx].start = -1;
300 res->dma_resource[idx].end = -1;
301 res->dma_resource[idx].flags =
302 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
304 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
305 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
306 continue;
307 res->port_resource[idx].start = 0;
308 res->port_resource[idx].end = 0;
309 res->port_resource[idx].flags =
310 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
312 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
313 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
314 continue;
315 res->mem_resource[idx].start = 0;
316 res->mem_resource[idx].end = 0;
317 res->mem_resource[idx].flags =
318 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
323 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
324 * @dev: pointer to the desired device
325 * @depnum: the dependent function number
327 * Only set depnum to 0 if the device does not have dependent options.
329 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
331 struct pnp_port *port;
332 struct pnp_mem *mem;
333 struct pnp_irq *irq;
334 struct pnp_dma *dma;
335 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
337 if (!pnp_can_configure(dev))
338 return -ENODEV;
340 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
341 mutex_lock(&pnp_res_mutex);
342 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
343 if (dev->independent) {
344 dev_dbg(&dev->dev, "assigning independent options\n");
345 port = dev->independent->port;
346 mem = dev->independent->mem;
347 irq = dev->independent->irq;
348 dma = dev->independent->dma;
349 while (port) {
350 if (!pnp_assign_port(dev, port, nport))
351 goto fail;
352 nport++;
353 port = port->next;
355 while (mem) {
356 if (!pnp_assign_mem(dev, mem, nmem))
357 goto fail;
358 nmem++;
359 mem = mem->next;
361 while (irq) {
362 if (!pnp_assign_irq(dev, irq, nirq))
363 goto fail;
364 nirq++;
365 irq = irq->next;
367 while (dma) {
368 pnp_assign_dma(dev, dma, ndma);
369 ndma++;
370 dma = dma->next;
374 if (depnum) {
375 struct pnp_option *dep;
376 int i;
378 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
379 for (i = 1, dep = dev->dependent; i < depnum;
380 i++, dep = dep->next)
381 if (!dep)
382 goto fail;
383 port = dep->port;
384 mem = dep->mem;
385 irq = dep->irq;
386 dma = dep->dma;
387 while (port) {
388 if (!pnp_assign_port(dev, port, nport))
389 goto fail;
390 nport++;
391 port = port->next;
393 while (mem) {
394 if (!pnp_assign_mem(dev, mem, nmem))
395 goto fail;
396 nmem++;
397 mem = mem->next;
399 while (irq) {
400 if (!pnp_assign_irq(dev, irq, nirq))
401 goto fail;
402 nirq++;
403 irq = irq->next;
405 while (dma) {
406 pnp_assign_dma(dev, dma, ndma);
407 ndma++;
408 dma = dma->next;
410 } else if (dev->dependent)
411 goto fail;
413 mutex_unlock(&pnp_res_mutex);
414 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
415 return 1;
417 fail:
418 pnp_clean_resource_table(&dev->res);
419 mutex_unlock(&pnp_res_mutex);
420 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
421 return 0;
425 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
426 * @dev: pointer to the desired device
427 * @res: pointer to the new resource config
428 * @mode: 0 or PNP_CONFIG_FORCE
430 * This function can be used by drivers that want to manually set thier resources.
432 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
433 int mode)
435 int i;
436 struct pnp_resource_table *bak;
438 if (!pnp_can_configure(dev))
439 return -ENODEV;
440 bak = pnp_alloc(sizeof(struct pnp_resource_table));
441 if (!bak)
442 return -ENOMEM;
443 *bak = dev->res;
445 mutex_lock(&pnp_res_mutex);
446 dev->res = *res;
447 if (!(mode & PNP_CONFIG_FORCE)) {
448 for (i = 0; i < PNP_MAX_PORT; i++) {
449 if (!pnp_check_port(dev, i))
450 goto fail;
452 for (i = 0; i < PNP_MAX_MEM; i++) {
453 if (!pnp_check_mem(dev, i))
454 goto fail;
456 for (i = 0; i < PNP_MAX_IRQ; i++) {
457 if (!pnp_check_irq(dev, i))
458 goto fail;
460 for (i = 0; i < PNP_MAX_DMA; i++) {
461 if (!pnp_check_dma(dev, i))
462 goto fail;
465 mutex_unlock(&pnp_res_mutex);
467 kfree(bak);
468 return 0;
470 fail:
471 dev->res = *bak;
472 mutex_unlock(&pnp_res_mutex);
473 kfree(bak);
474 return -EINVAL;
478 * pnp_auto_config_dev - automatically assigns resources to a device
479 * @dev: pointer to the desired device
481 int pnp_auto_config_dev(struct pnp_dev *dev)
483 struct pnp_option *dep;
484 int i = 1;
486 if (!pnp_can_configure(dev)) {
487 dev_dbg(&dev->dev, "configuration not supported\n");
488 return -ENODEV;
491 if (!dev->dependent) {
492 if (pnp_assign_resources(dev, 0))
493 return 0;
494 } else {
495 dep = dev->dependent;
496 do {
497 if (pnp_assign_resources(dev, i))
498 return 0;
499 dep = dep->next;
500 i++;
501 } while (dep);
504 dev_err(&dev->dev, "unable to assign resources\n");
505 return -EBUSY;
509 * pnp_start_dev - low-level start of the PnP device
510 * @dev: pointer to the desired device
512 * assumes that resources have already been allocated
514 int pnp_start_dev(struct pnp_dev *dev)
516 if (!pnp_can_write(dev)) {
517 dev_dbg(&dev->dev, "activation not supported\n");
518 return -EINVAL;
521 dbg_pnp_show_resources(dev, "pnp_start_dev");
522 if (dev->protocol->set(dev) < 0) {
523 dev_err(&dev->dev, "activation failed\n");
524 return -EIO;
527 dev_info(&dev->dev, "activated\n");
528 return 0;
532 * pnp_stop_dev - low-level disable of the PnP device
533 * @dev: pointer to the desired device
535 * does not free resources
537 int pnp_stop_dev(struct pnp_dev *dev)
539 if (!pnp_can_disable(dev)) {
540 dev_dbg(&dev->dev, "disabling not supported\n");
541 return -EINVAL;
543 if (dev->protocol->disable(dev) < 0) {
544 dev_err(&dev->dev, "disable failed\n");
545 return -EIO;
548 dev_info(&dev->dev, "disabled\n");
549 return 0;
553 * pnp_activate_dev - activates a PnP device for use
554 * @dev: pointer to the desired device
556 * does not validate or set resources so be careful.
558 int pnp_activate_dev(struct pnp_dev *dev)
560 int error;
562 if (dev->active)
563 return 0;
565 /* ensure resources are allocated */
566 if (pnp_auto_config_dev(dev))
567 return -EBUSY;
569 error = pnp_start_dev(dev);
570 if (error)
571 return error;
573 dev->active = 1;
574 return 0;
578 * pnp_disable_dev - disables device
579 * @dev: pointer to the desired device
581 * inform the correct pnp protocol so that resources can be used by other devices
583 int pnp_disable_dev(struct pnp_dev *dev)
585 int error;
587 if (!dev->active)
588 return 0;
590 error = pnp_stop_dev(dev);
591 if (error)
592 return error;
594 dev->active = 0;
596 /* release the resources so that other devices can use them */
597 mutex_lock(&pnp_res_mutex);
598 pnp_clean_resource_table(&dev->res);
599 mutex_unlock(&pnp_res_mutex);
601 return 0;
605 * pnp_resource_change - change one resource
606 * @resource: pointer to resource to be changed
607 * @start: start of region
608 * @size: size of region
610 void pnp_resource_change(struct resource *resource, resource_size_t start,
611 resource_size_t size)
613 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
614 resource->start = start;
615 resource->end = start + size - 1;
618 EXPORT_SYMBOL(pnp_manual_config_dev);
619 EXPORT_SYMBOL(pnp_start_dev);
620 EXPORT_SYMBOL(pnp_stop_dev);
621 EXPORT_SYMBOL(pnp_activate_dev);
622 EXPORT_SYMBOL(pnp_disable_dev);
623 EXPORT_SYMBOL(pnp_resource_change);
624 EXPORT_SYMBOL(pnp_init_resource_table);