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