cistpl.c 40.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * cistpl.c -- 16-bit PCMCIA Card Information Structure parser
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * The initial developer of the original code is David A. Hinds
 * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
 * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
 *
 * (C) 1999		David A. Hinds
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/pci.h>
#include <linux/ioport.h>
D
Dominik Brodowski 已提交
26
#include <linux/io.h>
L
Linus Torvalds 已提交
27
#include <asm/byteorder.h>
28
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#include <pcmcia/cs_types.h>
#include <pcmcia/ss.h>
#include <pcmcia/cs.h>
#include <pcmcia/cisreg.h>
#include <pcmcia/cistpl.h>
#include "cs_internal.h"

static const u_char mantissa[] = {
    10, 12, 13, 15, 20, 25, 30, 35,
    40, 45, 50, 55, 60, 70, 80, 90
};

static const u_int exponent[] = {
    1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
};

/* Convert an extended speed byte to a time in nanoseconds */
#define SPEED_CVT(v) \
    (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10)
/* Convert a power byte to a current in 0.1 microamps */
#define POWER_CVT(v) \
    (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10)
#define POWER_SCALE(v)		(exponent[(v)&7])

/* Upper limit on reasonable # of tuples */
#define MAX_TUPLES		200

/*====================================================================*/

/* Parameters that can be set with 'insmod' */

61 62 63
/* 16-bit CIS? */
static int cis_width;
module_param(cis_width, int, 0444);
L
Linus Torvalds 已提交
64 65 66

void release_cis_mem(struct pcmcia_socket *s)
{
67
    mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78
    if (s->cis_mem.flags & MAP_ACTIVE) {
	s->cis_mem.flags &= ~MAP_ACTIVE;
	s->ops->set_mem_map(s, &s->cis_mem);
	if (s->cis_mem.res) {
	    release_resource(s->cis_mem.res);
	    kfree(s->cis_mem.res);
	    s->cis_mem.res = NULL;
	}
	iounmap(s->cis_virt);
	s->cis_virt = NULL;
    }
79
    mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
80 81 82 83 84 85
}

/*
 * Map the card memory at "card_offset" into virtual space.
 * If flags & MAP_ATTRIB, map the attribute space, otherwise
 * map the memory space.
86 87
 *
 * Must be called with ops_mutex held.
L
Linus Torvalds 已提交
88 89 90 91
 */
static void __iomem *
set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
{
92 93 94 95 96 97
	pccard_mem_map *mem = &s->cis_mem;
	int ret;

	if (!(s->features & SS_CAP_STATIC_MAP) && (mem->res == NULL)) {
		mem->res = pcmcia_find_mem_region(0, s->map_size, s->map_size, 0, s);
		if (mem->res == NULL) {
98 99
			dev_printk(KERN_NOTICE, &s->dev,
				   "cs: unable to map card memory!\n");
100 101 102
			return NULL;
		}
		s->cis_virt = NULL;
L
Linus Torvalds 已提交
103
	}
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	if (!(s->features & SS_CAP_STATIC_MAP) && (!s->cis_virt))
		s->cis_virt = ioremap(mem->res->start, s->map_size);

	mem->card_start = card_offset;
	mem->flags = flags;

	ret = s->ops->set_mem_map(s, mem);
	if (ret) {
		iounmap(s->cis_virt);
		s->cis_virt = NULL;
		return NULL;
	}

	if (s->features & SS_CAP_STATIC_MAP) {
		if (s->cis_virt)
			iounmap(s->cis_virt);
		s->cis_virt = ioremap(mem->static_start, s->map_size);
	}

	return s->cis_virt;
L
Linus Torvalds 已提交
125 126 127 128 129 130
}

/*======================================================================

    Low-level functions to read and write CIS memory.  I think the
    write routine is only useful for writing one-byte registers.
D
Dominik Brodowski 已提交
131

L
Linus Torvalds 已提交
132 133 134 135 136 137
======================================================================*/

/* Bits in attr field */
#define IS_ATTR		1
#define IS_INDIRECT	8

138
int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
L
Linus Torvalds 已提交
139 140 141 142
		 u_int len, void *ptr)
{
    void __iomem *sys, *end;
    unsigned char *buf = ptr;
D
Dominik Brodowski 已提交
143

144
    dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
L
Linus Torvalds 已提交
145

146
    mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157
    if (attr & IS_INDIRECT) {
	/* Indirect accesses use a bunch of special registers at fixed
	   locations in common memory */
	u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
	if (attr & IS_ATTR) {
	    addr *= 2;
	    flags = ICTRL0_AUTOINC;
	}

	sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
	if (!sys) {
158
	    dev_dbg(&s->dev, "could not map memory\n");
L
Linus Torvalds 已提交
159
	    memset(ptr, 0xff, len);
160
	    mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173
	    return -1;
	}

	writeb(flags, sys+CISREG_ICTRL0);
	writeb(addr & 0xff, sys+CISREG_IADDR0);
	writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
	writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
	writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
	for ( ; len > 0; len--, buf++)
	    *buf = readb(sys+CISREG_IDATA0);
    } else {
	u_int inc = 1, card_offset, flags;

174 175 176
	if (addr > CISTPL_MAX_CIS_SIZE)
		dev_dbg(&s->dev, "attempt to read CIS mem at addr %#x", addr);

L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187
	flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
	if (attr) {
	    flags |= MAP_ATTRIB;
	    inc++;
	    addr *= 2;
	}

	card_offset = addr & ~(s->map_size-1);
	while (len) {
	    sys = set_cis_map(s, card_offset, flags);
	    if (!sys) {
188
		dev_dbg(&s->dev, "could not map memory\n");
L
Linus Torvalds 已提交
189
		memset(ptr, 0xff, len);
190
		mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203
		return -1;
	    }
	    end = sys + s->map_size;
	    sys = sys + (addr & (s->map_size-1));
	    for ( ; len > 0; len--, buf++, sys += inc) {
		if (sys == end)
		    break;
		*buf = readb(sys);
	    }
	    card_offset += s->map_size;
	    addr = 0;
	}
    }
204
    mutex_unlock(&s->ops_mutex);
205
    dev_dbg(&s->dev, "  %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
L
Linus Torvalds 已提交
206 207 208 209
	  *(u_char *)(ptr+0), *(u_char *)(ptr+1),
	  *(u_char *)(ptr+2), *(u_char *)(ptr+3));
    return 0;
}
210

L
Linus Torvalds 已提交
211

