blob: 936417c3e79e45798e509fa1c2b9e85f87e5791f [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{
67 if (s->cis_mem.flags & MAP_ACTIVE) {
68 s->cis_mem.flags &= ~MAP_ACTIVE;
69 s->ops->set_mem_map(s, &s->cis_mem);
70 if (s->cis_mem.res) {
71 release_resource(s->cis_mem.res);
72 kfree(s->cis_mem.res);
73 s->cis_mem.res = NULL;
74 }
75 iounmap(s->cis_virt);
76 s->cis_virt = NULL;
77 }
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * Map the card memory at "card_offset" into virtual space.
82 * If flags & MAP_ATTRIB, map the attribute space, otherwise
83 * map the memory space.
84 */
85static void __iomem *
86set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
87{
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -070088 pccard_mem_map *mem = &s->cis_mem;
89 int ret;
Dominik Brodowski2ad0a0a2005-06-27 16:28:58 -070090
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -070091 if (!(s->features & SS_CAP_STATIC_MAP) && (mem->res == NULL)) {
92 mem->res = pcmcia_find_mem_region(0, s->map_size, s->map_size, 0, s);
93 if (mem->res == NULL) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +020094 dev_printk(KERN_NOTICE, &s->dev,
95 "cs: unable to map card memory!\n");
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -070096 return NULL;
97 }
98 s->cis_virt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Dominik Brodowski2ad0a0a2005-06-27 16:28:58 -0700100
Dominik Brodowski2e5a3e72005-07-28 01:07:23 -0700101 if (!(s->features & SS_CAP_STATIC_MAP) && (!s->cis_virt))
102 s->cis_virt = ioremap(mem->res->start, s->map_size);
103
104 mem->card_start = card_offset;
105 mem->flags = flags;
106
107 ret = s->ops->set_mem_map(s, mem);
108 if (ret) {
109 iounmap(s->cis_virt);
110 s->cis_virt = NULL;
111 return NULL;
112 }
113
114 if (s->features & SS_CAP_STATIC_MAP) {
115 if (s->cis_virt)
116 iounmap(s->cis_virt);
117 s->cis_virt = ioremap(mem->static_start, s->map_size);
118 }
119
120 return s->cis_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/*======================================================================
124
125 Low-level functions to read and write CIS memory. I think the
126 write routine is only useful for writing one-byte registers.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128======================================================================*/
129
130/* Bits in attr field */
131#define IS_ATTR 1
132#define IS_INDIRECT 8
133
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -0700134int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 u_int len, void *ptr)
136{
137 void __iomem *sys, *end;
138 unsigned char *buf = ptr;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100139
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200140 dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (attr & IS_INDIRECT) {
143 /* Indirect accesses use a bunch of special registers at fixed
144 locations in common memory */
145 u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
146 if (attr & IS_ATTR) {
147 addr *= 2;
148 flags = ICTRL0_AUTOINC;
149 }
150
151 sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
152 if (!sys) {
153 memset(ptr, 0xff, len);
154 return -1;
155 }
156
157 writeb(flags, sys+CISREG_ICTRL0);
158 writeb(addr & 0xff, sys+CISREG_IADDR0);
159 writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
160 writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
161 writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
162 for ( ; len > 0; len--, buf++)
163 *buf = readb(sys+CISREG_IDATA0);
164 } else {
165 u_int inc = 1, card_offset, flags;
166
167 flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
168 if (attr) {
169 flags |= MAP_ATTRIB;
170 inc++;
171 addr *= 2;
172 }
173
174 card_offset = addr & ~(s->map_size-1);
175 while (len) {
176 sys = set_cis_map(s, card_offset, flags);
177 if (!sys) {
178 memset(ptr, 0xff, len);
179 return -1;
180 }
181 end = sys + s->map_size;
182 sys = sys + (addr & (s->map_size-1));
183 for ( ; len > 0; len--, buf++, sys += inc) {
184 if (sys == end)
185 break;
186 *buf = readb(sys);
187 }
188 card_offset += s->map_size;
189 addr = 0;
190 }
191 }
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200192 dev_dbg(&s->dev, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *(u_char *)(ptr+0), *(u_char *)(ptr+1),
194 *(u_char *)(ptr+2), *(u_char *)(ptr+3));
195 return 0;
196}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -0700199void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 u_int len, void *ptr)
201{
202 void __iomem *sys, *end;
203 unsigned char *buf = ptr;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100204
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200205 dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (attr & IS_INDIRECT) {
208 /* Indirect accesses use a bunch of special registers at fixed
209 locations in common memory */
210 u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
211 if (attr & IS_ATTR) {
212 addr *= 2;
213 flags = ICTRL0_AUTOINC;
214 }
215
216 sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
217 if (!sys)
218 return; /* FIXME: Error */
219
220 writeb(flags, sys+CISREG_ICTRL0);
221 writeb(addr & 0xff, sys+CISREG_IADDR0);
222 writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
223 writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
224 writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
225 for ( ; len > 0; len--, buf++)
226 writeb(*buf, sys+CISREG_IDATA0);
227 } else {
228 u_int inc = 1, card_offset, flags;
229
230 flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
231 if (attr & IS_ATTR) {
232 flags |= MAP_ATTRIB;
233 inc++;
234 addr *= 2;
235 }
236
237 card_offset = addr & ~(s->map_size-1);
238 while (len) {
239 sys = set_cis_map(s, card_offset, flags);
240 if (!sys)
241 return; /* FIXME: error */
242
243 end = sys + s->map_size;
244 sys = sys + (addr & (s->map_size-1));
245 for ( ; len > 0; len--, buf++, sys += inc) {
246 if (sys == end)
247 break;
248 writeb(*buf, sys);
249 }
250 card_offset += s->map_size;
251 addr = 0;
252 }
253 }
254}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/*======================================================================
258
259 This is a wrapper around read_cis_mem, with the same interface,
260 but which caches information, for cards whose CIS may not be
261 readable all the time.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263======================================================================*/
264
265static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
Dominik Brodowski53efec92008-07-28 19:44:05 +0200266 size_t len, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Dominik Brodowski57197b92010-01-02 17:27:33 +0100268 struct cis_cache_entry *cis;
269 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Dominik Brodowski57197b92010-01-02 17:27:33 +0100271 if (s->state & SOCKET_CARDBUS)
272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Dominik Brodowski57197b92010-01-02 17:27:33 +0100274 if (s->fake_cis) {
275 if (s->fake_cis_len >= addr+len)
276 memcpy(ptr, s->fake_cis+addr, len);
277 else
278 memset(ptr, 0xff, len);
279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dominik Brodowski57197b92010-01-02 17:27:33 +0100282 list_for_each_entry(cis, &s->cis_cache, node) {
283 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
284 memcpy(ptr, cis->cache, len);
285 return;
286 }
287 }
288
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -0700289 ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (ret == 0) {
292 /* Copy data into the cache */
293 cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL);
294 if (cis) {
295 cis->addr = addr;
296 cis->len = len;
297 cis->attr = attr;
298 memcpy(cis->cache, ptr, len);
299 list_add(&cis->node, &s->cis_cache);
300 }
301 }
302}
303
304static void
305remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
306{
307 struct cis_cache_entry *cis;
308
309 list_for_each_entry(cis, &s->cis_cache, node)
310 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
311 list_del(&cis->node);
312 kfree(cis);
313 break;
314 }
315}
316
Dominik Brodowski904e3772010-01-02 12:28:04 +0100317/**
318 * destroy_cis_cache() - destroy the CIS cache
319 * @s: pcmcia_socket for which CIS cache shall be destroyed
320 *
321 * This destroys the CIS cache but keeps any fake CIS alive.
322 */
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324void destroy_cis_cache(struct pcmcia_socket *s)
325{
326 struct list_head *l, *n;
Dominik Brodowski904e3772010-01-02 12:28:04 +0100327 struct cis_cache_entry *cis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 list_for_each_safe(l, n, &s->cis_cache) {
Dominik Brodowski904e3772010-01-02 12:28:04 +0100330 cis = list_entry(l, struct cis_cache_entry, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 list_del(&cis->node);
332 kfree(cis);
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336/*======================================================================
337
338 This verifies if the CIS of a card matches what is in the CIS
339 cache.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341======================================================================*/
342
343int verify_cis_cache(struct pcmcia_socket *s)
344{
345 struct cis_cache_entry *cis;
346 char *buf;
347
Dominik Brodowski57197b92010-01-02 17:27:33 +0100348 if (s->state & SOCKET_CARDBUS)
349 return -EINVAL;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 buf = kmalloc(256, GFP_KERNEL);
Dominik Brodowskie6895972008-11-02 19:55:45 +0100352 if (buf == NULL) {
Dominik Brodowski11683862008-08-03 10:22:47 +0200353 dev_printk(KERN_WARNING, &s->dev,
354 "no memory for verifying CIS\n");
355 return -ENOMEM;
Dominik Brodowskie6895972008-11-02 19:55:45 +0100356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 list_for_each_entry(cis, &s->cis_cache, node) {
358 int len = cis->len;
359
360 if (len > 256)
361 len = 256;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100362
363 pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (memcmp(buf, cis->cache, len) != 0) {
366 kfree(buf);
367 return -1;
368 }
369 }
370 kfree(buf);
371 return 0;
372}
373
374/*======================================================================
375
376 For really bad cards, we provide a facility for uploading a
377 replacement CIS.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379======================================================================*/
380
Dominik Brodowski53efec92008-07-28 19:44:05 +0200381int pcmcia_replace_cis(struct pcmcia_socket *s,
382 const u8 *data, const size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Dominik Brodowski11683862008-08-03 10:22:47 +0200384 if (len > CISTPL_MAX_CIS_SIZE) {
385 dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n");
386 return -EINVAL;
387 }
388 kfree(s->fake_cis);
389 s->fake_cis = kmalloc(len, GFP_KERNEL);
390 if (s->fake_cis == NULL) {
391 dev_printk(KERN_WARNING, &s->dev, "no memory to replace CIS\n");
392 return -ENOMEM;
393 }
394 s->fake_cis_len = len;
395 memcpy(s->fake_cis, data, len);
396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399/*======================================================================
400
401 The high-level CIS tuple services
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403======================================================================*/
404
405typedef struct tuple_flags {
406 u_int link_space:4;
407 u_int has_link:1;
408 u_int mfc_fn:3;
409 u_int space:4;
410} tuple_flags;
411
412#define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space)
413#define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link)
414#define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn)
415#define SPACE(f) (((tuple_flags *)(&(f)))->space)
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
418{
419 if (!s)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200420 return -EINVAL;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100421
422 if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 tuple->TupleLink = tuple->Flags = 0;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100425
426 /* Assume presence of a LONGLINK_C to address 0 */
427 tuple->CISOffset = tuple->LinkOffset = 0;
428 SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1;
429
430 if ((s->functions > 1) && !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 cisdata_t req = tuple->DesiredTuple;
432 tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200433 if (pccard_get_next_tuple(s, function, tuple) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 tuple->DesiredTuple = CISTPL_LINKTARGET;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200435 if (pccard_get_next_tuple(s, function, tuple) != 0)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200436 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else
438 tuple->CISOffset = tuple->TupleLink = 0;
439 tuple->DesiredTuple = req;
440 }
441 return pccard_get_next_tuple(s, function, tuple);
442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
445{
446 u_char link[5];
447 u_int ofs;
448
449 if (MFC_FN(tuple->Flags)) {
450 /* Get indirect link from the MFC tuple */
451 read_cis_cache(s, LINK_SPACE(tuple->Flags),
452 tuple->LinkOffset, 5, link);
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700453 ofs = get_unaligned_le32(link + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
455 /* Move to the next indirect link */
456 tuple->LinkOffset += 5;
457 MFC_FN(tuple->Flags)--;
458 } else if (HAS_LINK(tuple->Flags)) {
459 ofs = tuple->LinkOffset;
460 SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags);
461 HAS_LINK(tuple->Flags) = 0;
462 } else {
463 return -1;
464 }
Dominik Brodowski57197b92010-01-02 17:27:33 +0100465 if (SPACE(tuple->Flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* This is ugly, but a common CIS error is to code the long
467 link offset incorrectly, so we check the right spot... */
468 read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
469 if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
470 (strncmp(link+2, "CIS", 3) == 0))
471 return ofs;
472 remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
473 /* Then, we try the wrong spot... */
474 ofs = ofs >> 1;
475 }
476 read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
477 if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
478 (strncmp(link+2, "CIS", 3) == 0))
479 return ofs;
480 remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
481 return -1;
482}
483
484int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
485{
486 u_char link[2], tmp;
487 int ofs, i, attr;
488
489 if (!s)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200490 return -EINVAL;
Dominik Brodowski57197b92010-01-02 17:27:33 +0100491 if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200492 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 link[1] = tuple->TupleLink;
495 ofs = tuple->CISOffset + tuple->TupleLink;
496 attr = SPACE(tuple->Flags);
497
498 for (i = 0; i < MAX_TUPLES; i++) {
499 if (link[1] == 0xff) {
500 link[0] = CISTPL_END;
501 } else {
502 read_cis_cache(s, attr, ofs, 2, link);
503 if (link[0] == CISTPL_NULL) {
504 ofs++; continue;
505 }
506 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* End of chain? Follow long link if possible */
509 if (link[0] == CISTPL_END) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100510 ofs = follow_link(s, tuple);
511 if (ofs < 0)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200512 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 attr = SPACE(tuple->Flags);
514 read_cis_cache(s, attr, ofs, 2, link);
515 }
516
517 /* Is this a link tuple? Make a note of it */
518 if ((link[0] == CISTPL_LONGLINK_A) ||
519 (link[0] == CISTPL_LONGLINK_C) ||
520 (link[0] == CISTPL_LONGLINK_MFC) ||
521 (link[0] == CISTPL_LINKTARGET) ||
522 (link[0] == CISTPL_INDIRECT) ||
523 (link[0] == CISTPL_NO_LINK)) {
524 switch (link[0]) {
525 case CISTPL_LONGLINK_A:
526 HAS_LINK(tuple->Flags) = 1;
527 LINK_SPACE(tuple->Flags) = attr | IS_ATTR;
528 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
529 break;
530 case CISTPL_LONGLINK_C:
531 HAS_LINK(tuple->Flags) = 1;
532 LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR;
533 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
534 break;
535 case CISTPL_INDIRECT:
536 HAS_LINK(tuple->Flags) = 1;
537 LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT;
538 tuple->LinkOffset = 0;
539 break;
540 case CISTPL_LONGLINK_MFC:
541 tuple->LinkOffset = ofs + 3;
542 LINK_SPACE(tuple->Flags) = attr;
543 if (function == BIND_FN_ALL) {
544 /* Follow all the MFC links */
545 read_cis_cache(s, attr, ofs+2, 1, &tmp);
546 MFC_FN(tuple->Flags) = tmp;
547 } else {
548 /* Follow exactly one of the links */
549 MFC_FN(tuple->Flags) = 1;
550 tuple->LinkOffset += function * 5;
551 }
552 break;
553 case CISTPL_NO_LINK:
554 HAS_LINK(tuple->Flags) = 0;
555 break;
556 }
557 if ((tuple->Attributes & TUPLE_RETURN_LINK) &&
558 (tuple->DesiredTuple == RETURN_FIRST_TUPLE))
559 break;
560 } else
561 if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
562 break;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (link[0] == tuple->DesiredTuple)
565 break;
566 ofs += link[1] + 2;
567 }
568 if (i == MAX_TUPLES) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200569 dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n");
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200570 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 tuple->TupleCode = link[0];
574 tuple->TupleLink = link[1];
575 tuple->CISOffset = ofs + 2;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/*====================================================================*/
580
581#define _MIN(a, b) (((a) < (b)) ? (a) : (b))
582
583int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple)
584{
585 u_int len;
586
587 if (!s)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200588 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (tuple->TupleLink < tuple->TupleOffset)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200591 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 len = tuple->TupleLink - tuple->TupleOffset;
593 tuple->TupleDataLen = tuple->TupleLink;
594 if (len == 0)
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 read_cis_cache(s, SPACE(tuple->Flags),
597 tuple->CISOffset + tuple->TupleOffset,
598 _MIN(len, tuple->TupleDataMax), tuple->TupleData);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602
603/*======================================================================
604
605 Parsing routines for individual tuples
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607======================================================================*/
608
609static int parse_device(tuple_t *tuple, cistpl_device_t *device)
610{
611 int i;
612 u_char scale;
613 u_char *p, *q;
614
615 p = (u_char *)tuple->TupleData;
616 q = p + tuple->TupleDataLen;
617
618 device->ndev = 0;
619 for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100620
621 if (*p == 0xff)
622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 device->dev[i].type = (*p >> 4);
624 device->dev[i].wp = (*p & 0x08) ? 1 : 0;
625 switch (*p & 0x07) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100626 case 0:
627 device->dev[i].speed = 0;
628 break;
629 case 1:
630 device->dev[i].speed = 250;
631 break;
632 case 2:
633 device->dev[i].speed = 200;
634 break;
635 case 3:
636 device->dev[i].speed = 150;
637 break;
638 case 4:
639 device->dev[i].speed = 100;
640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 case 7:
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200642 if (++p == q)
643 return -EINVAL;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100644 device->dev[i].speed = SPEED_CVT(*p);
645 while (*p & 0x80)
646 if (++p == q)
647 return -EINVAL;
648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 default:
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200653 if (++p == q)
654 return -EINVAL;
655 if (*p == 0xff)
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 scale = *p & 7;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200658 if (scale == 7)
659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
661 device->ndev++;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200662 if (++p == q)
663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100665
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*====================================================================*/
670
671static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
672{
673 u_char *p;
674 if (tuple->TupleDataLen < 5)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200675 return -EINVAL;
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -0700676 p = (u_char *) tuple->TupleData;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700677 csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2;
678 csum->len = get_unaligned_le16(p + 2);
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -0700679 csum->sum = *(p + 4);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*====================================================================*/
684
685static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
686{
687 if (tuple->TupleDataLen < 4)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200688 return -EINVAL;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700689 link->addr = get_unaligned_le32(tuple->TupleData);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693/*====================================================================*/
694
695static int parse_longlink_mfc(tuple_t *tuple,
696 cistpl_longlink_mfc_t *link)
697{
698 u_char *p;
699 int i;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 p = (u_char *)tuple->TupleData;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 link->nfn = *p; p++;
704 if (tuple->TupleDataLen <= link->nfn*5)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200705 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 for (i = 0; i < link->nfn; i++) {
707 link->fn[i].space = *p; p++;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700708 link->fn[i].addr = get_unaligned_le32(p);
Daniel Ritzdc0cf6a2007-10-16 01:23:52 -0700709 p += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*====================================================================*/
715
716static int parse_strings(u_char *p, u_char *q, int max,
717 char *s, u_char *ofs, u_char *found)
718{
719 int i, j, ns;
720
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200721 if (p == q)
722 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ns = 0; j = 0;
724 for (i = 0; i < max; i++) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200725 if (*p == 0xff)
726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ofs[i] = j;
728 ns++;
729 for (;;) {
730 s[j++] = (*p == 0xff) ? '\0' : *p;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100731 if ((*p == '\0') || (*p == 0xff))
732 break;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200733 if (++p == q)
734 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100736 if ((*p == 0xff) || (++p == q))
737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 if (found) {
740 *found = ns;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } else {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200743 return (ns == max) ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745}
746
747/*====================================================================*/
748
749static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
750{
751 u_char *p, *q;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 p = (u_char *)tuple->TupleData;
754 q = p + tuple->TupleDataLen;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 vers_1->major = *p; p++;
757 vers_1->minor = *p; p++;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200758 if (p >= q)
759 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
762 vers_1->str, vers_1->ofs, &vers_1->ns);
763}
764
765/*====================================================================*/
766
767static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
768{
769 u_char *p, *q;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 p = (u_char *)tuple->TupleData;
772 q = p + tuple->TupleDataLen;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
775 altstr->str, altstr->ofs, &altstr->ns);
776}
777
778/*====================================================================*/
779
780static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
781{
782 u_char *p, *q;
783 int nid;
784
785 p = (u_char *)tuple->TupleData;
786 q = p + tuple->TupleDataLen;
787
788 for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100789 if (p > q-2)
790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 jedec->id[nid].mfr = p[0];
792 jedec->id[nid].info = p[1];
793 p += 2;
794 }
795 jedec->nid = nid;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*====================================================================*/
800
801static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (tuple->TupleDataLen < 4)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200804 return -EINVAL;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -0700805 m->manf = get_unaligned_le16(tuple->TupleData);
806 m->card = get_unaligned_le16(tuple->TupleData + 2);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*====================================================================*/
811
812static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
813{
814 u_char *p;
815 if (tuple->TupleDataLen < 2)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 p = (u_char *)tuple->TupleData;
818 f->func = p[0];
819 f->sysinit = p[1];
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823/*====================================================================*/
824
825static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
826{
827 u_char *p;
828 int i;
829 if (tuple->TupleDataLen < 1)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 p = (u_char *)tuple->TupleData;
832 f->type = p[0];
833 for (i = 1; i < tuple->TupleDataLen; i++)
834 f->data[i-1] = p[i];
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838/*====================================================================*/
839
840static int parse_config(tuple_t *tuple, cistpl_config_t *config)
841{
842 int rasz, rmsz, i;
843 u_char *p;
844
845 p = (u_char *)tuple->TupleData;
846 rasz = *p & 0x03;
847 rmsz = (*p & 0x3c) >> 2;
848 if (tuple->TupleDataLen < rasz+rmsz+4)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +0200849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 config->last_idx = *(++p);
851 p++;
852 config->base = 0;
853 for (i = 0; i <= rasz; i++)
854 config->base += p[i] << (8*i);
855 p += rasz+1;
856 for (i = 0; i < 4; i++)
857 config->rmask[i] = 0;
858 for (i = 0; i <= rmsz; i++)
859 config->rmask[i>>2] += p[i] << (8*(i%4));
860 config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864/*======================================================================
865
866 The following routines are all used to parse the nightmarish
867 config table entries.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869======================================================================*/
870
871static u_char *parse_power(u_char *p, u_char *q,
872 cistpl_power_t *pwr)
873{
874 int i;
875 u_int scale;
876
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100877 if (p == q)
878 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 pwr->present = *p;
880 pwr->flags = 0;
881 p++;
882 for (i = 0; i < 7; i++)
883 if (pwr->present & (1<<i)) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100884 if (p == q)
885 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 pwr->param[i] = POWER_CVT(*p);
887 scale = POWER_SCALE(*p);
888 while (*p & 0x80) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100889 if (++p == q)
890 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if ((*p & 0x7f) < 100)
892 pwr->param[i] += (*p & 0x7f) * scale / 100;
893 else if (*p == 0x7d)
894 pwr->flags |= CISTPL_POWER_HIGHZ_OK;
895 else if (*p == 0x7e)
896 pwr->param[i] = 0;
897 else if (*p == 0x7f)
898 pwr->flags |= CISTPL_POWER_HIGHZ_REQ;
899 else
900 return NULL;
901 }
902 p++;
903 }
904 return p;
905}
906
907/*====================================================================*/
908
909static u_char *parse_timing(u_char *p, u_char *q,
910 cistpl_timing_t *timing)
911{
912 u_char scale;
913
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100914 if (p == q)
915 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 scale = *p;
917 if ((scale & 3) != 3) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100918 if (++p == q)
919 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 timing->wait = SPEED_CVT(*p);
921 timing->waitscale = exponent[scale & 3];
922 } else
923 timing->wait = 0;
924 scale >>= 2;
925 if ((scale & 7) != 7) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100926 if (++p == q)
927 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 timing->ready = SPEED_CVT(*p);
929 timing->rdyscale = exponent[scale & 7];
930 } else
931 timing->ready = 0;
932 scale >>= 3;
933 if (scale != 7) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100934 if (++p == q)
935 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 timing->reserved = SPEED_CVT(*p);
937 timing->rsvscale = exponent[scale];
938 } else
939 timing->reserved = 0;
940 p++;
941 return p;
942}
943
944/*====================================================================*/
945
946static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
947{
948 int i, j, bsz, lsz;
949
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100950 if (p == q)
951 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 io->flags = *p;
953
954 if (!(*p & 0x80)) {
955 io->nwin = 1;
956 io->win[0].base = 0;
957 io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
958 return p+1;
959 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100960
961 if (++p == q)
962 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 io->nwin = (*p & 0x0f) + 1;
964 bsz = (*p & 0x30) >> 4;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100965 if (bsz == 3)
966 bsz++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 lsz = (*p & 0xc0) >> 6;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100968 if (lsz == 3)
969 lsz++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 p++;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 for (i = 0; i < io->nwin; i++) {
973 io->win[i].base = 0;
974 io->win[i].len = 1;
975 for (j = 0; j < bsz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100976 if (p == q)
977 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 io->win[i].base += *p << (j*8);
979 }
980 for (j = 0; j < lsz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100981 if (p == q)
982 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 io->win[i].len += *p << (j*8);
984 }
985 }
986 return p;
987}
988
989/*====================================================================*/
990
991static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
992{
993 int i, j, asz, lsz, has_ha;
994 u_int len, ca, ha;
995
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100996 if (p == q)
997 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 mem->nwin = (*p & 0x07) + 1;
1000 lsz = (*p & 0x18) >> 3;
1001 asz = (*p & 0x60) >> 5;
1002 has_ha = (*p & 0x80);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001003 if (++p == q)
1004 return NULL;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 for (i = 0; i < mem->nwin; i++) {
1007 len = ca = ha = 0;
1008 for (j = 0; j < lsz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001009 if (p == q)
1010 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 len += *p << (j*8);
1012 }
1013 for (j = 0; j < asz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001014 if (p == q)
1015 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 ca += *p << (j*8);
1017 }
1018 if (has_ha)
1019 for (j = 0; j < asz; j++, p++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001020 if (p == q)
1021 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ha += *p << (j*8);
1023 }
1024 mem->win[i].len = len << 8;
1025 mem->win[i].card_addr = ca << 8;
1026 mem->win[i].host_addr = ha << 8;
1027 }
1028 return p;
1029}
1030
1031/*====================================================================*/
1032
1033static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
1034{
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001035 if (p == q)
1036 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 irq->IRQInfo1 = *p; p++;
1038 if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001039 if (p+2 > q)
1040 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 irq->IRQInfo2 = (p[1]<<8) + p[0];
1042 p += 2;
1043 }
1044 return p;
1045}
1046
1047/*====================================================================*/
1048
1049static int parse_cftable_entry(tuple_t *tuple,
1050 cistpl_cftable_entry_t *entry)
1051{
1052 u_char *p, *q, features;
1053
1054 p = tuple->TupleData;
1055 q = p + tuple->TupleDataLen;
1056 entry->index = *p & 0x3f;
1057 entry->flags = 0;
1058 if (*p & 0x40)
1059 entry->flags |= CISTPL_CFTABLE_DEFAULT;
1060 if (*p & 0x80) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001061 if (++p == q)
1062 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (*p & 0x10)
1064 entry->flags |= CISTPL_CFTABLE_BVDS;
1065 if (*p & 0x20)
1066 entry->flags |= CISTPL_CFTABLE_WP;
1067 if (*p & 0x40)
1068 entry->flags |= CISTPL_CFTABLE_RDYBSY;
1069 if (*p & 0x80)
1070 entry->flags |= CISTPL_CFTABLE_MWAIT;
1071 entry->interface = *p & 0x0f;
1072 } else
1073 entry->interface = 0;
1074
1075 /* Process optional features */
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001076 if (++p == q)
1077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 features = *p; p++;
1079
1080 /* Power options */
1081 if ((features & 3) > 0) {
1082 p = parse_power(p, q, &entry->vcc);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001083 if (p == NULL)
1084 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 } else
1086 entry->vcc.present = 0;
1087 if ((features & 3) > 1) {
1088 p = parse_power(p, q, &entry->vpp1);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001089 if (p == NULL)
1090 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else
1092 entry->vpp1.present = 0;
1093 if ((features & 3) > 2) {
1094 p = parse_power(p, q, &entry->vpp2);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001095 if (p == NULL)
1096 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 } else
1098 entry->vpp2.present = 0;
1099
1100 /* Timing options */
1101 if (features & 0x04) {
1102 p = parse_timing(p, q, &entry->timing);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001103 if (p == NULL)
1104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106 entry->timing.wait = 0;
1107 entry->timing.ready = 0;
1108 entry->timing.reserved = 0;
1109 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* I/O window options */
1112 if (features & 0x08) {
1113 p = parse_io(p, q, &entry->io);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001114 if (p == NULL)
1115 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 } else
1117 entry->io.nwin = 0;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* Interrupt options */
1120 if (features & 0x10) {
1121 p = parse_irq(p, q, &entry->irq);
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->irq.IRQInfo1 = 0;
1126
1127 switch (features & 0x60) {
1128 case 0x00:
1129 entry->mem.nwin = 0;
1130 break;
1131 case 0x20:
1132 entry->mem.nwin = 1;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -07001133 entry->mem.win[0].len = get_unaligned_le16(p) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 entry->mem.win[0].card_addr = 0;
1135 entry->mem.win[0].host_addr = 0;
1136 p += 2;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001137 if (p > q)
1138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 case 0x40:
1141 entry->mem.nwin = 1;
Harvey Harrison6b1e6f62008-04-29 01:03:39 -07001142 entry->mem.win[0].len = get_unaligned_le16(p) << 8;
1143 entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 entry->mem.win[0].host_addr = 0;
1145 p += 4;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001146 if (p > q)
1147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case 0x60:
1150 p = parse_mem(p, q, &entry->mem);
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001151 if (p == NULL)
1152 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 }
1155
1156 /* Misc features */
1157 if (features & 0x80) {
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001158 if (p == q)
1159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 entry->flags |= (*p << 8);
1161 while (*p & 0x80)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001162 if (++p == q)
1163 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 p++;
1165 }
1166
1167 entry->subtuples = q-p;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001168
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172/*====================================================================*/
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
1175{
1176 u_char *p, *q;
1177 int n;
1178
1179 p = (u_char *)tuple->TupleData;
1180 q = p + tuple->TupleDataLen;
1181
1182 for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001183 if (p > q-6)
1184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 geo->geo[n].buswidth = p[0];
1186 geo->geo[n].erase_block = 1 << (p[1]-1);
1187 geo->geo[n].read_block = 1 << (p[2]-1);
1188 geo->geo[n].write_block = 1 << (p[3]-1);
1189 geo->geo[n].partition = 1 << (p[4]-1);
1190 geo->geo[n].interleave = 1 << (p[5]-1);
1191 p += 6;
1192 }
1193 geo->ngeo = n;
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/*====================================================================*/
1198
1199static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
1200{
1201 u_char *p, *q;
1202
1203 if (tuple->TupleDataLen < 10)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001204 return -EINVAL;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 p = tuple->TupleData;
1207 q = p + tuple->TupleDataLen;
1208
1209 v2->vers = p[0];
1210 v2->comply = p[1];
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001211 v2->dindex = get_unaligned_le16(p + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 v2->vspec8 = p[6];
1213 v2->vspec9 = p[7];
1214 v2->nhdr = p[8];
1215 p += 9;
1216 return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL);
1217}
1218
1219/*====================================================================*/
1220
1221static int parse_org(tuple_t *tuple, cistpl_org_t *org)
1222{
1223 u_char *p, *q;
1224 int i;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 p = tuple->TupleData;
1227 q = p + tuple->TupleDataLen;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001228 if (p == q)
1229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 org->data_org = *p;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001231 if (++p == q)
1232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 for (i = 0; i < 30; i++) {
1234 org->desc[i] = *p;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001235 if (*p == '\0')
1236 break;
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001237 if (++p == q)
1238 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/*====================================================================*/
1244
1245static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
1246{
1247 u_char *p;
1248
1249 if (tuple->TupleDataLen < 10)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001250 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 p = tuple->TupleData;
1253
1254 fmt->type = p[0];
1255 fmt->edc = p[1];
Harvey Harrison6b1e6f62008-04-29 01:03:39 -07001256 fmt->offset = get_unaligned_le32(p + 2);
1257 fmt->length = get_unaligned_le32(p + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262/*====================================================================*/
1263
Dominik Brodowski2f3061e2008-08-31 15:50:33 +02001264int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001266 int ret = 0;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (tuple->TupleDataLen > tuple->TupleDataMax)
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 switch (tuple->TupleCode) {
1271 case CISTPL_DEVICE:
1272 case CISTPL_DEVICE_A:
1273 ret = parse_device(tuple, &parse->device);
1274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 case CISTPL_CHECKSUM:
1276 ret = parse_checksum(tuple, &parse->checksum);
1277 break;
1278 case CISTPL_LONGLINK_A:
1279 case CISTPL_LONGLINK_C:
1280 ret = parse_longlink(tuple, &parse->longlink);
1281 break;
1282 case CISTPL_LONGLINK_MFC:
1283 ret = parse_longlink_mfc(tuple, &parse->longlink_mfc);
1284 break;
1285 case CISTPL_VERS_1:
1286 ret = parse_vers_1(tuple, &parse->version_1);
1287 break;
1288 case CISTPL_ALTSTR:
1289 ret = parse_altstr(tuple, &parse->altstr);
1290 break;
1291 case CISTPL_JEDEC_A:
1292 case CISTPL_JEDEC_C:
1293 ret = parse_jedec(tuple, &parse->jedec);
1294 break;
1295 case CISTPL_MANFID:
1296 ret = parse_manfid(tuple, &parse->manfid);
1297 break;
1298 case CISTPL_FUNCID:
1299 ret = parse_funcid(tuple, &parse->funcid);
1300 break;
1301 case CISTPL_FUNCE:
1302 ret = parse_funce(tuple, &parse->funce);
1303 break;
1304 case CISTPL_CONFIG:
1305 ret = parse_config(tuple, &parse->config);
1306 break;
1307 case CISTPL_CFTABLE_ENTRY:
1308 ret = parse_cftable_entry(tuple, &parse->cftable_entry);
1309 break;
1310 case CISTPL_DEVICE_GEO:
1311 case CISTPL_DEVICE_GEO_A:
1312 ret = parse_device_geo(tuple, &parse->device_geo);
1313 break;
1314 case CISTPL_VERS_2:
1315 ret = parse_vers_2(tuple, &parse->vers_2);
1316 break;
1317 case CISTPL_ORG:
1318 ret = parse_org(tuple, &parse->org);
1319 break;
1320 case CISTPL_FORMAT:
1321 case CISTPL_FORMAT_A:
1322 ret = parse_format(tuple, &parse->format);
1323 break;
1324 case CISTPL_NO_LINK:
1325 case CISTPL_LINKTARGET:
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001326 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
1328 default:
Dominik Brodowskide6405e2008-08-03 10:47:59 +02001329 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
1331 }
Dominik Brodowski3f9c5f42008-08-03 12:22:40 +02001332 if (ret)
Dominik Brodowskid50dbec2009-10-23 12:51:28 +02001333 pr_debug("parse_tuple failed %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return ret;
1335}
Dominik Brodowski2f3061e2008-08-31 15:50:33 +02001336EXPORT_SYMBOL(pcmcia_parse_tuple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338/*======================================================================
1339
1340 This is used internally by Card Services to look up CIS stuff.
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342======================================================================*/
1343
1344int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse)
1345{
1346 tuple_t tuple;
1347 cisdata_t *buf;
1348 int ret;
1349
1350 buf = kmalloc(256, GFP_KERNEL);
Dominik Brodowski11683862008-08-03 10:22:47 +02001351 if (buf == NULL) {
1352 dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
1353 return -ENOMEM;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 tuple.DesiredTuple = code;
Dominik Brodowski84897fc2009-10-18 23:51:09 +02001356 tuple.Attributes = 0;
1357 if (function == BIND_FN_ALL)
1358 tuple.Attributes = TUPLE_RETURN_COMMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 ret = pccard_get_first_tuple(s, function, &tuple);
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001360 if (ret != 0)
1361 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 tuple.TupleData = buf;
1363 tuple.TupleOffset = 0;
1364 tuple.TupleDataMax = 255;
1365 ret = pccard_get_tuple_data(s, &tuple);
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001366 if (ret != 0)
1367 goto done;
Dominik Brodowski2f3061e2008-08-31 15:50:33 +02001368 ret = pcmcia_parse_tuple(&tuple, parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369done:
1370 kfree(buf);
1371 return ret;
1372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dominik Brodowski91284222009-10-18 23:32:33 +02001374
1375/**
1376 * pccard_loop_tuple() - loop over tuples in the CIS
1377 * @s: the struct pcmcia_socket where the card is inserted
1378 * @function: the device function we loop for
1379 * @code: which CIS code shall we look for?
1380 * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
1381 * @priv_data: private data to be passed to the loop_tuple function.
1382 * @loop_tuple: function to call for each CIS entry of type @function. IT
1383 * gets passed the raw tuple, the paresed tuple (if @parse is
1384 * set) and @priv_data.
1385 *
1386 * pccard_loop_tuple() loops over all CIS entries of type @function, and
1387 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
1388 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
1389 */
1390int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
1391 cisdata_t code, cisparse_t *parse, void *priv_data,
1392 int (*loop_tuple) (tuple_t *tuple,
1393 cisparse_t *parse,
1394 void *priv_data))
1395{
1396 tuple_t tuple;
1397 cisdata_t *buf;
1398 int ret;
1399
1400 buf = kzalloc(256, GFP_KERNEL);
1401 if (buf == NULL) {
1402 dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
1403 return -ENOMEM;
1404 }
1405
1406 tuple.TupleData = buf;
1407 tuple.TupleDataMax = 255;
1408 tuple.TupleOffset = 0;
1409 tuple.DesiredTuple = code;
1410 tuple.Attributes = 0;
1411
1412 ret = pccard_get_first_tuple(s, function, &tuple);
1413 while (!ret) {
1414 if (pccard_get_tuple_data(s, &tuple))
1415 goto next_entry;
1416
1417 if (parse)
1418 if (pcmcia_parse_tuple(&tuple, parse))
1419 goto next_entry;
1420
1421 ret = loop_tuple(&tuple, parse, priv_data);
1422 if (!ret)
1423 break;
1424
1425next_entry:
1426 ret = pccard_get_next_tuple(s, function, &tuple);
1427 }
1428
1429 kfree(buf);
1430 return ret;
1431}
Dominik Brodowski91284222009-10-18 23:32:33 +02001432
1433
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001434/**
1435 * pccard_validate_cis() - check whether card has a sensible CIS
1436 * @s: the struct pcmcia_socket we are to check
1437 * @info: returns the number of tuples in the (valid) CIS, or 0
1438 *
1439 * This tries to determine if a card has a sensible CIS. In @info, it
1440 * returns the number of tuples in the CIS, or 0 if the CIS looks bad. The
1441 * checks include making sure several critical tuples are present and
1442 * valid; seeing if the total number of tuples is reasonable; and
1443 * looking for tuples that use reserved codes.
1444 *
1445 * The function returns 0 on success.
1446 */
Dominik Brodowski84897fc2009-10-18 23:51:09 +02001447int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001449 tuple_t *tuple;
1450 cisparse_t *p;
1451 unsigned int count = 0;
1452 int ret, reserved, dev_ok = 0, ident_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001454 if (!s)
1455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001457 /* We do not want to validate the CIS cache... */
1458 destroy_cis_cache(s);
Dominik Brodowski904e3772010-01-02 12:28:04 +01001459
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001460 tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
1461 if (tuple == NULL) {
1462 dev_warn(&s->dev, "no memory to validate CIS\n");
1463 return -ENOMEM;
1464 }
1465 p = kmalloc(sizeof(*p), GFP_KERNEL);
1466 if (p == NULL) {
1467 kfree(tuple);
1468 dev_warn(&s->dev, "no memory to validate CIS\n");
1469 return -ENOMEM;
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001472 count = reserved = 0;
1473 tuple->DesiredTuple = RETURN_FIRST_TUPLE;
1474 tuple->Attributes = TUPLE_RETURN_COMMON;
1475 ret = pccard_get_first_tuple(s, BIND_FN_ALL, tuple);
Dominik Brodowski4c89e882008-08-03 10:07:45 +02001476 if (ret != 0)
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001477 goto done;
1478
1479 /* First tuple should be DEVICE; we should really have either that
1480 or a CFTABLE_ENTRY of some sort */
1481 if ((tuple->TupleCode == CISTPL_DEVICE) ||
1482 (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY, p)) ||
1483 (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY_CB, p)))
1484 dev_ok++;
1485
1486 /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
1487 tuple, for card identification. Certain old D-Link and Linksys
1488 cards have only a broken VERS_2 tuple; hence the bogus test. */
1489 if ((pccard_read_tuple(s, BIND_FN_ALL, CISTPL_MANFID, p) == 0) ||
1490 (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_1, p) == 0) ||
1491 (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_2, p) != -ENOSPC))
1492 ident_ok++;
1493
1494 if (!dev_ok && !ident_ok)
1495 goto done;
1496
1497 for (count = 1; count < MAX_TUPLES; count++) {
1498 ret = pccard_get_next_tuple(s, BIND_FN_ALL, tuple);
1499 if (ret != 0)
1500 break;
1501 if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
1502 ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
1503 ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
1504 reserved++;
1505 }
1506 if ((count == MAX_TUPLES) || (reserved > 5) ||
1507 ((!dev_ok || !ident_ok) && (count > 10)))
1508 count = 0;
1509
1510 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512done:
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001513 /* invalidate CIS cache on failure */
1514 if (!dev_ok || !ident_ok || !count) {
1515 destroy_cis_cache(s);
1516 ret = -EIO;
1517 }
Dominik Brodowski904e3772010-01-02 12:28:04 +01001518
Dominik Brodowskif131ddc2010-01-02 12:58:10 +01001519 if (info)
1520 *info = count;
1521 kfree(tuple);
1522 kfree(p);
1523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +01001525
1526
1527#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
1528
1529static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf,
1530 loff_t off, size_t count)
1531{
1532 tuple_t tuple;
1533 int status, i;
1534 loff_t pointer = 0;
1535 ssize_t ret = 0;
1536 u_char *tuplebuffer;
1537 u_char *tempbuffer;
1538
1539 tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
1540 if (!tuplebuffer)
1541 return -ENOMEM;
1542
1543 tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
1544 if (!tempbuffer) {
1545 ret = -ENOMEM;
1546 goto free_tuple;
1547 }
1548
1549 memset(&tuple, 0, sizeof(tuple_t));
1550
1551 tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
1552 tuple.DesiredTuple = RETURN_FIRST_TUPLE;
1553 tuple.TupleOffset = 0;
1554
1555 status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
1556 while (!status) {
1557 tuple.TupleData = tuplebuffer;
1558 tuple.TupleDataMax = 255;
1559 memset(tuplebuffer, 0, sizeof(u_char) * 255);
1560
1561 status = pccard_get_tuple_data(s, &tuple);
1562 if (status)
1563 break;
1564
1565 if (off < (pointer + 2 + tuple.TupleDataLen)) {
1566 tempbuffer[0] = tuple.TupleCode & 0xff;
1567 tempbuffer[1] = tuple.TupleLink & 0xff;
1568 for (i = 0; i < tuple.TupleDataLen; i++)
1569 tempbuffer[i + 2] = tuplebuffer[i] & 0xff;
1570
1571 for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
1572 if (((i + pointer) >= off) &&
1573 (i + pointer) < (off + count)) {
1574 buf[ret] = tempbuffer[i];
1575 ret++;
1576 }
1577 }
1578 }
1579
1580 pointer += 2 + tuple.TupleDataLen;
1581
1582 if (pointer >= (off + count))
1583 break;
1584
1585 if (tuple.TupleCode == CISTPL_END)
1586 break;
1587 status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
1588 }
1589
1590 kfree(tempbuffer);
1591 free_tuple:
1592 kfree(tuplebuffer);
1593
1594 return ret;
1595}
1596
1597
1598static ssize_t pccard_show_cis(struct kobject *kobj,
1599 struct bin_attribute *bin_attr,
1600 char *buf, loff_t off, size_t count)
1601{
1602 unsigned int size = 0x200;
1603
1604 if (off >= size)
1605 count = 0;
1606 else {
1607 struct pcmcia_socket *s;
1608 unsigned int chains;
1609
1610 if (off + count > size)
1611 count = size - off;
1612
1613 s = to_socket(container_of(kobj, struct device, kobj));
1614
1615 if (!(s->state & SOCKET_PRESENT))
1616 return -ENODEV;
1617 if (pccard_validate_cis(s, &chains))
1618 return -EIO;
1619 if (!chains)
1620 return -ENODATA;
1621
1622 count = pccard_extract_cis(s, buf, off, count);
1623 }
1624
1625 return count;
1626}
1627
1628
1629static ssize_t pccard_store_cis(struct kobject *kobj,
1630 struct bin_attribute *bin_attr,
1631 char *buf, loff_t off, size_t count)
1632{
1633 struct pcmcia_socket *s;
1634 int error;
1635
1636 s = to_socket(container_of(kobj, struct device, kobj));
1637
1638 if (off)
1639 return -EINVAL;
1640
1641 if (count >= CISTPL_MAX_CIS_SIZE)
1642 return -EINVAL;
1643
1644 if (!(s->state & SOCKET_PRESENT))
1645 return -ENODEV;
1646
1647 error = pcmcia_replace_cis(s, buf, count);
1648 if (error)
1649 return -EIO;
1650
1651 mutex_lock(&s->skt_mutex);
1652 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
1653 !(s->state & SOCKET_CARDBUS)) {
1654 if (try_module_get(s->callback->owner)) {
1655 s->callback->requery(s, 1);
1656 module_put(s->callback->owner);
1657 }
1658 }
1659 mutex_unlock(&s->skt_mutex);
1660
1661 return count;
1662}
1663
1664
1665struct bin_attribute pccard_cis_attr = {
1666 .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR },
1667 .size = 0x200,
1668 .read = pccard_show_cis,
1669 .write = pccard_store_cis,
1670};