Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * The initial developer of the original code is David A. Hinds |
| 6 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds |
| 7 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. |
| 8 | * |
| 9 | * (C) 1999 David A. Hinds |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/timer.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/device.h> |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <pcmcia/ss.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <pcmcia/cistpl.h> |
| 30 | #include "cs_internal.h" |
| 31 | |
Dominik Brodowski | 49b1153 | 2010-03-07 16:41:57 +0100 | [diff] [blame] | 32 | /* moved to rsrc_mgr.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | MODULE_AUTHOR("David A. Hinds, Dominik Brodowski"); |
| 34 | MODULE_LICENSE("GPL"); |
Dominik Brodowski | 49b1153 | 2010-03-07 16:41:57 +0100 | [diff] [blame] | 35 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Parameters that can be set with 'insmod' */ |
| 38 | |
| 39 | #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444) |
| 40 | |
| 41 | INT_MODULE_PARM(probe_mem, 1); /* memory probe? */ |
| 42 | #ifdef CONFIG_PCMCIA_PROBE |
| 43 | INT_MODULE_PARM(probe_io, 1); /* IO port probe? */ |
| 44 | INT_MODULE_PARM(mem_limit, 0x10000); |
| 45 | #endif |
| 46 | |
| 47 | /* for io_db and mem_db */ |
| 48 | struct resource_map { |
| 49 | u_long base, num; |
| 50 | struct resource_map *next; |
| 51 | }; |
| 52 | |
| 53 | struct socket_data { |
| 54 | struct resource_map mem_db; |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 55 | struct resource_map mem_db_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct resource_map io_db; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define MEM_PROBE_LOW (1 << 0) |
| 60 | #define MEM_PROBE_HIGH (1 << 1) |
| 61 | |
Dominik Brodowski | 3dace8c | 2010-07-24 12:33:29 +0200 | [diff] [blame] | 62 | /* Action field */ |
| 63 | #define REMOVE_MANAGED_RESOURCE 1 |
| 64 | #define ADD_MANAGED_RESOURCE 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /*====================================================================== |
| 67 | |
| 68 | Linux resource management extensions |
| 69 | |
| 70 | ======================================================================*/ |
| 71 | |
| 72 | static struct resource * |
Greg Kroah-Hartman | 2427ddd | 2006-06-12 17:07:52 -0700 | [diff] [blame] | 73 | claim_region(struct pcmcia_socket *s, resource_size_t base, |
| 74 | resource_size_t size, int type, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | struct resource *res, *parent; |
| 77 | |
| 78 | parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource; |
Dominik Brodowski | 49b1153 | 2010-03-07 16:41:57 +0100 | [diff] [blame] | 79 | res = pcmcia_make_resource(base, size, type | IORESOURCE_BUSY, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | if (res) { |
| 82 | #ifdef CONFIG_PCI |
| 83 | if (s && s->cb_dev) |
| 84 | parent = pci_find_parent_resource(s->cb_dev, res); |
| 85 | #endif |
| 86 | if (!parent || request_resource(parent, res)) { |
| 87 | kfree(res); |
| 88 | res = NULL; |
| 89 | } |
| 90 | } |
| 91 | return res; |
| 92 | } |
| 93 | |
| 94 | static void free_region(struct resource *res) |
| 95 | { |
| 96 | if (res) { |
| 97 | release_resource(res); |
| 98 | kfree(res); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /*====================================================================== |
| 103 | |
| 104 | These manage the internal databases of available resources. |
| 105 | |
| 106 | ======================================================================*/ |
| 107 | |
| 108 | static int add_interval(struct resource_map *map, u_long base, u_long num) |
| 109 | { |
Dominik Brodowski | 1168386 | 2008-08-03 10:22:47 +0200 | [diff] [blame] | 110 | struct resource_map *p, *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Dominik Brodowski | 1168386 | 2008-08-03 10:22:47 +0200 | [diff] [blame] | 112 | for (p = map; ; p = p->next) { |
Dominik Brodowski | f309cb3 | 2010-02-17 14:35:33 +0100 | [diff] [blame] | 113 | if ((p != map) && (p->base+p->num >= base)) { |
| 114 | p->num = max(num + base - p->base, p->num); |
| 115 | return 0; |
| 116 | } |
Dominik Brodowski | 1168386 | 2008-08-03 10:22:47 +0200 | [diff] [blame] | 117 | if ((p->next == map) || (p->next->base > base+num-1)) |
| 118 | break; |
| 119 | } |
| 120 | q = kmalloc(sizeof(struct resource_map), GFP_KERNEL); |
| 121 | if (!q) { |
| 122 | printk(KERN_WARNING "out of memory to update resources\n"); |
| 123 | return -ENOMEM; |
| 124 | } |
| 125 | q->base = base; q->num = num; |
| 126 | q->next = p->next; p->next = q; |
| 127 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /*====================================================================*/ |
| 131 | |
| 132 | static int sub_interval(struct resource_map *map, u_long base, u_long num) |
| 133 | { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 134 | struct resource_map *p, *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 136 | for (p = map; ; p = q) { |
| 137 | q = p->next; |
| 138 | if (q == map) |
| 139 | break; |
| 140 | if ((q->base+q->num > base) && (base+num > q->base)) { |
| 141 | if (q->base >= base) { |
| 142 | if (q->base+q->num <= base+num) { |
| 143 | /* Delete whole block */ |
| 144 | p->next = q->next; |
| 145 | kfree(q); |
| 146 | /* don't advance the pointer yet */ |
| 147 | q = p; |
| 148 | } else { |
| 149 | /* Cut off bit from the front */ |
| 150 | q->num = q->base + q->num - base - num; |
| 151 | q->base = base + num; |
| 152 | } |
| 153 | } else if (q->base+q->num <= base+num) { |
| 154 | /* Cut off bit from the end */ |
| 155 | q->num = base - q->base; |
| 156 | } else { |
| 157 | /* Split the block into two pieces */ |
| 158 | p = kmalloc(sizeof(struct resource_map), |
| 159 | GFP_KERNEL); |
| 160 | if (!p) { |
| 161 | printk(KERN_WARNING "out of memory to update resources\n"); |
| 162 | return -ENOMEM; |
| 163 | } |
| 164 | p->base = base+num; |
| 165 | p->num = q->base+q->num - p->base; |
| 166 | q->num = base - q->base; |
| 167 | p->next = q->next ; q->next = p; |
| 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 171 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /*====================================================================== |
| 175 | |
| 176 | These routines examine a region of IO or memory addresses to |
| 177 | determine what ranges might be genuinely available. |
| 178 | |
| 179 | ======================================================================*/ |
| 180 | |
| 181 | #ifdef CONFIG_PCMCIA_PROBE |
Olof Johansson | 906da809c | 2008-02-04 22:27:35 -0800 | [diff] [blame] | 182 | static void do_io_probe(struct pcmcia_socket *s, unsigned int base, |
| 183 | unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 185 | struct resource *res; |
| 186 | struct socket_data *s_data = s->resource_data; |
| 187 | unsigned int i, j, bad; |
| 188 | int any; |
| 189 | u_char *b, hole, most; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 191 | dev_info(&s->dev, "cs: IO port probe %#x-%#x:", base, base+num-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 193 | /* First, what does a floating port look like? */ |
| 194 | b = kzalloc(256, GFP_KERNEL); |
| 195 | if (!b) { |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 196 | pr_cont("\n"); |
| 197 | dev_err(&s->dev, "do_io_probe: unable to kmalloc 256 bytes\n"); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 198 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 200 | for (i = base, most = 0; i < base+num; i += 8) { |
Dominik Brodowski | 509b086 | 2010-04-08 19:23:07 +0200 | [diff] [blame] | 201 | res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe"); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 202 | if (!res) |
| 203 | continue; |
| 204 | hole = inb(i); |
| 205 | for (j = 1; j < 8; j++) |
| 206 | if (inb(i+j) != hole) |
| 207 | break; |
| 208 | free_region(res); |
| 209 | if ((j == 8) && (++b[hole] > b[most])) |
| 210 | most = hole; |
| 211 | if (b[most] == 127) |
| 212 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 214 | kfree(b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 216 | bad = any = 0; |
| 217 | for (i = base; i < base+num; i += 8) { |
Dominik Brodowski | 509b086 | 2010-04-08 19:23:07 +0200 | [diff] [blame] | 218 | res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe"); |
| 219 | if (!res) { |
| 220 | if (!any) |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 221 | pr_cont(" excluding"); |
Dominik Brodowski | 509b086 | 2010-04-08 19:23:07 +0200 | [diff] [blame] | 222 | if (!bad) |
| 223 | bad = any = i; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 224 | continue; |
Dominik Brodowski | 509b086 | 2010-04-08 19:23:07 +0200 | [diff] [blame] | 225 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 226 | for (j = 0; j < 8; j++) |
| 227 | if (inb(i+j) != most) |
| 228 | break; |
| 229 | free_region(res); |
| 230 | if (j < 8) { |
| 231 | if (!any) |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 232 | pr_cont(" excluding"); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 233 | if (!bad) |
| 234 | bad = any = i; |
| 235 | } else { |
| 236 | if (bad) { |
| 237 | sub_interval(&s_data->io_db, bad, i-bad); |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 238 | pr_cont(" %#x-%#x", bad, i-1); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 239 | bad = 0; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | if (bad) { |
| 244 | if ((num > 16) && (bad == base) && (i == base+num)) { |
Dominik Brodowski | 509b086 | 2010-04-08 19:23:07 +0200 | [diff] [blame] | 245 | sub_interval(&s_data->io_db, bad, i-bad); |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 246 | pr_cont(" nothing: probe failed.\n"); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 247 | return; |
| 248 | } else { |
| 249 | sub_interval(&s_data->io_db, bad, i-bad); |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 250 | pr_cont(" %#x-%#x", bad, i-1); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 254 | pr_cont("%s\n", !any ? " clean" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | #endif |
| 257 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 258 | /*======================================================================*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Lee Jones | f20a759 | 2021-03-12 11:02:34 +0000 | [diff] [blame] | 260 | /* |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 261 | * readable() - iomem validation function for cards with a valid CIS |
| 262 | */ |
Dominik Brodowski | c5081d5 | 2008-06-19 20:12:34 +0200 | [diff] [blame] | 263 | static int readable(struct pcmcia_socket *s, struct resource *res, |
| 264 | unsigned int *count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 266 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Dominik Brodowski | 7ab24855 | 2010-02-17 18:00:07 +0100 | [diff] [blame] | 268 | if (s->fake_cis) { |
| 269 | dev_dbg(&s->dev, "fake CIS is being used: can't validate mem\n"); |
| 270 | return 0; |
| 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | s->cis_mem.res = res; |
| 274 | s->cis_virt = ioremap(res->start, s->map_size); |
| 275 | if (s->cis_virt) { |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 276 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 6e7b51a | 2010-01-06 13:57:43 +0100 | [diff] [blame] | 277 | /* as we're only called from pcmcia.c, we're safe */ |
| 278 | if (s->callback->validate) |
| 279 | ret = s->callback->validate(s, count); |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 280 | /* invalidate mapping */ |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 281 | mutex_lock(&s->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | iounmap(s->cis_virt); |
| 283 | s->cis_virt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | s->cis_mem.res = NULL; |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 286 | if ((ret) || (*count == 0)) |
| 287 | return -EINVAL; |
| 288 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Lee Jones | f20a759 | 2021-03-12 11:02:34 +0000 | [diff] [blame] | 291 | /* |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 292 | * checksum() - iomem validation function for simple memory cards |
| 293 | */ |
| 294 | static int checksum(struct pcmcia_socket *s, struct resource *res, |
| 295 | unsigned int *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | pccard_mem_map map; |
| 298 | int i, a = 0, b = -1, d; |
| 299 | void __iomem *virt; |
| 300 | |
| 301 | virt = ioremap(res->start, s->map_size); |
| 302 | if (virt) { |
| 303 | map.map = 0; |
| 304 | map.flags = MAP_ACTIVE; |
| 305 | map.speed = 0; |
| 306 | map.res = res; |
| 307 | map.card_start = 0; |
| 308 | s->ops->set_mem_map(s, &map); |
| 309 | |
| 310 | /* Don't bother checking every word... */ |
| 311 | for (i = 0; i < s->map_size; i += 44) { |
| 312 | d = readl(virt+i); |
| 313 | a += d; |
| 314 | b &= d; |
| 315 | } |
| 316 | |
| 317 | map.flags = 0; |
| 318 | s->ops->set_mem_map(s, &map); |
| 319 | |
| 320 | iounmap(virt); |
| 321 | } |
| 322 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 323 | if (b == -1) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | *value = a; |
| 327 | |
| 328 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 331 | /** |
| 332 | * do_validate_mem() - low level validate a memory region for PCMCIA use |
| 333 | * @s: PCMCIA socket to validate |
| 334 | * @base: start address of resource to check |
| 335 | * @size: size of resource to check |
| 336 | * @validate: validation function to use |
| 337 | * |
| 338 | * do_validate_mem() splits up the memory region which is to be checked |
| 339 | * into two parts. Both are passed to the @validate() function. If |
| 340 | * @validate() returns non-zero, or the value parameter to @validate() |
| 341 | * is zero, or the value parameter is different between both calls, |
| 342 | * the check fails, and -EINVAL is returned. Else, 0 is returned. |
| 343 | */ |
| 344 | static int do_validate_mem(struct pcmcia_socket *s, |
| 345 | unsigned long base, unsigned long size, |
Lee Jones | e9d503f | 2021-03-12 11:02:39 +0000 | [diff] [blame] | 346 | int (*validate)(struct pcmcia_socket *s, |
| 347 | struct resource *res, |
| 348 | unsigned int *value)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 350 | struct socket_data *s_data = s->resource_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | struct resource *res1, *res2; |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 352 | unsigned int info1 = 1, info2 = 1; |
| 353 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 355 | res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe"); |
| 356 | res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM, |
| 357 | "PCMCIA memprobe"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | if (res1 && res2) { |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 360 | ret = 0; |
| 361 | if (validate) { |
| 362 | ret = validate(s, res1, &info1); |
| 363 | ret += validate(s, res2, &info2); |
| 364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Dominik Brodowski | 3490a69 | 2018-01-22 21:36:27 +0100 | [diff] [blame] | 367 | dev_dbg(&s->dev, "cs: memory probe 0x%06lx-0x%06lx: %pr %pr %u %u %u", |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 368 | base, base+size-1, res1, res2, ret, info1, info2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Jesper Juhl | bec0b2c | 2013-01-20 23:14:43 +0100 | [diff] [blame] | 370 | free_region(res2); |
| 371 | free_region(res1); |
| 372 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 373 | if ((ret) || (info1 != info2) || (info1 == 0)) |
| 374 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 376 | if (validate && !s->fake_cis) { |
| 377 | /* move it to the validated data set */ |
| 378 | add_interval(&s_data->mem_db_valid, base, size); |
| 379 | sub_interval(&s_data->mem_db, base, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 382 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 386 | /** |
| 387 | * do_mem_probe() - validate a memory region for PCMCIA use |
| 388 | * @s: PCMCIA socket to validate |
| 389 | * @base: start address of resource to check |
| 390 | * @num: size of resource to check |
| 391 | * @validate: validation function to use |
| 392 | * @fallback: validation function to use if validate fails |
| 393 | * |
| 394 | * do_mem_probe() checks a memory region for use by the PCMCIA subsystem. |
| 395 | * To do so, the area is split up into sensible parts, and then passed |
| 396 | * into the @validate() function. Only if @validate() and @fallback() fail, |
tangmeng | 6c00ef0 | 2022-01-27 15:15:09 +0800 | [diff] [blame] | 397 | * the area is marked as unavailable for use by the PCMCIA subsystem. The |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 398 | * function returns the size of the usable memory area. |
| 399 | */ |
| 400 | static int do_mem_probe(struct pcmcia_socket *s, u_long base, u_long num, |
Lee Jones | e9d503f | 2021-03-12 11:02:39 +0000 | [diff] [blame] | 401 | int (*validate)(struct pcmcia_socket *s, |
| 402 | struct resource *res, |
| 403 | unsigned int *value), |
| 404 | int (*fallback)(struct pcmcia_socket *s, |
| 405 | struct resource *res, |
| 406 | unsigned int *value)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 408 | struct socket_data *s_data = s->resource_data; |
| 409 | u_long i, j, bad, fail, step; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 411 | dev_info(&s->dev, "cs: memory probe 0x%06lx-0x%06lx:", |
| 412 | base, base+num-1); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 413 | bad = fail = 0; |
| 414 | step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff); |
| 415 | /* don't allow too large steps */ |
| 416 | if (step > 0x800000) |
| 417 | step = 0x800000; |
| 418 | /* cis_readable wants to map 2x map_size */ |
| 419 | if (step < 2 * s->map_size) |
| 420 | step = 2 * s->map_size; |
| 421 | for (i = j = base; i < base+num; i = j + step) { |
| 422 | if (!fail) { |
| 423 | for (j = i; j < base+num; j += step) { |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 424 | if (!do_validate_mem(s, j, step, validate)) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | fail = ((i == base) && (j == base+num)); |
| 428 | } |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 429 | if ((fail) && (fallback)) { |
| 430 | for (j = i; j < base+num; j += step) |
| 431 | if (!do_validate_mem(s, j, step, fallback)) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | if (i != j) { |
| 435 | if (!bad) |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 436 | pr_cont(" excluding"); |
| 437 | pr_cont(" %#05lx-%#05lx", i, j-1); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 438 | sub_interval(&s_data->mem_db, i, j-i); |
| 439 | bad += j-i; |
| 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 442 | pr_cont("%s\n", !bad ? " clean" : ""); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 443 | return num - bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | #ifdef CONFIG_PCMCIA_PROBE |
| 448 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 449 | /** |
| 450 | * inv_probe() - top-to-bottom search for one usuable high memory area |
| 451 | * @s: PCMCIA socket to validate |
| 452 | * @m: resource_map to check |
| 453 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s) |
| 455 | { |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 456 | struct socket_data *s_data = s->resource_data; |
| 457 | u_long ok; |
| 458 | if (m == &s_data->mem_db) |
| 459 | return 0; |
| 460 | ok = inv_probe(m->next, s); |
| 461 | if (ok) { |
| 462 | if (m->base >= 0x100000) |
| 463 | sub_interval(&s_data->mem_db, m->base, m->num); |
| 464 | return ok; |
| 465 | } |
| 466 | if (m->base < 0x100000) |
| 467 | return 0; |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 468 | return do_mem_probe(s, m->base, m->num, readable, checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 471 | /** |
| 472 | * validate_mem() - memory probe function |
| 473 | * @s: PCMCIA socket to validate |
| 474 | * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH |
| 475 | * |
| 476 | * The memory probe. If the memory list includes a 64K-aligned block |
| 477 | * below 1MB, we probe in 64K chunks, and as soon as we accumulate at |
| 478 | * least mem_limit free space, we quit. Returns 0 on usuable ports. |
| 479 | */ |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 480 | static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 482 | struct resource_map *m, mm; |
| 483 | static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 }; |
| 484 | unsigned long b, i, ok = 0; |
| 485 | struct socket_data *s_data = s->resource_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 487 | /* We do up to four passes through the list */ |
| 488 | if (probe_mask & MEM_PROBE_HIGH) { |
| 489 | if (inv_probe(s_data->mem_db.next, s) > 0) |
| 490 | return 0; |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 491 | if (s_data->mem_db_valid.next != &s_data->mem_db_valid) |
| 492 | return 0; |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 493 | dev_notice(&s->dev, |
Dominik Brodowski | dbe4ea5 | 2008-08-02 21:36:19 +0200 | [diff] [blame] | 494 | "cs: warning: no high memory space available!\n"); |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 495 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 497 | |
| 498 | for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) { |
| 499 | mm = *m; |
| 500 | /* Only probe < 1 MB */ |
| 501 | if (mm.base >= 0x100000) |
| 502 | continue; |
| 503 | if ((mm.base | mm.num) & 0xffff) { |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 504 | ok += do_mem_probe(s, mm.base, mm.num, readable, |
| 505 | checksum); |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 506 | continue; |
| 507 | } |
| 508 | /* Special probe for 64K-aligned block */ |
| 509 | for (i = 0; i < 4; i++) { |
| 510 | b = order[i] << 12; |
| 511 | if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) { |
| 512 | if (ok >= mem_limit) |
| 513 | sub_interval(&s_data->mem_db, b, 0x10000); |
| 514 | else |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 515 | ok += do_mem_probe(s, b, 0x10000, |
| 516 | readable, checksum); |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 517 | } |
| 518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 520 | |
| 521 | if (ok > 0) |
| 522 | return 0; |
| 523 | |
| 524 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | #else /* CONFIG_PCMCIA_PROBE */ |
| 528 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 529 | /** |
| 530 | * validate_mem() - memory probe function |
| 531 | * @s: PCMCIA socket to validate |
| 532 | * @probe_mask: ignored |
| 533 | * |
| 534 | * Returns 0 on usuable ports. |
| 535 | */ |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 536 | static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
| 538 | struct resource_map *m, mm; |
| 539 | struct socket_data *s_data = s->resource_data; |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 540 | unsigned long ok = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) { |
| 543 | mm = *m; |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 544 | ok += do_mem_probe(s, mm.base, mm.num, readable, checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
Andrew Morton | 2cff944 | 2005-11-16 21:29:26 -0800 | [diff] [blame] | 546 | if (ok > 0) |
| 547 | return 0; |
| 548 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | #endif /* CONFIG_PCMCIA_PROBE */ |
| 552 | |
| 553 | |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 554 | /** |
| 555 | * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use |
| 556 | * @s: PCMCIA socket to validate |
| 557 | * |
| 558 | * This is tricky... when we set up CIS memory, we try to validate |
| 559 | * the memory window space allocations. |
| 560 | * |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 561 | * Locking note: Must be called with skt_mutex held! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | */ |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 563 | static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | struct socket_data *s_data = s->resource_data; |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 566 | unsigned int probe_mask = MEM_PROBE_LOW; |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 567 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 569 | if (!probe_mem || !(s->state & SOCKET_PRESENT)) |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 570 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 572 | if (s->features & SS_CAP_PAGE_REGS) |
| 573 | probe_mask = MEM_PROBE_HIGH; |
| 574 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 575 | ret = validate_mem(s, probe_mask); |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 576 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 577 | if (s_data->mem_db_valid.next != &s_data->mem_db_valid) |
| 578 | return 0; |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 579 | |
| 580 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | struct pcmcia_align_data { |
| 584 | unsigned long mask; |
| 585 | unsigned long offset; |
| 586 | struct resource_map *map; |
| 587 | }; |
| 588 | |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 589 | static resource_size_t pcmcia_common_align(struct pcmcia_align_data *align_data, |
| 590 | resource_size_t start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 592 | resource_size_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* |
| 594 | * Ensure that we have the correct start address |
| 595 | */ |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 596 | ret = (start & ~align_data->mask) + align_data->offset; |
| 597 | if (ret < start) |
| 598 | ret += align_data->mask + 1; |
| 599 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 602 | static resource_size_t |
Dominik Brodowski | 3b7a17f | 2010-01-01 17:40:50 +0100 | [diff] [blame] | 603 | pcmcia_align(void *align_data, const struct resource *res, |
| 604 | resource_size_t size, resource_size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
| 606 | struct pcmcia_align_data *data = align_data; |
| 607 | struct resource_map *m; |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 608 | resource_size_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 610 | start = pcmcia_common_align(data, res->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | for (m = data->map->next; m != data->map; m = m->next) { |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 613 | unsigned long map_start = m->base; |
| 614 | unsigned long map_end = m->base + m->num - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | * If the lower resources are not available, try aligning |
| 618 | * to this entry of the resource database to see if it'll |
| 619 | * fit here. |
| 620 | */ |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 621 | if (start < map_start) |
| 622 | start = pcmcia_common_align(data, map_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * If we're above the area which was passed in, there's |
| 626 | * no point proceeding. |
| 627 | */ |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 628 | if (start >= res->end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | break; |
| 630 | |
Dominik Brodowski | 147a274 | 2010-04-04 18:10:35 +0200 | [diff] [blame] | 631 | if ((start + size - 1) <= map_end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * If we failed to find something suitable, ensure we fail. |
| 637 | */ |
| 638 | if (m == data->map) |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 639 | start = res->end; |
| 640 | |
| 641 | return start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Adjust an existing IO region allocation, but making sure that we don't |
| 646 | * encroach outside the resources which the user supplied. |
| 647 | */ |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 648 | static int __nonstatic_adjust_io_region(struct pcmcia_socket *s, |
| 649 | unsigned long r_start, |
| 650 | unsigned long r_end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
| 652 | struct resource_map *m; |
| 653 | struct socket_data *s_data = s->resource_data; |
| 654 | int ret = -ENOMEM; |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) { |
| 657 | unsigned long start = m->base; |
| 658 | unsigned long end = m->base + m->num - 1; |
| 659 | |
| 660 | if (start > r_start || r_end > end) |
| 661 | continue; |
| 662 | |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 663 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | /*====================================================================== |
| 670 | |
| 671 | These find ranges of I/O ports or memory addresses that are not |
| 672 | currently allocated by other devices. |
| 673 | |
| 674 | The 'align' field should reflect the number of bits of address |
| 675 | that need to be preserved from the initial value of *base. It |
| 676 | should be a power of two, greater than or equal to 'num'. A value |
| 677 | of 0 means that all bits of *base are significant. *base should |
| 678 | also be strictly less than 'align'. |
| 679 | |
| 680 | ======================================================================*/ |
| 681 | |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 682 | static struct resource *__nonstatic_find_io_region(struct pcmcia_socket *s, |
| 683 | unsigned long base, int num, |
| 684 | unsigned long align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
Dominik Brodowski | 49b1153 | 2010-03-07 16:41:57 +0100 | [diff] [blame] | 686 | struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO, |
| 687 | dev_name(&s->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | struct socket_data *s_data = s->resource_data; |
| 689 | struct pcmcia_align_data data; |
| 690 | unsigned long min = base; |
| 691 | int ret; |
| 692 | |
Zhou Qingyang | ca0fe0d | 2021-12-01 00:59:23 +0800 | [diff] [blame] | 693 | if (!res) |
| 694 | return NULL; |
| 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | data.mask = align - 1; |
| 697 | data.offset = base & data.mask; |
| 698 | data.map = &s_data->io_db; |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | #ifdef CONFIG_PCI |
| 701 | if (s->cb_dev) { |
| 702 | ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, |
| 703 | min, 0, pcmcia_align, &data); |
| 704 | } else |
| 705 | #endif |
| 706 | ret = allocate_resource(&ioport_resource, res, num, min, ~0UL, |
| 707 | 1, pcmcia_align, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
| 709 | if (ret != 0) { |
| 710 | kfree(res); |
| 711 | res = NULL; |
| 712 | } |
| 713 | return res; |
| 714 | } |
| 715 | |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 716 | static int nonstatic_find_io(struct pcmcia_socket *s, unsigned int attr, |
| 717 | unsigned int *base, unsigned int num, |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 718 | unsigned int align, struct resource **parent) |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 719 | { |
| 720 | int i, ret = 0; |
| 721 | |
| 722 | /* Check for an already-allocated window that must conflict with |
| 723 | * what was asked for. It is a hack because it does not catch all |
| 724 | * potential conflicts, just the most obvious ones. |
| 725 | */ |
| 726 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 727 | if (!s->io[i].res) |
| 728 | continue; |
| 729 | |
| 730 | if (!*base) |
| 731 | continue; |
| 732 | |
| 733 | if ((s->io[i].res->start & (align-1)) == *base) |
| 734 | return -EBUSY; |
| 735 | } |
| 736 | |
| 737 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 738 | struct resource *res = s->io[i].res; |
| 739 | unsigned int try; |
| 740 | |
| 741 | if (res && (res->flags & IORESOURCE_BITS) != |
| 742 | (attr & IORESOURCE_BITS)) |
| 743 | continue; |
| 744 | |
| 745 | if (!res) { |
| 746 | if (align == 0) |
| 747 | align = 0x10000; |
| 748 | |
| 749 | res = s->io[i].res = __nonstatic_find_io_region(s, |
| 750 | *base, num, |
| 751 | align); |
| 752 | if (!res) |
| 753 | return -EINVAL; |
| 754 | |
| 755 | *base = res->start; |
| 756 | s->io[i].res->flags = |
| 757 | ((res->flags & ~IORESOURCE_BITS) | |
| 758 | (attr & IORESOURCE_BITS)); |
| 759 | s->io[i].InUse = num; |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 760 | *parent = res; |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | /* Try to extend top of window */ |
| 765 | try = res->end + 1; |
| 766 | if ((*base == 0) || (*base == try)) { |
| 767 | ret = __nonstatic_adjust_io_region(s, res->start, |
| 768 | res->end + num); |
| 769 | if (!ret) { |
| 770 | ret = adjust_resource(s->io[i].res, res->start, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 771 | resource_size(res) + num); |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 772 | if (ret) |
| 773 | continue; |
| 774 | *base = try; |
| 775 | s->io[i].InUse += num; |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 776 | *parent = res; |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /* Try to extend bottom of window */ |
| 782 | try = res->start - num; |
| 783 | if ((*base == 0) || (*base == try)) { |
| 784 | ret = __nonstatic_adjust_io_region(s, |
| 785 | res->start - num, |
| 786 | res->end); |
| 787 | if (!ret) { |
| 788 | ret = adjust_resource(s->io[i].res, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 789 | res->start - num, |
| 790 | resource_size(res) + num); |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 791 | if (ret) |
| 792 | continue; |
| 793 | *base = try; |
| 794 | s->io[i].InUse += num; |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 795 | *parent = res; |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | |
| 804 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 805 | static struct resource *nonstatic_find_mem_region(u_long base, u_long num, |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame] | 806 | u_long align, int low, struct pcmcia_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Dominik Brodowski | 49b1153 | 2010-03-07 16:41:57 +0100 | [diff] [blame] | 808 | struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_MEM, |
| 809 | dev_name(&s->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | struct socket_data *s_data = s->resource_data; |
| 811 | struct pcmcia_align_data data; |
| 812 | unsigned long min, max; |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 813 | int ret, i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Zhou Qingyang | 977d2e7 | 2021-12-01 02:11:40 +0800 | [diff] [blame] | 815 | if (!res) |
| 816 | return NULL; |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | low = low || !(s->features & SS_CAP_PAGE_REGS); |
| 819 | |
| 820 | data.mask = align - 1; |
| 821 | data.offset = base & data.mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | for (i = 0; i < 2; i++) { |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 824 | data.map = &s_data->mem_db_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if (low) { |
| 826 | max = 0x100000UL; |
| 827 | min = base < max ? base : 0; |
| 828 | } else { |
| 829 | max = ~0UL; |
| 830 | min = 0x100000UL + base; |
| 831 | } |
| 832 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 833 | for (j = 0; j < 2; j++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | #ifdef CONFIG_PCI |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 835 | if (s->cb_dev) { |
| 836 | ret = pci_bus_alloc_resource(s->cb_dev->bus, |
| 837 | res, num, 1, min, 0, |
| 838 | pcmcia_align, &data); |
| 839 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | #endif |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 841 | { |
| 842 | ret = allocate_resource(&iomem_resource, |
| 843 | res, num, min, max, 1, |
| 844 | pcmcia_align, &data); |
| 845 | } |
| 846 | if (ret == 0) |
| 847 | break; |
| 848 | data.map = &s_data->mem_db; |
| 849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (ret == 0 || low) |
| 851 | break; |
| 852 | low = 1; |
| 853 | } |
| 854 | |
| 855 | if (ret != 0) { |
| 856 | kfree(res); |
| 857 | res = NULL; |
| 858 | } |
| 859 | return res; |
| 860 | } |
| 861 | |
| 862 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 863 | 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] | 864 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | struct socket_data *data = s->resource_data; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 866 | unsigned long size = end - start + 1; |
| 867 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 869 | if (end < start) |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 870 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 872 | switch (action) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | case ADD_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 874 | ret = add_interval(&data->mem_db, start, size); |
Dominik Brodowski | 3f32b3c | 2010-01-02 22:22:50 +0100 | [diff] [blame] | 875 | if (!ret) |
| 876 | do_mem_probe(s, start, size, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | break; |
| 878 | case REMOVE_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 879 | ret = sub_interval(&data->mem_db, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | break; |
| 881 | default: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 882 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 889 | 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] | 890 | { |
| 891 | struct socket_data *data = s->resource_data; |
Dominik Brodowski | 41b97ab | 2010-04-15 19:01:53 +0200 | [diff] [blame] | 892 | unsigned long size; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 893 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
Dominik Brodowski | 9713ab2 | 2010-03-23 16:05:00 +0100 | [diff] [blame] | 895 | #if defined(CONFIG_X86) |
| 896 | /* on x86, avoid anything < 0x100 for it is often used for |
| 897 | * legacy platform devices */ |
| 898 | if (start < 0x100) |
| 899 | start = 0x100; |
| 900 | #endif |
| 901 | |
Dominik Brodowski | 41b97ab | 2010-04-15 19:01:53 +0200 | [diff] [blame] | 902 | size = end - start + 1; |
| 903 | |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 904 | if (end < start) |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 905 | return -EINVAL; |
| 906 | |
| 907 | if (end > IO_SPACE_LIMIT) |
| 908 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 910 | switch (action) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | case ADD_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 912 | if (add_interval(&data->io_db, start, size) != 0) { |
| 913 | ret = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | break; |
| 915 | } |
| 916 | #ifdef CONFIG_PCMCIA_PROBE |
| 917 | if (probe_io) |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 918 | do_io_probe(s, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | #endif |
| 920 | break; |
| 921 | case REMOVE_MANAGED_RESOURCE: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 922 | sub_interval(&data->io_db, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | break; |
| 924 | default: |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 925 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | break; |
| 927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 933 | #ifdef CONFIG_PCI |
| 934 | static int nonstatic_autoadd_resources(struct pcmcia_socket *s) |
| 935 | { |
| 936 | struct resource *res; |
| 937 | int i, done = 0; |
| 938 | |
| 939 | if (!s->cb_dev || !s->cb_dev->bus) |
| 940 | return -ENODEV; |
| 941 | |
Brian Gerst | 0d078f6 | 2005-10-30 14:59:20 -0800 | [diff] [blame] | 942 | #if defined(CONFIG_X86) |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 943 | /* If this is the root bus, the risk of hitting some strange |
| 944 | * system devices is too high: If a driver isn't loaded, the |
| 945 | * resources are not claimed; even if a driver is loaded, it |
| 946 | * may not request all resources or even the wrong one. We |
| 947 | * can neither trust the rest of the kernel nor ACPI/PNP and |
| 948 | * CRS parsing to get it right. Therefore, use several |
| 949 | * safeguards: |
| 950 | * |
| 951 | * - Do not auto-add resources if the CardBus bridge is on |
| 952 | * the PCI root bus |
| 953 | * |
| 954 | * - Avoid any I/O ports < 0x100. |
| 955 | * |
| 956 | * - On PCI-PCI bridges, only use resources which are set up |
| 957 | * exclusively for the secondary PCI bus: the risk of hitting |
| 958 | * system devices is quite low, as they usually aren't |
| 959 | * connected to the secondary PCI bus. |
Dominik Brodowski | b6d00f0 | 2005-06-27 16:29:02 -0700 | [diff] [blame] | 960 | */ |
| 961 | if (s->cb_dev->bus->number == 0) |
| 962 | return -EINVAL; |
Dominik Brodowski | b6d00f0 | 2005-06-27 16:29:02 -0700 | [diff] [blame] | 963 | |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 964 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { |
| 965 | res = s->cb_dev->bus->resource[i]; |
| 966 | #else |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 967 | pci_bus_for_each_resource(s->cb_dev->bus, res, i) { |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 968 | #endif |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 969 | if (!res) |
| 970 | continue; |
| 971 | |
| 972 | if (res->flags & IORESOURCE_IO) { |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 973 | /* safeguard against the root resource, where the |
| 974 | * risk of hitting any other device would be too |
| 975 | * high */ |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 976 | if (res == &ioport_resource) |
| 977 | continue; |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 978 | |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 979 | dev_info(&s->cb_dev->dev, |
| 980 | "pcmcia: parent PCI bridge window: %pR\n", |
| 981 | res); |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 982 | if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end)) |
| 983 | done |= IORESOURCE_IO; |
| 984 | |
| 985 | } |
| 986 | |
| 987 | if (res->flags & IORESOURCE_MEM) { |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 988 | /* safeguard against the root resource, where the |
| 989 | * risk of hitting any other device would be too |
| 990 | * high */ |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 991 | if (res == &iomem_resource) |
| 992 | continue; |
Dominik Brodowski | cf26e8d | 2010-03-29 21:40:35 +0200 | [diff] [blame] | 993 | |
Joe Perches | f2e6cf7 | 2014-10-10 09:12:47 -0700 | [diff] [blame] | 994 | dev_info(&s->cb_dev->dev, |
| 995 | "pcmcia: parent PCI bridge window: %pR\n", |
| 996 | res); |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 997 | if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end)) |
| 998 | done |= IORESOURCE_MEM; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /* if we got at least one of IO, and one of MEM, we can be glad and |
| 1003 | * activate the PCMCIA subsystem */ |
Dominik Brodowski | 54bb567 | 2005-09-28 19:29:59 +0200 | [diff] [blame] | 1004 | if (done == (IORESOURCE_MEM | IORESOURCE_IO)) |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1005 | s->resource_setup_done = 1; |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | #else |
| 1011 | |
| 1012 | static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s) |
| 1013 | { |
| 1014 | return -ENODEV; |
| 1015 | } |
| 1016 | |
| 1017 | #endif |
| 1018 | |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | static int nonstatic_init(struct pcmcia_socket *s) |
| 1021 | { |
| 1022 | struct socket_data *data; |
| 1023 | |
Dominik Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 1024 | data = kzalloc(sizeof(struct socket_data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | if (!data) |
| 1026 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | data->mem_db.next = &data->mem_db; |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 1029 | data->mem_db_valid.next = &data->mem_db_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | data->io_db.next = &data->io_db; |
| 1031 | |
| 1032 | s->resource_data = (void *) data; |
| 1033 | |
Dominik Brodowski | 3c29976 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1034 | nonstatic_autoadd_resources(s); |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | static void nonstatic_release_resource_db(struct pcmcia_socket *s) |
| 1040 | { |
| 1041 | struct socket_data *data = s->resource_data; |
| 1042 | struct resource_map *p, *q; |
| 1043 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 1044 | for (p = data->mem_db_valid.next; p != &data->mem_db_valid; p = q) { |
| 1045 | q = p->next; |
| 1046 | kfree(p); |
| 1047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | for (p = data->mem_db.next; p != &data->mem_db; p = q) { |
| 1049 | q = p->next; |
| 1050 | kfree(p); |
| 1051 | } |
| 1052 | for (p = data->io_db.next; p != &data->io_db; p = q) { |
| 1053 | q = p->next; |
| 1054 | kfree(p); |
| 1055 | } |
Armin Wolf | c85fd94 | 2023-05-12 20:45:29 +0200 | [diff] [blame] | 1056 | |
| 1057 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | struct pccard_resource_ops pccard_nonstatic_ops = { |
| 1062 | .validate_mem = pcmcia_nonstatic_validate_mem, |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 1063 | .find_io = nonstatic_find_io, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | .find_mem = nonstatic_find_mem_region, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | .init = nonstatic_init, |
| 1066 | .exit = nonstatic_release_resource_db, |
| 1067 | }; |
| 1068 | EXPORT_SYMBOL(pccard_nonstatic_ops); |
| 1069 | |
| 1070 | |
| 1071 | /* sysfs interface to the resource database */ |
| 1072 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1073 | static ssize_t show_io_db(struct device *dev, |
| 1074 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1076 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | struct socket_data *data; |
| 1078 | struct resource_map *p; |
| 1079 | ssize_t ret = 0; |
| 1080 | |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1081 | mutex_lock(&s->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | data = s->resource_data; |
| 1083 | |
| 1084 | for (p = data->io_db.next; p != &data->io_db; p = p->next) { |
| 1085 | if (ret > (PAGE_SIZE - 10)) |
| 1086 | continue; |
Dominik Brodowski | 78e0185 | 2021-10-15 09:13:23 +0200 | [diff] [blame] | 1087 | ret += sysfs_emit_at(buf, ret, |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1088 | "0x%08lx - 0x%08lx\n", |
| 1089 | ((unsigned long) p->base), |
| 1090 | ((unsigned long) p->base + p->num - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1093 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1094 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1097 | static ssize_t store_io_db(struct device *dev, |
| 1098 | struct device_attribute *attr, |
| 1099 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1101 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | unsigned long start_addr, end_addr; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1103 | unsigned int add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | ssize_t ret = 0; |
| 1105 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1106 | ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | if (ret != 2) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1108 | ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1109 | add = REMOVE_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (ret != 2) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1111 | ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr, |
| 1112 | &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1113 | add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (ret != 2) |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | } |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 1118 | if (end_addr < start_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return -EINVAL; |
| 1120 | |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1121 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1122 | ret = adjust_io(s, add, start_addr, end_addr); |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1123 | mutex_unlock(&s->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
| 1125 | return ret ? ret : count; |
| 1126 | } |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1127 | 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] | 1128 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1129 | static ssize_t show_mem_db(struct device *dev, |
| 1130 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1132 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | struct socket_data *data; |
| 1134 | struct resource_map *p; |
| 1135 | ssize_t ret = 0; |
| 1136 | |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1137 | mutex_lock(&s->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | data = s->resource_data; |
| 1139 | |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 1140 | for (p = data->mem_db_valid.next; p != &data->mem_db_valid; |
| 1141 | p = p->next) { |
| 1142 | if (ret > (PAGE_SIZE - 10)) |
| 1143 | continue; |
Dominik Brodowski | 78e0185 | 2021-10-15 09:13:23 +0200 | [diff] [blame] | 1144 | ret += sysfs_emit_at(buf, ret, |
Dominik Brodowski | 7b4884ca | 2010-02-17 16:25:53 +0100 | [diff] [blame] | 1145 | "0x%08lx - 0x%08lx\n", |
| 1146 | ((unsigned long) p->base), |
| 1147 | ((unsigned long) p->base + p->num - 1)); |
| 1148 | } |
| 1149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | for (p = data->mem_db.next; p != &data->mem_db; p = p->next) { |
| 1151 | if (ret > (PAGE_SIZE - 10)) |
| 1152 | continue; |
Dominik Brodowski | 78e0185 | 2021-10-15 09:13:23 +0200 | [diff] [blame] | 1153 | ret += sysfs_emit_at(buf, ret, |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1154 | "0x%08lx - 0x%08lx\n", |
| 1155 | ((unsigned long) p->base), |
| 1156 | ((unsigned long) p->base + p->num - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1159 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1160 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1163 | static ssize_t store_mem_db(struct device *dev, |
| 1164 | struct device_attribute *attr, |
| 1165 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1167 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | unsigned long start_addr, end_addr; |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1169 | unsigned int add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | ssize_t ret = 0; |
| 1171 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1172 | ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | if (ret != 2) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1174 | ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1175 | add = REMOVE_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | if (ret != 2) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1177 | ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr, |
| 1178 | &end_addr); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1179 | add = ADD_MANAGED_RESOURCE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | if (ret != 2) |
| 1181 | return -EINVAL; |
| 1182 | } |
| 1183 | } |
Dominik Brodowski | 1146bc7 | 2005-09-28 19:28:37 +0200 | [diff] [blame] | 1184 | if (end_addr < start_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | return -EINVAL; |
| 1186 | |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1187 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 2291663 | 2005-06-27 16:28:46 -0700 | [diff] [blame] | 1188 | ret = adjust_memory(s, add, start_addr, end_addr); |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 1189 | mutex_unlock(&s->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | return ret ? ret : count; |
| 1192 | } |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1193 | 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] | 1194 | |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1195 | static struct attribute *pccard_rsrc_attributes[] = { |
| 1196 | &dev_attr_available_resources_io.attr, |
| 1197 | &dev_attr_available_resources_mem.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | NULL, |
| 1199 | }; |
| 1200 | |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1201 | static const struct attribute_group rsrc_attributes = { |
| 1202 | .attrs = pccard_rsrc_attributes, |
| 1203 | }; |
| 1204 | |
Greg Kroah-Hartman | 2243acd | 2023-04-02 19:58:49 +0200 | [diff] [blame] | 1205 | static int pccard_sysfs_add_rsrc(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1207 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | if (s->resource_ops != &pccard_nonstatic_ops) |
| 1210 | return 0; |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1211 | return sysfs_create_group(&dev->kobj, &rsrc_attributes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Greg Kroah-Hartman | 2243acd | 2023-04-02 19:58:49 +0200 | [diff] [blame] | 1214 | static void pccard_sysfs_remove_rsrc(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1216 | struct pcmcia_socket *s = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | if (s->resource_ops != &pccard_nonstatic_ops) |
| 1219 | return; |
David Brownell | 7d57896 | 2008-06-12 12:13:55 -0700 | [diff] [blame] | 1220 | sysfs_remove_group(&dev->kobj, &rsrc_attributes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Sam Ravnborg | ed49f5d | 2008-05-01 04:34:50 -0700 | [diff] [blame] | 1223 | static struct class_interface pccard_rsrc_interface __refdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | .class = &pcmcia_socket_class, |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 1225 | .add_dev = &pccard_sysfs_add_rsrc, |
Bill Pemberton | 96364e3 | 2012-11-19 13:20:38 -0500 | [diff] [blame] | 1226 | .remove_dev = &pccard_sysfs_remove_rsrc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | }; |
| 1228 | |
| 1229 | static int __init nonstatic_sysfs_init(void) |
| 1230 | { |
| 1231 | return class_interface_register(&pccard_rsrc_interface); |
| 1232 | } |
| 1233 | |
| 1234 | static void __exit nonstatic_sysfs_exit(void) |
| 1235 | { |
| 1236 | class_interface_unregister(&pccard_rsrc_interface); |
| 1237 | } |
| 1238 | |
| 1239 | module_init(nonstatic_sysfs_init); |
| 1240 | module_exit(nonstatic_sysfs_exit); |