Home | History | Annotate | Line # | Download | only in scsipi
ss.c revision 1.16.2.3
      1  1.16.2.3  thorpej /*	$NetBSD: ss.c,v 1.16.2.3 1997/10/14 10:25:33 thorpej Exp $	*/
      2  1.16.2.2  thorpej 
      3  1.16.2.2  thorpej /*
      4  1.16.2.2  thorpej  * Copyright (c) 1995 Kenneth Stailey.  All rights reserved.
      5  1.16.2.2  thorpej  *   modified for configurable scanner support by Joachim Koenig
      6  1.16.2.2  thorpej  *
      7  1.16.2.2  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.16.2.2  thorpej  * modification, are permitted provided that the following conditions
      9  1.16.2.2  thorpej  * are met:
     10  1.16.2.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.16.2.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.16.2.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.16.2.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.16.2.2  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.16.2.2  thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.16.2.2  thorpej  *    must display the following acknowledgement:
     17  1.16.2.2  thorpej  *	This product includes software developed by Kenneth Stailey.
     18  1.16.2.2  thorpej  * 4. The name of the author may not be used to endorse or promote products
     19  1.16.2.2  thorpej  *    derived from this software without specific prior written permission.
     20  1.16.2.2  thorpej  *
     21  1.16.2.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.16.2.2  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.16.2.2  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.16.2.2  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.16.2.2  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.16.2.2  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.16.2.2  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.16.2.2  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.16.2.2  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.16.2.2  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.16.2.2  thorpej  */
     32  1.16.2.2  thorpej 
     33  1.16.2.2  thorpej #include <sys/types.h>
     34  1.16.2.2  thorpej #include <sys/param.h>
     35  1.16.2.2  thorpej #include <sys/systm.h>
     36  1.16.2.2  thorpej #include <sys/fcntl.h>
     37  1.16.2.2  thorpej #include <sys/errno.h>
     38  1.16.2.2  thorpej #include <sys/ioctl.h>
     39  1.16.2.2  thorpej #include <sys/malloc.h>
     40  1.16.2.2  thorpej #include <sys/buf.h>
     41  1.16.2.2  thorpej #include <sys/proc.h>
     42  1.16.2.2  thorpej #include <sys/user.h>
     43  1.16.2.2  thorpej #include <sys/device.h>
     44  1.16.2.2  thorpej #include <sys/conf.h>
     45  1.16.2.2  thorpej #include <sys/scanio.h>
     46  1.16.2.2  thorpej 
     47  1.16.2.2  thorpej #include <dev/scsipi/scsi_all.h>
     48  1.16.2.2  thorpej #include <dev/scsipi/scsipi_all.h>
     49  1.16.2.2  thorpej #include <dev/scsipi/scsi_scanner.h>
     50  1.16.2.2  thorpej #include <dev/scsipi/scsiconf.h>
     51  1.16.2.2  thorpej #include <dev/scsipi/ssvar.h>
     52  1.16.2.2  thorpej 
     53  1.16.2.2  thorpej #include <dev/scsipi/ss_mustek.h>
     54  1.16.2.2  thorpej 
     55  1.16.2.2  thorpej #define SSMODE(z)	( minor(z)       & 0x03)
     56  1.16.2.2  thorpej #define SSUNIT(z)	((minor(z) >> 4)       )
     57  1.16.2.2  thorpej 
     58  1.16.2.2  thorpej /*
     59  1.16.2.2  thorpej  * If the mode is 3 (e.g. minor = 3,7,11,15)
     60  1.16.2.2  thorpej  * then the device has been openned to set defaults
     61  1.16.2.2  thorpej  * This mode does NOT ALLOW I/O, only ioctls
     62  1.16.2.2  thorpej  */
     63  1.16.2.2  thorpej #define MODE_REWIND	0
     64  1.16.2.2  thorpej #define MODE_NONREWIND	1
     65  1.16.2.2  thorpej #define MODE_CONTROL	3
     66  1.16.2.2  thorpej 
     67  1.16.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
     68  1.16.2.2  thorpej int ssmatch __P((struct device *, void *, void *));
     69  1.16.2.2  thorpej #else
     70  1.16.2.2  thorpej int ssmatch __P((struct device *, struct cfdata *, void *));
     71  1.16.2.2  thorpej #endif
     72  1.16.2.2  thorpej void ssattach __P((struct device *, struct device *, void *));
     73  1.16.2.2  thorpej 
     74  1.16.2.2  thorpej struct cfattach ss_ca = {
     75  1.16.2.2  thorpej 	sizeof(struct ss_softc), ssmatch, ssattach
     76  1.16.2.2  thorpej };
     77  1.16.2.2  thorpej 
     78  1.16.2.2  thorpej struct cfdriver ss_cd = {
     79  1.16.2.2  thorpej 	NULL, "ss", DV_DULL
     80  1.16.2.2  thorpej };
     81  1.16.2.2  thorpej 
     82  1.16.2.2  thorpej void    ssstrategy __P((struct buf *));
     83  1.16.2.2  thorpej void    ssstart __P((void *));
     84  1.16.2.2  thorpej void	ssminphys __P((struct buf *));
     85  1.16.2.2  thorpej 
     86  1.16.2.2  thorpej struct scsipi_device ss_switch = {
     87  1.16.2.2  thorpej 	NULL,
     88  1.16.2.2  thorpej 	ssstart,
     89  1.16.2.2  thorpej 	NULL,
     90  1.16.2.2  thorpej 	NULL,
     91  1.16.2.2  thorpej };
     92  1.16.2.2  thorpej 
     93  1.16.2.2  thorpej struct scsipi_inquiry_pattern ss_patterns[] = {
     94  1.16.2.2  thorpej 	{T_SCANNER, T_FIXED,
     95  1.16.2.2  thorpej 	 "",         "",                 ""},
     96  1.16.2.2  thorpej 	{T_SCANNER, T_REMOV,
     97  1.16.2.2  thorpej 	 "",         "",                 ""},
     98  1.16.2.2  thorpej 	{T_PROCESSOR, T_FIXED,
     99  1.16.2.2  thorpej 	 "HP      ", "C1750A          ", ""},
    100  1.16.2.2  thorpej 	{T_PROCESSOR, T_FIXED,
    101  1.16.2.2  thorpej 	 "HP      ", "C2500A          ", ""},
    102  1.16.2.2  thorpej 	{T_PROCESSOR, T_FIXED,
    103  1.16.2.2  thorpej 	 "HP      ", "C1130A          ", ""},
    104  1.16.2.2  thorpej };
    105  1.16.2.2  thorpej 
    106  1.16.2.2  thorpej int
    107  1.16.2.2  thorpej ssmatch(parent, match, aux)
    108  1.16.2.2  thorpej 	struct device *parent;
    109  1.16.2.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    110  1.16.2.2  thorpej 	void *match;
    111  1.16.2.2  thorpej #else
    112  1.16.2.2  thorpej 	struct cfdata *match;
    113  1.16.2.2  thorpej #endif
    114  1.16.2.2  thorpej 	void *aux;
    115  1.16.2.2  thorpej {
    116  1.16.2.2  thorpej 	struct scsipibus_attach_args *sa = aux;
    117  1.16.2.2  thorpej 	int priority;
    118  1.16.2.2  thorpej 
    119  1.16.2.2  thorpej 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    120  1.16.2.3  thorpej 	    (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]),
    121  1.16.2.2  thorpej 	    sizeof(ss_patterns[0]), &priority);
    122  1.16.2.2  thorpej 	return (priority);
    123  1.16.2.2  thorpej }
    124  1.16.2.2  thorpej 
    125  1.16.2.2  thorpej /*
    126  1.16.2.2  thorpej  * The routine called by the low level scsi routine when it discovers
    127  1.16.2.2  thorpej  * A device suitable for this driver
    128  1.16.2.2  thorpej  * If it is a know special, call special attach routine to install
    129  1.16.2.2  thorpej  * special handlers into the ss_softc structure
    130  1.16.2.2  thorpej  */
    131  1.16.2.2  thorpej void
    132  1.16.2.2  thorpej ssattach(parent, self, aux)
    133  1.16.2.2  thorpej 	struct device *parent, *self;
    134  1.16.2.2  thorpej 	void *aux;
    135  1.16.2.2  thorpej {
    136  1.16.2.2  thorpej 	struct ss_softc *ss = (void *)self;
    137  1.16.2.2  thorpej 	struct scsipibus_attach_args *sa = aux;
    138  1.16.2.2  thorpej 	struct scsipi_link *sc_link = sa->sa_sc_link;
    139  1.16.2.2  thorpej 
    140  1.16.2.2  thorpej 	SC_DEBUG(sc_link, SDEV_DB2, ("ssattach: "));
    141  1.16.2.2  thorpej 
    142  1.16.2.2  thorpej 	/*
    143  1.16.2.2  thorpej 	 * Store information needed to contact our base driver
    144  1.16.2.2  thorpej 	 */
    145  1.16.2.2  thorpej 	ss->sc_link = sc_link;
    146  1.16.2.2  thorpej 	sc_link->device = &ss_switch;
    147  1.16.2.2  thorpej 	sc_link->device_softc = ss;
    148  1.16.2.2  thorpej 	sc_link->openings = 1;
    149  1.16.2.2  thorpej 
    150  1.16.2.2  thorpej 	/*
    151  1.16.2.2  thorpej 	 * look for non-standard scanners with help of the quirk table
    152  1.16.2.2  thorpej 	 * and install functions for special handling
    153  1.16.2.2  thorpej 	 */
    154  1.16.2.2  thorpej 	SC_DEBUG(sc_link, SDEV_DB2, ("ssattach:\n"));
    155  1.16.2.2  thorpej 	if (!bcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6))
    156  1.16.2.2  thorpej 		mustek_attach(ss, sa);
    157  1.16.2.2  thorpej 	if (!bcmp(sa->sa_inqbuf.vendor, "HP      ", 8))
    158  1.16.2.2  thorpej 		scanjet_attach(ss, sa);
    159  1.16.2.2  thorpej 	if (ss->special == NULL) {
    160  1.16.2.2  thorpej 		/* XXX add code to restart a SCSI2 scanner, if any */
    161  1.16.2.2  thorpej 	}
    162  1.16.2.2  thorpej 
    163  1.16.2.2  thorpej 	/*
    164  1.16.2.2  thorpej 	 * Set up the buf queue for this device
    165  1.16.2.2  thorpej 	 */
    166  1.16.2.2  thorpej 	ss->buf_queue.b_active = 0;
    167  1.16.2.2  thorpej 	ss->buf_queue.b_actf = 0;
    168  1.16.2.2  thorpej 	ss->buf_queue.b_actb = &ss->buf_queue.b_actf;
    169  1.16.2.2  thorpej }
    170  1.16.2.2  thorpej 
    171  1.16.2.2  thorpej /*
    172  1.16.2.2  thorpej  * open the device.
    173  1.16.2.2  thorpej  */
    174  1.16.2.2  thorpej int
    175  1.16.2.2  thorpej ssopen(dev, flag, mode, p)
    176  1.16.2.2  thorpej 	dev_t dev;
    177  1.16.2.2  thorpej 	int flag;
    178  1.16.2.2  thorpej 	int mode;
    179  1.16.2.2  thorpej 	struct proc *p;
    180  1.16.2.2  thorpej {
    181  1.16.2.2  thorpej 	int unit;
    182  1.16.2.2  thorpej 	u_int ssmode;
    183  1.16.2.2  thorpej 	int error = 0;
    184  1.16.2.2  thorpej 	struct ss_softc *ss;
    185  1.16.2.2  thorpej 	struct scsipi_link *sc_link;
    186  1.16.2.2  thorpej 
    187  1.16.2.2  thorpej 	unit = SSUNIT(dev);
    188  1.16.2.2  thorpej 	if (unit >= ss_cd.cd_ndevs)
    189  1.16.2.2  thorpej 		return (ENXIO);
    190  1.16.2.2  thorpej 	ss = ss_cd.cd_devs[unit];
    191  1.16.2.2  thorpej 	if (!ss)
    192  1.16.2.2  thorpej 		return (ENXIO);
    193  1.16.2.2  thorpej 
    194  1.16.2.2  thorpej 	ssmode = SSMODE(dev);
    195  1.16.2.2  thorpej 	sc_link = ss->sc_link;
    196  1.16.2.2  thorpej 
    197  1.16.2.2  thorpej 	SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    198  1.16.2.2  thorpej 	    unit, ss_cd.cd_ndevs));
    199  1.16.2.2  thorpej 
    200  1.16.2.2  thorpej 	if (sc_link->flags & SDEV_OPEN) {
    201  1.16.2.2  thorpej 		printf("%s: already open\n", ss->sc_dev.dv_xname);
    202  1.16.2.2  thorpej 		return (EBUSY);
    203  1.16.2.2  thorpej 	}
    204  1.16.2.2  thorpej 
    205  1.16.2.2  thorpej 	/*
    206  1.16.2.2  thorpej 	 * Catch any unit attention errors.
    207  1.16.2.2  thorpej 	 *
    208  1.16.2.2  thorpej 	 * SCSI_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners
    209  1.16.2.2  thorpej 	 * consider paper to be a changeable media
    210  1.16.2.2  thorpej 	 *
    211  1.16.2.2  thorpej 	 */
    212  1.16.2.2  thorpej 	error = scsipi_test_unit_ready(sc_link,
    213  1.16.2.2  thorpej 	    SCSI_IGNORE_MEDIA_CHANGE | SCSI_IGNORE_ILLEGAL_REQUEST |
    214  1.16.2.2  thorpej 	    (ssmode == MODE_CONTROL ? SCSI_IGNORE_NOT_READY : 0));
    215  1.16.2.2  thorpej 	if (error)
    216  1.16.2.2  thorpej 		goto bad;
    217  1.16.2.2  thorpej 
    218  1.16.2.2  thorpej 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    219  1.16.2.2  thorpej 
    220  1.16.2.2  thorpej 	/*
    221  1.16.2.2  thorpej 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    222  1.16.2.2  thorpej 	 * then the device has been opened to set defaults
    223  1.16.2.2  thorpej 	 * This mode does NOT ALLOW I/O, only ioctls
    224  1.16.2.2  thorpej 	 */
    225  1.16.2.2  thorpej 	if (ssmode == MODE_CONTROL)
    226  1.16.2.2  thorpej 		return (0);
    227  1.16.2.2  thorpej 
    228  1.16.2.2  thorpej 	SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
    229  1.16.2.2  thorpej 	return (0);
    230  1.16.2.2  thorpej 
    231  1.16.2.2  thorpej bad:
    232  1.16.2.2  thorpej 	sc_link->flags &= ~SDEV_OPEN;
    233  1.16.2.2  thorpej 	return (error);
    234  1.16.2.2  thorpej }
    235  1.16.2.2  thorpej 
    236  1.16.2.2  thorpej /*
    237  1.16.2.2  thorpej  * close the device.. only called if we are the LAST
    238  1.16.2.2  thorpej  * occurence of an open device
    239  1.16.2.2  thorpej  */
    240  1.16.2.2  thorpej int
    241  1.16.2.2  thorpej ssclose(dev, flag, mode, p)
    242  1.16.2.2  thorpej 	dev_t dev;
    243  1.16.2.2  thorpej 	int flag;
    244  1.16.2.2  thorpej 	int mode;
    245  1.16.2.2  thorpej 	struct proc *p;
    246  1.16.2.2  thorpej {
    247  1.16.2.2  thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    248  1.16.2.2  thorpej 	int error;
    249  1.16.2.2  thorpej 
    250  1.16.2.2  thorpej 	SC_DEBUG(ss->sc_link, SDEV_DB1, ("closing\n"));
    251  1.16.2.2  thorpej 
    252  1.16.2.2  thorpej 	if (SSMODE(dev) == MODE_REWIND) {
    253  1.16.2.2  thorpej 		if (ss->special->rewind_scanner) {
    254  1.16.2.2  thorpej 			/* call special handler to rewind/abort scan */
    255  1.16.2.2  thorpej 			error = (ss->special->rewind_scanner)(ss);
    256  1.16.2.2  thorpej 			if (error)
    257  1.16.2.2  thorpej 				return (error);
    258  1.16.2.2  thorpej 		} else {
    259  1.16.2.2  thorpej 			/* XXX add code to restart a SCSI2 scanner, if any */
    260  1.16.2.2  thorpej 		}
    261  1.16.2.2  thorpej 		ss->sio.scan_window_size = 0;
    262  1.16.2.2  thorpej 		ss->flags &= ~SSF_TRIGGERED;
    263  1.16.2.2  thorpej 	}
    264  1.16.2.2  thorpej 	ss->sc_link->flags &= ~SDEV_OPEN;
    265  1.16.2.2  thorpej 
    266  1.16.2.2  thorpej 	return (0);
    267  1.16.2.2  thorpej }
    268  1.16.2.2  thorpej 
    269  1.16.2.2  thorpej /*
    270  1.16.2.2  thorpej  * trim the size of the transfer if needed,
    271  1.16.2.2  thorpej  * called by physio
    272  1.16.2.2  thorpej  * basically the smaller of our min and the scsi driver's
    273  1.16.2.2  thorpej  * minphys
    274  1.16.2.2  thorpej  */
    275  1.16.2.2  thorpej void
    276  1.16.2.2  thorpej ssminphys(bp)
    277  1.16.2.2  thorpej 	struct buf *bp;
    278  1.16.2.2  thorpej {
    279  1.16.2.2  thorpej 	register struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    280  1.16.2.2  thorpej 
    281  1.16.2.2  thorpej 	(ss->sc_link->adapter->scsipi_minphys)(bp);
    282  1.16.2.2  thorpej 
    283  1.16.2.2  thorpej 	/*
    284  1.16.2.2  thorpej 	 * trim the transfer further for special devices this is
    285  1.16.2.2  thorpej 	 * because some scanners only read multiples of a line at a
    286  1.16.2.2  thorpej 	 * time, also some cannot disconnect, so the read must be
    287  1.16.2.2  thorpej 	 * short enough to happen quickly
    288  1.16.2.2  thorpej 	 */
    289  1.16.2.2  thorpej 	if (ss->special->minphys)
    290  1.16.2.2  thorpej 		(ss->special->minphys)(ss, bp);
    291  1.16.2.2  thorpej }
    292  1.16.2.2  thorpej 
    293  1.16.2.2  thorpej /*
    294  1.16.2.2  thorpej  * Do a read on a device for a user process.
    295  1.16.2.2  thorpej  * Prime scanner at start of read, check uio values, call ssstrategy
    296  1.16.2.2  thorpej  * via physio for the actual transfer.
    297  1.16.2.2  thorpej  */
    298  1.16.2.2  thorpej int
    299  1.16.2.2  thorpej ssread(dev, uio, flag)
    300  1.16.2.2  thorpej 	dev_t dev;
    301  1.16.2.2  thorpej 	struct uio *uio;
    302  1.16.2.2  thorpej 	int flag;
    303  1.16.2.2  thorpej {
    304  1.16.2.2  thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    305  1.16.2.2  thorpej 	int error;
    306  1.16.2.2  thorpej 
    307  1.16.2.2  thorpej 	/* if the scanner has not yet been started, do it now */
    308  1.16.2.2  thorpej 	if (!(ss->flags & SSF_TRIGGERED)) {
    309  1.16.2.2  thorpej 		if (ss->special->trigger_scanner) {
    310  1.16.2.2  thorpej 			error = (ss->special->trigger_scanner)(ss);
    311  1.16.2.2  thorpej 			if (error)
    312  1.16.2.2  thorpej 				return (error);
    313  1.16.2.2  thorpej 		}
    314  1.16.2.2  thorpej 		ss->flags |= SSF_TRIGGERED;
    315  1.16.2.2  thorpej 	}
    316  1.16.2.2  thorpej 
    317  1.16.2.2  thorpej 	return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio));
    318  1.16.2.2  thorpej }
    319  1.16.2.2  thorpej 
    320  1.16.2.2  thorpej /*
    321  1.16.2.2  thorpej  * Actually translate the requested transfer into one the physical
    322  1.16.2.2  thorpej  * driver can understand The transfer is described by a buf and will
    323  1.16.2.2  thorpej  * include only one physical transfer.
    324  1.16.2.2  thorpej  */
    325  1.16.2.2  thorpej void
    326  1.16.2.2  thorpej ssstrategy(bp)
    327  1.16.2.2  thorpej 	struct buf *bp;
    328  1.16.2.2  thorpej {
    329  1.16.2.2  thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    330  1.16.2.2  thorpej 	struct buf *dp;
    331  1.16.2.2  thorpej 	int s;
    332  1.16.2.2  thorpej 
    333  1.16.2.2  thorpej 	SC_DEBUG(ss->sc_link, SDEV_DB1,
    334  1.16.2.2  thorpej 	    ("ssstrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    335  1.16.2.2  thorpej 
    336  1.16.2.2  thorpej 	if (bp->b_bcount > ss->sio.scan_window_size)
    337  1.16.2.2  thorpej 		bp->b_bcount = ss->sio.scan_window_size;
    338  1.16.2.2  thorpej 
    339  1.16.2.2  thorpej 	/*
    340  1.16.2.2  thorpej 	 * If it's a null transfer, return immediatly
    341  1.16.2.2  thorpej 	 */
    342  1.16.2.2  thorpej 	if (bp->b_bcount == 0)
    343  1.16.2.2  thorpej 		goto done;
    344  1.16.2.2  thorpej 
    345  1.16.2.2  thorpej 	s = splbio();
    346  1.16.2.2  thorpej 
    347  1.16.2.2  thorpej 	/*
    348  1.16.2.2  thorpej 	 * Place it in the queue of activities for this scanner
    349  1.16.2.2  thorpej 	 * at the end (a bit silly because we only have on user..
    350  1.16.2.2  thorpej 	 * (but it could fork()))
    351  1.16.2.2  thorpej 	 */
    352  1.16.2.2  thorpej 	dp = &ss->buf_queue;
    353  1.16.2.2  thorpej 	bp->b_actf = NULL;
    354  1.16.2.2  thorpej 	bp->b_actb = dp->b_actb;
    355  1.16.2.2  thorpej 	*dp->b_actb = bp;
    356  1.16.2.2  thorpej 	dp->b_actb = &bp->b_actf;
    357  1.16.2.2  thorpej 
    358  1.16.2.2  thorpej 	/*
    359  1.16.2.2  thorpej 	 * Tell the device to get going on the transfer if it's
    360  1.16.2.2  thorpej 	 * not doing anything, otherwise just wait for completion
    361  1.16.2.2  thorpej 	 * (All a bit silly if we're only allowing 1 open but..)
    362  1.16.2.2  thorpej 	 */
    363  1.16.2.2  thorpej 	ssstart(ss);
    364  1.16.2.2  thorpej 
    365  1.16.2.2  thorpej 	splx(s);
    366  1.16.2.2  thorpej 	return;
    367  1.16.2.2  thorpej 	bp->b_flags |= B_ERROR;
    368  1.16.2.2  thorpej done:
    369  1.16.2.2  thorpej 	/*
    370  1.16.2.2  thorpej 	 * Correctly set the buf to indicate a completed xfer
    371  1.16.2.2  thorpej 	 */
    372  1.16.2.2  thorpej 	bp->b_resid = bp->b_bcount;
    373  1.16.2.2  thorpej 	biodone(bp);
    374  1.16.2.2  thorpej }
    375  1.16.2.2  thorpej 
    376  1.16.2.2  thorpej /*
    377  1.16.2.2  thorpej  * ssstart looks to see if there is a buf waiting for the device
    378  1.16.2.2  thorpej  * and that the device is not already busy. If both are true,
    379  1.16.2.2  thorpej  * It dequeues the buf and creates a scsi command to perform the
    380  1.16.2.2  thorpej  * transfer required. The transfer request will call scsipi_done
    381  1.16.2.2  thorpej  * on completion, which will in turn call this routine again
    382  1.16.2.2  thorpej  * so that the next queued transfer is performed.
    383  1.16.2.2  thorpej  * The bufs are queued by the strategy routine (ssstrategy)
    384  1.16.2.2  thorpej  *
    385  1.16.2.2  thorpej  * This routine is also called after other non-queued requests
    386  1.16.2.2  thorpej  * have been made of the scsi driver, to ensure that the queue
    387  1.16.2.2  thorpej  * continues to be drained.
    388  1.16.2.2  thorpej  * ssstart() is called at splbio
    389  1.16.2.2  thorpej  */
    390  1.16.2.2  thorpej void
    391  1.16.2.2  thorpej ssstart(v)
    392  1.16.2.2  thorpej 	void *v;
    393  1.16.2.2  thorpej {
    394  1.16.2.2  thorpej 	struct ss_softc *ss = v;
    395  1.16.2.2  thorpej 	struct scsipi_link *sc_link = ss->sc_link;
    396  1.16.2.2  thorpej 	register struct buf *bp, *dp;
    397  1.16.2.2  thorpej 
    398  1.16.2.2  thorpej 	SC_DEBUG(sc_link, SDEV_DB2, ("ssstart "));
    399  1.16.2.2  thorpej 	/*
    400  1.16.2.2  thorpej 	 * See if there is a buf to do and we are not already
    401  1.16.2.2  thorpej 	 * doing one
    402  1.16.2.2  thorpej 	 */
    403  1.16.2.2  thorpej 	while (sc_link->openings > 0) {
    404  1.16.2.2  thorpej 		/* if a special awaits, let it proceed first */
    405  1.16.2.2  thorpej 		if (sc_link->flags & SDEV_WAITING) {
    406  1.16.2.2  thorpej 			sc_link->flags &= ~SDEV_WAITING;
    407  1.16.2.2  thorpej 			wakeup((caddr_t)sc_link);
    408  1.16.2.2  thorpej 			return;
    409  1.16.2.2  thorpej 		}
    410  1.16.2.2  thorpej 
    411  1.16.2.2  thorpej 		/*
    412  1.16.2.2  thorpej 		 * See if there is a buf with work for us to do..
    413  1.16.2.2  thorpej 		 */
    414  1.16.2.2  thorpej 		dp = &ss->buf_queue;
    415  1.16.2.2  thorpej 		if ((bp = dp->b_actf) == NULL)
    416  1.16.2.2  thorpej 			return;
    417  1.16.2.2  thorpej 		if ((dp = bp->b_actf) != NULL)
    418  1.16.2.2  thorpej 			dp->b_actb = bp->b_actb;
    419  1.16.2.2  thorpej 		else
    420  1.16.2.2  thorpej 			ss->buf_queue.b_actb = bp->b_actb;
    421  1.16.2.2  thorpej 		*bp->b_actb = dp;
    422  1.16.2.2  thorpej 
    423  1.16.2.2  thorpej 		if (ss->special->read) {
    424  1.16.2.2  thorpej 			(ss->special->read)(ss, bp);
    425  1.16.2.2  thorpej 		} else {
    426  1.16.2.2  thorpej 			/* generic scsi2 scanner read */
    427  1.16.2.2  thorpej 			/* XXX add code for SCSI2 scanner read */
    428  1.16.2.2  thorpej 		}
    429  1.16.2.2  thorpej 	}
    430  1.16.2.2  thorpej }
    431  1.16.2.2  thorpej 
    432  1.16.2.2  thorpej /*
    433  1.16.2.2  thorpej  * Perform special action on behalf of the user;
    434  1.16.2.2  thorpej  * knows about the internals of this device
    435  1.16.2.2  thorpej  */
    436  1.16.2.2  thorpej int
    437  1.16.2.2  thorpej ssioctl(dev, cmd, addr, flag, p)
    438  1.16.2.2  thorpej 	dev_t dev;
    439  1.16.2.2  thorpej 	u_long cmd;
    440  1.16.2.2  thorpej 	caddr_t addr;
    441  1.16.2.2  thorpej 	int flag;
    442  1.16.2.2  thorpej 	struct proc *p;
    443  1.16.2.2  thorpej {
    444  1.16.2.2  thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    445  1.16.2.2  thorpej 	int error = 0;
    446  1.16.2.2  thorpej 	struct scan_io *sio;
    447  1.16.2.2  thorpej 
    448  1.16.2.2  thorpej 	switch (cmd) {
    449  1.16.2.2  thorpej 	case SCIOCGET:
    450  1.16.2.2  thorpej 		if (ss->special->get_params) {
    451  1.16.2.2  thorpej 			/* call special handler */
    452  1.16.2.2  thorpej 			error = (ss->special->get_params)(ss);
    453  1.16.2.2  thorpej 			if (error)
    454  1.16.2.2  thorpej 				return (error);
    455  1.16.2.2  thorpej 		} else {
    456  1.16.2.2  thorpej 			/* XXX add code for SCSI2 scanner, if any */
    457  1.16.2.2  thorpej 			return (EOPNOTSUPP);
    458  1.16.2.2  thorpej 		}
    459  1.16.2.2  thorpej 		bcopy(&ss->sio, addr, sizeof(struct scan_io));
    460  1.16.2.2  thorpej 		break;
    461  1.16.2.2  thorpej 	case SCIOCSET:
    462  1.16.2.2  thorpej 		sio = (struct scan_io *)addr;
    463  1.16.2.2  thorpej 
    464  1.16.2.2  thorpej 		if (ss->special->set_params) {
    465  1.16.2.2  thorpej 			/* call special handler */
    466  1.16.2.2  thorpej 			error = (ss->special->set_params)(ss, sio);
    467  1.16.2.2  thorpej 			if (error)
    468  1.16.2.2  thorpej 				return (error);
    469  1.16.2.2  thorpej 		} else {
    470  1.16.2.2  thorpej 			/* XXX add code for SCSI2 scanner, if any */
    471  1.16.2.2  thorpej 			return (EOPNOTSUPP);
    472  1.16.2.2  thorpej 		}
    473  1.16.2.2  thorpej 		break;
    474  1.16.2.2  thorpej 	case SCIOCRESTART:
    475  1.16.2.2  thorpej 		if (ss->special->rewind_scanner ) {
    476  1.16.2.2  thorpej 			/* call special handler */
    477  1.16.2.2  thorpej 			error = (ss->special->rewind_scanner)(ss);
    478  1.16.2.2  thorpej 			if (error)
    479  1.16.2.2  thorpej 				return (error);
    480  1.16.2.2  thorpej 		} else
    481  1.16.2.2  thorpej 			/* XXX add code for SCSI2 scanner, if any */
    482  1.16.2.2  thorpej 			return (EOPNOTSUPP);
    483  1.16.2.2  thorpej 		ss->flags &= ~SSF_TRIGGERED;
    484  1.16.2.2  thorpej 		break;
    485  1.16.2.2  thorpej #ifdef NOTYET
    486  1.16.2.2  thorpej 	case SCAN_USE_ADF:
    487  1.16.2.2  thorpej 		break;
    488  1.16.2.2  thorpej #endif
    489  1.16.2.2  thorpej 	default:
    490  1.16.2.2  thorpej 		if (SSMODE(dev) != MODE_CONTROL)
    491  1.16.2.2  thorpej 			return (ENOTTY);
    492  1.16.2.2  thorpej 		return (scsipi_do_ioctl(ss->sc_link, dev, cmd, addr, flag, p));
    493  1.16.2.2  thorpej 	}
    494  1.16.2.2  thorpej 	return (error);
    495  1.16.2.2  thorpej }
    496