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