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