2003-06-28 18:20:43 +04:00
|
|
|
/* $NetBSD: uhid.c,v 1.58 2003/06/28 14:21:46 darrenr Exp $ */
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-11-26 01:32:04 +03:00
|
|
|
* 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-11-26 01:32:04 +03:00
|
|
|
* Carlstedt Research & Technology.
|
1998-07-12 23:51:55 +04: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:
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1999-01-10 14:13:36 +03:00
|
|
|
/*
|
2003-03-11 19:44:00 +03:00
|
|
|
* HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
|
1999-01-10 14:13:36 +03:00
|
|
|
*/
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-11-13 09:24:53 +03:00
|
|
|
#include <sys/cdefs.h>
|
2003-06-28 18:20:43 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.58 2003/06/28 14:21:46 darrenr Exp $");
|
2001-11-13 09:24:53 +03:00
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
2000-04-14 18:12:47 +04:00
|
|
|
#include <sys/signalvar.h>
|
1999-01-08 14:58:25 +03:00
|
|
|
#include <sys/device.h>
|
1998-12-26 15:53:00 +03:00
|
|
|
#include <sys/ioctl.h>
|
1999-06-30 10:44:22 +04:00
|
|
|
#include <sys/conf.h>
|
1998-07-12 23:51:55 +04: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/usbhid.h>
|
|
|
|
|
2000-12-29 04:47:49 +03:00
|
|
|
#include <dev/usb/usbdevs.h>
|
1998-07-12 23:51:55 +04:00
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
#include <dev/usb/hid.h>
|
|
|
|
#include <dev/usb/usb_quirks.h>
|
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
#include <dev/usb/uhidev.h>
|
2000-12-29 04:47:49 +03:00
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#ifdef UHID_DEBUG
|
1999-08-14 18:49:31 +04:00
|
|
|
#define DPRINTF(x) if (uhiddebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (uhiddebug>(n)) logprintf x
|
1998-07-12 23:51:55 +04:00
|
|
|
int uhiddebug = 0;
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct uhid_softc {
|
2001-12-28 20:32:36 +03:00
|
|
|
struct uhidev sc_hdev;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
int sc_isize;
|
|
|
|
int sc_osize;
|
1998-07-13 14:49:41 +04:00
|
|
|
int sc_fsize;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2000-02-22 14:24:22 +03:00
|
|
|
u_char *sc_obuf;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
struct clist sc_q;
|
|
|
|
struct selinfo sc_rsel;
|
2001-12-31 15:15:21 +03:00
|
|
|
usb_proc_ptr sc_async; /* process that wants SIGIO */
|
1998-07-12 23:51:55 +04:00
|
|
|
u_char sc_state; /* driver state */
|
2001-12-28 20:32:36 +03:00
|
|
|
#define UHID_ASLP 0x01 /* waiting for device data */
|
|
|
|
#define UHID_IMMED 0x02 /* return read data immediately */
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
int sc_refcnt;
|
|
|
|
u_char sc_dying;
|
1998-07-12 23:51:55 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define UHIDUNIT(dev) (minor(dev))
|
|
|
|
#define UHID_CHUNK 128 /* chunk size for read */
|
|
|
|
#define UHID_BSIZE 1020 /* buffer size */
|
|
|
|
|
2002-09-06 17:18:43 +04:00
|
|
|
dev_type_open(uhidopen);
|
|
|
|
dev_type_close(uhidclose);
|
|
|
|
dev_type_read(uhidread);
|
|
|
|
dev_type_write(uhidwrite);
|
|
|
|
dev_type_ioctl(uhidioctl);
|
|
|
|
dev_type_poll(uhidpoll);
|
2002-10-23 13:10:23 +04:00
|
|
|
dev_type_kqfilter(uhidkqfilter);
|
2002-09-06 17:18:43 +04:00
|
|
|
|
|
|
|
const struct cdevsw uhid_cdevsw = {
|
|
|
|
uhidopen, uhidclose, uhidread, uhidwrite, uhidioctl,
|
2002-10-23 13:10:23 +04:00
|
|
|
nostop, notty, uhidpoll, nommap, uhidkqfilter,
|
2002-09-06 17:18:43 +04:00
|
|
|
};
|
1999-08-14 18:49:31 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
Static void uhid_intr(struct uhidev *, void *, u_int len);
|
1999-06-30 10:44:22 +04:00
|
|
|
|
2000-06-01 18:28:57 +04:00
|
|
|
Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
|
|
|
|
Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
|
2003-06-28 18:20:43 +04:00
|
|
|
Static int uhid_do_ioctl(struct uhid_softc*, u_long, caddr_t, int, struct lwp *);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_DECLARE_DRIVER(uhid);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
int
|
|
|
|
uhid_match(struct device *parent, struct cfdata *match, void *aux)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
2001-12-28 20:32:36 +03:00
|
|
|
struct uhidev_attach_arg *uha = aux;
|
|
|
|
|
|
|
|
DPRINTF(("uhid_match: report=%d\n", uha->reportid));
|
|
|
|
|
|
|
|
if (uha->matchlvl)
|
|
|
|
return (uha->matchlvl);
|
1998-07-12 23:51:55 +04:00
|
|
|
return (UMATCH_IFACECLASS_GENERIC);
|
|
|
|
}
|
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
void
|
|
|
|
uhid_attach(struct device *parent, struct device *self, void *aux)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
2001-12-28 20:32:36 +03:00
|
|
|
struct uhid_softc *sc = (struct uhid_softc *)self;
|
|
|
|
struct uhidev_attach_arg *uha = aux;
|
|
|
|
int size, repid;
|
1998-07-12 23:51:55 +04:00
|
|
|
void *desc;
|
2002-07-12 01:14:24 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
sc->sc_hdev.sc_intr = uhid_intr;
|
|
|
|
sc->sc_hdev.sc_parent = uha->parent;
|
|
|
|
sc->sc_hdev.sc_report_id = uha->reportid;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
uhidev_get_report_desc(uha->parent, &desc, &size);
|
|
|
|
repid = uha->reportid;
|
|
|
|
sc->sc_isize = hid_report_size(desc, size, hid_input, repid);
|
|
|
|
sc->sc_osize = hid_report_size(desc, size, hid_output, repid);
|
|
|
|
sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
printf(": input=%d, output=%d, feature=%d\n",
|
|
|
|
sc->sc_isize, sc->sc_osize, sc->sc_fsize);
|
2000-02-02 16:18:45 +03:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_SUCCESS_RETURN;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhid_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 uhid_softc *sc = (struct uhid_softc *)self;
|
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
break;
|
|
|
|
}
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
1998-12-26 15:53:00 +03:00
|
|
|
}
|
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
int
|
|
|
|
uhid_detach(struct device *self, int flags)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
2001-12-28 20:32:36 +03:00
|
|
|
struct uhid_softc *sc = (struct uhid_softc *)self;
|
1999-06-30 10:44:22 +04:00
|
|
|
int s;
|
1999-10-13 12:10:55 +04:00
|
|
|
int maj, mn;
|
1998-08-02 00:52:45 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
|
|
|
|
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
if (sc->sc_hdev.sc_state & UHIDEV_OPEN) {
|
1999-06-30 10:44:22 +04:00
|
|
|
s = splusb();
|
|
|
|
if (--sc->sc_refcnt >= 0) {
|
|
|
|
/* Wake everyone */
|
|
|
|
wakeup(&sc->sc_q);
|
|
|
|
/* Wait for processes to go away. */
|
2001-12-28 20:32:36 +03:00
|
|
|
usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* locate the major number */
|
2002-09-06 17:18:43 +04:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
maj = cdevsw_lookup_major(&uhid_cdevsw);
|
|
|
|
#elif defined(__OpenBSD__)
|
1999-06-30 10:44:22 +04:00
|
|
|
for (maj = 0; maj < nchrdev; maj++)
|
|
|
|
if (cdevsw[maj].d_open == uhidopen)
|
|
|
|
break;
|
2002-09-06 17:18:43 +04:00
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
/* Nuke the vnodes for any open instances (calls close). */
|
|
|
|
mn = self->dv_unit;
|
|
|
|
vdevgone(maj, mn, mn, VCHR);
|
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
#if 0
|
2002-07-12 01:14:24 +04:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
|
2001-12-28 20:32:36 +03:00
|
|
|
sc->sc_hdev.sc_parent->sc_udev,
|
|
|
|
USBDEV(sc->sc_hdev.sc_dev));
|
|
|
|
#endif
|
2000-02-02 16:18:45 +03:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-12-28 20:32:36 +03:00
|
|
|
uhid_intr(struct uhidev *addr, void *data, u_int len)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
2001-12-28 20:32:36 +03:00
|
|
|
struct uhid_softc *sc = (struct uhid_softc *)addr;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2000-02-22 14:24:22 +03:00
|
|
|
#ifdef UHID_DEBUG
|
|
|
|
if (uhiddebug > 5) {
|
2001-12-28 20:32:36 +03:00
|
|
|
u_int32_t i;
|
2002-07-12 01:14:24 +04:00
|
|
|
|
2000-02-22 14:24:22 +03:00
|
|
|
DPRINTF(("uhid_intr: data ="));
|
2001-12-28 20:32:36 +03:00
|
|
|
for (i = 0; i < len; i++)
|
2002-03-17 21:02:52 +03:00
|
|
|
DPRINTF((" %02x", ((u_char *)data)[i]));
|
2000-02-22 14:24:22 +03:00
|
|
|
DPRINTF(("\n"));
|
|
|
|
}
|
|
|
|
#endif
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
(void)b_to_q(data, len, &sc->sc_q);
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
if (sc->sc_state & UHID_ASLP) {
|
|
|
|
sc->sc_state &= ~UHID_ASLP;
|
2001-09-15 20:16:27 +04:00
|
|
|
DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
|
1999-06-30 10:44:22 +04:00
|
|
|
wakeup(&sc->sc_q);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
selnotify(&sc->sc_rsel, 0);
|
2000-04-14 18:12:47 +04:00
|
|
|
if (sc->sc_async != NULL) {
|
|
|
|
DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
|
|
|
|
psignal(sc->sc_async, SIGIO);
|
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-06-28 18:20:43 +04:00
|
|
|
uhidopen(dev_t dev, int flag, int mode, struct lwp *l)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct uhid_softc *sc;
|
2001-12-28 20:32:36 +03:00
|
|
|
int error;
|
1999-10-12 15:54:56 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
DPRINTF(("uhidopen: sc=%p\n", sc));
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (ENXIO);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
error = uhidev_open(&sc->sc_hdev);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-05-09 18:38:01 +04:00
|
|
|
if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
|
2001-12-28 20:32:36 +03:00
|
|
|
uhidev_close(&sc->sc_hdev);
|
1998-12-26 15:53:00 +03:00
|
|
|
return (ENOMEM);
|
1999-05-09 18:38:01 +04:00
|
|
|
}
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
|
1999-05-09 18:38:01 +04:00
|
|
|
sc->sc_state &= ~UHID_IMMED;
|
2001-12-28 20:32:36 +03:00
|
|
|
sc->sc_async = NULL;
|
2000-04-14 18:12:47 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
return (0);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-06-28 18:20:43 +04:00
|
|
|
uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct uhid_softc *sc;
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_GET_SC(uhid, UHIDUNIT(dev), sc);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
DPRINTF(("uhidclose: sc=%p\n", sc));
|
|
|
|
|
|
|
|
clfree(&sc->sc_q);
|
1999-06-30 10:44:22 +04:00
|
|
|
free(sc->sc_obuf, M_USBDEV);
|
2001-12-28 20:32:36 +03:00
|
|
|
sc->sc_async = NULL;
|
|
|
|
uhidev_close(&sc->sc_hdev);
|
2000-04-14 18:12:47 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
return (0);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
int error = 0;
|
2001-12-28 20:32:36 +03:00
|
|
|
int extra;
|
1998-07-12 23:51:55 +04:00
|
|
|
size_t length;
|
|
|
|
u_char buffer[UHID_CHUNK];
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
DPRINTFN(1, ("uhidread\n"));
|
1998-07-13 14:49:41 +04:00
|
|
|
if (sc->sc_state & UHID_IMMED) {
|
|
|
|
DPRINTFN(1, ("uhidread immed\n"));
|
2001-12-28 20:32:36 +03:00
|
|
|
extra = sc->sc_hdev.sc_report_id != 0;
|
|
|
|
err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
|
|
|
|
buffer, sc->sc_isize + extra);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err)
|
1998-07-13 14:49:41 +04:00
|
|
|
return (EIO);
|
2001-12-28 20:32:36 +03:00
|
|
|
return (uiomove(buffer+extra, sc->sc_isize, uio));
|
1998-07-13 14:49:41 +04:00
|
|
|
}
|
|
|
|
|
1998-12-09 03:18:10 +03:00
|
|
|
s = splusb();
|
1998-07-12 23:51:55 +04:00
|
|
|
while (sc->sc_q.c_cc == 0) {
|
|
|
|
if (flag & IO_NDELAY) {
|
|
|
|
splx(s);
|
1999-06-30 10:44:22 +04:00
|
|
|
return (EWOULDBLOCK);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
sc->sc_state |= UHID_ASLP;
|
2001-09-15 20:16:27 +04:00
|
|
|
DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
|
1999-06-30 10:44:22 +04:00
|
|
|
error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
|
1998-07-12 23:51:55 +04:00
|
|
|
DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
|
|
|
error = EIO;
|
1998-07-12 23:51:55 +04:00
|
|
|
if (error) {
|
|
|
|
sc->sc_state &= ~UHID_ASLP;
|
1999-06-30 10:44:22 +04:00
|
|
|
break;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* Transfer as many chunks as possible. */
|
1999-06-30 10:44:22 +04:00
|
|
|
while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
|
1998-07-12 23:51:55 +04:00
|
|
|
length = min(sc->sc_q.c_cc, uio->uio_resid);
|
|
|
|
if (length > sizeof(buffer))
|
|
|
|
length = sizeof(buffer);
|
|
|
|
|
|
|
|
/* Remove a small chunk from the input queue. */
|
|
|
|
(void) q_to_b(&sc->sc_q, buffer, length);
|
1999-12-19 01:47:11 +03:00
|
|
|
DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
/* Copy the data to the user process. */
|
|
|
|
if ((error = uiomove(buffer, length, uio)) != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhidread(dev_t dev, struct uio *uio, int flag)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct uhid_softc *sc;
|
1999-06-30 10:44:22 +04:00
|
|
|
int error;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(uhid, UHIDUNIT(dev), sc);
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_refcnt++;
|
|
|
|
error = uhid_do_read(sc, uio, flag);
|
|
|
|
if (--sc->sc_refcnt < 0)
|
2001-12-28 20:32:36 +03:00
|
|
|
usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int size;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
DPRINTFN(1, ("uhidwrite\n"));
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
size = sc->sc_osize;
|
|
|
|
error = 0;
|
1999-06-30 10:44:22 +04:00
|
|
|
if (uio->uio_resid != size)
|
|
|
|
return (EINVAL);
|
|
|
|
error = uiomove(sc->sc_obuf, size, uio);
|
|
|
|
if (!error) {
|
2001-12-28 20:32:36 +03:00
|
|
|
err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
|
|
|
|
sc->sc_obuf, size);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err)
|
1998-07-12 23:51:55 +04:00
|
|
|
error = EIO;
|
|
|
|
}
|
1999-06-30 10:44:22 +04:00
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhidwrite(dev_t dev, struct uio *uio, int flag)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct uhid_softc *sc;
|
1999-06-30 10:44:22 +04:00
|
|
|
int error;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(uhid, UHIDUNIT(dev), sc);
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_refcnt++;
|
|
|
|
error = uhid_do_write(sc, uio, flag);
|
|
|
|
if (--sc->sc_refcnt < 0)
|
2001-12-28 20:32:36 +03:00
|
|
|
usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
|
2003-06-28 18:20:43 +04:00
|
|
|
int flag, struct lwp *l)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
|
|
|
struct usb_ctl_report_desc *rd;
|
1998-07-13 14:49:41 +04:00
|
|
|
struct usb_ctl_report *re;
|
2001-12-28 20:32:36 +03:00
|
|
|
u_char buffer[UHID_CHUNK];
|
|
|
|
int size, extra;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
2001-12-28 20:32:36 +03:00
|
|
|
void *desc;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
|
|
|
|
|
|
|
|
if (sc->sc_dying)
|
1998-07-12 23:51:55 +04:00
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
switch (cmd) {
|
1998-07-13 14:49:41 +04:00
|
|
|
case FIONBIO:
|
|
|
|
/* All handled in the upper FS layer. */
|
|
|
|
break;
|
|
|
|
|
2000-04-14 18:12:47 +04:00
|
|
|
case FIOASYNC:
|
|
|
|
if (*(int *)addr) {
|
|
|
|
if (sc->sc_async != NULL)
|
|
|
|
return (EBUSY);
|
2003-06-28 18:20:43 +04:00
|
|
|
sc->sc_async = l->l_proc;
|
|
|
|
DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
|
2000-04-14 18:12:47 +04:00
|
|
|
} else
|
|
|
|
sc->sc_async = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* XXX this is not the most general solution. */
|
|
|
|
case TIOCSPGRP:
|
|
|
|
if (sc->sc_async == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
if (*(int *)addr != sc->sc_async->p_pgid)
|
|
|
|
return (EPERM);
|
|
|
|
break;
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
case USB_GET_REPORT_DESC:
|
2001-12-28 20:32:36 +03:00
|
|
|
uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
|
1998-07-12 23:51:55 +04:00
|
|
|
rd = (struct usb_ctl_report_desc *)addr;
|
2002-02-20 23:30:12 +03:00
|
|
|
size = min(size, sizeof rd->ucrd_data);
|
|
|
|
rd->ucrd_size = size;
|
|
|
|
memcpy(rd->ucrd_data, desc, size);
|
1998-07-12 23:51:55 +04:00
|
|
|
break;
|
1998-07-13 14:49:41 +04:00
|
|
|
|
|
|
|
case USB_SET_IMMED:
|
1998-12-03 23:43:19 +03:00
|
|
|
if (*(int *)addr) {
|
2001-12-28 20:32:36 +03:00
|
|
|
extra = sc->sc_hdev.sc_report_id != 0;
|
|
|
|
err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
|
|
|
|
buffer, sc->sc_isize + extra);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err)
|
1998-12-03 23:43:19 +03:00
|
|
|
return (EOPNOTSUPP);
|
1998-12-26 15:53:00 +03:00
|
|
|
|
1998-07-13 14:49:41 +04:00
|
|
|
sc->sc_state |= UHID_IMMED;
|
1998-12-03 23:43:19 +03:00
|
|
|
} else
|
1998-07-13 14:49:41 +04:00
|
|
|
sc->sc_state &= ~UHID_IMMED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_GET_REPORT:
|
|
|
|
re = (struct usb_ctl_report *)addr;
|
2002-02-20 23:30:12 +03:00
|
|
|
switch (re->ucr_report) {
|
1998-07-13 14:49:41 +04:00
|
|
|
case UHID_INPUT_REPORT:
|
|
|
|
size = sc->sc_isize;
|
|
|
|
break;
|
|
|
|
case UHID_OUTPUT_REPORT:
|
|
|
|
size = sc->sc_osize;
|
|
|
|
break;
|
|
|
|
case UHID_FEATURE_REPORT:
|
|
|
|
size = sc->sc_fsize;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2001-12-28 20:32:36 +03:00
|
|
|
extra = sc->sc_hdev.sc_report_id != 0;
|
2002-02-20 23:30:12 +03:00
|
|
|
err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
|
|
|
|
re->ucr_data, size + extra);
|
2001-12-28 20:32:36 +03:00
|
|
|
if (extra)
|
2002-02-20 23:30:12 +03:00
|
|
|
memcpy(re->ucr_data, re->ucr_data+1, size);
|
2000-03-20 01:23:28 +03:00
|
|
|
if (err)
|
|
|
|
return (EIO);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_SET_REPORT:
|
|
|
|
re = (struct usb_ctl_report *)addr;
|
2002-02-20 23:30:12 +03:00
|
|
|
switch (re->ucr_report) {
|
2000-03-20 01:23:28 +03:00
|
|
|
case UHID_INPUT_REPORT:
|
|
|
|
size = sc->sc_isize;
|
|
|
|
break;
|
|
|
|
case UHID_OUTPUT_REPORT:
|
|
|
|
size = sc->sc_osize;
|
|
|
|
break;
|
|
|
|
case UHID_FEATURE_REPORT:
|
|
|
|
size = sc->sc_fsize;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-02-20 23:30:12 +03:00
|
|
|
err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
|
|
|
|
re->ucr_data, size);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err)
|
1998-07-13 14:49:41 +04:00
|
|
|
return (EIO);
|
|
|
|
break;
|
|
|
|
|
2001-12-28 20:32:36 +03:00
|
|
|
case USB_GET_REPORT_ID:
|
|
|
|
*(int *)addr = sc->sc_hdev.sc_report_id;
|
|
|
|
break;
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
2003-06-28 18:20:43 +04:00
|
|
|
uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct lwp *l)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct uhid_softc *sc;
|
1999-06-30 10:44:22 +04:00
|
|
|
int error;
|
|
|
|
|
1999-10-12 15:54:56 +04:00
|
|
|
USB_GET_SC(uhid, UHIDUNIT(dev), sc);
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
sc->sc_refcnt++;
|
2003-06-28 18:20:43 +04:00
|
|
|
error = uhid_do_ioctl(sc, cmd, addr, flag, l);
|
1999-06-30 10:44:22 +04:00
|
|
|
if (--sc->sc_refcnt < 0)
|
2001-12-28 20:32:36 +03:00
|
|
|
usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
int
|
2003-06-28 18:20:43 +04:00
|
|
|
uhidpoll(dev_t dev, int events, struct lwp *l)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
1999-10-12 15:54:56 +04:00
|
|
|
struct uhid_softc *sc;
|
1998-07-12 23:51:55 +04:00
|
|
|
int revents = 0;
|
|
|
|
int s;
|
1999-10-12 15:54:56 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_GET_SC(uhid, UHIDUNIT(dev), sc);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
if (sc->sc_dying)
|
1998-07-12 23:51:55 +04:00
|
|
|
return (EIO);
|
|
|
|
|
1998-12-09 03:18:10 +03:00
|
|
|
s = splusb();
|
1998-07-12 23:51:55 +04:00
|
|
|
if (events & (POLLOUT | POLLWRNORM))
|
|
|
|
revents |= events & (POLLOUT | POLLWRNORM);
|
1998-08-20 23:55:06 +04:00
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
1998-07-12 23:51:55 +04:00
|
|
|
if (sc->sc_q.c_cc > 0)
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
2003-06-28 18:20:43 +04:00
|
|
|
selrecord(l, &sc->sc_rsel);
|
1998-08-20 23:55:06 +04:00
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return (revents);
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
filt_uhidrdetach(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct uhid_softc *sc = kn->kn_hook;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splusb();
|
2002-11-26 21:49:40 +03:00
|
|
|
SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
|
2002-10-23 13:10:23 +04:00
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filt_uhidread(struct knote *kn, long hint)
|
|
|
|
{
|
|
|
|
struct uhid_softc *sc = kn->kn_hook;
|
|
|
|
|
|
|
|
kn->kn_data = sc->sc_q.c_cc;
|
|
|
|
return (kn->kn_data > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct filterops uhidread_filtops =
|
|
|
|
{ 1, NULL, filt_uhidrdetach, filt_uhidread };
|
|
|
|
|
|
|
|
static const struct filterops uhid_seltrue_filtops =
|
|
|
|
{ 1, NULL, filt_uhidrdetach, filt_seltrue };
|
|
|
|
|
|
|
|
int
|
|
|
|
uhidkqfilter(dev_t dev, struct knote *kn)
|
|
|
|
{
|
|
|
|
struct uhid_softc *sc;
|
|
|
|
struct klist *klist;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
USB_GET_SC(uhid, UHIDUNIT(dev), sc);
|
|
|
|
|
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
switch (kn->kn_filter) {
|
|
|
|
case EVFILT_READ:
|
2002-11-26 21:49:40 +03:00
|
|
|
klist = &sc->sc_rsel.sel_klist;
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_fop = &uhidread_filtops;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVFILT_WRITE:
|
2002-11-26 21:49:40 +03:00
|
|
|
klist = &sc->sc_rsel.sel_klist;
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_fop = &uhid_seltrue_filtops;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
kn->kn_hook = sc;
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
SLIST_INSERT_HEAD(klist, kn, kn_selnext);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|