212
void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
L
Linus Torvalds 已提交
213 214 215 216
		   u_int len, void *ptr)
{
    void __iomem *sys, *end;
    unsigned char *buf = ptr;
D
Dominik Brodowski 已提交
217

218
    dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
L
Linus Torvalds 已提交
219

220
    mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230
    if (attr & IS_INDIRECT) {
	/* Indirect accesses use a bunch of special registers at fixed
	   locations in common memory */
	u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
	if (attr & IS_ATTR) {
	    addr *= 2;
	    flags = ICTRL0_AUTOINC;
	}

	sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
231 232 233
	if (!sys) {
		dev_dbg(&s->dev, "could not map memory\n");
		mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
234
		return; /* FIXME: Error */
235
	}
L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

	writeb(flags, sys+CISREG_ICTRL0);
	writeb(addr & 0xff, sys+CISREG_IADDR0);
	writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
	writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
	writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
	for ( ; len > 0; len--, buf++)
	    writeb(*buf, sys+CISREG_IDATA0);
    } else {
	u_int inc = 1, card_offset, flags;

	flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
	if (attr & IS_ATTR) {
	    flags |= MAP_ATTRIB;
	    inc++;
	    addr *= 2;
	}

	card_offset = addr & ~(s->map_size-1);
	while (len) {
	    sys = set_cis_map(s, card_offset, flags);
257 258 259
	    if (!sys) {
		dev_dbg(&s->dev, "could not map memory\n");
		mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
260
		return; /* FIXME: error */
261
	    }
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273

	    end = sys + s->map_size;
	    sys = sys + (addr & (s->map_size-1));
	    for ( ; len > 0; len--, buf++, sys += inc) {
		if (sys == end)
		    break;
		writeb(*buf, sys);
	    }
	    card_offset += s->map_size;
	    addr = 0;
	}
    }
274
    mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
275
}
276

L
Linus Torvalds 已提交
277 278 279 280 281 282

/*======================================================================

    This is a wrapper around read_cis_mem, with the same interface,
    but which caches information, for cards whose CIS may not be
    readable all the time.
D
Dominik Brodowski 已提交
283

L
Linus Torvalds 已提交
284 285 286
======================================================================*/

static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
287
			   size_t len, void *ptr)
L
Linus Torvalds 已提交
288
{
289 290
	struct cis_cache_entry *cis;
	int ret;
L
Linus Torvalds 已提交
291

292 293
	if (s->state & SOCKET_CARDBUS)
		return;
L
Linus Torvalds 已提交
294

295
	mutex_lock(&s->ops_mutex);
296 297 298 299 300
	if (s->fake_cis) {
		if (s->fake_cis_len >= addr+len)
			memcpy(ptr, s->fake_cis+addr, len);
		else
			memset(ptr, 0xff, len);
301
		mutex_unlock(&s->ops_mutex);
302 303 304 305 306 307
		return;
	}

	list_for_each_entry(cis, &s->cis_cache, node) {
		if (cis->addr == addr && cis->len == len && cis->attr == attr) {
			memcpy(ptr, cis->cache, len);
308
			mutex_unlock(&s->ops_mutex);
309 310
			return;
		}
L
Linus Torvalds 已提交
311
	}
312
	mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
313

314
	ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr);
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323

	if (ret == 0) {
		/* Copy data into the cache */
		cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL);
		if (cis) {
			cis->addr = addr;
			cis->len = len;
			cis->attr = attr;
			memcpy(cis->cache, ptr, len);
324
			mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
325
			list_add(&cis->node, &s->cis_cache);
326
			mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335
		}
	}
}

static void
remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
{
	struct cis_cache_entry *cis;

336
	mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
337 338 339 340 341 342
	list_for_each_entry(cis, &s->cis_cache, node)
		if (cis->addr == addr && cis->len == len && cis->attr == attr) {
			list_del(&cis->node);
			kfree(cis);
			break;
		}
343
	mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
344 345
}

346 347 348 349
/**
 * destroy_cis_cache() - destroy the CIS cache
 * @s:		pcmcia_socket for which CIS cache shall be destroyed
 *
350 351
 * This destroys the CIS cache but keeps any fake CIS alive. Must be
 * called with ops_mutex held.
352 353
 */

L
Linus Torvalds 已提交
354 355 356
void destroy_cis_cache(struct pcmcia_socket *s)
{
	struct list_head *l, *n;
357
	struct cis_cache_entry *cis;
L
Linus Torvalds 已提交
358 359

	list_for_each_safe(l, n, &s->cis_cache) {
360
		cis = list_entry(l, struct cis_cache_entry, node);
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369
		list_del(&cis->node);
		kfree(cis);
	}
}

/*======================================================================

    This verifies if the CIS of a card matches what is in the CIS
    cache.
D
Dominik Brodowski 已提交
370

L
Linus Torvalds 已提交
371 372 373 374 375 376 377
======================================================================*/

int verify_cis_cache(struct pcmcia_socket *s)
{
	struct cis_cache_entry *cis;
	char *buf;

378 379 380
	if (s->state & SOCKET_CARDBUS)
		return -EINVAL;

L
Linus Torvalds 已提交
381
	buf = kmalloc(256, GFP_KERNEL);
382
	if (buf == NULL) {
383 384 385
		dev_printk(KERN_WARNING, &s->dev,
			   "no memory for verifying CIS\n");
		return -ENOMEM;
386
	}
L
Linus Torvalds 已提交
387 388 389 390 391
	list_for_each_entry(cis, &s->cis_cache, node) {
		int len = cis->len;

		if (len > 256)
			len = 256;
392 393

		pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf);
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407

		if (memcmp(buf, cis->cache, len) != 0) {
			kfree(buf);
			return -1;
		}
	}
	kfree(buf);
	return 0;
}

/*======================================================================

    For really bad cards, we provide a facility for uploading a
    replacement CIS.
D
Dominik Brodowski 已提交
408

L
Linus Torvalds 已提交
409 410
======================================================================*/

411 412
int pcmcia_replace_cis(struct pcmcia_socket *s,
		       const u8 *data, const size_t len)
L
Linus Torvalds 已提交
413
{
414 415 416 417
	if (len > CISTPL_MAX_CIS_SIZE) {
		dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n");
		return -EINVAL;
	}
418
	mutex_lock(&s->ops_mutex);
419 420 421 422
	kfree(s->fake_cis);
	s->fake_cis = kmalloc(len, GFP_KERNEL);
	if (s->fake_cis == NULL) {
		dev_printk(KERN_WARNING, &s->dev, "no memory to replace CIS\n");
423
		mutex_unlock(&s->ops_mutex);
424 425 426 427
		return -ENOMEM;
	}
	s->fake_cis_len = len;
	memcpy(s->fake_cis, data, len);
428
	mutex_unlock(&s->ops_mutex);
429
	return 0;
L
Linus Torvalds 已提交
430 431 432 433 434
}

/*======================================================================

    The high-level CIS tuple services
D
Dominik Brodowski 已提交
435

L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
======================================================================*/

typedef struct tuple_flags {
    u_int		link_space:4;
    u_int		has_link:1;
    u_int		mfc_fn:3;
    u_int		space:4;
} tuple_flags;

#define LINK_SPACE(f)	(((tuple_flags *)(&(f)))->link_space)
#define HAS_LINK(f)	(((tuple_flags *)(&(f)))->has_link)
#define MFC_FN(f)	(((tuple_flags *)(&(f)))->mfc_fn)
#define SPACE(f)	(((tuple_flags *)(&(f)))->space)

int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
{
    if (!s)
453
	return -EINVAL;
454 455

    if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS))
D
Dominik Brodowski 已提交
456
	return -ENODEV;
L
Linus Torvalds 已提交
457
    tuple->TupleLink = tuple->Flags = 0;
458 459 460 461 462 463

    /* Assume presence of a LONGLINK_C to address 0 */
    tuple->CISOffset = tuple->LinkOffset = 0;
    SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1;

    if ((s->functions > 1) && !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
L
Linus Torvalds 已提交
464 465
	cisdata_t req = tuple->DesiredTuple;
	tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
D
Dominik Brodowski 已提交
466
	if (pccard_get_next_tuple(s, function, tuple) == 0) {
L
Linus Torvalds 已提交
467
	    tuple->DesiredTuple = CISTPL_LINKTARGET;
D
Dominik Brodowski 已提交
468
	    if (pccard_get_next_tuple(s, function, tuple) != 0)
469
		return -ENOSPC;
L
Linus Torvalds 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	} else
	    tuple->CISOffset = tuple->TupleLink = 0;
	tuple->DesiredTuple = req;
    }
    return pccard_get_next_tuple(s, function, tuple);
}

static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
{
    u_char link[5];
    u_int ofs;

    if (MFC_FN(tuple->Flags)) {
	/* Get indirect link from the MFC tuple */
	read_cis_cache(s, LINK_SPACE(tuple->Flags),
		       tuple->LinkOffset, 5, link);
486
	ofs = get_unaligned_le32(link + 1);
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494 495 496 497
	SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
	/* Move to the next indirect link */
	tuple->LinkOffset += 5;
	MFC_FN(tuple->Flags)--;
    } else if (HAS_LINK(tuple->Flags)) {
	ofs = tuple->LinkOffset;
	SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags);
	HAS_LINK(tuple->Flags) = 0;
    } else {
	return -1;
    }
498
    if (SPACE(tuple->Flags)) {
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	/* This is ugly, but a common CIS error is to code the long
	   link offset incorrectly, so we check the right spot... */
	read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
	if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
	    (strncmp(link+2, "CIS", 3) == 0))
	    return ofs;
	remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
	/* Then, we try the wrong spot... */
	ofs = ofs >> 1;
    }
    read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
    if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
	(strncmp(link+2, "CIS", 3) == 0))
	return ofs;
    remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
    return -1;
}

int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
{
    u_char link[2], tmp;
    int ofs, i, attr;

    if (!s)
523
	return -EINVAL;
524
    if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS))
D
Dominik Brodowski 已提交
525
	return -ENODEV;
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539

    link[1] = tuple->TupleLink;
    ofs = tuple->CISOffset + tuple->TupleLink;
    attr = SPACE(tuple->Flags);

    for (i = 0; i < MAX_TUPLES; i++) {
	if (link[1] == 0xff) {
	    link[0] = CISTPL_END;
	} else {
	    read_cis_cache(s, attr, ofs, 2, link);
	    if (link[0] == CISTPL_NULL) {
		ofs++; continue;
	    }
	}
D
Dominik Brodowski 已提交
540

L
Linus Torvalds 已提交
541 542
	/* End of chain?  Follow long link if possible */
	if (link[0] == CISTPL_END) {
D
Dominik Brodowski 已提交
543 544
	    ofs = follow_link(s, tuple);
	    if (ofs < 0)
545
		return -ENOSPC;
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	    attr = SPACE(tuple->Flags);
	    read_cis_cache(s, attr, ofs, 2, link);
	}

	/* Is this a link tuple?  Make a note of it */
	if ((link[0] == CISTPL_LONGLINK_A) ||
	    (link[0] == CISTPL_LONGLINK_C) ||
	    (link[0] == CISTPL_LONGLINK_MFC) ||
	    (link[0] == CISTPL_LINKTARGET) ||
	    (link[0] == CISTPL_INDIRECT) ||
	    (link[0] == CISTPL_NO_LINK)) {
	    switch (link[0]) {
	    case CISTPL_LONGLINK_A:
		HAS_LINK(tuple->Flags) = 1;
		LINK_SPACE(tuple->Flags) = attr | IS_ATTR;
		read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
		break;
	    case CISTPL_LONGLINK_C:
		HAS_LINK(tuple->Flags) = 1;
		LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR;
		read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
		break;
	    case CISTPL_INDIRECT:
		HAS_LINK(tuple->Flags) = 1;
		LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT;
		tuple->LinkOffset = 0;
		break;
	    case CISTPL_LONGLINK_MFC:
		tuple->LinkOffset = ofs + 3;
		LINK_SPACE(tuple->Flags) = attr;
		if (function == BIND_FN_ALL) {
		    /* Follow all the MFC links */
		    read_cis_cache(s, attr, ofs+2, 1, &tmp);
		    MFC_FN(tuple->Flags) = tmp;
		} else {
		    /* Follow exactly one of the links */
		    MFC_FN(tuple->Flags) = 1;
		    tuple->LinkOffset += function * 5;
		}
		break;
	    case CISTPL_NO_LINK:
		HAS_LINK(tuple->Flags) = 0;
		break;
	    }
	    if ((tuple->Attributes & TUPLE_RETURN_LINK) &&
		(tuple->DesiredTuple == RETURN_FIRST_TUPLE))
		break;
	} else
	    if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
		break;
D
Dominik Brodowski 已提交
596

L
Linus Torvalds 已提交
597 598 599 600 601
	if (link[0] == tuple->DesiredTuple)
	    break;
	ofs += link[1] + 2;
    }
    if (i == MAX_TUPLES) {
602
	dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n");
603
	return -ENOSPC;
L
Linus Torvalds 已提交
604
    }
D
Dominik Brodowski 已提交
605

L
Linus Torvalds 已提交
606 607 608
    tuple->TupleCode = link[0];
    tuple->TupleLink = link[1];
    tuple->CISOffset = ofs + 2;
D
Dominik Brodowski 已提交
609
    return 0;
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618 619 620
}

/*====================================================================*/

#define _MIN(a, b)		(((a) < (b)) ? (a) : (b))

int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple)
{
    u_int len;

    if (!s)
621
	return -EINVAL;
L
Linus Torvalds 已提交
622 623

    if (tuple->TupleLink < tuple->TupleOffset)
624
	return -ENOSPC;
L
Linus Torvalds 已提交
625 626 627
    len = tuple->TupleLink - tuple->TupleOffset;
    tuple->TupleDataLen = tuple->TupleLink;
    if (len == 0)
D
Dominik Brodowski 已提交
628
	return 0;
L
Linus Torvalds 已提交
629 630 631
    read_cis_cache(s, SPACE(tuple->Flags),
		   tuple->CISOffset + tuple->TupleOffset,
		   _MIN(len, tuple->TupleDataMax), tuple->TupleData);
D
Dominik Brodowski 已提交
632
    return 0;
L
Linus Torvalds 已提交
633 634 635 636 637 638
}


/*======================================================================

    Parsing routines for individual tuples
D
Dominik Brodowski 已提交
639

L
Linus Torvalds 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652
======================================================================*/

