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