blob: 61145491f363cd8b1d2b8c5b6962337816761c13 [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,
Bjorn Helgaasc2275362008-06-27 16:57:11 -060081 pnp_irq_mask_t *map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Bjorn Helgaasc2275362008-06-27 16:57:11 -060083 struct pnp_irq *data, *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
Bjorn Helgaasc2275362008-06-27 16:57:11 -060088 data = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
89 if (!data)
90 return -ENOMEM;
91
92 data->map = *map;
93 data->flags = flags;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ptr = option->irq;
96 while (ptr && ptr->next)
97 ptr = ptr->next;
98 if (ptr)
99 ptr->next = data;
100 else
101 option->irq = data;
102
103#ifdef CONFIG_PCI
104 {
105 int i;
106
107 for (i = 0; i < 16; i++)
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600108 if (test_bit(i, data->map.bits))
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500109 pcibios_penalize_isa_irq(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111#endif
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600112
113#ifdef DEBUG
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600114 bitmap_scnprintf(buf, sizeof(buf), data->map.bits, PNP_IRQ_NR);
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600115 dev_dbg(&dev->dev, " irq bitmask %s flags %#x\n", buf,
116 data->flags);
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600121int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600122 unsigned char map, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600124 struct pnp_dma *data, *ptr;
125
126 data = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
127 if (!data)
128 return -ENOMEM;
129
130 data->map = map;
131 data->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 ptr = option->dma;
134 while (ptr && ptr->next)
135 ptr = ptr->next;
136 if (ptr)
137 ptr->next = data;
138 else
139 option->dma = data;
140
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600141 dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map,
142 data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144}
145
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600146int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600147 resource_size_t min, resource_size_t max,
148 resource_size_t align, resource_size_t size,
149 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600151 struct pnp_port *data, *ptr;
152
153 data = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
156
157 data->min = min;
158 data->max = max;
159 data->align = align;
160 data->size = size;
161 data->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ptr = option->port;
164 while (ptr && ptr->next)
165 ptr = ptr->next;
166 if (ptr)
167 ptr->next = data;
168 else
169 option->port = data;
170
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600171 dev_dbg(&dev->dev, " io "
Bjorn Helgaas169aaff2008-06-27 16:57:06 -0600172 "min %#llx max %#llx align %lld size %lld flags %#x\n",
173 (unsigned long long) data->min,
174 (unsigned long long) data->max,
175 (unsigned long long) data->align,
176 (unsigned long long) data->size, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
178}
179
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600180int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600181 resource_size_t min, resource_size_t max,
182 resource_size_t align, resource_size_t size,
183 unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600185 struct pnp_mem *data, *ptr;
186
187 data = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
188 if (!data)
189 return -ENOMEM;
190
191 data->min = min;
192 data->max = max;
193 data->align = align;
194 data->size = size;
195 data->flags = flags;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ptr = option->mem;
198 while (ptr && ptr->next)
199 ptr = ptr->next;
200 if (ptr)
201 ptr->next = data;
202 else
203 option->mem = data;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600204
205 dev_dbg(&dev->dev, " mem "
Bjorn Helgaas169aaff2008-06-27 16:57:06 -0600206 "min %#llx max %#llx align %lld size %lld flags %#x\n",
207 (unsigned long long) data->min,
208 (unsigned long long) data->max,
209 (unsigned long long) data->align,
210 (unsigned long long) data->size, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
214static void pnp_free_port(struct pnp_port *port)
215{
216 struct pnp_port *next;
217
218 while (port) {
219 next = port->next;
220 kfree(port);
221 port = next;
222 }
223}
224
225static void pnp_free_irq(struct pnp_irq *irq)
226{
227 struct pnp_irq *next;
228
229 while (irq) {
230 next = irq->next;
231 kfree(irq);
232 irq = next;
233 }
234}
235
236static void pnp_free_dma(struct pnp_dma *dma)
237{
238 struct pnp_dma *next;
239
240 while (dma) {
241 next = dma->next;
242 kfree(dma);
243 dma = next;
244 }
245}
246
247static void pnp_free_mem(struct pnp_mem *mem)
248{
249 struct pnp_mem *next;
250
251 while (mem) {
252 next = mem->next;
253 kfree(mem);
254 mem = next;
255 }
256}
257
258void pnp_free_option(struct pnp_option *option)
259{
260 struct pnp_option *next;
261
262 while (option) {
263 next = option->next;
264 pnp_free_port(option->port);
265 pnp_free_irq(option->irq);
266 pnp_free_dma(option->dma);
267 pnp_free_mem(option->mem);
268 kfree(option);
269 option = next;
270 }
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/*
274 * resource validity checking
275 */
276
277#define length(start, end) (*(end) - *(start) + 1)
278
279/* Two ranges conflict if one doesn't end before the other starts */
280#define ranged_conflict(starta, enda, startb, endb) \
281 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
282
283#define cannot_compare(flags) \
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600284((flags) & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600286int pnp_check_port(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600288 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600290 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700291 resource_size_t *port, *end, *tport, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700292
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600293 port = &res->start;
294 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600297 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 1;
299
300 /* check if the resource is already in use, skip if the
301 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700302 if (!dev->active) {
303 if (__check_region(&ioport_resource, *port, length(port, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305 }
306
307 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600308 for (i = 0; i < 8; i++) {
309 int rport = pnp_reserve_io[i << 1];
310 int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700311 if (ranged_conflict(port, end, &rport, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313 }
314
315 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600316 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
317 if (tres != res && tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600318 tport = &tres->start;
319 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700320 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322 }
323 }
324
325 /* check for conflicts with other pnp devices */
326 pnp_for_each_dev(tdev) {
327 if (tdev == dev)
328 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600329 for (i = 0;
330 (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
331 i++) {
332 if (tres->flags & IORESOURCE_IO) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600333 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600335 tport = &tres->start;
336 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700337 if (ranged_conflict(port, end, tport, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339 }
340 }
341 }
342
343 return 1;
344}
345
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600346int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600348 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600350 struct resource *tres;
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700351 resource_size_t *addr, *end, *taddr, *tend;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700352
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600353 addr = &res->start;
354 end = &res->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600357 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 1;
359
360 /* check if the resource is already in use, skip if the
361 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700362 if (!dev->active) {
363 if (check_mem_region(*addr, length(addr, end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365 }
366
367 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600368 for (i = 0; i < 8; i++) {
369 int raddr = pnp_reserve_mem[i << 1];
370 int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700371 if (ranged_conflict(addr, end, &raddr, &rend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373 }
374
375 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600376 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
377 if (tres != res && tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600378 taddr = &tres->start;
379 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700380 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
382 }
383 }
384
385 /* check for conflicts with other pnp devices */
386 pnp_for_each_dev(tdev) {
387 if (tdev == dev)
388 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600389 for (i = 0;
390 (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
391 i++) {
392 if (tres->flags & IORESOURCE_MEM) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600393 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600395 taddr = &tres->start;
396 tend = &tres->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700397 if (ranged_conflict(addr, end, taddr, tend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399 }
400 }
401 }
402
403 return 1;
404}
405
David Howells7d12e782006-10-05 14:55:46 +0100406static irqreturn_t pnp_test_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 return IRQ_HANDLED;
409}
410
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600411int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600413 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600415 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600416 resource_size_t *irq;
417
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600418 irq = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600421 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 1;
423
424 /* check if the resource is valid */
425 if (*irq < 0 || *irq > 15)
426 return 0;
427
428 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600429 for (i = 0; i < 16; i++) {
430 if (pnp_reserve_irq[i] == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432 }
433
434 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600435 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
436 if (tres != res && tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600437 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439 }
440 }
441
442#ifdef CONFIG_PCI
443 /* check if the resource is being used by a pci device */
444 {
445 struct pci_dev *pci = NULL;
446 for_each_pci_dev(pci) {
Julia Lawall8ea50a32007-11-28 16:21:36 -0800447 if (pci->irq == *irq) {
448 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
Julia Lawall8ea50a32007-11-28 16:21:36 -0800450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453#endif
454
455 /* check if the resource is already in use, skip if the
456 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700457 if (!dev->active) {
Andrew Morton0cadaf42006-07-01 04:36:37 -0700458 if (request_irq(*irq, pnp_test_handler,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700459 IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461 free_irq(*irq, NULL);
462 }
463
464 /* check for conflicts with other pnp devices */
465 pnp_for_each_dev(tdev) {
466 if (tdev == dev)
467 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600468 for (i = 0;
469 (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
470 i++) {
471 if (tres->flags & IORESOURCE_IRQ) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600472 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600474 if (tres->start == *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476 }
477 }
478 }
479
480 return 1;
481}
482
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600483int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485#ifndef CONFIG_IA64
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600486 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct pnp_dev *tdev;
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600488 struct resource *tres;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600489 resource_size_t *dma;
490
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600491 dma = &res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* if the resource doesn't exist, don't complain about it */
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600494 if (cannot_compare(res->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 1;
496
497 /* check if the resource is valid */
498 if (*dma < 0 || *dma == 4 || *dma > 7)
499 return 0;
500
501 /* check if the resource is reserved */
Bjorn Helgaasecfa9352008-04-28 16:34:17 -0600502 for (i = 0; i < 8; i++) {
503 if (pnp_reserve_dma[i] == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505 }
506
507 /* check for internal conflicts */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600508 for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
509 if (tres != res && tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600510 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
512 }
513 }
514
515 /* check if the resource is already in use, skip if the
516 * device is active because it itself may be in use */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700517 if (!dev->active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (request_dma(*dma, "pnp"))
519 return 0;
520 free_dma(*dma);
521 }
522
523 /* check for conflicts with other pnp devices */
524 pnp_for_each_dev(tdev) {
525 if (tdev == dev)
526 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -0600527 for (i = 0;
528 (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
529 i++) {
530 if (tres->flags & IORESOURCE_DMA) {
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600531 if (cannot_compare(tres->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 continue;
Bjorn Helgaas30c016a2008-04-28 16:34:19 -0600533 if (tres->start == *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535 }
536 }
537 }
538
539 return 1;
540#else
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700541 /* IA64 does not have legacy DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
543#endif
544}
545
Bjorn Helgaas940e98d2008-06-27 16:56:54 -0600546int pnp_resource_type(struct resource *res)
547{
548 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
549 IORESOURCE_IRQ | IORESOURCE_DMA);
550}
551
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600552struct resource *pnp_get_resource(struct pnp_dev *dev,
553 unsigned int type, unsigned int num)
554{
555 struct pnp_resource *pnp_res;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600556 struct resource *res;
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600557
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600558 list_for_each_entry(pnp_res, &dev->resources, list) {
559 res = &pnp_res->res;
560 if (pnp_resource_type(res) == type && num-- == 0)
561 return res;
562 }
Bjorn Helgaas0a977f12008-04-28 16:34:31 -0600563 return NULL;
564}
Bjorn Helgaasb90eca02008-04-28 16:34:14 -0600565EXPORT_SYMBOL(pnp_get_resource);
566
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600567static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600568{
569 struct pnp_resource *pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600570
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600571 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
572 if (!pnp_res)
573 return NULL;
574
575 list_add_tail(&pnp_res->list, &dev->resources);
576 return pnp_res;
Bjorn Helgaasa50b6d72008-04-28 16:34:33 -0600577}
578
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600579struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
580 int flags)
581{
582 struct pnp_resource *pnp_res;
583 struct resource *res;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600584
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600585 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600586 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600587 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600588 return NULL;
589 }
590
591 res = &pnp_res->res;
592 res->flags = IORESOURCE_IRQ | flags;
593 res->start = irq;
594 res->end = irq;
595
596 dev_dbg(&dev->dev, " add irq %d flags %#x\n", irq, flags);
597 return pnp_res;
598}
599
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600600struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
601 int flags)
602{
603 struct pnp_resource *pnp_res;
604 struct resource *res;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600605
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600606 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600607 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600608 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600609 return NULL;
610 }
611
612 res = &pnp_res->res;
613 res->flags = IORESOURCE_DMA | flags;
614 res->start = dma;
615 res->end = dma;
616
617 dev_dbg(&dev->dev, " add dma %d flags %#x\n", dma, flags);
618 return pnp_res;
619}
620
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600621struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
622 resource_size_t start,
623 resource_size_t end, int flags)
624{
625 struct pnp_resource *pnp_res;
626 struct resource *res;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600627
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600628 pnp_res = pnp_new_resource(dev);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600629 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600630 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
631 (unsigned long long) start,
632 (unsigned long long) end);
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600633 return NULL;
634 }
635
636 res = &pnp_res->res;
637 res->flags = IORESOURCE_IO | flags;
638 res->start = start;
639 res->end = end;
640
641 dev_dbg(&dev->dev, " add io %#llx-%#llx flags %#x\n",
642 (unsigned long long) start, (unsigned long long) end, flags);
643 return pnp_res;
644}
645
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600646struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
647 resource_size_t start,
648 resource_size_t end, int flags)
649{
650 struct pnp_resource *pnp_res;
651 struct resource *res;
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600652
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600653 pnp_res = pnp_new_resource(dev);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600654 if (!pnp_res) {
Bjorn Helgaas25d39c32008-06-27 16:56:59 -0600655 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
656 (unsigned long long) start,
657 (unsigned long long) end);
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600658 return NULL;
659 }
660
661 res = &pnp_res->res;
662 res->flags = IORESOURCE_MEM | flags;
663 res->start = start;
664 res->end = end;
665
666 dev_dbg(&dev->dev, " add mem %#llx-%#llx flags %#x\n",
667 (unsigned long long) start, (unsigned long long) end, flags);
668 return pnp_res;
669}
670
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600671static int pnp_possible_option(struct pnp_option *option, int type,
672 resource_size_t start, resource_size_t size)
673{
674 struct pnp_option *tmp;
675 struct pnp_port *port;
676 struct pnp_mem *mem;
677 struct pnp_irq *irq;
678 struct pnp_dma *dma;
679
680 if (!option)
681 return 0;
682
683 for (tmp = option; tmp; tmp = tmp->next) {
684 switch (type) {
685 case IORESOURCE_IO:
686 for (port = tmp->port; port; port = port->next) {
687 if (port->min == start && port->size == size)
688 return 1;
689 }
690 break;
691 case IORESOURCE_MEM:
692 for (mem = tmp->mem; mem; mem = mem->next) {
693 if (mem->min == start && mem->size == size)
694 return 1;
695 }
696 break;
697 case IORESOURCE_IRQ:
698 for (irq = tmp->irq; irq; irq = irq->next) {
699 if (start < PNP_IRQ_NR &&
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600700 test_bit(start, irq->map.bits))
Bjorn Helgaas57fd51a2008-06-27 16:57:01 -0600701 return 1;
702 }
703 break;
704 case IORESOURCE_DMA:
705 for (dma = tmp->dma; dma; dma = dma->next) {
706 if (dma->map & (1 << start))
707 return 1;
708 }
709 break;
710 }
711 }
712
713 return 0;
714}
715
716/*
717 * Determine whether the specified resource is a possible configuration
718 * for this device.
719 */
720int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
721 resource_size_t size)
722{
723 if (pnp_possible_option(dev->independent, type, start, size))
724 return 1;
725
726 if (pnp_possible_option(dev->dependent, type, start, size))
727 return 1;
728
729 return 0;
730}
731EXPORT_SYMBOL(pnp_possible_config);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/* format is: pnp_reserve_irq=irq1[,irq2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static int __init pnp_setup_reserve_irq(char *str)
735{
736 int i;
737
738 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700739 if (get_option(&str, &pnp_reserve_irq[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 return 1;
742}
743
744__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
745
746/* format is: pnp_reserve_dma=dma1[,dma2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static int __init pnp_setup_reserve_dma(char *str)
748{
749 int i;
750
751 for (i = 0; i < 8; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700752 if (get_option(&str, &pnp_reserve_dma[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 return 1;
755}
756
757__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
758
759/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static int __init pnp_setup_reserve_io(char *str)
761{
762 int i;
763
764 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700765 if (get_option(&str, &pnp_reserve_io[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 return 1;
768}
769
770__setup("pnp_reserve_io=", pnp_setup_reserve_io);
771
772/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static int __init pnp_setup_reserve_mem(char *str)
774{
775 int i;
776
777 for (i = 0; i < 16; i++)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700778 if (get_option(&str, &pnp_reserve_mem[i]) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 return 1;
781}
782
783__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);