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