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