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