blob: 786fd356916d0a66f135decd11d7c51aab1fa6a5 [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++)
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) \
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600240((flags) & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600242int pnp_check_port(struct pnp_dev *dev, struct resource *res)
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 Helgaasf5d94ff2008-04-28 16:34:22 -0600246 struct resource *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 port = &res->start;
250 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600253 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700258 if (!dev->active) {
259 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261 }
262
263 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600264 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;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700267 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269 }
270
271 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600272 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
273 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600274 tport = &tres->start;
275 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700276 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278 }
279 }
280
281 /* check for conflicts with other pnp devices */
282 pnp_for_each_dev(tdev) {
283 if (tdev == dev)
284 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600285 for (i = 0;
286 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
287 i++) {
288 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600289 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600291 tport = &tres->start;
292 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700293 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295 }
296 }
297 }
298
299 return 1;
300}
301
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600302int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600304 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600306 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700307 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700308
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600309 addr = &res->start;
310 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600313 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700318 if (!dev->active) {
319 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321 }
322
323 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600324 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;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700327 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329 }
330
331 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600332 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
333 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600334 taddr = &tres->start;
335 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700336 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338 }
339 }
340
341 /* check for conflicts with other pnp devices */
342 pnp_for_each_dev(tdev) {
343 if (tdev == dev)
344 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600345 for (i = 0;
346 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
347 i++) {
348 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600349 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600351 taddr = &tres->start;
352 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700353 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
356 }
357 }
358
359 return 1;
360}
361
David Howells7d12e782006-10-05 14:55:46 +0100362static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 return IRQ_HANDLED;
365}
366
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600367int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600369 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600371 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600372 resource_size_t *irq;
373
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600374 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600377 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 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 */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600385 for (i = 0; i < 16; i++) {
386 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388 }
389
390 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600391 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
392 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600393 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
395 }
396 }
397
398#ifdef CONFIG_PCI
399 /* check if the resource is being used by a pci device */
400 {
401 struct pci_dev *pci = NULL;
402 for_each_pci_dev(pci) {
Julia Lawall8ea50a32007-11-28 16:21:36 -0800403 if (pci->irq == *irq) {
404 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
Julia Lawall8ea50a32007-11-28 16:21:36 -0800406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 }
409#endif
410
411 /* check if the resource is already in use, skip if the
412 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700413 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700414 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700415 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
417 free_irq(*irq, NULL);
418 }
419
420 /* check for conflicts with other pnp devices */
421 pnp_for_each_dev(tdev) {
422 if (tdev == dev)
423 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600424 for (i = 0;
425 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
426 i++) {
427 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600428 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600430 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432 }
433 }
434 }
435
436 return 1;
437}
438
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600439int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441#ifndef CONFIG_IA64
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600444 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600445 resource_size_t *dma;
446
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600447 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600450 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 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 */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600458 for (i = 0; i < 8; i++) {
459 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461 }
462
463 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600464 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
465 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600466 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468 }
469 }
470
471 /* check if the resource is already in use, skip if the
472 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700473 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (request_dma(*dma, "pnp"))
475 return 0;
476 free_dma(*dma);
477 }
478
479 /* check for conflicts with other pnp devices */
480 pnp_for_each_dev(tdev) {
481 if (tdev == dev)
482 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600483 for (i = 0;
484 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
485 i++) {
486 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600487 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600489 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
491 }
492 }
493 }
494
495 return 1;
496#else
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700497 /* IA64 does not have legacy DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499#endif
500}
501
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600502int pnp_resource_type(struct resource *res)
503{
504 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
505 IORESOURCE_IRQ | IORESOURCE_DMA);
506}
507
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600508struct resource *pnp_get_resource(struct pnp_dev *dev,
509 unsigned int type, unsigned int num)
510{
511 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600512 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600513
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600514 list_for_each_entry(pnp_res, &dev->resources, list) {
515 res = &pnp_res->res;
516 if (pnp_resource_type(res) == type && num-- == 0)
517 return res;
518 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600519 return NULL;
520}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600521EXPORT_SYMBOL(pnp_get_resource);
522
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600523static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600524{
525 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600526
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600527 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
528 if (!pnp_res)
529 return NULL;
530
531 list_add_tail(&pnp_res->list, &dev->resources);
532 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600533}
534
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600535struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
536 int flags)
537{
538 struct pnp_resource *pnp_res;
539 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600540
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600541 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600542 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600543 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600544 return NULL;
545 }
546
547 res = &pnp_res->res;
548 res->flags = IORESOURCE_IRQ | flags;
549 res->start = irq;
550 res->end = irq;
551
552 dev_dbg(&dev->dev, " add irq %d flags %#x\n", irq, flags);
553 return pnp_res;
554}
555
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600556struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
557 int flags)
558{
559 struct pnp_resource *pnp_res;
560 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600561
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600562 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600563 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600564 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600565 return NULL;
566 }
567
568 res = &pnp_res->res;
569 res->flags = IORESOURCE_DMA | flags;
570 res->start = dma;
571 res->end = dma;
572
573 dev_dbg(&dev->dev, " add dma %d flags %#x\n", dma, flags);
574 return pnp_res;
575}
576
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600577struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
578 resource_size_t start,
579 resource_size_t end, int flags)
580{
581 struct pnp_resource *pnp_res;
582 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600583
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600584 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600585 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600586 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
587 (unsigned long long) start,
588 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600589 return NULL;
590 }
591
592 res = &pnp_res->res;
593 res->flags = IORESOURCE_IO | flags;
594 res->start = start;
595 res->end = end;
596
597 dev_dbg(&dev->dev, " add io %#llx-%#llx flags %#x\n",
598 (unsigned long long) start, (unsigned long long) end, flags);
599 return pnp_res;
600}
601
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600602struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
603 resource_size_t start,
604 resource_size_t end, int flags)
605{
606 struct pnp_resource *pnp_res;
607 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600608
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600609 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600610 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600611 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
612 (unsigned long long) start,
613 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600614 return NULL;
615 }
616
617 res = &pnp_res->res;
618 res->flags = IORESOURCE_MEM | flags;
619 res->start = start;
620 res->end = end;
621
622 dev_dbg(&dev->dev, " add mem %#llx-%#llx flags %#x\n",
623 (unsigned long long) start, (unsigned long long) end, flags);
624 return pnp_res;
625}
626
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600627static int pnp_possible_option(struct pnp_option *option, int type,
628 resource_size_t start, resource_size_t size)
629{
630 struct pnp_option *tmp;
631 struct pnp_port *port;
632 struct pnp_mem *mem;
633 struct pnp_irq *irq;
634 struct pnp_dma *dma;
635
636 if (!option)
637 return 0;
638
639 for (tmp = option; tmp; tmp = tmp->next) {
640 switch (type) {
641 case IORESOURCE_IO:
642 for (port = tmp->port; port; port = port->next) {
643 if (port->min == start && port->size == size)
644 return 1;
645 }
646 break;
647 case IORESOURCE_MEM:
648 for (mem = tmp->mem; mem; mem = mem->next) {
649 if (mem->min == start && mem->size == size)
650 return 1;
651 }
652 break;
653 case IORESOURCE_IRQ:
654 for (irq = tmp->irq; irq; irq = irq->next) {
655 if (start < PNP_IRQ_NR &&
656 test_bit(start, irq->map))
657 return 1;
658 }
659 break;
660 case IORESOURCE_DMA:
661 for (dma = tmp->dma; dma; dma = dma->next) {
662 if (dma->map & (1 << start))
663 return 1;
664 }
665 break;
666 }
667 }
668
669 return 0;
670}
671
672/*
673 * Determine whether the specified resource is a possible configuration
674 * for this device.
675 */
676int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
677 resource_size_t size)
678{
679 if (pnp_possible_option(dev->independent, type, start, size))
680 return 1;
681
682 if (pnp_possible_option(dev->dependent, type, start, size))
683 return 1;
684
685 return 0;
686}
687EXPORT_SYMBOL(pnp_possible_config);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int __init pnp_setup_reserve_irq(char *str)
691{
692 int i;
693
694 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700695 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 return 1;
698}
699
700__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
701
702/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static int __init pnp_setup_reserve_dma(char *str)
704{
705 int i;
706
707 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700708 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 return 1;
711}
712
713__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
714
715/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int __init pnp_setup_reserve_io(char *str)
717{
718 int i;
719
720 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700721 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 return 1;
724}
725
726__setup("pnp_reserve_io=", pnp_setup_reserve_io);
727
728/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int __init pnp_setup_reserve_mem(char *str)
730{
731 int i;
732
733 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700734 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736 return 1;
737}
738
739__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);