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