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