The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/scd/scd.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 9d6ec9673c391b794a9ea58c6b533ef9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.