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/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
21 #include <linux/pnp.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;
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 */
39 /* check if this resource has been manually set, if so skip */
40 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
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 |= rule->flags | IORESOURCE_IO;
49 *flags &= ~IORESOURCE_UNSET;
52 *flags |= IORESOURCE_DISABLED;
53 return 1; /* skip disabled resource requests */
57 *end = *start + rule->size - 1;
59 /* run through until pnp_check_port is happy */
60 while (!pnp_check_port(dev, idx)) {
61 *start += rule->align;
62 *end = *start + rule->size - 1;
63 if (*start > rule->max || !rule->align)
69 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
71 unsigned long *start, *end, *flags;
76 if (idx >= PNP_MAX_MEM) {
77 pnp_err("More than 8 mems is incompatible with pnp specifications.");
78 /* pretend we were successful so at least the manager won't try again */
82 /* check if this resource has been manually set, if so skip */
83 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
86 start = &dev->res.mem_resource[idx].start;
87 end = &dev->res.mem_resource[idx].end;
88 flags = &dev->res.mem_resource[idx].flags;
90 /* set the initial values */
91 *flags |= rule->flags | IORESOURCE_MEM;
92 *flags &= ~IORESOURCE_UNSET;
94 /* convert pnp flags to standard Linux flags */
95 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
96 *flags |= IORESOURCE_READONLY;
97 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
98 *flags |= IORESOURCE_CACHEABLE;
99 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
100 *flags |= IORESOURCE_RANGELENGTH;
101 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
102 *flags |= IORESOURCE_SHADOWABLE;
105 *flags |= IORESOURCE_DISABLED;
106 return 1; /* skip disabled resource requests */
110 *end = *start + rule->size -1;
112 /* run through until pnp_check_mem is happy */
113 while (!pnp_check_mem(dev, idx)) {
114 *start += rule->align;
115 *end = *start + rule->size - 1;
116 if (*start > rule->max || !rule->align)
122 static int pnp_assign_irq(struct pnp_dev * dev, struct pnp_irq *rule, int idx)
124 unsigned long *start, *end, *flags;
127 /* IRQ priority: this table is good for i386 */
128 static unsigned short xtab[16] = {
129 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
135 if (idx >= PNP_MAX_IRQ) {
136 pnp_err("More than 2 irqs is incompatible with pnp specifications.");
137 /* pretend we were successful so at least the manager won't try again */
141 /* check if this resource has been manually set, if so skip */
142 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
145 start = &dev->res.irq_resource[idx].start;
146 end = &dev->res.irq_resource[idx].end;
147 flags = &dev->res.irq_resource[idx].flags;
149 /* set the initial values */
150 *flags |= rule->flags | IORESOURCE_IRQ;
151 *flags &= ~IORESOURCE_UNSET;
153 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
154 *flags |= IORESOURCE_DISABLED;
155 return 1; /* skip disabled resource requests */
158 /* TBD: need check for >16 IRQ */
159 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
160 if (*start < PNP_IRQ_NR) {
164 for (i = 0; i < 16; i++) {
165 if(test_bit(xtab[i], rule->map)) {
166 *start = *end = xtab[i];
167 if(pnp_check_irq(dev, idx))
174 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
176 unsigned long *start, *end, *flags;
179 /* DMA priority: this table is good for i386 */
180 static unsigned short xtab[8] = {
181 1, 3, 5, 6, 7, 0, 2, 4
187 if (idx >= PNP_MAX_DMA) {
188 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
189 /* pretend we were successful so at least the manager won't try again */
193 /* check if this resource has been manually set, if so skip */
194 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
197 start = &dev->res.dma_resource[idx].start;
198 end = &dev->res.dma_resource[idx].end;
199 flags = &dev->res.dma_resource[idx].flags;
201 /* set the initial values */
202 *flags |= rule->flags | IORESOURCE_DMA;
203 *flags &= ~IORESOURCE_UNSET;
206 *flags |= IORESOURCE_DISABLED;
207 return 1; /* skip disabled resource requests */
210 for (i = 0; i < 8; i++) {
211 if(rule->map & (1<<xtab[i])) {
212 *start = *end = xtab[i];
213 if(pnp_check_dma(dev, idx))
221 * pnp_init_resources - Resets a resource table to default values.
222 * @table: pointer to the desired resource table
225 void pnp_init_resource_table(struct pnp_resource_table *table)
228 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
229 table->irq_resource[idx].name = NULL;
230 table->irq_resource[idx].start = -1;
231 table->irq_resource[idx].end = -1;
232 table->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
234 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
235 table->dma_resource[idx].name = NULL;
236 table->dma_resource[idx].start = -1;
237 table->dma_resource[idx].end = -1;
238 table->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
240 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
241 table->port_resource[idx].name = NULL;
242 table->port_resource[idx].start = 0;
243 table->port_resource[idx].end = 0;
244 table->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
246 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
247 table->mem_resource[idx].name = NULL;
248 table->mem_resource[idx].start = 0;
249 table->mem_resource[idx].end = 0;
250 table->mem_resource[idx].flags = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
255 * pnp_clean_resources - clears resources that were not manually set
256 * @res - the resources to clean
259 static void pnp_clean_resource_table(struct pnp_resource_table * res)
262 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
263 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
265 res->irq_resource[idx].start = -1;
266 res->irq_resource[idx].end = -1;
267 res->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
269 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
270 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
272 res->dma_resource[idx].start = -1;
273 res->dma_resource[idx].end = -1;
274 res->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
276 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
277 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
279 res->port_resource[idx].start = 0;
280 res->port_resource[idx].end = 0;
281 res->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
283 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
284 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
286 res->mem_resource[idx].start = 0;
287 res->mem_resource[idx].end = 0;
288 res->mem_resource[idx].flags = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
293 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
294 * @dev: pointer to the desired device
295 * @depnum: the dependent function number
297 * Only set depnum to 0 if the device does not have dependent options.
299 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
301 struct pnp_port *port;
305 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
307 if (!pnp_can_configure(dev))
310 down(&pnp_res_mutex);
311 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
312 if (dev->independent) {
313 port = dev->independent->port;
314 mem = dev->independent->mem;
315 irq = dev->independent->irq;
316 dma = dev->independent->dma;
318 if (!pnp_assign_port(dev, port, nport))
324 if (!pnp_assign_mem(dev, mem, nmem))
330 if (!pnp_assign_irq(dev, irq, nirq))
336 if (!pnp_assign_dma(dev, dma, ndma))
344 struct pnp_option *dep;
346 for (i=1,dep=dev->dependent; i<depnum; i++, dep=dep->next)
354 if (!pnp_assign_port(dev, port, nport))
360 if (!pnp_assign_mem(dev, mem, nmem))
366 if (!pnp_assign_irq(dev, irq, nirq))
372 if (!pnp_assign_dma(dev, dma, ndma))
377 } else if (dev->dependent)
384 pnp_clean_resource_table(&dev->res);
390 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
391 * @dev: pointer to the desired device
392 * @res: pointer to the new resource config
394 * This function can be used by drivers that want to manually set thier resources.
396 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table * res, int mode)
399 struct pnp_resource_table * bak;
402 if (!pnp_can_configure(dev))
404 bak = pnp_alloc(sizeof(struct pnp_resource_table));
409 down(&pnp_res_mutex);
411 if (!(mode & PNP_CONFIG_FORCE)) {
412 for (i = 0; i < PNP_MAX_PORT; i++) {
413 if(!pnp_check_port(dev,i))
416 for (i = 0; i < PNP_MAX_MEM; i++) {
417 if(!pnp_check_mem(dev,i))
420 for (i = 0; i < PNP_MAX_IRQ; i++) {
421 if(!pnp_check_irq(dev,i))
424 for (i = 0; i < PNP_MAX_DMA; i++) {
425 if(!pnp_check_dma(dev,i))
442 * pnp_auto_config_dev - automatically assigns resources to a device
443 * @dev: pointer to the desired device
446 int pnp_auto_config_dev(struct pnp_dev *dev)
448 struct pnp_option *dep;
454 if(!pnp_can_configure(dev)) {
455 pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
459 if (!dev->dependent) {
460 if (pnp_assign_resources(dev, 0))
463 dep = dev->dependent;
465 if (pnp_assign_resources(dev, i))
472 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
477 * pnp_activate_dev - activates a PnP device for use
478 * @dev: pointer to the desired device
480 * does not validate or set resources so be careful.
482 int pnp_activate_dev(struct pnp_dev *dev)
487 return 0; /* the device is already active */
490 /* ensure resources are allocated */
491 if (pnp_auto_config_dev(dev))
494 if (!pnp_can_write(dev)) {
495 pnp_info("Device %s does not supported activation.", dev->dev.bus_id);
499 if (dev->protocol->set(dev, &dev->res)<0) {
500 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
505 pnp_info("Device %s activated.", dev->dev.bus_id);
511 * pnp_disable_dev - disables device
512 * @dev: pointer to the desired device
514 * inform the correct pnp protocol so that resources can be used by other devices
516 int pnp_disable_dev(struct pnp_dev *dev)
521 return 0; /* the device is already disabled */
524 if (!pnp_can_disable(dev)) {
525 pnp_info("Device %s does not supported disabling.", dev->dev.bus_id);
528 if (dev->protocol->disable(dev)<0) {
529 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
534 pnp_info("Device %s disabled.", dev->dev.bus_id);
536 /* release the resources so that other devices can use them */
537 down(&pnp_res_mutex);
538 pnp_clean_resource_table(&dev->res);
545 * pnp_resource_change - change one resource
546 * @resource: pointer to resource to be changed
547 * @start: start of region
548 * @size: size of region
551 void pnp_resource_change(struct resource *resource, unsigned long start, unsigned long size)
553 if (resource == NULL)
555 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
556 resource->start = start;
557 resource->end = start + size - 1;
561 EXPORT_SYMBOL(pnp_manual_config_dev);
562 EXPORT_SYMBOL(pnp_auto_config_dev);
563 EXPORT_SYMBOL(pnp_activate_dev);
564 EXPORT_SYMBOL(pnp_disable_dev);
565 EXPORT_SYMBOL(pnp_resource_change);
566 EXPORT_SYMBOL(pnp_init_resource_table);