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>
9 #include <linux/errno.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/pnp.h>
14 #include <linux/slab.h>
15 #include <linux/bitmap.h>
18 DECLARE_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;
28 if (idx >= PNP_MAX_PORT) {
29 pnp_err("More than 4 ports is incompatible with pnp specifications.");
30 /* 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 (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
38 start = &dev->res.port_resource[idx].start;
39 end = &dev->res.port_resource[idx].end;
40 flags = &dev->res.port_resource[idx].flags;
42 /* set the initial values */
43 *flags |= rule->flags | IORESOURCE_IO;
44 *flags &= ~IORESOURCE_UNSET;
47 *flags |= IORESOURCE_DISABLED;
48 return 1; /* skip disabled resource requests */
52 *end = *start + rule->size - 1;
54 /* run through until pnp_check_port is happy */
55 while (!pnp_check_port(dev, idx)) {
56 *start += rule->align;
57 *end = *start + rule->size - 1;
58 if (*start > rule->max || !rule->align)
64 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
66 resource_size_t *start, *end;
72 if (idx >= PNP_MAX_MEM) {
73 pnp_err("More than 8 mems is incompatible with pnp specifications.");
74 /* pretend we were successful so at least the manager won't try again */
78 /* check if this resource has been manually set, if so skip */
79 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
82 start = &dev->res.mem_resource[idx].start;
83 end = &dev->res.mem_resource[idx].end;
84 flags = &dev->res.mem_resource[idx].flags;
86 /* set the initial values */
87 *flags |= rule->flags | IORESOURCE_MEM;
88 *flags &= ~IORESOURCE_UNSET;
90 /* convert pnp flags to standard Linux flags */
91 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
92 *flags |= IORESOURCE_READONLY;
93 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
94 *flags |= IORESOURCE_CACHEABLE;
95 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
96 *flags |= IORESOURCE_RANGELENGTH;
97 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
98 *flags |= IORESOURCE_SHADOWABLE;
101 *flags |= IORESOURCE_DISABLED;
102 return 1; /* skip disabled resource requests */
106 *end = *start + rule->size -1;
108 /* run through until pnp_check_mem is happy */
109 while (!pnp_check_mem(dev, idx)) {
110 *start += rule->align;
111 *end = *start + rule->size - 1;
112 if (*start > rule->max || !rule->align)
118 static int pnp_assign_irq(struct pnp_dev * dev, struct pnp_irq *rule, int idx)
120 resource_size_t *start, *end;
121 unsigned long *flags;
124 /* IRQ priority: this table is good for i386 */
125 static unsigned short xtab[16] = {
126 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
132 if (idx >= PNP_MAX_IRQ) {
133 pnp_err("More than 2 irqs is incompatible with pnp specifications.");
134 /* pretend we were successful so at least the manager won't try again */
138 /* check if this resource has been manually set, if so skip */
139 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
142 start = &dev->res.irq_resource[idx].start;
143 end = &dev->res.irq_resource[idx].end;
144 flags = &dev->res.irq_resource[idx].flags;
146 /* set the initial values */
147 *flags |= rule->flags | IORESOURCE_IRQ;
148 *flags &= ~IORESOURCE_UNSET;
150 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
151 *flags |= IORESOURCE_DISABLED;
152 return 1; /* skip disabled resource requests */
155 /* TBD: need check for >16 IRQ */
156 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
157 if (*start < PNP_IRQ_NR) {
161 for (i = 0; i < 16; i++) {
162 if(test_bit(xtab[i], rule->map)) {
163 *start = *end = xtab[i];
164 if(pnp_check_irq(dev, idx))
171 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
173 resource_size_t *start, *end;
174 unsigned long *flags;
177 /* DMA priority: this table is good for i386 */
178 static unsigned short xtab[8] = {
179 1, 3, 5, 6, 7, 0, 2, 4
185 if (idx >= PNP_MAX_DMA) {
186 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
187 /* pretend we were successful so at least the manager won't try again */
191 /* check if this resource has been manually set, if so skip */
192 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
195 start = &dev->res.dma_resource[idx].start;
196 end = &dev->res.dma_resource[idx].end;
197 flags = &dev->res.dma_resource[idx].flags;
199 /* set the initial values */
200 *flags |= rule->flags | IORESOURCE_DMA;
201 *flags &= ~IORESOURCE_UNSET;
204 *flags |= IORESOURCE_DISABLED;
205 return 1; /* skip disabled resource requests */
208 for (i = 0; i < 8; i++) {
209 if(rule->map & (1<<xtab[i])) {
210 *start = *end = xtab[i];
211 if(pnp_check_dma(dev, idx))
219 * pnp_init_resources - Resets a resource table to default values.
220 * @table: pointer to the desired resource table
223 void pnp_init_resource_table(struct pnp_resource_table *table)
226 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
227 table->irq_resource[idx].name = NULL;
228 table->irq_resource[idx].start = -1;
229 table->irq_resource[idx].end = -1;
230 table->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
232 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
233 table->dma_resource[idx].name = NULL;
234 table->dma_resource[idx].start = -1;
235 table->dma_resource[idx].end = -1;
236 table->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
238 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
239 table->port_resource[idx].name = NULL;
240 table->port_resource[idx].start = 0;
241 table->port_resource[idx].end = 0;
242 table->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
244 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
245 table->mem_resource[idx].name = NULL;
246 table->mem_resource[idx].start = 0;
247 table->mem_resource[idx].end = 0;
248 table->mem_resource[idx].flags = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
253 * pnp_clean_resources - clears resources that were not manually set
254 * @res: the resources to clean
257 static void pnp_clean_resource_table(struct pnp_resource_table * res)
260 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
261 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
263 res->irq_resource[idx].start = -1;
264 res->irq_resource[idx].end = -1;
265 res->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
267 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
268 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
270 res->dma_resource[idx].start = -1;
271 res->dma_resource[idx].end = -1;
272 res->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
274 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
275 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
277 res->port_resource[idx].start = 0;
278 res->port_resource[idx].end = 0;
279 res->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
281 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
282 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
284 res->mem_resource[idx].start = 0;
285 res->mem_resource[idx].end = 0;
286 res->mem_resource[idx].flags = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
291 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
292 * @dev: pointer to the desired device
293 * @depnum: the dependent function number
295 * Only set depnum to 0 if the device does not have dependent options.
297 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
299 struct pnp_port *port;
303 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
305 if (!pnp_can_configure(dev))
308 down(&pnp_res_mutex);
309 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
310 if (dev->independent) {
311 port = dev->independent->port;
312 mem = dev->independent->mem;
313 irq = dev->independent->irq;
314 dma = dev->independent->dma;
316 if (!pnp_assign_port(dev, port, nport))
322 if (!pnp_assign_mem(dev, mem, nmem))
328 if (!pnp_assign_irq(dev, irq, nirq))
334 if (!pnp_assign_dma(dev, dma, ndma))
342 struct pnp_option *dep;
344 for (i=1,dep=dev->dependent; i<depnum; i++, dep=dep->next)
352 if (!pnp_assign_port(dev, port, nport))
358 if (!pnp_assign_mem(dev, mem, nmem))
364 if (!pnp_assign_irq(dev, irq, nirq))
370 if (!pnp_assign_dma(dev, dma, ndma))
375 } else if (dev->dependent)
382 pnp_clean_resource_table(&dev->res);
388 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
389 * @dev: pointer to the desired device
390 * @res: pointer to the new resource config
391 * @mode: 0 or PNP_CONFIG_FORCE
393 * This function can be used by drivers that want to manually set thier resources.
395 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table * res, int mode)
398 struct pnp_resource_table * bak;
401 if (!pnp_can_configure(dev))
403 bak = pnp_alloc(sizeof(struct pnp_resource_table));
408 down(&pnp_res_mutex);
410 if (!(mode & PNP_CONFIG_FORCE)) {
411 for (i = 0; i < PNP_MAX_PORT; i++) {
412 if(!pnp_check_port(dev,i))
415 for (i = 0; i < PNP_MAX_MEM; i++) {
416 if(!pnp_check_mem(dev,i))
419 for (i = 0; i < PNP_MAX_IRQ; i++) {
420 if(!pnp_check_irq(dev,i))
423 for (i = 0; i < PNP_MAX_DMA; i++) {
424 if(!pnp_check_dma(dev,i))
441 * pnp_auto_config_dev - automatically assigns resources to a device
442 * @dev: pointer to the desired device
445 int pnp_auto_config_dev(struct pnp_dev *dev)
447 struct pnp_option *dep;
453 if(!pnp_can_configure(dev)) {
454 pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
458 if (!dev->dependent) {
459 if (pnp_assign_resources(dev, 0))
462 dep = dev->dependent;
464 if (pnp_assign_resources(dev, i))
471 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
476 * pnp_start_dev - low-level start of the PnP device
477 * @dev: pointer to the desired device
479 * assumes that resources have alread been allocated
482 int pnp_start_dev(struct pnp_dev *dev)
484 if (!pnp_can_write(dev)) {
485 pnp_info("Device %s does not support activation.", dev->dev.bus_id);
489 if (dev->protocol->set(dev, &dev->res)<0) {
490 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
494 pnp_info("Device %s activated.", dev->dev.bus_id);
500 * pnp_stop_dev - low-level disable of the PnP device
501 * @dev: pointer to the desired device
503 * does not free resources
506 int pnp_stop_dev(struct pnp_dev *dev)
508 if (!pnp_can_disable(dev)) {
509 pnp_info("Device %s does not support disabling.", dev->dev.bus_id);
512 if (dev->protocol->disable(dev)<0) {
513 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
517 pnp_info("Device %s disabled.", dev->dev.bus_id);
523 * pnp_activate_dev - activates a PnP device for use
524 * @dev: pointer to the desired device
526 * does not validate or set resources so be careful.
528 int pnp_activate_dev(struct pnp_dev *dev)
535 return 0; /* the device is already active */
538 /* ensure resources are allocated */
539 if (pnp_auto_config_dev(dev))
542 error = pnp_start_dev(dev);
552 * pnp_disable_dev - disables device
553 * @dev: pointer to the desired device
555 * inform the correct pnp protocol so that resources can be used by other devices
557 int pnp_disable_dev(struct pnp_dev *dev)
564 return 0; /* the device is already disabled */
567 error = pnp_stop_dev(dev);
573 /* release the resources so that other devices can use them */
574 down(&pnp_res_mutex);
575 pnp_clean_resource_table(&dev->res);
582 * pnp_resource_change - change one resource
583 * @resource: pointer to resource to be changed
584 * @start: start of region
585 * @size: size of region
588 void pnp_resource_change(struct resource *resource, resource_size_t start,
589 resource_size_t size)
591 if (resource == NULL)
593 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
594 resource->start = start;
595 resource->end = start + size - 1;
599 EXPORT_SYMBOL(pnp_manual_config_dev);
601 EXPORT_SYMBOL(pnp_auto_config_dev);
603 EXPORT_SYMBOL(pnp_start_dev);
604 EXPORT_SYMBOL(pnp_stop_dev);
605 EXPORT_SYMBOL(pnp_activate_dev);
606 EXPORT_SYMBOL(pnp_disable_dev);
607 EXPORT_SYMBOL(pnp_resource_change);
608 EXPORT_SYMBOL(pnp_init_resource_table);