cdplay.c revision 1.44 1 /* $NetBSD: cdplay.c,v 1.44 2011/11/26 23:20:41 christos Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2000, 2001 Andrew Doran.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30 /*
31 * Compact Disc Control Utility, originally by Serge V. Vakulenko
32 * <vak (at) cronyx.ru>. First appeared in FreeBSD under the guise of
33 * cdcontrol(1). Based on the non-X based CD player by Jean-Marc
34 * Zucconi and Andrey A. Chernov. Fixed and further modified on
35 * by Jukka Ukkonen <jau (at) funet.fi>. Lots of fixes and improvements
36 * made subsequently by The NetBSD Project.
37 *
38 * from FreeBSD: cdcontrol.c,v 1.17.2.1 1999/01/31 15:36:01 billf Exp
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: cdplay.c,v 1.44 2011/11/26 23:20:41 christos Exp $");
44 #endif /* not lint */
45
46 #include <sys/types.h>
47
48 #include <sys/endian.h>
49 #include <sys/ioctl.h>
50 #include <sys/file.h>
51 #include <sys/cdio.h>
52 #include <sys/time.h>
53 #include <sys/audioio.h>
54 #include <sys/scsiio.h>
55
56 #include <assert.h>
57
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <histedit.h>
62 #include <limits.h>
63 #include <sched.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <util.h>
70
71 enum cmd {
72 CMD_ANALOG,
73 CMD_CLOSE,
74 CMD_DIGITAL,
75 CMD_EJECT,
76 CMD_HELP,
77 CMD_INFO,
78 CMD_NEXT,
79 CMD_PAUSE,
80 CMD_PLAY,
81 CMD_PREV,
82 CMD_QUIT,
83 CMD_RESET,
84 CMD_RESUME,
85 CMD_SET,
86 CMD_SHUFFLE,
87 CMD_SINGLE,
88 CMD_SKIP,
89 CMD_STATUS,
90 CMD_STOP,
91 CMD_VOLUME,
92 };
93
94 static struct cmdtab {
95 enum cmd command;
96 const char *name;
97 unsigned int min;
98 const char *args;
99 } const cmdtab[] = {
100 { CMD_ANALOG, "analog", 1, NULL },
101 { CMD_CLOSE, "close", 1, NULL },
102 { CMD_DIGITAL, "digital", 1, "fpw" },
103 { CMD_EJECT, "eject", 1, NULL },
104 { CMD_HELP, "?", 1, 0 },
105 { CMD_HELP, "help", 1, NULL },
106 { CMD_INFO, "info", 1, NULL },
107 { CMD_NEXT, "next", 1, NULL },
108 { CMD_PAUSE, "pause", 2, NULL },
109 { CMD_PLAY, "play", 1, "[#block [len]]" },
110 { CMD_PLAY, "play", 1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" },
111 { CMD_PLAY, "play", 1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" },
112 { CMD_PLAY, "play", 1, "track1[.index1] [track2[.index2]]" },
113 { CMD_PREV, "prev", 2, NULL },
114 { CMD_QUIT, "quit", 1, NULL },
115 { CMD_RESET, "reset", 4, NULL },
116 { CMD_RESUME, "resume", 4, NULL },
117 { CMD_SET, "set", 2, "msf | lba" },
118 { CMD_SHUFFLE, "shuffle", 2, NULL },
119 { CMD_SINGLE, "single", 2, "[<track>]" },
120 { CMD_SKIP, "skip", 2, NULL },
121 { CMD_STATUS, "status", 3, NULL },
122 { CMD_STOP, "stop", 3, NULL },
123 { CMD_VOLUME, "volume", 1, "<l> <r>|left|right|mute|mono|stereo" },
124 };
125
126 #define IOCTL_SIMPLE(fd, ctl) \
127 do { \
128 if (ioctl((fd), (ctl)) >= 0) { \
129 close(fd); \
130 fd = -1; \
131 } else \
132 warn("ioctl(" #ctl ")");\
133 } while (/* CONSTCOND */ 0)
134
135 #define CDDA_SIZE 2352
136
137 #define CD_MAX_TRACK 99 /* largest 2 digit BCD number */
138
139 static struct cd_toc_entry toc_buffer[CD_MAX_TRACK + 1];
140
141 static const char *cdname;
142 static int fd = -1;
143 static int msf = 1;
144 static int shuffle;
145 static int interactive = 1;
146 static int digital = 0;
147 static int tbvalid = 0;
148 static struct itimerval itv_timer;
149
150 static struct {
151 const char *auname;
152 u_char *audata, *aubuf;
153 int afd;
154 int fpw;
155 int lba_start, lba_end, lba_current;
156 int lowat, hiwat, readseek, playseek;
157 int playing, changed;
158 int read_errors;
159 } da;
160
161 static History *hist;
162 static HistEvent he;
163 static EditLine *elptr;
164
165 static int get_status(int *, int *, int *, int *, int *);
166 static void help(void);
167 static int info(const char *);
168 static void lba2msf(u_long, u_int *, u_int *, u_int *);
169 static u_int msf2lba(u_int, u_int, u_int);
170 static int opencd(void);
171 static int openaudio(void);
172 static const char *parse(char *, int *);
173 static int play(const char *, int);
174 static int play_blocks(int, int);
175 static int play_digital(int, int);
176 static int play_msf(int, int, int, int, int, int);
177 static int play_track(int, int, int, int);
178 static int print_status(const char *);
179 static void print_track(struct cd_toc_entry *);
180 static const char *prompt(void);
181 static int readaudio(int, int, int, u_char *);
182 static int read_toc_entrys(int);
183 static int run(int, const char *);
184 static int start_analog(void);
185 static int start_digital(const char *);
186 static int setvol(int, int);
187 static void sig_timer(int);
188 static int skip(int, int);
189 static const char *strstatus(int);
190 __dead static void usage(void);
191
192 static void toc2msf(u_int, u_int *, u_int *, u_int *);
193 static int toc2lba(u_int);
194 static void addmsf(u_int *, u_int *, u_int *, u_int, u_int, u_int);
195
196 int
197 main(int argc, char **argv)
198 {
199 const char *arg;
200 char buf[80], *p;
201 static char defdev[16];
202 int cmd, len, c;
203 char *line;
204 const char *elline;
205 int scratch, rv;
206 struct sigaction sa_timer;
207
208 cdname = getenv("MUSIC_CD");
209 if (cdname == NULL)
210 cdname = getenv("CD_DRIVE");
211 if (cdname == NULL)
212 cdname = getenv("DISC");
213 if (cdname == NULL)
214 cdname = getenv("CDPLAY");
215
216 da.auname = getenv("AUDIODEV");
217 if (!da.auname)
218 da.auname = getenv("SPEAKER");
219 if (!da.auname)
220 da.auname = "/dev/sound";
221
222 while ((c = getopt(argc, argv, "a:f:h")) != -1)
223 switch (c) {
224 case 'a':
225 da.auname = optarg;
226 continue;
227 case 'f':
228 cdname = optarg;
229 continue;
230 case 'h':
231 default:
232 usage();
233 /* NOTREACHED */
234 }
235 argc -= optind;
236 argv += optind;
237
238 if (argc > 0 && strcasecmp(*argv, "help") == 0)
239 usage();
240
241 if (cdname == NULL) {
242 snprintf(defdev, sizeof(defdev), "cd0%c",
243 'a' + getrawpartition());
244 cdname = defdev;
245 }
246
247 opencd();
248 srandom((u_long)time(NULL));
249 da.afd = -1;
250
251 if (argc > 0) {
252 interactive = 0;
253 for (p = buf; argc-- > 0; argv++) {
254 len = strlen(*argv);
255
256 if (p + len >= buf + sizeof(buf) - 1)
257 usage();
258 if (p > buf)
259 *p++ = ' ';
260
261 strlcpy(p, *argv, sizeof(buf) - (p - buf));
262 p += len;
263 }
264 *p = '\0';
265 arg = parse(buf, &cmd);
266 return (run(cmd, arg));
267 }
268
269 setbuf(stdout, NULL);
270 printf("Type `?' for command list\n\n");
271
272 hist = history_init();
273 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
274 elptr = el_init(getprogname(), stdin, stdout, stderr);
275 el_set(elptr, EL_EDITOR, "emacs");
276 el_set(elptr, EL_PROMPT, prompt);
277 el_set(elptr, EL_HIST, history, hist);
278 el_set(elptr, EL_SIGNAL, 1);
279 el_source(elptr, NULL);
280
281 sigemptyset(&sa_timer.sa_mask);
282 sa_timer.sa_handler = sig_timer;
283 sa_timer.sa_flags = SA_RESTART;
284 if ((rv = sigaction(SIGALRM, &sa_timer, NULL)) < 0)
285 err(EXIT_FAILURE, "sigaction()");
286
287 for (;;) {
288 line = NULL;
289 arg = NULL;
290 do {
291 if (((elline = el_gets(elptr, &scratch)) != NULL)
292 && (scratch != 0)){
293 history(hist, &he, H_ENTER, elline);
294 line = strdup(elline);
295 if (line != NULL) {
296 arg = parse(line, &cmd);
297 free(line);
298 }
299 } else {
300 cmd = CMD_QUIT;
301 warnx("\r\n");
302 arg = NULL;
303 break;
304 }
305 } while (arg == NULL);
306
307 if (run(cmd, arg) < 0) {
308 if (fd != -1)
309 close(fd);
310 fd = -1;
311 }
312 }
313
314 el_end(elptr);
315 history_end(hist);
316 exit(EXIT_SUCCESS);
317 /* NOTREACHED */
318 }
319
320 static void
321 usage(void)
322 {
323
324 fprintf(stderr, "usage: cdplay [-a audio_device] [-f cd_device] [command ...]\n");
325 exit(EXIT_FAILURE);
326 /* NOTREACHED */
327 }
328
329 static void
330 help(void)
331 {
332 const struct cmdtab *c, *mc;
333 const char *s;
334 int i, n;
335
336 mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
337 for (c = cmdtab; c < mc; c++) {
338 for (i = c->min, s = c->name; *s != '\0'; s++, i--) {
339 n = (i > 0 ? toupper((unsigned char)*s) : *s);
340 putchar(n);
341 }
342 if (c->args != NULL)
343 printf(" %s", c->args);
344 putchar('\n');
345 }
346 printf(
347 "\nThe word \"play\" is not required for the play commands.\n"
348 "The plain target address is taken as a synonym for play.\n");
349 }
350
351 static int
352 start_digital(const char *arg)
353 {
354
355 int fpw, intv_usecs, hz_usecs, rv;
356
357 fpw = atoi(arg);
358 if (fpw > 0)
359 da.fpw = fpw;
360 else
361 da.fpw = 5;
362 da.read_errors = 0;
363
364 /* real rate: 75 frames per second */
365 intv_usecs = 13333 * da.fpw;
366 /*
367 * interrupt earlier for safety, by a value which
368 * doesn't hurt interactice response if we block
369 * in the signal handler
370 */
371 intv_usecs -= 50000;
372 hz_usecs = 1000000 / sysconf(_SC_CLK_TCK);
373 if (intv_usecs < hz_usecs) {
374 /* can't have a shorter interval, increase
375 buffer size to compensate */
376 da.fpw += (hz_usecs - intv_usecs) / 13333;
377 intv_usecs = hz_usecs;
378 }
379
380 da.aubuf = malloc(da.fpw * CDDA_SIZE);
381 if (da.aubuf == NULL) {
382 warn("Not enough memory for audio buffers");
383 return (1);
384 }
385 if (da.afd == -1 && !openaudio()) {
386 warn("Cannot open audio device");
387 return (1);
388 }
389 itv_timer.it_interval.tv_sec = itv_timer.it_value.tv_sec =
390 intv_usecs / 1000000;
391 itv_timer.it_interval.tv_usec = itv_timer.it_value.tv_usec =
392 intv_usecs % 1000000;
393 rv = setitimer(ITIMER_REAL, &itv_timer, NULL);
394 if (rv == 0) {
395 digital = 1;
396 } else
397 warn("setitimer in CMD_DIGITAL");
398 msf = 0;
399 tbvalid = 0;
400 return rv;
401 }
402
403 static int
404 start_analog(void)
405 {
406 int rv;
407 if (shuffle == 1)
408 itv_timer.it_interval.tv_sec = itv_timer.it_value.tv_sec = 1;
409 else
410 itv_timer.it_interval.tv_sec = itv_timer.it_value.tv_sec = 0;
411 itv_timer.it_interval.tv_usec = itv_timer.it_value.tv_usec = 0;
412 digital = 0;
413 rv = setitimer(ITIMER_REAL, &itv_timer, NULL);
414 free(da.audata);
415 close(da.afd);
416 da.afd = -1;
417 return rv;
418 }
419
420 static int
421 run(int cmd, const char *arg)
422 {
423 int l, r, rv;
424
425 rv = 0;
426 if (cmd == CMD_QUIT) {
427 close(fd);
428 exit(EXIT_SUCCESS);
429 /* NOTREACHED */
430 }
431
432 if (fd < 0 && !opencd())
433 return (0);
434
435 switch (cmd) {
436 case CMD_INFO:
437 rv = info(arg);
438 break;
439
440 case CMD_STATUS:
441 rv = print_status(arg);
442 break;
443
444 case CMD_PAUSE:
445 if (digital) {
446 da.playing = 0;
447 return (0);
448 } else if ((rv = ioctl(fd, CDIOCPAUSE)) < 0)
449 warn("ioctl(CDIOCPAUSE)");
450 break;
451
452 case CMD_RESUME:
453 if (digital) {
454 da.playing = 1;
455 return (0);
456 } else if ((rv = ioctl(fd, CDIOCRESUME)) < 0)
457 warn("ioctl(CDIOCRESUME)");
458 break;
459
460 case CMD_STOP:
461 if (digital) {
462 da.playing = 0;
463 return (0);
464 } else {
465 if ((rv = ioctl(fd, CDIOCSTOP)) < 0)
466 warn("ioctl(CDIOCSTOP)");
467 if (ioctl(fd, CDIOCALLOW) < 0)
468 warn("ioctl(CDIOCALLOW)");
469 }
470 break;
471
472 case CMD_RESET:
473 tbvalid = 0;
474 IOCTL_SIMPLE(fd, CDIOCRESET);
475 return (0);
476
477 case CMD_EJECT:
478 tbvalid = 0;
479 if (digital)
480 da.playing = 0;
481 if (shuffle)
482 run(CMD_SHUFFLE, NULL);
483 if (ioctl(fd, CDIOCALLOW) < 0)
484 warn("ioctl(CDIOCALLOW)");
485 IOCTL_SIMPLE(fd, CDIOCEJECT);
486 break;
487
488 case CMD_CLOSE:
489 ioctl(fd, CDIOCALLOW);
490 IOCTL_SIMPLE(fd, CDIOCCLOSE);
491 if (interactive && fd == -1)
492 opencd();
493 break;
494
495 case CMD_PLAY:
496 while (isspace((unsigned char)*arg))
497 arg++;
498 rv = play(arg, 1);
499 break;
500
501 case CMD_PREV:
502 rv = skip(-1, 1);
503 break;
504
505 case CMD_NEXT:
506 rv = skip(1, 1);
507 break;
508
509 case CMD_SINGLE:
510 if (interactive == 0)
511 errx(EXIT_FAILURE,
512 "'single' valid only in interactive mode");
513 /*FALLTHROUGH*/
514 case CMD_SHUFFLE:
515 if (interactive == 0)
516 errx(EXIT_FAILURE,
517 "`shuffle' valid only in interactive mode");
518 if (shuffle == 0) {
519 if (digital == 0) {
520 itv_timer.it_interval.tv_sec = 1;
521 itv_timer.it_interval.tv_usec = 0;
522 itv_timer.it_value.tv_sec = 1;
523 itv_timer.it_value.tv_usec = 0;
524 if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0) {
525 if (cmd == CMD_SHUFFLE) {
526 shuffle = 1;
527 } else {
528 while (isspace((unsigned char)*arg))
529 arg++;
530 shuffle = -atoi(arg);
531 }
532 }
533 } else
534 shuffle = cmd == CMD_SINGLE ? -atoi(arg) : 1;
535 /*
536 if (shuffle)
537 rv = skip(0, 1);
538 */
539 } else {
540 if (digital == 0) {
541 itv_timer.it_interval.tv_sec = 0;
542 itv_timer.it_interval.tv_usec = 0;
543 itv_timer.it_value.tv_sec = 0;
544 itv_timer.it_value.tv_usec = 0;
545 setitimer(ITIMER_REAL, &itv_timer, NULL);
546 }
547 shuffle = 0;
548 }
549 if (shuffle < 0)
550 printf("single track:\t%d\n", -shuffle);
551 else
552 printf("shuffle play:\t%s\n", (shuffle != 0) ? "on" : "off");
553 rv = 0;
554 break;
555
556 case CMD_DIGITAL:
557 if (digital == 0)
558 rv = start_digital(arg);
559 else {
560 warnx("Already in digital mode");
561 rv = 1;
562 }
563 break;
564
565 case CMD_ANALOG:
566 if (digital == 1)
567 rv = start_analog();
568 else {
569 warnx("Already in analog mode");
570 rv = 1;
571 }
572 break;
573
574 case CMD_SKIP:
575 if (!interactive)
576 errx(EXIT_FAILURE,
577 "`skip' valid only in interactive mode");
578 if (!shuffle)
579 warnx("`skip' valid only in shuffle mode");
580 else
581 skip(0, 1);
582 break;
583
584 case CMD_SET:
585 tbvalid = 0;
586 if (strcasecmp(arg, "msf") == 0)
587 msf = 1;
588 else if (strcasecmp(arg, "lba") == 0)
589 msf = 0;
590 else
591 warnx("invalid command arguments");
592 break;
593
594 case CMD_VOLUME:
595 if (digital) {
596 rv = 0;
597 warnx("`volume' is ignored while in digital xfer mode");
598 } else if (strncasecmp(arg, "left", strlen(arg)) == 0)
599 rv = ioctl(fd, CDIOCSETLEFT);
600 else if (strncasecmp(arg, "right", strlen(arg)) == 0)
601 rv = ioctl(fd, CDIOCSETRIGHT);
602 else if (strncasecmp(arg, "mono", strlen(arg)) == 0)
603 rv = ioctl(fd, CDIOCSETMONO);
604 else if (strncasecmp(arg, "stereo", strlen(arg)) == 0)
605 rv = ioctl(fd, CDIOCSETSTEREO);
606 else if (strncasecmp(arg, "mute", strlen(arg)) == 0)
607 rv = ioctl(fd, CDIOCSETMUTE);
608 else {
609 rv = 0;
610 if (sscanf(arg, "%d %d", &l, &r) != 2) {
611 if (sscanf(arg, "%d", &l) == 1)
612 r = l;
613 else {
614 warnx("invalid command arguments");
615 break;
616 }
617 }
618 rv = setvol(l, r);
619 }
620 break;
621
622 case CMD_HELP:
623 default:
624 help();
625 rv = 0;
626 break;
627 }
628
629 return (rv);
630 }
631
632 static int
633 play(const char *arg, int fromuser)
634 {
635 int rv, start, end, istart, iend, blk, len, relend;
636 u_int n, tr1, tr2, m1, m2, s1, s2, f1, f2, tm, ts, tf;
637 struct ioc_toc_header h;
638
639 if (shuffle && fromuser) {
640 warnx("`play' not valid in shuffle mode");
641 return (0);
642 }
643
644 if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
645 warn("ioctl(CDIOREADTOCHEADER)");
646 return (rv);
647 }
648
649 end = 0;
650 istart = iend = 1;
651 n = h.ending_track - h.starting_track + 1;
652 rv = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
653 if (rv < 0)
654 return (rv);
655
656 if (arg == NULL || *arg == '\0') {
657 /* Play the whole disc */
658 return (play_track(h.starting_track, 1, h.ending_track, 99));
659 }
660
661 if (strchr(arg, '#') != NULL) {
662 /* Play block #blk [ len ] */
663 len = 0;
664
665 if (2 != sscanf(arg, "#%d%d", &blk, &len) &&
666 1 != sscanf(arg, "#%d", &blk))
667 goto Clean_up;
668
669 if (len == 0) {
670 len = toc2lba(n);
671 }
672 return (play_blocks(blk, len));
673 }
674
675 if (strchr(arg, ':') != NULL) {
676 /*
677 * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
678 *
679 * Will now also undestand timed addresses relative
680 * to the beginning of a track in the form...
681 *
682 * tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
683 */
684 relend = 1;
685 tr2 = m2 = s2 = f2 = f1 = 0;
686 if (8 == sscanf(arg, "%d %d:%d.%d %d %d:%d.%d", &tr1, &m1,
687 &s1, &f1, &tr2, &m2, &s2, &f2))
688 goto Play_Relative_Addresses;
689
690 tr2 = m2 = s2 = f2 = f1 = 0;
691 if (7 == sscanf(arg, "%d %d:%d %d %d:%d.%d", &tr1, &m1, &s1,
692 &tr2, &m2, &s2, &f2))
693 goto Play_Relative_Addresses;
694
695 tr2 = m2 = s2 = f2 = f1 = 0;
696 if (7 == sscanf(arg, "%d %d:%d.%d %d %d:%d", &tr1, &m1, &s1,
697 &f1, &tr2, &m2, &s2))
698 goto Play_Relative_Addresses;
699
700 tr2 = m2 = s2 = f2 = f1 = 0;
701 if (7 == sscanf(arg, "%d %d:%d.%d %d:%d.%d", &tr1, &m1, &s1,
702 &f1, &m2, &s2, &f2))
703 goto Play_Relative_Addresses;
704
705 tr2 = m2 = s2 = f2 = f1 = 0;
706 if (6 == sscanf(arg, "%d %d:%d.%d %d:%d", &tr1, &m1, &s1, &f1,
707 &m2, &s2))
708 goto Play_Relative_Addresses;
709
710 tr2 = m2 = s2 = f2 = f1 = 0;
711 if (6 == sscanf(arg, "%d %d:%d %d:%d.%d", &tr1, &m1, &s1, &m2,
712 &s2, &f2))
713 goto Play_Relative_Addresses;
714
715 tr2 = m2 = s2 = f2 = f1 = 0;
716 if (6 == sscanf(arg, "%d %d:%d.%d %d %d", &tr1, &m1, &s1, &f1,
717 &tr2, &m2))
718 goto Play_Relative_Addresses;
719
720 tr2 = m2 = s2 = f2 = f1 = 0;
721 if (6 == sscanf(arg, "%d %d:%d %d %d:%d", &tr1, &m1, &s1, &tr2,
722 &m2, &s2))
723 goto Play_Relative_Addresses;
724
725 tr2 = m2 = s2 = f2 = f1 = 0;
726 if (5 == sscanf(arg, "%d %d:%d %d:%d", &tr1, &m1, &s1, &m2,
727 &s2))
728 goto Play_Relative_Addresses;
729
730 tr2 = m2 = s2 = f2 = f1 = 0;
731 if (5 == sscanf(arg, "%d %d:%d %d %d", &tr1, &m1, &s1, &tr2,
732 &m2))
733 goto Play_Relative_Addresses;
734
735 relend=0;
736 tr2 = m2 = s2 = f2 = f1 = 0;
737 if (5 == sscanf(arg, "%d %d:%d.%d %d", &tr1, &m1, &s1, &f1,
738 &tr2))
739 goto Play_Relative_Addresses;
740
741 tr2 = m2 = s2 = f2 = f1 = 0;
742 if (4 == sscanf(arg, "%d %d:%d %d", &tr1, &m1, &s1, &tr2))
743 goto Play_Relative_Addresses;
744
745 tr2 = m2 = s2 = f2 = f1 = 0;
746 if (4 == sscanf(arg, "%d %d:%d.%d", &tr1, &m1, &s1, &f1))
747 goto Play_Relative_Addresses;
748
749 tr2 = m2 = s2 = f2 = f1 = 0;
750 if (3 == sscanf(arg, "%d %d:%d", &tr1, &m1, &s1))
751 goto Play_Relative_Addresses;
752
753 tr2 = m2 = s2 = f2 = f1 = 0;
754 goto Try_Absolute_Timed_Addresses;
755
756 Play_Relative_Addresses:
757 if (!tr1)
758 tr1 = 1;
759 else if (tr1 > n)
760 tr1 = n;
761
762 toc2msf(tr1-1, &tm, &ts, &tf);
763 addmsf(&m1, &s1, &f1, tm, ts, tf);
764
765 toc2msf(tr1, &tm, &ts, &tf);
766
767 if ((m1 > tm) || ((m1 == tm) && ((s1 > ts) || ((s1 == ts) &&
768 (f1 > tf))))) {
769 warnx("Track %d is not that long.", tr1);
770 return (0);
771 }
772 tr1--; /* XXXXX ???? */
773
774
775 if (!tr2) {
776 if (relend) {
777 tr2 = tr1;
778
779 addmsf(&m2, &s2, &f2, m1, s1, f1);
780 } else {
781 tr2 = n;
782
783 toc2msf(n, &m2, &s2, &f2);
784 }
785 } else {
786 if (tr2 > n) {
787 tr2 = n;
788 m2 = s2 = f2 = 0;
789 } else {
790 if (relend)
791 tr2--;
792
793 toc2msf(tr2, &tm, &ts, &tf);
794 addmsf(&m2, &s2, &f2, tm, ts, tf);
795 }
796 }
797
798 toc2msf(n, &tm, &ts, &tf);
799
800 if ((tr2 < n) && ((m2 > tm) || ((m2 == tm) && ((s2 > ts) ||
801 ((s2 == ts) && (f2 > tf)))))) {
802 warnx("The playing time of the disc is not that long.");
803 return (0);
804 }
805
806 return (play_msf(m1, s1, f1, m2, s2, f2));
807
808 Try_Absolute_Timed_Addresses:
809 m2 = UINT_MAX;
810
811 if (6 != sscanf(arg, "%d:%d.%d%d:%d.%d",
812 &m1, &s1, &f1, &m2, &s2, &f2) &&
813 5 != sscanf(arg, "%d:%d.%d%d:%d", &m1, &s1, &f1, &m2, &s2) &&
814 5 != sscanf(arg, "%d:%d%d:%d.%d", &m1, &s1, &m2, &s2, &f2) &&
815 3 != sscanf(arg, "%d:%d.%d", &m1, &s1, &f1) &&
816 4 != sscanf(arg, "%d:%d%d:%d", &m1, &s1, &m2, &s2) &&
817 2 != sscanf(arg, "%d:%d", &m1, &s1))
818 goto Clean_up;
819
820 if (m2 == UINT_MAX) {
821 if (msf) {
822 m2 = toc_buffer[n].addr.msf.minute;
823 s2 = toc_buffer[n].addr.msf.second;
824 f2 = toc_buffer[n].addr.msf.frame;
825 } else {
826 lba2msf(toc_buffer[n].addr.lba, &tm, &ts, &tf);
827 m2 = tm;
828 s2 = ts;
829 f2 = tf;
830 }
831 }
832 return (play_msf(m1, s1, f1, m2, s2, f2));
833 }
834
835 /*
836 * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
837 */
838 if (4 != sscanf(arg, "%d.%d%d.%d", &start, &istart, &end, &iend) &&
839 3 != sscanf(arg, "%d.%d%d", &start, &istart, &end) &&
840 3 != sscanf(arg, "%d%d.%d", &start, &end, &iend) &&
841 2 != sscanf(arg, "%d.%d", &start, &istart) &&
842 2 != sscanf(arg, "%d%d", &start, &end) &&
843 1 != sscanf(arg, "%d", &start))
844 goto Clean_up;
845
846 if (end == 0)
847 end = n;
848 return (play_track(start, istart, end, iend));
849
850 Clean_up:
851 warnx("invalid command arguments");
852 return (0);
853 }
854
855 static void
856 sig_timer(int sig)
857 {
858 int aulen, auwr, fpw;
859 sigset_t anymore;
860
861 sigpending(&anymore);
862 if (sigismember(&anymore, SIGALRM))
863 return;
864 if (digital) {
865 if (!da.playing)
866 return;
867 if (da.changed) {
868 da.lba_current = da.lba_start;
869 da.changed = 0;
870 }
871 /* read frames into circular buffer */
872 fpw = da.lba_end - da.lba_current + 1;
873 if (fpw > da.fpw)
874 fpw = da.fpw;
875 if (fpw > 0) {
876 aulen = readaudio(fd, da.lba_current, fpw, da.aubuf);
877 if (aulen > 0) {
878 auwr = write(da.afd, da.aubuf, aulen);
879 da.lba_current += fpw;
880 }
881 }
882 if (da.lba_current > da.lba_end)
883 da.playing = 0;
884 }
885 if (shuffle)
886 skip(0, 0);
887 #if 0
888 sched_yield();
889 #endif
890 setitimer(ITIMER_REAL, &itv_timer, NULL);
891 }
892
893 static int
894 skip(int dir, int fromuser)
895 {
896 char str[16];
897 int rv, trk, idx, m, s, f;
898 struct ioc_toc_header h;
899
900 if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
901 warn("ioctl(CDIOREADTOCHEADER)");
902 return (rv);
903 }
904 if ((rv = get_status(&trk, &idx, &m, &s, &f)) < 0)
905 return (rv);
906
907 if (dir == 0 || shuffle != 0) {
908 if (fromuser || (rv != CD_AS_PLAY_IN_PROGRESS &&
909 rv != CD_AS_PLAY_PAUSED))
910 trk = shuffle < 0 ? (-shuffle) :
911 (int)((h.starting_track +
912 arc4random() % (h.ending_track - h.starting_track + 1)));
913 else
914 return (0);
915 } else {
916 trk += dir;
917 if (trk > h.ending_track)
918 trk = h.starting_track;
919 else if(trk < h.starting_track)
920 trk = h.ending_track;
921 }
922
923 if (shuffle)
924 snprintf(str, sizeof(str), "%d %d", trk, trk);
925 else
926 snprintf(str, sizeof(str), "%d", trk);
927
928 return (play(str, 0));
929 }
930
931 static const char *
932 strstatus(int sts)
933 {
934 const char *str;
935
936 switch (sts) {
937 case CD_AS_AUDIO_INVALID:
938 str = "invalid";
939 break;
940 case CD_AS_PLAY_IN_PROGRESS:
941 str = "playing";
942 break;
943 case CD_AS_PLAY_PAUSED:
944 str = "paused";
945 break;
946 case CD_AS_PLAY_COMPLETED:
947 str = "completed";
948 break;
949 case CD_AS_PLAY_ERROR:
950 str = "error";
951 break;
952 case CD_AS_NO_STATUS:
953 str = "not playing";
954 break;
955 default:
956 str = "<unknown>";
957 break;
958 }
959
960 return (str);
961 }
962
963 static int
964 print_status(const char *arg)
965 {
966 struct cd_sub_channel_info data;
967 struct ioc_read_subchannel ss;
968 int rv, trk, idx, m, s, f;
969 struct ioc_vol v;
970
971 if ((rv = get_status(&trk, &idx, &m, &s, &f)) >= 0) {
972 printf("audio status:\t%s\n", strstatus(rv));
973 printf("current track:\t%d\n", trk);
974 if (!digital)
975 printf("current index:\t%d\n", idx);
976 printf("position:\t%d:%02d.%02d\n", m, s, f);
977 } else
978 printf("audio status:\tno info available\n");
979
980 if (shuffle < 0)
981 printf("single track:\t%d\n", -shuffle);
982 else
983 printf("shuffle play:\t%s\n", (shuffle != 0) ? "on" : "off");
984 if (digital)
985 printf("digital xfer:\tto %s "
986 "(%d frames per wakeup, %lld.%06lds period)\n",
987 da.auname, da.fpw,
988 (long long)itv_timer.it_interval.tv_sec,
989
990 (long)itv_timer.it_interval.tv_usec);
991 else
992 printf("digital xfer:\toff\n");
993
994 bzero(&ss, sizeof(ss));
995 ss.data = &data;
996 ss.data_len = sizeof(data);
997 ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
998 ss.data_format = CD_MEDIA_CATALOG;
999
1000 if (!digital && ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &ss) >= 0) {
1001 printf("media catalog:\t%sactive",
1002 ss.data->what.media_catalog.mc_valid ? "" : "in");
1003 if (ss.data->what.media_catalog.mc_valid &&
1004 ss.data->what.media_catalog.mc_number[0])
1005 printf(" (%.15s)",
1006 ss.data->what.media_catalog.mc_number);
1007 putchar('\n');
1008 } else
1009 printf("media catalog:\tnone\n");
1010
1011 if (digital)
1012 return (0);
1013 if (ioctl(fd, CDIOCGETVOL, &v) >= 0) {
1014 printf("left volume:\t%d\n", v.vol[0]);
1015 printf("right volume:\t%d\n", v.vol[1]);
1016 } else {
1017 printf("left volume:\tnot available\n");
1018 printf("right volume:\tnot available\n");
1019 }
1020
1021 ; return (0);
1022 }
1023
1024 static int
1025 info(const char *arg)
1026 {
1027 struct ioc_toc_header h;
1028 int rc, i, n;
1029
1030 if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
1031 warn("ioctl(CDIOREADTOCHEADER)");
1032 return (rc);
1033 }
1034
1035 n = h.ending_track - h.starting_track + 1;
1036 rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
1037 if (rc < 0)
1038 return (rc);
1039
1040 printf("track start duration block length type\n");
1041 printf("--------------------------------------------------\n");
1042
1043 for (i = 0; i < n; i++) {
1044 printf("%5d ", toc_buffer[i].track);
1045 print_track(toc_buffer + i);
1046 }
1047 printf(" - "); /* Lead-out area */
1048 print_track(toc_buffer + n);
1049 return (0);
1050 }
1051
1052 static void
1053 lba2msf(u_long lba, u_int *m, u_int *s, u_int *f)
1054 {
1055
1056 lba += 150; /* block start offset */
1057 lba &= 0xffffff; /* negative lbas use only 24 bits */
1058 *m = lba / (60 * 75);
1059 lba %= (60 * 75);
1060 *s = lba / 75;
1061 *f = lba % 75;
1062 }
1063
1064 static u_int
1065 msf2lba(u_int m, u_int s, u_int f)
1066 {
1067
1068 return (((m * 60) + s) * 75 + f) - 150;
1069 }
1070
1071 static void
1072 print_track(struct cd_toc_entry *e)
1073 {
1074 int block, next, len;
1075 u_int m, s, f;
1076
1077 if (msf) {
1078 /* Print track start */
1079 printf("%2d:%02d.%02d ", e->addr.msf.minute,
1080 e->addr.msf.second, e->addr.msf.frame);
1081
1082 block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
1083 e->addr.msf.frame);
1084 } else {
1085 block = e->addr.lba;
1086 lba2msf(block, &m, &s, &f);
1087 /* Print track start */
1088 printf("%2d:%02d.%02d ", m, s, f);
1089 }
1090 if (e->track > CD_MAX_TRACK) {
1091 /* lead-out area -- print block */
1092 printf(" - %6d - lead-out\n", block);
1093 return;
1094 }
1095 if (msf)
1096 next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
1097 e[1].addr.msf.frame);
1098 else
1099 next = e[1].addr.lba;
1100 len = next - block;
1101 /* XXX: take into account the 150 frame start offset time */
1102 /* XXX: this is a mis-use of lba2msf() because 'len' is a */
1103 /* XXX: length in frames and not a LBA! */
1104 lba2msf(len - 150, &m, &s, &f);
1105
1106 /* Print duration, block, length, type */
1107 printf("%2d:%02d.%02d %6d %6d %8s\n", m, s, f, block, len,
1108 (e->control & 4) ? "data" : "audio");
1109 }
1110
1111 static int
1112 play_track(int tstart, int istart, int tend, int iend)
1113 {
1114 struct ioc_play_track t;
1115 int rv;
1116
1117 if (digital) {
1118 tstart--;
1119 if (msf) {
1120 return (play_msf(toc_buffer[tstart].addr.msf.minute,
1121 toc_buffer[tstart].addr.msf.second, toc_buffer[tstart].addr.msf.frame,
1122 toc_buffer[tend].addr.msf.minute, toc_buffer[tend].addr.msf.second,
1123 toc_buffer[tend].addr.msf.frame));
1124 } else
1125 return (play_digital(toc_buffer[tstart].addr.lba,
1126 toc_buffer[tend].addr.lba));
1127 }
1128 t.start_track = tstart;
1129 t.start_index = istart;
1130 t.end_track = tend;
1131 t.end_index = iend;
1132
1133 if ((rv = ioctl(fd, CDIOCPLAYTRACKS, &t)) < 0) {
1134 int oerrno = errno;
1135 if (errno == EINVAL && start_digital("5") == 0)
1136 return play_track(tstart, istart, tend, iend);
1137 errno = oerrno;
1138 warn("ioctl(CDIOCPLAYTRACKS)");
1139 }
1140 return (rv);
1141 }
1142
1143 static int
1144 play_blocks(int blk, int len)
1145 {
1146 struct ioc_play_blocks t;
1147 int rv;
1148
1149 t.blk = blk;
1150 t.len = len;
1151
1152 if ((rv = ioctl(fd, CDIOCPLAYBLOCKS, &t)) < 0)
1153 warn("ioctl(CDIOCPLAYBLOCKS");
1154 return (rv);
1155 }
1156
1157 static int
1158 play_digital(int start, int end)
1159 {
1160 da.lba_start = start;
1161 da.lba_end = --end;
1162 da.changed = da.playing = 1;
1163 return (0);
1164 }
1165
1166 static int
1167 setvol(int left, int right)
1168 {
1169 struct ioc_vol v;
1170 int rv;
1171
1172 v.vol[0] = left;
1173 v.vol[1] = right;
1174 v.vol[2] = 0;
1175 v.vol[3] = 0;
1176
1177 if ((rv = ioctl(fd, CDIOCSETVOL, &v)) < 0)
1178 warn("ioctl(CDIOCSETVOL)");
1179 return (rv);
1180 }
1181
1182 static int
1183 read_toc_entrys(int len)
1184 {
1185 struct ioc_read_toc_entry t;
1186 int rv;
1187
1188 t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1189 t.starting_track = 0;
1190 t.data_len = len;
1191 t.data = toc_buffer;
1192
1193 if ((rv = ioctl(fd, CDIOREADTOCENTRYS, &t)) < 0)
1194 warn("ioctl(CDIOREADTOCENTRYS)");
1195 tbvalid = 1;
1196 return (rv);
1197 }
1198
1199 static int
1200 play_msf(int start_m, int start_s, int start_f, int end_m, int end_s,
1201 int end_f)
1202 {
1203 struct ioc_play_msf a;
1204 int rv;
1205
1206 if (digital)
1207 return (play_digital(msf2lba(start_m, start_s, start_f),
1208 msf2lba(end_m, end_s, end_f)));
1209 a.start_m = start_m;
1210 a.start_s = start_s;
1211 a.start_f = start_f;
1212 a.end_m = end_m;
1213 a.end_s = end_s;
1214 a.end_f = end_f;
1215
1216 if ((rv = ioctl(fd, CDIOCPLAYMSF, &a)) < 0)
1217 warn("ioctl(CDIOCPLAYMSF)");
1218 return (rv);
1219 }
1220
1221 static int
1222 get_status(int *trk, int *idx, int *min, int *sec, int *frame)
1223 {
1224 struct ioc_read_subchannel s;
1225 struct cd_sub_channel_info data;
1226 struct ioc_toc_header h;
1227 u_int mm, ss, ff;
1228 int rv;
1229 int i, n, rc;
1230 uint32_t lba;
1231
1232 if (!tbvalid) {
1233 if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
1234 warn("ioctl(CDIOREADTOCHEADER)");
1235 return (rc);
1236 }
1237
1238 n = h.ending_track - h.starting_track + 1;
1239 rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
1240 if (rc < 0)
1241 return (rc);
1242 }
1243
1244 #define SWAPLBA(x) (msf?be32toh(x):(x))
1245 if (digital && da.playing) {
1246 lba = da.lba_current + 150;
1247 for (i = 1; i < 99; i++) {
1248 if (lba < SWAPLBA(toc_buffer[i].addr.lba)) {
1249 lba -= SWAPLBA(toc_buffer[i - 1].addr.lba);
1250 *trk = i;
1251 break;
1252 }
1253 }
1254 lba2msf(lba - 150, &mm, &ss, &ff);
1255 *min = mm;
1256 *sec = ss;
1257 *frame = ff;
1258 *idx = 0;
1259 return CD_AS_PLAY_IN_PROGRESS;
1260 }
1261 bzero(&s, sizeof(s));
1262 s.data = &data;
1263 s.data_len = sizeof(data);
1264 s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1265 s.data_format = CD_CURRENT_POSITION;
1266
1267 if ((rv = ioctl(fd, CDIOCREADSUBCHANNEL, &s)) < 0) {
1268 warn("ioctl(CDIOCREADSUBCHANNEL)");
1269 return (rv);
1270 }
1271
1272 *trk = s.data->what.position.track_number;
1273 *idx = s.data->what.position.index_number;
1274 if (msf) {
1275 *min = s.data->what.position.reladdr.msf.minute;
1276 *sec = s.data->what.position.reladdr.msf.second;
1277 *frame = s.data->what.position.reladdr.msf.frame;
1278 } else {
1279 lba2msf(s.data->what.position.reladdr.lba, &mm,
1280 &ss, &ff);
1281 *min = mm;
1282 *sec = ss;
1283 *frame = ff;
1284 }
1285
1286 return (s.data->header.audio_status);
1287 }
1288
1289 static const char *
1290 prompt(void)
1291 {
1292
1293 return ("cdplay> ");
1294 }
1295
1296 static const char *
1297 parse(char *buf, int *cmd)
1298 {
1299 const struct cmdtab *c, *mc;
1300 char *p, *q;
1301 unsigned int len;
1302
1303 for (p = buf; isspace((unsigned char)*p); p++)
1304 continue;
1305
1306 if (isdigit((unsigned char)*p) || (p[0] == '#' && isdigit((unsigned char)p[1]))) {
1307 *cmd = CMD_PLAY;
1308 return (p);
1309 }
1310
1311 for (buf = p; *p != '\0' && !isspace((unsigned char)*p); p++)
1312 continue;
1313
1314 if ((len = p - buf) == 0)
1315 return (0);
1316
1317 if (*p != '\0') { /* It must be a spacing character! */
1318 *p++ = 0;
1319 for (q = p; *q != '\0' && *q != '\n' && *q != '\r'; q++)
1320 continue;
1321 *q = 0;
1322 }
1323
1324 *cmd = -1;
1325
1326 mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
1327 for (c = cmdtab; c < mc; c++) {
1328 /* Is it an exact match? */
1329 if (strcasecmp(buf, c->name) == 0) {
1330 *cmd = c->command;
1331 break;
1332 }
1333 /* Try short hand forms then... */
1334 if (len >= c->min && strncasecmp(buf, c->name, len) == 0) {
1335 if (*cmd != -1 && *cmd != (int)c->command) {
1336 warnx("ambiguous command");
1337 return (0);
1338 }
1339 *cmd = c->command;
1340 }
1341 }
1342
1343 if (*cmd == -1) {
1344 warnx("invalid command, enter ``help'' for commands");
1345 return (0);
1346 }
1347
1348 while (isspace((unsigned char)*p))
1349 p++;
1350 return (p);
1351 }
1352
1353 static int
1354 opencd(void)
1355 {
1356 char devbuf[80];
1357
1358 if (fd > -1)
1359 return (1);
1360
1361 fd = opendisk(cdname, O_RDONLY, devbuf, sizeof(devbuf), 0);
1362 if (fd < 0) {
1363 if (errno == ENXIO) {
1364 /*
1365 * ENXIO has an overloaded meaning here. The
1366 * original "Device not configured" should be
1367 * interpreted as "No disc in drive %s".
1368 */
1369 warnx("no disc in drive %s", devbuf);
1370 return (0);
1371 }
1372 err(EXIT_FAILURE, "%s", devbuf);
1373 }
1374 return (1);
1375 }
1376
1377 static int
1378 openaudio(void)
1379 {
1380 audio_info_t ai;
1381 audio_encoding_t ae;
1382 int rc, aei;
1383
1384 if (da.afd > -1)
1385 return (1);
1386 da.afd = open(da.auname, O_WRONLY);
1387 if (da.afd < 0) {
1388 warn("openaudio");
1389 return (0);
1390 }
1391 AUDIO_INITINFO(&ai);
1392 ae.index = 0;
1393 aei = -1;
1394 rc = ioctl(da.afd, AUDIO_GETENC, &ae);
1395 do {
1396 if (ae.encoding == AUDIO_ENCODING_SLINEAR_LE && ae.precision == 16)
1397 aei = ae.index;
1398 ae.index++;
1399 rc = ioctl(da.afd, AUDIO_GETENC, &ae);
1400 } while (rc == 0);
1401 if (aei == -1) {
1402 warn("No suitable audio encoding found!");
1403 close(da.afd);
1404 da.afd = -1;
1405 return (0);
1406 }
1407 ai.mode = AUMODE_PLAY_ALL;
1408 ai.play.sample_rate = 44100;
1409 ai.play.channels = 2;
1410 ai.play.precision = 16;
1411 ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
1412 ai.blocksize = 0;
1413 rc = ioctl(da.afd, AUDIO_SETINFO, &ai);
1414 if (rc < 0) {
1415 warn("AUDIO_SETINFO");
1416 close(da.afd);
1417 da.afd = -1;
1418 return (0);
1419 }
1420 return (1);
1421 }
1422
1423 static int
1424 readaudio(int afd, int lba, int blocks, u_char *data)
1425 {
1426 struct scsireq sc;
1427 int rc;
1428
1429 memset(&sc, 0, sizeof(sc));
1430 sc.cmd[0] = 0xBE;
1431 sc.cmd[1] = 1 << 2;
1432 sc.cmd[2] = (lba >> 24) & 0xff;
1433 sc.cmd[3] = (lba >> 16) & 0xff;
1434 sc.cmd[4] = (lba >> 8) & 0xff;
1435 sc.cmd[5] = lba & 0xff;
1436 sc.cmd[6] = (blocks >> 16) & 0xff;
1437 sc.cmd[7] = (blocks >> 8) & 0xff;
1438 sc.cmd[8] = blocks & 0xff;
1439 sc.cmd[9] = 1 << 4;
1440 sc.cmd[10] = 0;
1441 sc.cmdlen = 12;
1442 sc.databuf = (caddr_t) data;
1443 sc.datalen = CDDA_SIZE * blocks;
1444 sc.senselen = sizeof(sc.sense);
1445 sc.flags = SCCMD_READ;
1446 sc.timeout = 10000; /* 10s */
1447 rc = ioctl(afd, SCIOCCOMMAND, &sc);
1448 if (rc < 0 || sc.retsts != SCCMD_OK) {
1449 if (da.read_errors < 10) {
1450 warnx("scsi cmd failed: retsts %d status %d\n",
1451 sc.retsts, sc.status);
1452 }
1453 da.read_errors++;
1454 return -1;
1455 }
1456 return CDDA_SIZE * blocks;
1457 }
1458
1459 static void
1460 toc2msf(u_int i, u_int *m, u_int *s, u_int *f)
1461 {
1462 struct cd_toc_entry *ctep;
1463
1464 assert(i <= CD_MAX_TRACK);
1465
1466 ctep = &toc_buffer[i];
1467
1468 if (msf) {
1469 *m = ctep->addr.msf.minute;
1470 *s = ctep->addr.msf.second;
1471 *f = ctep->addr.msf.frame;
1472 } else {
1473 lba2msf(ctep->addr.lba, m, s, f);
1474 }
1475 }
1476
1477 static int
1478 toc2lba(u_int i)
1479 {
1480 struct cd_toc_entry *ctep;
1481
1482 assert(i > 0);
1483 assert(i <= CD_MAX_TRACK);
1484
1485 ctep = &toc_buffer[i-1];
1486
1487 if (msf) {
1488 return msf2lba(
1489 ctep->addr.msf.minute,
1490 ctep->addr.msf.second,
1491 ctep->addr.msf.frame);
1492 } else {
1493 return (ctep->addr.lba);
1494 }
1495 }
1496
1497 static void
1498 addmsf(u_int *m, u_int *s, u_int *f, u_int m2, u_int s2, u_int f2)
1499 {
1500 *f += f2;
1501 if (*f > 75) {
1502 *s += *f / 75;
1503 *f %= 75;
1504 }
1505
1506 *s += s2;
1507 if (*s > 60) {
1508 *m += *s / 60;
1509 *s %= 60;
1510 }
1511
1512 *m += m2;
1513 }
1514