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>
15 #include <linux/mutex.h>
18 DEFINE_MUTEX(pnp_res_mutex);
20 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
22 struct resource *res, local_res;
24 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
26 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
27 "flags %#lx\n", idx, (unsigned long long) res->start,
28 (unsigned long long) res->end, res->flags);
33 res->flags = rule->flags | IORESOURCE_AUTO;
38 res->flags |= IORESOURCE_DISABLED;
39 dev_dbg(&dev->dev, " io %d disabled\n", idx);
43 res->start = rule->min;
44 res->end = res->start + rule->size - 1;
46 while (!pnp_check_port(dev, res)) {
47 res->start += rule->align;
48 res->end = res->start + rule->size - 1;
49 if (res->start > rule->max || !rule->align) {
50 dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
56 pnp_add_io_resource(dev, res->start, res->end, res->flags);
60 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
62 struct resource *res, local_res;
64 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
66 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
67 "flags %#lx\n", idx, (unsigned long long) res->start,
68 (unsigned long long) res->end, res->flags);
73 res->flags = rule->flags | IORESOURCE_AUTO;
77 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
78 res->flags |= IORESOURCE_READONLY;
79 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
80 res->flags |= IORESOURCE_CACHEABLE;
81 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
82 res->flags |= IORESOURCE_RANGELENGTH;
83 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
84 res->flags |= IORESOURCE_SHADOWABLE;
87 res->flags |= IORESOURCE_DISABLED;
88 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
92 res->start = rule->min;
93 res->end = res->start + rule->size - 1;
95 while (!pnp_check_mem(dev, res)) {
96 res->start += rule->align;
97 res->end = res->start + rule->size - 1;
98 if (res->start > rule->max || !rule->align) {
99 dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
105 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
109 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
111 struct resource *res, local_res;
114 /* IRQ priority: this table is good for i386 */
115 static unsigned short xtab[16] = {
116 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
119 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
121 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
122 idx, (int) res->start, res->flags);
127 res->flags = rule->flags | IORESOURCE_AUTO;
131 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
132 res->flags |= IORESOURCE_DISABLED;
133 dev_dbg(&dev->dev, " irq %d disabled\n", idx);
137 /* TBD: need check for >16 IRQ */
138 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
139 if (res->start < PNP_IRQ_NR) {
140 res->end = res->start;
143 for (i = 0; i < 16; i++) {
144 if (test_bit(xtab[i], rule->map.bits)) {
145 res->start = res->end = xtab[i];
146 if (pnp_check_irq(dev, res))
150 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
154 pnp_add_irq_resource(dev, res->start, res->flags);
158 static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
160 struct resource *res, local_res;
163 /* DMA priority: this table is good for i386 */
164 static unsigned short xtab[8] = {
165 1, 3, 5, 6, 7, 0, 2, 4
168 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
170 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
171 idx, (int) res->start, res->flags);
176 res->flags = rule->flags | IORESOURCE_AUTO;
180 for (i = 0; i < 8; i++) {
181 if (rule->map & (1 << xtab[i])) {
182 res->start = res->end = xtab[i];
183 if (pnp_check_dma(dev, res))
187 #ifdef MAX_DMA_CHANNELS
188 res->start = res->end = MAX_DMA_CHANNELS;
190 res->flags |= IORESOURCE_DISABLED;
191 dev_dbg(&dev->dev, " disable dma %d\n", idx);
194 pnp_add_dma_resource(dev, res->start, res->flags);
197 void pnp_init_resources(struct pnp_dev *dev)
199 pnp_free_resources(dev);
202 static void pnp_clean_resource_table(struct pnp_dev *dev)
204 struct pnp_resource *pnp_res, *tmp;
206 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
207 if (pnp_res->res.flags & IORESOURCE_AUTO)
208 pnp_free_resource(pnp_res);
213 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
214 * @dev: pointer to the desired device
215 * @depnum: the dependent function number
217 * Only set depnum to 0 if the device does not have dependent options.
219 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
221 struct pnp_port *port;
225 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
227 if (!pnp_can_configure(dev))
230 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
231 mutex_lock(&pnp_res_mutex);
232 pnp_clean_resource_table(dev);
233 if (dev->independent) {
234 dev_dbg(&dev->dev, "assigning independent options\n");
235 port = dev->independent->port;
236 mem = dev->independent->mem;
237 irq = dev->independent->irq;
238 dma = dev->independent->dma;
240 if (!pnp_assign_port(dev, port, nport))
246 if (!pnp_assign_mem(dev, mem, nmem))
252 if (!pnp_assign_irq(dev, irq, nirq))
258 pnp_assign_dma(dev, dma, ndma);
265 struct pnp_option *dep;
268 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
269 for (i = 1, dep = dev->dependent; i < depnum;
270 i++, dep = dep->next)
278 if (!pnp_assign_port(dev, port, nport))
284 if (!pnp_assign_mem(dev, mem, nmem))
290 if (!pnp_assign_irq(dev, irq, nirq))
296 pnp_assign_dma(dev, dma, ndma);
300 } else if (dev->dependent)
303 mutex_unlock(&pnp_res_mutex);
304 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
308 pnp_clean_resource_table(dev);
309 mutex_unlock(&pnp_res_mutex);
310 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
315 * pnp_auto_config_dev - automatically assigns resources to a device
316 * @dev: pointer to the desired device
318 int pnp_auto_config_dev(struct pnp_dev *dev)
320 struct pnp_option *dep;
323 if (!pnp_can_configure(dev)) {
324 dev_dbg(&dev->dev, "configuration not supported\n");
328 if (!dev->dependent) {
329 if (pnp_assign_resources(dev, 0))
332 dep = dev->dependent;
334 if (pnp_assign_resources(dev, i))
341 dev_err(&dev->dev, "unable to assign resources\n");
346 * pnp_start_dev - low-level start of the PnP device
347 * @dev: pointer to the desired device
349 * assumes that resources have already been allocated
351 int pnp_start_dev(struct pnp_dev *dev)
353 if (!pnp_can_write(dev)) {
354 dev_dbg(&dev->dev, "activation not supported\n");
358 dbg_pnp_show_resources(dev, "pnp_start_dev");
359 if (dev->protocol->set(dev) < 0) {
360 dev_err(&dev->dev, "activation failed\n");
364 dev_info(&dev->dev, "activated\n");
369 * pnp_stop_dev - low-level disable of the PnP device
370 * @dev: pointer to the desired device
372 * does not free resources
374 int pnp_stop_dev(struct pnp_dev *dev)
376 if (!pnp_can_disable(dev)) {
377 dev_dbg(&dev->dev, "disabling not supported\n");
380 if (dev->protocol->disable(dev) < 0) {
381 dev_err(&dev->dev, "disable failed\n");
385 dev_info(&dev->dev, "disabled\n");
390 * pnp_activate_dev - activates a PnP device for use
391 * @dev: pointer to the desired device
393 * does not validate or set resources so be careful.
395 int pnp_activate_dev(struct pnp_dev *dev)
402 /* ensure resources are allocated */
403 if (pnp_auto_config_dev(dev))
406 error = pnp_start_dev(dev);
415 * pnp_disable_dev - disables device
416 * @dev: pointer to the desired device
418 * inform the correct pnp protocol so that resources can be used by other devices
420 int pnp_disable_dev(struct pnp_dev *dev)
427 error = pnp_stop_dev(dev);
433 /* release the resources so that other devices can use them */
434 mutex_lock(&pnp_res_mutex);
435 pnp_clean_resource_table(dev);
436 mutex_unlock(&pnp_res_mutex);
441 EXPORT_SYMBOL(pnp_start_dev);
442 EXPORT_SYMBOL(pnp_stop_dev);
443 EXPORT_SYMBOL(pnp_activate_dev);
444 EXPORT_SYMBOL(pnp_disable_dev);