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 (test_bit(i, data->map))
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 #ifndef CONFIG_IA64
425 int tmp;
426 struct pnp_dev *tdev;
427 unsigned long * dma = &dev->res.dma_resource[idx].start;
428
429 /* if the resource doesn't exist, don't complain about it */
430 if (cannot_compare(dev->res.dma_resource[idx].flags))
431 return 1;
432
433 /* check if the resource is valid */
434 if (*dma < 0 || *dma == 4 || *dma > 7)
435 return 0;
436
437 /* check if the resource is reserved */
438 for (tmp = 0; tmp < 8; tmp++) {
439 if (pnp_reserve_dma[tmp] == *dma)
440 return 0;
441 }
442
443 /* check for internal conflicts */
444 for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
445 if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
446 if (dev->res.dma_resource[tmp].start == *dma)
447 return 0;
448 }
449 }
450
451 /* check if the resource is already in use, skip if the
452 * device is active because it itself may be in use */
453 if(!dev->active) {
454 if (request_dma(*dma, "pnp"))
455 return 0;
456 free_dma(*dma);
457 }
458
459 /* check for conflicts with other pnp devices */
460 pnp_for_each_dev(tdev) {
461 if (tdev == dev)
462 continue;
463 for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
464 if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
465 if (cannot_compare(tdev->res.dma_resource[tmp].flags))
466 continue;
467 if ((tdev->res.dma_resource[tmp].start == *dma))
468 return 0;
469 }
470 }
471 }
472
473 return 1;
474 #else
475 /* IA64 hasn't legacy DMA */
476 return 0;
477 #endif
478 }
479
480
481 EXPORT_SYMBOL(pnp_register_dependent_option);
482 EXPORT_SYMBOL(pnp_register_independent_option);
483 EXPORT_SYMBOL(pnp_register_irq_resource);
484 EXPORT_SYMBOL(pnp_register_dma_resource);
485 EXPORT_SYMBOL(pnp_register_port_resource);
486 EXPORT_SYMBOL(pnp_register_mem_resource);
487
488
489 /* format is: pnp_reserve_irq=irq1[,irq2] .... */
490
491 static int __init pnp_setup_reserve_irq(char *str)
492 {
493 int i;
494
495 for (i = 0; i < 16; i++)
496 if (get_option(&str,&pnp_reserve_irq[i]) != 2)
497 break;
498 return 1;
499 }
500
501 __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
502
503 /* format is: pnp_reserve_dma=dma1[,dma2] .... */
504
505 static int __init pnp_setup_reserve_dma(char *str)
506 {
507 int i;
508
509 for (i = 0; i < 8; i++)
510 if (get_option(&str,&pnp_reserve_dma[i]) != 2)
511 break;
512 return 1;
513 }
514
515 __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
516
517 /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
518
519 static int __init pnp_setup_reserve_io(char *str)
520 {
521 int i;
522
523 for (i = 0; i < 16; i++)
524 if (get_option(&str,&pnp_reserve_io[i]) != 2)
525 break;
526 return 1;
527 }
528
529 __setup("pnp_reserve_io=", pnp_setup_reserve_io);
530
531 /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
532
533 static int __init pnp_setup_reserve_mem(char *str)
534 {
535 int i;
536
537 for (i = 0; i < 16; i++)
538 if (get_option(&str,&pnp_reserve_mem[i]) != 2)
539 break;
540 return 1;
541 }
542
543 __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);
544
|
This page was automatically generated by the
LXR engine.
|