Home | History | Annotate | Line # | Download | only in scsipi
ss.c revision 1.53
      1  1.53    bouyer /*	$NetBSD: ss.c,v 1.53 2004/08/27 20:37:29 bouyer 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.37     lukem 
     33  1.37     lukem #include <sys/cdefs.h>
     34  1.53    bouyer __KERNEL_RCSID(0, "$NetBSD: ss.c,v 1.53 2004/08/27 20:37:29 bouyer Exp $");
     35   1.1   mycroft 
     36   1.1   mycroft #include <sys/param.h>
     37   1.1   mycroft #include <sys/systm.h>
     38   1.1   mycroft #include <sys/fcntl.h>
     39   1.1   mycroft #include <sys/errno.h>
     40   1.1   mycroft #include <sys/ioctl.h>
     41   1.1   mycroft #include <sys/malloc.h>
     42   1.1   mycroft #include <sys/buf.h>
     43   1.1   mycroft #include <sys/proc.h>
     44   1.1   mycroft #include <sys/user.h>
     45   1.1   mycroft #include <sys/device.h>
     46   1.9  christos #include <sys/conf.h>
     47  1.32  augustss #include <sys/vnode.h>
     48   1.1   mycroft #include <sys/scanio.h>
     49   1.1   mycroft 
     50  1.16    bouyer #include <dev/scsipi/scsi_all.h>
     51  1.16    bouyer #include <dev/scsipi/scsipi_all.h>
     52  1.16    bouyer #include <dev/scsipi/scsi_scanner.h>
     53  1.16    bouyer #include <dev/scsipi/scsiconf.h>
     54  1.16    bouyer #include <dev/scsipi/ssvar.h>
     55   1.1   mycroft 
     56  1.16    bouyer #include <dev/scsipi/ss_mustek.h>
     57   1.1   mycroft 
     58   1.1   mycroft #define SSMODE(z)	( minor(z)       & 0x03)
     59   1.1   mycroft #define SSUNIT(z)	((minor(z) >> 4)       )
     60  1.33  augustss #define SSNMINOR 16
     61   1.1   mycroft 
     62   1.1   mycroft /*
     63   1.1   mycroft  * If the mode is 3 (e.g. minor = 3,7,11,15)
     64   1.1   mycroft  * then the device has been openned to set defaults
     65   1.1   mycroft  * This mode does NOT ALLOW I/O, only ioctls
     66   1.1   mycroft  */
     67   1.1   mycroft #define MODE_REWIND	0
     68   1.1   mycroft #define MODE_NONREWIND	1
     69   1.1   mycroft #define MODE_CONTROL	3
     70   1.1   mycroft 
     71  1.52   thorpej static int	ssmatch(struct device *, struct cfdata *, void *);
     72  1.52   thorpej static void	ssattach(struct device *, struct device *, void *);
     73  1.52   thorpej static int	ssdetach(struct device *self, int flags);
     74  1.52   thorpej static int	ssactivate(struct device *self, enum devact act);
     75   1.1   mycroft 
     76  1.43   thorpej CFATTACH_DECL(ss, sizeof(struct ss_softc),
     77  1.44   thorpej     ssmatch, ssattach, ssdetach, ssactivate);
     78   1.8   thorpej 
     79  1.19   thorpej extern struct cfdriver ss_cd;
     80   1.1   mycroft 
     81  1.52   thorpej static dev_type_open(ssopen);
     82  1.52   thorpej static dev_type_close(ssclose);
     83  1.52   thorpej static dev_type_read(ssread);
     84  1.52   thorpej static dev_type_ioctl(ssioctl);
     85  1.40   gehenna 
     86  1.40   gehenna const struct cdevsw ss_cdevsw = {
     87  1.40   gehenna 	ssopen, ssclose, ssread, nowrite, ssioctl,
     88  1.45  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
     89  1.40   gehenna };
     90  1.40   gehenna 
     91  1.52   thorpej static void	ssstrategy(struct buf *);
     92  1.52   thorpej static void	ssstart(struct scsipi_periph *);
     93  1.52   thorpej static void	ssminphys(struct buf *);
     94   1.1   mycroft 
     95  1.52   thorpej static const struct scsipi_periphsw ss_switch = {
     96   1.1   mycroft 	NULL,
     97   1.1   mycroft 	ssstart,
     98   1.1   mycroft 	NULL,
     99   1.1   mycroft 	NULL,
    100   1.1   mycroft };
    101   1.1   mycroft 
    102  1.52   thorpej static const struct scsipi_inquiry_pattern ss_patterns[] = {
    103   1.1   mycroft 	{T_SCANNER, T_FIXED,
    104   1.1   mycroft 	 "",         "",                 ""},
    105   1.1   mycroft 	{T_SCANNER, T_REMOV,
    106   1.1   mycroft 	 "",         "",                 ""},
    107   1.1   mycroft 	{T_PROCESSOR, T_FIXED,
    108  1.41       chs 	 "HP      ", "C1130A          ", ""},
    109  1.41       chs 	{T_PROCESSOR, T_FIXED,
    110   1.1   mycroft 	 "HP      ", "C1750A          ", ""},
    111   1.1   mycroft 	{T_PROCESSOR, T_FIXED,
    112   1.1   mycroft 	 "HP      ", "C2500A          ", ""},
    113  1.14   thorpej 	{T_PROCESSOR, T_FIXED,
    114  1.41       chs 	 "HP      ", "C2520A          ", ""},
    115  1.18  augustss 	{T_PROCESSOR, T_FIXED,
    116  1.18  augustss 	 "HP      ", "C5110A          ", ""},
    117  1.31      phil 	{T_PROCESSOR, T_FIXED,
    118  1.31      phil 	 "HP      ", "C7670A          ", ""},
    119  1.41       chs 	{T_PROCESSOR, T_FIXED,
    120  1.41       chs 	 "HP      ", "", ""},
    121   1.1   mycroft };
    122   1.1   mycroft 
    123  1.52   thorpej static int
    124  1.32  augustss ssmatch(struct device *parent, struct cfdata *match, void *aux)
    125   1.1   mycroft {
    126  1.16    bouyer 	struct scsipibus_attach_args *sa = aux;
    127   1.1   mycroft 	int priority;
    128   1.1   mycroft 
    129  1.16    bouyer 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    130  1.17     enami 	    (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]),
    131   1.1   mycroft 	    sizeof(ss_patterns[0]), &priority);
    132   1.1   mycroft 	return (priority);
    133   1.1   mycroft }
    134   1.1   mycroft 
    135   1.1   mycroft /*
    136   1.1   mycroft  * The routine called by the low level scsi routine when it discovers
    137   1.1   mycroft  * A device suitable for this driver
    138   1.1   mycroft  * If it is a know special, call special attach routine to install
    139   1.1   mycroft  * special handlers into the ss_softc structure
    140   1.1   mycroft  */
    141  1.52   thorpej static void
    142  1.32  augustss ssattach(struct device *parent, struct device *self, void *aux)
    143   1.1   mycroft {
    144   1.1   mycroft 	struct ss_softc *ss = (void *)self;
    145  1.16    bouyer 	struct scsipibus_attach_args *sa = aux;
    146  1.34    bouyer 	struct scsipi_periph *periph = sa->sa_periph;
    147   1.1   mycroft 
    148  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: "));
    149   1.1   mycroft 
    150  1.20        pk 	ss->flags |= SSF_AUTOCONF;
    151  1.20        pk 
    152   1.1   mycroft 	/*
    153   1.1   mycroft 	 * Store information needed to contact our base driver
    154   1.1   mycroft 	 */
    155  1.34    bouyer 	ss->sc_periph = periph;
    156  1.34    bouyer 	periph->periph_dev = &ss->sc_dev;
    157  1.34    bouyer 	periph->periph_switch = &ss_switch;
    158  1.28       abs 
    159  1.28       abs 	printf("\n");
    160   1.1   mycroft 
    161   1.1   mycroft 	/*
    162  1.39   hannken 	 * Set up the buf queue for this device
    163  1.39   hannken 	 */
    164  1.39   hannken 	bufq_alloc(&ss->buf_queue, BUFQ_FCFS);
    165  1.39   hannken 
    166  1.53    bouyer 	callout_init(&ss->sc_callout);
    167  1.53    bouyer 
    168  1.39   hannken 	/*
    169   1.1   mycroft 	 * look for non-standard scanners with help of the quirk table
    170   1.1   mycroft 	 * and install functions for special handling
    171   1.1   mycroft 	 */
    172  1.35    bouyer 	SC_DEBUG(periph, SCSIPI_DB2, ("ssattach:\n"));
    173  1.32  augustss 	if (memcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6) == 0)
    174   1.1   mycroft 		mustek_attach(ss, sa);
    175  1.32  augustss 	if (memcmp(sa->sa_inqbuf.vendor, "HP      ", 8) == 0 &&
    176  1.32  augustss 	    memcmp(sa->sa_inqbuf.product, "ScanJet 5300C", 13) != 0)
    177   1.1   mycroft 		scanjet_attach(ss, sa);
    178   1.1   mycroft 	if (ss->special == NULL) {
    179   1.1   mycroft 		/* XXX add code to restart a SCSI2 scanner, if any */
    180   1.1   mycroft 	}
    181   1.1   mycroft 
    182  1.20        pk 	ss->flags &= ~SSF_AUTOCONF;
    183   1.1   mycroft }
    184   1.1   mycroft 
    185  1.52   thorpej static int
    186  1.32  augustss ssdetach(struct device *self, int flags)
    187  1.32  augustss {
    188  1.32  augustss 	struct ss_softc *ss = (struct ss_softc *) self;
    189  1.32  augustss 	struct buf *bp;
    190  1.32  augustss 	int s, cmaj, mn;
    191  1.32  augustss 
    192  1.32  augustss 	/* locate the major number */
    193  1.40   gehenna 	cmaj = cdevsw_lookup_major(&ss_cdevsw);
    194  1.32  augustss 
    195  1.53    bouyer 	/* kill any pending restart */
    196  1.53    bouyer 	callout_stop(&ss->sc_callout);
    197  1.53    bouyer 
    198  1.32  augustss 	s = splbio();
    199  1.32  augustss 
    200  1.32  augustss 	/* Kill off any queued buffers. */
    201  1.39   hannken 	while ((bp = BUFQ_GET(&ss->buf_queue)) != NULL) {
    202  1.32  augustss 		bp->b_error = EIO;
    203  1.32  augustss 		bp->b_flags |= B_ERROR;
    204  1.32  augustss 		bp->b_resid = bp->b_bcount;
    205  1.32  augustss 		biodone(bp);
    206  1.32  augustss 	}
    207  1.32  augustss 
    208  1.39   hannken 	bufq_free(&ss->buf_queue);
    209  1.39   hannken 
    210  1.32  augustss 	/* Kill off any pending commands. */
    211  1.34    bouyer 	scsipi_kill_pending(ss->sc_periph);
    212  1.32  augustss 
    213  1.32  augustss 	splx(s);
    214  1.32  augustss 
    215  1.32  augustss 	/* Nuke the vnodes for any open instances */
    216  1.32  augustss 	mn = SSUNIT(self->dv_unit);
    217  1.33  augustss 	vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR);
    218  1.32  augustss 
    219  1.32  augustss 	return (0);
    220  1.32  augustss }
    221  1.32  augustss 
    222  1.52   thorpej static int
    223  1.32  augustss ssactivate(struct device *self, enum devact act)
    224  1.32  augustss {
    225  1.32  augustss 	int rv = 0;
    226  1.32  augustss 
    227  1.32  augustss 	switch (act) {
    228  1.32  augustss 	case DVACT_ACTIVATE:
    229  1.32  augustss 		rv = EOPNOTSUPP;
    230  1.32  augustss 		break;
    231  1.32  augustss 
    232  1.32  augustss 	case DVACT_DEACTIVATE:
    233  1.32  augustss 		/*
    234  1.32  augustss 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    235  1.32  augustss 		 */
    236  1.32  augustss 		break;
    237  1.32  augustss 	}
    238  1.32  augustss 	return (rv);
    239  1.32  augustss }
    240  1.32  augustss 
    241   1.1   mycroft /*
    242   1.1   mycroft  * open the device.
    243   1.1   mycroft  */
    244  1.52   thorpej static int
    245  1.49      fvdl ssopen(dev_t dev, int flag, int mode, struct proc *p)
    246   1.1   mycroft {
    247   1.1   mycroft 	int unit;
    248   1.5   mycroft 	u_int ssmode;
    249  1.23   thorpej 	int error;
    250   1.1   mycroft 	struct ss_softc *ss;
    251  1.34    bouyer 	struct scsipi_periph *periph;
    252  1.34    bouyer 	struct scsipi_adapter *adapt;
    253   1.1   mycroft 
    254   1.1   mycroft 	unit = SSUNIT(dev);
    255   1.8   thorpej 	if (unit >= ss_cd.cd_ndevs)
    256   1.1   mycroft 		return (ENXIO);
    257   1.8   thorpej 	ss = ss_cd.cd_devs[unit];
    258   1.1   mycroft 	if (!ss)
    259   1.1   mycroft 		return (ENXIO);
    260   1.1   mycroft 
    261  1.32  augustss 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    262  1.32  augustss 		return (ENODEV);
    263  1.32  augustss 
    264   1.5   mycroft 	ssmode = SSMODE(dev);
    265   1.1   mycroft 
    266  1.34    bouyer 	periph = ss->sc_periph;
    267  1.34    bouyer 	adapt = periph->periph_channel->chan_adapter;
    268  1.34    bouyer 
    269  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    270   1.8   thorpej 	    unit, ss_cd.cd_ndevs));
    271   1.1   mycroft 
    272  1.34    bouyer 	if (periph->periph_flags & PERIPH_OPEN) {
    273  1.13  christos 		printf("%s: already open\n", ss->sc_dev.dv_xname);
    274   1.1   mycroft 		return (EBUSY);
    275   1.1   mycroft 	}
    276   1.1   mycroft 
    277  1.34    bouyer 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    278  1.23   thorpej 		return (error);
    279  1.23   thorpej 
    280   1.1   mycroft 	/*
    281   1.1   mycroft 	 * Catch any unit attention errors.
    282   1.1   mycroft 	 *
    283  1.26   thorpej 	 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners
    284   1.1   mycroft 	 * consider paper to be a changeable media
    285   1.1   mycroft 	 *
    286   1.1   mycroft 	 */
    287  1.34    bouyer 	error = scsipi_test_unit_ready(periph,
    288  1.26   thorpej 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    289  1.26   thorpej 	    (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0));
    290   1.1   mycroft 	if (error)
    291   1.1   mycroft 		goto bad;
    292   1.1   mycroft 
    293  1.34    bouyer 	periph->periph_flags |= PERIPH_OPEN;	/* unit attn now errors */
    294   1.1   mycroft 
    295   1.1   mycroft 	/*
    296   1.1   mycroft 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    297   1.1   mycroft 	 * then the device has been opened to set defaults
    298   1.1   mycroft 	 * This mode does NOT ALLOW I/O, only ioctls
    299   1.1   mycroft 	 */
    300   1.5   mycroft 	if (ssmode == MODE_CONTROL)
    301   1.1   mycroft 		return (0);
    302   1.1   mycroft 
    303  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
    304   1.1   mycroft 	return (0);
    305   1.1   mycroft 
    306   1.1   mycroft bad:
    307  1.34    bouyer 	scsipi_adapter_delref(adapt);
    308  1.34    bouyer 	periph->periph_flags &= ~PERIPH_OPEN;
    309   1.1   mycroft 	return (error);
    310   1.1   mycroft }
    311   1.1   mycroft 
    312   1.1   mycroft /*
    313   1.1   mycroft  * close the device.. only called if we are the LAST
    314   1.1   mycroft  * occurence of an open device
    315   1.1   mycroft  */
    316  1.52   thorpej static int
    317  1.49      fvdl ssclose(dev_t dev, int flag, int mode, struct proc *p)
    318   1.1   mycroft {
    319   1.8   thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    320  1.34    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    321  1.34    bouyer 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    322   1.1   mycroft 	int error;
    323   1.1   mycroft 
    324  1.34    bouyer 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n"));
    325   1.1   mycroft 
    326   1.1   mycroft 	if (SSMODE(dev) == MODE_REWIND) {
    327  1.21  explorer 		if (ss->special && ss->special->rewind_scanner) {
    328   1.1   mycroft 			/* call special handler to rewind/abort scan */
    329   1.1   mycroft 			error = (ss->special->rewind_scanner)(ss);
    330   1.1   mycroft 			if (error)
    331   1.1   mycroft 				return (error);
    332   1.1   mycroft 		} else {
    333   1.1   mycroft 			/* XXX add code to restart a SCSI2 scanner, if any */
    334   1.1   mycroft 		}
    335   1.4   mycroft 		ss->sio.scan_window_size = 0;
    336   1.1   mycroft 		ss->flags &= ~SSF_TRIGGERED;
    337   1.1   mycroft 	}
    338  1.24   thorpej 
    339  1.34    bouyer 	scsipi_wait_drain(periph);
    340  1.23   thorpej 
    341  1.34    bouyer 	scsipi_adapter_delref(adapt);
    342  1.34    bouyer 	periph->periph_flags &= ~PERIPH_OPEN;
    343   1.1   mycroft 
    344   1.1   mycroft 	return (0);
    345   1.1   mycroft }
    346   1.1   mycroft 
    347   1.1   mycroft /*
    348   1.1   mycroft  * trim the size of the transfer if needed,
    349   1.1   mycroft  * called by physio
    350   1.1   mycroft  * basically the smaller of our min and the scsi driver's
    351   1.1   mycroft  * minphys
    352   1.1   mycroft  */
    353  1.52   thorpej static void
    354  1.32  augustss ssminphys(struct buf *bp)
    355   1.1   mycroft {
    356  1.30  augustss 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    357  1.34    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    358   1.1   mycroft 
    359  1.51   thorpej 	scsipi_adapter_minphys(periph->periph_channel, bp);
    360   1.1   mycroft 
    361   1.1   mycroft 	/*
    362   1.1   mycroft 	 * trim the transfer further for special devices this is
    363   1.1   mycroft 	 * because some scanners only read multiples of a line at a
    364   1.1   mycroft 	 * time, also some cannot disconnect, so the read must be
    365   1.1   mycroft 	 * short enough to happen quickly
    366   1.1   mycroft 	 */
    367  1.21  explorer 	if (ss->special && ss->special->minphys)
    368   1.1   mycroft 		(ss->special->minphys)(ss, bp);
    369   1.1   mycroft }
    370   1.1   mycroft 
    371   1.1   mycroft /*
    372   1.1   mycroft  * Do a read on a device for a user process.
    373   1.1   mycroft  * Prime scanner at start of read, check uio values, call ssstrategy
    374   1.1   mycroft  * via physio for the actual transfer.
    375   1.1   mycroft  */
    376  1.52   thorpej static int
    377  1.52   thorpej ssread(dev_t dev, struct uio *uio, int flag)
    378   1.1   mycroft {
    379   1.8   thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    380   1.1   mycroft 	int error;
    381   1.1   mycroft 
    382  1.32  augustss 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    383  1.32  augustss 		return (ENODEV);
    384  1.32  augustss 
    385   1.1   mycroft 	/* if the scanner has not yet been started, do it now */
    386   1.1   mycroft 	if (!(ss->flags & SSF_TRIGGERED)) {
    387  1.21  explorer 		if (ss->special && ss->special->trigger_scanner) {
    388   1.1   mycroft 			error = (ss->special->trigger_scanner)(ss);
    389   1.1   mycroft 			if (error)
    390   1.1   mycroft 				return (error);
    391   1.1   mycroft 		}
    392   1.1   mycroft 		ss->flags |= SSF_TRIGGERED;
    393   1.1   mycroft 	}
    394   1.1   mycroft 
    395   1.1   mycroft 	return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio));
    396   1.1   mycroft }
    397   1.1   mycroft 
    398   1.1   mycroft /*
    399   1.1   mycroft  * Actually translate the requested transfer into one the physical
    400   1.1   mycroft  * driver can understand The transfer is described by a buf and will
    401   1.1   mycroft  * include only one physical transfer.
    402   1.1   mycroft  */
    403  1.52   thorpej static void
    404  1.52   thorpej ssstrategy(struct buf *bp)
    405   1.1   mycroft {
    406   1.8   thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    407  1.34    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    408   1.1   mycroft 	int s;
    409   1.1   mycroft 
    410  1.34    bouyer 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
    411  1.47       dbj 	    ("ssstrategy %ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    412  1.25    bouyer 
    413  1.32  augustss 	/*
    414  1.32  augustss 	 * If the device has been made invalid, error out
    415  1.32  augustss 	 */
    416  1.32  augustss 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    417  1.32  augustss 		bp->b_flags |= B_ERROR;
    418  1.34    bouyer 		if (periph->periph_flags & PERIPH_OPEN)
    419  1.32  augustss 			bp->b_error = EIO;
    420  1.32  augustss 		else
    421  1.32  augustss 			bp->b_error = ENODEV;
    422  1.32  augustss 		goto done;
    423  1.32  augustss 	}
    424  1.32  augustss 
    425  1.25    bouyer 	/* If negative offset, error */
    426  1.25    bouyer 	if (bp->b_blkno < 0) {
    427  1.25    bouyer 		bp->b_flags |= B_ERROR;
    428  1.25    bouyer 		bp->b_error = EINVAL;
    429  1.25    bouyer 		goto done;
    430  1.25    bouyer 	}
    431   1.6   mycroft 
    432   1.6   mycroft 	if (bp->b_bcount > ss->sio.scan_window_size)
    433   1.6   mycroft 		bp->b_bcount = ss->sio.scan_window_size;
    434   1.1   mycroft 
    435   1.1   mycroft 	/*
    436   1.1   mycroft 	 * If it's a null transfer, return immediatly
    437   1.1   mycroft 	 */
    438   1.1   mycroft 	if (bp->b_bcount == 0)
    439   1.1   mycroft 		goto done;
    440   1.1   mycroft 
    441   1.1   mycroft 	s = splbio();
    442   1.1   mycroft 
    443   1.1   mycroft 	/*
    444   1.1   mycroft 	 * Place it in the queue of activities for this scanner
    445   1.1   mycroft 	 * at the end (a bit silly because we only have on user..
    446   1.1   mycroft 	 * (but it could fork()))
    447   1.1   mycroft 	 */
    448  1.39   hannken 	BUFQ_PUT(&ss->buf_queue, bp);
    449   1.1   mycroft 
    450   1.1   mycroft 	/*
    451   1.1   mycroft 	 * Tell the device to get going on the transfer if it's
    452   1.1   mycroft 	 * not doing anything, otherwise just wait for completion
    453   1.1   mycroft 	 * (All a bit silly if we're only allowing 1 open but..)
    454   1.1   mycroft 	 */
    455  1.34    bouyer 	ssstart(ss->sc_periph);
    456   1.1   mycroft 
    457   1.1   mycroft 	splx(s);
    458   1.1   mycroft 	return;
    459   1.1   mycroft done:
    460   1.1   mycroft 	/*
    461   1.1   mycroft 	 * Correctly set the buf to indicate a completed xfer
    462   1.1   mycroft 	 */
    463   1.1   mycroft 	bp->b_resid = bp->b_bcount;
    464   1.1   mycroft 	biodone(bp);
    465   1.1   mycroft }
    466   1.1   mycroft 
    467   1.1   mycroft /*
    468   1.1   mycroft  * ssstart looks to see if there is a buf waiting for the device
    469   1.1   mycroft  * and that the device is not already busy. If both are true,
    470   1.1   mycroft  * It dequeues the buf and creates a scsi command to perform the
    471  1.16    bouyer  * transfer required. The transfer request will call scsipi_done
    472   1.1   mycroft  * on completion, which will in turn call this routine again
    473   1.1   mycroft  * so that the next queued transfer is performed.
    474   1.1   mycroft  * The bufs are queued by the strategy routine (ssstrategy)
    475   1.1   mycroft  *
    476   1.1   mycroft  * This routine is also called after other non-queued requests
    477   1.1   mycroft  * have been made of the scsi driver, to ensure that the queue
    478   1.1   mycroft  * continues to be drained.
    479   1.1   mycroft  * ssstart() is called at splbio
    480   1.1   mycroft  */
    481  1.52   thorpej static void
    482  1.52   thorpej ssstart(struct scsipi_periph *periph)
    483   1.1   mycroft {
    484  1.34    bouyer 	struct ss_softc *ss = (void *)periph->periph_dev;
    485  1.30  augustss 	struct buf *bp;
    486   1.1   mycroft 
    487  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
    488   1.1   mycroft 	/*
    489   1.1   mycroft 	 * See if there is a buf to do and we are not already
    490   1.1   mycroft 	 * doing one
    491   1.1   mycroft 	 */
    492  1.34    bouyer 	while (periph->periph_active < periph->periph_openings) {
    493   1.1   mycroft 		/* if a special awaits, let it proceed first */
    494  1.34    bouyer 		if (periph->periph_flags & PERIPH_WAITING) {
    495  1.34    bouyer 			periph->periph_flags &= ~PERIPH_WAITING;
    496  1.34    bouyer 			wakeup((caddr_t)periph);
    497   1.1   mycroft 			return;
    498   1.1   mycroft 		}
    499   1.1   mycroft 
    500   1.1   mycroft 		/*
    501   1.1   mycroft 		 * See if there is a buf with work for us to do..
    502   1.1   mycroft 		 */
    503  1.53    bouyer 		if ((bp = BUFQ_PEEK(&ss->buf_queue)) == NULL)
    504   1.1   mycroft 			return;
    505   1.1   mycroft 
    506  1.21  explorer 		if (ss->special && ss->special->read) {
    507   1.1   mycroft 			(ss->special->read)(ss, bp);
    508   1.1   mycroft 		} else {
    509   1.1   mycroft 			/* generic scsi2 scanner read */
    510   1.1   mycroft 			/* XXX add code for SCSI2 scanner read */
    511   1.1   mycroft 		}
    512   1.1   mycroft 	}
    513   1.1   mycroft }
    514   1.1   mycroft 
    515  1.53    bouyer void
    516  1.53    bouyer ssrestart(void *v)
    517  1.53    bouyer {
    518  1.53    bouyer 	int s = splbio();
    519  1.53    bouyer 	ssstart((struct scsipi_periph *)v);
    520  1.53    bouyer 	splx(s);
    521  1.53    bouyer }
    522  1.53    bouyer 
    523  1.53    bouyer 
    524   1.1   mycroft /*
    525   1.1   mycroft  * Perform special action on behalf of the user;
    526   1.1   mycroft  * knows about the internals of this device
    527   1.1   mycroft  */
    528   1.1   mycroft int
    529  1.52   thorpej ssioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    530   1.1   mycroft {
    531   1.8   thorpej 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    532   1.1   mycroft 	int error = 0;
    533   1.1   mycroft 	struct scan_io *sio;
    534  1.32  augustss 
    535  1.32  augustss 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    536  1.32  augustss 		return (ENODEV);
    537   1.1   mycroft 
    538   1.1   mycroft 	switch (cmd) {
    539   1.1   mycroft 	case SCIOCGET:
    540  1.21  explorer 		if (ss->special && ss->special->get_params) {
    541   1.1   mycroft 			/* call special handler */
    542   1.3   mycroft 			error = (ss->special->get_params)(ss);
    543   1.3   mycroft 			if (error)
    544   1.1   mycroft 				return (error);
    545   1.1   mycroft 		} else {
    546   1.1   mycroft 			/* XXX add code for SCSI2 scanner, if any */
    547   1.1   mycroft 			return (EOPNOTSUPP);
    548   1.1   mycroft 		}
    549  1.36   thorpej 		memcpy(addr, &ss->sio, sizeof(struct scan_io));
    550   1.1   mycroft 		break;
    551   1.1   mycroft 	case SCIOCSET:
    552   1.1   mycroft 		sio = (struct scan_io *)addr;
    553   1.1   mycroft 
    554  1.21  explorer 		if (ss->special && ss->special->set_params) {
    555   1.1   mycroft 			/* call special handler */
    556   1.3   mycroft 			error = (ss->special->set_params)(ss, sio);
    557   1.3   mycroft 			if (error)
    558   1.1   mycroft 				return (error);
    559   1.1   mycroft 		} else {
    560   1.1   mycroft 			/* XXX add code for SCSI2 scanner, if any */
    561   1.1   mycroft 			return (EOPNOTSUPP);
    562   1.1   mycroft 		}
    563   1.1   mycroft 		break;
    564   1.1   mycroft 	case SCIOCRESTART:
    565  1.21  explorer 		if (ss->special && ss->special->rewind_scanner ) {
    566   1.1   mycroft 			/* call special handler */
    567   1.3   mycroft 			error = (ss->special->rewind_scanner)(ss);
    568   1.3   mycroft 			if (error)
    569   1.1   mycroft 				return (error);
    570   1.1   mycroft 		} else
    571   1.1   mycroft 			/* XXX add code for SCSI2 scanner, if any */
    572   1.1   mycroft 			return (EOPNOTSUPP);
    573   1.1   mycroft 		ss->flags &= ~SSF_TRIGGERED;
    574   1.1   mycroft 		break;
    575   1.1   mycroft #ifdef NOTYET
    576   1.1   mycroft 	case SCAN_USE_ADF:
    577   1.1   mycroft 		break;
    578   1.1   mycroft #endif
    579   1.1   mycroft 	default:
    580  1.34    bouyer 		return (scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr,
    581  1.49      fvdl 		    flag, p));
    582   1.1   mycroft 	}
    583   1.1   mycroft 	return (error);
    584   1.1   mycroft }
    585