xref: /netbsd-src/sys/dev/scsipi/ss.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: ss.c,v 1.86 2014/07/25 08:10:39 dholland 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.86 2014/07/25 08:10:39 dholland 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 
342 	scsipi_adapter_minphys(periph->periph_channel, 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 static int
360 ssread(dev_t dev, struct uio *uio, int flag)
361 {
362 	struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev));
363 	int error;
364 
365 	if (!device_is_active(ss->sc_dev))
366 		return ENODEV;
367 
368 	/* if the scanner has not yet been started, do it now */
369 	if (!(ss->flags & SSF_TRIGGERED)) {
370 		if (ss->special && ss->special->trigger_scanner) {
371 			error = (ss->special->trigger_scanner)(ss);
372 			if (error)
373 				return (error);
374 		}
375 		ss->flags |= SSF_TRIGGERED;
376 	}
377 
378 	return physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio);
379 }
380 
381 /*
382  * Actually translate the requested transfer into one the physical
383  * driver can understand The transfer is described by a buf and will
384  * include only one physical transfer.
385  */
386 static void
387 ssstrategy(struct buf *bp)
388 {
389 	struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev));
390 	struct scsipi_periph *periph = ss->sc_periph;
391 	int s;
392 
393 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
394 	    ("ssstrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount,
395 	    bp->b_blkno));
396 
397 	/* If the device has been made invalid, error out */
398 	if (!device_is_active(ss->sc_dev)) {
399 		if (periph->periph_flags & PERIPH_OPEN)
400 			bp->b_error = EIO;
401 		else
402 			bp->b_error = ENODEV;
403 		goto done;
404 	}
405 
406 	/* If negative offset, error */
407 	if (bp->b_blkno < 0) {
408 		bp->b_error = EINVAL;
409 		goto done;
410 	}
411 
412 	if (bp->b_bcount > ss->sio.scan_window_size)
413 		bp->b_bcount = ss->sio.scan_window_size;
414 
415 	/* If it's a null transfer, return immediatly */
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_put(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->sc_periph);
434 
435 	splx(s);
436 	return;
437 done:
438 	/* Correctly set the buf to indicate a completed xfer */
439 	bp->b_resid = bp->b_bcount;
440 	biodone(bp);
441 }
442 
443 /*
444  * ssstart looks to see if there is a buf waiting for the device
445  * and that the device is not already busy. If both are true,
446  * It dequeues the buf and creates a scsi command to perform the
447  * transfer required. The transfer request will call scsipi_done
448  * on completion, which will in turn call this routine again
449  * so that the next queued transfer is performed.
450  * The bufs are queued by the strategy routine (ssstrategy)
451  *
452  * This routine is also called after other non-queued requests
453  * have been made of the scsi driver, to ensure that the queue
454  * continues to be drained.
455  * ssstart() is called at splbio
456  */
457 static void
458 ssstart(struct scsipi_periph *periph)
459 {
460 	struct ss_softc *ss = device_private(periph->periph_dev);
461 	struct buf *bp;
462 
463 	SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
464 
465 	/* See if there is a buf to do and we are not already doing one */
466 	while (periph->periph_active < periph->periph_openings) {
467 		/* if a special awaits, let it proceed first */
468 		if (periph->periph_flags & PERIPH_WAITING) {
469 			periph->periph_flags &= ~PERIPH_WAITING;
470 			wakeup((void *)periph);
471 			return;
472 		}
473 
474 		/* See if there is a buf with work for us to do.. */
475 		if ((bp = bufq_peek(ss->buf_queue)) == NULL)
476 			return;
477 
478 		if (ss->special && ss->special->read)
479 			(ss->special->read)(ss, bp);
480 		else {
481 			/* generic scsi2 scanner read */
482 			/* XXX add code for SCSI2 scanner read */
483 		}
484 	}
485 }
486 
487 void
488 ssrestart(void *v)
489 {
490 	int s = splbio();
491 	ssstart((struct scsipi_periph *)v);
492 	splx(s);
493 }
494 
495 static void
496 ssdone(struct scsipi_xfer *xs, int error)
497 {
498 	struct buf *bp = xs->bp;
499 
500 	if (bp) {
501 		bp->b_error = error;
502 		bp->b_resid = xs->resid;
503 		biodone(bp);
504 	}
505 }
506 
507 
508 /*
509  * Perform special action on behalf of the user;
510  * knows about the internals of this device
511  */
512 int
513 ssioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
514 {
515 	struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev));
516 	int error = 0;
517 	struct scan_io *sio;
518 
519 	if (!device_is_active(ss->sc_dev))
520 		return ENODEV;
521 
522 	switch (cmd) {
523 	case SCIOCGET:
524 		if (ss->special && ss->special->get_params) {
525 			/* call special handler */
526 			error = (ss->special->get_params)(ss);
527 			if (error)
528 				return error;
529 		} else
530 			/* XXX add code for SCSI2 scanner, if any */
531 			return EOPNOTSUPP;
532 		memcpy(addr, &ss->sio, sizeof(struct scan_io));
533 		break;
534 	case SCIOCSET:
535 		sio = (struct scan_io *)addr;
536 
537 		if (ss->special && ss->special->set_params) {
538 			/* call special handler */
539 			error = (ss->special->set_params)(ss, sio);
540 			if (error)
541 				return error;
542 		} else
543 			/* XXX add code for SCSI2 scanner, if any */
544 			return EOPNOTSUPP;
545 		break;
546 	case SCIOCRESTART:
547 		if (ss->special && ss->special->rewind_scanner ) {
548 			/* call special handler */
549 			error = (ss->special->rewind_scanner)(ss);
550 			if (error)
551 				return error;
552 		} else
553 			/* XXX add code for SCSI2 scanner, if any */
554 			return EOPNOTSUPP;
555 		ss->flags &= ~SSF_TRIGGERED;
556 		break;
557 #ifdef NOTYET
558 	case SCAN_USE_ADF:
559 		break;
560 #endif
561 	default:
562 		return scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr, flag, l);
563 	}
564 	return error;
565 }
566