summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pccard/pccardd/readcis.c
blob: 550f4d9f25d473ef4ef2ea28f321444e2c0079b1 (plain)
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
26
27
28
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * Copyright (c) 1995 Andrew McRae.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>

#include <pccard/card.h>
#include <pccard/cis.h>

#include "readcis.h"

static int read_attr(int, char *, int);
static int ck_linktarget(int, off_t, int);
static void cis_info(struct cis *, unsigned char *, int);
static void device_desc(unsigned char *, int, struct dev_mem *);
static void config_map(struct cis *, unsigned char *, int);
static void cis_config(struct cis *, unsigned char *, int);
static struct tuple_list *read_one_tuplelist(int, int, off_t);
static struct tuple_list *read_tuples(int);
static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
static struct tuple_info *get_tuple_info(unsigned char);

static struct tuple_info tuple_info[] = {
	{"Null tuple", 0x00, 0},
	{"Common memory descriptor", 0x01, 255},
	{"Checksum", 0x10, 5},
	{"Long link to attribute memory", 0x11, 4},
	{"Long link to common memory", 0x12, 4},
	{"Link target", 0x13, 3},
	{"No link", 0x14, 0},
	{"Version 1 info", 0x15, 255},
	{"Alternate language string", 0x16, 255},
	{"Attribute memory descriptor", 0x17, 255},
	{"JEDEC descr for common memory", 0x18, 255},
	{"JEDEC descr for attribute memory", 0x19, 255},
	{"Configuration map", 0x1A, 255},
	{"Configuration entry", 0x1B, 255},
	{"Other conditions for common memory", 0x1C, 255},
	{"Other conditions for attribute memory", 0x1D, 255},
	{"Geometry info for common memory", 0x1E, 255},
	{"Geometry info for attribute memory", 0x1F, 255},
	{"Manufacturer ID", 0x20, 4},
	{"Functional ID", 0x21, 255},
	{"Functional EXT", 0x22, 255},
	{"Software interleave", 0x23, 2},
	{"Version 2 Info", 0x40, 255},
	{"Data format", 0x41, 255},
	{"Geometry", 0x42, 4},
	{"Byte order", 0x43, 2},
	{"Card init date", 0x44, 4},
	{"Battery replacement", 0x45, 4},
	{"Organisation", 0x46, 255},
	{"Terminator", 0xFF, 255},
	{0, 0, 0}
};

/*
 *	After reading the tuples, decode the relevant ones.
 */
struct cis *
readcis(int fd)
{
	struct tuple_list *tl;
	struct tuple *tp;
	struct cis *cp;

	cp = xmalloc(sizeof(*cp));
	cp->tlist = read_tuples(fd);
	if (cp->tlist == 0)
		return (NULL);

	for (tl = cp->tlist; tl; tl = tl->next)
		for (tp = tl->tuples; tp; tp = tp->next) {
#if 0
			printf("tuple code = 0x%02x, data is\n", tp->code);
			dump(tp->data, tp->length);
#endif
			switch (tp->code) {
			case CIS_MEM_COMMON:	/* 0x01 */
				device_desc(tp->data, tp->length, &cp->common_mem);
				break;
			case CIS_INFO_V1:	/* 0x15 */
				cis_info(cp, tp->data, tp->length);
				break;
			case CIS_MEM_ATTR:	/* 0x17 */
				device_desc(tp->data, tp->length, &cp->attr_mem);
				break;
			case CIS_CONF_MAP:	/* 0x1A */
				config_map(cp, tp->data, tp->length);
				break;
			case CIS_CONFIG:	/* 0x1B */
				cis_config(cp, tp->data, tp->length);
				break;
			}
		}
	return (cp);
}

/*
 *	free_cis - delete cis entry.
 */
void
freecis(struct cis *cp)
{
	struct cis_ioblk *io;
	struct cis_memblk *mem;
	struct cis_config *conf;
	struct tuple *tp;
	struct tuple_list *tl;

	while ((tl = cp->tlist) != 0) {
		cp->tlist = tl->next;
		while ((tp = tl->tuples) != 0) {
			tl->tuples = tp->next;
			if (tp->data)
				free(tp->data);
		}
	}

	while ((conf = cp->conf) != 0) {
		cp->conf = conf->next;
		while ((io = conf->io) != 0) {
			conf->io = io->next;
			free(io);
		}
		while ((mem = conf->mem) != 0) {
			conf->mem = mem->next;
			free(mem);
		}
		free(conf);
	}
	free(cp);
}

