ugen.c revision 1.21 1 /* $NetBSD: ugen.c,v 1.21 1999/09/04 22:26:11 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 usbd_device_handle 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
102 #define IN 1
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 int dir;
208
209 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
210 USBDEVNAME(sc->sc_dev), configno, sc));
211 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
212 /* Avoid setting the current value. */
213 r = usbd_set_config_no(dev, configno, 0);
214 if (r != USBD_NORMAL_COMPLETION)
215 return (r);
216 }
217
218 r = usbd_interface_count(dev, &niface);
219 if (r != USBD_NORMAL_COMPLETION)
220 return (r);
221 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
222 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
223 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
224 r = usbd_device2interface_handle(dev, ifaceno, &iface);
225 if (r != USBD_NORMAL_COMPLETION)
226 return (r);
227 r = usbd_endpoint_count(iface, &nendpt);
228 if (r != USBD_NORMAL_COMPLETION)
229 return (r);
230 for (endptno = 0; endptno < nendpt; endptno++) {
231 ed = usbd_interface2endpoint_descriptor(iface,endptno);
232 endpt = ed->bEndpointAddress;
233 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
234 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
235 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
236 "(%d,%d), sce=%p\n",
237 endptno, endpt, UE_GET_ADDR(endpt),
238 UE_GET_DIR(endpt), sce));
239 sce->sc = sc;
240 sce->edesc = ed;
241 sce->iface = iface;
242 }
243 }
244 return (USBD_NORMAL_COMPLETION);
245 }
246
247 int
248 ugenopen(dev, flag, mode, p)
249 dev_t dev;
250 int flag;
251 int mode;
252 struct proc *p;
253 {
254 int unit = UGENUNIT(dev);
255 int endpt = UGENENDPOINT(dev);
256 usb_endpoint_descriptor_t *edesc;
257 struct ugen_endpoint *sce;
258 int dir, isize;
259 usbd_status r;
260
261 USB_GET_SC_OPEN(ugen, unit, sc);
262 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
263 flag, mode, unit, endpt));
264
265 if (sc->sc_dying)
266 return (ENXIO);
267
268 if (sc->sc_is_open[endpt])
269 return (EBUSY);
270
271 if (endpt == USB_CONTROL_ENDPOINT) {
272 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
273 return (0);
274 }
275 /* Make sure there are pipes for all directions. */
276 for (dir = OUT; dir <= IN; dir++) {
277 if (flag & (dir == OUT ? FWRITE : FREAD)) {
278 sce = &sc->sc_endpoints[endpt][dir];
279 if (sce == 0 || sce->edesc == 0)
280 return (ENXIO);
281 }
282 }
283
284 /* Actually open the pipes. */
285 /* XXX Should back out properly if it fails. */
286 for (dir = OUT; dir <= IN; dir++) {
287 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
288 continue;
289 sce = &sc->sc_endpoints[endpt][dir];
290 sce->state = 0;
291 sce->timeout = USBD_NO_TIMEOUT;
292 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
293 sc, endpt, dir, sce));
294 edesc = sce->edesc;
295 if (!edesc)
296 return (ENXIO);
297 switch (edesc->bmAttributes & UE_XFERTYPE) {
298 case UE_INTERRUPT:
299 isize = UGETW(edesc->wMaxPacketSize);
300 if (isize == 0) /* shouldn't happen */
301 return (EINVAL);
302 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
303 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
304 endpt, isize));
305 #if defined(__NetBSD__) || defined(__OpenBSD__)
306 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
307 return (ENOMEM);
308 #elif defined(__FreeBSD__)
309 clist_alloc_cblocks(&sce->q, UGEN_IBSIZE, 0);
310 #endif
311 r = usbd_open_pipe_intr(sce->iface,
312 edesc->bEndpointAddress,
313 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
314 sce->ibuf, isize, ugenintr);
315 if (r != USBD_NORMAL_COMPLETION) {
316 free(sce->ibuf, M_USBDEV);
317 #if defined(__NetBSD__) || defined(__OpenBSD__)
318 clfree(&sce->q);
319 #elif defined(__FreeBSD__)
320 clist_free_cblocks(&sce->q);
321 #endif
322 return (EIO);
323 }
324 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
325 break;
326 case UE_BULK:
327 r = usbd_open_pipe(sce->iface,
328 edesc->bEndpointAddress, 0,
329 &sce->pipeh);
330 if (r != USBD_NORMAL_COMPLETION)
331 return (EIO);
332 break;
333 case UE_CONTROL:
334 case UE_ISOCHRONOUS:
335 return (EINVAL);
336 }
337 }
338 sc->sc_is_open[endpt] = 1;
339 return (0);
340 }
341
342 int
343 ugenclose(dev, flag, mode, p)
344 dev_t dev;
345 int flag;
346 int mode;
347 struct proc *p;
348 {
349 USB_GET_SC(ugen, UGENUNIT(dev), sc);
350 int endpt = UGENENDPOINT(dev);
351 struct ugen_endpoint *sce;
352 int dir;
353
354 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
355 flag, mode, UGENUNIT(dev), endpt));
356
357 #ifdef DIAGNOSTIC
358 if (!sc->sc_is_open[endpt]) {
359 printf("ugenclose: not open\n");
360 return (EINVAL);
361 }
362 #endif
363
364 if (endpt == USB_CONTROL_ENDPOINT) {
365 DPRINTFN(5, ("ugenclose: close control\n"));
366 sc->sc_is_open[endpt] = 0;
367 return (0);
368 }
369
370 for (dir = OUT; dir <= IN; dir++) {
371 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
372 continue;
373 sce = &sc->sc_endpoints[endpt][dir];
374 if (!sce || !sce->pipeh)
375 continue;
376 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
377 endpt, dir, sce));
378
379 usbd_abort_pipe(sce->pipeh);
380 usbd_close_pipe(sce->pipeh);
381 sce->pipeh = 0;
382
383 if (sce->ibuf) {
384 free(sce->ibuf, M_USBDEV);
385 sce->ibuf = 0;
386 clfree(&sce->q);
387 }
388 }
389 sc->sc_is_open[endpt] = 0;
390
391 return (0);
392 }
393
394 int
395 ugen_do_read(sc, endpt, uio, flag)
396 struct ugen_softc *sc;
397 int endpt;
398 struct uio *uio;
399 int flag;
400 {
401 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
402 u_int32_t n, tn;
403 char buf[UGEN_BBSIZE];
404 usbd_request_handle reqh;
405 usbd_status r;
406 int s;
407 int error = 0;
408 u_char buffer[UGEN_CHUNK];
409
410 DPRINTFN(5, ("ugenread: %d:%d\n", sc->sc_dev.dv_unit, endpt));
411 if (sc->sc_dying)
412 return (EIO);
413
414 #ifdef DIAGNOSTIC
415 if (!sce->edesc) {
416 printf("ugenread: no edesc\n");
417 return (EIO);
418 }
419 if (!sce->pipeh) {
420 printf("ugenread: no pipe\n");
421 return (EIO);
422 }
423 #endif
424
425 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
426 case UE_INTERRUPT:
427 /* Block until activity occured. */
428 s = splusb();
429 while (sce->q.c_cc == 0) {
430 if (flag & IO_NDELAY) {
431 splx(s);
432 return (EWOULDBLOCK);
433 }
434 sce->state |= UGEN_ASLP;
435 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
436 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
437 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
438 if (sc->sc_dying)
439 error = EIO;
440 if (error) {
441 sce->state &= ~UGEN_ASLP;
442 break;
443 }
444 }
445 splx(s);
446
447 /* Transfer as many chunks as possible. */
448 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
449 n = min(sce->q.c_cc, uio->uio_resid);
450 if (n > sizeof(buffer))
451 n = sizeof(buffer);
452
453 /* Remove a small chunk from the input queue. */
454 q_to_b(&sce->q, buffer, n);
455 DPRINTFN(5, ("ugenread: got %d chars\n", n));
456
457 /* Copy the data to the user process. */
458 error = uiomove(buffer, n, uio);
459 if (error)
460 break;
461 }
462 break;
463 case UE_BULK:
464 reqh = usbd_alloc_request();
465 if (reqh == 0)
466 return (ENOMEM);
467 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
468 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
469 tn = n;
470 r = usbd_bulk_transfer(
471 reqh, sce->pipeh,
472 sce->state & UGEN_SHORT_OK ?
473 USBD_SHORT_XFER_OK : 0,
474 sce->timeout, buf, &tn, "ugenrb");
475 if (r != USBD_NORMAL_COMPLETION) {
476 if (r == USBD_INTERRUPTED)
477 error = EINTR;
478 else if (r == USBD_TIMEOUT)
479 error = ETIMEDOUT;
480 else
481 error = EIO;
482 break;
483 }
484 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
485 error = uiomove(buf, tn, uio);
486 if (error || tn < n)
487 break;
488 }
489 usbd_free_request(reqh);
490 break;
491 default:
492 return (ENXIO);
493 }
494 return (error);
495 }
496
497 int
498 ugenread(dev, uio, flag)
499 dev_t dev;
500 struct uio *uio;
501 int flag;
502 {
503 USB_GET_SC(ugen, UGENUNIT(dev), sc);
504 int endpt = UGENENDPOINT(dev);
505 int error;
506
507 sc->sc_refcnt++;
508 error = ugen_do_read(sc, endpt, uio, flag);
509 if (--sc->sc_refcnt < 0)
510 usb_detach_wakeup(&sc->sc_dev);
511 return (error);
512 }
513
514 int
515 ugen_do_write(sc, endpt, uio, flag)
516 struct ugen_softc *sc;
517 int endpt;
518 struct uio *uio;
519 int flag;
520 {
521 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
522 u_int32_t n;
523 int error = 0;
524 char buf[UGEN_BBSIZE];
525 usbd_request_handle reqh;
526 usbd_status r;
527
528 if (sc->sc_dying)
529 return (EIO);
530
531 #ifdef DIAGNOSTIC
532 if (!sce->edesc) {
533 printf("ugenwrite: no edesc\n");
534 return (EIO);
535 }
536 if (!sce->pipeh) {
537 printf("ugenwrite: no pipe\n");
538 return (EIO);
539 }
540 #endif
541
542 DPRINTF(("ugenwrite\n"));
543 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
544 case UE_BULK:
545 reqh = usbd_alloc_request();
546 if (reqh == 0)
547 return (EIO);
548 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
549 error = uiomove(buf, n, uio);
550 if (error)
551 break;
552 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
553 r = usbd_bulk_transfer(reqh, sce->pipeh, 0,
554 sce->timeout, buf, &n,"ugenwb");
555 if (r != USBD_NORMAL_COMPLETION) {
556 if (r == USBD_INTERRUPTED)
557 error = EINTR;
558 else
559 error = EIO;
560 break;
561 }
562 }
563 usbd_free_request(reqh);
564 break;
565 default:
566 return (ENXIO);
567 }
568 return (error);
569 }
570
571 int
572 ugenwrite(dev, uio, flag)
573 dev_t dev;
574 struct uio *uio;
575 int flag;
576 {
577 USB_GET_SC(ugen, UGENUNIT(dev), sc);
578 int endpt = UGENENDPOINT(dev);
579 int error;
580
581 sc->sc_refcnt++;
582 error = ugen_do_write(sc, endpt, uio, flag);
583 if (--sc->sc_refcnt < 0)
584 usb_detach_wakeup(&sc->sc_dev);
585 return (error);
586 }
587
588 int
589 ugen_activate(self, act)
590 bdevice *self;
591 enum devact act;
592 {
593 struct ugen_softc *sc = (struct ugen_softc *)self;
594
595 switch (act) {
596 case DVACT_ACTIVATE:
597 return (EOPNOTSUPP);
598 break;
599
600 case DVACT_DEACTIVATE:
601 sc->sc_dying = 1;
602 break;
603 }
604 return (0);
605 }
606
607 int
608 ugen_detach(self, flags)
609 bdevice *self;
610 int flags;
611 {
612 struct ugen_softc *sc = (struct ugen_softc *)self;
613 struct ugen_endpoint *sce;
614 int maj, mn;
615 int i, dir;
616 int s;
617
618 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
619
620 sc->sc_dying = 1;
621 /* Abort all pipes. Causes processes waiting for transfer to wake. */
622 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
623 for (dir = OUT; dir <= IN; dir++) {
624 sce = &sc->sc_endpoints[i][dir];
625 if (sce && sce->pipeh)
626 usbd_abort_pipe(sce->pipeh);
627 }
628 }
629
630 s = splusb();
631 if (--sc->sc_refcnt >= 0) {
632 /* Wake everyone */
633 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
634 wakeup(&sc->sc_endpoints[i][IN]);
635 /* Wait for processes to go away. */
636 usb_detach_wait(&sc->sc_dev);
637 }
638 splx(s);
639
640 /* locate the major number */
641 for (maj = 0; maj < nchrdev; maj++)
642 if (cdevsw[maj].d_open == ugenopen)
643 break;
644
645 /* Nuke the vnodes for any open instances (calls close). */
646 mn = self->dv_unit * USB_MAX_ENDPOINTS;
647 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
648
649 return (0);
650 }
651
652 void
653 ugenintr(reqh, addr, status)
654 usbd_request_handle reqh;
655 usbd_private_handle addr;
656 usbd_status status;
657 {
658 struct ugen_endpoint *sce = addr;
659 /*struct ugen_softc *sc = sce->sc;*/
660 u_int32_t count;
661 u_char *ibuf;
662
663 if (status == USBD_CANCELLED)
664 return;
665
666 if (status != USBD_NORMAL_COMPLETION) {
667 DPRINTF(("ugenintr: status=%d\n", status));
668 usbd_clear_endpoint_stall_async(sce->pipeh);
669 return;
670 }
671
672 usbd_get_request_status(reqh, 0, 0, &count, 0);
673 ibuf = sce->ibuf;
674
675 DPRINTFN(5, ("ugenintr: reqh=%p status=%d count=%d\n",
676 reqh, status, count));
677 DPRINTFN(5, (" data = %02x %02x %02x\n",
678 ibuf[0], ibuf[1], ibuf[2]));
679
680 (void)b_to_q(ibuf, count, &sce->q);
681
682 if (sce->state & UGEN_ASLP) {
683 sce->state &= ~UGEN_ASLP;
684 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
685 wakeup(sce);
686 }
687 selwakeup(&sce->rsel);
688 }
689
690 usbd_status
691 ugen_set_interface(sc, ifaceidx, altno)
692 struct ugen_softc *sc;
693 int ifaceidx, altno;
694 {
695 usbd_interface_handle iface;
696 usb_endpoint_descriptor_t *ed;
697 usbd_status r;
698 struct ugen_endpoint *sce;
699 u_int8_t niface, nendpt, endptno, endpt;
700 int dir;
701
702 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
703
704 r = usbd_interface_count(sc->sc_udev, &niface);
705 if (r != USBD_NORMAL_COMPLETION)
706 return (r);
707 if (ifaceidx < 0 || ifaceidx >= niface)
708 return (USBD_INVAL);
709
710 r = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
711 if (r != USBD_NORMAL_COMPLETION)
712 return (r);
713 r = usbd_endpoint_count(iface, &nendpt);
714 if (r != USBD_NORMAL_COMPLETION)
715 return (r);
716 for (endptno = 0; endptno < nendpt; endptno++) {
717 ed = usbd_interface2endpoint_descriptor(iface,endptno);
718 endpt = ed->bEndpointAddress;
719 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
720 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
721 sce->sc = 0;
722 sce->edesc = 0;
723 sce->iface = 0;
724 }
725
726 /* change setting */
727 r = usbd_set_interface(iface, altno);
728 if (r != USBD_NORMAL_COMPLETION)
729 return (r);
730
731 r = usbd_endpoint_count(iface, &nendpt);
732 if (r != USBD_NORMAL_COMPLETION)
733 return (r);
734 for (endptno = 0; endptno < nendpt; endptno++) {
735 ed = usbd_interface2endpoint_descriptor(iface,endptno);
736 endpt = ed->bEndpointAddress;
737 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
738 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
739 sce->sc = sc;
740 sce->edesc = ed;
741 sce->iface = iface;
742 }
743 return (0);
744 }
745
746 /* Retrieve a complete descriptor for a certain device and index. */
747 usb_config_descriptor_t *
748 ugen_get_cdesc(sc, index, lenp)
749 struct ugen_softc *sc;
750 int index;
751 int *lenp;
752 {
753 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
754 int len;
755 usbd_status r;
756
757 if (index == USB_CURRENT_CONFIG_INDEX) {
758 tdesc = usbd_get_config_descriptor(sc->sc_udev);
759 len = UGETW(tdesc->wTotalLength);
760 if (lenp)
761 *lenp = len;
762 cdesc = malloc(len, M_TEMP, M_WAITOK);
763 memcpy(cdesc, tdesc, len);
764 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
765 } else {
766 r = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
767 if (r != USBD_NORMAL_COMPLETION)
768 return (0);
769 len = UGETW(cdescr.wTotalLength);
770 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
771 if (lenp)
772 *lenp = len;
773 cdesc = malloc(len, M_TEMP, M_WAITOK);
774 r = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
775 if (r != USBD_NORMAL_COMPLETION) {
776 free(cdesc, M_TEMP);
777 return (0);
778 }
779 }
780 return (cdesc);
781 }
782
783 int
784 ugen_get_alt_index(sc, ifaceidx)
785 struct ugen_softc *sc;
786 int ifaceidx;
787 {
788 usbd_interface_handle iface;
789 usbd_status r;
790
791 r = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
792 if (r != USBD_NORMAL_COMPLETION)
793 return (-1);
794 return (usbd_get_interface_altindex(iface));
795 }
796
797 int
798 ugen_do_ioctl(sc, endpt, cmd, addr, flag, p)
799 struct ugen_softc *sc;
800 int endpt;
801 u_long cmd;
802 caddr_t addr;
803 int flag;
804 struct proc *p;
805 {
806 struct ugen_endpoint *sce;
807 usbd_status r;
808 usbd_interface_handle iface;
809 struct usb_config_desc *cd;
810 usb_config_descriptor_t *cdesc;
811 struct usb_interface_desc *id;
812 usb_interface_descriptor_t *idesc;
813 struct usb_endpoint_desc *ed;
814 usb_endpoint_descriptor_t *edesc;
815 struct usb_alt_interface *ai;
816 struct usb_string_desc *si;
817 u_int8_t conf, alt;
818
819 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
820 if (sc->sc_dying)
821 return (EIO);
822
823 switch (cmd) {
824 case FIONBIO:
825 /* All handled in the upper FS layer. */
826 return (0);
827 case USB_SET_SHORT_XFER:
828 /* This flag only affects read */
829 if (endpt == USB_CONTROL_ENDPOINT)
830 return (EINVAL);
831 sce = &sc->sc_endpoints[endpt][IN];
832 #ifdef DIAGNOSTIC
833 if (!sce->pipeh) {
834 printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
835 return (EIO);
836 }
837 #endif
838 if (*(int *)addr)
839 sce->state |= UGEN_SHORT_OK;
840 else
841 sce->state &= ~UGEN_SHORT_OK;
842 return (0);
843 case USB_SET_TIMEOUT:
844 sce = &sc->sc_endpoints[endpt][IN];
845 #ifdef DIAGNOSTIC
846 if (!sce->pipeh) {
847 printf("ugenioctl: USB_SET_TIMEOUT, no pipe\n");
848 return (EIO);
849 }
850 #endif
851 sce->timeout = *(int *)addr;
852 return (0);
853 default:
854 break;
855 }
856
857 if (endpt != USB_CONTROL_ENDPOINT)
858 return (EINVAL);
859
860 switch (cmd) {
861 #ifdef USB_DEBUG
862 case USB_SETDEBUG:
863 ugendebug = *(int *)addr;
864 break;
865 #endif
866 case USB_GET_CONFIG:
867 r = usbd_get_config(sc->sc_udev, &conf);
868 if (r != USBD_NORMAL_COMPLETION)
869 return (EIO);
870 *(int *)addr = conf;
871 break;
872 case USB_SET_CONFIG:
873 if (!(flag & FWRITE))
874 return (EPERM);
875 r = ugen_set_config(sc, *(int *)addr);
876 if (r != USBD_NORMAL_COMPLETION)
877 return (EIO);
878 break;
879 case USB_GET_ALTINTERFACE:
880 ai = (struct usb_alt_interface *)addr;
881 r = usbd_device2interface_handle(sc->sc_udev,
882 ai->interface_index, &iface);
883 if (r != USBD_NORMAL_COMPLETION)
884 return (EINVAL);
885 idesc = usbd_get_interface_descriptor(iface);
886 if (!idesc)
887 return (EIO);
888 ai->alt_no = idesc->bAlternateSetting;
889 break;
890 case USB_SET_ALTINTERFACE:
891 if (!(flag & FWRITE))
892 return (EPERM);
893 ai = (struct usb_alt_interface *)addr;
894 r = usbd_device2interface_handle(sc->sc_udev,
895 ai->interface_index, &iface);
896 if (r != USBD_NORMAL_COMPLETION)
897 return (EINVAL);
898 r = ugen_set_interface(sc, ai->interface_index, ai->alt_no);
899 if (r != USBD_NORMAL_COMPLETION)
900 return (EINVAL);
901 break;
902 case USB_GET_NO_ALT:
903 ai = (struct usb_alt_interface *)addr;
904 cdesc = ugen_get_cdesc(sc, ai->config_index, 0);
905 if (!cdesc)
906 return (EINVAL);
907 idesc = usbd_find_idesc(cdesc, ai->interface_index, 0);
908 if (!idesc) {
909 free(cdesc, M_TEMP);
910 return (EINVAL);
911 }
912 ai->alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
913 free(cdesc, M_TEMP);
914 break;
915 case USB_GET_DEVICE_DESC:
916 *(usb_device_descriptor_t *)addr =
917 *usbd_get_device_descriptor(sc->sc_udev);
918 break;
919 case USB_GET_CONFIG_DESC:
920 cd = (struct usb_config_desc *)addr;
921 cdesc = ugen_get_cdesc(sc, cd->config_index, 0);
922 if (!cdesc)
923 return (EINVAL);
924 cd->desc = *cdesc;
925 free(cdesc, M_TEMP);
926 break;
927 case USB_GET_INTERFACE_DESC:
928 id = (struct usb_interface_desc *)addr;
929 cdesc = ugen_get_cdesc(sc, id->config_index, 0);
930 if (!cdesc)
931 return (EINVAL);
932 if (id->config_index == USB_CURRENT_CONFIG_INDEX &&
933 id->alt_index == USB_CURRENT_ALT_INDEX)
934 alt = ugen_get_alt_index(sc, id->interface_index);
935 else
936 alt = id->alt_index;
937 idesc = usbd_find_idesc(cdesc, id->interface_index, alt);
938 if (!idesc) {
939 free(cdesc, M_TEMP);
940 return (EINVAL);
941 }
942 id->desc = *idesc;
943 free(cdesc, M_TEMP);
944 break;
945 case USB_GET_ENDPOINT_DESC:
946 ed = (struct usb_endpoint_desc *)addr;
947 cdesc = ugen_get_cdesc(sc, ed->config_index, 0);
948 if (!cdesc)
949 return (EINVAL);
950 if (ed->config_index == USB_CURRENT_CONFIG_INDEX &&
951 ed->alt_index == USB_CURRENT_ALT_INDEX)
952 alt = ugen_get_alt_index(sc, ed->interface_index);
953 else
954 alt = ed->alt_index;
955 edesc = usbd_find_edesc(cdesc, ed->interface_index,
956 alt, ed->endpoint_index);
957 if (!edesc) {
958 free(cdesc, M_TEMP);
959 return (EINVAL);
960 }
961 ed->desc = *edesc;
962 free(cdesc, M_TEMP);
963 break;
964 case USB_GET_FULL_DESC:
965 {
966 int len;
967 struct iovec iov;
968 struct uio uio;
969 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
970 int error;
971
972 cdesc = ugen_get_cdesc(sc, fd->config_index, &len);
973 if (len > fd->size)
974 len = fd->size;
975 iov.iov_base = (caddr_t)fd->data;
976 iov.iov_len = len;
977 uio.uio_iov = &iov;
978 uio.uio_iovcnt = 1;
979 uio.uio_resid = len;
980 uio.uio_offset = 0;
981 uio.uio_segflg = UIO_USERSPACE;
982 uio.uio_rw = UIO_READ;
983 uio.uio_procp = p;
984 error = uiomove((caddr_t)cdesc, len, &uio);
985 free(cdesc, M_TEMP);
986 return (error);
987 }
988 case USB_GET_STRING_DESC:
989 si = (struct usb_string_desc *)addr;
990 r = usbd_get_string_desc(sc->sc_udev, si->string_index,
991 si->language_id, &si->desc);
992 if (r != USBD_NORMAL_COMPLETION)
993 return (EINVAL);
994 break;
995 case USB_DO_REQUEST:
996 {
997 struct usb_ctl_request *ur = (void *)addr;
998 int len = UGETW(ur->request.wLength);
999 struct iovec iov;
1000 struct uio uio;
1001 void *ptr = 0;
1002 usbd_status r;
1003 int error = 0;
1004
1005 if (!(flag & FWRITE))
1006 return (EPERM);
1007 /* Avoid requests that would damage the bus integrity. */
1008 if ((ur->request.bmRequestType == UT_WRITE_DEVICE &&
1009 ur->request.bRequest == UR_SET_ADDRESS) ||
1010 (ur->request.bmRequestType == UT_WRITE_DEVICE &&
1011 ur->request.bRequest == UR_SET_CONFIG) ||
1012 (ur->request.bmRequestType == UT_WRITE_INTERFACE &&
1013 ur->request.bRequest == UR_SET_INTERFACE))
1014 return (EINVAL);
1015
1016 if (len < 0 || len > 32767)
1017 return (EINVAL);
1018 if (len != 0) {
1019 iov.iov_base = (caddr_t)ur->data;
1020 iov.iov_len = len;
1021 uio.uio_iov = &iov;
1022 uio.uio_iovcnt = 1;
1023 uio.uio_resid = len;
1024 uio.uio_offset = 0;
1025 uio.uio_segflg = UIO_USERSPACE;
1026 uio.uio_rw =
1027 ur->request.bmRequestType & UT_READ ?
1028 UIO_READ : UIO_WRITE;
1029 uio.uio_procp = p;
1030 ptr = malloc(len, M_TEMP, M_WAITOK);
1031 if (uio.uio_rw == UIO_WRITE) {
1032 error = uiomove(ptr, len, &uio);
1033 if (error)
1034 goto ret;
1035 }
1036 }
1037 r = usbd_do_request_flags(sc->sc_udev, &ur->request,
1038 ptr, ur->flags, &ur->actlen);
1039 if (r != USBD_NORMAL_COMPLETION) {
1040 error = EIO;
1041 goto ret;
1042 }
1043 if (len != 0) {
1044 if (uio.uio_rw == UIO_READ) {
1045 error = uiomove(ptr, len, &uio);
1046 if (error)
1047 goto ret;
1048 }
1049 }
1050 ret:
1051 if (ptr)
1052 free(ptr, M_TEMP);
1053 return (error);
1054 }
1055 case USB_GET_DEVICEINFO:
1056 usbd_fill_deviceinfo(sc->sc_udev,
1057 (struct usb_device_info *)addr);
1058 break;
1059 default:
1060 return (EINVAL);
1061 }
1062 return (0);
1063 }
1064
1065 int
1066 ugenioctl(dev, cmd, addr, flag, p)
1067 dev_t dev;
1068 u_long cmd;
1069 caddr_t addr;
1070 int flag;
1071 struct proc *p;
1072 {
1073 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1074 int endpt = UGENENDPOINT(dev);
1075 int error;
1076
1077 sc->sc_refcnt++;
1078 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1079 if (--sc->sc_refcnt < 0)
1080 usb_detach_wakeup(&sc->sc_dev);
1081 return (error);
1082 }
1083
1084 int
1085 ugenpoll(dev, events, p)
1086 dev_t dev;
1087 int events;
1088 struct proc *p;
1089 {
1090 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1091 struct ugen_endpoint *sce;
1092 int revents = 0;
1093 int s;
1094
1095 if (sc->sc_dying)
1096 return (EIO);
1097
1098 /* XXX always IN */
1099 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1100 #ifdef DIAGNOSTIC
1101 if (!sce->edesc) {
1102 printf("ugenwrite: no edesc\n");
1103 return (EIO);
1104 }
1105 if (!sce->pipeh) {
1106 printf("ugenpoll: no pipe\n");
1107 return (EIO);
1108 }
1109 #endif
1110 s = splusb();
1111 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1112 case UE_INTERRUPT:
1113 if (events & (POLLIN | POLLRDNORM)) {
1114 if (sce->q.c_cc > 0)
1115 revents |= events & (POLLIN | POLLRDNORM);
1116 else
1117 selrecord(p, &sce->rsel);
1118 }
1119 break;
1120 case UE_BULK:
1121 /*
1122 * We have no easy way of determining if a read will
1123 * yield any data or a write will happen.
1124 * Pretend they will.
1125 */
1126 revents |= events &
1127 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1128 break;
1129 default:
1130 break;
1131 }
1132 splx(s);
1133 return (revents);
1134 }
1135
1136 #if defined(__FreeBSD__)
1137 DEV_DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, ugen_cdevsw, usbd_driver_load, 0);
1138 #endif
1139