3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/usr.sbin/fwcontrol/fwcontrol.c$");
41 #include <sys/param.h>
42 #include <sys/types.h>
54 #include <sys/param.h>
55 #include <sys/malloc.h>
56 #include <sys/types.h>
57 #include <sys/sysctl.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 #include <sys/errno.h>
61 #include <sys/eui64.h>
62 #include <dev/firewire/firewire.h>
63 #include <dev/firewire/iec13213.h>
64 #include <dev/firewire/fwphyreg.h>
65 #include <dev/firewire/iec68113.h>
76 #include <netinet/in.h>
77 #include "fwmethods.h"
80 static void sysctl_set_int(const char *, int);
87 "%s [-u bus_num] [-prt] [-c node] [-d node] [-o node] [-s node]\n"
88 "\t [-l file] [-g gap_count] [-f force_root ] [-b pri_req]\n"
89 "\t [-M mode] [-R filename] [-S filename] "
91 "[-m EUI64 | hostname]\n"
95 "\t-u: specify bus number\n"
96 "\t-p: Display current PHY register settings\n"
98 "\t-t: read topology map\n"
99 "\t-c: read configuration ROM\n"
100 "\t-d: hex dump of configuration ROM\n"
101 "\t-o: send link-on packet to the node\n"
102 "\t-s: write RESET_START register on the node\n"
103 "\t-l: load and parse hex dump file of configuration ROM\n"
104 "\t-g: set gap count\n"
105 "\t-f: force root node\n"
106 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
107 "\t-M: specify dv or mpeg\n"
108 "\t-R: Receive DV or MPEG TS stream\n"
109 "\t-S: Send DV stream\n"
111 "\t-m: set fwmem target\n"
114 fprintf(stderr
, "\n");
118 fweui2eui64(const struct fw_eui64
*fweui
, struct eui64
*eui
)
120 *(u_int32_t
*)&(eui
->octet
[0]) = htonl(fweui
->hi
);
121 *(u_int32_t
*)&(eui
->octet
[4]) = htonl(fweui
->lo
);
125 get_dev(int fd
, struct fw_devlstreq
*data
)
128 err(EX_SOFTWARE
, "%s: data malloc", __func__
);
129 if( ioctl(fd
, FW_GDEVLST
, data
) < 0) {
130 err(EX_IOERR
, "%s: ioctl", __func__
);
135 str2node(int fd
, const char *nodestr
)
137 struct eui64 eui
, tmpeui
;
138 struct fw_devlstreq
*data
;
146 * Deal with classic node specifications.
148 node
= strtol(nodestr
, &endptr
, 0);
153 * Try to get an eui and match it against available nodes.
156 if (eui64_aton(nodestr
, &eui
) != 0)
158 if (eui64_hostton(nodestr
, &eui
) != 0 && eui64_aton(nodestr
, &eui
) != 0)
162 data
= (struct fw_devlstreq
*)malloc(sizeof(struct fw_devlstreq
));
164 err(EX_SOFTWARE
, "%s: data malloc", __func__
);
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
;
175 if (i
>= data
->info_len
) {
182 if (node
< 0 || node
> 63)
191 struct fw_devlstreq
*data
;
192 struct fw_devinfo
*devinfo
;
194 char addr
[EUI64_SIZ
];
197 data
= (struct fw_devlstreq
*)malloc(sizeof(struct fw_devlstreq
));
199 err(1, "%s: data malloc", __func__
);
201 printf("%d devices (info_len=%d)\n", data
->n
, data
->info_len
);
202 printf("node EUI64 status\n");
203 for (i
= 0; i
< data
->info_len
; i
++) {
204 devinfo
= &data
->dev
[i
];
205 fweui2eui64(&devinfo
->eui
, &eui
);
206 eui64_ntoa(&eui
, addr
, sizeof(addr
));
207 printf("%4d %s %6d\n",
208 (devinfo
->status
|| i
== 0) ? devinfo
->dst
: -1,
217 read_write_quad(int fd
, struct fw_eui64 eui
, u_int32_t addr_lo
, int readmode
, u_int32_t data
)
219 struct fw_asyreq
*asyreq
;
222 asyreq
= (struct fw_asyreq
*)malloc(sizeof(struct fw_asyreq_t
) + 16);
224 err(EX_SOFTWARE
, "%s:asyreq malloc", __func__
);
225 asyreq
->req
.len
= 16;
227 asyreq
->req
.type
= FWASREQNODE
;
228 asyreq
->pkt
.mode
.rreqq
.dst
= FWLOCALBUS
| node
;
230 asyreq
->req
.type
= FWASREQEUI
;
231 asyreq
->req
.dst
.eui
= eui
;
233 asyreq
->pkt
.mode
.rreqq
.tlrt
= 0;
235 asyreq
->pkt
.mode
.rreqq
.tcode
= FWTCODE_RREQQ
;
237 asyreq
->pkt
.mode
.rreqq
.tcode
= FWTCODE_WREQQ
;
239 asyreq
->pkt
.mode
.rreqq
.dest_hi
= 0xffff;
240 asyreq
->pkt
.mode
.rreqq
.dest_lo
= addr_lo
;
242 qld
= (u_int32_t
*)&asyreq
->pkt
;
244 asyreq
->pkt
.mode
.wreqq
.data
= htonl(data
);
246 if (ioctl(fd
, FW_ASYREQ
, asyreq
) < 0) {
247 err(EX_IOERR
, "%s: ioctl", __func__
);
258 * Send a PHY Config Packet
259 * ieee 1394a-2005 4.3.4.3
261 * Message ID Root ID R T Gap Count
262 * 00(2 bits) (6 bits) 1 1 (6 bits)
264 * if "R" is set, then Root ID will be the next
265 * root node upon the next bus reset.
266 * if "T" is set, then Gap Count will be the
267 * value that all nodes use for their Gap Count
268 * if "R" and "T" are not set, then this message
269 * is either ignored or interpreted as an extended
270 * PHY config Packet as per 1394a-2005 4.3.4.4
273 send_phy_config(int fd
, int root_node
, int gap_count
)
275 struct fw_asyreq
*asyreq
;
277 asyreq
= (struct fw_asyreq
*)malloc(sizeof(struct fw_asyreq_t
) + 12);
279 err(EX_SOFTWARE
, "%s:asyreq malloc", __func__
);
280 asyreq
->req
.len
= 12;
281 asyreq
->req
.type
= FWASREQNODE
;
282 asyreq
->pkt
.mode
.ld
[0] = 0;
283 asyreq
->pkt
.mode
.ld
[1] = 0;
284 asyreq
->pkt
.mode
.common
.tcode
= FWTCODE_PHY
;
286 asyreq
->pkt
.mode
.ld
[1] |= ((root_node
<< 24) | (1 << 23));
288 asyreq
->pkt
.mode
.ld
[1] |= ((1 << 22) | (gap_count
<< 16));
289 asyreq
->pkt
.mode
.ld
[2] = ~asyreq
->pkt
.mode
.ld
[1];
291 printf("send phy_config root_node=%d gap_count=%d\n",
292 root_node
, gap_count
);
294 if (ioctl(fd
, FW_ASYREQ
, asyreq
) < 0)
295 err(EX_IOERR
, "%s: ioctl", __func__
);
300 link_on(int fd
, int node
)
302 struct fw_asyreq
*asyreq
;
304 asyreq
= (struct fw_asyreq
*)malloc(sizeof(struct fw_asyreq_t
) + 12);
306 err(EX_SOFTWARE
, "%s:asyreq malloc", __func__
);
307 asyreq
->req
.len
= 12;
308 asyreq
->req
.type
= FWASREQNODE
;
309 asyreq
->pkt
.mode
.common
.tcode
= FWTCODE_PHY
;
310 asyreq
->pkt
.mode
.ld
[1] |= (1 << 30) | ((node
& 0x3f) << 24);
311 asyreq
->pkt
.mode
.ld
[2] = ~asyreq
->pkt
.mode
.ld
[1];
313 if (ioctl(fd
, FW_ASYREQ
, asyreq
) < 0)
314 err(EX_IOERR
, "%s: ioctl", __func__
);
319 reset_start(int fd
, int node
)
321 struct fw_asyreq
*asyreq
;
323 asyreq
= (struct fw_asyreq
*)malloc(sizeof(struct fw_asyreq_t
) + 16);
325 err(EX_SOFTWARE
, "%s:asyreq malloc", __func__
);
326 asyreq
->req
.len
= 16;
327 asyreq
->req
.type
= FWASREQNODE
;
328 asyreq
->pkt
.mode
.wreqq
.dst
= FWLOCALBUS
| (node
& 0x3f);
329 asyreq
->pkt
.mode
.wreqq
.tlrt
= 0;
330 asyreq
->pkt
.mode
.wreqq
.tcode
= FWTCODE_WREQQ
;
332 asyreq
->pkt
.mode
.wreqq
.dest_hi
= 0xffff;
333 asyreq
->pkt
.mode
.wreqq
.dest_lo
= 0xf0000000 | RESET_START
;
335 asyreq
->pkt
.mode
.wreqq
.data
= htonl(0x1);
337 if (ioctl(fd
, FW_ASYREQ
, asyreq
) < 0)
338 err(EX_IOERR
, "%s: ioctl", __func__
);
343 set_pri_req(int fd
, u_int32_t pri_req
)
345 struct fw_devlstreq
*data
;
346 struct fw_devinfo
*devinfo
;
348 char addr
[EUI64_SIZ
];
349 u_int32_t max
, reg
, old
;
352 data
= (struct fw_devlstreq
*)malloc(sizeof(struct fw_devlstreq
));
354 err(EX_SOFTWARE
, "%s:data malloc", __func__
);
356 #define BUGET_REG 0xf0000218
357 for (i
= 0; i
< data
->info_len
; i
++) {
358 devinfo
= &data
->dev
[i
];
359 if (!devinfo
->status
)
361 reg
= read_write_quad(fd
, devinfo
->eui
, BUGET_REG
, 1, 0);
362 fweui2eui64(&devinfo
->eui
, &eui
);
363 eui64_ntoa(&eui
, addr
, sizeof(addr
));
364 printf("%d %s, %08x",
365 devinfo
->dst
, addr
, reg
);
368 max
= (reg
& 0x3f00) >> 8;
371 printf(" 0x%x -> 0x%x\n", old
, pri_req
);
372 read_write_quad(fd
, devinfo
->eui
, BUGET_REG
, 0, pri_req
);
381 parse_bus_info_block(u_int32_t
*p
)
383 char addr
[EUI64_SIZ
];
387 bi
= (struct bus_info
*)p
;
388 fweui2eui64(&bi
->eui64
, &eui
);
389 eui64_ntoa(&eui
, addr
, sizeof(addr
));
390 printf("bus_name: 0x%04x\n"
391 "irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
392 "cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
393 "generation:%d link_spd:%d\n"
396 bi
->irmc
, bi
->cmc
, bi
->isc
, bi
->bmc
, bi
->pmc
,
397 bi
->cyc_clk_acc
, bi
->max_rec
, bi
->max_rom
,
398 bi
->generation
, bi
->link_spd
,
403 get_crom(int fd
, int node
, void *crom_buf
, int len
)
405 struct fw_crom_buf buf
;
407 struct fw_devlstreq
*data
;
409 data
= (struct fw_devlstreq
*)malloc(sizeof(struct fw_devlstreq
));
411 err(EX_SOFTWARE
, "%s:data malloc", __func__
);
414 for (i
= 0; i
< data
->info_len
; i
++) {
415 if (data
->dev
[i
].dst
== node
&& data
->dev
[i
].eui
.lo
!= 0)
418 if (i
== data
->info_len
)
419 errx(1, "no such node %d.", node
);
421 buf
.eui
= data
->dev
[i
].eui
;
426 bzero(crom_buf
, len
);
427 if ((error
= ioctl(fd
, FW_GCROM
, &buf
)) < 0) {
428 err(EX_IOERR
, "%s: ioctl", __func__
);
435 show_crom(u_int32_t
*crom_buf
)
438 struct crom_context cc
;
439 char *desc
, info
[256];
440 static const char *key_types
= "ICLD";
442 struct csrdirectory
*dir
;
446 printf("first quad: 0x%08x ", *crom_buf
);
447 if (crom_buf
[0] == 0) {
448 printf("(Invalid Configuration ROM)\n");
451 hdr
= (struct csrhdr
*)crom_buf
;
452 if (hdr
->info_len
== 1) {
454 reg
= (struct csrreg
*)hdr
;
455 printf("verndor ID: 0x%06x\n", reg
->val
);
458 printf("info_len=%d crc_len=%d crc=0x%04x",
459 hdr
->info_len
, hdr
->crc_len
, hdr
->crc
);
460 crc
= crom_crc(crom_buf
+1, hdr
->crc_len
);
465 parse_bus_info_block(crom_buf
+1);
467 crom_init_context(&cc
, crom_buf
);
468 dir
= cc
.stack
[0].dir
;
470 printf("no root directory - giving up\n");
473 printf("root_directory: len=0x%04x(%d) crc=0x%04x",
474 dir
->crc_len
, dir
->crc_len
, dir
->crc
);
475 crc
= crom_crc((u_int32_t
*)&dir
->entry
[0], dir
->crc_len
);
480 if (dir
->crc_len
< 1)
482 while (cc
.depth
>= 0) {
483 desc
= crom_desc(&cc
, info
, sizeof(info
));
485 for (i
= 0; i
< cc
.depth
; i
++)
487 printf("%02x(%c:%02x) %06x %s: %s\n",
489 key_types
[(reg
->key
& CSRTYPE_MASK
)>>6],
490 reg
->key
& CSRKEY_MASK
, reg
->val
,
496 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
499 dump_crom(u_int32_t
*p
)
503 for (i
= 0; i
< len
/(4*8); i
++) {
505 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7]);
511 load_crom(char *filename
, u_int32_t
*p
)
516 if ((file
= fopen(filename
, "r")) == NULL
)
517 err(1, "load_crom %s", filename
);
518 for (i
= 0; i
< len
/(4*8); i
++) {
519 fscanf(file
, DUMP_FORMAT
,
520 p
, p
+1, p
+2, p
+3, p
+4, p
+5, p
+6, p
+7);
527 show_topology_map(int fd
)
529 struct fw_topology_map
*tmap
;
530 union fw_self_id sid
;
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
));
538 err(EX_SOFTWARE
, "%s:tmap malloc", __func__
);
539 if (ioctl(fd
, FW_GTPMAP
, tmap
) < 0) {
540 err(EX_IOERR
, "%s: ioctl", __func__
);
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"
547 for (i
= 0; i
< tmap
->crc_len
- 2; i
++) {
548 sid
= tmap
->self_id
[i
];
550 printf("%02d sequel packet\n", sid
.p0
.phy_id
);
553 printf("%02d %2d %2d %4s %d %3s"
558 speed
[sid
.p0
.phy_speed
],
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
,
572 read_phy_registers(int fd
, u_int8_t
*buf
, int offset
, int len
)
574 struct fw_reg_req_t reg
;
577 for (i
= 0; i
< len
; i
++) {
578 reg
.addr
= offset
+ i
;
579 if (ioctl(fd
, FWOHCI_RDPHYREG
, ®
) < 0)
580 err(EX_IOERR
, "%s: ioctl", __func__
);
581 buf
[i
] = (u_int8_t
) reg
.data
;
582 printf("0x%02x ", reg
.data
);
588 read_phy_page(int fd
, u_int8_t
*buf
, int page
, int port
)
590 struct fw_reg_req_t reg
;
593 reg
.data
= ((page
& 7) << 5) | (port
& 0xf);
594 if (ioctl(fd
, FWOHCI_WRPHYREG
, ®
) < 0)
595 err(EX_IOERR
, "%s: ioctl", __func__
);
596 read_phy_registers(fd
, buf
, 8, 8);
600 dump_phy_registers(int fd
)
602 struct phyreg_base b
;
603 struct phyreg_page0 p
;
604 struct phyreg_page1 v
;
607 printf("=== base register ===\n");
608 read_phy_registers(fd
, (u_int8_t
*)&b
, 0, 8);
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
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
);
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"
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
,
643 p
.loop_disable
, p
.in_standby
, p
.hard_disable
646 printf("\n=== page 1 ===\n");
647 read_phy_page(fd
, (u_int8_t
*)&v
, 1, 0);
651 "Product_ID:0x%06x\n",
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]
659 open_dev(int *fd
, char *devname
)
662 *fd
= open(devname
, O_RDWR
);
672 sysctl_set_int(const char *name
, int val
)
674 if (sysctlbyname(name
, NULL
, NULL
, &val
, sizeof(int)) < 0)
675 err(1, "sysctl %s failed.", name
);
680 detect_recv_fn(int fd
, char ich
)
683 struct fw_isochreq isoreq
;
684 struct fw_isobufreq bufreq
;
689 #define RECV_NUM_PACKET 16
690 #define RECV_PACKET_SZ 1024
692 bufreq
.rx
.nchunk
= 8;
693 bufreq
.rx
.npacket
= RECV_NUM_PACKET
;
694 bufreq
.rx
.psize
= RECV_PACKET_SZ
;
695 bufreq
.tx
.nchunk
= 0;
696 bufreq
.tx
.npacket
= 0;
699 if (ioctl(fd
, FW_SSTBUF
, &bufreq
) < 0)
700 err(EX_IOERR
, "%s: ioctl FW_SSTBUF", __func__
);
702 isoreq
.ch
= ich
& 0x3f;
703 isoreq
.tag
= (ich
>> 6) & 3;
705 if (ioctl(fd
, FW_SRSTREAM
, &isoreq
) < 0)
706 err(EX_IOERR
, "%s: ioctl FW_SRSTREAM", __func__
);
708 buf
= (char *)malloc(RECV_NUM_PACKET
* RECV_PACKET_SZ
);
710 err(EX_SOFTWARE
, "%s:buf malloc", __func__
);
712 * fwdev.c seems to return EIO on error and
713 * the return value of the last uiomove
714 * on success. For now, checking that the
715 * return is not less than zero should be
716 * sufficient. fwdev.c::fw_read() should
717 * return the total length read, not the value
718 * of the last uiomove().
720 len
= read(fd
, buf
, RECV_NUM_PACKET
* RECV_PACKET_SZ
);
722 err(EX_IOERR
, "%s: error reading from device", __func__
);
723 ptr
= (u_int32_t
*) buf
;
724 ciph
= (struct ciphdr
*)(ptr
+ 1);
728 fprintf(stderr
, "Detected DV format on input.\n");
732 fprintf(stderr
, "Detected MPEG TS format on input.\n");
736 errx(1, "Unsupported format for receiving: fmt=0x%x", ciph
->fmt
);
743 main(int argc
, char **argv
)
745 #define MAX_BOARDS 10
746 u_int32_t crom_buf
[1024/4];
747 u_int32_t crom_buf_hex
[1024/4];
750 const char *device_string
= "/dev/bus/fw/";
752 const char *device_string
= "/dev/fw";
754 int fd
= -1, ch
, len
=1024;
755 int32_t current_board
= 0;
757 * If !command_set, then -u will display the nodes for the board.
758 * This emulates the previous behavior when -u is passed by itself
760 bool command_set
= false;
761 bool open_needed
= false;
765 fwmethod
*recvfn
= NULL
;
767 * Holders for which functions
770 bool display_board_only
= false;
771 bool display_crom
= false;
772 bool send_bus_reset
= false;
773 bool display_crom_hex
= false;
774 bool load_crom_from_file
= false;
776 bool set_fwmem_target
= false;
778 bool dump_topology
= false;
779 bool dump_phy_reg
= false;
781 int32_t priority_budget
= -1;
782 int32_t set_root_node
= -1;
783 int32_t set_gap_count
= -1;
784 int32_t send_link_on
= -1;
785 int32_t send_reset_start
= -1;
787 char *crom_string
= NULL
;
788 char *crom_string_hex
= NULL
;
789 char *recv_data
= NULL
;
790 char *send_data
= NULL
;
793 for (current_board
= 0; current_board
< MAX_BOARDS
; current_board
++) {
794 snprintf(devbase
, sizeof(devbase
), "%s%d", device_string
, current_board
);
795 if (open_dev(&fd
, devbase
) < 0) {
796 if (current_board
== 0) {
798 err(EX_IOERR
, "%s: Error opening firewire controller #%d %s",
799 __func__
, current_board
, devbase
);
809 * Parse all command line options, then execute requested operations.
812 while ((ch
= getopt(argc
, argv
, "M:f:g:o:s:b:prtc:d:l:u:R:S:")) != -1) {
814 while ((ch
= getopt(argc
, argv
, "M:f:g:m:o:s:b:prtc:d:l:u:R:S:")) != -1) {
819 priority_budget
= strtol(optarg
, NULL
, 0);
820 if (priority_budget
< 0 || priority_budget
> INT32_MAX
)
821 errx(EX_USAGE
, "%s: priority_budget out of range: %s", __func__
, optarg
);
824 display_board_only
= false;
827 crom_string
= malloc(strlen(optarg
) + 1);
828 if (crom_string
== NULL
)
829 err(EX_SOFTWARE
, "%s:crom_string malloc", __func__
);
830 strcpy(crom_string
, optarg
);
831 if (strtol(crom_string
, NULL
, 0) < 0
832 || strtol(crom_string
, NULL
, 0) > MAX_BOARDS
)
833 errx(EX_USAGE
, "%s:Invalid value for node", __func__
);
837 display_board_only
= false;
840 crom_string_hex
= malloc(strlen(optarg
) + 1);
841 if (crom_string_hex
== NULL
)
842 err(EX_SOFTWARE
, "%s:crom_string_hex malloc", __func__
);
843 strcpy(crom_string_hex
, optarg
);
844 display_crom_hex
= 1;
847 display_board_only
= false;
850 #define MAX_PHY_CONFIG 0x3f
851 set_root_node
= strtol(optarg
, NULL
, 0);
852 if (set_root_node
< 0 || set_root_node
> MAX_PHY_CONFIG
)
853 errx(EX_USAGE
, "%s:set_root_node out of range", __func__
);
856 display_board_only
= false;
859 set_gap_count
= strtol(optarg
, NULL
, 0);
860 if (set_gap_count
< 0 || set_gap_count
> MAX_PHY_CONFIG
)
861 errx(EX_USAGE
, "%s:set_gap_count out of range", __func__
);
864 display_board_only
= false;
867 load_crom_from_file
= 1;
868 load_crom(optarg
, crom_buf
);
870 display_board_only
= false;
874 set_fwmem_target
= 1;
877 display_board_only
= false;
878 if (eui64_hostton(optarg
, &target
) != 0 &&
879 eui64_aton(optarg
, &target
) != 0)
880 errx(EX_USAGE
, "%s: invalid target: %s", __func__
, optarg
);
884 send_link_on
= str2node(fd
, optarg
);
885 if ( (send_link_on
< 0) || (send_link_on
> MAX_PHY_CONFIG
) )
886 errx(EX_USAGE
, "%s: node out of range: %s\n",__func__
, optarg
);
889 display_board_only
= false;
895 display_board_only
= false;
901 display_board_only
= false;
904 send_reset_start
= str2node(fd
, optarg
);
905 if ( (send_reset_start
< 0) || (send_reset_start
> MAX_PHY_CONFIG
) )
906 errx(EX_USAGE
, "%s: node out of range: %s\n", __func__
, optarg
);
909 display_board_only
= false;
915 display_board_only
= false;
919 display_board_only
= true;
920 current_board
= strtol(optarg
, NULL
, 0);
932 errx(EX_USAGE
, "unrecognized method: %s",
936 display_board_only
= false;
939 recv_data
= malloc(strlen(optarg
)+1);
940 if (recv_data
== NULL
)
941 err(EX_SOFTWARE
, "%s:recv_data malloc", __func__
);
942 strcpy(recv_data
, optarg
);
945 display_board_only
= false;
948 send_data
= malloc(strlen(optarg
)+1);
949 if (send_data
== NULL
)
950 err(EX_SOFTWARE
, "%s:send_data malloc", __func__
);
951 strcpy(send_data
, optarg
);
954 display_board_only
= false;
959 warnc(EINVAL
, "%s: Unknown command line arguments", __func__
);
965 * Catch the error case when the user
966 * executes the command with non ''-''
967 * delimited arguments.
968 * Generate the usage() display and exit.
970 if (!command_set
&& !display_board_only
) {
972 warnc(EINVAL
, "%s: Unknown command line arguments", __func__
);
977 * If -u <bus_number> is passed, execute
978 * command for that card only.
980 * If -u <bus_number> is not passed, execute
981 * command for card 0 only.
985 snprintf(devbase
, sizeof(devbase
), "%s%d", device_string
, current_board
);
986 if (open_dev(&fd
, devbase
) < 0) {
987 err(EX_IOERR
, "%s: Error opening firewire controller #%d %s", __func__
, current_board
, devbase
);
991 * display the nodes on this board "-u"
994 if (display_board_only
)
1001 dump_phy_registers(fd
);
1004 * send a BUS_RESET Event "-r"
1006 if (send_bus_reset
) {
1007 if(ioctl(fd
, FW_IBUSRST
, &tmp
) < 0)
1008 err(EX_IOERR
, "%s: Ioctl of bus reset failed for %s", __func__
, devbase
);
1011 * Print out the CROM for this node "-c"
1014 tmp
= str2node(fd
, crom_string
);
1015 get_crom(fd
, tmp
, crom_buf
, len
);
1016 show_crom(crom_buf
);
1020 * Hex Dump the CROM for this node "-d"
1022 if (display_crom_hex
) {
1023 tmp
= str2node(fd
, crom_string_hex
);
1024 get_crom(fd
, tmp
, crom_buf_hex
, len
);
1025 dump_crom(crom_buf_hex
);
1026 free(crom_string_hex
);
1029 * Set Priority Budget to value for this node "-b"
1031 if (priority_budget
>= 0)
1032 set_pri_req(fd
, priority_budget
);
1035 * Explicitly set the root node of this bus to value "-f"
1037 if (set_root_node
>= 0)
1038 send_phy_config(fd
, set_root_node
, -1);
1041 * Set the gap count for this card/bus "-g"
1043 if (set_gap_count
>= 0)
1044 send_phy_config(fd
, -1, set_gap_count
);
1047 * Load a CROM from a file "-l"
1049 if (load_crom_from_file
)
1050 show_crom(crom_buf
);
1053 * Set the fwmem target for a node to argument "-m"
1055 if (set_fwmem_target
) {
1056 eui
.hi
= ntohl(*(u_int32_t
*)&(target
.octet
[0]));
1057 eui
.lo
= ntohl(*(u_int32_t
*)&(target
.octet
[4]));
1058 #if defined(__FreeBSD__)
1059 sysctl_set_int("hw.firewire.fwmem.eui64_hi", eui
.hi
);
1060 sysctl_set_int("hw.firewire.fwmem.eui64_lo", eui
.lo
);
1061 #elif defined(__NetBSD__)
1062 sysctl_set_int("hw.fwmem.eui64_hi", eui
.hi
);
1063 sysctl_set_int("hw.fwmem.eui64_lo", eui
.lo
);
1065 #warning "You need to add support for your OS"
1070 * Send a link on to this board/bus "-o"
1072 if (send_link_on
>= 0)
1073 link_on(fd
, send_link_on
);
1076 * Send a reset start to this board/bus "-s"
1078 if (send_reset_start
>= 0)
1079 reset_start(fd
, send_reset_start
);
1082 * Dump the node topology for this board/bus "-t"
1085 show_topology_map(fd
);
1088 * Receive data file from node "-R"
1092 if (recv_data
!= NULL
){
1093 if (recvfn
== NULL
) { /* guess... */
1094 recvfn
= detect_recv_fn(fd
, TAG
| CHANNEL
);
1098 snprintf(devbase
, sizeof(devbase
), "%s%d", device_string
, current_board
);
1099 if (open_dev(&fd
, devbase
) < 0)
1100 err(EX_IOERR
, "%s: Error opening firewire controller #%d %s in recv_data\n", __func__
, current_board
, devbase
);
1101 (*recvfn
)(fd
, recv_data
, TAG
| CHANNEL
, -1);
1106 * Send data file to node "-S"
1108 if (send_data
!= NULL
){
1109 dvsend(fd
, send_data
, TAG
| CHANNEL
, -1);