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