Fix a bug-a-boo, the type uuid was being printed instead of the storage
[dragonfly.git] / sys / dev / usbmisc / ugen / ugen.c
CommitLineData
1550dfd9
MD
1/*
2 * $NetBSD: ugen.c,v 1.27 1999/10/28 12:08:38 augustss Exp $
3 * $NetBSD: ugen.c,v 1.59 2002/07/11 21:14:28 augustss Exp $
4 * $FreeBSD: src/sys/dev/usb/ugen.c,v 1.81 2003/11/09 09:17:22 tanimura Exp $
7c070ee7 5 * $DragonFly: src/sys/dev/usbmisc/ugen/ugen.c,v 1.25 2007/06/27 12:28:00 hasso Exp $
1550dfd9
MD
6 */
7
8/*
9 * Also already merged from NetBSD:
10 * $NetBSD: ugen.c,v 1.61 2002/09/23 05:51:20 simonb Exp $
11 * $NetBSD: ugen.c,v 1.64 2003/06/28 14:21:46 darrenr Exp $
12 * $NetBSD: ugen.c,v 1.65 2003/06/29 22:30:56 fvdl Exp $
13 */
984263bc
MD
14
15/*
16 * Copyright (c) 1998 The NetBSD Foundation, Inc.
17 * All rights reserved.
18 *
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Lennart Augustsson (lennart@augustsson.net) at
21 * Carlstedt Research & Technology.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the NetBSD
34 * Foundation, Inc. and its contributors.
35 * 4. Neither the name of The NetBSD Foundation nor the names of its
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/kernel.h>
56#include <sys/malloc.h>
984263bc
MD
57#include <sys/module.h>
58#include <sys/bus.h>
59#include <sys/ioccom.h>
60#include <sys/conf.h>
61#include <sys/fcntl.h>
62#include <sys/filio.h>
984263bc
MD
63#include <sys/tty.h>
64#include <sys/file.h>
65#include <sys/select.h>
984263bc
MD
66#include <sys/vnode.h>
67#include <sys/poll.h>
68#include <sys/sysctl.h>
4e01b467 69#include <sys/thread2.h>
984263bc 70
1f2de5d4
MD
71#include <bus/usb/usb.h>
72#include <bus/usb/usbdi.h>
73#include <bus/usb/usbdi_util.h>
984263bc 74
467306a6
MD
75#include "ugenbuf.h"
76
77SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
78
984263bc
MD
79#ifdef USB_DEBUG
80#define DPRINTF(x) if (ugendebug) logprintf x
81#define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
82int ugendebug = 0;
984263bc
MD
83SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
84 &ugendebug, 0, "ugen debug level");
85#else
86#define DPRINTF(x)
87#define DPRINTFN(n,x)
88#endif
89
467306a6
MD
90static int ugen_bufsize = 16384;
91SYSCTL_INT(_hw_usb_ugen, OID_AUTO, bufsize, CTLFLAG_RW,
92 &ugen_bufsize, 0, "ugen temporary buffer size");
93
984263bc
MD
94#define UGEN_CHUNK 128 /* chunk size for read */
95#define UGEN_IBSIZE 1020 /* buffer size */
984263bc
MD
96
97#define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
98#define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
99#define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
100
101struct ugen_endpoint {
102 struct ugen_softc *sc;
b13267a5 103 cdev_t dev;
984263bc
MD
104 usb_endpoint_descriptor_t *edesc;
105 usbd_interface_handle iface;
106 int state;
107#define UGEN_ASLP 0x02 /* waiting for data */
108#define UGEN_SHORT_OK 0x04 /* short xfers are OK */
109 usbd_pipe_handle pipeh;
110 struct clist q;
111 struct selinfo rsel;
112 u_char *ibuf; /* start of buffer (circular for isoc) */
113 u_char *fill; /* location for input (isoc) */
114 u_char *limit; /* end of circular buffer (isoc) */
115 u_char *cur; /* current read location (isoc) */
116 u_int32_t timeout;
117 struct isoreq {
118 struct ugen_endpoint *sce;
119 usbd_xfer_handle xfer;
120 void *dmabuf;
121 u_int16_t sizes[UGEN_NISORFRMS];
122 } isoreqs[UGEN_NISOREQS];
123};
124
125struct ugen_softc {
126 USBBASEDEVICE sc_dev; /* base device */
127 usbd_device_handle sc_udev;
128
129 char sc_is_open[USB_MAX_ENDPOINTS];
130 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
131#define OUT 0
132#define IN 1
133
134 int sc_refcnt;
135 u_char sc_dying;
136};
137
984263bc
MD
138d_open_t ugenopen;
139d_close_t ugenclose;
140d_read_t ugenread;
141d_write_t ugenwrite;
142d_ioctl_t ugenioctl;
143d_poll_t ugenpoll;
144
145#define UGEN_CDEV_MAJOR 114
146
fef8985e
MD
147Static struct dev_ops ugen_ops = {
148 { "ugen", UGEN_CDEV_MAJOR, 0 },
149 .d_open = ugenopen,
150 .d_close = ugenclose,
151 .d_read = ugenread,
152 .d_write = ugenwrite,
153 .d_ioctl = ugenioctl,
154 .d_poll = ugenpoll,
984263bc 155};
984263bc 156
1550dfd9
MD
157Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
158 usbd_status status);
984263bc
MD
159Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
160 usbd_status status);
161Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
162Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
1550dfd9 163Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
fef8985e 164 caddr_t, int);
1550dfd9
MD
165Static void ugen_make_devnodes(struct ugen_softc *sc);
166Static void ugen_destroy_devnodes(struct ugen_softc *sc);
984263bc
MD
167Static int ugen_set_config(struct ugen_softc *sc, int configno);
168Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
1550dfd9 169 int index, int *lenp);
984263bc
MD
170Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
171Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
172
e4c9c0c8 173#define UGENUNIT(n) ((lminor(n) >> 4) & 0xff)
984263bc 174#define UGENENDPOINT(n) (minor(n) & 0xf)
e4c9c0c8
MD
175#define UGENMINOR(u, e) (((u & 0xf) << 4) | ((u & 0xf0) << 12) | (e))
176#define UGENUNITMASK 0xffff00f0
984263bc
MD
177
178USB_DECLARE_DRIVER(ugen);
179
180USB_MATCH(ugen)
181{
182 USB_MATCH_START(ugen, uaa);
183
1550dfd9
MD
184#if 0
185 if (uaa->matchlvl)
186 return (uaa->matchlvl);
187#endif
984263bc
MD
188 if (uaa->usegeneric)
189 return (UMATCH_GENERIC);
190 else
191 return (UMATCH_NONE);
192}
193
194USB_ATTACH(ugen)
195{
196 USB_ATTACH_START(ugen, sc, uaa);
197 usbd_device_handle udev;
198 char devinfo[1024];
199 usbd_status err;
200 int conf;
1550dfd9 201
984263bc
MD
202 usbd_devinfo(uaa->device, 0, devinfo);
203 USB_ATTACH_SETUP;
e3869ec7 204 kprintf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
984263bc
MD
205
206 sc->sc_udev = udev = uaa->device;
207
208 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
209
210 /* First set configuration index 0, the default one for ugen. */
211 err = usbd_set_config_index(udev, 0, 0);
212 if (err) {
e3869ec7 213 kprintf("%s: setting configuration index 0 failed\n",
984263bc
MD
214 USBDEVNAME(sc->sc_dev));
215 sc->sc_dying = 1;
216 USB_ATTACH_ERROR_RETURN;
217 }
218 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
219
220 /* Set up all the local state for this configuration. */
221 err = ugen_set_config(sc, conf);
222 if (err) {
e3869ec7 223 kprintf("%s: setting configuration %d failed\n",
984263bc
MD
224 USBDEVNAME(sc->sc_dev), conf);
225 sc->sc_dying = 1;
226 USB_ATTACH_ERROR_RETURN;
227 }
228
1550dfd9 229 /* the main device, ctrl endpoint */
fef8985e 230 dev_ops_add(&ugen_ops,
e4c9c0c8 231 UGENUNITMASK, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
fef8985e 232 make_dev(&ugen_ops, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0),
1550dfd9 233 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
1550dfd9 234
984263bc
MD
235 USB_ATTACH_SUCCESS_RETURN;
236}
237
1550dfd9
MD
238Static void
239ugen_make_devnodes(struct ugen_softc *sc)
240{
241 int endptno;
b13267a5 242 cdev_t dev;
1550dfd9
MD
243
244 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
245 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
246 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
247 /* endpt can be 0x81 and 0x01, representing
248 * endpoint address 0x01 and IN/OUT directions.
249 * We map both endpts to the same device,
250 * IN is reading from it, OUT is writing to it.
251 *
252 * In the if clause above we check whether one
253 * of the structs is populated.
254 */
fef8985e 255 dev = make_dev(&ugen_ops,
1550dfd9
MD
256 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno),
257 UID_ROOT, GID_OPERATOR, 0644,
258 "%s.%d",
259 USBDEVNAME(sc->sc_dev), endptno);
e4c9c0c8 260 if (sc->sc_endpoints[endptno][IN].sc != NULL) {
e4c9c0c8
MD
261 reference_dev(dev);
262 if (sc->sc_endpoints[endptno][IN].dev)
263 release_dev(sc->sc_endpoints[endptno][IN].dev);
1550dfd9 264 sc->sc_endpoints[endptno][IN].dev = dev;
e4c9c0c8
MD
265 }
266 if (sc->sc_endpoints[endptno][OUT].sc != NULL) {
e4c9c0c8
MD
267 reference_dev(dev);
268 if (sc->sc_endpoints[endptno][OUT].dev)
269 release_dev(sc->sc_endpoints[endptno][OUT].dev);
1550dfd9 270 sc->sc_endpoints[endptno][OUT].dev = dev;
e4c9c0c8 271 }
1550dfd9
MD
272 }
273 }
274}
275
276Static void
277ugen_destroy_devnodes(struct ugen_softc *sc)
278{
279 int endptno;
b13267a5 280 cdev_t dev;
1550dfd9
MD
281
282 /* destroy all devices for the other (existing) endpoints as well */
283 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
284 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
285 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
286 /* endpt can be 0x81 and 0x01, representing
287 * endpoint address 0x01 and IN/OUT directions.
288 * We map both endpoint addresses to the same device,
289 * IN is reading from it, OUT is writing to it.
290 *
291 * In the if clause above we check whether one
292 * of the structs is populated.
293 */
e4c9c0c8
MD
294 dev = sc->sc_endpoints[endptno][IN].dev;
295 if (dev != NULL) {
e4c9c0c8
MD
296 destroy_dev(dev);
297 sc->sc_endpoints[endptno][IN].dev = NULL;
298 }
299 dev = sc->sc_endpoints[endptno][OUT].dev;
300 if (dev != NULL) {
e4c9c0c8
MD
301 destroy_dev(dev);
302 sc->sc_endpoints[endptno][OUT].dev = NULL;
303 }
1550dfd9
MD
304 }
305 }
306}
1550dfd9 307
984263bc
MD
308Static int
309ugen_set_config(struct ugen_softc *sc, int configno)
310{
311 usbd_device_handle dev = sc->sc_udev;
312 usbd_interface_handle iface;
313 usb_endpoint_descriptor_t *ed;
314 struct ugen_endpoint *sce;
315 u_int8_t niface, nendpt;
316 int ifaceno, endptno, endpt;
317 usbd_status err;
318 int dir;
319
320 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
321 USBDEVNAME(sc->sc_dev), configno, sc));
322
1550dfd9 323 ugen_destroy_devnodes(sc);
1550dfd9 324
984263bc
MD
325 /* We start at 1, not 0, because we don't care whether the
326 * control endpoint is open or not. It is always present.
327 */
e4c9c0c8 328 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
984263bc
MD
329 if (sc->sc_is_open[endptno]) {
330 DPRINTFN(1,
331 ("ugen_set_config: %s - endpoint %d is open\n",
332 USBDEVNAME(sc->sc_dev), endptno));
333 return (USBD_IN_USE);
334 }
e4c9c0c8 335 }
984263bc 336
1550dfd9 337 /* Avoid setting the current value. */
984263bc 338 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
1550dfd9 339 err = usbd_set_config_no(dev, configno, 1);
984263bc
MD
340 if (err)
341 return (err);
342 }
343
344 err = usbd_interface_count(dev, &niface);
345 if (err)
346 return (err);
347 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
348 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
349 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
350 err = usbd_device2interface_handle(dev, ifaceno, &iface);
351 if (err)
352 return (err);
353 err = usbd_endpoint_count(iface, &nendpt);
354 if (err)
355 return (err);
356 for (endptno = 0; endptno < nendpt; endptno++) {
357 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1550dfd9
MD
358 endpt = ed->bEndpointAddress;
359 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
360 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
984263bc 361 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
1550dfd9
MD
362 "(%d,%d), sce=%p\n",
363 endptno, endpt, UE_GET_ADDR(endpt),
364 UE_GET_DIR(endpt), sce));
984263bc
MD
365 sce->sc = sc;
366 sce->edesc = ed;
367 sce->iface = iface;
368 }
369 }
370
1550dfd9 371 ugen_make_devnodes(sc);
984263bc
MD
372
373 return (USBD_NORMAL_COMPLETION);
374}
375
376int
fef8985e 377ugenopen(struct dev_open_args *ap)
984263bc 378{
b13267a5 379 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
380 struct ugen_softc *sc;
381 int unit = UGENUNIT(dev);
382 int endpt = UGENENDPOINT(dev);
383 usb_endpoint_descriptor_t *edesc;
384 struct ugen_endpoint *sce;
385 int dir, isize;
386 usbd_status err;
387 usbd_xfer_handle xfer;
388 void *buf;
389 int i, j;
390
391 USB_GET_SC_OPEN(ugen, unit, sc);
392
1550dfd9 393 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
fef8985e 394 ap->a_oflags, ap->a_devtype, unit, endpt));
984263bc
MD
395
396 if (sc == NULL || sc->sc_dying)
397 return (ENXIO);
398
399 if (sc->sc_is_open[endpt])
400 return (EBUSY);
401
402 if (endpt == USB_CONTROL_ENDPOINT) {
403 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
404 return (0);
405 }
406
407 /* Make sure there are pipes for all directions. */
408 for (dir = OUT; dir <= IN; dir++) {
fef8985e 409 if (ap->a_oflags & (dir == OUT ? FWRITE : FREAD)) {
984263bc
MD
410 sce = &sc->sc_endpoints[endpt][dir];
411 if (sce == 0 || sce->edesc == 0)
412 return (ENXIO);
413 }
414 }
415
416 /* Actually open the pipes. */
417 /* XXX Should back out properly if it fails. */
418 for (dir = OUT; dir <= IN; dir++) {
fef8985e 419 if (!(ap->a_oflags & (dir == OUT ? FWRITE : FREAD)))
984263bc
MD
420 continue;
421 sce = &sc->sc_endpoints[endpt][dir];
422 sce->state = 0;
423 sce->timeout = USBD_NO_TIMEOUT;
1550dfd9 424 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
984263bc
MD
425 sc, endpt, dir, sce));
426 edesc = sce->edesc;
427 switch (edesc->bmAttributes & UE_XFERTYPE) {
428 case UE_INTERRUPT:
1550dfd9
MD
429 if (dir == OUT) {
430 err = usbd_open_pipe(sce->iface,
431 edesc->bEndpointAddress, 0, &sce->pipeh);
432 if (err)
433 return (EIO);
434 break;
435 }
984263bc
MD
436 isize = UGETW(edesc->wMaxPacketSize);
437 if (isize == 0) /* shouldn't happen */
438 return (EINVAL);
efda3bd0 439 sce->ibuf = kmalloc(isize, M_USBDEV, M_WAITOK);
1550dfd9 440 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
984263bc
MD
441 endpt, isize));
442 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
443 return (ENOMEM);
1550dfd9
MD
444 err = usbd_open_pipe_intr(sce->iface,
445 edesc->bEndpointAddress,
446 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
984263bc
MD
447 sce->ibuf, isize, ugenintr,
448 USBD_DEFAULT_INTERVAL);
449 if (err) {
efda3bd0 450 kfree(sce->ibuf, M_USBDEV);
984263bc
MD
451 clfree(&sce->q);
452 return (EIO);
453 }
454 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
455 break;
456 case UE_BULK:
1550dfd9 457 err = usbd_open_pipe(sce->iface,
984263bc
MD
458 edesc->bEndpointAddress, 0, &sce->pipeh);
459 if (err)
460 return (EIO);
461 break;
462 case UE_ISOCHRONOUS:
463 if (dir == OUT)
464 return (EINVAL);
465 isize = UGETW(edesc->wMaxPacketSize);
466 if (isize == 0) /* shouldn't happen */
467 return (EINVAL);
77652cad 468 sce->ibuf = kmalloc(isize * UGEN_NISOFRAMES,
984263bc
MD
469 M_USBDEV, M_WAITOK);
470 sce->cur = sce->fill = sce->ibuf;
471 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
1550dfd9 472 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
984263bc
MD
473 endpt, isize));
474 err = usbd_open_pipe(sce->iface,
475 edesc->bEndpointAddress, 0, &sce->pipeh);
476 if (err) {
efda3bd0 477 kfree(sce->ibuf, M_USBDEV);
984263bc
MD
478 return (EIO);
479 }
480 for(i = 0; i < UGEN_NISOREQS; ++i) {
481 sce->isoreqs[i].sce = sce;
482 xfer = usbd_alloc_xfer(sc->sc_udev);
483 if (xfer == 0)
484 goto bad;
485 sce->isoreqs[i].xfer = xfer;
486 buf = usbd_alloc_buffer
487 (xfer, isize * UGEN_NISORFRMS);
488 if (buf == 0) {
489 i++;
490 goto bad;
491 }
492 sce->isoreqs[i].dmabuf = buf;
493 for(j = 0; j < UGEN_NISORFRMS; ++j)
494 sce->isoreqs[i].sizes[j] = isize;
495 usbd_setup_isoc_xfer
496 (xfer, sce->pipeh, &sce->isoreqs[i],
497 sce->isoreqs[i].sizes,
498 UGEN_NISORFRMS, USBD_NO_COPY,
499 ugen_isoc_rintr);
500 (void)usbd_transfer(xfer);
501 }
502 DPRINTFN(5, ("ugenopen: isoc open done\n"));
503 break;
504 bad:
505 while (--i >= 0) /* implicit buffer free */
506 usbd_free_xfer(sce->isoreqs[i].xfer);
507 return (ENOMEM);
508 case UE_CONTROL:
1550dfd9 509 sce->timeout = USBD_DEFAULT_TIMEOUT;
984263bc
MD
510 return (EINVAL);
511 }
512 }
513 sc->sc_is_open[endpt] = 1;
514 return (0);
515}
516
517int
fef8985e 518ugenclose(struct dev_close_args *ap)
984263bc 519{
b13267a5 520 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
521 int endpt = UGENENDPOINT(dev);
522 struct ugen_softc *sc;
523 struct ugen_endpoint *sce;
524 int dir;
525 int i;
526
527 USB_GET_SC(ugen, UGENUNIT(dev), sc);
528
529 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
fef8985e 530 ap->a_fflag, ap->a_devtype, UGENUNIT(dev), endpt));
984263bc
MD
531
532#ifdef DIAGNOSTIC
533 if (!sc->sc_is_open[endpt]) {
e3869ec7 534 kprintf("ugenclose: not open\n");
984263bc
MD
535 return (EINVAL);
536 }
537#endif
538
539 if (endpt == USB_CONTROL_ENDPOINT) {
540 DPRINTFN(5, ("ugenclose: close control\n"));
541 sc->sc_is_open[endpt] = 0;
542 return (0);
543 }
544
545 for (dir = OUT; dir <= IN; dir++) {
fef8985e 546 if (!(ap->a_fflag & (dir == OUT ? FWRITE : FREAD)))
984263bc
MD
547 continue;
548 sce = &sc->sc_endpoints[endpt][dir];
549 if (sce == NULL || sce->pipeh == NULL)
550 continue;
1550dfd9 551 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
984263bc
MD
552 endpt, dir, sce));
553
554 usbd_abort_pipe(sce->pipeh);
555 usbd_close_pipe(sce->pipeh);
556 sce->pipeh = NULL;
557
558 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
559 case UE_INTERRUPT:
560 ndflush(&sce->q, sce->q.c_cc);
561 clfree(&sce->q);
562 break;
563 case UE_ISOCHRONOUS:
564 for (i = 0; i < UGEN_NISOREQS; ++i)
565 usbd_free_xfer(sce->isoreqs[i].xfer);
566 default:
567 break;
568 }
569
570 if (sce->ibuf != NULL) {
efda3bd0 571 kfree(sce->ibuf, M_USBDEV);
984263bc 572 sce->ibuf = NULL;
1550dfd9 573 clfree(&sce->q);
984263bc
MD
574 }
575 }
576 sc->sc_is_open[endpt] = 0;
577
578 return (0);
579}
580
581Static int
582ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
583{
584 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
585 u_int32_t n, tn;
467306a6 586 char *buf;
984263bc
MD
587 usbd_xfer_handle xfer;
588 usbd_status err;
984263bc 589 int error = 0;
467306a6 590 int ugen_bbsize;
984263bc
MD
591 u_char buffer[UGEN_CHUNK];
592
593 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
594
595 if (sc->sc_dying)
596 return (EIO);
597
598 if (endpt == USB_CONTROL_ENDPOINT)
599 return (ENODEV);
600
1550dfd9
MD
601 if (sce == NULL)
602 return (EINVAL);
603
984263bc 604 if (sce->edesc == NULL) {
e3869ec7 605 kprintf("ugenread: no edesc\n");
984263bc
MD
606 return (EIO);
607 }
608 if (sce->pipeh == NULL) {
e3869ec7 609 kprintf("ugenread: no pipe\n");
984263bc
MD
610 return (EIO);
611 }
984263bc 612
467306a6
MD
613 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
614
984263bc
MD
615 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
616 case UE_INTERRUPT:
1550dfd9 617 /* Block until activity occurred. */
4e01b467 618 crit_enter();
984263bc
MD
619 while (sce->q.c_cc == 0) {
620 if (flag & IO_NDELAY) {
4e01b467 621 crit_exit();
467306a6
MD
622 error = EWOULDBLOCK;
623 goto done;
984263bc
MD
624 }
625 sce->state |= UGEN_ASLP;
1550dfd9 626 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
377d4740 627 error = tsleep(sce, PCATCH, "ugenri", 0);
984263bc
MD
628 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
629 if (sc->sc_dying)
630 error = EIO;
631 if (error) {
632 sce->state &= ~UGEN_ASLP;
633 break;
634 }
635 }
4e01b467 636 crit_exit();
984263bc
MD
637
638 /* Transfer as many chunks as possible. */
639 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
640 n = min(sce->q.c_cc, uio->uio_resid);
641 if (n > sizeof(buffer))
642 n = sizeof(buffer);
643
644 /* Remove a small chunk from the input queue. */
645 q_to_b(&sce->q, buffer, n);
646 DPRINTFN(5, ("ugenread: got %d chars\n", n));
647
648 /* Copy the data to the user process. */
649 error = uiomove(buffer, n, uio);
650 if (error)
651 break;
652 }
653 break;
654 case UE_BULK:
655 xfer = usbd_alloc_xfer(sc->sc_udev);
467306a6
MD
656 if (xfer == 0) {
657 error = ENOMEM;
658 goto done;
659 }
660 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
984263bc
MD
661 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
662 tn = n;
663 err = usbd_bulk_transfer(
664 xfer, sce->pipeh,
1550dfd9
MD
665 sce->state & UGEN_SHORT_OK ?
666 USBD_SHORT_XFER_OK : 0,
984263bc
MD
667 sce->timeout, buf, &tn, "ugenrb");
668 if (err) {
669 if (err == USBD_INTERRUPTED)
670 error = EINTR;
671 else if (err == USBD_TIMEOUT)
672 error = ETIMEDOUT;
673 else
674 error = EIO;
675 break;
676 }
677 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
678 error = uiomove(buf, tn, uio);
679 if (error || tn < n)
680 break;
681 }
682 usbd_free_xfer(xfer);
683 break;
684 case UE_ISOCHRONOUS:
4e01b467 685 crit_enter();
984263bc
MD
686 while (sce->cur == sce->fill) {
687 if (flag & IO_NDELAY) {
4e01b467 688 crit_exit();
467306a6
MD
689 error = EWOULDBLOCK;
690 goto done;
984263bc
MD
691 }
692 sce->state |= UGEN_ASLP;
1550dfd9 693 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
377d4740 694 error = tsleep(sce, PCATCH, "ugenri", 0);
984263bc
MD
695 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
696 if (sc->sc_dying)
697 error = EIO;
698 if (error) {
699 sce->state &= ~UGEN_ASLP;
700 break;
701 }
702 }
703
704 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
705 if(sce->fill > sce->cur)
706 n = min(sce->fill - sce->cur, uio->uio_resid);
707 else
708 n = min(sce->limit - sce->cur, uio->uio_resid);
709
710 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
711
712 /* Copy the data to the user process. */
713 error = uiomove(sce->cur, n, uio);
714 if (error)
715 break;
716 sce->cur += n;
717 if(sce->cur >= sce->limit)
718 sce->cur = sce->ibuf;
719 }
4e01b467 720 crit_exit();
984263bc
MD
721 break;
722
1550dfd9 723
984263bc 724 default:
467306a6
MD
725 error = ENXIO;
726 break;
984263bc 727 }
467306a6
MD
728done:
729 relugenbuf(buf, ugen_bbsize);
984263bc
MD
730 return (error);
731}
732
733int
fef8985e 734ugenread(struct dev_read_args *ap)
984263bc 735{
b13267a5 736 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
737 int endpt = UGENENDPOINT(dev);
738 struct ugen_softc *sc;
739 int error;
740
741 USB_GET_SC(ugen, UGENUNIT(dev), sc);
742
743 sc->sc_refcnt++;
fef8985e 744 error = ugen_do_read(sc, endpt, ap->a_uio, ap->a_ioflag);
984263bc 745 if (--sc->sc_refcnt < 0)
2939c544 746 usb_detach_wakeup(sc->sc_dev);
984263bc
MD
747 return (error);
748}
749
750Static int
751ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
752{
753 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
754 u_int32_t n;
755 int error = 0;
467306a6
MD
756 int ugen_bbsize;
757 char *buf;
984263bc
MD
758 usbd_xfer_handle xfer;
759 usbd_status err;
760
1550dfd9 761 DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
984263bc
MD
762
763 if (sc->sc_dying)
764 return (EIO);
765
766 if (endpt == USB_CONTROL_ENDPOINT)
767 return (ENODEV);
768
1550dfd9
MD
769 if (sce == NULL)
770 return (EINVAL);
771
984263bc 772 if (sce->edesc == NULL) {
e3869ec7 773 kprintf("ugenwrite: no edesc\n");
984263bc
MD
774 return (EIO);
775 }
776 if (sce->pipeh == NULL) {
e3869ec7 777 kprintf("ugenwrite: no pipe\n");
984263bc
MD
778 return (EIO);
779 }
984263bc 780
467306a6
MD
781 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
782
984263bc
MD
783 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
784 case UE_BULK:
785 xfer = usbd_alloc_xfer(sc->sc_udev);
467306a6
MD
786 if (xfer == 0) {
787 error = EIO;
788 goto done;
789 }
790 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
984263bc
MD
791 error = uiomove(buf, n, uio);
792 if (error)
793 break;
1550dfd9
MD
794 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
795 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
984263bc
MD
796 sce->timeout, buf, &n,"ugenwb");
797 if (err) {
798 if (err == USBD_INTERRUPTED)
799 error = EINTR;
1550dfd9
MD
800 else if (err == USBD_TIMEOUT)
801 error = ETIMEDOUT;
802 else
803 error = EIO;
804 break;
805 }
806 }
807 usbd_free_xfer(xfer);
808 break;
809 case UE_INTERRUPT:
810 xfer = usbd_alloc_xfer(sc->sc_udev);
467306a6
MD
811 if (xfer == 0) {
812 error = EIO;
813 goto done;
814 }
1550dfd9
MD
815 while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
816 uio->uio_resid)) != 0) {
817 error = uiomove(buf, n, uio);
818 if (error)
819 break;
820 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
821 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
822 sce->timeout, buf, &n,"ugenwi");
823 if (err) {
824 if (err == USBD_INTERRUPTED)
825 error = EINTR;
826 else if (err == USBD_TIMEOUT)
827 error = ETIMEDOUT;
984263bc
MD
828 else
829 error = EIO;
830 break;
831 }
832 }
833 usbd_free_xfer(xfer);
834 break;
835 default:
467306a6
MD
836 error = ENXIO;
837 break;
984263bc 838 }
467306a6
MD
839done:
840 relugenbuf(buf, ugen_bbsize);
984263bc
MD
841 return (error);
842}
843
844int
fef8985e 845ugenwrite(struct dev_write_args *ap)
984263bc 846{
b13267a5 847 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
848 int endpt = UGENENDPOINT(dev);
849 struct ugen_softc *sc;
850 int error;
851
852 USB_GET_SC(ugen, UGENUNIT(dev), sc);
853
854 sc->sc_refcnt++;
fef8985e 855 error = ugen_do_write(sc, endpt, ap->a_uio, ap->a_ioflag);
984263bc 856 if (--sc->sc_refcnt < 0)
2939c544 857 usb_detach_wakeup(sc->sc_dev);
984263bc
MD
858 return (error);
859}
860
984263bc
MD
861USB_DETACH(ugen)
862{
863 USB_DETACH_START(ugen, sc);
864 struct ugen_endpoint *sce;
865 int i, dir;
984263bc 866
984263bc 867 DPRINTF(("ugen_detach: sc=%p\n", sc));
984263bc
MD
868
869 sc->sc_dying = 1;
870 /* Abort all pipes. Causes processes waiting for transfer to wake. */
871 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
872 for (dir = OUT; dir <= IN; dir++) {
873 sce = &sc->sc_endpoints[i][dir];
874 if (sce && sce->pipeh)
875 usbd_abort_pipe(sce->pipeh);
876 }
877 }
4e01b467 878 crit_enter();
984263bc
MD
879 if (--sc->sc_refcnt >= 0) {
880 /* Wake everyone */
881 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
882 wakeup(&sc->sc_endpoints[i][IN]);
883 /* Wait for processes to go away. */
2939c544 884 usb_detach_wait(sc->sc_dev);
984263bc 885 }
4e01b467 886 crit_exit();
984263bc 887
984263bc 888 /* destroy the device for the control endpoint */
1550dfd9 889 ugen_destroy_devnodes(sc);
fef8985e 890 dev_ops_remove(&ugen_ops,
e4c9c0c8 891 UGENUNITMASK, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
984263bc
MD
892 return (0);
893}
894
895Static void
896ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
897{
898 struct ugen_endpoint *sce = addr;
899 /*struct ugen_softc *sc = sce->sc;*/
900 u_int32_t count;
901 u_char *ibuf;
902
903 if (status == USBD_CANCELLED)
904 return;
905
906 if (status != USBD_NORMAL_COMPLETION) {
907 DPRINTF(("ugenintr: status=%d\n", status));
1550dfd9
MD
908 if (status == USBD_STALLED)
909 usbd_clear_endpoint_stall_async(sce->pipeh);
984263bc
MD
910 return;
911 }
912
913 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
914 ibuf = sce->ibuf;
915
1550dfd9 916 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
984263bc
MD
917 xfer, status, count));
918 DPRINTFN(5, (" data = %02x %02x %02x\n",
919 ibuf[0], ibuf[1], ibuf[2]));
920
921 (void)b_to_q(ibuf, count, &sce->q);
1550dfd9 922
984263bc
MD
923 if (sce->state & UGEN_ASLP) {
924 sce->state &= ~UGEN_ASLP;
925 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
926 wakeup(sce);
927 }
518fcd30 928 selwakeuppri(&sce->rsel, 0);
984263bc
MD
929}
930
931Static void
1550dfd9 932ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
984263bc
MD
933 usbd_status status)
934{
935 struct isoreq *req = addr;
936 struct ugen_endpoint *sce = req->sce;
937 u_int32_t count, n;
1550dfd9 938 int i, isize;
984263bc
MD
939
940 /* Return if we are aborting. */
941 if (status == USBD_CANCELLED)
942 return;
943
944 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1550dfd9
MD
945 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n",
946 (int)(req - sce->isoreqs),
984263bc
MD
947 count));
948
949 /* throw away oldest input if the buffer is full */
950 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
951 sce->cur += count;
952 if(sce->cur >= sce->limit)
953 sce->cur = sce->ibuf + (sce->limit - sce->cur);
954 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
955 count));
956 }
957
1550dfd9
MD
958 isize = UGETW(sce->edesc->wMaxPacketSize);
959 for (i = 0; i < UGEN_NISORFRMS; i++) {
960 u_int32_t actlen = req->sizes[i];
961 char const *buf = (char const *)req->dmabuf + isize * i;
962
963 /* copy data to buffer */
964 while (actlen > 0) {
965 n = min(actlen, sce->limit - sce->fill);
966 memcpy(sce->fill, buf, n);
967
968 buf += n;
969 actlen -= n;
970 sce->fill += n;
971 if(sce->fill == sce->limit)
972 sce->fill = sce->ibuf;
973 }
984263bc 974
1550dfd9
MD
975 /* setup size for next transfer */
976 req->sizes[i] = isize;
984263bc
MD
977 }
978
979 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
980 USBD_NO_COPY, ugen_isoc_rintr);
981 (void)usbd_transfer(xfer);
982
983 if (sce->state & UGEN_ASLP) {
984 sce->state &= ~UGEN_ASLP;
985 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
986 wakeup(sce);
987 }
518fcd30 988 selwakeuppri(&sce->rsel, 0);
984263bc
MD
989}
990
991Static usbd_status
992ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
993{
994 usbd_interface_handle iface;
995 usb_endpoint_descriptor_t *ed;
996 usbd_status err;
997 struct ugen_endpoint *sce;
998 u_int8_t niface, nendpt, endptno, endpt;
999 int dir;
1000
1001 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1002
1003 err = usbd_interface_count(sc->sc_udev, &niface);
1004 if (err)
1005 return (err);
1006 if (ifaceidx < 0 || ifaceidx >= niface)
1007 return (USBD_INVAL);
1550dfd9 1008
984263bc
MD
1009 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1010 if (err)
1011 return (err);
1012 err = usbd_endpoint_count(iface, &nendpt);
1013 if (err)
1014 return (err);
1550dfd9 1015
1550dfd9
MD
1016 /* destroy the existing devices, we remake the new ones in a moment */
1017 ugen_destroy_devnodes(sc);
1550dfd9
MD
1018
1019 /* XXX should only do this after setting new altno has succeeded */
984263bc
MD
1020 for (endptno = 0; endptno < nendpt; endptno++) {
1021 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1022 endpt = ed->bEndpointAddress;
1023 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1024 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1025 sce->sc = 0;
1026 sce->edesc = 0;
1027 sce->iface = 0;
1028 }
1029
1030 /* change setting */
1031 err = usbd_set_interface(iface, altno);
1032 if (err)
1033 return (err);
1034
1035 err = usbd_endpoint_count(iface, &nendpt);
1036 if (err)
1037 return (err);
1038 for (endptno = 0; endptno < nendpt; endptno++) {
1039 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1040 endpt = ed->bEndpointAddress;
1041 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1042 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1043 sce->sc = sc;
1044 sce->edesc = ed;
1045 sce->iface = iface;
1046 }
1550dfd9 1047
1550dfd9
MD
1048 /* make the new devices */
1049 ugen_make_devnodes(sc);
1550dfd9 1050
984263bc
MD
1051 return (0);
1052}
1053
1054/* Retrieve a complete descriptor for a certain device and index. */
1055Static usb_config_descriptor_t *
1056ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1057{
1058 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1059 int len;
1060 usbd_status err;
1061
1062 if (index == USB_CURRENT_CONFIG_INDEX) {
1063 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1064 len = UGETW(tdesc->wTotalLength);
1065 if (lenp)
1066 *lenp = len;
efda3bd0 1067 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
984263bc
MD
1068 memcpy(cdesc, tdesc, len);
1069 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1070 } else {
1071 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1072 if (err)
1073 return (0);
1074 len = UGETW(cdescr.wTotalLength);
1075 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1076 if (lenp)
1077 *lenp = len;
efda3bd0 1078 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
984263bc
MD
1079 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1080 if (err) {
efda3bd0 1081 kfree(cdesc, M_TEMP);
984263bc
MD
1082 return (0);
1083 }
1084 }
1085 return (cdesc);
1086}
1087
1088Static int
1089ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1090{
1091 usbd_interface_handle iface;
1092 usbd_status err;
1093
1094 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1095 if (err)
1550dfd9 1096 return (-1);
984263bc
MD
1097 return (usbd_get_interface_altindex(iface));
1098}
1099
1100Static int
1101ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
fef8985e 1102 caddr_t addr, int flag)
984263bc
MD
1103{
1104 struct ugen_endpoint *sce;
1105 usbd_status err;
1106 usbd_interface_handle iface;
1107 struct usb_config_desc *cd;
1108 usb_config_descriptor_t *cdesc;
1109 struct usb_interface_desc *id;
1110 usb_interface_descriptor_t *idesc;
1111 struct usb_endpoint_desc *ed;
1112 usb_endpoint_descriptor_t *edesc;
1113 struct usb_alt_interface *ai;
1114 struct usb_string_desc *si;
1115 u_int8_t conf, alt;
1116
1117 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1118 if (sc->sc_dying)
1119 return (EIO);
1120
1121 switch (cmd) {
984263bc
MD
1122 case USB_SET_SHORT_XFER:
1123 /* This flag only affects read */
1124 if (endpt == USB_CONTROL_ENDPOINT)
1125 return (EINVAL);
1126 sce = &sc->sc_endpoints[endpt][IN];
1127 if (sce == NULL)
1128 return (EINVAL);
1550dfd9 1129
984263bc 1130 if (sce->pipeh == NULL) {
e3869ec7 1131 kprintf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
984263bc
MD
1132 return (EIO);
1133 }
1550dfd9 1134
984263bc
MD
1135 if (*(int *)addr)
1136 sce->state |= UGEN_SHORT_OK;
1137 else
1138 sce->state &= ~UGEN_SHORT_OK;
1139 return (0);
1140 case USB_SET_TIMEOUT:
1141 sce = &sc->sc_endpoints[endpt][IN];
1142 if (sce == NULL)
1143 return (EINVAL);
984263bc
MD
1144 sce->timeout = *(int *)addr;
1145 return (0);
1146 default:
1147 break;
1148 }
1149
1150 if (endpt != USB_CONTROL_ENDPOINT)
1151 return (EINVAL);
1152
1153 switch (cmd) {
1154#ifdef USB_DEBUG
1155 case USB_SETDEBUG:
1156 ugendebug = *(int *)addr;
1157 break;
1158#endif
1159 case USB_GET_CONFIG:
1160 err = usbd_get_config(sc->sc_udev, &conf);
1161 if (err)
1162 return (EIO);
1163 *(int *)addr = conf;
1164 break;
1165 case USB_SET_CONFIG:
1166 if (!(flag & FWRITE))
1167 return (EPERM);
1168 err = ugen_set_config(sc, *(int *)addr);
1169 switch (err) {
1170 case USBD_NORMAL_COMPLETION:
1171 break;
1172 case USBD_IN_USE:
1173 return (EBUSY);
1174 default:
1175 return (EIO);
1176 }
1177 break;
1178 case USB_GET_ALTINTERFACE:
1179 ai = (struct usb_alt_interface *)addr;
1550dfd9 1180 err = usbd_device2interface_handle(sc->sc_udev,
984263bc
MD
1181 ai->uai_interface_index, &iface);
1182 if (err)
1183 return (EINVAL);
1184 idesc = usbd_get_interface_descriptor(iface);
1185 if (idesc == NULL)
1186 return (EIO);
1187 ai->uai_alt_no = idesc->bAlternateSetting;
1188 break;
1189 case USB_SET_ALTINTERFACE:
1190 if (!(flag & FWRITE))
1191 return (EPERM);
1192 ai = (struct usb_alt_interface *)addr;
1550dfd9 1193 err = usbd_device2interface_handle(sc->sc_udev,
984263bc
MD
1194 ai->uai_interface_index, &iface);
1195 if (err)
1196 return (EINVAL);
1197 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1198 if (err)
1199 return (EINVAL);
1200 break;
1201 case USB_GET_NO_ALT:
1202 ai = (struct usb_alt_interface *)addr;
1203 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1204 if (cdesc == NULL)
1205 return (EINVAL);
1206 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1207 if (idesc == NULL) {
efda3bd0 1208 kfree(cdesc, M_TEMP);
984263bc
MD
1209 return (EINVAL);
1210 }
1211 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
efda3bd0 1212 kfree(cdesc, M_TEMP);
984263bc
MD
1213 break;
1214 case USB_GET_DEVICE_DESC:
1215 *(usb_device_descriptor_t *)addr =
1216 *usbd_get_device_descriptor(sc->sc_udev);
1217 break;
1218 case USB_GET_CONFIG_DESC:
1219 cd = (struct usb_config_desc *)addr;
1220 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1221 if (cdesc == NULL)
1222 return (EINVAL);
1223 cd->ucd_desc = *cdesc;
efda3bd0 1224 kfree(cdesc, M_TEMP);
984263bc
MD
1225 break;
1226 case USB_GET_INTERFACE_DESC:
1227 id = (struct usb_interface_desc *)addr;
1228 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1229 if (cdesc == NULL)
1230 return (EINVAL);
1231 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1232 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1233 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1234 else
1235 alt = id->uid_alt_index;
1236 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1237 if (idesc == NULL) {
efda3bd0 1238 kfree(cdesc, M_TEMP);
984263bc
MD
1239 return (EINVAL);
1240 }
1241 id->uid_desc = *idesc;
efda3bd0 1242 kfree(cdesc, M_TEMP);
984263bc
MD
1243 break;
1244 case USB_GET_ENDPOINT_DESC:
1245 ed = (struct usb_endpoint_desc *)addr;
1246 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1247 if (cdesc == NULL)
1248 return (EINVAL);
1249 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1250 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1251 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1252 else
1253 alt = ed->ued_alt_index;
1550dfd9 1254 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
984263bc
MD
1255 alt, ed->ued_endpoint_index);
1256 if (edesc == NULL) {
efda3bd0 1257 kfree(cdesc, M_TEMP);
984263bc
MD
1258 return (EINVAL);
1259 }
1260 ed->ued_desc = *edesc;
efda3bd0 1261 kfree(cdesc, M_TEMP);
984263bc
MD
1262 break;
1263 case USB_GET_FULL_DESC:
1264 {
1265 int len;
1266 struct iovec iov;
1267 struct uio uio;
1268 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1269 int error;
1270
1271 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1272 if (len > fd->ufd_size)
1273 len = fd->ufd_size;
1274 iov.iov_base = (caddr_t)fd->ufd_data;
1275 iov.iov_len = len;
1276 uio.uio_iov = &iov;
1277 uio.uio_iovcnt = 1;
1278 uio.uio_resid = len;
1279 uio.uio_offset = 0;
1280 uio.uio_segflg = UIO_USERSPACE;
1281 uio.uio_rw = UIO_READ;
fef8985e 1282 uio.uio_procp = curthread;
984263bc 1283 error = uiomove((void *)cdesc, len, &uio);
efda3bd0 1284 kfree(cdesc, M_TEMP);
984263bc
MD
1285 return (error);
1286 }
1287 case USB_GET_STRING_DESC:
281cf3c2
SZ
1288 {
1289 int size;
1290
984263bc 1291 si = (struct usb_string_desc *)addr;
1550dfd9 1292 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
281cf3c2 1293 si->usd_language_id, &si->usd_desc, &size);
984263bc
MD
1294 if (err)
1295 return (EINVAL);
1296 break;
281cf3c2 1297 }
984263bc
MD
1298 case USB_DO_REQUEST:
1299 {
1300 struct usb_ctl_request *ur = (void *)addr;
1301 int len = UGETW(ur->ucr_request.wLength);
1302 struct iovec iov;
1303 struct uio uio;
1304 void *ptr = 0;
1305 usbd_status err;
1306 int error = 0;
1307
1308 if (!(flag & FWRITE))
1309 return (EPERM);
1310 /* Avoid requests that would damage the bus integrity. */
1311 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1312 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1313 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1314 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1315 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1316 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1317 return (EINVAL);
1318
1319 if (len < 0 || len > 32767)
1320 return (EINVAL);
1321 if (len != 0) {
1322 iov.iov_base = (caddr_t)ur->ucr_data;
1323 iov.iov_len = len;
1324 uio.uio_iov = &iov;
1325 uio.uio_iovcnt = 1;
1326 uio.uio_resid = len;
1327 uio.uio_offset = 0;
1328 uio.uio_segflg = UIO_USERSPACE;
1329 uio.uio_rw =
1550dfd9 1330 ur->ucr_request.bmRequestType & UT_READ ?
984263bc 1331 UIO_READ : UIO_WRITE;
fef8985e 1332 uio.uio_td = curthread;
efda3bd0 1333 ptr = kmalloc(len, M_TEMP, M_WAITOK);
984263bc
MD
1334 if (uio.uio_rw == UIO_WRITE) {
1335 error = uiomove(ptr, len, &uio);
1336 if (error)
1337 goto ret;
1338 }
1339 }
1550dfd9
MD
1340 sce = &sc->sc_endpoints[endpt][IN];
1341 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1342 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
984263bc
MD
1343 if (err) {
1344 error = EIO;
1345 goto ret;
1346 }
1347 if (len != 0) {
1348 if (uio.uio_rw == UIO_READ) {
1349 error = uiomove(ptr, len, &uio);
1350 if (error)
1351 goto ret;
1352 }
1353 }
1354 ret:
1355 if (ptr)
efda3bd0 1356 kfree(ptr, M_TEMP);
984263bc
MD
1357 return (error);
1358 }
1359 case USB_GET_DEVICEINFO:
1360 usbd_fill_deviceinfo(sc->sc_udev,
1550dfd9 1361 (struct usb_device_info *)addr, 1);
984263bc
MD
1362 break;
1363 default:
1364 return (EINVAL);
1365 }
1366 return (0);
1367}
1368
1369int
fef8985e 1370ugenioctl(struct dev_ioctl_args *ap)
984263bc 1371{
b13267a5 1372 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
1373 int endpt = UGENENDPOINT(dev);
1374 struct ugen_softc *sc;
1375 int error;
1376
1377 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1378
1379 sc->sc_refcnt++;
fef8985e 1380 error = ugen_do_ioctl(sc, endpt, ap->a_cmd, ap->a_data, ap->a_fflag);
984263bc 1381 if (--sc->sc_refcnt < 0)
2939c544 1382 usb_detach_wakeup(sc->sc_dev);
984263bc
MD
1383 return (error);
1384}
1385
1386int
fef8985e 1387ugenpoll(struct dev_poll_args *ap)
984263bc 1388{
b13267a5 1389 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
1390 struct ugen_softc *sc;
1391 struct ugen_endpoint *sce;
1392 int revents = 0;
984263bc
MD
1393
1394 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1395
1396 if (sc->sc_dying)
1397 return (EIO);
1398
1399 /* XXX always IN */
1400 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1401 if (sce == NULL)
1402 return (EINVAL);
1550dfd9 1403
984263bc 1404 if (!sce->edesc) {
e3869ec7 1405 kprintf("ugenpoll: no edesc\n");
984263bc
MD
1406 return (EIO);
1407 }
1408 if (!sce->pipeh) {
e3869ec7 1409 kprintf("ugenpoll: no pipe\n");
984263bc
MD
1410 return (EIO);
1411 }
1550dfd9 1412
4e01b467 1413 crit_enter();
984263bc
MD
1414 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1415 case UE_INTERRUPT:
fef8985e 1416 if (ap->a_events & (POLLIN | POLLRDNORM)) {
984263bc 1417 if (sce->q.c_cc > 0)
fef8985e 1418 revents |= ap->a_events & (POLLIN | POLLRDNORM);
984263bc 1419 else
fef8985e 1420 selrecord(curthread, &sce->rsel);
984263bc
MD
1421 }
1422 break;
1423 case UE_ISOCHRONOUS:
fef8985e 1424 if (ap->a_events & (POLLIN | POLLRDNORM)) {
984263bc 1425 if (sce->cur != sce->fill)
fef8985e 1426 revents |= ap->a_events & (POLLIN | POLLRDNORM);
984263bc 1427 else
fef8985e 1428 selrecord(curthread, &sce->rsel);
984263bc
MD
1429 }
1430 break;
1431 case UE_BULK:
1550dfd9 1432 /*
984263bc
MD
1433 * We have no easy way of determining if a read will
1434 * yield any data or a write will happen.
1435 * Pretend they will.
1436 */
fef8985e 1437 revents |= ap->a_events &
984263bc
MD
1438 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1439 break;
1440 default:
1441 break;
1442 }
4e01b467 1443 crit_exit();
fef8985e
MD
1444 ap->a_events = revents;
1445 return (0);
984263bc
MD
1446}
1447
984263bc 1448DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
7c070ee7 1449