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