PNP: don't fail device init if no DMA channel available
[safe/jmp/linux-2.6] / 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 "base.h"
16
17 DECLARE_MUTEX(pnp_res_mutex);
18
19 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
20 {
21         resource_size_t *start, *end;
22         unsigned long *flags;
23
24         if (idx >= PNP_MAX_PORT) {
25                 pnp_err
26                     ("More than 4 ports is incompatible with pnp specifications.");
27                 /* pretend we were successful so at least the manager won't try again */
28                 return 1;
29         }
30
31         /* check if this resource has been manually set, if so skip */
32         if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
33                 return 1;
34
35         start = &dev->res.port_resource[idx].start;
36         end = &dev->res.port_resource[idx].end;
37         flags = &dev->res.port_resource[idx].flags;
38
39         /* set the initial values */
40         *flags |= rule->flags | IORESOURCE_IO;
41         *flags &= ~IORESOURCE_UNSET;
42
43         if (!rule->size) {
44                 *flags |= IORESOURCE_DISABLED;
45                 return 1;       /* skip disabled resource requests */
46         }
47
48         *start = rule->min;
49         *end = *start + rule->size - 1;
50
51         /* run through until pnp_check_port is happy */
52         while (!pnp_check_port(dev, idx)) {
53                 *start += rule->align;
54                 *end = *start + rule->size - 1;
55                 if (*start > rule->max || !rule->align)
56                         return 0;
57         }
58         return 1;
59 }
60
61 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
62 {
63         resource_size_t *start, *end;
64         unsigned long *flags;
65
66         if (idx >= PNP_MAX_MEM) {
67                 pnp_err
68                     ("More than 8 mems is incompatible with pnp specifications.");
69                 /* pretend we were successful so at least the manager won't try again */
70                 return 1;
71         }
72
73         /* check if this resource has been manually set, if so skip */
74         if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
75                 return 1;
76
77         start = &dev->res.mem_resource[idx].start;
78         end = &dev->res.mem_resource[idx].end;
79         flags = &dev->res.mem_resource[idx].flags;
80
81         /* set the initial values */
82         *flags |= rule->flags | IORESOURCE_MEM;
83         *flags &= ~IORESOURCE_UNSET;
84
85         /* convert pnp flags to standard Linux flags */
86         if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
87                 *flags |= IORESOURCE_READONLY;
88         if (rule->flags & IORESOURCE_MEM_CACHEABLE)
89                 *flags |= IORESOURCE_CACHEABLE;
90         if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
91                 *flags |= IORESOURCE_RANGELENGTH;
92         if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
93                 *flags |= IORESOURCE_SHADOWABLE;
94
95         if (!rule->size) {
96                 *flags |= IORESOURCE_DISABLED;
97                 return 1;       /* skip disabled resource requests */
98         }
99
100         *start = rule->min;
101         *end = *start + rule->size - 1;
102
103         /* run through until pnp_check_mem is happy */
104         while (!pnp_check_mem(dev, idx)) {
105                 *start += rule->align;
106                 *end = *start + rule->size - 1;
107                 if (*start > rule->max || !rule->align)
108                         return 0;
109         }
110         return 1;
111 }
112
113 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
114 {
115         resource_size_t *start, *end;
116         unsigned long *flags;
117         int i;
118
119         /* IRQ priority: this table is good for i386 */
120         static unsigned short xtab[16] = {
121                 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
122         };
123
124         if (idx >= PNP_MAX_IRQ) {
125                 pnp_err
126                     ("More than 2 irqs is incompatible with pnp specifications.");
127                 /* pretend we were successful so at least the manager won't try again */
128                 return 1;
129         }
130
131         /* check if this resource has been manually set, if so skip */
132         if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
133                 return 1;
134
135         start = &dev->res.irq_resource[idx].start;
136         end = &dev->res.irq_resource[idx].end;
137         flags = &dev->res.irq_resource[idx].flags;
138
139         /* set the initial values */
140         *flags |= rule->flags | IORESOURCE_IRQ;
141         *flags &= ~IORESOURCE_UNSET;
142
143         if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
144                 *flags |= IORESOURCE_DISABLED;
145                 return 1;       /* skip disabled resource requests */
146         }
147
148         /* TBD: need check for >16 IRQ */
149         *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
150         if (*start < PNP_IRQ_NR) {
151                 *end = *start;
152                 return 1;
153         }
154         for (i = 0; i < 16; i++) {
155                 if (test_bit(xtab[i], rule->map)) {
156                         *start = *end = xtab[i];
157                         if (pnp_check_irq(dev, idx))
158                                 return 1;
159                 }
160         }
161         return 0;
162 }
163
164 static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
165 {
166         resource_size_t *start, *end;
167         unsigned long *flags;
168         int i;
169
170         /* DMA priority: this table is good for i386 */
171         static unsigned short xtab[8] = {
172                 1, 3, 5, 6, 7, 0, 2, 4
173         };
174
175         if (idx >= PNP_MAX_DMA) {
176                 pnp_err("More than 2 dmas is incompatible with pnp "
177                         "specifications.");
178                 return;
179         }
180
181         /* check if this resource has been manually set, if so skip */
182         if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
183                 return;
184
185         start = &dev->res.dma_resource[idx].start;
186         end = &dev->res.dma_resource[idx].end;
187         flags = &dev->res.dma_resource[idx].flags;
188
189         /* set the initial values */
190         *flags |= rule->flags | IORESOURCE_DMA;
191         *flags &= ~IORESOURCE_UNSET;
192
193         for (i = 0; i < 8; i++) {
194                 if (rule->map & (1 << xtab[i])) {
195                         *start = *end = xtab[i];
196                         if (pnp_check_dma(dev, idx))
197                                 return;
198                 }
199         }
200 #ifdef MAX_DMA_CHANNELS
201         *start = *end = MAX_DMA_CHANNELS;
202 #endif
203         *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
204 }
205
206 /**
207  * pnp_init_resources - Resets a resource table to default values.
208  * @table: pointer to the desired resource table
209  */
210 void pnp_init_resource_table(struct pnp_resource_table *table)
211 {
212         int idx;
213
214         for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
215                 table->irq_resource[idx].name = NULL;
216                 table->irq_resource[idx].start = -1;
217                 table->irq_resource[idx].end = -1;
218                 table->irq_resource[idx].flags =
219                     IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
220         }
221         for (idx = 0; idx < PNP_MAX_DMA; idx++) {
222                 table->dma_resource[idx].name = NULL;
223                 table->dma_resource[idx].start = -1;
224                 table->dma_resource[idx].end = -1;
225                 table->dma_resource[idx].flags =
226                     IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
227         }
228         for (idx = 0; idx < PNP_MAX_PORT; idx++) {
229                 table->port_resource[idx].name = NULL;
230                 table->port_resource[idx].start = 0;
231                 table->port_resource[idx].end = 0;
232                 table->port_resource[idx].flags =
233                     IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
234         }
235         for (idx = 0; idx < PNP_MAX_MEM; idx++) {
236                 table->mem_resource[idx].name = NULL;
237                 table->mem_resource[idx].start = 0;
238                 table->mem_resource[idx].end = 0;
239                 table->mem_resource[idx].flags =
240                     IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
241         }
242 }
243
244 /**
245  * pnp_clean_resources - clears resources that were not manually set
246  * @res: the resources to clean
247  */
248 static void pnp_clean_resource_table(struct pnp_resource_table *res)
249 {
250         int idx;
251
252         for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
253                 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
254                         continue;
255                 res->irq_resource[idx].start = -1;
256                 res->irq_resource[idx].end = -1;
257                 res->irq_resource[idx].flags =
258                     IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
259         }
260         for (idx = 0; idx < PNP_MAX_DMA; idx++) {
261                 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
262                         continue;
263                 res->dma_resource[idx].start = -1;
264                 res->dma_resource[idx].end = -1;
265                 res->dma_resource[idx].flags =
266                     IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
267         }
268         for (idx = 0; idx < PNP_MAX_PORT; idx++) {
269                 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
270                         continue;
271                 res->port_resource[idx].start = 0;
272                 res->port_resource[idx].end = 0;
273                 res->port_resource[idx].flags =
274                     IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
275         }
276         for (idx = 0; idx < PNP_MAX_MEM; idx++) {
277                 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
278                         continue;
279                 res->mem_resource[idx].start = 0;
280                 res->mem_resource[idx].end = 0;
281                 res->mem_resource[idx].flags =
282                     IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
283         }
284 }
285
286 /**
287  * pnp_assign_resources - assigns resources to the device based on the specified dependent number
288  * @dev: pointer to the desired device
289  * @depnum: the dependent function number
290  *
291  * Only set depnum to 0 if the device does not have dependent options.
292  */
293 static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
294 {
295         struct pnp_port *port;
296         struct pnp_mem *mem;
297         struct pnp_irq *irq;
298         struct pnp_dma *dma;
299         int nport = 0, nmem = 0, nirq = 0, ndma = 0;
300
301         if (!pnp_can_configure(dev))
302                 return -ENODEV;
303
304         down(&pnp_res_mutex);
305         pnp_clean_resource_table(&dev->res);    /* start with a fresh slate */
306         if (dev->independent) {
307                 port = dev->independent->port;
308                 mem = dev->independent->mem;
309                 irq = dev->independent->irq;
310                 dma = dev->independent->dma;
311                 while (port) {
312                         if (!pnp_assign_port(dev, port, nport))
313                                 goto fail;
314                         nport++;
315                         port = port->next;
316                 }
317                 while (mem) {
318                         if (!pnp_assign_mem(dev, mem, nmem))
319                                 goto fail;
320                         nmem++;
321                         mem = mem->next;
322                 }
323                 while (irq) {
324                         if (!pnp_assign_irq(dev, irq, nirq))
325                                 goto fail;
326                         nirq++;
327                         irq = irq->next;
328                 }
329                 while (dma) {
330                         pnp_assign_dma(dev, dma, ndma);
331                         ndma++;
332                         dma = dma->next;
333                 }
334         }
335
336         if (depnum) {
337                 struct pnp_option *dep;
338                 int i;
339                 for (i = 1, dep = dev->dependent; i < depnum;
340                      i++, dep = dep->next)
341                         if (!dep)
342                                 goto fail;
343                 port = dep->port;
344                 mem = dep->mem;
345                 irq = dep->irq;
346                 dma = dep->dma;
347                 while (port) {
348                         if (!pnp_assign_port(dev, port, nport))
349                                 goto fail;
350                         nport++;
351                         port = port->next;
352                 }
353                 while (mem) {
354                         if (!pnp_assign_mem(dev, mem, nmem))
355                                 goto fail;
356                         nmem++;
357                         mem = mem->next;
358                 }
359                 while (irq) {
360                         if (!pnp_assign_irq(dev, irq, nirq))
361                                 goto fail;
362                         nirq++;
363                         irq = irq->next;
364                 }
365                 while (dma) {
366                         pnp_assign_dma(dev, dma, ndma);
367                         ndma++;
368                         dma = dma->next;
369                 }
370         } else if (dev->dependent)
371                 goto fail;
372
373         up(&pnp_res_mutex);
374         return 1;
375
376 fail:
377         pnp_clean_resource_table(&dev->res);
378         up(&pnp_res_mutex);
379         return 0;
380 }
381
382 /**
383  * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
384  * @dev: pointer to the desired device
385  * @res: pointer to the new resource config
386  * @mode: 0 or PNP_CONFIG_FORCE
387  *
388  * This function can be used by drivers that want to manually set thier resources.
389  */
390 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
391                           int mode)
392 {
393         int i;
394         struct pnp_resource_table *bak;
395
396         if (!pnp_can_configure(dev))
397                 return -ENODEV;
398         bak = pnp_alloc(sizeof(struct pnp_resource_table));
399         if (!bak)
400                 return -ENOMEM;
401         *bak = dev->res;
402
403         down(&pnp_res_mutex);
404         dev->res = *res;
405         if (!(mode & PNP_CONFIG_FORCE)) {
406                 for (i = 0; i < PNP_MAX_PORT; i++) {
407                         if (!pnp_check_port(dev, i))
408                                 goto fail;
409                 }
410                 for (i = 0; i < PNP_MAX_MEM; i++) {
411                         if (!pnp_check_mem(dev, i))
412                                 goto fail;
413                 }
414                 for (i = 0; i < PNP_MAX_IRQ; i++) {
415                         if (!pnp_check_irq(dev, i))
416                                 goto fail;
417                 }
418                 for (i = 0; i < PNP_MAX_DMA; i++) {
419                         if (!pnp_check_dma(dev, i))
420                                 goto fail;
421                 }
422         }
423         up(&pnp_res_mutex);
424
425         kfree(bak);
426         return 0;
427
428 fail:
429         dev->res = *bak;
430         up(&pnp_res_mutex);
431         kfree(bak);
432         return -EINVAL;
433 }
434
435 /**
436  * pnp_auto_config_dev - automatically assigns resources to a device
437  * @dev: pointer to the desired device
438  */
439 int pnp_auto_config_dev(struct pnp_dev *dev)
440 {
441         struct pnp_option *dep;
442         int i = 1;
443
444         if (!pnp_can_configure(dev)) {
445                 pnp_dbg("Device %s does not support resource configuration.",
446                         dev->dev.bus_id);
447                 return -ENODEV;
448         }
449
450         if (!dev->dependent) {
451                 if (pnp_assign_resources(dev, 0))
452                         return 0;
453         } else {
454                 dep = dev->dependent;
455                 do {
456                         if (pnp_assign_resources(dev, i))
457                                 return 0;
458                         dep = dep->next;
459                         i++;
460                 } while (dep);
461         }
462
463         pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
464         return -EBUSY;
465 }
466
467 /**
468  * pnp_start_dev - low-level start of the PnP device
469  * @dev: pointer to the desired device
470  *
471  * assumes that resources have already been allocated
472  */
473 int pnp_start_dev(struct pnp_dev *dev)
474 {
475         if (!pnp_can_write(dev)) {
476                 pnp_dbg("Device %s does not support activation.",
477                         dev->dev.bus_id);
478                 return -EINVAL;
479         }
480
481         if (dev->protocol->set(dev, &dev->res) < 0) {
482                 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
483                 return -EIO;
484         }
485
486         pnp_info("Device %s activated.", dev->dev.bus_id);
487         return 0;
488 }
489
490 /**
491  * pnp_stop_dev - low-level disable of the PnP device
492  * @dev: pointer to the desired device
493  *
494  * does not free resources
495  */
496 int pnp_stop_dev(struct pnp_dev *dev)
497 {
498         if (!pnp_can_disable(dev)) {
499                 pnp_dbg("Device %s does not support disabling.",
500                         dev->dev.bus_id);
501                 return -EINVAL;
502         }
503         if (dev->protocol->disable(dev) < 0) {
504                 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
505                 return -EIO;
506         }
507
508         pnp_info("Device %s disabled.", dev->dev.bus_id);
509         return 0;
510 }
511
512 /**
513  * pnp_activate_dev - activates a PnP device for use
514  * @dev: pointer to the desired device
515  *
516  * does not validate or set resources so be careful.
517  */
518 int pnp_activate_dev(struct pnp_dev *dev)
519 {
520         int error;
521
522         if (dev->active)
523                 return 0;       /* the device is already active */
524
525         /* ensure resources are allocated */
526         if (pnp_auto_config_dev(dev))
527                 return -EBUSY;
528
529         error = pnp_start_dev(dev);
530         if (error)
531                 return error;
532
533         dev->active = 1;
534         return 1;
535 }
536
537 /**
538  * pnp_disable_dev - disables device
539  * @dev: pointer to the desired device
540  *
541  * inform the correct pnp protocol so that resources can be used by other devices
542  */
543 int pnp_disable_dev(struct pnp_dev *dev)
544 {
545         int error;
546
547         if (!dev->active)
548                 return 0;       /* the device is already disabled */
549
550         error = pnp_stop_dev(dev);
551         if (error)
552                 return error;
553
554         dev->active = 0;
555
556         /* release the resources so that other devices can use them */
557         down(&pnp_res_mutex);
558         pnp_clean_resource_table(&dev->res);
559         up(&pnp_res_mutex);
560
561         return 1;
562 }
563
564 /**
565  * pnp_resource_change - change one resource
566  * @resource: pointer to resource to be changed
567  * @start: start of region
568  * @size: size of region
569  */
570 void pnp_resource_change(struct resource *resource, resource_size_t start,
571                          resource_size_t size)
572 {
573         resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
574         resource->start = start;
575         resource->end = start + size - 1;
576 }
577
578 EXPORT_SYMBOL(pnp_manual_config_dev);
579 EXPORT_SYMBOL(pnp_start_dev);
580 EXPORT_SYMBOL(pnp_stop_dev);
581 EXPORT_SYMBOL(pnp_activate_dev);
582 EXPORT_SYMBOL(pnp_disable_dev);
583 EXPORT_SYMBOL(pnp_resource_change);
584 EXPORT_SYMBOL(pnp_init_resource_table);