summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pccard/pccardc/printcis.c
blob: 2a31d953cb482e3e80c7a065d95b72a04f3c0ec3 (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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
 * 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"

int     dump_pwr_desc(unsigned char *);
void    print_ext_speed(unsigned char, int);
void    dump_device_desc(unsigned char *p, int len, char *type);
void    dump_info_v1(unsigned char *p, int len);
void    dump_config_map(struct tuple *tp);
void    dump_cis_config(struct tuple *tp);
void    dump_other_cond(unsigned char *p);
void    dump_func_ext(unsigned char *p, int len);

void
dumpcis(struct cis *cp)
{
	struct tuple *tp;
	struct tuple_list *tl;
	int     count = 0, sz, ad, i;
	unsigned char *p;

	for (tl = cp->tlist; tl; tl = tl->next)
		for (tp = tl->tuples; tp; tp = tp->next) {
			printf("Tuple #%d, code = 0x%x (%s), length = %d\n",
			    ++count, tp->code, tuple_name(tp->code), tp->length);
			p = tp->data;
			sz = tp->length;
			ad = 0;
			while (sz > 0) {
				printf("    %03x: ", ad);
				for (i = 0; i < ((sz < 16) ? sz : 16); i++)
					printf(" %02x", p[i]);
				printf("\n");
				sz -= 16;
				p += 16;
				ad += 16;
			}
			switch (tp->code) {
			default:
				break;
			case CIS_MEM_COMMON:	/* 0x01 */
				dump_device_desc(tp->data, tp->length, "Common");
				break;
			case CIS_CHECKSUM:	/* 0x10 */
				if (tp->length == 5) {
					printf("\tChecksum from offset %d, length %d, value is 0x%x\n",
					    (short)((tp->data[1] << 8) | tp->data[0]),
					    (tp->data[3] << 8) | tp->data[2],
					    tp->data[4]);
				} else
					printf("\tIllegal length for checksum!\n");
				break;
			case CIS_LONGLINK_A:	/* 0x11 */
				printf("\tLong link to attribute memory, address 0x%x\n",
				    (tp->data[3] << 24) |
				    (tp->data[2] << 16) |
				    (tp->data[1] << 8)  |
				    tp->data[0]);
				break;
			case CIS_LONGLINK_C:	/* 0x12 */
				printf("\tLong link to common memory, address 0x%x\n",
				    (tp->data[3] << 24) |
				    (tp->data[2] << 16) |
				    (tp->data[1] << 8)  |
				    tp->data[0]);
				break;
				break;
			case CIS_INFO_V1:	/* 0x15 */
				dump_info_v1(tp->data, tp->length);
				break;
			case CIS_ALTSTR:	/* 0x16 */
				break;
			case CIS_MEM_ATTR:	/* 0x17 */
				dump_device_desc(tp->data, tp->length, "Attribute");
				break;
			case CIS_JEDEC_C:	/* 0x18 */
				break;
			case CIS_JEDEC_A:	/* 0x19 */
				break;
			case CIS_CONF_MAP:	/* 0x1A */
				dump_config_map(tp);
				break;
			case CIS_CONFIG:	/* 0x1B */
				dump_cis_config(tp);
				break;
			case CIS_DEVICE_OC:	/* 0x1C */
				dump_other_cond(tp->data);
				break;
			case CIS_DEVICE_OA:	/* 0x1D */
				dump_other_cond(tp->data);
				break;
			case CIS_DEVICEGEO:	/* 0x1E */
				break;
			case CIS_DEVICEGEO_A:	/* 0x1F */
				break;
			case CIS_MANUF_ID:	/* 0x20 */
				printf("\tPCMCIA ID = 0x%x, OEM ID = 0x%x\n",
				    (tp->data[1] << 8) | tp->data[0],
				    (tp->data[3] << 8) | tp->data[2]);
				break;
			case CIS_FUNC_ID:	/* 0x21 */
				switch (tp->data[0]) {
				default:
					printf("\tUnknown function");
					break;
				case 0:
					printf("\tMultifunction card");
					break;
				case 1:
					printf("\tMemory card");
					break;
				case 2:
					printf("\tSerial port/modem");
					break;
				case 3:
					printf("\tParallel port");
					break;
				case 4:
					printf("\tFixed disk card");
					break;
				case 5:
					printf("\tVideo adapter");
					break;
				case 6:
					printf("\tNetwork/LAN adapter");
					break;
				case 7:
					printf("\tAIMS");
					break;
				}
				printf("%s%s\n", (tp->data[1] & 1) ? " - POST initialize" : "",
				    (tp->data[1] & 2) ? " - Card has ROM" : "");
				break;
			case CIS_FUNC_EXT:	/* 0x22 */
				dump_func_ext(tp->data, tp->length);
				break;
			case CIS_VERS_2:	/* 0x40 */
				break;
			}
		}
}

/*
 *	Dump configuration map tuple.
 */
void
dump_config_map(struct tuple *tp)
{
	unsigned char *p, x;
	int     rlen, mlen;
	int     i;
	union {
		unsigned long l;
		unsigned char b[4];
	} u;

	rlen = (tp->data[0] & 3) + 1;
	mlen = ((tp->data[0] >> 2) & 3) + 1;
	u.l = 0;
	p = tp->data + 2;
	for (i = 0; i < rlen; i++)
		u.b[i] = *p++;
	printf("\tReg len = %d, config register addr = 0x%lx, last config = 0x%x\n",
	    rlen, u.l, tp->data[1]);
	if (mlen)
		printf("\tRegisters: ");
	for (i = 0; i < mlen; i++, p++) {
		for (x = 0x1; x; x <<= 1)
			printf("%c", x & *p ? 'X' : '-');
		printf(" ");
	}
	printf("\n");
}

/*
 *	Dump a config entry.
 */
void
dump_cis_config(struct tuple *tp)
{
	unsigned char *p, feat;
	int     i, j;
	char    c;

	p = tp->data;
	printf("\tConfig index = 0x%x%s\n", *p & 0x3F,
	    *p & 0x40 ? "(default)" : "");
	if (*p & 0x80) {
		p++;
		printf("\tInterface byte = 0x%x ", *p);
		switch (*p & 0xF) {
		default:
			printf("(reserved)");
			break;
		case 0:
			printf("(memory)");
			break;
		case 1:
			printf("(I/O)");
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
			printf("(custom)");
			break;
		}
		c = ' ';
		if (*p & 0x10) {
			printf(" BVD1/2 active");
			c = ',';
		}
		if (*p & 0x20) {
			printf("%c card WP active", c);	/* Write protect */
			c = ',';
		}
		if (*p & 0x40) {
			printf("%c +RDY/-BSY active", c);
			c = ',';
		}
		if (*p & 0x80)
			printf("%c wait signal supported", c);
		printf("\n");
	}
	p++;
	feat = *p++;
	switch (CIS_FEAT_POWER(feat)) {
	case 0:
		break;
	case 1:
		printf("\tVcc pwr:\n");
		p += dump_pwr_desc(p);
		break;
	case 2:
		printf("\tVcc pwr:\n");
		p += dump_pwr_desc(p);
		printf("\tVpp pwr:\n");
		p += dump_pwr_desc(p);
		break;
	case 3:
		printf("\tVcc pwr:\n");
		p += dump_pwr_desc(p);
		printf("\tVpp1 pwr:\n");
		p += dump_pwr_desc(p);
		printf("\tVpp2 pwr:\n");
		p += dump_pwr_desc(p);
		break;
	}
	if (feat & CIS_FEAT_TIMING) {
		i = CIS_WAIT_SCALE(*p);
		j = CIS_READY_SCALE(*p);
		p++;
		if (i != 3) {
			printf("\tWait scale ");
			print_ext_speed(*p, i);
			while (*p & 0x80)
				p++;
			printf("\n");
		}
		if (j != 7) {
			printf("\tRDY/BSY scale ");
			print_ext_speed(*p, j);
			while (*p & 0x80)
				p++;
			printf("\n");
		}
	}
	if (feat & CIS_FEAT_I_O) {
		if (CIS_IO_ADDR(*p))
			printf("\tCard decodes %d address lines",
				CIS_IO_ADDR(*p));
		else
			printf("\tCard provides address decode");
		switch (CIS_MEM_ADDRSZ(*p)) {
		case 0:
			break;
		case 1:
			printf(", 8 Bit I/O only");
			break;
		case 2:
			printf(", limited 8/16 Bit I/O");
			break;
		case 3:
			printf(", full 8/16 Bit I/O");
			break;
		}
		printf("\n");
		if (*p & CIS_IO_RANGE) {
			p++;
			c = *p++;
			for (i = 0; i <= CIS_IO_BLKS(c); i++) {
				printf("\t\tI/O address # %d: ", i + 1);
				switch (CIS_IO_ADSZ(c)) {
				case 0:
					break;
				case 1:
					printf("block start = 0x%x", *p++);
					break;
				case 2:
					printf("block start = 0x%x", (p[1] << 8) | *p);
					p += 2;
					break;
				case 3:
					printf("block start = 0x%x",
					    (p[3] << 24) | (p[2] << 16) |
					    (p[1] << 8) | *p);
					p += 4;
					break;
				}
				switch (CIS_IO_BLKSZ(c)) {
				case 0:
					break;
				case 1:
					printf(" block length = 0x%x", *p++ + 1);
					break;
				case 2:
					printf(" block length = 0x%x", ((p[1] << 8) | *p) + 1);
					p += 2;
					break;
				case 3:
					printf(" block length = 0x%x",
					    ((p[3] << 24) | (p[2] << 16) |
						(p[1] << 8) | *p) + 1);
					p += 4;
					break;
				}
				printf("\n");
			}
		}
	}

	/* IRQ descriptor */
	if (feat & CIS_FEAT_IRQ) {
		printf("\t\tIRQ modes:");
		c = ' ';
		if (*p & CIS_IRQ_LEVEL) {
			printf(" Level");
			c = ',';
		}
		if (*p & CIS_IRQ_PULSE) {
			printf("%c Pulse", c);
			c = ',';
		}
		if (*p & CIS_IRQ_SHARING)
			printf("%c Shared", c);
		printf("\n");
		if (*p & CIS_IRQ_MASK) {
			i = p[0] | (p[1] << 8);
			printf("\t\tIRQs: ");
			if (*p & 1)
				printf(" NMI");
			if (*p & 0x2)
				printf(" IOCK");
			if (*p & 0x4)
				printf(" BERR");
			if (*p & 0x8)
				printf(" VEND");
			for (j = 0; j < 16; j++)
				if (i & (1 << j))
					printf(" %d", j);
			printf("\n");
			p += 3;
		} else {
			printf("\t\tIRQ level = %d\n", CIS_IRQ_IRQN(*p));
			p++;
		}
	}
	switch (CIS_FEAT_MEMORY(feat)) {
	case 0:
		break;
	case 1:
		printf("\tMemory space length = 0x%x\n", (p[1] << 8) | p[0]);
		p += 2;
		break;
	case 2:
		printf("\tMemory space address = 0x%x, length = 0x%x\n",
		    (p[3] << 8) | p[2],
		    (p[1] << 8) | p[0]);
		p += 4;
		break;

	/* Memory descriptors. */
	case 3:
		c = *p++;
		for (i = 0; i <= (c & 7); i++) {
			printf("\tMemory descriptor %d\n\t\t", i + 1);
			switch (CIS_MEM_LENSZ(c)) {
			case 0:
				break;
			case 1:
				printf(" blk length = 0x%x00", *p++);
				break;
			case 2:
				printf(" blk length = 0x%x00", (p[1] << 8) | *p);
				p += 2;
				break;
			case 3:
				printf(" blk length = 0x%x00",
				    (p[3] << 24) | (p[2] << 16) |
				    (p[1] << 8) | *p);
				p += 4;
				break;
			}
			switch (CIS_MEM_ADDRSZ(c)) {
			case 0:
				break;
			case 1:
				printf(" card addr = 0x%x00", *p++);
				break;
			case 2:
				printf(" card addr = 0x%x00", (p[1] << 8) | *p);
				p += 2;
				break;
			case 3:
				printf(" card addr = 0x%x00",
				    (p[3] << 24) | (p[2] << 16) |
				    (p[1] << 8) | *p);
				p += 4;
				break;
			}
			if (c & CIS_MEM_HOST)
				switch ((c >> 5) & 3) {
				case 0:
					break;
				case 1:
					printf(" host addr = 0x%x00", *p++);
					break;
				case 2:
					printf(" host addr = 0x%x00", (p[1] << 8) | *p);
					p += 2;
					break;
				case 3:
					printf(" host addr = 0x%x00",
					    (p[3] << 24) | (p[2] << 16) |
					    (p[1] << 8) | *p);
					p += 4;
					break;
				}
			printf("\n");
		}
		break;
	}
	if (feat & CIS_FEAT_MISC) {
		printf("\tMax twin cards = %d\n", *p & 7);
		printf("\tMisc attr:");
		if (*p & 0x8)
			printf(" (Audio-BVD2)");
		if (*p & 0x10)
			printf(" (Read-only)");
		if (*p & 0x20)
			printf(" (Power down supported)");
		if (*p & 0x80) {
			printf(" (Ext byte = 0x%x)", p[1]);
			p++;
		}
		printf("\n");
		p++;
	}
}

/*
 *	dump_other_cond - Dump other conditions.
 */
void
dump_other_cond(unsigned char *p)
{
	if (p[0]) {
		printf("\t");
		if (p[0] & 1)
			printf("(MWAIT)");
		if (p[0] & 2)
			printf(" (3V card)");
		if (p[0] & 0x80)
			printf(" (Extension bytes follow)");
		printf("\n");
	}
}

/*
 *	Dump power descriptor.
 */
int
dump_pwr_desc(unsigned char *p)
{
	int     len = 1, i;
	unsigned char mask;
	char  **expp;
	static char *pname[] =
	{"Nominal operating supply voltage",
	 "Minimum operating supply voltage",
	 "Maximum operating supply voltage",
	 "Continuous supply current",
	 "Max current average over 1 second",
	 "Max current average over 10 ms",
	 "Power down supply current",
	 "Reserved"
	};
	static char *vexp[] =
	{"10uV", "100uV", "1mV", "10mV", "100mV", "1V", "10V", "100V"};
	static char *cexp[] =
	{"10nA", "1uA", "10uA", "100uA", "1mA", "10mA", "100mA", "1A"};
	static char *mant[] =
	{"1", "1.2", "1.3", "1.5", "2", "2.5", "3", "3.5", "4", "4.5",
	"5", "5.5", "6", "7", "8", "9"};

	mask = *p++;
	expp = vexp;
	for (i = 0; i < 8; i++)
		if (mask & (1 << i)) {
			len++;
			if (i >= 3)
				expp = cexp;
			printf("\t\t%s: ", pname[i]);
			printf("%s x %s",
			    mant[(*p >> 3) & 0xF],
			    expp[*p & 7]);
			while (*p & 0x80) {
				len++;
				p++;
				printf(", ext = 0x%x", *p);
			}
			printf("\n");
			p++;
		}
	return (len);
}

void
dump_device_desc(unsigned char *p, int len, char *type)
{
	static char *un_name[] =
	{"512b", "2Kb", "8Kb", "32Kb", "128Kb", "512Kb", "2Mb", "reserved"};
	static char *speed[] =
	{"No speed", "250nS", "200nS", "150nS",
	"100nS", "Reserved", "Reserved"};
	static char *dev[] =
	{"No device", "Mask ROM", "OTPROM", "UV EPROM",
	 "EEPROM", "FLASH EEPROM", "SRAM", "DRAM",
	 "Reserved", "Reserved", "Reserved", "Reserved",
	 "Reserved", "Function specific", "Extended",
	"Reserved"};
	int     count = 0;

	while (*p != 0xFF && len > 0) {
		unsigned char x;

		x = *p++;
		len -= 2;
		if (count++ == 0)
			printf("\t%s memory device information:\n", type);
		printf("\t\tDevice number %d, type %s, WPS = %s\n",
		    count, dev[x >> 4], (x & 0x8) ? "ON" : "OFF");
		if ((x & 7) == 7) {
			len--;
			if (*p) {
				printf("\t\t");
				print_ext_speed(*p, 0);
				while (*p & 0x80) {
					p++;
					len--;
				}
			}
			p++;
		} else
			printf("\t\tSpeed = %s", speed[x & 7]);
		printf(", Memory block size = %s, %d units\n",
		    un_name[*p & 7], (*p >> 3) + 1);
		p++;
	}
}

/*
 *	Print version info
 */
void
dump_info_v1(unsigned char *p, int len)
{
	printf("\tVersion = %d.%d", p[0], p[1]);
	p += 2;
	printf(", Manuf = [%s],", p);
	while (*p++);
	printf("card vers = [%s]\n", p);
	while (*p++);
	printf("\tAddit. info = [%s]", p);
	while (*p++);
	printf(",[%s]\n", p);
}

/*
 *	dump functional extension tuple.
 */
void
dump_func_ext(unsigned char *p, int len)
{
	if (len == 0)
		return;
	switch (p[0]) {
	case 0:
	case 8:
	case 10:
		if (len != 4) {
			printf("\tWrong length for serial extension\n");
			return;
		}
		printf("\tSerial interface extension:\n");
		switch (p[1] & 0x1F) {
		default:
			printf("\t\tUnkn device");
			break;
		case 0:
			printf("\t\t8250 UART");
			break;
		case 1:
			printf("\t\t16450 UART");
			break;
		case 2:
			printf("\t\t16550 UART");
			break;
		}
		printf(", Parity - %s%s%s%s",
		    (p[2] & 1) ? "Space," : "",
		    (p[2] & 2) ? "Mark," : "",
		    (p[2] & 4) ? "Odd," : "",
		    (p[2] & 8) ? "Even," : "");
		printf("\n");
		break;
	case 1:
	case 5:
	case 6:
	case 7:
		printf("\tModem interface capabilities:\n");
		break;
	case 2:
		printf("\tData modem services available:\n");
		break;
	case 9:
		printf("\tFax/modem services available:\n");
		break;
	case 4:
		printf("\tVoice services available:\n");
		break;
	}
}

/*
 *	print_ext_speed - Print extended speed.
 */
void
print_ext_speed(unsigned char x, int scale)
{
	static char *mant[] =
	{"Reserved", "1.0", "1.2", "1.3", "1.5", "2.0", "2.5", "3.0",
	"3.5", "4.0", "4.5", "5.0", "5.5", "6.0", "7.0", "8.0"};
	static char *exp[] =
	{"1 ns", "10 ns", "100 ns", "1 us", "10 us", "100 us",
	"1 ms", "10 ms"};
	static char *scale_name[] =
	{"None", "10", "100", "1,000", "10,000", "100,000",
	"1,000,000", "10,000,000"};

	printf("Speed = %s x %s", mant[(x >> 3) & 0xF], exp[x & 7]);
	if (scale)
		printf(", scaled by %s", scale_name[scale & 7]);
}
OpenPOWER on IntegriCloud