1 /* $NetBSD: mcd.c,v 1.109 2009/05/12 09:10:15 cegger 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.109 2009/05/12 09:10:15 cegger 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(device_t, cfdata_t, void *); 194 void mcdattach(device_t, device_t, 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(device_t parent, device_t 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(device_t parent, cfdata_t match, void *aux) 913 { 914 struct isa_attach_args *ia = aux; 915 struct mcd_softc sc; 916 bus_space_tag_t iot = ia->ia_iot; 917 bus_space_handle_t ioh; 918 int rv; 919 920 if (ia->ia_nio < 1) 921 return (0); 922 if (ia->ia_nirq < 1) 923 return (0); 924 925 if (ISA_DIRECT_CONFIG(ia)) 926 return (0); 927 928 /* Disallow wildcarded i/o address. */ 929 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 930 return (0); 931 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) 932 return (0); 933 934 /* Map i/o space */ 935 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) 936 return 0; 937 938 sc.debug = 0; 939 sc.probe = 1; 940 941 rv = mcd_find(iot, ioh, &sc); 942 943 bus_space_unmap(iot, ioh, MCD_NPORT); 944 945 if (rv) { 946 ia->ia_nio = 1; 947 ia->ia_io[0].ir_size = MCD_NPORT; 948 949 ia->ia_nirq = 1; 950 951 ia->ia_niomem = 0; 952 ia->ia_ndrq = 0; 953 } 954 955 return (rv); 956 } 957 958 int 959 mcd_getreply(struct mcd_softc *sc) 960 { 961 bus_space_tag_t iot = sc->sc_iot; 962 bus_space_handle_t ioh = sc->sc_ioh; 963 int i; 964 965 /* Wait until xfer port senses data ready. */ 966 for (i = DELAY_GETREPLY; i; i--) { 967 if ((bus_space_read_1(iot, ioh, MCD_XFER) & 968 MCD_XF_STATUSUNAVAIL) == 0) 969 break; 970 delay(DELAY_GRANULARITY); 971 } 972 if (!i) 973 return -1; 974 975 /* Get the data. */ 976 return bus_space_read_1(iot, ioh, MCD_STATUS); 977 } 978 979 int 980 mcd_getstat(struct mcd_softc *sc) 981 { 982 struct mcd_mbox mbx; 983 984 mbx.cmd.opcode = MCD_CMDGETSTAT; 985 mbx.cmd.length = 0; 986 mbx.res.length = 0; 987 return mcd_send(sc, &mbx, 1); 988 } 989 990 int 991 mcd_getresult(struct mcd_softc *sc, struct mcd_result *res) 992 { 993 int i, x; 994 995 if (sc->debug) 996 printf("%s: mcd_getresult: %d", device_xname(&sc->sc_dev), 997 res->length); 998 999 if ((x = mcd_getreply(sc)) < 0) { 1000 if (sc->debug) 1001 printf(" timeout\n"); 1002 else if (!sc->probe) 1003 printf("%s: timeout in getresult\n", device_xname(&sc->sc_dev)); 1004 return EIO; 1005 } 1006 if (sc->debug) 1007 printf(" %02x", (u_int)x); 1008 sc->status = x; 1009 mcd_setflags(sc); 1010 1011 if ((sc->status & MCD_ST_CMDCHECK) != 0) 1012 return EINVAL; 1013 1014 for (i = 0; i < res->length; i++) { 1015 if ((x = mcd_getreply(sc)) < 0) { 1016 if (sc->debug) 1017 printf(" timeout\n"); 1018 else 1019 printf("%s: timeout in getresult\n", device_xname(&sc->sc_dev)); 1020 return EIO; 1021 } 1022 if (sc->debug) 1023 printf(" %02x", (u_int)x); 1024 res->data.raw.data[i] = x; 1025 } 1026 1027 if (sc->debug) 1028 printf(" succeeded\n"); 1029 1030 #ifdef MCDDEBUG 1031 delay(10); 1032 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) & 1033 MCD_XF_STATUSUNAVAIL) == 0) { 1034 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS); 1035 printf("%s: got extra byte %02x during getstatus\n", 1036 device_xname(&sc->sc_dev), (u_int)x); 1037 delay(10); 1038 } 1039 #endif 1040 1041 return 0; 1042 } 1043 1044 void 1045 mcd_setflags(struct mcd_softc *sc) 1046 { 1047 1048 /* Check flags. */ 1049 if ((sc->flags & MCDF_LOADED) != 0 && 1050 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) != 1051 MCD_ST_DSKIN) { 1052 if ((sc->status & MCD_ST_DOOROPEN) != 0) 1053 printf("%s: door open\n", device_xname(&sc->sc_dev)); 1054 else if ((sc->status & MCD_ST_DSKIN) == 0) 1055 printf("%s: no disk present\n", device_xname(&sc->sc_dev)); 1056 else if ((sc->status & MCD_ST_DSKCHNG) != 0) 1057 printf("%s: media change\n", device_xname(&sc->sc_dev)); 1058 sc->flags &= ~MCDF_LOADED; 1059 } 1060 1061 if ((sc->status & MCD_ST_AUDIOBSY) != 0) 1062 sc->audio_status = CD_AS_PLAY_IN_PROGRESS; 1063 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS || 1064 sc->audio_status == CD_AS_AUDIO_INVALID) 1065 sc->audio_status = CD_AS_PLAY_COMPLETED; 1066 } 1067 1068 int 1069 mcd_send(struct mcd_softc *sc, struct mcd_mbox *mbx, int diskin) 1070 { 1071 int retry, i, error; 1072 bus_space_tag_t iot = sc->sc_iot; 1073 bus_space_handle_t ioh = sc->sc_ioh; 1074 1075 if (sc->debug) { 1076 printf("%s: mcd_send: %d %02x", device_xname(&sc->sc_dev), 1077 mbx->cmd.length, (u_int)mbx->cmd.opcode); 1078 for (i = 0; i < mbx->cmd.length; i++) 1079 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]); 1080 printf("\n"); 1081 } 1082 1083 for (retry = MCD_RETRIES; retry; retry--) { 1084 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode); 1085 for (i = 0; i < mbx->cmd.length; i++) 1086 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]); 1087 if ((error = mcd_getresult(sc, &mbx->res)) == 0) 1088 break; 1089 if (error == EINVAL) 1090 return error; 1091 } 1092 if (!retry) 1093 return error; 1094 if (diskin && (sc->flags & MCDF_LOADED) == 0) 1095 return EIO; 1096 1097 return 0; 1098 } 1099 1100 static int 1101 bcd2bin(bcd_t b) 1102 { 1103 1104 return (b >> 4) * 10 + (b & 15); 1105 } 1106 1107 static bcd_t 1108 bin2bcd(int b) 1109 { 1110 1111 return ((b / 10) << 4) | (b % 10); 1112 } 1113 1114 static void 1115 hsg2msf(int hsg, bcd_t *msf) 1116 { 1117 1118 hsg += 150; 1119 F_msf(msf) = bin2bcd(hsg % 75); 1120 hsg /= 75; 1121 S_msf(msf) = bin2bcd(hsg % 60); 1122 hsg /= 60; 1123 M_msf(msf) = bin2bcd(hsg); 1124 } 1125 1126 static daddr_t 1127 msf2hsg(bcd_t *msf, int relative) 1128 { 1129 daddr_t blkno; 1130 1131 blkno = bcd2bin(M_msf(msf)) * 75 * 60 + 1132 bcd2bin(S_msf(msf)) * 75 + 1133 bcd2bin(F_msf(msf)); 1134 if (!relative) 1135 blkno -= 150; 1136 return blkno; 1137 } 1138 1139 void 1140 mcd_pseudointr(void *v) 1141 { 1142 struct mcd_softc *sc = v; 1143 int s; 1144 1145 s = splbio(); 1146 (void) mcdintr(sc); 1147 splx(s); 1148 } 1149 1150 /* 1151 * State machine to process read requests. 1152 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode 1153 * MCD_S_WAITMODE: waits for status reply from set mode, set read command 1154 * MCD_S_WAITREAD: wait for read ready, read data. 1155 */ 1156 int 1157 mcdintr(void *arg) 1158 { 1159 struct mcd_softc *sc = arg; 1160 struct mcd_mbx *mbx = &sc->mbx; 1161 struct buf *bp = mbx->bp; 1162 bus_space_tag_t iot = sc->sc_iot; 1163 bus_space_handle_t ioh = sc->sc_ioh; 1164 1165 int i; 1166 u_char x; 1167 bcd_t msf[3]; 1168 1169 switch (mbx->state) { 1170 case MCD_S_IDLE: 1171 return 0; 1172 1173 case MCD_S_BEGIN: 1174 tryagain: 1175 if (mbx->mode == sc->lastmode) 1176 goto firstblock; 1177 1178 sc->lastmode = MCD_MD_UNKNOWN; 1179 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE); 1180 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode); 1181 1182 mbx->count = RDELAY_WAITMODE; 1183 mbx->state = MCD_S_WAITMODE; 1184 1185 case MCD_S_WAITMODE: 1186 callout_stop(&sc->sc_pintr_ch); 1187 for (i = 20; i; i--) { 1188 x = bus_space_read_1(iot, ioh, MCD_XFER); 1189 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1190 break; 1191 delay(50); 1192 } 1193 if (i == 0) 1194 goto hold; 1195 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1196 mcd_setflags(sc); 1197 if ((sc->flags & MCDF_LOADED) == 0) 1198 goto changed; 1199 MCD_TRACE("doread: got WAITMODE delay=%d\n", 1200 RDELAY_WAITMODE - mbx->count); 1201 1202 sc->lastmode = mbx->mode; 1203 1204 firstblock: 1205 MCD_TRACE("doread: read blkno=%d for bp=0x%p\n", 1206 (int) mbx->blkno, bp); 1207 1208 /* Build parameter block. */ 1209 hsg2msf(mbx->blkno, msf); 1210 1211 /* Send the read command. */ 1212 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd); 1213 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]); 1214 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]); 1215 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]); 1216 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1217 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1218 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk); 1219 1220 mbx->count = RDELAY_WAITREAD; 1221 mbx->state = MCD_S_WAITREAD; 1222 1223 case MCD_S_WAITREAD: 1224 callout_stop(&sc->sc_pintr_ch); 1225 nextblock: 1226 loop: 1227 for (i = 20; i; i--) { 1228 x = bus_space_read_1(iot, ioh, MCD_XFER); 1229 if ((x & MCD_XF_DATAUNAVAIL) == 0) 1230 goto gotblock; 1231 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1232 break; 1233 delay(50); 1234 } 1235 if (i == 0) 1236 goto hold; 1237 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1238 mcd_setflags(sc); 1239 if ((sc->flags & MCDF_LOADED) == 0) 1240 goto changed; 1241 #if 0 1242 printf("%s: got status byte %02x during read\n", 1243 device_xname(&sc->sc_dev), (u_int)sc->status); 1244 #endif 1245 goto loop; 1246 1247 gotblock: 1248 MCD_TRACE("doread: got data delay=%d\n", 1249 RDELAY_WAITREAD - mbx->count); 1250 1251 /* Data is ready. */ 1252 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */ 1253 bus_space_read_multi_1(iot, ioh, MCD_RDATA, 1254 (char *)bp->b_data + mbx->skip, mbx->sz); 1255 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */ 1256 mbx->blkno += 1; 1257 mbx->skip += mbx->sz; 1258 if (--mbx->nblk > 0) 1259 goto nextblock; 1260 1261 mbx->state = MCD_S_IDLE; 1262 1263 /* Return buffer. */ 1264 bp->b_resid = 0; 1265 disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ)); 1266 biodone(bp); 1267 1268 mcdstart(sc); 1269 return 1; 1270 1271 hold: 1272 if (mbx->count-- < 0) { 1273 printf("%s: timeout in state %d", 1274 device_xname(&sc->sc_dev), mbx->state); 1275 goto readerr; 1276 } 1277 1278 #if 0 1279 printf("%s: sleep in state %d\n", device_xname(&sc->sc_dev), 1280 mbx->state); 1281 #endif 1282 callout_reset(&sc->sc_pintr_ch, hz / 100, 1283 mcd_pseudointr, sc); 1284 return -1; 1285 } 1286 1287 readerr: 1288 if (mbx->retry-- > 0) { 1289 printf("; retrying\n"); 1290 goto tryagain; 1291 } else 1292 printf("; giving up\n"); 1293 1294 changed: 1295 /* Invalidate the buffer. */ 1296 bp->b_error = EIO; 1297 bp->b_resid = bp->b_bcount - mbx->skip; 1298 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid), 1299 (bp->b_flags & B_READ)); 1300 biodone(bp); 1301 1302 mcdstart(sc); 1303 return -1; 1304 1305 #ifdef notyet 1306 printf("%s: unit timeout; resetting\n", device_xname(&sc->sc_dev)); 1307 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET); 1308 delay(300000); 1309 (void) mcd_getstat(sc, 1); 1310 (void) mcd_getstat(sc, 1); 1311 /*sc->status &= ~MCD_ST_DSKCHNG; */ 1312 sc->debug = 1; /* preventive set debug mode */ 1313 #endif 1314 } 1315 1316 void 1317 mcd_soft_reset(struct mcd_softc *sc) 1318 { 1319 1320 sc->debug = 0; 1321 sc->flags = 0; 1322 sc->lastmode = MCD_MD_UNKNOWN; 1323 sc->lastupc = MCD_UPC_UNKNOWN; 1324 sc->audio_status = CD_AS_AUDIO_INVALID; 1325 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */ 1326 } 1327 1328 int 1329 mcd_hard_reset(struct mcd_softc *sc) 1330 { 1331 struct mcd_mbox mbx; 1332 1333 mcd_soft_reset(sc); 1334 1335 mbx.cmd.opcode = MCD_CMDRESET; 1336 mbx.cmd.length = 0; 1337 mbx.res.length = 0; 1338 return mcd_send(sc, &mbx, 0); 1339 } 1340 1341 int 1342 mcd_setmode(struct mcd_softc *sc, int mode) 1343 { 1344 struct mcd_mbox mbx; 1345 int error; 1346 1347 if (sc->lastmode == mode) 1348 return 0; 1349 if (sc->debug) 1350 printf("%s: setting mode to %d\n", device_xname(&sc->sc_dev), mode); 1351 sc->lastmode = MCD_MD_UNKNOWN; 1352 1353 mbx.cmd.opcode = MCD_CMDSETMODE; 1354 mbx.cmd.length = sizeof(mbx.cmd.data.datamode); 1355 mbx.cmd.data.datamode.mode = mode; 1356 mbx.res.length = 0; 1357 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1358 return error; 1359 1360 sc->lastmode = mode; 1361 return 0; 1362 } 1363 1364 int 1365 mcd_setupc(struct mcd_softc *sc, int upc) 1366 { 1367 struct mcd_mbox mbx; 1368 int error; 1369 1370 if (sc->lastupc == upc) 1371 return 0; 1372 if (sc->debug) 1373 printf("%s: setting upc to %d\n", device_xname(&sc->sc_dev), upc); 1374 sc->lastupc = MCD_UPC_UNKNOWN; 1375 1376 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE; 1377 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1; 1378 mbx.cmd.data.config.subcommand = MCD_CF_READUPC; 1379 mbx.cmd.data.config.data1 = upc; 1380 mbx.res.length = 0; 1381 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1382 return error; 1383 1384 sc->lastupc = upc; 1385 return 0; 1386 } 1387 1388 int 1389 mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th) 1390 { 1391 1392 if (sc->debug) 1393 printf("%s: mcd_toc_header: reading toc header\n", 1394 device_xname(&sc->sc_dev)); 1395 1396 th->len = msf2hsg(sc->volinfo.vol_msf, 0); 1397 th->starting_track = bcd2bin(sc->volinfo.trk_low); 1398 th->ending_track = bcd2bin(sc->volinfo.trk_high); 1399 1400 return 0; 1401 } 1402 1403 int 1404 mcd_read_toc(struct mcd_softc *sc) 1405 { 1406 struct ioc_toc_header th; 1407 union mcd_qchninfo q; 1408 int error, trk, idx, retry; 1409 1410 if ((error = mcd_toc_header(sc, &th)) != 0) 1411 return error; 1412 1413 if ((error = mcd_stop(sc)) != 0) 1414 return error; 1415 1416 if (sc->debug) 1417 printf("%s: read_toc: reading qchannel info\n", 1418 device_xname(&sc->sc_dev)); 1419 1420 for (trk = th.starting_track; trk <= th.ending_track; trk++) 1421 sc->toc[trk].toc.idx_no = 0x00; 1422 trk = th.ending_track - th.starting_track + 1; 1423 for (retry = 300; retry && trk > 0; retry--) { 1424 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0) 1425 break; 1426 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00) 1427 continue; 1428 idx = bcd2bin(q.toc.idx_no); 1429 if (idx < MCD_MAXTOCS && 1430 sc->toc[idx].toc.idx_no == 0x00) { 1431 sc->toc[idx] = q; 1432 trk--; 1433 } 1434 } 1435 1436 /* Inform the drive that we're finished so it turns off the light. */ 1437 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1438 return error; 1439 1440 if (trk != 0) 1441 return EINVAL; 1442 1443 /* Add a fake last+1 for mcd_playtracks(). */ 1444 idx = th.ending_track + 1; 1445 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control; 1446 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type; 1447 sc->toc[idx].toc.trk_no = 0x00; 1448 sc->toc[idx].toc.idx_no = 0xaa; 1449 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0]; 1450 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1]; 1451 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2]; 1452 1453 return 0; 1454 } 1455 1456 int 1457 mcd_toc_entries(struct mcd_softc *sc, struct ioc_read_toc_entry *te, struct cd_toc_entry *entries, int *count) 1458 { 1459 int len = te->data_len; 1460 struct ioc_toc_header header; 1461 u_char trk; 1462 daddr_t lba; 1463 int error, n; 1464 1465 if (len < sizeof(struct cd_toc_entry)) 1466 return EINVAL; 1467 if (te->address_format != CD_MSF_FORMAT && 1468 te->address_format != CD_LBA_FORMAT) 1469 return EINVAL; 1470 1471 /* Copy the TOC header. */ 1472 if ((error = mcd_toc_header(sc, &header)) != 0) 1473 return error; 1474 1475 /* Verify starting track. */ 1476 trk = te->starting_track; 1477 if (trk == 0x00) 1478 trk = header.starting_track; 1479 else if (trk == 0xaa) 1480 trk = header.ending_track + 1; 1481 else if (trk < header.starting_track || 1482 trk > header.ending_track + 1) 1483 return EINVAL; 1484 1485 /* Copy the TOC data. */ 1486 for (n = 0; trk <= header.ending_track + 1; n++, trk++) { 1487 if (n * sizeof entries[0] > len) 1488 break; 1489 if (sc->toc[trk].toc.idx_no == 0x00) 1490 continue; 1491 entries[n].control = sc->toc[trk].toc.control; 1492 entries[n].addr_type = sc->toc[trk].toc.addr_type; 1493 entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no); 1494 switch (te->address_format) { 1495 case CD_MSF_FORMAT: 1496 entries[n].addr.addr[0] = 0; 1497 entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]); 1498 entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]); 1499 entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]); 1500 break; 1501 case CD_LBA_FORMAT: 1502 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0); 1503 entries[n].addr.addr[0] = lba >> 24; 1504 entries[n].addr.addr[1] = lba >> 16; 1505 entries[n].addr.addr[2] = lba >> 8; 1506 entries[n].addr.addr[3] = lba; 1507 break; 1508 } 1509 } 1510 1511 *count = n; 1512 return 0; 1513 } 1514 1515 int 1516 mcd_stop(struct mcd_softc *sc) 1517 { 1518 struct mcd_mbox mbx; 1519 int error; 1520 1521 if (sc->debug) 1522 printf("%s: mcd_stop: stopping play\n", device_xname(&sc->sc_dev)); 1523 1524 mbx.cmd.opcode = MCD_CMDSTOPAUDIO; 1525 mbx.cmd.length = 0; 1526 mbx.res.length = 0; 1527 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1528 return error; 1529 1530 sc->audio_status = CD_AS_PLAY_COMPLETED; 1531 return 0; 1532 } 1533 1534 int 1535 mcd_getqchan(struct mcd_softc *sc, union mcd_qchninfo *q, int qchn) 1536 { 1537 struct mcd_mbox mbx; 1538 int error; 1539 1540 if (qchn == CD_TRACK_INFO) { 1541 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0) 1542 return error; 1543 } else { 1544 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1545 return error; 1546 } 1547 if (qchn == CD_MEDIA_CATALOG) { 1548 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0) 1549 return error; 1550 } else { 1551 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0) 1552 return error; 1553 } 1554 1555 mbx.cmd.opcode = MCD_CMDGETQCHN; 1556 mbx.cmd.length = 0; 1557 mbx.res.length = sizeof(mbx.res.data.qchninfo); 1558 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1559 return error; 1560 1561 *q = mbx.res.data.qchninfo; 1562 return 0; 1563 } 1564 1565 int 1566 mcd_read_subchannel(struct mcd_softc *sc, struct ioc_read_subchannel *ch, struct cd_sub_channel_info *info) 1567 { 1568 int len = ch->data_len; 1569 union mcd_qchninfo q; 1570 daddr_t lba; 1571 int error; 1572 1573 if (sc->debug) 1574 printf("%s: subchan: af=%d df=%d\n", device_xname(&sc->sc_dev), 1575 ch->address_format, ch->data_format); 1576 1577 if (len > sizeof(*info) || len < sizeof(info->header)) 1578 return EINVAL; 1579 if (ch->address_format != CD_MSF_FORMAT && 1580 ch->address_format != CD_LBA_FORMAT) 1581 return EINVAL; 1582 if (ch->data_format != CD_CURRENT_POSITION && 1583 ch->data_format != CD_MEDIA_CATALOG) 1584 return EINVAL; 1585 1586 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0) 1587 return error; 1588 1589 info->header.audio_status = sc->audio_status; 1590 info->what.media_catalog.data_format = ch->data_format; 1591 1592 switch (ch->data_format) { 1593 case CD_MEDIA_CATALOG: 1594 info->what.media_catalog.mc_valid = 1; 1595 #if 0 1596 info->what.media_catalog.mc_number = 1597 #endif 1598 break; 1599 1600 case CD_CURRENT_POSITION: 1601 info->what.position.track_number = bcd2bin(q.current.trk_no); 1602 info->what.position.index_number = bcd2bin(q.current.idx_no); 1603 switch (ch->address_format) { 1604 case CD_MSF_FORMAT: 1605 info->what.position.reladdr.addr[0] = 0; 1606 info->what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]); 1607 info->what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]); 1608 info->what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]); 1609 info->what.position.absaddr.addr[0] = 0; 1610 info->what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]); 1611 info->what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]); 1612 info->what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]); 1613 break; 1614 case CD_LBA_FORMAT: 1615 lba = msf2hsg(q.current.relative_pos, 1); 1616 /* 1617 * Pre-gap has index number of 0, and decreasing MSF 1618 * address. Must be converted to negative LBA, per 1619 * SCSI spec. 1620 */ 1621 if (info->what.position.index_number == 0x00) 1622 lba = -lba; 1623 info->what.position.reladdr.addr[0] = lba >> 24; 1624 info->what.position.reladdr.addr[1] = lba >> 16; 1625 info->what.position.reladdr.addr[2] = lba >> 8; 1626 info->what.position.reladdr.addr[3] = lba; 1627 lba = msf2hsg(q.current.absolute_pos, 0); 1628 info->what.position.absaddr.addr[0] = lba >> 24; 1629 info->what.position.absaddr.addr[1] = lba >> 16; 1630 info->what.position.absaddr.addr[2] = lba >> 8; 1631 info->what.position.absaddr.addr[3] = lba; 1632 break; 1633 } 1634 break; 1635 } 1636 1637 return 0; 1638 } 1639 1640 int 1641 mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *p) 1642 { 1643 struct mcd_mbox mbx; 1644 int a = p->start_track; 1645 int z = p->end_track; 1646 int error; 1647 1648 if (sc->debug) 1649 printf("%s: playtracks: from %d:%d to %d:%d\n", 1650 device_xname(&sc->sc_dev), 1651 a, p->start_index, z, p->end_index); 1652 1653 if (a < bcd2bin(sc->volinfo.trk_low) || 1654 a > bcd2bin(sc->volinfo.trk_high) || 1655 a > z || 1656 z < bcd2bin(sc->volinfo.trk_low) || 1657 z > bcd2bin(sc->volinfo.trk_high)) 1658 return EINVAL; 1659 1660 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1661 return error; 1662 1663 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1664 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1665 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0]; 1666 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1]; 1667 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2]; 1668 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0]; 1669 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1]; 1670 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2]; 1671 sc->lastpb = mbx.cmd; 1672 mbx.res.length = 0; 1673 return mcd_send(sc, &mbx, 1); 1674 } 1675 1676 int 1677 mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p) 1678 { 1679 struct mcd_mbox mbx; 1680 int error; 1681 1682 if (sc->debug) 1683 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n", 1684 device_xname(&sc->sc_dev), 1685 p->start_m, p->start_s, p->start_f, 1686 p->end_m, p->end_s, p->end_f); 1687 1688 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >= 1689 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f)) 1690 return EINVAL; 1691 1692 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1693 return error; 1694 1695 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1696 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1697 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m); 1698 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s); 1699 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f); 1700 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m); 1701 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s); 1702 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f); 1703 sc->lastpb = mbx.cmd; 1704 mbx.res.length = 0; 1705 return mcd_send(sc, &mbx, 1); 1706 } 1707 1708 int 1709 mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p) 1710 { 1711 struct mcd_mbox mbx; 1712 int error; 1713 1714 if (sc->debug) 1715 printf("%s: playblocks: blkno %d length %d\n", 1716 device_xname(&sc->sc_dev), p->blk, p->len); 1717 1718 if (p->blk > sc->disksize || p->len > sc->disksize || 1719 (p->blk + p->len) > sc->disksize) 1720 return 0; 1721 1722 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1723 return error; 1724 1725 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1726 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1727 hsg2msf(p->blk, mbx.cmd.data.play.start_msf); 1728 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf); 1729 sc->lastpb = mbx.cmd; 1730 mbx.res.length = 0; 1731 return mcd_send(sc, &mbx, 1); 1732 } 1733 1734 int 1735 mcd_pause(struct mcd_softc *sc) 1736 { 1737 union mcd_qchninfo q; 1738 int error; 1739 1740 /* Verify current status. */ 1741 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) { 1742 printf("%s: pause: attempted when not playing\n", 1743 device_xname(&sc->sc_dev)); 1744 return EINVAL; 1745 } 1746 1747 /* Get the current position. */ 1748 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0) 1749 return error; 1750 1751 /* Copy it into lastpb. */ 1752 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0]; 1753 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1]; 1754 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2]; 1755 1756 /* Stop playing. */ 1757 if ((error = mcd_stop(sc)) != 0) 1758 return error; 1759 1760 /* Set the proper status and exit. */ 1761 sc->audio_status = CD_AS_PLAY_PAUSED; 1762 return 0; 1763 } 1764 1765 int 1766 mcd_resume(struct mcd_softc *sc) 1767 { 1768 struct mcd_mbox mbx; 1769 int error; 1770 1771 if (sc->audio_status != CD_AS_PLAY_PAUSED) 1772 return EINVAL; 1773 1774 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1775 return error; 1776 1777 mbx.cmd = sc->lastpb; 1778 mbx.res.length = 0; 1779 return mcd_send(sc, &mbx, 1); 1780 } 1781 1782 int 1783 mcd_eject(struct mcd_softc *sc) 1784 { 1785 struct mcd_mbox mbx; 1786 1787 mbx.cmd.opcode = MCD_CMDEJECTDISK; 1788 mbx.cmd.length = 0; 1789 mbx.res.length = 0; 1790 return mcd_send(sc, &mbx, 0); 1791 } 1792 1793 int 1794 mcd_setlock(struct mcd_softc *sc, int mode) 1795 { 1796 struct mcd_mbox mbx; 1797 1798 mbx.cmd.opcode = MCD_CMDSETLOCK; 1799 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode); 1800 mbx.cmd.data.lockmode.mode = mode; 1801 mbx.res.length = 0; 1802 return mcd_send(sc, &mbx, 1); 1803 } 1804