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