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