Home | History | Annotate | Line # | Download | only in scsipi
ss.c revision 1.54
      1 /*	$NetBSD: ss.c,v 1.54 2004/09/17 23:10:52 mycroft 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.54 2004/09/17 23:10:52 mycroft 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/proc.h>
     44 #include <sys/user.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(struct device *, struct cfdata *, void *);
     72 static void	ssattach(struct device *, struct device *, void *);
     73 static int	ssdetach(struct device *self, int flags);
     74 static int	ssactivate(struct device *self, enum devact act);
     75 
     76 CFATTACH_DECL(ss, sizeof(struct ss_softc),
     77     ssmatch, ssattach, ssdetach, ssactivate);
     78 
     79 extern struct cfdriver ss_cd;
     80 
     81 static dev_type_open(ssopen);
     82 static dev_type_close(ssclose);
     83 static dev_type_read(ssread);
     84 static dev_type_ioctl(ssioctl);
     85 
     86 const struct cdevsw ss_cdevsw = {
     87 	ssopen, ssclose, ssread, nowrite, ssioctl,
     88 	nostop, notty, nopoll, nommap, nokqfilter,
     89 };
     90 
     91 static void	ssstrategy(struct buf *);
     92 static void	ssstart(struct scsipi_periph *);
     93 #if 0
     94 static void	ssrestart(void *));
     95 #endif
     96 static void	ssdone(struct scsipi_xfer *, int);
     97 static void	ssminphys(struct buf *);
     98 
     99 static const struct scsipi_periphsw ss_switch = {
    100 	NULL,
    101 	ssstart,
    102 	NULL,
    103 	ssdone,
    104 };
    105 
    106 static const struct scsipi_inquiry_pattern ss_patterns[] = {
    107 	{T_SCANNER, T_FIXED,
    108 	 "",         "",                 ""},
    109 	{T_SCANNER, T_REMOV,
    110 	 "",         "",                 ""},
    111 	{T_PROCESSOR, T_FIXED,
    112 	 "HP      ", "C1130A          ", ""},
    113 	{T_PROCESSOR, T_FIXED,
    114 	 "HP      ", "C1750A          ", ""},
    115 	{T_PROCESSOR, T_FIXED,
    116 	 "HP      ", "C2500A          ", ""},
    117 	{T_PROCESSOR, T_FIXED,
    118 	 "HP      ", "C2520A          ", ""},
    119 	{T_PROCESSOR, T_FIXED,
    120 	 "HP      ", "C5110A          ", ""},
    121 	{T_PROCESSOR, T_FIXED,
    122 	 "HP      ", "C7670A          ", ""},
    123 	{T_PROCESSOR, T_FIXED,
    124 	 "HP      ", "", ""},
    125 };
    126 
    127 static int
    128 ssmatch(struct device *parent, struct cfdata *match, void *aux)
    129 {
    130 	struct scsipibus_attach_args *sa = aux;
    131 	int priority;
    132 
    133 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    134 	    (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]),
    135 	    sizeof(ss_patterns[0]), &priority);
    136 	return (priority);
    137 }
    138 
    139 /*
    140  * The routine called by the low level scsi routine when it discovers
    141  * A device suitable for this driver
    142  * If it is a know special, call special attach routine to install
    143  * special handlers into the ss_softc structure
    144  */
    145 static void
    146 ssattach(struct device *parent, struct device *self, void *aux)
    147 {
    148 	struct ss_softc *ss = (void *)self;
    149 	struct scsipibus_attach_args *sa = aux;
    150 	struct scsipi_periph *periph = sa->sa_periph;
    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, BUFQ_FCFS);
    169 
    170 	callout_init(&ss->sc_callout);
    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 
    189 static int
    190 ssdetach(struct device *self, int flags)
    191 {
    192 	struct ss_softc *ss = (struct ss_softc *) self;
    193 	struct buf *bp;
    194 	int s, cmaj, mn;
    195 
    196 	/* locate the major number */
    197 	cmaj = cdevsw_lookup_major(&ss_cdevsw);
    198 
    199 	/* kill any pending restart */
    200 	callout_stop(&ss->sc_callout);
    201 
    202 	s = splbio();
    203 
    204 	/* Kill off any queued buffers. */
    205 	while ((bp = BUFQ_GET(&ss->buf_queue)) != NULL) {
    206 		bp->b_error = EIO;
    207 		bp->b_flags |= B_ERROR;
    208 		bp->b_resid = bp->b_bcount;
    209 		biodone(bp);
    210 	}
    211 
    212 	bufq_free(&ss->buf_queue);
    213 
    214 	/* Kill off any pending commands. */
    215 	scsipi_kill_pending(ss->sc_periph);
    216 
    217 	splx(s);
    218 
    219 	/* Nuke the vnodes for any open instances */
    220 	mn = SSUNIT(self->dv_unit);
    221 	vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR);
    222 
    223 	return (0);
    224 }
    225 
    226 static int
    227 ssactivate(struct device *self, enum devact act)
    228 {
    229 	int rv = 0;
    230 
    231 	switch (act) {
    232 	case DVACT_ACTIVATE:
    233 		rv = EOPNOTSUPP;
    234 		break;
    235 
    236 	case DVACT_DEACTIVATE:
    237 		/*
    238 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    239 		 */
    240 		break;
    241 	}
    242 	return (rv);
    243 }
    244 
    245 /*
    246  * open the device.
    247  */
    248 static int
    249 ssopen(dev_t dev, int flag, int mode, struct proc *p)
    250 {
    251 	int unit;
    252 	u_int ssmode;
    253 	int error;
    254 	struct ss_softc *ss;
    255 	struct scsipi_periph *periph;
    256 	struct scsipi_adapter *adapt;
    257 
    258 	unit = SSUNIT(dev);
    259 	if (unit >= ss_cd.cd_ndevs)
    260 		return (ENXIO);
    261 	ss = ss_cd.cd_devs[unit];
    262 	if (!ss)
    263 		return (ENXIO);
    264 
    265 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    266 		return (ENODEV);
    267 
    268 	ssmode = SSMODE(dev);
    269 
    270 	periph = ss->sc_periph;
    271 	adapt = periph->periph_channel->chan_adapter;
    272 
    273 	SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    274 	    unit, ss_cd.cd_ndevs));
    275 
    276 	if (periph->periph_flags & PERIPH_OPEN) {
    277 		printf("%s: already open\n", ss->sc_dev.dv_xname);
    278 		return (EBUSY);
    279 	}
    280 
    281 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    282 		return (error);
    283 
    284 	/*
    285 	 * Catch any unit attention errors.
    286 	 *
    287 	 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners
    288 	 * consider paper to be a changeable media
    289 	 *
    290 	 */
    291 	error = scsipi_test_unit_ready(periph,
    292 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    293 	    (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0));
    294 	if (error)
    295 		goto bad;
    296 
    297 	periph->periph_flags |= PERIPH_OPEN;	/* unit attn now errors */
    298 
    299 	/*
    300 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    301 	 * then the device has been opened to set defaults
    302 	 * This mode does NOT ALLOW I/O, only ioctls
    303 	 */
    304 	if (ssmode == MODE_CONTROL)
    305 		return (0);
    306 
    307 	SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
    308 	return (0);
    309 
    310 bad:
    311 	scsipi_adapter_delref(adapt);
    312 	periph->periph_flags &= ~PERIPH_OPEN;
    313 	return (error);
    314 }
    315 
    316 /*
    317  * close the device.. only called if we are the LAST
    318  * occurence of an open device
    319  */
    320 static int
    321 ssclose(dev_t dev, int flag, int mode, struct proc *p)
    322 {
    323 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    324 	struct scsipi_periph *periph = ss->sc_periph;
    325 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    326 	int error;
    327 
    328 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n"));
    329 
    330 	if (SSMODE(dev) == MODE_REWIND) {
    331 		if (ss->special && ss->special->rewind_scanner) {
    332 			/* call special handler to rewind/abort scan */
    333 			error = (ss->special->rewind_scanner)(ss);
    334 			if (error)
    335 				return (error);
    336 		} else {
    337 			/* XXX add code to restart a SCSI2 scanner, if any */
    338 		}
    339 		ss->sio.scan_window_size = 0;
    340 		ss->flags &= ~SSF_TRIGGERED;
    341 	}
    342 
    343 	scsipi_wait_drain(periph);
    344 
    345 	scsipi_adapter_delref(adapt);
    346 	periph->periph_flags &= ~PERIPH_OPEN;
    347 
    348 	return (0);
    349 }
    350 
    351 /*
    352  * trim the size of the transfer if needed,
    353  * called by physio
    354  * basically the smaller of our min and the scsi driver's
    355  * minphys
    356  */
    357 static void
    358 ssminphys(struct buf *bp)
    359 {
    360 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    361 	struct scsipi_periph *periph = ss->sc_periph;
    362 
    363 	scsipi_adapter_minphys(periph->periph_channel, bp);
    364 
    365 	/*
    366 	 * trim the transfer further for special devices this is
    367 	 * because some scanners only read multiples of a line at a
    368 	 * time, also some cannot disconnect, so the read must be
    369 	 * short enough to happen quickly
    370 	 */
    371 	if (ss->special && ss->special->minphys)
    372 		(ss->special->minphys)(ss, bp);
    373 }
    374 
    375 /*
    376  * Do a read on a device for a user process.
    377  * Prime scanner at start of read, check uio values, call ssstrategy
    378  * via physio for the actual transfer.
    379  */
    380 static int
    381 ssread(dev_t dev, struct uio *uio, int flag)
    382 {
    383 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    384 	int error;
    385 
    386 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    387 		return (ENODEV);
    388 
    389 	/* if the scanner has not yet been started, do it now */
    390 	if (!(ss->flags & SSF_TRIGGERED)) {
    391 		if (ss->special && ss->special->trigger_scanner) {
    392 			error = (ss->special->trigger_scanner)(ss);
    393 			if (error)
    394 				return (error);
    395 		}
    396 		ss->flags |= SSF_TRIGGERED;
    397 	}
    398 
    399 	return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio));
    400 }
    401 
    402 /*
    403  * Actually translate the requested transfer into one the physical
    404  * driver can understand The transfer is described by a buf and will
    405  * include only one physical transfer.
    406  */
    407 static void
    408 ssstrategy(struct buf *bp)
    409 {
    410 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    411 	struct scsipi_periph *periph = ss->sc_periph;
    412 	int s;
    413 
    414 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
    415 	    ("ssstrategy %ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    416 
    417 	/*
    418 	 * If the device has been made invalid, error out
    419 	 */
    420 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    421 		bp->b_flags |= B_ERROR;
    422 		if (periph->periph_flags & PERIPH_OPEN)
    423 			bp->b_error = EIO;
    424 		else
    425 			bp->b_error = ENODEV;
    426 		goto done;
    427 	}
    428 
    429 	/* If negative offset, error */
    430 	if (bp->b_blkno < 0) {
    431 		bp->b_flags |= B_ERROR;
    432 		bp->b_error = EINVAL;
    433 		goto done;
    434 	}
    435 
    436 	if (bp->b_bcount > ss->sio.scan_window_size)
    437 		bp->b_bcount = ss->sio.scan_window_size;
    438 
    439 	/*
    440 	 * If it's a null transfer, return immediatly
    441 	 */
    442 	if (bp->b_bcount == 0)
    443 		goto done;
    444 
    445 	s = splbio();
    446 
    447 	/*
    448 	 * Place it in the queue of activities for this scanner
    449 	 * at the end (a bit silly because we only have on user..
    450 	 * (but it could fork()))
    451 	 */
    452 	BUFQ_PUT(&ss->buf_queue, bp);
    453 
    454 	/*
    455 	 * Tell the device to get going on the transfer if it's
    456 	 * not doing anything, otherwise just wait for completion
    457 	 * (All a bit silly if we're only allowing 1 open but..)
    458 	 */
    459 	ssstart(ss->sc_periph);
    460 
    461 	splx(s);
    462 	return;
    463 done:
    464 	/*
    465 	 * Correctly set the buf to indicate a completed xfer
    466 	 */
    467 	bp->b_resid = bp->b_bcount;
    468 	biodone(bp);
    469 }
    470 
    471 /*
    472  * ssstart looks to see if there is a buf waiting for the device
    473  * and that the device is not already busy. If both are true,
    474  * It dequeues the buf and creates a scsi command to perform the
    475  * transfer required. The transfer request will call scsipi_done
    476  * on completion, which will in turn call this routine again
    477  * so that the next queued transfer is performed.
    478  * The bufs are queued by the strategy routine (ssstrategy)
    479  *
    480  * This routine is also called after other non-queued requests
    481  * have been made of the scsi driver, to ensure that the queue
    482  * continues to be drained.
    483  * ssstart() is called at splbio
    484  */
    485 static void
    486 ssstart(struct scsipi_periph *periph)
    487 {
    488 	struct ss_softc *ss = (void *)periph->periph_dev;
    489 	struct buf *bp;
    490 
    491 	SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
    492 	/*
    493 	 * See if there is a buf to do and we are not already
    494 	 * doing one
    495 	 */
    496 	while (periph->periph_active < periph->periph_openings) {
    497 		/* if a special awaits, let it proceed first */
    498 		if (periph->periph_flags & PERIPH_WAITING) {
    499 			periph->periph_flags &= ~PERIPH_WAITING;
    500 			wakeup((caddr_t)periph);
    501 			return;
    502 		}
    503 
    504 		/*
    505 		 * See if there is a buf with work for us to do..
    506 		 */
    507 		if ((bp = BUFQ_PEEK(&ss->buf_queue)) == NULL)
    508 			return;
    509 
    510 		if (ss->special && ss->special->read) {
    511 			(ss->special->read)(ss, bp);
    512 		} else {
    513 			/* generic scsi2 scanner read */
    514 			/* XXX add code for SCSI2 scanner read */
    515 		}
    516 	}
    517 }
    518 
    519 #if 0
    520 static void
    521 ssrestart(void *v)
    522 {
    523 	int s = splbio();
    524 	ssstart((struct scsipi_periph *)v);
    525 	splx(s);
    526 }
    527 #endif
    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 		if (error)
    538 			bp->b_flags |= B_ERROR;
    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, caddr_t addr, int flag, struct proc *p)
    550 {
    551 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    552 	int error = 0;
    553 	struct scan_io *sio;
    554 
    555 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    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, p));
    602 	}
    603 	return (error);
    604 }
    605