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