Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.34
      1 /*	$NetBSD: ums.c,v 1.34 1999/11/12 00:34:58 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 #include <sys/device.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/tty.h>
     51 #include <sys/file.h>
     52 #include <sys/select.h>
     53 #include <sys/proc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/poll.h>
     56 
     57 #include <dev/usb/usb.h>
     58 #include <dev/usb/usbhid.h>
     59 
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdi_util.h>
     62 #include <dev/usb/usbdevs.h>
     63 #include <dev/usb/usb_quirks.h>
     64 #include <dev/usb/hid.h>
     65 
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wscons/wsmousevar.h>
     68 
     69 #ifdef USB_DEBUG
     70 #define DPRINTF(x)	if (umsdebug) logprintf x
     71 #define DPRINTFN(n,x)	if (umsdebug>(n)) logprintf x
     72 int	umsdebug = 0;
     73 #else
     74 #define DPRINTF(x)
     75 #define DPRINTFN(n,x)
     76 #endif
     77 
     78 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
     79 
     80 #define UMSUNIT(s)	(minor(s))
     81 
     82 #define PS2LBUTMASK	x01
     83 #define PS2RBUTMASK	x02
     84 #define PS2MBUTMASK	x04
     85 #define PS2BUTMASK 0x0f
     86 
     87 struct ums_softc {
     88 	USBBASEDEVICE sc_dev;		/* base device */
     89 	usbd_interface_handle sc_iface;	/* interface */
     90 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     91 	int sc_ep_addr;
     92 
     93 	u_char *sc_ibuf;
     94 	u_int8_t sc_iid;
     95 	int sc_isize;
     96 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
     97 	struct hid_location *sc_loc_btn;
     98 
     99 	int sc_enabled;
    100 
    101 	int flags;		/* device configuration */
    102 #define UMS_Z		0x01	/* z direction available */
    103 	int nbuttons;
    104 #define MAX_BUTTONS	31	/* chosen because sc_buttons is u_int32_t */
    105 
    106 	char revz;		/* Z-axis is reversed */
    107 
    108 	u_int32_t sc_buttons;	/* mouse button status */
    109 	struct device *sc_wsmousedev;
    110 
    111 	char			sc_dying;
    112 };
    113 
    114 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    115 #define MOUSE_FLAGS (HIO_RELATIVE)
    116 
    117 static void ums_intr __P((usbd_xfer_handle, usbd_private_handle,
    118 			  usbd_status));
    119 
    120 static int	ums_enable __P((void *));
    121 static void	ums_disable __P((void *));
    122 static int	ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    123 
    124 const struct wsmouse_accessops ums_accessops = {
    125 	ums_enable,
    126 	ums_ioctl,
    127 	ums_disable,
    128 };
    129 
    130 USB_DECLARE_DRIVER(ums);
    131 
    132 USB_MATCH(ums)
    133 {
    134 	USB_MATCH_START(ums, uaa);
    135 	usb_interface_descriptor_t *id;
    136 	int size, ret;
    137 	void *desc;
    138 	usbd_status err;
    139 
    140 	if (uaa->iface == NULL)
    141 		return (UMATCH_NONE);
    142 	id = usbd_get_interface_descriptor(uaa->iface);
    143 	if (id == NULL || id->bInterfaceClass != UCLASS_HID)
    144 		return (UMATCH_NONE);
    145 
    146 	err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    147 	if (err)
    148 		return (UMATCH_NONE);
    149 
    150 	if (hid_is_collection(desc, size,
    151 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    152 		ret = UMATCH_IFACECLASS;
    153 	else
    154 		ret = UMATCH_NONE;
    155 
    156 	free(desc, M_TEMP);
    157 	return (ret);
    158 }
    159 
    160 USB_ATTACH(ums)
    161 {
    162 	USB_ATTACH_START(ums, sc, uaa);
    163 	usbd_interface_handle iface = uaa->iface;
    164 	usb_interface_descriptor_t *id;
    165 	usb_endpoint_descriptor_t *ed;
    166 	struct wsmousedev_attach_args a;
    167 	int size;
    168 	void *desc;
    169 	usbd_status err;
    170 	char devinfo[1024];
    171 	u_int32_t flags;
    172 	int i;
    173 	struct hid_location loc_btn;
    174 
    175 	sc->sc_iface = iface;
    176 	id = usbd_get_interface_descriptor(iface);
    177 	usbd_devinfo(uaa->device, 0, devinfo);
    178 	USB_ATTACH_SETUP;
    179 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    180 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    181 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    182 	if (ed == NULL) {
    183 		printf("%s: could not read endpoint descriptor\n",
    184 		       USBDEVNAME(sc->sc_dev));
    185 		USB_ATTACH_ERROR_RETURN;
    186 	}
    187 
    188 	DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
    189 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    190 		     " bInterval=%d\n",
    191 		     ed->bLength, ed->bDescriptorType,
    192 		     ed->bEndpointAddress & UE_ADDR,
    193 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    194 		     ed->bmAttributes & UE_XFERTYPE,
    195 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    196 
    197 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    198 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    199 		printf("%s: unexpected endpoint\n",
    200 		       USBDEVNAME(sc->sc_dev));
    201 		USB_ATTACH_ERROR_RETURN;
    202 	}
    203 
    204 	sc->revz = (usbd_get_quirks(uaa->device)->uq_flags & UQ_MS_REVZ) != 0;
    205 
    206 	err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    207 	if (err)
    208 		USB_ATTACH_ERROR_RETURN;
    209 
    210 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    211 		       hid_input, &sc->sc_loc_x, &flags)) {
    212 		printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
    213 		USB_ATTACH_ERROR_RETURN;
    214 	}
    215 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    216 		printf("%s: X report 0x%04x not supported\n",
    217 		       USBDEVNAME(sc->sc_dev), flags);
    218 		USB_ATTACH_ERROR_RETURN;
    219 	}
    220 
    221 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    222 		       hid_input, &sc->sc_loc_y, &flags)) {
    223 		printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
    224 		USB_ATTACH_ERROR_RETURN;
    225 	}
    226 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    227 		printf("%s: Y report 0x%04x not supported\n",
    228 		       USBDEVNAME(sc->sc_dev), flags);
    229 		USB_ATTACH_ERROR_RETURN;
    230 	}
    231 
    232 	/* Try to guess the Z activator: first check Z, then WHEEL. */
    233 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    234 		       hid_input, &sc->sc_loc_z, &flags) ||
    235 	    hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    236 		       hid_input, &sc->sc_loc_z, &flags)) {
    237 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    238 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    239 		} else {
    240 			sc->flags |= UMS_Z;
    241 		}
    242 	}
    243 
    244 	/* figure out the number of buttons */
    245 	for (i = 1; i <= MAX_BUTTONS; i++)
    246 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    247 				hid_input, &loc_btn, 0))
    248 			break;
    249 	sc->nbuttons = i - 1;
    250 	sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
    251 				M_USBDEV, M_NOWAIT);
    252 	if (!sc->sc_loc_btn) {
    253 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    254 		USB_ATTACH_ERROR_RETURN;
    255 	}
    256 
    257 	printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
    258 	       sc->nbuttons, sc->flags & UMS_Z ? " and Z dir." : "");
    259 
    260 	for (i = 1; i <= sc->nbuttons; i++)
    261 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    262 				hid_input, &sc->sc_loc_btn[i-1], 0);
    263 
    264 	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
    265 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
    266 	if (sc->sc_ibuf == NULL) {
    267 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    268 		free(sc->sc_loc_btn, M_USBDEV);
    269 		USB_ATTACH_ERROR_RETURN;
    270 	}
    271 
    272 	sc->sc_ep_addr = ed->bEndpointAddress;
    273 	free(desc, M_TEMP);
    274 
    275 #ifdef USB_DEBUG
    276 	DPRINTF(("ums_attach: sc=%p\n", sc));
    277 	DPRINTF(("ums_attach: X\t%d/%d\n",
    278 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    279 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    280 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    281 	if (sc->flags & UMS_Z)
    282 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    283 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    284 	for (i = 1; i <= sc->nbuttons; i++) {
    285 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    286 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    287 	}
    288 	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
    289 #endif
    290 
    291 	a.accessops = &ums_accessops;
    292 	a.accesscookie = sc;
    293 
    294 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    295 
    296 	USB_ATTACH_SUCCESS_RETURN;
    297 }
    298 
    299 int
    300 ums_activate(self, act)
    301 	device_ptr_t self;
    302 	enum devact act;
    303 {
    304 	struct ums_softc *sc = (struct ums_softc *)self;
    305 	int rv = 0;
    306 
    307 	switch (act) {
    308 	case DVACT_ACTIVATE:
    309 		return (EOPNOTSUPP);
    310 		break;
    311 
    312 	case DVACT_DEACTIVATE:
    313 		if (sc->sc_wsmousedev != NULL)
    314 			rv = config_deactivate(sc->sc_wsmousedev);
    315 		sc->sc_dying = 1;
    316 		break;
    317 	}
    318 	return (rv);
    319 }
    320 
    321 USB_DETACH(ums)
    322 {
    323 	USB_DETACH_START(ums, sc);
    324 	int rv = 0;
    325 
    326 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    327 
    328 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    329 	if (sc->sc_wsmousedev != NULL)
    330 		rv = config_detach(sc->sc_wsmousedev, flags);
    331 	if (rv == 0) {
    332 		free(sc->sc_loc_btn, M_USBDEV);
    333 		free(sc->sc_ibuf, M_USBDEV);
    334 	}
    335 	return (rv);
    336 }
    337 
    338 void
    339 ums_intr(xfer, addr, status)
    340 	usbd_xfer_handle xfer;
    341 	usbd_private_handle addr;
    342 	usbd_status status;
    343 {
    344 	struct ums_softc *sc = addr;
    345 	u_char *ibuf;
    346 	int dx, dy, dz;
    347 	u_int32_t buttons = 0;
    348 	int i;
    349 	int s;
    350 
    351 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    352 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    353 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    354 
    355 	if (status == USBD_CANCELLED)
    356 		return;
    357 
    358 	if (status != USBD_NORMAL_COMPLETION) {
    359 		DPRINTF(("ums_intr: status=%d\n", status));
    360 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    361 		return;
    362 	}
    363 
    364 	ibuf = sc->sc_ibuf;
    365 	if (sc->sc_iid != 0) {
    366 		if (*ibuf++ != sc->sc_iid)
    367 			return;
    368 	}
    369 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    370 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    371 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    372 	if (sc->revz)
    373 		dz = -dz;
    374 	for (i = 0; i < sc->nbuttons; i++)
    375 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    376 			buttons |= (1 << UMS_BUT(i));
    377 
    378 	if (dx || dy || dz || buttons != sc->sc_buttons) {
    379 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    380 			dx, dy, dz, buttons));
    381 		sc->sc_buttons = buttons;
    382 		if (sc->sc_wsmousedev) {
    383 			s = spltty();
    384 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
    385 			splx(s);
    386 		}
    387 	}
    388 }
    389 
    390 static int
    391 ums_enable(v)
    392 	void *v;
    393 {
    394 	struct ums_softc *sc = v;
    395 
    396 	usbd_status err;
    397 
    398 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    399 
    400 	if (sc->sc_dying)
    401 		return (EIO);
    402 
    403 	if (sc->sc_enabled)
    404 		return (EBUSY);
    405 
    406 	sc->sc_enabled = 1;
    407 	sc->sc_buttons = 0;
    408 
    409 	/* Set up interrupt pipe. */
    410 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    411 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    412 		  sc->sc_ibuf, sc->sc_isize, ums_intr);
    413 	if (err) {
    414 		DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
    415 			 err));
    416 		sc->sc_enabled = 0;
    417 		return (EIO);
    418 	}
    419 	return (0);
    420 }
    421 
    422 static void
    423 ums_disable(v)
    424 	void *v;
    425 {
    426 	struct ums_softc *sc = v;
    427 
    428 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    429 #ifdef DIAGNOSTIC
    430 	if (!sc->sc_enabled) {
    431 		printf("ums_disable: not enabled\n");
    432 		return;
    433 	}
    434 #endif
    435 
    436 	/* Disable interrupts. */
    437 	usbd_abort_pipe(sc->sc_intrpipe);
    438 	usbd_close_pipe(sc->sc_intrpipe);
    439 
    440 	sc->sc_enabled = 0;
    441 }
    442 
    443 static int
    444 ums_ioctl(v, cmd, data, flag, p)
    445 	void *v;
    446 	u_long cmd;
    447 	caddr_t data;
    448 	int flag;
    449 	struct proc *p;
    450 
    451 {
    452 	switch (cmd) {
    453 	case WSMOUSEIO_GTYPE:
    454 		*(u_int *)data = WSMOUSE_TYPE_USB;
    455 		return (0);
    456 	}
    457 
    458 	return (-1);
    459 }
    460