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