Make UEFI boot-platform build again
[haiku.git] / src / bin / fwcontrol / fwdv.c
bloba6612f84397edd0a16a235ecedb7edea2ddf0800
1 /*
2 * Copyright (C) 2003
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
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:
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
32 * SUCH DAMAGE.
34 * $FreeBSD: src/usr.sbin/fwcontrol/fwdv.c$
36 #include <sys/param.h>
37 #ifndef __HAIKU__
38 #include <sys/ioctl.h>
39 #endif
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/uio.h>
44 #if __FreeBSD_version >= 500000 || defined(__HAIKU__)
45 #include <arpa/inet.h>
46 #endif
48 #include <err.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #ifdef __HAIKU__
56 #include <stdint.h>
57 #include <strings.h>
58 #include <endian.h>
59 #include "firewire.h"
60 #include "iec68113.h"
61 #else
62 #include <sysexits.h>
64 #include <dev/firewire/firewire.h>
65 #include <dev/firewire/iec68113.h>
66 #endif
68 #include "fwmethods.h"
70 #define DEBUG 0
71 #define FIX_FRAME 1
73 struct frac {
74 int n,d;
77 struct frac frame_cycle[2] = {
78 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
79 {320, 1}, /* PAL 8000 cycle / 25 Hz */
81 int npackets[] = {
82 250 /* NTSC */,
83 300 /* PAL */
85 struct frac pad_rate[2] = {
86 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
87 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
89 char *system_name[] = {"NTSC", "PAL"};
90 int frame_rate[] = {30, 25};
92 #define PSIZE 512
93 #define DSIZE 480
94 #define NCHUNK 64
96 #define NPACKET_R 256
97 #define NPACKET_T 255
98 #define TNBUF 100 /* XXX too large value causes block noise */
99 #define NEMPTY 10 /* depends on TNBUF */
100 #define RBUFSIZE (PSIZE * NPACKET_R)
101 #define MAXBLOCKS (300)
102 #define CYCLE_FRAC 0xc00
104 void
105 dvrecv(int d, const char *filename, char ich, int count)
107 struct fw_isochreq isoreq;
108 struct fw_isobufreq bufreq;
109 struct dvdbc *dv;
110 struct ciphdr *ciph;
111 struct fw_pkt *pkt;
112 char *pad, *buf;
113 u_int32_t *ptr;
114 int len, tlen, npad, fd, k, m, vec, system = -1, nb;
115 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
116 struct iovec wbuf[NPACKET_R];
118 if(strcmp(filename, "-") == 0) {
119 fd = STDOUT_FILENO;
120 } else {
121 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
122 if (fd == -1)
123 err(EX_NOINPUT, "%s", filename);
125 buf = malloc(RBUFSIZE);
126 pad = malloc(DSIZE*MAXBLOCKS);
127 memset(pad, 0xff, DSIZE*MAXBLOCKS);
128 bzero(wbuf, sizeof(wbuf));
130 bufreq.rx.nchunk = NCHUNK;
131 bufreq.rx.npacket = NPACKET_R;
132 bufreq.rx.psize = PSIZE;
133 bufreq.tx.nchunk = 0;
134 bufreq.tx.npacket = 0;
135 bufreq.tx.psize = 0;
136 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
137 err(1, "ioctl FW_SSTBUF");
139 isoreq.ch = ich & 0x3f;
140 isoreq.tag = (ich >> 6) & 3;
142 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
143 err(1, "ioctl");
145 k = m = 0;
146 while (count <= 0 || k <= count) {
147 #if 0
148 tlen = 0;
149 while ((len = read(d, buf + tlen, PSIZE
150 /* RBUFSIZE - tlen */)) > 0) {
151 if (len < 0) {
152 if (errno == EAGAIN) {
153 fprintf(stderr, "(EAGAIN)\n");
154 fflush(stderr);
155 if (len <= 0)
156 continue;
157 } else
158 err(1, "read failed");
160 tlen += len;
161 if ((RBUFSIZE - tlen) < PSIZE)
162 break;
164 #else
165 tlen = len = read(d, buf, RBUFSIZE);
166 if (len < 0) {
167 if (errno == EAGAIN) {
168 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
169 fflush(stderr);
170 if (len <= 0)
171 continue;
172 } else
173 err(1, "read failed");
175 #endif
176 vec = 0;
177 ptr = (u_int32_t *) buf;
178 again:
179 pkt = (struct fw_pkt *) ptr;
180 #if DEBUG
181 fprintf(stderr, "%08x %08x %08x %08x\n",
182 htonl(ptr[0]), htonl(ptr[1]),
183 htonl(ptr[2]), htonl(ptr[3]));
184 #endif
185 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
186 if (ciph->fmt != CIP_FMT_DVCR)
187 errx(1, "unknown format 0x%x", ciph->fmt);
188 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
189 #if DEBUG
190 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
191 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
193 #endif
194 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
195 /* no payload */
196 goto next;
197 for (dv = (struct dvdbc *)ptr;
198 (char *)dv < (char *)(ptr + ciph->len);
199 dv+=6) {
201 #if DEBUG
202 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
203 #endif
204 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
205 if (system < 0) {
206 system = ciph->fdf.dv.fs;
207 fprintf(stderr, "%s\n", system_name[system]);
210 /* Fix DSF bit */
211 if (system == 1 &&
212 (dv->payload[0] & DV_DSF_12) == 0)
213 dv->payload[0] |= DV_DSF_12;
214 nb = nblocks[system];
215 fprintf(stderr, "%d:%02d:%02d %d\r",
216 k / (3600 * frame_rate[system]),
217 (k / (60 * frame_rate[system])) % 60,
218 (k / frame_rate[system]) % 60,
219 k % frame_rate[system]);
221 #if FIX_FRAME
222 if (m > 0 && m != nb) {
223 /* padding bad frame */
224 npad = ((nb - m) % nb);
225 if (npad < 0)
226 npad += nb;
227 fprintf(stderr, "\n%d blocks padded\n",
228 npad);
229 npad *= DSIZE;
230 wbuf[vec].iov_base = pad;
231 wbuf[vec++].iov_len = npad;
232 if (vec >= NPACKET_R) {
233 writev(fd, wbuf, vec);
234 vec = 0;
237 #endif
238 k++;
239 fflush(stderr);
240 m = 0;
242 if (k == 0 || (count > 0 && k > count))
243 continue;
244 m++;
245 wbuf[vec].iov_base = (char *) dv;
246 wbuf[vec++].iov_len = DSIZE;
247 if (vec >= NPACKET_R) {
248 writev(fd, wbuf, vec);
249 vec = 0;
252 ptr = (u_int32_t *)dv;
253 next:
254 if ((char *)ptr < buf + tlen)
255 goto again;
256 if (vec > 0)
257 writev(fd, wbuf, vec);
259 if (fd != STDOUT_FILENO)
260 close(fd);
261 fprintf(stderr, "\n");
265 void
266 dvsend(int d, const char *filename, char ich, int count)
268 struct fw_isochreq isoreq;
269 struct fw_isobufreq bufreq;
270 struct dvdbc *dv;
271 struct fw_pkt *pkt;
272 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
273 int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac;
274 struct iovec wbuf[TNBUF*2 + NEMPTY];
275 char *pbuf;
276 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
277 struct ciphdr *ciph;
278 struct timeval start, end;
279 double rtime;
281 fd = open(filename, O_RDONLY);
282 if (fd == -1)
283 err(EX_NOINPUT, "%s", filename);
285 pbuf = malloc(DSIZE * TNBUF);
286 bzero(wbuf, sizeof(wbuf));
288 bufreq.rx.nchunk = 0;
289 bufreq.rx.npacket = 0;
290 bufreq.rx.psize = 0;
291 bufreq.tx.nchunk = NCHUNK;
292 bufreq.tx.npacket = NPACKET_T;
293 bufreq.tx.psize = PSIZE;
294 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
295 err(1, "ioctl FW_SSTBUF");
297 isoreq.ch = ich & 0x3f;
298 isoreq.tag = (ich >> 6) & 3;
300 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
301 err(1, "ioctl FW_STSTREAM");
303 iso_data = 0;
304 pkt = (struct fw_pkt *) &iso_data;
305 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
306 pkt->mode.stream.sy = 0;
307 pkt->mode.stream.tcode = FWTCODE_STREAM;
308 pkt->mode.stream.chtag = ich;
309 iso_empty = iso_data;
310 pkt = (struct fw_pkt *) &iso_empty;
311 pkt->mode.stream.len = sizeof(struct ciphdr);
313 bzero(hdr[0], sizeof(hdr[0]));
314 hdr[0][0] = iso_data;
315 ciph = (struct ciphdr *)&hdr[0][1];
316 ciph->src = 0; /* XXX */
317 ciph->len = 120;
318 ciph->dbc = 0;
319 ciph->eoh1 = 1;
320 ciph->fdf.dv.cyc = 0xffff;
322 for (i = 1; i < TNBUF; i++)
323 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
325 gettimeofday(&start, NULL);
326 #if DEBUG
327 fprintf(stderr, "%08x %08x %08x\n",
328 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
329 #endif
330 frames = 0;
331 packets = 0;
332 pad_acc = 0;
333 while (1) {
334 tlen = 0;
335 while (tlen < DSIZE * TNBUF) {
336 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
337 if (len <= 0) {
338 if (tlen > 0)
339 break;
340 if (len < 0)
341 warn("read");
342 else
343 fprintf(stderr, "\nend of file\n");
344 goto send_end;
346 tlen += len;
348 vec = 0;
349 offset = 0;
350 nhdr = 0;
351 next:
352 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
353 #if 0
354 header = (dv->sct == 0 && dv->dseq == 0);
355 #else
356 header = (packets == 0 || packets % npackets[system] == 0);
357 #endif
359 ciph = (struct ciphdr *)&hdr[nhdr][1];
360 if (header) {
361 if (system < 0) {
362 system = ((dv->payload[0] & DV_DSF_12) != 0);
363 printf("%s\n", system_name[system]);
364 cycle = 1;
365 cycle_acc = frame_cycle[system].d * cycle;
367 fprintf(stderr, "%d", frames % 10);
368 frames ++;
369 if (count > 0 && frames > count)
370 break;
371 if (frames % frame_rate[system] == 0)
372 fprintf(stderr, "\n");
373 fflush(stderr);
374 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
375 f_frac = (cycle_acc % frame_cycle[system].d
376 * CYCLE_FRAC) / frame_cycle[system].d;
377 #if 0
378 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
379 #else
380 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
381 #endif
382 cycle_acc += frame_cycle[system].n;
383 cycle_acc %= frame_cycle[system].d * 0x10;
385 } else {
386 ciph->fdf.dv.cyc = 0xffff;
388 ciph->dbc = packets++ % 256;
389 pad_acc += pad_rate[system].n;
390 if (pad_acc >= pad_rate[system].d) {
391 pad_acc -= pad_rate[system].d;
392 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
393 hdr[nhdr][0] = iso_empty;
394 wbuf[vec].iov_base = (char *)hdr[nhdr];
395 wbuf[vec++].iov_len = sizeof(hdr[0]);
396 nhdr ++;
397 cycle ++;
399 hdr[nhdr][0] = iso_data;
400 wbuf[vec].iov_base = (char *)hdr[nhdr];
401 wbuf[vec++].iov_len = sizeof(hdr[0]);
402 wbuf[vec].iov_base = (char *)dv;
403 wbuf[vec++].iov_len = DSIZE;
404 nhdr ++;
405 cycle ++;
406 offset ++;
407 if (offset * DSIZE < tlen)
408 goto next;
410 again:
411 len = writev(d, wbuf, vec);
412 if (len < 0) {
413 if (errno == EAGAIN) {
414 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
415 goto again;
417 err(1, "write failed");
420 close(fd);
421 fprintf(stderr, "\n");
422 send_end:
423 gettimeofday(&end, NULL);
424 rtime = end.tv_sec - start.tv_sec
425 + (end.tv_usec - start.tv_usec) * 1e-6;
426 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
427 frames, rtime, frames/rtime);