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