/*
 *	Fills in CIS version data.
 */
static void
cis_info(struct cis *cp, unsigned char *p, int len)
{
	cp->maj_v = *p++;
	cp->min_v = *p++;
	strncpy(cp->manuf, p, CIS_MAXSTR - 1);
	while (*p++);
	strncpy(cp->vers, p, CIS_MAXSTR - 1);
	while (*p++);
	strncpy(cp->add_info1, p, CIS_MAXSTR - 1);
	while (*p++);
	strncpy(cp->add_info2, p, CIS_MAXSTR - 1);
}

/*
 *	device_desc - decode device descriptor.
 */
static void
device_desc(unsigned char *p, int len, struct dev_mem *dp)
{
	while (len > 0 && *p != 0xFF) {
		dp->valid = 1;
		dp->type = (*p & 0xF0) >> 4;
		dp->wps = !!(*p & 0x8);
		dp->speed = *p & 7;
		p++;
		if (*p != 0xFF) {
			dp->addr = *p >> 3;
			dp->units = *p & 7;
		}
		p++;
		len -= 2;
	}
}

/*
 *	configuration map of card control register.
 */
static void
config_map(struct cis *cp, unsigned char *p, int len)
{
	unsigned char *p1;
	int     i;
	union {
		unsigned long l;
		unsigned char b[4];
	} u;

	p1 = p + 1;
	cp->last_config = *p1++ & 0x3F;
	u.l = 0;
	for (i = 0; i <= (*p & 3); i++)
		u.b[i] = *p1++;
	cp->reg_addr = u.l;
	cp->ccrs = *p1;
}

/*
 *	CIS config entry - Decode and build configuration entry.
 */
static void
cis_config(struct cis *cp, unsigned char *p, int len)
{
	int     x;
	int     i, j;
	union {
		unsigned long l;
		unsigned char b[4];
	} u;
	struct cis_config *conf, *last;
	struct cis_memblk *mem;
	unsigned char feat;
	struct cis_memblk *lastmem = 0;

	conf = xmalloc(sizeof(*conf));
	if ((last = cp->conf) != 0) {
		while (last->next)
			last = last->next;
		last->next = conf;
	} else
		cp->conf = conf;
	conf->id = *p & 0x3F;
	if (*p & 0x40)
		cp->def_config = conf;
	if (*p++ & 0x80)
		p++;
	feat = *p++;
	for (i = 0; i < CIS_FEAT_POWER(feat); i++) {
		unsigned char parms = *p++;

		conf->pwr = 1;
		for (j = 0; j < 8; j++)
			if (parms & (1 << j))
				while (*p++ & 0x80);
	}
	if (feat & CIS_FEAT_TIMING) {
		conf->timing = 1;
		i = *p++;
		if (CIS_WAIT_SCALE(i) != 3)
			p++;
		if (CIS_READY_SCALE(i) != 7)
			p++;
		if (CIS_RESERVED_SCALE(i) != 7)
			p++;
	}
	if (feat & CIS_FEAT_I_O) {
		conf->iospace = 1;
		if (CIS_IO_RANGE & *p)
			conf->io_blks = CIS_IO_BLKS(p[1]) + 1;
		conf->io_addr = CIS_IO_ADDR(*p);
		conf->io_bus = (*p >> 5) & 3;
		if (*p++ & CIS_IO_RANGE) {
			struct cis_ioblk *io, *last = 0;
			i = CIS_IO_ADSZ(*p);
			j = CIS_IO_BLKSZ(*p++);
			for (x = 0; x < conf->io_blks; x++) {
				io = xmalloc(sizeof(*io));
				if (last)
					last->next = io;
				else
					conf->io = io;
				last = io;
				u.l = 0;
				switch (i) {
				case 0:
					break;
				case 1:
					u.b[0] = *p++;
					break;
				case 2:
					u.b[0] = *p++;
					u.b[1] = *p++;
					break;
				case 3:
					u.b[0] = *p++;
					u.b[1] = *p++;
					u.b[2] = *p++;
					u.b[3] = *p++;
					break;
				}
				io->addr = u.l;
				u.l = 0;
				switch (j) {
				case 0:
					break;
				case 1:
					u.b[0] = *p++;
					u.l++;
					break;
				case 2:
					u.b[0] = *p++;
					u.b[1] = *p++;
					u.l++;
					break;
				case 3:
					u.b[0] = *p++;
					u.b[1] = *p++;
					u.b[2] = *p++;
					u.b[3] = *p++;
					u.l++;
					break;
				}
				io->size = u.l;
			}
		}
	}
	if (feat & CIS_FEAT_IRQ) {
		conf->irq = 1;
		conf->irqlevel = *p & 0xF;
		conf->irq_flags = *p & 0xF0;
		if (*p++ & CIS_IRQ_MASK) {
			conf->irq_mask = (p[1] << 8) | p[0];
			p += 2;
		}
	}
	switch (CIS_FEAT_MEMORY(feat)) {
	case 0:
		break;
	case 1:
		conf->memspace = 1;
		conf->mem = xmalloc(sizeof(*conf->mem));
		conf->mem->length = ((p[1] << 8) | p[0]) << 8;
		break;
	case 2:
		conf->memspace = 1;
		conf->mem = xmalloc(sizeof(*conf->mem));
		conf->mem->length = ((p[1] << 8) | p[0]) << 8;
		conf->mem->address = ((p[3] << 8) | p[2]) << 8;
		break;
	case 3:
		conf->memspace = 1;
		x = *p++;
		conf->memwins = CIS_MEM_WINS(x);
		for (i = 0; i < conf->memwins; i++) {
			mem = xmalloc(sizeof(*mem));
			if (i == 0)
				conf->mem = mem;
			else
				lastmem->next = mem;
			lastmem = mem;
			u.l = 0;
			for (j = 0; j < CIS_MEM_LENSZ(x); j++)
				u.b[j] = *p++;
			mem->length = u.l << 8;
			u.l = 0;
			for (j = 0; j < CIS_MEM_ADDRSZ(x); j++)
				u.b[j] = *p++;
			mem->address = u.l << 8;
			if (x & CIS_MEM_HOST) {
				u.l = 0;
				for (j = 0; j < CIS_MEM_ADDRSZ(x); j++)
					u.b[j] = *p++;
				mem->host_address = u.l << 8;
			}
		}
		break;
	}
	if (feat & 0x80) {
		conf->misc_valid = 1;
		conf->misc = *p++;
	}
}

