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