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