static int parse_device(tuple_t *tuple, cistpl_device_t *device)
{
    int i;
    u_char scale;
    u_char *p, *q;

    p = (u_char *)tuple->TupleData;
    q = p + tuple->TupleDataLen;

    device->ndev = 0;
    for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
D
Dominik Brodowski 已提交
653 654 655

	if (*p == 0xff)
		break;
L
Linus Torvalds 已提交
656 657 658
	device->dev[i].type = (*p >> 4);
	device->dev[i].wp = (*p & 0x08) ? 1 : 0;
	switch (*p & 0x07) {
D
Dominik Brodowski 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	case 0:
		device->dev[i].speed = 0;
		break;
	case 1:
		device->dev[i].speed = 250;
		break;
	case 2:
		device->dev[i].speed = 200;
		break;
	case 3:
		device->dev[i].speed = 150;
		break;
	case 4:
		device->dev[i].speed = 100;
		break;
L
Linus Torvalds 已提交
674
	case 7:
675 676
		if (++p == q)
			return -EINVAL;
D
Dominik Brodowski 已提交
677 678 679 680 681
		device->dev[i].speed = SPEED_CVT(*p);
		while (*p & 0x80)
			if (++p == q)
				return -EINVAL;
		break;
L
Linus Torvalds 已提交
682
	default:
D
Dominik Brodowski 已提交
683
		return -EINVAL;
L
Linus Torvalds 已提交
684 685
	}

686 687 688 689
	if (++p == q)
		return -EINVAL;
	if (*p == 0xff)
		break;
L
Linus Torvalds 已提交
690
	scale = *p & 7;
691 692
	if (scale == 7)
		return -EINVAL;
L
Linus Torvalds 已提交
693 694
	device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
	device->ndev++;
695 696
	if (++p == q)
		break;
L
Linus Torvalds 已提交
697
    }
D
Dominik Brodowski 已提交
698

D
Dominik Brodowski 已提交
699
    return 0;
L
Linus Torvalds 已提交
700 701 702 703 704 705 706 707
}

/*====================================================================*/

static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
{
    u_char *p;
    if (tuple->TupleDataLen < 5)
708
	return -EINVAL;
709
    p = (u_char *) tuple->TupleData;
710 711
    csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2;
    csum->len = get_unaligned_le16(p + 2);
712
    csum->sum = *(p + 4);
D
Dominik Brodowski 已提交
713
    return 0;
L
Linus Torvalds 已提交
714 715 716 717 718 719 720
}

/*====================================================================*/

static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
{
    if (tuple->TupleDataLen < 4)
721
	return -EINVAL;
722
    link->addr = get_unaligned_le32(tuple->TupleData);
D
Dominik Brodowski 已提交
723
    return 0;
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732
}

/*====================================================================*/

static int parse_longlink_mfc(tuple_t *tuple,
			      cistpl_longlink_mfc_t *link)
{
    u_char *p;
    int i;
D
Dominik Brodowski 已提交
733

L
Linus Torvalds 已提交
734
    p = (u_char *)tuple->TupleData;
D
Dominik Brodowski 已提交
735

L
Linus Torvalds 已提交
736 737
    link->nfn = *p; p++;
    if (tuple->TupleDataLen <= link->nfn*5)
738
	return -EINVAL;
L
Linus Torvalds 已提交
739 740
    for (i = 0; i < link->nfn; i++) {
	link->fn[i].space = *p; p++;
741
	link->fn[i].addr = get_unaligned_le32(p);
742
	p += 4;
L
Linus Torvalds 已提交
743
    }
D
Dominik Brodowski 已提交
744
    return 0;
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753
}

/*====================================================================*/

static int parse_strings(u_char *p, u_char *q, int max,
			 char *s, u_char *ofs, u_char *found)
{
    int i, j, ns;

754 755
    if (p == q)
	    return -EINVAL;
L
Linus Torvalds 已提交
756 757
    ns = 0; j = 0;
    for (i = 0; i < max; i++) {
758 759
	if (*p == 0xff)
		break;
L
Linus Torvalds 已提交
760 761 762 763
	ofs[i] = j;
	ns++;
	for (;;) {
	    s[j++] = (*p == 0xff) ? '\0' : *p;
D
Dominik Brodowski 已提交
764 765
	    if ((*p == '\0') || (*p == 0xff))
		    break;
766 767
	    if (++p == q)
		    return -EINVAL;
L
Linus Torvalds 已提交
768
	}
D
Dominik Brodowski 已提交
769 770
	if ((*p == 0xff) || (++p == q))
		break;
L
Linus Torvalds 已提交
771 772 773
    }
    if (found) {
	*found = ns;
D
Dominik Brodowski 已提交
774
	return 0;
L
Linus Torvalds 已提交
775
    } else {
776
	return (ns == max) ? 0 : -EINVAL;
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784
    }
}

/*====================================================================*/

static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
{
    u_char *p, *q;
D
Dominik Brodowski 已提交
785

L
Linus Torvalds 已提交
786 787
    p = (u_char *)tuple->TupleData;
    q = p + tuple->TupleDataLen;
D
Dominik Brodowski 已提交
788

L
Linus Torvalds 已提交
789 790
    vers_1->major = *p; p++;
    vers_1->minor = *p; p++;
791 792
    if (p >= q)
	    return -EINVAL;
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801 802

    return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
			 vers_1->str, vers_1->ofs, &vers_1->ns);
}

/*====================================================================*/

static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
{
    u_char *p, *q;
D
Dominik Brodowski 已提交
803

L
Linus Torvalds 已提交
804 805
    p = (u_char *)tuple->TupleData;
    q = p + tuple->TupleDataLen;
D
Dominik Brodowski 已提交
806

L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
    return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
			 altstr->str, altstr->ofs, &altstr->ns);
}

/*====================================================================*/

static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
{
    u_char *p, *q;
    int nid;

    p = (u_char *)tuple->TupleData;
    q = p + tuple->TupleDataLen;

    for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
D
Dominik Brodowski 已提交
822 823
	if (p > q-2)
		break;
L
Linus Torvalds 已提交
824 825 826 827 828
	jedec->id[nid].mfr = p[0];
	jedec->id[nid].info = p[1];
	p += 2;
    }
    jedec->nid = nid;
D
Dominik Brodowski 已提交
829
    return 0;
L
Linus Torvalds 已提交
830 831 832 833 834 835 836
}

/*====================================================================*/

static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
{
    if (tuple->TupleDataLen < 4)
837
	return -EINVAL;
838 839
    m->manf = get_unaligned_le16(tuple->TupleData);
    m->card = get_unaligned_le16(tuple->TupleData + 2);
D
Dominik Brodowski 已提交
840
    return 0;
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848
}

/*====================================================================*/

static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
{
    u_char *p;
    if (tuple->TupleDataLen < 2)
849
	return -EINVAL;
L
Linus Torvalds 已提交
850 851 852
    p = (u_char *)tuple->TupleData;
    f->func = p[0];
    f->sysinit = p[1];
D
Dominik Brodowski 已提交
853
    return 0;
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862
}

