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