PNP: convert assign, interface to use pnp_get_resource(), not pnp_resource_table
[deliverable/linux.git] / drivers / pnp / resource.c
CommitLineData
1da177e4
LT
1/*
2 * resource.c - Contains functions for registering and analyzing resource information
3 *
c1017a4c 4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
1da177e4 5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
1da177e4
LT
6 */
7
1da177e4
LT
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
07d4e9af
BH
22static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
23static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
24static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
25static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
1da177e4
LT
26
27/*
28 * option registration
29 */
30
9dd78466 31static struct pnp_option *pnp_build_option(int priority)
1da177e4
LT
32{
33 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
34
1da177e4
LT
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
9dd78466 46struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
1da177e4
LT
47{
48 struct pnp_option *option;
07d4e9af 49
1da177e4
LT
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)
a05d0781 54 dev_err(&dev->dev, "independent resource already registered\n");
1da177e4 55 dev->independent = option;
c1caf06c
BH
56
57 dev_dbg(&dev->dev, "new independent option\n");
1da177e4
LT
58 return option;
59}
60
9dd78466
BH
61struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
62 int priority)
1da177e4
LT
63{
64 struct pnp_option *option;
07d4e9af 65
1da177e4
LT
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;
c1caf06c
BH
75
76 dev_dbg(&dev->dev, "new dependent option (priority %#x)\n", priority);
1da177e4
LT
77 return option;
78}
79
c1caf06c
BH
80int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option,
81 struct pnp_irq *data)
1da177e4
LT
82{
83 struct pnp_irq *ptr;
c1caf06c
BH
84#ifdef DEBUG
85 char buf[PNP_IRQ_NR]; /* hex-encoded, so this is overkill but safe */
86#endif
07d4e9af 87
1da177e4
LT
88 ptr = option->irq;
89 while (ptr && ptr->next)
90 ptr = ptr->next;
91 if (ptr)
92 ptr->next = data;
93 else
94 option->irq = data;
95
96#ifdef CONFIG_PCI
97 {
98 int i;
99
100 for (i = 0; i < 16; i++)
101 if (test_bit(i, data->map))
c9c3e457 102 pcibios_penalize_isa_irq(i, 0);
1da177e4
LT
103 }
104#endif
c1caf06c
BH
105
106#ifdef DEBUG
107 bitmap_scnprintf(buf, sizeof(buf), data->map, PNP_IRQ_NR);
108 dev_dbg(&dev->dev, " irq bitmask %s flags %#x\n", buf,
109 data->flags);
110#endif
1da177e4
LT
111 return 0;
112}
113
c1caf06c
BH
114int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option,
115 struct pnp_dma *data)
1da177e4
LT
116{
117 struct pnp_dma *ptr;
07d4e9af 118
1da177e4
LT
119 ptr = option->dma;
120 while (ptr && ptr->next)
121 ptr = ptr->next;
122 if (ptr)
123 ptr->next = data;
124 else
125 option->dma = data;
126
c1caf06c
BH
127 dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map,
128 data->flags);
1da177e4
LT
129 return 0;
130}
131
c1caf06c
BH
132int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option,
133 struct pnp_port *data)
1da177e4
LT
134{
135 struct pnp_port *ptr;
07d4e9af 136
1da177e4
LT
137 ptr = option->port;
138 while (ptr && ptr->next)
139 ptr = ptr->next;
140 if (ptr)
141 ptr->next = data;
142 else
143 option->port = data;
144
c1caf06c
BH
145 dev_dbg(&dev->dev, " io "
146 "min %#x max %#x align %d size %d flags %#x\n",
147 data->min, data->max, data->align, data->size, data->flags);
1da177e4
LT
148 return 0;
149}
150
c1caf06c
BH
151int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
152 struct pnp_mem *data)
1da177e4
LT
153{
154 struct pnp_mem *ptr;
07d4e9af 155
1da177e4
LT
156 ptr = option->mem;
157 while (ptr && ptr->next)
158 ptr = ptr->next;
159 if (ptr)
160 ptr->next = data;
161 else
162 option->mem = data;
c1caf06c
BH
163
164 dev_dbg(&dev->dev, " mem "
165 "min %#x max %#x align %d size %d flags %#x\n",
166 data->min, data->max, data->align, data->size, data->flags);
1da177e4
LT
167 return 0;
168}
169
170static void pnp_free_port(struct pnp_port *port)
171{
172 struct pnp_port *next;
173
174 while (port) {
175 next = port->next;
176 kfree(port);
177 port = next;
178 }
179}
180
181static void pnp_free_irq(struct pnp_irq *irq)
182{
183 struct pnp_irq *next;
184
185 while (irq) {
186 next = irq->next;
187 kfree(irq);
188 irq = next;
189 }
190}
191
192static void pnp_free_dma(struct pnp_dma *dma)
193{
194 struct pnp_dma *next;
195
196 while (dma) {
197 next = dma->next;
198 kfree(dma);
199 dma = next;
200 }
201}
202
203static void pnp_free_mem(struct pnp_mem *mem)
204{
205 struct pnp_mem *next;
206
207 while (mem) {
208 next = mem->next;
209 kfree(mem);
210 mem = next;
211 }
212}
213
214void pnp_free_option(struct pnp_option *option)
215{
216 struct pnp_option *next;
217
218 while (option) {
219 next = option->next;
220 pnp_free_port(option->port);
221 pnp_free_irq(option->irq);
222 pnp_free_dma(option->dma);
223 pnp_free_mem(option->mem);
224 kfree(option);
225 option = next;
226 }
227}
228
1da177e4
LT
229/*
230 * resource validity checking
231 */
232
233#define length(start, end) (*(end) - *(start) + 1)
234
235/* Two ranges conflict if one doesn't end before the other starts */
236#define ranged_conflict(starta, enda, startb, endb) \
237 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
238
239#define cannot_compare(flags) \
240((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
241
f5d94ff0 242int pnp_check_port(struct pnp_dev *dev, struct resource *res)
1da177e4 243{
ecfa935a 244 int i;
1da177e4 245 struct pnp_dev *tdev;
f5d94ff0 246 struct resource *tres;
b60ba834 247 resource_size_t *port, *end, *tport, *tend;
07d4e9af 248
30c016a0
BH
249 port = &res->start;
250 end = &res->end;
1da177e4
LT
251
252 /* if the resource doesn't exist, don't complain about it */
30c016a0 253 if (cannot_compare(res->flags))
1da177e4
LT
254 return 1;
255
256 /* check if the resource is already in use, skip if the
257 * device is active because it itself may be in use */
9dd78466
BH
258 if (!dev->active) {
259 if (__check_region(&ioport_resource, *port, length(port, end)))
1da177e4
LT
260 return 0;
261 }
262
263 /* check if the resource is reserved */
ecfa935a
BH
264 for (i = 0; i < 8; i++) {
265 int rport = pnp_reserve_io[i << 1];
266 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
9dd78466 267 if (ranged_conflict(port, end, &rport, &rend))
1da177e4
LT
268 return 0;
269 }
270
271 /* check for internal conflicts */
db9eaeab 272 for (i = 0; i < PNP_MAX_PORT; i++) {
be81b4a4
BH
273 tres = pnp_get_resource(dev, IORESOURCE_IO, i);
274 if (tres && tres != res && tres->flags & IORESOURCE_IO) {
30c016a0
BH
275 tport = &tres->start;
276 tend = &tres->end;
9dd78466 277 if (ranged_conflict(port, end, tport, tend))
1da177e4
LT
278 return 0;
279 }
280 }
281
282 /* check for conflicts with other pnp devices */
283 pnp_for_each_dev(tdev) {
284 if (tdev == dev)
285 continue;
ecfa935a 286 for (i = 0; i < PNP_MAX_PORT; i++) {
be81b4a4
BH
287 tres = pnp_get_resource(tdev, IORESOURCE_IO, i);
288 if (tres && tres->flags & IORESOURCE_IO) {
30c016a0 289 if (cannot_compare(tres->flags))
1da177e4 290 continue;
30c016a0
BH
291 tport = &tres->start;
292 tend = &tres->end;
9dd78466 293 if (ranged_conflict(port, end, tport, tend))
1da177e4
LT
294 return 0;
295 }
296 }
297 }
298
299 return 1;
300}
301
f5d94ff0 302int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
1da177e4 303{
ecfa935a 304 int i;
1da177e4 305 struct pnp_dev *tdev;
f5d94ff0 306 struct resource *tres;
b60ba834 307 resource_size_t *addr, *end, *taddr, *tend;
07d4e9af 308
30c016a0
BH
309 addr = &res->start;
310 end = &res->end;
1da177e4
LT
311
312 /* if the resource doesn't exist, don't complain about it */
30c016a0 313 if (cannot_compare(res->flags))
1da177e4
LT
314 return 1;
315
316 /* check if the resource is already in use, skip if the
317 * device is active because it itself may be in use */
9dd78466
BH
318 if (!dev->active) {
319 if (check_mem_region(*addr, length(addr, end)))
1da177e4
LT
320 return 0;
321 }
322
323 /* check if the resource is reserved */
ecfa935a
BH
324 for (i = 0; i < 8; i++) {
325 int raddr = pnp_reserve_mem[i << 1];
326 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
9dd78466 327 if (ranged_conflict(addr, end, &raddr, &rend))
1da177e4
LT
328 return 0;
329 }
330
331 /* check for internal conflicts */
db9eaeab 332 for (i = 0; i < PNP_MAX_MEM; i++) {
be81b4a4
BH
333 tres = pnp_get_resource(dev, IORESOURCE_MEM, i);
334 if (tres && tres != res && tres->flags & IORESOURCE_MEM) {
30c016a0
BH
335 taddr = &tres->start;
336 tend = &tres->end;
9dd78466 337 if (ranged_conflict(addr, end, taddr, tend))
1da177e4
LT
338 return 0;
339 }
340 }
341
342 /* check for conflicts with other pnp devices */
343 pnp_for_each_dev(tdev) {
344 if (tdev == dev)
345 continue;
ecfa935a 346 for (i = 0; i < PNP_MAX_MEM; i++) {
be81b4a4
BH
347 tres = pnp_get_resource(tdev, IORESOURCE_MEM, i);
348 if (tres && tres->flags & IORESOURCE_MEM) {
30c016a0 349 if (cannot_compare(tres->flags))
1da177e4 350 continue;
30c016a0
BH
351 taddr = &tres->start;
352 tend = &tres->end;
9dd78466 353 if (ranged_conflict(addr, end, taddr, tend))
1da177e4
LT
354 return 0;
355 }
356 }
357 }
358
359 return 1;
360}
361
7d12e780 362static irqreturn_t pnp_test_handler(int irq, void *dev_id)
1da177e4
LT
363{
364 return IRQ_HANDLED;
365}
366
f5d94ff0 367int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
1da177e4 368{
ecfa935a 369 int i;
1da177e4 370 struct pnp_dev *tdev;
f5d94ff0 371 struct resource *tres;
30c016a0
BH
372 resource_size_t *irq;
373
30c016a0 374 irq = &res->start;
1da177e4
LT
375
376 /* if the resource doesn't exist, don't complain about it */
30c016a0 377 if (cannot_compare(res->flags))
1da177e4
LT
378 return 1;
379
380 /* check if the resource is valid */
381 if (*irq < 0 || *irq > 15)
382 return 0;
383
384 /* check if the resource is reserved */
ecfa935a
BH
385 for (i = 0; i < 16; i++) {
386 if (pnp_reserve_irq[i] == *irq)
1da177e4
LT
387 return 0;
388 }
389
390 /* check for internal conflicts */
db9eaeab 391 for (i = 0; i < PNP_MAX_IRQ; i++) {
be81b4a4
BH
392 tres = pnp_get_resource(dev, IORESOURCE_IRQ, i);
393 if (tres && tres != res && tres->flags & IORESOURCE_IRQ) {
30c016a0 394 if (tres->start == *irq)
1da177e4
LT
395 return 0;
396 }
397 }
398
399#ifdef CONFIG_PCI
400 /* check if the resource is being used by a pci device */
401 {
402 struct pci_dev *pci = NULL;
403 for_each_pci_dev(pci) {
8ea50a3f
JL
404 if (pci->irq == *irq) {
405 pci_dev_put(pci);
1da177e4 406 return 0;
8ea50a3f 407 }
1da177e4
LT
408 }
409 }
410#endif
411
412 /* check if the resource is already in use, skip if the
413 * device is active because it itself may be in use */
9dd78466 414 if (!dev->active) {
0cadaf45 415 if (request_irq(*irq, pnp_test_handler,
9dd78466 416 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
1da177e4
LT
417 return 0;
418 free_irq(*irq, NULL);
419 }
420
421 /* check for conflicts with other pnp devices */
422 pnp_for_each_dev(tdev) {
423 if (tdev == dev)
424 continue;
ecfa935a 425 for (i = 0; i < PNP_MAX_IRQ; i++) {
be81b4a4
BH
426 tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i);
427 if (tres && tres->flags & IORESOURCE_IRQ) {
30c016a0 428 if (cannot_compare(tres->flags))
1da177e4 429 continue;
30c016a0 430 if (tres->start == *irq)
1da177e4
LT
431 return 0;
432 }
433 }
434 }
435
436 return 1;
437}
438
f5d94ff0 439int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
1da177e4
LT
440{
441#ifndef CONFIG_IA64
ecfa935a 442 int i;
1da177e4 443 struct pnp_dev *tdev;
f5d94ff0 444 struct resource *tres;
30c016a0
BH
445 resource_size_t *dma;
446
30c016a0 447 dma = &res->start;
1da177e4
LT
448
449 /* if the resource doesn't exist, don't complain about it */
30c016a0 450 if (cannot_compare(res->flags))
1da177e4
LT
451 return 1;
452
453 /* check if the resource is valid */
454 if (*dma < 0 || *dma == 4 || *dma > 7)
455 return 0;
456
457 /* check if the resource is reserved */
ecfa935a
BH
458 for (i = 0; i < 8; i++) {
459 if (pnp_reserve_dma[i] == *dma)
1da177e4
LT
460 return 0;
461 }
462
463 /* check for internal conflicts */
db9eaeab 464 for (i = 0; i < PNP_MAX_DMA; i++) {
be81b4a4
BH
465 tres = pnp_get_resource(dev, IORESOURCE_DMA, i);
466 if (tres && tres != res && tres->flags & IORESOURCE_DMA) {
30c016a0 467 if (tres->start == *dma)
1da177e4
LT
468 return 0;
469 }
470 }
471
472 /* check if the resource is already in use, skip if the
473 * device is active because it itself may be in use */
9dd78466 474 if (!dev->active) {
1da177e4
LT
475 if (request_dma(*dma, "pnp"))
476 return 0;
477 free_dma(*dma);
478 }
479
480 /* check for conflicts with other pnp devices */
481 pnp_for_each_dev(tdev) {
482 if (tdev == dev)
483 continue;
ecfa935a 484 for (i = 0; i < PNP_MAX_DMA; i++) {
be81b4a4
BH
485 tres = pnp_get_resource(tdev, IORESOURCE_DMA, i);
486 if (tres && tres->flags & IORESOURCE_DMA) {
30c016a0 487 if (cannot_compare(tres->flags))
1da177e4 488 continue;
30c016a0 489 if (tres->start == *dma)
1da177e4
LT
490 return 0;
491 }
492 }
493 }
494
495 return 1;
496#else
07d4e9af 497 /* IA64 does not have legacy DMA */
1da177e4
LT
498 return 0;
499#endif
500}
501
b90eca0a
BH
502struct resource *pnp_get_resource(struct pnp_dev *dev,
503 unsigned int type, unsigned int num)
504{
505 struct pnp_resource_table *res = &dev->res;
506
507 switch (type) {
508 case IORESOURCE_IO:
509 if (num >= PNP_MAX_PORT)
510 return NULL;
511 return &res->port_resource[num];
512 case IORESOURCE_MEM:
513 if (num >= PNP_MAX_MEM)
514 return NULL;
515 return &res->mem_resource[num];
516 case IORESOURCE_IRQ:
517 if (num >= PNP_MAX_IRQ)
518 return NULL;
519 return &res->irq_resource[num];
520 case IORESOURCE_DMA:
521 if (num >= PNP_MAX_DMA)
522 return NULL;
523 return &res->dma_resource[num];
524 }
525 return NULL;
526}
527EXPORT_SYMBOL(pnp_get_resource);
528
1da177e4 529/* format is: pnp_reserve_irq=irq1[,irq2] .... */
1da177e4
LT
530static int __init pnp_setup_reserve_irq(char *str)
531{
532 int i;
533
534 for (i = 0; i < 16; i++)
9dd78466 535 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
1da177e4
LT
536 break;
537 return 1;
538}
539
540__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
541
542/* format is: pnp_reserve_dma=dma1[,dma2] .... */
1da177e4
LT
543static int __init pnp_setup_reserve_dma(char *str)
544{
545 int i;
546
547 for (i = 0; i < 8; i++)
9dd78466 548 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
1da177e4
LT
549 break;
550 return 1;
551}
552
553__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
554
555/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
1da177e4
LT
556static int __init pnp_setup_reserve_io(char *str)
557{
558 int i;
559
560 for (i = 0; i < 16; i++)
9dd78466 561 if (get_option(&str, &pnp_reserve_io[i]) != 2)
1da177e4
LT
562 break;
563 return 1;
564}
565
566__setup("pnp_reserve_io=", pnp_setup_reserve_io);
567
568/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
1da177e4
LT
569static int __init pnp_setup_reserve_mem(char *str)
570{
571 int i;
572
573 for (i = 0; i < 16; i++)
9dd78466 574 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
1da177e4
LT
575 break;
576 return 1;
577}
578
579__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);
This page took 0.33069 seconds and 5 git commands to generate.