/*====================================================================*/

static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
{
    u_char *p;
    int i;
    if (tuple->TupleDataLen < 1)
863
	return -EINVAL;
L
Linus Torvalds 已提交
864 865 866 867
    p = (u_char *)tuple->TupleData;
    f->type = p[0];
    for (i = 1; i < tuple->TupleDataLen; i++)
	f->data[i-1] = p[i];
D
Dominik Brodowski 已提交
868
    return 0;
L
Linus Torvalds 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881
}

/*====================================================================*/

static int parse_config(tuple_t *tuple, cistpl_config_t *config)
{
    int rasz, rmsz, i;
    u_char *p;

    p = (u_char *)tuple->TupleData;
    rasz = *p & 0x03;
    rmsz = (*p & 0x3c) >> 2;
    if (tuple->TupleDataLen < rasz+rmsz+4)
882
	return -EINVAL;
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893
    config->last_idx = *(++p);
    p++;
    config->base = 0;
    for (i = 0; i <= rasz; i++)
	config->base += p[i] << (8*i);
    p += rasz+1;
    for (i = 0; i < 4; i++)
	config->rmask[i] = 0;
    for (i = 0; i <= rmsz; i++)
	config->rmask[i>>2] += p[i] << (8*(i%4));
    config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
D
Dominik Brodowski 已提交
894
    return 0;
L
Linus Torvalds 已提交
895 896 897 898 899 900
}

/*======================================================================

    The following routines are all used to parse the nightmarish
    config table entries.
D
Dominik Brodowski 已提交
901

L
Linus Torvalds 已提交
902 903 904 905 906 907 908 909
======================================================================*/

static u_char *parse_power(u_char *p, u_char *q,
			   cistpl_power_t *pwr)
{
    int i;
    u_int scale;

D
Dominik Brodowski 已提交
910 911
    if (p == q)
	    return NULL;
L
Linus Torvalds 已提交
912 913 914 915 916
    pwr->present = *p;
    pwr->flags = 0;
    p++;
    for (i = 0; i < 7; i++)
	if (pwr->present & (1<<i)) {
D
Dominik Brodowski 已提交
917 918
	    if (p == q)
		    return NULL;
L
Linus Torvalds 已提交
919 920 921
	    pwr->param[i] = POWER_CVT(*p);
	    scale = POWER_SCALE(*p);
	    while (*p & 0x80) {
D
Dominik Brodowski 已提交
922 923
		if (++p == q)
			return NULL;
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
		if ((*p & 0x7f) < 100)
		    pwr->param[i] += (*p & 0x7f) * scale / 100;
		else if (*p == 0x7d)
		    pwr->flags |= CISTPL_POWER_HIGHZ_OK;
		else if (*p == 0x7e)
		    pwr->param[i] = 0;
		else if (*p == 0x7f)
		    pwr->flags |= CISTPL_POWER_HIGHZ_REQ;
		else
		    return NULL;
	    }
	    p++;
	}
    return p;
}

/*====================================================================*/

static u_char *parse_timing(u_char *p, u_char *q,
			    cistpl_timing_t *timing)
{
    u_char scale;

D
Dominik Brodowski 已提交
947 948
    if (p == q)
	    return NULL;
L
Linus Torvalds 已提交
949 950
    scale = *p;
    if ((scale & 3) != 3) {
D
Dominik Brodowski 已提交
951 952
	if (++p == q)
		return NULL;
L
Linus Torvalds 已提交
953 954 955 956 957 958
	timing->wait = SPEED_CVT(*p);
	timing->waitscale = exponent[scale & 3];
    } else
	timing->wait = 0;
    scale >>= 2;
    if ((scale & 7) != 7) {
D
Dominik Brodowski 已提交
959 960
	if (++p == q)
		return NULL;
L
Linus Torvalds 已提交
961 962 963 964 965 966
	timing->ready = SPEED_CVT(*p);
	timing->rdyscale = exponent[scale & 7];
    } else
	timing->ready = 0;
    scale >>= 3;
    if (scale != 7) {
D
Dominik Brodowski 已提交
967 968
	if (++p == q)
		return NULL;
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982
	timing->reserved = SPEED_CVT(*p);
	timing->rsvscale = exponent[scale];
    } else
	timing->reserved = 0;
    p++;
    return p;
}

/*====================================================================*/

static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
{
    int i, j, bsz, lsz;

D
Dominik Brodowski 已提交
983 984
    if (p == q)
	    return NULL;
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992
    io->flags = *p;

    if (!(*p & 0x80)) {
	io->nwin = 1;
	io->win[0].base = 0;
	io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
	return p+1;
    }
D
Dominik Brodowski 已提交
993 994 995

    if (++p == q)
	    return NULL;
L
Linus Torvalds 已提交
996 997
    io->nwin = (*p & 0x0f) + 1;
    bsz = (*p & 0x30) >> 4;
D
Dominik Brodowski 已提交
998 999
    if (bsz == 3)
	    bsz++;
L
Linus Torvalds 已提交
1000
    lsz = (*p & 0xc0) >> 6;
D
Dominik Brodowski 已提交
1001 1002
    if (lsz == 3)
	    lsz++;
L
Linus Torvalds 已提交
1003
    p++;
D
Dominik Brodowski 已提交
1004

L
Linus Torvalds 已提交
1005 1006 1007 1008
    for (i = 0; i < io->nwin; i++) {
	io->win[i].base = 0;
	io->win[i].len = 1;
	for (j = 0; j < bsz; j++, p++) {
D
Dominik Brodowski 已提交
1009 1010
	    if (p == q)
		    return NULL;
L
Linus Torvalds 已提交
1011 1012 1013
	    io->win[i].base += *p << (j*8);
	}
	for (j = 0; j < lsz; j++, p++) {
D
Dominik Brodowski 已提交
1014 1015
	    if (p == q)
		    return NULL;
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	    io->win[i].len += *p << (j*8);
	}
    }
    return p;
}

/*====================================================================*/

static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
{
    int i, j, asz, lsz, has_ha;
    u_int len, ca, ha;

D
Dominik Brodowski 已提交
1029 1030
    if (p == q)
	    return NULL;
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035

    mem->nwin = (*p & 0x07) + 1;
    lsz = (*p & 0x18) >> 3;
    asz = (*p & 0x60) >> 5;
    has_ha = (*p & 0x80);
D
Dominik Brodowski 已提交
1036 1037 1038
    if (++p == q)
	    return NULL;

L
Linus Torvalds 已提交
1039 1040 1041
    for (i = 0; i < mem->nwin; i++) {
	len = ca = ha = 0;
	for (j = 0; j < lsz; j++, p++) {
D
Dominik Brodowski 已提交
1042 1043
	    if (p == q)
		    return NULL;
L
Linus Torvalds 已提交
1044 1045 1046
	    len += *p << (j*8);
	}
	for (j = 0; j < asz; j++, p++) {
D
Dominik Brodowski 已提交
1047 1048
	    if (p == q)
		    return NULL;
L
Linus Torvalds 已提交
1049 1050 1051 1052
	    ca += *p << (j*8);
	}
	if (has_ha)
	    for (j = 0; j < asz; j++, p++) {
D
Dominik Brodowski 已提交
1053 1054
		if (p == q)
			return NULL;
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
		ha += *p << (j*8);
	    }
	mem->win[i].len = len << 8;
	mem->win[i].card_addr = ca << 8;
	mem->win[i].host_addr = ha << 8;
    }
    return p;
}

