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