scd.c revision 12502
12SN/A/*- 26SN/A * Copyright (c) 1995 Mikael Hybsch 32SN/A * All rights reserved. 42SN/A * 52SN/A * Portions of this file are copied from mcd.c 62SN/A * which has the following copyrights: 72SN/A * 82SN/A * Copyright 1993 by Holger Veit (data part) 92SN/A * Copyright 1993 by Brian Moore (audio part) 102SN/A * Changes Copyright 1993 by Gary Clark II 112SN/A * Changes Copyright (C) 1994 by Andrew A. Chernov 122SN/A * 132SN/A * Rewrote probe routine to work on newer Mitsumi drives. 142SN/A * Additional changes (C) 1994 by Jordan K. Hubbard 152SN/A * 162SN/A * All rights reserved. 172SN/A * 182SN/A * Redistribution and use in source and binary forms, with or without 192SN/A * modification, are permitted provided that the following conditions 202SN/A * are met: 212SN/A * 1. Redistributions of source code must retain the above copyright 222SN/A * notice, this list of conditions and the following disclaimer 232SN/A * in this position and unchanged. 242SN/A * 2. Redistributions in binary form must reproduce the above copyright 252SN/A * notice, this list of conditions and the following disclaimer in the 262SN/A * documentation and/or other materials provided with the distribution. 272SN/A * 3. The name of the author may not be used to endorse or promote products 282SN/A * derived from this software withough specific prior written permission 292SN/A * 302SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 312SN/A * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 322SN/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 332SN/A * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 342SN/A * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 352SN/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 362SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 372SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 382SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 392SN/A * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402SN/A * 412SN/A */ 422SN/A 432SN/A 442SN/A/* $Id: scd.c,v 1.9 1995/11/04 13:23:39 bde Exp $ */ 452SN/A 462SN/A/* Please send any comments to micke@dynas.se */ 472SN/A 482SN/A#define SCD_DEBUG 0 492SN/A 502SN/A#include "scd.h" 512SN/A#if NSCD > 0 522SN/A#include <sys/types.h> 532SN/A#include <sys/param.h> 542SN/A#include <sys/systm.h> 552SN/A#include <sys/conf.h> 562SN/A#include <sys/file.h> 572SN/A#include <sys/buf.h> 5810SN/A#include <sys/proc.h> 592SN/A#include <sys/stat.h> 602SN/A#include <sys/uio.h> 612SN/A#include <sys/ioctl.h> 622SN/A#include <sys/cdio.h> 632SN/A#include <sys/errno.h> 642SN/A#include <sys/dkbad.h> 652SN/A#include <sys/disklabel.h> 662SN/A#include <sys/devconf.h> 672SN/A 682SN/A#include <machine/clock.h> 692SN/A#include <machine/stdarg.h> 702SN/A 712SN/A#include <i386/isa/isa.h> 722SN/A#include <i386/isa/isa_device.h> 732SN/A#include <i386/isa/scdreg.h> 742SN/A 752SN/A#ifdef JREMOD 762SN/A#define CDEV_MAJOR 45 772SN/A#define BDEV_MAJOR 16 782SN/Astatic void scd_devsw_install(); 792SN/A#endif /*JREMOD */ 802SN/A 812SN/A#define scd_part(dev) ((minor(dev)) & 7) 82111SN/A#define scd_unit(dev) (((minor(dev)) & 0x38) >> 3) 832SN/A#define scd_phys(dev) (((minor(dev)) & 0x40) >> 6) 842SN/A#define RAW_PART 2 852SN/A 862SN/A/* flags */ 872SN/A#define SCDOPEN 0x0001 /* device opened */ 882SN/A#define SCDVALID 0x0002 /* parameters loaded */ 892SN/A#define SCDINIT 0x0004 /* device is init'd */ 902SN/A#define SCDPROBING 0x0020 /* probing */ 912SN/A#define SCDTOC 0x0100 /* already read toc */ 922SN/A#define SCDMBXBSY 0x0200 /* local mbx is busy */ 932SN/A#define SCDSPINNING 0x0400 /* drive is spun up */ 942SN/A 952SN/A#define SCD_S_BEGIN 0 962SN/A#define SCD_S_BEGIN1 1 972SN/A#define SCD_S_WAITSTAT 2 982SN/A#define SCD_S_WAITFIFO 3 992SN/A#define SCD_S_WAITSPIN 4 1002SN/A#define SCD_S_WAITREAD 5 1012SN/A#define SCD_S_WAITPARAM 6 1022SN/A 1032SN/A#define RDELAY_WAIT 300 1042SN/A#define RDELAY_WAITREAD 300 1052SN/A 1062SN/A#define SCDBLKSIZE 2048 1072SN/A 1082SN/A#ifdef SCD_DEBUG 1092SN/A static int scd_debuglevel = SCD_DEBUG; 1102SN/A# define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;} 1112SN/A#else 1122SN/A# define XDEBUG(level, data) 1132SN/A#endif 1142SN/A 1152SN/Astruct scd_mbx { 1161260Shannesw short unit; 1172SN/A short port; 1182SN/A short retry; 1192SN/A short nblk; 1202SN/A int sz; 1212SN/A u_long skip; 1222SN/A struct buf *bp; 1232SN/A int p_offset; 1242SN/A short count; 1252SN/A}; 1262SN/A 1272SN/Astatic struct scd_data { 1282SN/A int iobase; 1292SN/A char double_speed; 1302SN/A char *name; 1312SN/A short flags; 1322SN/A int blksize; 1332SN/A u_long disksize; 1342SN/A struct disklabel dlabel; 1352SN/A int openflag; 1362SN/A struct { 1372SN/A unsigned char adr :4; 1382SN/A unsigned char ctl :4; /* xcdplayer needs this */ 1392SN/A unsigned char start_msf[3]; 1402SN/A } toc[MAX_TRACKS]; 1412SN/A short first_track; 1422SN/A short last_track; 1432SN/A struct ioc_play_msf last_play; 1442SN/A 1452SN/A short audio_status; 1462SN/A struct buf head; /* head of buf queue */ 1472SN/A struct scd_mbx mbx; 1482SN/A} scd_data[NSCD]; 1492SN/A 1502SN/A/* prototypes */ 1512SN/Astatic int bcd2bin(bcd_t b); 1522SN/Astatic bcd_t bin2bcd(int b); 1532SN/Astatic void hsg2msf(int hsg, bcd_t *msf); 1542SN/Astatic int msf2hsg(bcd_t *msf); 1552SN/A 1562SN/Astatic void process_attention(unsigned unit); 1572SN/Astatic inline void write_control(unsigned port, unsigned data); 1582SN/Astatic int waitfor_status_bits(int unit, int bits_set, int bits_clear); 1592SN/Astatic int send_cmd(u_int unit, u_char cmd, u_int nargs, ...); 1602SN/Astatic void init_drive(unsigned unit); 1612SN/Astatic int spin_up(unsigned unit); 1622SN/Astatic int read_toc(dev_t dev); 1632SN/Astatic int get_result(u_int unit, int result_len, u_char *result); 1642SN/Astatic void print_error(int unit, int errcode); 1652SN/A 1662SN/Astatic void scd_start(int unit); 1672SN/Astatic void scd_doread(int state, struct scd_mbx *mbxin); 1682SN/A 1692SN/Astatic int scd_eject(int unit); 1702SN/Astatic int scd_stop(int unit); 1712SN/Astatic int scd_pause(int unit); 1722SN/Astatic int scd_resume(int unit); 1732SN/Astatic int scd_playtracks(int unit, struct ioc_play_track *pt); 1742SN/Astatic int scd_playmsf(int unit, struct ioc_play_msf *msf); 1752SN/Astatic int scd_play(int unit, struct ioc_play_msf *msf); 1762SN/Astatic int scd_subchan(int unit, struct ioc_read_subchannel *sc); 1772SN/Astatic int read_subcode(int unit, struct sony_subchannel_position_data *sc); 1782SN/A 1792SN/A/* for xcdplayer */ 1802SN/Astatic int scd_toc_header(int unit, struct ioc_toc_header *th); 1812SN/Astatic int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te); 1822SN/A#define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */ 1832SN/A 1842SN/Aextern int hz; 1852SN/A 1862SN/Astatic int scd_probe(struct isa_device *dev); 1872SN/Astatic int scd_attach(struct isa_device *dev); 1882SN/Astruct isa_driver scddriver = { scd_probe, scd_attach, "scd" }; 1892SN/A 1902SN/Astatic struct kern_devconf kdc_scd[NSCD] = { { 1912SN/A 0, 0, 0, /* filled in by dev_attach */ 1922SN/A "scd", 0, { MDDT_ISA, 0, "bio" }, 1932SN/A isa_generic_externalize, 0, 0, ISA_EXTERNALLEN, 1942SN/A &kdc_isa0, /* parent */ 1952SN/A 0, /* parentdata */ 1962SN/A DC_UNCONFIGURED, /* status */ 1972SN/A "Sony CD-ROM drive", /* properly filled later */ 1982SN/A DC_CLS_RDISK /* class */ 1992SN/A} }; 2002SN/A 2012SN/Astatic inline void 2022SN/Ascd_registerdev(struct isa_device *id) 2032SN/A{ 2042SN/A if(id->id_unit) 2052SN/A kdc_scd[id->id_unit] = kdc_scd[0]; 2062SN/A kdc_scd[id->id_unit].kdc_unit = id->id_unit; 2072SN/A kdc_scd[id->id_unit].kdc_isa = id; 2082SN/A dev_attach(&kdc_scd[id->id_unit]); 2092SN/A} 2102SN/A 2112SN/Aint scd_attach(struct isa_device *dev) 2122SN/A{ 2132SN/A struct scd_data *cd = scd_data + dev->id_unit; 2142SN/A 2152SN/A cd->iobase = dev->id_iobase; /* Already set by probe, but ... */ 2162SN/A 2172SN/A kdc_scd[dev->id_unit].kdc_state = DC_IDLE; 2182SN/A /* name filled in probe */ 2192SN/A kdc_scd[dev->id_unit].kdc_description = scd_data[dev->id_unit].name; 2202SN/A printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name); 2212SN/A 2222SN/A init_drive(dev->id_unit); 2232SN/A 2242SN/A cd->flags = SCDINIT; 2252SN/A cd->audio_status = CD_AS_AUDIO_INVALID; 2262SN/A 2272SN/A#ifdef JREMOD 2282SN/A scd_devsw_install(); 2292SN/A#endif /*JREMOD*/ 2302SN/A 2312SN/A return 1; 2322SN/A} 2332SN/A 2342SN/Aint 2352SN/Ascdopen(dev_t dev, int flags, int fmt, struct proc *p) 2362SN/A{ 237 int unit,part,phys; 238 int rc; 239 struct scd_data *cd; 240 241 unit = scd_unit(dev); 242 if (unit >= NSCD) 243 return ENXIO; 244 245 cd = scd_data + unit; 246 part = scd_part(dev); 247 phys = scd_phys(dev); 248 249 /* not initialized*/ 250 if (!(cd->flags & SCDINIT)) 251 return ENXIO; 252 253 /* invalidated in the meantime? mark all open part's invalid */ 254 if (cd->openflag) 255 return ENXIO; 256 257 XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS))); 258 259 if ((rc = spin_up(unit)) != 0) { 260 print_error(unit, rc); 261 return EIO; 262 } 263 if (!(cd->flags & SCDTOC)) { 264 int loop_count = 3; 265 266 while (loop_count-- > 0 && (rc = read_toc(dev)) != 0) { 267 if (rc == ERR_NOT_SPINNING) { 268 rc = spin_up(unit); 269 if (rc) { 270 print_error(unit, rc);\ 271 return EIO; 272 } 273 continue; 274 } 275 printf("scd%d: TOC read error 0x%x\n", unit, rc); 276 return EIO; 277 } 278 } 279 280 cd->openflag = 1; 281 cd->flags |= SCDVALID; 282 kdc_scd[unit].kdc_state = DC_BUSY; 283 284 return 0; 285} 286 287int 288scdclose(dev_t dev, int flags, int fmt, struct proc *p) 289{ 290 int unit,part,phys; 291 struct scd_data *cd; 292 293 unit = scd_unit(dev); 294 if (unit >= NSCD) 295 return ENXIO; 296 297 cd = scd_data + unit; 298 part = scd_part(dev); 299 phys = scd_phys(dev); 300 301 if (!(cd->flags & SCDINIT) || !cd->openflag) 302 return ENXIO; 303 304 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) { 305 (void)send_cmd(unit, CMD_SPIN_DOWN, 0); 306 cd->flags &= ~SCDSPINNING; 307 } 308 309 kdc_scd[unit].kdc_state = DC_IDLE; 310 311 /* close channel */ 312 cd->openflag = 0; 313 314 return 0; 315} 316 317void 318scdstrategy(struct buf *bp) 319{ 320 struct scd_data *cd; 321 struct buf *qp; 322 int s; 323 int unit = scd_unit(bp->b_dev); 324 325 cd = scd_data + unit; 326 327 XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n", unit, bp->b_blkno, bp->b_bcount)); 328 329 if (unit >= NSCD || bp->b_blkno < 0 || (bp->b_bcount % SCDBLKSIZE)) { 330 printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n", 331 unit, bp->b_blkno, bp->b_bcount); 332 bp->b_error = EINVAL; 333 bp->b_flags |= B_ERROR; 334 goto bad; 335 } 336 337 /* if device invalidated (e.g. media change, door open), error */ 338 if (!(cd->flags & SCDVALID)) { 339 printf("scd%d: media changed\n", unit); 340 bp->b_error = EIO; 341 goto bad; 342 } 343 344 /* read only */ 345 if (!(bp->b_flags & B_READ)) { 346 bp->b_error = EROFS; 347 goto bad; 348 } 349 350 /* no data to read */ 351 if (bp->b_bcount == 0) 352 goto done; 353 354 if (!(cd->flags & SCDTOC)) { 355 bp->b_error = EIO; 356 goto bad; 357 } 358 /* adjust transfer if necessary */ 359 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) 360 goto done; 361 362 bp->b_pblkno = bp->b_blkno; 363 bp->b_resid = 0; 364 365 /* queue it */ 366 qp = &cd->head; 367 s = splbio(); 368 disksort(qp,bp); 369 splx(s); 370 371 /* now check whether we can perform processing */ 372 scd_start(unit); 373 return; 374 375bad: 376 bp->b_flags |= B_ERROR; 377done: 378 bp->b_resid = bp->b_bcount; 379 biodone(bp); 380 return; 381} 382 383static void 384scd_start(int unit) 385{ 386 struct scd_data *cd = scd_data + unit; 387 struct buf *bp, *qp = &cd->head; 388 struct partition *p; 389 register s = splbio(); 390 391 if (cd->flags & SCDMBXBSY) { 392 splx(s); 393 return; 394 } 395 396 if ((bp = qp->b_actf) != 0) { 397 /* block found to process, dequeue */ 398 qp->b_actf = bp->b_actf; 399 cd->flags |= SCDMBXBSY; 400 splx(s); 401 } else { 402 /* nothing to do */ 403 splx(s); 404 return; 405 } 406 407 p = cd->dlabel.d_partitions + scd_part(bp->b_dev); 408 409 cd->mbx.unit = unit; 410 cd->mbx.port = cd->iobase; 411 cd->mbx.retry = 3; 412 cd->mbx.bp = bp; 413 cd->mbx.p_offset = p->p_offset; 414 splx(s); 415 416 scd_doread(SCD_S_BEGIN,&(cd->mbx)); 417 return; 418} 419 420int 421scdioctl(dev_t dev, int cmd, caddr_t addr, int flags, struct proc *p) 422{ 423 struct scd_data *cd; 424 int unit,part; 425 426 unit = scd_unit(dev); 427 part = scd_part(dev); 428 cd = scd_data + unit; 429 430 XDEBUG(1, ("scd%d: ioctl: cmd=0x%x\n", unit, cmd)); 431 432 if (!(cd->flags & SCDVALID)) 433 return EIO; 434 435 switch (cmd) { 436 case DIOCSBAD: 437 return EINVAL; 438 case DIOCGDINFO: 439 *(struct disklabel *)addr = cd->dlabel; 440 return 0; 441 case DIOCGPART: 442 ((struct partinfo *)addr)->disklab = &cd->dlabel; 443 ((struct partinfo *)addr)->part = 444 &cd->dlabel.d_partitions[0]; 445 return 0; 446 case CDIOCPLAYTRACKS: 447 return scd_playtracks(unit, (struct ioc_play_track *) addr); 448 case CDIOCPLAYBLOCKS: 449 return EINVAL; 450 case CDIOCPLAYMSF: 451 return scd_playmsf(unit, (struct ioc_play_msf *) addr); 452 case CDIOCREADSUBCHANNEL: 453 return scd_subchan(unit, (struct ioc_read_subchannel *) addr); 454 case CDIOREADTOCHEADER: 455 return scd_toc_header (unit, (struct ioc_toc_header *) addr); 456 case CDIOREADTOCENTRYS: 457 return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr); 458 case CDIOCSETPATCH: 459 case CDIOCGETVOL: 460 case CDIOCSETVOL: 461 case CDIOCSETMONO: 462 case CDIOCSETSTERIO: 463 case CDIOCSETMUTE: 464 case CDIOCSETLEFT: 465 case CDIOCSETRIGHT: 466 return EINVAL; 467 case CDIOCRESUME: 468 return scd_resume(unit); 469 case CDIOCPAUSE: 470 return scd_pause(unit); 471 case CDIOCSTART: 472 return EINVAL; 473 case CDIOCSTOP: 474 return scd_stop(unit); 475 case CDIOCEJECT: 476 return scd_eject(unit); 477 case CDIOCALLOW: 478 return 0; 479 case CDIOCSETDEBUG: 480#ifdef SCD_DEBUG 481 scd_debuglevel++; 482#endif 483 return 0; 484 case CDIOCCLRDEBUG: 485#ifdef SCD_DEBUG 486 scd_debuglevel = 0; 487 488#endif 489 return 0; 490 default: 491 printf("scd%d: unsupported ioctl (cmd=0x%x)\n", unit, cmd); 492 return ENOTTY; 493 } 494} 495 496int 497scdsize(dev_t dev) 498{ 499 return -1; 500} 501 502/*************************************************************** 503 * lower level of driver starts here 504 **************************************************************/ 505 506static int 507scd_playtracks(int unit, struct ioc_play_track *pt) 508{ 509 struct scd_data *cd = scd_data + unit; 510 struct ioc_play_msf msf; 511 int a = pt->start_track; 512 int z = pt->end_track; 513 int rc; 514 515 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { 516 if (rc == -ERR_NOT_SPINNING) { 517 if (spin_up(unit) != 0) 518 return EIO; 519 rc = read_toc(unit); 520 } 521 if (rc != 0) { 522 print_error(unit, rc); 523 return EIO; 524 } 525 } 526 527 XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit, 528 a, pt->start_index, z, pt->end_index)); 529 530 if ( a < cd->first_track 531 || a > cd->last_track 532 || a > z 533 || z > cd->last_track) 534 return EINVAL; 535 536 bcopy(cd->toc[a].start_msf, &msf.start_m, 3); 537 hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m); 538 539 return scd_play(unit, &msf); 540} 541 542/* The start/end msf is expected to be in bin format */ 543static int 544scd_playmsf(int unit, struct ioc_play_msf *msfin) 545{ 546 struct ioc_play_msf msf; 547 548 msf.start_m = bin2bcd(msfin->start_m); 549 msf.start_s = bin2bcd(msfin->start_s); 550 msf.start_f = bin2bcd(msfin->start_f); 551 msf.end_m = bin2bcd(msfin->end_m); 552 msf.end_s = bin2bcd(msfin->end_s); 553 msf.end_f = bin2bcd(msfin->end_f); 554 555 return scd_play(unit, &msf); 556} 557 558/* The start/end msf is expected to be in bcd format */ 559static int 560scd_play(int unit, struct ioc_play_msf *msf) 561{ 562 struct scd_data *cd = scd_data + unit; 563 int i, rc; 564 565 XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit, 566 msf->start_m, msf->start_s, msf->start_f, 567 msf->end_m, msf->end_s, msf->end_f)); 568 569 for (i = 0; i < 2; i++) { 570 rc = send_cmd(unit, CMD_PLAY_AUDIO, 7, 571 0x03, 572 msf->start_m, msf->start_s, msf->start_f, 573 msf->end_m, msf->end_s, msf->end_f); 574 if (rc == -ERR_NOT_SPINNING) { 575 cd->flags &= ~SCDSPINNING; 576 if (spin_up(unit) != 0) 577 return EIO; 578 } else if (rc < 0) { 579 print_error(unit, rc); 580 return EIO; 581 } else { 582 break; 583 } 584 } 585 cd->audio_status = CD_AS_PLAY_IN_PROGRESS; 586 bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf)); 587 return 0; 588} 589 590static int 591scd_stop(int unit) 592{ 593 struct scd_data *cd = scd_data + unit; 594 595 (void)send_cmd(unit, CMD_STOP_AUDIO, 0); 596 cd->audio_status = CD_AS_PLAY_COMPLETED; 597 return 0; 598} 599 600static int 601scd_pause(int unit) 602{ 603 struct scd_data *cd = scd_data + unit; 604 struct sony_subchannel_position_data subpos; 605 606 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) 607 return EINVAL; 608 609 if (read_subcode(unit, &subpos) != 0) 610 return EIO; 611 612 if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0) 613 return EIO; 614 615 cd->last_play.start_m = subpos.abs_msf[0]; 616 cd->last_play.start_s = subpos.abs_msf[1]; 617 cd->last_play.start_f = subpos.abs_msf[2]; 618 cd->audio_status = CD_AS_PLAY_PAUSED; 619 620 XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit, 621 cd->last_play.start_m, 622 cd->last_play.start_s, 623 cd->last_play.start_f)); 624 625 return 0; 626} 627 628static int 629scd_resume(int unit) 630{ 631 if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED) 632 return EINVAL; 633 return scd_play(unit, &scd_data[unit].last_play); 634} 635 636static int 637scd_eject(int unit) 638{ 639 struct scd_data *cd = scd_data + unit; 640 641 cd->audio_status = CD_AS_AUDIO_INVALID; 642 cd->flags &= ~(SCDSPINNING|SCDTOC); 643 644 if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 || 645 send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 || 646 send_cmd(unit, CMD_EJECT, 0) != 0) 647 { 648 return EIO; 649 } 650 return 0; 651} 652 653static int 654scd_subchan(int unit, struct ioc_read_subchannel *sc) 655{ 656 struct scd_data *cd = scd_data + unit; 657 struct sony_subchannel_position_data q; 658 struct cd_sub_channel_info data; 659 660 XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit, 661 sc->address_format, 662 sc->data_format)); 663 664 if (sc->address_format != CD_MSF_FORMAT) 665 return EINVAL; 666 667 if (sc->data_format != CD_CURRENT_POSITION) 668 return EINVAL; 669 670 if (read_subcode(unit, &q) != 0) 671 return EIO; 672 673 data.header.audio_status = cd->audio_status; 674 data.what.position.data_format = CD_MSF_FORMAT; 675 data.what.position.track_number = bcd2bin(q.track_number); 676 data.what.position.reladdr.msf.unused = 0; 677 data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]); 678 data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]); 679 data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]); 680 data.what.position.absaddr.msf.unused = 0; 681 data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]); 682 data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]); 683 data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]); 684 685 if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0) 686 return EFAULT; 687 return 0; 688} 689 690int 691scd_probe(struct isa_device *dev) 692{ 693 struct sony_drive_configuration drive_config; 694 int unit = dev->id_unit; 695 int rc; 696 static char namebuf[8+16+8+3]; 697 char *s = namebuf; 698 int loop_count = 0; 699 700 scd_data[unit].flags = SCDPROBING; 701 scd_data[unit].iobase = dev->id_iobase; 702 703 bzero(&drive_config, sizeof(drive_config)); 704 705 scd_registerdev(dev); 706 707again: 708 /* Reset drive */ 709 write_control(dev->id_iobase, CBIT_RESET_DRIVE); 710 711 /* Calm down */ 712 DELAY(300000); 713 714 /* Only the ATTENTION bit may be set */ 715 if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) { 716 XDEBUG(1, ("scd: too many bits set. probe failed.\n")); 717 return 0; 718 } 719 rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0); 720 if (rc != sizeof(drive_config)) { 721 /* Sometimes if the drive is playing audio I get */ 722 /* the bad result 82. Fix by repeating the reset */ 723 if (rc > 0 && loop_count++ == 0) 724 goto again; 725 return 0; 726 } 727 if (get_result(unit, rc, (u_char *)&drive_config) != 0) 728 return 0; 729 730 bcopy(drive_config.vendor, namebuf, 8); 731 s = namebuf+8; 732 while (*(s-1) == ' ') /* Strip trailing spaces */ 733 s--; 734 *s++ = ' '; 735 bcopy(drive_config.product, s, 16); 736 s += 16; 737 while (*(s-1) == ' ') 738 s--; 739 *s++ = ' '; 740 bcopy(drive_config.revision, s, 8); 741 s += 8; 742 while (*(s-1) == ' ') 743 s--; 744 *s = 0; 745 746 scd_data[unit].name = namebuf; 747 748 if (drive_config.config & 0x10) 749 scd_data[unit].double_speed = 1; 750 else 751 scd_data[unit].double_speed = 0; 752 753 return 4; 754} 755 756static int 757read_subcode(int unit, struct sony_subchannel_position_data *sc) 758{ 759 int rc; 760 761 rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0); 762 if (rc < 0 || rc < sizeof(*sc)) 763 return EIO; 764 if (get_result(unit, rc, (u_char *)sc) != 0) 765 return EIO; 766 return 0; 767} 768 769/* State machine copied from mcd.c */ 770 771/* This (and the code in mcd.c) will not work with more than one drive */ 772/* because there is only one mbxsave below. Should fix that some day. */ 773/* (mbxsave & state should probably be included in the scd_data struct and */ 774/* the unit number used as first argument to scd_doread().) /Micke */ 775 776/* state machine to process read requests 777 * initialize with SCD_S_BEGIN: reset state machine 778 * SCD_S_WAITSTAT: wait for ready (!busy) 779 * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning) 780 * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command. 781 * SCD_S_WAITREAD: wait for data ready, read data 782 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read. 783 */ 784 785static struct scd_mbx *mbxsave; 786 787static void 788scd_doread(int state, struct scd_mbx *mbxin) 789{ 790 struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin; 791 int unit = mbx->unit; 792 int port = mbx->port; 793 struct buf *bp = mbx->bp; 794 struct scd_data *cd = scd_data + unit; 795 int reg,i; 796 int blknum; 797 caddr_t addr; 798 static char sdata[3]; /* Must be preserved between calls to this function */ 799 800loop: 801 switch (state) { 802 case SCD_S_BEGIN: 803 mbx = mbxsave = mbxin; 804 805 case SCD_S_BEGIN1: 806 /* get status */ 807 mbx->count = RDELAY_WAIT; 808 809 process_attention(unit); 810 goto trystat; 811 812 case SCD_S_WAITSTAT: 813 untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITSTAT); 814 if (mbx->count-- <= 0) { 815 printf("scd%d: timeout. drive busy.\n",unit); 816 goto harderr; 817 } 818 819trystat: 820 if (IS_BUSY(port)) { 821 timeout((timeout_func_t)scd_doread, 822 (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */ 823 return; 824 } 825 826 process_attention(unit); 827 828 /* reject, if audio active */ 829 if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) { 830 printf("scd%d: audio is active\n",unit); 831 goto harderr; 832 } 833 834 mbx->sz = cd->blksize; 835 836 /* for first block */ 837 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz; 838 mbx->skip = 0; 839 840nextblock: 841 if (!(cd->flags & SCDVALID)) 842 goto changed; 843 844 blknum = (bp->b_blkno / (mbx->sz/DEV_BSIZE)) 845 + mbx->p_offset + mbx->skip/mbx->sz; 846 847 XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum)); 848 849 /* build parameter block */ 850 hsg2msf(blknum, sdata); 851 852 write_control(port, CBIT_RESULT_READY_CLEAR); 853 write_control(port, CBIT_RPARAM_CLEAR); 854 write_control(port, CBIT_DATA_READY_CLEAR); 855 856 if (FSTATUS_BIT(port, FBIT_WPARAM_READY)) 857 goto writeparam; 858 859 mbx->count = 100; 860 timeout((timeout_func_t)scd_doread, 861 (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */ 862 return; 863 864 case SCD_S_WAITSPIN: 865 untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITSPIN); 866 if (mbx->count-- <= 0) { 867 printf("scd%d: timeout waiting for drive to spin up.\n", unit); 868 goto harderr; 869 } 870 if (!STATUS_BIT(port, SBIT_RESULT_READY)) { 871 timeout((timeout_func_t)scd_doread, 872 (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */ 873 return; 874 } 875 write_control(port, CBIT_RESULT_READY_CLEAR); 876 switch ((i = inb(port+IREG_RESULT)) & 0xf0) { 877 case 0x20: 878 i = inb(port+IREG_RESULT); 879 print_error(unit, i); 880 goto harderr; 881 case 0x00: 882 (void)inb(port+IREG_RESULT); 883 cd->flags |= SCDSPINNING; 884 break; 885 } 886 XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit)); 887 888 state = SCD_S_BEGIN1; 889 goto loop; 890 891 case SCD_S_WAITFIFO: 892 untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITFIFO); 893 if (mbx->count-- <= 0) { 894 printf("scd%d: timeout. write param not ready.\n",unit); 895 goto harderr; 896 } 897 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) { 898 timeout((timeout_func_t)scd_doread, 899 (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */ 900 return; 901 } 902 XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100)); 903 904writeparam: 905 /* The reason this test isn't done 'till now is to make sure */ 906 /* that it is ok to send the SPIN_UP cmd below. */ 907 if (!(cd->flags & SCDSPINNING)) { 908 XDEBUG(1, ("scd%d: spinning up drive ...\n", unit)); 909 outb(port+OREG_COMMAND, CMD_SPIN_UP); 910 mbx->count = 300; 911 timeout((timeout_func_t)scd_doread, 912 (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */ 913 return; 914 } 915 916 reg = port + OREG_WPARAMS; 917 /* send the read command */ 918 disable_intr(); 919 outb(reg, sdata[0]); 920 outb(reg, sdata[1]); 921 outb(reg, sdata[2]); 922 outb(reg, 0); 923 outb(reg, 0); 924 outb(reg, 1); 925 outb(port+OREG_COMMAND, CMD_READ); 926 enable_intr(); 927 928 mbx->count = RDELAY_WAITREAD; 929 for (i = 0; i < 50; i++) { 930 if (STATUS_BIT(port, SBIT_DATA_READY)) 931 goto got_data; 932 DELAY(100); 933 } 934 935 timeout((timeout_func_t)scd_doread, 936 (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */ 937 return; 938 939 case SCD_S_WAITREAD: 940 untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITREAD); 941 if (mbx->count-- <= 0) { 942 if (STATUS_BIT(port, SBIT_RESULT_READY)) 943 goto got_param; 944 printf("scd%d: timeout while reading data\n",unit); 945 goto readerr; 946 } 947 if (!STATUS_BIT(port, SBIT_DATA_READY)) { 948 process_attention(unit); 949 if (!(cd->flags & SCDVALID)) 950 goto changed; 951 timeout((timeout_func_t)scd_doread, 952 (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */ 953 return; 954 } 955 XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD)); 956 957got_data: 958 /* data is ready */ 959 addr = bp->b_un.b_addr + mbx->skip; 960 write_control(port, CBIT_DATA_READY_CLEAR); 961 insb(port+IREG_DATA, addr, mbx->sz); 962 963 mbx->count = 100; 964 for (i = 0; i < 20; i++) { 965 if (STATUS_BIT(port, SBIT_RESULT_READY)) 966 goto waitfor_param; 967 DELAY(100); 968 } 969 goto waitfor_param; 970 971 case SCD_S_WAITPARAM: 972 untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITPARAM); 973 if (mbx->count-- <= 0) { 974 printf("scd%d: timeout waiting for params\n",unit); 975 goto readerr; 976 } 977 978waitfor_param: 979 if (!STATUS_BIT(port, SBIT_RESULT_READY)) { 980 timeout((timeout_func_t)scd_doread, 981 (caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */ 982 return; 983 } 984#if SCD_DEBUG 985 if (mbx->count < 100 && scd_debuglevel > 0) 986 printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100); 987#endif 988 989got_param: 990 write_control(port, CBIT_RESULT_READY_CLEAR); 991 switch ((i = inb(port+IREG_RESULT)) & 0xf0) { 992 case 0x50: 993 switch (i) { 994 case ERR_FATAL_READ_ERROR1: 995 case ERR_FATAL_READ_ERROR2: 996 printf("scd%d: unrecoverable read error 0x%x\n", unit, i); 997 goto harderr; 998 } 999 break; 1000 case 0x20: 1001 i = inb(port+IREG_RESULT); 1002 switch (i) { 1003 case ERR_NOT_SPINNING: 1004 XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit)); 1005 if (mbx->retry-- > 0) { 1006 state = SCD_S_BEGIN1; 1007 cd->flags &= ~SCDSPINNING; 1008 goto loop; 1009 } 1010 goto harderr; 1011 default: 1012 print_error(unit, i); 1013 goto readerr; 1014 } 1015 case 0x00: 1016 i = inb(port+IREG_RESULT); 1017 break; 1018 } 1019 1020 if (--mbx->nblk > 0) { 1021 mbx->skip += mbx->sz; 1022 goto nextblock; 1023 } 1024 1025 /* return buffer */ 1026 bp->b_resid = 0; 1027 biodone(bp); 1028 1029 cd->flags &= ~SCDMBXBSY; 1030 scd_start(mbx->unit); 1031 return; 1032 } 1033 1034readerr: 1035 if (mbx->retry-- > 0) { 1036 printf("scd%d: retrying ...\n",unit); 1037 state = SCD_S_BEGIN1; 1038 goto loop; 1039 } 1040harderr: 1041 /* invalidate the buffer */ 1042 bp->b_error = EIO; 1043 bp->b_flags |= B_ERROR; 1044 bp->b_resid = bp->b_bcount; 1045 biodone(bp); 1046 1047 cd->flags &= ~SCDMBXBSY; 1048 scd_start(mbx->unit); 1049 return; 1050 1051changed: 1052 printf("scd%d: media changed\n", unit); 1053 goto harderr; 1054} 1055 1056static int 1057bcd2bin(bcd_t b) 1058{ 1059 return (b >> 4) * 10 + (b & 15); 1060} 1061 1062static bcd_t 1063bin2bcd(int b) 1064{ 1065 return ((b / 10) << 4) | (b % 10); 1066} 1067 1068static void 1069hsg2msf(int hsg, bcd_t *msf) 1070{ 1071 hsg += 150; 1072 M_msf(msf) = bin2bcd(hsg / 4500); 1073 hsg %= 4500; 1074 S_msf(msf) = bin2bcd(hsg / 75); 1075 F_msf(msf) = bin2bcd(hsg % 75); 1076} 1077 1078static int 1079msf2hsg(bcd_t *msf) 1080{ 1081 return (bcd2bin(M_msf(msf)) * 60 + 1082 bcd2bin(S_msf(msf))) * 75 + 1083 bcd2bin(F_msf(msf)) - 150; 1084} 1085 1086static void 1087process_attention(unsigned unit) 1088{ 1089 unsigned port = scd_data[unit].iobase; 1090 unsigned char code; 1091 int count = 0; 1092 1093 while (IS_ATTENTION(port) && count++ < 30) { 1094 write_control(port, CBIT_ATTENTION_CLEAR); 1095 code = inb(port+IREG_RESULT); 1096 1097#if SCD_DEBUG 1098 if (scd_debuglevel > 0) { 1099 if (count == 1) 1100 printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code); 1101 else 1102 printf(",0x%x", code); 1103 } 1104#endif 1105 1106 switch (code) { 1107 case ATTEN_SPIN_DOWN: 1108 scd_data[unit].flags &= ~SCDSPINNING; 1109 break; 1110 1111 case ATTEN_SPIN_UP_DONE: 1112 scd_data[unit].flags |= SCDSPINNING; 1113 break; 1114 1115 case ATTEN_AUDIO_DONE: 1116 scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED; 1117 break; 1118 1119 case ATTEN_DRIVE_LOADED: 1120 scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID); 1121 scd_data[unit].audio_status = CD_AS_AUDIO_INVALID; 1122 break; 1123 1124 case ATTEN_EJECT_PUSHED: 1125 scd_data[unit].flags &= ~SCDVALID; 1126 break; 1127 } 1128 DELAY(100); 1129 } 1130#if SCD_DEBUG 1131 if (scd_debuglevel > 0 && count > 0) 1132 printf("\n"); 1133#endif 1134} 1135 1136/* Returns 0 OR sony error code */ 1137static int 1138spin_up(unsigned unit) 1139{ 1140 unsigned char res_reg[12]; 1141 unsigned int res_size; 1142 int rc; 1143 int loop_count = 0; 1144 1145again: 1146 rc = send_cmd(unit, CMD_SPIN_UP, NULL, 0, res_reg, &res_size); 1147 if (rc != 0) { 1148 XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc)); 1149 return rc; 1150 } 1151 1152 if (!(scd_data[unit].flags & SCDTOC)) { 1153 rc = send_cmd(unit, CMD_READ_TOC, 0); 1154 if (rc == ERR_NOT_SPINNING) { 1155 if (loop_count++ < 3) 1156 goto again; 1157 return rc; 1158 } 1159 if (rc != 0) 1160 return rc; 1161 } 1162 1163 scd_data[unit].flags |= SCDSPINNING; 1164 1165 return 0; 1166} 1167 1168static struct sony_tracklist * 1169get_tl(struct sony_toc *toc, int size) 1170{ 1171 struct sony_tracklist *tl = &toc->tracks[0]; 1172 1173 if (tl->track != 0xb0) 1174 return tl; 1175 (char *)tl += 9; 1176 if (tl->track != 0xb1) 1177 return tl; 1178 (char *)tl += 9; 1179 if (tl->track != 0xb2) 1180 return tl; 1181 (char *)tl += 9; 1182 if (tl->track != 0xb3) 1183 return tl; 1184 (char *)tl += 9; 1185 if (tl->track != 0xb4) 1186 return tl; 1187 (char *)tl += 9; 1188 if (tl->track != 0xc0) 1189 return tl; 1190 (char *)tl += 9; 1191 return tl; 1192} 1193 1194static int 1195read_toc(dev_t dev) 1196{ 1197 unsigned unit; 1198 struct scd_data *cd; 1199 unsigned part = 0; /* For now ... */ 1200 struct sony_toc toc; 1201 struct sony_tracklist *tl; 1202 int rc, i, j; 1203 u_long first, last; 1204 1205 unit = scd_unit(dev); 1206 cd = scd_data + unit; 1207 1208 rc = send_cmd(unit, CMD_GET_TOC, 1, part+1); 1209 if (rc < 0) 1210 return rc; 1211 if (rc > sizeof(toc)) { 1212 printf("scd%d: program error: toc too large (%d)\n", unit, rc); 1213 return EIO; 1214 } 1215 if (get_result(unit, rc, (u_char *)&toc) != 0) 1216 return EIO; 1217 1218 XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc))); 1219 1220 tl = get_tl(&toc, rc); 1221 first = msf2hsg(tl->start_msf); 1222 last = msf2hsg(toc.lead_out_start_msf); 1223 cd->blksize = SCDBLKSIZE; 1224 cd->disksize = last*cd->blksize/DEV_BSIZE; 1225 1226 XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit, 1227 first, last)); 1228 1229 cd->first_track = bcd2bin(toc.first_track); 1230 cd->last_track = bcd2bin(toc.last_track); 1231 if (cd->last_track > (MAX_TRACKS-2)) 1232 cd->last_track = MAX_TRACKS-2; 1233 for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) { 1234 cd->toc[i].adr = tl[j].adr; 1235 cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */ 1236 bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3); 1237#ifdef SCD_DEBUG 1238 if (scd_debuglevel > 0) { 1239 if ((j % 3) == 0) 1240 printf("\nscd%d: tracks ", unit); 1241 printf("[%03d: %2d %2d %2d] ", i, 1242 bcd2bin(cd->toc[i].start_msf[0]), 1243 bcd2bin(cd->toc[i].start_msf[1]), 1244 bcd2bin(cd->toc[i].start_msf[2])); 1245 } 1246#endif 1247 } 1248 bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3); 1249#ifdef SCD_DEBUG 1250 if (scd_debuglevel > 0) { 1251 i = cd->last_track+1; 1252 printf("[END: %2d %2d %2d]\n", 1253 bcd2bin(cd->toc[i].start_msf[0]), 1254 bcd2bin(cd->toc[i].start_msf[1]), 1255 bcd2bin(cd->toc[i].start_msf[2])); 1256 } 1257#endif 1258 1259 bzero(&cd->dlabel,sizeof(struct disklabel)); 1260 /* filled with spaces first */ 1261 strncpy(cd->dlabel.d_typename," ", 1262 sizeof(cd->dlabel.d_typename)); 1263 strncpy(cd->dlabel.d_typename, cd->name, 1264 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1)); 1265 strncpy(cd->dlabel.d_packname,"unknown ", 1266 sizeof(cd->dlabel.d_packname)); 1267 cd->dlabel.d_secsize = cd->blksize; 1268 cd->dlabel.d_nsectors = 100; 1269 cd->dlabel.d_ntracks = 1; 1270 cd->dlabel.d_ncylinders = (cd->disksize/100)+1; 1271 cd->dlabel.d_secpercyl = 100; 1272 cd->dlabel.d_secperunit = cd->disksize; 1273 cd->dlabel.d_rpm = 300; 1274 cd->dlabel.d_interleave = 1; 1275 cd->dlabel.d_flags = D_REMOVABLE; 1276 cd->dlabel.d_npartitions= 1; 1277 cd->dlabel.d_partitions[0].p_offset = 0; 1278 cd->dlabel.d_partitions[0].p_size = cd->disksize; 1279 cd->dlabel.d_partitions[0].p_fstype = 9; 1280 cd->dlabel.d_magic = DISKMAGIC; 1281 cd->dlabel.d_magic2 = DISKMAGIC; 1282 cd->dlabel.d_checksum = dkcksum(&cd->dlabel); 1283 1284 cd->flags |= SCDTOC; 1285 1286 return 0; 1287} 1288 1289static inline void 1290write_control(unsigned port, unsigned data) 1291{ 1292 outb(port + OREG_CONTROL, data); 1293} 1294 1295static void 1296init_drive(unsigned unit) 1297{ 1298 int rc; 1299 1300 rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2, 1301 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0)); 1302 if (rc != 0) 1303 printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc); 1304} 1305 1306/* Returns 0 or errno */ 1307static int 1308get_result(u_int unit, int result_len, u_char *result) 1309{ 1310 unsigned int port = scd_data[unit].iobase; 1311 unsigned int res_reg = port + IREG_RESULT; 1312 int loop_index = 2; /* send_cmd() reads two bytes ... */ 1313 1314 XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len)); 1315 1316 while (result_len-- > 0) { 1317 if (loop_index++ >= 10) { 1318 loop_index = 1; 1319 if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0)) 1320 return EIO; 1321 write_control(port, CBIT_RESULT_READY_CLEAR); 1322 } 1323 if (result) 1324 *result++ = inb(res_reg); 1325 else 1326 (void)inb(res_reg); 1327 } 1328 return 0; 1329} 1330 1331/* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */ 1332static int 1333send_cmd(u_int unit, u_char cmd, u_int nargs, ...) 1334{ 1335 va_list ap; 1336 u_int port = scd_data[unit].iobase; 1337 u_int reg; 1338 u_char c; 1339 int rc; 1340 int i; 1341 1342 if (waitfor_status_bits(unit, 0, SBIT_BUSY)) { 1343 printf("scd%d: drive busy\n", unit); 1344 return -0x100; 1345 } 1346 1347 XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs)); 1348 1349 write_control(port, CBIT_RESULT_READY_CLEAR); 1350 write_control(port, CBIT_RPARAM_CLEAR); 1351 1352 for (i = 0; i < 100; i++) 1353 if (FSTATUS_BIT(port, FBIT_WPARAM_READY)) 1354 break; 1355 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) { 1356 XDEBUG(1, ("\nscd%d: wparam timeout\n", unit)); 1357 return -EIO; 1358 } 1359 1360 va_start(ap, nargs); 1361 reg = port + OREG_WPARAMS; 1362 for (i = 0; i < nargs; i++) { 1363 c = (u_char)va_arg(ap, int); 1364 outb(reg, c); 1365 XDEBUG(1, (",{0x%x}", c)); 1366 } 1367 va_end(ap); 1368 XDEBUG(1, ("\n")); 1369 1370 outb(port+OREG_COMMAND, cmd); 1371 1372 rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY); 1373 if (rc) 1374 return -0x100; 1375 1376 reg = port + IREG_RESULT; 1377 write_control(port, CBIT_RESULT_READY_CLEAR); 1378 switch ((rc = inb(reg)) & 0xf0) { 1379 case 0x20: 1380 rc = inb(reg); 1381 /* FALL TROUGH */ 1382 case 0x50: 1383 XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc)); 1384 return -rc; 1385 case 0x00: 1386 default: 1387 rc = inb(reg); 1388 XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc)); 1389 return rc; 1390 } 1391} 1392 1393static void 1394print_error(int unit, int errcode) 1395{ 1396 switch (errcode) { 1397 case -ERR_CD_NOT_LOADED: 1398 printf("scd%d: door is open\n", unit); 1399 break; 1400 case -ERR_NO_CD_INSIDE: 1401 printf("scd%d: no cd inside\n", unit); 1402 break; 1403 default: 1404 if (errcode == -0x100 || errcode > 0) 1405 printf("scd%d: device timeout\n", unit); 1406 else 1407 printf("scd%d: unexpected error 0x%x\n", unit, -errcode); 1408 break; 1409 } 1410} 1411 1412/* Returns 0 or errno value */ 1413static int 1414waitfor_status_bits(int unit, int bits_set, int bits_clear) 1415{ 1416 u_int port = scd_data[unit].iobase; 1417 u_int flags = scd_data[unit].flags; 1418 u_int reg = port + IREG_STATUS; 1419 u_int max_loop; 1420 u_char c = 0; 1421 1422 if (flags & SCDPROBING) { 1423 max_loop = 0; 1424 while (max_loop++ < 1000) { 1425 c = inb(reg); 1426 if (c == 0xff) 1427 return EIO; 1428 if (c & SBIT_ATTENTION) { 1429 process_attention(unit); 1430 continue; 1431 } 1432 if ((c & bits_set) == bits_set && 1433 (c & bits_clear) == 0) 1434 { 1435 break; 1436 } 1437 DELAY(10000); 1438 } 1439 } else { 1440 max_loop = 100; 1441 while (max_loop-- > 0) { 1442 c = inb(reg); 1443 if (c & SBIT_ATTENTION) { 1444 process_attention(unit); 1445 continue; 1446 } 1447 if ((c & bits_set) == bits_set && 1448 (c & bits_clear) == 0) 1449 { 1450 break; 1451 } 1452 tsleep(waitfor_status_bits, PZERO - 1, "waitfor", hz/10); 1453 } 1454 } 1455 if ((c & bits_set) == bits_set && 1456 (c & bits_clear) == 0) 1457 { 1458 return 0; 1459 } 1460#ifdef SCD_DEBUG 1461 if (scd_debuglevel > 0) 1462 printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear); 1463 else 1464#endif 1465 printf("scd%d: timeout.\n", unit); 1466 return EIO; 1467} 1468 1469/* these two routines for xcdplayer - "borrowed" from mcd.c */ 1470static int 1471scd_toc_header (int unit, struct ioc_toc_header* th) 1472{ 1473 struct scd_data *cd = scd_data + unit; 1474 int rc; 1475 1476 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { 1477 print_error(unit, rc); 1478 return EIO; 1479 } 1480 1481 th->starting_track = cd->first_track; 1482 th->ending_track = cd->last_track; 1483 th->len = 0; /* not used */ 1484 1485 return 0; 1486} 1487 1488static int 1489scd_toc_entrys (int unit, struct ioc_read_toc_entry *te) 1490{ 1491 struct scd_data *cd = scd_data + unit; 1492 struct cd_toc_entry toc_entry; 1493 int rc, i, len = te->data_len; 1494 1495 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { 1496 print_error(unit, rc); 1497 return EIO; 1498 } 1499 1500 /* find the toc to copy*/ 1501 i = te->starting_track; 1502 if (i == SCD_LASTPLUS1) 1503 i = cd->last_track + 1; 1504 1505 /* verify starting track */ 1506 if (i < cd->first_track || i > cd->last_track+1) 1507 return EINVAL; 1508 1509 /* valid length ? */ 1510 if (len < sizeof(struct cd_toc_entry) 1511 || (len % sizeof(struct cd_toc_entry)) != 0) 1512 return EINVAL; 1513 1514 /* copy the toc data */ 1515 toc_entry.control = cd->toc[i].ctl; 1516 toc_entry.addr_type = te->address_format; 1517 toc_entry.track = i; 1518 if (te->address_format == CD_MSF_FORMAT) { 1519 toc_entry.addr.msf.unused = 0; 1520 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]); 1521 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]); 1522 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]); 1523 } 1524 1525 /* copy the data back */ 1526 if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0) 1527 return EFAULT; 1528 1529 return 0; 1530} 1531 1532#ifdef JREMOD 1533struct bdevsw scd_bdevsw = 1534 { scdopen, scdclose, scdstrategy, scdioctl, /*16*/ 1535 nxdump, scdsize, 0 }; 1536 1537struct cdevsw scd_cdevsw = 1538 { scdopen, scdclose, rawread, nowrite, /*45*/ 1539 scdioctl, nostop, nullreset, nodevtotty,/* sony cd */ 1540 seltrue, nommap, scdstrategy }; 1541 1542static scd_devsw_installed = 0; 1543 1544static void scd_devsw_install() 1545{ 1546 dev_t descript; 1547 if( ! scd_devsw_installed ) { 1548 descript = makedev(CDEV_MAJOR,0); 1549 cdevsw_add(&descript,&scd_cdevsw,NULL); 1550#if defined(BDEV_MAJOR) 1551 descript = makedev(BDEV_MAJOR,0); 1552 bdevsw_add(&descript,&scd_bdevsw,NULL); 1553#endif /*BDEV_MAJOR*/ 1554 scd_devsw_installed = 1; 1555 } 1556} 1557#endif /* JREMOD */ 1558 1559#endif /* NSCD > 0 */ 1560