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