ugen.c revision 1.124.2.5 1 /* $NetBSD: ugen.c,v 1.124.2.5 2018/08/25 17:00:14 martin Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology.
10 *
11 * Copyright (c) 2006 BBN Technologies Corp. All rights reserved.
12 * Effort sponsored in part by the Defense Advanced Research Projects
13 * Agency (DARPA) and the Department of the Interior National Business
14 * Center under agreement number NBCHC050166.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.124.2.5 2018/08/25 17:00:14 martin Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_compat_netbsd.h"
44 #include "opt_usb.h"
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/kmem.h>
51 #include <sys/device.h>
52 #include <sys/ioctl.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 #include <sys/file.h>
56 #include <sys/select.h>
57 #include <sys/proc.h>
58 #include <sys/vnode.h>
59 #include <sys/poll.h>
60
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64
65 #ifdef UGEN_DEBUG
66 #define DPRINTF(x) if (ugendebug) printf x
67 #define DPRINTFN(n,x) if (ugendebug>(n)) printf x
68 int ugendebug = 0;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 #define UGEN_CHUNK 128 /* chunk size for read */
75 #define UGEN_IBSIZE 1020 /* buffer size */
76 #define UGEN_BBSIZE 1024
77
78 #define UGEN_NISOREQS 4 /* number of outstanding xfer requests */
79 #define UGEN_NISORFRMS 8 /* number of transactions per req */
80 #define UGEN_NISOFRAMES (UGEN_NISORFRMS * UGEN_NISOREQS)
81
82 #define UGEN_BULK_RA_WB_BUFSIZE 16384 /* default buffer size */
83 #define UGEN_BULK_RA_WB_BUFMAX (1 << 20) /* maximum allowed buffer */
84
85 struct isoreq {
86 struct ugen_endpoint *sce;
87 struct usbd_xfer *xfer;
88 void *dmabuf;
89 uint16_t sizes[UGEN_NISORFRMS];
90 };
91
92 struct ugen_endpoint {
93 struct ugen_softc *sc;
94 usb_endpoint_descriptor_t *edesc;
95 struct usbd_interface *iface;
96 int state;
97 #define UGEN_ASLP 0x02 /* waiting for data */
98 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
99 #define UGEN_BULK_RA 0x08 /* in bulk read-ahead mode */
100 #define UGEN_BULK_WB 0x10 /* in bulk write-behind mode */
101 #define UGEN_RA_WB_STOP 0x20 /* RA/WB xfer is stopped (buffer full/empty) */
102 struct usbd_pipe *pipeh;
103 struct clist q;
104 u_char *ibuf; /* start of buffer (circular for isoc) */
105 u_char *fill; /* location for input (isoc) */
106 u_char *limit; /* end of circular buffer (isoc) */
107 u_char *cur; /* current read location (isoc) */
108 uint32_t timeout;
109 uint32_t ra_wb_bufsize; /* requested size for RA/WB buffer */
110 uint32_t ra_wb_reqsize; /* requested xfer length for RA/WB */
111 uint32_t ra_wb_used; /* how much is in buffer */
112 uint32_t ra_wb_xferlen; /* current xfer length for RA/WB */
113 struct usbd_xfer *ra_wb_xfer;
114 struct isoreq isoreqs[UGEN_NISOREQS];
115 /* Keep these last; we don't overwrite them in ugen_set_config() */
116 #define UGEN_ENDPOINT_NONZERO_CRUFT offsetof(struct ugen_endpoint, rsel)
117 struct selinfo rsel;
118 kcondvar_t cv;
119 };
120
121 struct ugen_softc {
122 device_t sc_dev; /* base device */
123 struct usbd_device *sc_udev;
124
125 kmutex_t sc_lock;
126 kcondvar_t sc_detach_cv;
127
128 char sc_is_open[USB_MAX_ENDPOINTS];
129 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
130 #define OUT 0
131 #define IN 1
132
133 int sc_refcnt;
134 char sc_buffer[UGEN_BBSIZE];
135 u_char sc_dying;
136 };
137
138 dev_type_open(ugenopen);
139 dev_type_close(ugenclose);
140 dev_type_read(ugenread);
141 dev_type_write(ugenwrite);
142 dev_type_ioctl(ugenioctl);
143 dev_type_poll(ugenpoll);
144 dev_type_kqfilter(ugenkqfilter);
145
146 const struct cdevsw ugen_cdevsw = {
147 .d_open = ugenopen,
148 .d_close = ugenclose,
149 .d_read = ugenread,
150 .d_write = ugenwrite,
151 .d_ioctl = ugenioctl,
152 .d_stop = nostop,
153 .d_tty = notty,
154 .d_poll = ugenpoll,
155 .d_mmap = nommap,
156 .d_kqfilter = ugenkqfilter,
157 .d_discard = nodiscard,
158 .d_flag = D_OTHER,
159 };
160
161 Static void ugenintr(struct usbd_xfer *, void *,
162 usbd_status);
163 Static void ugen_isoc_rintr(struct usbd_xfer *, void *,
164 usbd_status);
165 Static void ugen_bulkra_intr(struct usbd_xfer *, void *,
166 usbd_status);
167 Static void ugen_bulkwb_intr(struct usbd_xfer *, void *,
168 usbd_status);
169 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
170 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
171 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
172 void *, int, struct lwp *);
173 Static int ugen_set_config(struct ugen_softc *, int);
174 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *,
175 int, int *);
176 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
177 Static int ugen_get_alt_index(struct ugen_softc *, int);
178 Static void ugen_clear_endpoints(struct ugen_softc *);
179
180 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
181 #define UGENENDPOINT(n) (minor(n) & 0xf)
182 #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
183
184 int ugen_match(device_t, cfdata_t, void *);
185 void ugen_attach(device_t, device_t, void *);
186 int ugen_detach(device_t, int);
187 int ugen_activate(device_t, enum devact);
188 extern struct cfdriver ugen_cd;
189 CFATTACH_DECL_NEW(ugen, sizeof(struct ugen_softc), ugen_match, ugen_attach,
190 ugen_detach, ugen_activate);
191
192 /* toggle to control attach priority. -1 means "let autoconf decide" */
193 int ugen_override = -1;
194
195 int
196 ugen_match(device_t parent, cfdata_t match, void *aux)
197 {
198 struct usb_attach_arg *uaa = aux;
199 int override;
200
201 if (ugen_override != -1)
202 override = ugen_override;
203 else
204 override = match->cf_flags & 1;
205
206 if (override)
207 return UMATCH_HIGHEST;
208 else if (uaa->uaa_usegeneric)
209 return UMATCH_GENERIC;
210 else
211 return UMATCH_NONE;
212 }
213
214 void
215 ugen_attach(device_t parent, device_t self, void *aux)
216 {
217 struct ugen_softc *sc = device_private(self);
218 struct usb_attach_arg *uaa = aux;
219 struct usbd_device *udev;
220 char *devinfop;
221 usbd_status err;
222 int i, dir, conf;
223
224 aprint_naive("\n");
225 aprint_normal("\n");
226
227 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
228 cv_init(&sc->sc_detach_cv, "ugendet");
229
230 devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
231 aprint_normal_dev(self, "%s\n", devinfop);
232 usbd_devinfo_free(devinfop);
233
234 sc->sc_dev = self;
235 sc->sc_udev = udev = uaa->uaa_device;
236
237 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
238 for (dir = OUT; dir <= IN; dir++) {
239 struct ugen_endpoint *sce;
240
241 sce = &sc->sc_endpoints[i][dir];
242 selinit(&sce->rsel);
243 cv_init(&sce->cv, "ugensce");
244 }
245 }
246
247 /* First set configuration index 0, the default one for ugen. */
248 err = usbd_set_config_index(udev, 0, 0);
249 if (err) {
250 aprint_error_dev(self,
251 "setting configuration index 0 failed\n");
252 sc->sc_dying = 1;
253 return;
254 }
255 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
256
257 /* Set up all the local state for this configuration. */
258 err = ugen_set_config(sc, conf);
259 if (err) {
260 aprint_error_dev(self, "setting configuration %d failed\n",
261 conf);
262 sc->sc_dying = 1;
263 return;
264 }
265
266 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
267
268 if (!pmf_device_register(self, NULL, NULL))
269 aprint_error_dev(self, "couldn't establish power handler\n");
270
271 return;
272 }
273
274 Static void
275 ugen_clear_endpoints(struct ugen_softc *sc)
276 {
277
278 /* Clear out the old info, but leave the selinfo and cv initialised. */
279 for (int i = 0; i < USB_MAX_ENDPOINTS; i++) {
280 for (int dir = OUT; dir <= IN; dir++) {
281 struct ugen_endpoint *sce = &sc->sc_endpoints[i][dir];
282 memset(sce, 0, UGEN_ENDPOINT_NONZERO_CRUFT);
283 }
284 }
285 }
286
287 Static int
288 ugen_set_config(struct ugen_softc *sc, int configno)
289 {
290 struct usbd_device *dev = sc->sc_udev;
291 usb_config_descriptor_t *cdesc;
292 struct usbd_interface *iface;
293 usb_endpoint_descriptor_t *ed;
294 struct ugen_endpoint *sce;
295 uint8_t niface, nendpt;
296 int ifaceno, endptno, endpt;
297 usbd_status err;
298 int dir;
299
300 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
301 device_xname(sc->sc_dev), configno, sc));
302
303 /*
304 * We start at 1, not 0, because we don't care whether the
305 * control endpoint is open or not. It is always present.
306 */
307 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
308 if (sc->sc_is_open[endptno]) {
309 DPRINTFN(1,
310 ("ugen_set_config: %s - endpoint %d is open\n",
311 device_xname(sc->sc_dev), endptno));
312 return USBD_IN_USE;
313 }
314
315 /* Avoid setting the current value. */
316 cdesc = usbd_get_config_descriptor(dev);
317 if (!cdesc || cdesc->bConfigurationValue != configno) {
318 err = usbd_set_config_no(dev, configno, 1);
319 if (err)
320 return err;
321 }
322
323 err = usbd_interface_count(dev, &niface);
324 if (err)
325 return err;
326
327 ugen_clear_endpoints(sc);
328
329 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
330 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
331 err = usbd_device2interface_handle(dev, ifaceno, &iface);
332 if (err)
333 return err;
334 err = usbd_endpoint_count(iface, &nendpt);
335 if (err)
336 return err;
337 for (endptno = 0; endptno < nendpt; endptno++) {
338 ed = usbd_interface2endpoint_descriptor(iface,endptno);
339 KASSERT(ed != NULL);
340 endpt = ed->bEndpointAddress;
341 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
342 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
343 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
344 "(%d,%d), sce=%p\n",
345 endptno, endpt, UE_GET_ADDR(endpt),
346 UE_GET_DIR(endpt), sce));
347 sce->sc = sc;
348 sce->edesc = ed;
349 sce->iface = iface;
350 }
351 }
352 return USBD_NORMAL_COMPLETION;
353 }
354
355 int
356 ugenopen(dev_t dev, int flag, int mode, struct lwp *l)
357 {
358 struct ugen_softc *sc;
359 int unit = UGENUNIT(dev);
360 int endpt = UGENENDPOINT(dev);
361 usb_endpoint_descriptor_t *edesc;
362 struct ugen_endpoint *sce;
363 int dir, isize;
364 usbd_status err;
365 struct usbd_xfer *xfer;
366 int i, j;
367
368 sc = device_lookup_private(&ugen_cd, unit);
369 if (sc == NULL || sc->sc_dying)
370 return ENXIO;
371
372 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
373 flag, mode, unit, endpt));
374
375 /* The control endpoint allows multiple opens. */
376 if (endpt == USB_CONTROL_ENDPOINT) {
377 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
378 return 0;
379 }
380
381 if (sc->sc_is_open[endpt])
382 return EBUSY;
383
384 /* Make sure there are pipes for all directions. */
385 for (dir = OUT; dir <= IN; dir++) {
386 if (flag & (dir == OUT ? FWRITE : FREAD)) {
387 sce = &sc->sc_endpoints[endpt][dir];
388 if (sce->edesc == NULL)
389 return ENXIO;
390 }
391 }
392
393 /* Actually open the pipes. */
394 /* XXX Should back out properly if it fails. */
395 for (dir = OUT; dir <= IN; dir++) {
396 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
397 continue;
398 sce = &sc->sc_endpoints[endpt][dir];
399 sce->state = 0;
400 sce->timeout = USBD_NO_TIMEOUT;
401 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
402 sc, endpt, dir, sce));
403 edesc = sce->edesc;
404 switch (edesc->bmAttributes & UE_XFERTYPE) {
405 case UE_INTERRUPT:
406 if (dir == OUT) {
407 err = usbd_open_pipe(sce->iface,
408 edesc->bEndpointAddress, 0, &sce->pipeh);
409 if (err)
410 return EIO;
411 break;
412 }
413 isize = UGETW(edesc->wMaxPacketSize);
414 if (isize == 0) /* shouldn't happen */
415 return EINVAL;
416 sce->ibuf = kmem_alloc(isize, KM_SLEEP);
417 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
418 endpt, isize));
419 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1) {
420 kmem_free(sce->ibuf, isize);
421 sce->ibuf = NULL;
422 return ENOMEM;
423 }
424 err = usbd_open_pipe_intr(sce->iface,
425 edesc->bEndpointAddress,
426 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
427 sce->ibuf, isize, ugenintr,
428 USBD_DEFAULT_INTERVAL);
429 if (err) {
430 clfree(&sce->q);
431 kmem_free(sce->ibuf, isize);
432 sce->ibuf = NULL;
433 return EIO;
434 }
435 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
436 break;
437 case UE_BULK:
438 err = usbd_open_pipe(sce->iface,
439 edesc->bEndpointAddress, 0, &sce->pipeh);
440 if (err)
441 return EIO;
442 sce->ra_wb_bufsize = UGEN_BULK_RA_WB_BUFSIZE;
443 /*
444 * Use request size for non-RA/WB transfers
445 * as the default.
446 */
447 sce->ra_wb_reqsize = UGEN_BBSIZE;
448 break;
449 case UE_ISOCHRONOUS:
450 if (dir == OUT)
451 return EINVAL;
452 isize = UGETW(edesc->wMaxPacketSize);
453 if (isize == 0) /* shouldn't happen */
454 return EINVAL;
455 sce->ibuf = kmem_alloc(isize * UGEN_NISOFRAMES,
456 KM_SLEEP);
457 sce->cur = sce->fill = sce->ibuf;
458 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
459 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
460 endpt, isize));
461 err = usbd_open_pipe(sce->iface,
462 edesc->bEndpointAddress, 0, &sce->pipeh);
463 if (err) {
464 kmem_free(sce->ibuf, isize * UGEN_NISOFRAMES);
465 sce->ibuf = NULL;
466 return EIO;
467 }
468 for (i = 0; i < UGEN_NISOREQS; ++i) {
469 sce->isoreqs[i].sce = sce;
470 err = usbd_create_xfer(sce->pipeh,
471 isize * UGEN_NISORFRMS, 0, UGEN_NISORFRMS,
472 &xfer);
473 if (err)
474 goto bad;
475 sce->isoreqs[i].xfer = xfer;
476 sce->isoreqs[i].dmabuf = usbd_get_buffer(xfer);
477 for (j = 0; j < UGEN_NISORFRMS; ++j)
478 sce->isoreqs[i].sizes[j] = isize;
479 usbd_setup_isoc_xfer(xfer, &sce->isoreqs[i],
480 sce->isoreqs[i].sizes, UGEN_NISORFRMS, 0,
481 ugen_isoc_rintr);
482 (void)usbd_transfer(xfer);
483 }
484 DPRINTFN(5, ("ugenopen: isoc open done\n"));
485 break;
486 bad:
487 while (--i >= 0) /* implicit buffer free */
488 usbd_destroy_xfer(sce->isoreqs[i].xfer);
489 usbd_close_pipe(sce->pipeh);
490 sce->pipeh = NULL;
491 kmem_free(sce->ibuf, isize * UGEN_NISOFRAMES);
492 sce->ibuf = NULL;
493 return ENOMEM;
494 case UE_CONTROL:
495 sce->timeout = USBD_DEFAULT_TIMEOUT;
496 return EINVAL;
497 }
498 }
499 sc->sc_is_open[endpt] = 1;
500 return 0;
501 }
502
503 int
504 ugenclose(dev_t dev, int flag, int mode, struct lwp *l)
505 {
506 int endpt = UGENENDPOINT(dev);
507 struct ugen_softc *sc;
508 struct ugen_endpoint *sce;
509 int dir;
510 int i;
511
512 sc = device_lookup_private(& ugen_cd, UGENUNIT(dev));
513 if (sc == NULL || sc->sc_dying)
514 return ENXIO;
515
516 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
517 flag, mode, UGENUNIT(dev), endpt));
518
519 #ifdef DIAGNOSTIC
520 if (!sc->sc_is_open[endpt]) {
521 printf("ugenclose: not open\n");
522 return EINVAL;
523 }
524 #endif
525
526 if (endpt == USB_CONTROL_ENDPOINT) {
527 DPRINTFN(5, ("ugenclose: close control\n"));
528 sc->sc_is_open[endpt] = 0;
529 return 0;
530 }
531
532 for (dir = OUT; dir <= IN; dir++) {
533 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
534 continue;
535 sce = &sc->sc_endpoints[endpt][dir];
536 if (sce->pipeh == NULL)
537 continue;
538 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
539 endpt, dir, sce));
540
541 usbd_abort_pipe(sce->pipeh);
542
543 int isize = UGETW(sce->edesc->wMaxPacketSize);
544 int msize = 0;
545
546 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
547 case UE_INTERRUPT:
548 ndflush(&sce->q, sce->q.c_cc);
549 clfree(&sce->q);
550 msize = isize;
551 break;
552 case UE_ISOCHRONOUS:
553 for (i = 0; i < UGEN_NISOREQS; ++i)
554 usbd_destroy_xfer(sce->isoreqs[i].xfer);
555 msize = isize * UGEN_NISOFRAMES;
556 break;
557 case UE_BULK:
558 if (sce->state & (UGEN_BULK_RA | UGEN_BULK_WB)) {
559 usbd_destroy_xfer(sce->ra_wb_xfer);
560 msize = sce->ra_wb_bufsize;
561 }
562 break;
563 default:
564 break;
565 }
566 usbd_close_pipe(sce->pipeh);
567 sce->pipeh = NULL;
568 if (sce->ibuf != NULL) {
569 kmem_free(sce->ibuf, msize);
570 sce->ibuf = NULL;
571 }
572 }
573 sc->sc_is_open[endpt] = 0;
574
575 return 0;
576 }
577
578 Static int
579 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
580 {
581 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
582 uint32_t n, tn;
583 struct usbd_xfer *xfer;
584 usbd_status err;
585 int error = 0;
586
587 DPRINTFN(5, ("%s: ugenread: %d\n", device_xname(sc->sc_dev), endpt));
588
589 if (endpt == USB_CONTROL_ENDPOINT)
590 return ENODEV;
591
592 #ifdef DIAGNOSTIC
593 if (sce->edesc == NULL) {
594 printf("ugenread: no edesc\n");
595 return EIO;
596 }
597 if (sce->pipeh == NULL) {
598 printf("ugenread: no pipe\n");
599 return EIO;
600 }
601 #endif
602
603 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
604 case UE_INTERRUPT:
605 /* Block until activity occurred. */
606 mutex_enter(&sc->sc_lock);
607 while (sce->q.c_cc == 0) {
608 if (flag & IO_NDELAY) {
609 mutex_exit(&sc->sc_lock);
610 return EWOULDBLOCK;
611 }
612 sce->state |= UGEN_ASLP;
613 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
614 /* "ugenri" */
615 error = cv_timedwait_sig(&sce->cv, &sc->sc_lock,
616 mstohz(sce->timeout));
617 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
618 if (sc->sc_dying)
619 error = EIO;
620 if (error) {
621 sce->state &= ~UGEN_ASLP;
622 break;
623 }
624 }
625 mutex_exit(&sc->sc_lock);
626
627 /* Transfer as many chunks as possible. */
628 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
629 n = min(sce->q.c_cc, uio->uio_resid);
630 if (n > sizeof(sc->sc_buffer))
631 n = sizeof(sc->sc_buffer);
632
633 /* Remove a small chunk from the input queue. */
634 q_to_b(&sce->q, sc->sc_buffer, n);
635 DPRINTFN(5, ("ugenread: got %d chars\n", n));
636
637 /* Copy the data to the user process. */
638 error = uiomove(sc->sc_buffer, n, uio);
639 if (error)
640 break;
641 }
642 break;
643 case UE_BULK:
644 if (sce->state & UGEN_BULK_RA) {
645 DPRINTFN(5, ("ugenread: BULK_RA req: %zd used: %d\n",
646 uio->uio_resid, sce->ra_wb_used));
647 xfer = sce->ra_wb_xfer;
648
649 mutex_enter(&sc->sc_lock);
650 if (sce->ra_wb_used == 0 && flag & IO_NDELAY) {
651 mutex_exit(&sc->sc_lock);
652 return EWOULDBLOCK;
653 }
654 while (uio->uio_resid > 0 && !error) {
655 while (sce->ra_wb_used == 0) {
656 sce->state |= UGEN_ASLP;
657 DPRINTFN(5,
658 ("ugenread: sleep on %p\n",
659 sce));
660 /* "ugenrb" */
661 error = cv_timedwait_sig(&sce->cv,
662 &sc->sc_lock, mstohz(sce->timeout));
663 DPRINTFN(5,
664 ("ugenread: woke, error=%d\n",
665 error));
666 if (sc->sc_dying)
667 error = EIO;
668 if (error) {
669 sce->state &= ~UGEN_ASLP;
670 break;
671 }
672 }
673
674 /* Copy data to the process. */
675 while (uio->uio_resid > 0
676 && sce->ra_wb_used > 0) {
677 n = min(uio->uio_resid,
678 sce->ra_wb_used);
679 n = min(n, sce->limit - sce->cur);
680 error = uiomove(sce->cur, n, uio);
681 if (error)
682 break;
683 sce->cur += n;
684 sce->ra_wb_used -= n;
685 if (sce->cur == sce->limit)
686 sce->cur = sce->ibuf;
687 }
688
689 /*
690 * If the transfers stopped because the
691 * buffer was full, restart them.
692 */
693 if (sce->state & UGEN_RA_WB_STOP &&
694 sce->ra_wb_used < sce->limit - sce->ibuf) {
695 n = (sce->limit - sce->ibuf)
696 - sce->ra_wb_used;
697 usbd_setup_xfer(xfer, sce, NULL,
698 min(n, sce->ra_wb_xferlen),
699 0, USBD_NO_TIMEOUT,
700 ugen_bulkra_intr);
701 sce->state &= ~UGEN_RA_WB_STOP;
702 err = usbd_transfer(xfer);
703 if (err != USBD_IN_PROGRESS)
704 /*
705 * The transfer has not been
706 * queued. Setting STOP
707 * will make us try
708 * again at the next read.
709 */
710 sce->state |= UGEN_RA_WB_STOP;
711 }
712 }
713 mutex_exit(&sc->sc_lock);
714 break;
715 }
716 error = usbd_create_xfer(sce->pipeh, UGEN_BBSIZE,
717 0, 0, &xfer);
718 if (error)
719 return error;
720 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
721 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
722 tn = n;
723 err = usbd_bulk_transfer(xfer, sce->pipeh,
724 sce->state & UGEN_SHORT_OK ? USBD_SHORT_XFER_OK : 0,
725 sce->timeout, sc->sc_buffer, &tn);
726 if (err) {
727 if (err == USBD_INTERRUPTED)
728 error = EINTR;
729 else if (err == USBD_TIMEOUT)
730 error = ETIMEDOUT;
731 else
732 error = EIO;
733 break;
734 }
735 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
736 error = uiomove(sc->sc_buffer, tn, uio);
737 if (error || tn < n)
738 break;
739 }
740 usbd_destroy_xfer(xfer);
741 break;
742 case UE_ISOCHRONOUS:
743 mutex_enter(&sc->sc_lock);
744 while (sce->cur == sce->fill) {
745 if (flag & IO_NDELAY) {
746 mutex_exit(&sc->sc_lock);
747 return EWOULDBLOCK;
748 }
749 sce->state |= UGEN_ASLP;
750 /* "ugenri" */
751 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
752 error = cv_timedwait_sig(&sce->cv, &sc->sc_lock,
753 mstohz(sce->timeout));
754 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
755 if (sc->sc_dying)
756 error = EIO;
757 if (error) {
758 sce->state &= ~UGEN_ASLP;
759 break;
760 }
761 }
762
763 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
764 if(sce->fill > sce->cur)
765 n = min(sce->fill - sce->cur, uio->uio_resid);
766 else
767 n = min(sce->limit - sce->cur, uio->uio_resid);
768
769 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
770
771 /* Copy the data to the user process. */
772 error = uiomove(sce->cur, n, uio);
773 if (error)
774 break;
775 sce->cur += n;
776 if (sce->cur >= sce->limit)
777 sce->cur = sce->ibuf;
778 }
779 mutex_exit(&sc->sc_lock);
780 break;
781
782
783 default:
784 return ENXIO;
785 }
786 return error;
787 }
788
789 int
790 ugenread(dev_t dev, struct uio *uio, int flag)
791 {
792 int endpt = UGENENDPOINT(dev);
793 struct ugen_softc *sc;
794 int error;
795
796 sc = device_lookup_private(& ugen_cd, UGENUNIT(dev));
797 if (sc == NULL || sc->sc_dying)
798 return ENXIO;
799
800 mutex_enter(&sc->sc_lock);
801 sc->sc_refcnt++;
802 mutex_exit(&sc->sc_lock);
803
804 error = ugen_do_read(sc, endpt, uio, flag);
805
806 mutex_enter(&sc->sc_lock);
807 if (--sc->sc_refcnt < 0)
808 cv_broadcast(&sc->sc_detach_cv);
809 mutex_exit(&sc->sc_lock);
810
811 return error;
812 }
813
814 Static int
815 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio,
816 int flag)
817 {
818 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
819 uint32_t n;
820 int error = 0;
821 uint32_t tn;
822 char *dbuf;
823 struct usbd_xfer *xfer;
824 usbd_status err;
825
826 DPRINTFN(5, ("%s: ugenwrite: %d\n", device_xname(sc->sc_dev), endpt));
827
828 if (endpt == USB_CONTROL_ENDPOINT)
829 return ENODEV;
830
831 #ifdef DIAGNOSTIC
832 if (sce->edesc == NULL) {
833 printf("ugenwrite: no edesc\n");
834 return EIO;
835 }
836 if (sce->pipeh == NULL) {
837 printf("ugenwrite: no pipe\n");
838 return EIO;
839 }
840 #endif
841
842 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
843 case UE_BULK:
844 if (sce->state & UGEN_BULK_WB) {
845 DPRINTFN(5, ("ugenwrite: BULK_WB req: %zd used: %d\n",
846 uio->uio_resid, sce->ra_wb_used));
847 xfer = sce->ra_wb_xfer;
848
849 mutex_enter(&sc->sc_lock);
850 if (sce->ra_wb_used == sce->limit - sce->ibuf &&
851 flag & IO_NDELAY) {
852 mutex_exit(&sc->sc_lock);
853 return EWOULDBLOCK;
854 }
855 while (uio->uio_resid > 0 && !error) {
856 while (sce->ra_wb_used ==
857 sce->limit - sce->ibuf) {
858 sce->state |= UGEN_ASLP;
859 DPRINTFN(5,
860 ("ugenwrite: sleep on %p\n",
861 sce));
862 /* "ugenwb" */
863 error = cv_timedwait_sig(&sce->cv,
864 &sc->sc_lock, mstohz(sce->timeout));
865 DPRINTFN(5,
866 ("ugenwrite: woke, error=%d\n",
867 error));
868 if (sc->sc_dying)
869 error = EIO;
870 if (error) {
871 sce->state &= ~UGEN_ASLP;
872 break;
873 }
874 }
875
876 /* Copy data from the process. */
877 while (uio->uio_resid > 0 &&
878 sce->ra_wb_used < sce->limit - sce->ibuf) {
879 n = min(uio->uio_resid,
880 (sce->limit - sce->ibuf)
881 - sce->ra_wb_used);
882 n = min(n, sce->limit - sce->fill);
883 error = uiomove(sce->fill, n, uio);
884 if (error)
885 break;
886 sce->fill += n;
887 sce->ra_wb_used += n;
888 if (sce->fill == sce->limit)
889 sce->fill = sce->ibuf;
890 }
891
892 /*
893 * If the transfers stopped because the
894 * buffer was empty, restart them.
895 */
896 if (sce->state & UGEN_RA_WB_STOP &&
897 sce->ra_wb_used > 0) {
898 dbuf = (char *)usbd_get_buffer(xfer);
899 n = min(sce->ra_wb_used,
900 sce->ra_wb_xferlen);
901 tn = min(n, sce->limit - sce->cur);
902 memcpy(dbuf, sce->cur, tn);
903 dbuf += tn;
904 if (n - tn > 0)
905 memcpy(dbuf, sce->ibuf,
906 n - tn);
907 usbd_setup_xfer(xfer, sce, NULL, n,
908 0, USBD_NO_TIMEOUT,
909 ugen_bulkwb_intr);
910 sce->state &= ~UGEN_RA_WB_STOP;
911 err = usbd_transfer(xfer);
912 if (err != USBD_IN_PROGRESS)
913 /*
914 * The transfer has not been
915 * queued. Setting STOP
916 * will make us try again
917 * at the next read.
918 */
919 sce->state |= UGEN_RA_WB_STOP;
920 }
921 }
922 mutex_exit(&sc->sc_lock);
923 break;
924 }
925 error = usbd_create_xfer(sce->pipeh, UGEN_BBSIZE,
926 0, 0, &xfer);
927 if (error)
928 return error;
929 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
930 error = uiomove(sc->sc_buffer, n, uio);
931 if (error)
932 break;
933 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
934 err = usbd_bulk_transfer(xfer, sce->pipeh, 0, sce->timeout,
935 sc->sc_buffer, &n);
936 if (err) {
937 if (err == USBD_INTERRUPTED)
938 error = EINTR;
939 else if (err == USBD_TIMEOUT)
940 error = ETIMEDOUT;
941 else
942 error = EIO;
943 break;
944 }
945 }
946 usbd_destroy_xfer(xfer);
947 break;
948 case UE_INTERRUPT:
949 error = usbd_create_xfer(sce->pipeh,
950 UGETW(sce->edesc->wMaxPacketSize), 0, 0, &xfer);
951 if (error)
952 return error;
953 while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
954 uio->uio_resid)) != 0) {
955 error = uiomove(sc->sc_buffer, n, uio);
956 if (error)
957 break;
958 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
959 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
960 sce->timeout, sc->sc_buffer, &n);
961 if (err) {
962 if (err == USBD_INTERRUPTED)
963 error = EINTR;
964 else if (err == USBD_TIMEOUT)
965 error = ETIMEDOUT;
966 else
967 error = EIO;
968 break;
969 }
970 }
971 usbd_destroy_xfer(xfer);
972 break;
973 default:
974 return ENXIO;
975 }
976 return error;
977 }
978
979 int
980 ugenwrite(dev_t dev, struct uio *uio, int flag)
981 {
982 int endpt = UGENENDPOINT(dev);
983 struct ugen_softc *sc;
984 int error;
985
986 sc = device_lookup_private(& ugen_cd, UGENUNIT(dev));
987 if (sc == NULL || sc->sc_dying)
988 return ENXIO;
989
990 mutex_enter(&sc->sc_lock);
991 sc->sc_refcnt++;
992 mutex_exit(&sc->sc_lock);
993
994 error = ugen_do_write(sc, endpt, uio, flag);
995
996 mutex_enter(&sc->sc_lock);
997 if (--sc->sc_refcnt < 0)
998 cv_broadcast(&sc->sc_detach_cv);
999 mutex_exit(&sc->sc_lock);
1000
1001 return error;
1002 }
1003
1004 int
1005 ugen_activate(device_t self, enum devact act)
1006 {
1007 struct ugen_softc *sc = device_private(self);
1008
1009 switch (act) {
1010 case DVACT_DEACTIVATE:
1011 sc->sc_dying = 1;
1012 return 0;
1013 default:
1014 return EOPNOTSUPP;
1015 }
1016 }
1017
1018 int
1019 ugen_detach(device_t self, int flags)
1020 {
1021 struct ugen_softc *sc = device_private(self);
1022 struct ugen_endpoint *sce;
1023 int i, dir;
1024 int maj, mn;
1025
1026 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
1027
1028 sc->sc_dying = 1;
1029 pmf_device_deregister(self);
1030 /* Abort all pipes. Causes processes waiting for transfer to wake. */
1031 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
1032 for (dir = OUT; dir <= IN; dir++) {
1033 sce = &sc->sc_endpoints[i][dir];
1034 if (sce->pipeh)
1035 usbd_abort_pipe(sce->pipeh);
1036 }
1037 }
1038
1039 mutex_enter(&sc->sc_lock);
1040 if (--sc->sc_refcnt >= 0) {
1041 /* Wake everyone */
1042 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
1043 cv_signal(&sc->sc_endpoints[i][IN].cv);
1044 /* Wait for processes to go away. */
1045 if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60)) {
1046 printf("%s: %s didn't detach\n", __func__,
1047 device_xname(sc->sc_dev));
1048 }
1049 }
1050 mutex_exit(&sc->sc_lock);
1051
1052 /* locate the major number */
1053 maj = cdevsw_lookup_major(&ugen_cdevsw);
1054
1055 /* Nuke the vnodes for any open instances (calls close). */
1056 mn = device_unit(self) * USB_MAX_ENDPOINTS;
1057 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
1058
1059 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
1060
1061 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
1062 for (dir = OUT; dir <= IN; dir++) {
1063 sce = &sc->sc_endpoints[i][dir];
1064 seldestroy(&sce->rsel);
1065 cv_destroy(&sce->cv);
1066 }
1067 }
1068
1069 cv_destroy(&sc->sc_detach_cv);
1070 mutex_destroy(&sc->sc_lock);
1071
1072 return 0;
1073 }
1074
1075 Static void
1076 ugenintr(struct usbd_xfer *xfer, void *addr, usbd_status status)
1077 {
1078 struct ugen_endpoint *sce = addr;
1079 struct ugen_softc *sc = sce->sc;
1080 uint32_t count;
1081 u_char *ibuf;
1082
1083 if (status == USBD_CANCELLED)
1084 return;
1085
1086 if (status != USBD_NORMAL_COMPLETION) {
1087 DPRINTF(("ugenintr: status=%d\n", status));
1088 if (status == USBD_STALLED)
1089 usbd_clear_endpoint_stall_async(sce->pipeh);
1090 return;
1091 }
1092
1093 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1094 ibuf = sce->ibuf;
1095
1096 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
1097 xfer, status, count));
1098 DPRINTFN(5, (" data = %02x %02x %02x\n",
1099 ibuf[0], ibuf[1], ibuf[2]));
1100
1101 (void)b_to_q(ibuf, count, &sce->q);
1102
1103 mutex_enter(&sc->sc_lock);
1104 if (sce->state & UGEN_ASLP) {
1105 sce->state &= ~UGEN_ASLP;
1106 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
1107 cv_signal(&sce->cv);
1108 }
1109 mutex_exit(&sc->sc_lock);
1110 selnotify(&sce->rsel, 0, 0);
1111 }
1112
1113 Static void
1114 ugen_isoc_rintr(struct usbd_xfer *xfer, void *addr,
1115 usbd_status status)
1116 {
1117 struct isoreq *req = addr;
1118 struct ugen_endpoint *sce = req->sce;
1119 struct ugen_softc *sc = sce->sc;
1120 uint32_t count, n;
1121 int i, isize;
1122
1123 /* Return if we are aborting. */
1124 if (status == USBD_CANCELLED)
1125 return;
1126
1127 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1128 DPRINTFN(5,("ugen_isoc_rintr: xfer %ld, count=%d\n",
1129 (long)(req - sce->isoreqs), count));
1130
1131 /* throw away oldest input if the buffer is full */
1132 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
1133 sce->cur += count;
1134 if(sce->cur >= sce->limit)
1135 sce->cur = sce->ibuf + (sce->limit - sce->cur);
1136 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
1137 count));
1138 }
1139
1140 isize = UGETW(sce->edesc->wMaxPacketSize);
1141 for (i = 0; i < UGEN_NISORFRMS; i++) {
1142 uint32_t actlen = req->sizes[i];
1143 char const *tbuf = (char const *)req->dmabuf + isize * i;
1144
1145 /* copy data to buffer */
1146 while (actlen > 0) {
1147 n = min(actlen, sce->limit - sce->fill);
1148 memcpy(sce->fill, tbuf, n);
1149
1150 tbuf += n;
1151 actlen -= n;
1152 sce->fill += n;
1153 if(sce->fill == sce->limit)
1154 sce->fill = sce->ibuf;
1155 }
1156
1157 /* setup size for next transfer */
1158 req->sizes[i] = isize;
1159 }
1160
1161 usbd_setup_isoc_xfer(xfer, req, req->sizes, UGEN_NISORFRMS, 0,
1162 ugen_isoc_rintr);
1163 (void)usbd_transfer(xfer);
1164
1165 mutex_enter(&sc->sc_lock);
1166 if (sce->state & UGEN_ASLP) {
1167 sce->state &= ~UGEN_ASLP;
1168 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
1169 cv_signal(&sce->cv);
1170 }
1171 mutex_exit(&sc->sc_lock);
1172 selnotify(&sce->rsel, 0, 0);
1173 }
1174
1175 Static void
1176 ugen_bulkra_intr(struct usbd_xfer *xfer, void *addr,
1177 usbd_status status)
1178 {
1179 struct ugen_endpoint *sce = addr;
1180 struct ugen_softc *sc = sce->sc;
1181 uint32_t count, n;
1182 char const *tbuf;
1183 usbd_status err;
1184
1185 /* Return if we are aborting. */
1186 if (status == USBD_CANCELLED)
1187 return;
1188
1189 if (status != USBD_NORMAL_COMPLETION) {
1190 DPRINTF(("ugen_bulkra_intr: status=%d\n", status));
1191 sce->state |= UGEN_RA_WB_STOP;
1192 if (status == USBD_STALLED)
1193 usbd_clear_endpoint_stall_async(sce->pipeh);
1194 return;
1195 }
1196
1197 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1198
1199 /* Keep track of how much is in the buffer. */
1200 sce->ra_wb_used += count;
1201
1202 /* Copy data to buffer. */
1203 tbuf = (char const *)usbd_get_buffer(sce->ra_wb_xfer);
1204 n = min(count, sce->limit - sce->fill);
1205 memcpy(sce->fill, tbuf, n);
1206 tbuf += n;
1207 count -= n;
1208 sce->fill += n;
1209 if (sce->fill == sce->limit)
1210 sce->fill = sce->ibuf;
1211 if (count > 0) {
1212 memcpy(sce->fill, tbuf, count);
1213 sce->fill += count;
1214 }
1215
1216 /* Set up the next request if necessary. */
1217 n = (sce->limit - sce->ibuf) - sce->ra_wb_used;
1218 if (n > 0) {
1219 usbd_setup_xfer(xfer, sce, NULL, min(n, sce->ra_wb_xferlen), 0,
1220 USBD_NO_TIMEOUT, ugen_bulkra_intr);
1221 err = usbd_transfer(xfer);
1222 if (err != USBD_IN_PROGRESS) {
1223 printf("usbd_bulkra_intr: error=%d\n", err);
1224 /*
1225 * The transfer has not been queued. Setting STOP
1226 * will make us try again at the next read.
1227 */
1228 sce->state |= UGEN_RA_WB_STOP;
1229 }
1230 }
1231 else
1232 sce->state |= UGEN_RA_WB_STOP;
1233
1234 mutex_enter(&sc->sc_lock);
1235 if (sce->state & UGEN_ASLP) {
1236 sce->state &= ~UGEN_ASLP;
1237 DPRINTFN(5, ("ugen_bulkra_intr: waking %p\n", sce));
1238 cv_signal(&sce->cv);
1239 }
1240 mutex_exit(&sc->sc_lock);
1241 selnotify(&sce->rsel, 0, 0);
1242 }
1243
1244 Static void
1245 ugen_bulkwb_intr(struct usbd_xfer *xfer, void *addr,
1246 usbd_status status)
1247 {
1248 struct ugen_endpoint *sce = addr;
1249 struct ugen_softc *sc = sce->sc;
1250 uint32_t count, n;
1251 char *tbuf;
1252 usbd_status err;
1253
1254 /* Return if we are aborting. */
1255 if (status == USBD_CANCELLED)
1256 return;
1257
1258 if (status != USBD_NORMAL_COMPLETION) {
1259 DPRINTF(("ugen_bulkwb_intr: status=%d\n", status));
1260 sce->state |= UGEN_RA_WB_STOP;
1261 if (status == USBD_STALLED)
1262 usbd_clear_endpoint_stall_async(sce->pipeh);
1263 return;
1264 }
1265
1266 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1267
1268 /* Keep track of how much is in the buffer. */
1269 sce->ra_wb_used -= count;
1270
1271 /* Update buffer pointers. */
1272 sce->cur += count;
1273 if (sce->cur >= sce->limit)
1274 sce->cur = sce->ibuf + (sce->cur - sce->limit);
1275
1276 /* Set up next request if necessary. */
1277 if (sce->ra_wb_used > 0) {
1278 /* copy data from buffer */
1279 tbuf = (char *)usbd_get_buffer(sce->ra_wb_xfer);
1280 count = min(sce->ra_wb_used, sce->ra_wb_xferlen);
1281 n = min(count, sce->limit - sce->cur);
1282 memcpy(tbuf, sce->cur, n);
1283 tbuf += n;
1284 if (count - n > 0)
1285 memcpy(tbuf, sce->ibuf, count - n);
1286
1287 usbd_setup_xfer(xfer, sce, NULL, count, 0, USBD_NO_TIMEOUT,
1288 ugen_bulkwb_intr);
1289 err = usbd_transfer(xfer);
1290 if (err != USBD_IN_PROGRESS) {
1291 printf("usbd_bulkwb_intr: error=%d\n", err);
1292 /*
1293 * The transfer has not been queued. Setting STOP
1294 * will make us try again at the next write.
1295 */
1296 sce->state |= UGEN_RA_WB_STOP;
1297 }
1298 }
1299 else
1300 sce->state |= UGEN_RA_WB_STOP;
1301
1302 mutex_enter(&sc->sc_lock);
1303 if (sce->state & UGEN_ASLP) {
1304 sce->state &= ~UGEN_ASLP;
1305 DPRINTFN(5, ("ugen_bulkwb_intr: waking %p\n", sce));
1306 cv_signal(&sce->cv);
1307 }
1308 mutex_exit(&sc->sc_lock);
1309 selnotify(&sce->rsel, 0, 0);
1310 }
1311
1312 Static usbd_status
1313 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
1314 {
1315 struct usbd_interface *iface;
1316 usb_endpoint_descriptor_t *ed;
1317 usbd_status err;
1318 struct ugen_endpoint *sce;
1319 uint8_t niface, nendpt, endptno, endpt;
1320 int dir;
1321
1322 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1323
1324 err = usbd_interface_count(sc->sc_udev, &niface);
1325 if (err)
1326 return err;
1327 if (ifaceidx < 0 || ifaceidx >= niface)
1328 return USBD_INVAL;
1329
1330 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1331 if (err)
1332 return err;
1333 err = usbd_endpoint_count(iface, &nendpt);
1334 if (err)
1335 return err;
1336
1337 /* change setting */
1338 err = usbd_set_interface(iface, altno);
1339 if (err)
1340 return err;
1341
1342 err = usbd_endpoint_count(iface, &nendpt);
1343 if (err)
1344 return err;
1345
1346 ugen_clear_endpoints(sc);
1347
1348 for (endptno = 0; endptno < nendpt; endptno++) {
1349 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1350 KASSERT(ed != NULL);
1351 endpt = ed->bEndpointAddress;
1352 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1353 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1354 sce->sc = sc;
1355 sce->edesc = ed;
1356 sce->iface = iface;
1357 }
1358 return 0;
1359 }
1360
1361 /* Retrieve a complete descriptor for a certain device and index. */
1362 Static usb_config_descriptor_t *
1363 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1364 {
1365 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1366 int len;
1367 usbd_status err;
1368
1369 if (index == USB_CURRENT_CONFIG_INDEX) {
1370 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1371 len = UGETW(tdesc->wTotalLength);
1372 if (lenp)
1373 *lenp = len;
1374 cdesc = kmem_alloc(len, KM_SLEEP);
1375 memcpy(cdesc, tdesc, len);
1376 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1377 } else {
1378 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1379 if (err)
1380 return 0;
1381 len = UGETW(cdescr.wTotalLength);
1382 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1383 if (lenp)
1384 *lenp = len;
1385 cdesc = kmem_alloc(len, KM_SLEEP);
1386 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1387 if (err) {
1388 kmem_free(cdesc, len);
1389 return 0;
1390 }
1391 }
1392 return cdesc;
1393 }
1394
1395 Static int
1396 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1397 {
1398 struct usbd_interface *iface;
1399 usbd_status err;
1400
1401 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1402 if (err)
1403 return -1;
1404 return usbd_get_interface_altindex(iface);
1405 }
1406
1407 Static int
1408 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1409 void *addr, int flag, struct lwp *l)
1410 {
1411 struct ugen_endpoint *sce;
1412 usbd_status err;
1413 struct usbd_interface *iface;
1414 struct usb_config_desc *cd;
1415 usb_config_descriptor_t *cdesc;
1416 struct usb_interface_desc *id;
1417 usb_interface_descriptor_t *idesc;
1418 struct usb_endpoint_desc *ed;
1419 usb_endpoint_descriptor_t *edesc;
1420 struct usb_alt_interface *ai;
1421 struct usb_string_desc *si;
1422 uint8_t conf, alt;
1423 int cdesclen;
1424 int error;
1425
1426 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1427 if (sc->sc_dying)
1428 return EIO;
1429
1430 switch (cmd) {
1431 case FIONBIO:
1432 /* All handled in the upper FS layer. */
1433 return 0;
1434 case USB_SET_SHORT_XFER:
1435 if (endpt == USB_CONTROL_ENDPOINT)
1436 return EINVAL;
1437 /* This flag only affects read */
1438 sce = &sc->sc_endpoints[endpt][IN];
1439 if (sce == NULL || sce->pipeh == NULL)
1440 return EINVAL;
1441 if (*(int *)addr)
1442 sce->state |= UGEN_SHORT_OK;
1443 else
1444 sce->state &= ~UGEN_SHORT_OK;
1445 return 0;
1446 case USB_SET_TIMEOUT:
1447 sce = &sc->sc_endpoints[endpt][IN];
1448 if (sce == NULL
1449 /* XXX this shouldn't happen, but the distinction between
1450 input and output pipes isn't clear enough.
1451 || sce->pipeh == NULL */
1452 )
1453 return EINVAL;
1454 sce->timeout = *(int *)addr;
1455 return 0;
1456 case USB_SET_BULK_RA:
1457 if (endpt == USB_CONTROL_ENDPOINT)
1458 return EINVAL;
1459 sce = &sc->sc_endpoints[endpt][IN];
1460 if (sce == NULL || sce->pipeh == NULL)
1461 return EINVAL;
1462 edesc = sce->edesc;
1463 if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK)
1464 return EINVAL;
1465
1466 if (*(int *)addr) {
1467 /* Only turn RA on if it's currently off. */
1468 if (sce->state & UGEN_BULK_RA)
1469 return 0;
1470
1471 if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0)
1472 /* shouldn't happen */
1473 return EINVAL;
1474 error = usbd_create_xfer(sce->pipeh,
1475 sce->ra_wb_reqsize, 0, 0, &sce->ra_wb_xfer);
1476 if (error)
1477 return error;
1478 sce->ra_wb_xferlen = sce->ra_wb_reqsize;
1479 sce->ibuf = kmem_alloc(sce->ra_wb_bufsize, KM_SLEEP);
1480 sce->fill = sce->cur = sce->ibuf;
1481 sce->limit = sce->ibuf + sce->ra_wb_bufsize;
1482 sce->ra_wb_used = 0;
1483 sce->state |= UGEN_BULK_RA;
1484 sce->state &= ~UGEN_RA_WB_STOP;
1485 /* Now start reading. */
1486 usbd_setup_xfer(sce->ra_wb_xfer, sce, NULL,
1487 min(sce->ra_wb_xferlen, sce->ra_wb_bufsize),
1488 0, USBD_NO_TIMEOUT, ugen_bulkra_intr);
1489 err = usbd_transfer(sce->ra_wb_xfer);
1490 if (err != USBD_IN_PROGRESS) {
1491 sce->state &= ~UGEN_BULK_RA;
1492 kmem_free(sce->ibuf, sce->ra_wb_bufsize);
1493 sce->ibuf = NULL;
1494 usbd_destroy_xfer(sce->ra_wb_xfer);
1495 return EIO;
1496 }
1497 } else {
1498 /* Only turn RA off if it's currently on. */
1499 if (!(sce->state & UGEN_BULK_RA))
1500 return 0;
1501
1502 sce->state &= ~UGEN_BULK_RA;
1503 usbd_abort_pipe(sce->pipeh);
1504 usbd_destroy_xfer(sce->ra_wb_xfer);
1505 /*
1506 * XXX Discard whatever's in the buffer, but we
1507 * should keep it around and drain the buffer
1508 * instead.
1509 */
1510 kmem_free(sce->ibuf, sce->ra_wb_bufsize);
1511 sce->ibuf = NULL;
1512 }
1513 return 0;
1514 case USB_SET_BULK_WB:
1515 if (endpt == USB_CONTROL_ENDPOINT)
1516 return EINVAL;
1517 sce = &sc->sc_endpoints[endpt][OUT];
1518 if (sce == NULL || sce->pipeh == NULL)
1519 return EINVAL;
1520 edesc = sce->edesc;
1521 if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK)
1522 return EINVAL;
1523
1524 if (*(int *)addr) {
1525 /* Only turn WB on if it's currently off. */
1526 if (sce->state & UGEN_BULK_WB)
1527 return 0;
1528
1529 if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0)
1530 /* shouldn't happen */
1531 return EINVAL;
1532 error = usbd_create_xfer(sce->pipeh, sce->ra_wb_reqsize,
1533 0, 0, &sce->ra_wb_xfer);
1534 sce->ra_wb_xferlen = sce->ra_wb_reqsize;
1535 sce->ibuf = kmem_alloc(sce->ra_wb_bufsize, KM_SLEEP);
1536 sce->fill = sce->cur = sce->ibuf;
1537 sce->limit = sce->ibuf + sce->ra_wb_bufsize;
1538 sce->ra_wb_used = 0;
1539 sce->state |= UGEN_BULK_WB | UGEN_RA_WB_STOP;
1540 } else {
1541 /* Only turn WB off if it's currently on. */
1542 if (!(sce->state & UGEN_BULK_WB))
1543 return 0;
1544
1545 sce->state &= ~UGEN_BULK_WB;
1546 /*
1547 * XXX Discard whatever's in the buffer, but we
1548 * should keep it around and keep writing to
1549 * drain the buffer instead.
1550 */
1551 usbd_abort_pipe(sce->pipeh);
1552 usbd_destroy_xfer(sce->ra_wb_xfer);
1553 kmem_free(sce->ibuf, sce->ra_wb_bufsize);
1554 sce->ibuf = NULL;
1555 }
1556 return 0;
1557 case USB_SET_BULK_RA_OPT:
1558 case USB_SET_BULK_WB_OPT:
1559 {
1560 struct usb_bulk_ra_wb_opt *opt;
1561
1562 if (endpt == USB_CONTROL_ENDPOINT)
1563 return EINVAL;
1564 opt = (struct usb_bulk_ra_wb_opt *)addr;
1565 if (cmd == USB_SET_BULK_RA_OPT)
1566 sce = &sc->sc_endpoints[endpt][IN];
1567 else
1568 sce = &sc->sc_endpoints[endpt][OUT];
1569 if (sce == NULL || sce->pipeh == NULL)
1570 return EINVAL;
1571 if (opt->ra_wb_buffer_size < 1 ||
1572 opt->ra_wb_buffer_size > UGEN_BULK_RA_WB_BUFMAX ||
1573 opt->ra_wb_request_size < 1 ||
1574 opt->ra_wb_request_size > opt->ra_wb_buffer_size)
1575 return EINVAL;
1576 /*
1577 * XXX These changes do not take effect until the
1578 * next time RA/WB mode is enabled but they ought to
1579 * take effect immediately.
1580 */
1581 sce->ra_wb_bufsize = opt->ra_wb_buffer_size;
1582 sce->ra_wb_reqsize = opt->ra_wb_request_size;
1583 return 0;
1584 }
1585 default:
1586 break;
1587 }
1588
1589 if (endpt != USB_CONTROL_ENDPOINT)
1590 return EINVAL;
1591
1592 switch (cmd) {
1593 #ifdef UGEN_DEBUG
1594 case USB_SETDEBUG:
1595 ugendebug = *(int *)addr;
1596 break;
1597 #endif
1598 case USB_GET_CONFIG:
1599 err = usbd_get_config(sc->sc_udev, &conf);
1600 if (err)
1601 return EIO;
1602 *(int *)addr = conf;
1603 break;
1604 case USB_SET_CONFIG:
1605 if (!(flag & FWRITE))
1606 return EPERM;
1607 err = ugen_set_config(sc, *(int *)addr);
1608 switch (err) {
1609 case USBD_NORMAL_COMPLETION:
1610 break;
1611 case USBD_IN_USE:
1612 return EBUSY;
1613 default:
1614 return EIO;
1615 }
1616 break;
1617 case USB_GET_ALTINTERFACE:
1618 ai = (struct usb_alt_interface *)addr;
1619 err = usbd_device2interface_handle(sc->sc_udev,
1620 ai->uai_interface_index, &iface);
1621 if (err)
1622 return EINVAL;
1623 idesc = usbd_get_interface_descriptor(iface);
1624 if (idesc == NULL)
1625 return EIO;
1626 ai->uai_alt_no = idesc->bAlternateSetting;
1627 break;
1628 case USB_SET_ALTINTERFACE:
1629 if (!(flag & FWRITE))
1630 return EPERM;
1631 ai = (struct usb_alt_interface *)addr;
1632 err = usbd_device2interface_handle(sc->sc_udev,
1633 ai->uai_interface_index, &iface);
1634 if (err)
1635 return EINVAL;
1636 err = ugen_set_interface(sc, ai->uai_interface_index,
1637 ai->uai_alt_no);
1638 if (err)
1639 return EINVAL;
1640 break;
1641 case USB_GET_NO_ALT:
1642 ai = (struct usb_alt_interface *)addr;
1643 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, &cdesclen);
1644 if (cdesc == NULL)
1645 return EINVAL;
1646 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1647 if (idesc == NULL) {
1648 kmem_free(cdesc, cdesclen);
1649 return EINVAL;
1650 }
1651 ai->uai_alt_no = usbd_get_no_alts(cdesc,
1652 idesc->bInterfaceNumber);
1653 kmem_free(cdesc, cdesclen);
1654 break;
1655 case USB_GET_DEVICE_DESC:
1656 *(usb_device_descriptor_t *)addr =
1657 *usbd_get_device_descriptor(sc->sc_udev);
1658 break;
1659 case USB_GET_CONFIG_DESC:
1660 cd = (struct usb_config_desc *)addr;
1661 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, &cdesclen);
1662 if (cdesc == NULL)
1663 return EINVAL;
1664 cd->ucd_desc = *cdesc;
1665 kmem_free(cdesc, cdesclen);
1666 break;
1667 case USB_GET_INTERFACE_DESC:
1668 id = (struct usb_interface_desc *)addr;
1669 cdesc = ugen_get_cdesc(sc, id->uid_config_index, &cdesclen);
1670 if (cdesc == NULL)
1671 return EINVAL;
1672 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1673 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1674 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1675 else
1676 alt = id->uid_alt_index;
1677 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1678 if (idesc == NULL) {
1679 kmem_free(cdesc, cdesclen);
1680 return EINVAL;
1681 }
1682 id->uid_desc = *idesc;
1683 kmem_free(cdesc, cdesclen);
1684 break;
1685 case USB_GET_ENDPOINT_DESC:
1686 ed = (struct usb_endpoint_desc *)addr;
1687 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, &cdesclen);
1688 if (cdesc == NULL)
1689 return EINVAL;
1690 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1691 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1692 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1693 else
1694 alt = ed->ued_alt_index;
1695 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1696 alt, ed->ued_endpoint_index);
1697 if (edesc == NULL) {
1698 kmem_free(cdesc, cdesclen);
1699 return EINVAL;
1700 }
1701 ed->ued_desc = *edesc;
1702 kmem_free(cdesc, cdesclen);
1703 break;
1704 case USB_GET_FULL_DESC:
1705 {
1706 int len;
1707 struct iovec iov;
1708 struct uio uio;
1709 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1710
1711 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &cdesclen);
1712 if (cdesc == NULL)
1713 return EINVAL;
1714 len = cdesclen;
1715 if (len > fd->ufd_size)
1716 len = fd->ufd_size;
1717 iov.iov_base = (void *)fd->ufd_data;
1718 iov.iov_len = len;
1719 uio.uio_iov = &iov;
1720 uio.uio_iovcnt = 1;
1721 uio.uio_resid = len;
1722 uio.uio_offset = 0;
1723 uio.uio_rw = UIO_READ;
1724 uio.uio_vmspace = l->l_proc->p_vmspace;
1725 error = uiomove((void *)cdesc, len, &uio);
1726 kmem_free(cdesc, cdesclen);
1727 return error;
1728 }
1729 case USB_GET_STRING_DESC: {
1730 int len;
1731 si = (struct usb_string_desc *)addr;
1732 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1733 si->usd_language_id, &si->usd_desc, &len);
1734 if (err)
1735 return EINVAL;
1736 break;
1737 }
1738 case USB_DO_REQUEST:
1739 {
1740 struct usb_ctl_request *ur = (void *)addr;
1741 int len = UGETW(ur->ucr_request.wLength);
1742 struct iovec iov;
1743 struct uio uio;
1744 void *ptr = 0;
1745 usbd_status xerr;
1746
1747 error = 0;
1748
1749 if (!(flag & FWRITE))
1750 return EPERM;
1751 /* Avoid requests that would damage the bus integrity. */
1752 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1753 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1754 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1755 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1756 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1757 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1758 return EINVAL;
1759
1760 if (len < 0 || len > 32767)
1761 return EINVAL;
1762 if (len != 0) {
1763 iov.iov_base = (void *)ur->ucr_data;
1764 iov.iov_len = len;
1765 uio.uio_iov = &iov;
1766 uio.uio_iovcnt = 1;
1767 uio.uio_resid = len;
1768 uio.uio_offset = 0;
1769 uio.uio_rw =
1770 ur->ucr_request.bmRequestType & UT_READ ?
1771 UIO_READ : UIO_WRITE;
1772 uio.uio_vmspace = l->l_proc->p_vmspace;
1773 ptr = kmem_alloc(len, KM_SLEEP);
1774 if (uio.uio_rw == UIO_WRITE) {
1775 error = uiomove(ptr, len, &uio);
1776 if (error)
1777 goto ret;
1778 }
1779 }
1780 sce = &sc->sc_endpoints[endpt][IN];
1781 xerr = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1782 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1783 if (xerr) {
1784 error = EIO;
1785 goto ret;
1786 }
1787 if (len != 0) {
1788 if (uio.uio_rw == UIO_READ) {
1789 size_t alen = min(len, ur->ucr_actlen);
1790 error = uiomove(ptr, alen, &uio);
1791 if (error)
1792 goto ret;
1793 }
1794 }
1795 ret:
1796 if (ptr)
1797 kmem_free(ptr, len);
1798 return error;
1799 }
1800 case USB_GET_DEVICEINFO:
1801 usbd_fill_deviceinfo(sc->sc_udev,
1802 (struct usb_device_info *)addr, 0);
1803 break;
1804 #ifdef COMPAT_30
1805 case USB_GET_DEVICEINFO_OLD:
1806 usbd_fill_deviceinfo_old(sc->sc_udev,
1807 (struct usb_device_info_old *)addr, 0);
1808
1809 break;
1810 #endif
1811 default:
1812 return EINVAL;
1813 }
1814 return 0;
1815 }
1816
1817 int
1818 ugenioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1819 {
1820 int endpt = UGENENDPOINT(dev);
1821 struct ugen_softc *sc;
1822 int error;
1823
1824 sc = device_lookup_private(& ugen_cd, UGENUNIT(dev));
1825 if (sc == NULL || sc->sc_dying)
1826 return ENXIO;
1827
1828 sc->sc_refcnt++;
1829 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, l);
1830 if (--sc->sc_refcnt < 0)
1831 cv_broadcast(&sc->sc_detach_cv);
1832 return error;
1833 }
1834
1835 int
1836 ugenpoll(dev_t dev, int events, struct lwp *l)
1837 {
1838 struct ugen_softc *sc;
1839 struct ugen_endpoint *sce_in, *sce_out;
1840 int revents = 0;
1841
1842 sc = device_lookup_private(&ugen_cd, UGENUNIT(dev));
1843 if (sc == NULL)
1844 return ENXIO;
1845
1846 if (sc->sc_dying)
1847 return POLLHUP;
1848
1849 if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT)
1850 return ENODEV;
1851
1852 sce_in = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1853 sce_out = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1854 if (sce_in == NULL && sce_out == NULL)
1855 return POLLERR;
1856 #ifdef DIAGNOSTIC
1857 if (!sce_in->edesc && !sce_out->edesc) {
1858 printf("ugenpoll: no edesc\n");
1859 return POLLERR;
1860 }
1861 /* It's possible to have only one pipe open. */
1862 if (!sce_in->pipeh && !sce_out->pipeh) {
1863 printf("ugenpoll: no pipe\n");
1864 return POLLERR;
1865 }
1866 #endif
1867
1868 mutex_enter(&sc->sc_lock);
1869 if (sce_in && sce_in->pipeh && (events & (POLLIN | POLLRDNORM)))
1870 switch (sce_in->edesc->bmAttributes & UE_XFERTYPE) {
1871 case UE_INTERRUPT:
1872 if (sce_in->q.c_cc > 0)
1873 revents |= events & (POLLIN | POLLRDNORM);
1874 else
1875 selrecord(l, &sce_in->rsel);
1876 break;
1877 case UE_ISOCHRONOUS:
1878 if (sce_in->cur != sce_in->fill)
1879 revents |= events & (POLLIN | POLLRDNORM);
1880 else
1881 selrecord(l, &sce_in->rsel);
1882 break;
1883 case UE_BULK:
1884 if (sce_in->state & UGEN_BULK_RA) {
1885 if (sce_in->ra_wb_used > 0)
1886 revents |= events &
1887 (POLLIN | POLLRDNORM);
1888 else
1889 selrecord(l, &sce_in->rsel);
1890 break;
1891 }
1892 /*
1893 * We have no easy way of determining if a read will
1894 * yield any data or a write will happen.
1895 * Pretend they will.
1896 */
1897 revents |= events & (POLLIN | POLLRDNORM);
1898 break;
1899 default:
1900 break;
1901 }
1902 if (sce_out && sce_out->pipeh && (events & (POLLOUT | POLLWRNORM)))
1903 switch (sce_out->edesc->bmAttributes & UE_XFERTYPE) {
1904 case UE_INTERRUPT:
1905 case UE_ISOCHRONOUS:
1906 /* XXX unimplemented */
1907 break;
1908 case UE_BULK:
1909 if (sce_out->state & UGEN_BULK_WB) {
1910 if (sce_out->ra_wb_used <
1911 sce_out->limit - sce_out->ibuf)
1912 revents |= events &
1913 (POLLOUT | POLLWRNORM);
1914 else
1915 selrecord(l, &sce_out->rsel);
1916 break;
1917 }
1918 /*
1919 * We have no easy way of determining if a read will
1920 * yield any data or a write will happen.
1921 * Pretend they will.
1922 */
1923 revents |= events & (POLLOUT | POLLWRNORM);
1924 break;
1925 default:
1926 break;
1927 }
1928
1929 mutex_exit(&sc->sc_lock);
1930
1931 return revents;
1932 }
1933
1934 static void
1935 filt_ugenrdetach(struct knote *kn)
1936 {
1937 struct ugen_endpoint *sce = kn->kn_hook;
1938 struct ugen_softc *sc = sce->sc;
1939
1940 mutex_enter(&sc->sc_lock);
1941 SLIST_REMOVE(&sce->rsel.sel_klist, kn, knote, kn_selnext);
1942 mutex_exit(&sc->sc_lock);
1943 }
1944
1945 static int
1946 filt_ugenread_intr(struct knote *kn, long hint)
1947 {
1948 struct ugen_endpoint *sce = kn->kn_hook;
1949 struct ugen_softc *sc = sce->sc;
1950
1951 if (sc->sc_dying)
1952 return 0;
1953
1954 kn->kn_data = sce->q.c_cc;
1955 return kn->kn_data > 0;
1956 }
1957
1958 static int
1959 filt_ugenread_isoc(struct knote *kn, long hint)
1960 {
1961 struct ugen_endpoint *sce = kn->kn_hook;
1962 struct ugen_softc *sc = sce->sc;
1963
1964 if (sc->sc_dying)
1965 return 0;
1966
1967 if (sce->cur == sce->fill)
1968 return 0;
1969
1970 if (sce->cur < sce->fill)
1971 kn->kn_data = sce->fill - sce->cur;
1972 else
1973 kn->kn_data = (sce->limit - sce->cur) +
1974 (sce->fill - sce->ibuf);
1975
1976 return 1;
1977 }
1978
1979 static int
1980 filt_ugenread_bulk(struct knote *kn, long hint)
1981 {
1982 struct ugen_endpoint *sce = kn->kn_hook;
1983 struct ugen_softc *sc = sce->sc;
1984
1985 if (sc->sc_dying)
1986 return 0;
1987
1988 if (!(sce->state & UGEN_BULK_RA))
1989 /*
1990 * We have no easy way of determining if a read will
1991 * yield any data or a write will happen.
1992 * So, emulate "seltrue".
1993 */
1994 return filt_seltrue(kn, hint);
1995
1996 if (sce->ra_wb_used == 0)
1997 return 0;
1998
1999 kn->kn_data = sce->ra_wb_used;
2000
2001 return 1;
2002 }
2003
2004 static int
2005 filt_ugenwrite_bulk(struct knote *kn, long hint)
2006 {
2007 struct ugen_endpoint *sce = kn->kn_hook;
2008 struct ugen_softc *sc = sce->sc;
2009
2010 if (sc->sc_dying)
2011 return 0;
2012
2013 if (!(sce->state & UGEN_BULK_WB))
2014 /*
2015 * We have no easy way of determining if a read will
2016 * yield any data or a write will happen.
2017 * So, emulate "seltrue".
2018 */
2019 return filt_seltrue(kn, hint);
2020
2021 if (sce->ra_wb_used == sce->limit - sce->ibuf)
2022 return 0;
2023
2024 kn->kn_data = (sce->limit - sce->ibuf) - sce->ra_wb_used;
2025
2026 return 1;
2027 }
2028
2029 static const struct filterops ugenread_intr_filtops =
2030 { 1, NULL, filt_ugenrdetach, filt_ugenread_intr };
2031
2032 static const struct filterops ugenread_isoc_filtops =
2033 { 1, NULL, filt_ugenrdetach, filt_ugenread_isoc };
2034
2035 static const struct filterops ugenread_bulk_filtops =
2036 { 1, NULL, filt_ugenrdetach, filt_ugenread_bulk };
2037
2038 static const struct filterops ugenwrite_bulk_filtops =
2039 { 1, NULL, filt_ugenrdetach, filt_ugenwrite_bulk };
2040
2041 int
2042 ugenkqfilter(dev_t dev, struct knote *kn)
2043 {
2044 struct ugen_softc *sc;
2045 struct ugen_endpoint *sce;
2046 struct klist *klist;
2047
2048 sc = device_lookup_private(&ugen_cd, UGENUNIT(dev));
2049 if (sc == NULL || sc->sc_dying)
2050 return ENXIO;
2051
2052 if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT)
2053 return ENODEV;
2054
2055 switch (kn->kn_filter) {
2056 case EVFILT_READ:
2057 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
2058 if (sce == NULL)
2059 return EINVAL;
2060
2061 klist = &sce->rsel.sel_klist;
2062 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
2063 case UE_INTERRUPT:
2064 kn->kn_fop = &ugenread_intr_filtops;
2065 break;
2066 case UE_ISOCHRONOUS:
2067 kn->kn_fop = &ugenread_isoc_filtops;
2068 break;
2069 case UE_BULK:
2070 kn->kn_fop = &ugenread_bulk_filtops;
2071 break;
2072 default:
2073 return EINVAL;
2074 }
2075 break;
2076
2077 case EVFILT_WRITE:
2078 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
2079 if (sce == NULL)
2080 return EINVAL;
2081
2082 klist = &sce->rsel.sel_klist;
2083 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
2084 case UE_INTERRUPT:
2085 case UE_ISOCHRONOUS:
2086 /* XXX poll doesn't support this */
2087 return EINVAL;
2088
2089 case UE_BULK:
2090 kn->kn_fop = &ugenwrite_bulk_filtops;
2091 break;
2092 default:
2093 return EINVAL;
2094 }
2095 break;
2096
2097 default:
2098 return EINVAL;
2099 }
2100
2101 kn->kn_hook = sce;
2102
2103 mutex_enter(&sc->sc_lock);
2104 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
2105 mutex_exit(&sc->sc_lock);
2106
2107 return 0;
2108 }
2109