61145491f363cd8b1d2b8c5b6962337816761c13
[safe/jmp/linux-2.6] / 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@perex.cz>
5  * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6  */
7
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <asm/io.h>
13 #include <asm/dma.h>
14 #include <asm/irq.h>
15 #include <linux/pci.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18
19 #include <linux/pnp.h>
20 #include "base.h"
21
22 static int pnp_reserve_irq[16] = {[0 ... 15] = -1 };    /* reserve (don't use) some IRQ */
23 static int pnp_reserve_dma[8] = {[0 ... 7] = -1 };      /* reserve (don't use) some DMA */
24 static int pnp_reserve_io[16] = {[0 ... 15] = -1 };     /* reserve (don't use) some I/O region */
25 static int pnp_reserve_mem[16] = {[0 ... 15] = -1 };    /* reserve (don't use) some memory region */
26
27 /*
28  * option registration
29  */
30
31 struct pnp_option *pnp_build_option(int priority)
32 {
33         struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
34
35         if (!option)
36                 return NULL;
37
38         option->priority = priority & 0xff;
39         /* make sure the priority is valid */
40         if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
41                 option->priority = PNP_RES_PRIORITY_INVALID;
42
43         return option;
44 }
45
46 struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
47 {
48         struct pnp_option *option;
49
50         option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
51
52         /* this should never happen but if it does we'll try to continue */
53         if (dev->independent)
54                 dev_err(&dev->dev, "independent resource already registered\n");
55         dev->independent = option;
56
57         dev_dbg(&dev->dev, "new independent option\n");
58         return option;
59 }
60
61 struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
62                                                  int priority)
63 {
64         struct pnp_option *option;
65
66         option = pnp_build_option(priority);
67
68         if (dev->dependent) {
69                 struct pnp_option *parent = dev->dependent;
70                 while (parent->next)
71                         parent = parent->next;
72                 parent->next = option;
73         } else
74                 dev->dependent = option;
75
76         dev_dbg(&dev->dev, "new dependent option (priority %#x)\n", priority);
77         return option;
78 }
79
80 int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option,
81                               pnp_irq_mask_t *map, unsigned char flags)
82 {
83         struct pnp_irq *data, *ptr;
84 #ifdef DEBUG
85         char buf[PNP_IRQ_NR];   /* hex-encoded, so this is overkill but safe */
86 #endif
87
88         data = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
89         if (!data)
90                 return -ENOMEM;
91
92         data->map = *map;
93         data->flags = flags;
94
95         ptr = option->irq;
96         while (ptr && ptr->next)
97                 ptr = ptr->next;
98         if (ptr)
99                 ptr->next = data;
100         else
101                 option->irq = data;
102
103 #ifdef CONFIG_PCI
104         {
105                 int i;
106
107                 for (i = 0; i < 16; i++)
108                         if (test_bit(i, data->map.bits))
109                                 pcibios_penalize_isa_irq(i, 0);
110         }
111 #endif
112
113 #ifdef DEBUG
114         bitmap_scnprintf(buf, sizeof(buf), data->map.bits, PNP_IRQ_NR);
115         dev_dbg(&dev->dev, "  irq bitmask %s flags %#x\n", buf,
116                 data->flags);
117 #endif
118         return 0;
119 }
120
121 int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option,
122                               unsigned char map, unsigned char flags)
123 {
124         struct pnp_dma *data, *ptr;
125
126         data = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
127         if (!data)
128                 return -ENOMEM;
129
130         data->map = map;
131         data->flags = flags;
132
133         ptr = option->dma;
134         while (ptr && ptr->next)
135                 ptr = ptr->next;
136         if (ptr)
137                 ptr->next = data;
138         else
139                 option->dma = data;
140
141         dev_dbg(&dev->dev, "  dma bitmask %#x flags %#x\n", data->map,
142                 data->flags);
143         return 0;
144 }
145
146 int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option,
147                                resource_size_t min, resource_size_t max,
148                                resource_size_t align, resource_size_t size,
149                                unsigned char flags)
150 {
151         struct pnp_port *data, *ptr;
152
153         data = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
154         if (!data)
155                 return -ENOMEM;
156
157         data->min = min;
158         data->max = max;
159         data->align = align;
160         data->size = size;
161         data->flags = flags;
162
163         ptr = option->port;
164         while (ptr && ptr->next)
165                 ptr = ptr->next;
166         if (ptr)
167                 ptr->next = data;
168         else
169                 option->port = data;
170
171         dev_dbg(&dev->dev, "  io  "
172                 "min %#llx max %#llx align %lld size %lld flags %#x\n",
173                 (unsigned long long) data->min,
174                 (unsigned long long) data->max,
175                 (unsigned long long) data->align,
176                 (unsigned long long) data->size, data->flags);
177         return 0;
178 }
179
180 int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
181                               resource_size_t min, resource_size_t max,
182                               resource_size_t align, resource_size_t size,
183                               unsigned char flags)
184 {
185         struct pnp_mem *data, *ptr;
186
187         data = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
188         if (!data)
189                 return -ENOMEM;
190
191         data->min = min;
192         data->max = max;
193         data->align = align;
194         data->size = size;
195         data->flags = flags;
196
197         ptr = option->mem;
198         while (ptr && ptr->next)
199                 ptr = ptr->next;
200         if (ptr)
201                 ptr->next = data;
202         else
203                 option->mem = data;
204
205         dev_dbg(&dev->dev, "  mem "
206                 "min %#llx max %#llx align %lld size %lld flags %#x\n",
207                 (unsigned long long) data->min,
208                 (unsigned long long) data->max,
209                 (unsigned long long) data->align,
210                 (unsigned long long) data->size, data->flags);
211         return 0;
212 }
213
214 static void pnp_free_port(struct pnp_port *port)
215 {
216         struct pnp_port *next;
217
218         while (port) {
219                 next = port->next;
220                 kfree(port);
221                 port = next;
222         }
223 }
224
225 static void pnp_free_irq(struct pnp_irq *irq)
226 {
227         struct pnp_irq *next;
228
229         while (irq) {
230                 next = irq->next;
231                 kfree(irq);
232                 irq = next;
233         }
234 }
235
236 static void pnp_free_dma(struct pnp_dma *dma)
237 {
238         struct pnp_dma *next;
239
240         while (dma) {
241                 next = dma->next;
242                 kfree(dma);
243                 dma = next;
244         }
245 }
246
247 static void pnp_free_mem(struct pnp_mem *mem)
248 {
249         struct pnp_mem *next;
250
251         while (mem) {
252                 next = mem->next;
253                 kfree(mem);
254                 mem = next;
255         }
256 }
257
258 void pnp_free_option(struct pnp_option *option)
259 {
260         struct pnp_option *next;
261
262         while (option) {
263                 next = option->next;
264                 pnp_free_port(option->port);
265                 pnp_free_irq(option->irq);
266                 pnp_free_dma(option->dma);
267                 pnp_free_mem(option->mem);
268                 kfree(option);
269                 option = next;
270         }
271 }
272
273 /*
274  * resource validity checking
275  */
276
277 #define length(start, end) (*(end) - *(start) + 1)
278
279 /* Two ranges conflict if one doesn't end before the other starts */
280 #define ranged_conflict(starta, enda, startb, endb) \
281         !((*(enda) < *(startb)) || (*(endb) < *(starta)))
282
283 #define cannot_compare(flags) \
284 ((flags) & IORESOURCE_DISABLED)
285
286 int pnp_check_port(struct pnp_dev *dev, struct resource *res)
287 {
288         int i;
289         struct pnp_dev *tdev;
290         struct resource *tres;
291         resource_size_t *port, *end, *tport, *tend;
292
293         port = &res->start;
294         end = &res->end;
295
296         /* if the resource doesn't exist, don't complain about it */
297         if (cannot_compare(res->flags))
298                 return 1;
299
300         /* check if the resource is already in use, skip if the
301          * device is active because it itself may be in use */
302         if (!dev->active) {
303                 if (__check_region(&ioport_resource, *port, length(port, end)))
304                         return 0;
305         }
306
307         /* check if the resource is reserved */
308         for (i = 0; i < 8; i++) {
309                 int rport = pnp_reserve_io[i << 1];
310                 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
311                 if (ranged_conflict(port, end, &rport, &rend))
312                         return 0;
313         }
314
315         /* check for internal conflicts */
316         for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
317                 if (tres != res && tres->flags & IORESOURCE_IO) {
318                         tport = &tres->start;
319                         tend = &tres->end;
320                         if (ranged_conflict(port, end, tport, tend))
321                                 return 0;
322                 }
323         }
324
325         /* check for conflicts with other pnp devices */
326         pnp_for_each_dev(tdev) {
327                 if (tdev == dev)
328                         continue;
329                 for (i = 0;
330                      (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
331                      i++) {
332                         if (tres->flags & IORESOURCE_IO) {
333                                 if (cannot_compare(tres->flags))
334                                         continue;
335                                 tport = &tres->start;
336                                 tend = &tres->end;
337                                 if (ranged_conflict(port, end, tport, tend))
338                                         return 0;
339                         }
340                 }
341         }
342
343         return 1;
344 }
345
346 int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
347 {
348         int i;
349         struct pnp_dev *tdev;
350         struct resource *tres;
351         resource_size_t *addr, *end, *taddr, *tend;
352
353         addr = &res->start;
354         end = &res->end;
355
356         /* if the resource doesn't exist, don't complain about it */
357         if (cannot_compare(res->flags))
358                 return 1;
359
360         /* check if the resource is already in use, skip if the
361          * device is active because it itself may be in use */
362         if (!dev->active) {
363                 if (check_mem_region(*addr, length(addr, end)))
364                         return 0;
365         }
366
367         /* check if the resource is reserved */
368         for (i = 0; i < 8; i++) {
369                 int raddr = pnp_reserve_mem[i << 1];
370                 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
371                 if (ranged_conflict(addr, end, &raddr, &rend))
372                         return 0;
373         }
374
375         /* check for internal conflicts */
376         for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
377                 if (tres != res && tres->flags & IORESOURCE_MEM) {
378                         taddr = &tres->start;
379                         tend = &tres->end;
380                         if (ranged_conflict(addr, end, taddr, tend))
381                                 return 0;
382                 }
383         }
384
385         /* check for conflicts with other pnp devices */
386         pnp_for_each_dev(tdev) {
387                 if (tdev == dev)
388                         continue;
389                 for (i = 0;
390                      (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
391                      i++) {
392                         if (tres->flags & IORESOURCE_MEM) {
393                                 if (cannot_compare(tres->flags))
394                                         continue;
395                                 taddr = &tres->start;
396                                 tend = &tres->end;
397                                 if (ranged_conflict(addr, end, taddr, tend))
398                                         return 0;
399                         }
400                 }
401         }
402
403         return 1;
404 }
405
406 static irqreturn_t pnp_test_handler(int irq, void *dev_id)
407 {
408         return IRQ_HANDLED;
409 }
410
411 int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
412 {
413         int i;
414         struct pnp_dev *tdev;
415         struct resource *tres;
416         resource_size_t *irq;
417
418         irq = &res->start;
419
420         /* if the resource doesn't exist, don't complain about it */
421         if (cannot_compare(res->flags))
422                 return 1;
423
424         /* check if the resource is valid */
425         if (*irq < 0 || *irq > 15)
426                 return 0;
427
428         /* check if the resource is reserved */
429         for (i = 0; i < 16; i++) {
430                 if (pnp_reserve_irq[i] == *irq)
431                         return 0;
432         }
433
434         /* check for internal conflicts */
435         for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
436                 if (tres != res && tres->flags & IORESOURCE_IRQ) {
437                         if (tres->start == *irq)
438                                 return 0;
439                 }
440         }
441
442 #ifdef CONFIG_PCI
443         /* check if the resource is being used by a pci device */
444         {
445                 struct pci_dev *pci = NULL;
446                 for_each_pci_dev(pci) {
447                         if (pci->irq == *irq) {
448                                 pci_dev_put(pci);
449                                 return 0;
450                         }
451                 }
452         }
453 #endif
454
455         /* check if the resource is already in use, skip if the
456          * device is active because it itself may be in use */
457         if (!dev->active) {
458                 if (request_irq(*irq, pnp_test_handler,
459                                 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
460                         return 0;
461                 free_irq(*irq, NULL);
462         }
463
464         /* check for conflicts with other pnp devices */
465         pnp_for_each_dev(tdev) {
466                 if (tdev == dev)
467                         continue;
468                 for (i = 0;
469                      (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
470                      i++) {
471                         if (tres->flags & IORESOURCE_IRQ) {
472                                 if (cannot_compare(tres->flags))
473                                         continue;
474                                 if (tres->start == *irq)
475                                         return 0;
476                         }
477                 }
478         }
479
480         return 1;
481 }
482
483 int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
484 {
485 #ifndef CONFIG_IA64
486         int i;
487         struct pnp_dev *tdev;
488         struct resource *tres;
489         resource_size_t *dma;
490
491         dma = &res->start;
492
493         /* if the resource doesn't exist, don't complain about it */
494         if (cannot_compare(res->flags))
495                 return 1;
496
497         /* check if the resource is valid */
498         if (*dma < 0 || *dma == 4 || *dma > 7)
499                 return 0;
500
501         /* check if the resource is reserved */
502         for (i = 0; i < 8; i++) {
503                 if (pnp_reserve_dma[i] == *dma)
504                         return 0;
505         }
506
507         /* check for internal conflicts */
508         for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
509                 if (tres != res && tres->flags & IORESOURCE_DMA) {
510                         if (tres->start == *dma)
511                                 return 0;
512                 }
513         }
514
515         /* check if the resource is already in use, skip if the
516          * device is active because it itself may be in use */
517         if (!dev->active) {
518                 if (request_dma(*dma, "pnp"))
519                         return 0;
520                 free_dma(*dma);
521         }
522
523         /* check for conflicts with other pnp devices */
524         pnp_for_each_dev(tdev) {
525                 if (tdev == dev)
526                         continue;
527                 for (i = 0;
528                      (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
529                      i++) {
530                         if (tres->flags & IORESOURCE_DMA) {
531                                 if (cannot_compare(tres->flags))
532                                         continue;
533                                 if (tres->start == *dma)
534                                         return 0;
535                         }
536                 }
537         }
538
539         return 1;
540 #else
541         /* IA64 does not have legacy DMA */
542         return 0;
543 #endif
544 }
545
546 int pnp_resource_type(struct resource *res)
547 {
548         return res->flags & (IORESOURCE_IO  | IORESOURCE_MEM |
549                              IORESOURCE_IRQ | IORESOURCE_DMA);
550 }
551
552 struct resource *pnp_get_resource(struct pnp_dev *dev,
553                                   unsigned int type, unsigned int num)
554 {
555         struct pnp_resource *pnp_res;
556         struct resource *res;
557
558         list_for_each_entry(pnp_res, &dev->resources, list) {
559                 res = &pnp_res->res;
560                 if (pnp_resource_type(res) == type && num-- == 0)
561                         return res;
562         }
563         return NULL;
564 }
565 EXPORT_SYMBOL(pnp_get_resource);
566
567 static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
568 {
569         struct pnp_resource *pnp_res;
570
571         pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
572         if (!pnp_res)
573                 return NULL;
574
575         list_add_tail(&pnp_res->list, &dev->resources);
576         return pnp_res;
577 }
578
579 struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
580                                           int flags)
581 {
582         struct pnp_resource *pnp_res;
583         struct resource *res;
584
585         pnp_res = pnp_new_resource(dev);
586         if (!pnp_res) {
587                 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
588                 return NULL;
589         }
590
591         res = &pnp_res->res;
592         res->flags = IORESOURCE_IRQ | flags;
593         res->start = irq;
594         res->end = irq;
595
596         dev_dbg(&dev->dev, "  add irq %d flags %#x\n", irq, flags);
597         return pnp_res;
598 }
599
600 struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
601                                           int flags)
602 {
603         struct pnp_resource *pnp_res;
604         struct resource *res;
605
606         pnp_res = pnp_new_resource(dev);
607         if (!pnp_res) {
608                 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
609                 return NULL;
610         }
611
612         res = &pnp_res->res;
613         res->flags = IORESOURCE_DMA | flags;
614         res->start = dma;
615         res->end = dma;
616
617         dev_dbg(&dev->dev, "  add dma %d flags %#x\n", dma, flags);
618         return pnp_res;
619 }
620
621 struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
622                                          resource_size_t start,
623                                          resource_size_t end, int flags)
624 {
625         struct pnp_resource *pnp_res;
626         struct resource *res;
627
628         pnp_res = pnp_new_resource(dev);
629         if (!pnp_res) {
630                 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
631                         (unsigned long long) start,
632                         (unsigned long long) end);
633                 return NULL;
634         }
635
636         res = &pnp_res->res;
637         res->flags = IORESOURCE_IO | flags;
638         res->start = start;
639         res->end = end;
640
641         dev_dbg(&dev->dev, "  add io  %#llx-%#llx flags %#x\n",
642                 (unsigned long long) start, (unsigned long long) end, flags);
643         return pnp_res;
644 }
645
646 struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
647                                           resource_size_t start,
648                                           resource_size_t end, int flags)
649 {
650         struct pnp_resource *pnp_res;
651         struct resource *res;
652
653         pnp_res = pnp_new_resource(dev);
654         if (!pnp_res) {
655                 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
656                         (unsigned long long) start,
657                         (unsigned long long) end);
658                 return NULL;
659         }
660
661         res = &pnp_res->res;
662         res->flags = IORESOURCE_MEM | flags;
663         res->start = start;
664         res->end = end;
665
666         dev_dbg(&dev->dev, "  add mem %#llx-%#llx flags %#x\n",
667                 (unsigned long long) start, (unsigned long long) end, flags);
668         return pnp_res;
669 }
670
671 static int pnp_possible_option(struct pnp_option *option, int type,
672                                resource_size_t start, resource_size_t size)
673 {
674         struct pnp_option *tmp;
675         struct pnp_port *port;
676         struct pnp_mem *mem;
677         struct pnp_irq *irq;
678         struct pnp_dma *dma;
679
680         if (!option)
681                 return 0;
682
683         for (tmp = option; tmp; tmp = tmp->next) {
684                 switch (type) {
685                 case IORESOURCE_IO:
686                         for (port = tmp->port; port; port = port->next) {
687                                 if (port->min == start && port->size == size)
688                                         return 1;
689                         }
690                         break;
691                 case IORESOURCE_MEM:
692                         for (mem = tmp->mem; mem; mem = mem->next) {
693                                 if (mem->min == start && mem->size == size)
694                                         return 1;
695                         }
696                         break;
697                 case IORESOURCE_IRQ:
698                         for (irq = tmp->irq; irq; irq = irq->next) {
699                                 if (start < PNP_IRQ_NR &&
700                                     test_bit(start, irq->map.bits))
701                                         return 1;
702                         }
703                         break;
704                 case IORESOURCE_DMA:
705                         for (dma = tmp->dma; dma; dma = dma->next) {
706                                 if (dma->map & (1 << start))
707                                         return 1;
708                         }
709                         break;
710                 }
711         }
712
713         return 0;
714 }
715
716 /*
717  * Determine whether the specified resource is a possible configuration
718  * for this device.
719  */
720 int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
721                         resource_size_t size)
722 {
723         if (pnp_possible_option(dev->independent, type, start, size))
724                 return 1;
725
726         if (pnp_possible_option(dev->dependent, type, start, size))
727                 return 1;
728
729         return 0;
730 }
731 EXPORT_SYMBOL(pnp_possible_config);
732
733 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
734 static int __init pnp_setup_reserve_irq(char *str)
735 {
736         int i;
737
738         for (i = 0; i < 16; i++)
739                 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
740                         break;
741         return 1;
742 }
743
744 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
745
746 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
747 static int __init pnp_setup_reserve_dma(char *str)
748 {
749         int i;
750
751         for (i = 0; i < 8; i++)
752                 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
753                         break;
754         return 1;
755 }
756
757 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
758
759 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
760 static int __init pnp_setup_reserve_io(char *str)
761 {
762         int i;
763
764         for (i = 0; i < 16; i++)
765                 if (get_option(&str, &pnp_reserve_io[i]) != 2)
766                         break;
767         return 1;
768 }
769
770 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
771
772 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
773 static int __init pnp_setup_reserve_mem(char *str)
774 {
775         int i;
776
777         for (i = 0; i < 16; i++)
778                 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
779                         break;
780         return 1;
781 }
782
783 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);