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