Home | History | Annotate | Line # | Download | only in fwctl
fwcontrol.c revision 1.6
      1 /*	$NetBSD: fwcontrol.c,v 1.6 2007/11/06 17:02:15 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: 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 	char *desc, info[256];
    392 	static const char *key_types = "ICLD";
    393 	struct csrreg *reg;
    394 	struct csrdirectory *dir;
    395 	struct csrhdr *hdr;
    396 	u_int16_t crc;
    397 
    398 	printf("first quad: 0x%08x ", *crom_buf);
    399 	if (crom_buf[0] == 0) {
    400 		printf("(Invalid Configuration ROM)\n");
    401 		return;
    402 	}
    403 	hdr = (struct csrhdr *)crom_buf;
    404 	if (hdr->info_len == 1) {
    405 		/* minimum ROM */
    406 		reg = (struct csrreg *)hdr;
    407 		printf("verndor ID: 0x%06x\n",  reg->val);
    408 		return;
    409 	}
    410 	printf("info_len=%d crc_len=%d crc=0x%04x",
    411 		hdr->info_len, hdr->crc_len, hdr->crc);
    412 	crc = crom_crc(crom_buf+1, hdr->crc_len);
    413 	if (crc == hdr->crc)
    414 		printf("(OK)\n");
    415 	else
    416 		printf("(NG)\n");
    417 	parse_bus_info_block(crom_buf+1);
    418 
    419 	crom_init_context(&cc, crom_buf);
    420 	dir = cc.stack[0].dir;
    421 	if (!dir) {
    422 		printf("no root directory - giving up\n");
    423 		return;
    424 	}
    425 	printf("root_directory: len=0x%04x(%d) crc=0x%04x",
    426 			dir->crc_len, dir->crc_len, dir->crc);
    427 	crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
    428 	if (crc == dir->crc)
    429 		printf("(OK)\n");
    430 	else
    431 		printf("(NG)\n");
    432 	if (dir->crc_len < 1)
    433 		return;
    434 	while (cc.depth >= 0) {
    435 		desc = crom_desc(&cc, info, sizeof(info));
    436 		reg = crom_get(&cc);
    437 		for (i = 0; i < cc.depth; i++)
    438 			printf("\t");
    439 		printf("%02x(%c:%02x) %06x %s: %s\n",
    440 			reg->key,
    441 			key_types[(reg->key & CSRTYPE_MASK)>>6],
    442 			reg->key & CSRKEY_MASK, reg->val,
    443 			desc, info);
    444 		crom_next(&cc);
    445 	}
    446 }
    447 
    448 #define DUMP_FORMAT	"%08x %08x %08x %08x %08x %08x %08x %08x\n"
    449 
    450 static void
    451 dump_crom(u_int32_t *p)
    452 {
    453 	int len=1024, i;
    454 
    455 	for (i = 0; i < len/(4*8); i ++) {
    456 		printf(DUMP_FORMAT,
    457 			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
    458 		p += 8;
    459 	}
    460 }
    461 
    462 static void
    463 load_crom(char *filename, u_int32_t *p)
    464 {
    465 	FILE *file;
    466 	int len=1024, i;
    467 
    468 	if ((file = fopen(filename, "r")) == NULL)
    469 		err(1, "load_crom");
    470 	for (i = 0; i < len/(4*8); i ++) {
    471 		fscanf(file, DUMP_FORMAT,
    472 			p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
    473 		p += 8;
    474 	}
    475 	(void)fclose(file);
    476 }
    477 
    478 static void
    479 show_topology_map(int fd)
    480 {
    481 	struct fw_topology_map *tmap;
    482 	union fw_self_id sid;
    483 	int i;
    484 	static const char *port_status[] = {" ", "-", "P", "C"};
    485 	static const char *pwr_class[] = {" 0W", "15W", "30W", "45W",
    486 					"-1W", "-2W", "-5W", "-9W"};
    487 	static const char *speed[] = {"S100", "S200", "S400", "S800"};
    488 	tmap = malloc(sizeof(*tmap));
    489 	if (tmap == NULL)
    490 		err(1, "malloc");
    491 	if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
    492        		err(1, "ioctl");
    493 	}
    494 	printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
    495 		tmap->crc_len, tmap->generation,
    496 		tmap->node_count, tmap->self_id_count);
    497 	printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
    498 		" ini more\n");
    499 	for (i = 0; i < tmap->crc_len - 2; i++) {
    500 		sid = tmap->self_id[i];
    501 		if (sid.p0.sequel) {
    502 			printf("%02d sequel packet\n", sid.p0.phy_id);
    503 			continue;
    504 		}
    505 		printf("%02d   %2d      %2d  %4s     %d    %d   %3s"
    506 				"     %s     %s     %s   %d    %d\n",
    507 			sid.p0.phy_id,
    508 			sid.p0.link_active,
    509 			sid.p0.gap_count,
    510 			speed[sid.p0.phy_speed],
    511 			sid.p0.phy_delay,
    512 			sid.p0.contender,
    513 			pwr_class[sid.p0.power_class],
    514 			port_status[sid.p0.port0],
    515 			port_status[sid.p0.port1],
    516 			port_status[sid.p0.port2],
    517 			sid.p0.initiated_reset,
    518 			sid.p0.more_packets
    519 		);
    520 	}
    521 	free(tmap);
    522 }
    523 
    524 static void
    525 read_phy_registers(int fd, u_int8_t *buf, int offset, int len)
    526 {
    527 	struct fw_reg_req_t reg;
    528 	int i;
    529 
    530 	for (i = 0; i < len; i++) {
    531 		reg.addr = offset + i;
    532 		if (ioctl(fd, FWOHCI_RDPHYREG, &reg) < 0)
    533        			err(1, "ioctl");
    534 		buf[i] = (u_int8_t) reg.data;
    535 		printf("0x%02x ",  reg.data);
    536 	}
    537 	printf("\n");
    538 }
    539 
    540 static void
    541 read_phy_page(int fd, u_int8_t *buf, int page, int port)
    542 {
    543 	struct fw_reg_req_t reg;
    544 
    545 	reg.addr = 0x7;
    546 	reg.data = ((page & 7) << 5) | (port & 0xf);
    547 	if (ioctl(fd, FWOHCI_WRPHYREG, &reg) < 0)
    548        		err(1, "ioctl");
    549 	read_phy_registers(fd, buf, 8, 8);
    550 }
    551 
    552 static void
    553 dump_phy_registers(int fd)
    554 {
    555 	struct phyreg_base b;
    556 	struct phyreg_page0 p;
    557 	struct phyreg_page1 v;
    558 	int i;
    559 
    560 	printf("=== base register ===\n");
    561 	read_phy_registers(fd, (u_int8_t *)&b, 0, 8);
    562 	printf(
    563 	    "Physical_ID:%d  R:%d  CPS:%d\n"
    564 	    "RHB:%d  IBR:%d  Gap_Count:%d\n"
    565 	    "Extended:%d Num_Ports:%d\n"
    566 	    "PHY_Speed:%d Delay:%d\n"
    567 	    "LCtrl:%d C:%d Jitter:%d Pwr_Class:%d\n"
    568 	    "WDIE:%d ISBR:%d CTOI:%d CPSI:%d STOI:%d PEI:%d EAA:%d EMC:%d\n"
    569 	    "Max_Legacy_SPD:%d BLINK:%d Bridge:%d\n"
    570 	    "Page_Select:%d Port_Select%d\n",
    571 	    b.phy_id, b.r, b.cps,
    572 	    b.rhb, b.ibr, b.gap_count,
    573 	    b.extended, b.num_ports,
    574 	    b.phy_speed, b.delay,
    575 	    b.lctrl, b.c, b.jitter, b.pwr_class,
    576 	    b.wdie, b.isbr, b.ctoi, b.cpsi, b.stoi, b.pei, b.eaa, b.emc,
    577 	    b.legacy_spd, b.blink, b.bridge,
    578 	    b.page_select, b.port_select
    579 	);
    580 
    581 	for (i = 0; i < b.num_ports; i ++) {
    582 		printf("\n=== page 0 port %d ===\n", i);
    583 		read_phy_page(fd, (u_int8_t *)&p, 0, i);
    584 		printf(
    585 		    "Astat:%d BStat:%d Ch:%d Con:%d RXOK:%d Dis:%d\n"
    586 		    "Negotiated_speed:%d PIE:%d Fault:%d Stanby_fault:%d Disscrm:%d B_Only:%d\n"
    587 		    "DC_connected:%d Max_port_speed:%d LPP:%d Cable_speed:%d\n"
    588 		    "Connection_unreliable:%d Beta_mode:%d\n"
    589 		    "Port_error:0x%x\n"
    590 		    "Loop_disable:%d In_standby:%d Hard_disable:%d\n",
    591 		    p.astat, p.bstat, p.ch, p.con, p.rxok, p.dis,
    592 		    p.negotiated_speed, p.pie, p.fault, p.stanby_fault, p.disscrm, p.b_only,
    593 		    p.dc_connected, p.max_port_speed, p.lpp, p.cable_speed,
    594 		    p.connection_unreliable, p.beta_mode,
    595 		    p.port_error,
    596 		    p.loop_disable, p.in_standby, p.hard_disable
    597 		);
    598 	}
    599 	printf("\n=== page 1 ===\n");
    600 	read_phy_page(fd, (u_int8_t *)&v, 1, 0);
    601 	printf(
    602 	    "Compliance:%d\n"
    603 	    "Vendor_ID:0x%06x\n"
    604 	    "Product_ID:0x%06x\n",
    605 	    v.compliance,
    606 	    (v.vendor_id[0] << 16) | (v.vendor_id[1] << 8) | v.vendor_id[2],
    607 	    (v.product_id[0] << 16) | (v.product_id[1] << 8) | v.product_id[2]
    608 	);
    609 }
    610 
    611 static void
    612 open_dev(int *fd, char *devbase)
    613 {
    614 	char name[256];
    615 	int i;
    616 
    617 	if (*fd < 0) {
    618 		for (i = 0; i < 4; i++) {
    619 			snprintf(name, sizeof(name), "%s.%d", devbase, i);
    620 			if ((*fd = open(name, O_RDWR)) >= 0)
    621 				break;
    622 		}
    623 		if (*fd < 0)
    624 			err(1, "open");
    625 
    626 	}
    627 }
    628 
    629 static void
    630 sysctl_set_int(const char *name, int val)
    631 {
    632 	if (sysctlbyname(name, NULL, NULL, &val, sizeof(int)) < 0)
    633 		err(1, "sysctl %s failed.", name);
    634 }
    635 
    636 static fwmethod *
    637 detect_recv_fn(int fd, char ich)
    638 {
    639 	char *buf;
    640 	struct fw_isochreq isoreq;
    641 	struct fw_isobufreq bufreq;
    642 	int len;
    643 	u_int32_t *ptr;
    644 	struct ciphdr *ciph;
    645 	fwmethod *retfn;
    646 
    647 	bufreq.rx.nchunk = 8;
    648 	bufreq.rx.npacket = 16;
    649 	bufreq.rx.psize = 1024;
    650 	bufreq.tx.nchunk = 0;
    651 	bufreq.tx.npacket = 0;
    652 	bufreq.tx.psize = 0;
    653 
    654 	if (ioctl(fd, FW_SSTBUF, &bufreq) < 0)
    655 		err(1, "ioctl FW_SSTBUF");
    656 
    657 	isoreq.ch = ich & 0x3f;
    658 	isoreq.tag = (ich >> 6) & 3;
    659 
    660 	if (ioctl(fd, FW_SRSTREAM, &isoreq) < 0)
    661 		err(1, "ioctl FW_SRSTREAM");
    662 
    663 	buf = (char *)malloc(1024*16);
    664 	len = read(fd, buf, 1024*16);
    665 	ptr = (u_int32_t *) buf;
    666 	ciph = (struct ciphdr *)(ptr + 1);
    667 
    668 	switch(ciph->fmt) {
    669 		case CIP_FMT_DVCR:
    670 			fprintf(stderr, "Detected DV format on input.\n");
    671 			retfn = dvrecv;
    672 			break;
    673 		case CIP_FMT_MPEG:
    674 			fprintf(stderr, "Detected MPEG TS format on input.\n");
    675 			retfn = mpegtsrecv;
    676 			break;
    677 		default:
    678 			errx(1, "Unsupported format for receiving: fmt=0x%x",
    679 			    ciph->fmt);
    680 	}
    681 	free(buf);
    682 	return retfn;
    683 }
    684 
    685 int
    686 main(int argc, char **argv)
    687 {
    688 	u_int32_t crom_buf[1024/4];
    689 	char devbase[1024] = "/dev/fw0";
    690 	int fd, ch, len=1024;
    691 	long tmp;
    692 	struct fw_eui64 eui;
    693 	struct eui64 target;
    694 	fwmethod *recvfn = NULL;
    695 
    696 	fd = -1;
    697 
    698 	if (argc < 2) {
    699 		open_dev(&fd, devbase);
    700 		list_dev(fd);
    701 	}
    702 
    703 	while ((ch = getopt(argc, argv, "M:g:m:o:s:b:prtc:d:l:u:R:S:")) != -1)
    704 		switch(ch) {
    705 		case 'b':
    706 			tmp = strtol(optarg, NULL, 0);
    707 			if (tmp < 0 || tmp > (long)0xffffffff)
    708 				errx(EX_USAGE, "invalid number: %s", optarg);
    709 			open_dev(&fd, devbase);
    710 			set_pri_req(fd, tmp);
    711 			break;
    712 		case 'c':
    713 			open_dev(&fd, devbase);
    714 			tmp = str2node(fd, optarg);
    715 			get_crom(fd, tmp, crom_buf, len);
    716 			show_crom(crom_buf);
    717 			break;
    718 		case 'd':
    719 			open_dev(&fd, devbase);
    720 			tmp = str2node(fd, optarg);
    721 			get_crom(fd, tmp, crom_buf, len);
    722 			dump_crom(crom_buf);
    723 			break;
    724 		case 'g':
    725 			tmp = strtol(optarg, NULL, 0);
    726 			open_dev(&fd, devbase);
    727 			send_phy_config(fd, -1, tmp);
    728 			break;
    729 		case 'l':
    730 			load_crom(optarg, crom_buf);
    731 			show_crom(crom_buf);
    732 			break;
    733 		case 'm':
    734 		       if (eui64_hostton(optarg, &target) != 0 &&
    735 			   eui64_aton(optarg, &target) != 0)
    736 				errx(EX_USAGE, "invalid target: %s", optarg);
    737 			eui.hi = ntohl(*(u_int32_t*)&(target.octet[0]));
    738 			eui.lo = ntohl(*(u_int32_t*)&(target.octet[4]));
    739 			sysctl_set_int("hw.fwmem.eui64_hi", eui.hi);
    740 			sysctl_set_int("hw.fwmem.eui64_lo", eui.lo);
    741 			break;
    742 		case 'o':
    743 			open_dev(&fd, devbase);
    744 			tmp = str2node(fd, optarg);
    745 			send_link_on(fd, tmp);
    746 			break;
    747 		case 'p':
    748 			open_dev(&fd, devbase);
    749 			dump_phy_registers(fd);
    750 			break;
    751 		case 'r':
    752 			open_dev(&fd, devbase);
    753 			if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
    754                        		err(1, "ioctl");
    755 			break;
    756 		case 's':
    757 			open_dev(&fd, devbase);
    758 			tmp = str2node(fd, optarg);
    759 			reset_start(fd, tmp);
    760 			break;
    761 		case 't':
    762 			open_dev(&fd, devbase);
    763 			show_topology_map(fd);
    764 			break;
    765 		case 'u':
    766 			tmp = strtol(optarg, NULL, 0);
    767 			snprintf(devbase, sizeof(devbase), "/dev/fw%ld",  tmp);
    768 			if (fd > 0) {
    769 				close(fd);
    770 				fd = -1;
    771 			}
    772 			if (argc == optind) {
    773 				open_dev(&fd, devbase);
    774 				list_dev(fd);
    775 			}
    776 			break;
    777 #define TAG	(1<<6)
    778 #define CHANNEL	63
    779 		case 'M':
    780 			switch (optarg[0]) {
    781 			case 'm':
    782 				recvfn = mpegtsrecv;
    783 				break;
    784 			case 'd':
    785 				recvfn = dvrecv;
    786 				break;
    787 			default:
    788 				errx(EX_USAGE, "unrecognized method: %s",
    789 				    optarg);
    790 			}
    791 			break;
    792 		case 'R':
    793 			open_dev(&fd, devbase);
    794 			if (recvfn == NULL) /* guess... */
    795 				recvfn = detect_recv_fn(fd, TAG | CHANNEL);
    796 			close(fd);
    797 			fd = -1;
    798 			open_dev(&fd, devbase);
    799 			(*recvfn)(fd, optarg, TAG | CHANNEL, -1);
    800 			break;
    801 		case 'S':
    802 			open_dev(&fd, devbase);
    803 			dvsend(fd, optarg, TAG | CHANNEL, -1);
    804 			break;
    805 		default:
    806 			usage();
    807 		}
    808 	return 0;
    809 }
    810