scd.c revision 320921
1/*- 2 * Copyright (c) 1995 Mikael Hybsch 3 * All rights reserved. 4 * 5 * Portions of this file are copied from mcd.c 6 * which has the following copyrights: 7 * 8 * Copyright 1993 by Holger Veit (data part) 9 * Copyright 1993 by Brian Moore (audio part) 10 * Changes Copyright 1993 by Gary Clark II 11 * Changes Copyright (C) 1994 by Andrew A. Chernov 12 * 13 * Rewrote probe routine to work on newer Mitsumi drives. 14 * Additional changes (C) 1994 by Jordan K. Hubbard 15 * 16 * All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer 23 * in this position and unchanged. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 3. The name of the author may not be used to endorse or promote products 28 * derived from this software without specific prior written permission 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 */ 42 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: stable/11/sys/dev/scd/scd.c 320921 2017-07-12 20:10:53Z jhb $"); 45 46 47#undef SCD_DEBUG 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/kernel.h> 52#include <sys/conf.h> 53#include <sys/fcntl.h> 54#include <sys/bio.h> 55#include <sys/cdio.h> 56#include <sys/disk.h> 57#include <sys/bus.h> 58 59#include <machine/stdarg.h> 60 61#include <machine/bus.h> 62#include <machine/resource.h> 63#include <sys/rman.h> 64 65#include <isa/isavar.h> 66 67#include <dev/scd/scdreg.h> 68#include <dev/scd/scdvar.h> 69 70/* flags */ 71#define SCDOPEN 0x0001 /* device opened */ 72#define SCDVALID 0x0002 /* parameters loaded */ 73#define SCDINIT 0x0004 /* device is init'd */ 74#define SCDPROBING 0x0020 /* probing */ 75#define SCDTOC 0x0100 /* already read toc */ 76#define SCDMBXBSY 0x0200 /* local mbx is busy */ 77#define SCDSPINNING 0x0400 /* drive is spun up */ 78 79#define SCD_S_BEGIN 0 80#define SCD_S_BEGIN1 1 81#define SCD_S_WAITSTAT 2 82#define SCD_S_WAITFIFO 3 83#define SCD_S_WAITSPIN 4 84#define SCD_S_WAITREAD 5 85#define SCD_S_WAITPARAM 6 86 87#define RDELAY_WAIT 300 88#define RDELAY_WAITREAD 300 89 90#define SCDBLKSIZE 2048 91 92#ifdef SCD_DEBUG 93 static int scd_debuglevel = SCD_DEBUG; 94# define XDEBUG(sc, level, fmt, args...) \ 95 do { \ 96 if (scd_debuglevel >= level) \ 97 device_printf(sc->dev, fmt, ## args); \ 98 } while (0) 99#else 100# define XDEBUG(sc, level, fmt, args...) 101#endif 102 103#define IS_ATTENTION(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_ATTENTION) != 0) 104#define IS_BUSY(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_BUSY) != 0) 105#define IS_DATA_RDY(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_DATA_READY) != 0) 106#define STATUS_BIT(sc, bit) ((SCD_READ(sc, IREG_STATUS) & (bit)) != 0) 107#define FSTATUS_BIT(sc, bit) ((SCD_READ(sc, IREG_FSTATUS) & (bit)) != 0) 108 109/* prototypes */ 110static void hsg2msf(int hsg, bcd_t *msf); 111static int msf2hsg(bcd_t *msf); 112 113static void process_attention(struct scd_softc *); 114static int waitfor_status_bits(struct scd_softc *, int bits_set, int bits_clear); 115static int send_cmd(struct scd_softc *, u_char cmd, u_int nargs, ...); 116static void init_drive(struct scd_softc *); 117static int spin_up(struct scd_softc *); 118static int read_toc(struct scd_softc *); 119static int get_result(struct scd_softc *, int result_len, u_char *result); 120static void print_error(struct scd_softc *, int errcode); 121 122static void scd_start(struct scd_softc *); 123static void scd_timeout(void *); 124static void scd_doread(struct scd_softc *, int state, struct scd_mbx *mbxin); 125 126static int scd_eject(struct scd_softc *); 127static int scd_stop(struct scd_softc *); 128static int scd_pause(struct scd_softc *); 129static int scd_resume(struct scd_softc *); 130static int scd_playtracks(struct scd_softc *, struct ioc_play_track *pt); 131static int scd_playmsf(struct scd_softc *, struct ioc_play_msf *msf); 132static int scd_play(struct scd_softc *, struct ioc_play_msf *msf); 133static int scd_subchan(struct scd_softc *, struct ioc_read_subchannel *sch, int nocopyout); 134static int read_subcode(struct scd_softc *, struct sony_subchannel_position_data *sch); 135 136/* for xcdplayer */ 137static int scd_toc_header(struct scd_softc *, struct ioc_toc_header *th); 138static int scd_toc_entrys(struct scd_softc *, struct ioc_read_toc_entry *te); 139static int scd_toc_entry(struct scd_softc *, struct ioc_read_toc_single_entry *te); 140#define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */ 141 142static d_open_t scdopen; 143static d_close_t scdclose; 144static d_ioctl_t scdioctl; 145static d_strategy_t scdstrategy; 146 147 148static struct cdevsw scd_cdevsw = { 149 .d_version = D_VERSION, 150 .d_open = scdopen, 151 .d_close = scdclose, 152 .d_read = physread, 153 .d_ioctl = scdioctl, 154 .d_strategy = scdstrategy, 155 .d_name = "scd", 156 .d_flags = D_DISK, 157}; 158 159int 160scd_attach(struct scd_softc *sc) 161{ 162 int unit; 163 164 unit = device_get_unit(sc->dev); 165 166 SCD_LOCK(sc); 167 init_drive(sc); 168 169 sc->data.flags = SCDINIT; 170 sc->data.audio_status = CD_AS_AUDIO_INVALID; 171 bioq_init(&sc->data.head); 172 SCD_UNLOCK(sc); 173 174 sc->scd_dev_t = make_dev(&scd_cdevsw, 8 * unit, 175 UID_ROOT, GID_OPERATOR, 0640, "scd%d", unit); 176 sc->scd_dev_t->si_drv1 = (void *)sc; 177 device_printf(sc->dev, 178 "WARNING: This driver is deprecated and will be removed.\n"); 179 180 return (0); 181} 182 183static int 184scdopen(struct cdev *dev, int flags, int fmt, struct thread *td) 185{ 186 struct scd_softc *sc; 187 int rc; 188 189 sc = (struct scd_softc *)dev->si_drv1; 190 191 /* mark all open part's invalid */ 192 SCD_LOCK(sc); 193 if (sc->data.openflag) { 194 SCD_UNLOCK(sc); 195 return (ENXIO); 196 } 197 198 XDEBUG(sc, 1, "DEBUG: status = 0x%x\n", SCD_READ(sc, IREG_STATUS)); 199 200 if ((rc = spin_up(sc)) != 0) { 201 print_error(sc, rc); 202 SCD_UNLOCK(sc); 203 return (EIO); 204 } 205 if (!(sc->data.flags & SCDTOC)) { 206 int loop_count = 3; 207 208 while (loop_count-- > 0 && (rc = read_toc(sc)) != 0) { 209 if (rc == ERR_NOT_SPINNING) { 210 rc = spin_up(sc); 211 if (rc) { 212 print_error(sc, rc); 213 SCD_UNLOCK(sc); 214 return (EIO); 215 } 216 continue; 217 } 218 device_printf(sc->dev, "TOC read error 0x%x\n", rc); 219 SCD_UNLOCK(sc); 220 return (EIO); 221 } 222 } 223 224 sc->data.openflag = 1; 225 sc->data.flags |= SCDVALID; 226 SCD_UNLOCK(sc); 227 228 return (0); 229} 230 231static int 232scdclose(struct cdev *dev, int flags, int fmt, struct thread *td) 233{ 234 struct scd_softc *sc; 235 236 sc = (struct scd_softc *)dev->si_drv1; 237 238 SCD_LOCK(sc); 239 KASSERT(sc->data.openflag, ("device not open")); 240 241 if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS) { 242 (void)send_cmd(sc, CMD_SPIN_DOWN, 0); 243 sc->data.flags &= ~SCDSPINNING; 244 } 245 246 /* close channel */ 247 sc->data.openflag = 0; 248 SCD_UNLOCK(sc); 249 250 return (0); 251} 252 253static void 254scdstrategy(struct bio *bp) 255{ 256 struct scd_softc *sc; 257 258 sc = (struct scd_softc *)bp->bio_dev->si_drv1; 259 260 /* if device invalidated (e.g. media change, door open), error */ 261 SCD_LOCK(sc); 262 if (!(sc->data.flags & SCDVALID)) { 263 device_printf(sc->dev, "media changed\n"); 264 bp->bio_error = EIO; 265 goto bad; 266 } 267 268 /* read only */ 269 if (!(bp->bio_cmd == BIO_READ)) { 270 bp->bio_error = EROFS; 271 goto bad; 272 } 273 274 /* no data to read */ 275 if (bp->bio_bcount == 0) 276 goto done; 277 278 if (!(sc->data.flags & SCDTOC)) { 279 bp->bio_error = EIO; 280 goto bad; 281 } 282 283 bp->bio_resid = 0; 284 285 /* queue it */ 286 bioq_disksort(&sc->data.head, bp); 287 288 /* now check whether we can perform processing */ 289 scd_start(sc); 290 SCD_UNLOCK(sc); 291 return; 292 293bad: 294 bp->bio_flags |= BIO_ERROR; 295done: 296 SCD_UNLOCK(sc); 297 bp->bio_resid = bp->bio_bcount; 298 biodone(bp); 299 return; 300} 301 302static void 303scd_start(struct scd_softc *sc) 304{ 305 struct bio *bp; 306 307 SCD_ASSERT_LOCKED(sc); 308 if (sc->data.flags & SCDMBXBSY) 309 return; 310 311 bp = bioq_takefirst(&sc->data.head); 312 if (bp != 0) { 313 /* block found to process, dequeue */ 314 sc->data.flags |= SCDMBXBSY; 315 } else { 316 /* nothing to do */ 317 return; 318 } 319 320 sc->data.mbx.retry = 3; 321 sc->data.mbx.bp = bp; 322 323 scd_doread(sc, SCD_S_BEGIN, &(sc->data.mbx)); 324 return; 325} 326 327static int 328scdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 329{ 330 struct scd_softc *sc; 331 int error; 332 333 sc = (struct scd_softc *)dev->si_drv1; 334 335 XDEBUG(sc, 1, "ioctl: cmd=0x%lx\n", cmd); 336 337 SCD_LOCK(sc); 338 if (!(sc->data.flags & SCDVALID)) { 339 SCD_UNLOCK(sc); 340 return (EIO); 341 } 342 343 error = 0; 344 switch (cmd) { 345 case DIOCGMEDIASIZE: 346 *(off_t *)addr = (off_t)sc->data.disksize * sc->data.blksize; 347 break; 348 case DIOCGSECTORSIZE: 349 *(u_int *)addr = sc->data.blksize; 350 break; 351 case CDIOCPLAYTRACKS: 352 error = scd_playtracks(sc, (struct ioc_play_track *) addr); 353 break; 354 case CDIOCPLAYBLOCKS: 355 error = EINVAL; 356 break; 357 case CDIOCPLAYMSF: 358 error = scd_playmsf(sc, (struct ioc_play_msf *) addr); 359 break; 360 case CDIOCREADSUBCHANNEL_SYSSPACE: 361 return scd_subchan(sc, (struct ioc_read_subchannel *) addr, 1); 362 case CDIOCREADSUBCHANNEL: 363 return scd_subchan(sc, (struct ioc_read_subchannel *) addr, 0); 364 case CDIOREADTOCHEADER: 365 error = scd_toc_header (sc, (struct ioc_toc_header *) addr); 366 break; 367 case CDIOREADTOCENTRYS: 368 return scd_toc_entrys (sc, (struct ioc_read_toc_entry*) addr); 369 case CDIOREADTOCENTRY: 370 error = scd_toc_entry (sc, (struct ioc_read_toc_single_entry*) addr); 371 break; 372 case CDIOCSETPATCH: 373 case CDIOCGETVOL: 374 case CDIOCSETVOL: 375 case CDIOCSETMONO: 376 case CDIOCSETSTERIO: 377 case CDIOCSETMUTE: 378 case CDIOCSETLEFT: 379 case CDIOCSETRIGHT: 380 error = EINVAL; 381 break; 382 case CDIOCRESUME: 383 error = scd_resume(sc); 384 break; 385 case CDIOCPAUSE: 386 error = scd_pause(sc); 387 break; 388 case CDIOCSTART: 389 error = EINVAL; 390 break; 391 case CDIOCSTOP: 392 error = scd_stop(sc); 393 break; 394 case CDIOCEJECT: 395 error = scd_eject(sc); 396 break; 397 case CDIOCALLOW: 398 break; 399 case CDIOCSETDEBUG: 400#ifdef SCD_DEBUG 401 scd_debuglevel++; 402#endif 403 break; 404 case CDIOCCLRDEBUG: 405#ifdef SCD_DEBUG 406 scd_debuglevel = 0; 407 408#endif 409 break; 410 default: 411 device_printf(sc->dev, "unsupported ioctl (cmd=0x%lx)\n", cmd); 412 error = ENOTTY; 413 break; 414 } 415 SCD_UNLOCK(sc); 416 return (error); 417} 418 419/*************************************************************** 420 * lower level of driver starts here 421 **************************************************************/ 422 423static int 424scd_playtracks(struct scd_softc *sc, struct ioc_play_track *pt) 425{ 426 struct ioc_play_msf msf; 427 int a = pt->start_track; 428 int z = pt->end_track; 429 int rc; 430 431 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) { 432 if (rc == -ERR_NOT_SPINNING) { 433 if (spin_up(sc) != 0) 434 return (EIO); 435 rc = read_toc(sc); 436 } 437 if (rc != 0) { 438 print_error(sc, rc); 439 return (EIO); 440 } 441 } 442 443 XDEBUG(sc, 1, "playtracks from %d:%d to %d:%d\n", 444 a, pt->start_index, z, pt->end_index); 445 446 if ( a < sc->data.first_track 447 || a > sc->data.last_track 448 || a > z 449 || z > sc->data.last_track) 450 return (EINVAL); 451 452 bcopy(sc->data.toc[a].start_msf, &msf.start_m, 3); 453 hsg2msf(msf2hsg(sc->data.toc[z+1].start_msf)-1, &msf.end_m); 454 455 return scd_play(sc, &msf); 456} 457 458/* The start/end msf is expected to be in bin format */ 459static int 460scd_playmsf(struct scd_softc *sc, struct ioc_play_msf *msfin) 461{ 462 struct ioc_play_msf msf; 463 464 msf.start_m = bin2bcd(msfin->start_m); 465 msf.start_s = bin2bcd(msfin->start_s); 466 msf.start_f = bin2bcd(msfin->start_f); 467 msf.end_m = bin2bcd(msfin->end_m); 468 msf.end_s = bin2bcd(msfin->end_s); 469 msf.end_f = bin2bcd(msfin->end_f); 470 471 return scd_play(sc, &msf); 472} 473 474/* The start/end msf is expected to be in bcd format */ 475static int 476scd_play(struct scd_softc *sc, struct ioc_play_msf *msf) 477{ 478 int i, rc; 479 480 XDEBUG(sc, 1, "playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", 481 msf->start_m, msf->start_s, msf->start_f, 482 msf->end_m, msf->end_s, msf->end_f); 483 484 for (i = 0; i < 2; i++) { 485 rc = send_cmd(sc, CMD_PLAY_AUDIO, 7, 486 0x03, 487 msf->start_m, msf->start_s, msf->start_f, 488 msf->end_m, msf->end_s, msf->end_f); 489 if (rc == -ERR_NOT_SPINNING) { 490 sc->data.flags &= ~SCDSPINNING; 491 if (spin_up(sc) != 0) 492 return (EIO); 493 } else if (rc < 0) { 494 print_error(sc, rc); 495 return (EIO); 496 } else { 497 break; 498 } 499 } 500 sc->data.audio_status = CD_AS_PLAY_IN_PROGRESS; 501 bcopy((char *)msf, (char *)&sc->data.last_play, sizeof(struct ioc_play_msf)); 502 return (0); 503} 504 505static int 506scd_stop(struct scd_softc *sc) 507{ 508 509 (void)send_cmd(sc, CMD_STOP_AUDIO, 0); 510 sc->data.audio_status = CD_AS_PLAY_COMPLETED; 511 return (0); 512} 513 514static int 515scd_pause(struct scd_softc *sc) 516{ 517 struct sony_subchannel_position_data subpos; 518 519 if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS) 520 return (EINVAL); 521 522 if (read_subcode(sc, &subpos) != 0) 523 return (EIO); 524 525 if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0) 526 return (EIO); 527 528 sc->data.last_play.start_m = subpos.abs_msf[0]; 529 sc->data.last_play.start_s = subpos.abs_msf[1]; 530 sc->data.last_play.start_f = subpos.abs_msf[2]; 531 sc->data.audio_status = CD_AS_PLAY_PAUSED; 532 533 XDEBUG(sc, 1, "pause @ %02x:%02x:%02x\n", 534 sc->data.last_play.start_m, 535 sc->data.last_play.start_s, 536 sc->data.last_play.start_f); 537 538 return (0); 539} 540 541static int 542scd_resume(struct scd_softc *sc) 543{ 544 545 if (sc->data.audio_status != CD_AS_PLAY_PAUSED) 546 return (EINVAL); 547 return scd_play(sc, &sc->data.last_play); 548} 549 550static int 551scd_eject(struct scd_softc *sc) 552{ 553 554 sc->data.audio_status = CD_AS_AUDIO_INVALID; 555 sc->data.flags &= ~(SCDSPINNING|SCDTOC); 556 557 if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0 || 558 send_cmd(sc, CMD_SPIN_DOWN, 0) != 0 || 559 send_cmd(sc, CMD_EJECT, 0) != 0) 560 { 561 return (EIO); 562 } 563 return (0); 564} 565 566static int 567scd_subchan(struct scd_softc *sc, struct ioc_read_subchannel *sch, int nocopyout) 568{ 569 struct sony_subchannel_position_data q; 570 struct cd_sub_channel_info data; 571 572 XDEBUG(sc, 1, "subchan af=%d, df=%d\n", 573 sch->address_format, sch->data_format); 574 575 if (sch->address_format != CD_MSF_FORMAT) 576 return (EINVAL); 577 578 if (sch->data_format != CD_CURRENT_POSITION) 579 return (EINVAL); 580 581 if (read_subcode(sc, &q) != 0) 582 return (EIO); 583 584 data.header.audio_status = sc->data.audio_status; 585 data.what.position.data_format = CD_MSF_FORMAT; 586 data.what.position.track_number = bcd2bin(q.track_number); 587 data.what.position.reladdr.msf.unused = 0; 588 data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]); 589 data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]); 590 data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]); 591 data.what.position.absaddr.msf.unused = 0; 592 data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]); 593 data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]); 594 data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]); 595 SCD_UNLOCK(sc); 596 597 if (nocopyout == 0) { 598 if (copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len))!=0) 599 return (EFAULT); 600 } else { 601 bcopy(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len)); 602 } 603 return (0); 604} 605 606int 607scd_probe(struct scd_softc *sc) 608{ 609 struct sony_drive_configuration drive_config; 610 int rc; 611 static char namebuf[8+16+8+3]; 612 char *s = namebuf; 613 int loop_count = 0; 614 615 sc->data.flags = SCDPROBING; 616 617 bzero(&drive_config, sizeof(drive_config)); 618 619again: 620 /* Reset drive */ 621 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESET_DRIVE); 622 623 /* Calm down */ 624 DELAY(300000); 625 626 /* Only the ATTENTION bit may be set */ 627 if ((SCD_READ(sc, IREG_STATUS) & ~1) != 0) { 628 XDEBUG(sc, 1, "too many bits set. probe failed.\n"); 629 return (ENXIO); 630 } 631 rc = send_cmd(sc, CMD_GET_DRIVE_CONFIG, 0); 632 if (rc != sizeof(drive_config)) { 633 /* Sometimes if the drive is playing audio I get */ 634 /* the bad result 82. Fix by repeating the reset */ 635 if (rc > 0 && loop_count++ == 0) 636 goto again; 637 return (ENXIO); 638 } 639 if (get_result(sc, rc, (u_char *)&drive_config) != 0) 640 return (ENXIO); 641 642 bcopy(drive_config.vendor, namebuf, 8); 643 s = namebuf+8; 644 while (*(s-1) == ' ') /* Strip trailing spaces */ 645 s--; 646 *s++ = ' '; 647 bcopy(drive_config.product, s, 16); 648 s += 16; 649 while (*(s-1) == ' ') 650 s--; 651 *s++ = ' '; 652 bcopy(drive_config.revision, s, 8); 653 s += 8; 654 while (*(s-1) == ' ') 655 s--; 656 *s = 0; 657 658 sc->data.name = namebuf; 659 660 if (drive_config.config & 0x10) 661 sc->data.double_speed = 1; 662 else 663 sc->data.double_speed = 0; 664 665 return (0); 666} 667 668static int 669read_subcode(struct scd_softc *sc, struct sony_subchannel_position_data *scp) 670{ 671 int rc; 672 673 rc = send_cmd(sc, CMD_GET_SUBCHANNEL_DATA, 0); 674 if (rc < 0 || rc < sizeof(*scp)) 675 return (EIO); 676 if (get_result(sc, rc, (u_char *)scp) != 0) 677 return (EIO); 678 return (0); 679} 680 681/* State machine copied from mcd.c */ 682 683/* This (and the code in mcd.c) will not work with more than one drive */ 684/* because there is only one sc->ch_mbxsave below. Should fix that some day. */ 685/* (sc->ch_mbxsave & state should probably be included in the scd_data struct and */ 686/* the unit number used as first argument to scd_doread().) /Micke */ 687 688/* state machine to process read requests 689 * initialize with SCD_S_BEGIN: reset state machine 690 * SCD_S_WAITSTAT: wait for ready (!busy) 691 * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning) 692 * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command. 693 * SCD_S_WAITREAD: wait for data ready, read data 694 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read. 695 */ 696 697static void 698scd_timeout(void *arg) 699{ 700 struct scd_softc *sc; 701 sc = (struct scd_softc *)arg; 702 703 SCD_ASSERT_LOCKED(sc); 704 scd_doread(sc, sc->ch_state, sc->ch_mbxsave); 705} 706 707static void 708scd_doread(struct scd_softc *sc, int state, struct scd_mbx *mbxin) 709{ 710 struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? sc->ch_mbxsave : mbxin; 711 struct bio *bp = mbx->bp; 712 int i; 713 int blknum; 714 caddr_t addr; 715 static char sdata[3]; /* Must be preserved between calls to this function */ 716 717 SCD_ASSERT_LOCKED(sc); 718loop: 719 switch (state) { 720 case SCD_S_BEGIN: 721 mbx = sc->ch_mbxsave = mbxin; 722 723 case SCD_S_BEGIN1: 724 /* get status */ 725 mbx->count = RDELAY_WAIT; 726 727 process_attention(sc); 728 goto trystat; 729 730 case SCD_S_WAITSTAT: 731 sc->ch_state = SCD_S_WAITSTAT; 732 callout_stop(&sc->timer); 733 if (mbx->count-- <= 0) { 734 device_printf(sc->dev, "timeout. drive busy.\n"); 735 goto harderr; 736 } 737 738trystat: 739 if (IS_BUSY(sc)) { 740 sc->ch_state = SCD_S_WAITSTAT; 741 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 742 return; 743 } 744 745 process_attention(sc); 746 747 /* reject, if audio active */ 748 if (sc->data.audio_status & CD_AS_PLAY_IN_PROGRESS) { 749 device_printf(sc->dev, "audio is active\n"); 750 goto harderr; 751 } 752 753 mbx->sz = sc->data.blksize; 754 755 /* for first block */ 756 mbx->nblk = howmany(bp->bio_bcount, mbx->sz); 757 mbx->skip = 0; 758 759nextblock: 760 if (!(sc->data.flags & SCDVALID)) 761 goto changed; 762 763 blknum = bp->bio_offset / mbx->sz + mbx->skip/mbx->sz; 764 765 XDEBUG(sc, 2, "scd_doread: read blknum=%d\n", blknum); 766 767 /* build parameter block */ 768 hsg2msf(blknum, sdata); 769 770 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); 771 SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR); 772 SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR); 773 774 if (FSTATUS_BIT(sc, FBIT_WPARAM_READY)) 775 goto writeparam; 776 777 mbx->count = 100; 778 sc->ch_state = SCD_S_WAITFIFO; 779 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 780 return; 781 782 case SCD_S_WAITSPIN: 783 sc->ch_state = SCD_S_WAITSPIN; 784 callout_stop(&sc->timer); 785 if (mbx->count-- <= 0) { 786 device_printf(sc->dev, "timeout waiting for drive to spin up.\n"); 787 goto harderr; 788 } 789 if (!STATUS_BIT(sc, SBIT_RESULT_READY)) { 790 sc->ch_state = SCD_S_WAITSPIN; 791 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 792 return; 793 } 794 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); 795 switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) { 796 case 0x20: 797 i = SCD_READ(sc, IREG_RESULT); 798 print_error(sc, i); 799 goto harderr; 800 case 0x00: 801 (void)SCD_READ(sc, IREG_RESULT); 802 sc->data.flags |= SCDSPINNING; 803 break; 804 } 805 XDEBUG(sc, 1, "DEBUG: spin up complete\n"); 806 807 state = SCD_S_BEGIN1; 808 goto loop; 809 810 case SCD_S_WAITFIFO: 811 sc->ch_state = SCD_S_WAITFIFO; 812 callout_stop(&sc->timer); 813 if (mbx->count-- <= 0) { 814 device_printf(sc->dev, "timeout. write param not ready.\n"); 815 goto harderr; 816 } 817 if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) { 818 sc->ch_state = SCD_S_WAITFIFO; 819 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 820 return; 821 } 822 XDEBUG(sc, 1, "mbx->count (writeparamwait) = %d(%d)\n", mbx->count, 100); 823 824writeparam: 825 /* The reason this test isn't done 'till now is to make sure */ 826 /* that it is ok to send the SPIN_UP cmd below. */ 827 if (!(sc->data.flags & SCDSPINNING)) { 828 XDEBUG(sc, 1, "spinning up drive ...\n"); 829 SCD_WRITE(sc, OREG_COMMAND, CMD_SPIN_UP); 830 mbx->count = 300; 831 sc->ch_state = SCD_S_WAITSPIN; 832 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 833 return; 834 } 835 836 /* send the read command */ 837 SCD_WRITE(sc, OREG_WPARAMS, sdata[0]); 838 SCD_WRITE(sc, OREG_WPARAMS, sdata[1]); 839 SCD_WRITE(sc, OREG_WPARAMS, sdata[2]); 840 SCD_WRITE(sc, OREG_WPARAMS, 0); 841 SCD_WRITE(sc, OREG_WPARAMS, 0); 842 SCD_WRITE(sc, OREG_WPARAMS, 1); 843 SCD_WRITE(sc, OREG_COMMAND, CMD_READ); 844 845 mbx->count = RDELAY_WAITREAD; 846 for (i = 0; i < 50; i++) { 847 if (STATUS_BIT(sc, SBIT_DATA_READY)) 848 goto got_data; 849 DELAY(100); 850 } 851 852 sc->ch_state = SCD_S_WAITREAD; 853 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 854 return; 855 856 case SCD_S_WAITREAD: 857 sc->ch_state = SCD_S_WAITREAD; 858 callout_stop(&sc->timer); 859 if (mbx->count-- <= 0) { 860 if (STATUS_BIT(sc, SBIT_RESULT_READY)) 861 goto got_param; 862 device_printf(sc->dev, "timeout while reading data\n"); 863 goto readerr; 864 } 865 if (!STATUS_BIT(sc, SBIT_DATA_READY)) { 866 process_attention(sc); 867 if (!(sc->data.flags & SCDVALID)) 868 goto changed; 869 sc->ch_state = SCD_S_WAITREAD; 870 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 871 return; 872 } 873 XDEBUG(sc, 2, "mbx->count (after RDY_BIT) = %d(%d)\n", mbx->count, RDELAY_WAITREAD); 874 875got_data: 876 /* data is ready */ 877 addr = bp->bio_data + mbx->skip; 878 SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR); 879 SCD_READ_MULTI(sc, IREG_DATA, addr, mbx->sz); 880 881 mbx->count = 100; 882 for (i = 0; i < 20; i++) { 883 if (STATUS_BIT(sc, SBIT_RESULT_READY)) 884 goto waitfor_param; 885 DELAY(100); 886 } 887 goto waitfor_param; 888 889 case SCD_S_WAITPARAM: 890 sc->ch_state = SCD_S_WAITPARAM; 891 callout_stop(&sc->timer); 892 if (mbx->count-- <= 0) { 893 device_printf(sc->dev, "timeout waiting for params\n"); 894 goto readerr; 895 } 896 897waitfor_param: 898 if (!STATUS_BIT(sc, SBIT_RESULT_READY)) { 899 sc->ch_state = SCD_S_WAITPARAM; 900 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */ 901 return; 902 } 903#ifdef SCD_DEBUG 904 if (mbx->count < 100 && scd_debuglevel > 0) 905 device_printf(sc->dev, "mbx->count (paramwait) = %d(%d)\n", mbx->count, 100); 906#endif 907 908got_param: 909 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); 910 switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) { 911 case 0x50: 912 switch (i) { 913 case ERR_FATAL_READ_ERROR1: 914 case ERR_FATAL_READ_ERROR2: 915 device_printf(sc->dev, "unrecoverable read error 0x%x\n", i); 916 goto harderr; 917 } 918 break; 919 case 0x20: 920 i = SCD_READ(sc, IREG_RESULT); 921 switch (i) { 922 case ERR_NOT_SPINNING: 923 XDEBUG(sc, 1, "read error: drive not spinning\n"); 924 if (mbx->retry-- > 0) { 925 state = SCD_S_BEGIN1; 926 sc->data.flags &= ~SCDSPINNING; 927 goto loop; 928 } 929 goto harderr; 930 default: 931 print_error(sc, i); 932 goto readerr; 933 } 934 case 0x00: 935 i = SCD_READ(sc, IREG_RESULT); 936 break; 937 } 938 939 if (--mbx->nblk > 0) { 940 mbx->skip += mbx->sz; 941 goto nextblock; 942 } 943 944 /* return buffer */ 945 bp->bio_resid = 0; 946 biodone(bp); 947 948 sc->data.flags &= ~SCDMBXBSY; 949 scd_start(sc); 950 return; 951 } 952 953readerr: 954 if (mbx->retry-- > 0) { 955 device_printf(sc->dev, "retrying ...\n"); 956 state = SCD_S_BEGIN1; 957 goto loop; 958 } 959harderr: 960 /* invalidate the buffer */ 961 bp->bio_error = EIO; 962 bp->bio_flags |= BIO_ERROR; 963 bp->bio_resid = bp->bio_bcount; 964 biodone(bp); 965 966 sc->data.flags &= ~SCDMBXBSY; 967 scd_start(sc); 968 return; 969 970changed: 971 device_printf(sc->dev, "media changed\n"); 972 goto harderr; 973} 974 975static void 976hsg2msf(int hsg, bcd_t *msf) 977{ 978 979 hsg += 150; 980 M_msf(msf) = bin2bcd(hsg / 4500); 981 hsg %= 4500; 982 S_msf(msf) = bin2bcd(hsg / 75); 983 F_msf(msf) = bin2bcd(hsg % 75); 984} 985 986static int 987msf2hsg(bcd_t *msf) 988{ 989 990 return (bcd2bin(M_msf(msf)) * 60 + 991 bcd2bin(S_msf(msf))) * 75 + 992 bcd2bin(F_msf(msf)) - 150; 993} 994 995static void 996process_attention(struct scd_softc *sc) 997{ 998 unsigned char code; 999 int count = 0; 1000 1001 while (IS_ATTENTION(sc) && count++ < 30) { 1002 SCD_WRITE(sc, OREG_CONTROL, CBIT_ATTENTION_CLEAR); 1003 code = SCD_READ(sc, IREG_RESULT); 1004 1005#ifdef SCD_DEBUG 1006 if (scd_debuglevel > 0) { 1007 if (count == 1) 1008 device_printf(sc->dev, "DEBUG: ATTENTIONS = 0x%x", code); 1009 else 1010 printf(",0x%x", code); 1011 } 1012#endif 1013 1014 switch (code) { 1015 case ATTEN_SPIN_DOWN: 1016 sc->data.flags &= ~SCDSPINNING; 1017 break; 1018 1019 case ATTEN_SPIN_UP_DONE: 1020 sc->data.flags |= SCDSPINNING; 1021 break; 1022 1023 case ATTEN_AUDIO_DONE: 1024 sc->data.audio_status = CD_AS_PLAY_COMPLETED; 1025 break; 1026 1027 case ATTEN_DRIVE_LOADED: 1028 sc->data.flags &= ~(SCDTOC|SCDSPINNING|SCDVALID); 1029 sc->data.audio_status = CD_AS_AUDIO_INVALID; 1030 break; 1031 1032 case ATTEN_EJECT_PUSHED: 1033 sc->data.flags &= ~SCDVALID; 1034 break; 1035 } 1036 DELAY(100); 1037 } 1038#ifdef SCD_DEBUG 1039 if (scd_debuglevel > 0 && count > 0) 1040 printf("\n"); 1041#endif 1042} 1043 1044/* Returns 0 OR sony error code */ 1045static int 1046spin_up(struct scd_softc *sc) 1047{ 1048 unsigned char res_reg[12]; 1049 unsigned int res_size; 1050 int rc; 1051 int loop_count = 0; 1052 1053again: 1054 rc = send_cmd(sc, CMD_SPIN_UP, 0, 0, res_reg, &res_size); 1055 if (rc != 0) { 1056 XDEBUG(sc, 2, "CMD_SPIN_UP error 0x%x\n", rc); 1057 return (rc); 1058 } 1059 1060 if (!(sc->data.flags & SCDTOC)) { 1061 rc = send_cmd(sc, CMD_READ_TOC, 0); 1062 if (rc == ERR_NOT_SPINNING) { 1063 if (loop_count++ < 3) 1064 goto again; 1065 return (rc); 1066 } 1067 if (rc != 0) 1068 return (rc); 1069 } 1070 1071 sc->data.flags |= SCDSPINNING; 1072 1073 return (0); 1074} 1075 1076static struct sony_tracklist * 1077get_tl(struct sony_toc *toc, int size) 1078{ 1079 struct sony_tracklist *tl = &toc->tracks[0]; 1080 1081 if (tl->track != 0xb0) 1082 return (tl); 1083 if (tl->track != 0xb1) 1084 return (tl); 1085 tl = (struct sony_tracklist *)((char *)tl + 9); 1086 if (tl->track != 0xb2) 1087 return (tl); 1088 tl = (struct sony_tracklist *)((char *)tl + 9); 1089 if (tl->track != 0xb3) 1090 return (tl); 1091 tl = (struct sony_tracklist *)((char *)tl + 9); 1092 if (tl->track != 0xb4) 1093 return (tl); 1094 tl = (struct sony_tracklist *)((char *)tl + 9); 1095 if (tl->track != 0xc0) 1096 return (tl); 1097 tl = (struct sony_tracklist *)((char *)tl + 9); 1098 return (tl); 1099} 1100 1101static int 1102read_toc(struct scd_softc *sc) 1103{ 1104 struct sony_toc toc; 1105 struct sony_tracklist *tl; 1106 int rc, i, j; 1107 u_long first, last; 1108 1109 rc = send_cmd(sc, CMD_GET_TOC, 1, 1); 1110 if (rc < 0) 1111 return (rc); 1112 if (rc > sizeof(toc)) { 1113 device_printf(sc->dev, "program error: toc too large (%d)\n", rc); 1114 return (EIO); 1115 } 1116 if (get_result(sc, rc, (u_char *)&toc) != 0) 1117 return (EIO); 1118 1119 XDEBUG(sc, 1, "toc read. len = %d, sizeof(toc) = %d\n", rc, sizeof(toc)); 1120 1121 tl = get_tl(&toc, rc); 1122 first = msf2hsg(tl->start_msf); 1123 last = msf2hsg(toc.lead_out_start_msf); 1124 sc->data.blksize = SCDBLKSIZE; 1125 sc->data.disksize = last*sc->data.blksize/DEV_BSIZE; 1126 1127 XDEBUG(sc, 1, "firstsector = %ld, lastsector = %ld", first, last); 1128 1129 sc->data.first_track = bcd2bin(toc.first_track); 1130 sc->data.last_track = bcd2bin(toc.last_track); 1131 if (sc->data.last_track > (MAX_TRACKS-2)) 1132 sc->data.last_track = MAX_TRACKS-2; 1133 for (j = 0, i = sc->data.first_track; i <= sc->data.last_track; i++, j++) { 1134 sc->data.toc[i].adr = tl[j].adr; 1135 sc->data.toc[i].ctl = tl[j].ctl; /* for xcdplayer */ 1136 bcopy(tl[j].start_msf, sc->data.toc[i].start_msf, 3); 1137#ifdef SCD_DEBUG 1138 if (scd_debuglevel > 0) { 1139 if ((j % 3) == 0) { 1140 printf("\n"); 1141 device_printf(sc->dev, "tracks "); 1142 } 1143 printf("[%03d: %2d %2d %2d] ", i, 1144 bcd2bin(sc->data.toc[i].start_msf[0]), 1145 bcd2bin(sc->data.toc[i].start_msf[1]), 1146 bcd2bin(sc->data.toc[i].start_msf[2])); 1147 } 1148#endif 1149 } 1150 bcopy(toc.lead_out_start_msf, sc->data.toc[sc->data.last_track+1].start_msf, 3); 1151#ifdef SCD_DEBUG 1152 if (scd_debuglevel > 0) { 1153 i = sc->data.last_track+1; 1154 printf("[END: %2d %2d %2d]\n", 1155 bcd2bin(sc->data.toc[i].start_msf[0]), 1156 bcd2bin(sc->data.toc[i].start_msf[1]), 1157 bcd2bin(sc->data.toc[i].start_msf[2])); 1158 } 1159#endif 1160 1161 sc->data.flags |= SCDTOC; 1162 1163 return (0); 1164} 1165 1166static void 1167init_drive(struct scd_softc *sc) 1168{ 1169 int rc; 1170 1171 rc = send_cmd(sc, CMD_SET_DRIVE_PARAM, 2, 1172 0x05, 0x03 | ((sc->data.double_speed) ? 0x04: 0)); 1173 if (rc != 0) 1174 device_printf(sc->dev, "Unable to set parameters. Errcode = 0x%x\n", rc); 1175} 1176 1177/* Returns 0 or errno */ 1178static int 1179get_result(struct scd_softc *sc, int result_len, u_char *result) 1180{ 1181 int loop_index = 2; /* send_cmd() reads two bytes ... */ 1182 1183 XDEBUG(sc, 1, "DEBUG: get_result: bytes=%d\n", result_len); 1184 1185 while (result_len-- > 0) { 1186 if (loop_index++ >= 10) { 1187 loop_index = 1; 1188 if (waitfor_status_bits(sc, SBIT_RESULT_READY, 0)) 1189 return (EIO); 1190 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); 1191 } 1192 if (result) 1193 *result++ = SCD_READ(sc, IREG_RESULT); 1194 else 1195 (void)SCD_READ(sc, IREG_RESULT); 1196 } 1197 return (0); 1198} 1199 1200/* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */ 1201static int 1202send_cmd(struct scd_softc *sc, u_char cmd, u_int nargs, ...) 1203{ 1204 va_list ap; 1205 u_char c; 1206 int rc; 1207 int i; 1208 1209 if (waitfor_status_bits(sc, 0, SBIT_BUSY)) { 1210 device_printf(sc->dev, "drive busy\n"); 1211 return (-0x100); 1212 } 1213 1214 XDEBUG(sc, 1, "DEBUG: send_cmd: cmd=0x%x nargs=%d", cmd, nargs); 1215 1216 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); 1217 SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR); 1218 1219 for (i = 0; i < 100; i++) 1220 if (FSTATUS_BIT(sc, FBIT_WPARAM_READY)) 1221 break; 1222 if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) { 1223 XDEBUG(sc, 1, "\nwparam timeout\n"); 1224 return (-EIO); 1225 } 1226 1227 va_start(ap, nargs); 1228 for (i = 0; i < nargs; i++) { 1229 c = (u_char)va_arg(ap, int); 1230 SCD_WRITE(sc, OREG_WPARAMS, c); 1231 XDEBUG(sc, 1, ",{0x%x}", c); 1232 } 1233 va_end(ap); 1234 XDEBUG(sc, 1, "\n"); 1235 1236 SCD_WRITE(sc, OREG_COMMAND, cmd); 1237 1238 rc = waitfor_status_bits(sc, SBIT_RESULT_READY, SBIT_BUSY); 1239 if (rc) 1240 return (-0x100); 1241 1242 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); 1243 switch ((rc = SCD_READ(sc, IREG_RESULT)) & 0xf0) { 1244 case 0x20: 1245 rc = SCD_READ(sc, IREG_RESULT); 1246 /* FALLTHROUGH */ 1247 case 0x50: 1248 XDEBUG(sc, 1, "DEBUG: send_cmd: drive_error=0x%x\n", rc); 1249 return (-rc); 1250 case 0x00: 1251 default: 1252 rc = SCD_READ(sc, IREG_RESULT); 1253 XDEBUG(sc, 1, "DEBUG: send_cmd: result_len=%d\n", rc); 1254 return (rc); 1255 } 1256} 1257 1258static void 1259print_error(struct scd_softc *sc, int errcode) 1260{ 1261 1262 switch (errcode) { 1263 case -ERR_CD_NOT_LOADED: 1264 device_printf(sc->dev, "door is open\n"); 1265 break; 1266 case -ERR_NO_CD_INSIDE: 1267 device_printf(sc->dev, "no cd inside\n"); 1268 break; 1269 default: 1270 if (errcode == -0x100 || errcode > 0) 1271 device_printf(sc->dev, "device timeout\n"); 1272 else 1273 device_printf(sc->dev, "unexpected error 0x%x\n", -errcode); 1274 break; 1275 } 1276} 1277 1278/* Returns 0 or errno value */ 1279static int 1280waitfor_status_bits(struct scd_softc *sc, int bits_set, int bits_clear) 1281{ 1282 u_int flags = sc->data.flags; 1283 u_int max_loop; 1284 u_char c = 0; 1285 1286 if (flags & SCDPROBING) { 1287 max_loop = 0; 1288 while (max_loop++ < 1000) { 1289 c = SCD_READ(sc, IREG_STATUS); 1290 if (c == 0xff) 1291 return (EIO); 1292 if (c & SBIT_ATTENTION) { 1293 process_attention(sc); 1294 continue; 1295 } 1296 if ((c & bits_set) == bits_set && 1297 (c & bits_clear) == 0) 1298 { 1299 break; 1300 } 1301 DELAY(10000); 1302 } 1303 } else { 1304 max_loop = 100; 1305 while (max_loop-- > 0) { 1306 c = SCD_READ(sc, IREG_STATUS); 1307 if (c & SBIT_ATTENTION) { 1308 process_attention(sc); 1309 continue; 1310 } 1311 if ((c & bits_set) == bits_set && 1312 (c & bits_clear) == 0) 1313 { 1314 break; 1315 } 1316 SCD_UNLOCK(sc); 1317 pause("waitfor", hz/10); 1318 SCD_LOCK(sc); 1319 } 1320 } 1321 if ((c & bits_set) == bits_set && 1322 (c & bits_clear) == 0) 1323 { 1324 return (0); 1325 } 1326#ifdef SCD_DEBUG 1327 if (scd_debuglevel > 0) 1328 device_printf(sc->dev, "DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", c, bits_set, bits_clear); 1329 else 1330#endif 1331 device_printf(sc->dev, "timeout.\n"); 1332 return (EIO); 1333} 1334 1335/* these two routines for xcdplayer - "borrowed" from mcd.c */ 1336static int 1337scd_toc_header (struct scd_softc *sc, struct ioc_toc_header* th) 1338{ 1339 int rc; 1340 1341 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) { 1342 print_error(sc, rc); 1343 return (EIO); 1344 } 1345 1346 th->starting_track = sc->data.first_track; 1347 th->ending_track = sc->data.last_track; 1348 th->len = 0; /* not used */ 1349 1350 return (0); 1351} 1352 1353static int 1354scd_toc_entrys (struct scd_softc *sc, struct ioc_read_toc_entry *te) 1355{ 1356 struct cd_toc_entry toc_entry; 1357 int rc, i, len = te->data_len; 1358 1359 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) { 1360 print_error(sc, rc); 1361 return (EIO); 1362 } 1363 1364 /* find the toc to copy*/ 1365 i = te->starting_track; 1366 if (i == SCD_LASTPLUS1) 1367 i = sc->data.last_track + 1; 1368 1369 /* verify starting track */ 1370 if (i < sc->data.first_track || i > sc->data.last_track+1) 1371 return (EINVAL); 1372 1373 /* valid length ? */ 1374 if (len < sizeof(struct cd_toc_entry) 1375 || (len % sizeof(struct cd_toc_entry)) != 0) 1376 return (EINVAL); 1377 1378 /* copy the toc data */ 1379 toc_entry.control = sc->data.toc[i].ctl; 1380 toc_entry.addr_type = te->address_format; 1381 toc_entry.track = i; 1382 if (te->address_format == CD_MSF_FORMAT) { 1383 toc_entry.addr.msf.unused = 0; 1384 toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]); 1385 toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]); 1386 toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]); 1387 } 1388 SCD_UNLOCK(sc); 1389 1390 /* copy the data back */ 1391 if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0) 1392 return (EFAULT); 1393 1394 return (0); 1395} 1396 1397 1398static int 1399scd_toc_entry (struct scd_softc *sc, struct ioc_read_toc_single_entry *te) 1400{ 1401 struct cd_toc_entry toc_entry; 1402 int rc, i; 1403 1404 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) { 1405 print_error(sc, rc); 1406 return (EIO); 1407 } 1408 1409 /* find the toc to copy*/ 1410 i = te->track; 1411 if (i == SCD_LASTPLUS1) 1412 i = sc->data.last_track + 1; 1413 1414 /* verify starting track */ 1415 if (i < sc->data.first_track || i > sc->data.last_track+1) 1416 return (EINVAL); 1417 1418 /* copy the toc data */ 1419 toc_entry.control = sc->data.toc[i].ctl; 1420 toc_entry.addr_type = te->address_format; 1421 toc_entry.track = i; 1422 if (te->address_format == CD_MSF_FORMAT) { 1423 toc_entry.addr.msf.unused = 0; 1424 toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]); 1425 toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]); 1426 toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]); 1427 } 1428 1429 /* copy the data back */ 1430 bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry)); 1431 1432 return (0); 1433} 1434