ugen.c revision 1.60 1 /* $NetBSD: ugen.c,v 1.60 2002/09/06 13:18:43 gehenna Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/ugen.c,v 1.26 1999/11/17 22:33:41 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998 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 (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.60 2002/09/06 13:18:43 gehenna Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #if defined(__NetBSD__) || defined(__OpenBSD__)
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/module.h>
54 #include <sys/bus.h>
55 #include <sys/ioccom.h>
56 #include <sys/conf.h>
57 #include <sys/fcntl.h>
58 #include <sys/filio.h>
59 #endif
60 #include <sys/conf.h>
61 #include <sys/tty.h>
62 #include <sys/file.h>
63 #include <sys/select.h>
64 #include <sys/proc.h>
65 #include <sys/vnode.h>
66 #include <sys/poll.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71
72 #ifdef UGEN_DEBUG
73 #define DPRINTF(x) if (ugendebug) logprintf x
74 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
75 int ugendebug = 0;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n,x)
79 #endif
80
81 #define UGEN_CHUNK 128 /* chunk size for read */
82 #define UGEN_IBSIZE 1020 /* buffer size */
83 #define UGEN_BBSIZE 1024
84
85 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
86 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
87 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
88
89 struct ugen_endpoint {
90 struct ugen_softc *sc;
91 usb_endpoint_descriptor_t *edesc;
92 usbd_interface_handle iface;
93 int state;
94 #define UGEN_ASLP 0x02 /* waiting for data */
95 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
96 usbd_pipe_handle pipeh;
97 struct clist q;
98 struct selinfo rsel;
99 u_char *ibuf; /* start of buffer (circular for isoc) */
100 u_char *fill; /* location for input (isoc) */
101 u_char *limit; /* end of circular buffer (isoc) */
102 u_char *cur; /* current read location (isoc) */
103 u_int32_t timeout;
104 struct isoreq {
105 struct ugen_endpoint *sce;
106 usbd_xfer_handle xfer;
107 void *dmabuf;
108 u_int16_t sizes[UGEN_NISORFRMS];
109 } isoreqs[UGEN_NISOREQS];
110 };
111
112 struct ugen_softc {
113 USBBASEDEVICE sc_dev; /* base device */
114 usbd_device_handle sc_udev;
115
116 char sc_is_open[USB_MAX_ENDPOINTS];
117 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
118 #define OUT 0
119 #define IN 1
120
121 int sc_refcnt;
122 u_char sc_dying;
123 };
124
125 #if defined(__NetBSD__)
126 dev_type_open(ugenopen);
127 dev_type_close(ugenclose);
128 dev_type_read(ugenread);
129 dev_type_write(ugenwrite);
130 dev_type_ioctl(ugenioctl);
131 dev_type_poll(ugenpoll);
132
133 const struct cdevsw ugen_cdevsw = {
134 ugenopen, ugenclose, ugenread, ugenwrite, ugenioctl,
135 nostop, notty, ugenpoll, nommap,
136 };
137 #elif defined(__OpenBSD__)
138 cdev_decl(ugen);
139 #elif defined(__FreeBSD__)
140 d_open_t ugenopen;
141 d_close_t ugenclose;
142 d_read_t ugenread;
143 d_write_t ugenwrite;
144 d_ioctl_t ugenioctl;
145 d_poll_t ugenpoll;
146
147 #define UGEN_CDEV_MAJOR 114
148
149 Static struct cdevsw ugen_cdevsw = {
150 /* open */ ugenopen,
151 /* close */ ugenclose,
152 /* read */ ugenread,
153 /* write */ ugenwrite,
154 /* ioctl */ ugenioctl,
155 /* poll */ ugenpoll,
156 /* mmap */ nommap,
157 /* strategy */ nostrategy,
158 /* name */ "ugen",
159 /* maj */ UGEN_CDEV_MAJOR,
160 /* dump */ nodump,
161 /* psize */ nopsize,
162 /* flags */ 0,
163 /* bmaj */ -1
164 };
165 #endif
166
167 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
168 usbd_status status);
169 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
170 usbd_status status);
171 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
172 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
173 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
174 caddr_t, int, usb_proc_ptr);
175 Static int ugen_set_config(struct ugen_softc *sc, int configno);
176 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
177 int index, int *lenp);
178 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
179 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
180
181 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
182 #define UGENENDPOINT(n) (minor(n) & 0xf)
183 #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
184
185 USB_DECLARE_DRIVER(ugen);
186
187 USB_MATCH(ugen)
188 {
189 USB_MATCH_START(ugen, uaa);
190
191 #if 0
192 if (uaa->matchlvl)
193 return (uaa->matchlvl);
194 #endif
195 if (uaa->usegeneric)
196 return (UMATCH_GENERIC);
197 else
198 return (UMATCH_NONE);
199 }
200
201 USB_ATTACH(ugen)
202 {
203 USB_ATTACH_START(ugen, sc, uaa);
204 usbd_device_handle udev;
205 char devinfo[1024];
206 usbd_status err;
207 int conf;
208
209 usbd_devinfo(uaa->device, 0, devinfo);
210 USB_ATTACH_SETUP;
211 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
212
213 sc->sc_udev = udev = uaa->device;
214
215 /* First set configuration index 0, the default one for ugen. */
216 err = usbd_set_config_index(udev, 0, 0);
217 if (err) {
218 printf("%s: setting configuration index 0 failed\n",
219 USBDEVNAME(sc->sc_dev));
220 sc->sc_dying = 1;
221 USB_ATTACH_ERROR_RETURN;
222 }
223 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
224
225 /* Set up all the local state for this configuration. */
226 err = ugen_set_config(sc, conf);
227 if (err) {
228 printf("%s: setting configuration %d failed\n",
229 USBDEVNAME(sc->sc_dev), conf);
230 sc->sc_dying = 1;
231 USB_ATTACH_ERROR_RETURN;
232 }
233
234 #ifdef __FreeBSD__
235 {
236 static int global_init_done = 0;
237 if (!global_init_done) {
238 cdevsw_add(&ugen_cdevsw);
239 global_init_done = 1;
240 }
241 }
242 #endif
243
244 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
245 USBDEV(sc->sc_dev));
246
247 USB_ATTACH_SUCCESS_RETURN;
248 }
249
250 Static int
251 ugen_set_config(struct ugen_softc *sc, int configno)
252 {
253 usbd_device_handle dev = sc->sc_udev;
254 usbd_interface_handle iface;
255 usb_endpoint_descriptor_t *ed;
256 struct ugen_endpoint *sce;
257 u_int8_t niface, nendpt;
258 int ifaceno, endptno, endpt;
259 usbd_status err;
260 int dir;
261
262 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
263 USBDEVNAME(sc->sc_dev), configno, sc));
264
265 /*
266 * We start at 1, not 0, because we don't care whether the
267 * control endpoint is open or not. It is always present.
268 */
269 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
270 if (sc->sc_is_open[endptno]) {
271 DPRINTFN(1,
272 ("ugen_set_config: %s - endpoint %d is open\n",
273 USBDEVNAME(sc->sc_dev), endptno));
274 return (USBD_IN_USE);
275 }
276
277 /* Avoid setting the current value. */
278 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
279 err = usbd_set_config_no(dev, configno, 1);
280 if (err)
281 return (err);
282 }
283
284 err = usbd_interface_count(dev, &niface);
285 if (err)
286 return (err);
287 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
288 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
289 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
290 err = usbd_device2interface_handle(dev, ifaceno, &iface);
291 if (err)
292 return (err);
293 err = usbd_endpoint_count(iface, &nendpt);
294 if (err)
295 return (err);
296 for (endptno = 0; endptno < nendpt; endptno++) {
297 ed = usbd_interface2endpoint_descriptor(iface,endptno);
298 endpt = ed->bEndpointAddress;
299 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
300 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
301 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
302 "(%d,%d), sce=%p\n",
303 endptno, endpt, UE_GET_ADDR(endpt),
304 UE_GET_DIR(endpt), sce));
305 sce->sc = sc;
306 sce->edesc = ed;
307 sce->iface = iface;
308 }
309 }
310 return (USBD_NORMAL_COMPLETION);
311 }
312
313 int
314 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
315 {
316 struct ugen_softc *sc;
317 int unit = UGENUNIT(dev);
318 int endpt = UGENENDPOINT(dev);
319 usb_endpoint_descriptor_t *edesc;
320 struct ugen_endpoint *sce;
321 int dir, isize;
322 usbd_status err;
323 usbd_xfer_handle xfer;
324 void *buf;
325 int i, j;
326
327 USB_GET_SC_OPEN(ugen, unit, sc);
328
329 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
330 flag, mode, unit, endpt));
331
332 if (sc == NULL || sc->sc_dying)
333 return (ENXIO);
334
335 if (sc->sc_is_open[endpt])
336 return (EBUSY);
337
338 if (endpt == USB_CONTROL_ENDPOINT) {
339 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
340 return (0);
341 }
342
343 /* Make sure there are pipes for all directions. */
344 for (dir = OUT; dir <= IN; dir++) {
345 if (flag & (dir == OUT ? FWRITE : FREAD)) {
346 sce = &sc->sc_endpoints[endpt][dir];
347 if (sce == 0 || sce->edesc == 0)
348 return (ENXIO);
349 }
350 }
351
352 /* Actually open the pipes. */
353 /* XXX Should back out properly if it fails. */
354 for (dir = OUT; dir <= IN; dir++) {
355 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
356 continue;
357 sce = &sc->sc_endpoints[endpt][dir];
358 sce->state = 0;
359 sce->timeout = USBD_NO_TIMEOUT;
360 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
361 sc, endpt, dir, sce));
362 edesc = sce->edesc;
363 switch (edesc->bmAttributes & UE_XFERTYPE) {
364 case UE_INTERRUPT:
365 isize = UGETW(edesc->wMaxPacketSize);
366 if (isize == 0) /* shouldn't happen */
367 return (EINVAL);
368 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
369 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
370 endpt, isize));
371 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
372 return (ENOMEM);
373 err = usbd_open_pipe_intr(sce->iface,
374 edesc->bEndpointAddress,
375 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
376 sce->ibuf, isize, ugenintr,
377 USBD_DEFAULT_INTERVAL);
378 if (err) {
379 free(sce->ibuf, M_USBDEV);
380 clfree(&sce->q);
381 return (EIO);
382 }
383 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
384 break;
385 case UE_BULK:
386 err = usbd_open_pipe(sce->iface,
387 edesc->bEndpointAddress, 0, &sce->pipeh);
388 if (err)
389 return (EIO);
390 break;
391 case UE_ISOCHRONOUS:
392 if (dir == OUT)
393 return (EINVAL);
394 isize = UGETW(edesc->wMaxPacketSize);
395 if (isize == 0) /* shouldn't happen */
396 return (EINVAL);
397 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
398 M_USBDEV, M_WAITOK);
399 sce->cur = sce->fill = sce->ibuf;
400 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
401 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
402 endpt, isize));
403 err = usbd_open_pipe(sce->iface,
404 edesc->bEndpointAddress, 0, &sce->pipeh);
405 if (err) {
406 free(sce->ibuf, M_USBDEV);
407 return (EIO);
408 }
409 for(i = 0; i < UGEN_NISOREQS; ++i) {
410 sce->isoreqs[i].sce = sce;
411 xfer = usbd_alloc_xfer(sc->sc_udev);
412 if (xfer == 0)
413 goto bad;
414 sce->isoreqs[i].xfer = xfer;
415 buf = usbd_alloc_buffer
416 (xfer, isize * UGEN_NISORFRMS);
417 if (buf == 0) {
418 i++;
419 goto bad;
420 }
421 sce->isoreqs[i].dmabuf = buf;
422 for(j = 0; j < UGEN_NISORFRMS; ++j)
423 sce->isoreqs[i].sizes[j] = isize;
424 usbd_setup_isoc_xfer
425 (xfer, sce->pipeh, &sce->isoreqs[i],
426 sce->isoreqs[i].sizes,
427 UGEN_NISORFRMS, USBD_NO_COPY,
428 ugen_isoc_rintr);
429 (void)usbd_transfer(xfer);
430 }
431 DPRINTFN(5, ("ugenopen: isoc open done\n"));
432 break;
433 bad:
434 while (--i >= 0) /* implicit buffer free */
435 usbd_free_xfer(sce->isoreqs[i].xfer);
436 return (ENOMEM);
437 case UE_CONTROL:
438 sce->timeout = USBD_DEFAULT_TIMEOUT;
439 return (EINVAL);
440 }
441 }
442 sc->sc_is_open[endpt] = 1;
443 return (0);
444 }
445
446 int
447 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
448 {
449 int endpt = UGENENDPOINT(dev);
450 struct ugen_softc *sc;
451 struct ugen_endpoint *sce;
452 int dir;
453 int i;
454
455 USB_GET_SC(ugen, UGENUNIT(dev), sc);
456
457 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
458 flag, mode, UGENUNIT(dev), endpt));
459
460 #ifdef DIAGNOSTIC
461 if (!sc->sc_is_open[endpt]) {
462 printf("ugenclose: not open\n");
463 return (EINVAL);
464 }
465 #endif
466
467 if (endpt == USB_CONTROL_ENDPOINT) {
468 DPRINTFN(5, ("ugenclose: close control\n"));
469 sc->sc_is_open[endpt] = 0;
470 return (0);
471 }
472
473 for (dir = OUT; dir <= IN; dir++) {
474 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
475 continue;
476 sce = &sc->sc_endpoints[endpt][dir];
477 if (sce == NULL || sce->pipeh == NULL)
478 continue;
479 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
480 endpt, dir, sce));
481
482 usbd_abort_pipe(sce->pipeh);
483 usbd_close_pipe(sce->pipeh);
484 sce->pipeh = NULL;
485
486 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
487 case UE_INTERRUPT:
488 ndflush(&sce->q, sce->q.c_cc);
489 clfree(&sce->q);
490 break;
491 case UE_ISOCHRONOUS:
492 for (i = 0; i < UGEN_NISOREQS; ++i)
493 usbd_free_xfer(sce->isoreqs[i].xfer);
494
495 default:
496 break;
497 }
498
499 if (sce->ibuf != NULL) {
500 free(sce->ibuf, M_USBDEV);
501 sce->ibuf = NULL;
502 clfree(&sce->q);
503 }
504 }
505 sc->sc_is_open[endpt] = 0;
506
507 return (0);
508 }
509
510 Static int
511 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
512 {
513 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
514 u_int32_t n, tn;
515 char buf[UGEN_BBSIZE];
516 usbd_xfer_handle xfer;
517 usbd_status err;
518 int s;
519 int error = 0;
520 u_char buffer[UGEN_CHUNK];
521
522 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
523
524 if (sc->sc_dying)
525 return (EIO);
526
527 if (endpt == USB_CONTROL_ENDPOINT)
528 return (ENODEV);
529
530 #ifdef DIAGNOSTIC
531 if (sce->edesc == NULL) {
532 printf("ugenread: no edesc\n");
533 return (EIO);
534 }
535 if (sce->pipeh == NULL) {
536 printf("ugenread: no pipe\n");
537 return (EIO);
538 }
539 #endif
540
541 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
542 case UE_INTERRUPT:
543 /* Block until activity occurred. */
544 s = splusb();
545 while (sce->q.c_cc == 0) {
546 if (flag & IO_NDELAY) {
547 splx(s);
548 return (EWOULDBLOCK);
549 }
550 sce->state |= UGEN_ASLP;
551 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
552 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
553 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
554 if (sc->sc_dying)
555 error = EIO;
556 if (error) {
557 sce->state &= ~UGEN_ASLP;
558 break;
559 }
560 }
561 splx(s);
562
563 /* Transfer as many chunks as possible. */
564 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
565 n = min(sce->q.c_cc, uio->uio_resid);
566 if (n > sizeof(buffer))
567 n = sizeof(buffer);
568
569 /* Remove a small chunk from the input queue. */
570 q_to_b(&sce->q, buffer, n);
571 DPRINTFN(5, ("ugenread: got %d chars\n", n));
572
573 /* Copy the data to the user process. */
574 error = uiomove(buffer, n, uio);
575 if (error)
576 break;
577 }
578 break;
579 case UE_BULK:
580 xfer = usbd_alloc_xfer(sc->sc_udev);
581 if (xfer == 0)
582 return (ENOMEM);
583 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
584 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
585 tn = n;
586 err = usbd_bulk_transfer(
587 xfer, sce->pipeh,
588 sce->state & UGEN_SHORT_OK ?
589 USBD_SHORT_XFER_OK : 0,
590 sce->timeout, buf, &tn, "ugenrb");
591 if (err) {
592 if (err == USBD_INTERRUPTED)
593 error = EINTR;
594 else if (err == USBD_TIMEOUT)
595 error = ETIMEDOUT;
596 else
597 error = EIO;
598 break;
599 }
600 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
601 error = uiomove(buf, tn, uio);
602 if (error || tn < n)
603 break;
604 }
605 usbd_free_xfer(xfer);
606 break;
607 case UE_ISOCHRONOUS:
608 s = splusb();
609 while (sce->cur == sce->fill) {
610 if (flag & IO_NDELAY) {
611 splx(s);
612 return (EWOULDBLOCK);
613 }
614 sce->state |= UGEN_ASLP;
615 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
616 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
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
626 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
627 if(sce->fill > sce->cur)
628 n = min(sce->fill - sce->cur, uio->uio_resid);
629 else
630 n = min(sce->limit - sce->cur, uio->uio_resid);
631
632 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
633
634 /* Copy the data to the user process. */
635 error = uiomove(sce->cur, n, uio);
636 if (error)
637 break;
638 sce->cur += n;
639 if(sce->cur >= sce->limit)
640 sce->cur = sce->ibuf;
641 }
642 splx(s);
643 break;
644
645
646 default:
647 return (ENXIO);
648 }
649 return (error);
650 }
651
652 int
653 ugenread(dev_t dev, struct uio *uio, int flag)
654 {
655 int endpt = UGENENDPOINT(dev);
656 struct ugen_softc *sc;
657 int error;
658
659 USB_GET_SC(ugen, UGENUNIT(dev), sc);
660
661 sc->sc_refcnt++;
662 error = ugen_do_read(sc, endpt, uio, flag);
663 if (--sc->sc_refcnt < 0)
664 usb_detach_wakeup(USBDEV(sc->sc_dev));
665 return (error);
666 }
667
668 Static int
669 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
670 {
671 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
672 u_int32_t n;
673 int error = 0;
674 char buf[UGEN_BBSIZE];
675 usbd_xfer_handle xfer;
676 usbd_status err;
677
678 DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
679
680 if (sc->sc_dying)
681 return (EIO);
682
683 if (endpt == USB_CONTROL_ENDPOINT)
684 return (ENODEV);
685
686 #ifdef DIAGNOSTIC
687 if (sce->edesc == NULL) {
688 printf("ugenwrite: no edesc\n");
689 return (EIO);
690 }
691 if (sce->pipeh == NULL) {
692 printf("ugenwrite: no pipe\n");
693 return (EIO);
694 }
695 #endif
696
697 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
698 case UE_BULK:
699 xfer = usbd_alloc_xfer(sc->sc_udev);
700 if (xfer == 0)
701 return (EIO);
702 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
703 error = uiomove(buf, n, uio);
704 if (error)
705 break;
706 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
707 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
708 sce->timeout, buf, &n,"ugenwb");
709 if (err) {
710 if (err == USBD_INTERRUPTED)
711 error = EINTR;
712 else if (err == USBD_TIMEOUT)
713 error = ETIMEDOUT;
714 else
715 error = EIO;
716 break;
717 }
718 }
719 usbd_free_xfer(xfer);
720 break;
721 default:
722 return (ENXIO);
723 }
724 return (error);
725 }
726
727 int
728 ugenwrite(dev_t dev, struct uio *uio, int flag)
729 {
730 int endpt = UGENENDPOINT(dev);
731 struct ugen_softc *sc;
732 int error;
733
734 USB_GET_SC(ugen, UGENUNIT(dev), sc);
735
736 sc->sc_refcnt++;
737 error = ugen_do_write(sc, endpt, uio, flag);
738 if (--sc->sc_refcnt < 0)
739 usb_detach_wakeup(USBDEV(sc->sc_dev));
740 return (error);
741 }
742
743 #if defined(__NetBSD__) || defined(__OpenBSD__)
744 int
745 ugen_activate(device_ptr_t self, enum devact act)
746 {
747 struct ugen_softc *sc = (struct ugen_softc *)self;
748
749 switch (act) {
750 case DVACT_ACTIVATE:
751 return (EOPNOTSUPP);
752 break;
753
754 case DVACT_DEACTIVATE:
755 sc->sc_dying = 1;
756 break;
757 }
758 return (0);
759 }
760 #endif
761
762 USB_DETACH(ugen)
763 {
764 USB_DETACH_START(ugen, sc);
765 struct ugen_endpoint *sce;
766 int i, dir;
767 int s;
768 #if defined(__NetBSD__) || defined(__OpenBSD__)
769 int maj, mn;
770
771 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
772 #elif defined(__FreeBSD__)
773 DPRINTF(("ugen_detach: sc=%p\n", sc));
774 #endif
775
776 sc->sc_dying = 1;
777 /* Abort all pipes. Causes processes waiting for transfer to wake. */
778 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
779 for (dir = OUT; dir <= IN; dir++) {
780 sce = &sc->sc_endpoints[i][dir];
781 if (sce && sce->pipeh)
782 usbd_abort_pipe(sce->pipeh);
783 }
784 }
785
786 s = splusb();
787 if (--sc->sc_refcnt >= 0) {
788 /* Wake everyone */
789 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
790 wakeup(&sc->sc_endpoints[i][IN]);
791 /* Wait for processes to go away. */
792 usb_detach_wait(USBDEV(sc->sc_dev));
793 }
794 splx(s);
795
796 #if defined(__NetBSD__) || defined(__OpenBSD__)
797 /* locate the major number */
798 #if defined(__NetBSD__)
799 maj = cdevsw_lookup_major(&ugen_cdevsw);
800 #elif defined(__OpenBSD__)
801 for (maj = 0; maj < nchrdev; maj++)
802 if (cdevsw[maj].d_open == ugenopen)
803 break;
804 #endif
805
806 /* Nuke the vnodes for any open instances (calls close). */
807 mn = self->dv_unit * USB_MAX_ENDPOINTS;
808 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
809 #elif defined(__FreeBSD__)
810 /* XXX not implemented yet */
811 #endif
812
813 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
814 USBDEV(sc->sc_dev));
815
816 return (0);
817 }
818
819 Static void
820 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
821 {
822 struct ugen_endpoint *sce = addr;
823 /*struct ugen_softc *sc = sce->sc;*/
824 u_int32_t count;
825 u_char *ibuf;
826
827 if (status == USBD_CANCELLED)
828 return;
829
830 if (status != USBD_NORMAL_COMPLETION) {
831 DPRINTF(("ugenintr: status=%d\n", status));
832 if (status == USBD_STALLED)
833 usbd_clear_endpoint_stall_async(sce->pipeh);
834 return;
835 }
836
837 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
838 ibuf = sce->ibuf;
839
840 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
841 xfer, status, count));
842 DPRINTFN(5, (" data = %02x %02x %02x\n",
843 ibuf[0], ibuf[1], ibuf[2]));
844
845 (void)b_to_q(ibuf, count, &sce->q);
846
847 if (sce->state & UGEN_ASLP) {
848 sce->state &= ~UGEN_ASLP;
849 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
850 wakeup(sce);
851 }
852 selwakeup(&sce->rsel);
853 }
854
855 Static void
856 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
857 usbd_status status)
858 {
859 struct isoreq *req = addr;
860 struct ugen_endpoint *sce = req->sce;
861 u_int32_t count, n;
862 int i, isize;
863
864 /* Return if we are aborting. */
865 if (status == USBD_CANCELLED)
866 return;
867
868 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
869 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
870 count));
871
872 /* throw away oldest input if the buffer is full */
873 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
874 sce->cur += count;
875 if(sce->cur >= sce->limit)
876 sce->cur = sce->ibuf + (sce->limit - sce->cur);
877 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
878 count));
879 }
880
881 isize = UGETW(sce->edesc->wMaxPacketSize);
882 for (i = 0; i < UGEN_NISORFRMS; i++) {
883 u_int32_t actlen = req->sizes[i];
884 char const *buf = (char const *)req->dmabuf + isize * i;
885
886 /* copy data to buffer */
887 while (actlen > 0) {
888 n = min(actlen, sce->limit - sce->fill);
889 memcpy(sce->fill, buf, n);
890
891 buf += n;
892 actlen -= n;
893 sce->fill += n;
894 if(sce->fill == sce->limit)
895 sce->fill = sce->ibuf;
896 }
897
898 /* setup size for next transfer */
899 req->sizes[i] = isize;
900 }
901
902 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
903 USBD_NO_COPY, ugen_isoc_rintr);
904 (void)usbd_transfer(xfer);
905
906 if (sce->state & UGEN_ASLP) {
907 sce->state &= ~UGEN_ASLP;
908 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
909 wakeup(sce);
910 }
911 selwakeup(&sce->rsel);
912 }
913
914 Static usbd_status
915 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
916 {
917 usbd_interface_handle iface;
918 usb_endpoint_descriptor_t *ed;
919 usbd_status err;
920 struct ugen_endpoint *sce;
921 u_int8_t niface, nendpt, endptno, endpt;
922 int dir;
923
924 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
925
926 err = usbd_interface_count(sc->sc_udev, &niface);
927 if (err)
928 return (err);
929 if (ifaceidx < 0 || ifaceidx >= niface)
930 return (USBD_INVAL);
931
932 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
933 if (err)
934 return (err);
935 err = usbd_endpoint_count(iface, &nendpt);
936 if (err)
937 return (err);
938 /* XXX should only do this after setting new altno has succeeded */
939 for (endptno = 0; endptno < nendpt; endptno++) {
940 ed = usbd_interface2endpoint_descriptor(iface,endptno);
941 endpt = ed->bEndpointAddress;
942 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
943 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
944 sce->sc = 0;
945 sce->edesc = 0;
946 sce->iface = 0;
947 }
948
949 /* change setting */
950 err = usbd_set_interface(iface, altno);
951 if (err)
952 return (err);
953
954 err = usbd_endpoint_count(iface, &nendpt);
955 if (err)
956 return (err);
957 for (endptno = 0; endptno < nendpt; endptno++) {
958 ed = usbd_interface2endpoint_descriptor(iface,endptno);
959 endpt = ed->bEndpointAddress;
960 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
961 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
962 sce->sc = sc;
963 sce->edesc = ed;
964 sce->iface = iface;
965 }
966 return (0);
967 }
968
969 /* Retrieve a complete descriptor for a certain device and index. */
970 Static usb_config_descriptor_t *
971 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
972 {
973 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
974 int len;
975 usbd_status err;
976
977 if (index == USB_CURRENT_CONFIG_INDEX) {
978 tdesc = usbd_get_config_descriptor(sc->sc_udev);
979 len = UGETW(tdesc->wTotalLength);
980 if (lenp)
981 *lenp = len;
982 cdesc = malloc(len, M_TEMP, M_WAITOK);
983 memcpy(cdesc, tdesc, len);
984 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
985 } else {
986 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
987 if (err)
988 return (0);
989 len = UGETW(cdescr.wTotalLength);
990 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
991 if (lenp)
992 *lenp = len;
993 cdesc = malloc(len, M_TEMP, M_WAITOK);
994 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
995 if (err) {
996 free(cdesc, M_TEMP);
997 return (0);
998 }
999 }
1000 return (cdesc);
1001 }
1002
1003 Static int
1004 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1005 {
1006 usbd_interface_handle iface;
1007 usbd_status err;
1008
1009 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1010 if (err)
1011 return (-1);
1012 return (usbd_get_interface_altindex(iface));
1013 }
1014
1015 Static int
1016 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1017 caddr_t addr, int flag, usb_proc_ptr p)
1018 {
1019 struct ugen_endpoint *sce;
1020 usbd_status err;
1021 usbd_interface_handle iface;
1022 struct usb_config_desc *cd;
1023 usb_config_descriptor_t *cdesc;
1024 struct usb_interface_desc *id;
1025 usb_interface_descriptor_t *idesc;
1026 struct usb_endpoint_desc *ed;
1027 usb_endpoint_descriptor_t *edesc;
1028 struct usb_alt_interface *ai;
1029 struct usb_string_desc *si;
1030 u_int8_t conf, alt;
1031
1032 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1033 if (sc->sc_dying)
1034 return (EIO);
1035
1036 switch (cmd) {
1037 case FIONBIO:
1038 /* All handled in the upper FS layer. */
1039 return (0);
1040 case USB_SET_SHORT_XFER:
1041 if (endpt == USB_CONTROL_ENDPOINT)
1042 return (EINVAL);
1043 /* This flag only affects read */
1044 sce = &sc->sc_endpoints[endpt][IN];
1045 if (sce == NULL || sce->pipeh == NULL)
1046 return (EINVAL);
1047 if (*(int *)addr)
1048 sce->state |= UGEN_SHORT_OK;
1049 else
1050 sce->state &= ~UGEN_SHORT_OK;
1051 return (0);
1052 case USB_SET_TIMEOUT:
1053 sce = &sc->sc_endpoints[endpt][IN];
1054 if (sce == NULL
1055 /* XXX this shouldn't happen, but the distinction between
1056 input and output pipes isn't clear enough.
1057 || sce->pipeh == NULL */
1058 )
1059 return (EINVAL);
1060 sce->timeout = *(int *)addr;
1061 return (0);
1062 default:
1063 break;
1064 }
1065
1066 if (endpt != USB_CONTROL_ENDPOINT)
1067 return (EINVAL);
1068
1069 switch (cmd) {
1070 #ifdef UGEN_DEBUG
1071 case USB_SETDEBUG:
1072 ugendebug = *(int *)addr;
1073 break;
1074 #endif
1075 case USB_GET_CONFIG:
1076 err = usbd_get_config(sc->sc_udev, &conf);
1077 if (err)
1078 return (EIO);
1079 *(int *)addr = conf;
1080 break;
1081 case USB_SET_CONFIG:
1082 if (!(flag & FWRITE))
1083 return (EPERM);
1084 err = ugen_set_config(sc, *(int *)addr);
1085 switch (err) {
1086 case USBD_NORMAL_COMPLETION:
1087 break;
1088 case USBD_IN_USE:
1089 return (EBUSY);
1090 default:
1091 return (EIO);
1092 }
1093 break;
1094 case USB_GET_ALTINTERFACE:
1095 ai = (struct usb_alt_interface *)addr;
1096 err = usbd_device2interface_handle(sc->sc_udev,
1097 ai->uai_interface_index, &iface);
1098 if (err)
1099 return (EINVAL);
1100 idesc = usbd_get_interface_descriptor(iface);
1101 if (idesc == NULL)
1102 return (EIO);
1103 ai->uai_alt_no = idesc->bAlternateSetting;
1104 break;
1105 case USB_SET_ALTINTERFACE:
1106 if (!(flag & FWRITE))
1107 return (EPERM);
1108 ai = (struct usb_alt_interface *)addr;
1109 err = usbd_device2interface_handle(sc->sc_udev,
1110 ai->uai_interface_index, &iface);
1111 if (err)
1112 return (EINVAL);
1113 err = ugen_set_interface(sc, ai->uai_interface_index,
1114 ai->uai_alt_no);
1115 if (err)
1116 return (EINVAL);
1117 break;
1118 case USB_GET_NO_ALT:
1119 ai = (struct usb_alt_interface *)addr;
1120 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1121 if (cdesc == NULL)
1122 return (EINVAL);
1123 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1124 if (idesc == NULL) {
1125 free(cdesc, M_TEMP);
1126 return (EINVAL);
1127 }
1128 ai->uai_alt_no = usbd_get_no_alts(cdesc,
1129 idesc->bInterfaceNumber);
1130 free(cdesc, M_TEMP);
1131 break;
1132 case USB_GET_DEVICE_DESC:
1133 *(usb_device_descriptor_t *)addr =
1134 *usbd_get_device_descriptor(sc->sc_udev);
1135 break;
1136 case USB_GET_CONFIG_DESC:
1137 cd = (struct usb_config_desc *)addr;
1138 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1139 if (cdesc == NULL)
1140 return (EINVAL);
1141 cd->ucd_desc = *cdesc;
1142 free(cdesc, M_TEMP);
1143 break;
1144 case USB_GET_INTERFACE_DESC:
1145 id = (struct usb_interface_desc *)addr;
1146 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1147 if (cdesc == NULL)
1148 return (EINVAL);
1149 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1150 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1151 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1152 else
1153 alt = id->uid_alt_index;
1154 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1155 if (idesc == NULL) {
1156 free(cdesc, M_TEMP);
1157 return (EINVAL);
1158 }
1159 id->uid_desc = *idesc;
1160 free(cdesc, M_TEMP);
1161 break;
1162 case USB_GET_ENDPOINT_DESC:
1163 ed = (struct usb_endpoint_desc *)addr;
1164 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1165 if (cdesc == NULL)
1166 return (EINVAL);
1167 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1168 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1169 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1170 else
1171 alt = ed->ued_alt_index;
1172 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1173 alt, ed->ued_endpoint_index);
1174 if (edesc == NULL) {
1175 free(cdesc, M_TEMP);
1176 return (EINVAL);
1177 }
1178 ed->ued_desc = *edesc;
1179 free(cdesc, M_TEMP);
1180 break;
1181 case USB_GET_FULL_DESC:
1182 {
1183 int len;
1184 struct iovec iov;
1185 struct uio uio;
1186 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1187 int error;
1188
1189 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1190 if (len > fd->ufd_size)
1191 len = fd->ufd_size;
1192 iov.iov_base = (caddr_t)fd->ufd_data;
1193 iov.iov_len = len;
1194 uio.uio_iov = &iov;
1195 uio.uio_iovcnt = 1;
1196 uio.uio_resid = len;
1197 uio.uio_offset = 0;
1198 uio.uio_segflg = UIO_USERSPACE;
1199 uio.uio_rw = UIO_READ;
1200 uio.uio_procp = p;
1201 error = uiomove((void *)cdesc, len, &uio);
1202 free(cdesc, M_TEMP);
1203 return (error);
1204 }
1205 case USB_GET_STRING_DESC:
1206 si = (struct usb_string_desc *)addr;
1207 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1208 si->usd_language_id, &si->usd_desc);
1209 if (err)
1210 return (EINVAL);
1211 break;
1212 case USB_DO_REQUEST:
1213 {
1214 struct usb_ctl_request *ur = (void *)addr;
1215 int len = UGETW(ur->ucr_request.wLength);
1216 struct iovec iov;
1217 struct uio uio;
1218 void *ptr = 0;
1219 usbd_status err;
1220 int error = 0;
1221
1222 if (!(flag & FWRITE))
1223 return (EPERM);
1224 /* Avoid requests that would damage the bus integrity. */
1225 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1226 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1227 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1228 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1229 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1230 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1231 return (EINVAL);
1232
1233 if (len < 0 || len > 32767)
1234 return (EINVAL);
1235 if (len != 0) {
1236 iov.iov_base = (caddr_t)ur->ucr_data;
1237 iov.iov_len = len;
1238 uio.uio_iov = &iov;
1239 uio.uio_iovcnt = 1;
1240 uio.uio_resid = len;
1241 uio.uio_offset = 0;
1242 uio.uio_segflg = UIO_USERSPACE;
1243 uio.uio_rw =
1244 ur->ucr_request.bmRequestType & UT_READ ?
1245 UIO_READ : UIO_WRITE;
1246 uio.uio_procp = p;
1247 ptr = malloc(len, M_TEMP, M_WAITOK);
1248 if (uio.uio_rw == UIO_WRITE) {
1249 error = uiomove(ptr, len, &uio);
1250 if (error)
1251 goto ret;
1252 }
1253 }
1254 sce = &sc->sc_endpoints[endpt][IN];
1255 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1256 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1257 if (err) {
1258 error = EIO;
1259 goto ret;
1260 }
1261 if (len != 0) {
1262 if (uio.uio_rw == UIO_READ) {
1263 error = uiomove(ptr, len, &uio);
1264 if (error)
1265 goto ret;
1266 }
1267 }
1268 ret:
1269 if (ptr)
1270 free(ptr, M_TEMP);
1271 return (error);
1272 }
1273 case USB_GET_DEVICEINFO:
1274 usbd_fill_deviceinfo(sc->sc_udev,
1275 (struct usb_device_info *)addr, 1);
1276 break;
1277 default:
1278 return (EINVAL);
1279 }
1280 return (0);
1281 }
1282
1283 int
1284 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1285 {
1286 int endpt = UGENENDPOINT(dev);
1287 struct ugen_softc *sc;
1288 int error;
1289
1290 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1291
1292 sc->sc_refcnt++;
1293 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1294 if (--sc->sc_refcnt < 0)
1295 usb_detach_wakeup(USBDEV(sc->sc_dev));
1296 return (error);
1297 }
1298
1299 int
1300 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
1301 {
1302 struct ugen_softc *sc;
1303 struct ugen_endpoint *sce;
1304 int revents = 0;
1305 int s;
1306
1307 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1308
1309 if (sc->sc_dying)
1310 return (EIO);
1311
1312 /* XXX always IN */
1313 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1314 if (sce == NULL)
1315 return (EINVAL);
1316 #ifdef DIAGNOSTIC
1317 if (!sce->edesc) {
1318 printf("ugenpoll: no edesc\n");
1319 return (EIO);
1320 }
1321 if (!sce->pipeh) {
1322 printf("ugenpoll: no pipe\n");
1323 return (EIO);
1324 }
1325 #endif
1326 s = splusb();
1327 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1328 case UE_INTERRUPT:
1329 if (events & (POLLIN | POLLRDNORM)) {
1330 if (sce->q.c_cc > 0)
1331 revents |= events & (POLLIN | POLLRDNORM);
1332 else
1333 selrecord(p, &sce->rsel);
1334 }
1335 break;
1336 case UE_ISOCHRONOUS:
1337 if (events & (POLLIN | POLLRDNORM)) {
1338 if (sce->cur != sce->fill)
1339 revents |= events & (POLLIN | POLLRDNORM);
1340 else
1341 selrecord(p, &sce->rsel);
1342 }
1343 break;
1344 case UE_BULK:
1345 /*
1346 * We have no easy way of determining if a read will
1347 * yield any data or a write will happen.
1348 * Pretend they will.
1349 */
1350 revents |= events &
1351 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1352 break;
1353 default:
1354 break;
1355 }
1356 splx(s);
1357 return (revents);
1358 }
1359
1360 #if defined(__FreeBSD__)
1361 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
1362 #endif
1363