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