/*====================================================================*/

static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
{
1068 1069
    if (p == q)
	    return NULL;
L
Linus Torvalds 已提交
1070 1071
    irq->IRQInfo1 = *p; p++;
    if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
1072 1073
	if (p+2 > q)
		return NULL;
L
Linus Torvalds 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	irq->IRQInfo2 = (p[1]<<8) + p[0];
	p += 2;
    }
    return p;
}

/*====================================================================*/

static int parse_cftable_entry(tuple_t *tuple,
			       cistpl_cftable_entry_t *entry)
{
    u_char *p, *q, features;

    p = tuple->TupleData;
    q = p + tuple->TupleDataLen;
    entry->index = *p & 0x3f;
    entry->flags = 0;
    if (*p & 0x40)
	entry->flags |= CISTPL_CFTABLE_DEFAULT;
    if (*p & 0x80) {
1094 1095
	if (++p == q)
		return -EINVAL;
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	if (*p & 0x10)
	    entry->flags |= CISTPL_CFTABLE_BVDS;
	if (*p & 0x20)
	    entry->flags |= CISTPL_CFTABLE_WP;
	if (*p & 0x40)
	    entry->flags |= CISTPL_CFTABLE_RDYBSY;
	if (*p & 0x80)
	    entry->flags |= CISTPL_CFTABLE_MWAIT;
	entry->interface = *p & 0x0f;
    } else
	entry->interface = 0;

    /* Process optional features */
1109 1110
    if (++p == q)
	    return -EINVAL;
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115
    features = *p; p++;

    /* Power options */
    if ((features & 3) > 0) {
	p = parse_power(p, q, &entry->vcc);
1116 1117
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1118 1119 1120 1121
    } else
	entry->vcc.present = 0;
    if ((features & 3) > 1) {
	p = parse_power(p, q, &entry->vpp1);
1122 1123
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1124 1125 1126 1127
    } else
	entry->vpp1.present = 0;
    if ((features & 3) > 2) {
	p = parse_power(p, q, &entry->vpp2);
1128 1129
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134 1135
    } else
	entry->vpp2.present = 0;

    /* Timing options */
    if (features & 0x04) {
	p = parse_timing(p, q, &entry->timing);
1136 1137
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1138 1139 1140 1141 1142
    } else {
	entry->timing.wait = 0;
	entry->timing.ready = 0;
	entry->timing.reserved = 0;
    }
D
Dominik Brodowski 已提交
1143

L
Linus Torvalds 已提交
1144 1145 1146
    /* I/O window options */
    if (features & 0x08) {
	p = parse_io(p, q, &entry->io);
1147 1148
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1149 1150
    } else
	entry->io.nwin = 0;
D
Dominik Brodowski 已提交
1151

L
Linus Torvalds 已提交
1152 1153 1154
    /* Interrupt options */
    if (features & 0x10) {
	p = parse_irq(p, q, &entry->irq);
1155 1156
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165
    } else
	entry->irq.IRQInfo1 = 0;

    switch (features & 0x60) {
    case 0x00:
	entry->mem.nwin = 0;
	break;
    case 0x20:
	entry->mem.nwin = 1;
1166
	entry->mem.win[0].len = get_unaligned_le16(p) << 8;
L
Linus Torvalds 已提交
1167 1168 1169
	entry->mem.win[0].card_addr = 0;
	entry->mem.win[0].host_addr = 0;
	p += 2;
1170 1171
	if (p > q)
		return -EINVAL;
L
Linus Torvalds 已提交
1172 1173 1174
	break;
    case 0x40:
	entry->mem.nwin = 1;
1175 1176
	entry->mem.win[0].len = get_unaligned_le16(p) << 8;
	entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8;
L
Linus Torvalds 已提交
1177 1178
	entry->mem.win[0].host_addr = 0;
	p += 4;
1179 1180
	if (p > q)
		return -EINVAL;
L
Linus Torvalds 已提交
1181 1182 1183
	break;
    case 0x60:
	p = parse_mem(p, q, &entry->mem);
1184 1185
	if (p == NULL)
		return -EINVAL;
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190
	break;
    }

    /* Misc features */
    if (features & 0x80) {
1191 1192
	if (p == q)
		return -EINVAL;
L
Linus Torvalds 已提交
1193 1194
	entry->flags |= (*p << 8);
	while (*p & 0x80)
1195 1196
	    if (++p == q)
		    return -EINVAL;
L
Linus Torvalds 已提交
1197 1198 1199 1200
	p++;
    }

    entry->subtuples = q-p;
D
Dominik Brodowski 已提交
1201

D
Dominik Brodowski 已提交
1202
    return 0;
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
}

/*====================================================================*/

static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
{
    u_char *p, *q;
    int n;

    p = (u_char *)tuple->TupleData;
    q = p + tuple->TupleDataLen;

    for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
D
Dominik Brodowski 已提交
1216 1217
	if (p > q-6)
		break;
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226
	geo->geo[n].buswidth = p[0];
	geo->geo[n].erase_block = 1 << (p[1]-1);
	geo->geo[n].read_block  = 1 << (p[2]-1);
	geo->geo[n].write_block = 1 << (p[3]-1);
	geo->geo[n].partition   = 1 << (p[4]-1);
	geo->geo[n].interleave  = 1 << (p[5]-1);
	p += 6;
    }
    geo->ngeo = n;
D
Dominik Brodowski 已提交
1227
    return 0;
L
Linus Torvalds 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236
}

/*====================================================================*/

static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
{
    u_char *p, *q;

    if (tuple->TupleDataLen < 10)
1237
	return -EINVAL;
D
Dominik Brodowski 已提交
1238

L
Linus Torvalds 已提交
1239 1240 1241 1242 1243
    p = tuple->TupleData;
    q = p + tuple->TupleDataLen;

    v2->vers = p[0];
    v2->comply = p[1];
D
Dominik Brodowski 已提交
1244
    v2->dindex = get_unaligned_le16(p + 2);
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    v2->vspec8 = p[6];
    v2->vspec9 = p[7];
    v2->nhdr = p[8];
    p += 9;
    return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL);
}

/*====================================================================*/

