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