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