static int parse_org(tuple_t *tuple, cistpl_org_t *org)
{
    u_char *p, *q;
    int i;
D
Dominik Brodowski 已提交
1258

L
Linus Torvalds 已提交
1259 1260
    p = tuple->TupleData;
    q = p + tuple->TupleDataLen;
1261 1262
    if (p == q)
	    return -EINVAL;
L
Linus Torvalds 已提交
1263
    org->data_org = *p;
1264 1265
    if (++p == q)
	    return -EINVAL;
L
Linus Torvalds 已提交
1266 1267
    for (i = 0; i < 30; i++) {
	org->desc[i] = *p;
D
Dominik Brodowski 已提交
1268 1269
	if (*p == '\0')
		break;
1270 1271
	if (++p == q)
		return -EINVAL;
L
Linus Torvalds 已提交
1272
    }
D
Dominik Brodowski 已提交
1273
    return 0;
L
Linus Torvalds 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282
}

/*====================================================================*/

static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
{
    u_char *p;

    if (tuple->TupleDataLen < 10)
1283
	return -EINVAL;
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288

    p = tuple->TupleData;

    fmt->type = p[0];
    fmt->edc = p[1];
1289 1290
    fmt->offset = get_unaligned_le32(p + 2);
    fmt->length = get_unaligned_le32(p + 6);
L
Linus Torvalds 已提交
1291

D
Dominik Brodowski 已提交
1292
    return 0;
L
Linus Torvalds 已提交
1293 1294 1295 1296
}

/*====================================================================*/

1297
int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse)
L
Linus Torvalds 已提交
1298
{
D
Dominik Brodowski 已提交
1299
    int ret = 0;
D
Dominik Brodowski 已提交
1300

L
Linus Torvalds 已提交
1301
    if (tuple->TupleDataLen > tuple->TupleDataMax)
1302
	return -EINVAL;
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
    switch (tuple->TupleCode) {
    case CISTPL_DEVICE:
    case CISTPL_DEVICE_A:
	ret = parse_device(tuple, &parse->device);
	break;
    case CISTPL_CHECKSUM:
	ret = parse_checksum(tuple, &parse->checksum);
	break;
    case CISTPL_LONGLINK_A:
    case CISTPL_LONGLINK_C:
	ret = parse_longlink(tuple, &parse->longlink);
	break;
    case CISTPL_LONGLINK_MFC:
	ret = parse_longlink_mfc(tuple, &parse->longlink_mfc);
	break;
    case CISTPL_VERS_1:
	ret = parse_vers_1(tuple, &parse->version_1);
	break;
    case CISTPL_ALTSTR:
	ret = parse_altstr(tuple, &parse->altstr);
	break;
    case CISTPL_JEDEC_A:
    case CISTPL_JEDEC_C:
	ret = parse_jedec(tuple, &parse->jedec);
	break;
    case CISTPL_MANFID:
	ret = parse_manfid(tuple, &parse->manfid);
	break;
    case CISTPL_FUNCID:
	ret = parse_funcid(tuple, &parse->funcid);
	break;
    case CISTPL_FUNCE:
	ret = parse_funce(tuple, &parse->funce);
	break;
    case CISTPL_CONFIG:
	ret = parse_config(tuple, &parse->config);
	break;
    case CISTPL_CFTABLE_ENTRY:
	ret = parse_cftable_entry(tuple, &parse->cftable_entry);
	break;
    case CISTPL_DEVICE_GEO:
    case CISTPL_DEVICE_GEO_A:
	ret = parse_device_geo(tuple, &parse->device_geo);
	break;
    case CISTPL_VERS_2:
	ret = parse_vers_2(tuple, &parse->vers_2);
	break;
    case CISTPL_ORG:
	ret = parse_org(tuple, &parse->org);
	break;
    case CISTPL_FORMAT:
    case CISTPL_FORMAT_A:
	ret = parse_format(tuple, &parse->format);
	break;
    case CISTPL_NO_LINK:
    case CISTPL_LINKTARGET:
D
Dominik Brodowski 已提交
1359
	ret = 0;
L
Linus Torvalds 已提交
1360 1361
	break;
    default:
1362
	ret = -EINVAL;
L
Linus Torvalds 已提交
1363 1364
	break;
    }
1365
    if (ret)
1366
	    pr_debug("parse_tuple failed %d\n", ret);
L
Linus Torvalds 已提交
1367 1368
    return ret;
}
1369
EXPORT_SYMBOL(pcmcia_parse_tuple);
L
Linus Torvalds 已提交
1370 1371 1372 1373

/*======================================================================

    This is used internally by Card Services to look up CIS stuff.
D
Dominik Brodowski 已提交
1374

L
Linus Torvalds 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383
======================================================================*/

int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse)
{
    tuple_t tuple;
    cisdata_t *buf;
    int ret;

    buf = kmalloc(256, GFP_KERNEL);
1384 1385 1386 1387
    if (buf == NULL) {
	    dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
	    return -ENOMEM;
    }
L
Linus Torvalds 已提交
1388
    tuple.DesiredTuple = code;
1389 1390 1391
    tuple.Attributes = 0;
    if (function == BIND_FN_ALL)
	    tuple.Attributes = TUPLE_RETURN_COMMON;
L
Linus Torvalds 已提交
1392
    ret = pccard_get_first_tuple(s, function, &tuple);
D
Dominik Brodowski 已提交
1393 1394
    if (ret != 0)
	    goto done;
L
Linus Torvalds 已提交
1395 1396 1397 1398
    tuple.TupleData = buf;
    tuple.TupleOffset = 0;
    tuple.TupleDataMax = 255;
    ret = pccard_get_tuple_data(s, &tuple);
D
Dominik Brodowski 已提交
1399 1400
    if (ret != 0)
	    goto done;
1401
    ret = pcmcia_parse_tuple(&tuple, parse);
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406
done:
    kfree(buf);
    return ret;
}

1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

/**
 * pccard_loop_tuple() - loop over tuples in the CIS
 * @s:		the struct pcmcia_socket where the card is inserted
 * @function:	the device function we loop for
 * @code:	which CIS code shall we look for?
 * @parse:	buffer where the tuple shall be parsed (or NULL, if no parse)
 * @priv_data:	private data to be passed to the loop_tuple function.
 * @loop_tuple:	function to call for each CIS entry of type @function. IT
 *		gets passed the raw tuple, the paresed tuple (if @parse is
 *		set) and @priv_data.
 *
 * pccard_loop_tuple() loops over all CIS entries of type @function, and
 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
 */
int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
		      cisdata_t code, cisparse_t *parse, void *priv_data,
		      int (*loop_tuple) (tuple_t *tuple,
					 cisparse_t *parse,
					 void *priv_data))
{
	tuple_t tuple;
	cisdata_t *buf;
	int ret;

	buf = kzalloc(256, GFP_KERNEL);
	if (buf == NULL) {
		dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
		return -ENOMEM;
	}

	tuple.TupleData = buf;
	tuple.TupleDataMax = 255;
	tuple.TupleOffset = 0;
	tuple.DesiredTuple = code;
	tuple.Attributes = 0;

	ret = pccard_get_first_tuple(s, function, &tuple);
	while (!ret) {
		if (pccard_get_tuple_data(s, &tuple))
			goto next_entry;

		if (parse)
			if (pcmcia_parse_tuple(&tuple, parse))
				goto next_entry;

		ret = loop_tuple(&tuple, parse, priv_data);
		if (!ret)
			break;

next_entry:
		ret = pccard_get_next_tuple(s, function, &tuple);
	}

	kfree(buf);
	return ret;
}


