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