3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/usr.sbin/fwcontrol/fwcontrol.c,v 1.1.2.8 2003/05/01 06:26:35 simokawa Exp $
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <bus/firewire/firewire.h>
43 #include <bus/firewire/iec13213.h>
45 #include <netinet/in.h>
53 extern int dvrecv(int, char *, char, int);
54 extern int dvsend(int, char *, char, int);
60 "fwcontrol [-g gap_count] [-o node] [-b pri_req] [-c node]"
61 " [-r] [-t] [-d node] [-l file] [-R file] [-S file]\n"
62 "\t-g: broadcast gap_count by phy_config packet\n"
63 "\t-o: send link-on packet to the node\n"
64 "\t-s: write RESET_START register on the node\n"
65 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
66 "\t-c: read configuration ROM\n"
68 "\t-t: read topology map\n"
69 "\t-d: hex dump of configuration ROM\n"
70 "\t-l: load and parse hex dump file of configuration ROM\n"
71 "\t-R: Receive DV stream\n"
72 "\t-S: Send DV stream\n");
76 static struct fw_devlstreq *
79 struct fw_devlstreq *data;
81 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
84 if( ioctl(fd, FW_GDEVLST, data) < 0) {
93 struct fw_devlstreq *data;
94 struct fw_devinfo *devinfo;
98 printf("%d devices (info_len=%d)\n", data->n, data->info_len);
99 printf("node EUI64 status\n");
100 for (i = 0; i < data->info_len; i++) {
101 devinfo = &data->dev[i];
102 printf("%4d %08x%08x %6d\n",
103 (devinfo->status || i == 0) ? devinfo->dst : -1,
113 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int readmode, u_int32_t data)
115 struct fw_asyreq *asyreq;
118 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
119 asyreq->req.len = 16;
121 asyreq->req.type = FWASREQNODE;
122 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
124 asyreq->req.type = FWASREQEUI;
125 asyreq->req.dst.eui = eui;
127 asyreq->pkt.mode.rreqq.tlrt = 0;
129 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
131 asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
133 asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
134 asyreq->pkt.mode.rreqq.dest_lo = addr_lo;
136 qld = (u_int32_t *)&asyreq->pkt;
138 asyreq->pkt.mode.wreqq.data = data;
140 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
152 send_phy_config(int fd, int root_node, int gap_count)
154 struct fw_asyreq *asyreq;
156 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
157 asyreq->req.len = 12;
158 asyreq->req.type = FWASREQNODE;
159 asyreq->pkt.mode.ld[0] = 0;
160 asyreq->pkt.mode.ld[1] = 0;
161 asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
163 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
165 asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
166 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
168 printf("send phy_config root_node=%d gap_count=%d\n",
169 root_node, gap_count);
171 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
177 send_link_on(int fd, int node)
179 struct fw_asyreq *asyreq;
181 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
182 asyreq->req.len = 12;
183 asyreq->req.type = FWASREQNODE;
184 asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
185 asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
186 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
188 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
194 reset_start(int fd, int node)
196 struct fw_asyreq *asyreq;
198 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
199 asyreq->req.len = 16;
200 asyreq->req.type = FWASREQNODE;
201 asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
202 asyreq->pkt.mode.wreqq.tlrt = 0;
203 asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;
205 asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
206 asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
208 asyreq->pkt.mode.wreqq.data = htonl(0x1);
210 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
216 set_pri_req(int fd, u_int32_t pri_req)
218 struct fw_devlstreq *data;
219 struct fw_devinfo *devinfo;
220 u_int32_t max, reg, old;
224 #define BUGET_REG 0xf0000218
225 for (i = 0; i < data->info_len; i++) {
226 devinfo = &data->dev[i];
227 if (!devinfo->status)
229 reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
230 printf("%d %08x:%08x, %08x",
231 devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg);
234 max = (reg & 0x3f00) >> 8;
237 printf(" 0x%x -> 0x%x\n", old, pri_req);
238 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
247 parse_bus_info_block(u_int32_t *p, int info_len)
251 for (i = 0; i < info_len; i++) {
252 printf("bus_info%d: 0x%08x\n", i, *p++);
257 get_crom(int fd, int node, void *crom_buf, int len)
259 struct fw_crom_buf buf;
261 struct fw_devlstreq *data;
265 for (i = 0; i < data->info_len; i++) {
266 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
269 if (i == data->info_len)
270 errx(1, "no such node %d.", node);
272 errx(1, "node %d is myself.", node);
274 buf.eui = data->dev[i].eui;
279 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
287 show_crom(u_int32_t *crom_buf)
290 struct crom_context cc;
293 static const char *key_types = "ICLD";
295 struct csrdirectory *dir;
299 printf("first quad: 0x%08x ", *crom_buf);
300 hdr = (struct csrhdr *)crom_buf;
301 if (hdr->info_len == 1) {
303 reg = (struct csrreg *)hdr;
304 printf("verndor ID: 0x%06x\n", reg->val);
307 printf("info_len=%d crc_len=%d crc=0x%04x",
308 hdr->info_len, hdr->crc_len, hdr->crc);
309 crc = crom_crc(crom_buf+1, hdr->crc_len);
314 parse_bus_info_block(crom_buf+1, hdr->info_len);
316 crom_init_context(&cc, crom_buf);
317 dir = cc.stack[0].dir;
318 printf("root_directory: len=0x%04x(%d) crc=0x%04x",
319 dir->crc_len, dir->crc_len, dir->crc);
320 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
325 if (dir->crc_len < 1)
327 while (cc.depth >= 0) {
328 desc = crom_desc(&cc, info, sizeof(info));
330 for (i = 0; i < cc.depth; i++)
332 printf("%02x(%c:%02x) %06x %s: %s\n",
334 key_types[(reg->key & CSRTYPE_MASK)>>6],
335 reg->key & CSRKEY_MASK, reg->val,
341 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
344 dump_crom(u_int32_t *p)
348 for (i = 0; i < len/(4*8); i ++) {
350 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
356 load_crom(char *filename, u_int32_t *p)
361 if ((file = fopen(filename, "r")) == NULL)
363 for (i = 0; i < len/(4*8); i ++) {
364 fscanf(file, DUMP_FORMAT,
365 p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
371 show_topology_map(int fd)
373 struct fw_topology_map *tmap;
374 union fw_self_id sid;
376 static const char *port_status[] = {" ", "-", "P", "C"};
377 static const char *pwr_class[] = {" 0W", "15W", "30W", "45W",
378 "-1W", "-2W", "-5W", "-9W"};
379 static const char *speed[] = {"S100", "S200", "S400", "S800"};
380 tmap = malloc(sizeof(struct fw_topology_map));
383 if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
386 printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
387 tmap->crc_len, tmap->generation,
388 tmap->node_count, tmap->self_id_count);
389 printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
391 for (i = 0; i < tmap->crc_len - 2; i++) {
392 sid = tmap->self_id[i];
394 printf("%02d sequel packet\n", sid.p0.phy_id);
397 printf("%02d %2d %2d %4s %d %d %3s"
402 speed[sid.p0.phy_speed],
405 pwr_class[sid.p0.power_class],
406 port_status[sid.p0.port0],
407 port_status[sid.p0.port1],
408 port_status[sid.p0.port2],
409 sid.p0.initiated_reset,
417 main(int argc, char **argv)
419 char devicename[256];
420 u_int32_t crom_buf[1024/4];
421 int fd, i, tmp, ch, len=1024;
423 for (i = 0; i < 4; i++) {
424 snprintf(devicename, sizeof(devicename), "/dev/fw%d", i);
425 if ((fd = open(devicename, O_RDWR)) >= 0)
435 while ((ch = getopt(argc, argv, "g:o:s:b:rtc:d:l:R:S:")) != -1)
438 tmp = strtol(optarg, NULL, 0);
439 send_phy_config(fd, -1, tmp);
442 tmp = strtol(optarg, NULL, 0);
443 send_link_on(fd, tmp);
446 tmp = strtol(optarg, NULL, 0);
447 reset_start(fd, tmp);
450 tmp = strtol(optarg, NULL, 0);
451 set_pri_req(fd, tmp);
454 if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
458 show_topology_map(fd);
461 tmp = strtol(optarg, NULL, 0);
462 get_crom(fd, tmp, crom_buf, len);
466 tmp = strtol(optarg, NULL, 0);
467 get_crom(fd, tmp, crom_buf, len);
471 load_crom(optarg, crom_buf);
477 dvrecv(fd, optarg, TAG | CHANNEL, -1);
480 dvsend(fd, optarg, TAG | CHANNEL, -1);