ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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@suse.cz>
5  * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6  *
7  */
8
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>
14
15 #ifdef CONFIG_PNP_DEBUG
16         #define DEBUG
17 #else
18         #undef DEBUG
19 #endif
20
21 #include <linux/pnp.h>
22 #include "base.h"
23
24 DECLARE_MUTEX(pnp_res_mutex);
25
26 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
27 {
28         unsigned long *start, *end, *flags;
29
30         if (!dev || !rule)
31                 return -EINVAL;
32
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 */
36                 return 1;
37         }
38
39         /* check if this resource has been manually set, if so skip */
40         if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
41                 return 1;
42
43         start = &dev->res.port_resource[idx].start;
44         end = &dev->res.port_resource[idx].end;
45         flags = &dev->res.port_resource[idx].flags;
46
47         /* set the initial values */
48         *flags |= rule->flags | IORESOURCE_IO;
49         *flags &=  ~IORESOURCE_UNSET;
50
51         if (!rule->size) {
52                 *flags |= IORESOURCE_DISABLED;
53                 return 1; /* skip disabled resource requests */
54         }
55
56         *start = rule->min;
57         *end = *start + rule->size - 1;
58
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)
64                         return 0;
65         }
66         return 1;
67 }
68
69 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
70 {
71         unsigned long *start, *end, *flags;
72
73         if (!dev || !rule)
74                 return -EINVAL;
75
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 */
79                 return 1;
80         }
81
82         /* check if this resource has been manually set, if so skip */
83         if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
84                 return 1;
85
86         start = &dev->res.mem_resource[idx].start;
87         end = &dev->res.mem_resource[idx].end;
88         flags = &dev->res.mem_resource[idx].flags;
89
90         /* set the initial values */
91         *flags |= rule->flags | IORESOURCE_MEM;
92         *flags &=  ~IORESOURCE_UNSET;
93
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;
103
104         if (!rule->size) {
105                 *flags |= IORESOURCE_DISABLED;
106                 return 1; /* skip disabled resource requests */
107         }
108
109         *start = rule->min;
110         *end = *start + rule->size -1;
111
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)
117                         return 0;
118         }
119         return 1;
120 }
121
122 static int pnp_assign_irq(struct pnp_dev * dev, struct pnp_irq *rule, int idx)
123 {
124         unsigned long *start, *end, *flags;
125         int i;
126
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
130         };
131
132         if (!dev || !rule)
133                 return -EINVAL;
134
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 */
138                 return 1;
139         }
140
141         /* check if this resource has been manually set, if so skip */
142         if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
143                 return 1;
144
145         start = &dev->res.irq_resource[idx].start;
146         end = &dev->res.irq_resource[idx].end;
147         flags = &dev->res.irq_resource[idx].flags;
148
149         /* set the initial values */
150         *flags |= rule->flags | IORESOURCE_IRQ;
151         *flags &=  ~IORESOURCE_UNSET;
152
153         if (!rule->map) {
154                 *flags |= IORESOURCE_DISABLED;
155                 return 1; /* skip disabled resource requests */
156         }
157
158         for (i = 0; i < 16; i++) {
159                 if(rule->map & (1<<xtab[i])) {
160                         *start = *end = xtab[i];
161                         if(pnp_check_irq(dev, idx))
162                                 return 1;
163                 }
164         }
165         return 0;
166 }
167
168 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
169 {
170         unsigned long *start, *end, *flags;
171         int i;
172
173         /* DMA priority: this table is good for i386 */
174         static unsigned short xtab[8] = {
175                 1, 3, 5, 6, 7, 0, 2, 4
176         };
177
178         if (!dev || !rule)
179                 return -EINVAL;
180
181         if (idx >= PNP_MAX_DMA) {
182                 pnp_err("More than 2 dmas is incompatible with pnp specifications.");
183                 /* pretend we were successful so at least the manager won't try again */
184                 return 1;
185         }
186
187         /* check if this resource has been manually set, if so skip */
188         if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
189                 return 1;
190
191         start = &dev->res.dma_resource[idx].start;
192         end = &dev->res.dma_resource[idx].end;
193         flags = &dev->res.dma_resource[idx].flags;
194
195         /* set the initial values */
196         *flags |= rule->flags | IORESOURCE_DMA;
197         *flags &=  ~IORESOURCE_UNSET;
198
199         if (!rule->map) {
200                 *flags |= IORESOURCE_DISABLED;
201                 return 1; /* skip disabled resource requests */
202         }
203
204         for (i = 0; i < 8; i++) {
205                 if(rule->map & (1<<xtab[i])) {
206                         *start = *end = xtab[i];
207                         if(pnp_check_dma(dev, idx))
208                                 return 1;
209                 }
210         }
211         return 0;
212 }
213
214 /**
215  * pnp_init_resources - Resets a resource table to default values.
216  * @table: pointer to the desired resource table
217  *
218  */
219 void pnp_init_resource_table(struct pnp_resource_table *table)
220 {
221         int idx;
222         for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
223                 table->irq_resource[idx].name = NULL;
224                 table->irq_resource[idx].start = -1;
225                 table->irq_resource[idx].end = -1;
226                 table->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
227         }
228         for (idx = 0; idx < PNP_MAX_DMA; idx++) {
229                 table->dma_resource[idx].name = NULL;
230                 table->dma_resource[idx].start = -1;
231                 table->dma_resource[idx].end = -1;
232                 table->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
233         }
234         for (idx = 0; idx < PNP_MAX_PORT; idx++) {
235                 table->port_resource[idx].name = NULL;
236                 table->port_resource[idx].start = 0;
237                 table->port_resource[idx].end = 0;
238                 table->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
239         }
240         for (idx = 0; idx < PNP_MAX_MEM; idx++) {
241                 table->mem_resource[idx].name = NULL;
242                 table->mem_resource[idx].start = 0;
243                 table->mem_resource[idx].end = 0;
244                 table->mem_resource[idx].flags = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
245         }
246 }
247
248 /**
249  * pnp_clean_resources - clears resources that were not manually set
250  * @res - the resources to clean
251  *
252  */
253 static void pnp_clean_resource_table(struct pnp_resource_table * res)
254 {
255         int idx;
256         for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
257                 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
258                         continue;
259                 res->irq_resource[idx].start = -1;
260                 res->irq_resource[idx].end = -1;
261                 res->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
262         }
263         for (idx = 0; idx < PNP_MAX_DMA; idx++) {
264                 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
265                         continue;
266                 res->dma_resource[idx].start = -1;
267                 res->dma_resource[idx].end = -1;
268                 res->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
269         }
270         for (idx = 0; idx < PNP_MAX_PORT; idx++) {
271                 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
272                         continue;
273                 res->port_resource[idx].start = 0;
274                 res->port_resource[idx].end = 0;
275                 res->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
276         }
277         for (idx = 0; idx < PNP_MAX_MEM; idx++) {
278                 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
279                         continue;
280                 res->mem_resource[idx].start = 0;
281                 res->mem_resource[idx].end = 0;
282                 res->mem_resource[idx].flags = 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 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                         if (!pnp_assign_dma(dev, dma, ndma))
331                                 goto fail;
332                         ndma++;
333                         dma = dma->next;
334                 }
335         }
336
337         if (depnum) {
338                 struct pnp_option *dep;
339                 int i;
340                 for (i=1,dep=dev->dependent; i<depnum; 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                         if (!pnp_assign_dma(dev, dma, ndma))
367                                 goto fail;
368                         ndma++;
369                         dma = dma->next;
370                 }
371         } else if (dev->dependent)
372                 goto fail;
373
374         up(&pnp_res_mutex);
375         return 1;
376
377 fail:
378         pnp_clean_resource_table(&dev->res);
379         up(&pnp_res_mutex);
380         return 0;
381 }
382
383 /**
384  * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
385  * @dev: pointer to the desired device
386  * @res: pointer to the new resource config
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, int mode)
391 {
392         int i;
393         struct pnp_resource_table * bak;
394         if (!dev || !res)
395                 return -EINVAL;
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  */
440 int pnp_auto_config_dev(struct pnp_dev *dev)
441 {
442         struct pnp_option *dep;
443         int i = 1;
444
445         if(!dev)
446                 return -EINVAL;
447
448         if(!pnp_can_configure(dev)) {
449                 pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
450                 return -ENODEV;
451         }
452
453         if (!dev->dependent) {
454                 if (pnp_assign_resources(dev, 0))
455                         return 0;
456         } else {
457                 dep = dev->dependent;
458                 do {
459                         if (pnp_assign_resources(dev, i))
460                                 return 0;
461                         dep = dep->next;
462                         i++;
463                 } while (dep);
464         }
465
466         pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
467         return -EBUSY;
468 }
469
470 /**
471  * pnp_activate_dev - activates a PnP device for use
472  * @dev: pointer to the desired device
473  *
474  * does not validate or set resources so be careful.
475  */
476 int pnp_activate_dev(struct pnp_dev *dev)
477 {
478         if (!dev)
479                 return -EINVAL;
480         if (dev->active) {
481                 return 0; /* the device is already active */
482         }
483
484         /* ensure resources are allocated */
485         if (pnp_auto_config_dev(dev))
486                 return -EBUSY;
487
488         if (!pnp_can_write(dev)) {
489                 pnp_info("Device %s does not supported activation.", dev->dev.bus_id);
490                 return -EINVAL;
491         }
492
493         if (dev->protocol->set(dev, &dev->res)<0) {
494                 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
495                 return -EIO;
496         }
497
498         dev->active = 1;
499         pnp_info("Device %s activated.", dev->dev.bus_id);
500
501         return 1;
502 }
503
504 /**
505  * pnp_disable_dev - disables device
506  * @dev: pointer to the desired device
507  *
508  * inform the correct pnp protocol so that resources can be used by other devices
509  */
510 int pnp_disable_dev(struct pnp_dev *dev)
511 {
512         if (!dev)
513                 return -EINVAL;
514         if (!dev->active) {
515                 return 0; /* the device is already disabled */
516         }
517
518         if (!pnp_can_disable(dev)) {
519                 pnp_info("Device %s does not supported disabling.", dev->dev.bus_id);
520                 return -EINVAL;
521         }
522         if (dev->protocol->disable(dev)<0) {
523                 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
524                 return -EIO;
525         }
526
527         dev->active = 0;
528         pnp_info("Device %s disabled.", dev->dev.bus_id);
529
530         /* release the resources so that other devices can use them */
531         down(&pnp_res_mutex);
532         pnp_clean_resource_table(&dev->res);
533         up(&pnp_res_mutex);
534
535         return 1;
536 }
537
538 /**
539  * pnp_resource_change - change one resource
540  * @resource: pointer to resource to be changed
541  * @start: start of region
542  * @size: size of region
543  *
544  */
545 void pnp_resource_change(struct resource *resource, unsigned long start, unsigned long size)
546 {
547         if (resource == NULL)
548                 return;
549         resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
550         resource->start = start;
551         resource->end = start + size - 1;
552 }
553
554
555 EXPORT_SYMBOL(pnp_assign_resources);
556 EXPORT_SYMBOL(pnp_manual_config_dev);
557 EXPORT_SYMBOL(pnp_auto_config_dev);
558 EXPORT_SYMBOL(pnp_activate_dev);
559 EXPORT_SYMBOL(pnp_disable_dev);
560 EXPORT_SYMBOL(pnp_resource_change);
561 EXPORT_SYMBOL(pnp_init_resource_table);