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>
17 DECLARE_MUTEX(pnp_res_mutex);
19 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
21 resource_size_t *start, *end;
24 if (idx >= PNP_MAX_PORT) {
26 ("More than 4 ports is incompatible with pnp specifications.");
27 /* pretend we were successful so at least the manager won't try again */
31 /* check if this resource has been manually set, if so skip */
32 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
35 start = &dev->res.port_resource[idx].start;
36 end = &dev->res.port_resource[idx].end;
37 flags = &dev->res.port_resource[idx].flags;
39 /* set the initial values */
40 *flags |= rule->flags | IORESOURCE_IO;
41 *flags &= ~IORESOURCE_UNSET;
44 *flags |= IORESOURCE_DISABLED;
45 return 1; /* skip disabled resource requests */
49 *end = *start + rule->size - 1;
51 /* run through until pnp_check_port is happy */
52 while (!pnp_check_port(dev, idx)) {
53 *start += rule->align;
54 *end = *start + rule->size - 1;
55 if (*start > rule->max || !rule->align)
61 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
63 resource_size_t *start, *end;
66 if (idx >= PNP_MAX_MEM) {
68 ("More than 8 mems is incompatible with pnp specifications.");
69 /* pretend we were successful so at least the manager won't try again */
73 /* check if this resource has been manually set, if so skip */
74 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
77 start = &dev->res.mem_resource[idx].start;
78 end = &dev->res.mem_resource[idx].end;
79 flags = &dev->res.mem_resource[idx].flags;
81 /* set the initial values */
82 *flags |= rule->flags | IORESOURCE_MEM;
83 *flags &= ~IORESOURCE_UNSET;
85 /* convert pnp flags to standard Linux flags */
86 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
87 *flags |= IORESOURCE_READONLY;
88 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
89 *flags |= IORESOURCE_CACHEABLE;
90 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
91 *flags |= IORESOURCE_RANGELENGTH;
92 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
93 *flags |= IORESOURCE_SHADOWABLE;
96 *flags |= IORESOURCE_DISABLED;
97 return 1; /* skip disabled resource requests */
101 *end = *start + rule->size - 1;
103 /* run through until pnp_check_mem is happy */
104 while (!pnp_check_mem(dev, idx)) {
105 *start += rule->align;
106 *end = *start + rule->size - 1;
107 if (*start > rule->max || !rule->align)
113 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
115 resource_size_t *start, *end;
116 unsigned long *flags;
119 /* IRQ priority: this table is good for i386 */
120 static unsigned short xtab[16] = {
121 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
124 if (idx >= PNP_MAX_IRQ) {
126 ("More than 2 irqs is incompatible with pnp specifications.");
127 /* pretend we were successful so at least the manager won't try again */
131 /* check if this resource has been manually set, if so skip */
132 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
135 start = &dev->res.irq_resource[idx].start;
136 end = &dev->res.irq_resource[idx].end;
137 flags = &dev->res.irq_resource[idx].flags;
139 /* set the initial values */
140 *flags |= rule->flags | IORESOURCE_IRQ;
141 *flags &= ~IORESOURCE_UNSET;
143 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
144 *flags |= IORESOURCE_DISABLED;
145 return 1; /* skip disabled resource requests */
148 /* TBD: need check for >16 IRQ */
149 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
150 if (*start < PNP_IRQ_NR) {
154 for (i = 0; i < 16; i++) {
155 if (test_bit(xtab[i], rule->map)) {
156 *start = *end = xtab[i];
157 if (pnp_check_irq(dev, idx))
164 static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
166 resource_size_t *start, *end;
167 unsigned long *flags;
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 (idx >= PNP_MAX_DMA) {
176 pnp_err("More than 2 dmas is incompatible with pnp "
181 /* check if this resource has been manually set, if so skip */
182 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
185 start = &dev->res.dma_resource[idx].start;
186 end = &dev->res.dma_resource[idx].end;
187 flags = &dev->res.dma_resource[idx].flags;
189 /* set the initial values */
190 *flags |= rule->flags | IORESOURCE_DMA;
191 *flags &= ~IORESOURCE_UNSET;
193 for (i = 0; i < 8; i++) {
194 if (rule->map & (1 << xtab[i])) {
195 *start = *end = xtab[i];
196 if (pnp_check_dma(dev, idx))
200 #ifdef MAX_DMA_CHANNELS
201 *start = *end = MAX_DMA_CHANNELS;
203 *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
207 * pnp_init_resources - Resets a resource table to default values.
208 * @table: pointer to the desired resource table
210 void pnp_init_resource_table(struct pnp_resource_table *table)
214 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
215 table->irq_resource[idx].name = NULL;
216 table->irq_resource[idx].start = -1;
217 table->irq_resource[idx].end = -1;
218 table->irq_resource[idx].flags =
219 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
221 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
222 table->dma_resource[idx].name = NULL;
223 table->dma_resource[idx].start = -1;
224 table->dma_resource[idx].end = -1;
225 table->dma_resource[idx].flags =
226 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
228 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
229 table->port_resource[idx].name = NULL;
230 table->port_resource[idx].start = 0;
231 table->port_resource[idx].end = 0;
232 table->port_resource[idx].flags =
233 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
235 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
236 table->mem_resource[idx].name = NULL;
237 table->mem_resource[idx].start = 0;
238 table->mem_resource[idx].end = 0;
239 table->mem_resource[idx].flags =
240 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
245 * pnp_clean_resources - clears resources that were not manually set
246 * @res: the resources to clean
248 static void pnp_clean_resource_table(struct pnp_resource_table *res)
252 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
253 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
255 res->irq_resource[idx].start = -1;
256 res->irq_resource[idx].end = -1;
257 res->irq_resource[idx].flags =
258 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
260 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
261 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
263 res->dma_resource[idx].start = -1;
264 res->dma_resource[idx].end = -1;
265 res->dma_resource[idx].flags =
266 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
268 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
269 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
271 res->port_resource[idx].start = 0;
272 res->port_resource[idx].end = 0;
273 res->port_resource[idx].flags =
274 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
276 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
277 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
279 res->mem_resource[idx].start = 0;
280 res->mem_resource[idx].end = 0;
281 res->mem_resource[idx].flags =
282 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
287 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
288 * @dev: pointer to the desired device
289 * @depnum: the dependent function number
291 * Only set depnum to 0 if the device does not have dependent options.
293 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
295 struct pnp_port *port;
299 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
301 if (!pnp_can_configure(dev))
304 down(&pnp_res_mutex);
305 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
306 if (dev->independent) {
307 port = dev->independent->port;
308 mem = dev->independent->mem;
309 irq = dev->independent->irq;
310 dma = dev->independent->dma;
312 if (!pnp_assign_port(dev, port, nport))
318 if (!pnp_assign_mem(dev, mem, nmem))
324 if (!pnp_assign_irq(dev, irq, nirq))
330 pnp_assign_dma(dev, dma, ndma);
337 struct pnp_option *dep;
339 for (i = 1, dep = dev->dependent; i < depnum;
340 i++, dep = dep->next)
348 if (!pnp_assign_port(dev, port, nport))
354 if (!pnp_assign_mem(dev, mem, nmem))
360 if (!pnp_assign_irq(dev, irq, nirq))
366 pnp_assign_dma(dev, dma, ndma);
370 } else if (dev->dependent)
377 pnp_clean_resource_table(&dev->res);
383 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
384 * @dev: pointer to the desired device
385 * @res: pointer to the new resource config
386 * @mode: 0 or PNP_CONFIG_FORCE
388 * This function can be used by drivers that want to manually set thier resources.
390 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
394 struct pnp_resource_table *bak;
396 if (!pnp_can_configure(dev))
398 bak = pnp_alloc(sizeof(struct pnp_resource_table));
403 down(&pnp_res_mutex);
405 if (!(mode & PNP_CONFIG_FORCE)) {
406 for (i = 0; i < PNP_MAX_PORT; i++) {
407 if (!pnp_check_port(dev, i))
410 for (i = 0; i < PNP_MAX_MEM; i++) {
411 if (!pnp_check_mem(dev, i))
414 for (i = 0; i < PNP_MAX_IRQ; i++) {
415 if (!pnp_check_irq(dev, i))
418 for (i = 0; i < PNP_MAX_DMA; i++) {
419 if (!pnp_check_dma(dev, i))
436 * pnp_auto_config_dev - automatically assigns resources to a device
437 * @dev: pointer to the desired device
439 int pnp_auto_config_dev(struct pnp_dev *dev)
441 struct pnp_option *dep;
444 if (!pnp_can_configure(dev)) {
445 pnp_dbg("Device %s does not support resource configuration.",
450 if (!dev->dependent) {
451 if (pnp_assign_resources(dev, 0))
454 dep = dev->dependent;
456 if (pnp_assign_resources(dev, i))
463 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
468 * pnp_start_dev - low-level start of the PnP device
469 * @dev: pointer to the desired device
471 * assumes that resources have already been allocated
473 int pnp_start_dev(struct pnp_dev *dev)
475 if (!pnp_can_write(dev)) {
476 pnp_dbg("Device %s does not support activation.",
481 if (dev->protocol->set(dev, &dev->res) < 0) {
482 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
486 pnp_info("Device %s activated.", dev->dev.bus_id);
491 * pnp_stop_dev - low-level disable of the PnP device
492 * @dev: pointer to the desired device
494 * does not free resources
496 int pnp_stop_dev(struct pnp_dev *dev)
498 if (!pnp_can_disable(dev)) {
499 pnp_dbg("Device %s does not support disabling.",
503 if (dev->protocol->disable(dev) < 0) {
504 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
508 pnp_info("Device %s disabled.", dev->dev.bus_id);
513 * pnp_activate_dev - activates a PnP device for use
514 * @dev: pointer to the desired device
516 * does not validate or set resources so be careful.
518 int pnp_activate_dev(struct pnp_dev *dev)
523 return 0; /* the device is already active */
525 /* ensure resources are allocated */
526 if (pnp_auto_config_dev(dev))
529 error = pnp_start_dev(dev);
538 * pnp_disable_dev - disables device
539 * @dev: pointer to the desired device
541 * inform the correct pnp protocol so that resources can be used by other devices
543 int pnp_disable_dev(struct pnp_dev *dev)
548 return 0; /* the device is already disabled */
550 error = pnp_stop_dev(dev);
556 /* release the resources so that other devices can use them */
557 down(&pnp_res_mutex);
558 pnp_clean_resource_table(&dev->res);
565 * pnp_resource_change - change one resource
566 * @resource: pointer to resource to be changed
567 * @start: start of region
568 * @size: size of region
570 void pnp_resource_change(struct resource *resource, resource_size_t start,
571 resource_size_t size)
573 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
574 resource->start = start;
575 resource->end = start + size - 1;
578 EXPORT_SYMBOL(pnp_manual_config_dev);
579 EXPORT_SYMBOL(pnp_start_dev);
580 EXPORT_SYMBOL(pnp_stop_dev);
581 EXPORT_SYMBOL(pnp_activate_dev);
582 EXPORT_SYMBOL(pnp_disable_dev);
583 EXPORT_SYMBOL(pnp_resource_change);
584 EXPORT_SYMBOL(pnp_init_resource_table);