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