blob: eab16e5520ae7b81b16d28ee89634c3ad150bf77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * resource.c - Contains functions for registering and analyzing resource information
3 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070022static 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
28 * option registration
29 */
30
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070031static struct pnp_option *pnp_build_option(int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070046struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 struct pnp_option *option;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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)
Bjorn Helgaasa05d0782007-10-16 23:31:10 -070054 dev_err(&dev->dev, "independent resource already registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 dev->independent = option;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060056
57 dev_dbg(&dev->dev, "new independent option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return option;
59}
60
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070061struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
62 int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct pnp_option *option;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060075
76 dev_dbg(&dev->dev, "new dependent option (priority %#x)\n", priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return option;
78}
79
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060080int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option,
81 struct pnp_irq *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct pnp_irq *ptr;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -060084#ifdef DEBUG
85 char buf[PNP_IRQ_NR]; /* hex-encoded, so this is overkill but safe */
86#endif
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 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))
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500102 pcibios_penalize_isa_irq(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104#endif
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600105
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return 0;
112}
113
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600114int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option,
115 struct pnp_dma *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct pnp_dma *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600127 dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map,
128 data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600132int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option,
133 struct pnp_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct pnp_port *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 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
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600145 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
149}
150
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600151int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
152 struct pnp_mem *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct pnp_mem *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 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;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600163
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700242int pnp_check_port(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600244 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct pnp_dev *tdev;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600246 struct resource *res, *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700247 resource_size_t *port, *end, *tport, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700248
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600249 res = &dev->res.port_resource[idx];
250 port = &res->start;
251 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600254 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 1;
256
257 /* check if the resource is already in use, skip if the
258 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700259 if (!dev->active) {
260 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262 }
263
264 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600265 for (i = 0; i < 8; i++) {
266 int rport = pnp_reserve_io[i << 1];
267 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700268 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270 }
271
272 /* check for internal conflicts */
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600273 for (i = 0; i < PNP_MAX_PORT; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600274 tres = &dev->res.port_resource[i];
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600275 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600276 tport = &tres->start;
277 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700278 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280 }
281 }
282
283 /* check for conflicts with other pnp devices */
284 pnp_for_each_dev(tdev) {
285 if (tdev == dev)
286 continue;
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600287 for (i = 0; i < PNP_MAX_PORT; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600288 tres = &tdev->res.port_resource[i];
289 if (tres->flags & IORESOURCE_IO) {
290 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600292 tport = &tres->start;
293 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700294 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296 }
297 }
298 }
299
300 return 1;
301}
302
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700303int pnp_check_mem(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600305 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct pnp_dev *tdev;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600307 struct resource *res, *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700308 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700309
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600310 res = &dev->res.mem_resource[idx];
311 addr = &res->start;
312 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600315 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 1;
317
318 /* check if the resource is already in use, skip if the
319 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700320 if (!dev->active) {
321 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323 }
324
325 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600326 for (i = 0; i < 8; i++) {
327 int raddr = pnp_reserve_mem[i << 1];
328 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700329 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331 }
332
333 /* check for internal conflicts */
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600334 for (i = 0; i < PNP_MAX_MEM; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600335 tres = &dev->res.mem_resource[i];
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600336 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600337 taddr = &tres->start;
338 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700339 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341 }
342 }
343
344 /* check for conflicts with other pnp devices */
345 pnp_for_each_dev(tdev) {
346 if (tdev == dev)
347 continue;
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600348 for (i = 0; i < PNP_MAX_MEM; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600349 tres = &tdev->res.mem_resource[i];
350 if (tres->flags & IORESOURCE_MEM) {
351 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600353 taddr = &tres->start;
354 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700355 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357 }
358 }
359 }
360
361 return 1;
362}
363
David Howells7d12e782006-10-05 14:55:46 +0100364static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 return IRQ_HANDLED;
367}
368
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700369int pnp_check_irq(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600371 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct pnp_dev *tdev;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600373 struct resource *res, *tres;
374 resource_size_t *irq;
375
376 res = &dev->res.irq_resource[idx];
377 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600380 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 1;
382
383 /* check if the resource is valid */
384 if (*irq < 0 || *irq > 15)
385 return 0;
386
387 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600388 for (i = 0; i < 16; i++) {
389 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391 }
392
393 /* check for internal conflicts */
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600394 for (i = 0; i < PNP_MAX_IRQ; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600395 tres = &dev->res.irq_resource[i];
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600396 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600397 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399 }
400 }
401
402#ifdef CONFIG_PCI
403 /* check if the resource is being used by a pci device */
404 {
405 struct pci_dev *pci = NULL;
406 for_each_pci_dev(pci) {
Julia Lawall8ea50a32007-11-28 16:21:36 -0800407 if (pci->irq == *irq) {
408 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
Julia Lawall8ea50a32007-11-28 16:21:36 -0800410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
413#endif
414
415 /* check if the resource is already in use, skip if the
416 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700417 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700418 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700419 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421 free_irq(*irq, NULL);
422 }
423
424 /* check for conflicts with other pnp devices */
425 pnp_for_each_dev(tdev) {
426 if (tdev == dev)
427 continue;
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600428 for (i = 0; i < PNP_MAX_IRQ; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600429 tres = &tdev->res.irq_resource[i];
430 if (tres->flags & IORESOURCE_IRQ) {
431 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600433 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435 }
436 }
437 }
438
439 return 1;
440}
441
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700442int pnp_check_dma(struct pnp_dev *dev, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444#ifndef CONFIG_IA64
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600445 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct pnp_dev *tdev;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600447 struct resource *res, *tres;
448 resource_size_t *dma;
449
450 res = &dev->res.dma_resource[idx];
451 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600454 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 1;
456
457 /* check if the resource is valid */
458 if (*dma < 0 || *dma == 4 || *dma > 7)
459 return 0;
460
461 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600462 for (i = 0; i < 8; i++) {
463 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
465 }
466
467 /* check for internal conflicts */
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600468 for (i = 0; i < PNP_MAX_DMA; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600469 tres = &dev->res.dma_resource[i];
Bjorn Helgaasdb9eaea2008-04-28 16:34:21 -0600470 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600471 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473 }
474 }
475
476 /* check if the resource is already in use, skip if the
477 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700478 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (request_dma(*dma, "pnp"))
480 return 0;
481 free_dma(*dma);
482 }
483
484 /* check for conflicts with other pnp devices */
485 pnp_for_each_dev(tdev) {
486 if (tdev == dev)
487 continue;
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600488 for (i = 0; i < PNP_MAX_DMA; i++) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600489 tres = &tdev->res.dma_resource[i];
490 if (tres->flags & IORESOURCE_DMA) {
491 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600493 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495 }
496 }
497 }
498
499 return 1;
500#else
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700501 /* IA64 does not have legacy DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503#endif
504}
505
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600506struct resource *pnp_get_resource(struct pnp_dev *dev,
507 unsigned int type, unsigned int num)
508{
509 struct pnp_resource_table *res = &dev->res;
510
511 switch (type) {
512 case IORESOURCE_IO:
513 if (num >= PNP_MAX_PORT)
514 return NULL;
515 return &res->port_resource[num];
516 case IORESOURCE_MEM:
517 if (num >= PNP_MAX_MEM)
518 return NULL;
519 return &res->mem_resource[num];
520 case IORESOURCE_IRQ:
521 if (num >= PNP_MAX_IRQ)
522 return NULL;
523 return &res->irq_resource[num];
524 case IORESOURCE_DMA:
525 if (num >= PNP_MAX_DMA)
526 return NULL;
527 return &res->dma_resource[num];
528 }
529 return NULL;
530}
531EXPORT_SYMBOL(pnp_get_resource);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534static int __init pnp_setup_reserve_irq(char *str)
535{
536 int i;
537
538 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700539 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541 return 1;
542}
543
544__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
545
546/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static int __init pnp_setup_reserve_dma(char *str)
548{
549 int i;
550
551 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700552 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 return 1;
555}
556
557__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
558
559/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static int __init pnp_setup_reserve_io(char *str)
561{
562 int i;
563
564 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700565 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567 return 1;
568}
569
570__setup("pnp_reserve_io=", pnp_setup_reserve_io);
571
572/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static int __init pnp_setup_reserve_mem(char *str)
574{
575 int i;
576
577 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700578 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 return 1;
581}
582
583__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);