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