readcis.c revision 50479
1/*
2 * Copyright (c) 1995 Andrew McRae.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef lint
28static const char rcsid[] =
29  "$FreeBSD: head/usr.sbin/pccard/pccardd/readcis.c 50479 1999-08-28 01:35:59Z peter $";
30#endif /* not lint */
31
32#include <err.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include <sys/ioctl.h>
38
39#include <pccard/cardinfo.h>
40#include <pccard/cis.h>
41
42#include "readcis.h"
43
44static int read_attr(int, char *, int);
45static int ck_linktarget(int, off_t, int);
46static void cis_info(struct cis *, unsigned char *, int);
47static void device_desc(unsigned char *, int, struct dev_mem *);
48static void config_map(struct cis *, unsigned char *, int);
49static void cis_config(struct cis *, unsigned char *, int);
50static struct tuple_list *read_one_tuplelist(int, int, off_t);
51static struct tuple_list *read_tuples(int);
52static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
53static struct tuple_info *get_tuple_info(unsigned char);
54
55static struct tuple_info tuple_info[] = {
56	{"Null tuple", 0x00, 0},
57	{"Common memory descriptor", 0x01, 255},
58	{"Checksum", 0x10, 5},
59	{"Long link to attribute memory", 0x11, 4},
60	{"Long link to common memory", 0x12, 4},
61	{"Link target", 0x13, 3},
62	{"No link", 0x14, 0},
63	{"Version 1 info", 0x15, 255},
64	{"Alternate language string", 0x16, 255},
65	{"Attribute memory descriptor", 0x17, 255},
66	{"JEDEC descr for common memory", 0x18, 255},
67	{"JEDEC descr for attribute memory", 0x19, 255},
68	{"Configuration map", 0x1A, 255},
69	{"Configuration entry", 0x1B, 255},
70	{"Other conditions for common memory", 0x1C, 255},
71	{"Other conditions for attribute memory", 0x1D, 255},
72	{"Geometry info for common memory", 0x1E, 255},
73	{"Geometry info for attribute memory", 0x1F, 255},
74	{"Manufacturer ID", 0x20, 4},
75	{"Functional ID", 0x21, 255},
76	{"Functional EXT", 0x22, 255},
77	{"Software interleave", 0x23, 2},
78	{"Version 2 Info", 0x40, 255},
79	{"Data format", 0x41, 255},
80	{"Geometry", 0x42, 4},
81	{"Byte order", 0x43, 2},
82	{"Card init date", 0x44, 4},
83	{"Battery replacement", 0x45, 4},
84	{"Organisation", 0x46, 255},
85	{"Terminator", 0xFF, 255},
86	{0, 0, 0}
87};
88
89/*
90 *	After reading the tuples, decode the relevant ones.
91 */
92struct cis *
93readcis(int fd)
94{
95	struct tuple_list *tl;
96	struct tuple *tp;
97	struct cis *cp;
98
99	cp = xmalloc(sizeof(*cp));
100	cp->tlist = read_tuples(fd);
101	if (cp->tlist == 0)
102		return (NULL);
103
104	for (tl = cp->tlist; tl; tl = tl->next)
105		for (tp = tl->tuples; tp; tp = tp->next) {
106#if 0
107			printf("tuple code = 0x%02x, data is\n", tp->code);
108			dump(tp->data, tp->length);
109#endif
110			switch (tp->code) {
111			case CIS_MEM_COMMON:	/* 0x01 */
112				device_desc(tp->data, tp->length, &cp->common_mem);
113				break;
114			case CIS_INFO_V1:	/* 0x15 */
115				cis_info(cp, tp->data, tp->length);
116				break;
117			case CIS_MEM_ATTR:	/* 0x17 */
118				device_desc(tp->data, tp->length, &cp->attr_mem);
119				break;
120			case CIS_CONF_MAP:	/* 0x1A */
121				config_map(cp, tp->data, tp->length);
122				break;
123			case CIS_CONFIG:	/* 0x1B */
124				cis_config(cp, tp->data, tp->length);
125				break;
126			}
127		}
128	return (cp);
129}
130
131/*
132 *	free_cis - delete cis entry.
133 */
134void
135freecis(struct cis *cp)
136{
137	struct cis_ioblk *io;
138	struct cis_memblk *mem;
139	struct cis_config *conf;
140	struct tuple *tp;
141	struct tuple_list *tl;
142
143	while ((tl = cp->tlist) != 0) {
144		cp->tlist = tl->next;
145		while ((tp = tl->tuples) != 0) {
146			tl->tuples = tp->next;
147			if (tp->data)
148				free(tp->data);
149		}
150	}
151
152	while ((conf = cp->conf) != 0) {
153		cp->conf = conf->next;
154		while ((io = conf->io) != 0) {
155			conf->io = io->next;
156			free(io);
157		}
158		while ((mem = conf->mem) != 0) {
159			conf->mem = mem->next;
160			free(mem);
161		}
162		free(conf);
163	}
164	free(cp);
165}
166
167/*
168 *	Fills in CIS version data.
169 */
170static void
171cis_info(struct cis *cp, unsigned char *p, int len)
172{
173	*cp->manuf = *cp->vers = *cp->add_info1 = *cp->add_info2 = '\0';
174	cp->maj_v = *p++;
175	cp->min_v = *p++;
176	strncpy(cp->manuf, p, CIS_MAXSTR - 1);
177	while (*p++);
178	strncpy(cp->vers, p, CIS_MAXSTR - 1);
179	while (*p++);
180	strncpy(cp->add_info1, p, CIS_MAXSTR - 1);
181	while (*p++);
182	strncpy(cp->add_info2, p, CIS_MAXSTR - 1);
183}
184
185/*
186 *	device_desc - decode device descriptor.
187 */
188static void
189device_desc(unsigned char *p, int len, struct dev_mem *dp)
190{
191	while (len > 0 && *p != 0xFF) {
192		dp->valid = 1;
193		dp->type = (*p & 0xF0) >> 4;
194		dp->wps = !!(*p & 0x8);
195		dp->speed = *p & 7;
196		p++;
197		if (*p != 0xFF) {
198			dp->addr = *p >> 3;
199			dp->units = *p & 7;
200		}
201		p++;
202		len -= 2;
203	}
204}
205
206/*
207 *	configuration map of card control register.
208 */
209static void
210config_map(struct cis *cp, unsigned char *p, int len)
211{
212	unsigned char *p1;
213	int     i;
214	union {
215		unsigned long l;
216		unsigned char b[4];
217	} u;
218
219	p1 = p + 1;
220	cp->last_config = *p1++ & 0x3F;
221	u.l = 0;
222	for (i = 0; i <= (*p & 3); i++)
223		u.b[i] = *p1++;
224	cp->reg_addr = u.l;
225	cp->ccrs = *p1;
226}
227
228/*
229 *	CIS config entry - Decode and build configuration entry.
230 */
231static void
232cis_config(struct cis *cp, unsigned char *p, int len)
233{
234	int     x;
235	int     i, j;
236	union {
237		unsigned long l;
238		unsigned char b[4];
239	} u;
240	struct cis_config *conf, *last;
241	struct cis_memblk *mem;
242	unsigned char feat;
243	struct cis_memblk *lastmem = 0;
244
245	conf = xmalloc(sizeof(*conf));
246	if ((last = cp->conf) != 0) {
247		while (last->next)
248			last = last->next;
249		last->next = conf;
250	} else
251		cp->conf = conf;
252	conf->id = *p & 0x3F;
253	if (*p & 0x40)
254		cp->def_config = conf;
255	if (*p++ & 0x80)
256		p++;
257	feat = *p++;
258	for (i = 0; i < CIS_FEAT_POWER(feat); i++) {
259		unsigned char parms = *p++;
260
261		conf->pwr = 1;
262		for (j = 0; j < 8; j++)
263			if (parms & (1 << j))
264				while (*p++ & 0x80);
265	}
266	if (feat & CIS_FEAT_TIMING) {
267		conf->timing = 1;
268		i = *p++;
269		if (CIS_WAIT_SCALE(i) != 3)
270			p++;
271		if (CIS_READY_SCALE(i) != 7)
272			p++;
273		if (CIS_RESERVED_SCALE(i) != 7)
274			p++;
275	}
276	if (feat & CIS_FEAT_I_O) {
277		conf->iospace = 1;
278		if (CIS_IO_RANGE & *p)
279			conf->io_blks = CIS_IO_BLKS(p[1]) + 1;
280		conf->io_addr = CIS_IO_ADDR(*p);
281		conf->io_bus = (*p >> 5) & 3;
282		if (*p++ & CIS_IO_RANGE) {
283			struct cis_ioblk *io, *last = 0;
284			i = CIS_IO_ADSZ(*p);
285			j = CIS_IO_BLKSZ(*p++);
286			for (x = 0; x < conf->io_blks; x++) {
287				io = xmalloc(sizeof(*io));
288				if (last)
289					last->next = io;
290				else
291					conf->io = io;
292				last = io;
293				u.l = 0;
294				switch (i) {
295				case 0:
296					break;
297				case 1:
298					u.b[0] = *p++;
299					break;
300				case 2:
301					u.b[0] = *p++;
302					u.b[1] = *p++;
303					break;
304				case 3:
305					u.b[0] = *p++;
306					u.b[1] = *p++;
307					u.b[2] = *p++;
308					u.b[3] = *p++;
309					break;
310				}
311				io->addr = u.l;
312				u.l = 0;
313				switch (j) {
314				case 0:
315					break;
316				case 1:
317					u.b[0] = *p++;
318					u.l++;
319					break;
320				case 2:
321					u.b[0] = *p++;
322					u.b[1] = *p++;
323					u.l++;
324					break;
325				case 3:
326					u.b[0] = *p++;
327					u.b[1] = *p++;
328					u.b[2] = *p++;
329					u.b[3] = *p++;
330					u.l++;
331					break;
332				}
333				io->size = u.l;
334			}
335		}
336	}
337	if (feat & CIS_FEAT_IRQ) {
338		conf->irq = 1;
339		conf->irqlevel = *p & 0xF;
340		conf->irq_flags = *p & 0xF0;
341		if (*p++ & CIS_IRQ_MASK) {
342			conf->irq_mask = (p[1] << 8) | p[0];
343			p += 2;
344		}
345	}
346	switch (CIS_FEAT_MEMORY(feat)) {
347	case 0:
348		break;
349	case 1:
350		conf->memspace = 1;
351		conf->mem = xmalloc(sizeof(*conf->mem));
352		conf->mem->length = ((p[1] << 8) | p[0]) << 8;
353		break;
354	case 2:
355		conf->memspace = 1;
356		conf->mem = xmalloc(sizeof(*conf->mem));
357		conf->mem->length = ((p[1] << 8) | p[0]) << 8;
358		conf->mem->address = ((p[3] << 8) | p[2]) << 8;
359		break;
360	case 3:
361		conf->memspace = 1;
362		x = *p++;
363		conf->memwins = CIS_MEM_WINS(x);
364		for (i = 0; i < conf->memwins; i++) {
365			mem = xmalloc(sizeof(*mem));
366			if (i == 0)
367				conf->mem = mem;
368			else
369				lastmem->next = mem;
370			lastmem = mem;
371			u.l = 0;
372			for (j = 0; j < CIS_MEM_LENSZ(x); j++)
373				u.b[j] = *p++;
374			mem->length = u.l << 8;
375			u.l = 0;
376			for (j = 0; j < CIS_MEM_ADDRSZ(x); j++)
377				u.b[j] = *p++;
378			mem->address = u.l << 8;
379			if (x & CIS_MEM_HOST) {
380				u.l = 0;
381				for (j = 0; j < CIS_MEM_ADDRSZ(x); j++)
382					u.b[j] = *p++;
383				mem->host_address = u.l << 8;
384			}
385		}
386		break;
387	}
388	if (feat & 0x80) {
389		conf->misc_valid = 1;
390		conf->misc = *p++;
391	}
392}
393
394/*
395 *	Read the tuples from the card.
396 *	The processing of tuples is as follows:
397 *		- Read tuples at attribute memory, offset 0.
398 *		- If a CIS_END is the first tuple, look for
399 *		  a tuple list at common memory offset 0; this list
400 *		  must start with a LINKTARGET.
401 *		- If a long link tuple was encountered, execute the long
402 *		  link.
403 *		- If a no-link tuple was seen, terminate processing.
404 *		- If no no-link tuple exists, and no long link tuple
405 *		  exists while processing the primary tuple list,
406 *		  then look for a LINKTARGET tuple in common memory.
407 *		- If a long link tuple is found in any list, then process
408 *		  it. Only one link is allowed per list.
409 */
410static struct tuple_list *tlist;
411
412static struct tuple_list *
413read_tuples(int fd)
414{
415	struct tuple_list *tl = 0, *last_tl;
416	struct tuple *tp;
417	int     flag;
418	off_t   offs;
419
420	tlist = 0;
421	last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
422
423	/* Now start processing the links (if any). */
424	do {
425		flag = MDF_ATTR;
426		tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
427		if (tp == 0) {
428			flag = 0;
429			tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
430		}
431		if (tp && tp->length == 4) {
432			offs = tp->data[0] |
433			    (tp->data[1] << 8) |
434			    (tp->data[2] << 16) |
435			    (tp->data[3] << 24);
436#ifdef	DEBUG
437			printf("Checking long link at %ld (%s memory)\n",
438			    offs, flag ? "Attribute" : "Common");
439#endif
440			/* If a link was found, read the tuple list from it. */
441			if (ck_linktarget(fd, offs, flag)) {
442				tl = read_one_tuplelist(fd, flag, offs);
443				last_tl->next = tl;
444				last_tl = tl;
445			}
446		} else
447			tl = 0;
448	} while (tl);
449
450	/*
451	 * If the primary list had no NOLINK tuple, and no LINKTARGET,
452	 * then try to read a tuple list at common memory (offset 0).
453	 */
454	if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 && tlist->next == 0 &&
455	    ck_linktarget(fd, (off_t) 0, 0)) {
456#ifdef	DEBUG
457		printf("Reading long link at %ld (%s memory)\n",
458		    offs, flag ? "Attribute" : "Common");
459#endif
460		tlist->next = read_one_tuplelist(fd, 0, (off_t) 0);
461	}
462	return (tlist);
463}
464
465/*
466 *	Read one tuple list from the card.
467 */
468static struct tuple_list *
469read_one_tuplelist(int fd, int flags, off_t offs)
470{
471	struct tuple *tp, *last_tp = 0;
472	struct tuple_list *tl;
473	struct tuple_info *tinfo;
474	int     total = 0;
475	unsigned char code, length;
476
477	/* Check to see if this memory has already been scanned. */
478	for (tl = tlist; tl; tl = tl->next)
479		if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
480			return (0);
481	tl = xmalloc(sizeof(*tl));
482	tl->offs = offs;
483	tl->flags = flags & MDF_ATTR;
484	ioctl(fd, PIOCRWFLAG, &flags);
485	lseek(fd, offs, SEEK_SET);
486	do {
487		if (read_attr(fd, &code, 1) != 1) {
488			warn("CIS code read");
489			break;
490		}
491		total++;
492		if (code == CIS_NULL)
493			continue;
494		tp = xmalloc(sizeof(*tp));
495		tp->code = code;
496		if (read_attr(fd, &length, 1) != 1) {
497			warn("CIS len read");
498			break;
499		}
500		total++;
501		tp->length = length;
502#ifdef	DEBUG
503		printf("Tuple code = 0x%x, len = %d\n", code, length);
504#endif
505		if (length == 0xFF) {
506			length = tp->length = 0;
507			code = CIS_END;
508		}
509		if (length != 0) {
510			total += length;
511			tp->data = xmalloc(length);
512			if (read_attr(fd, tp->data, length) != length) {
513				warn("CIS read");
514				break;
515			}
516		}
517
518		/*
519		 * Check the tuple, and ignore it if it isn't in the table
520		 * or the length is illegal.
521		 */
522		tinfo = get_tuple_info(code);
523		if (tinfo == 0 || (tinfo->length != 255 && tinfo->length != length)) {
524			printf("code %s ignored\n", tuple_name(code));
525			tp->code = CIS_NULL;
526		}
527		if (tl->tuples == 0)
528			tl->tuples = tp;
529		else
530			last_tp->next = tp;
531		last_tp = tp;
532	} while (code != CIS_END && total < 1024);
533	return (tl);
534}
535
536/*
537 *	return true if the offset points to a LINKTARGET tuple.
538 */
539static int
540ck_linktarget(int fd, off_t offs, int flag)
541{
542	char    blk[5];
543
544	ioctl(fd, PIOCRWFLAG, &flag);
545	lseek(fd, offs, SEEK_SET);
546	if (read_attr(fd, blk, 5) != 5)
547		return (0);
548	if (blk[0] == 0x13 &&
549	    blk[1] == 0x3 &&
550	    blk[2] == 'C' &&
551	    blk[3] == 'I' &&
552	    blk[4] == 'S')
553		return (1);
554	return (0);
555}
556
557/*
558 *	find_tuple_in_list - find a tuple within a
559 *	single tuple list.
560 */
561static struct tuple *
562find_tuple_in_list(struct tuple_list *tl, unsigned char code)
563{
564	struct tuple *tp;
565
566	for (tp = tl->tuples; tp; tp = tp->next)
567		if (tp->code == code)
568			break;
569	return (tp);
570}
571
572static int
573read_attr(int fd, char *bp, int len)
574{
575	char    blk[1024], *p = blk;
576	int     i, l;
577
578	if (len > sizeof(blk) / 2)
579		len = sizeof(blk) / 2;
580	l = i = read(fd, blk, len * 2);
581	if (i <= 0) {
582		printf("Read return %d bytes (expected %d)\n", i, len * 2);
583		return (i);
584	}
585	while (i > 0) {
586		*bp++ = *p++;
587		p++;
588		i -= 2;
589	}
590	return (l / 2);
591}
592
593/*
594 *	return table entry for code.
595 */
596static struct tuple_info *
597get_tuple_info(unsigned char code)
598{
599	struct tuple_info *tp;
600
601	for (tp = tuple_info; tp->name; tp++)
602		if (tp->code == code)
603			return (tp);
604	printf("Code %d not found\n", code);
605	return (0);
606}
607
608char *
609tuple_name(unsigned char code)
610{
611	struct tuple_info *tp;
612
613	tp = get_tuple_info(code);
614	if (tp)
615		return (tp->name);
616	return ("Unknown");
617}
618