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