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