Home | History | Annotate | Line # | Download | only in scsipi
ss.c revision 1.72.26.3
      1 /*	$NetBSD: ss.c,v 1.72.26.3 2008/06/02 13:23:51 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.3 2008/06/02 13:23:51 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 	if (unit >= ss_cd.cd_ndevs)
    266 		return (ENXIO);
    267 	ss = ss_cd.cd_devs[unit];
    268 	if (!ss)
    269 		return (ENXIO);
    270 
    271 	if (!device_is_active(&ss->sc_dev))
    272 		return (ENODEV);
    273 
    274 	ssmode = SSMODE(dev);
    275 
    276 	periph = ss->sc_periph;
    277 	adapt = periph->periph_channel->chan_adapter;
    278 
    279 	SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    280 	    unit, ss_cd.cd_ndevs));
    281 
    282 	if (periph->periph_flags & PERIPH_OPEN) {
    283 		aprint_error_dev(&ss->sc_dev, "already open\n");
    284 		return (EBUSY);
    285 	}
    286 
    287 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    288 		return (error);
    289 
    290 	/*
    291 	 * Catch any unit attention errors.
    292 	 *
    293 	 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners
    294 	 * consider paper to be a changeable media
    295 	 *
    296 	 */
    297 	error = scsipi_test_unit_ready(periph,
    298 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    299 	    (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0));
    300 	if (error)
    301 		goto bad;
    302 
    303 	periph->periph_flags |= PERIPH_OPEN;	/* unit attn now errors */
    304 
    305 	/*
    306 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    307 	 * then the device has been opened to set defaults
    308 	 * This mode does NOT ALLOW I/O, only ioctls
    309 	 */
    310 	if (ssmode == MODE_CONTROL)
    311 		return (0);
    312 
    313 	SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
    314 	return (0);
    315 
    316 bad:
    317 	scsipi_adapter_delref(adapt);
    318 	periph->periph_flags &= ~PERIPH_OPEN;
    319 	return (error);
    320 }
    321 
    322 /*
    323  * close the device.. only called if we are the LAST
    324  * occurence of an open device
    325  */
    326 static int
    327 ssclose(dev_t dev, int flag, int mode, struct lwp *l)
    328 {
    329 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    330 	struct scsipi_periph *periph = ss->sc_periph;
    331 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    332 	int error;
    333 
    334 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n"));
    335 
    336 	if (SSMODE(dev) == MODE_REWIND) {
    337 		if (ss->special && ss->special->rewind_scanner) {
    338 			/* call special handler to rewind/abort scan */
    339 			error = (ss->special->rewind_scanner)(ss);
    340 			if (error)
    341 				return (error);
    342 		} else {
    343 			/* XXX add code to restart a SCSI2 scanner, if any */
    344 		}
    345 		ss->sio.scan_window_size = 0;
    346 		ss->flags &= ~SSF_TRIGGERED;
    347 	}
    348 
    349 	scsipi_wait_drain(periph);
    350 
    351 	scsipi_adapter_delref(adapt);
    352 	periph->periph_flags &= ~PERIPH_OPEN;
    353 
    354 	return (0);
    355 }
    356 
    357 /*
    358  * trim the size of the transfer if needed,
    359  * called by physio
    360  * basically the smaller of our min and the scsi driver's
    361  * minphys
    362  */
    363 static void
    364 ssminphys(struct buf *bp)
    365 {
    366 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    367 	struct scsipi_periph *periph = ss->sc_periph;
    368 
    369 	scsipi_adapter_minphys(periph->periph_channel, bp);
    370 
    371 	/*
    372 	 * trim the transfer further for special devices this is
    373 	 * because some scanners only read multiples of a line at a
    374 	 * time, also some cannot disconnect, so the read must be
    375 	 * short enough to happen quickly
    376 	 */
    377 	if (ss->special && ss->special->minphys)
    378 		(ss->special->minphys)(ss, bp);
    379 }
    380 
    381 /*
    382  * Do a read on a device for a user process.
    383  * Prime scanner at start of read, check uio values, call ssstrategy
    384  * via physio for the actual transfer.
    385  */
    386 static int
    387 ssread(dev_t dev, struct uio *uio, int flag)
    388 {
    389 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    390 	int error;
    391 
    392 	if (!device_is_active(&ss->sc_dev))
    393 		return (ENODEV);
    394 
    395 	/* if the scanner has not yet been started, do it now */
    396 	if (!(ss->flags & SSF_TRIGGERED)) {
    397 		if (ss->special && ss->special->trigger_scanner) {
    398 			error = (ss->special->trigger_scanner)(ss);
    399 			if (error)
    400 				return (error);
    401 		}
    402 		ss->flags |= SSF_TRIGGERED;
    403 	}
    404 
    405 	return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio));
    406 }
    407 
    408 /*
    409  * Actually translate the requested transfer into one the physical
    410  * driver can understand The transfer is described by a buf and will
    411  * include only one physical transfer.
    412  */
    413 static void
    414 ssstrategy(struct buf *bp)
    415 {
    416 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    417 	struct scsipi_periph *periph = ss->sc_periph;
    418 	int s;
    419 
    420 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
    421 	    ("ssstrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    422 
    423 	/*
    424 	 * If the device has been made invalid, error out
    425 	 */
    426 	if (!device_is_active(&ss->sc_dev)) {
    427 		if (periph->periph_flags & PERIPH_OPEN)
    428 			bp->b_error = EIO;
    429 		else
    430 			bp->b_error = ENODEV;
    431 		goto done;
    432 	}
    433 
    434 	/* If negative offset, error */
    435 	if (bp->b_blkno < 0) {
    436 		bp->b_error = EINVAL;
    437 		goto done;
    438 	}
    439 
    440 	if (bp->b_bcount > ss->sio.scan_window_size)
    441 		bp->b_bcount = ss->sio.scan_window_size;
    442 
    443 	/*
    444 	 * If it's a null transfer, return immediatly
    445 	 */
    446 	if (bp->b_bcount == 0)
    447 		goto done;
    448 
    449 	s = splbio();
    450 
    451 	/*
    452 	 * Place it in the queue of activities for this scanner
    453 	 * at the end (a bit silly because we only have on user..
    454 	 * (but it could fork()))
    455 	 */
    456 	BUFQ_PUT(ss->buf_queue, bp);
    457 
    458 	/*
    459 	 * Tell the device to get going on the transfer if it's
    460 	 * not doing anything, otherwise just wait for completion
    461 	 * (All a bit silly if we're only allowing 1 open but..)
    462 	 */
    463 	ssstart(ss->sc_periph);
    464 
    465 	splx(s);
    466 	return;
    467 done:
    468 	/*
    469 	 * Correctly set the buf to indicate a completed xfer
    470 	 */
    471 	bp->b_resid = bp->b_bcount;
    472 	biodone(bp);
    473 }
    474 
    475 /*
    476  * ssstart looks to see if there is a buf waiting for the device
    477  * and that the device is not already busy. If both are true,
    478  * It dequeues the buf and creates a scsi command to perform the
    479  * transfer required. The transfer request will call scsipi_done
    480  * on completion, which will in turn call this routine again
    481  * so that the next queued transfer is performed.
    482  * The bufs are queued by the strategy routine (ssstrategy)
    483  *
    484  * This routine is also called after other non-queued requests
    485  * have been made of the scsi driver, to ensure that the queue
    486  * continues to be drained.
    487  * ssstart() is called at splbio
    488  */
    489 static void
    490 ssstart(struct scsipi_periph *periph)
    491 {
    492 	struct ss_softc *ss = (void *)periph->periph_dev;
    493 	struct buf *bp;
    494 
    495 	SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
    496 	/*
    497 	 * See if there is a buf to do and we are not already
    498 	 * doing one
    499 	 */
    500 	while (periph->periph_active < periph->periph_openings) {
    501 		/* if a special awaits, let it proceed first */
    502 		if (periph->periph_flags & PERIPH_WAITING) {
    503 			periph->periph_flags &= ~PERIPH_WAITING;
    504 			wakeup((void *)periph);
    505 			return;
    506 		}
    507 
    508 		/*
    509 		 * See if there is a buf with work for us to do..
    510 		 */
    511 		if ((bp = BUFQ_PEEK(ss->buf_queue)) == NULL)
    512 			return;
    513 
    514 		if (ss->special && ss->special->read) {
    515 			(ss->special->read)(ss, bp);
    516 		} else {
    517 			/* generic scsi2 scanner read */
    518 			/* XXX add code for SCSI2 scanner read */
    519 		}
    520 	}
    521 }
    522 
    523 void
    524 ssrestart(void *v)
    525 {
    526 	int s = splbio();
    527 	ssstart((struct scsipi_periph *)v);
    528 	splx(s);
    529 }
    530 
    531 static void
    532 ssdone(struct scsipi_xfer *xs, int error)
    533 {
    534 	struct buf *bp = xs->bp;
    535 
    536 	if (bp) {
    537 		bp->b_error = error;
    538 		bp->b_resid = xs->resid;
    539 		biodone(bp);
    540 	}
    541 }
    542 
    543 
    544 /*
    545  * Perform special action on behalf of the user;
    546  * knows about the internals of this device
    547  */
    548 int
    549 ssioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    550 {
    551 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    552 	int error = 0;
    553 	struct scan_io *sio;
    554 
    555 	if (!device_is_active(&ss->sc_dev))
    556 		return (ENODEV);
    557 
    558 	switch (cmd) {
    559 	case SCIOCGET:
    560 		if (ss->special && ss->special->get_params) {
    561 			/* call special handler */
    562 			error = (ss->special->get_params)(ss);
    563 			if (error)
    564 				return (error);
    565 		} else {
    566 			/* XXX add code for SCSI2 scanner, if any */
    567 			return (EOPNOTSUPP);
    568 		}
    569 		memcpy(addr, &ss->sio, sizeof(struct scan_io));
    570 		break;
    571 	case SCIOCSET:
    572 		sio = (struct scan_io *)addr;
    573 
    574 		if (ss->special && ss->special->set_params) {
    575 			/* call special handler */
    576 			error = (ss->special->set_params)(ss, sio);
    577 			if (error)
    578 				return (error);
    579 		} else {
    580 			/* XXX add code for SCSI2 scanner, if any */
    581 			return (EOPNOTSUPP);
    582 		}
    583 		break;
    584 	case SCIOCRESTART:
    585 		if (ss->special && ss->special->rewind_scanner ) {
    586 			/* call special handler */
    587 			error = (ss->special->rewind_scanner)(ss);
    588 			if (error)
    589 				return (error);
    590 		} else
    591 			/* XXX add code for SCSI2 scanner, if any */
    592 			return (EOPNOTSUPP);
    593 		ss->flags &= ~SSF_TRIGGERED;
    594 		break;
    595 #ifdef NOTYET
    596 	case SCAN_USE_ADF:
    597 		break;
    598 #endif
    599 	default:
    600 		return (scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr,
    601 		    flag, l));
    602 	}
    603 	return (error);
    604 }
    605