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