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