Index: head/usr.sbin/fwcontrol/fwcontrol.8 =================================================================== --- head/usr.sbin/fwcontrol/fwcontrol.8 (revision 109990) +++ head/usr.sbin/fwcontrol/fwcontrol.8 (revision 109991) @@ -1,119 +1,125 @@ .\" Copyright (c) 2002 Hidetoshi Shimokawa .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE .\" DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN .\" ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .\" $FreeBSD$ .\" .\" .Dd December 30, 2002 .Dt FWCONTROL 8 .Os .Sh NAME .Nm fwcontrol .Nd FireWire control utility .Sh SYNOPSIS .Nm .Op Fl rt .Op Fl c Ar node .Op Fl d Ar node .Op Fl l Ar file .Op Fl g Ar gap_count .Op Fl b Ar pri_req .Op Fl R Ar filename .Op Fl S Ar filename .Sh DESCRIPTION The .Nm utility is designed to provide a way for users to access and control the .Fx FireWire subsystem. .Pp .Bl -tag -width indent .It Fl r Initiate bus reset. .It Fl t Show the topology map. .It Fl c Ar node Show the configuration ROM on the node. .It Fl d Ar node Hex dump of the configuration ROM. .It Fl l Ar file Load hex dump file of the configuration ROM and parse it. .It Fl g Ar gap_count Broadcast gap_count by phy_config packet. .It Fl i Ar pri_req Set PRIORITY_BUDGET register on all supported nodes. .It Fl R Ar filename Receive DV stream and dump it to a file. Use Ctl-C to stop the receiving. Some DV cameras seem not to send the stream if a bus manager exits. If you cannot get the stream, try the following commands. .Bd -literal -offset indent sysctl hw.firewire.try_bmr=0; fwcontrol -r .Ed +.Pp +The resulting file contains raw DV data excluding isochronus header +and CIP header. It can be handled by +.Ic libdv +in the ports collection. .It Fl S Ar filename Send a DV file as isochronus stream. .El .Sh EXAMPLES Each DV frame has fixed size and it is easy to edit those frame order. .Bd -literal -offset indent fwcontrol -R original.dv .Ed .Pp Receive stream. .Bd -literal -offset indent dd if=original.dv of=first.dv bs=120000 count=30 .Ed .Pp Get first 30 frames(NTSC). .Bd -literal -offset indent dd if=original.dv of=second.dv bs=120000 skip=30 count=30 .Ed .Pp Get second 30 frames(NTSC). .Bd -literal -offset indent cat second.dv first.dv | fwcontrol -S /dev/stdin .Ed .Pp Swap first and second 30 frames and send them to DV recorder. +.Pp For PAL, replace 'bs=120000' with 'bs=144000'. - +.Pp .Sh FILES .Bl -tag -width indent .It Pa /dev/fw0 .El .Sh SEE ALSO .Xr firewire 4 , .Xr fwohci 4 , .Xr sbp 4 , .Xr fwe 4 .Sh HISTORY The .Nm utility first appeared in .Fx 5.0 . .Pp .Sh AUTHORS .An Hidetoshi Shimokawa Aq simokawa@FreeBSD.org .Sh BUGS This utility is still under development and provided for debug purpose. .Pp Index: head/usr.sbin/fwcontrol/fwcontrol.c =================================================================== --- head/usr.sbin/fwcontrol/fwcontrol.c (revision 109990) +++ head/usr.sbin/fwcontrol/fwcontrol.c (revision 109991) @@ -1,421 +1,426 @@ /* * Copyright (C) 2002 * Hidetoshi Shimokawa. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * * This product includes software developed by Hidetoshi Shimokawa. * * 4. Neither the name of the author nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include extern int dvrecv(int, char *, char, int); extern int dvsend(int, char *, char, int); static void usage(void) { fprintf(stderr, "fwcontrol [-g gap_count] [-b pri_req] [-c node]" " [-r] [-t] [-d node] [-l file] [-R file] [-S file]\n"); fprintf(stderr, "\t-g: broadcast gap_count by phy_config packet\n"); fprintf(stderr, "\t-b: set PRIORITY_BUDGET register on all supported nodes\n"); fprintf(stderr, "\t-c: read configuration ROM\n"); fprintf(stderr, "\t-r: bus reset\n"); fprintf(stderr, "\t-t: read topology map\n"); fprintf(stderr, "\t-d: hex dump of configuration ROM\n"); fprintf(stderr, "\t-l: load and parse hex dump file of configuration ROM\n"); fprintf(stderr, "\t-R: Receive DV stream\n"); fprintf(stderr, "\t-S: Send DV stream\n"); exit(0); } static struct fw_devlstreq * get_dev(int fd) { struct fw_devlstreq *data; data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq)); if (data == NULL) err(1, "malloc"); if( ioctl(fd, FW_GDEVLST, data) < 0) { err(1, "ioctl"); } return data; } static void list_dev(int fd) { struct fw_devlstreq *data; struct fw_devinfo *devinfo; int i; data = get_dev(fd); printf("%d devices (info_len=%d)\n", data->n, data->info_len); for (i = 0; i < data->info_len; i++) { devinfo = &data->dev[i]; printf("%d node %d eui:%08x%08x status:%d\n", i, devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, devinfo->status ); } free((void *)data); } static u_int32_t read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data) { struct fw_asyreq *asyreq; u_int32_t *qld, res; asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); asyreq->req.len = 16; asyreq->req.type = FWASREQEUI; asyreq->req.dst.eui = eui; #if 0 asyreq->pkt.mode.rreqq.dst = htons(FWLOCALBUS | node); #endif asyreq->pkt.mode.rreqq.tlrt = 0; if (read) asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ; else asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ; asyreq->pkt.mode.rreqq.dest_hi = htons(0xffff); asyreq->pkt.mode.rreqq.dest_lo = htonl(addr_lo); qld = (u_int32_t *)&asyreq->pkt; if (!read) asyreq->pkt.mode.wreqq.data = data; if (ioctl(fd, FW_ASYREQ, asyreq) < 0) { err(1, "ioctl"); } res = qld[3]; free(asyreq); if (read) return ntohl(res); else return 0; } static void send_phy_config(int fd, int root_node, int gap_count) { struct fw_asyreq *asyreq; asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); asyreq->req.len = 12; asyreq->req.type = FWASREQNODE; asyreq->pkt.mode.ld[0] = 0; asyreq->pkt.mode.ld[1] = 0; asyreq->pkt.mode.common.tcode = FWTCODE_PHY; if (root_node >= 0) asyreq->pkt.mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23); if (gap_count >= 0) asyreq->pkt.mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16); asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1]; printf("send phy_config root_node=%d gap_count=%d\n", root_node, gap_count); if (ioctl(fd, FW_ASYREQ, asyreq) < 0) { err(1, "ioctl"); } } static void set_pri_req(int fd, int pri_req) { struct fw_devlstreq *data; struct fw_devinfo *devinfo; u_int32_t max, reg, old; int i; data = get_dev(fd); #define BUGET_REG 0xf0000218 for (i = 0; i < data->info_len; i++) { devinfo = &data->dev[i]; if (!devinfo->status) continue; reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0); printf("%d %08x:%08x, %08x", devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg); if (reg > 0 && pri_req >= 0) { old = (reg & 0x3f); max = (reg & 0x3f00) >> 8; if (pri_req > max) pri_req = max; printf(" 0x%x -> 0x%x\n", old, pri_req); read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req); } else { printf("\n"); } } free((void *)data); } static void parse_bus_info_block(u_int32_t *p, int info_len) { int i; for (i = 0; i < info_len; i++) { printf("bus_info%d: 0x%08x\n", i, *p++); } } static int get_crom(int fd, int node, void *crom_buf, int len) { struct fw_crom_buf buf; - int i, n, error; + int i, error; struct fw_devlstreq *data; data = get_dev(fd); for (i = 0; i < data->info_len; i++) { if (data->dev[i].dst == node && data->dev[i].eui.lo != 0) break; } if (i == data->info_len) errx(1, "no such node %d.", node); else if (i == 0) errx(1, "node %d is myself.", node); else buf.eui = data->dev[i].eui; free((void *)data); buf.len = len; buf.ptr = crom_buf; if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) { err(1, "ioctl"); } return error; } static void show_crom(u_int32_t *crom_buf) { int i; struct crom_context cc; char *desc, info[256]; static char *key_types = "ICLD"; struct csrreg *reg; struct csrdirectory *dir; struct csrhdr *hdr; printf("first quad: 0x%08x\n", *crom_buf); hdr = (struct csrhdr *)crom_buf; if (hdr->info_len == 1) { /* minimum ROM */ struct csrreg *reg; reg = (struct csrreg *)hdr; printf("verndor ID: 0x%06x\n", reg->val); return; } printf("len: %d\n", hdr->crc_len); parse_bus_info_block(crom_buf+1, hdr->info_len); crom_init_context(&cc, crom_buf); dir = cc.stack[0].dir; printf("root_directory: len=0x%04x(%d) crc=0x%04x\n", dir->crc_len, dir->crc_len, dir->crc); if (dir->crc_len < 1) return; while (cc.depth >= 0) { desc = crom_desc(&cc, info, sizeof(info)); reg = crom_get(&cc); for (i = 0; i < cc.depth; i++) printf("\t"); printf("%02x(%c:%02x) %06x %s: %s\n", reg->key, key_types[(reg->key & CSRTYPE_MASK)>>6], reg->key & CSRKEY_MASK, reg->val, desc, info); crom_next(&cc); } } #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n" static void dump_crom(u_int32_t *p) { int len=1024, i; for (i = 0; i < len/(4*8); i ++) { printf(DUMP_FORMAT, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); p += 8; } } static void load_crom(char *filename, u_int32_t *p) { FILE *file; int len=1024, i; if ((file = fopen(filename, "r")) == NULL) err(1, "load_crom"); for (i = 0; i < len/(4*8); i ++) { fscanf(file, DUMP_FORMAT, p, p+1, p+2, p+3, p+4, p+5, p+6, p+7); p += 8; } } static void show_topology_map(int fd) { struct fw_topology_map *tmap; union fw_self_id sid; int i; static char *port_status[] = {" ", "-", "P", "C"}; static char *pwr_class[] = {" 0W", "15W", "30W", "45W", "-1W", "-2W", "-5W", "-9W"}; static char *speed[] = {"S100", "S200", "S400", "S800"}; tmap = malloc(sizeof(struct fw_topology_map)); if (tmap == NULL) return; if (ioctl(fd, FW_GTPMAP, tmap) < 0) { err(1, "ioctl"); } printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n", tmap->crc_len, tmap->generation, tmap->node_count, tmap->self_id_count); printf("id link gap_cnt speed delay cIRM power port0 port1 port2" " ini more\n"); for (i = 0; i < tmap->crc_len - 2; i++) { sid = tmap->self_id[i]; if (sid.p0.sequel) { printf("%02d sequel packet\n", sid.p0.phy_id); continue; } printf("%02d %2d %d %4s %d %d %3s" " %s %s %s %d %d\n", sid.p0.phy_id, sid.p0.link_active, sid.p0.gap_count, speed[sid.p0.phy_speed], sid.p0.phy_delay, sid.p0.contender, pwr_class[sid.p0.power_class], port_status[sid.p0.port0], port_status[sid.p0.port1], port_status[sid.p0.port2], sid.p0.initiated_reset, sid.p0.more_packets ); } free(tmap); } int main(int argc, char **argv) { - char devname[] = "/dev/fw1"; + char devname[256]; u_int32_t crom_buf[1024/4]; - int fd, tmp, ch, len=1024; + int fd, i, tmp, ch, len=1024; - if ((fd = open(devname, O_RDWR)) < 0) + for (i = 0; i < 4; i++) { + snprintf(devname, sizeof(devname), "/dev/fw%d", i); + if ((fd = open(devname, O_RDWR)) >= 0) + break; + } + if (fd < 0) err(1, "open"); if (argc < 2) { list_dev(fd); usage(); } while ((ch = getopt(argc, argv, "g:b:rtc:d:l:R:S:")) != -1) switch(ch) { case 'g': /* gap count */ tmp = strtol(optarg, NULL, 0); send_phy_config(fd, -1, tmp); break; case 'b': tmp = strtol(optarg, NULL, 0); set_pri_req(fd, tmp); break; case 'r': if(ioctl(fd, FW_IBUSRST, &tmp) < 0) err(1, "ioctl"); break; case 't': show_topology_map(fd); break; case 'c': tmp = strtol(optarg, NULL, 0); get_crom(fd, tmp, crom_buf, len); show_crom(crom_buf); break; case 'd': tmp = strtol(optarg, NULL, 0); get_crom(fd, tmp, crom_buf, len); dump_crom(crom_buf); break; case 'l': load_crom(optarg, crom_buf); show_crom(crom_buf); break; #define TAG (1<<6) #define CHANNEL 63 case 'R': dvrecv(fd, optarg, TAG | CHANNEL, -1); break; case 'S': dvsend(fd, optarg, TAG | CHANNEL, -1); break; default: usage(); } return 0; } Index: head/usr.sbin/fwcontrol/fwdv.c =================================================================== --- head/usr.sbin/fwcontrol/fwdv.c (revision 109990) +++ head/usr.sbin/fwcontrol/fwdv.c (revision 109991) @@ -1,403 +1,433 @@ /* * Copyright (C) 2003 * Hidetoshi Shimokawa. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * * This product includes software developed by Hidetoshi Shimokawa. * * 4. Neither the name of the author nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #if __FreeBSD_version >= 500000 #include #endif #include #include #include #include #include #include #include #include #include struct frac { int n,d; }; struct frac frame_cycle[2] = { {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */ {320, 1}, /* PAL 8000 cycle / 25 Hz */ }; int npackets[] = { 250 /* NTSC */, 300 /* PAL */ }; struct frac pad_rate[2] = { {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */ {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */ }; #define PSIZE 512 #define DSIZE 480 -#define NVEC 50 -#define BUFSIZE (PSIZE * 256) -#define BLOCKSIZE 80 -#define MAXBLOCKS (300*6) +#define NCHUNK 8 + +#define NPACKET_R 256 +#define NPACKET_T 255 +#define NEMPTY 20 +#define BUFSIZE (PSIZE * NPACKET_R) +#define MAXBLOCKS (300) #define CYCLE_FRAC 0xc00 -int dvrecv(int d, char *filename, char ich, int count) +int +dvrecv(int d, char *filename, char ich, int count) { struct fw_isochreq isoreq; struct fw_isobufreq bufreq; struct dvdbc *dv; struct ciphdr *ciph; struct fw_pkt *pkt; char *pad, *buf; u_int32_t *ptr; - int len, npad, fd, k, m, vec, pal, nb; - int nblocks[] = {250*6 /* NTSC */, 300*6 /* PAL */}; - struct iovec wbuf[NVEC]; - struct iovec *v; + int len, tlen, npad, fd, k, m, vec, pal, nb; + int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; + struct iovec wbuf[NPACKET_R]; fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); buf = (char *)malloc(BUFSIZE); - pad = (char *)malloc(BLOCKSIZE*MAXBLOCKS); - bzero(pad, BLOCKSIZE*MAXBLOCKS); + pad = (char *)malloc(DSIZE*MAXBLOCKS); + bzero(pad, DSIZE*MAXBLOCKS); bzero(wbuf, sizeof(wbuf)); - bufreq.rx.nchunk = 16; - bufreq.rx.npacket = 256; + bufreq.rx.nchunk = NCHUNK; + bufreq.rx.npacket = NPACKET_R; bufreq.rx.psize = PSIZE; bufreq.tx.nchunk = 0; bufreq.tx.npacket = 0; bufreq.tx.psize = 0; if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { err(1, "ioctl"); } isoreq.ch = ich & 0x3f; isoreq.tag = (ich >> 6) & 3; if( ioctl(d, FW_SRSTREAM, &isoreq) < 0) err(1, "ioctl"); k = m = 0; while (count <= 0 || k <= count) { - vec = 0; - wbuf[0].iov_len = 0; - len = read(d, buf, BUFSIZE); +#if 0 + tlen = 0; + while ((len = read(d, buf + tlen, PSIZE + /*BUFSIZE - tlen*/)) > 0) { + if (len < 0) { + if (errno == EAGAIN) { + fprintf(stderr, "(EAGAIN)\n"); + fflush(stderr); + if (len <= 0) + continue; + } else + err(1, "read failed"); + } + tlen += len; + if ((BUFSIZE - tlen) < PSIZE) + break; + }; +#else + tlen = len = read(d, buf, BUFSIZE); if (len < 0) { if (errno == EAGAIN) { fprintf(stderr, "(EAGAIN)\n"); fflush(stderr); - continue; - } - err(1, "read failed"); + if (len <= 0) + continue; + } else + err(1, "read failed"); } +#endif + vec = 0; ptr = (u_int32_t *) buf; again: pkt = (struct fw_pkt *) ptr; - if (ntohs(pkt->mode.stream.len) <= sizeof(struct ciphdr)) { - ptr ++; /* skip header */ - ptr += ntohs(pkt->mode.stream.len)/sizeof(u_int32_t); - goto next; - } #if 0 fprintf(stderr, "%08x %08x %08x %08x\n", htonl(ptr[0]), htonl(ptr[1]), htonl(ptr[2]), htonl(ptr[3])); #endif ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ if (ciph->fmt != CIP_FMT_DVCR) - err(1, "unknown format 0x%x", ciph->fmt); + errx(1, "unknown format 0x%x", ciph->fmt); ptr = (u_int32_t *) (ciph + 1); /* skip cip header */ #if 0 if (ciph->fdf.dv.cyc != 0xffff && k == 0) { fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); } #endif + if (ntohs(pkt->mode.stream.len) <= sizeof(struct ciphdr)) + /* no payload */ + goto next; for (dv = (struct dvdbc *)ptr; (char *)dv < (char *)(ptr + ciph->len); - dv++) { + dv+=6) { #if 0 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); #endif if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { #if 0 fprintf(stderr, "%d(%d) ", k, m); #else fprintf(stderr, "%d", k%10); #endif pal = ((dv->payload[0] & DV_DSF_12) != 0); nb = nblocks[pal]; #if 1 if (m > 0 && m != nb) { /* padding bad frame */ npad = ((nb - m) % nb); if (npad < 0) npad += nb; fprintf(stderr, "(%d blocks padded)", npad); - npad *= BLOCKSIZE; - npad = write(fd, pad, npad); + npad *= DSIZE; + wbuf[vec].iov_base = pad; + wbuf[vec++].iov_len = npad; + if (vec >= NPACKET_R) { + writev(fd, wbuf, vec); + vec = 0; + } } #endif k++; if (k % 30 == 0) { /* every second */ fprintf(stderr, "\n"); } fflush(stderr); m = 0; } if (k == 0 || (count > 0 && k > count)) continue; m++; - v = &wbuf[vec]; - if ((v->iov_base + v->iov_len) == (char *) dv) { - v->iov_len += sizeof(struct dvdbc); - } else { - if (v->iov_len != 0) - vec++; - if (vec >= NVEC) { - writev(fd, wbuf, vec); - vec = 0; - } - v = &wbuf[vec]; - v->iov_base = (char *) dv; - v->iov_len = sizeof(struct dvdbc); + wbuf[vec].iov_base = (char *) dv; + wbuf[vec++].iov_len = DSIZE; + if (vec >= NPACKET_R) { + writev(fd, wbuf, vec); + vec = 0; } } ptr = (u_int32_t *)dv; next: - if ((char *)ptr < buf + len) { + if ((char *)ptr < buf + tlen) goto again; - } - if (wbuf[0].iov_len > 0) { - writev(fd, wbuf, vec+1); - } + if (vec > 0) + writev(fd, wbuf, vec); } close(fd); fprintf(stderr, "\n"); return 0; } -int dvsend(int d, char *filename, char ich, int count) + +int +dvsend(int d, char *filename, char ich, int count) { struct fw_isochreq isoreq; struct fw_isobufreq bufreq; struct dvdbc *dv; - struct ciphdr *ciph; struct fw_pkt *pkt; - int len, dlen, header, fd, frames, packets, vec; + int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i; int system=0, pad_acc, cycle_acc, cycle, f_cycle, f_frac; - struct iovec wbuf[NVEC]; + struct iovec wbuf[NPACKET_T*2 + NEMPTY]; char *pbuf; - u_int32_t hdr[3]; + u_int32_t iso_data, iso_empty, hdr[NPACKET_T + NEMPTY][3]; + struct ciphdr *ciph; struct timeval start, end; double rtime; fd = open(filename, O_RDONLY); - pbuf = (char *)malloc(BUFSIZE); + pbuf = (char *)malloc(DSIZE * NPACKET_T); bzero(wbuf, sizeof(wbuf)); bufreq.rx.nchunk = 0; bufreq.rx.npacket = 0; bufreq.rx.psize = 0; - bufreq.tx.nchunk = 10; - bufreq.tx.npacket = 255; + bufreq.tx.nchunk = NCHUNK; + bufreq.tx.npacket = NPACKET_T; bufreq.tx.psize = PSIZE; if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { err(1, "ioctl"); } isoreq.ch = ich & 0x3f; isoreq.tag = (ich >> 6) & 3; if( ioctl(d, FW_STSTREAM, &isoreq) < 0) err(1, "ioctl"); - bzero(hdr, sizeof(hdr)); - pkt = (struct fw_pkt *) &hdr[0]; + pkt = (struct fw_pkt *) &iso_data; pkt->mode.stream.len = htons(DSIZE + sizeof(struct ciphdr)); pkt->mode.stream.sy = 0; pkt->mode.stream.tcode = FWTCODE_STREAM; pkt->mode.stream.chtag = ich; + iso_empty = iso_data; + pkt = (struct fw_pkt *) &iso_empty; + pkt->mode.stream.len = htons(sizeof(struct ciphdr)); - ciph = (struct ciphdr *) &hdr[1]; - ciph->src = 0; /* XXX */ + hdr[0][0] = iso_data; + ciph = (struct ciphdr *)&hdr[0][1]; + ciph->src = 0; ciph->len = 120; ciph->dbc = 0; ciph->eoh1 = 1; ciph->fdf.dv.cyc = 0xffff; + for (i = 1; i < NPACKET_T; i++) { + bcopy(hdr[0], hdr[i], sizeof(hdr[0])); + } + gettimeofday(&start, NULL); #if 0 fprintf(stderr, "%08x %08x %08x\n", htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); #endif frames = 0; packets = 0; pad_acc = 0; - cycle_acc = frame_cycle[system].d * 1; cycle = 1; + cycle_acc = frame_cycle[system].d * cycle; while (1) { - vec = 0; - wbuf[0].iov_len = 0; - dlen = 0; - while (dlen < DSIZE) { - len = read(fd, pbuf + dlen, DSIZE - dlen); + tlen = 0; + while (tlen < DSIZE * NPACKET_T) { + len = read(fd, pbuf + tlen, DSIZE * NPACKET_T - tlen); if (len <= 0) { - fprintf(stderr, "\nend of file(len=%d)\n", len); + if (tlen > 0) + break; + if (len < 0) + warn("read"); + else + printf("\nend of file\n"); goto send_end; } - dlen += len; + tlen += len; } - dv = (struct dvdbc *)pbuf; + vec = 0; + count = 0; + offset = 0; + nhdr = 0; +next: + dv = (struct dvdbc *)(pbuf + offset * DSIZE); #if 0 header = (dv->sct == 0 && dv->dseq == 0); #else header = (packets % npackets[system] == 0); #endif + ciph = (struct ciphdr *)&hdr[nhdr][1]; if (header) { fprintf(stderr, "%d", frames % 10); frames ++; if (count > 0 && frames > count) break; if (frames % 30 == 0) fprintf(stderr, "\n"); fflush(stderr); system = ((dv->payload[0] & DV_DSF_12) != 0); f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf; f_frac = (cycle_acc % frame_cycle[system].d * CYCLE_FRAC) / frame_cycle[system].d; #if 0 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); #else ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); #endif cycle_acc += frame_cycle[system].n; cycle_acc %= frame_cycle[system].d * 0x10; } else { - ciph->fdf.dv.cyc = 0xffff; /* XXX */ + ciph->fdf.dv.cyc = 0xffff; } ciph->dbc = packets++ % 256; - wbuf[0].iov_base = (char *)&hdr[0]; - wbuf[0].iov_len = 3 * sizeof(u_int32_t); - wbuf[1].iov_base = pbuf; - wbuf[1].iov_len = 480; - pad_acc += pad_rate[system].n; if (pad_acc >= pad_rate[system].d) { pad_acc -= pad_rate[system].d; - pkt->mode.stream.len = htons(sizeof(struct ciphdr)); + bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); + hdr[nhdr][0] = iso_empty; + wbuf[vec].iov_base = (char *)hdr[nhdr]; + wbuf[vec++].iov_len = sizeof(hdr[0]); + nhdr ++; cycle ++; -again1: - len = writev(d, wbuf, 1); - if (len < 0) { - if (errno == EAGAIN) { - fprintf(stderr, "(EAGAIN)\n"); - fflush(stderr); - goto again1; - } - err(1, "write failed"); - } } - - pkt->mode.stream.len = htons(480 + sizeof(struct ciphdr)); + hdr[nhdr][0] = iso_data; + wbuf[vec].iov_base = (char *)hdr[nhdr]; + wbuf[vec++].iov_len = sizeof(hdr[0]); + wbuf[vec].iov_base = (char *)dv; + wbuf[vec++].iov_len = DSIZE; + nhdr ++; cycle ++; -again2: - len = writev(d, wbuf, 2); + offset ++; + if (offset * DSIZE < tlen) + goto next; + +again: + len = writev(d, wbuf, vec); if (len < 0) { if (errno == EAGAIN) { fprintf(stderr, "(EAGAIN)\n"); fflush(stderr); - goto again2; + goto again; } err(1, "write failed"); } } close(fd); fprintf(stderr, "\n"); send_end: gettimeofday(&end, NULL); rtime = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) * 1e-6; fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", frames, rtime, frames/rtime); return 0; } #if 0 int main(int argc, char *argv[]) { extern char *optarg; extern int optind; int ch; int fd; char devname[] = "/dev/fw0"; while ((ch = getopt(argc, argv, "I:")) != -1){ switch(ch) { case 'I': strcpy(devname, optarg); break; default: usage(); break; } } argc -= optind; argv += optind; if (argc < 2) usage(); fd = open(devname, O_RDWR); if (fd < 0) err(1, "open"); dvrec(fd, argv[0], (TAG<<6) | CHANNEL, atoi(argv[1])); close(fd); exit(0); } #endif