1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
/**
 * pccard_validate_cis() - check whether card has a sensible CIS
 * @s:		the struct pcmcia_socket we are to check
 * @info:	returns the number of tuples in the (valid) CIS, or 0
 *
 * This tries to determine if a card has a sensible CIS.  In @info, it
 * returns the number of tuples in the CIS, or 0 if the CIS looks bad. The
 * checks include making sure several critical tuples are present and
 * valid; seeing if the total number of tuples is reasonable; and
 * looking for tuples that use reserved codes.
 *
 * The function returns 0 on success.
 */
1480
int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info)
L
Linus Torvalds 已提交
1481
{
1482 1483 1484 1485
	tuple_t *tuple;
	cisparse_t *p;
	unsigned int count = 0;
	int ret, reserved, dev_ok = 0, ident_ok = 0;
L
Linus Torvalds 已提交
1486

1487 1488
	if (!s)
		return -EINVAL;
L
Linus Torvalds 已提交
1489

1490
	/* We do not want to validate the CIS cache... */
1491
	mutex_lock(&s->ops_mutex);
1492
	destroy_cis_cache(s);
1493
	mutex_unlock(&s->ops_mutex);
1494

1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
	tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
	if (tuple == NULL) {
		dev_warn(&s->dev, "no memory to validate CIS\n");
		return -ENOMEM;
	}
	p = kmalloc(sizeof(*p), GFP_KERNEL);
	if (p == NULL) {
		kfree(tuple);
		dev_warn(&s->dev, "no memory to validate CIS\n");
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
1506

1507 1508 1509 1510
	count = reserved = 0;
	tuple->DesiredTuple = RETURN_FIRST_TUPLE;
	tuple->Attributes = TUPLE_RETURN_COMMON;
	ret = pccard_get_first_tuple(s, BIND_FN_ALL, tuple);
D
Dominik Brodowski 已提交
1511
	if (ret != 0)
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
		goto done;

	/* First tuple should be DEVICE; we should really have either that
	   or a CFTABLE_ENTRY of some sort */
	if ((tuple->TupleCode == CISTPL_DEVICE) ||
	    (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY, p)) ||
	    (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY_CB, p)))
		dev_ok++;

	/* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
	   tuple, for card identification.  Certain old D-Link and Linksys
	   cards have only a broken VERS_2 tuple; hence the bogus test. */
	if ((pccard_read_tuple(s, BIND_FN_ALL, CISTPL_MANFID, p) == 0) ||
	    (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_1, p) == 0) ||
	    (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_2, p) != -ENOSPC))
		ident_ok++;

	if (!dev_ok && !ident_ok)
		goto done;

	for (count = 1; count < MAX_TUPLES; count++) {
		ret = pccard_get_next_tuple(s, BIND_FN_ALL, tuple);
		if (ret != 0)
			break;
		if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
		    ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
		    ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
			reserved++;
	}
	if ((count == MAX_TUPLES) || (reserved > 5) ||
		((!dev_ok || !ident_ok) && (count > 10)))
		count = 0;

	ret = 0;
L
Linus Torvalds 已提交
1546 1547

done:
1548 1549
	/* invalidate CIS cache on failure */
	if (!dev_ok || !ident_ok || !count) {
1550
		mutex_lock(&s->ops_mutex);
1551
		destroy_cis_cache(s);
1552
		mutex_unlock(&s->ops_mutex);
1553 1554 1555 1556 1557 1558 1559 1560
		ret = -EIO;
	}

	if (info)
		*info = count;
	kfree(tuple);
	kfree(p);
	return ret;
L
Linus Torvalds 已提交
1561
}
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687


#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)

static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf,
				  loff_t off, size_t count)
{
	tuple_t tuple;
	int status, i;
	loff_t pointer = 0;
	ssize_t ret = 0;
	u_char *tuplebuffer;
	u_char *tempbuffer;

	tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
	if (!tuplebuffer)
		return -ENOMEM;

	tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
	if (!tempbuffer) {
		ret = -ENOMEM;
		goto free_tuple;
	}

	memset(&tuple, 0, sizeof(tuple_t));

	tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
	tuple.DesiredTuple = RETURN_FIRST_TUPLE;
	tuple.TupleOffset = 0;

	status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
	while (!status) {
		tuple.TupleData = tuplebuffer;
		tuple.TupleDataMax = 255;
		memset(tuplebuffer, 0, sizeof(u_char) * 255);

		status = pccard_get_tuple_data(s, &tuple);
		if (status)
			break;

		if (off < (pointer + 2 + tuple.TupleDataLen)) {
			tempbuffer[0] = tuple.TupleCode & 0xff;
			tempbuffer[1] = tuple.TupleLink & 0xff;
			for (i = 0; i < tuple.TupleDataLen; i++)
				tempbuffer[i + 2] = tuplebuffer[i] & 0xff;

			for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
				if (((i + pointer) >= off) &&
				    (i + pointer) < (off + count)) {
					buf[ret] = tempbuffer[i];
					ret++;
				}
			}
		}

		pointer += 2 + tuple.TupleDataLen;

		if (pointer >= (off + count))
			break;

		if (tuple.TupleCode == CISTPL_END)
			break;
		status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
	}

	kfree(tempbuffer);
 free_tuple:
	kfree(tuplebuffer);

	return ret;
}


static ssize_t pccard_show_cis(struct kobject *kobj,
			       struct bin_attribute *bin_attr,
			       char *buf, loff_t off, size_t count)
{
	unsigned int size = 0x200;

	if (off >= size)
		count = 0;
	else {
		struct pcmcia_socket *s;
		unsigned int chains;

		if (off + count > size)
			count = size - off;

		s = to_socket(container_of(kobj, struct device, kobj));

		if (!(s->state & SOCKET_PRESENT))
			return -ENODEV;
		if (pccard_validate_cis(s, &chains))
			return -EIO;
		if (!chains)
			return -ENODATA;

		count = pccard_extract_cis(s, buf, off, count);
	}

	return count;
}


static ssize_t pccard_store_cis(struct kobject *kobj,
				struct bin_attribute *bin_attr,
				char *buf, loff_t off, size_t count)
{
	struct pcmcia_socket *s;
	int error;

	s = to_socket(container_of(kobj, struct device, kobj));

	if (off)
		return -EINVAL;

	if (count >= CISTPL_MAX_CIS_SIZE)
		return -EINVAL;

	if (!(s->state & SOCKET_PRESENT))
		return -ENODEV;

	error = pcmcia_replace_cis(s, buf, count);
	if (error)
		return -EIO;

1688
	pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699

	return count;
}


struct bin_attribute pccard_cis_attr = {
	.attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR },
	.size = 0x200,
	.read = pccard_show_cis,
	.write = pccard_store_cis,
};