Device layer rollup commit.
[dragonfly.git] / sys / dev / usbmisc / uscanner / uscanner.c
CommitLineData
62ade751 1/*
1550dfd9
MD
2 * $NetBSD: uscanner.c,v 1.30 2002/07/11 21:14:36 augustss Exp $
3 * $FreeBSD: src/sys/dev/usb/uscanner.c,v 1.48 2003/12/22 19:58:27 sanpei Exp $
e4c9c0c8 4 * $DragonFly: src/sys/dev/usbmisc/uscanner/uscanner.c,v 1.9 2004/05/19 22:52:52 dillon Exp $
1550dfd9
MD
5 */
6
7/* Also already merged from NetBSD:
8 * $NetBSD: uscanner.c,v 1.33 2002/09/23 05:51:24 simonb Exp $
62ade751 9 */
984263bc
MD
10
11/*
12 * Copyright (c) 2000 The NetBSD Foundation, Inc.
13 * All rights reserved.
14 *
15 * This code is derived from software contributed to The NetBSD Foundation
16 * by Lennart Augustsson (lennart@augustsson.net) at
17 * Carlstedt Research & Technology
18 * and Nick Hibma (n_hibma@qubesoft.com).
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the NetBSD
31 * Foundation, Inc. and its contributors.
32 * 4. Neither the name of The NetBSD Foundation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 * POSSIBILITY OF SUCH DAMAGE.
47 */
48
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/kernel.h>
53#include <sys/malloc.h>
54#if defined(__NetBSD__) || defined(__OpenBSD__)
55#include <sys/device.h>
d47ac1a2 56#elif defined(__FreeBSD__) || defined(__DragonFly__)
984263bc
MD
57#include <sys/module.h>
58#include <sys/bus.h>
59#include <sys/conf.h>
60#include <sys/fcntl.h>
61#include <sys/filio.h>
62#endif
63#include <sys/tty.h>
64#include <sys/file.h>
d47ac1a2 65#if defined(__FreeBSD__) && __FreeBSD_version >= 500014
1550dfd9
MD
66#include <sys/selinfo.h>
67#else
984263bc 68#include <sys/select.h>
1550dfd9 69#endif
984263bc
MD
70#include <sys/proc.h>
71#include <sys/vnode.h>
72#include <sys/poll.h>
73#include <sys/conf.h>
74#include <sys/sysctl.h>
75
1f2de5d4
MD
76#include <bus/usb/usb.h>
77#include <bus/usb/usbdi.h>
78#include <bus/usb/usbdi_util.h>
984263bc 79
1f2de5d4 80#include <bus/usb/usbdevs.h>
984263bc
MD
81
82#ifdef USB_DEBUG
83#define DPRINTF(x) if (uscannerdebug) logprintf x
84#define DPRINTFN(n,x) if (uscannerdebug>(n)) logprintf x
85int uscannerdebug = 0;
86SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
87SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
88 &uscannerdebug, 0, "uscanner debug level");
89#else
90#define DPRINTF(x)
91#define DPRINTFN(n,x)
92#endif
93
94struct uscan_info {
95 struct usb_devno devno;
96 u_int flags;
97#define USC_KEEP_OPEN 1
98};
99
100/* Table of scanners that may work with this driver. */
101static const struct uscan_info uscanner_devs[] = {
102 /* Acer Peripherals */
103 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
104 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
105 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
106 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
107
108 /* AGFA */
109 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
110 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
111 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
112 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
113 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
114 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
115 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
116 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
117 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
118 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
119
120 /* Avision */
121 {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
122
123 /* Canon */
124 {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
125
126 /* Kye */
127 {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
128
129 /* HP */
130 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
131 {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
132 {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
133 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
134 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
135 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
136 {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
137 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
138 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
139 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
140 {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
141 {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
142
143 /* Microtek */
144 {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
145 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
146 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
147 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
148 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
149 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
150 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
151 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
152
62ade751
MD
153 /* Minolta */
154 {{ USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_5400 }, 0 },
155
984263bc
MD
156 /* Mustek */
157 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
158 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
62ade751 159 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200TA }, 0 },
984263bc
MD
160 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
161 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
162 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
163 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
164 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
165 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
166
167 /* National */
168 {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
169 {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
170
171 /* Primax */
172 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
173 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
174 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
175 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
176 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
177 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
178 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
179 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
180 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
181 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
182 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
183 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
184 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
185
186 /* Epson */
187 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
188 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
189 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
190 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
62ade751 191 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1250 }, 0 },
984263bc
MD
192 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
193 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
194 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
195 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
196 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
197 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
198 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
62ade751 199 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9300UF }, 0 },
984263bc
MD
200
201 /* UMAX */
202 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
203 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
204 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
1550dfd9 205 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2100U }, 0 },
984263bc
MD
206 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
207 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
208
209 /* Visioneer */
210 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
211 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
212 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
213 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
214 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
215 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
216 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
217
218 /* Ultima */
219 {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
220
221};
222#define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
223
224#define USCANNER_BUFFERSIZE 1024
225
226struct uscanner_softc {
227 USBBASEDEVICE sc_dev; /* base device */
228 usbd_device_handle sc_udev;
229 usbd_interface_handle sc_iface;
230
231 u_int sc_dev_flags;
232
233 usbd_pipe_handle sc_bulkin_pipe;
234 int sc_bulkin;
235 usbd_xfer_handle sc_bulkin_xfer;
236 void *sc_bulkin_buffer;
237 int sc_bulkin_bufferlen;
238 int sc_bulkin_datalen;
239
240 usbd_pipe_handle sc_bulkout_pipe;
241 int sc_bulkout;
242 usbd_xfer_handle sc_bulkout_xfer;
243 void *sc_bulkout_buffer;
244 int sc_bulkout_bufferlen;
245 int sc_bulkout_datalen;
246
247 u_char sc_state;
248#define USCANNER_OPEN 0x01 /* opened */
249
250 int sc_refcnt;
251 u_char sc_dying;
252};
253
254#if defined(__NetBSD__) || defined(__OpenBSD__)
255cdev_decl(uscanner);
d47ac1a2 256#elif defined(__FreeBSD__) || defined(__DragonFly__)
984263bc
MD
257d_open_t uscanneropen;
258d_close_t uscannerclose;
259d_read_t uscannerread;
260d_write_t uscannerwrite;
984263bc
MD
261d_poll_t uscannerpoll;
262
263#define USCANNER_CDEV_MAJOR 156
264
265Static struct cdevsw uscanner_cdevsw = {
fabb8ceb
MD
266 /* name */ "uscanner",
267 /* maj */ USCANNER_CDEV_MAJOR,
268 /* flags */ 0,
269 /* port */ NULL,
455fcd7e 270 /* clone */ NULL,
fabb8ceb 271
984263bc
MD
272 /* open */ uscanneropen,
273 /* close */ uscannerclose,
274 /* read */ uscannerread,
275 /* write */ uscannerwrite,
1550dfd9 276 /* ioctl */ noioctl,
984263bc
MD
277 /* poll */ uscannerpoll,
278 /* mmap */ nommap,
279 /* strategy */ nostrategy,
984263bc 280 /* dump */ nodump,
fabb8ceb 281 /* psize */ nopsize
984263bc
MD
282};
283#endif
284
285Static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
286Static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
287Static void uscanner_do_close(struct uscanner_softc *);
288
289#define USCANNERUNIT(n) (minor(n))
290
291USB_DECLARE_DRIVER(uscanner);
292
293USB_MATCH(uscanner)
294{
295 USB_MATCH_START(uscanner, uaa);
296
297 if (uaa->iface != NULL)
298 return UMATCH_NONE;
299
300 return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
301 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
302}
303
304USB_ATTACH(uscanner)
305{
306 USB_ATTACH_START(uscanner, sc, uaa);
307 usb_interface_descriptor_t *id = 0;
308 usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
309 char devinfo[1024];
310 int i;
311 usbd_status err;
312
313 usbd_devinfo(uaa->device, 0, devinfo);
314 USB_ATTACH_SETUP;
315 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
316
317 sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
318
319 sc->sc_udev = uaa->device;
320
321 err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
322 if (err) {
323 printf("%s: setting config no failed\n",
324 USBDEVNAME(sc->sc_dev));
325 USB_ATTACH_ERROR_RETURN;
326 }
327
328 /* XXX We only check the first interface */
329 err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
330 if (!err && sc->sc_iface)
331 id = usbd_get_interface_descriptor(sc->sc_iface);
332 if (err || id == 0) {
333 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
334 USBDEVNAME(sc->sc_dev), err, id);
335 USB_ATTACH_ERROR_RETURN;
336 }
337
338 /* Find the two first bulk endpoints */
339 for (i = 0 ; i < id->bNumEndpoints; i++) {
340 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
341 if (ed == 0) {
342 printf("%s: could not read endpoint descriptor\n",
343 USBDEVNAME(sc->sc_dev));
344 USB_ATTACH_ERROR_RETURN;
345 }
346
347 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
348 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
349 ed_bulkin = ed;
350 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
351 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
352 ed_bulkout = ed;
353 }
354
355 if (ed_bulkin && ed_bulkout) /* found all we need */
356 break;
357 }
358
359 /* Verify that we goething sensible */
360 if (ed_bulkin == NULL || ed_bulkout == NULL) {
361 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
362 USBDEVNAME(sc->sc_dev));
363 USB_ATTACH_ERROR_RETURN;
364 }
365
366 sc->sc_bulkin = ed_bulkin->bEndpointAddress;
367 sc->sc_bulkout = ed_bulkout->bEndpointAddress;
368
d47ac1a2 369#if defined(__FreeBSD__) || defined(__DragonFly__)
984263bc 370 /* the main device, ctrl endpoint */
e4c9c0c8
MD
371 cdevsw_add(&uscanner_cdevsw, -1, USBDEVUNIT(sc->sc_dev));
372 make_dev(&uscanner_cdevsw, USBDEVUNIT(sc->sc_dev),
984263bc
MD
373 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
374#endif
375
1550dfd9
MD
376 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
377 USBDEV(sc->sc_dev));
378
984263bc
MD
379 USB_ATTACH_SUCCESS_RETURN;
380}
381
382int
383uscanneropen(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
1550dfd9 391 DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
984263bc
MD
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
445int
446uscannerclose(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
467void
468uscanner_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
504Static int
505uscanner_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
543int
544uscannerread(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
559Static int
560uscanner_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
593int
594uscannerwrite(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__)
609int
610uscanner_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);
984263bc
MD
617
618 case DVACT_DEACTIVATE:
619 sc->sc_dying = 1;
620 break;
621 }
622 return (0);
623}
624#endif
625
626USB_DETACH(uscanner)
627{
628 USB_DETACH_START(uscanner, sc);
629 int s;
630#if defined(__NetBSD__) || defined(__OpenBSD__)
631 int maj, mn;
984263bc
MD
632#endif
633
634#if defined(__NetBSD__) || defined(__OpenBSD__)
635 DPRINTF(("uscanner_detach: sc=%p flags=%d\n", sc, flags));
d47ac1a2 636#elif defined(__FreeBSD__) || defined(__DragonFly__)
984263bc
MD
637 DPRINTF(("uscanner_detach: sc=%p\n", sc));
638#endif
639
640 sc->sc_dying = 1;
641 sc->sc_dev_flags = 0; /* make close really close device */
642
643 /* Abort all pipes. Causes processes waiting for transfer to wake. */
644 if (sc->sc_bulkin_pipe != NULL)
645 usbd_abort_pipe(sc->sc_bulkin_pipe);
646 if (sc->sc_bulkout_pipe != NULL)
647 usbd_abort_pipe(sc->sc_bulkout_pipe);
648
649 s = splusb();
650 if (--sc->sc_refcnt >= 0) {
651 /* Wait for processes to go away. */
652 usb_detach_wait(USBDEV(sc->sc_dev));
653 }
654 splx(s);
655
656#if defined(__NetBSD__) || defined(__OpenBSD__)
657 /* locate the major number */
658 for (maj = 0; maj < nchrdev; maj++)
659 if (cdevsw[maj].d_open == uscanneropen)
660 break;
661
662 /* Nuke the vnodes for any open instances (calls close). */
663 mn = self->dv_unit * USB_MAX_ENDPOINTS;
664 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
d47ac1a2 665#elif defined(__FreeBSD__) || defined(__DragonFly__)
984263bc 666 /* destroy the device for the control endpoint */
e4c9c0c8 667 cdevsw_remove(&uscanner_cdevsw, -1, USBDEVUNIT(sc->sc_dev));
984263bc
MD
668#endif
669
1550dfd9
MD
670 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
671 USBDEV(sc->sc_dev));
672
984263bc
MD
673 return (0);
674}
675
676int
677uscannerpoll(dev_t dev, int events, usb_proc_ptr p)
678{
679 struct uscanner_softc *sc;
680 int revents = 0;
681
682 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
683
684 if (sc->sc_dying)
685 return (EIO);
686
1550dfd9 687 /*
984263bc
MD
688 * We have no easy way of determining if a read will
689 * yield any data or a write will happen.
690 * Pretend they will.
691 */
1550dfd9 692 revents |= events &
984263bc
MD
693 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
694
695 return (revents);
696}
697
d47ac1a2 698#if defined(__FreeBSD__) || defined(__DragonFly__)
984263bc
MD
699DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
700#endif