PNP: remove redundant pnp_can_configure() check
[linux-2.6.git] / drivers / pnp / manager.c
1 /*
2  * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
3  *
4  * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5  * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6  */
7
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>
16 #include "base.h"
17
18 DEFINE_MUTEX(pnp_res_mutex);
19
20 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
21 {
22         struct resource *res, local_res;
23
24         res = pnp_get_resource(dev, IORESOURCE_IO, idx);
25         if (res) {
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);
29                 return 0;
30         }
31
32         res = &local_res;
33         res->flags = rule->flags | IORESOURCE_AUTO;
34         res->start = 0;
35         res->end = 0;
36
37         if (!rule->size) {
38                 res->flags |= IORESOURCE_DISABLED;
39                 dev_dbg(&dev->dev, "  io %d disabled\n", idx);
40                 goto __add;
41         }
42
43         res->start = rule->min;
44         res->end = res->start + rule->size - 1;
45
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 "
51                                 "(min %#llx max %#llx)\n", idx,
52                                 (unsigned long long) rule->min,
53                                 (unsigned long long) rule->max);
54                         return -EBUSY;
55                 }
56         }
57
58 __add:
59         pnp_add_io_resource(dev, res->start, res->end, res->flags);
60         return 0;
61 }
62
63 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
64 {
65         struct resource *res, local_res;
66
67         res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
68         if (res) {
69                 dev_dbg(&dev->dev, "  mem %d already set to %#llx-%#llx "
70                         "flags %#lx\n", idx, (unsigned long long) res->start,
71                         (unsigned long long) res->end, res->flags);
72                 return 0;
73         }
74
75         res = &local_res;
76         res->flags = rule->flags | IORESOURCE_AUTO;
77         res->start = 0;
78         res->end = 0;
79
80         if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
81                 res->flags |= IORESOURCE_READONLY;
82         if (rule->flags & IORESOURCE_MEM_CACHEABLE)
83                 res->flags |= IORESOURCE_CACHEABLE;
84         if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
85                 res->flags |= IORESOURCE_RANGELENGTH;
86         if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
87                 res->flags |= IORESOURCE_SHADOWABLE;
88
89         if (!rule->size) {
90                 res->flags |= IORESOURCE_DISABLED;
91                 dev_dbg(&dev->dev, "  mem %d disabled\n", idx);
92                 goto __add;
93         }
94
95         res->start = rule->min;
96         res->end = res->start + rule->size - 1;
97
98         while (!pnp_check_mem(dev, res)) {
99                 res->start += rule->align;
100                 res->end = res->start + rule->size - 1;
101                 if (res->start > rule->max || !rule->align) {
102                         dev_dbg(&dev->dev, "  couldn't assign mem %d "
103                                 "(min %#llx max %#llx)\n", idx,
104                                 (unsigned long long) rule->min,
105                                 (unsigned long long) rule->max);
106                         return -EBUSY;
107                 }
108         }
109
110 __add:
111         pnp_add_mem_resource(dev, res->start, res->end, res->flags);
112         return 0;
113 }
114
115 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
116 {
117         struct resource *res, local_res;
118         int i;
119
120         /* IRQ priority: this table is good for i386 */
121         static unsigned short xtab[16] = {
122                 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
123         };
124
125         res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
126         if (res) {
127                 dev_dbg(&dev->dev, "  irq %d already set to %d flags %#lx\n",
128                         idx, (int) res->start, res->flags);
129                 return 0;
130         }
131
132         res = &local_res;
133         res->flags = rule->flags | IORESOURCE_AUTO;
134         res->start = -1;
135         res->end = -1;
136
137         if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
138                 res->flags |= IORESOURCE_DISABLED;
139                 dev_dbg(&dev->dev, "  irq %d disabled\n", idx);
140                 goto __add;
141         }
142
143         /* TBD: need check for >16 IRQ */
144         res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
145         if (res->start < PNP_IRQ_NR) {
146                 res->end = res->start;
147                 goto __add;
148         }
149         for (i = 0; i < 16; i++) {
150                 if (test_bit(xtab[i], rule->map.bits)) {
151                         res->start = res->end = xtab[i];
152                         if (pnp_check_irq(dev, res))
153                                 goto __add;
154                 }
155         }
156         dev_dbg(&dev->dev, "  couldn't assign irq %d\n", idx);
157         return -EBUSY;
158
159 __add:
160         pnp_add_irq_resource(dev, res->start, res->flags);
161         return 0;
162 }
163
164 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
165 {
166         struct resource *res, local_res;
167         int i;
168
169         /* DMA priority: this table is good for i386 */
170         static unsigned short xtab[8] = {
171                 1, 3, 5, 6, 7, 0, 2, 4
172         };
173
174         res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
175         if (res) {
176                 dev_dbg(&dev->dev, "  dma %d already set to %d flags %#lx\n",
177                         idx, (int) res->start, res->flags);
178                 return 0;
179         }
180
181         res = &local_res;
182         res->flags = rule->flags | IORESOURCE_AUTO;
183         res->start = -1;
184         res->end = -1;
185
186         for (i = 0; i < 8; i++) {
187                 if (rule->map & (1 << xtab[i])) {
188                         res->start = res->end = xtab[i];
189                         if (pnp_check_dma(dev, res))
190                                 goto __add;
191                 }
192         }
193 #ifdef MAX_DMA_CHANNELS
194         res->start = res->end = MAX_DMA_CHANNELS;
195 #endif
196         res->flags |= IORESOURCE_DISABLED;
197         dev_dbg(&dev->dev, "  disable dma %d\n", idx);
198
199 __add:
200         pnp_add_dma_resource(dev, res->start, res->flags);
201         return 0;
202 }
203
204 void pnp_init_resources(struct pnp_dev *dev)
205 {
206         pnp_free_resources(dev);
207 }
208
209 static void pnp_clean_resource_table(struct pnp_dev *dev)
210 {
211         struct pnp_resource *pnp_res, *tmp;
212
213         list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
214                 if (pnp_res->res.flags & IORESOURCE_AUTO)
215                         pnp_free_resource(pnp_res);
216         }
217 }
218
219 /**
220  * pnp_assign_resources - assigns resources to the device based on the specified dependent number
221  * @dev: pointer to the desired device
222  * @depnum: the dependent function number
223  *
224  * Only set depnum to 0 if the device does not have dependent options.
225  */
226 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
227 {
228         struct pnp_port *port;
229         struct pnp_mem *mem;
230         struct pnp_irq *irq;
231         struct pnp_dma *dma;
232         int nport = 0, nmem = 0, nirq = 0, ndma = 0;
233
234         dbg_pnp_show_resources(dev, "before pnp_assign_resources");
235         mutex_lock(&pnp_res_mutex);
236         pnp_clean_resource_table(dev);
237         if (dev->independent) {
238                 dev_dbg(&dev->dev, "assigning independent options\n");
239                 port = dev->independent->port;
240                 mem = dev->independent->mem;
241                 irq = dev->independent->irq;
242                 dma = dev->independent->dma;
243                 while (port) {
244                         if (pnp_assign_port(dev, port, nport) < 0)
245                                 goto fail;
246                         nport++;
247                         port = port->next;
248                 }
249                 while (mem) {
250                         if (pnp_assign_mem(dev, mem, nmem) < 0)
251                                 goto fail;
252                         nmem++;
253                         mem = mem->next;
254                 }
255                 while (irq) {
256                         if (pnp_assign_irq(dev, irq, nirq) < 0)
257                                 goto fail;
258                         nirq++;
259                         irq = irq->next;
260                 }
261                 while (dma) {
262                         if (pnp_assign_dma(dev, dma, ndma) < 0)
263                                 goto fail;
264                         ndma++;
265                         dma = dma->next;
266                 }
267         }
268
269         if (depnum) {
270                 struct pnp_option *dep;
271                 int i;
272
273                 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
274                 for (i = 1, dep = dev->dependent; i < depnum;
275                      i++, dep = dep->next)
276                         if (!dep)
277                                 goto fail;
278                 port = dep->port;
279                 mem = dep->mem;
280                 irq = dep->irq;
281                 dma = dep->dma;
282                 while (port) {
283                         if (pnp_assign_port(dev, port, nport) < 0)
284                                 goto fail;
285                         nport++;
286                         port = port->next;
287                 }
288                 while (mem) {
289                         if (pnp_assign_mem(dev, mem, nmem) < 0)
290                                 goto fail;
291                         nmem++;
292                         mem = mem->next;
293                 }
294                 while (irq) {
295                         if (pnp_assign_irq(dev, irq, nirq) < 0)
296                                 goto fail;
297                         nirq++;
298                         irq = irq->next;
299                 }
300                 while (dma) {
301                         if (pnp_assign_dma(dev, dma, ndma) < 0)
302                                 goto fail;
303                         ndma++;
304                         dma = dma->next;
305                 }
306         } else if (dev->dependent)
307                 goto fail;
308
309         mutex_unlock(&pnp_res_mutex);
310         dbg_pnp_show_resources(dev, "after pnp_assign_resources");
311         return 1;
312
313 fail:
314         pnp_clean_resource_table(dev);
315         mutex_unlock(&pnp_res_mutex);
316         dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
317         return 0;
318 }
319
320 /**
321  * pnp_auto_config_dev - automatically assigns resources to a device
322  * @dev: pointer to the desired device
323  */
324 int pnp_auto_config_dev(struct pnp_dev *dev)
325 {
326         struct pnp_option *dep;
327         int i = 1;
328
329         if (!pnp_can_configure(dev)) {
330                 dev_dbg(&dev->dev, "configuration not supported\n");
331                 return -ENODEV;
332         }
333
334         if (!dev->dependent) {
335                 if (pnp_assign_resources(dev, 0))
336                         return 0;
337         } else {
338                 dep = dev->dependent;
339                 do {
340                         if (pnp_assign_resources(dev, i))
341                                 return 0;
342                         dep = dep->next;
343                         i++;
344                 } while (dep);
345         }
346
347         dev_err(&dev->dev, "unable to assign resources\n");
348         return -EBUSY;
349 }
350
351 /**
352  * pnp_start_dev - low-level start of the PnP device
353  * @dev: pointer to the desired device
354  *
355  * assumes that resources have already been allocated
356  */
357 int pnp_start_dev(struct pnp_dev *dev)
358 {
359         if (!pnp_can_write(dev)) {
360                 dev_dbg(&dev->dev, "activation not supported\n");
361                 return -EINVAL;
362         }
363
364         dbg_pnp_show_resources(dev, "pnp_start_dev");
365         if (dev->protocol->set(dev) < 0) {
366                 dev_err(&dev->dev, "activation failed\n");
367                 return -EIO;
368         }
369
370         dev_info(&dev->dev, "activated\n");
371         return 0;
372 }
373
374 /**
375  * pnp_stop_dev - low-level disable of the PnP device
376  * @dev: pointer to the desired device
377  *
378  * does not free resources
379  */
380 int pnp_stop_dev(struct pnp_dev *dev)
381 {
382         if (!pnp_can_disable(dev)) {
383                 dev_dbg(&dev->dev, "disabling not supported\n");
384                 return -EINVAL;
385         }
386         if (dev->protocol->disable(dev) < 0) {
387                 dev_err(&dev->dev, "disable failed\n");
388                 return -EIO;
389         }
390
391         dev_info(&dev->dev, "disabled\n");
392         return 0;
393 }
394
395 /**
396  * pnp_activate_dev - activates a PnP device for use
397  * @dev: pointer to the desired device
398  *
399  * does not validate or set resources so be careful.
400  */
401 int pnp_activate_dev(struct pnp_dev *dev)
402 {
403         int error;
404
405         if (dev->active)
406                 return 0;
407
408         /* ensure resources are allocated */
409         if (pnp_auto_config_dev(dev))
410                 return -EBUSY;
411
412         error = pnp_start_dev(dev);
413         if (error)
414                 return error;
415
416         dev->active = 1;
417         return 0;
418 }
419
420 /**
421  * pnp_disable_dev - disables device
422  * @dev: pointer to the desired device
423  *
424  * inform the correct pnp protocol so that resources can be used by other devices
425  */
426 int pnp_disable_dev(struct pnp_dev *dev)
427 {
428         int error;
429
430         if (!dev->active)
431                 return 0;
432
433         error = pnp_stop_dev(dev);
434         if (error)
435                 return error;
436
437         dev->active = 0;
438
439         /* release the resources so that other devices can use them */
440         mutex_lock(&pnp_res_mutex);
441         pnp_clean_resource_table(dev);
442         mutex_unlock(&pnp_res_mutex);
443
444         return 0;
445 }
446
447 EXPORT_SYMBOL(pnp_start_dev);
448 EXPORT_SYMBOL(pnp_stop_dev);
449 EXPORT_SYMBOL(pnp_activate_dev);
450 EXPORT_SYMBOL(pnp_disable_dev);