ss.c revision 1.26.2.4 1 /* $NetBSD: ss.c,v 1.26.2.4 2001/01/05 17:36:27 bouyer 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 #define SSNMINOR 16
59
60 /*
61 * If the mode is 3 (e.g. minor = 3,7,11,15)
62 * then the device has been openned to set defaults
63 * This mode does NOT ALLOW I/O, only ioctls
64 */
65 #define MODE_REWIND 0
66 #define MODE_NONREWIND 1
67 #define MODE_CONTROL 3
68
69 int ssmatch(struct device *, struct cfdata *, void *);
70 void ssattach(struct device *, struct device *, void *);
71 int ssdetach(struct device *self, int flags);
72 int ssactivate(struct device *self, enum devact act);
73
74 struct cfattach ss_ca = {
75 sizeof(struct ss_softc), ssmatch, ssattach, ssdetach, ssactivate
76 };
77
78 extern struct cfdriver ss_cd;
79
80 void ssstrategy __P((struct buf *));
81 void ssstart __P((struct scsipi_periph *));
82 void ssminphys __P((struct buf *));
83
84 const struct scsipi_periphsw ss_switch = {
85 NULL,
86 ssstart,
87 NULL,
88 NULL,
89 };
90
91 struct scsipi_inquiry_pattern ss_patterns[] = {
92 {T_SCANNER, T_FIXED,
93 "", "", ""},
94 {T_SCANNER, T_REMOV,
95 "", "", ""},
96 {T_PROCESSOR, T_FIXED,
97 "HP ", "C1750A ", ""},
98 {T_PROCESSOR, T_FIXED,
99 "HP ", "C2500A ", ""},
100 {T_PROCESSOR, T_FIXED,
101 "HP ", "C1130A ", ""},
102 {T_PROCESSOR, T_FIXED,
103 "HP ", "C5110A ", ""},
104 {T_PROCESSOR, T_FIXED,
105 "HP ", "C7670A ", ""},
106 };
107
108 int
109 ssmatch(struct device *parent, struct cfdata *match, void *aux)
110 {
111 struct scsipibus_attach_args *sa = aux;
112 int priority;
113
114 (void)scsipi_inqmatch(&sa->sa_inqbuf,
115 (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]),
116 sizeof(ss_patterns[0]), &priority);
117 return (priority);
118 }
119
120 /*
121 * The routine called by the low level scsi routine when it discovers
122 * A device suitable for this driver
123 * If it is a know special, call special attach routine to install
124 * special handlers into the ss_softc structure
125 */
126 void
127 ssattach(struct device *parent, struct device *self, void *aux)
128 {
129 struct ss_softc *ss = (void *)self;
130 struct scsipibus_attach_args *sa = aux;
131 struct scsipi_periph *periph = sa->sa_periph;
132
133 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: "));
134
135 ss->flags |= SSF_AUTOCONF;
136
137 /*
138 * Store information needed to contact our base driver
139 */
140 ss->sc_periph = periph;
141 periph->periph_dev = &ss->sc_dev;
142 periph->periph_switch = &ss_switch;
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+SSNMINOR-1, 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_periph *periph;
232 struct scsipi_adapter *adapt;
233
234 unit = SSUNIT(dev);
235 if (unit >= ss_cd.cd_ndevs)
236 return (ENXIO);
237 ss = ss_cd.cd_devs[unit];
238 if (!ss)
239 return (ENXIO);
240
241 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
242 return (ENODEV);
243
244 ssmode = SSMODE(dev);
245
246 periph = ss->sc_periph;
247 adapt = periph->periph_channel->chan_adapter;
248
249 SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
250 unit, ss_cd.cd_ndevs));
251
252 if (periph->periph_flags & PERIPH_OPEN) {
253 printf("%s: already open\n", ss->sc_dev.dv_xname);
254 return (EBUSY);
255 }
256
257 if ((error = scsipi_adapter_addref(adapt)) != 0)
258 return (error);
259
260 /*
261 * Catch any unit attention errors.
262 *
263 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners
264 * consider paper to be a changeable media
265 *
266 */
267 error = scsipi_test_unit_ready(periph,
268 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST |
269 (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0));
270 if (error)
271 goto bad;
272
273 periph->periph_flags |= PERIPH_OPEN; /* unit attn now errors */
274
275 /*
276 * If the mode is 3 (e.g. minor = 3,7,11,15)
277 * then the device has been opened to set defaults
278 * This mode does NOT ALLOW I/O, only ioctls
279 */
280 if (ssmode == MODE_CONTROL)
281 return (0);
282
283 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
284 return (0);
285
286 bad:
287 scsipi_adapter_delref(adapt);
288 periph->periph_flags &= ~PERIPH_OPEN;
289 return (error);
290 }
291
292 /*
293 * close the device.. only called if we are the LAST
294 * occurence of an open device
295 */
296 int
297 ssclose(dev_t dev, int flag, int mode, struct proc *p)
298 {
299 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
300 struct scsipi_periph *periph = ss->sc_periph;
301 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
302 int error;
303
304 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n"));
305
306 if (SSMODE(dev) == MODE_REWIND) {
307 if (ss->special && ss->special->rewind_scanner) {
308 /* call special handler to rewind/abort scan */
309 error = (ss->special->rewind_scanner)(ss);
310 if (error)
311 return (error);
312 } else {
313 /* XXX add code to restart a SCSI2 scanner, if any */
314 }
315 ss->sio.scan_window_size = 0;
316 ss->flags &= ~SSF_TRIGGERED;
317 }
318
319 scsipi_wait_drain(periph);
320
321 scsipi_adapter_delref(adapt);
322 periph->periph_flags &= ~PERIPH_OPEN;
323
324 return (0);
325 }
326
327 /*
328 * trim the size of the transfer if needed,
329 * called by physio
330 * basically the smaller of our min and the scsi driver's
331 * minphys
332 */
333 void
334 ssminphys(struct buf *bp)
335 {
336 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
337 struct scsipi_periph *periph = ss->sc_periph;
338
339 (*periph->periph_channel->chan_adapter->adapt_minphys)(bp);
340
341 /*
342 * trim the transfer further for special devices this is
343 * because some scanners only read multiples of a line at a
344 * time, also some cannot disconnect, so the read must be
345 * short enough to happen quickly
346 */
347 if (ss->special && ss->special->minphys)
348 (ss->special->minphys)(ss, bp);
349 }
350
351 /*
352 * Do a read on a device for a user process.
353 * Prime scanner at start of read, check uio values, call ssstrategy
354 * via physio for the actual transfer.
355 */
356 int
357 ssread(dev, uio, flag)
358 dev_t dev;
359 struct uio *uio;
360 int flag;
361 {
362 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
363 int error;
364
365 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
366 return (ENODEV);
367
368 /* if the scanner has not yet been started, do it now */
369 if (!(ss->flags & SSF_TRIGGERED)) {
370 if (ss->special && ss->special->trigger_scanner) {
371 error = (ss->special->trigger_scanner)(ss);
372 if (error)
373 return (error);
374 }
375 ss->flags |= SSF_TRIGGERED;
376 }
377
378 return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio));
379 }
380
381 /*
382 * Actually translate the requested transfer into one the physical
383 * driver can understand The transfer is described by a buf and will
384 * include only one physical transfer.
385 */
386 void
387 ssstrategy(bp)
388 struct buf *bp;
389 {
390 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)];
391 int s;
392
393 SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
394 ("ssstrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
395
396 /*
397 * If the device has been made invalid, error out
398 */
399 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
400 bp->b_flags |= B_ERROR;
401 if (ss->sc_link->flags & SDEV_OPEN)
402 bp->b_error = EIO;
403 else
404 bp->b_error = ENODEV;
405 goto done;
406 }
407
408 /* If negative offset, error */
409 if (bp->b_blkno < 0) {
410 bp->b_flags |= B_ERROR;
411 bp->b_error = EINVAL;
412 goto done;
413 }
414
415 if (bp->b_bcount > ss->sio.scan_window_size)
416 bp->b_bcount = ss->sio.scan_window_size;
417
418 /*
419 * If it's a null transfer, return immediatly
420 */
421 if (bp->b_bcount == 0)
422 goto done;
423
424 s = splbio();
425
426 /*
427 * Place it in the queue of activities for this scanner
428 * at the end (a bit silly because we only have on user..
429 * (but it could fork()))
430 */
431 BUFQ_INSERT_TAIL(&ss->buf_queue, bp);
432
433 /*
434 * Tell the device to get going on the transfer if it's
435 * not doing anything, otherwise just wait for completion
436 * (All a bit silly if we're only allowing 1 open but..)
437 */
438 ssstart(ss->sc_periph);
439
440 splx(s);
441 return;
442 bp->b_flags |= B_ERROR;
443 done:
444 /*
445 * Correctly set the buf to indicate a completed xfer
446 */
447 bp->b_resid = bp->b_bcount;
448 biodone(bp);
449 }
450
451 /*
452 * ssstart looks to see if there is a buf waiting for the device
453 * and that the device is not already busy. If both are true,
454 * It dequeues the buf and creates a scsi command to perform the
455 * transfer required. The transfer request will call scsipi_done
456 * on completion, which will in turn call this routine again
457 * so that the next queued transfer is performed.
458 * The bufs are queued by the strategy routine (ssstrategy)
459 *
460 * This routine is also called after other non-queued requests
461 * have been made of the scsi driver, to ensure that the queue
462 * continues to be drained.
463 * ssstart() is called at splbio
464 */
465 void
466 ssstart(periph)
467 struct scsipi_periph *periph;
468 {
469 struct ss_softc *ss = (void *)periph->periph_dev;
470 struct buf *bp;
471
472 SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
473 /*
474 * See if there is a buf to do and we are not already
475 * doing one
476 */
477 while (periph->periph_active < periph->periph_openings) {
478 /* if a special awaits, let it proceed first */
479 if (periph->periph_flags & PERIPH_WAITING) {
480 periph->periph_flags &= ~PERIPH_WAITING;
481 wakeup((caddr_t)periph);
482 return;
483 }
484
485 /*
486 * See if there is a buf with work for us to do..
487 */
488 if ((bp = BUFQ_FIRST(&ss->buf_queue)) == NULL)
489 return;
490 BUFQ_REMOVE(&ss->buf_queue, bp);
491
492 if (ss->special && ss->special->read) {
493 (ss->special->read)(ss, bp);
494 } else {
495 /* generic scsi2 scanner read */
496 /* XXX add code for SCSI2 scanner read */
497 }
498 }
499 }
500
501 /*
502 * Perform special action on behalf of the user;
503 * knows about the internals of this device
504 */
505 int
506 ssioctl(dev, cmd, addr, flag, p)
507 dev_t dev;
508 u_long cmd;
509 caddr_t addr;
510 int flag;
511 struct proc *p;
512 {
513 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)];
514 int error = 0;
515 struct scan_io *sio;
516
517 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0)
518 return (ENODEV);
519
520 switch (cmd) {
521 case SCIOCGET:
522 if (ss->special && ss->special->get_params) {
523 /* call special handler */
524 error = (ss->special->get_params)(ss);
525 if (error)
526 return (error);
527 } else {
528 /* XXX add code for SCSI2 scanner, if any */
529 return (EOPNOTSUPP);
530 }
531 bcopy(&ss->sio, addr, sizeof(struct scan_io));
532 break;
533 case SCIOCSET:
534 sio = (struct scan_io *)addr;
535
536 if (ss->special && ss->special->set_params) {
537 /* call special handler */
538 error = (ss->special->set_params)(ss, sio);
539 if (error)
540 return (error);
541 } else {
542 /* XXX add code for SCSI2 scanner, if any */
543 return (EOPNOTSUPP);
544 }
545 break;
546 case SCIOCRESTART:
547 if (ss->special && ss->special->rewind_scanner ) {
548 /* call special handler */
549 error = (ss->special->rewind_scanner)(ss);
550 if (error)
551 return (error);
552 } else
553 /* XXX add code for SCSI2 scanner, if any */
554 return (EOPNOTSUPP);
555 ss->flags &= ~SSF_TRIGGERED;
556 break;
557 #ifdef NOTYET
558 case SCAN_USE_ADF:
559 break;
560 #endif
561 default:
562 return (scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr,
563 flag, p));
564 }
565 return (error);
566 }
567