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