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