2006-06-05 14:44:39 +00:00
|
|
|
/* $NetBSD: ugen.c,v 1.79 2006/03/01 12:38:13 yamt Exp $ */
|
1998-12-14 09:32:25 +00:00
|
|
|
|
2003-07-14 18:25:47 +00:00
|
|
|
/* Also already merged from NetBSD:
|
|
|
|
* $NetBSD: ugen.c,v 1.61 2002/09/23 05:51:20 simonb Exp $
|
2003-07-14 20:11:52 +00:00
|
|
|
* $NetBSD: ugen.c,v 1.64 2003/06/28 14:21:46 darrenr Exp $
|
|
|
|
* $NetBSD: ugen.c,v 1.65 2003/06/29 22:30:56 fvdl Exp $
|
MFNetBSD.
uhid.c (1.61), author: jdolecek
add support for USB_GET_DEVICEINFO and USB_GET_STRING_DESC ioctls,
with same meaning as for ugen(4)
usbdi_util.h (1.29), usb_quirks.c (1.50), uhid.c (1.62),
ugen.c (1.68), usb_subr.c (1.114) author: mycroft
Yes, some devices return incorrect lengths in their string
descriptors. Rather than losing, do what Windows does: just
request the maximum size, and allow a shorter response. Obsoletes
the need for UQ_NO_STRINGS, and therefore these "quirks" are removed.
usb_subr.c (1.116), author: mycroft
In the "seemed like a good idea until I found the fatal flaw"
department... Attempting to read a maximum-size string descriptor
causes my kue device to go completely apeshit. So, go back to the
original method, but allow the device to return a shorter string than
it claimed.
Obtained from: NetBSD
2004-06-26 10:35:10 +00:00
|
|
|
* $NetBSD: ugen.c,v 1.68 2004/06/23 02:30:52 mycroft Exp $
|
2003-07-14 18:25:47 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
1998-11-26 23:13:13 +00:00
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1999-01-07 23:07:57 +00:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2000-05-14 16:43:10 +00:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) at
|
1999-01-07 23:07:57 +00:00
|
|
|
* Carlstedt Research & Technology.
|
1998-11-26 23:13:13 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
2004-12-13 07:45:02 +00:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
1998-11-26 23:13:13 +00:00
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2008-07-24 09:54:10 +00:00
|
|
|
#include <sys/clist.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/kernel.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#include <sys/malloc.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#include <sys/ioccom.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/conf.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#include <sys/file.h>
|
2001-01-09 04:33:49 +00:00
|
|
|
#include <sys/selinfo.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#include <sys/poll.h>
|
2002-07-31 13:33:55 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-12-13 07:08:22 +00:00
|
|
|
#include <sys/uio.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
|
2002-07-31 14:34:36 +00:00
|
|
|
#ifdef USB_DEBUG
|
2006-09-06 21:53:28 +00:00
|
|
|
#define DPRINTF(x) if (ugendebug) printf x
|
|
|
|
#define DPRINTFN(n,x) if (ugendebug>(n)) printf x
|
2000-05-14 17:33:26 +00:00
|
|
|
int ugendebug = 0;
|
2002-08-08 12:05:51 +00:00
|
|
|
SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
|
|
|
|
SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
|
2002-07-31 13:33:55 +00:00
|
|
|
&ugendebug, 0, "ugen debug level");
|
1998-11-26 23:13:13 +00:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
2000-12-07 10:28:25 +00:00
|
|
|
#define UGEN_CHUNK 128 /* chunk size for read */
|
|
|
|
#define UGEN_IBSIZE 1020 /* buffer size */
|
|
|
|
#define UGEN_BBSIZE 1024
|
|
|
|
|
|
|
|
#define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
|
|
|
|
#define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
|
|
|
|
#define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
struct ugen_endpoint {
|
|
|
|
struct ugen_softc *sc;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
1999-01-07 23:07:57 +00:00
|
|
|
usb_endpoint_descriptor_t *edesc;
|
|
|
|
usbd_interface_handle iface;
|
|
|
|
int state;
|
|
|
|
#define UGEN_ASLP 0x02 /* waiting for data */
|
|
|
|
#define UGEN_SHORT_OK 0x04 /* short xfers are OK */
|
|
|
|
usbd_pipe_handle pipeh;
|
|
|
|
struct clist q;
|
|
|
|
struct selinfo rsel;
|
2000-12-07 10:28:25 +00:00
|
|
|
u_char *ibuf; /* start of buffer (circular for isoc) */
|
|
|
|
u_char *fill; /* location for input (isoc) */
|
|
|
|
u_char *limit; /* end of circular buffer (isoc) */
|
|
|
|
u_char *cur; /* current read location (isoc) */
|
1999-10-07 19:26:38 +00:00
|
|
|
u_int32_t timeout;
|
2000-12-07 10:28:25 +00:00
|
|
|
struct isoreq {
|
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
void *dmabuf;
|
|
|
|
u_int16_t sizes[UGEN_NISORFRMS];
|
|
|
|
} isoreqs[UGEN_NISOREQS];
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
struct ugen_softc {
|
2006-09-06 23:29:53 +00:00
|
|
|
device_t sc_dev; /* base device */
|
1999-10-07 19:26:38 +00:00
|
|
|
usbd_device_handle sc_udev;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
char sc_is_open[USB_MAX_ENDPOINTS];
|
1999-01-07 23:07:57 +00:00
|
|
|
struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
|
1999-10-07 19:26:38 +00:00
|
|
|
#define OUT 0
|
|
|
|
#define IN 1
|
1998-11-26 23:13:13 +00:00
|
|
|
|
2004-10-02 22:49:54 +00:00
|
|
|
#define UGEN_DEV_REF(dev, sc) \
|
|
|
|
if ((sc)->sc_dying || dev_refthread(dev) == NULL) \
|
|
|
|
return (ENXIO)
|
|
|
|
#define UGEN_DEV_RELE(dev, sc) \
|
|
|
|
dev_relthread(dev)
|
|
|
|
#define UGEN_DEV_OPEN(dev, sc) \
|
|
|
|
/* handled by dev layer */
|
|
|
|
#define UGEN_DEV_CLOSE(dev, sc) \
|
|
|
|
/* handled by dev layer */
|
1999-10-07 19:26:38 +00:00
|
|
|
u_char sc_dying;
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
|
1999-01-21 22:56:10 +00:00
|
|
|
d_open_t ugenopen;
|
|
|
|
d_close_t ugenclose;
|
|
|
|
d_read_t ugenread;
|
|
|
|
d_write_t ugenwrite;
|
|
|
|
d_ioctl_t ugenioctl;
|
|
|
|
d_poll_t ugenpoll;
|
2004-10-02 22:49:54 +00:00
|
|
|
d_purge_t ugenpurge;
|
1999-01-21 22:56:10 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static struct cdevsw ugenctl_cdevsw = {
|
2004-10-02 22:49:54 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
|
|
|
.d_open = ugenopen,
|
|
|
|
.d_close = ugenclose,
|
|
|
|
.d_ioctl = ugenioctl,
|
|
|
|
.d_purge = ugenpurge,
|
|
|
|
.d_name = "ugenctl",
|
|
|
|
};
|
1999-01-21 22:56:10 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static struct cdevsw ugen_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = ugenopen,
|
|
|
|
.d_close = ugenclose,
|
|
|
|
.d_read = ugenread,
|
|
|
|
.d_write = ugenwrite,
|
|
|
|
.d_ioctl = ugenioctl,
|
|
|
|
.d_poll = ugenpoll,
|
2004-10-02 22:49:54 +00:00
|
|
|
.d_purge = ugenpurge,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_name = "ugen",
|
1999-01-21 22:56:10 +00:00
|
|
|
};
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
|
2000-12-07 10:13:18 +00:00
|
|
|
usbd_status status);
|
2006-09-06 21:53:28 +00:00
|
|
|
static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
|
2000-12-07 10:28:25 +00:00
|
|
|
usbd_status status);
|
2006-09-06 21:53:28 +00:00
|
|
|
static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
|
|
|
|
static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
|
|
|
|
static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
|
2006-09-06 23:29:53 +00:00
|
|
|
caddr_t, int, struct thread *);
|
2006-09-06 21:53:28 +00:00
|
|
|
static void ugen_make_devnodes(struct ugen_softc *sc);
|
|
|
|
static void ugen_destroy_devnodes(struct ugen_softc *sc);
|
|
|
|
static int ugen_set_config(struct ugen_softc *sc, int configno);
|
|
|
|
static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
|
2000-12-07 10:13:18 +00:00
|
|
|
int index, int *lenp);
|
2006-09-06 21:53:28 +00:00
|
|
|
static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
|
|
|
|
static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
2008-09-27 08:51:18 +00:00
|
|
|
#define UGENUNIT(n) ((dev2unit(n) >> 4) & 0xf)
|
|
|
|
#define UGENENDPOINT(n) (dev2unit(n) & 0xf)
|
1999-11-30 01:48:12 +00:00
|
|
|
#define UGENMINOR(u, e) (((u) << 4) | (e))
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static device_probe_t ugen_match;
|
|
|
|
static device_attach_t ugen_attach;
|
|
|
|
static device_detach_t ugen_detach;
|
|
|
|
|
|
|
|
static devclass_t ugen_devclass;
|
|
|
|
|
|
|
|
static device_method_t ugen_methods[] = {
|
|
|
|
DEVMETHOD(device_probe, ugen_match),
|
|
|
|
DEVMETHOD(device_attach, ugen_attach),
|
|
|
|
DEVMETHOD(device_detach, ugen_detach),
|
|
|
|
{0,0},
|
|
|
|
{0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t ugen_driver = {
|
|
|
|
"ugen",
|
|
|
|
ugen_methods,
|
|
|
|
sizeof(struct ugen_softc)
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEPEND(ugen, usb, 1, 1, 1);
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
|
|
|
ugen_match(device_t self)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
2006-09-06 21:53:28 +00:00
|
|
|
struct usb_attach_arg *uaa = device_get_ivars(self);
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2001-12-31 00:52:17 +00:00
|
|
|
#if 0
|
|
|
|
if (uaa->matchlvl)
|
|
|
|
return (uaa->matchlvl);
|
|
|
|
#endif
|
1999-01-07 23:07:57 +00:00
|
|
|
if (uaa->usegeneric)
|
|
|
|
return (UMATCH_GENERIC);
|
|
|
|
else
|
|
|
|
return (UMATCH_NONE);
|
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
|
|
|
ugen_attach(device_t self)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
2006-09-06 21:53:28 +00:00
|
|
|
struct ugen_softc *sc = device_get_softc(self);
|
|
|
|
struct usb_attach_arg *uaa = device_get_ivars(self);
|
2000-05-14 17:33:26 +00:00
|
|
|
usbd_device_handle udev;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-01-07 23:07:57 +00:00
|
|
|
int conf;
|
2003-07-04 01:50:39 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc->sc_dev = self;
|
2000-05-14 17:33:26 +00:00
|
|
|
sc->sc_udev = udev = uaa->device;
|
|
|
|
|
2000-12-12 10:19:19 +00:00
|
|
|
memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
|
|
|
|
|
2000-05-14 17:33:26 +00:00
|
|
|
/* First set configuration index 0, the default one for ugen. */
|
|
|
|
err = usbd_set_config_index(udev, 0, 0);
|
|
|
|
if (err) {
|
2003-07-04 01:50:39 +00:00
|
|
|
printf("%s: setting configuration index 0 failed\n",
|
2006-09-06 23:29:53 +00:00
|
|
|
device_get_nameunit(sc->sc_dev));
|
2000-05-14 17:33:26 +00:00
|
|
|
sc->sc_dying = 1;
|
2006-09-06 21:53:28 +00:00
|
|
|
return (ENXIO);
|
2000-05-14 17:33:26 +00:00
|
|
|
}
|
|
|
|
conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
|
|
|
|
|
|
|
|
/* Set up all the local state for this configuration. */
|
1999-11-17 22:33:51 +00:00
|
|
|
err = ugen_set_config(sc, conf);
|
|
|
|
if (err) {
|
2003-07-04 01:50:39 +00:00
|
|
|
printf("%s: setting configuration %d failed\n",
|
2006-09-06 23:29:53 +00:00
|
|
|
device_get_nameunit(sc->sc_dev), conf);
|
1999-10-07 19:26:38 +00:00
|
|
|
sc->sc_dying = 1;
|
2006-09-06 21:53:28 +00:00
|
|
|
return (ENXIO);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
1999-10-07 19:26:38 +00:00
|
|
|
|
2002-07-09 01:01:25 +00:00
|
|
|
/* the main device, ctrl endpoint */
|
2004-10-02 22:49:54 +00:00
|
|
|
sc->dev = make_dev(&ugenctl_cdevsw,
|
2006-09-06 23:29:53 +00:00
|
|
|
UGENMINOR(device_get_unit(sc->sc_dev), 0), UID_ROOT, GID_OPERATOR, 0644,
|
|
|
|
"%s", device_get_nameunit(sc->sc_dev));
|
2004-10-02 22:49:54 +00:00
|
|
|
ugen_make_devnodes(sc);
|
2006-09-06 23:29:53 +00:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
|
2006-09-06 21:53:28 +00:00
|
|
|
return (0);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static void
|
2000-12-12 10:19:19 +00:00
|
|
|
ugen_make_devnodes(struct ugen_softc *sc)
|
|
|
|
{
|
|
|
|
int endptno;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2000-12-12 10:19:19 +00:00
|
|
|
|
|
|
|
for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
|
|
|
|
if (sc->sc_endpoints[endptno][IN].sc != NULL ||
|
|
|
|
sc->sc_endpoints[endptno][OUT].sc != NULL ) {
|
|
|
|
/* endpt can be 0x81 and 0x01, representing
|
|
|
|
* endpoint address 0x01 and IN/OUT directions.
|
|
|
|
* We map both endpts to the same device,
|
|
|
|
* IN is reading from it, OUT is writing to it.
|
|
|
|
*
|
|
|
|
* In the if clause above we check whether one
|
|
|
|
* of the structs is populated.
|
|
|
|
*/
|
2003-10-01 14:49:53 +00:00
|
|
|
dev = make_dev(&ugen_cdevsw,
|
2006-09-06 23:29:53 +00:00
|
|
|
UGENMINOR(device_get_unit(sc->sc_dev), endptno),
|
2000-12-12 10:19:19 +00:00
|
|
|
UID_ROOT, GID_OPERATOR, 0644,
|
|
|
|
"%s.%d",
|
2006-09-06 23:29:53 +00:00
|
|
|
device_get_nameunit(sc->sc_dev), endptno);
|
2004-10-02 22:49:54 +00:00
|
|
|
dev_depends(sc->dev, dev);
|
2003-10-01 13:53:51 +00:00
|
|
|
if (sc->sc_endpoints[endptno][IN].sc != NULL)
|
2003-10-12 15:51:40 +00:00
|
|
|
sc->sc_endpoints[endptno][IN].dev = dev;
|
2003-10-01 13:53:51 +00:00
|
|
|
if (sc->sc_endpoints[endptno][OUT].sc != NULL)
|
2003-10-12 15:51:40 +00:00
|
|
|
sc->sc_endpoints[endptno][OUT].dev = dev;
|
2000-12-12 10:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static void
|
2000-12-12 10:19:19 +00:00
|
|
|
ugen_destroy_devnodes(struct ugen_softc *sc)
|
|
|
|
{
|
2006-06-05 14:44:39 +00:00
|
|
|
int endptno, prev_sc_dying;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2000-12-12 10:19:19 +00:00
|
|
|
|
2006-06-05 14:44:39 +00:00
|
|
|
prev_sc_dying = sc->sc_dying;
|
|
|
|
sc->sc_dying = 1;
|
2000-12-12 10:19:19 +00:00
|
|
|
/* destroy all devices for the other (existing) endpoints as well */
|
|
|
|
for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
|
|
|
|
if (sc->sc_endpoints[endptno][IN].sc != NULL ||
|
|
|
|
sc->sc_endpoints[endptno][OUT].sc != NULL ) {
|
|
|
|
/* endpt can be 0x81 and 0x01, representing
|
|
|
|
* endpoint address 0x01 and IN/OUT directions.
|
|
|
|
* We map both endpoint addresses to the same device,
|
|
|
|
* IN is reading from it, OUT is writing to it.
|
|
|
|
*
|
|
|
|
* In the if clause above we check whether one
|
|
|
|
* of the structs is populated.
|
|
|
|
*/
|
2003-10-01 13:53:51 +00:00
|
|
|
if (sc->sc_endpoints[endptno][IN].sc != NULL)
|
2003-10-12 15:51:40 +00:00
|
|
|
dev = sc->sc_endpoints[endptno][IN].dev;
|
2003-10-01 13:53:51 +00:00
|
|
|
else
|
2003-10-12 15:51:40 +00:00
|
|
|
dev = sc->sc_endpoints[endptno][OUT].dev;
|
2006-06-05 14:44:39 +00:00
|
|
|
|
|
|
|
KASSERT(dev != NULL,
|
|
|
|
("ugen_destroy_devnodes: NULL dev"));
|
|
|
|
if(dev != NULL)
|
|
|
|
destroy_dev(dev);
|
|
|
|
|
|
|
|
sc->sc_endpoints[endptno][IN].sc = NULL;
|
|
|
|
sc->sc_endpoints[endptno][OUT].sc = NULL;
|
2000-12-12 10:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-05 14:44:39 +00:00
|
|
|
sc->sc_dying = prev_sc_dying;
|
2000-12-12 10:19:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_set_config(struct ugen_softc *sc, int configno)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
|
|
|
usbd_device_handle dev = sc->sc_udev;
|
|
|
|
usbd_interface_handle iface;
|
|
|
|
usb_endpoint_descriptor_t *ed;
|
2004-12-12 02:27:30 +00:00
|
|
|
struct ugen_endpoint *sce, **sce_cache, ***sce_cache_arr;
|
|
|
|
u_int8_t niface, niface_cache, nendpt, *nendpt_cache;
|
1999-01-07 23:07:57 +00:00
|
|
|
int ifaceno, endptno, endpt;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-10-07 19:26:38 +00:00
|
|
|
int dir;
|
1999-01-07 23:07:57 +00:00
|
|
|
|
|
|
|
DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
|
2006-09-06 23:29:53 +00:00
|
|
|
device_get_nameunit(sc->sc_dev), configno, sc));
|
2000-01-23 15:48:29 +00:00
|
|
|
|
|
|
|
/* We start at 1, not 0, because we don't care whether the
|
|
|
|
* control endpoint is open or not. It is always present.
|
|
|
|
*/
|
|
|
|
for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
|
2000-01-24 10:23:57 +00:00
|
|
|
if (sc->sc_is_open[endptno]) {
|
|
|
|
DPRINTFN(1,
|
|
|
|
("ugen_set_config: %s - endpoint %d is open\n",
|
2006-09-06 23:29:53 +00:00
|
|
|
device_get_nameunit(sc->sc_dev), endptno));
|
2000-01-23 15:48:29 +00:00
|
|
|
return (USBD_IN_USE);
|
2000-01-24 10:23:57 +00:00
|
|
|
}
|
2000-01-23 15:48:29 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_interface_count(dev, &niface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
2004-12-12 02:27:30 +00:00
|
|
|
/* store an array of endpoint descriptors to clear if the configuration
|
|
|
|
* change succeeds - these aren't available afterwards */
|
2006-06-05 14:44:39 +00:00
|
|
|
nendpt_cache = malloc(sizeof(u_int8_t) * niface, M_TEMP, M_WAITOK |
|
|
|
|
M_ZERO);
|
2004-12-12 02:27:30 +00:00
|
|
|
sce_cache_arr = malloc(sizeof(struct ugen_endpoint **) * niface, M_TEMP,
|
2006-06-05 14:44:39 +00:00
|
|
|
M_WAITOK | M_ZERO);
|
2004-12-12 02:27:30 +00:00
|
|
|
niface_cache = niface;
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
for (ifaceno = 0; ifaceno < niface; ifaceno++) {
|
|
|
|
DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_device2interface_handle(dev, ifaceno, &iface);
|
|
|
|
if (err)
|
2004-12-12 02:27:30 +00:00
|
|
|
panic("ugen_set_config: can't obtain interface handle");
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
2004-12-12 02:27:30 +00:00
|
|
|
panic("ugen_set_config: endpoint count failed");
|
|
|
|
|
|
|
|
/* store endpoint descriptors for each interface */
|
|
|
|
nendpt_cache[ifaceno] = nendpt;
|
|
|
|
sce_cache = malloc(sizeof(struct ugen_endpoint *) * nendpt, M_TEMP,
|
|
|
|
M_WAITOK);
|
|
|
|
sce_cache_arr[ifaceno] = sce_cache;
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
2001-12-31 00:52:17 +00:00
|
|
|
endpt = ed->bEndpointAddress;
|
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
2004-12-12 02:27:30 +00:00
|
|
|
sce_cache[endptno] = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Avoid setting the current value. */
|
|
|
|
if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
|
|
|
|
/* attempt to perform the configuration change */
|
|
|
|
err = usbd_set_config_no(dev, configno, 1);
|
|
|
|
if (err) {
|
|
|
|
for(ifaceno = 0; ifaceno < niface_cache; ifaceno++)
|
|
|
|
free(sce_cache_arr[ifaceno], M_TEMP);
|
|
|
|
free(sce_cache_arr, M_TEMP);
|
|
|
|
free(nendpt_cache, M_TEMP);
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ugen_destroy_devnodes(sc);
|
|
|
|
|
|
|
|
/* now we can clear the old interface's ugen_endpoints */
|
|
|
|
for(ifaceno = 0; ifaceno < niface_cache; ifaceno++) {
|
|
|
|
sce_cache = sce_cache_arr[ifaceno];
|
|
|
|
for(endptno = 0; endptno < nendpt_cache[ifaceno]; endptno++) {
|
|
|
|
sce = sce_cache[endptno];
|
|
|
|
sce->sc = 0;
|
|
|
|
sce->edesc = 0;
|
|
|
|
sce->iface = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and free the cache storing them */
|
|
|
|
for(ifaceno = 0; ifaceno < niface_cache; ifaceno++)
|
|
|
|
free(sce_cache_arr[ifaceno], M_TEMP);
|
|
|
|
free(sce_cache_arr, M_TEMP);
|
|
|
|
free(nendpt_cache, M_TEMP);
|
|
|
|
|
|
|
|
/* no endpoints if the device is in the unconfigured state */
|
|
|
|
if (configno != USB_UNCONFIG_NO)
|
|
|
|
{
|
|
|
|
/* set the new configuration's ugen_endpoints */
|
|
|
|
err = usbd_interface_count(dev, &niface);
|
|
|
|
if (err)
|
|
|
|
panic("ugen_set_config: interface count failed");
|
|
|
|
|
|
|
|
memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
|
|
|
|
for (ifaceno = 0; ifaceno < niface; ifaceno++) {
|
|
|
|
DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
|
|
|
|
err = usbd_device2interface_handle(dev, ifaceno, &iface);
|
|
|
|
if (err)
|
|
|
|
panic("ugen_set_config: can't obtain interface handle");
|
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
|
|
|
panic("ugen_set_config: endpoint count failed");
|
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
|
|
|
endpt = ed->bEndpointAddress;
|
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
|
|
|
sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
|
|
|
DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
|
|
|
|
"(%d,%d), sce=%p\n",
|
|
|
|
endptno, endpt, UE_GET_ADDR(endpt),
|
|
|
|
UE_GET_DIR(endpt), sce));
|
|
|
|
sce->sc = sc;
|
|
|
|
sce->edesc = ed;
|
|
|
|
sce->iface = iface;
|
|
|
|
}
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
1999-11-28 21:01:06 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
return (USBD_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
int
|
2006-09-06 23:29:53 +00:00
|
|
|
ugenopen(struct cdev *dev, int flag, int mode, struct thread *p)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
struct ugen_softc *sc;
|
1999-01-07 23:07:57 +00:00
|
|
|
int unit = UGENUNIT(dev);
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
|
|
|
usb_endpoint_descriptor_t *edesc;
|
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
int dir, isize;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
2000-12-07 10:28:25 +00:00
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
void *buf;
|
|
|
|
int i, j;
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, unit);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
|
1999-01-07 23:07:57 +00:00
|
|
|
flag, mode, unit, endpt));
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (sc == NULL || sc->sc_dying)
|
1999-10-07 19:26:38 +00:00
|
|
|
return (ENXIO);
|
1999-01-07 23:07:57 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (sc->sc_is_open[endpt])
|
|
|
|
return (EBUSY);
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT) {
|
1999-11-17 22:33:51 +00:00
|
|
|
sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_OPEN(dev, sc);
|
1999-10-07 19:26:38 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2000-05-14 17:33:26 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
/* Make sure there are pipes for all directions. */
|
|
|
|
for (dir = OUT; dir <= IN; dir++) {
|
|
|
|
if (flag & (dir == OUT ? FWRITE : FREAD)) {
|
|
|
|
sce = &sc->sc_endpoints[endpt][dir];
|
2004-10-02 22:33:26 +00:00
|
|
|
if (sce->edesc == 0)
|
1999-10-07 19:26:38 +00:00
|
|
|
return (ENXIO);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Actually open the pipes. */
|
|
|
|
/* XXX Should back out properly if it fails. */
|
|
|
|
for (dir = OUT; dir <= IN; dir++) {
|
|
|
|
if (!(flag & (dir == OUT ? FWRITE : FREAD)))
|
|
|
|
continue;
|
1999-01-07 23:07:57 +00:00
|
|
|
sce = &sc->sc_endpoints[endpt][dir];
|
1999-10-07 19:26:38 +00:00
|
|
|
sce->state = 0;
|
|
|
|
sce->timeout = USBD_NO_TIMEOUT;
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
|
1999-01-07 23:07:57 +00:00
|
|
|
sc, endpt, dir, sce));
|
|
|
|
edesc = sce->edesc;
|
|
|
|
switch (edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_INTERRUPT:
|
2003-02-28 19:28:29 +00:00
|
|
|
if (dir == OUT) {
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_open_pipe(sce->iface,
|
2003-02-28 19:28:29 +00:00
|
|
|
edesc->bEndpointAddress, 0, &sce->pipeh);
|
|
|
|
if (err)
|
|
|
|
return (EIO);
|
|
|
|
break;
|
|
|
|
}
|
1999-01-07 23:07:57 +00:00
|
|
|
isize = UGETW(edesc->wMaxPacketSize);
|
|
|
|
if (isize == 0) /* shouldn't happen */
|
|
|
|
return (EINVAL);
|
2003-02-19 05:47:46 +00:00
|
|
|
sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
|
1999-01-07 23:07:57 +00:00
|
|
|
endpt, isize));
|
2006-09-06 23:29:53 +00:00
|
|
|
if ((clist_alloc_cblocks(&sce->q, UGEN_IBSIZE,
|
|
|
|
UGEN_IBSIZE), 0) == -1)
|
1999-11-17 22:33:51 +00:00
|
|
|
return (ENOMEM);
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_open_pipe_intr(sce->iface,
|
2004-12-13 07:45:02 +00:00
|
|
|
edesc->bEndpointAddress,
|
|
|
|
USBD_SHORT_XFER_OK, &sce->pipeh, sce,
|
|
|
|
sce->ibuf, isize, ugenintr,
|
|
|
|
USBD_DEFAULT_INTERVAL);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err) {
|
1999-10-07 19:26:38 +00:00
|
|
|
free(sce->ibuf, M_USBDEV);
|
2006-09-06 23:29:53 +00:00
|
|
|
clist_free_cblocks(&sce->q);
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
DPRINTFN(5, ("ugenopen: interrupt open done\n"));
|
|
|
|
break;
|
|
|
|
case UE_BULK:
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_open_pipe(sce->iface,
|
1999-11-17 22:33:51 +00:00
|
|
|
edesc->bEndpointAddress, 0, &sce->pipeh);
|
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
break;
|
|
|
|
case UE_ISOCHRONOUS:
|
2000-12-07 10:28:25 +00:00
|
|
|
if (dir == OUT)
|
|
|
|
return (EINVAL);
|
|
|
|
isize = UGETW(edesc->wMaxPacketSize);
|
|
|
|
if (isize == 0) /* shouldn't happen */
|
|
|
|
return (EINVAL);
|
|
|
|
sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
|
2003-02-19 05:47:46 +00:00
|
|
|
M_USBDEV, M_WAITOK);
|
2000-12-07 10:28:25 +00:00
|
|
|
sce->cur = sce->fill = sce->ibuf;
|
|
|
|
sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
|
2000-12-07 10:28:25 +00:00
|
|
|
endpt, isize));
|
|
|
|
err = usbd_open_pipe(sce->iface,
|
|
|
|
edesc->bEndpointAddress, 0, &sce->pipeh);
|
|
|
|
if (err) {
|
|
|
|
free(sce->ibuf, M_USBDEV);
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
for(i = 0; i < UGEN_NISOREQS; ++i) {
|
|
|
|
sce->isoreqs[i].sce = sce;
|
|
|
|
xfer = usbd_alloc_xfer(sc->sc_udev);
|
|
|
|
if (xfer == 0)
|
|
|
|
goto bad;
|
|
|
|
sce->isoreqs[i].xfer = xfer;
|
|
|
|
buf = usbd_alloc_buffer
|
|
|
|
(xfer, isize * UGEN_NISORFRMS);
|
|
|
|
if (buf == 0) {
|
|
|
|
i++;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
sce->isoreqs[i].dmabuf = buf;
|
|
|
|
for(j = 0; j < UGEN_NISORFRMS; ++j)
|
|
|
|
sce->isoreqs[i].sizes[j] = isize;
|
|
|
|
usbd_setup_isoc_xfer
|
|
|
|
(xfer, sce->pipeh, &sce->isoreqs[i],
|
|
|
|
sce->isoreqs[i].sizes,
|
|
|
|
UGEN_NISORFRMS, USBD_NO_COPY,
|
|
|
|
ugen_isoc_rintr);
|
|
|
|
(void)usbd_transfer(xfer);
|
|
|
|
}
|
|
|
|
DPRINTFN(5, ("ugenopen: isoc open done\n"));
|
|
|
|
break;
|
|
|
|
bad:
|
|
|
|
while (--i >= 0) /* implicit buffer free */
|
|
|
|
usbd_free_xfer(sce->isoreqs[i].xfer);
|
|
|
|
return (ENOMEM);
|
|
|
|
case UE_CONTROL:
|
2002-05-06 18:23:36 +00:00
|
|
|
sce->timeout = USBD_DEFAULT_TIMEOUT;
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
sc->sc_is_open[endpt] = 1;
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_OPEN(dev, sc);
|
1999-01-07 23:07:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-09-06 23:29:53 +00:00
|
|
|
ugenclose(struct cdev *dev, int flag, int mode, struct thread *p)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-01-07 23:07:57 +00:00
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
struct ugen_softc *sc;
|
1999-01-07 23:07:57 +00:00
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
int dir;
|
2000-12-07 10:28:25 +00:00
|
|
|
int i;
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
|
1999-11-17 22:33:51 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
|
|
|
|
flag, mode, UGENUNIT(dev), endpt));
|
1999-08-12 18:55:41 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
1999-11-17 22:33:51 +00:00
|
|
|
if (!sc->sc_is_open[endpt]) {
|
1999-10-07 19:26:38 +00:00
|
|
|
printf("ugenclose: not open\n");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-07 23:07:57 +00:00
|
|
|
|
|
|
|
if (endpt == USB_CONTROL_ENDPOINT) {
|
|
|
|
DPRINTFN(5, ("ugenclose: close control\n"));
|
1999-11-17 22:33:51 +00:00
|
|
|
sc->sc_is_open[endpt] = 0;
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_CLOSE(dev, sc);
|
1999-01-07 23:07:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (dir = OUT; dir <= IN; dir++) {
|
1999-10-07 19:26:38 +00:00
|
|
|
if (!(flag & (dir == OUT ? FWRITE : FREAD)))
|
|
|
|
continue;
|
|
|
|
sce = &sc->sc_endpoints[endpt][dir];
|
2004-10-02 22:33:26 +00:00
|
|
|
if (sce->pipeh == NULL)
|
1999-10-07 19:26:38 +00:00
|
|
|
continue;
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
|
1999-10-07 19:26:38 +00:00
|
|
|
endpt, dir, sce));
|
2000-12-07 10:28:25 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
usbd_abort_pipe(sce->pipeh);
|
|
|
|
usbd_close_pipe(sce->pipeh);
|
1999-11-17 22:33:51 +00:00
|
|
|
sce->pipeh = NULL;
|
2000-12-07 10:28:25 +00:00
|
|
|
|
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_INTERRUPT:
|
2000-03-02 16:01:53 +00:00
|
|
|
ndflush(&sce->q, sce->q.c_cc);
|
2006-09-06 23:29:53 +00:00
|
|
|
clist_free_cblocks(&sce->q);
|
2000-12-07 10:28:25 +00:00
|
|
|
break;
|
|
|
|
case UE_ISOCHRONOUS:
|
|
|
|
for (i = 0; i < UGEN_NISOREQS; ++i)
|
|
|
|
usbd_free_xfer(sce->isoreqs[i].xfer);
|
2004-12-13 07:45:02 +00:00
|
|
|
|
2000-12-07 10:28:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2000-12-07 10:28:25 +00:00
|
|
|
if (sce->ibuf != NULL) {
|
|
|
|
free(sce->ibuf, M_USBDEV);
|
|
|
|
sce->ibuf = NULL;
|
2006-09-06 23:29:53 +00:00
|
|
|
clist_free_cblocks(&sce->q);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
sc->sc_is_open[endpt] = 0;
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_CLOSE(dev, sc);
|
1999-01-07 23:07:57 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
2001-12-31 00:52:17 +00:00
|
|
|
struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
|
1999-01-07 23:07:57 +00:00
|
|
|
u_int32_t n, tn;
|
2004-10-13 04:12:20 +00:00
|
|
|
char buf[UGEN_BBSIZE];
|
|
|
|
usbd_xfer_handle xfer;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-01-07 23:07:57 +00:00
|
|
|
int s;
|
2006-06-03 10:37:42 +00:00
|
|
|
int error = 0, doneone = 0;
|
1999-01-07 23:07:57 +00:00
|
|
|
u_char buffer[UGEN_CHUNK];
|
|
|
|
|
2006-09-06 23:29:53 +00:00
|
|
|
DPRINTFN(5, ("%s: ugenread: %d\n", device_get_nameunit(sc->sc_dev), endpt));
|
1999-11-17 22:33:51 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
if (sc->sc_dying)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return (ENODEV);
|
|
|
|
|
2004-12-13 07:45:02 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
2001-12-31 00:52:17 +00:00
|
|
|
if (sce->edesc == NULL) {
|
|
|
|
printf("ugenread: no edesc\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
if (sce->pipeh == NULL) {
|
|
|
|
printf("ugenread: no pipe\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
2004-12-13 07:45:02 +00:00
|
|
|
#endif
|
2001-12-31 00:52:17 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_INTERRUPT:
|
2001-12-31 00:52:17 +00:00
|
|
|
/* Block until activity occurred. */
|
1999-01-07 23:07:57 +00:00
|
|
|
s = splusb();
|
|
|
|
while (sce->q.c_cc == 0) {
|
2004-12-22 17:31:44 +00:00
|
|
|
if (flag & O_NONBLOCK) {
|
1999-01-07 23:07:57 +00:00
|
|
|
splx(s);
|
|
|
|
return (EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
sce->state |= UGEN_ASLP;
|
2001-12-31 00:52:17 +00:00
|
|
|
DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
|
2007-06-28 05:15:33 +00:00
|
|
|
error = tsleep(sce, PZERO | PCATCH, "ugenri",
|
2007-06-28 06:22:40 +00:00
|
|
|
(sce->timeout * hz + 999) / 1000);
|
2006-06-05 14:44:39 +00:00
|
|
|
sce->state &= ~UGEN_ASLP;
|
1999-01-07 23:07:57 +00:00
|
|
|
DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
|
1999-10-07 19:26:38 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
error = EIO;
|
2007-06-28 05:15:33 +00:00
|
|
|
if (error == EAGAIN) {
|
|
|
|
error = 0; /* timeout, return 0 bytes */
|
|
|
|
break;
|
|
|
|
}
|
2006-06-05 14:44:39 +00:00
|
|
|
if (error)
|
1999-10-07 19:26:38 +00:00
|
|
|
break;
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* Transfer as many chunks as possible. */
|
1999-10-07 19:26:38 +00:00
|
|
|
while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
|
1999-01-07 23:07:57 +00:00
|
|
|
n = min(sce->q.c_cc, uio->uio_resid);
|
|
|
|
if (n > sizeof(buffer))
|
|
|
|
n = sizeof(buffer);
|
|
|
|
|
|
|
|
/* Remove a small chunk from the input queue. */
|
|
|
|
q_to_b(&sce->q, buffer, n);
|
|
|
|
DPRINTFN(5, ("ugenread: got %d chars\n", n));
|
|
|
|
|
|
|
|
/* Copy the data to the user process. */
|
|
|
|
error = uiomove(buffer, n, uio);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UE_BULK:
|
2004-12-13 07:45:02 +00:00
|
|
|
xfer = usbd_alloc_xfer(sc->sc_udev);
|
|
|
|
if (xfer == 0)
|
|
|
|
return (ENOMEM);
|
2006-06-03 10:37:42 +00:00
|
|
|
while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0 ||
|
|
|
|
!doneone) {
|
1999-10-07 19:26:38 +00:00
|
|
|
DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
|
1999-01-10 18:42:54 +00:00
|
|
|
tn = n;
|
2006-06-03 10:37:42 +00:00
|
|
|
doneone = 1;
|
2004-12-13 07:45:02 +00:00
|
|
|
err = usbd_bulk_transfer(
|
|
|
|
xfer, sce->pipeh,
|
|
|
|
sce->state & UGEN_SHORT_OK ?
|
|
|
|
USBD_SHORT_XFER_OK : 0,
|
|
|
|
sce->timeout, buf, &tn, "ugenrb");
|
|
|
|
if (err) {
|
|
|
|
if (err == USBD_INTERRUPTED)
|
|
|
|
error = EINTR;
|
|
|
|
else if (err == USBD_TIMEOUT)
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
else
|
|
|
|
error = EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
|
|
|
|
error = uiomove(buf, tn, uio);
|
|
|
|
if (error || tn < n)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usbd_free_xfer(xfer);
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
2000-12-07 10:28:25 +00:00
|
|
|
case UE_ISOCHRONOUS:
|
|
|
|
s = splusb();
|
|
|
|
while (sce->cur == sce->fill) {
|
2004-12-22 17:31:44 +00:00
|
|
|
if (flag & O_NONBLOCK) {
|
2000-12-07 10:28:25 +00:00
|
|
|
splx(s);
|
|
|
|
return (EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
sce->state |= UGEN_ASLP;
|
2001-12-31 00:52:17 +00:00
|
|
|
DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
|
2007-06-28 05:15:33 +00:00
|
|
|
error = tsleep(sce, PZERO | PCATCH, "ugenri",
|
2007-06-28 06:22:40 +00:00
|
|
|
(sce->timeout * hz + 999) / 1000);
|
2006-06-05 14:44:39 +00:00
|
|
|
sce->state &= ~UGEN_ASLP;
|
2000-12-07 10:28:25 +00:00
|
|
|
DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
|
|
|
|
if (sc->sc_dying)
|
|
|
|
error = EIO;
|
2007-06-28 05:15:33 +00:00
|
|
|
if (error == EAGAIN) {
|
|
|
|
error = 0; /* timeout, return 0 bytes */
|
|
|
|
break;
|
|
|
|
}
|
2006-06-05 14:44:39 +00:00
|
|
|
if (error)
|
2000-12-07 10:28:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
|
2007-06-28 05:15:33 +00:00
|
|
|
if (sce->fill > sce->cur)
|
2000-12-07 10:28:25 +00:00
|
|
|
n = min(sce->fill - sce->cur, uio->uio_resid);
|
|
|
|
else
|
|
|
|
n = min(sce->limit - sce->cur, uio->uio_resid);
|
|
|
|
|
|
|
|
DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
|
|
|
|
|
|
|
|
/* Copy the data to the user process. */
|
|
|
|
error = uiomove(sce->cur, n, uio);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
sce->cur += n;
|
|
|
|
if(sce->cur >= sce->limit)
|
|
|
|
sce->cur = sce->ibuf;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
break;
|
|
|
|
|
2003-07-04 01:50:39 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
default:
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
return (error);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
int
|
2004-06-16 09:47:26 +00:00
|
|
|
ugenread(struct cdev *dev, struct uio *uio, int flag)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
struct ugen_softc *sc;
|
1999-10-07 19:26:38 +00:00
|
|
|
int error;
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2006-06-05 14:44:39 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
|
|
|
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_REF(dev, sc);
|
1999-10-07 19:26:38 +00:00
|
|
|
error = ugen_do_read(sc, endpt, uio, flag);
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_RELE(dev, sc);
|
1999-10-07 19:26:38 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
2001-12-31 00:52:17 +00:00
|
|
|
struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
|
1999-10-07 19:26:38 +00:00
|
|
|
u_int32_t n;
|
2006-06-03 10:37:42 +00:00
|
|
|
int error = 0, doneone = 0;
|
1999-01-07 23:07:57 +00:00
|
|
|
char buf[UGEN_BBSIZE];
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
usbd_status err;
|
|
|
|
|
2006-09-06 23:29:53 +00:00
|
|
|
DPRINTFN(5, ("%s: ugenwrite: %d\n", device_get_nameunit(sc->sc_dev), endpt));
|
1999-01-07 23:07:57 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
if (sc->sc_dying)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return (ENODEV);
|
|
|
|
|
2004-12-13 07:45:02 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
2001-12-31 00:52:17 +00:00
|
|
|
if (sce->edesc == NULL) {
|
|
|
|
printf("ugenwrite: no edesc\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
if (sce->pipeh == NULL) {
|
|
|
|
printf("ugenwrite: no pipe\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
2004-12-13 07:45:02 +00:00
|
|
|
#endif
|
2001-12-31 00:52:17 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_BULK:
|
1999-11-28 21:01:06 +00:00
|
|
|
xfer = usbd_alloc_xfer(sc->sc_udev);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (xfer == 0)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
2006-06-03 10:37:42 +00:00
|
|
|
while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0 ||
|
|
|
|
!doneone) {
|
|
|
|
doneone = 1;
|
1999-01-07 23:07:57 +00:00
|
|
|
error = uiomove(buf, n, uio);
|
|
|
|
if (error)
|
|
|
|
break;
|
2001-12-31 00:52:17 +00:00
|
|
|
DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
|
1999-11-17 22:33:51 +00:00
|
|
|
sce->timeout, buf, &n,"ugenwb");
|
|
|
|
if (err) {
|
|
|
|
if (err == USBD_INTERRUPTED)
|
1999-01-07 23:07:57 +00:00
|
|
|
error = EINTR;
|
2001-12-31 00:52:17 +00:00
|
|
|
else if (err == USBD_TIMEOUT)
|
|
|
|
error = ETIMEDOUT;
|
1999-01-07 23:07:57 +00:00
|
|
|
else
|
|
|
|
error = EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-11-28 21:01:06 +00:00
|
|
|
usbd_free_xfer(xfer);
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
2003-02-28 19:28:29 +00:00
|
|
|
case UE_INTERRUPT:
|
|
|
|
xfer = usbd_alloc_xfer(sc->sc_udev);
|
|
|
|
if (xfer == 0)
|
|
|
|
return (EIO);
|
|
|
|
while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
|
2006-06-03 10:37:42 +00:00
|
|
|
uio->uio_resid)) != 0 || !doneone) {
|
|
|
|
doneone = 1;
|
2003-02-28 19:28:29 +00:00
|
|
|
error = uiomove(buf, n, uio);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_intr_transfer(xfer, sce->pipeh, 0,
|
2004-12-13 07:45:02 +00:00
|
|
|
sce->timeout, buf, &n, "ugenwi");
|
2003-02-28 19:28:29 +00:00
|
|
|
if (err) {
|
|
|
|
if (err == USBD_INTERRUPTED)
|
|
|
|
error = EINTR;
|
|
|
|
else if (err == USBD_TIMEOUT)
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
else
|
|
|
|
error = EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usbd_free_xfer(xfer);
|
|
|
|
break;
|
1999-01-07 23:07:57 +00:00
|
|
|
default:
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
int
|
2004-06-16 09:47:26 +00:00
|
|
|
ugenwrite(struct cdev *dev, struct uio *uio, int flag)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
struct ugen_softc *sc;
|
1999-10-07 19:26:38 +00:00
|
|
|
int error;
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2006-06-05 14:44:39 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
|
|
|
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_REF(dev, sc);
|
1999-10-07 19:26:38 +00:00
|
|
|
error = ugen_do_write(sc, endpt, uio, flag);
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_RELE(dev, sc);
|
1999-10-07 19:26:38 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-10-02 22:49:54 +00:00
|
|
|
void
|
|
|
|
ugenpurge(struct cdev *dev)
|
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
2004-10-12 04:02:06 +00:00
|
|
|
struct ugen_endpoint *sce;
|
2004-10-02 22:49:54 +00:00
|
|
|
struct ugen_softc *sc;
|
|
|
|
|
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return;
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
|
2004-10-12 04:02:06 +00:00
|
|
|
sce = &sc->sc_endpoints[endpt][IN];
|
|
|
|
if (sce->pipeh)
|
|
|
|
usbd_abort_pipe(sce->pipeh);
|
2006-06-05 14:44:39 +00:00
|
|
|
if (sce->state & UGEN_ASLP) {
|
|
|
|
DPRINTFN(5, ("ugenpurge: waking %p\n", sce));
|
|
|
|
wakeup(sce);
|
|
|
|
}
|
|
|
|
selwakeuppri(&sce->rsel, PZERO);
|
|
|
|
|
|
|
|
sce = &sc->sc_endpoints[endpt][OUT];
|
|
|
|
if (sce->pipeh)
|
|
|
|
usbd_abort_pipe(sce->pipeh);
|
|
|
|
if (sce->state & UGEN_ASLP) {
|
|
|
|
DPRINTFN(5, ("ugenpurge: waking %p\n", sce));
|
|
|
|
wakeup(sce);
|
|
|
|
}
|
|
|
|
selwakeuppri(&sce->rsel, PZERO);
|
2004-10-02 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
|
|
|
ugen_detach(device_t self)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
2006-09-06 21:53:28 +00:00
|
|
|
struct ugen_softc *sc = device_get_softc(self);
|
1999-10-07 19:26:38 +00:00
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
int i, dir;
|
|
|
|
|
|
|
|
DPRINTF(("ugen_detach: sc=%p\n", sc));
|
|
|
|
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
/* Abort all pipes. Causes processes waiting for transfer to wake. */
|
|
|
|
for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
|
|
|
|
for (dir = OUT; dir <= IN; dir++) {
|
|
|
|
sce = &sc->sc_endpoints[i][dir];
|
2004-10-02 22:33:26 +00:00
|
|
|
if (sce->pipeh)
|
1999-10-07 19:26:38 +00:00
|
|
|
usbd_abort_pipe(sce->pipeh);
|
2006-06-05 14:44:39 +00:00
|
|
|
selwakeuppri(&sce->rsel, PZERO);
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-23 15:42:08 +00:00
|
|
|
/* destroy the device for the control endpoint */
|
2003-10-01 13:53:51 +00:00
|
|
|
destroy_dev(sc->dev);
|
2006-09-06 23:29:53 +00:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
|
1999-10-07 19:26:38 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static void
|
2000-12-07 10:13:18 +00:00
|
|
|
ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-01-07 23:07:57 +00:00
|
|
|
struct ugen_endpoint *sce = addr;
|
|
|
|
/*struct ugen_softc *sc = sce->sc;*/
|
|
|
|
u_int32_t count;
|
|
|
|
u_char *ibuf;
|
|
|
|
|
|
|
|
if (status == USBD_CANCELLED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (status != USBD_NORMAL_COMPLETION) {
|
|
|
|
DPRINTF(("ugenintr: status=%d\n", status));
|
2001-09-02 09:26:14 +00:00
|
|
|
if (status == USBD_STALLED)
|
|
|
|
usbd_clear_endpoint_stall_async(sce->pipeh);
|
1999-01-07 23:07:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-05-14 17:33:26 +00:00
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
|
1999-01-07 23:07:57 +00:00
|
|
|
ibuf = sce->ibuf;
|
|
|
|
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
|
1999-11-17 22:33:51 +00:00
|
|
|
xfer, status, count));
|
1999-01-07 23:07:57 +00:00
|
|
|
DPRINTFN(5, (" data = %02x %02x %02x\n",
|
|
|
|
ibuf[0], ibuf[1], ibuf[2]));
|
|
|
|
|
|
|
|
(void)b_to_q(ibuf, count, &sce->q);
|
2003-07-04 01:50:39 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
if (sce->state & UGEN_ASLP) {
|
|
|
|
sce->state &= ~UGEN_ASLP;
|
|
|
|
DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
|
1999-10-07 19:26:38 +00:00
|
|
|
wakeup(sce);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&sce->rsel, PZERO);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static void
|
2003-07-04 01:50:39 +00:00
|
|
|
ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
|
2000-12-07 10:28:25 +00:00
|
|
|
usbd_status status)
|
|
|
|
{
|
|
|
|
struct isoreq *req = addr;
|
|
|
|
struct ugen_endpoint *sce = req->sce;
|
|
|
|
u_int32_t count, n;
|
2001-12-31 00:52:17 +00:00
|
|
|
int i, isize;
|
2000-12-07 10:28:25 +00:00
|
|
|
|
|
|
|
/* Return if we are aborting. */
|
|
|
|
if (status == USBD_CANCELLED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
|
2005-12-15 21:57:32 +00:00
|
|
|
DPRINTFN(5,("ugen_isoc_rintr: xfer %td, count=%d\n", req - sce->isoreqs,
|
2000-12-07 10:28:25 +00:00
|
|
|
count));
|
|
|
|
|
|
|
|
/* throw away oldest input if the buffer is full */
|
|
|
|
if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
|
|
|
|
sce->cur += count;
|
|
|
|
if(sce->cur >= sce->limit)
|
2004-12-13 07:45:02 +00:00
|
|
|
sce->cur = sce->ibuf + (sce->limit - sce->cur);
|
2000-12-07 10:28:25 +00:00
|
|
|
DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
|
|
|
|
count));
|
|
|
|
}
|
|
|
|
|
2001-12-31 00:52:17 +00:00
|
|
|
isize = UGETW(sce->edesc->wMaxPacketSize);
|
|
|
|
for (i = 0; i < UGEN_NISORFRMS; i++) {
|
|
|
|
u_int32_t actlen = req->sizes[i];
|
|
|
|
char const *buf = (char const *)req->dmabuf + isize * i;
|
|
|
|
|
|
|
|
/* copy data to buffer */
|
|
|
|
while (actlen > 0) {
|
|
|
|
n = min(actlen, sce->limit - sce->fill);
|
|
|
|
memcpy(sce->fill, buf, n);
|
|
|
|
|
|
|
|
buf += n;
|
|
|
|
actlen -= n;
|
|
|
|
sce->fill += n;
|
|
|
|
if(sce->fill == sce->limit)
|
|
|
|
sce->fill = sce->ibuf;
|
|
|
|
}
|
2000-12-07 10:28:25 +00:00
|
|
|
|
2001-12-31 00:52:17 +00:00
|
|
|
/* setup size for next transfer */
|
|
|
|
req->sizes[i] = isize;
|
2000-12-07 10:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
|
|
|
|
USBD_NO_COPY, ugen_isoc_rintr);
|
|
|
|
(void)usbd_transfer(xfer);
|
|
|
|
|
|
|
|
if (sce->state & UGEN_ASLP) {
|
|
|
|
sce->state &= ~UGEN_ASLP;
|
|
|
|
DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
|
|
|
|
wakeup(sce);
|
|
|
|
}
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&sce->rsel, PZERO);
|
2000-12-07 10:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static usbd_status
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-01-07 23:07:57 +00:00
|
|
|
usbd_interface_handle iface;
|
|
|
|
usb_endpoint_descriptor_t *ed;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
2004-12-12 02:27:30 +00:00
|
|
|
struct ugen_endpoint *sce, **sce_cache;
|
|
|
|
u_int8_t niface, nendpt, nendpt_cache, endptno, endpt;
|
1999-10-07 19:26:38 +00:00
|
|
|
int dir;
|
1999-01-07 23:07:57 +00:00
|
|
|
|
|
|
|
DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_interface_count(sc->sc_udev, &niface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
1999-01-07 23:07:57 +00:00
|
|
|
if (ifaceidx < 0 || ifaceidx >= niface)
|
|
|
|
return (USBD_INVAL);
|
2003-07-04 01:50:39 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
2003-05-18 21:22:00 +00:00
|
|
|
|
2004-12-12 02:27:30 +00:00
|
|
|
/* store an array of endpoint descriptors to clear if the interface
|
|
|
|
* change succeeds - these aren't available afterwards */
|
|
|
|
sce_cache = malloc(sizeof(struct ugen_endpoint *) * nendpt, M_TEMP,
|
|
|
|
M_WAITOK);
|
|
|
|
nendpt_cache = nendpt;
|
2003-05-18 21:22:00 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
|
|
|
endpt = ed->bEndpointAddress;
|
1999-10-07 19:26:38 +00:00
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
2004-12-12 02:27:30 +00:00
|
|
|
sce_cache[endptno] = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* change setting */
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_set_interface(iface, altno);
|
2004-12-12 02:27:30 +00:00
|
|
|
if (err) {
|
|
|
|
free(sce_cache, M_TEMP);
|
1999-11-17 22:33:51 +00:00
|
|
|
return (err);
|
2004-12-12 02:27:30 +00:00
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
2004-12-12 02:27:30 +00:00
|
|
|
panic("ugen_set_interface: endpoint count failed");
|
|
|
|
|
|
|
|
/* destroy the existing devices, we remake the new ones in a moment */
|
|
|
|
ugen_destroy_devnodes(sc);
|
|
|
|
|
|
|
|
/* now we can clear the old interface's ugen_endpoints */
|
|
|
|
for (endptno = 0; endptno < nendpt_cache; endptno++) {
|
|
|
|
sce = sce_cache[endptno];
|
|
|
|
sce->sc = 0;
|
|
|
|
sce->edesc = 0;
|
|
|
|
sce->iface = 0;
|
|
|
|
}
|
|
|
|
free(sce_cache, M_TEMP);
|
|
|
|
|
|
|
|
/* set the new interface's ugen_endpoints */
|
1999-01-07 23:07:57 +00:00
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
|
|
|
endpt = ed->bEndpointAddress;
|
1999-10-07 19:26:38 +00:00
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
|
|
|
sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
1999-01-07 23:07:57 +00:00
|
|
|
sce->sc = sc;
|
|
|
|
sce->edesc = ed;
|
|
|
|
sce->iface = iface;
|
|
|
|
}
|
2003-05-29 23:47:12 +00:00
|
|
|
|
|
|
|
/* make the new devices */
|
|
|
|
ugen_make_devnodes(sc);
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve a complete descriptor for a certain device and index. */
|
2006-09-06 21:53:28 +00:00
|
|
|
static usb_config_descriptor_t *
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
|
|
|
usb_config_descriptor_t *cdesc, *tdesc, cdescr;
|
|
|
|
int len;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-01-07 23:07:57 +00:00
|
|
|
|
|
|
|
if (index == USB_CURRENT_CONFIG_INDEX) {
|
|
|
|
tdesc = usbd_get_config_descriptor(sc->sc_udev);
|
|
|
|
len = UGETW(tdesc->wTotalLength);
|
|
|
|
if (lenp)
|
|
|
|
*lenp = len;
|
2003-02-19 05:47:46 +00:00
|
|
|
cdesc = malloc(len, M_TEMP, M_WAITOK);
|
1999-01-07 23:07:57 +00:00
|
|
|
memcpy(cdesc, tdesc, len);
|
|
|
|
DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
|
|
|
|
} else {
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
|
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (0);
|
|
|
|
len = UGETW(cdescr.wTotalLength);
|
|
|
|
DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
|
|
|
|
if (lenp)
|
|
|
|
*lenp = len;
|
2003-02-19 05:47:46 +00:00
|
|
|
cdesc = malloc(len, M_TEMP, M_WAITOK);
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
|
|
|
|
if (err) {
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (cdesc);
|
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
|
|
|
usbd_interface_handle iface;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-01-07 23:07:57 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
|
|
|
|
if (err)
|
2000-12-07 10:13:18 +00:00
|
|
|
return (-1);
|
1999-01-07 23:07:57 +00:00
|
|
|
return (usbd_get_interface_altindex(iface));
|
|
|
|
}
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
static int
|
2000-12-07 10:13:18 +00:00
|
|
|
ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
|
2006-09-06 23:29:53 +00:00
|
|
|
caddr_t addr, int flag, struct thread *p)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
|
|
|
struct ugen_endpoint *sce;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-01-07 23:07:57 +00:00
|
|
|
usbd_interface_handle iface;
|
|
|
|
struct usb_config_desc *cd;
|
|
|
|
usb_config_descriptor_t *cdesc;
|
|
|
|
struct usb_interface_desc *id;
|
|
|
|
usb_interface_descriptor_t *idesc;
|
|
|
|
struct usb_endpoint_desc *ed;
|
|
|
|
usb_endpoint_descriptor_t *edesc;
|
|
|
|
struct usb_alt_interface *ai;
|
|
|
|
struct usb_string_desc *si;
|
|
|
|
u_int8_t conf, alt;
|
|
|
|
|
|
|
|
DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
|
1999-10-07 19:26:38 +00:00
|
|
|
if (sc->sc_dying)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FIONBIO:
|
2004-10-13 04:13:05 +00:00
|
|
|
case FIOASYNC:
|
1999-01-07 23:07:57 +00:00
|
|
|
/* All handled in the upper FS layer. */
|
|
|
|
return (0);
|
|
|
|
case USB_SET_SHORT_XFER:
|
1999-10-07 19:26:38 +00:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return (EINVAL);
|
2004-12-13 07:45:02 +00:00
|
|
|
/* This flag only affects read */
|
1999-01-07 23:07:57 +00:00
|
|
|
sce = &sc->sc_endpoints[endpt][IN];
|
2001-12-31 00:52:17 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (sce->pipeh == NULL) {
|
1999-10-07 19:26:38 +00:00
|
|
|
printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
}
|
2001-12-31 00:52:17 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
if (*(int *)addr)
|
|
|
|
sce->state |= UGEN_SHORT_OK;
|
|
|
|
else
|
|
|
|
sce->state &= ~UGEN_SHORT_OK;
|
|
|
|
return (0);
|
1999-10-07 19:26:38 +00:00
|
|
|
case USB_SET_TIMEOUT:
|
|
|
|
sce = &sc->sc_endpoints[endpt][IN];
|
|
|
|
sce->timeout = *(int *)addr;
|
2006-06-03 10:37:42 +00:00
|
|
|
sce = &sc->sc_endpoints[endpt][OUT];
|
|
|
|
sce->timeout = *(int *)addr;
|
1999-10-07 19:26:38 +00:00
|
|
|
return (0);
|
1999-01-07 23:07:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
if (endpt != USB_CONTROL_ENDPOINT)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
switch (cmd) {
|
2002-07-31 14:34:36 +00:00
|
|
|
#ifdef USB_DEBUG
|
1999-01-07 23:07:57 +00:00
|
|
|
case USB_SETDEBUG:
|
|
|
|
ugendebug = *(int *)addr;
|
|
|
|
break;
|
1998-11-26 23:13:13 +00:00
|
|
|
#endif
|
1999-01-07 23:07:57 +00:00
|
|
|
case USB_GET_CONFIG:
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_get_config(sc->sc_udev, &conf);
|
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
|
|
|
*(int *)addr = conf;
|
|
|
|
break;
|
|
|
|
case USB_SET_CONFIG:
|
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1999-11-17 22:33:51 +00:00
|
|
|
err = ugen_set_config(sc, *(int *)addr);
|
2000-01-24 10:23:57 +00:00
|
|
|
switch (err) {
|
|
|
|
case USBD_NORMAL_COMPLETION:
|
2004-10-02 22:49:54 +00:00
|
|
|
ugen_make_devnodes(sc);
|
2000-01-24 10:23:57 +00:00
|
|
|
break;
|
|
|
|
case USBD_IN_USE:
|
|
|
|
return (EBUSY);
|
|
|
|
default:
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
2000-01-24 10:23:57 +00:00
|
|
|
}
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
|
|
|
case USB_GET_ALTINTERFACE:
|
|
|
|
ai = (struct usb_alt_interface *)addr;
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev,
|
2002-02-20 20:47:21 +00:00
|
|
|
ai->uai_interface_index, &iface);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
idesc = usbd_get_interface_descriptor(iface);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (idesc == NULL)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EIO);
|
2002-02-20 20:47:21 +00:00
|
|
|
ai->uai_alt_no = idesc->bAlternateSetting;
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
|
|
|
case USB_SET_ALTINTERFACE:
|
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
|
|
|
ai = (struct usb_alt_interface *)addr;
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev,
|
2002-02-20 20:47:21 +00:00
|
|
|
ai->uai_interface_index, &iface);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
2004-12-13 07:45:02 +00:00
|
|
|
err = ugen_set_interface(sc, ai->uai_interface_index,
|
|
|
|
ai->uai_alt_no);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
break;
|
|
|
|
case USB_GET_NO_ALT:
|
|
|
|
ai = (struct usb_alt_interface *)addr;
|
2002-02-20 20:47:21 +00:00
|
|
|
cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (cdesc == NULL)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
2002-02-20 20:47:21 +00:00
|
|
|
idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (idesc == NULL) {
|
1999-10-07 19:26:38 +00:00
|
|
|
free(cdesc, M_TEMP);
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
2004-12-13 07:45:02 +00:00
|
|
|
ai->uai_alt_no = usbd_get_no_alts(cdesc,
|
|
|
|
idesc->bInterfaceNumber);
|
1999-10-07 19:26:38 +00:00
|
|
|
free(cdesc, M_TEMP);
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
|
|
|
case USB_GET_DEVICE_DESC:
|
|
|
|
*(usb_device_descriptor_t *)addr =
|
|
|
|
*usbd_get_device_descriptor(sc->sc_udev);
|
|
|
|
break;
|
|
|
|
case USB_GET_CONFIG_DESC:
|
|
|
|
cd = (struct usb_config_desc *)addr;
|
2002-02-20 20:47:21 +00:00
|
|
|
cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (cdesc == NULL)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
2002-02-20 20:47:21 +00:00
|
|
|
cd->ucd_desc = *cdesc;
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
break;
|
|
|
|
case USB_GET_INTERFACE_DESC:
|
|
|
|
id = (struct usb_interface_desc *)addr;
|
2002-02-20 20:47:21 +00:00
|
|
|
cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (cdesc == NULL)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
2002-02-20 20:47:21 +00:00
|
|
|
if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
|
|
|
|
id->uid_alt_index == USB_CURRENT_ALT_INDEX)
|
|
|
|
alt = ugen_get_alt_index(sc, id->uid_interface_index);
|
1999-01-07 23:07:57 +00:00
|
|
|
else
|
2002-02-20 20:47:21 +00:00
|
|
|
alt = id->uid_alt_index;
|
|
|
|
idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (idesc == NULL) {
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-02-20 20:47:21 +00:00
|
|
|
id->uid_desc = *idesc;
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
break;
|
|
|
|
case USB_GET_ENDPOINT_DESC:
|
|
|
|
ed = (struct usb_endpoint_desc *)addr;
|
2002-02-20 20:47:21 +00:00
|
|
|
cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (cdesc == NULL)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
2002-02-20 20:47:21 +00:00
|
|
|
if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
|
|
|
|
ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
|
|
|
|
alt = ugen_get_alt_index(sc, ed->ued_interface_index);
|
1999-01-07 23:07:57 +00:00
|
|
|
else
|
2002-02-20 20:47:21 +00:00
|
|
|
alt = ed->ued_alt_index;
|
2003-07-04 01:50:39 +00:00
|
|
|
edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
|
2002-02-20 20:47:21 +00:00
|
|
|
alt, ed->ued_endpoint_index);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (edesc == NULL) {
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-02-20 20:47:21 +00:00
|
|
|
ed->ued_desc = *edesc;
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
break;
|
|
|
|
case USB_GET_FULL_DESC:
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
|
|
|
struct usb_full_desc *fd = (struct usb_full_desc *)addr;
|
|
|
|
int error;
|
|
|
|
|
2002-02-20 20:47:21 +00:00
|
|
|
cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
|
|
|
|
if (len > fd->ufd_size)
|
|
|
|
len = fd->ufd_size;
|
|
|
|
iov.iov_base = (caddr_t)fd->ufd_data;
|
1999-01-07 23:07:57 +00:00
|
|
|
iov.iov_len = len;
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_resid = len;
|
|
|
|
uio.uio_offset = 0;
|
|
|
|
uio.uio_segflg = UIO_USERSPACE;
|
|
|
|
uio.uio_rw = UIO_READ;
|
2006-09-06 23:29:53 +00:00
|
|
|
uio.uio_td = p;
|
1999-10-07 19:26:38 +00:00
|
|
|
error = uiomove((void *)cdesc, len, &uio);
|
1999-01-07 23:07:57 +00:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
MFNetBSD.
uhid.c (1.61), author: jdolecek
add support for USB_GET_DEVICEINFO and USB_GET_STRING_DESC ioctls,
with same meaning as for ugen(4)
usbdi_util.h (1.29), usb_quirks.c (1.50), uhid.c (1.62),
ugen.c (1.68), usb_subr.c (1.114) author: mycroft
Yes, some devices return incorrect lengths in their string
descriptors. Rather than losing, do what Windows does: just
request the maximum size, and allow a shorter response. Obsoletes
the need for UQ_NO_STRINGS, and therefore these "quirks" are removed.
usb_subr.c (1.116), author: mycroft
In the "seemed like a good idea until I found the fatal flaw"
department... Attempting to read a maximum-size string descriptor
causes my kue device to go completely apeshit. So, go back to the
original method, but allow the device to return a shorter string than
it claimed.
Obtained from: NetBSD
2004-06-26 10:35:10 +00:00
|
|
|
case USB_GET_STRING_DESC: {
|
|
|
|
int len;
|
1999-01-07 23:07:57 +00:00
|
|
|
si = (struct usb_string_desc *)addr;
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
|
MFNetBSD.
uhid.c (1.61), author: jdolecek
add support for USB_GET_DEVICEINFO and USB_GET_STRING_DESC ioctls,
with same meaning as for ugen(4)
usbdi_util.h (1.29), usb_quirks.c (1.50), uhid.c (1.62),
ugen.c (1.68), usb_subr.c (1.114) author: mycroft
Yes, some devices return incorrect lengths in their string
descriptors. Rather than losing, do what Windows does: just
request the maximum size, and allow a shorter response. Obsoletes
the need for UQ_NO_STRINGS, and therefore these "quirks" are removed.
usb_subr.c (1.116), author: mycroft
In the "seemed like a good idea until I found the fatal flaw"
department... Attempting to read a maximum-size string descriptor
causes my kue device to go completely apeshit. So, go back to the
original method, but allow the device to return a shorter string than
it claimed.
Obtained from: NetBSD
2004-06-26 10:35:10 +00:00
|
|
|
si->usd_language_id, &si->usd_desc, &len);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
break;
|
MFNetBSD.
uhid.c (1.61), author: jdolecek
add support for USB_GET_DEVICEINFO and USB_GET_STRING_DESC ioctls,
with same meaning as for ugen(4)
usbdi_util.h (1.29), usb_quirks.c (1.50), uhid.c (1.62),
ugen.c (1.68), usb_subr.c (1.114) author: mycroft
Yes, some devices return incorrect lengths in their string
descriptors. Rather than losing, do what Windows does: just
request the maximum size, and allow a shorter response. Obsoletes
the need for UQ_NO_STRINGS, and therefore these "quirks" are removed.
usb_subr.c (1.116), author: mycroft
In the "seemed like a good idea until I found the fatal flaw"
department... Attempting to read a maximum-size string descriptor
causes my kue device to go completely apeshit. So, go back to the
original method, but allow the device to return a shorter string than
it claimed.
Obtained from: NetBSD
2004-06-26 10:35:10 +00:00
|
|
|
}
|
1999-01-07 23:07:57 +00:00
|
|
|
case USB_DO_REQUEST:
|
|
|
|
{
|
|
|
|
struct usb_ctl_request *ur = (void *)addr;
|
2002-02-20 20:47:21 +00:00
|
|
|
int len = UGETW(ur->ucr_request.wLength);
|
1999-01-07 23:07:57 +00:00
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
|
|
|
void *ptr = 0;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-01-07 23:07:57 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
|
|
|
/* Avoid requests that would damage the bus integrity. */
|
2002-02-20 20:47:21 +00:00
|
|
|
if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
|
|
|
|
ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
|
|
|
|
(ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
|
|
|
|
ur->ucr_request.bRequest == UR_SET_CONFIG) ||
|
|
|
|
(ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
|
|
|
|
ur->ucr_request.bRequest == UR_SET_INTERFACE))
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (len < 0 || len > 32767)
|
|
|
|
return (EINVAL);
|
|
|
|
if (len != 0) {
|
2002-02-20 20:47:21 +00:00
|
|
|
iov.iov_base = (caddr_t)ur->ucr_data;
|
1999-01-07 23:07:57 +00:00
|
|
|
iov.iov_len = len;
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_resid = len;
|
|
|
|
uio.uio_offset = 0;
|
|
|
|
uio.uio_segflg = UIO_USERSPACE;
|
|
|
|
uio.uio_rw =
|
2003-07-04 01:50:39 +00:00
|
|
|
ur->ucr_request.bmRequestType & UT_READ ?
|
1999-01-07 23:07:57 +00:00
|
|
|
UIO_READ : UIO_WRITE;
|
2006-09-06 23:29:53 +00:00
|
|
|
uio.uio_td = p;
|
2003-02-19 05:47:46 +00:00
|
|
|
ptr = malloc(len, M_TEMP, M_WAITOK);
|
1999-01-07 23:07:57 +00:00
|
|
|
if (uio.uio_rw == UIO_WRITE) {
|
|
|
|
error = uiomove(ptr, len, &uio);
|
|
|
|
if (error)
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
2002-05-06 18:23:36 +00:00
|
|
|
sce = &sc->sc_endpoints[endpt][IN];
|
2003-07-04 01:50:39 +00:00
|
|
|
err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
|
2002-05-06 18:23:36 +00:00
|
|
|
ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err) {
|
1999-01-07 23:07:57 +00:00
|
|
|
error = EIO;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if (len != 0) {
|
|
|
|
if (uio.uio_rw == UIO_READ) {
|
|
|
|
error = uiomove(ptr, len, &uio);
|
|
|
|
if (error)
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret:
|
|
|
|
if (ptr)
|
|
|
|
free(ptr, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
case USB_GET_DEVICEINFO:
|
|
|
|
usbd_fill_deviceinfo(sc->sc_udev,
|
2001-07-05 10:12:59 +00:00
|
|
|
(struct usb_device_info *)addr, 1);
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
int
|
2006-09-06 23:29:53 +00:00
|
|
|
ugenioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
|
|
|
|
struct thread *p)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
struct ugen_softc *sc;
|
1999-10-07 19:26:38 +00:00
|
|
|
int error;
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2006-06-05 14:44:39 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
|
|
|
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_REF(dev, sc);
|
2002-01-02 16:33:53 +00:00
|
|
|
error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
|
2004-10-02 22:49:54 +00:00
|
|
|
UGEN_DEV_RELE(dev, sc);
|
1999-10-07 19:26:38 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
int
|
2006-09-06 23:29:53 +00:00
|
|
|
ugenpoll(struct cdev *dev, int events, struct thread *p)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
struct ugen_softc *sc;
|
2006-06-05 14:44:39 +00:00
|
|
|
struct ugen_endpoint *sce_in, *sce_out;
|
|
|
|
usb_endpoint_descriptor_t *edesc;
|
1999-01-07 23:07:57 +00:00
|
|
|
int revents = 0;
|
|
|
|
int s;
|
|
|
|
|
2006-09-06 21:53:28 +00:00
|
|
|
sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
|
1999-11-17 22:33:51 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
if (sc->sc_dying)
|
2006-06-05 14:44:39 +00:00
|
|
|
return ((events & (POLLIN | POLLOUT | POLLRDNORM |
|
|
|
|
POLLWRNORM)) | POLLHUP);
|
|
|
|
/* Do not allow to poll a control endpoint */
|
|
|
|
if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT)
|
|
|
|
return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
|
|
|
|
|
|
|
|
sce_in = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
|
|
|
|
sce_out = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
|
|
|
|
edesc = (sce_in->edesc != NULL) ? sce_in->edesc : sce_out->edesc;
|
|
|
|
KASSERT(edesc != NULL, ("ugenpoll: NULL edesc"));
|
|
|
|
if (sce_in->edesc == NULL || sce_in->pipeh == NULL)
|
|
|
|
sce_in = NULL;
|
|
|
|
if (sce_out->edesc == NULL || sce_out->pipeh == NULL)
|
|
|
|
sce_out = NULL;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
s = splusb();
|
2006-06-05 14:44:39 +00:00
|
|
|
switch (edesc->bmAttributes & UE_XFERTYPE) {
|
1999-01-07 23:07:57 +00:00
|
|
|
case UE_INTERRUPT:
|
2006-06-05 14:44:39 +00:00
|
|
|
if (sce_in != NULL && (events & (POLLIN | POLLRDNORM))) {
|
|
|
|
if (sce_in->q.c_cc > 0)
|
1999-01-07 23:07:57 +00:00
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
2006-06-05 14:44:39 +00:00
|
|
|
selrecord(p, &sce_in->rsel);
|
|
|
|
}
|
|
|
|
if (sce_out != NULL && (events & (POLLOUT | POLLWRNORM))) {
|
|
|
|
if (sce_out->q.c_cc > 0)
|
|
|
|
revents |= events & (POLLOUT | POLLWRNORM);
|
|
|
|
else
|
|
|
|
selrecord(p, &sce_out->rsel);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-12-07 10:28:25 +00:00
|
|
|
case UE_ISOCHRONOUS:
|
2006-06-05 14:44:39 +00:00
|
|
|
if (sce_in != NULL && (events & (POLLIN | POLLRDNORM))) {
|
|
|
|
if (sce_in->cur != sce_in->fill)
|
2000-12-07 10:28:25 +00:00
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
2006-06-05 14:44:39 +00:00
|
|
|
selrecord(p, &sce_in->rsel);
|
|
|
|
}
|
|
|
|
if (sce_out != NULL && (events & (POLLOUT | POLLWRNORM))) {
|
|
|
|
if (sce_out->cur != sce_out->fill)
|
|
|
|
revents |= events & (POLLOUT | POLLWRNORM);
|
|
|
|
else
|
|
|
|
selrecord(p, &sce_out->rsel);
|
2000-12-07 10:28:25 +00:00
|
|
|
}
|
|
|
|
break;
|
1999-01-07 23:07:57 +00:00
|
|
|
case UE_BULK:
|
2003-07-04 01:50:39 +00:00
|
|
|
/*
|
2004-10-13 04:12:20 +00:00
|
|
|
* We have no easy way of determining if a read will
|
|
|
|
* yield any data or a write will happen.
|
|
|
|
* Pretend they will.
|
1999-01-07 23:07:57 +00:00
|
|
|
*/
|
2004-10-13 04:12:20 +00:00
|
|
|
revents |= events &
|
|
|
|
(POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
|
1999-01-07 23:07:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
return (revents);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-11-08 07:24:55 +00:00
|
|
|
DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
|