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