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