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