/*
 *	Read the tuples from the card.
 *	The processing of tuples is as follows:
 *		- Read tuples at attribute memory, offset 0.
 *		- If a CIS_END is the first tuple, look for
 *		  a tuple list at common memory offset 0; this list
 *		  must start with a LINKTARGET.
 *		- If a long link tuple was encountered, execute the long
 *		  link.
 *		- If a no-link tuple was seen, terminate processing.
 *		- If no no-link tuple exists, and no long link tuple
 *		  exists while processing the primary tuple list,
 *		  then look for a LINKTARGET tuple in common memory.
 *		- If a long link tuple is found in any list, then process
 *		  it. Only one link is allowed per list.
 */
static struct tuple_list *tlist;

static struct tuple_list *
read_tuples(int fd)
{
	struct tuple_list *tl = 0, *last_tl;
	struct tuple *tp;
	int     flag;
	off_t   offs;

	tlist = 0;
	last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);

	/* Now start processing the links (if any). */
	do {
		flag = MDF_ATTR;
		tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
		if (tp == 0) {
			flag = 0;
			tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
		}
		if (tp && tp->length == 4) {
			offs = tp->data[0] |
			    (tp->data[1] << 8) |
			    (tp->data[2] << 16) |
			    (tp->data[3] << 24);
#ifdef	DEBUG
			printf("Checking long link at %ld (%s memory)\n",
			    offs, flag ? "Attribute" : "Common");
#endif
			/* If a link was found, read the tuple list from it. */
			if (ck_linktarget(fd, offs, flag)) {
				tl = read_one_tuplelist(fd, flag, offs);
				last_tl->next = tl;
				last_tl = tl;
			}
		}
	} while (tl);

	/*
	 * If the primary list had no NOLINK tuple, and no LINKTARGET,
	 * then try to read a tuple list at common memory (offset 0).
	 */
	if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 && tlist->next == 0 &&
	    ck_linktarget(fd, (off_t) 0, 0)) {
#ifdef	DEBUG
		printf("Reading long link at %ld (%s memory)\n",
		    offs, flag ? "Attribute" : "Common");
#endif
		tlist->next = read_one_tuplelist(fd, 0, (off_t) 0);
	}
	return (tlist);
}

