xref: /netbsd-src/usr.sbin/fwctl/fwcontrol.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: fwcontrol.c,v 1.7 2008/05/02 19:59:19 xtraeme Exp $	*/
2 /*
3  * Copyright (C) 2002
4  * 	Hidetoshi Shimokawa. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *
17  *	This product includes software developed by Hidetoshi Shimokawa.
18  *
19  * 4. Neither the name of the author nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #if defined(__FreeBSD__)
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/usr.sbin/fwcontrol/fwcontrol.c,v 1.23 2006/10/26 22:33:38 imp Exp $");
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #if defined(__FreeBSD__)
49 #include <sys/eui64.h>
50 #include <dev/firewire/firewire.h>
51 #include <dev/firewire/iec13213.h>
52 #include <dev/firewire/fwphyreg.h>
53 #include <dev/firewire/iec68113.h>
54 #elif defined(__NetBSD__)
55 #include "eui64.h"
56 #include <dev/ieee1394/firewire.h>
57 #include <dev/ieee1394/iec13213.h>
58 #include <dev/ieee1394/fwphyreg.h>
59 #include <dev/ieee1394/iec68113.h>
60 #endif
61 
62 #include <netinet/in.h>
63 #include <fcntl.h>
64 #include <stdio.h>
65 #include <err.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <sysexits.h>
69 #include <unistd.h>
70 #include "fwmethods.h"
71 
72 static void sysctl_set_int(const char *, int);
73 
74 static void
75 usage(void)
76 {
77 	fprintf(stderr,
78 		"%s [-prt] [-b pri_req] [-c node] [-d node]"
79 		" [-g gap_count] [-l file]\n"
80 		"\t[-m EUI64 | hostname] [-o node] [-R filename]"
81 		" [-S filename]\n"
82 		"\t[-s node] [-u bus_num]\n"
83 		"\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
84 		"\t-c: read configuration ROM\n"
85 		"\t-d: hex dump of configuration ROM\n"
86 		"\t-g: broadcast gap_count by phy_config packet\n"
87 		"\t-l: load and parse hex dump file of configuration ROM\n"
88 		"\t-m: set fwmem target\n"
89 		"\t-o: send link-on packet to the node\n"
90 		"\t-p: dump PHY registers\n"
91 		"\t-R: receive DV or MPEG TS stream\n"
92 		"\t-r: bus reset\n"
93 		"\t-S: send DV stream\n"
94 		"\t-s: write RESET_START register on the node\n"
95 		"\t-t: read topology map\n"
96 		"\t-u: specify bus number\n", getprogname());
97 	exit(EX_USAGE);
98 }
99 
100 static void
101 fweui2eui64(const struct fw_eui64 *fweui, struct eui64 *eui)
102 {
103 	*(u_int32_t*)&(eui->octet[0]) = htonl(fweui->hi);
104 	*(u_int32_t*)&(eui->octet[4]) = htonl(fweui->lo);
105 }
106 
107 static struct fw_devlstreq *
108 get_dev(int fd)
109 {
110 	struct fw_devlstreq *data;
111 
112 	data = malloc(sizeof(*data));
113 	if (data == NULL)
114 		err(1, "malloc");
115 	if( ioctl(fd, FW_GDEVLST, data) < 0) {
116        			err(1, "ioctl");
117 	}
118 	return data;
119 }
120 
121 static int
122 str2node(int fd, const char *nodestr)
123 {
124 	struct eui64 eui, tmpeui;
125 	struct fw_devlstreq *data;
126 	char *endptr;
127 	int i, node;
128 
129 	if (nodestr == '\0')
130 		return (-1);
131 
132 	/*
133 	 * Deal with classic node specifications.
134 	 */
135 	node = strtol(nodestr, &endptr, 0);
136 	if (*endptr == '\0')
137 		goto gotnode;
138 
139 	/*
140 	 * Try to get an eui and match it against available nodes.
141 	 */
142 	if (eui64_hostton(nodestr, &eui) != 0 && eui64_aton(nodestr, &eui) != 0)
143 		return (-1);
144 
145 	data = get_dev(fd);
146 
147 	for (i = 0; i < data->info_len; i++) {
148 		fweui2eui64(&data->dev[i].eui, &tmpeui);
149 		if (memcmp(&eui, &tmpeui, sizeof(struct eui64)) == 0) {
150 			node = data->dev[i].dst;
151 			goto gotnode;
152 		}
153 	}
154 	if (i >= data->info_len)
155 		return (-1);
156 
157 gotnode:
158 	if (node < 0 || node > 63)
159 		return (-1);
160 	else
161 		return (node);
162 }
163 
164 static void
165 list_dev(int fd)
166 {
167 	struct fw_devlstreq *data;
168 	struct fw_devinfo *devinfo;
169 	struct eui64 eui;
170 	char addr[EUI64_SIZ];
171 	int i;
172 
173 	data = get_dev(fd);
174 	printf("%d devices (info_len=%d)\n", data->n, data->info_len);
175 	printf("node           EUI64          status\n");
176 	for (i = 0; i < data->info_len; i++) {
177 		devinfo = &data->dev[i];
178 		fweui2eui64(&devinfo->eui, &eui);
179 		eui64_ntoa(&eui, addr, sizeof(addr));
180 		printf("%4d  %s %6d\n",
181 			(devinfo->status || i == 0) ? devinfo->dst : -1,
182 			addr,
183 			devinfo->status
184 		);
185 	}
186 	free((void *)data);
187 }
188 
189 static u_int32_t
190 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int readmode, u_int32_t data)
191 {
192         struct fw_asyreq *asyreq;
193 	u_int32_t *qld, res;
194 
195         asyreq = malloc(sizeof(*asyreq));
196 	if (asyreq == NULL)
197 		err(1, "malloc");
198 	asyreq->req.len = 16;
199 #if 0
200 	asyreq->req.type = FWASREQNODE;
201 	asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
202 #else
203 	asyreq->req.type = FWASREQEUI;
204 	asyreq->req.dst.eui = eui;
205 #endif
206 	asyreq->pkt.mode.rreqq.tlrt = 0;
207 	if (readmode)
208 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
209 	else
210 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
211 
212 	asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
213 	asyreq->pkt.mode.rreqq.dest_lo = addr_lo;
214 
215 	qld = (u_int32_t *)&asyreq->pkt;
216 	if (!readmode)
217 		asyreq->pkt.mode.wreqq.data = data;
218 
219 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
220        		err(1, "ioctl");
221 	}
222 	res = qld[3];
223 	free(asyreq);
224 	if (readmode)
225 		return ntohl(res);
226 	else
227 		return 0;
228 }
229 
230 static void
231 send_phy_config(int fd, int root_node, int gap_count)
232 {
233         struct fw_asyreq *asyreq;
234 
235 	asyreq = malloc(sizeof(*asyreq));
236 	if (asyreq == NULL)
237 		err(1, "malloc");
238 	asyreq->req.len = 12;
239 	asyreq->req.type = FWASREQNODE;
240 	asyreq->pkt.mode.ld[0] = 0;
241 	asyreq->pkt.mode.ld[1] = 0;
242 	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
243 	if (root_node >= 0)
244 		asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
245 	if (gap_count >= 0)
246 		asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
247 	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
248 
249 	printf("send phy_config root_node=%d gap_count=%d\n",
250 						root_node, gap_count);
251 
252 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
253        		err(1, "ioctl");
254 	free(asyreq);
255 }
256 
257 static void
258 send_link_on(int fd, int node)
259 {
260         struct fw_asyreq *asyreq;
261 
262 	asyreq = malloc(sizeof(*asyreq));
263 	if (asyreq == NULL)
264 		err(1, "malloc");
265 	asyreq->req.len = 12;
266 	asyreq->req.type = FWASREQNODE;
267 	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
268 	asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
269 	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
270 
271 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
272        		err(1, "ioctl");
273 	free(asyreq);
274 }
275 
276 static void
277 reset_start(int fd, int node)
278 {
279         struct fw_asyreq *asyreq;
280 
281 	asyreq = malloc(sizeof(*asyreq));
282 	if (asyreq == NULL)
283 		err(1, "malloc");
284 	asyreq->req.len = 16;
285 	asyreq->req.type = FWASREQNODE;
286 	asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
287 	asyreq->pkt.mode.wreqq.tlrt = 0;
288 	asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;
289 
290 	asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
291 	asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
292 
293 	asyreq->pkt.mode.wreqq.data = htonl(0x1);
294 
295 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
296        		err(1, "ioctl");
297 	free(asyreq);
298 }
299 
300 static void
301 set_pri_req(int fd, u_int32_t pri_req)
302 {
303 	struct fw_devlstreq *data;
304 	struct fw_devinfo *devinfo;
305 	struct eui64 eui;
306 	char addr[EUI64_SIZ];
307 	u_int32_t max, reg, old;
308 	int i;
309 
310 	data = get_dev(fd);
311 #define BUGET_REG 0xf0000218
312 	for (i = 0; i < data->info_len; i++) {
313 		devinfo = &data->dev[i];
314 		if (!devinfo->status)
315 			continue;
316 		reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
317 		fweui2eui64(&devinfo->eui, &eui);
318 		eui64_ntoa(&eui, addr, sizeof(addr));
319 		printf("%d %s, %08x",
320 			devinfo->dst, addr, reg);
321 		if (reg > 0) {
322 			old = (reg & 0x3f);
323 			max = (reg & 0x3f00) >> 8;
324 			if (pri_req > max)
325 				pri_req =  max;
326 			printf(" 0x%x -> 0x%x\n", old, pri_req);
327 			read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
328 		} else {
329 			printf("\n");
330 		}
331 	}
332 	free((void *)data);
333 }
334 
335 static void
336 parse_bus_info_block(u_int32_t *p)
337 {
338 	char addr[EUI64_SIZ];
339 	struct bus_info *bi;
340 	struct eui64 eui;
341 
342 	bi = (struct bus_info *)p;
343 	fweui2eui64(&bi->eui64, &eui);
344 	eui64_ntoa(&eui, addr, sizeof(addr));
345 	printf("bus_name: 0x%04x\n"
346 		"irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
347 		"cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
348 		"generation:%d link_spd:%d\n"
349 		"EUI64: %s\n",
350 		bi->bus_name,
351 		bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc,
352 		bi->cyc_clk_acc, bi->max_rec, bi->max_rom,
353 		bi->generation, bi->link_spd,
354 		addr);
355 }
356 
357 static int
358 get_crom(int fd, int node, void *crom_buf, int len)
359 {
360 	struct fw_crom_buf buf;
361 	int i, error;
362 	struct fw_devlstreq *data;
363 
364 	data = get_dev(fd);
365 
366 	for (i = 0; i < data->info_len; i++) {
367 		if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
368 			break;
369 	}
370 	if (i == data->info_len)
371 		errx(1, "no such node %d.", node);
372 	else
373 		buf.eui = data->dev[i].eui;
374 	free((void *)data);
375 
376 	buf.len = len;
377 	buf.ptr = crom_buf;
378 	bzero(crom_buf, len);
379 	if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
380        		err(1, "ioctl");
381 	}
382 
383 	return error;
384 }
385 
386 static void
387 show_crom(u_int32_t *crom_buf)
388 {
389 	int i;
390 	struct crom_context cc;
391 	const char *desc;
392 	char info[256];
393 	static const char *key_types = "ICLD";
394 	struct csrreg *reg;
395 	struct csrdirectory *dir;
396 	struct csrhdr *hdr;
397 	u_int16_t crc;
398 
399 	printf("first quad: 0x%08x ", *crom_buf);
400 	if (crom_buf[0] == 0) {
401 		printf("(Invalid Configuration ROM)\n");
402 		return;
403 	}
404 	hdr = (struct csrhdr *)crom_buf;
405 	if (hdr->info_len == 1) {
406 		/* minimum ROM */
407 		reg = (struct csrreg *)hdr;
408 		printf("verndor ID: 0x%06x\n",  reg->val);
409 		return;
410 	}
411 	printf("info_len=%d crc_len=%d crc=0x%04x",
412 		hdr->info_len, hdr->crc_len, hdr->crc);
413 	crc = crom_crc(crom_buf+1, hdr->crc_len);
414 	if (crc == hdr->crc)
415 		printf("(OK)\n");
416 	else
417 		printf("(NG)\n");
418 	parse_bus_info_block(crom_buf+1);
419 
420 	crom_init_context(&cc, crom_buf);
421 	dir = cc.stack[0].dir;
422 	if (!dir) {
423 		printf("no root directory - giving up\n");
424 		return;
425 	}
426 	printf("root_directory: len=0x%04x(%d) crc=0x%04x",
427 			dir->crc_len, dir->crc_len, dir->crc);
428 	crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
429 	if (crc == dir->crc)
430 		printf("(OK)\n");
431 	else
432 		printf("(NG)\n");
433 	if (dir->crc_len < 1)
434 		return;
435 	while (cc.depth >= 0) {
436 		desc = crom_desc(&cc, info, sizeof(info));
437 		reg = crom_get(&cc);
438 		for (i = 0; i < cc.depth; i++)
439 			printf("\t");
440 		printf("%02x(%c:%02x) %06x %s: %s\n",
441 			reg->key,
442 			key_types[(reg->key & CSRTYPE_MASK)>>6],
443 			reg->key & CSRKEY_MASK, reg->val,
444 			desc, info);
445 		crom_next(&cc);
446 	}
447 }
448 
449 #define DUMP_FORMAT	"%08x %08x %08x %08x %08x %08x %08x %08x\n"
450 
451 static void
452 dump_crom(u_int32_t *p)
453 {
454 	int len=1024, i;
455 
456 	for (i = 0; i < len/(4*8); i ++) {
457 		printf(DUMP_FORMAT,
458 			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
459 		p += 8;
460 	}
461 }
462 
463 static void
464 load_crom(char *filename, u_int32_t *p)
465 {
466 	FILE *file;
467 	int len=1024, i;
468 
469 	if ((file = fopen(filename, "r")) == NULL)
470 		err(1, "load_crom");
471 	for (i = 0; i < len/(4*8); i ++) {
472 		fscanf(file, DUMP_FORMAT,
473 			p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
474 		p += 8;
475 	}
476 	(void)fclose(file);
477 }
478 
479 static void
480 show_topology_map(int fd)
481 {
482 	struct fw_topology_map *tmap;
483 	union fw_self_id sid;
484 	int i;
485 	static const char *port_status[] = {" ", "-", "P", "C"};
486 	static const char *pwr_class[] = {" 0W", "15W", "30W", "45W",
487 					"-1W", "-2W", "-5W", "-9W"};
488 	static const char *speed[] = {"S100", "S200", "S400", "S800"};
489 	tmap = malloc(sizeof(*tmap));
490 	if (tmap == NULL)
491 		err(1, "malloc");
492 	if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
493        		err(1, "ioctl");
494 	}
495 	printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
496 		tmap->crc_len, tmap->generation,
497 		tmap->node_count, tmap->self_id_count);
498 	printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
499 		" ini more\n");
500 	for (i = 0; i < tmap->crc_len - 2; i++) {
501 		sid = tmap->self_id[i];
502 		if (sid.p0.sequel) {
503 			printf("%02d sequel packet\n", sid.p0.phy_id);
504 			continue;
505 		}
506 		printf("%02d   %2d      %2d  %4s     %d    %d   %3s"
507 				"     %s     %s     %s   %d    %d\n",
508 			sid.p0.phy_id,
509 			sid.p0.link_active,
510 			sid.p0.gap_count,
511 			speed[sid.p0.phy_speed],
512 			sid.p0.phy_delay,
513 			sid.p0.contender,
514 			pwr_class[sid.p0.power_class],
515 			port_status[sid.p0.port0],
516 			port_status[sid.p0.port1],
517 			port_status[sid.p0.port2],
518 			sid.p0.initiated_reset,
519 			sid.p0.more_packets
520 		);
521 	}
522 	free(tmap);
523 }
524 
525 static void
526 read_phy_registers(int fd, u_int8_t *buf, int offset, int len)
527 {
528 	struct fw_reg_req_t reg;
529 	int i;
530 
531 	for (i = 0; i < len; i++) {
532 		reg.addr = offset + i;
533 		if (ioctl(fd, FWOHCI_RDPHYREG, &reg) < 0)
534        			err(1, "ioctl");
535 		buf[i] = (u_int8_t) reg.data;
536 		printf("0x%02x ",  reg.data);
537 	}
538 	printf("\n");
539 }
540 
541 static void
542 read_phy_page(int fd, u_int8_t *buf, int page, int port)
543 {
544 	struct fw_reg_req_t reg;
545 
546 	reg.addr = 0x7;
547 	reg.data = ((page & 7) << 5) | (port & 0xf);
548 	if (ioctl(fd, FWOHCI_WRPHYREG, &reg) < 0)
549        		err(1, "ioctl");
550 	read_phy_registers(fd, buf, 8, 8);
551 }
552 
553 static void
554 dump_phy_registers(int fd)
555 {
556 	struct phyreg_base b;
557 	struct phyreg_page0 p;
558 	struct phyreg_page1 v;
559 	int i;
560 
561 	printf("=== base register ===\n");
562 	read_phy_registers(fd, (u_int8_t *)&b, 0, 8);
563 	printf(
564 	    "Physical_ID:%d  R:%d  CPS:%d\n"
565 	    "RHB:%d  IBR:%d  Gap_Count:%d\n"
566 	    "Extended:%d Num_Ports:%d\n"
567 	    "PHY_Speed:%d Delay:%d\n"
568 	    "LCtrl:%d C:%d Jitter:%d Pwr_Class:%d\n"
569 	    "WDIE:%d ISBR:%d CTOI:%d CPSI:%d STOI:%d PEI:%d EAA:%d EMC:%d\n"
570 	    "Max_Legacy_SPD:%d BLINK:%d Bridge:%d\n"
571 	    "Page_Select:%d Port_Select%d\n",
572 	    b.phy_id, b.r, b.cps,
573 	    b.rhb, b.ibr, b.gap_count,
574 	    b.extended, b.num_ports,
575 	    b.phy_speed, b.delay,
576 	    b.lctrl, b.c, b.jitter, b.pwr_class,
577 	    b.wdie, b.isbr, b.ctoi, b.cpsi, b.stoi, b.pei, b.eaa, b.emc,
578 	    b.legacy_spd, b.blink, b.bridge,
579 	    b.page_select, b.port_select
580 	);
581 
582 	for (i = 0; i < b.num_ports; i ++) {
583 		printf("\n=== page 0 port %d ===\n", i);
584 		read_phy_page(fd, (u_int8_t *)&p, 0, i);
585 		printf(
586 		    "Astat:%d BStat:%d Ch:%d Con:%d RXOK:%d Dis:%d\n"
587 		    "Negotiated_speed:%d PIE:%d Fault:%d Stanby_fault:%d Disscrm:%d B_Only:%d\n"
588 		    "DC_connected:%d Max_port_speed:%d LPP:%d Cable_speed:%d\n"
589 		    "Connection_unreliable:%d Beta_mode:%d\n"
590 		    "Port_error:0x%x\n"
591 		    "Loop_disable:%d In_standby:%d Hard_disable:%d\n",
592 		    p.astat, p.bstat, p.ch, p.con, p.rxok, p.dis,
593 		    p.negotiated_speed, p.pie, p.fault, p.stanby_fault, p.disscrm, p.b_only,
594 		    p.dc_connected, p.max_port_speed, p.lpp, p.cable_speed,
595 		    p.connection_unreliable, p.beta_mode,
596 		    p.port_error,
597 		    p.loop_disable, p.in_standby, p.hard_disable
598 		);
599 	}
600 	printf("\n=== page 1 ===\n");
601 	read_phy_page(fd, (u_int8_t *)&v, 1, 0);
602 	printf(
603 	    "Compliance:%d\n"
604 	    "Vendor_ID:0x%06x\n"
605 	    "Product_ID:0x%06x\n",
606 	    v.compliance,
607 	    (v.vendor_id[0] << 16) | (v.vendor_id[1] << 8) | v.vendor_id[2],
608 	    (v.product_id[0] << 16) | (v.product_id[1] << 8) | v.product_id[2]
609 	);
610 }
611 
612 static void
613 open_dev(int *fd, char *devbase)
614 {
615 	char name[256];
616 	int i;
617 
618 	if (*fd < 0) {
619 		for (i = 0; i < 4; i++) {
620 			snprintf(name, sizeof(name), "%s.%d", devbase, i);
621 			if ((*fd = open(name, O_RDWR)) >= 0)
622 				break;
623 		}
624 		if (*fd < 0)
625 			err(1, "open");
626 
627 	}
628 }
629 
630 static void
631 sysctl_set_int(const char *name, int val)
632 {
633 	if (sysctlbyname(name, NULL, NULL, &val, sizeof(int)) < 0)
634 		err(1, "sysctl %s failed.", name);
635 }
636 
637 static fwmethod *
638 detect_recv_fn(int fd, char ich)
639 {
640 	char *buf;
641 	struct fw_isochreq isoreq;
642 	struct fw_isobufreq bufreq;
643 	int len;
644 	u_int32_t *ptr;
645 	struct ciphdr *ciph;
646 	fwmethod *retfn;
647 
648 	bufreq.rx.nchunk = 8;
649 	bufreq.rx.npacket = 16;
650 	bufreq.rx.psize = 1024;
651 	bufreq.tx.nchunk = 0;
652 	bufreq.tx.npacket = 0;
653 	bufreq.tx.psize = 0;
654 
655 	if (ioctl(fd, FW_SSTBUF, &bufreq) < 0)
656 		err(1, "ioctl FW_SSTBUF");
657 
658 	isoreq.ch = ich & 0x3f;
659 	isoreq.tag = (ich >> 6) & 3;
660 
661 	if (ioctl(fd, FW_SRSTREAM, &isoreq) < 0)
662 		err(1, "ioctl FW_SRSTREAM");
663 
664 	buf = (char *)malloc(1024*16);
665 	len = read(fd, buf, 1024*16);
666 	ptr = (u_int32_t *) buf;
667 	ciph = (struct ciphdr *)(ptr + 1);
668 
669 	switch(ciph->fmt) {
670 		case CIP_FMT_DVCR:
671 			fprintf(stderr, "Detected DV format on input.\n");
672 			retfn = dvrecv;
673 			break;
674 		case CIP_FMT_MPEG:
675 			fprintf(stderr, "Detected MPEG TS format on input.\n");
676 			retfn = mpegtsrecv;
677 			break;
678 		default:
679 			errx(1, "Unsupported format for receiving: fmt=0x%x",
680 			    ciph->fmt);
681 	}
682 	free(buf);
683 	return retfn;
684 }
685 
686 int
687 main(int argc, char **argv)
688 {
689 	u_int32_t crom_buf[1024/4];
690 	char devbase[1024] = "/dev/fw0";
691 	int fd, ch, len=1024;
692 	long tmp;
693 	struct fw_eui64 eui;
694 	struct eui64 target;
695 	fwmethod *recvfn = NULL;
696 
697 	fd = -1;
698 
699 	if (argc < 2) {
700 		open_dev(&fd, devbase);
701 		list_dev(fd);
702 	}
703 
704 	while ((ch = getopt(argc, argv, "M:g:m:o:s:b:prtc:d:l:u:R:S:")) != -1)
705 		switch(ch) {
706 		case 'b':
707 			tmp = strtol(optarg, NULL, 0);
708 			if (tmp < 0 || tmp > (long)0xffffffff)
709 				errx(EX_USAGE, "invalid number: %s", optarg);
710 			open_dev(&fd, devbase);
711 			set_pri_req(fd, tmp);
712 			break;
713 		case 'c':
714 			open_dev(&fd, devbase);
715 			tmp = str2node(fd, optarg);
716 			get_crom(fd, tmp, crom_buf, len);
717 			show_crom(crom_buf);
718 			break;
719 		case 'd':
720 			open_dev(&fd, devbase);
721 			tmp = str2node(fd, optarg);
722 			get_crom(fd, tmp, crom_buf, len);
723 			dump_crom(crom_buf);
724 			break;
725 		case 'g':
726 			tmp = strtol(optarg, NULL, 0);
727 			open_dev(&fd, devbase);
728 			send_phy_config(fd, -1, tmp);
729 			break;
730 		case 'l':
731 			load_crom(optarg, crom_buf);
732 			show_crom(crom_buf);
733 			break;
734 		case 'm':
735 		       if (eui64_hostton(optarg, &target) != 0 &&
736 			   eui64_aton(optarg, &target) != 0)
737 				errx(EX_USAGE, "invalid target: %s", optarg);
738 			eui.hi = ntohl(*(u_int32_t*)&(target.octet[0]));
739 			eui.lo = ntohl(*(u_int32_t*)&(target.octet[4]));
740 			sysctl_set_int("hw.fwmem.eui64_hi", eui.hi);
741 			sysctl_set_int("hw.fwmem.eui64_lo", eui.lo);
742 			break;
743 		case 'o':
744 			open_dev(&fd, devbase);
745 			tmp = str2node(fd, optarg);
746 			send_link_on(fd, tmp);
747 			break;
748 		case 'p':
749 			open_dev(&fd, devbase);
750 			dump_phy_registers(fd);
751 			break;
752 		case 'r':
753 			open_dev(&fd, devbase);
754 			if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
755                        		err(1, "ioctl");
756 			break;
757 		case 's':
758 			open_dev(&fd, devbase);
759 			tmp = str2node(fd, optarg);
760 			reset_start(fd, tmp);
761 			break;
762 		case 't':
763 			open_dev(&fd, devbase);
764 			show_topology_map(fd);
765 			break;
766 		case 'u':
767 			tmp = strtol(optarg, NULL, 0);
768 			snprintf(devbase, sizeof(devbase), "/dev/fw%ld",  tmp);
769 			if (fd > 0) {
770 				close(fd);
771 				fd = -1;
772 			}
773 			if (argc == optind) {
774 				open_dev(&fd, devbase);
775 				list_dev(fd);
776 			}
777 			break;
778 #define TAG	(1<<6)
779 #define CHANNEL	63
780 		case 'M':
781 			switch (optarg[0]) {
782 			case 'm':
783 				recvfn = mpegtsrecv;
784 				break;
785 			case 'd':
786 				recvfn = dvrecv;
787 				break;
788 			default:
789 				errx(EX_USAGE, "unrecognized method: %s",
790 				    optarg);
791 			}
792 			break;
793 		case 'R':
794 			open_dev(&fd, devbase);
795 			if (recvfn == NULL) /* guess... */
796 				recvfn = detect_recv_fn(fd, TAG | CHANNEL);
797 			close(fd);
798 			fd = -1;
799 			open_dev(&fd, devbase);
800 			(*recvfn)(fd, optarg, TAG | CHANNEL, -1);
801 			break;
802 		case 'S':
803 			open_dev(&fd, devbase);
804 			dvsend(fd, optarg, TAG | CHANNEL, -1);
805 			break;
806 		default:
807 			usage();
808 		}
809 	return 0;
810 }
811