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