/*
 *	Read one tuple list from the card.
 */
static struct tuple_list *
read_one_tuplelist(int fd, int flags, off_t offs)
{
	struct tuple *tp, *last_tp = 0;
	struct tuple_list *tl;
	struct tuple_info *tinfo;
	int     total = 0;
	unsigned char code, length;

	/* Check to see if this memory has already been scanned. */
	for (tl = tlist; tl; tl = tl->next)
		if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
			return (0);
	tl = xmalloc(sizeof(*tl));
	tl->offs = offs;
	tl->flags = flags & MDF_ATTR;
	ioctl(fd, PIOCRWFLAG, &flags);
	lseek(fd, offs, SEEK_SET);
	do {
		if (read_attr(fd, &code, 1) != 1) {
			perror("CIS code read");
			break;
		}
		total++;
		if (code == CIS_NULL)
			continue;
		tp = xmalloc(sizeof(*tp));
		tp->code = code;
		if (read_attr(fd, &length, 1) != 1) {
			perror("CIS len read");
			break;
		}
		total++;
		tp->length = length;
#ifdef	DEBUG
		printf("Tuple code = 0x%x, len = %d\n", code, length);
#endif
		if (length == 0xFF) {
			length = tp->length = 0;
			code = CIS_END;
		}
		if (length != 0) {
			total += length;
			tp->data = xmalloc(length);
			if (read_attr(fd, tp->data, length) != length) {
				perror("CIS read");
				break;
			}
		}

		/*
		 * Check the tuple, and ignore it if it isn't in the table
		 * or the length is illegal.
		 */
		tinfo = get_tuple_info(code);
		if (tinfo == 0 || (tinfo->length != 255 && tinfo->length != length)) {
			printf("code %s ignored\n", tuple_name(code));
			tp->code = CIS_NULL;
		}
		if (tl->tuples == 0)
			tl->tuples = tp;
		else
			last_tp->next = tp;
		last_tp = tp;
	} while (code != CIS_END && total < 1024);
	return (tl);
}

/*
 *	return true if the offset points to a LINKTARGET tuple.
 */
static int
ck_linktarget(int fd, off_t offs, int flag)
{
	char    blk[5];

	ioctl(fd, PIOCRWFLAG, &flag);
	lseek(fd, offs, SEEK_SET);
	if (read_attr(fd, blk, 5) != 5)
		return (0);
	if (blk[0] == 0x13 &&
	    blk[1] == 0x3 &&
	    blk[2] == 'C' &&
	    blk[3] == 'I' &&
	    blk[4] == 'S')
		return (1);
	return (0);
}

/*
 *	find_tuple_in_list - find a tuple within a
 *	single tuple list.
 */
static struct tuple *
find_tuple_in_list(struct tuple_list *tl, unsigned char code)
{
	struct tuple *tp;

	for (tp = tl->tuples; tp; tp = tp->next)
		if (tp->code == code)
			break;
	return (tp);
}

static int
read_attr(int fd, char *bp, int len)
{
	char    blk[1024], *p = blk;
	int     i, l;

	if (len > sizeof(blk) / 2)
		len = sizeof(blk) / 2;
	l = i = read(fd, blk, len * 2);
	if (i <= 0) {
		printf("Read return %d bytes (expected %d)\n", i, len * 2);
		return (i);
	}
	while (i > 0) {
		*bp++ = *p++;
		p++;
		i -= 2;
	}
	return (l / 2);
}

/*
 *	return table entry for code.
 */
static struct tuple_info *
get_tuple_info(unsigned char code)
{
	struct tuple_info *tp;

	for (tp = tuple_info; tp->name; tp++)
		if (tp->code == code)
			return (tp);
	printf("Code %d not found\n", code);
	return (0);
}

char *
tuple_name(unsigned char code)
{
	struct tuple_info *tp;

	tp = get_tuple_info(code);
	if (tp)
		return (tp->name);
	return ("Unknown");
}
OpenPOWER on IntegriCloud