Home | History | Annotate | Line # | Download | only in scsipi
ss.c revision 1.51.2.1
      1 /*	$NetBSD: ss.c,v 1.51.2.1 2004/09/11 12:56:41 he 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.51.2.1 2004/09/11 12:56:41 he 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 int ssmatch(struct device *, struct cfdata *, void *);
     72 void ssattach(struct device *, struct device *, void *);
     73 int ssdetach(struct device *self, int flags);
     74 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 dev_type_open(ssopen);
     82 dev_type_close(ssclose);
     83 dev_type_read(ssread);
     84 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 void    ssstrategy __P((struct buf *));
     92 void    ssstart __P((struct scsipi_periph *));
     93 void	ssminphys __P((struct buf *));
     94 
     95 const struct scsipi_periphsw ss_switch = {
     96 	NULL,
     97 	ssstart,
     98 	NULL,
     99 	NULL,
    100 };
    101 
    102 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 int
    124 ssmatch(struct device *parent, struct cfdata *match, void *aux)
    125 {
    126 	struct scsipibus_attach_args *sa = aux;
    127 	int priority;
    128 
    129 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    130 	    (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]),
    131 	    sizeof(ss_patterns[0]), &priority);
    132 	return (priority);
    133 }
    134 
    135 /*
    136  * The routine called by the low level scsi routine when it discovers
    137  * A device suitable for this driver
    138  * If it is a know special, call special attach routine to install
    139  * special handlers into the ss_softc structure
    140  */
    141 void
    142 ssattach(struct device *parent, struct device *self, void *aux)
    143 {
    144 	struct ss_softc *ss = (void *)self;
    145 	struct scsipibus_attach_args *sa = aux;
    146 	struct scsipi_periph *periph = sa->sa_periph;
    147 
    148 	SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: "));
    149 
    150 	ss->flags |= SSF_AUTOCONF;
    151 
    152 	/*
    153 	 * Store information needed to contact our base driver
    154 	 */
    155 	ss->sc_periph = periph;
    156 	periph->periph_dev = &ss->sc_dev;
    157 	periph->periph_switch = &ss_switch;
    158 
    159 	printf("\n");
    160 
    161 	/*
    162 	 * Set up the buf queue for this device
    163 	 */
    164 	bufq_alloc(&ss->buf_queue, BUFQ_FCFS);
    165 
    166 	callout_init(&ss->sc_callout);
    167 
    168 	/*
    169 	 * look for non-standard scanners with help of the quirk table
    170 	 * and install functions for special handling
    171 	 */
    172 	SC_DEBUG(periph, SCSIPI_DB2, ("ssattach:\n"));
    173 	if (memcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6) == 0)
    174 		mustek_attach(ss, sa);
    175 	if (memcmp(sa->sa_inqbuf.vendor, "HP      ", 8) == 0 &&
    176 	    memcmp(sa->sa_inqbuf.product, "ScanJet 5300C", 13) != 0)
    177 		scanjet_attach(ss, sa);
    178 	if (ss->special == NULL) {
    179 		/* XXX add code to restart a SCSI2 scanner, if any */
    180 	}
    181 
    182 	ss->flags &= ~SSF_AUTOCONF;
    183 }
    184 
    185 int
    186 ssdetach(struct device *self, int flags)
    187 {
    188 	struct ss_softc *ss = (struct ss_softc *) self;
    189 	struct buf *bp;
    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 	while ((bp = BUFQ_GET(&ss->buf_queue)) != NULL) {
    202 		bp->b_error = EIO;
    203 		bp->b_flags |= B_ERROR;
    204 		bp->b_resid = bp->b_bcount;
    205 		biodone(bp);
    206 	}
    207 
    208 	bufq_free(&ss->buf_queue);
    209 
    210 	/* Kill off any pending commands. */
    211 	scsipi_kill_pending(ss->sc_periph);
    212 
    213 	splx(s);
    214 
    215 	/* Nuke the vnodes for any open instances */
    216 	mn = SSUNIT(self->dv_unit);
    217 	vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR);
    218 
    219 	return (0);
    220 }
    221 
    222 int
    223 ssactivate(struct device *self, enum devact act)
    224 {
    225 	int rv = 0;
    226 
    227 	switch (act) {
    228 	case DVACT_ACTIVATE:
    229 		rv = EOPNOTSUPP;
    230 		break;
    231 
    232 	case DVACT_DEACTIVATE:
    233 		/*
    234 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    235 		 */
    236 		break;
    237 	}
    238 	return (rv);
    239 }
    240 
    241 /*
    242  * open the device.
    243  */
    244 int
    245 ssopen(dev_t dev, int flag, int mode, struct proc *p)
    246 {
    247 	int unit;
    248 	u_int ssmode;
    249 	int error;
    250 	struct ss_softc *ss;
    251 	struct scsipi_periph *periph;
    252 	struct scsipi_adapter *adapt;
    253 
    254 	unit = SSUNIT(dev);
    255 	if (unit >= ss_cd.cd_ndevs)
    256 		return (ENXIO);
    257 	ss = ss_cd.cd_devs[unit];
    258 	if (!ss)
    259 		return (ENXIO);
    260 
    261 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    262 		return (ENODEV);
    263 
    264 	ssmode = SSMODE(dev);
    265 
    266 	periph = ss->sc_periph;
    267 	adapt = periph->periph_channel->chan_adapter;
    268 
    269 	SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    270 	    unit, ss_cd.cd_ndevs));
    271 
    272 	if (periph->periph_flags & PERIPH_OPEN) {
    273 		printf("%s: already open\n", ss->sc_dev.dv_xname);
    274 		return (EBUSY);
    275 	}
    276 
    277 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    278 		return (error);
    279 
    280 	/*
    281 	 * Catch any unit attention errors.
    282 	 *
    283 	 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners
    284 	 * consider paper to be a changeable media
    285 	 *
    286 	 */
    287 	error = scsipi_test_unit_ready(periph,
    288 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    289 	    (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0));
    290 	if (error)
    291 		goto bad;
    292 
    293 	periph->periph_flags |= PERIPH_OPEN;	/* unit attn now errors */
    294 
    295 	/*
    296 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    297 	 * then the device has been opened to set defaults
    298 	 * This mode does NOT ALLOW I/O, only ioctls
    299 	 */
    300 	if (ssmode == MODE_CONTROL)
    301 		return (0);
    302 
    303 	SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
    304 	return (0);
    305 
    306 bad:
    307 	scsipi_adapter_delref(adapt);
    308 	periph->periph_flags &= ~PERIPH_OPEN;
    309 	return (error);
    310 }
    311 
    312 /*
    313  * close the device.. only called if we are the LAST
    314  * occurence of an open device
    315  */
    316 int
    317 ssclose(dev_t dev, int flag, int mode, struct proc *p)
    318 {
    319 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    320 	struct scsipi_periph *periph = ss->sc_periph;
    321 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    322 	int error;
    323 
    324 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n"));
    325 
    326 	if (SSMODE(dev) == MODE_REWIND) {
    327 		if (ss->special && ss->special->rewind_scanner) {
    328 			/* call special handler to rewind/abort scan */
    329 			error = (ss->special->rewind_scanner)(ss);
    330 			if (error)
    331 				return (error);
    332 		} else {
    333 			/* XXX add code to restart a SCSI2 scanner, if any */
    334 		}
    335 		ss->sio.scan_window_size = 0;
    336 		ss->flags &= ~SSF_TRIGGERED;
    337 	}
    338 
    339 	scsipi_wait_drain(periph);
    340 
    341 	scsipi_adapter_delref(adapt);
    342 	periph->periph_flags &= ~PERIPH_OPEN;
    343 
    344 	return (0);
    345 }
    346 
    347 /*
    348  * trim the size of the transfer if needed,
    349  * called by physio
    350  * basically the smaller of our min and the scsi driver's
    351  * minphys
    352  */
    353 void
    354 ssminphys(struct buf *bp)
    355 {
    356 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
    357 	struct scsipi_periph *periph = ss->sc_periph;
    358 
    359 	scsipi_adapter_minphys(periph->periph_channel, bp);
    360 
    361 	/*
    362 	 * trim the transfer further for special devices this is
    363 	 * because some scanners only read multiples of a line at a
    364 	 * time, also some cannot disconnect, so the read must be
    365 	 * short enough to happen quickly
    366 	 */
    367 	if (ss->special && ss->special->minphys)
    368 		(ss->special->minphys)(ss, bp);
    369 }
    370 
    371 /*
    372  * Do a read on a device for a user process.
    373  * Prime scanner at start of read, check uio values, call ssstrategy
    374  * via physio for the actual transfer.
    375  */
    376 int
    377 ssread(dev, uio, flag)
    378 	dev_t dev;
    379 	struct uio *uio;
    380 	int flag;
    381 {
    382 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    383 	int error;
    384 
    385 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    386 		return (ENODEV);
    387 
    388 	/* if the scanner has not yet been started, do it now */
    389 	if (!(ss->flags & SSF_TRIGGERED)) {
    390 		if (ss->special && ss->special->trigger_scanner) {
    391 			error = (ss->special->trigger_scanner)(ss);
    392 			if (error)
    393 				return (error);
    394 		}
    395 		ss->flags |= SSF_TRIGGERED;
    396 	}
    397 
    398 	return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio));
    399 }
    400 
    401 /*
    402  * Actually translate the requested transfer into one the physical
    403  * driver can understand The transfer is described by a buf and will
    404  * include only one physical transfer.
    405  */
    406 void
    407 ssstrategy(bp)
    408 	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 void
    486 ssstart(periph)
    487 	struct scsipi_periph *periph;
    488 {
    489 	struct ss_softc *ss = (void *)periph->periph_dev;
    490 	struct buf *bp;
    491 
    492 	SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
    493 	/*
    494 	 * See if there is a buf to do and we are not already
    495 	 * doing one
    496 	 */
    497 	while (periph->periph_active < periph->periph_openings) {
    498 		/* if a special awaits, let it proceed first */
    499 		if (periph->periph_flags & PERIPH_WAITING) {
    500 			periph->periph_flags &= ~PERIPH_WAITING;
    501 			wakeup((caddr_t)periph);
    502 			return;
    503 		}
    504 
    505 		/*
    506 		 * See if there is a buf with work for us to do..
    507 		 */
    508 		if ((bp = BUFQ_PEEK(&ss->buf_queue)) == NULL)
    509 			return;
    510 
    511 		if (ss->special && ss->special->read) {
    512 			(ss->special->read)(ss, bp);
    513 		} else {
    514 			/* generic scsi2 scanner read */
    515 			/* XXX add code for SCSI2 scanner read */
    516 		}
    517 	}
    518 }
    519 
    520 void
    521 ssrestart(void *v)
    522 {
    523 	int s = splbio();
    524 	ssstart((struct scsipi_periph *)v);
    525 	splx(s);
    526 }
    527 
    528 
    529 /*
    530  * Perform special action on behalf of the user;
    531  * knows about the internals of this device
    532  */
    533 int
    534 ssioctl(dev, cmd, addr, flag, p)
    535 	dev_t dev;
    536 	u_long cmd;
    537 	caddr_t addr;
    538 	int flag;
    539 	struct proc *p;
    540 {
    541 	struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
    542 	int error = 0;
    543 	struct scan_io *sio;
    544 
    545 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    546 		return (ENODEV);
    547 
    548 	switch (cmd) {
    549 	case SCIOCGET:
    550 		if (ss->special && ss->special->get_params) {
    551 			/* call special handler */
    552 			error = (ss->special->get_params)(ss);
    553 			if (error)
    554 				return (error);
    555 		} else {
    556 			/* XXX add code for SCSI2 scanner, if any */
    557 			return (EOPNOTSUPP);
    558 		}
    559 		memcpy(addr, &ss->sio, sizeof(struct scan_io));
    560 		break;
    561 	case SCIOCSET:
    562 		sio = (struct scan_io *)addr;
    563 
    564 		if (ss->special && ss->special->set_params) {
    565 			/* call special handler */
    566 			error = (ss->special->set_params)(ss, sio);
    567 			if (error)
    568 				return (error);
    569 		} else {
    570 			/* XXX add code for SCSI2 scanner, if any */
    571 			return (EOPNOTSUPP);
    572 		}
    573 		break;
    574 	case SCIOCRESTART:
    575 		if (ss->special && ss->special->rewind_scanner ) {
    576 			/* call special handler */
    577 			error = (ss->special->rewind_scanner)(ss);
    578 			if (error)
    579 				return (error);
    580 		} else
    581 			/* XXX add code for SCSI2 scanner, if any */
    582 			return (EOPNOTSUPP);
    583 		ss->flags &= ~SSF_TRIGGERED;
    584 		break;
    585 #ifdef NOTYET
    586 	case SCAN_USE_ADF:
    587 		break;
    588 #endif
    589 	default:
    590 		return (scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr,
    591 		    flag, p));
    592 	}
    593 	return (error);
    594 }
    595