Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * resource.c - Contains functions for registering and analyzing resource information |
| 3 | * |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 4 | * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright 2003 Adam Belay <ambx1@neo.rr.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/dma.h> |
| 14 | #include <asm/irq.h> |
| 15 | #include <linux/pci.h> |
| 16 | #include <linux/ioport.h> |
| 17 | #include <linux/init.h> |
| 18 | |
| 19 | #include <linux/pnp.h> |
| 20 | #include "base.h" |
| 21 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 22 | static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */ |
| 23 | static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */ |
| 24 | static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */ |
| 25 | static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * option registration |
| 29 | */ |
| 30 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 31 | static struct pnp_option *pnp_build_option(int priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option)); |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | if (!option) |
| 36 | return NULL; |
| 37 | |
| 38 | option->priority = priority & 0xff; |
| 39 | /* make sure the priority is valid */ |
| 40 | if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL) |
| 41 | option->priority = PNP_RES_PRIORITY_INVALID; |
| 42 | |
| 43 | return option; |
| 44 | } |
| 45 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 46 | struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | struct pnp_option *option; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED); |
| 51 | |
| 52 | /* this should never happen but if it does we'll try to continue */ |
| 53 | if (dev->independent) |
Bjorn Helgaas | a05d078 | 2007-10-16 23:31:10 -0700 | [diff] [blame] | 54 | dev_err(&dev->dev, "independent resource already registered\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | dev->independent = option; |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 56 | |
| 57 | dev_dbg(&dev->dev, "new independent option\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return option; |
| 59 | } |
| 60 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 61 | struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev, |
| 62 | int priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | struct pnp_option *option; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | option = pnp_build_option(priority); |
| 67 | |
| 68 | if (dev->dependent) { |
| 69 | struct pnp_option *parent = dev->dependent; |
| 70 | while (parent->next) |
| 71 | parent = parent->next; |
| 72 | parent->next = option; |
| 73 | } else |
| 74 | dev->dependent = option; |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 75 | |
| 76 | dev_dbg(&dev->dev, "new dependent option (priority %#x)\n", priority); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return option; |
| 78 | } |
| 79 | |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 80 | int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option, |
| 81 | struct pnp_irq *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | struct pnp_irq *ptr; |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 84 | #ifdef DEBUG |
| 85 | char buf[PNP_IRQ_NR]; /* hex-encoded, so this is overkill but safe */ |
| 86 | #endif |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ptr = option->irq; |
| 89 | while (ptr && ptr->next) |
| 90 | ptr = ptr->next; |
| 91 | if (ptr) |
| 92 | ptr->next = data; |
| 93 | else |
| 94 | option->irq = data; |
| 95 | |
| 96 | #ifdef CONFIG_PCI |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < 16; i++) |
| 101 | if (test_bit(i, data->map)) |
David Shaohua Li | c9c3e45 | 2005-04-01 00:07:31 -0500 | [diff] [blame] | 102 | pcibios_penalize_isa_irq(i, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | #endif |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 105 | |
| 106 | #ifdef DEBUG |
| 107 | bitmap_scnprintf(buf, sizeof(buf), data->map, PNP_IRQ_NR); |
| 108 | dev_dbg(&dev->dev, " irq bitmask %s flags %#x\n", buf, |
| 109 | data->flags); |
| 110 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 114 | int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option, |
| 115 | struct pnp_dma *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | struct pnp_dma *ptr; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | ptr = option->dma; |
| 120 | while (ptr && ptr->next) |
| 121 | ptr = ptr->next; |
| 122 | if (ptr) |
| 123 | ptr->next = data; |
| 124 | else |
| 125 | option->dma = data; |
| 126 | |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 127 | dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map, |
| 128 | data->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 132 | int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option, |
| 133 | struct pnp_port *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | struct pnp_port *ptr; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | ptr = option->port; |
| 138 | while (ptr && ptr->next) |
| 139 | ptr = ptr->next; |
| 140 | if (ptr) |
| 141 | ptr->next = data; |
| 142 | else |
| 143 | option->port = data; |
| 144 | |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 145 | dev_dbg(&dev->dev, " io " |
| 146 | "min %#x max %#x align %d size %d flags %#x\n", |
| 147 | data->min, data->max, data->align, data->size, data->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 151 | int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option, |
| 152 | struct pnp_mem *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | struct pnp_mem *ptr; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | ptr = option->mem; |
| 157 | while (ptr && ptr->next) |
| 158 | ptr = ptr->next; |
| 159 | if (ptr) |
| 160 | ptr->next = data; |
| 161 | else |
| 162 | option->mem = data; |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 163 | |
| 164 | dev_dbg(&dev->dev, " mem " |
| 165 | "min %#x max %#x align %d size %d flags %#x\n", |
| 166 | data->min, data->max, data->align, data->size, data->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static 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 | |
| 181 | static 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 | |
| 192 | static 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 | |
| 203 | static 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 | |
| 214 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* |
| 230 | * resource validity checking |
| 231 | */ |
| 232 | |
| 233 | #define length(start, end) (*(end) - *(start) + 1) |
| 234 | |
| 235 | /* Two ranges conflict if one doesn't end before the other starts */ |
| 236 | #define ranged_conflict(starta, enda, startb, endb) \ |
| 237 | !((*(enda) < *(startb)) || (*(endb) < *(starta))) |
| 238 | |
| 239 | #define cannot_compare(flags) \ |
| 240 | ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED)) |
| 241 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 242 | int pnp_check_port(struct pnp_dev *dev, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 244 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | struct pnp_dev *tdev; |
Greg Kroah-Hartman | b60ba83 | 2006-06-12 17:07:07 -0700 | [diff] [blame] | 246 | resource_size_t *port, *end, *tport, *tend; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | port = &dev->res.port_resource[idx].start; |
| 249 | end = &dev->res.port_resource[idx].end; |
| 250 | |
| 251 | /* if the resource doesn't exist, don't complain about it */ |
| 252 | if (cannot_compare(dev->res.port_resource[idx].flags)) |
| 253 | return 1; |
| 254 | |
| 255 | /* check if the resource is already in use, skip if the |
| 256 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 257 | if (!dev->active) { |
| 258 | if (__check_region(&ioport_resource, *port, length(port, end))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 263 | for (i = 0; i < 8; i++) { |
| 264 | int rport = pnp_reserve_io[i << 1]; |
| 265 | int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 266 | if (ranged_conflict(port, end, &rport, &rend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* check for internal conflicts */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 271 | for (i = 0; i < PNP_MAX_PORT && i != idx; i++) { |
| 272 | if (dev->res.port_resource[i].flags & IORESOURCE_IO) { |
| 273 | tport = &dev->res.port_resource[i].start; |
| 274 | tend = &dev->res.port_resource[i].end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 275 | if (ranged_conflict(port, end, tport, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* check for conflicts with other pnp devices */ |
| 281 | pnp_for_each_dev(tdev) { |
| 282 | if (tdev == dev) |
| 283 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 284 | for (i = 0; i < PNP_MAX_PORT; i++) { |
| 285 | if (tdev->res.port_resource[i].flags & IORESOURCE_IO) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 286 | if (cannot_compare |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 287 | (tdev->res.port_resource[i].flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 289 | tport = &tdev->res.port_resource[i].start; |
| 290 | tend = &tdev->res.port_resource[i].end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 291 | if (ranged_conflict(port, end, tport, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return 1; |
| 298 | } |
| 299 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 300 | int pnp_check_mem(struct pnp_dev *dev, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 302 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | struct pnp_dev *tdev; |
Greg Kroah-Hartman | b60ba83 | 2006-06-12 17:07:07 -0700 | [diff] [blame] | 304 | resource_size_t *addr, *end, *taddr, *tend; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | addr = &dev->res.mem_resource[idx].start; |
| 307 | end = &dev->res.mem_resource[idx].end; |
| 308 | |
| 309 | /* if the resource doesn't exist, don't complain about it */ |
| 310 | if (cannot_compare(dev->res.mem_resource[idx].flags)) |
| 311 | return 1; |
| 312 | |
| 313 | /* check if the resource is already in use, skip if the |
| 314 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 315 | if (!dev->active) { |
| 316 | if (check_mem_region(*addr, length(addr, end))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 321 | for (i = 0; i < 8; i++) { |
| 322 | int raddr = pnp_reserve_mem[i << 1]; |
| 323 | int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 324 | if (ranged_conflict(addr, end, &raddr, &rend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* check for internal conflicts */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 329 | for (i = 0; i < PNP_MAX_MEM && i != idx; i++) { |
| 330 | if (dev->res.mem_resource[i].flags & IORESOURCE_MEM) { |
| 331 | taddr = &dev->res.mem_resource[i].start; |
| 332 | tend = &dev->res.mem_resource[i].end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 333 | if (ranged_conflict(addr, end, taddr, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* check for conflicts with other pnp devices */ |
| 339 | pnp_for_each_dev(tdev) { |
| 340 | if (tdev == dev) |
| 341 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 342 | for (i = 0; i < PNP_MAX_MEM; i++) { |
| 343 | if (tdev->res.mem_resource[i].flags & IORESOURCE_MEM) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 344 | if (cannot_compare |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 345 | (tdev->res.mem_resource[i].flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 347 | taddr = &tdev->res.mem_resource[i].start; |
| 348 | tend = &tdev->res.mem_resource[i].end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 349 | if (ranged_conflict(addr, end, taddr, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return 1; |
| 356 | } |
| 357 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 358 | static irqreturn_t pnp_test_handler(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
| 360 | return IRQ_HANDLED; |
| 361 | } |
| 362 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 363 | int pnp_check_irq(struct pnp_dev *dev, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 365 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | struct pnp_dev *tdev; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 367 | resource_size_t *irq = &dev->res.irq_resource[idx].start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* if the resource doesn't exist, don't complain about it */ |
| 370 | if (cannot_compare(dev->res.irq_resource[idx].flags)) |
| 371 | return 1; |
| 372 | |
| 373 | /* check if the resource is valid */ |
| 374 | if (*irq < 0 || *irq > 15) |
| 375 | return 0; |
| 376 | |
| 377 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 378 | for (i = 0; i < 16; i++) { |
| 379 | if (pnp_reserve_irq[i] == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* check for internal conflicts */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 384 | for (i = 0; i < PNP_MAX_IRQ && i != idx; i++) { |
| 385 | if (dev->res.irq_resource[i].flags & IORESOURCE_IRQ) { |
| 386 | if (dev->res.irq_resource[i].start == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | #ifdef CONFIG_PCI |
| 392 | /* check if the resource is being used by a pci device */ |
| 393 | { |
| 394 | struct pci_dev *pci = NULL; |
| 395 | for_each_pci_dev(pci) { |
Julia Lawall | 8ea50a3 | 2007-11-28 16:21:36 -0800 | [diff] [blame] | 396 | if (pci->irq == *irq) { |
| 397 | pci_dev_put(pci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return 0; |
Julia Lawall | 8ea50a3 | 2007-11-28 16:21:36 -0800 | [diff] [blame] | 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | /* check if the resource is already in use, skip if the |
| 405 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 406 | if (!dev->active) { |
Andrew Morton | 0cadaf4 | 2006-07-01 04:36:37 -0700 | [diff] [blame] | 407 | if (request_irq(*irq, pnp_test_handler, |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 408 | IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | free_irq(*irq, NULL); |
| 411 | } |
| 412 | |
| 413 | /* check for conflicts with other pnp devices */ |
| 414 | pnp_for_each_dev(tdev) { |
| 415 | if (tdev == dev) |
| 416 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 417 | for (i = 0; i < PNP_MAX_IRQ; i++) { |
| 418 | if (tdev->res.irq_resource[i].flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 419 | if (cannot_compare |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 420 | (tdev->res.irq_resource[i].flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 422 | if ((tdev->res.irq_resource[i].start == *irq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return 1; |
| 429 | } |
| 430 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 431 | int pnp_check_dma(struct pnp_dev *dev, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | #ifndef CONFIG_IA64 |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 434 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | struct pnp_dev *tdev; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 436 | resource_size_t *dma = &dev->res.dma_resource[idx].start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* if the resource doesn't exist, don't complain about it */ |
| 439 | if (cannot_compare(dev->res.dma_resource[idx].flags)) |
| 440 | return 1; |
| 441 | |
| 442 | /* check if the resource is valid */ |
| 443 | if (*dma < 0 || *dma == 4 || *dma > 7) |
| 444 | return 0; |
| 445 | |
| 446 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 447 | for (i = 0; i < 8; i++) { |
| 448 | if (pnp_reserve_dma[i] == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* check for internal conflicts */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 453 | for (i = 0; i < PNP_MAX_DMA && i != idx; i++) { |
| 454 | if (dev->res.dma_resource[i].flags & IORESOURCE_DMA) { |
| 455 | if (dev->res.dma_resource[i].start == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* check if the resource is already in use, skip if the |
| 461 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 462 | if (!dev->active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (request_dma(*dma, "pnp")) |
| 464 | return 0; |
| 465 | free_dma(*dma); |
| 466 | } |
| 467 | |
| 468 | /* check for conflicts with other pnp devices */ |
| 469 | pnp_for_each_dev(tdev) { |
| 470 | if (tdev == dev) |
| 471 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 472 | for (i = 0; i < PNP_MAX_DMA; i++) { |
| 473 | if (tdev->res.dma_resource[i].flags & IORESOURCE_DMA) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 474 | if (cannot_compare |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 475 | (tdev->res.dma_resource[i].flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | continue; |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame^] | 477 | if ((tdev->res.dma_resource[i].start == *dma)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return 1; |
| 484 | #else |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 485 | /* IA64 does not have legacy DMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return 0; |
| 487 | #endif |
| 488 | } |
| 489 | |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 490 | struct resource *pnp_get_resource(struct pnp_dev *dev, |
| 491 | unsigned int type, unsigned int num) |
| 492 | { |
| 493 | struct pnp_resource_table *res = &dev->res; |
| 494 | |
| 495 | switch (type) { |
| 496 | case IORESOURCE_IO: |
| 497 | if (num >= PNP_MAX_PORT) |
| 498 | return NULL; |
| 499 | return &res->port_resource[num]; |
| 500 | case IORESOURCE_MEM: |
| 501 | if (num >= PNP_MAX_MEM) |
| 502 | return NULL; |
| 503 | return &res->mem_resource[num]; |
| 504 | case IORESOURCE_IRQ: |
| 505 | if (num >= PNP_MAX_IRQ) |
| 506 | return NULL; |
| 507 | return &res->irq_resource[num]; |
| 508 | case IORESOURCE_DMA: |
| 509 | if (num >= PNP_MAX_DMA) |
| 510 | return NULL; |
| 511 | return &res->dma_resource[num]; |
| 512 | } |
| 513 | return NULL; |
| 514 | } |
| 515 | EXPORT_SYMBOL(pnp_get_resource); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* format is: pnp_reserve_irq=irq1[,irq2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | static int __init pnp_setup_reserve_irq(char *str) |
| 519 | { |
| 520 | int i; |
| 521 | |
| 522 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 523 | if (get_option(&str, &pnp_reserve_irq[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | break; |
| 525 | return 1; |
| 526 | } |
| 527 | |
| 528 | __setup("pnp_reserve_irq=", pnp_setup_reserve_irq); |
| 529 | |
| 530 | /* format is: pnp_reserve_dma=dma1[,dma2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | static int __init pnp_setup_reserve_dma(char *str) |
| 532 | { |
| 533 | int i; |
| 534 | |
| 535 | for (i = 0; i < 8; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 536 | if (get_option(&str, &pnp_reserve_dma[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | break; |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | __setup("pnp_reserve_dma=", pnp_setup_reserve_dma); |
| 542 | |
| 543 | /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | static int __init pnp_setup_reserve_io(char *str) |
| 545 | { |
| 546 | int i; |
| 547 | |
| 548 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 549 | if (get_option(&str, &pnp_reserve_io[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | break; |
| 551 | return 1; |
| 552 | } |
| 553 | |
| 554 | __setup("pnp_reserve_io=", pnp_setup_reserve_io); |
| 555 | |
| 556 | /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | static int __init pnp_setup_reserve_mem(char *str) |
| 558 | { |
| 559 | int i; |
| 560 | |
| 561 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 562 | if (get_option(&str, &pnp_reserve_mem[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | break; |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | __setup("pnp_reserve_mem=", pnp_setup_reserve_mem); |