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