blob: 391828c7f2079a6a0a3727ae5a1f7fdfdbbc4f2a [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
Rene Hermanbc033c92008-05-14 16:05:34 -070031struct 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++)
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600101 if (test_bit(i, data->map.bits))
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
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600107 bitmap_scnprintf(buf, sizeof(buf), data->map.bits, PNP_IRQ_NR);
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600108 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 "
Bjorn Helgaas169aaff2008-06-27 16:57:06 -0600146 "min %#llx max %#llx align %lld size %lld flags %#x\n",
147 (unsigned long long) data->min,
148 (unsigned long long) data->max,
149 (unsigned long long) data->align,
150 (unsigned long long) data->size, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return 0;
152}
153
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600154int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
155 struct pnp_mem *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct pnp_mem *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 ptr = option->mem;
160 while (ptr && ptr->next)
161 ptr = ptr->next;
162 if (ptr)
163 ptr->next = data;
164 else
165 option->mem = data;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600166
167 dev_dbg(&dev->dev, " mem "
Bjorn Helgaas169aaff2008-06-27 16:57:06 -0600168 "min %#llx max %#llx align %lld size %lld flags %#x\n",
169 (unsigned long long) data->min,
170 (unsigned long long) data->max,
171 (unsigned long long) data->align,
172 (unsigned long long) data->size, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
176static void pnp_free_port(struct pnp_port *port)
177{
178 struct pnp_port *next;
179
180 while (port) {
181 next = port->next;
182 kfree(port);
183 port = next;
184 }
185}
186
187static void pnp_free_irq(struct pnp_irq *irq)
188{
189 struct pnp_irq *next;
190
191 while (irq) {
192 next = irq->next;
193 kfree(irq);
194 irq = next;
195 }
196}
197
198static void pnp_free_dma(struct pnp_dma *dma)
199{
200 struct pnp_dma *next;
201
202 while (dma) {
203 next = dma->next;
204 kfree(dma);
205 dma = next;
206 }
207}
208
209static void pnp_free_mem(struct pnp_mem *mem)
210{
211 struct pnp_mem *next;
212
213 while (mem) {
214 next = mem->next;
215 kfree(mem);
216 mem = next;
217 }
218}
219
220void pnp_free_option(struct pnp_option *option)
221{
222 struct pnp_option *next;
223
224 while (option) {
225 next = option->next;
226 pnp_free_port(option->port);
227 pnp_free_irq(option->irq);
228 pnp_free_dma(option->dma);
229 pnp_free_mem(option->mem);
230 kfree(option);
231 option = next;
232 }
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
236 * resource validity checking
237 */
238
239#define length(start, end) (*(end) - *(start) + 1)
240
241/* Two ranges conflict if one doesn't end before the other starts */
242#define ranged_conflict(starta, enda, startb, endb) \
243 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
244
245#define cannot_compare(flags) \
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600246((flags) & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600248int pnp_check_port(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600250 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600252 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700253 resource_size_t *port, *end, *tport, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700254
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600255 port = &res->start;
256 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600259 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 1;
261
262 /* check if the resource is already in use, skip if the
263 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700264 if (!dev->active) {
265 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267 }
268
269 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600270 for (i = 0; i < 8; i++) {
271 int rport = pnp_reserve_io[i << 1];
272 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700273 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
275 }
276
277 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600278 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
279 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600280 tport = &tres->start;
281 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700282 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284 }
285 }
286
287 /* check for conflicts with other pnp devices */
288 pnp_for_each_dev(tdev) {
289 if (tdev == dev)
290 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600291 for (i = 0;
292 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
293 i++) {
294 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600295 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600297 tport = &tres->start;
298 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700299 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301 }
302 }
303 }
304
305 return 1;
306}
307
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600308int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600310 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600312 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700313 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700314
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600315 addr = &res->start;
316 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600319 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 1;
321
322 /* check if the resource is already in use, skip if the
323 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700324 if (!dev->active) {
325 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327 }
328
329 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600330 for (i = 0; i < 8; i++) {
331 int raddr = pnp_reserve_mem[i << 1];
332 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700333 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335 }
336
337 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600338 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
339 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600340 taddr = &tres->start;
341 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700342 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344 }
345 }
346
347 /* check for conflicts with other pnp devices */
348 pnp_for_each_dev(tdev) {
349 if (tdev == dev)
350 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600351 for (i = 0;
352 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
353 i++) {
354 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600355 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600357 taddr = &tres->start;
358 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700359 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 }
362 }
363 }
364
365 return 1;
366}
367
David Howells7d12e782006-10-05 14:55:46 +0100368static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 return IRQ_HANDLED;
371}
372
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600373int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600375 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600377 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600378 resource_size_t *irq;
379
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600380 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600383 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 1;
385
386 /* check if the resource is valid */
387 if (*irq < 0 || *irq > 15)
388 return 0;
389
390 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600391 for (i = 0; i < 16; i++) {
392 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394 }
395
396 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600397 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
398 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600399 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401 }
402 }
403
404#ifdef CONFIG_PCI
405 /* check if the resource is being used by a pci device */
406 {
407 struct pci_dev *pci = NULL;
408 for_each_pci_dev(pci) {
Julia Lawall8ea50a32007-11-28 16:21:36 -0800409 if (pci->irq == *irq) {
410 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
Julia Lawall8ea50a32007-11-28 16:21:36 -0800412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415#endif
416
417 /* check if the resource is already in use, skip if the
418 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700419 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700420 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700421 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423 free_irq(*irq, NULL);
424 }
425
426 /* check for conflicts with other pnp devices */
427 pnp_for_each_dev(tdev) {
428 if (tdev == dev)
429 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600430 for (i = 0;
431 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
432 i++) {
433 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600434 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600436 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438 }
439 }
440 }
441
442 return 1;
443}
444
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600445int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447#ifndef CONFIG_IA64
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600448 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600450 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600451 resource_size_t *dma;
452
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600453 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600456 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 1;
458
459 /* check if the resource is valid */
460 if (*dma < 0 || *dma == 4 || *dma > 7)
461 return 0;
462
463 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600464 for (i = 0; i < 8; i++) {
465 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467 }
468
469 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600470 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
471 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600472 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474 }
475 }
476
477 /* check if the resource is already in use, skip if the
478 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700479 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (request_dma(*dma, "pnp"))
481 return 0;
482 free_dma(*dma);
483 }
484
485 /* check for conflicts with other pnp devices */
486 pnp_for_each_dev(tdev) {
487 if (tdev == dev)
488 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600489 for (i = 0;
490 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
491 i++) {
492 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600493 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600495 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497 }
498 }
499 }
500
501 return 1;
502#else
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700503 /* IA64 does not have legacy DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505#endif
506}
507
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600508int pnp_resource_type(struct resource *res)
509{
510 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
511 IORESOURCE_IRQ | IORESOURCE_DMA);
512}
513
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600514struct resource *pnp_get_resource(struct pnp_dev *dev,
515 unsigned int type, unsigned int num)
516{
517 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600518 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600519
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600520 list_for_each_entry(pnp_res, &dev->resources, list) {
521 res = &pnp_res->res;
522 if (pnp_resource_type(res) == type && num-- == 0)
523 return res;
524 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600525 return NULL;
526}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600527EXPORT_SYMBOL(pnp_get_resource);
528
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600529static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600530{
531 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600532
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600533 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
534 if (!pnp_res)
535 return NULL;
536
537 list_add_tail(&pnp_res->list, &dev->resources);
538 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600539}
540
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600541struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
542 int flags)
543{
544 struct pnp_resource *pnp_res;
545 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600546
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600547 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600548 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600549 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600550 return NULL;
551 }
552
553 res = &pnp_res->res;
554 res->flags = IORESOURCE_IRQ | flags;
555 res->start = irq;
556 res->end = irq;
557
558 dev_dbg(&dev->dev, " add irq %d flags %#x\n", irq, flags);
559 return pnp_res;
560}
561
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600562struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
563 int flags)
564{
565 struct pnp_resource *pnp_res;
566 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600567
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600568 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600569 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600570 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600571 return NULL;
572 }
573
574 res = &pnp_res->res;
575 res->flags = IORESOURCE_DMA | flags;
576 res->start = dma;
577 res->end = dma;
578
579 dev_dbg(&dev->dev, " add dma %d flags %#x\n", dma, flags);
580 return pnp_res;
581}
582
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600583struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
584 resource_size_t start,
585 resource_size_t end, int flags)
586{
587 struct pnp_resource *pnp_res;
588 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600589
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600590 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600591 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600592 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
593 (unsigned long long) start,
594 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600595 return NULL;
596 }
597
598 res = &pnp_res->res;
599 res->flags = IORESOURCE_IO | flags;
600 res->start = start;
601 res->end = end;
602
603 dev_dbg(&dev->dev, " add io %#llx-%#llx flags %#x\n",
604 (unsigned long long) start, (unsigned long long) end, flags);
605 return pnp_res;
606}
607
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600608struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
609 resource_size_t start,
610 resource_size_t end, int flags)
611{
612 struct pnp_resource *pnp_res;
613 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600614
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600615 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600616 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600617 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
618 (unsigned long long) start,
619 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600620 return NULL;
621 }
622
623 res = &pnp_res->res;
624 res->flags = IORESOURCE_MEM | flags;
625 res->start = start;
626 res->end = end;
627
628 dev_dbg(&dev->dev, " add mem %#llx-%#llx flags %#x\n",
629 (unsigned long long) start, (unsigned long long) end, flags);
630 return pnp_res;
631}
632
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600633static int pnp_possible_option(struct pnp_option *option, int type,
634 resource_size_t start, resource_size_t size)
635{
636 struct pnp_option *tmp;
637 struct pnp_port *port;
638 struct pnp_mem *mem;
639 struct pnp_irq *irq;
640 struct pnp_dma *dma;
641
642 if (!option)
643 return 0;
644
645 for (tmp = option; tmp; tmp = tmp->next) {
646 switch (type) {
647 case IORESOURCE_IO:
648 for (port = tmp->port; port; port = port->next) {
649 if (port->min == start && port->size == size)
650 return 1;
651 }
652 break;
653 case IORESOURCE_MEM:
654 for (mem = tmp->mem; mem; mem = mem->next) {
655 if (mem->min == start && mem->size == size)
656 return 1;
657 }
658 break;
659 case IORESOURCE_IRQ:
660 for (irq = tmp->irq; irq; irq = irq->next) {
661 if (start < PNP_IRQ_NR &&
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600662 test_bit(start, irq->map.bits))
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600663 return 1;
664 }
665 break;
666 case IORESOURCE_DMA:
667 for (dma = tmp->dma; dma; dma = dma->next) {
668 if (dma->map & (1 << start))
669 return 1;
670 }
671 break;
672 }
673 }
674
675 return 0;
676}
677
678/*
679 * Determine whether the specified resource is a possible configuration
680 * for this device.
681 */
682int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
683 resource_size_t size)
684{
685 if (pnp_possible_option(dev->independent, type, start, size))
686 return 1;
687
688 if (pnp_possible_option(dev->dependent, type, start, size))
689 return 1;
690
691 return 0;
692}
693EXPORT_SYMBOL(pnp_possible_config);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static int __init pnp_setup_reserve_irq(char *str)
697{
698 int i;
699
700 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700701 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703 return 1;
704}
705
706__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
707
708/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static int __init pnp_setup_reserve_dma(char *str)
710{
711 int i;
712
713 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700714 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 return 1;
717}
718
719__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
720
721/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int __init pnp_setup_reserve_io(char *str)
723{
724 int i;
725
726 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700727 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 return 1;
730}
731
732__setup("pnp_reserve_io=", pnp_setup_reserve_io);
733
734/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735static int __init pnp_setup_reserve_mem(char *str)
736{
737 int i;
738
739 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700740 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 return 1;
743}
744
745__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);