Home | History | Annotate | Line # | Download | only in scsipi
ss.c revision 1.36.2.2
      1 /*	$NetBSD: ss.c,v 1.36.2.2 2001/09/26 15:28:18 fvdl 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(vdev_rdev(devvp));
    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 	vdev_setprivdata(devvp, ss);
    244 
    245 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    246 		return (ENODEV);
    247 
    248 	ssmode = SSMODE(vdev_rdev(devvp));
    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;
    304 	struct scsipi_periph *periph;
    305 	struct scsipi_adapter *adapt;
    306 	int error;
    307 
    308 	ss = vdev_privdata(devvp);
    309 	periph = ss->sc_periph;
    310 	adapt = periph->periph_channel->chan_adapter;
    311 
    312 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n"));
    313 
    314 	if (SSMODE(vdev_rdev(devvp)) == MODE_REWIND) {
    315 		if (ss->special && ss->special->rewind_scanner) {
    316 			/* call special handler to rewind/abort scan */
    317 			error = (ss->special->rewind_scanner)(ss);
    318 			if (error)
    319 				return (error);
    320 		} else {
    321 			/* XXX add code to restart a SCSI2 scanner, if any */
    322 		}
    323 		ss->sio.scan_window_size = 0;
    324 		ss->flags &= ~SSF_TRIGGERED;
    325 	}
    326 
    327 	scsipi_wait_drain(periph);
    328 
    329 	scsipi_adapter_delref(adapt);
    330 	periph->periph_flags &= ~PERIPH_OPEN;
    331 
    332 	return (0);
    333 }
    334 
    335 /*
    336  * trim the size of the transfer if needed,
    337  * called by physio
    338  * basically the smaller of our min and the scsi driver's
    339  * minphys
    340  */
    341 void
    342 ssminphys(struct buf *bp)
    343 {
    344 	struct ss_softc *ss;
    345 	struct scsipi_periph *periph;
    346 
    347 	ss = vdev_privdata(bp->b_devvp);
    348 	periph = ss->sc_periph;
    349 
    350 	(*periph->periph_channel->chan_adapter->adapt_minphys)(bp);
    351 
    352 	/*
    353 	 * trim the transfer further for special devices this is
    354 	 * because some scanners only read multiples of a line at a
    355 	 * time, also some cannot disconnect, so the read must be
    356 	 * short enough to happen quickly
    357 	 */
    358 	if (ss->special && ss->special->minphys)
    359 		(ss->special->minphys)(ss, bp);
    360 }
    361 
    362 /*
    363  * Do a read on a device for a user process.
    364  * Prime scanner at start of read, check uio values, call ssstrategy
    365  * via physio for the actual transfer.
    366  */
    367 int
    368 ssread(devvp, uio, flag)
    369 	struct vnode *devvp;
    370 	struct uio *uio;
    371 	int flag;
    372 {
    373 	struct ss_softc *ss;
    374 	int error;
    375 
    376 	ss = vdev_privdata(devvp);
    377 
    378 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    379 		return (ENODEV);
    380 
    381 	/* if the scanner has not yet been started, do it now */
    382 	if (!(ss->flags & SSF_TRIGGERED)) {
    383 		if (ss->special && ss->special->trigger_scanner) {
    384 			error = (ss->special->trigger_scanner)(ss);
    385 			if (error)
    386 				return (error);
    387 		}
    388 		ss->flags |= SSF_TRIGGERED;
    389 	}
    390 
    391 	return (physio(ssstrategy, NULL, devvp, B_READ, ssminphys, uio));
    392 }
    393 
    394 /*
    395  * Actually translate the requested transfer into one the physical
    396  * driver can understand The transfer is described by a buf and will
    397  * include only one physical transfer.
    398  */
    399 void
    400 ssstrategy(bp)
    401 	struct buf *bp;
    402 {
    403 	struct ss_softc *ss;
    404 	struct scsipi_periph *periph;
    405 	int s;
    406 
    407 	ss = vdev_privdata(bp->b_devvp);
    408 	periph = ss->sc_periph;
    409 
    410 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
    411 	    ("ssstrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    412 
    413 	/*
    414 	 * If the device has been made invalid, error out
    415 	 */
    416 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    417 		bp->b_flags |= B_ERROR;
    418 		if (periph->periph_flags & PERIPH_OPEN)
    419 			bp->b_error = EIO;
    420 		else
    421 			bp->b_error = ENODEV;
    422 		goto done;
    423 	}
    424 
    425 	/* If negative offset, error */
    426 	if (bp->b_blkno < 0) {
    427 		bp->b_flags |= B_ERROR;
    428 		bp->b_error = EINVAL;
    429 		goto done;
    430 	}
    431 
    432 	if (bp->b_bcount > ss->sio.scan_window_size)
    433 		bp->b_bcount = ss->sio.scan_window_size;
    434 
    435 	/*
    436 	 * If it's a null transfer, return immediatly
    437 	 */
    438 	if (bp->b_bcount == 0)
    439 		goto done;
    440 
    441 	s = splbio();
    442 
    443 	/*
    444 	 * Place it in the queue of activities for this scanner
    445 	 * at the end (a bit silly because we only have on user..
    446 	 * (but it could fork()))
    447 	 */
    448 	BUFQ_INSERT_TAIL(&ss->buf_queue, bp);
    449 
    450 	/*
    451 	 * Tell the device to get going on the transfer if it's
    452 	 * not doing anything, otherwise just wait for completion
    453 	 * (All a bit silly if we're only allowing 1 open but..)
    454 	 */
    455 	ssstart(ss->sc_periph);
    456 
    457 	splx(s);
    458 	return;
    459 	bp->b_flags |= B_ERROR;
    460 done:
    461 	/*
    462 	 * Correctly set the buf to indicate a completed xfer
    463 	 */
    464 	bp->b_resid = bp->b_bcount;
    465 	biodone(bp);
    466 }
    467 
    468 /*
    469  * ssstart looks to see if there is a buf waiting for the device
    470  * and that the device is not already busy. If both are true,
    471  * It dequeues the buf and creates a scsi command to perform the
    472  * transfer required. The transfer request will call scsipi_done
    473  * on completion, which will in turn call this routine again
    474  * so that the next queued transfer is performed.
    475  * The bufs are queued by the strategy routine (ssstrategy)
    476  *
    477  * This routine is also called after other non-queued requests
    478  * have been made of the scsi driver, to ensure that the queue
    479  * continues to be drained.
    480  * ssstart() is called at splbio
    481  */
    482 void
    483 ssstart(periph)
    484 	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_FIRST(&ss->buf_queue)) == NULL)
    506 			return;
    507 		BUFQ_REMOVE(&ss->buf_queue, bp);
    508 
    509 		if (ss->special && ss->special->read) {
    510 			(ss->special->read)(ss, bp);
    511 		} else {
    512 			/* generic scsi2 scanner read */
    513 			/* XXX add code for SCSI2 scanner read */
    514 		}
    515 	}
    516 }
    517 
    518 /*
    519  * Perform special action on behalf of the user;
    520  * knows about the internals of this device
    521  */
    522 int
    523 ssioctl(devvp, cmd, addr, flag, p)
    524 	struct vnode *devvp;
    525 	u_long cmd;
    526 	caddr_t addr;
    527 	int flag;
    528 	struct proc *p;
    529 {
    530 	struct ss_softc *ss;
    531 	int error = 0;
    532 	struct scan_io *sio;
    533 
    534 	ss = vdev_privdata(devvp);
    535 
    536 	if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    537 		return (ENODEV);
    538 
    539 	switch (cmd) {
    540 	case SCIOCGET:
    541 		if (ss->special && ss->special->get_params) {
    542 			/* call special handler */
    543 			error = (ss->special->get_params)(ss);
    544 			if (error)
    545 				return (error);
    546 		} else {
    547 			/* XXX add code for SCSI2 scanner, if any */
    548 			return (EOPNOTSUPP);
    549 		}
    550 		memcpy(addr, &ss->sio, sizeof(struct scan_io));
    551 		break;
    552 	case SCIOCSET:
    553 		sio = (struct scan_io *)addr;
    554 
    555 		if (ss->special && ss->special->set_params) {
    556 			/* call special handler */
    557 			error = (ss->special->set_params)(ss, sio);
    558 			if (error)
    559 				return (error);
    560 		} else {
    561 			/* XXX add code for SCSI2 scanner, if any */
    562 			return (EOPNOTSUPP);
    563 		}
    564 		break;
    565 	case SCIOCRESTART:
    566 		if (ss->special && ss->special->rewind_scanner ) {
    567 			/* call special handler */
    568 			error = (ss->special->rewind_scanner)(ss);
    569 			if (error)
    570 				return (error);
    571 		} else
    572 			/* XXX add code for SCSI2 scanner, if any */
    573 			return (EOPNOTSUPP);
    574 		ss->flags &= ~SSF_TRIGGERED;
    575 		break;
    576 #ifdef NOTYET
    577 	case SCAN_USE_ADF:
    578 		break;
    579 #endif
    580 	default:
    581 		return (scsipi_do_ioctl(ss->sc_periph, devvp, cmd, addr,
    582 		    flag, p));
    583 	}
    584 	return (error);
    585 }
    586