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