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