Home | History | Annotate | Line # | Download | only in fwctl
fwcontrol.c revision 1.9
      1  1.9    cegger /*	$NetBSD: fwcontrol.c,v 1.9 2010/08/24 08:41:24 cegger Exp $	*/
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (C) 2002
      4  1.1  kiyohara  * 	Hidetoshi Shimokawa. All rights reserved.
      5  1.6  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  * 3. All advertising materials mentioning features or use of this software
     15  1.1  kiyohara  *    must display the following acknowledgement:
     16  1.1  kiyohara  *
     17  1.1  kiyohara  *	This product includes software developed by Hidetoshi Shimokawa.
     18  1.1  kiyohara  *
     19  1.1  kiyohara  * 4. Neither the name of the author nor the names of its contributors
     20  1.1  kiyohara  *    may be used to endorse or promote products derived from this software
     21  1.1  kiyohara  *    without specific prior written permission.
     22  1.6  kiyohara  *
     23  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  kiyohara  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  kiyohara  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  kiyohara  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  kiyohara  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  kiyohara  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  kiyohara  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  kiyohara  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  kiyohara  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  kiyohara  * SUCH DAMAGE.
     34  1.1  kiyohara  */
     35  1.1  kiyohara #include <sys/cdefs.h>
     36  1.9    cegger //__FBSDID("$FreeBSD: src/usr.sbin/fwcontrol/fwcontrol.c,v 1.23 2006/10/26 22:33:38 imp Exp $");
     37  1.9    cegger __RCSID("$NetBSD: fwcontrol.c,v 1.9 2010/08/24 08:41:24 cegger Exp $");
     38  1.1  kiyohara 
     39  1.1  kiyohara #include <sys/param.h>
     40  1.1  kiyohara #include <sys/malloc.h>
     41  1.1  kiyohara #include <sys/types.h>
     42  1.1  kiyohara #include <sys/sysctl.h>
     43  1.1  kiyohara #include <sys/socket.h>
     44  1.1  kiyohara #include <sys/ioctl.h>
     45  1.1  kiyohara #include <sys/errno.h>
     46  1.1  kiyohara #include "eui64.h"
     47  1.1  kiyohara #include <dev/ieee1394/firewire.h>
     48  1.1  kiyohara #include <dev/ieee1394/iec13213.h>
     49  1.1  kiyohara #include <dev/ieee1394/fwphyreg.h>
     50  1.6  kiyohara #include <dev/ieee1394/iec68113.h>
     51  1.1  kiyohara 
     52  1.1  kiyohara #include <netinet/in.h>
     53  1.1  kiyohara #include <fcntl.h>
     54  1.1  kiyohara #include <stdio.h>
     55  1.1  kiyohara #include <err.h>
     56  1.1  kiyohara #include <stdlib.h>
     57  1.1  kiyohara #include <string.h>
     58  1.6  kiyohara #include <sysexits.h>
     59  1.1  kiyohara #include <unistd.h>
     60  1.6  kiyohara #include "fwmethods.h"
     61  1.1  kiyohara 
     62  1.6  kiyohara static void sysctl_set_int(const char *, int);
     63  1.1  kiyohara 
     64  1.1  kiyohara static void
     65  1.1  kiyohara usage(void)
     66  1.1  kiyohara {
     67  1.1  kiyohara 	fprintf(stderr,
     68  1.9    cegger 	    "%s [-prt] [-b pri_req] [-c node] [-d node] [-f force_root ]\n"
     69  1.9    cegger 	    "\t[-g gap_count] [-l file] [-M mode] [-m EUI64 | hostname]\n"
     70  1.9    cegger 	    "\t[-o node] [-R filename] [-S filename] [-s node] [-u bus_num]\n"
     71  1.9    cegger 	    "\n"
     72  1.9    cegger 	    "\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
     73  1.9    cegger 	    "\t-c: read configuration ROM\n"
     74  1.9    cegger 	    "\t-d: hex dump of configuration ROM\n"
     75  1.9    cegger 	    "\t-f: force root node\n"
     76  1.9    cegger 	    "\t-g: broadcast gap_count by phy_config packet\n"
     77  1.9    cegger 	    "\t-l: load and parse hex dump file of configuration ROM\n"
     78  1.9    cegger 	    "\t-M: specify dv or mpeg\n"
     79  1.9    cegger 	    "\t-m: set fwmem target\n"
     80  1.9    cegger 	    "\t-o: send link-on packet to the node\n"
     81  1.9    cegger 	    "\t-p: dump PHY registers\n"
     82  1.9    cegger 	    "\t-R: receive DV or MPEG TS stream\n"
     83  1.9    cegger 	    "\t-r: bus reset\n"
     84  1.9    cegger 	    "\t-S: send DV stream\n"
     85  1.9    cegger 	    "\t-s: write RESET_START register on the node\n"
     86  1.9    cegger 	    "\t-t: read topology map\n"
     87  1.9    cegger 	    "\t-u: specify bus number\n", getprogname());
     88  1.6  kiyohara 	exit(EX_USAGE);
     89  1.1  kiyohara }
     90  1.1  kiyohara 
     91  1.1  kiyohara static void
     92  1.1  kiyohara fweui2eui64(const struct fw_eui64 *fweui, struct eui64 *eui)
     93  1.1  kiyohara {
     94  1.9    cegger 	*(uint32_t*)&(eui->octet[0]) = htonl(fweui->hi);
     95  1.9    cegger 	*(uint32_t*)&(eui->octet[4]) = htonl(fweui->lo);
     96  1.1  kiyohara }
     97  1.1  kiyohara 
     98  1.9    cegger static void
     99  1.9    cegger get_dev(int fd, struct fw_devlstreq *data)
    100  1.1  kiyohara {
    101  1.1  kiyohara 
    102  1.1  kiyohara 	if (data == NULL)
    103  1.9    cegger 		err(EX_SOFTWARE, "%s: data malloc", __func__);
    104  1.9    cegger 	if (ioctl(fd, FW_GDEVLST, data) < 0)
    105  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    106  1.1  kiyohara }
    107  1.1  kiyohara 
    108  1.1  kiyohara static int
    109  1.1  kiyohara str2node(int fd, const char *nodestr)
    110  1.1  kiyohara {
    111  1.1  kiyohara 	struct eui64 eui, tmpeui;
    112  1.1  kiyohara 	struct fw_devlstreq *data;
    113  1.1  kiyohara 	char *endptr;
    114  1.1  kiyohara 	int i, node;
    115  1.1  kiyohara 
    116  1.1  kiyohara 	if (nodestr == '\0')
    117  1.9    cegger 		return -1;
    118  1.1  kiyohara 
    119  1.1  kiyohara 	/*
    120  1.1  kiyohara 	 * Deal with classic node specifications.
    121  1.1  kiyohara 	 */
    122  1.1  kiyohara 	node = strtol(nodestr, &endptr, 0);
    123  1.1  kiyohara 	if (*endptr == '\0')
    124  1.1  kiyohara 		goto gotnode;
    125  1.1  kiyohara 
    126  1.1  kiyohara 	/*
    127  1.1  kiyohara 	 * Try to get an eui and match it against available nodes.
    128  1.1  kiyohara 	 */
    129  1.1  kiyohara 	if (eui64_hostton(nodestr, &eui) != 0 && eui64_aton(nodestr, &eui) != 0)
    130  1.9    cegger 		return -1;
    131  1.1  kiyohara 
    132  1.9    cegger 	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
    133  1.9    cegger 	if (data == NULL)
    134  1.9    cegger 		err(EX_SOFTWARE, "%s: data malloc", __func__);
    135  1.9    cegger 	get_dev(fd,data);
    136  1.1  kiyohara 
    137  1.1  kiyohara 	for (i = 0; i < data->info_len; i++) {
    138  1.1  kiyohara 		fweui2eui64(&data->dev[i].eui, &tmpeui);
    139  1.1  kiyohara 		if (memcmp(&eui, &tmpeui, sizeof(struct eui64)) == 0) {
    140  1.1  kiyohara 			node = data->dev[i].dst;
    141  1.9    cegger 			if (data != NULL)
    142  1.9    cegger 				free(data);
    143  1.1  kiyohara 			goto gotnode;
    144  1.1  kiyohara 		}
    145  1.1  kiyohara 	}
    146  1.9    cegger 	if (i >= data->info_len) {
    147  1.9    cegger 		if (data != NULL)
    148  1.9    cegger 			free(data);
    149  1.9    cegger 		return -1;
    150  1.9    cegger 	}
    151  1.1  kiyohara 
    152  1.1  kiyohara gotnode:
    153  1.1  kiyohara 	if (node < 0 || node > 63)
    154  1.9    cegger 		return -1;
    155  1.1  kiyohara 	else
    156  1.9    cegger 		return node;
    157  1.1  kiyohara }
    158  1.1  kiyohara 
    159  1.1  kiyohara static void
    160  1.1  kiyohara list_dev(int fd)
    161  1.1  kiyohara {
    162  1.1  kiyohara 	struct fw_devlstreq *data;
    163  1.1  kiyohara 	struct fw_devinfo *devinfo;
    164  1.1  kiyohara 	struct eui64 eui;
    165  1.9    cegger 	char addr[EUI64_SIZ], hostname[40];
    166  1.1  kiyohara 	int i;
    167  1.1  kiyohara 
    168  1.9    cegger 	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
    169  1.9    cegger 	if (data == NULL)
    170  1.9    cegger 		err(EX_SOFTWARE, "%s:data malloc", __func__);
    171  1.9    cegger 	get_dev(fd, data);
    172  1.1  kiyohara 	printf("%d devices (info_len=%d)\n", data->n, data->info_len);
    173  1.9    cegger 	printf("node           EUI64          status    hostname\n");
    174  1.1  kiyohara 	for (i = 0; i < data->info_len; i++) {
    175  1.1  kiyohara 		devinfo = &data->dev[i];
    176  1.1  kiyohara 		fweui2eui64(&devinfo->eui, &eui);
    177  1.1  kiyohara 		eui64_ntoa(&eui, addr, sizeof(addr));
    178  1.9    cegger 		if (eui64_ntohost(hostname, sizeof(hostname), &eui))
    179  1.9    cegger 			hostname[0] = 0;
    180  1.9    cegger 		printf("%4d  %s %6d    %s\n",
    181  1.1  kiyohara 			(devinfo->status || i == 0) ? devinfo->dst : -1,
    182  1.9    cegger 			addr, devinfo->status, hostname);
    183  1.1  kiyohara 	}
    184  1.1  kiyohara 	free((void *)data);
    185  1.1  kiyohara }
    186  1.1  kiyohara 
    187  1.9    cegger static uint32_t
    188  1.9    cegger read_write_quad(int fd, struct fw_eui64 eui, uint32_t addr_lo, int readmode,
    189  1.9    cegger 		uint32_t data)
    190  1.1  kiyohara {
    191  1.1  kiyohara         struct fw_asyreq *asyreq;
    192  1.9    cegger 	uint32_t *qld, res;
    193  1.1  kiyohara 
    194  1.9    cegger 	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
    195  1.4  christos 	if (asyreq == NULL)
    196  1.9    cegger 		err(EX_SOFTWARE, "%s:asyreq malloc", __func__);
    197  1.1  kiyohara 	asyreq->req.len = 16;
    198  1.1  kiyohara #if 0
    199  1.1  kiyohara 	asyreq->req.type = FWASREQNODE;
    200  1.1  kiyohara 	asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
    201  1.1  kiyohara #else
    202  1.1  kiyohara 	asyreq->req.type = FWASREQEUI;
    203  1.1  kiyohara 	asyreq->req.dst.eui = eui;
    204  1.1  kiyohara #endif
    205  1.1  kiyohara 	asyreq->pkt.mode.rreqq.tlrt = 0;
    206  1.1  kiyohara 	if (readmode)
    207  1.1  kiyohara 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
    208  1.1  kiyohara 	else
    209  1.1  kiyohara 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
    210  1.1  kiyohara 
    211  1.1  kiyohara 	asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
    212  1.1  kiyohara 	asyreq->pkt.mode.rreqq.dest_lo = addr_lo;
    213  1.1  kiyohara 
    214  1.9    cegger 	qld = (uint32_t *)&asyreq->pkt;
    215  1.1  kiyohara 	if (!readmode)
    216  1.9    cegger 		asyreq->pkt.mode.wreqq.data = htonl(data);
    217  1.1  kiyohara 
    218  1.9    cegger 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
    219  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    220  1.1  kiyohara 	res = qld[3];
    221  1.1  kiyohara 	free(asyreq);
    222  1.1  kiyohara 	if (readmode)
    223  1.1  kiyohara 		return ntohl(res);
    224  1.1  kiyohara 	else
    225  1.1  kiyohara 		return 0;
    226  1.1  kiyohara }
    227  1.1  kiyohara 
    228  1.9    cegger /*
    229  1.9    cegger  * Send a PHY Config Packet
    230  1.9    cegger  * ieee 1394a-2005 4.3.4.3
    231  1.9    cegger  *
    232  1.9    cegger  * Message ID   Root ID    R  T   Gap Count
    233  1.9    cegger  * 00(2 bits)   (6 bits)   1  1   (6 bits)
    234  1.9    cegger  *
    235  1.9    cegger  * if "R" is set, then Root ID will be the next
    236  1.9    cegger  * root node upon the next bus reset.
    237  1.9    cegger  * if "T" is set, then Gap Count will be the
    238  1.9    cegger  * value that all nodes use for their Gap Count
    239  1.9    cegger  * if "R" and "T" are not set, then this message
    240  1.9    cegger  * is either ignored or interpreted as an extended
    241  1.9    cegger  * PHY config Packet as per 1394a-2005 4.3.4.4
    242  1.9    cegger  */
    243  1.1  kiyohara static void
    244  1.1  kiyohara send_phy_config(int fd, int root_node, int gap_count)
    245  1.1  kiyohara {
    246  1.1  kiyohara         struct fw_asyreq *asyreq;
    247  1.1  kiyohara 
    248  1.9    cegger 	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
    249  1.4  christos 	if (asyreq == NULL)
    250  1.9    cegger 		err(EX_SOFTWARE, "%s:asyreq malloc", __func__);
    251  1.1  kiyohara 	asyreq->req.len = 12;
    252  1.1  kiyohara 	asyreq->req.type = FWASREQNODE;
    253  1.1  kiyohara 	asyreq->pkt.mode.ld[0] = 0;
    254  1.1  kiyohara 	asyreq->pkt.mode.ld[1] = 0;
    255  1.1  kiyohara 	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
    256  1.1  kiyohara 	if (root_node >= 0)
    257  1.9    cegger 		asyreq->pkt.mode.ld[1] |= ((root_node << 24) | (1 << 23));
    258  1.1  kiyohara 	if (gap_count >= 0)
    259  1.9    cegger 		asyreq->pkt.mode.ld[1] |= ((1 << 22) | (gap_count << 16));
    260  1.1  kiyohara 	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
    261  1.1  kiyohara 
    262  1.1  kiyohara 	printf("send phy_config root_node=%d gap_count=%d\n",
    263  1.1  kiyohara 						root_node, gap_count);
    264  1.1  kiyohara 
    265  1.1  kiyohara 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
    266  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    267  1.1  kiyohara 	free(asyreq);
    268  1.1  kiyohara }
    269  1.1  kiyohara 
    270  1.1  kiyohara static void
    271  1.9    cegger link_on(int fd, int node)
    272  1.1  kiyohara {
    273  1.1  kiyohara         struct fw_asyreq *asyreq;
    274  1.1  kiyohara 
    275  1.9    cegger 	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
    276  1.4  christos 	if (asyreq == NULL)
    277  1.9    cegger 		err(EX_SOFTWARE, "%s:asyreq malloc", __func__);
    278  1.1  kiyohara 	asyreq->req.len = 12;
    279  1.1  kiyohara 	asyreq->req.type = FWASREQNODE;
    280  1.1  kiyohara 	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
    281  1.1  kiyohara 	asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
    282  1.1  kiyohara 	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
    283  1.1  kiyohara 
    284  1.1  kiyohara 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
    285  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    286  1.1  kiyohara 	free(asyreq);
    287  1.1  kiyohara }
    288  1.1  kiyohara 
    289  1.1  kiyohara static void
    290  1.1  kiyohara reset_start(int fd, int node)
    291  1.1  kiyohara {
    292  1.1  kiyohara         struct fw_asyreq *asyreq;
    293  1.1  kiyohara 
    294  1.9    cegger 	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
    295  1.4  christos 	if (asyreq == NULL)
    296  1.9    cegger 		err(EX_SOFTWARE, "%s:asyreq malloc", __func__);
    297  1.1  kiyohara 	asyreq->req.len = 16;
    298  1.1  kiyohara 	asyreq->req.type = FWASREQNODE;
    299  1.1  kiyohara 	asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
    300  1.1  kiyohara 	asyreq->pkt.mode.wreqq.tlrt = 0;
    301  1.1  kiyohara 	asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;
    302  1.1  kiyohara 
    303  1.1  kiyohara 	asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
    304  1.1  kiyohara 	asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
    305  1.1  kiyohara 
    306  1.1  kiyohara 	asyreq->pkt.mode.wreqq.data = htonl(0x1);
    307  1.1  kiyohara 
    308  1.1  kiyohara 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
    309  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    310  1.1  kiyohara 	free(asyreq);
    311  1.1  kiyohara }
    312  1.1  kiyohara 
    313  1.1  kiyohara static void
    314  1.9    cegger set_pri_req(int fd, uint32_t pri_req)
    315  1.1  kiyohara {
    316  1.1  kiyohara 	struct fw_devlstreq *data;
    317  1.1  kiyohara 	struct fw_devinfo *devinfo;
    318  1.1  kiyohara 	struct eui64 eui;
    319  1.1  kiyohara 	char addr[EUI64_SIZ];
    320  1.9    cegger 	uint32_t max, reg, old;
    321  1.1  kiyohara 	int i;
    322  1.1  kiyohara 
    323  1.9    cegger 	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
    324  1.9    cegger 	if (data == NULL)
    325  1.9    cegger 		err(EX_SOFTWARE, "%s:data malloc", __func__);
    326  1.9    cegger 	get_dev(fd, data);
    327  1.1  kiyohara #define BUGET_REG 0xf0000218
    328  1.1  kiyohara 	for (i = 0; i < data->info_len; i++) {
    329  1.1  kiyohara 		devinfo = &data->dev[i];
    330  1.1  kiyohara 		if (!devinfo->status)
    331  1.1  kiyohara 			continue;
    332  1.1  kiyohara 		reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
    333  1.1  kiyohara 		fweui2eui64(&devinfo->eui, &eui);
    334  1.1  kiyohara 		eui64_ntoa(&eui, addr, sizeof(addr));
    335  1.1  kiyohara 		printf("%d %s, %08x",
    336  1.1  kiyohara 			devinfo->dst, addr, reg);
    337  1.6  kiyohara 		if (reg > 0) {
    338  1.1  kiyohara 			old = (reg & 0x3f);
    339  1.1  kiyohara 			max = (reg & 0x3f00) >> 8;
    340  1.1  kiyohara 			if (pri_req > max)
    341  1.1  kiyohara 				pri_req =  max;
    342  1.1  kiyohara 			printf(" 0x%x -> 0x%x\n", old, pri_req);
    343  1.1  kiyohara 			read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
    344  1.1  kiyohara 		} else {
    345  1.1  kiyohara 			printf("\n");
    346  1.1  kiyohara 		}
    347  1.1  kiyohara 	}
    348  1.1  kiyohara 	free((void *)data);
    349  1.1  kiyohara }
    350  1.1  kiyohara 
    351  1.1  kiyohara static void
    352  1.9    cegger parse_bus_info_block(uint32_t *p)
    353  1.1  kiyohara {
    354  1.1  kiyohara 	char addr[EUI64_SIZ];
    355  1.1  kiyohara 	struct bus_info *bi;
    356  1.1  kiyohara 	struct eui64 eui;
    357  1.1  kiyohara 
    358  1.1  kiyohara 	bi = (struct bus_info *)p;
    359  1.1  kiyohara 	fweui2eui64(&bi->eui64, &eui);
    360  1.1  kiyohara 	eui64_ntoa(&eui, addr, sizeof(addr));
    361  1.1  kiyohara 	printf("bus_name: 0x%04x\n"
    362  1.1  kiyohara 		"irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
    363  1.1  kiyohara 		"cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
    364  1.1  kiyohara 		"generation:%d link_spd:%d\n"
    365  1.1  kiyohara 		"EUI64: %s\n",
    366  1.1  kiyohara 		bi->bus_name,
    367  1.1  kiyohara 		bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc,
    368  1.1  kiyohara 		bi->cyc_clk_acc, bi->max_rec, bi->max_rom,
    369  1.1  kiyohara 		bi->generation, bi->link_spd,
    370  1.1  kiyohara 		addr);
    371  1.1  kiyohara }
    372  1.1  kiyohara 
    373  1.1  kiyohara static int
    374  1.1  kiyohara get_crom(int fd, int node, void *crom_buf, int len)
    375  1.1  kiyohara {
    376  1.1  kiyohara 	struct fw_crom_buf buf;
    377  1.1  kiyohara 	int i, error;
    378  1.1  kiyohara 	struct fw_devlstreq *data;
    379  1.1  kiyohara 
    380  1.9    cegger 	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
    381  1.9    cegger 	if (data == NULL)
    382  1.9    cegger 		err(EX_SOFTWARE, "%s:data malloc", __func__);
    383  1.9    cegger 	get_dev(fd, data);
    384  1.1  kiyohara 
    385  1.9    cegger 	for (i = 0; i < data->info_len; i++)
    386  1.1  kiyohara 		if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
    387  1.1  kiyohara 			break;
    388  1.1  kiyohara 	if (i == data->info_len)
    389  1.1  kiyohara 		errx(1, "no such node %d.", node);
    390  1.1  kiyohara 	else
    391  1.1  kiyohara 		buf.eui = data->dev[i].eui;
    392  1.9    cegger 	free(data);
    393  1.1  kiyohara 
    394  1.1  kiyohara 	buf.len = len;
    395  1.1  kiyohara 	buf.ptr = crom_buf;
    396  1.9    cegger 	memset(crom_buf, 0, len);
    397  1.9    cegger 	if ((error = ioctl(fd, FW_GCROM, &buf)) < 0)
    398  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    399  1.1  kiyohara 
    400  1.1  kiyohara 	return error;
    401  1.1  kiyohara }
    402  1.1  kiyohara 
    403  1.1  kiyohara static void
    404  1.9    cegger show_crom(uint32_t *crom_buf)
    405  1.1  kiyohara {
    406  1.1  kiyohara 	int i;
    407  1.1  kiyohara 	struct crom_context cc;
    408  1.7   xtraeme 	const char *desc;
    409  1.7   xtraeme 	char info[256];
    410  1.1  kiyohara 	static const char *key_types = "ICLD";
    411  1.1  kiyohara 	struct csrreg *reg;
    412  1.1  kiyohara 	struct csrdirectory *dir;
    413  1.1  kiyohara 	struct csrhdr *hdr;
    414  1.9    cegger 	uint16_t crc;
    415  1.1  kiyohara 
    416  1.1  kiyohara 	printf("first quad: 0x%08x ", *crom_buf);
    417  1.1  kiyohara 	if (crom_buf[0] == 0) {
    418  1.1  kiyohara 		printf("(Invalid Configuration ROM)\n");
    419  1.1  kiyohara 		return;
    420  1.1  kiyohara 	}
    421  1.1  kiyohara 	hdr = (struct csrhdr *)crom_buf;
    422  1.1  kiyohara 	if (hdr->info_len == 1) {
    423  1.1  kiyohara 		/* minimum ROM */
    424  1.1  kiyohara 		reg = (struct csrreg *)hdr;
    425  1.1  kiyohara 		printf("verndor ID: 0x%06x\n",  reg->val);
    426  1.1  kiyohara 		return;
    427  1.1  kiyohara 	}
    428  1.1  kiyohara 	printf("info_len=%d crc_len=%d crc=0x%04x",
    429  1.1  kiyohara 		hdr->info_len, hdr->crc_len, hdr->crc);
    430  1.1  kiyohara 	crc = crom_crc(crom_buf+1, hdr->crc_len);
    431  1.1  kiyohara 	if (crc == hdr->crc)
    432  1.1  kiyohara 		printf("(OK)\n");
    433  1.1  kiyohara 	else
    434  1.1  kiyohara 		printf("(NG)\n");
    435  1.6  kiyohara 	parse_bus_info_block(crom_buf+1);
    436  1.1  kiyohara 
    437  1.1  kiyohara 	crom_init_context(&cc, crom_buf);
    438  1.1  kiyohara 	dir = cc.stack[0].dir;
    439  1.1  kiyohara 	if (!dir) {
    440  1.1  kiyohara 		printf("no root directory - giving up\n");
    441  1.1  kiyohara 		return;
    442  1.1  kiyohara 	}
    443  1.1  kiyohara 	printf("root_directory: len=0x%04x(%d) crc=0x%04x",
    444  1.1  kiyohara 			dir->crc_len, dir->crc_len, dir->crc);
    445  1.9    cegger 	crc = crom_crc((uint32_t *)&dir->entry[0], dir->crc_len);
    446  1.1  kiyohara 	if (crc == dir->crc)
    447  1.1  kiyohara 		printf("(OK)\n");
    448  1.1  kiyohara 	else
    449  1.1  kiyohara 		printf("(NG)\n");
    450  1.1  kiyohara 	if (dir->crc_len < 1)
    451  1.1  kiyohara 		return;
    452  1.1  kiyohara 	while (cc.depth >= 0) {
    453  1.1  kiyohara 		desc = crom_desc(&cc, info, sizeof(info));
    454  1.1  kiyohara 		reg = crom_get(&cc);
    455  1.1  kiyohara 		for (i = 0; i < cc.depth; i++)
    456  1.1  kiyohara 			printf("\t");
    457  1.1  kiyohara 		printf("%02x(%c:%02x) %06x %s: %s\n",
    458  1.1  kiyohara 			reg->key,
    459  1.1  kiyohara 			key_types[(reg->key & CSRTYPE_MASK)>>6],
    460  1.1  kiyohara 			reg->key & CSRKEY_MASK, reg->val,
    461  1.1  kiyohara 			desc, info);
    462  1.1  kiyohara 		crom_next(&cc);
    463  1.1  kiyohara 	}
    464  1.1  kiyohara }
    465  1.1  kiyohara 
    466  1.1  kiyohara #define DUMP_FORMAT	"%08x %08x %08x %08x %08x %08x %08x %08x\n"
    467  1.1  kiyohara 
    468  1.1  kiyohara static void
    469  1.9    cegger dump_crom(uint32_t *p)
    470  1.1  kiyohara {
    471  1.1  kiyohara 	int len=1024, i;
    472  1.1  kiyohara 
    473  1.1  kiyohara 	for (i = 0; i < len/(4*8); i ++) {
    474  1.1  kiyohara 		printf(DUMP_FORMAT,
    475  1.1  kiyohara 			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
    476  1.1  kiyohara 		p += 8;
    477  1.1  kiyohara 	}
    478  1.1  kiyohara }
    479  1.1  kiyohara 
    480  1.1  kiyohara static void
    481  1.9    cegger load_crom(const char *filename, uint32_t *p)
    482  1.1  kiyohara {
    483  1.1  kiyohara 	FILE *file;
    484  1.1  kiyohara 	int len=1024, i;
    485  1.1  kiyohara 
    486  1.1  kiyohara 	if ((file = fopen(filename, "r")) == NULL)
    487  1.9    cegger 		err(EX_IOERR, "load_crom %s", filename);
    488  1.1  kiyohara 	for (i = 0; i < len/(4*8); i ++) {
    489  1.9    cegger 		fscanf(file, DUMP_FORMAT, p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
    490  1.1  kiyohara 		p += 8;
    491  1.1  kiyohara 	}
    492  1.1  kiyohara }
    493  1.1  kiyohara 
    494  1.1  kiyohara static void
    495  1.1  kiyohara show_topology_map(int fd)
    496  1.1  kiyohara {
    497  1.1  kiyohara 	struct fw_topology_map *tmap;
    498  1.1  kiyohara 	union fw_self_id sid;
    499  1.1  kiyohara 	int i;
    500  1.1  kiyohara 	static const char *port_status[] = {" ", "-", "P", "C"};
    501  1.1  kiyohara 	static const char *pwr_class[] = {" 0W", "15W", "30W", "45W",
    502  1.1  kiyohara 					"-1W", "-2W", "-5W", "-9W"};
    503  1.1  kiyohara 	static const char *speed[] = {"S100", "S200", "S400", "S800"};
    504  1.9    cegger 
    505  1.9    cegger 	tmap = malloc(sizeof(struct fw_topology_map));
    506  1.1  kiyohara 	if (tmap == NULL)
    507  1.9    cegger 		err(EX_SOFTWARE, "%s:tmap malloc", __func__);
    508  1.9    cegger 	if (ioctl(fd, FW_GTPMAP, tmap) < 0)
    509  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    510  1.1  kiyohara 	printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
    511  1.1  kiyohara 		tmap->crc_len, tmap->generation,
    512  1.1  kiyohara 		tmap->node_count, tmap->self_id_count);
    513  1.1  kiyohara 	printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
    514  1.1  kiyohara 		" ini more\n");
    515  1.1  kiyohara 	for (i = 0; i < tmap->crc_len - 2; i++) {
    516  1.1  kiyohara 		sid = tmap->self_id[i];
    517  1.1  kiyohara 		if (sid.p0.sequel) {
    518  1.1  kiyohara 			printf("%02d sequel packet\n", sid.p0.phy_id);
    519  1.1  kiyohara 			continue;
    520  1.1  kiyohara 		}
    521  1.9    cegger 		printf("%02d   %2d      %2d  %4s     %d   %3s"
    522  1.1  kiyohara 				"     %s     %s     %s   %d    %d\n",
    523  1.1  kiyohara 			sid.p0.phy_id,
    524  1.1  kiyohara 			sid.p0.link_active,
    525  1.1  kiyohara 			sid.p0.gap_count,
    526  1.1  kiyohara 			speed[sid.p0.phy_speed],
    527  1.1  kiyohara 			sid.p0.contender,
    528  1.1  kiyohara 			pwr_class[sid.p0.power_class],
    529  1.1  kiyohara 			port_status[sid.p0.port0],
    530  1.1  kiyohara 			port_status[sid.p0.port1],
    531  1.1  kiyohara 			port_status[sid.p0.port2],
    532  1.1  kiyohara 			sid.p0.initiated_reset,
    533  1.1  kiyohara 			sid.p0.more_packets
    534  1.1  kiyohara 		);
    535  1.1  kiyohara 	}
    536  1.1  kiyohara 	free(tmap);
    537  1.1  kiyohara }
    538  1.1  kiyohara 
    539  1.1  kiyohara static void
    540  1.9    cegger read_phy_registers(int fd, uint8_t *buf, int offset, int len)
    541  1.1  kiyohara {
    542  1.1  kiyohara 	struct fw_reg_req_t reg;
    543  1.1  kiyohara 	int i;
    544  1.1  kiyohara 
    545  1.1  kiyohara 	for (i = 0; i < len; i++) {
    546  1.1  kiyohara 		reg.addr = offset + i;
    547  1.1  kiyohara 		if (ioctl(fd, FWOHCI_RDPHYREG, &reg) < 0)
    548  1.9    cegger 			err(EX_IOERR, "%s: ioctl", __func__);
    549  1.9    cegger 		buf[i] = (uint8_t) reg.data;
    550  1.1  kiyohara 		printf("0x%02x ",  reg.data);
    551  1.1  kiyohara 	}
    552  1.1  kiyohara 	printf("\n");
    553  1.1  kiyohara }
    554  1.1  kiyohara 
    555  1.1  kiyohara static void
    556  1.9    cegger read_phy_page(int fd, uint8_t *buf, int page, int port)
    557  1.1  kiyohara {
    558  1.1  kiyohara 	struct fw_reg_req_t reg;
    559  1.1  kiyohara 
    560  1.1  kiyohara 	reg.addr = 0x7;
    561  1.1  kiyohara 	reg.data = ((page & 7) << 5) | (port & 0xf);
    562  1.1  kiyohara 	if (ioctl(fd, FWOHCI_WRPHYREG, &reg) < 0)
    563  1.9    cegger 		err(EX_IOERR, "%s: ioctl", __func__);
    564  1.1  kiyohara 	read_phy_registers(fd, buf, 8, 8);
    565  1.1  kiyohara }
    566  1.1  kiyohara 
    567  1.1  kiyohara static void
    568  1.1  kiyohara dump_phy_registers(int fd)
    569  1.1  kiyohara {
    570  1.1  kiyohara 	struct phyreg_base b;
    571  1.1  kiyohara 	struct phyreg_page0 p;
    572  1.1  kiyohara 	struct phyreg_page1 v;
    573  1.1  kiyohara 	int i;
    574  1.1  kiyohara 
    575  1.1  kiyohara 	printf("=== base register ===\n");
    576  1.9    cegger 	read_phy_registers(fd, (uint8_t *)&b, 0, 8);
    577  1.1  kiyohara 	printf(
    578  1.1  kiyohara 	    "Physical_ID:%d  R:%d  CPS:%d\n"
    579  1.1  kiyohara 	    "RHB:%d  IBR:%d  Gap_Count:%d\n"
    580  1.1  kiyohara 	    "Extended:%d Num_Ports:%d\n"
    581  1.1  kiyohara 	    "PHY_Speed:%d Delay:%d\n"
    582  1.1  kiyohara 	    "LCtrl:%d C:%d Jitter:%d Pwr_Class:%d\n"
    583  1.1  kiyohara 	    "WDIE:%d ISBR:%d CTOI:%d CPSI:%d STOI:%d PEI:%d EAA:%d EMC:%d\n"
    584  1.1  kiyohara 	    "Max_Legacy_SPD:%d BLINK:%d Bridge:%d\n"
    585  1.1  kiyohara 	    "Page_Select:%d Port_Select%d\n",
    586  1.1  kiyohara 	    b.phy_id, b.r, b.cps,
    587  1.6  kiyohara 	    b.rhb, b.ibr, b.gap_count,
    588  1.1  kiyohara 	    b.extended, b.num_ports,
    589  1.1  kiyohara 	    b.phy_speed, b.delay,
    590  1.1  kiyohara 	    b.lctrl, b.c, b.jitter, b.pwr_class,
    591  1.1  kiyohara 	    b.wdie, b.isbr, b.ctoi, b.cpsi, b.stoi, b.pei, b.eaa, b.emc,
    592  1.1  kiyohara 	    b.legacy_spd, b.blink, b.bridge,
    593  1.1  kiyohara 	    b.page_select, b.port_select
    594  1.1  kiyohara 	);
    595  1.1  kiyohara 
    596  1.1  kiyohara 	for (i = 0; i < b.num_ports; i ++) {
    597  1.1  kiyohara 		printf("\n=== page 0 port %d ===\n", i);
    598  1.9    cegger 		read_phy_page(fd, (uint8_t *)&p, 0, i);
    599  1.1  kiyohara 		printf(
    600  1.1  kiyohara 		    "Astat:%d BStat:%d Ch:%d Con:%d RXOK:%d Dis:%d\n"
    601  1.1  kiyohara 		    "Negotiated_speed:%d PIE:%d Fault:%d Stanby_fault:%d Disscrm:%d B_Only:%d\n"
    602  1.1  kiyohara 		    "DC_connected:%d Max_port_speed:%d LPP:%d Cable_speed:%d\n"
    603  1.1  kiyohara 		    "Connection_unreliable:%d Beta_mode:%d\n"
    604  1.1  kiyohara 		    "Port_error:0x%x\n"
    605  1.1  kiyohara 		    "Loop_disable:%d In_standby:%d Hard_disable:%d\n",
    606  1.1  kiyohara 		    p.astat, p.bstat, p.ch, p.con, p.rxok, p.dis,
    607  1.1  kiyohara 		    p.negotiated_speed, p.pie, p.fault, p.stanby_fault, p.disscrm, p.b_only,
    608  1.1  kiyohara 		    p.dc_connected, p.max_port_speed, p.lpp, p.cable_speed,
    609  1.1  kiyohara 		    p.connection_unreliable, p.beta_mode,
    610  1.1  kiyohara 		    p.port_error,
    611  1.1  kiyohara 		    p.loop_disable, p.in_standby, p.hard_disable
    612  1.1  kiyohara 		);
    613  1.1  kiyohara 	}
    614  1.1  kiyohara 	printf("\n=== page 1 ===\n");
    615  1.9    cegger 	read_phy_page(fd, (uint8_t *)&v, 1, 0);
    616  1.1  kiyohara 	printf(
    617  1.1  kiyohara 	    "Compliance:%d\n"
    618  1.1  kiyohara 	    "Vendor_ID:0x%06x\n"
    619  1.1  kiyohara 	    "Product_ID:0x%06x\n",
    620  1.1  kiyohara 	    v.compliance,
    621  1.1  kiyohara 	    (v.vendor_id[0] << 16) | (v.vendor_id[1] << 8) | v.vendor_id[2],
    622  1.1  kiyohara 	    (v.product_id[0] << 16) | (v.product_id[1] << 8) | v.product_id[2]
    623  1.1  kiyohara 	);
    624  1.1  kiyohara }
    625  1.1  kiyohara 
    626  1.9    cegger static int
    627  1.9    cegger open_dev(int *fd, const char *_devname)
    628  1.1  kiyohara {
    629  1.1  kiyohara 
    630  1.1  kiyohara 	if (*fd < 0) {
    631  1.9    cegger 		*fd = open(_devname, O_RDWR);
    632  1.1  kiyohara 		if (*fd < 0)
    633  1.9    cegger 			return -1;
    634  1.1  kiyohara 	}
    635  1.9    cegger 	return 0;
    636  1.1  kiyohara }
    637  1.1  kiyohara 
    638  1.6  kiyohara static void
    639  1.1  kiyohara sysctl_set_int(const char *name, int val)
    640  1.1  kiyohara {
    641  1.1  kiyohara 	if (sysctlbyname(name, NULL, NULL, &val, sizeof(int)) < 0)
    642  1.1  kiyohara 		err(1, "sysctl %s failed.", name);
    643  1.6  kiyohara }
    644  1.6  kiyohara 
    645  1.6  kiyohara static fwmethod *
    646  1.6  kiyohara detect_recv_fn(int fd, char ich)
    647  1.6  kiyohara {
    648  1.6  kiyohara 	char *buf;
    649  1.6  kiyohara 	struct fw_isochreq isoreq;
    650  1.6  kiyohara 	struct fw_isobufreq bufreq;
    651  1.6  kiyohara 	int len;
    652  1.9    cegger 	uint32_t *ptr;
    653  1.6  kiyohara 	struct ciphdr *ciph;
    654  1.6  kiyohara 	fwmethod *retfn;
    655  1.9    cegger #define RECV_NUM_PACKET	16
    656  1.9    cegger #define RECV_PACKET_SZ	1024
    657  1.6  kiyohara 
    658  1.6  kiyohara 	bufreq.rx.nchunk = 8;
    659  1.9    cegger 	bufreq.rx.npacket = RECV_NUM_PACKET;
    660  1.9    cegger 	bufreq.rx.psize = RECV_PACKET_SZ;
    661  1.6  kiyohara 	bufreq.tx.nchunk = 0;
    662  1.6  kiyohara 	bufreq.tx.npacket = 0;
    663  1.6  kiyohara 	bufreq.tx.psize = 0;
    664  1.6  kiyohara 
    665  1.6  kiyohara 	if (ioctl(fd, FW_SSTBUF, &bufreq) < 0)
    666  1.9    cegger 		err(EX_IOERR, "%s: ioctl FW_SSTBUF", __func__);
    667  1.6  kiyohara 
    668  1.6  kiyohara 	isoreq.ch = ich & 0x3f;
    669  1.6  kiyohara 	isoreq.tag = (ich >> 6) & 3;
    670  1.6  kiyohara 
    671  1.6  kiyohara 	if (ioctl(fd, FW_SRSTREAM, &isoreq) < 0)
    672  1.9    cegger 		err(EX_IOERR, "%s: ioctl FW_SRSTREAM", __func__);
    673  1.6  kiyohara 
    674  1.9    cegger 	buf = (char *)malloc(RECV_NUM_PACKET * RECV_PACKET_SZ);
    675  1.9    cegger 	if (buf == NULL)
    676  1.9    cegger 		err(EX_SOFTWARE, "%s:buf malloc", __func__);
    677  1.9    cegger 	/*
    678  1.9    cegger 	 * fwdev.c seems to return EIO on error and
    679  1.9    cegger 	 * the return value of the last uiomove
    680  1.9    cegger 	 * on success.  For now, checking that the
    681  1.9    cegger 	 * return is not less than zero should be
    682  1.9    cegger 	 * sufficient.  fwdev.c::fw_read() should
    683  1.9    cegger 	 * return the total length read, not the value
    684  1.9    cegger 	 * of the last uiomove().
    685  1.9    cegger 	 */
    686  1.9    cegger 	len = read(fd, buf, RECV_NUM_PACKET * RECV_PACKET_SZ);
    687  1.9    cegger 	if (len < 0)
    688  1.9    cegger 		err(EX_IOERR, "%s: error reading from device", __func__);
    689  1.9    cegger 	ptr = (uint32_t *) buf;
    690  1.6  kiyohara 	ciph = (struct ciphdr *)(ptr + 1);
    691  1.6  kiyohara 
    692  1.9    cegger 	switch (ciph->fmt) {
    693  1.9    cegger 	case CIP_FMT_DVCR:
    694  1.9    cegger 		fprintf(stderr, "Detected DV format on input.\n");
    695  1.9    cegger 		retfn = dvrecv;
    696  1.9    cegger 		break;
    697  1.9    cegger 	case CIP_FMT_MPEG:
    698  1.9    cegger 		fprintf(stderr, "Detected MPEG TS format on input.\n");
    699  1.9    cegger 		retfn = mpegtsrecv;
    700  1.9    cegger 		break;
    701  1.9    cegger 	default:
    702  1.9    cegger 		errx(EXIT_FAILURE, "Unsupported format for receiving: fmt=0x%x",
    703  1.9    cegger 		    ciph->fmt);
    704  1.6  kiyohara 	}
    705  1.6  kiyohara 	free(buf);
    706  1.6  kiyohara 	return retfn;
    707  1.1  kiyohara }
    708  1.1  kiyohara 
    709  1.1  kiyohara int
    710  1.1  kiyohara main(int argc, char **argv)
    711  1.1  kiyohara {
    712  1.9    cegger #define MAX_BOARDS 10
    713  1.9    cegger 	uint32_t crom_buf[1024/4];
    714  1.9    cegger 	uint32_t crom_buf_hex[1024/4];
    715  1.9    cegger 	char devbase[64];
    716  1.9    cegger 	const char *device_string = "/dev/fw";
    717  1.9    cegger 	int fd = -1, ch, len=1024;
    718  1.9    cegger 	int32_t current_board = 0;
    719  1.9    cegger /*
    720  1.9    cegger  * If !command_set, then -u will display the nodes for the board.
    721  1.9    cegger  * This emulates the previous behavior when -u is passed by itself
    722  1.9    cegger  */
    723  1.9    cegger 	bool command_set = false;
    724  1.9    cegger 	bool open_needed = false;
    725  1.6  kiyohara 	long tmp;
    726  1.1  kiyohara 	struct fw_eui64 eui;
    727  1.1  kiyohara 	struct eui64 target;
    728  1.6  kiyohara 	fwmethod *recvfn = NULL;
    729  1.1  kiyohara 
    730  1.9    cegger /*
    731  1.9    cegger  * Holders for which functions
    732  1.9    cegger  * to iterate through
    733  1.9    cegger  */
    734  1.9    cegger 	bool display_board_only = false;
    735  1.9    cegger 	bool display_crom = false;
    736  1.9    cegger 	bool send_bus_reset = false;
    737  1.9    cegger 	bool display_crom_hex = false;
    738  1.9    cegger 	bool load_crom_from_file = false;
    739  1.9    cegger 	bool set_fwmem_target = false;
    740  1.9    cegger 	bool dump_topology = false;
    741  1.9    cegger 	bool dump_phy_reg = false;
    742  1.9    cegger 
    743  1.9    cegger 	int32_t priority_budget = -1;
    744  1.9    cegger 	int32_t set_root_node = -1;
    745  1.9    cegger 	int32_t set_gap_count = -1;
    746  1.9    cegger 	int32_t send_link_on = -1;
    747  1.9    cegger 	int32_t send_reset_start = -1;
    748  1.9    cegger 
    749  1.9    cegger 	char *crom_string = NULL;
    750  1.9    cegger 	char *crom_string_hex = NULL;
    751  1.9    cegger 	char *recv_data = NULL;
    752  1.9    cegger 	char *send_data = NULL;
    753  1.1  kiyohara 
    754  1.1  kiyohara 	if (argc < 2) {
    755  1.9    cegger 		for (current_board = 0; current_board < MAX_BOARDS; current_board++) {
    756  1.9    cegger 			snprintf(devbase, sizeof(devbase), "%s%d.0", device_string, current_board);
    757  1.9    cegger 			if (open_dev(&fd, devbase) < 0) {
    758  1.9    cegger 				if (current_board == 0) {
    759  1.9    cegger 					usage();
    760  1.9    cegger 					err(EX_IOERR, "%s: Error opening firewire controller #%d %s",
    761  1.9    cegger 					    __func__, current_board, devbase);
    762  1.9    cegger 				}
    763  1.9    cegger 				return EIO;
    764  1.9    cegger 			}
    765  1.9    cegger 			list_dev(fd);
    766  1.9    cegger 			close(fd);
    767  1.9    cegger 			fd = -1;
    768  1.9    cegger 		}
    769  1.1  kiyohara 	}
    770  1.9    cegger 	/*
    771  1.9    cegger 	 * Parse all command line options, then execute requested operations.
    772  1.9    cegger 	 */
    773  1.9    cegger 	while ((ch = getopt(argc, argv, "b:c:d:f:g:l:M:m:o:pR:rS:s:tu:")) != -1) {
    774  1.9    cegger 		switch (ch) {
    775  1.1  kiyohara 		case 'b':
    776  1.9    cegger 			priority_budget = strtol(optarg, NULL, 0);
    777  1.9    cegger 			if (priority_budget < 0 || priority_budget > INT32_MAX)
    778  1.9    cegger 				errx(EX_USAGE,
    779  1.9    cegger 				    "%s: priority_budget out of range: %s",
    780  1.9    cegger 				    __func__, optarg);
    781  1.9    cegger 			command_set = true;
    782  1.9    cegger 			open_needed = true;
    783  1.9    cegger 			display_board_only = false;
    784  1.1  kiyohara 			break;
    785  1.1  kiyohara 		case 'c':
    786  1.9    cegger 			crom_string = malloc(strlen(optarg)+1);
    787  1.9    cegger 			if (crom_string == NULL)
    788  1.9    cegger 				err(EX_SOFTWARE, "%s:crom_string malloc",
    789  1.9    cegger 				    __func__);
    790  1.9    cegger 			if (strtol(crom_string, NULL, 0) < 0 ||
    791  1.9    cegger 			    strtol(crom_string, NULL, 0) > MAX_BOARDS)
    792  1.9    cegger 				errx(EX_USAGE, "%s:Invalid value for node",
    793  1.9    cegger 				    __func__);
    794  1.9    cegger 			strcpy(crom_string, optarg);
    795  1.9    cegger 			display_crom = 1;
    796  1.9    cegger 			open_needed = true;
    797  1.9    cegger 			command_set = true;
    798  1.9    cegger 			display_board_only = false;
    799  1.1  kiyohara 			break;
    800  1.1  kiyohara 		case 'd':
    801  1.9    cegger 			crom_string_hex = malloc(strlen(optarg)+1);
    802  1.9    cegger 			if (crom_string_hex == NULL)
    803  1.9    cegger 				err(EX_SOFTWARE, "%s:crom_string_hex malloc",
    804  1.9    cegger 				    __func__);
    805  1.9    cegger 			strcpy(crom_string_hex, optarg);
    806  1.9    cegger 			display_crom_hex = 1;
    807  1.9    cegger 			open_needed = true;
    808  1.9    cegger 			command_set = true;
    809  1.9    cegger 			display_board_only = false;
    810  1.9    cegger 			break;
    811  1.9    cegger 		case 'f':
    812  1.9    cegger #define MAX_PHY_CONFIG 0x3f
    813  1.9    cegger 			set_root_node = strtol(optarg, NULL, 0);
    814  1.9    cegger 			if (set_root_node < 0 || set_root_node > MAX_PHY_CONFIG)
    815  1.9    cegger 				errx(EX_USAGE, "%s:set_root_node out of range",
    816  1.9    cegger 				    __func__);
    817  1.9    cegger 			open_needed = true;
    818  1.9    cegger 			command_set = true;
    819  1.9    cegger 			display_board_only = false;
    820  1.1  kiyohara 			break;
    821  1.1  kiyohara 		case 'g':
    822  1.9    cegger 			set_gap_count = strtol(optarg, NULL, 0);
    823  1.9    cegger 			if (set_gap_count < 0 || set_gap_count > MAX_PHY_CONFIG)
    824  1.9    cegger 				errx(EX_USAGE, "%s:set_gap_count out of range",
    825  1.9    cegger 				    __func__);
    826  1.9    cegger 			open_needed = true;
    827  1.9    cegger 			command_set = true;
    828  1.9    cegger 			display_board_only = false;
    829  1.1  kiyohara 			break;
    830  1.1  kiyohara 		case 'l':
    831  1.9    cegger 			load_crom_from_file = 1;
    832  1.1  kiyohara 			load_crom(optarg, crom_buf);
    833  1.9    cegger 			command_set = true;
    834  1.9    cegger 			display_board_only = false;
    835  1.1  kiyohara 			break;
    836  1.1  kiyohara 		case 'm':
    837  1.9    cegger 			set_fwmem_target = 1;
    838  1.9    cegger 			open_needed = 0;
    839  1.9    cegger 			command_set = true;
    840  1.9    cegger 			display_board_only = false;
    841  1.9    cegger 			if (eui64_hostton(optarg, &target) != 0 &&
    842  1.9    cegger 			    eui64_aton(optarg, &target) != 0)
    843  1.9    cegger 				errx(EX_USAGE, "%s: invalid target: %s",
    844  1.9    cegger 				    __func__, optarg);
    845  1.1  kiyohara 			break;
    846  1.1  kiyohara 		case 'o':
    847  1.9    cegger 			send_link_on = str2node(fd, optarg);
    848  1.9    cegger 			if (send_link_on < 0 || send_link_on > MAX_PHY_CONFIG)
    849  1.9    cegger 				errx(EX_USAGE, "%s: node out of range: %s\n",
    850  1.9    cegger 				    __func__, optarg);
    851  1.9    cegger 			open_needed = true;
    852  1.9    cegger 			command_set = true;
    853  1.9    cegger 			display_board_only = false;
    854  1.1  kiyohara 			break;
    855  1.1  kiyohara 		case 'p':
    856  1.9    cegger 			dump_phy_reg = 1;
    857  1.9    cegger 			open_needed = true;
    858  1.9    cegger 			command_set = true;
    859  1.9    cegger 			display_board_only = false;
    860  1.1  kiyohara 			break;
    861  1.1  kiyohara 		case 'r':
    862  1.9    cegger 			send_bus_reset = 1;
    863  1.9    cegger 			open_needed = true;
    864  1.9    cegger 			command_set = true;
    865  1.9    cegger 			display_board_only = false;
    866  1.1  kiyohara 			break;
    867  1.1  kiyohara 		case 's':
    868  1.9    cegger 			send_reset_start  = str2node(fd, optarg);
    869  1.9    cegger 			if (send_reset_start < 0 ||
    870  1.9    cegger 			    send_reset_start > MAX_PHY_CONFIG)
    871  1.9    cegger 				errx(EX_USAGE, "%s: node out of range: %s\n",
    872  1.9    cegger 				    __func__, optarg);
    873  1.9    cegger 			open_needed = true;
    874  1.9    cegger 			command_set = true;
    875  1.9    cegger 			display_board_only = false;
    876  1.1  kiyohara 			break;
    877  1.1  kiyohara 		case 't':
    878  1.9    cegger 			dump_topology = 1;
    879  1.9    cegger 			open_needed = true;
    880  1.9    cegger 			command_set = true;
    881  1.9    cegger 			display_board_only = false;
    882  1.1  kiyohara 			break;
    883  1.1  kiyohara 		case 'u':
    884  1.9    cegger 			if (!command_set)
    885  1.9    cegger 				display_board_only = true;
    886  1.9    cegger 			current_board = strtol(optarg, NULL, 0);
    887  1.9    cegger 			open_needed = true;
    888  1.1  kiyohara 			break;
    889  1.6  kiyohara 		case 'M':
    890  1.6  kiyohara 			switch (optarg[0]) {
    891  1.6  kiyohara 			case 'm':
    892  1.6  kiyohara 				recvfn = mpegtsrecv;
    893  1.6  kiyohara 				break;
    894  1.6  kiyohara 			case 'd':
    895  1.6  kiyohara 				recvfn = dvrecv;
    896  1.6  kiyohara 				break;
    897  1.6  kiyohara 			default:
    898  1.6  kiyohara 				errx(EX_USAGE, "unrecognized method: %s",
    899  1.6  kiyohara 				    optarg);
    900  1.6  kiyohara 			}
    901  1.9    cegger 			command_set = true;
    902  1.9    cegger 			display_board_only = false;
    903  1.6  kiyohara 			break;
    904  1.1  kiyohara 		case 'R':
    905  1.9    cegger 			recv_data = malloc(strlen(optarg)+1);
    906  1.9    cegger 			if (recv_data == NULL)
    907  1.9    cegger 				err(EX_SOFTWARE, "%s:recv_data malloc",
    908  1.9    cegger 				    __func__);
    909  1.9    cegger 			strcpy(recv_data, optarg);
    910  1.9    cegger 			open_needed = false;
    911  1.9    cegger 			command_set = true;
    912  1.9    cegger 			display_board_only = false;
    913  1.1  kiyohara 			break;
    914  1.1  kiyohara 		case 'S':
    915  1.9    cegger 			send_data = malloc(strlen(optarg)+1);
    916  1.9    cegger 			if (send_data == NULL)
    917  1.9    cegger 				err(EX_SOFTWARE, "%s:send_data malloc",
    918  1.9    cegger 				    __func__);
    919  1.9    cegger 			strcpy(send_data, optarg);
    920  1.9    cegger 			open_needed = true;
    921  1.9    cegger 			command_set = true;
    922  1.9    cegger 			display_board_only = false;
    923  1.1  kiyohara 			break;
    924  1.9    cegger 		case '?':
    925  1.1  kiyohara 		default:
    926  1.1  kiyohara 			usage();
    927  1.9    cegger 			errx(EINVAL, "%s: Unknown command line arguments",
    928  1.9    cegger 			    __func__);
    929  1.9    cegger 			return 0;
    930  1.1  kiyohara 		}
    931  1.9    cegger 	} /* end while */
    932  1.9    cegger 
    933  1.9    cegger 	/*
    934  1.9    cegger 	 * Catch the error case when the user
    935  1.9    cegger 	 * executes the command with non ''-''
    936  1.9    cegger 	 * delimited arguments.
    937  1.9    cegger 	 * Generate the usage() display and exit.
    938  1.9    cegger 	 */
    939  1.9    cegger 	if (!command_set && !display_board_only) {
    940  1.9    cegger 		usage();
    941  1.9    cegger 		errx(EINVAL, "%s: Unknown command line arguments", __func__);
    942  1.9    cegger 		return 0;
    943  1.9    cegger 	}
    944  1.9    cegger 
    945  1.9    cegger 	/*
    946  1.9    cegger 	 * If -u <bus_number> is passed, execute
    947  1.9    cegger 	 * command for that card only.
    948  1.9    cegger 	 *
    949  1.9    cegger 	 * If -u <bus_number> is not passed, execute
    950  1.9    cegger 	 * command for card 0 only.
    951  1.9    cegger 	 *
    952  1.9    cegger 	 */
    953  1.9    cegger 	if (open_needed) {
    954  1.9    cegger 		snprintf(devbase, sizeof(devbase),
    955  1.9    cegger 		    "%s%d.0", device_string, current_board);
    956  1.9    cegger 		if (open_dev(&fd, devbase) < 0) {
    957  1.9    cegger 			err(EX_IOERR,
    958  1.9    cegger 			    "%s: Error opening firewire controller #%d %s",
    959  1.9    cegger 			    __func__, current_board, devbase);
    960  1.9    cegger 		}
    961  1.9    cegger 	}
    962  1.9    cegger 	/*
    963  1.9    cegger 	 * display the nodes on this board "-u" only
    964  1.9    cegger 	*/
    965  1.9    cegger 	if (display_board_only)
    966  1.9    cegger 		list_dev(fd);
    967  1.9    cegger 
    968  1.9    cegger 	/*
    969  1.9    cegger 	 * dump_phy_reg "-p"
    970  1.9    cegger 	 */
    971  1.9    cegger 	if (dump_phy_reg)
    972  1.9    cegger 		dump_phy_registers(fd);
    973  1.9    cegger 
    974  1.9    cegger 	/*
    975  1.9    cegger 	 * send a BUS_RESET Event "-r"
    976  1.9    cegger 	 */
    977  1.9    cegger 	if (send_bus_reset)
    978  1.9    cegger 		if (ioctl(fd, FW_IBUSRST, &tmp) < 0)
    979  1.9    cegger 			err(EX_IOERR, "%s: Ioctl of bus reset failed for %s",
    980  1.9    cegger 			    __func__, devbase);
    981  1.9    cegger 	/*
    982  1.9    cegger 	 * Print out the CROM for this node "-c"
    983  1.9    cegger 	 */
    984  1.9    cegger 	if (display_crom) {
    985  1.9    cegger 		tmp = str2node(fd, crom_string);
    986  1.9    cegger 		get_crom(fd, tmp, crom_buf, len);
    987  1.9    cegger 		show_crom(crom_buf);
    988  1.9    cegger 		free(crom_string);
    989  1.9    cegger 	}
    990  1.9    cegger 	/*
    991  1.9    cegger 	 * Hex Dump the CROM for this node "-d"
    992  1.9    cegger 	 */
    993  1.9    cegger 	if (display_crom_hex) {
    994  1.9    cegger 		tmp = str2node(fd, crom_string_hex);
    995  1.9    cegger 		get_crom(fd, tmp, crom_buf_hex, len);
    996  1.9    cegger 		dump_crom(crom_buf_hex);
    997  1.9    cegger 		free(crom_string_hex);
    998  1.9    cegger 	}
    999  1.9    cegger 	/*
   1000  1.9    cegger 	 * Set Priority Budget to value for this node "-b"
   1001  1.9    cegger 	 */
   1002  1.9    cegger 	if (priority_budget >= 0)
   1003  1.9    cegger 		set_pri_req(fd, priority_budget);
   1004  1.9    cegger 
   1005  1.9    cegger 	/*
   1006  1.9    cegger 	 * Explicitly set the root node of this bus to value "-f"
   1007  1.9    cegger 	 */
   1008  1.9    cegger 	if (set_root_node >= 0)
   1009  1.9    cegger 		send_phy_config(fd, set_root_node, -1);
   1010  1.9    cegger 
   1011  1.9    cegger 	/*
   1012  1.9    cegger 	 * Set the gap count for this card/bus  "-g"
   1013  1.9    cegger 	 */
   1014  1.9    cegger 	if (set_gap_count >= 0)
   1015  1.9    cegger 		send_phy_config(fd, -1, set_gap_count);
   1016  1.9    cegger 
   1017  1.9    cegger 	/*
   1018  1.9    cegger 	 * Load a CROM from a file "-l"
   1019  1.9    cegger 	 */
   1020  1.9    cegger 	if (load_crom_from_file)
   1021  1.9    cegger 		show_crom(crom_buf);
   1022  1.9    cegger 	/*
   1023  1.9    cegger 	 * Set the fwmem target for a node to argument "-m"
   1024  1.9    cegger 	 */
   1025  1.9    cegger 	if (set_fwmem_target) {
   1026  1.9    cegger 		eui.hi = ntohl(*(uint32_t*)&(target.octet[0]));
   1027  1.9    cegger 		eui.lo = ntohl(*(uint32_t*)&(target.octet[4]));
   1028  1.9    cegger 		sysctl_set_int("hw.fwmem.eui64_hi", eui.hi);
   1029  1.9    cegger 		sysctl_set_int("hw.fwmem.eui64_lo", eui.lo);
   1030  1.9    cegger 	}
   1031  1.9    cegger 
   1032  1.9    cegger 	/*
   1033  1.9    cegger 	 * Send a link on to this board/bus "-o"
   1034  1.9    cegger 	 */
   1035  1.9    cegger 	if (send_link_on >= 0)
   1036  1.9    cegger 		link_on(fd, send_link_on);
   1037  1.9    cegger 
   1038  1.9    cegger 	/*
   1039  1.9    cegger 	 * Send a reset start to this board/bus "-s"
   1040  1.9    cegger 	 */
   1041  1.9    cegger 	if (send_reset_start >= 0)
   1042  1.9    cegger 		reset_start(fd, send_reset_start);
   1043  1.9    cegger 
   1044  1.9    cegger 	/*
   1045  1.9    cegger 	 * Dump the node topology for this board/bus "-t"
   1046  1.9    cegger 	 */
   1047  1.9    cegger 	if (dump_topology)
   1048  1.9    cegger 		show_topology_map(fd);
   1049  1.9    cegger 
   1050  1.9    cegger 	/*
   1051  1.9    cegger 	 * Recieve data file from node "-R"
   1052  1.9    cegger 	 */
   1053  1.9    cegger #define TAG	(1<<6)
   1054  1.9    cegger #define CHANNEL	63
   1055  1.9    cegger 	if (recv_data != NULL){
   1056  1.9    cegger 		if (recvfn == NULL) { /* guess... */
   1057  1.9    cegger 			recvfn = detect_recv_fn(fd, TAG | CHANNEL);
   1058  1.9    cegger 			close(fd);
   1059  1.9    cegger 			fd = -1;
   1060  1.9    cegger 		}
   1061  1.9    cegger 		snprintf(devbase, sizeof(devbase), "%s%d.0",
   1062  1.9    cegger 		    device_string, current_board);
   1063  1.9    cegger 		if (open_dev(&fd, devbase) < 0)
   1064  1.9    cegger 			err(EX_IOERR, "%s: Error opening firewire controller #%d %s in recv_data\n",
   1065  1.9    cegger 			    __func__, current_board, devbase);
   1066  1.9    cegger 		(*recvfn)(fd, recv_data, TAG | CHANNEL, -1);
   1067  1.9    cegger 		free(recv_data);
   1068  1.9    cegger 	}
   1069  1.9    cegger 
   1070  1.9    cegger 	/*
   1071  1.9    cegger 	 * Send data file to node "-S"
   1072  1.9    cegger 	 */
   1073  1.9    cegger 	if (send_data != NULL){
   1074  1.9    cegger 		dvsend(fd, send_data, TAG | CHANNEL, -1);
   1075  1.9    cegger 		free(send_data);
   1076  1.9    cegger 	}
   1077  1.9    cegger 
   1078  1.9    cegger 	if (fd > 0) {
   1079  1.9    cegger 		close(fd);
   1080  1.9    cegger 		fd = -1;
   1081  1.9    cegger 	}
   1082  1.1  kiyohara 	return 0;
   1083  1.1  kiyohara }
   1084