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