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