2000-11-27 00:05:02 +03:00
|
|
|
/* $NetBSD: ugen.c,v 1.44 2000/11/26 21:05:02 augustss Exp $ */
|
1999-11-19 02:32:25 +03:00
|
|
|
/* $FreeBSD: src/sys/dev/usb/ugen.c,v 1.26 1999/11/17 22:33:41 n_hibma Exp $ */
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2000-04-27 19:26:44 +04:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) at
|
1998-12-08 18:39:00 +03:00
|
|
|
* Carlstedt Research & Technology.
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 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>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1999-08-17 20:06:20 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1998-12-08 18:39:00 +03:00
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/ioctl.h>
|
1999-01-08 14:58:25 +03:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/ioccom.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
#include <sys/conf.h>
|
1998-12-08 18:39:00 +03:00
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
|
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#ifdef UGEN_DEBUG
|
1999-08-14 18:49:31 +04:00
|
|
|
#define DPRINTF(x) if (ugendebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
|
1998-12-09 03:18:10 +03:00
|
|
|
int ugendebug = 0;
|
1998-12-08 18:39:00 +03:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
2000-09-08 04:55:26 +04: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 */
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
struct ugen_endpoint {
|
|
|
|
struct ugen_softc *sc;
|
|
|
|
usb_endpoint_descriptor_t *edesc;
|
|
|
|
usbd_interface_handle iface;
|
|
|
|
int state;
|
|
|
|
#define UGEN_ASLP 0x02 /* waiting for data */
|
1999-01-01 18:31:24 +03:00
|
|
|
#define UGEN_SHORT_OK 0x04 /* short xfers are OK */
|
1998-12-08 18:39:00 +03:00
|
|
|
usbd_pipe_handle pipeh;
|
|
|
|
struct clist q;
|
|
|
|
struct selinfo rsel;
|
2000-09-08 04:55:26 +04: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-08-23 00:12:39 +04:00
|
|
|
u_int32_t timeout;
|
2000-09-08 04:55:26 +04:00
|
|
|
struct isoreq {
|
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
void *dmabuf;
|
|
|
|
u_int16_t sizes[UGEN_NISORFRMS];
|
|
|
|
} isoreqs[UGEN_NISOREQS];
|
1998-12-08 18:39:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ugen_softc {
|
1999-09-05 23:32:18 +04:00
|
|
|
USBBASEDEVICE sc_dev; /* base device */
|
1999-08-28 14:01:59 +04:00
|
|
|
usbd_device_handle sc_udev;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-08-02 23:32:56 +04:00
|
|
|
char sc_is_open[USB_MAX_ENDPOINTS];
|
1998-12-08 18:39:00 +03:00
|
|
|
struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
|
1999-09-05 02:26:11 +04:00
|
|
|
#define OUT 0
|
|
|
|
#define IN 1
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
int sc_refcnt;
|
|
|
|
u_char sc_dying;
|
1998-12-08 18:39:00 +03:00
|
|
|
};
|
|
|
|
|
1999-08-17 20:06:20 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-08-14 18:49:31 +04:00
|
|
|
cdev_decl(ugen);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
d_open_t ugenopen;
|
|
|
|
d_close_t ugenclose;
|
|
|
|
d_read_t ugenread;
|
|
|
|
d_write_t ugenwrite;
|
|
|
|
d_ioctl_t ugenioctl;
|
|
|
|
d_poll_t ugenpoll;
|
|
|
|
|
|
|
|
#define UGEN_CDEV_MAJOR 114
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static struct cdevsw ugen_cdevsw = {
|
1999-08-14 18:49:31 +04:00
|
|
|
/* open */ ugenopen,
|
|
|
|
/* close */ ugenclose,
|
|
|
|
/* read */ ugenread,
|
|
|
|
/* write */ ugenwrite,
|
|
|
|
/* ioctl */ ugenioctl,
|
|
|
|
/* poll */ ugenpoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ "ugen",
|
|
|
|
/* maj */ UGEN_CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
/* bmaj */ -1
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2000-06-01 18:28:57 +04:00
|
|
|
Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
|
|
|
|
usbd_status status);
|
2000-09-08 05:27:12 +04:00
|
|
|
Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
|
|
|
|
usbd_status status);
|
2000-06-01 18:28:57 +04: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,
|
|
|
|
caddr_t, int, struct proc *);
|
|
|
|
Static int ugen_set_config(struct ugen_softc *sc, int configno);
|
|
|
|
Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
|
|
|
|
int index, int *lenp);
|
|
|
|
Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
|
|
|
|
Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
#define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
|
|
|
|
#define UGENENDPOINT(n) (minor(n) & 0xf)
|
|
|
|
#define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_DECLARE_DRIVER(ugen);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_MATCH(ugen)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_MATCH_START(ugen, uaa);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
if (uaa->usegeneric)
|
|
|
|
return (UMATCH_GENERIC);
|
|
|
|
else
|
|
|
|
return (UMATCH_NONE);
|
|
|
|
}
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH(ugen)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_START(ugen, sc, uaa);
|
1999-12-19 02:22:54 +03:00
|
|
|
usbd_device_handle udev;
|
1998-12-08 18:39:00 +03:00
|
|
|
char devinfo[1024];
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-12-29 18:33:10 +03:00
|
|
|
int conf;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
usbd_devinfo(uaa->device, 0, devinfo);
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_SETUP;
|
|
|
|
printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-12-19 02:22:54 +03:00
|
|
|
sc->sc_udev = udev = uaa->device;
|
|
|
|
|
|
|
|
/* First set configuration index 0, the default one for ugen. */
|
|
|
|
err = usbd_set_config_index(udev, 0, 0);
|
|
|
|
if (err) {
|
|
|
|
printf("%s: setting configuration index 0 failed\n",
|
|
|
|
USBDEVNAME(sc->sc_dev));
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
USB_ATTACH_ERROR_RETURN;
|
|
|
|
}
|
|
|
|
conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
|
|
|
|
|
|
|
|
/* Set up all the local state for this configuration. */
|
1999-11-12 03:34:57 +03:00
|
|
|
err = ugen_set_config(sc, conf);
|
|
|
|
if (err) {
|
1998-12-29 18:33:10 +03:00
|
|
|
printf("%s: setting configuration %d failed\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), conf);
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_dying = 1;
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_ERROR_RETURN;
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
1999-11-19 02:32:25 +03:00
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
{
|
|
|
|
static int global_init_done = 0;
|
|
|
|
if (!global_init_done) {
|
|
|
|
cdevsw_add(&ugen_cdevsw);
|
|
|
|
global_init_done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-02-02 16:20:59 +03:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
|
|
|
|
USBDEV(sc->sc_dev));
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_SUCCESS_RETURN;
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_set_config(struct ugen_softc *sc, int configno)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
usbd_device_handle dev = sc->sc_udev;
|
|
|
|
usbd_interface_handle iface;
|
|
|
|
usb_endpoint_descriptor_t *ed;
|
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
u_int8_t niface, nendpt;
|
|
|
|
int ifaceno, endptno, endpt;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1999-09-05 02:26:11 +04:00
|
|
|
int dir;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), configno, sc));
|
1999-12-19 02:22:54 +03:00
|
|
|
/* Avoid setting the current value. */
|
1998-12-29 18:33:10 +03:00
|
|
|
if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
|
2000-10-24 18:53:59 +04:00
|
|
|
err = usbd_set_config_no(dev, configno, 1);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err)
|
|
|
|
return (err);
|
1998-12-29 18:33:10 +03:00
|
|
|
}
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_interface_count(dev, &niface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
1998-12-08 18:39:00 +03:00
|
|
|
memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
|
|
|
|
for (ifaceno = 0; ifaceno < niface; ifaceno++) {
|
|
|
|
DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_device2interface_handle(dev, ifaceno, &iface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
1998-12-08 18:39:00 +03:00
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
|
|
|
endpt = ed->bEndpointAddress;
|
1999-09-05 02:26:11 +04:00
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
|
|
|
sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
1998-12-11 02:16:47 +03:00
|
|
|
DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
|
|
|
|
"(%d,%d), sce=%p\n",
|
1998-12-08 18:39:00 +03:00
|
|
|
endptno, endpt, UE_GET_ADDR(endpt),
|
1999-09-05 02:26:11 +04:00
|
|
|
UE_GET_DIR(endpt), sce));
|
1998-12-08 18:39:00 +03:00
|
|
|
sce->sc = sc;
|
|
|
|
sce->edesc = ed;
|
|
|
|
sce->iface = iface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (USBD_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenopen(dev_t dev, int flag, int mode, struct proc *p)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct ugen_softc *sc;
|
1998-12-08 18:39:00 +03:00
|
|
|
int unit = UGENUNIT(dev);
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
|
|
|
usb_endpoint_descriptor_t *edesc;
|
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
int dir, isize;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
2000-09-08 04:55:26 +04:00
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
void *buf;
|
|
|
|
int i, j;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-01-08 14:58:25 +03:00
|
|
|
USB_GET_SC_OPEN(ugen, unit, sc);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-01-08 14:58:25 +03:00
|
|
|
DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
|
1999-01-03 04:03:22 +03:00
|
|
|
flag, mode, unit, endpt));
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-11-19 02:32:25 +03:00
|
|
|
if (sc == NULL || sc->sc_dying)
|
1999-06-30 10:44:22 +04:00
|
|
|
return (ENXIO);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-08-02 23:32:56 +04:00
|
|
|
if (sc->sc_is_open[endpt])
|
|
|
|
return (EBUSY);
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT) {
|
1999-08-02 23:32:56 +04:00
|
|
|
sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
2000-03-01 00:37:00 +03:00
|
|
|
|
1999-08-02 23:32:56 +04: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];
|
|
|
|
if (sce == 0 || sce->edesc == 0)
|
|
|
|
return (ENXIO);
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
1999-08-02 23:32:56 +04: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;
|
1998-12-08 18:39:00 +03:00
|
|
|
sce = &sc->sc_endpoints[endpt][dir];
|
1999-08-02 23:32:56 +04:00
|
|
|
sce->state = 0;
|
1999-08-23 00:12:39 +04:00
|
|
|
sce->timeout = USBD_NO_TIMEOUT;
|
1998-12-11 02:16:47 +03:00
|
|
|
DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
|
|
|
|
sc, endpt, dir, sce));
|
1998-12-08 18:39:00 +03:00
|
|
|
edesc = sce->edesc;
|
|
|
|
switch (edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_INTERRUPT:
|
|
|
|
isize = UGETW(edesc->wMaxPacketSize);
|
|
|
|
if (isize == 0) /* shouldn't happen */
|
|
|
|
return (EINVAL);
|
1999-06-30 10:44:22 +04:00
|
|
|
sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
|
1998-12-08 18:39:00 +03:00
|
|
|
DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
|
|
|
|
endpt, isize));
|
1999-01-08 14:58:25 +03:00
|
|
|
if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
|
|
|
|
return (ENOMEM);
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_open_pipe_intr(sce->iface,
|
|
|
|
edesc->bEndpointAddress,
|
|
|
|
USBD_SHORT_XFER_OK, &sce->pipeh, sce,
|
2000-01-19 03:23:58 +03:00
|
|
|
sce->ibuf, isize, ugenintr,
|
|
|
|
USBD_DEFAULT_INTERVAL);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err) {
|
1999-06-30 10:44:22 +04:00
|
|
|
free(sce->ibuf, M_USBDEV);
|
1998-12-08 18:39:00 +03:00
|
|
|
clfree(&sce->q);
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
DPRINTFN(5, ("ugenopen: interrupt open done\n"));
|
|
|
|
break;
|
|
|
|
case UE_BULK:
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_open_pipe(sce->iface,
|
|
|
|
edesc->bEndpointAddress, 0, &sce->pipeh);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
break;
|
|
|
|
case UE_ISOCHRONOUS:
|
2000-09-08 04:55:26 +04:00
|
|
|
if (dir == OUT)
|
|
|
|
return (EINVAL);
|
|
|
|
isize = UGETW(edesc->wMaxPacketSize);
|
|
|
|
if (isize == 0) /* shouldn't happen */
|
|
|
|
return (EINVAL);
|
|
|
|
sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
|
|
|
|
M_USBDEV, M_WAITOK);
|
|
|
|
sce->cur = sce->fill = sce->ibuf;
|
|
|
|
sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
|
|
|
|
DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
|
|
|
|
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:
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
1999-08-02 23:32:56 +04:00
|
|
|
sc->sc_is_open[endpt] = 1;
|
1998-12-08 18:39:00 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenclose(dev_t dev, int flag, int mode, struct proc *p)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-10-12 15:54:56 +04:00
|
|
|
struct ugen_softc *sc;
|
1998-12-08 18:39:00 +03:00
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
int dir;
|
2000-09-08 04:55:26 +04:00
|
|
|
int i;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(ugen, UGENUNIT(dev), sc);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-08-02 23:32:56 +04:00
|
|
|
DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
|
|
|
|
flag, mode, UGENUNIT(dev), endpt));
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (!sc->sc_is_open[endpt]) {
|
|
|
|
printf("ugenclose: not open\n");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
#endif
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
if (endpt == USB_CONTROL_ENDPOINT) {
|
1999-01-03 04:03:22 +03:00
|
|
|
DPRINTFN(5, ("ugenclose: close control\n"));
|
1999-08-02 23:32:56 +04:00
|
|
|
sc->sc_is_open[endpt] = 0;
|
1998-12-08 18:39:00 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-01-03 04:03:22 +03:00
|
|
|
for (dir = OUT; dir <= IN; dir++) {
|
1999-08-02 23:32:56 +04:00
|
|
|
if (!(flag & (dir == OUT ? FWRITE : FREAD)))
|
|
|
|
continue;
|
|
|
|
sce = &sc->sc_endpoints[endpt][dir];
|
1999-11-19 02:32:25 +03:00
|
|
|
if (sce == NULL || sce->pipeh == NULL)
|
1999-08-02 23:32:56 +04:00
|
|
|
continue;
|
|
|
|
DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
|
|
|
|
endpt, dir, sce));
|
|
|
|
|
|
|
|
usbd_abort_pipe(sce->pipeh);
|
|
|
|
usbd_close_pipe(sce->pipeh);
|
1999-11-12 03:34:57 +03:00
|
|
|
sce->pipeh = NULL;
|
2000-09-08 04:55:26 +04:00
|
|
|
|
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_INTERRUPT:
|
|
|
|
ndflush(&sce->q, sce->q.c_cc);
|
|
|
|
clfree(&sce->q);
|
|
|
|
break;
|
|
|
|
case UE_ISOCHRONOUS:
|
|
|
|
for (i = 0; i < UGEN_NISOREQS; ++i)
|
|
|
|
usbd_free_xfer(sce->isoreqs[i].xfer);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->ibuf != NULL) {
|
1999-08-02 23:32:56 +04:00
|
|
|
free(sce->ibuf, M_USBDEV);
|
1999-11-19 02:32:25 +03:00
|
|
|
sce->ibuf = NULL;
|
1999-08-02 23:32:56 +04:00
|
|
|
clfree(&sce->q);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-01-03 04:03:22 +03:00
|
|
|
}
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
1999-08-02 23:32:56 +04:00
|
|
|
sc->sc_is_open[endpt] = 0;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
|
1999-01-03 04:03:22 +03:00
|
|
|
u_int32_t n, tn;
|
1998-12-08 18:39:00 +03:00
|
|
|
char buf[UGEN_BBSIZE];
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03:00
|
|
|
int s;
|
|
|
|
int error = 0;
|
|
|
|
u_char buffer[UGEN_CHUNK];
|
|
|
|
|
2000-11-27 00:05:02 +03:00
|
|
|
DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
|
1999-10-28 11:28:51 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
|
1999-10-28 11:28:51 +04:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return (ENODEV);
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->edesc == NULL) {
|
1998-12-08 18:39:00 +03:00
|
|
|
printf("ugenread: no edesc\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->pipeh == NULL) {
|
1999-01-03 04:03:22 +03:00
|
|
|
printf("ugenread: no pipe\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
1998-12-08 18:39:00 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_INTERRUPT:
|
|
|
|
/* Block until activity occured. */
|
1998-12-09 03:18:10 +03:00
|
|
|
s = splusb();
|
1998-12-08 18:39:00 +03:00
|
|
|
while (sce->q.c_cc == 0) {
|
|
|
|
if (flag & IO_NDELAY) {
|
|
|
|
splx(s);
|
1999-01-01 18:31:24 +03:00
|
|
|
return (EWOULDBLOCK);
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
|
|
|
sce->state |= UGEN_ASLP;
|
|
|
|
DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
|
1999-06-30 10:44:22 +04:00
|
|
|
error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
|
1998-12-08 18:39:00 +03:00
|
|
|
DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
|
|
|
error = EIO;
|
1998-12-08 18:39:00 +03:00
|
|
|
if (error) {
|
|
|
|
sce->state &= ~UGEN_ASLP;
|
1999-06-30 10:44:22 +04:00
|
|
|
break;
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* Transfer as many chunks as possible. */
|
1999-06-30 10:44:22 +04:00
|
|
|
while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
|
1998-12-08 18:39:00 +03: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:
|
1999-11-18 02:00:49 +03:00
|
|
|
xfer = usbd_alloc_xfer(sc->sc_udev);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (xfer == 0)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (ENOMEM);
|
|
|
|
while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
|
1999-01-07 05:22:20 +03:00
|
|
|
DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
|
|
|
|
tn = n;
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_bulk_transfer(
|
|
|
|
xfer, sce->pipeh,
|
|
|
|
sce->state & UGEN_SHORT_OK ?
|
|
|
|
USBD_SHORT_XFER_OK : 0,
|
|
|
|
sce->timeout, buf, &tn, "ugenrb");
|
1999-11-19 02:32:25 +03:00
|
|
|
if (err) {
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err == USBD_INTERRUPTED)
|
1999-01-01 18:31:24 +03:00
|
|
|
error = EINTR;
|
1999-11-12 03:34:57 +03:00
|
|
|
else if (err == USBD_TIMEOUT)
|
1999-08-23 00:12:39 +04:00
|
|
|
error = ETIMEDOUT;
|
1999-01-01 18:31:24 +03:00
|
|
|
else
|
|
|
|
error = EIO;
|
1998-12-08 18:39:00 +03:00
|
|
|
break;
|
|
|
|
}
|
1999-01-07 05:22:20 +03:00
|
|
|
DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
|
1999-01-03 04:03:22 +03:00
|
|
|
error = uiomove(buf, tn, uio);
|
|
|
|
if (error || tn < n)
|
1998-12-08 18:39:00 +03:00
|
|
|
break;
|
|
|
|
}
|
1999-11-18 02:00:49 +03:00
|
|
|
usbd_free_xfer(xfer);
|
1998-12-08 18:39:00 +03:00
|
|
|
break;
|
2000-09-08 04:55:26 +04:00
|
|
|
case UE_ISOCHRONOUS:
|
|
|
|
s = splusb();
|
|
|
|
while (sce->cur == sce->fill) {
|
|
|
|
if (flag & IO_NDELAY) {
|
|
|
|
splx(s);
|
|
|
|
return (EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
sce->state |= UGEN_ASLP;
|
|
|
|
DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
|
|
|
|
error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
|
|
|
|
DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
|
|
|
|
if (sc->sc_dying)
|
|
|
|
error = EIO;
|
|
|
|
if (error) {
|
|
|
|
sce->state &= ~UGEN_ASLP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
|
|
|
|
if(sce->fill > sce->cur)
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
default:
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenread(dev_t dev, struct uio *uio, int flag)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-10-12 15:54:56 +04:00
|
|
|
struct ugen_softc *sc;
|
1999-06-30 10:44:22 +04:00
|
|
|
int error;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(ugen, UGENUNIT(dev), sc);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_refcnt++;
|
|
|
|
error = ugen_do_read(sc, endpt, uio, flag);
|
|
|
|
if (--sc->sc_refcnt < 0)
|
1999-09-05 23:32:18 +04:00
|
|
|
usb_detach_wakeup(USBDEV(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-01-07 05:22:20 +03:00
|
|
|
struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
|
1999-08-19 23:57:21 +04:00
|
|
|
u_int32_t n;
|
1998-12-08 18:39:00 +03:00
|
|
|
int error = 0;
|
|
|
|
char buf[UGEN_BBSIZE];
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_xfer_handle xfer;
|
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-11-19 02:32:25 +03:00
|
|
|
DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
|
1999-10-28 11:28:51 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
|
1999-10-28 11:28:51 +04:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return (ENODEV);
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->edesc == NULL) {
|
1998-12-08 18:39:00 +03:00
|
|
|
printf("ugenwrite: no edesc\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->pipeh == NULL) {
|
1999-01-03 04:03:22 +03:00
|
|
|
printf("ugenwrite: no pipe\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
1998-12-08 18:39:00 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
|
|
|
case UE_BULK:
|
1999-11-18 02:00:49 +03:00
|
|
|
xfer = usbd_alloc_xfer(sc->sc_udev);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (xfer == 0)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
|
|
|
|
error = uiomove(buf, n, uio);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
|
|
|
|
sce->timeout, buf, &n,"ugenwb");
|
|
|
|
if (err) {
|
|
|
|
if (err == USBD_INTERRUPTED)
|
1999-01-01 18:31:24 +03:00
|
|
|
error = EINTR;
|
2000-03-06 23:59:17 +03:00
|
|
|
else if (err == USBD_TIMEOUT)
|
|
|
|
error = ETIMEDOUT;
|
1999-01-01 18:31:24 +03:00
|
|
|
else
|
|
|
|
error = EIO;
|
1998-12-08 18:39:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-11-18 02:00:49 +03:00
|
|
|
usbd_free_xfer(xfer);
|
1998-12-08 18:39:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenwrite(dev_t dev, struct uio *uio, int flag)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-10-12 15:54:56 +04:00
|
|
|
struct ugen_softc *sc;
|
1999-06-30 10:44:22 +04:00
|
|
|
int error;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(ugen, UGENUNIT(dev), sc);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_refcnt++;
|
|
|
|
error = ugen_do_write(sc, endpt, uio, flag);
|
|
|
|
if (--sc->sc_refcnt < 0)
|
1999-09-05 23:32:18 +04:00
|
|
|
usb_detach_wakeup(USBDEV(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_activate(device_ptr_t self, enum devact act)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-08-24 02:55:14 +04:00
|
|
|
struct ugen_softc *sc = (struct ugen_softc *)self;
|
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
break;
|
|
|
|
}
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1999-10-13 12:10:55 +04:00
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
USB_DETACH(ugen)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-10-13 12:10:55 +04:00
|
|
|
USB_DETACH_START(ugen, sc);
|
1999-06-30 10:44:22 +04:00
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
int i, dir;
|
|
|
|
int s;
|
1999-10-13 12:10:55 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
int maj, mn;
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
|
1999-10-13 12:10:55 +04:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
DPRINTF(("ugen_detach: sc=%p\n", sc));
|
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
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];
|
|
|
|
if (sce && sce->pipeh)
|
|
|
|
usbd_abort_pipe(sce->pipeh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
if (--sc->sc_refcnt >= 0) {
|
|
|
|
/* Wake everyone */
|
|
|
|
for (i = 0; i < USB_MAX_ENDPOINTS; i++)
|
|
|
|
wakeup(&sc->sc_endpoints[i][IN]);
|
|
|
|
/* Wait for processes to go away. */
|
1999-09-05 23:32:18 +04:00
|
|
|
usb_detach_wait(USBDEV(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-06-30 10:44:22 +04:00
|
|
|
/* locate the major number */
|
|
|
|
for (maj = 0; maj < nchrdev; maj++)
|
|
|
|
if (cdevsw[maj].d_open == ugenopen)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Nuke the vnodes for any open instances (calls close). */
|
|
|
|
mn = self->dv_unit * USB_MAX_ENDPOINTS;
|
|
|
|
vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
|
1999-10-13 12:10:55 +04:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
/* XXX not implemented yet */
|
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
|
2000-02-02 16:20:59 +03:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
|
|
|
|
USBDEV(sc->sc_dev));
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1998-12-08 18:39:00 +03:00
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static void
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
struct ugen_endpoint *sce = addr;
|
|
|
|
/*struct ugen_softc *sc = sce->sc;*/
|
1999-01-01 18:31:24 +03:00
|
|
|
u_int32_t count;
|
1998-12-08 18:39:00 +03:00
|
|
|
u_char *ibuf;
|
|
|
|
|
|
|
|
if (status == USBD_CANCELLED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (status != USBD_NORMAL_COMPLETION) {
|
|
|
|
DPRINTF(("ugenintr: status=%d\n", status));
|
|
|
|
usbd_clear_endpoint_stall_async(sce->pipeh);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-02-08 21:45:27 +03:00
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
|
1998-12-08 18:39:00 +03:00
|
|
|
ibuf = sce->ibuf;
|
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
|
|
|
|
xfer, status, count));
|
1998-12-08 18:39:00 +03:00
|
|
|
DPRINTFN(5, (" data = %02x %02x %02x\n",
|
1999-01-01 18:31:24 +03:00
|
|
|
ibuf[0], ibuf[1], ibuf[2]));
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-01-01 18:31:24 +03:00
|
|
|
(void)b_to_q(ibuf, count, &sce->q);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
if (sce->state & UGEN_ASLP) {
|
|
|
|
sce->state &= ~UGEN_ASLP;
|
|
|
|
DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
|
1999-06-30 10:44:22 +04:00
|
|
|
wakeup(sce);
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
|
|
|
selwakeup(&sce->rsel);
|
|
|
|
}
|
|
|
|
|
2000-09-08 04:55:26 +04:00
|
|
|
Static void
|
2000-09-08 05:27:12 +04:00
|
|
|
ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
|
|
|
|
usbd_status status)
|
2000-09-08 04:55:26 +04:00
|
|
|
{
|
|
|
|
struct isoreq *req = addr;
|
|
|
|
struct ugen_endpoint *sce = req->sce;
|
|
|
|
u_int32_t count, n;
|
|
|
|
|
|
|
|
/* Return if we are aborting. */
|
|
|
|
if (status == USBD_CANCELLED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
|
|
|
|
DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
|
|
|
|
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)
|
|
|
|
sce->cur = sce->ibuf + (sce->limit - sce->cur);
|
|
|
|
DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
|
|
|
|
count));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy data to buffer */
|
2000-09-08 05:27:12 +04:00
|
|
|
while (count > 0) {
|
2000-09-08 04:55:26 +04:00
|
|
|
n = min(count, sce->limit - sce->fill);
|
|
|
|
memcpy(sce->fill, req->dmabuf, n);
|
|
|
|
|
|
|
|
count -= n;
|
|
|
|
sce->fill += n;
|
|
|
|
if(sce->fill == sce->limit)
|
|
|
|
sce->fill = sce->ibuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
selwakeup(&sce->rsel);
|
|
|
|
}
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static usbd_status
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
usbd_interface_handle iface;
|
|
|
|
usb_endpoint_descriptor_t *ed;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03:00
|
|
|
struct ugen_endpoint *sce;
|
|
|
|
u_int8_t niface, nendpt, endptno, endpt;
|
1999-09-05 02:26:11 +04:00
|
|
|
int dir;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
|
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_interface_count(sc->sc_udev, &niface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
1998-12-08 18:39:00 +03:00
|
|
|
if (ifaceidx < 0 || ifaceidx >= niface)
|
|
|
|
return (USBD_INVAL);
|
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
2000-05-31 20:16:49 +04:00
|
|
|
/* XXX should only do this after setting new altno has succeeded */
|
1998-12-08 18:39:00 +03:00
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
|
|
|
endpt = ed->bEndpointAddress;
|
1999-09-05 02:26:11 +04:00
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
|
|
|
sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
1998-12-08 18:39:00 +03:00
|
|
|
sce->sc = 0;
|
|
|
|
sce->edesc = 0;
|
|
|
|
sce->iface = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* change setting */
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_set_interface(iface, altno);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_endpoint_count(iface, &nendpt);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
1998-12-08 18:39:00 +03:00
|
|
|
for (endptno = 0; endptno < nendpt; endptno++) {
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface,endptno);
|
|
|
|
endpt = ed->bEndpointAddress;
|
1999-09-05 02:26:11 +04:00
|
|
|
dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
|
|
|
|
sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
|
1998-12-08 18:39:00 +03:00
|
|
|
sce->sc = sc;
|
|
|
|
sce->edesc = ed;
|
|
|
|
sce->iface = iface;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve a complete descriptor for a certain device and index. */
|
2000-03-27 16:33:53 +04:00
|
|
|
Static usb_config_descriptor_t *
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
usb_config_descriptor_t *cdesc, *tdesc, cdescr;
|
|
|
|
int len;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
|
|
|
if (index == USB_CURRENT_CONFIG_INDEX) {
|
|
|
|
tdesc = usbd_get_config_descriptor(sc->sc_udev);
|
|
|
|
len = UGETW(tdesc->wTotalLength);
|
|
|
|
if (lenp)
|
|
|
|
*lenp = len;
|
|
|
|
cdesc = malloc(len, M_TEMP, M_WAITOK);
|
|
|
|
memcpy(cdesc, tdesc, len);
|
|
|
|
DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
|
|
|
|
} else {
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (0);
|
|
|
|
len = UGETW(cdescr.wTotalLength);
|
|
|
|
DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
|
|
|
|
if (lenp)
|
|
|
|
*lenp = len;
|
|
|
|
cdesc = malloc(len, M_TEMP, M_WAITOK);
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc,len);
|
|
|
|
if (err) {
|
1998-12-08 18:39:00 +03:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (cdesc);
|
|
|
|
}
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
|
|
|
usbd_interface_handle iface;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03:00
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
|
|
|
|
if (err)
|
2000-05-31 20:16:49 +04:00
|
|
|
return (-1);
|
1998-12-09 03:18:10 +03:00
|
|
|
return (usbd_get_interface_altindex(iface));
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
|
|
|
|
caddr_t addr, int flag, struct proc *p)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
1999-01-01 18:31:24 +03:00
|
|
|
struct ugen_endpoint *sce;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03: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;
|
1998-12-09 03:18:10 +03:00
|
|
|
struct usb_string_desc *si;
|
1998-12-08 18:39:00 +03:00
|
|
|
u_int8_t conf, alt;
|
|
|
|
|
|
|
|
DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
|
1998-12-09 03:18:10 +03:00
|
|
|
switch (cmd) {
|
|
|
|
case FIONBIO:
|
|
|
|
/* All handled in the upper FS layer. */
|
|
|
|
return (0);
|
1999-01-01 18:31:24 +03:00
|
|
|
case USB_SET_SHORT_XFER:
|
|
|
|
/* This flag only affects read */
|
1999-08-02 23:32:56 +04:00
|
|
|
if (endpt == USB_CONTROL_ENDPOINT)
|
|
|
|
return (EINVAL);
|
1999-01-01 18:31:24 +03:00
|
|
|
sce = &sc->sc_endpoints[endpt][IN];
|
1999-10-28 16:08:38 +04:00
|
|
|
if (sce == NULL)
|
|
|
|
return (EINVAL);
|
1999-01-03 04:03:22 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->pipeh == NULL) {
|
1999-08-23 00:12:39 +04:00
|
|
|
printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
|
1999-01-03 04:03:22 +03:00
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-01 18:31:24 +03:00
|
|
|
if (*(int *)addr)
|
|
|
|
sce->state |= UGEN_SHORT_OK;
|
|
|
|
else
|
|
|
|
sce->state &= ~UGEN_SHORT_OK;
|
|
|
|
return (0);
|
1999-08-23 00:12:39 +04:00
|
|
|
case USB_SET_TIMEOUT:
|
|
|
|
sce = &sc->sc_endpoints[endpt][IN];
|
1999-10-28 16:08:38 +04:00
|
|
|
if (sce == NULL)
|
|
|
|
return (EINVAL);
|
1999-08-23 00:12:39 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1999-11-12 03:34:57 +03:00
|
|
|
if (sce->pipeh == NULL) {
|
1999-08-23 00:12:39 +04:00
|
|
|
printf("ugenioctl: USB_SET_TIMEOUT, no pipe\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sce->timeout = *(int *)addr;
|
|
|
|
return (0);
|
1998-12-09 03:18:10 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
if (endpt != USB_CONTROL_ENDPOINT)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
switch (cmd) {
|
1999-11-19 02:32:25 +03:00
|
|
|
#ifdef UGEN_DEBUG
|
1999-01-01 18:31:24 +03:00
|
|
|
case USB_SETDEBUG:
|
|
|
|
ugendebug = *(int *)addr;
|
|
|
|
break;
|
|
|
|
#endif
|
1998-12-08 18:39:00 +03:00
|
|
|
case USB_GET_CONFIG:
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_get_config(sc->sc_udev, &conf);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
*(int *)addr = conf;
|
|
|
|
break;
|
|
|
|
case USB_SET_CONFIG:
|
1998-12-09 03:18:10 +03:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1999-11-12 03:34:57 +03:00
|
|
|
err = ugen_set_config(sc, *(int *)addr);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
break;
|
|
|
|
case USB_GET_ALTINTERFACE:
|
|
|
|
ai = (struct usb_alt_interface *)addr;
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev,
|
|
|
|
ai->interface_index, &iface);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
|
|
|
idesc = usbd_get_interface_descriptor(iface);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (idesc == NULL)
|
1998-12-09 03:18:10 +03:00
|
|
|
return (EIO);
|
1998-12-08 18:39:00 +03:00
|
|
|
ai->alt_no = idesc->bAlternateSetting;
|
|
|
|
break;
|
|
|
|
case USB_SET_ALTINTERFACE:
|
1998-12-09 03:18:10 +03:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1998-12-08 18:39:00 +03:00
|
|
|
ai = (struct usb_alt_interface *)addr;
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_device2interface_handle(sc->sc_udev,
|
|
|
|
ai->interface_index, &iface);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
1999-11-12 03:34:57 +03:00
|
|
|
err = ugen_set_interface(sc, ai->interface_index, ai->alt_no);
|
|
|
|
if (err)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
|
|
|
break;
|
|
|
|
case USB_GET_NO_ALT:
|
|
|
|
ai = (struct usb_alt_interface *)addr;
|
1998-12-09 03:18:10 +03:00
|
|
|
cdesc = ugen_get_cdesc(sc, ai->config_index, 0);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (cdesc == NULL)
|
1998-12-09 03:18:10 +03:00
|
|
|
return (EINVAL);
|
|
|
|
idesc = usbd_find_idesc(cdesc, ai->interface_index, 0);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (idesc == NULL) {
|
1999-06-30 10:44:22 +04:00
|
|
|
free(cdesc, M_TEMP);
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
1999-06-30 10:44:22 +04:00
|
|
|
}
|
1998-12-09 03:18:10 +03:00
|
|
|
ai->alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
|
1999-06-30 10:44:22 +04:00
|
|
|
free(cdesc, M_TEMP);
|
1998-12-08 18:39:00 +03: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;
|
|
|
|
cdesc = ugen_get_cdesc(sc, cd->config_index, 0);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (cdesc == NULL)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
|
|
|
cd->desc = *cdesc;
|
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
break;
|
|
|
|
case USB_GET_INTERFACE_DESC:
|
|
|
|
id = (struct usb_interface_desc *)addr;
|
|
|
|
cdesc = ugen_get_cdesc(sc, id->config_index, 0);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (cdesc == NULL)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
|
|
|
if (id->config_index == USB_CURRENT_CONFIG_INDEX &&
|
|
|
|
id->alt_index == USB_CURRENT_ALT_INDEX)
|
1998-12-09 03:18:10 +03:00
|
|
|
alt = ugen_get_alt_index(sc, id->interface_index);
|
1998-12-08 18:39:00 +03:00
|
|
|
else
|
|
|
|
alt = id->alt_index;
|
|
|
|
idesc = usbd_find_idesc(cdesc, id->interface_index, alt);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (idesc == NULL) {
|
1998-12-08 18:39:00 +03:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
id->desc = *idesc;
|
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
break;
|
|
|
|
case USB_GET_ENDPOINT_DESC:
|
|
|
|
ed = (struct usb_endpoint_desc *)addr;
|
|
|
|
cdesc = ugen_get_cdesc(sc, ed->config_index, 0);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (cdesc == NULL)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EINVAL);
|
|
|
|
if (ed->config_index == USB_CURRENT_CONFIG_INDEX &&
|
|
|
|
ed->alt_index == USB_CURRENT_ALT_INDEX)
|
1998-12-09 03:18:10 +03:00
|
|
|
alt = ugen_get_alt_index(sc, ed->interface_index);
|
1998-12-08 18:39:00 +03:00
|
|
|
else
|
|
|
|
alt = ed->alt_index;
|
|
|
|
edesc = usbd_find_edesc(cdesc, ed->interface_index,
|
|
|
|
alt, ed->endpoint_index);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (edesc == NULL) {
|
1998-12-08 18:39:00 +03:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
ed->desc = *edesc;
|
|
|
|
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;
|
|
|
|
|
|
|
|
cdesc = ugen_get_cdesc(sc, fd->config_index, &len);
|
|
|
|
if (len > fd->size)
|
|
|
|
len = fd->size;
|
|
|
|
iov.iov_base = (caddr_t)fd->data;
|
|
|
|
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;
|
|
|
|
uio.uio_procp = p;
|
1999-11-19 02:32:25 +03:00
|
|
|
error = uiomove((void *)cdesc, len, &uio);
|
1998-12-08 18:39:00 +03:00
|
|
|
free(cdesc, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
1998-12-09 03:18:10 +03:00
|
|
|
case USB_GET_STRING_DESC:
|
|
|
|
si = (struct usb_string_desc *)addr;
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_get_string_desc(sc->sc_udev, si->string_index,
|
|
|
|
si->language_id, &si->desc);
|
|
|
|
if (err)
|
1998-12-09 03:18:10 +03:00
|
|
|
return (EINVAL);
|
|
|
|
break;
|
1998-12-08 18:39:00 +03:00
|
|
|
case USB_DO_REQUEST:
|
|
|
|
{
|
|
|
|
struct usb_ctl_request *ur = (void *)addr;
|
|
|
|
int len = UGETW(ur->request.wLength);
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
|
|
|
void *ptr = 0;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-12-08 18:39:00 +03:00
|
|
|
int error = 0;
|
|
|
|
|
1998-12-09 03:18:10 +03:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1998-12-08 18:39:00 +03:00
|
|
|
/* Avoid requests that would damage the bus integrity. */
|
|
|
|
if ((ur->request.bmRequestType == UT_WRITE_DEVICE &&
|
|
|
|
ur->request.bRequest == UR_SET_ADDRESS) ||
|
|
|
|
(ur->request.bmRequestType == UT_WRITE_DEVICE &&
|
|
|
|
ur->request.bRequest == UR_SET_CONFIG) ||
|
|
|
|
(ur->request.bmRequestType == UT_WRITE_INTERFACE &&
|
|
|
|
ur->request.bRequest == UR_SET_INTERFACE))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (len < 0 || len > 32767)
|
|
|
|
return (EINVAL);
|
|
|
|
if (len != 0) {
|
|
|
|
iov.iov_base = (caddr_t)ur->data;
|
|
|
|
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 =
|
|
|
|
ur->request.bmRequestType & UT_READ ?
|
|
|
|
UIO_READ : UIO_WRITE;
|
|
|
|
uio.uio_procp = p;
|
|
|
|
ptr = malloc(len, M_TEMP, M_WAITOK);
|
|
|
|
if (uio.uio_rw == UIO_WRITE) {
|
|
|
|
error = uiomove(ptr, len, &uio);
|
|
|
|
if (error)
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_do_request_flags(sc->sc_udev, &ur->request,
|
|
|
|
ptr, ur->flags, &ur->actlen);
|
|
|
|
if (err) {
|
1998-12-08 18:39:00 +03: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);
|
|
|
|
}
|
1998-12-09 03:18:10 +03:00
|
|
|
case USB_GET_DEVICEINFO:
|
|
|
|
usbd_fill_deviceinfo(sc->sc_udev,
|
|
|
|
(struct usb_device_info *)addr);
|
|
|
|
break;
|
1998-12-08 18:39:00 +03:00
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
|
|
|
int endpt = UGENENDPOINT(dev);
|
1999-10-12 15:54:56 +04:00
|
|
|
struct ugen_softc *sc;
|
1999-06-30 10:44:22 +04:00
|
|
|
int error;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(ugen, UGENUNIT(dev), sc);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_refcnt++;
|
|
|
|
error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
|
|
|
|
if (--sc->sc_refcnt < 0)
|
1999-09-05 23:32:18 +04:00
|
|
|
usb_detach_wakeup(USBDEV(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1998-12-08 18:39:00 +03:00
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
ugenpoll(dev_t dev, int events, struct proc *p)
|
1998-12-08 18:39:00 +03:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct ugen_softc *sc;
|
1998-12-09 03:18:10 +03:00
|
|
|
struct ugen_endpoint *sce;
|
1998-12-08 18:39:00 +03:00
|
|
|
int revents = 0;
|
|
|
|
int s;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(ugen, UGENUNIT(dev), sc);
|
1999-11-19 02:32:25 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
1998-12-08 18:39:00 +03:00
|
|
|
return (EIO);
|
|
|
|
|
1999-08-02 23:32:56 +04:00
|
|
|
/* XXX always IN */
|
1998-12-12 14:59:28 +03:00
|
|
|
sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
|
1999-10-28 16:08:38 +04:00
|
|
|
if (sce == NULL)
|
|
|
|
return (EINVAL);
|
1999-01-03 04:03:22 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (!sce->edesc) {
|
2000-03-06 23:59:17 +03:00
|
|
|
printf("ugenpoll: no edesc\n");
|
1999-01-03 04:03:22 +03:00
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
if (!sce->pipeh) {
|
|
|
|
printf("ugenpoll: no pipe\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
#endif
|
1998-12-09 03:18:10 +03:00
|
|
|
s = splusb();
|
|
|
|
switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
|
1998-12-08 18:39:00 +03:00
|
|
|
case UE_INTERRUPT:
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
|
|
|
if (sce->q.c_cc > 0)
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
|
|
|
selrecord(p, &sce->rsel);
|
|
|
|
}
|
|
|
|
break;
|
2000-09-08 04:55:26 +04:00
|
|
|
case UE_ISOCHRONOUS:
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
|
|
|
if (sce->cur != sce->fill)
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
|
|
|
selrecord(p, &sce->rsel);
|
|
|
|
}
|
|
|
|
break;
|
1998-12-09 03:18:10 +03:00
|
|
|
case UE_BULK:
|
|
|
|
/*
|
|
|
|
* We have no easy way of determining if a read will
|
|
|
|
* yield any data or a write will happen.
|
|
|
|
* Pretend they will.
|
|
|
|
*/
|
|
|
|
revents |= events &
|
|
|
|
(POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
1998-12-08 18:39:00 +03:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
return (revents);
|
|
|
|
}
|
1998-12-26 15:53:00 +03:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
1999-11-19 02:32:25 +03:00
|
|
|
DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
|
1998-12-26 15:53:00 +03:00
|
|
|
#endif
|