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