1 /* $OpenBSD: uscanner.c,v 1.17 2004/07/08 22:18:45 deraadt Exp $ */
2 /* $NetBSD: uscanner.c,v 1.40 2003/01/27 00:32:44 wiz Exp $ */
3
4 /*
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology
11 * and Nick Hibma (n_hibma@qubesoft.com).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #if defined(__NetBSD__) || defined(__OpenBSD__)
48 #include <sys/device.h>
49 #elif defined(__FreeBSD__)
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/filio.h>
55 #endif
56 #include <sys/tty.h>
57 #include <sys/file.h>
58 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
59 #include <sys/selinfo.h>
60 #else
61 #include <sys/select.h>
62 #endif
63 #include <sys/proc.h>
64 #include <sys/vnode.h>
65 #include <sys/poll.h>
66 #include <sys/conf.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71
72 #include <dev/usb/usbdevs.h>
73
74 #ifdef USCANNER_DEBUG
75 #define DPRINTF(x) do { if (uscannerdebug) logprintf x; } while (0)
76 #define DPRINTFN(n,x) do { if (uscannerdebug>(n)) logprintf x; } while (0)
77 int uscannerdebug = 0;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 struct uscan_info {
84 struct usb_devno devno;
85 u_int flags;
86 #define USC_KEEP_OPEN 1
87 };
88
89 /* Table of scanners that may work with this driver. */
90 static const struct uscan_info uscanner_devs[] = {
91 /* Acer Peripherals */
92 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
93 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
94 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
95 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
96
97 /* AGFA */
98 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
99 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
100 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
101 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
102 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
103 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
104 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
105 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
106 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
107 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
108
109 /* Avision */
110 {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
111
112 /* Canon */
113 {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
114
115 /* Kye */
116 {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
117
118 /* HP */
119 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
120 {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
121 {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
122 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
123 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
124 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
125 {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
126 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
127 #if 0
128 /* Handled by usscanner */
129 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
130 #endif
131 {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
132 {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
133
134 #if 0
135 /* XXX Should be handled by usscanner */
136 /* Microtek */
137 {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
138 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
139 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
140 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
141 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
142 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
143 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
144 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
145 #endif
146
147 /* Mustek */
148 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
149 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
150 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
151 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
152 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
153 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
154 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
155 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
156
157 /* National */
158 {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
159 {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
160
161 /* Primax */
162 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
163 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
164 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
165 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
166 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
167 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
168 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
169 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
170 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
171 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
172 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
173 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
174 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
175
176 /* Epson */
177 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
178 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
179 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
180 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
181 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
182 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
183 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
184 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
185 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
186 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
187 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_2400 }, 0 },
188 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
189
190 /* UMAX */
191 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
192 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
193 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
194 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
195 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
196
197 /* Visioneer */
198 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
199 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
200 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
201 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
202 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
203 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
204 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
205
206 /* Ultima */
207 {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
208
209 };
210 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
211
212 #define USCANNER_BUFFERSIZE 1024
213
214 struct uscanner_softc {
215 USBBASEDEVICE sc_dev; /* base device */
216 usbd_device_handle sc_udev;
217 usbd_interface_handle sc_iface;
218
219 u_int sc_dev_flags;
220
221 usbd_pipe_handle sc_bulkin_pipe;
222 int sc_bulkin;
223 usbd_xfer_handle sc_bulkin_xfer;
224 void *sc_bulkin_buffer;
225 int sc_bulkin_bufferlen;
226 int sc_bulkin_datalen;
227
228 usbd_pipe_handle sc_bulkout_pipe;
229 int sc_bulkout;
230 usbd_xfer_handle sc_bulkout_xfer;
231 void *sc_bulkout_buffer;
232 int sc_bulkout_bufferlen;
233 int sc_bulkout_datalen;
234
235 struct selinfo sc_selq;
236
237 u_char sc_state;
238 #define USCANNER_OPEN 0x01 /* opened */
239
240 int sc_refcnt;
241 u_char sc_dying;
242 };
243
244 #if defined(__NetBSD__)
245 dev_type_open(uscanneropen);
246 dev_type_close(uscannerclose);
247 dev_type_read(uscannerread);
248 dev_type_write(uscannerwrite);
249 dev_type_ioctl(uscannerioctl);
250 dev_type_poll(uscannerpoll);
251 dev_type_kqfilter(uscannerkqfilter);
252
253 const struct cdevsw uscanner_cdevsw = {
254 uscanneropen, uscannerclose, uscannerread, uscannerwrite,
255 uscannerioctl, nostop, notty, uscannerpoll, nommap, uscannerkqfilter,
256 };
257 #elif defined(__FreeBSD__)
258 d_open_t uscanneropen;
259 d_close_t uscannerclose;
260 d_read_t uscannerread;
261 d_write_t uscannerwrite;
262 d_poll_t uscannerpoll;
263
264 #define USCANNER_CDEV_MAJOR 156
265
266 Static struct cdevsw uscanner_cdevsw = {
267 /* open */ uscanneropen,
268 /* close */ uscannerclose,
269 /* read */ uscannerread,
270 /* write */ uscannerwrite,
271 /* ioctl */ noioctl,
272 /* poll */ uscannerpoll,
273 /* mmap */ nommap,
274 /* strategy */ nostrategy,
275 /* name */ "uscanner",
276 /* maj */ USCANNER_CDEV_MAJOR,
277 /* dump */ nodump,
278 /* psize */ nopsize,
279 /* flags */ 0,
280 #if !defined(__FreeBSD__) || (__FreeBSD__ < 5)
281 /* bmaj */ -1
282 #endif
283 };
284 #endif
285
286 Static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
287 Static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
288 Static void uscanner_do_close(struct uscanner_softc *);
289
290 #define USCANNERUNIT(n) (minor(n))
291
292 USB_DECLARE_DRIVER(uscanner);
293
USB_MATCH(uscanner)294 USB_MATCH(uscanner)
295 {
296 USB_MATCH_START(uscanner, uaa);
297
298 if (uaa->iface != NULL)
299 return UMATCH_NONE;
300
301 return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
302 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
303 }
304
USB_ATTACH(uscanner)305 USB_ATTACH(uscanner)
306 {
307 USB_ATTACH_START(uscanner, sc, uaa);
308 usb_interface_descriptor_t *id = 0;
309 usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
310 char devinfo[1024];
311 int i;
312 usbd_status err;
313
314 usbd_devinfo(uaa->device, 0, devinfo, sizeof devinfo);
315 USB_ATTACH_SETUP;
316 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
317
318 sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
319
320 sc->sc_udev = uaa->device;
321
322 err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
323 if (err) {
324 printf("%s: setting config no failed\n",
325 USBDEVNAME(sc->sc_dev));
326 USB_ATTACH_ERROR_RETURN;
327 }
328
329 /* XXX We only check the first interface */
330 err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
331 if (!err && sc->sc_iface)
332 id = usbd_get_interface_descriptor(sc->sc_iface);
333 if (err || id == 0) {
334 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
335 USBDEVNAME(sc->sc_dev), err, id);
336 USB_ATTACH_ERROR_RETURN;
337 }
338
339 /* Find the two first bulk endpoints */
340 for (i = 0 ; i < id->bNumEndpoints; i++) {
341 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
342 if (ed == 0) {
343 printf("%s: could not read endpoint descriptor\n",
344 USBDEVNAME(sc->sc_dev));
345 USB_ATTACH_ERROR_RETURN;
346 }
347
348 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
349 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
350 ed_bulkin = ed;
351 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
352 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
353 ed_bulkout = ed;
354 }
355
356 if (ed_bulkin && ed_bulkout) /* found all we need */
357 break;
358 }
359
360 /* Verify that we goething sensible */
361 if (ed_bulkin == NULL || ed_bulkout == NULL) {
362 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
363 USBDEVNAME(sc->sc_dev));
364 USB_ATTACH_ERROR_RETURN;
365 }
366
367 sc->sc_bulkin = ed_bulkin->bEndpointAddress;
368 sc->sc_bulkout = ed_bulkout->bEndpointAddress;
369
370 #ifdef __FreeBSD__
371 /* the main device, ctrl endpoint */
372 make_dev(&uscanner_cdevsw, USBDEVUNIT(sc->sc_dev),
373 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
374 #endif
375
376 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
377 USBDEV(sc->sc_dev));
378
379 USB_ATTACH_SUCCESS_RETURN;
380 }
381
382 int
uscanneropen(dev_t dev,int flag,int mode,usb_proc_ptr p)383 uscanneropen(dev_t dev, int flag, int mode, usb_proc_ptr p)
384 {
385 struct uscanner_softc *sc;
386 int unit = USCANNERUNIT(dev);
387 usbd_status err;
388
389 USB_GET_SC_OPEN(uscanner, unit, sc);
390
391 DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
392 flag, mode, unit));
393
394 if (sc->sc_dying)
395 return (ENXIO);
396
397 if (sc->sc_state & USCANNER_OPEN)
398 return (EBUSY);
399
400 sc->sc_state |= USCANNER_OPEN;
401
402 sc->sc_bulkin_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
403 sc->sc_bulkout_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
404 /* No need to check buffers for NULL since we have WAITOK */
405
406 sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
407 sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
408
409 /* We have decided on which endpoints to use, now open the pipes */
410 if (sc->sc_bulkin_pipe == NULL) {
411 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
412 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
413 if (err) {
414 printf("%s: cannot open bulk-in pipe (addr %d)\n",
415 USBDEVNAME(sc->sc_dev), sc->sc_bulkin);
416 uscanner_do_close(sc);
417 return (EIO);
418 }
419 }
420 if (sc->sc_bulkout_pipe == NULL) {
421 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
422 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
423 if (err) {
424 printf("%s: cannot open bulk-out pipe (addr %d)\n",
425 USBDEVNAME(sc->sc_dev), sc->sc_bulkout);
426 uscanner_do_close(sc);
427 return (EIO);
428 }
429 }
430
431 sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
432 if (sc->sc_bulkin_xfer == NULL) {
433 uscanner_do_close(sc);
434 return (ENOMEM);
435 }
436 sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
437 if (sc->sc_bulkout_xfer == NULL) {
438 uscanner_do_close(sc);
439 return (ENOMEM);
440 }
441
442 return (0); /* success */
443 }
444
445 int
uscannerclose(dev_t dev,int flag,int mode,usb_proc_ptr p)446 uscannerclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
447 {
448 struct uscanner_softc *sc;
449
450 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
451
452 DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
453 flag, mode, USCANNERUNIT(dev)));
454
455 #ifdef DIAGNOSTIC
456 if (!(sc->sc_state & USCANNER_OPEN)) {
457 printf("uscannerclose: not open\n");
458 return (EINVAL);
459 }
460 #endif
461
462 uscanner_do_close(sc);
463
464 return (0);
465 }
466
467 void
uscanner_do_close(struct uscanner_softc * sc)468 uscanner_do_close(struct uscanner_softc *sc)
469 {
470 if (sc->sc_bulkin_xfer) {
471 usbd_free_xfer(sc->sc_bulkin_xfer);
472 sc->sc_bulkin_xfer = NULL;
473 }
474 if (sc->sc_bulkout_xfer) {
475 usbd_free_xfer(sc->sc_bulkout_xfer);
476 sc->sc_bulkout_xfer = NULL;
477 }
478
479 if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
480 if (sc->sc_bulkin_pipe != NULL) {
481 usbd_abort_pipe(sc->sc_bulkin_pipe);
482 usbd_close_pipe(sc->sc_bulkin_pipe);
483 sc->sc_bulkin_pipe = NULL;
484 }
485 if (sc->sc_bulkout_pipe != NULL) {
486 usbd_abort_pipe(sc->sc_bulkout_pipe);
487 usbd_close_pipe(sc->sc_bulkout_pipe);
488 sc->sc_bulkout_pipe = NULL;
489 }
490 }
491
492 if (sc->sc_bulkin_buffer) {
493 free(sc->sc_bulkin_buffer, M_USBDEV);
494 sc->sc_bulkin_buffer = NULL;
495 }
496 if (sc->sc_bulkout_buffer) {
497 free(sc->sc_bulkout_buffer, M_USBDEV);
498 sc->sc_bulkout_buffer = NULL;
499 }
500
501 sc->sc_state &= ~USCANNER_OPEN;
502 }
503
504 Static int
uscanner_do_read(struct uscanner_softc * sc,struct uio * uio,int flag)505 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
506 {
507 u_int32_t n, tn;
508 usbd_status err;
509 int error = 0;
510
511 DPRINTFN(5, ("%s: uscannerread\n", USBDEVNAME(sc->sc_dev)));
512
513 if (sc->sc_dying)
514 return (EIO);
515
516 while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
517 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
518 tn = n;
519
520 err = usbd_bulk_transfer(
521 sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
522 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
523 sc->sc_bulkin_buffer, &tn,
524 "uscnrb");
525 if (err) {
526 if (err == USBD_INTERRUPTED)
527 error = EINTR;
528 else if (err == USBD_TIMEOUT)
529 error = ETIMEDOUT;
530 else
531 error = EIO;
532 break;
533 }
534 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
535 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
536 if (error || tn < n)
537 break;
538 }
539
540 return (error);
541 }
542
543 int
uscannerread(dev_t dev,struct uio * uio,int flag)544 uscannerread(dev_t dev, struct uio *uio, int flag)
545 {
546 struct uscanner_softc *sc;
547 int error;
548
549 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
550
551 sc->sc_refcnt++;
552 error = uscanner_do_read(sc, uio, flag);
553 if (--sc->sc_refcnt < 0)
554 usb_detach_wakeup(USBDEV(sc->sc_dev));
555
556 return (error);
557 }
558
559 Static int
uscanner_do_write(struct uscanner_softc * sc,struct uio * uio,int flag)560 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
561 {
562 u_int32_t n;
563 int error = 0;
564 usbd_status err;
565
566 DPRINTFN(5, ("%s: uscanner_do_write\n", USBDEVNAME(sc->sc_dev)));
567
568 if (sc->sc_dying)
569 return (EIO);
570
571 while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
572 error = uiomove(sc->sc_bulkout_buffer, n, uio);
573 if (error)
574 break;
575 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
576 err = usbd_bulk_transfer(
577 sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
578 0, USBD_NO_TIMEOUT,
579 sc->sc_bulkout_buffer, &n,
580 "uscnwb");
581 if (err) {
582 if (err == USBD_INTERRUPTED)
583 error = EINTR;
584 else
585 error = EIO;
586 break;
587 }
588 }
589
590 return (error);
591 }
592
593 int
uscannerwrite(dev_t dev,struct uio * uio,int flag)594 uscannerwrite(dev_t dev, struct uio *uio, int flag)
595 {
596 struct uscanner_softc *sc;
597 int error;
598
599 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
600
601 sc->sc_refcnt++;
602 error = uscanner_do_write(sc, uio, flag);
603 if (--sc->sc_refcnt < 0)
604 usb_detach_wakeup(USBDEV(sc->sc_dev));
605 return (error);
606 }
607
608 #if defined(__NetBSD__) || defined(__OpenBSD__)
609 int
uscanner_activate(device_ptr_t self,enum devact act)610 uscanner_activate(device_ptr_t self, enum devact act)
611 {
612 struct uscanner_softc *sc = (struct uscanner_softc *)self;
613
614 switch (act) {
615 case DVACT_ACTIVATE:
616 return (EOPNOTSUPP);
617
618 case DVACT_DEACTIVATE:
619 sc->sc_dying = 1;
620 break;
621 }
622 return (0);
623 }
624 #endif
625
USB_DETACH(uscanner)626 USB_DETACH(uscanner)
627 {
628 USB_DETACH_START(uscanner, sc);
629 int s;
630 #if defined(__NetBSD__) || defined(__OpenBSD__)
631 int maj, mn;
632 #elif defined(__FreeBSD__)
633 dev_t dev;
634 struct vnode *vp;
635 #endif
636
637 #if defined(__NetBSD__) || defined(__OpenBSD__)
638 DPRINTF(("uscanner_detach: sc=%p flags=%d\n", sc, flags));
639 #elif defined(__FreeBSD__)
640 DPRINTF(("uscanner_detach: sc=%p\n", sc));
641 #endif
642
643 sc->sc_dying = 1;
644 sc->sc_dev_flags = 0; /* make close really close device */
645
646 /* Abort all pipes. Causes processes waiting for transfer to wake. */
647 if (sc->sc_bulkin_pipe != NULL)
648 usbd_abort_pipe(sc->sc_bulkin_pipe);
649 if (sc->sc_bulkout_pipe != NULL)
650 usbd_abort_pipe(sc->sc_bulkout_pipe);
651
652 s = splusb();
653 if (--sc->sc_refcnt >= 0) {
654 /* Wait for processes to go away. */
655 usb_detach_wait(USBDEV(sc->sc_dev));
656 }
657 splx(s);
658
659 #if defined(__NetBSD__) || defined(__OpenBSD__)
660 /* locate the major number */
661 #if defined(__NetBSD__)
662 maj = cdevsw_lookup_major(&uscanner_cdevsw);
663 #elif defined(__OpenBSD__)
664 for (maj = 0; maj < nchrdev; maj++)
665 if (cdevsw[maj].d_open == uscanneropen)
666 break;
667 #endif
668
669 /* Nuke the vnodes for any open instances (calls close). */
670 mn = self->dv_unit * USB_MAX_ENDPOINTS;
671 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
672 #elif defined(__FreeBSD__)
673 /* destroy the device for the control endpoint */
674 dev = makedev(USCANNER_CDEV_MAJOR, USBDEVUNIT(sc->sc_dev));
675 vp = SLIST_FIRST(&dev->si_hlist);
676 if (vp)
677 VOP_REVOKE(vp, REVOKEALL);
678 destroy_dev(dev);
679 #endif
680
681 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
682 USBDEV(sc->sc_dev));
683
684 return (0);
685 }
686
687 int
uscannerpoll(dev_t dev,int events,usb_proc_ptr p)688 uscannerpoll(dev_t dev, int events, usb_proc_ptr p)
689 {
690 struct uscanner_softc *sc;
691 int revents = 0;
692
693 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
694
695 if (sc->sc_dying)
696 return (EIO);
697
698 /*
699 * We have no easy way of determining if a read will
700 * yield any data or a write will happen.
701 * Pretend they will.
702 */
703 revents |= events &
704 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
705
706 return (revents);
707 }
708
709 int
uscannerioctl(dev_t dev,u_long cmd,caddr_t addr,int flag,usb_proc_ptr p)710 uscannerioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
711 {
712 return (EINVAL);
713 }
714
715 Static void filt_uscannerdetach(struct knote *);
716 int uscannerkqfilter(dev_t, struct knote *);
717
718 Static void
filt_uscannerdetach(struct knote * kn)719 filt_uscannerdetach(struct knote *kn)
720 {
721 struct uscanner_softc *sc = (void *)kn->kn_hook;
722
723 SLIST_REMOVE(&sc->sc_selq.sel_klist, kn, knote, kn_selnext);
724 }
725
726 Static struct filterops uscanner_seltrue_filtops =
727 { 1, NULL, filt_uscannerdetach, filt_seltrue };
728
729 int
uscannerkqfilter(dev_t dev,struct knote * kn)730 uscannerkqfilter(dev_t dev, struct knote *kn)
731 {
732 struct uscanner_softc *sc;
733 struct klist *klist;
734
735 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
736
737 if (sc->sc_dying)
738 return (1);
739
740 switch (kn->kn_filter) {
741 case EVFILT_READ:
742 case EVFILT_WRITE:
743 /*
744 * We have no easy way of determining if a read will
745 * yield any data or a write will happen.
746 * Pretend they will.
747 */
748 klist = &sc->sc_selq.sel_klist;
749 kn->kn_fop = &uscanner_seltrue_filtops;
750 break;
751
752 default:
753 return (1);
754 }
755
756 kn->kn_hook = (void *)sc;
757
758 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
759
760 return (0);
761 }
762
763 #if defined(__FreeBSD__)
764 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
765 #endif
766