ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / pnp / resource.c
1 /*
2  * resource.c - Contains functions for registering and analyzing resource information
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/module.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <asm/io.h>
15 #include <asm/dma.h>
16 #include <asm/irq.h>
17 #include <linux/pci.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20
21 #include <linux/pnp.h>
22 #include "base.h"
23
24 int pnp_skip_pci_scan;                          /* skip PCI resource scanning */
25 int pnp_reserve_irq[16] = { [0 ... 15] = -1 };  /* reserve (don't use) some IRQ */
26 int pnp_reserve_dma[8] = { [0 ... 7] = -1 };    /* reserve (don't use) some DMA */
27 int pnp_reserve_io[16] = { [0 ... 15] = -1 };   /* reserve (don't use) some I/O region */
28 int pnp_reserve_mem[16] = { [0 ... 15] = -1 };  /* reserve (don't use) some memory region */
29
30
31 /*
32  * option registration
33  */
34
35 static struct pnp_option * pnp_build_option(int priority)
36 {
37         struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
38
39         /* check if pnp_alloc ran out of memory */
40         if (!option)
41                 return NULL;
42
43         option->priority = priority & 0xff;
44         /* make sure the priority is valid */
45         if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
46                 option->priority = PNP_RES_PRIORITY_INVALID;
47
48         return option;
49 }
50
51 struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev)
52 {
53         struct pnp_option *option;
54         if (!dev)
55                 return NULL;
56
57         option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
58
59         /* this should never happen but if it does we'll try to continue */
60         if (dev->independent)
61                 pnp_err("independent resource already registered");
62         dev->independent = option;
63         return option;
64 }
65
66 struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority)
67 {
68         struct pnp_option *option;
69         if (!dev)
70                 return NULL;
71
72         option = pnp_build_option(priority);
73
74         if (dev->dependent) {
75                 struct pnp_option *parent = dev->dependent;
76                 while (parent->next)
77                         parent = parent->next;
78                 parent->next = option;
79         } else
80                 dev->dependent = option;
81         return option;
82 }
83
84 int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
85 {
86         struct pnp_irq *ptr;
87         if (!option)
88                 return -EINVAL;
89         if (!data)
90                 return -EINVAL;
91
92         ptr = option->irq;
93         while (ptr && ptr->next)
94                 ptr = ptr->next;
95         if (ptr)
96                 ptr->next = data;
97         else
98                 option->irq = data;
99
100 #ifdef CONFIG_PCI
101         {
102                 int i;
103
104                 for (i=0; i<16; i++)
105                         if (data->map & (1<<i))
106                                 pcibios_penalize_isa_irq(i);
107         }
108 #endif
109         return 0;
110 }
111
112 int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
113 {
114         struct pnp_dma *ptr;
115         if (!option)
116                 return -EINVAL;
117         if (!data)
118                 return -EINVAL;
119
120         ptr = option->dma;
121         while (ptr && ptr->next)
122                 ptr = ptr->next;
123         if (ptr)
124                 ptr->next = data;
125         else
126                 option->dma = data;
127
128         return 0;
129 }
130
131 int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
132 {
133         struct pnp_port *ptr;
134         if (!option)
135                 return -EINVAL;
136         if (!data)
137                 return -EINVAL;
138
139         ptr = option->port;
140         while (ptr && ptr->next)
141                 ptr = ptr->next;
142         if (ptr)
143                 ptr->next = data;
144         else
145                 option->port = data;
146
147         return 0;
148 }
149
150 int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
151 {
152         struct pnp_mem *ptr;
153         if (!option)
154                 return -EINVAL;
155         if (!data)
156                 return -EINVAL;
157
158         ptr = option->mem;
159         while (ptr && ptr->next)
160                 ptr = ptr->next;
161         if (ptr)
162                 ptr->next = data;
163         else
164                 option->mem = data;
165         return 0;
166 }
167
168 static void pnp_free_port(struct pnp_port *port)
169 {
170         struct pnp_port *next;
171
172         while (port) {
173                 next = port->next;
174                 kfree(port);
175                 port = next;
176         }
177 }
178
179 static void pnp_free_irq(struct pnp_irq *irq)
180 {
181         struct pnp_irq *next;
182
183         while (irq) {
184                 next = irq->next;
185                 kfree(irq);
186                 irq = next;
187         }
188 }
189
190 static void pnp_free_dma(struct pnp_dma *dma)
191 {
192         struct pnp_dma *next;
193
194         while (dma) {
195                 next = dma->next;
196                 kfree(dma);
197                 dma = next;
198         }
199 }
200
201 static void pnp_free_mem(struct pnp_mem *mem)
202 {
203         struct pnp_mem *next;
204
205         while (mem) {
206                 next = mem->next;
207                 kfree(mem);
208                 mem = next;
209         }
210 }
211
212 void pnp_free_option(struct pnp_option *option)
213 {
214         struct pnp_option *next;
215
216         while (option) {
217                 next = option->next;
218                 pnp_free_port(option->port);
219                 pnp_free_irq(option->irq);
220                 pnp_free_dma(option->dma);
221                 pnp_free_mem(option->mem);
222                 kfree(option);
223                 option = next;
224         }
225 }
226
227
228 /*
229  * resource validity checking
230  */
231
232 #define length(start, end) (*(end) - *(start) + 1)
233
234 /* Two ranges conflict if one doesn't end before the other starts */
235 #define ranged_conflict(starta, enda, startb, endb) \
236         !((*(enda) < *(startb)) || (*(endb) < *(starta)))
237
238 #define cannot_compare(flags) \
239 ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
240
241 int pnp_check_port(struct pnp_dev * dev, int idx)
242 {
243         int tmp;
244         struct pnp_dev *tdev;
245         unsigned long *port, *end, *tport, *tend;
246         port = &dev->res.port_resource[idx].start;
247         end = &dev->res.port_resource[idx].end;
248
249         /* if the resource doesn't exist, don't complain about it */
250         if (cannot_compare(dev->res.port_resource[idx].flags))
251                 return 1;
252
253         /* check if the resource is already in use, skip if the
254          * device is active because it itself may be in use */
255         if(!dev->active) {
256                 if (__check_region(&ioport_resource, *port, length(port,end)))
257                         return 0;
258         }
259
260         /* check if the resource is reserved */
261         for (tmp = 0; tmp < 8; tmp++) {
262                 int rport = pnp_reserve_io[tmp << 1];
263                 int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
264                 if (ranged_conflict(port,end,&rport,&rend))
265                         return 0;
266         }
267
268         /* check for internal conflicts */
269         for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
270                 if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
271                         tport = &dev->res.port_resource[tmp].start;
272                         tend = &dev->res.port_resource[tmp].end;
273                         if (ranged_conflict(port,end,tport,tend))
274                                 return 0;
275                 }
276         }
277
278         /* check for conflicts with other pnp devices */
279         pnp_for_each_dev(tdev) {
280                 if (tdev == dev)
281                         continue;
282                 for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
283                         if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
284                                 if (cannot_compare(tdev->res.port_resource[tmp].flags))
285                                         continue;
286                                 tport = &tdev->res.port_resource[tmp].start;
287                                 tend = &tdev->res.port_resource[tmp].end;
288                                 if (ranged_conflict(port,end,tport,tend))
289                                         return 0;
290                         }
291                 }
292         }
293
294         return 1;
295 }
296
297 int pnp_check_mem(struct pnp_dev * dev, int idx)
298 {
299         int tmp;
300         struct pnp_dev *tdev;
301         unsigned long *addr, *end, *taddr, *tend;
302         addr = &dev->res.mem_resource[idx].start;
303         end = &dev->res.mem_resource[idx].end;
304
305         /* if the resource doesn't exist, don't complain about it */
306         if (cannot_compare(dev->res.mem_resource[idx].flags))
307                 return 1;
308
309         /* check if the resource is already in use, skip if the
310          * device is active because it itself may be in use */
311         if(!dev->active) {
312                 if (check_mem_region(*addr, length(addr,end)))
313                         return 0;
314         }
315
316         /* check if the resource is reserved */
317         for (tmp = 0; tmp < 8; tmp++) {
318                 int raddr = pnp_reserve_mem[tmp << 1];
319                 int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
320                 if (ranged_conflict(addr,end,&raddr,&rend))
321                         return 0;
322         }
323
324         /* check for internal conflicts */
325         for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
326                 if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
327                         taddr = &dev->res.mem_resource[tmp].start;
328                         tend = &dev->res.mem_resource[tmp].end;
329                         if (ranged_conflict(addr,end,taddr,tend))
330                                 return 0;
331                 }
332         }
333
334         /* check for conflicts with other pnp devices */
335         pnp_for_each_dev(tdev) {
336                 if (tdev == dev)
337                         continue;
338                 for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
339                         if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
340                                 if (cannot_compare(tdev->res.mem_resource[tmp].flags))
341                                         continue;
342                                 taddr = &tdev->res.mem_resource[tmp].start;
343                                 tend = &tdev->res.mem_resource[tmp].end;
344                                 if (ranged_conflict(addr,end,taddr,tend))
345                                         return 0;
346                         }
347                 }
348         }
349
350         return 1;
351 }
352
353 static irqreturn_t pnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
354 {
355         return IRQ_HANDLED;
356 }
357
358 int pnp_check_irq(struct pnp_dev * dev, int idx)
359 {
360         int tmp;
361         struct pnp_dev *tdev;
362         unsigned long * irq = &dev->res.irq_resource[idx].start;
363
364         /* if the resource doesn't exist, don't complain about it */
365         if (cannot_compare(dev->res.irq_resource[idx].flags))
366                 return 1;
367
368         /* check if the resource is valid */
369         if (*irq < 0 || *irq > 15)
370                 return 0;
371
372         /* check if the resource is reserved */
373         for (tmp = 0; tmp < 16; tmp++) {
374                 if (pnp_reserve_irq[tmp] == *irq)
375                         return 0;
376         }
377
378         /* check for internal conflicts */
379         for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
380                 if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
381                         if (dev->res.irq_resource[tmp].start == *irq)
382                                 return 0;
383                 }
384         }
385
386 #ifdef CONFIG_PCI
387         /* check if the resource is being used by a pci device */
388         if (!pnp_skip_pci_scan) {
389                 struct pci_dev * pci = NULL;
390                 while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
391                         if (pci->irq == *irq)
392                                 return 0;
393                 }
394         }
395 #endif
396
397         /* check if the resource is already in use, skip if the
398          * device is active because it itself may be in use */
399         if(!dev->active) {
400                 if (request_irq(*irq, pnp_test_handler, SA_INTERRUPT, "pnp", NULL))
401                         return 0;
402                 free_irq(*irq, NULL);
403         }
404
405         /* check for conflicts with other pnp devices */
406         pnp_for_each_dev(tdev) {
407                 if (tdev == dev)
408                         continue;
409                 for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
410                         if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
411                                 if (cannot_compare(tdev->res.irq_resource[tmp].flags))
412                                         continue;
413                                 if ((tdev->res.irq_resource[tmp].start == *irq))
414                                         return 0;
415                         }
416                 }
417         }
418
419         return 1;
420 }
421
422 int pnp_check_dma(struct pnp_dev * dev, int idx)
423 {
424         int tmp;
425         struct pnp_dev *tdev;
426         unsigned long * dma = &dev->res.dma_resource[idx].start;
427
428         /* if the resource doesn't exist, don't complain about it */
429         if (cannot_compare(dev->res.dma_resource[idx].flags))
430                 return 1;
431
432         /* check if the resource is valid */
433         if (*dma < 0 || *dma == 4 || *dma > 7)
434                 return 0;
435
436         /* check if the resource is reserved */
437         for (tmp = 0; tmp < 8; tmp++) {
438                 if (pnp_reserve_dma[tmp] == *dma)
439                         return 0;
440         }
441
442         /* check for internal conflicts */
443         for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
444                 if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
445                         if (dev->res.dma_resource[tmp].start == *dma)
446                                 return 0;
447                 }
448         }
449
450         /* check if the resource is already in use, skip if the
451          * device is active because it itself may be in use */
452         if(!dev->active) {
453                 if (request_dma(*dma, "pnp"))
454                         return 0;
455                 free_dma(*dma);
456         }
457
458         /* check for conflicts with other pnp devices */
459         pnp_for_each_dev(tdev) {
460                 if (tdev == dev)
461                         continue;
462                 for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
463                         if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
464                                 if (cannot_compare(tdev->res.dma_resource[tmp].flags))
465                                         continue;
466                                 if ((tdev->res.dma_resource[tmp].start == *dma))
467                                         return 0;
468                         }
469                 }
470         }
471
472         return 1;
473 }
474
475
476 EXPORT_SYMBOL(pnp_register_dependent_option);
477 EXPORT_SYMBOL(pnp_register_independent_option);
478 EXPORT_SYMBOL(pnp_register_irq_resource);
479 EXPORT_SYMBOL(pnp_register_dma_resource);
480 EXPORT_SYMBOL(pnp_register_port_resource);
481 EXPORT_SYMBOL(pnp_register_mem_resource);
482
483
484 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
485
486 static int __init pnp_setup_reserve_irq(char *str)
487 {
488         int i;
489
490         for (i = 0; i < 16; i++)
491                 if (get_option(&str,&pnp_reserve_irq[i]) != 2)
492                         break;
493         return 1;
494 }
495
496 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
497
498 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
499
500 static int __init pnp_setup_reserve_dma(char *str)
501 {
502         int i;
503
504         for (i = 0; i < 8; i++)
505                 if (get_option(&str,&pnp_reserve_dma[i]) != 2)
506                         break;
507         return 1;
508 }
509
510 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
511
512 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
513
514 static int __init pnp_setup_reserve_io(char *str)
515 {
516         int i;
517
518         for (i = 0; i < 16; i++)
519                 if (get_option(&str,&pnp_reserve_io[i]) != 2)
520                         break;
521         return 1;
522 }
523
524 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
525
526 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
527
528 static int __init pnp_setup_reserve_mem(char *str)
529 {
530         int i;
531
532         for (i = 0; i < 16; i++)
533                 if (get_option(&str,&pnp_reserve_mem[i]) != 2)
534                         break;
535         return 1;
536 }
537
538 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);