Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * The initial developer of the original code is David A. Hinds |
| 9 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds |
| 10 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. |
| 11 | * |
| 12 | * (C) 1999 David A. Hinds |
| 13 | */ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/timer.h> |
| 25 | #include <linux/pci.h> |
| 26 | #include <linux/device.h> |
| 27 | |
| 28 | #include <asm/irq.h> |
| 29 | #include <asm/io.h> |
| 30 | |
| 31 | #include <pcmcia/cs_types.h> |
| 32 | #include <pcmcia/ss.h> |
| 33 | #include <pcmcia/cs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <pcmcia/cistpl.h> |
| 35 | #include "cs_internal.h" |
| 36 | |
| 37 | MODULE_AUTHOR("David A. Hinds, Dominik Brodowski"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
| 40 | /* Parameters that can be set with 'insmod' */ |
| 41 | |
| 42 | #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444) |
| 43 | |
| 44 | INT_MODULE_PARM(probe_mem, 1); /* memory probe? */ |
| 45 | #ifdef CONFIG_PCMCIA_PROBE |
| 46 | INT_MODULE_PARM(probe_io, 1); /* IO port probe? */ |
| 47 | INT_MODULE_PARM(mem_limit, 0x10000); |
| 48 | #endif |
| 49 | |
| 50 | /* for io_db and mem_db */ |
| 51 | struct resource_map { |
| 52 | u_long base, num; |
| 53 | struct resource_map *next; |
| 54 | }; |
| 55 | |
| 56 | struct socket_data { |
| 57 | struct resource_map mem_db; |
| 58 | struct resource_map io_db; |
| 59 | unsigned int rsrc_mem_probe; |
| 60 | }; |
| 61 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 62 | static DEFINE_MUTEX(rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define MEM_PROBE_LOW (1 << 0) |
| 64 | #define MEM_PROBE_HIGH (1 << 1) |
| 65 | |
| 66 | |
| 67 | /*====================================================================== |
| 68 | |
| 69 | Linux resource management extensions |
| 70 | |
| 71 | ======================================================================*/ |
| 72 | |
| 73 | static struct resource * |
Greg Kroah-Hartman | 2427ddd | 2006-06-12 17:07:52 -0700 | [diff] [blame] | 74 | make_resource(resource_size_t b, resource_size_t n, int flags, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Dominik Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 76 | struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | if (res) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | res->name = name; |
| 80 | res->start = b; |
| 81 | res->end = b + n - 1; |
| 82 | res->flags = flags; |
| 83 | } |
| 84 | return res; |
| 85 | } |
| 86 | |
| 87 | static struct resource * |
Greg Kroah-Hartman | 2427ddd | 2006-06-12 17:07:52 -0700 | [diff] [blame] | 88 | claim_region(struct pcmcia_socket *s, resource_size_t base, |
| 89 | resource_size_t size, int type, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | struct resource *res, *parent; |
| 92 | |
| 93 | parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource; |
| 94 | res = make_resource(base, size, type | IORESOURCE_BUSY, name); |
| 95 | |
| 96 | if (res) { |
| 97 | #ifdef CONFIG_PCI |
| 98 | if (s && s->cb_dev) |
| 99 | parent = pci_find_parent_resource(s->cb_dev, res); |
| 100 | #endif |
| 101 | if (!parent || request_resource(parent, res)) { |
| 102 | kfree(res); |
| 103 | res = NULL; |
| 104 | } |
| 105 | } |
| 106 | return res; |
| 107 | } |
| 108 | |
| 109 | static void free_region(struct resource *res) |
| 110 | { |
| 111 | if (res) { |
| 112 | release_resource(res); |
| 113 | kfree(res); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /*====================================================================== |
| 118 | |
| 119 | These manage the internal databases of available resources. |
| 120 | |
| 121 | ======================================================================*/ |
| 122 | |
| 123 | static int add_interval(struct resource_map *map, u_long base, u_long num) |
| 124 | { |
| 125 | struct resource_map *p, *q; |
| 126 | |
| 127 | for (p = map; ; p = p->next) { |
| 128 | if ((p != map) && (p->base+p->num-1 >= base)) |
| 129 | return -1; |
| 130 | if ((p->next == map) || (p->next->base > base+num-1)) |
| 131 | break; |
| 132 | } |
| 133 | q = kmalloc(sizeof(struct resource_map), GFP_KERNEL); |
| 134 | if (!q) return CS_OUT_OF_RESOURCE; |
| 135 | q->base = base; q->num = num; |
| 136 | q->next = p->next; p->next = q; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame^] | 137 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /*====================================================================*/ |
| 141 | |
| 142 | static int sub_interval(struct resource_map *map, u_long base, u_long num) |
| 143 | { |
| 144 | struct resource_map *p, *q; |
| 145 | |
| 146 | for (p = map; ; p = q) { |
| 147 | q = p->next; |
| 148 | if (q == map) |
| 149 | break; |
| 150 | if ((q->base+q->num > base) && (base+num > q->base)) { |
| 151 | if (q->base >= base) { |
| 152 | if (q->base+q->num <= base+num) { |
| 153 | /* Delete whole block */ |
| 154 | p->next = q->next; |
| 155 | kfree(q); |
| 156 | /* don't advance the pointer yet */ |
| 157 | q = p; |
| 158 | } else { |
| 159 | /* Cut off bit from the front */ |
| 160 | q->num = q->base + q->num - base - num; |
| 161 | q->base = base + num; |
| 162 | } |
| 163 | } else if (q->base+q->num <= base+num) { |
| 164 | /* Cut off bit from the end */ |
| 165 | q->num = base - q->base; |
| 166 | } else { |
| 167 | /* Split the block into two pieces */ |
| 168 | p = kmalloc(sizeof(struct resource_map), GFP_KERNEL); |
| 169 | if (!p) return CS_OUT_OF_RESOURCE; |
| 170 | p->base = base+num; |
| 171 | p->num = q->base+q->num - p->base; |
| 172 | q->num = base - q->base; |
| 173 | p->next = q->next ; q->next = p; |
| 174 | } |
| 175 | } |
| 176 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame^] | 177 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /*====================================================================== |
| 181 | |
| 182 | These routines examine a region of IO or memory addresses to |
| 183 | determine what ranges might be genuinely available. |
| 184 | |
| 185 | ======================================================================*/ |
| 186 | |
| 187 | #ifdef CONFIG_PCMCIA_PROBE |
Olof Johansson | 906da80 | 2008-02-04 22:27:35 -0800 | [diff] [blame] | 188 | static void do_io_probe(struct pcmcia_socket *s, unsigned int base, |
| 189 | unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | struct resource *res; |
| 192 | struct socket_data *s_data = s->resource_data; |
Olof Johansson | 906da80 | 2008-02-04 22:27:35 -0800 | [diff] [blame] | 193 | unsigned int i, j, bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | int any; |
| 195 | u_char *b, hole, most; |
| 196 | |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 197 | dev_printk(KERN_INFO, &s->dev, "cs: IO port probe %#x-%#x:", |
| 198 | base, base+num-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* First, what does a floating port look like? */ |
Dominik Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 201 | b = kzalloc(256, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (!b) { |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 203 | dev_printk(KERN_ERR, &s->dev, |
| 204 | "do_io_probe: unable to kmalloc 256 bytes"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | for (i = base, most = 0; i < base+num; i += 8) { |
| 208 | res = claim_region(NULL, i, 8, IORESOURCE_IO, "PCMCIA IO probe"); |
| 209 | if (!res) |
| 210 | continue; |
| 211 | hole = inb(i); |
| 212 | for (j = 1; j < 8; j++) |
| 213 | if (inb(i+j) != hole) break; |
| 214 | free_region(res); |
| 215 | if ((j == 8) && (++b[hole] > b[most])) |
| 216 | most = hole; |
| 217 | if (b[most] == 127) break; |
| 218 | } |
| 219 | kfree(b); |
| 220 | |
| 221 | bad = any = 0; |
| 222 | for (i = base; i < base+num; i += 8) { |
| 223 | res = claim_region(NULL, i, 8, IORESOURCE_IO, "PCMCIA IO probe"); |
| 224 | if (!res) |
| 225 | continue; |
| 226 | for (j = 0; j < 8; j++) |
| 227 | if (inb(i+j) != most) break; |
| 228 | free_region(res); |
| 229 | if (j < 8) { |
| 230 | if (!any) |
| 231 | printk(" excluding"); |
| 232 | if (!bad) |
| 233 | bad = any = i; |
| 234 | } else { |
| 235 | if (bad) { |
| 236 | sub_interval(&s_data->io_db, bad, i-bad); |
Olof Johansson | 906da80 | 2008-02-04 22:27:35 -0800 | [diff] [blame] | 237 | printk(" %#x-%#x", bad, i-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | bad = 0; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if (bad) { |
| 243 | if ((num > 16) && (bad == base) && (i == base+num)) { |
| 244 | printk(" nothing: probe failed.\n"); |
| 245 | return; |
| 246 | } else { |
| 247 | sub_interval(&s_data->io_db, bad, i-bad); |
Olof Johansson | 906da80 | 2008-02-04 22:27:35 -0800 | [diff] [blame] | 248 | printk(" %#x-%#x", bad, i-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | printk(any ? "\n" : " clean.\n"); |
| 253 | } |
| 254 | #endif |
| 255 | |
| 256 | /*====================================================================== |
| 257 | |
| 258 | This is tricky... when we set up CIS memory, we try to validate |
| 259 | the memory window space allocations. |
| 260 | |
| 261 | ======================================================================*/ |
| 262 | |
| 263 | /* Validation function for cards with a valid CIS */ |
Dominik Brodowski | c5081d5 | 2008-06-19 20:12:34 +0200 | [diff] [blame] | 264 | static int readable(struct pcmcia_socket *s, struct resource *res, |
| 265 | unsigned int *count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
| 267 | int ret = -1; |
| 268 | |
| 269 | s->cis_mem.res = res; |
| 270 | s->cis_virt = ioremap(res->start, s->map_size); |
| 271 | if (s->cis_virt) { |
Dominik Brodowski | c5081d5 | 2008-06-19 20:12:34 +0200 | [diff] [blame] | 272 | ret = pccard_validate_cis(s, BIND_FN_ALL, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* invalidate mapping and CIS cache */ |
| 274 | iounmap(s->cis_virt); |
| 275 | s->cis_virt = NULL; |
| 276 | destroy_cis_cache(s); |
| 277 | } |
| 278 | s->cis_mem.res = NULL; |
Dominik Brodowski | c5081d5 | 2008-06-19 20:12:34 +0200 | [diff] [blame] | 279 | if ((ret != 0) || (count == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | /* Validation function for simple memory cards */ |
| 285 | static int checksum(struct pcmcia_socket *s, struct resource *res) |
| 286 | { |
| 287 | pccard_mem_map map; |
| 288 | int i, a = 0, b = -1, d; |
| 289 | void __iomem *virt; |
| 290 | |
| 291 | virt = ioremap(res->start, s->map_size); |
| 292 | if (virt) { |
| 293 | map.map = 0; |
| 294 | map.flags = MAP_ACTIVE; |
| 295 | map.speed = 0; |
| 296 | map.res = res; |
| 297 | map.card_start = 0; |
| 298 | s->ops->set_mem_map(s, &map); |
| 299 | |
| 300 | /* Don't bother checking every word... */ |
| 301 | for (i = 0; i < s->map_size; i += 44) { |
| 302 | d = readl(virt+i); |
| 303 | a += d; |
| 304 | b &= d; |
| 305 | } |
| 306 | |
| 307 | map.flags = 0; |
| 308 | s->ops->set_mem_map(s, &map); |
| 309 | |
| 310 | iounmap(virt); |
| 311 | } |
| 312 | |
| 313 | return (b == -1) ? -1 : (a>>1); |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | cis_readable(struct pcmcia_socket *s, unsigned long base, unsigned long size) |
| 318 | { |
| 319 | struct resource *res1, *res2; |
Dominik Brodowski | c5081d5 | 2008-06-19 20:12:34 +0200 | [diff] [blame] | 320 | unsigned int info1, info2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | int ret = 0; |
| 322 | |
| 323 | res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "cs memory probe"); |
| 324 | res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM, "cs memory probe"); |
| 325 | |
| 326 | if (res1 && res2) { |
| 327 | ret = readable(s, res1, &info1); |
| 328 | ret += readable(s, res2, &info2); |
| 329 | } |
| 330 | |
| 331 | free_region(res2); |
| 332 | free_region(res1); |
| 333 | |
Dominik Brodowski | c5081d5 | 2008-06-19 20:12:34 +0200 | [diff] [blame] | 334 | return (ret == 2) && (info1 == info2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int |
| 338 | checksum_match(struct pcmcia_socket *s, unsigned long base, unsigned long size) |
| 339 | { |
| 340 | struct resource *res1, *res2; |
| 341 | int a = -1, b = -1; |
| 342 | |
| 343 | res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "cs memory probe"); |
| 344 | res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM, "cs memory probe"); |
| 345 | |
| 346 | if (res1 && res2) { |
| 347 | a = checksum(s, res1); |
| 348 | b = checksum(s, res2); |
| 349 | } |
| 350 | |
| 351 | free_region(res2); |
| 352 | free_region(res1); |
| 353 | |
| 354 | return (a == b) && (a >= 0); |
| 355 | } |
| 356 | |
| 357 | /*====================================================================== |
| 358 | |
| 359 | The memory probe. If the memory list includes a 64K-aligned block |
| 360 | below 1MB, we probe in 64K chunks, and as soon as we accumulate at |
| 361 | least mem_limit free space, we quit. |
| 362 | |
| 363 | ======================================================================*/ |
| 364 | |
| 365 | static int do_mem_probe(u_long base, u_long num, struct pcmcia_socket *s) |
| 366 | { |
| 367 | struct socket_data *s_data = s->resource_data; |
| 368 | u_long i, j, bad, fail, step; |
| 369 | |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 370 | dev_printk(KERN_INFO, &s->dev, "cs: memory probe 0x%06lx-0x%06lx:", |
| 371 | base, base+num-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | bad = fail = 0; |
| 373 | step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff); |
Dominik Brodowski | 2ad0a0a | 2005-06-27 16:28:58 -0700 | [diff] [blame] | 374 | /* don't allow too large steps */ |
| 375 | if (step > 0x800000) |
| 376 | step = 0x800000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* cis_readable wants to map 2x map_size */ |
| 378 | if (step < 2 * s->map_size) |
| 379 | step = 2 * s->map_size; |
| 380 | for (i = j = base; i < base+num; i = j + step) { |
| 381 | if (!fail) { |
| 382 | for (j = i; j < base+num; j += step) { |
| 383 | if (cis_readable(s, j, step)) |
| 384 | break; |
| 385 | } |
| 386 | fail = ((i == base) && (j == base+num)); |
| 387 | } |
| 388 | if (fail) { |
| 389 | for (j = i; j < base+num; j += 2*step) |
| 390 | if (checksum_match(s, j, step) && |
| 391 | checksum_match(s, j + step, step)) |
| 392 | break; |
| 393 | } |
| 394 | if (i != j) { |
| 395 | if (!bad) printk(" excluding"); |
| 396 | printk(" %#05lx-%#05lx", i, j-1); |
| 397 | sub_interval(&s_data->mem_db, i, j-i); |
| 398 | bad += j-i; |
| 399 | } |
| 400 | } |
| 401 | printk(bad ? "\n" : " clean.\n"); |
| 402 | return (num - bad); |
| 403 | } |
| 404 | |
| 405 | #ifdef CONFIG_PCMCIA_PROBE |
| 406 | |
| 407 | static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s) |
| 408 | { |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 409 | struct socket_data *s_data = s->resource_data; |
| 410 | u_long ok; |
| 411 | if (m == &s_data->mem_db) |
| 412 | return 0; |
| 413 | ok = inv_probe(m->next, s); |
| 414 | if (ok) { |
| 415 | if (m->base >= 0x100000) |
| 416 | sub_interval(&s_data->mem_db, m->base, m->num); |
| 417 | return ok; |
| 418 | } |
| 419 | if (m->base < 0x100000) |
| 420 | return 0; |
| 421 | return do_mem_probe(m->base, m->num, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 424 | static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 426 | struct resource_map *m, mm; |
| 427 | static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 }; |
| 428 | unsigned long b, i, ok = 0; |
| 429 | struct socket_data *s_data = s->resource_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 431 | /* We do up to four passes through the list */ |
| 432 | if (probe_mask & MEM_PROBE_HIGH) { |
| 433 | if (inv_probe(s_data->mem_db.next, s) > 0) |
| 434 | return 0; |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 435 | dev_printk(KERN_NOTICE, &s->dev, |
| 436 | "cs: warning: no high memory space available!\n"); |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 437 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 439 | |
| 440 | for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) { |
| 441 | mm = *m; |
| 442 | /* Only probe < 1 MB */ |
| 443 | if (mm.base >= 0x100000) |
| 444 | continue; |
| 445 | if ((mm.base | mm.num) & 0xffff) { |
| 446 | ok += do_mem_probe(mm.base, mm.num, s); |
| 447 | continue; |
| 448 | } |
| 449 | /* Special probe for 64K-aligned block */ |
| 450 | for (i = 0; i < 4; i++) { |
| 451 | b = order[i] << 12; |
| 452 | if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) { |
| 453 | if (ok >= mem_limit) |
| 454 | sub_interval(&s_data->mem_db, b, 0x10000); |
| 455 | else |
| 456 | ok += do_mem_probe(b, 0x10000, s); |
| 457 | } |
| 458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 460 | |
| 461 | if (ok > 0) |
| 462 | return 0; |
| 463 | |
| 464 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | #else /* CONFIG_PCMCIA_PROBE */ |
| 468 | |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 469 | static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | struct resource_map *m, mm; |
| 472 | struct socket_data *s_data = s->resource_data; |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 473 | unsigned long ok = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) { |
| 476 | mm = *m; |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 477 | ok += do_mem_probe(mm.base, mm.num, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 479 | if (ok > 0) |
| 480 | return 0; |
| 481 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | #endif /* CONFIG_PCMCIA_PROBE */ |
| 485 | |
| 486 | |
| 487 | /* |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 488 | * Locking note: Must be called with skt_mutex held! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | */ |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 490 | static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | struct socket_data *s_data = s->resource_data; |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 493 | unsigned int probe_mask = MEM_PROBE_LOW; |
| 494 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 496 | if (!probe_mem) |
| 497 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 499 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 501 | if (s->features & SS_CAP_PAGE_REGS) |
| 502 | probe_mask = MEM_PROBE_HIGH; |
| 503 | |
| 504 | if (probe_mask & ~s_data->rsrc_mem_probe) { |
| 505 | if (s->state & SOCKET_PRESENT) |
| 506 | ret = validate_mem(s, probe_mask); |
| 507 | if (!ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | s_data->rsrc_mem_probe |= probe_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 510 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 511 | mutex_unlock(&rsrc_mutex); |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 512 | |
| 513 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | struct pcmcia_align_data { |
| 517 | unsigned long mask; |
| 518 | unsigned long offset; |
| 519 | struct resource_map *map; |
| 520 | }; |
| 521 | |
| 522 | static void |
| 523 | pcmcia_common_align(void *align_data, struct resource *res, |
Greg Kroah-Hartman | 2427ddd | 2006-06-12 17:07:52 -0700 | [diff] [blame] | 524 | resource_size_t size, resource_size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | struct pcmcia_align_data *data = align_data; |
Greg Kroah-Hartman | 2427ddd | 2006-06-12 17:07:52 -0700 | [diff] [blame] | 527 | resource_size_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | /* |
| 529 | * Ensure that we have the correct start address |
| 530 | */ |
| 531 | start = (res->start & ~data->mask) + data->offset; |
| 532 | if (start < res->start) |
| 533 | start += data->mask + 1; |
| 534 | res->start = start; |
| 535 | } |
| 536 | |
| 537 | static void |
Greg Kroah-Hartman | 2427ddd | 2006-06-12 17:07:52 -0700 | [diff] [blame] | 538 | pcmcia_align(void *align_data, struct resource *res, resource_size_t size, |
| 539 | resource_size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
| 541 | struct pcmcia_align_data *data = align_data; |
| 542 | struct resource_map *m; |
| 543 | |
| 544 | pcmcia_common_align(data, res, size, align); |
| 545 | |
| 546 | for (m = data->map->next; m != data->map; m = m->next) { |
| 547 | unsigned long start = m->base; |
| 548 | unsigned long end = m->base + m->num - 1; |
| 549 | |
| 550 | /* |
| 551 | * If the lower resources are not available, try aligning |
| 552 | * to this entry of the resource database to see if it'll |
| 553 | * fit here. |
| 554 | */ |
| 555 | if (res->start < start) { |
| 556 | res->start = start; |
| 557 | pcmcia_common_align(data, res, size, align); |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * If we're above the area which was passed in, there's |
| 562 | * no point proceeding. |
| 563 | */ |
| 564 | if (res->start >= res->end) |
| 565 | break; |
| 566 | |
| 567 | if ((res->start + size - 1) <= end) |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * If we failed to find something suitable, ensure we fail. |
| 573 | */ |
| 574 | if (m == data->map) |
| 575 | res->start = res->end; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Adjust an existing IO region allocation, but making sure that we don't |
| 580 | * encroach outside the resources which the user supplied. |
| 581 | */ |
| 582 | static int nonstatic_adjust_io_region(struct resource *res, unsigned long r_start, |
| 583 | unsigned long r_end, struct pcmcia_socket *s) |
| 584 | { |
| 585 | struct resource_map *m; |
| 586 | struct socket_data *s_data = s->resource_data; |
| 587 | int ret = -ENOMEM; |
| 588 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 589 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) { |
| 591 | unsigned long start = m->base; |
| 592 | unsigned long end = m->base + m->num - 1; |
| 593 | |
| 594 | if (start > r_start || r_end > end) |
| 595 | continue; |
| 596 | |
| 597 | ret = adjust_resource(res, r_start, r_end - r_start + 1); |
| 598 | break; |
| 599 | } |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 600 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | /*====================================================================== |
| 606 | |
| 607 | These find ranges of I/O ports or memory addresses that are not |
| 608 | currently allocated by other devices. |
| 609 | |
| 610 | The 'align' field should reflect the number of bits of address |
| 611 | that need to be preserved from the initial value of *base. It |
| 612 | should be a power of two, greater than or equal to 'num'. A value |
| 613 | of 0 means that all bits of *base are significant. *base should |
| 614 | also be strictly less than 'align'. |
| 615 | |
| 616 | ======================================================================*/ |
| 617 | |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame] | 618 | static struct resource *nonstatic_find_io_region(unsigned long base, int num, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | unsigned long align, struct pcmcia_socket *s) |
| 620 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 621 | struct resource *res = make_resource(0, num, IORESOURCE_IO, s->dev.bus_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | struct socket_data *s_data = s->resource_data; |
| 623 | struct pcmcia_align_data data; |
| 624 | unsigned long min = base; |
| 625 | int ret; |
| 626 | |
| 627 | if (align == 0) |
| 628 | align = 0x10000; |
| 629 | |
| 630 | data.mask = align - 1; |
| 631 | data.offset = base & data.mask; |
| 632 | data.map = &s_data->io_db; |
| 633 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 634 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | #ifdef CONFIG_PCI |
| 636 | if (s->cb_dev) { |
| 637 | ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, |
| 638 | min, 0, pcmcia_align, &data); |
| 639 | } else |
| 640 | #endif |
| 641 | ret = allocate_resource(&ioport_resource, res, num, min, ~0UL, |
| 642 | 1, pcmcia_align, &data); |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 643 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
| 645 | if (ret != 0) { |
| 646 | kfree(res); |
| 647 | res = NULL; |
| 648 | } |
| 649 | return res; |
| 650 | } |
| 651 | |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame] | 652 | static struct resource * nonstatic_find_mem_region(u_long base, u_long num, |
| 653 | u_long align, int low, struct pcmcia_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 655 | struct resource *res = make_resource(0, num, IORESOURCE_MEM, s->dev.bus_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | struct socket_data *s_data = s->resource_data; |
| 657 | struct pcmcia_align_data data; |
| 658 | unsigned long min, max; |
| 659 | int ret, i; |
| 660 | |
| 661 | low = low || !(s->features & SS_CAP_PAGE_REGS); |
| 662 | |
| 663 | data.mask = align - 1; |
| 664 | data.offset = base & data.mask; |
| 665 | data.map = &s_data->mem_db; |
| 666 | |
| 667 | for (i = 0; i < 2; i++) { |
| 668 | if (low) { |
| 669 | max = 0x100000UL; |
| 670 | min = base < max ? base : 0; |
| 671 | } else { |
| 672 | max = ~0UL; |
| 673 | min = 0x100000UL + base; |
| 674 | } |
| 675 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 676 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | #ifdef CONFIG_PCI |
| 678 | if (s->cb_dev) { |
| 679 | ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, |
| 680 | 1, min, 0, |
| 681 | pcmcia_align, &data); |
| 682 | } else |
| 683 | #endif |
| 684 | ret = allocate_resource(&iomem_resource, res, num, min, |
| 685 | max, 1, pcmcia_align, &data); |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 686 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | if (ret == 0 || low) |
| 688 | break; |
| 689 | low = 1; |
| 690 | } |
| 691 | |
| 692 | if (ret != 0) { |
| 693 | kfree(res); |
| 694 | res = NULL; |
| 695 | } |
| 696 | return res; |
| 697 | } |
| 698 | |
| 699 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 700 | static int adjust_memory(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | struct socket_data *data = s->resource_data; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 703 | unsigned long size = end - start + 1; |
| 704 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 706 | if (end < start) |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 707 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 709 | mutex_lock(&rsrc_mutex); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 710 | switch (action) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | case ADD_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 712 | ret = add_interval(&data->mem_db, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | break; |
| 714 | case REMOVE_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 715 | ret = sub_interval(&data->mem_db, start, size); |
| 716 | if (!ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | struct pcmcia_socket *socket; |
| 718 | down_read(&pcmcia_socket_list_rwsem); |
| 719 | list_for_each_entry(socket, &pcmcia_socket_list, socket_list) |
| 720 | release_cis_mem(socket); |
| 721 | up_read(&pcmcia_socket_list_rwsem); |
| 722 | } |
| 723 | break; |
| 724 | default: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 725 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 727 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 733 | static int adjust_io(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
| 735 | struct socket_data *data = s->resource_data; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 736 | unsigned long size = end - start + 1; |
| 737 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 739 | if (end < start) |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 740 | return -EINVAL; |
| 741 | |
| 742 | if (end > IO_SPACE_LIMIT) |
| 743 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 745 | mutex_lock(&rsrc_mutex); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 746 | switch (action) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | case ADD_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 748 | if (add_interval(&data->io_db, start, size) != 0) { |
| 749 | ret = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | #ifdef CONFIG_PCMCIA_PROBE |
| 753 | if (probe_io) |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 754 | do_io_probe(s, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | #endif |
| 756 | break; |
| 757 | case REMOVE_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 758 | sub_interval(&data->io_db, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | break; |
| 760 | default: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 761 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | break; |
| 763 | } |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 764 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 770 | #ifdef CONFIG_PCI |
| 771 | static int nonstatic_autoadd_resources(struct pcmcia_socket *s) |
| 772 | { |
| 773 | struct resource *res; |
| 774 | int i, done = 0; |
| 775 | |
| 776 | if (!s->cb_dev || !s->cb_dev->bus) |
| 777 | return -ENODEV; |
| 778 | |
Brian Gerst | 0d078f6 | 2005-10-30 14:59:20 -0800 | [diff] [blame] | 779 | #if defined(CONFIG_X86) |
Dominik Brodowski | b6d00f0 | 2005-06-27 16:29:02 -0700 | [diff] [blame] | 780 | /* If this is the root bus, the risk of hitting |
| 781 | * some strange system devices which aren't protected |
| 782 | * by either ACPI resource tables or properly requested |
| 783 | * resources is too big. Therefore, don't do auto-adding |
| 784 | * of resources at the moment. |
| 785 | */ |
| 786 | if (s->cb_dev->bus->number == 0) |
| 787 | return -EINVAL; |
| 788 | #endif |
| 789 | |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 790 | for (i=0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 791 | res = s->cb_dev->bus->resource[i]; |
| 792 | if (!res) |
| 793 | continue; |
| 794 | |
| 795 | if (res->flags & IORESOURCE_IO) { |
| 796 | if (res == &ioport_resource) |
| 797 | continue; |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 798 | dev_printk(KERN_INFO, &s->cb_dev->dev, |
| 799 | "pcmcia: parent PCI bridge I/O " |
| 800 | "window: 0x%llx - 0x%llx\n", |
| 801 | (unsigned long long)res->start, |
| 802 | (unsigned long long)res->end); |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 803 | if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end)) |
| 804 | done |= IORESOURCE_IO; |
| 805 | |
| 806 | } |
| 807 | |
| 808 | if (res->flags & IORESOURCE_MEM) { |
| 809 | if (res == &iomem_resource) |
| 810 | continue; |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 811 | dev_printk(KERN_INFO, &s->cb_dev->dev, |
| 812 | "pcmcia: parent PCI bridge Memory " |
| 813 | "window: 0x%llx - 0x%llx\n", |
| 814 | (unsigned long long)res->start, |
| 815 | (unsigned long long)res->end); |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 816 | if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end)) |
| 817 | done |= IORESOURCE_MEM; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /* if we got at least one of IO, and one of MEM, we can be glad and |
| 822 | * activate the PCMCIA subsystem */ |
Dominik Brodowski | 54bb567 | 2005-09-28 19:29:59 +0200 | [diff] [blame] | 823 | if (done == (IORESOURCE_MEM | IORESOURCE_IO)) |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 824 | s->resource_setup_done = 1; |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | #else |
| 830 | |
| 831 | static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s) |
| 832 | { |
| 833 | return -ENODEV; |
| 834 | } |
| 835 | |
| 836 | #endif |
| 837 | |
| 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | static int nonstatic_init(struct pcmcia_socket *s) |
| 840 | { |
| 841 | struct socket_data *data; |
| 842 | |
Dominik Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 843 | data = kzalloc(sizeof(struct socket_data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | if (!data) |
| 845 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
| 847 | data->mem_db.next = &data->mem_db; |
| 848 | data->io_db.next = &data->io_db; |
| 849 | |
| 850 | s->resource_data = (void *) data; |
| 851 | |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 852 | nonstatic_autoadd_resources(s); |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static void nonstatic_release_resource_db(struct pcmcia_socket *s) |
| 858 | { |
| 859 | struct socket_data *data = s->resource_data; |
| 860 | struct resource_map *p, *q; |
| 861 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 862 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | for (p = data->mem_db.next; p != &data->mem_db; p = q) { |
| 864 | q = p->next; |
| 865 | kfree(p); |
| 866 | } |
| 867 | for (p = data->io_db.next; p != &data->io_db; p = q) { |
| 868 | q = p->next; |
| 869 | kfree(p); |
| 870 | } |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 871 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | |
| 875 | struct pccard_resource_ops pccard_nonstatic_ops = { |
| 876 | .validate_mem = pcmcia_nonstatic_validate_mem, |
| 877 | .adjust_io_region = nonstatic_adjust_io_region, |
| 878 | .find_io = nonstatic_find_io_region, |
| 879 | .find_mem = nonstatic_find_mem_region, |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 880 | .add_io = adjust_io, |
| 881 | .add_mem = adjust_memory, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | .init = nonstatic_init, |
| 883 | .exit = nonstatic_release_resource_db, |
| 884 | }; |
| 885 | EXPORT_SYMBOL(pccard_nonstatic_ops); |
| 886 | |
| 887 | |
| 888 | /* sysfs interface to the resource database */ |
| 889 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 890 | static ssize_t show_io_db(struct device *dev, |
| 891 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 893 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | struct socket_data *data; |
| 895 | struct resource_map *p; |
| 896 | ssize_t ret = 0; |
| 897 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 898 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | data = s->resource_data; |
| 900 | |
| 901 | for (p = data->io_db.next; p != &data->io_db; p = p->next) { |
| 902 | if (ret > (PAGE_SIZE - 10)) |
| 903 | continue; |
| 904 | ret += snprintf (&buf[ret], (PAGE_SIZE - ret - 1), |
| 905 | "0x%08lx - 0x%08lx\n", |
| 906 | ((unsigned long) p->base), |
| 907 | ((unsigned long) p->base + p->num - 1)); |
| 908 | } |
| 909 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 910 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | return (ret); |
| 912 | } |
| 913 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 914 | static ssize_t store_io_db(struct device *dev, |
| 915 | struct device_attribute *attr, |
| 916 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 918 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | unsigned long start_addr, end_addr; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 920 | unsigned int add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | ssize_t ret = 0; |
| 922 | |
| 923 | ret = sscanf (buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr); |
| 924 | if (ret != 2) { |
| 925 | ret = sscanf (buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 926 | add = REMOVE_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | if (ret != 2) { |
| 928 | ret = sscanf (buf, "0x%lx - 0x%lx", &start_addr, &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 929 | add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (ret != 2) |
| 931 | return -EINVAL; |
| 932 | } |
| 933 | } |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 934 | if (end_addr < start_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return -EINVAL; |
| 936 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 937 | ret = adjust_io(s, add, start_addr, end_addr); |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 938 | if (!ret) |
| 939 | s->resource_setup_new = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | return ret ? ret : count; |
| 942 | } |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 943 | static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 945 | static ssize_t show_mem_db(struct device *dev, |
| 946 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 948 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | struct socket_data *data; |
| 950 | struct resource_map *p; |
| 951 | ssize_t ret = 0; |
| 952 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 953 | mutex_lock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | data = s->resource_data; |
| 955 | |
| 956 | for (p = data->mem_db.next; p != &data->mem_db; p = p->next) { |
| 957 | if (ret > (PAGE_SIZE - 10)) |
| 958 | continue; |
| 959 | ret += snprintf (&buf[ret], (PAGE_SIZE - ret - 1), |
| 960 | "0x%08lx - 0x%08lx\n", |
| 961 | ((unsigned long) p->base), |
| 962 | ((unsigned long) p->base + p->num - 1)); |
| 963 | } |
| 964 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 965 | mutex_unlock(&rsrc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return (ret); |
| 967 | } |
| 968 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 969 | static ssize_t store_mem_db(struct device *dev, |
| 970 | struct device_attribute *attr, |
| 971 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 973 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | unsigned long start_addr, end_addr; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 975 | unsigned int add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | ssize_t ret = 0; |
| 977 | |
| 978 | ret = sscanf (buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr); |
| 979 | if (ret != 2) { |
| 980 | ret = sscanf (buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 981 | add = REMOVE_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | if (ret != 2) { |
| 983 | ret = sscanf (buf, "0x%lx - 0x%lx", &start_addr, &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 984 | add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | if (ret != 2) |
| 986 | return -EINVAL; |
| 987 | } |
| 988 | } |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 989 | if (end_addr < start_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | return -EINVAL; |
| 991 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 992 | ret = adjust_memory(s, add, start_addr, end_addr); |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 993 | if (!ret) |
| 994 | s->resource_setup_new = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | return ret ? ret : count; |
| 997 | } |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 998 | static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1000 | static struct attribute *pccard_rsrc_attributes[] = { |
| 1001 | &dev_attr_available_resources_io.attr, |
| 1002 | &dev_attr_available_resources_mem.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | NULL, |
| 1004 | }; |
| 1005 | |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1006 | static const struct attribute_group rsrc_attributes = { |
| 1007 | .attrs = pccard_rsrc_attributes, |
| 1008 | }; |
| 1009 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1010 | static int __devinit pccard_sysfs_add_rsrc(struct device *dev, |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 1011 | struct class_interface *class_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1013 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (s->resource_ops != &pccard_nonstatic_ops) |
| 1016 | return 0; |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1017 | return sysfs_create_group(&dev->kobj, &rsrc_attributes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1020 | static void __devexit pccard_sysfs_remove_rsrc(struct device *dev, |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 1021 | struct class_interface *class_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1023 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | if (s->resource_ops != &pccard_nonstatic_ops) |
| 1026 | return; |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1027 | sysfs_remove_group(&dev->kobj, &rsrc_attributes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Sam Ravnborg | ed49f5d | 2008-05-01 04:34:50 -0700 | [diff] [blame] | 1030 | static struct class_interface pccard_rsrc_interface __refdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | .class = &pcmcia_socket_class, |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1032 | .add_dev = &pccard_sysfs_add_rsrc, |
| 1033 | .remove_dev = __devexit_p(&pccard_sysfs_remove_rsrc), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | }; |
| 1035 | |
| 1036 | static int __init nonstatic_sysfs_init(void) |
| 1037 | { |
| 1038 | return class_interface_register(&pccard_rsrc_interface); |
| 1039 | } |
| 1040 | |
| 1041 | static void __exit nonstatic_sysfs_exit(void) |
| 1042 | { |
| 1043 | class_interface_unregister(&pccard_rsrc_interface); |
| 1044 | } |
| 1045 | |
| 1046 | module_init(nonstatic_sysfs_init); |
| 1047 | module_exit(nonstatic_sysfs_exit); |