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