Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * cistpl.c -- 16-bit PCMCIA Card Information Structure parser |
| 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/kernel.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/major.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/timer.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/pci.h> |
| 25 | #include <linux/ioport.h> |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 26 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/byteorder.h> |
Daniel Ritz | dc0cf6a | 2007-10-16 01:23:52 -0700 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <pcmcia/cs_types.h> |
| 31 | #include <pcmcia/ss.h> |
| 32 | #include <pcmcia/cs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <pcmcia/cisreg.h> |
| 34 | #include <pcmcia/cistpl.h> |
| 35 | #include "cs_internal.h" |
| 36 | |
| 37 | static const u_char mantissa[] = { |
| 38 | 10, 12, 13, 15, 20, 25, 30, 35, |
| 39 | 40, 45, 50, 55, 60, 70, 80, 90 |
| 40 | }; |
| 41 | |
| 42 | static const u_int exponent[] = { |
| 43 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 |
| 44 | }; |
| 45 | |
| 46 | /* Convert an extended speed byte to a time in nanoseconds */ |
| 47 | #define SPEED_CVT(v) \ |
| 48 | (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10) |
| 49 | /* Convert a power byte to a current in 0.1 microamps */ |
| 50 | #define POWER_CVT(v) \ |
| 51 | (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10) |
| 52 | #define POWER_SCALE(v) (exponent[(v)&7]) |
| 53 | |
| 54 | /* Upper limit on reasonable # of tuples */ |
| 55 | #define MAX_TUPLES 200 |
| 56 | |
| 57 | /*====================================================================*/ |
| 58 | |
| 59 | /* Parameters that can be set with 'insmod' */ |
| 60 | |
Pavel Machek | 37f7795 | 2005-09-07 16:00:26 -0700 | [diff] [blame] | 61 | /* 16-bit CIS? */ |
| 62 | static int cis_width; |
| 63 | module_param(cis_width, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | void release_cis_mem(struct pcmcia_socket *s) |
| 66 | { |
| 67 | if (s->cis_mem.flags & MAP_ACTIVE) { |
| 68 | s->cis_mem.flags &= ~MAP_ACTIVE; |
| 69 | s->ops->set_mem_map(s, &s->cis_mem); |
| 70 | if (s->cis_mem.res) { |
| 71 | release_resource(s->cis_mem.res); |
| 72 | kfree(s->cis_mem.res); |
| 73 | s->cis_mem.res = NULL; |
| 74 | } |
| 75 | iounmap(s->cis_virt); |
| 76 | s->cis_virt = NULL; |
| 77 | } |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Map the card memory at "card_offset" into virtual space. |
| 82 | * If flags & MAP_ATTRIB, map the attribute space, otherwise |
| 83 | * map the memory space. |
| 84 | */ |
| 85 | static void __iomem * |
| 86 | set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags) |
| 87 | { |
Dominik Brodowski | 2e5a3e7 | 2005-07-28 01:07:23 -0700 | [diff] [blame] | 88 | pccard_mem_map *mem = &s->cis_mem; |
| 89 | int ret; |
Dominik Brodowski | 2ad0a0a | 2005-06-27 16:28:58 -0700 | [diff] [blame] | 90 | |
Dominik Brodowski | 2e5a3e7 | 2005-07-28 01:07:23 -0700 | [diff] [blame] | 91 | if (!(s->features & SS_CAP_STATIC_MAP) && (mem->res == NULL)) { |
| 92 | mem->res = pcmcia_find_mem_region(0, s->map_size, s->map_size, 0, s); |
| 93 | if (mem->res == NULL) { |
Dominik Brodowski | 2e55bf6 | 2008-08-02 18:08:38 +0200 | [diff] [blame] | 94 | dev_printk(KERN_NOTICE, &s->dev, |
| 95 | "cs: unable to map card memory!\n"); |
Dominik Brodowski | 2e5a3e7 | 2005-07-28 01:07:23 -0700 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | s->cis_virt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Dominik Brodowski | 2ad0a0a | 2005-06-27 16:28:58 -0700 | [diff] [blame] | 100 | |
Dominik Brodowski | 2e5a3e7 | 2005-07-28 01:07:23 -0700 | [diff] [blame] | 101 | if (!(s->features & SS_CAP_STATIC_MAP) && (!s->cis_virt)) |
| 102 | s->cis_virt = ioremap(mem->res->start, s->map_size); |
| 103 | |
| 104 | mem->card_start = card_offset; |
| 105 | mem->flags = flags; |
| 106 | |
| 107 | ret = s->ops->set_mem_map(s, mem); |
| 108 | if (ret) { |
| 109 | iounmap(s->cis_virt); |
| 110 | s->cis_virt = NULL; |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | if (s->features & SS_CAP_STATIC_MAP) { |
| 115 | if (s->cis_virt) |
| 116 | iounmap(s->cis_virt); |
| 117 | s->cis_virt = ioremap(mem->static_start, s->map_size); |
| 118 | } |
| 119 | |
| 120 | return s->cis_virt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /*====================================================================== |
| 124 | |
| 125 | Low-level functions to read and write CIS memory. I think the |
| 126 | write routine is only useful for writing one-byte registers. |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | ======================================================================*/ |
| 129 | |
| 130 | /* Bits in attr field */ |
| 131 | #define IS_ATTR 1 |
| 132 | #define IS_INDIRECT 8 |
| 133 | |
Dominik Brodowski | e6ea0b9e | 2005-06-27 16:28:52 -0700 | [diff] [blame] | 134 | int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | u_int len, void *ptr) |
| 136 | { |
| 137 | void __iomem *sys, *end; |
| 138 | unsigned char *buf = ptr; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 139 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 140 | dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | if (attr & IS_INDIRECT) { |
| 143 | /* Indirect accesses use a bunch of special registers at fixed |
| 144 | locations in common memory */ |
| 145 | u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN; |
| 146 | if (attr & IS_ATTR) { |
| 147 | addr *= 2; |
| 148 | flags = ICTRL0_AUTOINC; |
| 149 | } |
| 150 | |
| 151 | sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0)); |
| 152 | if (!sys) { |
| 153 | memset(ptr, 0xff, len); |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | writeb(flags, sys+CISREG_ICTRL0); |
| 158 | writeb(addr & 0xff, sys+CISREG_IADDR0); |
| 159 | writeb((addr>>8) & 0xff, sys+CISREG_IADDR1); |
| 160 | writeb((addr>>16) & 0xff, sys+CISREG_IADDR2); |
| 161 | writeb((addr>>24) & 0xff, sys+CISREG_IADDR3); |
| 162 | for ( ; len > 0; len--, buf++) |
| 163 | *buf = readb(sys+CISREG_IDATA0); |
| 164 | } else { |
| 165 | u_int inc = 1, card_offset, flags; |
| 166 | |
| 167 | flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0); |
| 168 | if (attr) { |
| 169 | flags |= MAP_ATTRIB; |
| 170 | inc++; |
| 171 | addr *= 2; |
| 172 | } |
| 173 | |
| 174 | card_offset = addr & ~(s->map_size-1); |
| 175 | while (len) { |
| 176 | sys = set_cis_map(s, card_offset, flags); |
| 177 | if (!sys) { |
| 178 | memset(ptr, 0xff, len); |
| 179 | return -1; |
| 180 | } |
| 181 | end = sys + s->map_size; |
| 182 | sys = sys + (addr & (s->map_size-1)); |
| 183 | for ( ; len > 0; len--, buf++, sys += inc) { |
| 184 | if (sys == end) |
| 185 | break; |
| 186 | *buf = readb(sys); |
| 187 | } |
| 188 | card_offset += s->map_size; |
| 189 | addr = 0; |
| 190 | } |
| 191 | } |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 192 | dev_dbg(&s->dev, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | *(u_char *)(ptr+0), *(u_char *)(ptr+1), |
| 194 | *(u_char *)(ptr+2), *(u_char *)(ptr+3)); |
| 195 | return 0; |
| 196 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Dominik Brodowski | e6ea0b9e | 2005-06-27 16:28:52 -0700 | [diff] [blame] | 199 | void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | u_int len, void *ptr) |
| 201 | { |
| 202 | void __iomem *sys, *end; |
| 203 | unsigned char *buf = ptr; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 204 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 205 | dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | if (attr & IS_INDIRECT) { |
| 208 | /* Indirect accesses use a bunch of special registers at fixed |
| 209 | locations in common memory */ |
| 210 | u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN; |
| 211 | if (attr & IS_ATTR) { |
| 212 | addr *= 2; |
| 213 | flags = ICTRL0_AUTOINC; |
| 214 | } |
| 215 | |
| 216 | sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0)); |
| 217 | if (!sys) |
| 218 | return; /* FIXME: Error */ |
| 219 | |
| 220 | writeb(flags, sys+CISREG_ICTRL0); |
| 221 | writeb(addr & 0xff, sys+CISREG_IADDR0); |
| 222 | writeb((addr>>8) & 0xff, sys+CISREG_IADDR1); |
| 223 | writeb((addr>>16) & 0xff, sys+CISREG_IADDR2); |
| 224 | writeb((addr>>24) & 0xff, sys+CISREG_IADDR3); |
| 225 | for ( ; len > 0; len--, buf++) |
| 226 | writeb(*buf, sys+CISREG_IDATA0); |
| 227 | } else { |
| 228 | u_int inc = 1, card_offset, flags; |
| 229 | |
| 230 | flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0); |
| 231 | if (attr & IS_ATTR) { |
| 232 | flags |= MAP_ATTRIB; |
| 233 | inc++; |
| 234 | addr *= 2; |
| 235 | } |
| 236 | |
| 237 | card_offset = addr & ~(s->map_size-1); |
| 238 | while (len) { |
| 239 | sys = set_cis_map(s, card_offset, flags); |
| 240 | if (!sys) |
| 241 | return; /* FIXME: error */ |
| 242 | |
| 243 | end = sys + s->map_size; |
| 244 | sys = sys + (addr & (s->map_size-1)); |
| 245 | for ( ; len > 0; len--, buf++, sys += inc) { |
| 246 | if (sys == end) |
| 247 | break; |
| 248 | writeb(*buf, sys); |
| 249 | } |
| 250 | card_offset += s->map_size; |
| 251 | addr = 0; |
| 252 | } |
| 253 | } |
| 254 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /*====================================================================== |
| 258 | |
| 259 | This is a wrapper around read_cis_mem, with the same interface, |
| 260 | but which caches information, for cards whose CIS may not be |
| 261 | readable all the time. |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | ======================================================================*/ |
| 264 | |
| 265 | static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, |
Dominik Brodowski | 53efec9 | 2008-07-28 19:44:05 +0200 | [diff] [blame] | 266 | size_t len, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 268 | struct cis_cache_entry *cis; |
| 269 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 271 | if (s->state & SOCKET_CARDBUS) |
| 272 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 274 | if (s->fake_cis) { |
| 275 | if (s->fake_cis_len >= addr+len) |
| 276 | memcpy(ptr, s->fake_cis+addr, len); |
| 277 | else |
| 278 | memset(ptr, 0xff, len); |
| 279 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 282 | list_for_each_entry(cis, &s->cis_cache, node) { |
| 283 | if (cis->addr == addr && cis->len == len && cis->attr == attr) { |
| 284 | memcpy(ptr, cis->cache, len); |
| 285 | return; |
| 286 | } |
| 287 | } |
| 288 | |
Dominik Brodowski | e6ea0b9e | 2005-06-27 16:28:52 -0700 | [diff] [blame] | 289 | ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (ret == 0) { |
| 292 | /* Copy data into the cache */ |
| 293 | cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL); |
| 294 | if (cis) { |
| 295 | cis->addr = addr; |
| 296 | cis->len = len; |
| 297 | cis->attr = attr; |
| 298 | memcpy(cis->cache, ptr, len); |
| 299 | list_add(&cis->node, &s->cis_cache); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len) |
| 306 | { |
| 307 | struct cis_cache_entry *cis; |
| 308 | |
| 309 | list_for_each_entry(cis, &s->cis_cache, node) |
| 310 | if (cis->addr == addr && cis->len == len && cis->attr == attr) { |
| 311 | list_del(&cis->node); |
| 312 | kfree(cis); |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 317 | /** |
| 318 | * destroy_cis_cache() - destroy the CIS cache |
| 319 | * @s: pcmcia_socket for which CIS cache shall be destroyed |
| 320 | * |
| 321 | * This destroys the CIS cache but keeps any fake CIS alive. |
| 322 | */ |
| 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | void destroy_cis_cache(struct pcmcia_socket *s) |
| 325 | { |
| 326 | struct list_head *l, *n; |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 327 | struct cis_cache_entry *cis; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | list_for_each_safe(l, n, &s->cis_cache) { |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 330 | cis = list_entry(l, struct cis_cache_entry, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | list_del(&cis->node); |
| 332 | kfree(cis); |
| 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /*====================================================================== |
| 337 | |
| 338 | This verifies if the CIS of a card matches what is in the CIS |
| 339 | cache. |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | ======================================================================*/ |
| 342 | |
| 343 | int verify_cis_cache(struct pcmcia_socket *s) |
| 344 | { |
| 345 | struct cis_cache_entry *cis; |
| 346 | char *buf; |
| 347 | |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 348 | if (s->state & SOCKET_CARDBUS) |
| 349 | return -EINVAL; |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | buf = kmalloc(256, GFP_KERNEL); |
Dominik Brodowski | e689597 | 2008-11-02 19:55:45 +0100 | [diff] [blame] | 352 | if (buf == NULL) { |
Dominik Brodowski | 1168386 | 2008-08-03 10:22:47 +0200 | [diff] [blame] | 353 | dev_printk(KERN_WARNING, &s->dev, |
| 354 | "no memory for verifying CIS\n"); |
| 355 | return -ENOMEM; |
Dominik Brodowski | e689597 | 2008-11-02 19:55:45 +0100 | [diff] [blame] | 356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | list_for_each_entry(cis, &s->cis_cache, node) { |
| 358 | int len = cis->len; |
| 359 | |
| 360 | if (len > 256) |
| 361 | len = 256; |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 362 | |
| 363 | pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | if (memcmp(buf, cis->cache, len) != 0) { |
| 366 | kfree(buf); |
| 367 | return -1; |
| 368 | } |
| 369 | } |
| 370 | kfree(buf); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /*====================================================================== |
| 375 | |
| 376 | For really bad cards, we provide a facility for uploading a |
| 377 | replacement CIS. |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | ======================================================================*/ |
| 380 | |
Dominik Brodowski | 53efec9 | 2008-07-28 19:44:05 +0200 | [diff] [blame] | 381 | int pcmcia_replace_cis(struct pcmcia_socket *s, |
| 382 | const u8 *data, const size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Dominik Brodowski | 1168386 | 2008-08-03 10:22:47 +0200 | [diff] [blame] | 384 | if (len > CISTPL_MAX_CIS_SIZE) { |
| 385 | dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n"); |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | kfree(s->fake_cis); |
| 389 | s->fake_cis = kmalloc(len, GFP_KERNEL); |
| 390 | if (s->fake_cis == NULL) { |
| 391 | dev_printk(KERN_WARNING, &s->dev, "no memory to replace CIS\n"); |
| 392 | return -ENOMEM; |
| 393 | } |
| 394 | s->fake_cis_len = len; |
| 395 | memcpy(s->fake_cis, data, len); |
| 396 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | /*====================================================================== |
| 400 | |
| 401 | The high-level CIS tuple services |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | ======================================================================*/ |
| 404 | |
| 405 | typedef struct tuple_flags { |
| 406 | u_int link_space:4; |
| 407 | u_int has_link:1; |
| 408 | u_int mfc_fn:3; |
| 409 | u_int space:4; |
| 410 | } tuple_flags; |
| 411 | |
| 412 | #define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space) |
| 413 | #define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link) |
| 414 | #define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn) |
| 415 | #define SPACE(f) (((tuple_flags *)(&(f)))->space) |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple) |
| 418 | { |
| 419 | if (!s) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 420 | return -EINVAL; |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 421 | |
| 422 | if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS)) |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 423 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | tuple->TupleLink = tuple->Flags = 0; |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 425 | |
| 426 | /* Assume presence of a LONGLINK_C to address 0 */ |
| 427 | tuple->CISOffset = tuple->LinkOffset = 0; |
| 428 | SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1; |
| 429 | |
| 430 | if ((s->functions > 1) && !(tuple->Attributes & TUPLE_RETURN_COMMON)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | cisdata_t req = tuple->DesiredTuple; |
| 432 | tuple->DesiredTuple = CISTPL_LONGLINK_MFC; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 433 | if (pccard_get_next_tuple(s, function, tuple) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | tuple->DesiredTuple = CISTPL_LINKTARGET; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 435 | if (pccard_get_next_tuple(s, function, tuple) != 0) |
Dominik Brodowski | 635d19b | 2008-08-03 11:47:29 +0200 | [diff] [blame] | 436 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } else |
| 438 | tuple->CISOffset = tuple->TupleLink = 0; |
| 439 | tuple->DesiredTuple = req; |
| 440 | } |
| 441 | return pccard_get_next_tuple(s, function, tuple); |
| 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | static int follow_link(struct pcmcia_socket *s, tuple_t *tuple) |
| 445 | { |
| 446 | u_char link[5]; |
| 447 | u_int ofs; |
| 448 | |
| 449 | if (MFC_FN(tuple->Flags)) { |
| 450 | /* Get indirect link from the MFC tuple */ |
| 451 | read_cis_cache(s, LINK_SPACE(tuple->Flags), |
| 452 | tuple->LinkOffset, 5, link); |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 453 | ofs = get_unaligned_le32(link + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR); |
| 455 | /* Move to the next indirect link */ |
| 456 | tuple->LinkOffset += 5; |
| 457 | MFC_FN(tuple->Flags)--; |
| 458 | } else if (HAS_LINK(tuple->Flags)) { |
| 459 | ofs = tuple->LinkOffset; |
| 460 | SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags); |
| 461 | HAS_LINK(tuple->Flags) = 0; |
| 462 | } else { |
| 463 | return -1; |
| 464 | } |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 465 | if (SPACE(tuple->Flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | /* This is ugly, but a common CIS error is to code the long |
| 467 | link offset incorrectly, so we check the right spot... */ |
| 468 | read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link); |
| 469 | if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) && |
| 470 | (strncmp(link+2, "CIS", 3) == 0)) |
| 471 | return ofs; |
| 472 | remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5); |
| 473 | /* Then, we try the wrong spot... */ |
| 474 | ofs = ofs >> 1; |
| 475 | } |
| 476 | read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link); |
| 477 | if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) && |
| 478 | (strncmp(link+2, "CIS", 3) == 0)) |
| 479 | return ofs; |
| 480 | remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5); |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple) |
| 485 | { |
| 486 | u_char link[2], tmp; |
| 487 | int ofs, i, attr; |
| 488 | |
| 489 | if (!s) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 490 | return -EINVAL; |
Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 491 | if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS)) |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 492 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | link[1] = tuple->TupleLink; |
| 495 | ofs = tuple->CISOffset + tuple->TupleLink; |
| 496 | attr = SPACE(tuple->Flags); |
| 497 | |
| 498 | for (i = 0; i < MAX_TUPLES; i++) { |
| 499 | if (link[1] == 0xff) { |
| 500 | link[0] = CISTPL_END; |
| 501 | } else { |
| 502 | read_cis_cache(s, attr, ofs, 2, link); |
| 503 | if (link[0] == CISTPL_NULL) { |
| 504 | ofs++; continue; |
| 505 | } |
| 506 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | /* End of chain? Follow long link if possible */ |
| 509 | if (link[0] == CISTPL_END) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 510 | ofs = follow_link(s, tuple); |
| 511 | if (ofs < 0) |
Dominik Brodowski | 635d19b | 2008-08-03 11:47:29 +0200 | [diff] [blame] | 512 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | attr = SPACE(tuple->Flags); |
| 514 | read_cis_cache(s, attr, ofs, 2, link); |
| 515 | } |
| 516 | |
| 517 | /* Is this a link tuple? Make a note of it */ |
| 518 | if ((link[0] == CISTPL_LONGLINK_A) || |
| 519 | (link[0] == CISTPL_LONGLINK_C) || |
| 520 | (link[0] == CISTPL_LONGLINK_MFC) || |
| 521 | (link[0] == CISTPL_LINKTARGET) || |
| 522 | (link[0] == CISTPL_INDIRECT) || |
| 523 | (link[0] == CISTPL_NO_LINK)) { |
| 524 | switch (link[0]) { |
| 525 | case CISTPL_LONGLINK_A: |
| 526 | HAS_LINK(tuple->Flags) = 1; |
| 527 | LINK_SPACE(tuple->Flags) = attr | IS_ATTR; |
| 528 | read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset); |
| 529 | break; |
| 530 | case CISTPL_LONGLINK_C: |
| 531 | HAS_LINK(tuple->Flags) = 1; |
| 532 | LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR; |
| 533 | read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset); |
| 534 | break; |
| 535 | case CISTPL_INDIRECT: |
| 536 | HAS_LINK(tuple->Flags) = 1; |
| 537 | LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT; |
| 538 | tuple->LinkOffset = 0; |
| 539 | break; |
| 540 | case CISTPL_LONGLINK_MFC: |
| 541 | tuple->LinkOffset = ofs + 3; |
| 542 | LINK_SPACE(tuple->Flags) = attr; |
| 543 | if (function == BIND_FN_ALL) { |
| 544 | /* Follow all the MFC links */ |
| 545 | read_cis_cache(s, attr, ofs+2, 1, &tmp); |
| 546 | MFC_FN(tuple->Flags) = tmp; |
| 547 | } else { |
| 548 | /* Follow exactly one of the links */ |
| 549 | MFC_FN(tuple->Flags) = 1; |
| 550 | tuple->LinkOffset += function * 5; |
| 551 | } |
| 552 | break; |
| 553 | case CISTPL_NO_LINK: |
| 554 | HAS_LINK(tuple->Flags) = 0; |
| 555 | break; |
| 556 | } |
| 557 | if ((tuple->Attributes & TUPLE_RETURN_LINK) && |
| 558 | (tuple->DesiredTuple == RETURN_FIRST_TUPLE)) |
| 559 | break; |
| 560 | } else |
| 561 | if (tuple->DesiredTuple == RETURN_FIRST_TUPLE) |
| 562 | break; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (link[0] == tuple->DesiredTuple) |
| 565 | break; |
| 566 | ofs += link[1] + 2; |
| 567 | } |
| 568 | if (i == MAX_TUPLES) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 569 | dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n"); |
Dominik Brodowski | 635d19b | 2008-08-03 11:47:29 +0200 | [diff] [blame] | 570 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | tuple->TupleCode = link[0]; |
| 574 | tuple->TupleLink = link[1]; |
| 575 | tuple->CISOffset = ofs + 2; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 576 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | /*====================================================================*/ |
| 580 | |
| 581 | #define _MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 582 | |
| 583 | int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple) |
| 584 | { |
| 585 | u_int len; |
| 586 | |
| 587 | if (!s) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 588 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | if (tuple->TupleLink < tuple->TupleOffset) |
Dominik Brodowski | 635d19b | 2008-08-03 11:47:29 +0200 | [diff] [blame] | 591 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | len = tuple->TupleLink - tuple->TupleOffset; |
| 593 | tuple->TupleDataLen = tuple->TupleLink; |
| 594 | if (len == 0) |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | read_cis_cache(s, SPACE(tuple->Flags), |
| 597 | tuple->CISOffset + tuple->TupleOffset, |
| 598 | _MIN(len, tuple->TupleDataMax), tuple->TupleData); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 599 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | |
| 603 | /*====================================================================== |
| 604 | |
| 605 | Parsing routines for individual tuples |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | ======================================================================*/ |
| 608 | |
| 609 | static int parse_device(tuple_t *tuple, cistpl_device_t *device) |
| 610 | { |
| 611 | int i; |
| 612 | u_char scale; |
| 613 | u_char *p, *q; |
| 614 | |
| 615 | p = (u_char *)tuple->TupleData; |
| 616 | q = p + tuple->TupleDataLen; |
| 617 | |
| 618 | device->ndev = 0; |
| 619 | for (i = 0; i < CISTPL_MAX_DEVICES; i++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 620 | |
| 621 | if (*p == 0xff) |
| 622 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | device->dev[i].type = (*p >> 4); |
| 624 | device->dev[i].wp = (*p & 0x08) ? 1 : 0; |
| 625 | switch (*p & 0x07) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 626 | case 0: |
| 627 | device->dev[i].speed = 0; |
| 628 | break; |
| 629 | case 1: |
| 630 | device->dev[i].speed = 250; |
| 631 | break; |
| 632 | case 2: |
| 633 | device->dev[i].speed = 200; |
| 634 | break; |
| 635 | case 3: |
| 636 | device->dev[i].speed = 150; |
| 637 | break; |
| 638 | case 4: |
| 639 | device->dev[i].speed = 100; |
| 640 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | case 7: |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 642 | if (++p == q) |
| 643 | return -EINVAL; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 644 | device->dev[i].speed = SPEED_CVT(*p); |
| 645 | while (*p & 0x80) |
| 646 | if (++p == q) |
| 647 | return -EINVAL; |
| 648 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | default: |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 650 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 653 | if (++p == q) |
| 654 | return -EINVAL; |
| 655 | if (*p == 0xff) |
| 656 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | scale = *p & 7; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 658 | if (scale == 7) |
| 659 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2)); |
| 661 | device->ndev++; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 662 | if (++p == q) |
| 663 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 665 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 666 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /*====================================================================*/ |
| 670 | |
| 671 | static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum) |
| 672 | { |
| 673 | u_char *p; |
| 674 | if (tuple->TupleDataLen < 5) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 675 | return -EINVAL; |
Daniel Ritz | dc0cf6a | 2007-10-16 01:23:52 -0700 | [diff] [blame] | 676 | p = (u_char *) tuple->TupleData; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 677 | csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2; |
| 678 | csum->len = get_unaligned_le16(p + 2); |
Daniel Ritz | dc0cf6a | 2007-10-16 01:23:52 -0700 | [diff] [blame] | 679 | csum->sum = *(p + 4); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /*====================================================================*/ |
| 684 | |
| 685 | static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link) |
| 686 | { |
| 687 | if (tuple->TupleDataLen < 4) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 688 | return -EINVAL; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 689 | link->addr = get_unaligned_le32(tuple->TupleData); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 690 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /*====================================================================*/ |
| 694 | |
| 695 | static int parse_longlink_mfc(tuple_t *tuple, |
| 696 | cistpl_longlink_mfc_t *link) |
| 697 | { |
| 698 | u_char *p; |
| 699 | int i; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | p = (u_char *)tuple->TupleData; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | link->nfn = *p; p++; |
| 704 | if (tuple->TupleDataLen <= link->nfn*5) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 705 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | for (i = 0; i < link->nfn; i++) { |
| 707 | link->fn[i].space = *p; p++; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 708 | link->fn[i].addr = get_unaligned_le32(p); |
Daniel Ritz | dc0cf6a | 2007-10-16 01:23:52 -0700 | [diff] [blame] | 709 | p += 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 711 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /*====================================================================*/ |
| 715 | |
| 716 | static int parse_strings(u_char *p, u_char *q, int max, |
| 717 | char *s, u_char *ofs, u_char *found) |
| 718 | { |
| 719 | int i, j, ns; |
| 720 | |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 721 | if (p == q) |
| 722 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | ns = 0; j = 0; |
| 724 | for (i = 0; i < max; i++) { |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 725 | if (*p == 0xff) |
| 726 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | ofs[i] = j; |
| 728 | ns++; |
| 729 | for (;;) { |
| 730 | s[j++] = (*p == 0xff) ? '\0' : *p; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 731 | if ((*p == '\0') || (*p == 0xff)) |
| 732 | break; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 733 | if (++p == q) |
| 734 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 736 | if ((*p == 0xff) || (++p == q)) |
| 737 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | if (found) { |
| 740 | *found = ns; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 741 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } else { |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 743 | return (ns == max) ? 0 : -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | /*====================================================================*/ |
| 748 | |
| 749 | static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1) |
| 750 | { |
| 751 | u_char *p, *q; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | p = (u_char *)tuple->TupleData; |
| 754 | q = p + tuple->TupleDataLen; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | vers_1->major = *p; p++; |
| 757 | vers_1->minor = *p; p++; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 758 | if (p >= q) |
| 759 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
| 761 | return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS, |
| 762 | vers_1->str, vers_1->ofs, &vers_1->ns); |
| 763 | } |
| 764 | |
| 765 | /*====================================================================*/ |
| 766 | |
| 767 | static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr) |
| 768 | { |
| 769 | u_char *p, *q; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | p = (u_char *)tuple->TupleData; |
| 772 | q = p + tuple->TupleDataLen; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS, |
| 775 | altstr->str, altstr->ofs, &altstr->ns); |
| 776 | } |
| 777 | |
| 778 | /*====================================================================*/ |
| 779 | |
| 780 | static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec) |
| 781 | { |
| 782 | u_char *p, *q; |
| 783 | int nid; |
| 784 | |
| 785 | p = (u_char *)tuple->TupleData; |
| 786 | q = p + tuple->TupleDataLen; |
| 787 | |
| 788 | for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 789 | if (p > q-2) |
| 790 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | jedec->id[nid].mfr = p[0]; |
| 792 | jedec->id[nid].info = p[1]; |
| 793 | p += 2; |
| 794 | } |
| 795 | jedec->nid = nid; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 796 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /*====================================================================*/ |
| 800 | |
| 801 | static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m) |
| 802 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if (tuple->TupleDataLen < 4) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 804 | return -EINVAL; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 805 | m->manf = get_unaligned_le16(tuple->TupleData); |
| 806 | m->card = get_unaligned_le16(tuple->TupleData + 2); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 807 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /*====================================================================*/ |
| 811 | |
| 812 | static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f) |
| 813 | { |
| 814 | u_char *p; |
| 815 | if (tuple->TupleDataLen < 2) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 816 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | p = (u_char *)tuple->TupleData; |
| 818 | f->func = p[0]; |
| 819 | f->sysinit = p[1]; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 820 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /*====================================================================*/ |
| 824 | |
| 825 | static int parse_funce(tuple_t *tuple, cistpl_funce_t *f) |
| 826 | { |
| 827 | u_char *p; |
| 828 | int i; |
| 829 | if (tuple->TupleDataLen < 1) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 830 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | p = (u_char *)tuple->TupleData; |
| 832 | f->type = p[0]; |
| 833 | for (i = 1; i < tuple->TupleDataLen; i++) |
| 834 | f->data[i-1] = p[i]; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 835 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /*====================================================================*/ |
| 839 | |
| 840 | static int parse_config(tuple_t *tuple, cistpl_config_t *config) |
| 841 | { |
| 842 | int rasz, rmsz, i; |
| 843 | u_char *p; |
| 844 | |
| 845 | p = (u_char *)tuple->TupleData; |
| 846 | rasz = *p & 0x03; |
| 847 | rmsz = (*p & 0x3c) >> 2; |
| 848 | if (tuple->TupleDataLen < rasz+rmsz+4) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 849 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | config->last_idx = *(++p); |
| 851 | p++; |
| 852 | config->base = 0; |
| 853 | for (i = 0; i <= rasz; i++) |
| 854 | config->base += p[i] << (8*i); |
| 855 | p += rasz+1; |
| 856 | for (i = 0; i < 4; i++) |
| 857 | config->rmask[i] = 0; |
| 858 | for (i = 0; i <= rmsz; i++) |
| 859 | config->rmask[i>>2] += p[i] << (8*(i%4)); |
| 860 | config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 861 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /*====================================================================== |
| 865 | |
| 866 | The following routines are all used to parse the nightmarish |
| 867 | config table entries. |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | ======================================================================*/ |
| 870 | |
| 871 | static u_char *parse_power(u_char *p, u_char *q, |
| 872 | cistpl_power_t *pwr) |
| 873 | { |
| 874 | int i; |
| 875 | u_int scale; |
| 876 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 877 | if (p == q) |
| 878 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | pwr->present = *p; |
| 880 | pwr->flags = 0; |
| 881 | p++; |
| 882 | for (i = 0; i < 7; i++) |
| 883 | if (pwr->present & (1<<i)) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 884 | if (p == q) |
| 885 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | pwr->param[i] = POWER_CVT(*p); |
| 887 | scale = POWER_SCALE(*p); |
| 888 | while (*p & 0x80) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 889 | if (++p == q) |
| 890 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | if ((*p & 0x7f) < 100) |
| 892 | pwr->param[i] += (*p & 0x7f) * scale / 100; |
| 893 | else if (*p == 0x7d) |
| 894 | pwr->flags |= CISTPL_POWER_HIGHZ_OK; |
| 895 | else if (*p == 0x7e) |
| 896 | pwr->param[i] = 0; |
| 897 | else if (*p == 0x7f) |
| 898 | pwr->flags |= CISTPL_POWER_HIGHZ_REQ; |
| 899 | else |
| 900 | return NULL; |
| 901 | } |
| 902 | p++; |
| 903 | } |
| 904 | return p; |
| 905 | } |
| 906 | |
| 907 | /*====================================================================*/ |
| 908 | |
| 909 | static u_char *parse_timing(u_char *p, u_char *q, |
| 910 | cistpl_timing_t *timing) |
| 911 | { |
| 912 | u_char scale; |
| 913 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 914 | if (p == q) |
| 915 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | scale = *p; |
| 917 | if ((scale & 3) != 3) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 918 | if (++p == q) |
| 919 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | timing->wait = SPEED_CVT(*p); |
| 921 | timing->waitscale = exponent[scale & 3]; |
| 922 | } else |
| 923 | timing->wait = 0; |
| 924 | scale >>= 2; |
| 925 | if ((scale & 7) != 7) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 926 | if (++p == q) |
| 927 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | timing->ready = SPEED_CVT(*p); |
| 929 | timing->rdyscale = exponent[scale & 7]; |
| 930 | } else |
| 931 | timing->ready = 0; |
| 932 | scale >>= 3; |
| 933 | if (scale != 7) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 934 | if (++p == q) |
| 935 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | timing->reserved = SPEED_CVT(*p); |
| 937 | timing->rsvscale = exponent[scale]; |
| 938 | } else |
| 939 | timing->reserved = 0; |
| 940 | p++; |
| 941 | return p; |
| 942 | } |
| 943 | |
| 944 | /*====================================================================*/ |
| 945 | |
| 946 | static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io) |
| 947 | { |
| 948 | int i, j, bsz, lsz; |
| 949 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 950 | if (p == q) |
| 951 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | io->flags = *p; |
| 953 | |
| 954 | if (!(*p & 0x80)) { |
| 955 | io->nwin = 1; |
| 956 | io->win[0].base = 0; |
| 957 | io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK)); |
| 958 | return p+1; |
| 959 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 960 | |
| 961 | if (++p == q) |
| 962 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | io->nwin = (*p & 0x0f) + 1; |
| 964 | bsz = (*p & 0x30) >> 4; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 965 | if (bsz == 3) |
| 966 | bsz++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | lsz = (*p & 0xc0) >> 6; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 968 | if (lsz == 3) |
| 969 | lsz++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | p++; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | for (i = 0; i < io->nwin; i++) { |
| 973 | io->win[i].base = 0; |
| 974 | io->win[i].len = 1; |
| 975 | for (j = 0; j < bsz; j++, p++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 976 | if (p == q) |
| 977 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | io->win[i].base += *p << (j*8); |
| 979 | } |
| 980 | for (j = 0; j < lsz; j++, p++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 981 | if (p == q) |
| 982 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | io->win[i].len += *p << (j*8); |
| 984 | } |
| 985 | } |
| 986 | return p; |
| 987 | } |
| 988 | |
| 989 | /*====================================================================*/ |
| 990 | |
| 991 | static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem) |
| 992 | { |
| 993 | int i, j, asz, lsz, has_ha; |
| 994 | u_int len, ca, ha; |
| 995 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 996 | if (p == q) |
| 997 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | mem->nwin = (*p & 0x07) + 1; |
| 1000 | lsz = (*p & 0x18) >> 3; |
| 1001 | asz = (*p & 0x60) >> 5; |
| 1002 | has_ha = (*p & 0x80); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1003 | if (++p == q) |
| 1004 | return NULL; |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | for (i = 0; i < mem->nwin; i++) { |
| 1007 | len = ca = ha = 0; |
| 1008 | for (j = 0; j < lsz; j++, p++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1009 | if (p == q) |
| 1010 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | len += *p << (j*8); |
| 1012 | } |
| 1013 | for (j = 0; j < asz; j++, p++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1014 | if (p == q) |
| 1015 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | ca += *p << (j*8); |
| 1017 | } |
| 1018 | if (has_ha) |
| 1019 | for (j = 0; j < asz; j++, p++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1020 | if (p == q) |
| 1021 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | ha += *p << (j*8); |
| 1023 | } |
| 1024 | mem->win[i].len = len << 8; |
| 1025 | mem->win[i].card_addr = ca << 8; |
| 1026 | mem->win[i].host_addr = ha << 8; |
| 1027 | } |
| 1028 | return p; |
| 1029 | } |
| 1030 | |
| 1031 | /*====================================================================*/ |
| 1032 | |
| 1033 | static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq) |
| 1034 | { |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1035 | if (p == q) |
| 1036 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | irq->IRQInfo1 = *p; p++; |
| 1038 | if (irq->IRQInfo1 & IRQ_INFO2_VALID) { |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1039 | if (p+2 > q) |
| 1040 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | irq->IRQInfo2 = (p[1]<<8) + p[0]; |
| 1042 | p += 2; |
| 1043 | } |
| 1044 | return p; |
| 1045 | } |
| 1046 | |
| 1047 | /*====================================================================*/ |
| 1048 | |
| 1049 | static int parse_cftable_entry(tuple_t *tuple, |
| 1050 | cistpl_cftable_entry_t *entry) |
| 1051 | { |
| 1052 | u_char *p, *q, features; |
| 1053 | |
| 1054 | p = tuple->TupleData; |
| 1055 | q = p + tuple->TupleDataLen; |
| 1056 | entry->index = *p & 0x3f; |
| 1057 | entry->flags = 0; |
| 1058 | if (*p & 0x40) |
| 1059 | entry->flags |= CISTPL_CFTABLE_DEFAULT; |
| 1060 | if (*p & 0x80) { |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1061 | if (++p == q) |
| 1062 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | if (*p & 0x10) |
| 1064 | entry->flags |= CISTPL_CFTABLE_BVDS; |
| 1065 | if (*p & 0x20) |
| 1066 | entry->flags |= CISTPL_CFTABLE_WP; |
| 1067 | if (*p & 0x40) |
| 1068 | entry->flags |= CISTPL_CFTABLE_RDYBSY; |
| 1069 | if (*p & 0x80) |
| 1070 | entry->flags |= CISTPL_CFTABLE_MWAIT; |
| 1071 | entry->interface = *p & 0x0f; |
| 1072 | } else |
| 1073 | entry->interface = 0; |
| 1074 | |
| 1075 | /* Process optional features */ |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1076 | if (++p == q) |
| 1077 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | features = *p; p++; |
| 1079 | |
| 1080 | /* Power options */ |
| 1081 | if ((features & 3) > 0) { |
| 1082 | p = parse_power(p, q, &entry->vcc); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1083 | if (p == NULL) |
| 1084 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } else |
| 1086 | entry->vcc.present = 0; |
| 1087 | if ((features & 3) > 1) { |
| 1088 | p = parse_power(p, q, &entry->vpp1); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1089 | if (p == NULL) |
| 1090 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } else |
| 1092 | entry->vpp1.present = 0; |
| 1093 | if ((features & 3) > 2) { |
| 1094 | p = parse_power(p, q, &entry->vpp2); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1095 | if (p == NULL) |
| 1096 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } else |
| 1098 | entry->vpp2.present = 0; |
| 1099 | |
| 1100 | /* Timing options */ |
| 1101 | if (features & 0x04) { |
| 1102 | p = parse_timing(p, q, &entry->timing); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1103 | if (p == NULL) |
| 1104 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } else { |
| 1106 | entry->timing.wait = 0; |
| 1107 | entry->timing.ready = 0; |
| 1108 | entry->timing.reserved = 0; |
| 1109 | } |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | /* I/O window options */ |
| 1112 | if (features & 0x08) { |
| 1113 | p = parse_io(p, q, &entry->io); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1114 | if (p == NULL) |
| 1115 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } else |
| 1117 | entry->io.nwin = 0; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* Interrupt options */ |
| 1120 | if (features & 0x10) { |
| 1121 | p = parse_irq(p, q, &entry->irq); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1122 | if (p == NULL) |
| 1123 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } else |
| 1125 | entry->irq.IRQInfo1 = 0; |
| 1126 | |
| 1127 | switch (features & 0x60) { |
| 1128 | case 0x00: |
| 1129 | entry->mem.nwin = 0; |
| 1130 | break; |
| 1131 | case 0x20: |
| 1132 | entry->mem.nwin = 1; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 1133 | entry->mem.win[0].len = get_unaligned_le16(p) << 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | entry->mem.win[0].card_addr = 0; |
| 1135 | entry->mem.win[0].host_addr = 0; |
| 1136 | p += 2; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1137 | if (p > q) |
| 1138 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | break; |
| 1140 | case 0x40: |
| 1141 | entry->mem.nwin = 1; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 1142 | entry->mem.win[0].len = get_unaligned_le16(p) << 8; |
| 1143 | entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | entry->mem.win[0].host_addr = 0; |
| 1145 | p += 4; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1146 | if (p > q) |
| 1147 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | break; |
| 1149 | case 0x60: |
| 1150 | p = parse_mem(p, q, &entry->mem); |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1151 | if (p == NULL) |
| 1152 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | /* Misc features */ |
| 1157 | if (features & 0x80) { |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1158 | if (p == q) |
| 1159 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | entry->flags |= (*p << 8); |
| 1161 | while (*p & 0x80) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1162 | if (++p == q) |
| 1163 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | p++; |
| 1165 | } |
| 1166 | |
| 1167 | entry->subtuples = q-p; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1168 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1169 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /*====================================================================*/ |
| 1173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo) |
| 1175 | { |
| 1176 | u_char *p, *q; |
| 1177 | int n; |
| 1178 | |
| 1179 | p = (u_char *)tuple->TupleData; |
| 1180 | q = p + tuple->TupleDataLen; |
| 1181 | |
| 1182 | for (n = 0; n < CISTPL_MAX_DEVICES; n++) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1183 | if (p > q-6) |
| 1184 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | geo->geo[n].buswidth = p[0]; |
| 1186 | geo->geo[n].erase_block = 1 << (p[1]-1); |
| 1187 | geo->geo[n].read_block = 1 << (p[2]-1); |
| 1188 | geo->geo[n].write_block = 1 << (p[3]-1); |
| 1189 | geo->geo[n].partition = 1 << (p[4]-1); |
| 1190 | geo->geo[n].interleave = 1 << (p[5]-1); |
| 1191 | p += 6; |
| 1192 | } |
| 1193 | geo->ngeo = n; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1194 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /*====================================================================*/ |
| 1198 | |
| 1199 | static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2) |
| 1200 | { |
| 1201 | u_char *p, *q; |
| 1202 | |
| 1203 | if (tuple->TupleDataLen < 10) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1204 | return -EINVAL; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | p = tuple->TupleData; |
| 1207 | q = p + tuple->TupleDataLen; |
| 1208 | |
| 1209 | v2->vers = p[0]; |
| 1210 | v2->comply = p[1]; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1211 | v2->dindex = get_unaligned_le16(p + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | v2->vspec8 = p[6]; |
| 1213 | v2->vspec9 = p[7]; |
| 1214 | v2->nhdr = p[8]; |
| 1215 | p += 9; |
| 1216 | return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL); |
| 1217 | } |
| 1218 | |
| 1219 | /*====================================================================*/ |
| 1220 | |
| 1221 | static int parse_org(tuple_t *tuple, cistpl_org_t *org) |
| 1222 | { |
| 1223 | u_char *p, *q; |
| 1224 | int i; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | p = tuple->TupleData; |
| 1227 | q = p + tuple->TupleDataLen; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1228 | if (p == q) |
| 1229 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | org->data_org = *p; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1231 | if (++p == q) |
| 1232 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | for (i = 0; i < 30; i++) { |
| 1234 | org->desc[i] = *p; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1235 | if (*p == '\0') |
| 1236 | break; |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1237 | if (++p == q) |
| 1238 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1240 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /*====================================================================*/ |
| 1244 | |
| 1245 | static int parse_format(tuple_t *tuple, cistpl_format_t *fmt) |
| 1246 | { |
| 1247 | u_char *p; |
| 1248 | |
| 1249 | if (tuple->TupleDataLen < 10) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1250 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
| 1252 | p = tuple->TupleData; |
| 1253 | |
| 1254 | fmt->type = p[0]; |
| 1255 | fmt->edc = p[1]; |
Harvey Harrison | 6b1e6f6 | 2008-04-29 01:03:39 -0700 | [diff] [blame] | 1256 | fmt->offset = get_unaligned_le32(p + 2); |
| 1257 | fmt->length = get_unaligned_le32(p + 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1259 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /*====================================================================*/ |
| 1263 | |
Dominik Brodowski | 2f3061e | 2008-08-31 15:50:33 +0200 | [diff] [blame] | 1264 | int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1266 | int ret = 0; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (tuple->TupleDataLen > tuple->TupleDataMax) |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1269 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | switch (tuple->TupleCode) { |
| 1271 | case CISTPL_DEVICE: |
| 1272 | case CISTPL_DEVICE_A: |
| 1273 | ret = parse_device(tuple, &parse->device); |
| 1274 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | case CISTPL_CHECKSUM: |
| 1276 | ret = parse_checksum(tuple, &parse->checksum); |
| 1277 | break; |
| 1278 | case CISTPL_LONGLINK_A: |
| 1279 | case CISTPL_LONGLINK_C: |
| 1280 | ret = parse_longlink(tuple, &parse->longlink); |
| 1281 | break; |
| 1282 | case CISTPL_LONGLINK_MFC: |
| 1283 | ret = parse_longlink_mfc(tuple, &parse->longlink_mfc); |
| 1284 | break; |
| 1285 | case CISTPL_VERS_1: |
| 1286 | ret = parse_vers_1(tuple, &parse->version_1); |
| 1287 | break; |
| 1288 | case CISTPL_ALTSTR: |
| 1289 | ret = parse_altstr(tuple, &parse->altstr); |
| 1290 | break; |
| 1291 | case CISTPL_JEDEC_A: |
| 1292 | case CISTPL_JEDEC_C: |
| 1293 | ret = parse_jedec(tuple, &parse->jedec); |
| 1294 | break; |
| 1295 | case CISTPL_MANFID: |
| 1296 | ret = parse_manfid(tuple, &parse->manfid); |
| 1297 | break; |
| 1298 | case CISTPL_FUNCID: |
| 1299 | ret = parse_funcid(tuple, &parse->funcid); |
| 1300 | break; |
| 1301 | case CISTPL_FUNCE: |
| 1302 | ret = parse_funce(tuple, &parse->funce); |
| 1303 | break; |
| 1304 | case CISTPL_CONFIG: |
| 1305 | ret = parse_config(tuple, &parse->config); |
| 1306 | break; |
| 1307 | case CISTPL_CFTABLE_ENTRY: |
| 1308 | ret = parse_cftable_entry(tuple, &parse->cftable_entry); |
| 1309 | break; |
| 1310 | case CISTPL_DEVICE_GEO: |
| 1311 | case CISTPL_DEVICE_GEO_A: |
| 1312 | ret = parse_device_geo(tuple, &parse->device_geo); |
| 1313 | break; |
| 1314 | case CISTPL_VERS_2: |
| 1315 | ret = parse_vers_2(tuple, &parse->vers_2); |
| 1316 | break; |
| 1317 | case CISTPL_ORG: |
| 1318 | ret = parse_org(tuple, &parse->org); |
| 1319 | break; |
| 1320 | case CISTPL_FORMAT: |
| 1321 | case CISTPL_FORMAT_A: |
| 1322 | ret = parse_format(tuple, &parse->format); |
| 1323 | break; |
| 1324 | case CISTPL_NO_LINK: |
| 1325 | case CISTPL_LINKTARGET: |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1326 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | break; |
| 1328 | default: |
Dominik Brodowski | de6405e | 2008-08-03 10:47:59 +0200 | [diff] [blame] | 1329 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | break; |
| 1331 | } |
Dominik Brodowski | 3f9c5f4 | 2008-08-03 12:22:40 +0200 | [diff] [blame] | 1332 | if (ret) |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 1333 | pr_debug("parse_tuple failed %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | return ret; |
| 1335 | } |
Dominik Brodowski | 2f3061e | 2008-08-31 15:50:33 +0200 | [diff] [blame] | 1336 | EXPORT_SYMBOL(pcmcia_parse_tuple); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | /*====================================================================== |
| 1339 | |
| 1340 | This is used internally by Card Services to look up CIS stuff. |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | ======================================================================*/ |
| 1343 | |
| 1344 | int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse) |
| 1345 | { |
| 1346 | tuple_t tuple; |
| 1347 | cisdata_t *buf; |
| 1348 | int ret; |
| 1349 | |
| 1350 | buf = kmalloc(256, GFP_KERNEL); |
Dominik Brodowski | 1168386 | 2008-08-03 10:22:47 +0200 | [diff] [blame] | 1351 | if (buf == NULL) { |
| 1352 | dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n"); |
| 1353 | return -ENOMEM; |
| 1354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | tuple.DesiredTuple = code; |
Dominik Brodowski | 84897fc | 2009-10-18 23:51:09 +0200 | [diff] [blame] | 1356 | tuple.Attributes = 0; |
| 1357 | if (function == BIND_FN_ALL) |
| 1358 | tuple.Attributes = TUPLE_RETURN_COMMON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | ret = pccard_get_first_tuple(s, function, &tuple); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1360 | if (ret != 0) |
| 1361 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | tuple.TupleData = buf; |
| 1363 | tuple.TupleOffset = 0; |
| 1364 | tuple.TupleDataMax = 255; |
| 1365 | ret = pccard_get_tuple_data(s, &tuple); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1366 | if (ret != 0) |
| 1367 | goto done; |
Dominik Brodowski | 2f3061e | 2008-08-31 15:50:33 +0200 | [diff] [blame] | 1368 | ret = pcmcia_parse_tuple(&tuple, parse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | done: |
| 1370 | kfree(buf); |
| 1371 | return ret; |
| 1372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1374 | |
| 1375 | /** |
| 1376 | * pccard_loop_tuple() - loop over tuples in the CIS |
| 1377 | * @s: the struct pcmcia_socket where the card is inserted |
| 1378 | * @function: the device function we loop for |
| 1379 | * @code: which CIS code shall we look for? |
| 1380 | * @parse: buffer where the tuple shall be parsed (or NULL, if no parse) |
| 1381 | * @priv_data: private data to be passed to the loop_tuple function. |
| 1382 | * @loop_tuple: function to call for each CIS entry of type @function. IT |
| 1383 | * gets passed the raw tuple, the paresed tuple (if @parse is |
| 1384 | * set) and @priv_data. |
| 1385 | * |
| 1386 | * pccard_loop_tuple() loops over all CIS entries of type @function, and |
| 1387 | * calls the @loop_tuple function for each entry. If the call to @loop_tuple |
| 1388 | * returns 0, the loop exits. Returns 0 on success or errorcode otherwise. |
| 1389 | */ |
| 1390 | int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function, |
| 1391 | cisdata_t code, cisparse_t *parse, void *priv_data, |
| 1392 | int (*loop_tuple) (tuple_t *tuple, |
| 1393 | cisparse_t *parse, |
| 1394 | void *priv_data)) |
| 1395 | { |
| 1396 | tuple_t tuple; |
| 1397 | cisdata_t *buf; |
| 1398 | int ret; |
| 1399 | |
| 1400 | buf = kzalloc(256, GFP_KERNEL); |
| 1401 | if (buf == NULL) { |
| 1402 | dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n"); |
| 1403 | return -ENOMEM; |
| 1404 | } |
| 1405 | |
| 1406 | tuple.TupleData = buf; |
| 1407 | tuple.TupleDataMax = 255; |
| 1408 | tuple.TupleOffset = 0; |
| 1409 | tuple.DesiredTuple = code; |
| 1410 | tuple.Attributes = 0; |
| 1411 | |
| 1412 | ret = pccard_get_first_tuple(s, function, &tuple); |
| 1413 | while (!ret) { |
| 1414 | if (pccard_get_tuple_data(s, &tuple)) |
| 1415 | goto next_entry; |
| 1416 | |
| 1417 | if (parse) |
| 1418 | if (pcmcia_parse_tuple(&tuple, parse)) |
| 1419 | goto next_entry; |
| 1420 | |
| 1421 | ret = loop_tuple(&tuple, parse, priv_data); |
| 1422 | if (!ret) |
| 1423 | break; |
| 1424 | |
| 1425 | next_entry: |
| 1426 | ret = pccard_get_next_tuple(s, function, &tuple); |
| 1427 | } |
| 1428 | |
| 1429 | kfree(buf); |
| 1430 | return ret; |
| 1431 | } |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1432 | |
| 1433 | |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1434 | /** |
| 1435 | * pccard_validate_cis() - check whether card has a sensible CIS |
| 1436 | * @s: the struct pcmcia_socket we are to check |
| 1437 | * @info: returns the number of tuples in the (valid) CIS, or 0 |
| 1438 | * |
| 1439 | * This tries to determine if a card has a sensible CIS. In @info, it |
| 1440 | * returns the number of tuples in the CIS, or 0 if the CIS looks bad. The |
| 1441 | * checks include making sure several critical tuples are present and |
| 1442 | * valid; seeing if the total number of tuples is reasonable; and |
| 1443 | * looking for tuples that use reserved codes. |
| 1444 | * |
| 1445 | * The function returns 0 on success. |
| 1446 | */ |
Dominik Brodowski | 84897fc | 2009-10-18 23:51:09 +0200 | [diff] [blame] | 1447 | int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | { |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1449 | tuple_t *tuple; |
| 1450 | cisparse_t *p; |
| 1451 | unsigned int count = 0; |
| 1452 | int ret, reserved, dev_ok = 0, ident_ok = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1454 | if (!s) |
| 1455 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1457 | /* We do not want to validate the CIS cache... */ |
| 1458 | destroy_cis_cache(s); |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 1459 | |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1460 | tuple = kmalloc(sizeof(*tuple), GFP_KERNEL); |
| 1461 | if (tuple == NULL) { |
| 1462 | dev_warn(&s->dev, "no memory to validate CIS\n"); |
| 1463 | return -ENOMEM; |
| 1464 | } |
| 1465 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 1466 | if (p == NULL) { |
| 1467 | kfree(tuple); |
| 1468 | dev_warn(&s->dev, "no memory to validate CIS\n"); |
| 1469 | return -ENOMEM; |
| 1470 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1472 | count = reserved = 0; |
| 1473 | tuple->DesiredTuple = RETURN_FIRST_TUPLE; |
| 1474 | tuple->Attributes = TUPLE_RETURN_COMMON; |
| 1475 | ret = pccard_get_first_tuple(s, BIND_FN_ALL, tuple); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1476 | if (ret != 0) |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1477 | goto done; |
| 1478 | |
| 1479 | /* First tuple should be DEVICE; we should really have either that |
| 1480 | or a CFTABLE_ENTRY of some sort */ |
| 1481 | if ((tuple->TupleCode == CISTPL_DEVICE) || |
| 1482 | (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY, p)) || |
| 1483 | (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY_CB, p))) |
| 1484 | dev_ok++; |
| 1485 | |
| 1486 | /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2 |
| 1487 | tuple, for card identification. Certain old D-Link and Linksys |
| 1488 | cards have only a broken VERS_2 tuple; hence the bogus test. */ |
| 1489 | if ((pccard_read_tuple(s, BIND_FN_ALL, CISTPL_MANFID, p) == 0) || |
| 1490 | (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_1, p) == 0) || |
| 1491 | (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_2, p) != -ENOSPC)) |
| 1492 | ident_ok++; |
| 1493 | |
| 1494 | if (!dev_ok && !ident_ok) |
| 1495 | goto done; |
| 1496 | |
| 1497 | for (count = 1; count < MAX_TUPLES; count++) { |
| 1498 | ret = pccard_get_next_tuple(s, BIND_FN_ALL, tuple); |
| 1499 | if (ret != 0) |
| 1500 | break; |
| 1501 | if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) || |
| 1502 | ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) || |
| 1503 | ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff))) |
| 1504 | reserved++; |
| 1505 | } |
| 1506 | if ((count == MAX_TUPLES) || (reserved > 5) || |
| 1507 | ((!dev_ok || !ident_ok) && (count > 10))) |
| 1508 | count = 0; |
| 1509 | |
| 1510 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
| 1512 | done: |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1513 | /* invalidate CIS cache on failure */ |
| 1514 | if (!dev_ok || !ident_ok || !count) { |
| 1515 | destroy_cis_cache(s); |
| 1516 | ret = -EIO; |
| 1517 | } |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 1518 | |
Dominik Brodowski | f131ddc | 2010-01-02 12:58:10 +0100 | [diff] [blame] | 1519 | if (info) |
| 1520 | *info = count; |
| 1521 | kfree(tuple); |
| 1522 | kfree(p); |
| 1523 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
Dominik Brodowski | 6e7b51a | 2010-01-06 13:57:43 +0100 | [diff] [blame^] | 1525 | |
| 1526 | |
| 1527 | #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev) |
| 1528 | |
| 1529 | static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, |
| 1530 | loff_t off, size_t count) |
| 1531 | { |
| 1532 | tuple_t tuple; |
| 1533 | int status, i; |
| 1534 | loff_t pointer = 0; |
| 1535 | ssize_t ret = 0; |
| 1536 | u_char *tuplebuffer; |
| 1537 | u_char *tempbuffer; |
| 1538 | |
| 1539 | tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL); |
| 1540 | if (!tuplebuffer) |
| 1541 | return -ENOMEM; |
| 1542 | |
| 1543 | tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL); |
| 1544 | if (!tempbuffer) { |
| 1545 | ret = -ENOMEM; |
| 1546 | goto free_tuple; |
| 1547 | } |
| 1548 | |
| 1549 | memset(&tuple, 0, sizeof(tuple_t)); |
| 1550 | |
| 1551 | tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON; |
| 1552 | tuple.DesiredTuple = RETURN_FIRST_TUPLE; |
| 1553 | tuple.TupleOffset = 0; |
| 1554 | |
| 1555 | status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple); |
| 1556 | while (!status) { |
| 1557 | tuple.TupleData = tuplebuffer; |
| 1558 | tuple.TupleDataMax = 255; |
| 1559 | memset(tuplebuffer, 0, sizeof(u_char) * 255); |
| 1560 | |
| 1561 | status = pccard_get_tuple_data(s, &tuple); |
| 1562 | if (status) |
| 1563 | break; |
| 1564 | |
| 1565 | if (off < (pointer + 2 + tuple.TupleDataLen)) { |
| 1566 | tempbuffer[0] = tuple.TupleCode & 0xff; |
| 1567 | tempbuffer[1] = tuple.TupleLink & 0xff; |
| 1568 | for (i = 0; i < tuple.TupleDataLen; i++) |
| 1569 | tempbuffer[i + 2] = tuplebuffer[i] & 0xff; |
| 1570 | |
| 1571 | for (i = 0; i < (2 + tuple.TupleDataLen); i++) { |
| 1572 | if (((i + pointer) >= off) && |
| 1573 | (i + pointer) < (off + count)) { |
| 1574 | buf[ret] = tempbuffer[i]; |
| 1575 | ret++; |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | pointer += 2 + tuple.TupleDataLen; |
| 1581 | |
| 1582 | if (pointer >= (off + count)) |
| 1583 | break; |
| 1584 | |
| 1585 | if (tuple.TupleCode == CISTPL_END) |
| 1586 | break; |
| 1587 | status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple); |
| 1588 | } |
| 1589 | |
| 1590 | kfree(tempbuffer); |
| 1591 | free_tuple: |
| 1592 | kfree(tuplebuffer); |
| 1593 | |
| 1594 | return ret; |
| 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | static ssize_t pccard_show_cis(struct kobject *kobj, |
| 1599 | struct bin_attribute *bin_attr, |
| 1600 | char *buf, loff_t off, size_t count) |
| 1601 | { |
| 1602 | unsigned int size = 0x200; |
| 1603 | |
| 1604 | if (off >= size) |
| 1605 | count = 0; |
| 1606 | else { |
| 1607 | struct pcmcia_socket *s; |
| 1608 | unsigned int chains; |
| 1609 | |
| 1610 | if (off + count > size) |
| 1611 | count = size - off; |
| 1612 | |
| 1613 | s = to_socket(container_of(kobj, struct device, kobj)); |
| 1614 | |
| 1615 | if (!(s->state & SOCKET_PRESENT)) |
| 1616 | return -ENODEV; |
| 1617 | if (pccard_validate_cis(s, &chains)) |
| 1618 | return -EIO; |
| 1619 | if (!chains) |
| 1620 | return -ENODATA; |
| 1621 | |
| 1622 | count = pccard_extract_cis(s, buf, off, count); |
| 1623 | } |
| 1624 | |
| 1625 | return count; |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | static ssize_t pccard_store_cis(struct kobject *kobj, |
| 1630 | struct bin_attribute *bin_attr, |
| 1631 | char *buf, loff_t off, size_t count) |
| 1632 | { |
| 1633 | struct pcmcia_socket *s; |
| 1634 | int error; |
| 1635 | |
| 1636 | s = to_socket(container_of(kobj, struct device, kobj)); |
| 1637 | |
| 1638 | if (off) |
| 1639 | return -EINVAL; |
| 1640 | |
| 1641 | if (count >= CISTPL_MAX_CIS_SIZE) |
| 1642 | return -EINVAL; |
| 1643 | |
| 1644 | if (!(s->state & SOCKET_PRESENT)) |
| 1645 | return -ENODEV; |
| 1646 | |
| 1647 | error = pcmcia_replace_cis(s, buf, count); |
| 1648 | if (error) |
| 1649 | return -EIO; |
| 1650 | |
| 1651 | mutex_lock(&s->skt_mutex); |
| 1652 | if ((s->callback) && (s->state & SOCKET_PRESENT) && |
| 1653 | !(s->state & SOCKET_CARDBUS)) { |
| 1654 | if (try_module_get(s->callback->owner)) { |
| 1655 | s->callback->requery(s, 1); |
| 1656 | module_put(s->callback->owner); |
| 1657 | } |
| 1658 | } |
| 1659 | mutex_unlock(&s->skt_mutex); |
| 1660 | |
| 1661 | return count; |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | struct bin_attribute pccard_cis_attr = { |
| 1666 | .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR }, |
| 1667 | .size = 0x200, |
| 1668 | .read = pccard_show_cis, |
| 1669 | .write = pccard_store_cis, |
| 1670 | }; |