blob: 602f574898dc62561f56ce5dfdd5591896da493e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070015#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 Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pci.h>
25#include <linux/ioport.h>
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010026#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/byteorder.h>
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -070028#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <pcmcia/cs_types.h>
31#include <pcmcia/ss.h>
32#include <pcmcia/cs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <pcmcia/cisreg.h>
34#include <pcmcia/cistpl.h>
35#include "cs_internal.h"
36
37static const u_char mantissa[] = {
38 10, 12, 13, 15, 20, 25, 30, 35,
39 40, 45, 50, 55, 60, 70, 80, 90
40};
41
42static 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 Machek37f77952005-09-07 16:00:26 -070061/* 16-bit CIS? */
62static int cis_width;
63module_param(cis_width, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65void release_cis_mem(struct pcmcia_socket *s)
66{
Dominik Brodowski6b8e0872010-01-12 21:42:51 +010067 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 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 Brodowski6b8e0872010-01-12 21:42:51 +010079 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
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 Brodowski7ab248552010-02-17 18:00:07 +010086 *
87 * Must be called with ops_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89static void __iomem *
90set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
91{
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -070092 pccard_mem_map *mem = &s->cis_mem;
93 int ret;
Dominik Brodowski2ad0a0a2005-06-27 16:28:58 -070094
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -070095 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 Brodowski2e55bf62008-08-02 18:08:38 +020098 dev_printk(KERN_NOTICE, &s->dev,
99 "cs: unable to map card memory!\n");
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -0700100 return NULL;
101 }
102 s->cis_virt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Dominik Brodowski2ad0a0a2005-06-27 16:28:58 -0700104
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -0700105 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 Torvalds1da177e2005-04-16 15:20:36 -0700125}
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 Brodowski9fea84f2009-12-07 22:11:45 +0100131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132======================================================================*/
133
134/* Bits in attr field */
135#define IS_ATTR 1
136#define IS_INDIRECT 8
137
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -0700138int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 u_int len, void *ptr)
140{
141 void __iomem *sys, *end;
142 unsigned char *buf = ptr;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100143
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200144 dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dominik Brodowski7ab248552010-02-17 18:00:07 +0100146 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 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 Brodowski7ab248552010-02-17 18:00:07 +0100158 dev_dbg(&s->dev, "could not map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 memset(ptr, 0xff, len);
Dominik Brodowski7ab248552010-02-17 18:00:07 +0100160 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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 Brodowski7ab248552010-02-17 18:00:07 +0100174 if (addr > CISTPL_MAX_CIS_SIZE)
175 dev_dbg(&s->dev, "attempt to read CIS mem at addr %#x", addr);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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 Brodowski7ab248552010-02-17 18:00:07 +0100188 dev_dbg(&s->dev, "could not map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 memset(ptr, 0xff, len);
Dominik Brodowski7ab248552010-02-17 18:00:07 +0100190 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 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 Brodowski7ab248552010-02-17 18:00:07 +0100204 mutex_unlock(&s->ops_mutex);
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200205 dev_dbg(&s->dev, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *(u_char *)(ptr+0), *(u_char *)(ptr+1),
207 *(u_char *)(ptr+2), *(u_char *)(ptr+3));
208 return 0;
209}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -0700212void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 u_int len, void *ptr)
214{
215 void __iomem *sys, *end;
216 unsigned char *buf = ptr;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100217
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200218 dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Dominik Brodowski7ab248552010-02-17 18:00:07 +0100220 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 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 Brodowski7ab248552010-02-17 18:00:07 +0100231 if (!sys) {
232 dev_dbg(&s->dev, "could not map memory\n");
233 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return; /* FIXME: Error */
Dominik Brodowski7ab248552010-02-17 18:00:07 +0100235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
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 Brodowski7ab248552010-02-17 18:00:07 +0100257 if (!sys) {
258 dev_dbg(&s->dev, "could not map memory\n");
259 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return; /* FIXME: error */
Dominik Brodowski7ab248552010-02-17 18:00:07 +0100261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
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 Brodowski7ab248552010-02-17 18:00:07 +0100274 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
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 Brodowski9fea84f2009-12-07 22:11:45 +0100283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284======================================================================*/
285
286static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
Dominik Brodowski53efec92008-07-28 19:44:05 +0200287 size_t len, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Dominik Brodowski57197b92010-01-02 17:27:33 +0100289 struct cis_cache_entry *cis;
290 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dominik Brodowski57197b92010-01-02 17:27:33 +0100292 if (s->state & SOCKET_CARDBUS)
293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100295 mutex_lock(&s->ops_mutex);
Dominik Brodowski57197b92010-01-02 17:27:33 +0100296 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 Brodowski8680c4b2010-01-12 22:05:36 +0100301 mutex_unlock(&s->ops_mutex);
Dominik Brodowski57197b92010-01-02 17:27:33 +0100302 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dominik Brodowski57197b92010-01-02 17:27:33 +0100305 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 Brodowski8680c4b2010-01-12 22:05:36 +0100308 mutex_unlock(&s->ops_mutex);
Dominik Brodowski57197b92010-01-02 17:27:33 +0100309 return;
310 }
311 }
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100312 mutex_unlock(&s->ops_mutex);
Dominik Brodowski57197b92010-01-02 17:27:33 +0100313
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -0700314 ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
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 Brodowski8680c4b2010-01-12 22:05:36 +0100324 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 list_add(&cis->node, &s->cis_cache);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100326 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
329}
330
331static void
332remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
333{
334 struct cis_cache_entry *cis;
335
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100336 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 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 Brodowski8680c4b2010-01-12 22:05:36 +0100343 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Dominik Brodowski904e3772010-01-02 12:28:04 +0100346/**
347 * destroy_cis_cache() - destroy the CIS cache
348 * @s: pcmcia_socket for which CIS cache shall be destroyed
349 *
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100350 * This destroys the CIS cache but keeps any fake CIS alive. Must be
351 * called with ops_mutex held.
Dominik Brodowski904e3772010-01-02 12:28:04 +0100352 */
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354void destroy_cis_cache(struct pcmcia_socket *s)
355{
356 struct list_head *l, *n;
Dominik Brodowski904e3772010-01-02 12:28:04 +0100357 struct cis_cache_entry *cis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 list_for_each_safe(l, n, &s->cis_cache) {
Dominik Brodowski904e3772010-01-02 12:28:04 +0100360 cis = list_entry(l, struct cis_cache_entry, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 list_del(&cis->node);
362 kfree(cis);
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366/*======================================================================
367
368 This verifies if the CIS of a card matches what is in the CIS
369 cache.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371======================================================================*/
372
373int verify_cis_cache(struct pcmcia_socket *s)
374{
375 struct cis_cache_entry *cis;
376 char *buf;
377
Dominik Brodowski57197b92010-01-02 17:27:33 +0100378 if (s->state & SOCKET_CARDBUS)
379 return -EINVAL;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 buf = kmalloc(256, GFP_KERNEL);
Dominik Brodowskie6895972008-11-02 19:55:45 +0100382 if (buf == NULL) {
Dominik Brodowski11683862008-08-03 10:22:47 +0200383 dev_printk(KERN_WARNING, &s->dev,
384 "no memory for verifying CIS\n");
385 return -ENOMEM;
Dominik Brodowskie6895972008-11-02 19:55:45 +0100386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 list_for_each_entry(cis, &s->cis_cache, node) {
388 int len = cis->len;
389
390 if (len > 256)
391 len = 256;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100392
393 pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
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 Brodowski9fea84f2009-12-07 22:11:45 +0100408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409======================================================================*/
410
Dominik Brodowski53efec92008-07-28 19:44:05 +0200411int pcmcia_replace_cis(struct pcmcia_socket *s,
412 const u8 *data, const size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Dominik Brodowski11683862008-08-03 10:22:47 +0200414 if (len > CISTPL_MAX_CIS_SIZE) {
415 dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n");
416 return -EINVAL;
417 }
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100418 mutex_lock(&s->ops_mutex);
Dominik Brodowski11683862008-08-03 10:22:47 +0200419 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 Brodowski8680c4b2010-01-12 22:05:36 +0100423 mutex_unlock(&s->ops_mutex);
Dominik Brodowski11683862008-08-03 10:22:47 +0200424 return -ENOMEM;
425 }
426 s->fake_cis_len = len;
427 memcpy(s->fake_cis, data, len);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +0100428 mutex_unlock(&s->ops_mutex);
Dominik Brodowski11683862008-08-03 10:22:47 +0200429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/*======================================================================
433
434 The high-level CIS tuple services
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436======================================================================*/
437
438typedef 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 Torvalds1da177e2005-04-16 15:20:36 -0700450int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
451{
452 if (!s)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200453 return -EINVAL;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100454
455 if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200456 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 tuple->TupleLink = tuple->Flags = 0;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100458
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 Torvalds1da177e2005-04-16 15:20:36 -0700464 cisdata_t req = tuple->DesiredTuple;
465 tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200466 if (pccard_get_next_tuple(s, function, tuple) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 tuple->DesiredTuple = CISTPL_LINKTARGET;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200468 if (pccard_get_next_tuple(s, function, tuple) != 0)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200469 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else
471 tuple->CISOffset = tuple->TupleLink = 0;
472 tuple->DesiredTuple = req;
473 }
474 return pccard_get_next_tuple(s, function, tuple);
475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477static 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 Harrison6b1e6f62008-04-29 01:03:39 -0700486 ofs = get_unaligned_le32(link + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 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 Brodowski57197b92010-01-02 17:27:33 +0100498 if (SPACE(tuple->Flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* 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
517int 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 Brodowskiffb8da22008-08-03 11:18:00 +0200523 return -EINVAL;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100524 if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200525 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
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 Brodowski9fea84f2009-12-07 22:11:45 +0100540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* End of chain? Follow long link if possible */
542 if (link[0] == CISTPL_END) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100543 ofs = follow_link(s, tuple);
544 if (ofs < 0)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200545 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 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 Brodowski9fea84f2009-12-07 22:11:45 +0100596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (link[0] == tuple->DesiredTuple)
598 break;
599 ofs += link[1] + 2;
600 }
601 if (i == MAX_TUPLES) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200602 dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n");
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200603 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tuple->TupleCode = link[0];
607 tuple->TupleLink = link[1];
608 tuple->CISOffset = ofs + 2;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612/*====================================================================*/
613
614#define _MIN(a, b) (((a) < (b)) ? (a) : (b))
615
616int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple)
617{
618 u_int len;
619
620 if (!s)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200621 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (tuple->TupleLink < tuple->TupleOffset)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200624 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 len = tuple->TupleLink - tuple->TupleOffset;
626 tuple->TupleDataLen = tuple->TupleLink;
627 if (len == 0)
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 read_cis_cache(s, SPACE(tuple->Flags),
630 tuple->CISOffset + tuple->TupleOffset,
631 _MIN(len, tuple->TupleDataMax), tuple->TupleData);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635
636/*======================================================================
637
638 Parsing routines for individual tuples
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640======================================================================*/
641
642static 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 Brodowski9fea84f2009-12-07 22:11:45 +0100653
654 if (*p == 0xff)
655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 device->dev[i].type = (*p >> 4);
657 device->dev[i].wp = (*p & 0x08) ? 1 : 0;
658 switch (*p & 0x07) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100659 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 Torvalds1da177e2005-04-16 15:20:36 -0700674 case 7:
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200675 if (++p == q)
676 return -EINVAL;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100677 device->dev[i].speed = SPEED_CVT(*p);
678 while (*p & 0x80)
679 if (++p == q)
680 return -EINVAL;
681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 default:
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200686 if (++p == q)
687 return -EINVAL;
688 if (*p == 0xff)
689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 scale = *p & 7;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200691 if (scale == 7)
692 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
694 device->ndev++;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200695 if (++p == q)
696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100698
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*====================================================================*/
703
704static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
705{
706 u_char *p;
707 if (tuple->TupleDataLen < 5)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200708 return -EINVAL;
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -0700709 p = (u_char *) tuple->TupleData;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700710 csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2;
711 csum->len = get_unaligned_le16(p + 2);
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -0700712 csum->sum = *(p + 4);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*====================================================================*/
717
718static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
719{
720 if (tuple->TupleDataLen < 4)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200721 return -EINVAL;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700722 link->addr = get_unaligned_le32(tuple->TupleData);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*====================================================================*/
727
728static int parse_longlink_mfc(tuple_t *tuple,
729 cistpl_longlink_mfc_t *link)
730{
731 u_char *p;
732 int i;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 p = (u_char *)tuple->TupleData;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 link->nfn = *p; p++;
737 if (tuple->TupleDataLen <= link->nfn*5)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 for (i = 0; i < link->nfn; i++) {
740 link->fn[i].space = *p; p++;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700741 link->fn[i].addr = get_unaligned_le32(p);
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -0700742 p += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747/*====================================================================*/
748
749static 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 Brodowski3f9c5f42008-08-03 12:22:40 +0200754 if (p == q)
755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ns = 0; j = 0;
757 for (i = 0; i < max; i++) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200758 if (*p == 0xff)
759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 ofs[i] = j;
761 ns++;
762 for (;;) {
763 s[j++] = (*p == 0xff) ? '\0' : *p;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100764 if ((*p == '\0') || (*p == 0xff))
765 break;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200766 if (++p == q)
767 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100769 if ((*p == 0xff) || (++p == q))
770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 if (found) {
773 *found = ns;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 } else {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200776 return (ns == max) ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778}
779
780/*====================================================================*/
781
782static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
783{
784 u_char *p, *q;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 p = (u_char *)tuple->TupleData;
787 q = p + tuple->TupleDataLen;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 vers_1->major = *p; p++;
790 vers_1->minor = *p; p++;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200791 if (p >= q)
792 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
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
800static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
801{
802 u_char *p, *q;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 p = (u_char *)tuple->TupleData;
805 q = p + tuple->TupleDataLen;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
808 altstr->str, altstr->ofs, &altstr->ns);
809}
810
811/*====================================================================*/
812
813static 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 Brodowski9fea84f2009-12-07 22:11:45 +0100822 if (p > q-2)
823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 jedec->id[nid].mfr = p[0];
825 jedec->id[nid].info = p[1];
826 p += 2;
827 }
828 jedec->nid = nid;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*====================================================================*/
833
834static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (tuple->TupleDataLen < 4)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200837 return -EINVAL;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700838 m->manf = get_unaligned_le16(tuple->TupleData);
839 m->card = get_unaligned_le16(tuple->TupleData + 2);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/*====================================================================*/
844
845static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
846{
847 u_char *p;
848 if (tuple->TupleDataLen < 2)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 p = (u_char *)tuple->TupleData;
851 f->func = p[0];
852 f->sysinit = p[1];
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856/*====================================================================*/
857
858static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
859{
860 u_char *p;
861 int i;
862 if (tuple->TupleDataLen < 1)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200863 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 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 Brodowski4c89e882008-08-03 10:07:45 +0200868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*====================================================================*/
872
873static 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 Brodowski3f9c5f42008-08-03 12:22:40 +0200882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 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 Brodowski4c89e882008-08-03 10:07:45 +0200894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
897/*======================================================================
898
899 The following routines are all used to parse the nightmarish
900 config table entries.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902======================================================================*/
903
904static 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 Brodowski9fea84f2009-12-07 22:11:45 +0100910 if (p == q)
911 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 pwr->present = *p;
913 pwr->flags = 0;
914 p++;
915 for (i = 0; i < 7; i++)
916 if (pwr->present & (1<<i)) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100917 if (p == q)
918 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 pwr->param[i] = POWER_CVT(*p);
920 scale = POWER_SCALE(*p);
921 while (*p & 0x80) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100922 if (++p == q)
923 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 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
942static u_char *parse_timing(u_char *p, u_char *q,
943 cistpl_timing_t *timing)
944{
945 u_char scale;
946
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100947 if (p == q)
948 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 scale = *p;
950 if ((scale & 3) != 3) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100951 if (++p == q)
952 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 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 Brodowski9fea84f2009-12-07 22:11:45 +0100959 if (++p == q)
960 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 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 Brodowski9fea84f2009-12-07 22:11:45 +0100967 if (++p == q)
968 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 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
979static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
980{
981 int i, j, bsz, lsz;
982
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100983 if (p == q)
984 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 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 Brodowski9fea84f2009-12-07 22:11:45 +0100993
994 if (++p == q)
995 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 io->nwin = (*p & 0x0f) + 1;
997 bsz = (*p & 0x30) >> 4;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100998 if (bsz == 3)
999 bsz++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 lsz = (*p & 0xc0) >> 6;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001001 if (lsz == 3)
1002 lsz++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 p++;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 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 Brodowski9fea84f2009-12-07 22:11:45 +01001009 if (p == q)
1010 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 io->win[i].base += *p << (j*8);
1012 }
1013 for (j = 0; j < lsz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001014 if (p == q)
1015 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 io->win[i].len += *p << (j*8);
1017 }
1018 }
1019 return p;
1020}
1021
1022/*====================================================================*/
1023
1024static 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 Brodowski9fea84f2009-12-07 22:11:45 +01001029 if (p == q)
1030 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 mem->nwin = (*p & 0x07) + 1;
1033 lsz = (*p & 0x18) >> 3;
1034 asz = (*p & 0x60) >> 5;
1035 has_ha = (*p & 0x80);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001036 if (++p == q)
1037 return NULL;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 for (i = 0; i < mem->nwin; i++) {
1040 len = ca = ha = 0;
1041 for (j = 0; j < lsz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001042 if (p == q)
1043 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 len += *p << (j*8);
1045 }
1046 for (j = 0; j < asz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001047 if (p == q)
1048 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ca += *p << (j*8);
1050 }
1051 if (has_ha)
1052 for (j = 0; j < asz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001053 if (p == q)
1054 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 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
1066static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
1067{
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001068 if (p == q)
1069 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 irq->IRQInfo1 = *p; p++;
1071 if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001072 if (p+2 > q)
1073 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 irq->IRQInfo2 = (p[1]<<8) + p[0];
1075 p += 2;
1076 }
1077 return p;
1078}
1079
1080/*====================================================================*/
1081
1082static 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 Brodowski3f9c5f42008-08-03 12:22:40 +02001094 if (++p == q)
1095 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 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 Brodowski3f9c5f42008-08-03 12:22:40 +02001109 if (++p == q)
1110 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 features = *p; p++;
1112
1113 /* Power options */
1114 if ((features & 3) > 0) {
1115 p = parse_power(p, q, &entry->vcc);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001116 if (p == NULL)
1117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 } else
1119 entry->vcc.present = 0;
1120 if ((features & 3) > 1) {
1121 p = parse_power(p, q, &entry->vpp1);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001122 if (p == NULL)
1123 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 } else
1125 entry->vpp1.present = 0;
1126 if ((features & 3) > 2) {
1127 p = parse_power(p, q, &entry->vpp2);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001128 if (p == NULL)
1129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 } else
1131 entry->vpp2.present = 0;
1132
1133 /* Timing options */
1134 if (features & 0x04) {
1135 p = parse_timing(p, q, &entry->timing);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001136 if (p == NULL)
1137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 } else {
1139 entry->timing.wait = 0;
1140 entry->timing.ready = 0;
1141 entry->timing.reserved = 0;
1142 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* I/O window options */
1145 if (features & 0x08) {
1146 p = parse_io(p, q, &entry->io);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001147 if (p == NULL)
1148 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 } else
1150 entry->io.nwin = 0;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* Interrupt options */
1153 if (features & 0x10) {
1154 p = parse_irq(p, q, &entry->irq);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001155 if (p == NULL)
1156 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } 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 Harrison6b1e6f62008-04-29 01:03:39 -07001166 entry->mem.win[0].len = get_unaligned_le16(p) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 entry->mem.win[0].card_addr = 0;
1168 entry->mem.win[0].host_addr = 0;
1169 p += 2;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001170 if (p > q)
1171 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 case 0x40:
1174 entry->mem.nwin = 1;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -07001175 entry->mem.win[0].len = get_unaligned_le16(p) << 8;
1176 entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 entry->mem.win[0].host_addr = 0;
1178 p += 4;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001179 if (p > q)
1180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182 case 0x60:
1183 p = parse_mem(p, q, &entry->mem);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001184 if (p == NULL)
1185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 break;
1187 }
1188
1189 /* Misc features */
1190 if (features & 0x80) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001191 if (p == q)
1192 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 entry->flags |= (*p << 8);
1194 while (*p & 0x80)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001195 if (++p == q)
1196 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 p++;
1198 }
1199
1200 entry->subtuples = q-p;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001201
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205/*====================================================================*/
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207static 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 Brodowski9fea84f2009-12-07 22:11:45 +01001216 if (p > q-6)
1217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 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 Brodowski4c89e882008-08-03 10:07:45 +02001227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230/*====================================================================*/
1231
1232static 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 Brodowski3f9c5f42008-08-03 12:22:40 +02001237 return -EINVAL;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 p = tuple->TupleData;
1240 q = p + tuple->TupleDataLen;
1241
1242 v2->vers = p[0];
1243 v2->comply = p[1];
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001244 v2->dindex = get_unaligned_le16(p + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 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
1254static int parse_org(tuple_t *tuple, cistpl_org_t *org)
1255{
1256 u_char *p, *q;
1257 int i;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 p = tuple->TupleData;
1260 q = p + tuple->TupleDataLen;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001261 if (p == q)
1262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 org->data_org = *p;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001264 if (++p == q)
1265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 for (i = 0; i < 30; i++) {
1267 org->desc[i] = *p;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001268 if (*p == '\0')
1269 break;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001270 if (++p == q)
1271 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276/*====================================================================*/
1277
1278static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
1279{
1280 u_char *p;
1281
1282 if (tuple->TupleDataLen < 10)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001283 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 p = tuple->TupleData;
1286
1287 fmt->type = p[0];
1288 fmt->edc = p[1];
Harvey Harrison6b1e6f62008-04-29 01:03:39 -07001289 fmt->offset = get_unaligned_le32(p + 2);
1290 fmt->length = get_unaligned_le32(p + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/*====================================================================*/
1296
Dominik Brodowski2f3061e2008-08-31 15:50:33 +02001297int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001299 int ret = 0;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (tuple->TupleDataLen > tuple->TupleDataMax)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 switch (tuple->TupleCode) {
1304 case CISTPL_DEVICE:
1305 case CISTPL_DEVICE_A:
1306 ret = parse_device(tuple, &parse->device);
1307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 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 Brodowski4c89e882008-08-03 10:07:45 +02001359 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 default:
Dominik Brodowskide6405e2008-08-03 10:47:59 +02001362 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
1364 }
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001365 if (ret)
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001366 pr_debug("parse_tuple failed %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return ret;
1368}
Dominik Brodowski2f3061e2008-08-31 15:50:33 +02001369EXPORT_SYMBOL(pcmcia_parse_tuple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371/*======================================================================
1372
1373 This is used internally by Card Services to look up CIS stuff.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375======================================================================*/
1376
1377int 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 Brodowski11683862008-08-03 10:22:47 +02001384 if (buf == NULL) {
1385 dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
1386 return -ENOMEM;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 tuple.DesiredTuple = code;
Dominik Brodowski84897fc2009-10-18 23:51:09 +02001389 tuple.Attributes = 0;
1390 if (function == BIND_FN_ALL)
1391 tuple.Attributes = TUPLE_RETURN_COMMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ret = pccard_get_first_tuple(s, function, &tuple);
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001393 if (ret != 0)
1394 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 tuple.TupleData = buf;
1396 tuple.TupleOffset = 0;
1397 tuple.TupleDataMax = 255;
1398 ret = pccard_get_tuple_data(s, &tuple);
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001399 if (ret != 0)
1400 goto done;
Dominik Brodowski2f3061e2008-08-31 15:50:33 +02001401 ret = pcmcia_parse_tuple(&tuple, parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402done:
1403 kfree(buf);
1404 return ret;
1405}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Dominik Brodowski91284222009-10-18 23:32:33 +02001407
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 */
1423int 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
1458next_entry:
1459 ret = pccard_get_next_tuple(s, function, &tuple);
1460 }
1461
1462 kfree(buf);
1463 return ret;
1464}
Dominik Brodowski91284222009-10-18 23:32:33 +02001465
1466
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001467/**
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 Brodowski84897fc2009-10-18 23:51:09 +02001480int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001482 tuple_t *tuple;
1483 cisparse_t *p;
1484 unsigned int count = 0;
1485 int ret, reserved, dev_ok = 0, ident_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001487 if (!s)
1488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001490 /* We do not want to validate the CIS cache... */
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001491 mutex_lock(&s->ops_mutex);
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001492 destroy_cis_cache(s);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001493 mutex_unlock(&s->ops_mutex);
Dominik Brodowski904e3772010-01-02 12:28:04 +01001494
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001495 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 Torvalds1da177e2005-04-16 15:20:36 -07001506
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001507 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 Brodowski4c89e882008-08-03 10:07:45 +02001511 if (ret != 0)
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001512 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 Torvalds1da177e2005-04-16 15:20:36 -07001546
1547done:
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001548 /* invalidate CIS cache on failure */
1549 if (!dev_ok || !ident_ok || !count) {
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001550 mutex_lock(&s->ops_mutex);
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001551 destroy_cis_cache(s);
Dominik Brodowski8680c4b2010-01-12 22:05:36 +01001552 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001553 ret = -EIO;
1554 }
Dominik Brodowski904e3772010-01-02 12:28:04 +01001555
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001556 if (info)
1557 *info = count;
1558 kfree(tuple);
1559 kfree(p);
1560 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001562
1563
1564#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
1565
1566static 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
1635static 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
1666static 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 Brodowskiaf461fc2010-01-17 19:30:53 +01001688 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001689
1690 return count;
1691}
1692
1693
1694struct 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};