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