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 | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 242 | int pnp_check_port(struct pnp_dev *dev, struct resource *res) |
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; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 246 | struct resource *tres; |
Greg Kroah-Hartman | b60ba83 | 2006-06-12 17:07:07 -0700 | [diff] [blame] | 247 | resource_size_t *port, *end, *tport, *tend; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 248 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 249 | port = &res->start; |
| 250 | end = &res->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 253 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 258 | if (!dev->active) { |
| 259 | if (__check_region(&ioport_resource, *port, length(port, end))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 264 | 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 267 | if (ranged_conflict(port, end, &rport, &rend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 272 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) { |
| 273 | if (tres != res && tres->flags & IORESOURCE_IO) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 274 | tport = &tres->start; |
| 275 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 276 | if (ranged_conflict(port, end, tport, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | 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 Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 285 | for (i = 0; |
| 286 | (tres = pnp_get_resource(tdev, IORESOURCE_IO, i)); |
| 287 | i++) { |
| 288 | if (tres->flags & IORESOURCE_IO) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 289 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 291 | tport = &tres->start; |
| 292 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 293 | if (ranged_conflict(port, end, tport, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return 1; |
| 300 | } |
| 301 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 302 | int pnp_check_mem(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 304 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 306 | struct resource *tres; |
Greg Kroah-Hartman | b60ba83 | 2006-06-12 17:07:07 -0700 | [diff] [blame] | 307 | resource_size_t *addr, *end, *taddr, *tend; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 308 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 309 | addr = &res->start; |
| 310 | end = &res->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 313 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 318 | if (!dev->active) { |
| 319 | if (check_mem_region(*addr, length(addr, end))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 324 | 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 327 | if (ranged_conflict(addr, end, &raddr, &rend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 332 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) { |
| 333 | if (tres != res && tres->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 334 | taddr = &tres->start; |
| 335 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 336 | if (ranged_conflict(addr, end, taddr, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | 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 Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 345 | for (i = 0; |
| 346 | (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i)); |
| 347 | i++) { |
| 348 | if (tres->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 349 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 351 | taddr = &tres->start; |
| 352 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 353 | if (ranged_conflict(addr, end, taddr, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return 1; |
| 360 | } |
| 361 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 362 | static irqreturn_t pnp_test_handler(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | return IRQ_HANDLED; |
| 365 | } |
| 366 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 367 | int pnp_check_irq(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 369 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 371 | struct resource *tres; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 372 | resource_size_t *irq; |
| 373 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 374 | irq = &res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 377 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | 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 Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 385 | for (i = 0; i < 16; i++) { |
| 386 | if (pnp_reserve_irq[i] == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 391 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) { |
| 392 | if (tres != res && tres->flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 393 | if (tres->start == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | 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 Lawall | 8ea50a3 | 2007-11-28 16:21:36 -0800 | [diff] [blame] | 403 | if (pci->irq == *irq) { |
| 404 | pci_dev_put(pci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return 0; |
Julia Lawall | 8ea50a3 | 2007-11-28 16:21:36 -0800 | [diff] [blame] | 406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 413 | if (!dev->active) { |
Andrew Morton | 0cadaf4 | 2006-07-01 04:36:37 -0700 | [diff] [blame] | 414 | if (request_irq(*irq, pnp_test_handler, |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 415 | IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | 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 Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 424 | for (i = 0; |
| 425 | (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i)); |
| 426 | i++) { |
| 427 | if (tres->flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 428 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 430 | if (tres->start == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return 1; |
| 437 | } |
| 438 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 439 | int pnp_check_dma(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | #ifndef CONFIG_IA64 |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 442 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 444 | struct resource *tres; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 445 | resource_size_t *dma; |
| 446 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 447 | dma = &res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 450 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | 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 Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 458 | for (i = 0; i < 8; i++) { |
| 459 | if (pnp_reserve_dma[i] == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 464 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) { |
| 465 | if (tres != res && tres->flags & IORESOURCE_DMA) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 466 | if (tres->start == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 473 | if (!dev->active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | 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 Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 483 | for (i = 0; |
| 484 | (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i)); |
| 485 | i++) { |
| 486 | if (tres->flags & IORESOURCE_DMA) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 487 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 489 | if (tres->start == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return 1; |
| 496 | #else |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 497 | /* IA64 does not have legacy DMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return 0; |
| 499 | #endif |
| 500 | } |
| 501 | |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 502 | struct pnp_resource *pnp_get_pnp_resource(struct pnp_dev *dev, |
| 503 | unsigned int type, unsigned int num) |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 504 | { |
Bjorn Helgaas | 02d83b5 | 2008-04-28 16:34:28 -0600 | [diff] [blame] | 505 | struct pnp_resource_table *res = dev->res; |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 506 | |
| 507 | switch (type) { |
| 508 | case IORESOURCE_IO: |
| 509 | if (num >= PNP_MAX_PORT) |
| 510 | return NULL; |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 511 | return &res->port[num]; |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 512 | case IORESOURCE_MEM: |
| 513 | if (num >= PNP_MAX_MEM) |
| 514 | return NULL; |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 515 | return &res->mem[num]; |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 516 | case IORESOURCE_IRQ: |
| 517 | if (num >= PNP_MAX_IRQ) |
| 518 | return NULL; |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 519 | return &res->irq[num]; |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 520 | case IORESOURCE_DMA: |
| 521 | if (num >= PNP_MAX_DMA) |
| 522 | return NULL; |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 523 | return &res->dma[num]; |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 524 | } |
| 525 | return NULL; |
| 526 | } |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 527 | |
| 528 | struct resource *pnp_get_resource(struct pnp_dev *dev, |
| 529 | unsigned int type, unsigned int num) |
| 530 | { |
| 531 | struct pnp_resource *pnp_res; |
| 532 | |
| 533 | pnp_res = pnp_get_pnp_resource(dev, type, num); |
| 534 | if (pnp_res) |
| 535 | return &pnp_res->res; |
| 536 | |
| 537 | return NULL; |
| 538 | } |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 539 | EXPORT_SYMBOL(pnp_get_resource); |
| 540 | |
Bjorn Helgaas | a50b6d7 | 2008-04-28 16:34:33 -0600 | [diff] [blame^] | 541 | static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev, int type) |
| 542 | { |
| 543 | struct pnp_resource *pnp_res; |
| 544 | int i; |
| 545 | |
| 546 | switch (type) { |
| 547 | case IORESOURCE_IO: |
| 548 | for (i = 0; i < PNP_MAX_PORT; i++) { |
| 549 | pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, i); |
| 550 | if (pnp_res && !pnp_resource_valid(&pnp_res->res)) |
| 551 | return pnp_res; |
| 552 | } |
| 553 | break; |
| 554 | case IORESOURCE_MEM: |
| 555 | for (i = 0; i < PNP_MAX_MEM; i++) { |
| 556 | pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, i); |
| 557 | if (pnp_res && !pnp_resource_valid(&pnp_res->res)) |
| 558 | return pnp_res; |
| 559 | } |
| 560 | break; |
| 561 | case IORESOURCE_IRQ: |
| 562 | for (i = 0; i < PNP_MAX_IRQ; i++) { |
| 563 | pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, i); |
| 564 | if (pnp_res && !pnp_resource_valid(&pnp_res->res)) |
| 565 | return pnp_res; |
| 566 | } |
| 567 | break; |
| 568 | case IORESOURCE_DMA: |
| 569 | for (i = 0; i < PNP_MAX_DMA; i++) { |
| 570 | pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, i); |
| 571 | if (pnp_res && !pnp_resource_valid(&pnp_res->res)) |
| 572 | return pnp_res; |
| 573 | } |
| 574 | break; |
| 575 | } |
| 576 | return NULL; |
| 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* format is: pnp_reserve_irq=irq1[,irq2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | static int __init pnp_setup_reserve_irq(char *str) |
| 581 | { |
| 582 | int i; |
| 583 | |
| 584 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 585 | if (get_option(&str, &pnp_reserve_irq[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | break; |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | __setup("pnp_reserve_irq=", pnp_setup_reserve_irq); |
| 591 | |
| 592 | /* format is: pnp_reserve_dma=dma1[,dma2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | static int __init pnp_setup_reserve_dma(char *str) |
| 594 | { |
| 595 | int i; |
| 596 | |
| 597 | for (i = 0; i < 8; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 598 | if (get_option(&str, &pnp_reserve_dma[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | break; |
| 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | __setup("pnp_reserve_dma=", pnp_setup_reserve_dma); |
| 604 | |
| 605 | /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | static int __init pnp_setup_reserve_io(char *str) |
| 607 | { |
| 608 | int i; |
| 609 | |
| 610 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 611 | if (get_option(&str, &pnp_reserve_io[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | break; |
| 613 | return 1; |
| 614 | } |
| 615 | |
| 616 | __setup("pnp_reserve_io=", pnp_setup_reserve_io); |
| 617 | |
| 618 | /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | static int __init pnp_setup_reserve_mem(char *str) |
| 620 | { |
| 621 | int i; |
| 622 | |
| 623 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 624 | if (get_option(&str, &pnp_reserve_mem[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | break; |
| 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | __setup("pnp_reserve_mem=", pnp_setup_reserve_mem); |