1 /* $NetBSD: mcd.c,v 1.74 2002/01/07 21:47:11 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles M. Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * Copyright 1993 by Holger Veit (data part) 21 * Copyright 1993 by Brian Moore (audio part) 22 * All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This software was developed by Holger Veit and Brian Moore 35 * for use with "386BSD" and similar operating systems. 36 * "Similar operating systems" includes mainly non-profit oriented 37 * systems for research and education, including but not restricted to 38 * "NetBSD", "FreeBSD", "Mach" (by CMU). 39 * 4. Neither the name of the developer(s) nor the name "386BSD" 40 * may be used to endorse or promote products derived from this 41 * software without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY 44 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE 47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 48 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 49 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 51 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/ 57 58 #include <sys/cdefs.h> 59 __KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.74 2002/01/07 21:47:11 thorpej Exp $"); 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/callout.h> 64 #include <sys/kernel.h> 65 #include <sys/proc.h> 66 #include <sys/conf.h> 67 #include <sys/file.h> 68 #include <sys/buf.h> 69 #include <sys/stat.h> 70 #include <sys/uio.h> 71 #include <sys/ioctl.h> 72 #include <sys/cdio.h> 73 #include <sys/errno.h> 74 #include <sys/disklabel.h> 75 #include <sys/device.h> 76 #include <sys/disk.h> 77 78 #include <machine/cpu.h> 79 #include <machine/intr.h> 80 #include <machine/bus.h> 81 82 #include <dev/isa/isavar.h> 83 #include <dev/isa/mcdreg.h> 84 85 #ifndef MCDDEBUG 86 #define MCD_TRACE(fmt,a,b,c,d) 87 #else 88 #define MCD_TRACE(fmt,a,b,c,d) {if (sc->debug) {printf("%s: st=%02x: ", sc->sc_dev.dv_xname, sc->status); printf(fmt,a,b,c,d);}} 89 #endif 90 91 #define MCDPART(dev) DISKPART(dev) 92 #define MCDUNIT(dev) DISKUNIT(dev) 93 94 /* toc */ 95 #define MCD_MAXTOCS 104 /* from the Linux driver */ 96 97 /* control promiscuous match */ 98 #include "opt_mcd_promisc.h" 99 100 #ifdef MCD_PROMISC 101 int mcd_promisc = 1; 102 #else 103 int mcd_promisc = 0; 104 #endif 105 106 struct mcd_mbx { 107 int retry, count; 108 struct buf *bp; 109 daddr_t blkno; 110 int nblk; 111 int sz; 112 u_long skip; 113 int state; 114 #define MCD_S_IDLE 0 115 #define MCD_S_BEGIN 1 116 #define MCD_S_WAITMODE 2 117 #define MCD_S_WAITREAD 3 118 int mode; 119 }; 120 121 struct mcd_softc { 122 struct device sc_dev; 123 struct disk sc_dk; 124 void *sc_ih; 125 126 struct callout sc_pintr_ch; 127 128 bus_space_tag_t sc_iot; 129 bus_space_handle_t sc_ioh; 130 131 int irq, drq; 132 133 char *type; 134 int flags; 135 #define MCDF_LOCKED 0x01 136 #define MCDF_WANTED 0x02 137 #define MCDF_WLABEL 0x04 /* label is writable */ 138 #define MCDF_LABELLING 0x08 /* writing label */ 139 #define MCDF_LOADED 0x10 /* parameters loaded */ 140 short status; 141 short audio_status; 142 int blksize; 143 u_long disksize; 144 struct mcd_volinfo volinfo; 145 union mcd_qchninfo toc[MCD_MAXTOCS]; 146 struct mcd_command lastpb; 147 struct mcd_mbx mbx; 148 int lastmode; 149 #define MCD_MD_UNKNOWN -1 150 int lastupc; 151 #define MCD_UPC_UNKNOWN -1 152 struct buf_queue buf_queue; 153 int active; 154 u_char readcmd; 155 u_char debug; 156 u_char probe; 157 }; 158 159 /* prototypes */ 160 /* XXX does not belong here */ 161 cdev_decl(mcd); 162 bdev_decl(mcd); 163 164 static int bcd2bin __P((bcd_t)); 165 static bcd_t bin2bcd __P((int)); 166 static void hsg2msf __P((int, bcd_t *)); 167 static daddr_t msf2hsg __P((bcd_t *, int)); 168 169 int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *)); 170 int mcd_playmsf __P((struct mcd_softc *, struct ioc_play_msf *)); 171 int mcd_playblocks __P((struct mcd_softc *, struct ioc_play_blocks *)); 172 int mcd_stop __P((struct mcd_softc *)); 173 int mcd_eject __P((struct mcd_softc *)); 174 int mcd_read_subchannel __P((struct mcd_softc *, struct ioc_read_subchannel *)); 175 int mcd_pause __P((struct mcd_softc *)); 176 int mcd_resume __P((struct mcd_softc *)); 177 int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *)); 178 int mcd_toc_entries __P((struct mcd_softc *, struct ioc_read_toc_entry *)); 179 180 int mcd_getreply __P((struct mcd_softc *)); 181 int mcd_getstat __P((struct mcd_softc *)); 182 int mcd_getresult __P((struct mcd_softc *, struct mcd_result *)); 183 void mcd_setflags __P((struct mcd_softc *)); 184 int mcd_get __P((struct mcd_softc *, char *, int)); 185 int mcd_send __P((struct mcd_softc *, struct mcd_mbox *, int)); 186 int mcdintr __P((void *)); 187 void mcd_soft_reset __P((struct mcd_softc *)); 188 int mcd_hard_reset __P((struct mcd_softc *)); 189 int mcd_setmode __P((struct mcd_softc *, int)); 190 int mcd_setupc __P((struct mcd_softc *, int)); 191 int mcd_read_toc __P((struct mcd_softc *)); 192 int mcd_getqchan __P((struct mcd_softc *, union mcd_qchninfo *, int)); 193 int mcd_setlock __P((struct mcd_softc *, int)); 194 195 int mcd_find __P((bus_space_tag_t, bus_space_handle_t, struct mcd_softc *)); 196 int mcdprobe __P((struct device *, struct cfdata *, void *)); 197 void mcdattach __P((struct device *, struct device *, void *)); 198 199 struct cfattach mcd_ca = { 200 sizeof(struct mcd_softc), mcdprobe, mcdattach 201 }; 202 203 extern struct cfdriver mcd_cd; 204 205 void mcdgetdefaultlabel __P((struct mcd_softc *, struct disklabel *)); 206 void mcdgetdisklabel __P((struct mcd_softc *)); 207 int mcd_get_parms __P((struct mcd_softc *)); 208 void mcdstrategy __P((struct buf *)); 209 void mcdstart __P((struct mcd_softc *)); 210 int mcdlock __P((struct mcd_softc *)); 211 void mcdunlock __P((struct mcd_softc *)); 212 void mcd_pseudointr __P((void *)); 213 214 struct dkdriver mcddkdriver = { mcdstrategy }; 215 216 #define MCD_RETRIES 3 217 #define MCD_RDRETRIES 3 218 219 /* several delays */ 220 #define RDELAY_WAITMODE 300 221 #define RDELAY_WAITREAD 800 222 223 #define DELAY_GRANULARITY 25 /* 25us */ 224 #define DELAY_GETREPLY 100000 /* 100000 * 25us */ 225 226 void 227 mcdattach(parent, self, aux) 228 struct device *parent, *self; 229 void *aux; 230 { 231 struct mcd_softc *sc = (void *)self; 232 struct isa_attach_args *ia = aux; 233 bus_space_tag_t iot = ia->ia_iot; 234 bus_space_handle_t ioh; 235 struct mcd_mbox mbx; 236 237 /* Map i/o space */ 238 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) { 239 printf(": can't map i/o space\n"); 240 return; 241 } 242 243 sc->sc_iot = iot; 244 sc->sc_ioh = ioh; 245 246 sc->probe = 0; 247 sc->debug = 0; 248 249 if (!mcd_find(iot, ioh, sc)) { 250 printf(": mcd_find failed\n"); 251 return; 252 } 253 254 BUFQ_INIT(&sc->buf_queue); 255 callout_init(&sc->sc_pintr_ch); 256 257 /* 258 * Initialize and attach the disk structure. 259 */ 260 sc->sc_dk.dk_driver = &mcddkdriver; 261 sc->sc_dk.dk_name = sc->sc_dev.dv_xname; 262 disk_attach(&sc->sc_dk); 263 264 printf(": model %s\n", sc->type != 0 ? sc->type : "unknown"); 265 266 (void) mcd_setlock(sc, MCD_LK_UNLOCK); 267 268 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE; 269 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1; 270 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE; 271 mbx.cmd.data.config.data1 = 0x01; 272 mbx.res.length = 0; 273 (void) mcd_send(sc, &mbx, 0); 274 275 mcd_soft_reset(sc); 276 277 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq, 278 IST_EDGE, IPL_BIO, mcdintr, sc); 279 } 280 281 /* 282 * Wait interruptibly for an exclusive lock. 283 * 284 * XXX 285 * Several drivers do this; it should be abstracted and made MP-safe. 286 */ 287 int 288 mcdlock(sc) 289 struct mcd_softc *sc; 290 { 291 int error; 292 293 while ((sc->flags & MCDF_LOCKED) != 0) { 294 sc->flags |= MCDF_WANTED; 295 if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0) 296 return error; 297 } 298 sc->flags |= MCDF_LOCKED; 299 return 0; 300 } 301 302 /* 303 * Unlock and wake up any waiters. 304 */ 305 void 306 mcdunlock(sc) 307 struct mcd_softc *sc; 308 { 309 310 sc->flags &= ~MCDF_LOCKED; 311 if ((sc->flags & MCDF_WANTED) != 0) { 312 sc->flags &= ~MCDF_WANTED; 313 wakeup(sc); 314 } 315 } 316 317 int 318 mcdopen(dev, flag, fmt, p) 319 dev_t dev; 320 int flag, fmt; 321 struct proc *p; 322 { 323 int error, part; 324 struct mcd_softc *sc; 325 326 sc = device_lookup(&mcd_cd, MCDUNIT(dev)); 327 if (sc == NULL) 328 return ENXIO; 329 330 if ((error = mcdlock(sc)) != 0) 331 return error; 332 333 if (sc->sc_dk.dk_openmask != 0) { 334 /* 335 * If any partition is open, but the disk has been invalidated, 336 * disallow further opens. 337 */ 338 if ((sc->flags & MCDF_LOADED) == 0) { 339 error = EIO; 340 goto bad3; 341 } 342 } else { 343 /* 344 * Lock the drawer. This will also notice any pending disk 345 * change or door open indicator and clear the MCDF_LOADED bit 346 * if necessary. 347 */ 348 (void) mcd_setlock(sc, MCD_LK_LOCK); 349 350 if ((sc->flags & MCDF_LOADED) == 0) { 351 /* Partially reset the state. */ 352 sc->lastmode = MCD_MD_UNKNOWN; 353 sc->lastupc = MCD_UPC_UNKNOWN; 354 355 sc->flags |= MCDF_LOADED; 356 357 /* Set the mode, causing the disk to spin up. */ 358 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 359 goto bad2; 360 361 /* Load the physical device parameters. */ 362 if (mcd_get_parms(sc) != 0) { 363 error = ENXIO; 364 goto bad2; 365 } 366 367 /* Read the table of contents. */ 368 if ((error = mcd_read_toc(sc)) != 0) 369 goto bad2; 370 371 /* Fabricate a disk label. */ 372 mcdgetdisklabel(sc); 373 } 374 } 375 376 MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part, 377 sc->disksize, sc->blksize, 0); 378 379 part = MCDPART(dev); 380 381 /* Check that the partition exists. */ 382 if (part != RAW_PART && 383 (part >= sc->sc_dk.dk_label->d_npartitions || 384 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 385 error = ENXIO; 386 goto bad; 387 } 388 389 /* Insure only one open at a time. */ 390 switch (fmt) { 391 case S_IFCHR: 392 sc->sc_dk.dk_copenmask |= (1 << part); 393 break; 394 case S_IFBLK: 395 sc->sc_dk.dk_bopenmask |= (1 << part); 396 break; 397 } 398 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 399 400 mcdunlock(sc); 401 return 0; 402 403 bad2: 404 sc->flags &= ~MCDF_LOADED; 405 406 bad: 407 if (sc->sc_dk.dk_openmask == 0) { 408 #if 0 409 (void) mcd_setmode(sc, MCD_MD_SLEEP); 410 #endif 411 (void) mcd_setlock(sc, MCD_LK_UNLOCK); 412 } 413 414 bad3: 415 mcdunlock(sc); 416 return error; 417 } 418 419 int 420 mcdclose(dev, flag, fmt, p) 421 dev_t dev; 422 int flag, fmt; 423 struct proc *p; 424 { 425 struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(dev)); 426 int part = MCDPART(dev); 427 int error; 428 429 MCD_TRACE("close: partition=%d\n", part, 0, 0, 0); 430 431 if ((error = mcdlock(sc)) != 0) 432 return error; 433 434 switch (fmt) { 435 case S_IFCHR: 436 sc->sc_dk.dk_copenmask &= ~(1 << part); 437 break; 438 case S_IFBLK: 439 sc->sc_dk.dk_bopenmask &= ~(1 << part); 440 break; 441 } 442 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 443 444 if (sc->sc_dk.dk_openmask == 0) { 445 /* XXXX Must wait for I/O to complete! */ 446 447 #if 0 448 (void) mcd_setmode(sc, MCD_MD_SLEEP); 449 #endif 450 (void) mcd_setlock(sc, MCD_LK_UNLOCK); 451 } 452 453 mcdunlock(sc); 454 return 0; 455 } 456 457 void 458 mcdstrategy(bp) 459 struct buf *bp; 460 { 461 struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(bp->b_dev)); 462 struct disklabel *lp = sc->sc_dk.dk_label; 463 daddr_t blkno; 464 int s; 465 466 /* Test validity. */ 467 MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp, 468 bp->b_blkno, bp->b_bcount, 0); 469 if (bp->b_blkno < 0 || 470 (bp->b_bcount % sc->blksize) != 0) { 471 printf("%s: strategy: blkno = %d bcount = %ld\n", 472 sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount); 473 bp->b_error = EINVAL; 474 goto bad; 475 } 476 477 /* If device invalidated (e.g. media change, door open), error. */ 478 if ((sc->flags & MCDF_LOADED) == 0) { 479 MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0); 480 bp->b_error = EIO; 481 goto bad; 482 } 483 484 /* No data to read. */ 485 if (bp->b_bcount == 0) 486 goto done; 487 488 /* 489 * Do bounds checking, adjust transfer. if error, process. 490 * If end of partition, just return. 491 */ 492 if (MCDPART(bp->b_dev) != RAW_PART && 493 bounds_check_with_label(bp, lp, 494 (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0) 495 goto done; 496 497 /* 498 * Now convert the block number to absolute and put it in 499 * terms of the device's logical block size. 500 */ 501 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 502 if (MCDPART(bp->b_dev) != RAW_PART) 503 blkno += lp->d_partitions[MCDPART(bp->b_dev)].p_offset; 504 505 bp->b_rawblkno = blkno; 506 507 /* Queue it. */ 508 s = splbio(); 509 disksort_blkno(&sc->buf_queue, bp); 510 splx(s); 511 if (!sc->active) 512 mcdstart(sc); 513 return; 514 515 bad: 516 bp->b_flags |= B_ERROR; 517 done: 518 bp->b_resid = bp->b_bcount; 519 biodone(bp); 520 } 521 522 void 523 mcdstart(sc) 524 struct mcd_softc *sc; 525 { 526 struct buf *bp; 527 int s; 528 529 loop: 530 s = splbio(); 531 532 if ((bp = BUFQ_FIRST(&sc->buf_queue)) == NULL) { 533 /* Nothing to do. */ 534 sc->active = 0; 535 splx(s); 536 return; 537 } 538 539 /* Block found to process; dequeue. */ 540 MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0); 541 BUFQ_REMOVE(&sc->buf_queue, bp); 542 splx(s); 543 544 /* Changed media? */ 545 if ((sc->flags & MCDF_LOADED) == 0) { 546 MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0); 547 bp->b_error = EIO; 548 bp->b_flags |= B_ERROR; 549 biodone(bp); 550 goto loop; 551 } 552 553 sc->active = 1; 554 555 /* Instrumentation. */ 556 s = splbio(); 557 disk_busy(&sc->sc_dk); 558 splx(s); 559 560 sc->mbx.retry = MCD_RDRETRIES; 561 sc->mbx.bp = bp; 562 sc->mbx.blkno = bp->b_rawblkno; 563 sc->mbx.nblk = bp->b_bcount / sc->blksize; 564 sc->mbx.sz = sc->blksize; 565 sc->mbx.skip = 0; 566 sc->mbx.state = MCD_S_BEGIN; 567 sc->mbx.mode = MCD_MD_COOKED; 568 569 s = splbio(); 570 (void) mcdintr(sc); 571 splx(s); 572 } 573 574 int 575 mcdread(dev, uio, flags) 576 dev_t dev; 577 struct uio *uio; 578 int flags; 579 { 580 581 return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio)); 582 } 583 584 int 585 mcdwrite(dev, uio, flags) 586 dev_t dev; 587 struct uio *uio; 588 int flags; 589 { 590 591 return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio)); 592 } 593 594 int 595 mcdioctl(dev, cmd, addr, flag, p) 596 dev_t dev; 597 u_long cmd; 598 caddr_t addr; 599 int flag; 600 struct proc *p; 601 { 602 struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(dev)); 603 int error; 604 int part; 605 #ifdef __HAVE_OLD_DISKLABEL 606 struct disklabel newlabel; 607 #endif 608 609 MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0); 610 611 if ((sc->flags & MCDF_LOADED) == 0) 612 return EIO; 613 614 part = MCDPART(dev); 615 switch (cmd) { 616 case DIOCGDINFO: 617 *(struct disklabel *)addr = *(sc->sc_dk.dk_label); 618 return 0; 619 #ifdef __HAVE_OLD_DISKLABEL 620 case ODIOCGDINFO: 621 newlabel = *(sc->sc_dk.dk_label); 622 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 623 return ENOTTY; 624 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 625 return 0; 626 #endif 627 628 case DIOCGPART: 629 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label; 630 ((struct partinfo *)addr)->part = 631 &sc->sc_dk.dk_label->d_partitions[part]; 632 return 0; 633 634 case DIOCWDINFO: 635 case DIOCSDINFO: 636 #ifdef __HAVE_OLD_DISKLABEL 637 case ODIOCWDINFO: 638 case ODIOCSDINFO: 639 #endif 640 { 641 struct disklabel *lp; 642 643 if ((flag & FWRITE) == 0) 644 return EBADF; 645 646 #ifdef __HAVE_OLD_DISKLABEL 647 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 648 memset(&newlabel, 0, sizeof newlabel); 649 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 650 lp = &newlabel; 651 } else 652 #endif 653 lp = (struct disklabel *)addr; 654 655 if ((error = mcdlock(sc)) != 0) 656 return error; 657 sc->flags |= MCDF_LABELLING; 658 659 error = setdisklabel(sc->sc_dk.dk_label, 660 lp, /*sc->sc_dk.dk_openmask : */0, 661 sc->sc_dk.dk_cpulabel); 662 if (error == 0) { 663 } 664 665 sc->flags &= ~MCDF_LABELLING; 666 mcdunlock(sc); 667 return error; 668 } 669 670 case DIOCWLABEL: 671 return EBADF; 672 673 case DIOCGDEFLABEL: 674 mcdgetdefaultlabel(sc, (struct disklabel *)addr); 675 return 0; 676 677 #ifdef __HAVE_OLD_DISKLABEL 678 case ODIOCGDEFLABEL: 679 mcdgetdefaultlabel(sc, &newlabel); 680 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 681 return ENOTTY; 682 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 683 return 0; 684 #endif 685 686 case CDIOCPLAYTRACKS: 687 return mcd_playtracks(sc, (struct ioc_play_track *)addr); 688 case CDIOCPLAYMSF: 689 return mcd_playmsf(sc, (struct ioc_play_msf *)addr); 690 case CDIOCPLAYBLOCKS: 691 return mcd_playblocks(sc, (struct ioc_play_blocks *)addr); 692 case CDIOCREADSUBCHANNEL: 693 return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr); 694 case CDIOREADTOCHEADER: 695 return mcd_toc_header(sc, (struct ioc_toc_header *)addr); 696 case CDIOREADTOCENTRYS: 697 return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr); 698 case CDIOCSETPATCH: 699 case CDIOCGETVOL: 700 case CDIOCSETVOL: 701 case CDIOCSETMONO: 702 case CDIOCSETSTEREO: 703 case CDIOCSETMUTE: 704 case CDIOCSETLEFT: 705 case CDIOCSETRIGHT: 706 return EINVAL; 707 case CDIOCRESUME: 708 return mcd_resume(sc); 709 case CDIOCPAUSE: 710 return mcd_pause(sc); 711 case CDIOCSTART: 712 return EINVAL; 713 case CDIOCSTOP: 714 return mcd_stop(sc); 715 case DIOCEJECT: 716 if (*(int *)addr == 0) { 717 /* 718 * Don't force eject: check that we are the only 719 * partition open. If so, unlock it. 720 */ 721 if ((sc->sc_dk.dk_openmask & ~(1 << part)) == 0 && 722 sc->sc_dk.dk_bopenmask + sc->sc_dk.dk_copenmask == 723 sc->sc_dk.dk_openmask) { 724 error = mcd_setlock(sc, MCD_LK_UNLOCK); 725 if (error) 726 return (error); 727 } else { 728 return (EBUSY); 729 } 730 } 731 /* FALLTHROUGH */ 732 case CDIOCEJECT: /* FALLTHROUGH */ 733 case ODIOCEJECT: 734 return mcd_eject(sc); 735 case CDIOCALLOW: 736 return mcd_setlock(sc, MCD_LK_UNLOCK); 737 case CDIOCPREVENT: 738 return mcd_setlock(sc, MCD_LK_LOCK); 739 case DIOCLOCK: 740 return mcd_setlock(sc, 741 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK); 742 case CDIOCSETDEBUG: 743 sc->debug = 1; 744 return 0; 745 case CDIOCCLRDEBUG: 746 sc->debug = 0; 747 return 0; 748 case CDIOCRESET: 749 return mcd_hard_reset(sc); 750 751 default: 752 return ENOTTY; 753 } 754 755 #ifdef DIAGNOSTIC 756 panic("mcdioctl: impossible"); 757 #endif 758 } 759 760 void 761 mcdgetdefaultlabel(sc, lp) 762 struct mcd_softc *sc; 763 struct disklabel *lp; 764 { 765 766 memset(lp, 0, sizeof(struct disklabel)); 767 768 lp->d_secsize = sc->blksize; 769 lp->d_ntracks = 1; 770 lp->d_nsectors = 100; 771 lp->d_ncylinders = (sc->disksize / 100) + 1; 772 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 773 774 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16); 775 lp->d_type = 0; /* XXX */ 776 strncpy(lp->d_packname, "fictitious", 16); 777 lp->d_secperunit = sc->disksize; 778 lp->d_rpm = 300; 779 lp->d_interleave = 1; 780 lp->d_flags = D_REMOVABLE; 781 782 lp->d_partitions[0].p_offset = 0; 783 lp->d_partitions[0].p_size = 784 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 785 lp->d_partitions[0].p_fstype = FS_ISO9660; 786 lp->d_partitions[RAW_PART].p_offset = 0; 787 lp->d_partitions[RAW_PART].p_size = 788 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 789 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 790 lp->d_npartitions = RAW_PART + 1; 791 792 lp->d_magic = DISKMAGIC; 793 lp->d_magic2 = DISKMAGIC; 794 lp->d_checksum = dkcksum(lp); 795 } 796 797 /* 798 * This could have been taken from scsi/cd.c, but it is not clear 799 * whether the scsi cd driver is linked in. 800 */ 801 void 802 mcdgetdisklabel(sc) 803 struct mcd_softc *sc; 804 { 805 struct disklabel *lp = sc->sc_dk.dk_label; 806 807 memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 808 809 mcdgetdefaultlabel(sc, lp); 810 } 811 812 int 813 mcd_get_parms(sc) 814 struct mcd_softc *sc; 815 { 816 struct mcd_mbox mbx; 817 daddr_t size; 818 int error; 819 820 /* Send volume info command. */ 821 mbx.cmd.opcode = MCD_CMDGETVOLINFO; 822 mbx.cmd.length = 0; 823 mbx.res.length = sizeof(mbx.res.data.volinfo); 824 if ((error = mcd_send(sc, &mbx, 1)) != 0) 825 return error; 826 827 if (mbx.res.data.volinfo.trk_low == 0x00 && 828 mbx.res.data.volinfo.trk_high == 0x00) 829 return EINVAL; 830 831 /* Volinfo is OK. */ 832 sc->volinfo = mbx.res.data.volinfo; 833 sc->blksize = MCD_BLKSIZE_COOKED; 834 size = msf2hsg(sc->volinfo.vol_msf, 0); 835 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE); 836 return 0; 837 } 838 839 int 840 mcdsize(dev) 841 dev_t dev; 842 { 843 844 /* CD-ROMs are read-only. */ 845 return -1; 846 } 847 848 int 849 mcddump(dev, blkno, va, size) 850 dev_t dev; 851 daddr_t blkno; 852 caddr_t va; 853 size_t size; 854 { 855 856 /* Not implemented. */ 857 return ENXIO; 858 } 859 860 /* 861 * Find the board and fill in the softc. 862 */ 863 int 864 mcd_find(iot, ioh, sc) 865 bus_space_tag_t iot; 866 bus_space_handle_t ioh; 867 struct mcd_softc *sc; 868 { 869 int i; 870 struct mcd_mbox mbx; 871 872 sc->sc_iot = iot; 873 sc->sc_ioh = ioh; 874 875 /* Send a reset. */ 876 bus_space_write_1(iot, ioh, MCD_RESET, 0); 877 delay(1000000); 878 /* Get any pending status and throw away. */ 879 for (i = 10; i; i--) 880 bus_space_read_1(iot, ioh, MCD_STATUS); 881 delay(1000); 882 883 /* Send get status command. */ 884 mbx.cmd.opcode = MCD_CMDGETSTAT; 885 mbx.cmd.length = 0; 886 mbx.res.length = 0; 887 if (mcd_send(sc, &mbx, 0) != 0) 888 return 0; 889 890 /* Get info about the drive. */ 891 mbx.cmd.opcode = MCD_CMDCONTINFO; 892 mbx.cmd.length = 0; 893 mbx.res.length = sizeof(mbx.res.data.continfo); 894 if (mcd_send(sc, &mbx, 0) != 0) 895 return 0; 896 897 /* 898 * The following is code which is not guaranteed to work for all 899 * drives, because the meaning of the expected 'M' is not clear 900 * (M_itsumi is an obvious assumption, but I don't trust that). 901 * Also, the original hack had a bogus condition that always 902 * returned true. 903 * 904 * Note: Which models support interrupts? >=LU005S? 905 */ 906 sc->readcmd = MCD_CMDREADSINGLESPEED; 907 switch (mbx.res.data.continfo.code) { 908 case 'M': 909 if (mbx.res.data.continfo.version <= 2) 910 sc->type = "LU002S"; 911 else if (mbx.res.data.continfo.version <= 5) 912 sc->type = "LU005S"; 913 else 914 sc->type = "LU006S"; 915 break; 916 case 'F': 917 sc->type = "FX001"; 918 break; 919 case 'D': 920 sc->type = "FX001D"; 921 sc->readcmd = MCD_CMDREADDOUBLESPEED; 922 break; 923 default: 924 /* 925 * mcd_send() says the response looked OK but the 926 * drive type is unknown. If mcd_promisc, match anyway. 927 */ 928 if (mcd_promisc != 0) 929 return 0; 930 931 #ifdef MCDDEBUG 932 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n", 933 sc->sc_dev.dv_xname, 934 mbx.res.data.continfo.code, mbx.res.data.continfo.version); 935 #endif 936 sc->type = 0; 937 break; 938 } 939 940 return 1; 941 942 } 943 944 int 945 mcdprobe(parent, match, aux) 946 struct device *parent; 947 struct cfdata *match; 948 void *aux; 949 { 950 struct isa_attach_args *ia = aux; 951 struct mcd_softc sc; 952 bus_space_tag_t iot = ia->ia_iot; 953 bus_space_handle_t ioh; 954 int rv; 955 956 if (ia->ia_nio < 1) 957 return (0); 958 if (ia->ia_nirq < 1) 959 return (0); 960 961 if (ISA_DIRECT_CONFIG(ia)) 962 return (0); 963 964 /* Disallow wildcarded i/o address. */ 965 if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT) 966 return (0); 967 if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT) 968 return (0); 969 970 /* Map i/o space */ 971 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) 972 return 0; 973 974 sc.debug = 0; 975 sc.probe = 1; 976 977 rv = mcd_find(iot, ioh, &sc); 978 979 bus_space_unmap(iot, ioh, MCD_NPORT); 980 981 if (rv) { 982 ia->ia_nio = 1; 983 ia->ia_io[0].ir_addr = MCD_NPORT; 984 985 ia->ia_nirq = 1; 986 987 ia->ia_niomem = 0; 988 ia->ia_ndrq = 0; 989 } 990 991 return (rv); 992 } 993 994 int 995 mcd_getreply(sc) 996 struct mcd_softc *sc; 997 { 998 bus_space_tag_t iot = sc->sc_iot; 999 bus_space_handle_t ioh = sc->sc_ioh; 1000 int i; 1001 1002 /* Wait until xfer port senses data ready. */ 1003 for (i = DELAY_GETREPLY; i; i--) { 1004 if ((bus_space_read_1(iot, ioh, MCD_XFER) & 1005 MCD_XF_STATUSUNAVAIL) == 0) 1006 break; 1007 delay(DELAY_GRANULARITY); 1008 } 1009 if (!i) 1010 return -1; 1011 1012 /* Get the data. */ 1013 return bus_space_read_1(iot, ioh, MCD_STATUS); 1014 } 1015 1016 int 1017 mcd_getstat(sc) 1018 struct mcd_softc *sc; 1019 { 1020 struct mcd_mbox mbx; 1021 1022 mbx.cmd.opcode = MCD_CMDGETSTAT; 1023 mbx.cmd.length = 0; 1024 mbx.res.length = 0; 1025 return mcd_send(sc, &mbx, 1); 1026 } 1027 1028 int 1029 mcd_getresult(sc, res) 1030 struct mcd_softc *sc; 1031 struct mcd_result *res; 1032 { 1033 int i, x; 1034 1035 if (sc->debug) 1036 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname, 1037 res->length); 1038 1039 if ((x = mcd_getreply(sc)) < 0) { 1040 if (sc->debug) 1041 printf(" timeout\n"); 1042 else if (!sc->probe) 1043 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname); 1044 return EIO; 1045 } 1046 if (sc->debug) 1047 printf(" %02x", (u_int)x); 1048 sc->status = x; 1049 mcd_setflags(sc); 1050 1051 if ((sc->status & MCD_ST_CMDCHECK) != 0) 1052 return EINVAL; 1053 1054 for (i = 0; i < res->length; i++) { 1055 if ((x = mcd_getreply(sc)) < 0) { 1056 if (sc->debug) 1057 printf(" timeout\n"); 1058 else 1059 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname); 1060 return EIO; 1061 } 1062 if (sc->debug) 1063 printf(" %02x", (u_int)x); 1064 res->data.raw.data[i] = x; 1065 } 1066 1067 if (sc->debug) 1068 printf(" succeeded\n"); 1069 1070 #ifdef MCDDEBUG 1071 delay(10); 1072 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) & 1073 MCD_XF_STATUSUNAVAIL) == 0) { 1074 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS); 1075 printf("%s: got extra byte %02x during getstatus\n", 1076 sc->sc_dev.dv_xname, (u_int)x); 1077 delay(10); 1078 } 1079 #endif 1080 1081 return 0; 1082 } 1083 1084 void 1085 mcd_setflags(sc) 1086 struct mcd_softc *sc; 1087 { 1088 1089 /* Check flags. */ 1090 if ((sc->flags & MCDF_LOADED) != 0 && 1091 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) != 1092 MCD_ST_DSKIN) { 1093 if ((sc->status & MCD_ST_DOOROPEN) != 0) 1094 printf("%s: door open\n", sc->sc_dev.dv_xname); 1095 else if ((sc->status & MCD_ST_DSKIN) == 0) 1096 printf("%s: no disk present\n", sc->sc_dev.dv_xname); 1097 else if ((sc->status & MCD_ST_DSKCHNG) != 0) 1098 printf("%s: media change\n", sc->sc_dev.dv_xname); 1099 sc->flags &= ~MCDF_LOADED; 1100 } 1101 1102 if ((sc->status & MCD_ST_AUDIOBSY) != 0) 1103 sc->audio_status = CD_AS_PLAY_IN_PROGRESS; 1104 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS || 1105 sc->audio_status == CD_AS_AUDIO_INVALID) 1106 sc->audio_status = CD_AS_PLAY_COMPLETED; 1107 } 1108 1109 int 1110 mcd_send(sc, mbx, diskin) 1111 struct mcd_softc *sc; 1112 struct mcd_mbox *mbx; 1113 int diskin; 1114 { 1115 int retry, i, error; 1116 bus_space_tag_t iot = sc->sc_iot; 1117 bus_space_handle_t ioh = sc->sc_ioh; 1118 1119 if (sc->debug) { 1120 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname, 1121 mbx->cmd.length, (u_int)mbx->cmd.opcode); 1122 for (i = 0; i < mbx->cmd.length; i++) 1123 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]); 1124 printf("\n"); 1125 } 1126 1127 for (retry = MCD_RETRIES; retry; retry--) { 1128 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode); 1129 for (i = 0; i < mbx->cmd.length; i++) 1130 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]); 1131 if ((error = mcd_getresult(sc, &mbx->res)) == 0) 1132 break; 1133 if (error == EINVAL) 1134 return error; 1135 } 1136 if (!retry) 1137 return error; 1138 if (diskin && (sc->flags & MCDF_LOADED) == 0) 1139 return EIO; 1140 1141 return 0; 1142 } 1143 1144 static int 1145 bcd2bin(b) 1146 bcd_t b; 1147 { 1148 1149 return (b >> 4) * 10 + (b & 15); 1150 } 1151 1152 static bcd_t 1153 bin2bcd(b) 1154 int b; 1155 { 1156 1157 return ((b / 10) << 4) | (b % 10); 1158 } 1159 1160 static void 1161 hsg2msf(hsg, msf) 1162 int hsg; 1163 bcd_t *msf; 1164 { 1165 1166 hsg += 150; 1167 F_msf(msf) = bin2bcd(hsg % 75); 1168 hsg /= 75; 1169 S_msf(msf) = bin2bcd(hsg % 60); 1170 hsg /= 60; 1171 M_msf(msf) = bin2bcd(hsg); 1172 } 1173 1174 static daddr_t 1175 msf2hsg(msf, relative) 1176 bcd_t *msf; 1177 int relative; 1178 { 1179 daddr_t blkno; 1180 1181 blkno = bcd2bin(M_msf(msf)) * 75 * 60 + 1182 bcd2bin(S_msf(msf)) * 75 + 1183 bcd2bin(F_msf(msf)); 1184 if (!relative) 1185 blkno -= 150; 1186 return blkno; 1187 } 1188 1189 void 1190 mcd_pseudointr(v) 1191 void *v; 1192 { 1193 struct mcd_softc *sc = v; 1194 int s; 1195 1196 s = splbio(); 1197 (void) mcdintr(sc); 1198 splx(s); 1199 } 1200 1201 /* 1202 * State machine to process read requests. 1203 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode 1204 * MCD_S_WAITMODE: waits for status reply from set mode, set read command 1205 * MCD_S_WAITREAD: wait for read ready, read data. 1206 */ 1207 int 1208 mcdintr(arg) 1209 void *arg; 1210 { 1211 struct mcd_softc *sc = arg; 1212 struct mcd_mbx *mbx = &sc->mbx; 1213 struct buf *bp = mbx->bp; 1214 bus_space_tag_t iot = sc->sc_iot; 1215 bus_space_handle_t ioh = sc->sc_ioh; 1216 1217 int i; 1218 u_char x; 1219 bcd_t msf[3]; 1220 1221 switch (mbx->state) { 1222 case MCD_S_IDLE: 1223 return 0; 1224 1225 case MCD_S_BEGIN: 1226 tryagain: 1227 if (mbx->mode == sc->lastmode) 1228 goto firstblock; 1229 1230 sc->lastmode = MCD_MD_UNKNOWN; 1231 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE); 1232 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode); 1233 1234 mbx->count = RDELAY_WAITMODE; 1235 mbx->state = MCD_S_WAITMODE; 1236 1237 case MCD_S_WAITMODE: 1238 callout_stop(&sc->sc_pintr_ch); 1239 for (i = 20; i; i--) { 1240 x = bus_space_read_1(iot, ioh, MCD_XFER); 1241 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1242 break; 1243 delay(50); 1244 } 1245 if (i == 0) 1246 goto hold; 1247 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1248 mcd_setflags(sc); 1249 if ((sc->flags & MCDF_LOADED) == 0) 1250 goto changed; 1251 MCD_TRACE("doread: got WAITMODE delay=%d\n", 1252 RDELAY_WAITMODE - mbx->count, 0, 0, 0); 1253 1254 sc->lastmode = mbx->mode; 1255 1256 firstblock: 1257 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno, 1258 bp, 0, 0); 1259 1260 /* Build parameter block. */ 1261 hsg2msf(mbx->blkno, msf); 1262 1263 /* Send the read command. */ 1264 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd); 1265 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]); 1266 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]); 1267 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]); 1268 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1269 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1270 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk); 1271 1272 mbx->count = RDELAY_WAITREAD; 1273 mbx->state = MCD_S_WAITREAD; 1274 1275 case MCD_S_WAITREAD: 1276 callout_stop(&sc->sc_pintr_ch); 1277 nextblock: 1278 loop: 1279 for (i = 20; i; i--) { 1280 x = bus_space_read_1(iot, ioh, MCD_XFER); 1281 if ((x & MCD_XF_DATAUNAVAIL) == 0) 1282 goto gotblock; 1283 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1284 break; 1285 delay(50); 1286 } 1287 if (i == 0) 1288 goto hold; 1289 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1290 mcd_setflags(sc); 1291 if ((sc->flags & MCDF_LOADED) == 0) 1292 goto changed; 1293 #if 0 1294 printf("%s: got status byte %02x during read\n", 1295 sc->sc_dev.dv_xname, (u_int)sc->status); 1296 #endif 1297 goto loop; 1298 1299 gotblock: 1300 MCD_TRACE("doread: got data delay=%d\n", 1301 RDELAY_WAITREAD - mbx->count, 0, 0, 0); 1302 1303 /* Data is ready. */ 1304 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */ 1305 bus_space_read_multi_1(iot, ioh, MCD_RDATA, 1306 bp->b_data + mbx->skip, mbx->sz); 1307 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */ 1308 mbx->blkno += 1; 1309 mbx->skip += mbx->sz; 1310 if (--mbx->nblk > 0) 1311 goto nextblock; 1312 1313 mbx->state = MCD_S_IDLE; 1314 1315 /* Return buffer. */ 1316 bp->b_resid = 0; 1317 disk_unbusy(&sc->sc_dk, bp->b_bcount); 1318 biodone(bp); 1319 1320 mcdstart(sc); 1321 return 1; 1322 1323 hold: 1324 if (mbx->count-- < 0) { 1325 printf("%s: timeout in state %d", 1326 sc->sc_dev.dv_xname, mbx->state); 1327 goto readerr; 1328 } 1329 1330 #if 0 1331 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname, 1332 mbx->state); 1333 #endif 1334 callout_reset(&sc->sc_pintr_ch, hz / 100, 1335 mcd_pseudointr, sc); 1336 return -1; 1337 } 1338 1339 readerr: 1340 if (mbx->retry-- > 0) { 1341 printf("; retrying\n"); 1342 goto tryagain; 1343 } else 1344 printf("; giving up\n"); 1345 1346 changed: 1347 /* Invalidate the buffer. */ 1348 bp->b_flags |= B_ERROR; 1349 bp->b_resid = bp->b_bcount - mbx->skip; 1350 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid)); 1351 biodone(bp); 1352 1353 mcdstart(sc); 1354 return -1; 1355 1356 #ifdef notyet 1357 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname); 1358 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET); 1359 delay(300000); 1360 (void) mcd_getstat(sc, 1); 1361 (void) mcd_getstat(sc, 1); 1362 /*sc->status &= ~MCD_ST_DSKCHNG; */ 1363 sc->debug = 1; /* preventive set debug mode */ 1364 #endif 1365 } 1366 1367 void 1368 mcd_soft_reset(sc) 1369 struct mcd_softc *sc; 1370 { 1371 1372 sc->debug = 0; 1373 sc->flags = 0; 1374 sc->lastmode = MCD_MD_UNKNOWN; 1375 sc->lastupc = MCD_UPC_UNKNOWN; 1376 sc->audio_status = CD_AS_AUDIO_INVALID; 1377 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */ 1378 } 1379 1380 int 1381 mcd_hard_reset(sc) 1382 struct mcd_softc *sc; 1383 { 1384 struct mcd_mbox mbx; 1385 1386 mcd_soft_reset(sc); 1387 1388 mbx.cmd.opcode = MCD_CMDRESET; 1389 mbx.cmd.length = 0; 1390 mbx.res.length = 0; 1391 return mcd_send(sc, &mbx, 0); 1392 } 1393 1394 int 1395 mcd_setmode(sc, mode) 1396 struct mcd_softc *sc; 1397 int mode; 1398 { 1399 struct mcd_mbox mbx; 1400 int error; 1401 1402 if (sc->lastmode == mode) 1403 return 0; 1404 if (sc->debug) 1405 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode); 1406 sc->lastmode = MCD_MD_UNKNOWN; 1407 1408 mbx.cmd.opcode = MCD_CMDSETMODE; 1409 mbx.cmd.length = sizeof(mbx.cmd.data.datamode); 1410 mbx.cmd.data.datamode.mode = mode; 1411 mbx.res.length = 0; 1412 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1413 return error; 1414 1415 sc->lastmode = mode; 1416 return 0; 1417 } 1418 1419 int 1420 mcd_setupc(sc, upc) 1421 struct mcd_softc *sc; 1422 int upc; 1423 { 1424 struct mcd_mbox mbx; 1425 int error; 1426 1427 if (sc->lastupc == upc) 1428 return 0; 1429 if (sc->debug) 1430 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc); 1431 sc->lastupc = MCD_UPC_UNKNOWN; 1432 1433 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE; 1434 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1; 1435 mbx.cmd.data.config.subcommand = MCD_CF_READUPC; 1436 mbx.cmd.data.config.data1 = upc; 1437 mbx.res.length = 0; 1438 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1439 return error; 1440 1441 sc->lastupc = upc; 1442 return 0; 1443 } 1444 1445 int 1446 mcd_toc_header(sc, th) 1447 struct mcd_softc *sc; 1448 struct ioc_toc_header *th; 1449 { 1450 1451 if (sc->debug) 1452 printf("%s: mcd_toc_header: reading toc header\n", 1453 sc->sc_dev.dv_xname); 1454 1455 th->len = msf2hsg(sc->volinfo.vol_msf, 0); 1456 th->starting_track = bcd2bin(sc->volinfo.trk_low); 1457 th->ending_track = bcd2bin(sc->volinfo.trk_high); 1458 1459 return 0; 1460 } 1461 1462 int 1463 mcd_read_toc(sc) 1464 struct mcd_softc *sc; 1465 { 1466 struct ioc_toc_header th; 1467 union mcd_qchninfo q; 1468 int error, trk, idx, retry; 1469 1470 if ((error = mcd_toc_header(sc, &th)) != 0) 1471 return error; 1472 1473 if ((error = mcd_stop(sc)) != 0) 1474 return error; 1475 1476 if (sc->debug) 1477 printf("%s: read_toc: reading qchannel info\n", 1478 sc->sc_dev.dv_xname); 1479 1480 for (trk = th.starting_track; trk <= th.ending_track; trk++) 1481 sc->toc[trk].toc.idx_no = 0x00; 1482 trk = th.ending_track - th.starting_track + 1; 1483 for (retry = 300; retry && trk > 0; retry--) { 1484 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0) 1485 break; 1486 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00) 1487 continue; 1488 idx = bcd2bin(q.toc.idx_no); 1489 if (idx < MCD_MAXTOCS && 1490 sc->toc[idx].toc.idx_no == 0x00) { 1491 sc->toc[idx] = q; 1492 trk--; 1493 } 1494 } 1495 1496 /* Inform the drive that we're finished so it turns off the light. */ 1497 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1498 return error; 1499 1500 if (trk != 0) 1501 return EINVAL; 1502 1503 /* Add a fake last+1 for mcd_playtracks(). */ 1504 idx = th.ending_track + 1; 1505 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control; 1506 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type; 1507 sc->toc[idx].toc.trk_no = 0x00; 1508 sc->toc[idx].toc.idx_no = 0xaa; 1509 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0]; 1510 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1]; 1511 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2]; 1512 1513 return 0; 1514 } 1515 1516 int 1517 mcd_toc_entries(sc, te) 1518 struct mcd_softc *sc; 1519 struct ioc_read_toc_entry *te; 1520 { 1521 int len = te->data_len; 1522 struct ret_toc { 1523 struct ioc_toc_header header; 1524 struct cd_toc_entry entries[MCD_MAXTOCS]; 1525 } data; 1526 u_char trk; 1527 daddr_t lba; 1528 int error, n; 1529 1530 if (len > sizeof(data.entries) || 1531 len < sizeof(struct cd_toc_entry)) 1532 return EINVAL; 1533 if (te->address_format != CD_MSF_FORMAT && 1534 te->address_format != CD_LBA_FORMAT) 1535 return EINVAL; 1536 1537 /* Copy the TOC header. */ 1538 if ((error = mcd_toc_header(sc, &data.header)) != 0) 1539 return error; 1540 1541 /* Verify starting track. */ 1542 trk = te->starting_track; 1543 if (trk == 0x00) 1544 trk = data.header.starting_track; 1545 else if (trk == 0xaa) 1546 trk = data.header.ending_track + 1; 1547 else if (trk < data.header.starting_track || 1548 trk > data.header.ending_track + 1) 1549 return EINVAL; 1550 1551 /* Copy the TOC data. */ 1552 for (n = 0; trk <= data.header.ending_track + 1; trk++) { 1553 if (sc->toc[trk].toc.idx_no == 0x00) 1554 continue; 1555 data.entries[n].control = sc->toc[trk].toc.control; 1556 data.entries[n].addr_type = sc->toc[trk].toc.addr_type; 1557 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no); 1558 switch (te->address_format) { 1559 case CD_MSF_FORMAT: 1560 data.entries[n].addr.addr[0] = 0; 1561 data.entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]); 1562 data.entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]); 1563 data.entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]); 1564 break; 1565 case CD_LBA_FORMAT: 1566 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0); 1567 data.entries[n].addr.addr[0] = lba >> 24; 1568 data.entries[n].addr.addr[1] = lba >> 16; 1569 data.entries[n].addr.addr[2] = lba >> 8; 1570 data.entries[n].addr.addr[3] = lba; 1571 break; 1572 } 1573 n++; 1574 } 1575 1576 len = min(len, n * sizeof(struct cd_toc_entry)); 1577 1578 /* Copy the data back. */ 1579 return copyout(&data.entries[0], te->data, len); 1580 } 1581 1582 int 1583 mcd_stop(sc) 1584 struct mcd_softc *sc; 1585 { 1586 struct mcd_mbox mbx; 1587 int error; 1588 1589 if (sc->debug) 1590 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname); 1591 1592 mbx.cmd.opcode = MCD_CMDSTOPAUDIO; 1593 mbx.cmd.length = 0; 1594 mbx.res.length = 0; 1595 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1596 return error; 1597 1598 sc->audio_status = CD_AS_PLAY_COMPLETED; 1599 return 0; 1600 } 1601 1602 int 1603 mcd_getqchan(sc, q, qchn) 1604 struct mcd_softc *sc; 1605 union mcd_qchninfo *q; 1606 int qchn; 1607 { 1608 struct mcd_mbox mbx; 1609 int error; 1610 1611 if (qchn == CD_TRACK_INFO) { 1612 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0) 1613 return error; 1614 } else { 1615 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1616 return error; 1617 } 1618 if (qchn == CD_MEDIA_CATALOG) { 1619 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0) 1620 return error; 1621 } else { 1622 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0) 1623 return error; 1624 } 1625 1626 mbx.cmd.opcode = MCD_CMDGETQCHN; 1627 mbx.cmd.length = 0; 1628 mbx.res.length = sizeof(mbx.res.data.qchninfo); 1629 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1630 return error; 1631 1632 *q = mbx.res.data.qchninfo; 1633 return 0; 1634 } 1635 1636 int 1637 mcd_read_subchannel(sc, ch) 1638 struct mcd_softc *sc; 1639 struct ioc_read_subchannel *ch; 1640 { 1641 int len = ch->data_len; 1642 union mcd_qchninfo q; 1643 struct cd_sub_channel_info data; 1644 daddr_t lba; 1645 int error; 1646 1647 if (sc->debug) 1648 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname, 1649 ch->address_format, ch->data_format); 1650 1651 if (len > sizeof(data) || 1652 len < sizeof(struct cd_sub_channel_header)) 1653 return EINVAL; 1654 if (ch->address_format != CD_MSF_FORMAT && 1655 ch->address_format != CD_LBA_FORMAT) 1656 return EINVAL; 1657 if (ch->data_format != CD_CURRENT_POSITION && 1658 ch->data_format != CD_MEDIA_CATALOG) 1659 return EINVAL; 1660 1661 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0) 1662 return error; 1663 1664 data.header.audio_status = sc->audio_status; 1665 data.what.media_catalog.data_format = ch->data_format; 1666 1667 switch (ch->data_format) { 1668 case CD_MEDIA_CATALOG: 1669 data.what.media_catalog.mc_valid = 1; 1670 #if 0 1671 data.what.media_catalog.mc_number = 1672 #endif 1673 break; 1674 1675 case CD_CURRENT_POSITION: 1676 data.what.position.track_number = bcd2bin(q.current.trk_no); 1677 data.what.position.index_number = bcd2bin(q.current.idx_no); 1678 switch (ch->address_format) { 1679 case CD_MSF_FORMAT: 1680 data.what.position.reladdr.addr[0] = 0; 1681 data.what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]); 1682 data.what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]); 1683 data.what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]); 1684 data.what.position.absaddr.addr[0] = 0; 1685 data.what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]); 1686 data.what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]); 1687 data.what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]); 1688 break; 1689 case CD_LBA_FORMAT: 1690 lba = msf2hsg(q.current.relative_pos, 1); 1691 /* 1692 * Pre-gap has index number of 0, and decreasing MSF 1693 * address. Must be converted to negative LBA, per 1694 * SCSI spec. 1695 */ 1696 if (data.what.position.index_number == 0x00) 1697 lba = -lba; 1698 data.what.position.reladdr.addr[0] = lba >> 24; 1699 data.what.position.reladdr.addr[1] = lba >> 16; 1700 data.what.position.reladdr.addr[2] = lba >> 8; 1701 data.what.position.reladdr.addr[3] = lba; 1702 lba = msf2hsg(q.current.absolute_pos, 0); 1703 data.what.position.absaddr.addr[0] = lba >> 24; 1704 data.what.position.absaddr.addr[1] = lba >> 16; 1705 data.what.position.absaddr.addr[2] = lba >> 8; 1706 data.what.position.absaddr.addr[3] = lba; 1707 break; 1708 } 1709 break; 1710 } 1711 1712 return copyout(&data, ch->data, len); 1713 } 1714 1715 int 1716 mcd_playtracks(sc, p) 1717 struct mcd_softc *sc; 1718 struct ioc_play_track *p; 1719 { 1720 struct mcd_mbox mbx; 1721 int a = p->start_track; 1722 int z = p->end_track; 1723 int error; 1724 1725 if (sc->debug) 1726 printf("%s: playtracks: from %d:%d to %d:%d\n", 1727 sc->sc_dev.dv_xname, 1728 a, p->start_index, z, p->end_index); 1729 1730 if (a < bcd2bin(sc->volinfo.trk_low) || 1731 a > bcd2bin(sc->volinfo.trk_high) || 1732 a > z || 1733 z < bcd2bin(sc->volinfo.trk_low) || 1734 z > bcd2bin(sc->volinfo.trk_high)) 1735 return EINVAL; 1736 1737 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1738 return error; 1739 1740 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1741 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1742 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0]; 1743 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1]; 1744 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2]; 1745 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0]; 1746 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1]; 1747 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2]; 1748 sc->lastpb = mbx.cmd; 1749 mbx.res.length = 0; 1750 return mcd_send(sc, &mbx, 1); 1751 } 1752 1753 int 1754 mcd_playmsf(sc, p) 1755 struct mcd_softc *sc; 1756 struct ioc_play_msf *p; 1757 { 1758 struct mcd_mbox mbx; 1759 int error; 1760 1761 if (sc->debug) 1762 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n", 1763 sc->sc_dev.dv_xname, 1764 p->start_m, p->start_s, p->start_f, 1765 p->end_m, p->end_s, p->end_f); 1766 1767 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >= 1768 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f)) 1769 return EINVAL; 1770 1771 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1772 return error; 1773 1774 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1775 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1776 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m); 1777 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s); 1778 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f); 1779 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m); 1780 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s); 1781 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f); 1782 sc->lastpb = mbx.cmd; 1783 mbx.res.length = 0; 1784 return mcd_send(sc, &mbx, 1); 1785 } 1786 1787 int 1788 mcd_playblocks(sc, p) 1789 struct mcd_softc *sc; 1790 struct ioc_play_blocks *p; 1791 { 1792 struct mcd_mbox mbx; 1793 int error; 1794 1795 if (sc->debug) 1796 printf("%s: playblocks: blkno %d length %d\n", 1797 sc->sc_dev.dv_xname, p->blk, p->len); 1798 1799 if (p->blk > sc->disksize || p->len > sc->disksize || 1800 (p->blk + p->len) > sc->disksize) 1801 return 0; 1802 1803 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1804 return error; 1805 1806 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1807 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1808 hsg2msf(p->blk, mbx.cmd.data.play.start_msf); 1809 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf); 1810 sc->lastpb = mbx.cmd; 1811 mbx.res.length = 0; 1812 return mcd_send(sc, &mbx, 1); 1813 } 1814 1815 int 1816 mcd_pause(sc) 1817 struct mcd_softc *sc; 1818 { 1819 union mcd_qchninfo q; 1820 int error; 1821 1822 /* Verify current status. */ 1823 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) { 1824 printf("%s: pause: attempted when not playing\n", 1825 sc->sc_dev.dv_xname); 1826 return EINVAL; 1827 } 1828 1829 /* Get the current position. */ 1830 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0) 1831 return error; 1832 1833 /* Copy it into lastpb. */ 1834 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0]; 1835 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1]; 1836 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2]; 1837 1838 /* Stop playing. */ 1839 if ((error = mcd_stop(sc)) != 0) 1840 return error; 1841 1842 /* Set the proper status and exit. */ 1843 sc->audio_status = CD_AS_PLAY_PAUSED; 1844 return 0; 1845 } 1846 1847 int 1848 mcd_resume(sc) 1849 struct mcd_softc *sc; 1850 { 1851 struct mcd_mbox mbx; 1852 int error; 1853 1854 if (sc->audio_status != CD_AS_PLAY_PAUSED) 1855 return EINVAL; 1856 1857 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1858 return error; 1859 1860 mbx.cmd = sc->lastpb; 1861 mbx.res.length = 0; 1862 return mcd_send(sc, &mbx, 1); 1863 } 1864 1865 int 1866 mcd_eject(sc) 1867 struct mcd_softc *sc; 1868 { 1869 struct mcd_mbox mbx; 1870 1871 mbx.cmd.opcode = MCD_CMDEJECTDISK; 1872 mbx.cmd.length = 0; 1873 mbx.res.length = 0; 1874 return mcd_send(sc, &mbx, 0); 1875 } 1876 1877 int 1878 mcd_setlock(sc, mode) 1879 struct mcd_softc *sc; 1880 int mode; 1881 { 1882 struct mcd_mbox mbx; 1883 1884 mbx.cmd.opcode = MCD_CMDSETLOCK; 1885 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode); 1886 mbx.cmd.data.lockmode.mode = mode; 1887 mbx.res.length = 0; 1888 return mcd_send(sc, &mbx, 1); 1889 } 1890