b43: Fix Bugzilla #14181 and the bug from the previous 'fix'
[linux-2.6/verdex.git] / drivers / pnp / manager.c
blob00fd3577b98575abb218972a4e35d1d62f6366c2
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 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
8 */
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/pnp.h>
15 #include <linux/slab.h>
16 #include <linux/bitmap.h>
17 #include <linux/mutex.h>
18 #include "base.h"
20 DEFINE_MUTEX(pnp_res_mutex);
22 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
24 struct resource *res, local_res;
26 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
27 if (res) {
28 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
29 "flags %#lx\n", idx, (unsigned long long) res->start,
30 (unsigned long long) res->end, res->flags);
31 return 0;
34 res = &local_res;
35 res->flags = rule->flags | IORESOURCE_AUTO;
36 res->start = 0;
37 res->end = 0;
39 if (!rule->size) {
40 res->flags |= IORESOURCE_DISABLED;
41 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
42 goto __add;
45 res->start = rule->min;
46 res->end = res->start + rule->size - 1;
48 while (!pnp_check_port(dev, res)) {
49 res->start += rule->align;
50 res->end = res->start + rule->size - 1;
51 if (res->start > rule->max || !rule->align) {
52 pnp_dbg(&dev->dev, " couldn't assign io %d "
53 "(min %#llx max %#llx)\n", idx,
54 (unsigned long long) rule->min,
55 (unsigned long long) rule->max);
56 return -EBUSY;
60 __add:
61 pnp_add_io_resource(dev, res->start, res->end, res->flags);
62 return 0;
65 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
67 struct resource *res, local_res;
69 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
70 if (res) {
71 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
72 "flags %#lx\n", idx, (unsigned long long) res->start,
73 (unsigned long long) res->end, res->flags);
74 return 0;
77 res = &local_res;
78 res->flags = rule->flags | IORESOURCE_AUTO;
79 res->start = 0;
80 res->end = 0;
82 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
83 res->flags |= IORESOURCE_READONLY;
84 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
85 res->flags |= IORESOURCE_CACHEABLE;
86 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
87 res->flags |= IORESOURCE_RANGELENGTH;
88 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
89 res->flags |= IORESOURCE_SHADOWABLE;
91 if (!rule->size) {
92 res->flags |= IORESOURCE_DISABLED;
93 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
94 goto __add;
97 res->start = rule->min;
98 res->end = res->start + rule->size - 1;
100 while (!pnp_check_mem(dev, res)) {
101 res->start += rule->align;
102 res->end = res->start + rule->size - 1;
103 if (res->start > rule->max || !rule->align) {
104 pnp_dbg(&dev->dev, " couldn't assign mem %d "
105 "(min %#llx max %#llx)\n", idx,
106 (unsigned long long) rule->min,
107 (unsigned long long) rule->max);
108 return -EBUSY;
112 __add:
113 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
114 return 0;
117 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
119 struct resource *res, local_res;
120 int i;
122 /* IRQ priority: this table is good for i386 */
123 static unsigned short xtab[16] = {
124 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
127 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
128 if (res) {
129 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
130 idx, (int) res->start, res->flags);
131 return 0;
134 res = &local_res;
135 res->flags = rule->flags | IORESOURCE_AUTO;
136 res->start = -1;
137 res->end = -1;
139 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
140 res->flags |= IORESOURCE_DISABLED;
141 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
142 goto __add;
145 /* TBD: need check for >16 IRQ */
146 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
147 if (res->start < PNP_IRQ_NR) {
148 res->end = res->start;
149 goto __add;
151 for (i = 0; i < 16; i++) {
152 if (test_bit(xtab[i], rule->map.bits)) {
153 res->start = res->end = xtab[i];
154 if (pnp_check_irq(dev, res))
155 goto __add;
159 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
160 res->start = -1;
161 res->end = -1;
162 res->flags |= IORESOURCE_DISABLED;
163 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
164 goto __add;
167 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
168 return -EBUSY;
170 __add:
171 pnp_add_irq_resource(dev, res->start, res->flags);
172 return 0;
175 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
177 struct resource *res, local_res;
178 int i;
180 /* DMA priority: this table is good for i386 */
181 static unsigned short xtab[8] = {
182 1, 3, 5, 6, 7, 0, 2, 4
185 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
186 if (res) {
187 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
188 idx, (int) res->start, res->flags);
189 return 0;
192 res = &local_res;
193 res->flags = rule->flags | IORESOURCE_AUTO;
194 res->start = -1;
195 res->end = -1;
197 for (i = 0; i < 8; i++) {
198 if (rule->map & (1 << xtab[i])) {
199 res->start = res->end = xtab[i];
200 if (pnp_check_dma(dev, res))
201 goto __add;
204 #ifdef MAX_DMA_CHANNELS
205 res->start = res->end = MAX_DMA_CHANNELS;
206 #endif
207 res->flags |= IORESOURCE_DISABLED;
208 pnp_dbg(&dev->dev, " disable dma %d\n", idx);
210 __add:
211 pnp_add_dma_resource(dev, res->start, res->flags);
212 return 0;
215 void pnp_init_resources(struct pnp_dev *dev)
217 pnp_free_resources(dev);
220 static void pnp_clean_resource_table(struct pnp_dev *dev)
222 struct pnp_resource *pnp_res, *tmp;
224 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
225 if (pnp_res->res.flags & IORESOURCE_AUTO)
226 pnp_free_resource(pnp_res);
231 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
232 * @dev: pointer to the desired device
233 * @set: the dependent function number
235 static int pnp_assign_resources(struct pnp_dev *dev, int set)
237 struct pnp_option *option;
238 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
239 int ret = 0;
241 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
242 mutex_lock(&pnp_res_mutex);
243 pnp_clean_resource_table(dev);
245 list_for_each_entry(option, &dev->options, list) {
246 if (pnp_option_is_dependent(option) &&
247 pnp_option_set(option) != set)
248 continue;
250 switch (option->type) {
251 case IORESOURCE_IO:
252 ret = pnp_assign_port(dev, &option->u.port, nport++);
253 break;
254 case IORESOURCE_MEM:
255 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
256 break;
257 case IORESOURCE_IRQ:
258 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
259 break;
260 case IORESOURCE_DMA:
261 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
262 break;
263 default:
264 ret = -EINVAL;
265 break;
267 if (ret < 0)
268 break;
271 mutex_unlock(&pnp_res_mutex);
272 if (ret < 0) {
273 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
274 pnp_clean_resource_table(dev);
275 } else
276 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
277 return ret;
281 * pnp_auto_config_dev - automatically assigns resources to a device
282 * @dev: pointer to the desired device
284 int pnp_auto_config_dev(struct pnp_dev *dev)
286 int i, ret;
288 if (!pnp_can_configure(dev)) {
289 pnp_dbg(&dev->dev, "configuration not supported\n");
290 return -ENODEV;
293 ret = pnp_assign_resources(dev, 0);
294 if (ret == 0)
295 return 0;
297 for (i = 1; i < dev->num_dependent_sets; i++) {
298 ret = pnp_assign_resources(dev, i);
299 if (ret == 0)
300 return 0;
303 dev_err(&dev->dev, "unable to assign resources\n");
304 return ret;
308 * pnp_start_dev - low-level start of the PnP device
309 * @dev: pointer to the desired device
311 * assumes that resources have already been allocated
313 int pnp_start_dev(struct pnp_dev *dev)
315 if (!pnp_can_write(dev)) {
316 pnp_dbg(&dev->dev, "activation not supported\n");
317 return -EINVAL;
320 dbg_pnp_show_resources(dev, "pnp_start_dev");
321 if (dev->protocol->set(dev) < 0) {
322 dev_err(&dev->dev, "activation failed\n");
323 return -EIO;
326 dev_info(&dev->dev, "activated\n");
327 return 0;
331 * pnp_stop_dev - low-level disable of the PnP device
332 * @dev: pointer to the desired device
334 * does not free resources
336 int pnp_stop_dev(struct pnp_dev *dev)
338 if (!pnp_can_disable(dev)) {
339 pnp_dbg(&dev->dev, "disabling not supported\n");
340 return -EINVAL;
342 if (dev->protocol->disable(dev) < 0) {
343 dev_err(&dev->dev, "disable failed\n");
344 return -EIO;
347 dev_info(&dev->dev, "disabled\n");
348 return 0;
352 * pnp_activate_dev - activates a PnP device for use
353 * @dev: pointer to the desired device
355 * does not validate or set resources so be careful.
357 int pnp_activate_dev(struct pnp_dev *dev)
359 int error;
361 if (dev->active)
362 return 0;
364 /* ensure resources are allocated */
365 if (pnp_auto_config_dev(dev))
366 return -EBUSY;
368 error = pnp_start_dev(dev);
369 if (error)
370 return error;
372 dev->active = 1;
373 return 0;
377 * pnp_disable_dev - disables device
378 * @dev: pointer to the desired device
380 * inform the correct pnp protocol so that resources can be used by other devices
382 int pnp_disable_dev(struct pnp_dev *dev)
384 int error;
386 if (!dev->active)
387 return 0;
389 error = pnp_stop_dev(dev);
390 if (error)
391 return error;
393 dev->active = 0;
395 /* release the resources so that other devices can use them */
396 mutex_lock(&pnp_res_mutex);
397 pnp_clean_resource_table(dev);
398 mutex_unlock(&pnp_res_mutex);
400 return 0;
403 EXPORT_SYMBOL(pnp_start_dev);
404 EXPORT_SYMBOL(pnp_stop_dev);
405 EXPORT_SYMBOL(pnp_activate_dev);
406 EXPORT_SYMBOL(pnp_disable_dev);