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 "eui64.h" 44 #include "firewire.h" 45 #include "iec13213.h" 46 #include "fwphyreg.h" 47 #include "iec68113.h" 48 #include <stdint.h> 49 #include <netinet/in.h> 50 #include <fcntl.h> 51 #include <stdio.h> 52 #include <err.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <stdint.h> 56 #else 57 #include <sys/param.h> 58 #include <sys/malloc.h> 59 #include <sys/types.h> 60 #include <sys/sysctl.h> 61 #include <sys/socket.h> 62 #include <sys/ioctl.h> 63 #include <sys/errno.h> 64 #include <sys/eui64.h> 65 #include <dev/firewire/firewire.h> 66 #include <dev/firewire/iec13213.h> 67 #include <dev/firewire/fwphyreg.h> 68 #include <dev/firewire/iec68113.h> 69 70 #include <netinet/in.h> 71 #include <fcntl.h> 72 #include <stdio.h> 73 #include <err.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <sysexits.h> 77 #endif 78 #include <unistd.h> 79 #include "fwmethods.h" 80 81 #ifndef __HAIKU__ 82 static void sysctl_set_int(const char *, int); 83 #endif 84 85 static void 86 usage(void) 87 { 88 fprintf(stderr, 89 "fwcontrol [-u bus_num] [-rt] [-g gap_count] [-o node] " 90 "[-b pri_req] [-c node] [-d node] [-l file] " 91 #ifdef __HAIKU__ 92 "[-R file] [-S file] \n" 93 #else 94 "[-R file] [-S file] [-m target]\n" 95 #endif 96 97 "\t-u: specify bus number\n" 98 "\t-g: broadcast gap_count by phy_config packet\n" 99 "\t-o: send link-on packet to the node\n" 100 "\t-s: write RESET_START register on the node\n" 101 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n" 102 "\t-c: read configuration ROM\n" 103 "\t-r: bus reset\n" 104 "\t-t: read topology map\n" 105 "\t-d: hex dump of configuration ROM\n" 106 "\t-l: load and parse hex dump file of configuration ROM\n" 107 "\t-R: Receive DV or MPEG TS stream\n" 108 #ifdef __HAIKU__ 109 "\t-S: Send DV stream\n"); 110 #else 111 "\t-S: Send DV stream\n" 112 "\t-m: set fwmem target\n"); 113 #endif 114 115 exit(EX_USAGE); 116 } 117 118 static void 119 fweui2eui64(const struct fw_eui64 *fweui, struct eui64 *eui) 120 { 121 *(u_int32_t*)&(eui->octet[0]) = htonl(fweui->hi); 122 *(u_int32_t*)&(eui->octet[4]) = htonl(fweui->lo); 123 } 124 125 static struct fw_devlstreq * 126 get_dev(int fd) 127 { 128 struct fw_devlstreq *data; 129 130 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq)); 131 if (data == NULL) 132 err(1, "malloc"); 133 if( ioctl(fd, FW_GDEVLST, data) < 0) { 134 err(1, "ioctl"); 135 } 136 return data; 137 } 138 139 static int 140 str2node(int fd, const char *nodestr) 141 { 142 struct eui64 eui, tmpeui; 143 struct fw_devlstreq *data; 144 char *endptr; 145 int i, node; 146 147 if (nodestr == '\0') 148 return (-1); 149 150 /* 151 * Deal with classic node specifications. 152 */ 153 node = strtol(nodestr, &endptr, 0); 154 if (*endptr == '\0') 155 goto gotnode; 156 157 /* 158 * Try to get an eui and match it against available nodes. 159 */ 160 #ifdef __HAIKU__ 161 if (eui64_aton(nodestr, &eui) != 0) 162 #else 163 if (eui64_hostton(nodestr, &eui) != 0 && eui64_aton(nodestr, &eui) != 0) 164 #endif 165 return (-1); 166 167 data = get_dev(fd); 168 169 for (i = 0; i < data->info_len; i++) { 170 fweui2eui64(&data->dev[i].eui, &tmpeui); 171 if (memcmp(&eui, &tmpeui, sizeof(struct eui64)) == 0) { 172 node = data->dev[i].dst; 173 goto gotnode; 174 } 175 } 176 if (i >= data->info_len) 177 return (-1); 178 179 gotnode: 180 if (node < 0 || node > 63) 181 return (-1); 182 else 183 return (node); 184 } 185 186 static void 187 list_dev(int fd) 188 { 189 struct fw_devlstreq *data; 190 struct fw_devinfo *devinfo; 191 struct eui64 eui; 192 char addr[EUI64_SIZ]; 193 int i; 194 195 data = get_dev(fd); 196 printf("%d devices (info_len=%d)\n", data->n, data->info_len); 197 printf("node EUI64 status\n"); 198 for (i = 0; i < data->info_len; i++) { 199 devinfo = &data->dev[i]; 200 fweui2eui64(&devinfo->eui, &eui); 201 eui64_ntoa(&eui, addr, sizeof(addr)); 202 printf("%4d %s %6d\n", 203 (devinfo->status || i == 0) ? devinfo->dst : -1, 204 addr, 205 devinfo->status 206 ); 207 } 208 free((void *)data); 209 } 210 211 static u_int32_t 212 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int readmode, u_int32_t data) 213 { 214 struct fw_asyreq *asyreq; 215 u_int32_t *qld, res; 216 217 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); 218 asyreq->req.len = 16; 219 #if 0 220 asyreq->req.type = FWASREQNODE; 221 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node; 222 #else 223 asyreq->req.type = FWASREQEUI; 224 asyreq->req.dst.eui = eui; 225 #endif 226 asyreq->pkt.mode.rreqq.tlrt = 0; 227 if (readmode) 228 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ; 229 else 230 asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ; 231 232 asyreq->pkt.mode.rreqq.dest_hi = 0xffff; 233 asyreq->pkt.mode.rreqq.dest_lo = addr_lo; 234 235 qld = (u_int32_t *)&asyreq->pkt; 236 if (!readmode) 237 asyreq->pkt.mode.wreqq.data = data; 238 239 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) { 240 err(1, "ioctl"); 241 } 242 res = qld[3]; 243 free(asyreq); 244 if (readmode) 245 return ntohl(res); 246 else 247 return 0; 248 } 249 250 static void 251 send_phy_config(int fd, int root_node, int gap_count) 252 { 253 struct fw_asyreq *asyreq; 254 255 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); 256 asyreq->req.len = 12; 257 asyreq->req.type = FWASREQNODE; 258 asyreq->pkt.mode.ld[0] = 0; 259 asyreq->pkt.mode.ld[1] = 0; 260 asyreq->pkt.mode.common.tcode = FWTCODE_PHY; 261 if (root_node >= 0) 262 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 263 if (gap_count >= 0) 264 asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 265 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1]; 266 267 printf("send phy_config root_node=%d gap_count=%d\n", 268 root_node, gap_count); 269 270 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 271 err(1, "ioctl"); 272 free(asyreq); 273 } 274 275 static void 276 send_link_on(int fd, int node) 277 { 278 struct fw_asyreq *asyreq; 279 280 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); 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(1, "ioctl"); 289 free(asyreq); 290 } 291 292 static void 293 reset_start(int fd, int node) 294 { 295 struct fw_asyreq *asyreq; 296 297 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); 298 asyreq->req.len = 16; 299 asyreq->req.type = FWASREQNODE; 300 asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f); 301 asyreq->pkt.mode.wreqq.tlrt = 0; 302 asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ; 303 304 asyreq->pkt.mode.wreqq.dest_hi = 0xffff; 305 asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 306 307 asyreq->pkt.mode.wreqq.data = htonl(0x1); 308 309 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 310 err(1, "ioctl"); 311 free(asyreq); 312 } 313 314 static void 315 set_pri_req(int fd, u_int32_t pri_req) 316 { 317 struct fw_devlstreq *data; 318 struct fw_devinfo *devinfo; 319 struct eui64 eui; 320 char addr[EUI64_SIZ]; 321 u_int32_t max, reg, old; 322 int i; 323 324 data = get_dev(fd); 325 #define BUGET_REG 0xf0000218 326 for (i = 0; i < data->info_len; i++) { 327 devinfo = &data->dev[i]; 328 if (!devinfo->status) 329 continue; 330 reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0); 331 fweui2eui64(&devinfo->eui, &eui); 332 eui64_ntoa(&eui, addr, sizeof(addr)); 333 printf("%d %s, %08x", 334 devinfo->dst, addr, reg); 335 if (reg > 0) { 336 old = (reg & 0x3f); 337 max = (reg & 0x3f00) >> 8; 338 if (pri_req > max) 339 pri_req = max; 340 printf(" 0x%x -> 0x%x\n", old, pri_req); 341 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req); 342 } else { 343 printf("\n"); 344 } 345 } 346 free((void *)data); 347 } 348 349 static void 350 parse_bus_info_block(u_int32_t *p) 351 { 352 char addr[EUI64_SIZ]; 353 struct bus_info *bi; 354 struct eui64 eui; 355 356 bi = (struct bus_info *)p; 357 fweui2eui64(&bi->eui64, &eui); 358 eui64_ntoa(&eui, addr, sizeof(addr)); 359 printf("bus_name: 0x%04x\n" 360 "irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n" 361 "cyc_clk_acc:%d max_rec:%d max_rom:%d\n" 362 "generation:%d link_spd:%d\n" 363 "EUI64: %s\n", 364 bi->bus_name, 365 bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc, 366 bi->cyc_clk_acc, bi->max_rec, bi->max_rom, 367 bi->generation, bi->link_spd, 368 addr); 369 } 370 371 static int 372 get_crom(int fd, int node, void *crom_buf, int len) 373 { 374 struct fw_crom_buf buf; 375 int i, error; 376 struct fw_devlstreq *data; 377 378 data = get_dev(fd); 379 380 for (i = 0; i < data->info_len; i++) { 381 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0) 382 break; 383 } 384 if (i == data->info_len) 385 errx(1, "no such node %d.", node); 386 else 387 buf.eui = data->dev[i].eui; 388 free((void *)data); 389 390 buf.len = len; 391 buf.ptr = crom_buf; 392 bzero(crom_buf, len); 393 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) { 394 err(1, "ioctl"); 395 } 396 397 return error; 398 } 399 400 static void 401 show_crom(u_int32_t *crom_buf) 402 { 403 int i; 404 struct crom_context cc; 405 char *desc, info[256]; 406 static const char *key_types = "ICLD"; 407 struct csrreg *reg; 408 struct csrdirectory *dir; 409 struct csrhdr *hdr; 410 u_int16_t crc; 411 412 printf("first quad: 0x%08x ", *crom_buf); 413 if (crom_buf[0] == 0) { 414 printf("(Invalid Configuration ROM)\n"); 415 return; 416 } 417 hdr = (struct csrhdr *)crom_buf; 418 if (hdr->info_len == 1) { 419 /* minimum ROM */ 420 reg = (struct csrreg *)hdr; 421 printf("verndor ID: 0x%06x\n", reg->val); 422 return; 423 } 424 printf("info_len=%d crc_len=%d crc=0x%04x", 425 hdr->info_len, hdr->crc_len, hdr->crc); 426 crc = crom_crc(crom_buf+1, hdr->crc_len); 427 if (crc == hdr->crc) 428 printf("(OK)\n"); 429 else 430 printf("(NG)\n"); 431 parse_bus_info_block(crom_buf+1); 432 433 crom_init_context(&cc, crom_buf); 434 dir = cc.stack[0].dir; 435 if (!dir) { 436 printf("no root directory - giving up\n"); 437 return; 438 } 439 printf("root_directory: len=0x%04x(%d) crc=0x%04x", 440 dir->crc_len, dir->crc_len, dir->crc); 441 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len); 442 if (crc == dir->crc) 443 printf("(OK)\n"); 444 else 445 printf("(NG)\n"); 446 if (dir->crc_len < 1) 447 return; 448 while (cc.depth >= 0) { 449 desc = crom_desc(&cc, info, sizeof(info)); 450 reg = crom_get(&cc); 451 for (i = 0; i < cc.depth; i++) 452 printf("\t"); 453 printf("%02x(%c:%02x) %06x %s: %s\n", 454 reg->key, 455 key_types[(reg->key & CSRTYPE_MASK)>>6], 456 reg->key & CSRKEY_MASK, reg->val, 457 desc, info); 458 crom_next(&cc); 459 } 460 } 461 462 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n" 463 464 static void 465 dump_crom(u_int32_t *p) 466 { 467 int len=1024, i; 468 469 for (i = 0; i < len/(4*8); i ++) { 470 printf(DUMP_FORMAT, 471 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 472 p += 8; 473 } 474 } 475 476 static void 477 load_crom(char *filename, u_int32_t *p) 478 { 479 FILE *file; 480 int len=1024, i; 481 482 if ((file = fopen(filename, "r")) == NULL) 483 err(1, "load_crom"); 484 for (i = 0; i < len/(4*8); i ++) { 485 fscanf(file, DUMP_FORMAT, 486 p, p+1, p+2, p+3, p+4, p+5, p+6, p+7); 487 p += 8; 488 } 489 } 490 491 static void 492 show_topology_map(int fd) 493 { 494 struct fw_topology_map *tmap; 495 union fw_self_id sid; 496 int i; 497 static const char *port_status[] = {" ", "-", "P", "C"}; 498 static const char *pwr_class[] = {" 0W", "15W", "30W", "45W", 499 "-1W", "-2W", "-5W", "-9W"}; 500 static const char *speed[] = {"S100", "S200", "S400", "S800"}; 501 tmap = malloc(sizeof(struct fw_topology_map)); 502 if (tmap == NULL) 503 return; 504 if (ioctl(fd, FW_GTPMAP, tmap) < 0) { 505 err(1, "ioctl"); 506 } 507 printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n", 508 tmap->crc_len, tmap->generation, 509 tmap->node_count, tmap->self_id_count); 510 printf("id link gap_cnt speed delay cIRM power port0 port1 port2" 511 " ini more\n"); 512 for (i = 0; i < tmap->crc_len - 2; i++) { 513 sid = tmap->self_id[i]; 514 if (sid.p0.sequel) { 515 printf("%02d sequel packet\n", sid.p0.phy_id); 516 continue; 517 } 518 printf("%02d %2d %2d %4s %d %d %3s" 519 " %s %s %s %d %d\n", 520 sid.p0.phy_id, 521 sid.p0.link_active, 522 sid.p0.gap_count, 523 speed[sid.p0.phy_speed], 524 sid.p0.phy_delay, 525 sid.p0.contender, 526 pwr_class[sid.p0.power_class], 527 port_status[sid.p0.port0], 528 port_status[sid.p0.port1], 529 port_status[sid.p0.port2], 530 sid.p0.initiated_reset, 531 sid.p0.more_packets 532 ); 533 } 534 free(tmap); 535 } 536 537 static void 538 read_phy_registers(int fd, u_int8_t *buf, int offset, int len) 539 { 540 struct fw_reg_req_t reg; 541 int i; 542 543 for (i = 0; i < len; i++) { 544 reg.addr = offset + i; 545 if (ioctl(fd, FWOHCI_RDPHYREG, ®) < 0) 546 err(1, "ioctl"); 547 buf[i] = (u_int8_t) reg.data; 548 printf("0x%02x ", reg.data); 549 } 550 printf("\n"); 551 } 552 553 static void 554 read_phy_page(int fd, u_int8_t *buf, int page, int port) 555 { 556 struct fw_reg_req_t reg; 557 558 reg.addr = 0x7; 559 reg.data = ((page & 7) << 5) | (port & 0xf); 560 if (ioctl(fd, FWOHCI_WRPHYREG, ®) < 0) 561 err(1, "ioctl"); 562 read_phy_registers(fd, buf, 8, 8); 563 } 564 565 static void 566 dump_phy_registers(int fd) 567 { 568 struct phyreg_base b; 569 struct phyreg_page0 p; 570 struct phyreg_page1 v; 571 int i; 572 573 printf("=== base register ===\n"); 574 read_phy_registers(fd, (u_int8_t *)&b, 0, 8); 575 printf( 576 "Physical_ID:%d R:%d CPS:%d\n" 577 "RHB:%d IBR:%d Gap_Count:%d\n" 578 "Extended:%d Num_Ports:%d\n" 579 "PHY_Speed:%d Delay:%d\n" 580 "LCtrl:%d C:%d Jitter:%d Pwr_Class:%d\n" 581 "WDIE:%d ISBR:%d CTOI:%d CPSI:%d STOI:%d PEI:%d EAA:%d EMC:%d\n" 582 "Max_Legacy_SPD:%d BLINK:%d Bridge:%d\n" 583 "Page_Select:%d Port_Select%d\n", 584 b.phy_id, b.r, b.cps, 585 b.rhb, b.ibr, b.gap_count, 586 b.extended, b.num_ports, 587 b.phy_speed, b.delay, 588 b.lctrl, b.c, b.jitter, b.pwr_class, 589 b.wdie, b.isbr, b.ctoi, b.cpsi, b.stoi, b.pei, b.eaa, b.emc, 590 b.legacy_spd, b.blink, b.bridge, 591 b.page_select, b.port_select 592 ); 593 594 for (i = 0; i < b.num_ports; i ++) { 595 printf("\n=== page 0 port %d ===\n", i); 596 read_phy_page(fd, (u_int8_t *)&p, 0, i); 597 printf( 598 "Astat:%d BStat:%d Ch:%d Con:%d RXOK:%d Dis:%d\n" 599 "Negotiated_speed:%d PIE:%d Fault:%d Stanby_fault:%d Disscrm:%d B_Only:%d\n" 600 "DC_connected:%d Max_port_speed:%d LPP:%d Cable_speed:%d\n" 601 "Connection_unreliable:%d Beta_mode:%d\n" 602 "Port_error:0x%x\n" 603 "Loop_disable:%d In_standby:%d Hard_disable:%d\n", 604 p.astat, p.bstat, p.ch, p.con, p.rxok, p.dis, 605 p.negotiated_speed, p.pie, p.fault, p.stanby_fault, p.disscrm, p.b_only, 606 p.dc_connected, p.max_port_speed, p.lpp, p.cable_speed, 607 p.connection_unreliable, p.beta_mode, 608 p.port_error, 609 p.loop_disable, p.in_standby, p.hard_disable 610 ); 611 } 612 printf("\n=== page 1 ===\n"); 613 read_phy_page(fd, (u_int8_t *)&v, 1, 0); 614 printf( 615 "Compliance:%d\n" 616 "Vendor_ID:0x%06x\n" 617 "Product_ID:0x%06x\n", 618 v.compliance, 619 (v.vendor_id[0] << 16) | (v.vendor_id[1] << 8) | v.vendor_id[2], 620 (v.product_id[0] << 16) | (v.product_id[1] << 8) | v.product_id[2] 621 ); 622 } 623 624 static void 625 open_dev(int *fd, char *devbase) 626 { 627 char name[256]; 628 int i; 629 630 if (*fd < 0) { 631 for (i = 0; i < 4; i++) { 632 snprintf(name, sizeof(name), "%s.%d", devbase, i); 633 if ((*fd = open(name, O_RDWR)) >= 0) 634 break; 635 } 636 if (*fd < 0) 637 err(1, "open"); 638 639 } 640 } 641 642 #ifndef __HAIKU__ 643 static void 644 sysctl_set_int(const char *name, int val) 645 { 646 if (sysctlbyname(name, NULL, NULL, &val, sizeof(int)) < 0) 647 err(1, "sysctl %s failed.", name); 648 } 649 #endif 650 651 static fwmethod * 652 detect_recv_fn(int fd, char ich) 653 { 654 char *buf; 655 struct fw_isochreq isoreq; 656 struct fw_isobufreq bufreq; 657 int len; 658 u_int32_t *ptr; 659 struct ciphdr *ciph; 660 fwmethod *retfn; 661 662 bufreq.rx.nchunk = 8; 663 bufreq.rx.npacket = 16; 664 bufreq.rx.psize = 1024; 665 bufreq.tx.nchunk = 0; 666 bufreq.tx.npacket = 0; 667 bufreq.tx.psize = 0; 668 669 if (ioctl(fd, FW_SSTBUF, &bufreq) < 0) 670 err(1, "ioctl FW_SSTBUF"); 671 672 isoreq.ch = ich & 0x3f; 673 isoreq.tag = (ich >> 6) & 3; 674 675 if (ioctl(fd, FW_SRSTREAM, &isoreq) < 0) 676 err(1, "ioctl FW_SRSTREAM"); 677 678 buf = (char *)malloc(1024*16); 679 len = read(fd, buf, 1024*16); 680 ptr = (u_int32_t *) buf; 681 ciph = (struct ciphdr *)(ptr + 1); 682 683 switch(ciph->fmt) { 684 case CIP_FMT_DVCR: 685 fprintf(stderr, "Detected DV format on input.\n"); 686 retfn = dvrecv; 687 break; 688 case CIP_FMT_MPEG: 689 fprintf(stderr, "Detected MPEG TS format on input.\n"); 690 retfn = mpegtsrecv; 691 break; 692 default: 693 errx(1, "Unsupported format for receiving: fmt=0x%x", ciph->fmt); 694 } 695 free(buf); 696 return retfn; 697 } 698 699 int 700 main(int argc, char **argv) 701 { 702 u_int32_t crom_buf[1024/4]; 703 #ifdef __HAIKU__ 704 char devbase[1024] = "/dev/fw"; 705 #else 706 char devbase[1024] = "/dev/fw0"; 707 #endif 708 int fd, ch, len=1024; 709 long tmp; 710 struct fw_eui64 eui; 711 struct eui64 target; 712 fwmethod *recvfn = NULL; 713 714 fd = -1; 715 716 if (argc < 2) { 717 open_dev(&fd, devbase); 718 list_dev(fd); 719 } 720 721 #ifdef __HAIKU__ 722 while ((ch = getopt(argc, argv, "M:g:o:s:b:prtc:d:l:u:R:S:")) != -1) 723 #else 724 while ((ch = getopt(argc, argv, "M:g:m:o:s:b:prtc:d:l:u:R:S:")) != -1) 725 #endif 726 switch(ch) { 727 case 'b': 728 tmp = strtol(optarg, NULL, 0); 729 if (tmp < 0 || tmp > (long)0xffffffff) 730 errx(EX_USAGE, "invalid number: %s", optarg); 731 open_dev(&fd, devbase); 732 set_pri_req(fd, tmp); 733 break; 734 case 'c': 735 open_dev(&fd, devbase); 736 tmp = str2node(fd, optarg); 737 get_crom(fd, tmp, crom_buf, len); 738 show_crom(crom_buf); 739 break; 740 case 'd': 741 open_dev(&fd, devbase); 742 tmp = str2node(fd, optarg); 743 get_crom(fd, tmp, crom_buf, len); 744 dump_crom(crom_buf); 745 break; 746 case 'g': 747 tmp = strtol(optarg, NULL, 0); 748 open_dev(&fd, devbase); 749 send_phy_config(fd, -1, tmp); 750 break; 751 case 'l': 752 load_crom(optarg, crom_buf); 753 show_crom(crom_buf); 754 break; 755 #ifndef __HAIKU__ 756 case 'm': 757 if (eui64_hostton(optarg, &target) != 0 && 758 eui64_aton(optarg, &target) != 0) 759 errx(EX_USAGE, "invalid target: %s", optarg); 760 eui.hi = ntohl(*(u_int32_t*)&(target.octet[0])); 761 eui.lo = ntohl(*(u_int32_t*)&(target.octet[4])); 762 sysctl_set_int("hw.firewire.fwmem.eui64_hi", eui.hi); 763 sysctl_set_int("hw.firewire.fwmem.eui64_lo", eui.lo); 764 break; 765 #endif 766 case 'o': 767 open_dev(&fd, devbase); 768 tmp = str2node(fd, optarg); 769 send_link_on(fd, tmp); 770 break; 771 case 'p': 772 open_dev(&fd, devbase); 773 dump_phy_registers(fd); 774 break; 775 case 'r': 776 open_dev(&fd, devbase); 777 if(ioctl(fd, FW_IBUSRST, &tmp) < 0) 778 err(1, "ioctl"); 779 break; 780 case 's': 781 open_dev(&fd, devbase); 782 tmp = str2node(fd, optarg); 783 reset_start(fd, tmp); 784 break; 785 case 't': 786 open_dev(&fd, devbase); 787 show_topology_map(fd); 788 break; 789 case 'u': 790 tmp = strtol(optarg, NULL, 0); 791 snprintf(devbase, sizeof(devbase), "/dev/fw%ld", tmp); 792 if (fd > 0) { 793 close(fd); 794 fd = -1; 795 } 796 if (argc == optind) { 797 open_dev(&fd, devbase); 798 list_dev(fd); 799 } 800 break; 801 #define TAG (1<<6) 802 #define CHANNEL 63 803 case 'M': 804 switch (optarg[0]) { 805 case 'm': 806 recvfn = mpegtsrecv; 807 break; 808 case 'd': 809 recvfn = dvrecv; 810 break; 811 default: 812 errx(EX_USAGE, "unrecognized method: %s", 813 optarg); 814 } 815 break; 816 case 'R': 817 open_dev(&fd, devbase); 818 if (recvfn == NULL) /* guess... */ 819 recvfn = detect_recv_fn(fd, TAG | CHANNEL); 820 close(fd); 821 fd = -1; 822 open_dev(&fd, devbase); 823 (*recvfn)(fd, optarg, TAG | CHANNEL, -1); 824 break; 825 case 'S': 826 open_dev(&fd, devbase); 827 dvsend(fd, optarg, TAG | CHANNEL, -1); 828 break; 829 default: 830 usage(); 831 } 832 return 0; 833 } 834