⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ums.c

📁 基于组件方式开发操作系统的OSKIT源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*	$NetBSD: ums.c,v 1.22 1999/01/12 22:06:48 augustss Exp $	*//*	$FreeBSD: src/sys/dev/usb/ums.c,v 1.9.2.1 1999/05/08 23:04:51 n_hibma Exp $	*//* * Copyright (c) 1998 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Lennart Augustsson (augustss@carlstedt.se) at * 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. *//* * HID spec: http://www.usb.org/developers/data/usbhid10.pdf */#include <sys/param.h>#include <sys/systm.h>#include <sys/kernel.h>#include <sys/malloc.h>#if defined(__NetBSD__)#include <sys/device.h>#include <sys/ioctl.h>#elif defined(__FreeBSD__)#include <sys/module.h>#include <sys/bus.h>#include <sys/ioccom.h>#include <sys/conf.h>#endif#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>#include <dev/usb/usbdi.h>#include <dev/usb/usbdi_util.h>#include <dev/usb/usbdevs.h>#include <dev/usb/usb_quirks.h>#include <dev/usb/hid.h>#if defined(__NetBSD__)#include <dev/wscons/wsconsio.h>#include <dev/wscons/wsmousevar.h>#elif defined(__FreeBSD__)#include <machine/mouse.h>#endif#ifdef UMS_DEBUG#define DPRINTF(x)	if (umsdebug) logprintf x#define DPRINTFN(n,x)	if (umsdebug>(n)) logprintf xint	umsdebug = 1;#else#define DPRINTF(x)#define DPRINTFN(n,x)#endif#define UMSUNIT(s)	(minor(s)&0x1f)#define QUEUE_BUFSIZE	400	/* MUST be divisible by 5 _and_ 8 */#ifndef MOUSE_MSC_MAXBUTTON#define MOUSE_MSC_MAXBUTTON	3#endif#ifndef MOUSE_IF_USB#define MOUSE_IF_USB	MOUSE_IF_UNKNOWN#endifstruct ums_softc {	bdevice sc_dev;			/* base device */	usbd_interface_handle sc_iface;	/* interface */	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */	int sc_ep_addr;	u_char *sc_ibuf;	u_int8_t sc_iid;	int sc_isize;	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;	struct hid_location *sc_loc_btn;	int sc_enabled;	int sc_disconnected;	/* device is gone */	int flags;		/* device configuration */#define UMS_Z		0x01	/* z direction available */	int nbuttons;#define MAX_BUTTONS	7	/* chosen because sc_buttons is u_char */#if defined(__NetBSD__)	u_char sc_buttons;	/* mouse button status */	struct device *sc_wsmousedev;#elif defined(__FreeBSD__)	u_char		qbuf[QUEUE_BUFSIZE];	/* must be divisable by 3&4 */	u_char		dummy[100];	/* XXX just for safety and for now */	int		qcount, qhead, qtail;	mousehw_t	hw;	mousemode_t	mode;	mousestatus_t	status;	int		state;#	  define	UMS_ASLEEP	0x01	/* readFromDevice is waiting */#	  define	UMS_SELECT	0x02	/* select is waiting */	struct selinfo	rsel;		/* process waiting in select */#endif};#define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)#define MOUSE_FLAGS (HIO_RELATIVE)void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));void ums_disco __P((void *));static int	ums_enable __P((void *));static void	ums_disable __P((void *));#if defined(__NetBSD__)static int	ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));const struct wsmouse_accessops ums_accessops = {	ums_enable,	ums_ioctl,	ums_disable,};#elif defined(__FreeBSD__)static d_open_t ums_open;static d_close_t ums_close;static d_read_t ums_read;static d_ioctl_t ums_ioctl;static d_poll_t ums_poll;#define UMS_CDEV_MAJOR	111static struct cdevsw ums_cdevsw = {	ums_open,	ums_close,	ums_read,	nowrite,	ums_ioctl,	nostop,		nullreset,	nodevtotty,	ums_poll,	nommap,		nostrat,	"ums",		NULL,		-1};#endifUSB_DECLARE_DRIVER(ums);USB_MATCH(ums){	USB_MATCH_START(ums, uaa);	usb_interface_descriptor_t *id;	int size, ret;	void *desc;	usbd_status r;		if (!uaa->iface)		return (UMATCH_NONE);	id = usbd_get_interface_descriptor(uaa->iface);	if (!id || id->bInterfaceClass != UCLASS_HID)		return (UMATCH_NONE);	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);	if (r != USBD_NORMAL_COMPLETION)		return (UMATCH_NONE);	if (hid_is_collection(desc, size, 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))		ret = UMATCH_IFACECLASS;	else		ret = UMATCH_NONE;	free(desc, M_TEMP);	return (ret);}USB_ATTACH(ums){	USB_ATTACH_START(ums, sc, uaa);	usbd_interface_handle iface = uaa->iface;	usb_interface_descriptor_t *id;	usb_endpoint_descriptor_t *ed;#if defined(__NetBSD__)	struct wsmousedev_attach_args a;#endif	int size;	void *desc;	usbd_status r;	char devinfo[1024];	u_int32_t flags;	int i;	struct hid_location loc_btn;		sc->sc_disconnected = 1;	sc->sc_iface = iface;	id = usbd_get_interface_descriptor(iface);	usbd_devinfo(uaa->device, 0, devinfo);	USB_ATTACH_SETUP;	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);	ed = usbd_interface2endpoint_descriptor(iface, 0);	if (!ed) {		printf("%s: could not read endpoint descriptor\n",		       USBDEVNAME(sc->sc_dev));		USB_ATTACH_ERROR_RETURN;	}	DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"		     " bInterval=%d\n",		     ed->bLength, ed->bDescriptorType, 		     ed->bEndpointAddress & UE_ADDR,		     ed->bEndpointAddress & UE_IN ? "in" : "out",		     ed->bmAttributes & UE_XFERTYPE,		     UGETW(ed->wMaxPacketSize), ed->bInterval));	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {		printf("%s: unexpected endpoint\n",		       USBDEVNAME(sc->sc_dev));		USB_ATTACH_ERROR_RETURN;	}	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);	if (r != USBD_NORMAL_COMPLETION)		USB_ATTACH_ERROR_RETURN;	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),		       hid_input, &sc->sc_loc_x, &flags)) {		printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));		USB_ATTACH_ERROR_RETURN;	}	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {		printf("%s: X report 0x%04x not supported\n",		       USBDEVNAME(sc->sc_dev), flags);		USB_ATTACH_ERROR_RETURN;	}	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),		       hid_input, &sc->sc_loc_y, &flags)) {		printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));		USB_ATTACH_ERROR_RETURN;	}	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {		printf("%s: Y report 0x%04x not supported\n",		       USBDEVNAME(sc->sc_dev), flags);		USB_ATTACH_ERROR_RETURN;	}	/* try to guess the Z activator: first check Z, then WHEEL */	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),		       hid_input, &sc->sc_loc_z, &flags) ||	    hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),		       hid_input, &sc->sc_loc_z, &flags)) {		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */		} else {			sc->flags |= UMS_Z;		}	}	/* figure out the number of buttons */	for (i = 1; i <= MAX_BUTTONS; i++)		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),				hid_input, &loc_btn, 0))			break;	sc->nbuttons = i - 1;	sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons, 				M_USBDEV, M_NOWAIT);	if (!sc->sc_loc_btn) {		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));		USB_ATTACH_ERROR_RETURN;	}	printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),	       sc->nbuttons, sc->flags & UMS_Z? " and Z dir." : "");	for (i = 1; i <= sc->nbuttons; i++)		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),				hid_input, &sc->sc_loc_btn[i-1], 0);	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);	sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);	if (!sc->sc_ibuf) {		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));		free(sc->sc_loc_btn, M_USB);		USB_ATTACH_ERROR_RETURN;	}	sc->sc_ep_addr = ed->bEndpointAddress;	sc->sc_disconnected = 0;	free(desc, M_TEMP);#ifdef UMS_DEBUG	DPRINTF(("ums_attach: sc=%p\n", sc));	DPRINTF(("ums_attach: X\t%d/%d\n", 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));	DPRINTF(("ums_attach: Y\t%d/%d\n", 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));	if (sc->flags & UMS_Z)		DPRINTF(("ums_attach: Z\t%d/%d\n", 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));	for (i = 1; i <= sc->nbuttons; i++) {		DPRINTF(("ums_attach: B%d\t%d/%d\n",			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));	}	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));#endif#if defined(__NetBSD__)	a.accessops = &ums_accessops;	a.accesscookie = sc;	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);#elif defined(__FreeBSD__)	if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)		sc->hw.buttons = MOUSE_MSC_MAXBUTTON;	else		sc->hw.buttons = sc->nbuttons;	sc->hw.iftype = MOUSE_IF_USB;	sc->hw.type = MOUSE_MOUSE;	sc->hw.model = MOUSE_MODEL_GENERIC;	sc->hw.hwid = 0;	sc->mode.protocol = MOUSE_PROTO_MSC;	sc->mode.rate = -1;	sc->mode.resolution = MOUSE_RES_UNKNOWN;	sc->mode.accelfactor = 0;	sc->mode.level = 0;	sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;	sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;	sc->mode.syncmask[1] = MOUSE_MSC_SYNC;	sc->status.flags = 0;	sc->status.button = sc->status.obutton = 0;	sc->status.dx = sc->status.dy = sc->status.dz = 0;	sc->rsel.si_flags = 0;	sc->rsel.si_pid = 0;#endif	USB_ATTACH_SUCCESS_RETURN;}#if defined(__FreeBSD__)static intums_detach(device_t self){	struct ums_softc *sc = device_get_softc(self);	if (sc->sc_enabled) {		usbd_abort_pipe(sc->sc_intrpipe);		usbd_close_pipe(sc->sc_intrpipe);	}	sc->sc_disconnected = 1;	DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));	device_set_desc(self, NULL);	free(sc->sc_loc_btn, M_USB);	free(sc->sc_ibuf, M_USB);	/* someone waiting for data */	if (sc->state & UMS_ASLEEP) {		sc->state &= ~UMS_ASLEEP;		wakeup(sc);	}	if (sc->state & UMS_SELECT) {		sc->state &= ~UMS_SELECT;		selwakeup(&sc->rsel);	}	return 0;}#endifvoidums_disco(p)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -