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