Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.50
      1 /*	$NetBSD: ums.c,v 1.50 2001/10/28 17:16: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 (lennart (at) augustsson.net) 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/devclass/hid1_1.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_device_handle sc_udev;
     90 	usbd_interface_handle sc_iface;	/* interface */
     91 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     92 	int sc_ep_addr;
     93 
     94 	u_char *sc_ibuf;
     95 	u_int8_t sc_iid;
     96 	int sc_isize;
     97 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
     98 	struct hid_location *sc_loc_btn;
     99 
    100 	int sc_enabled;
    101 
    102 	int flags;		/* device configuration */
    103 #define UMS_Z		0x01	/* z direction available */
    104 #define UMS_SPUR_BUT_UP	0x02	/* spurious button up events */
    105 #define UMS_REVZ	0x04	/* Z-axis is reversed */
    106 
    107 	int nbuttons;
    108 #define MAX_BUTTONS	31	/* chosen because sc_buttons is u_int32_t */
    109 
    110 	u_int32_t sc_buttons;	/* mouse button status */
    111 	struct device *sc_wsmousedev;
    112 
    113 	char			sc_dying;
    114 };
    115 
    116 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    117 #define MOUSE_FLAGS (HIO_RELATIVE)
    118 
    119 Static void ums_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    120 
    121 Static int	ums_enable(void *);
    122 Static void	ums_disable(void *);
    123 Static int	ums_ioctl(void *, u_long, caddr_t, int, struct proc *);
    124 
    125 const struct wsmouse_accessops ums_accessops = {
    126 	ums_enable,
    127 	ums_ioctl,
    128 	ums_disable,
    129 };
    130 
    131 USB_DECLARE_DRIVER(ums);
    132 
    133 USB_MATCH(ums)
    134 {
    135 	USB_MATCH_START(ums, uaa);
    136 	usb_interface_descriptor_t *id;
    137 	int size, ret;
    138 	void *desc;
    139 	usbd_status err;
    140 
    141 	if (uaa->iface == NULL)
    142 		return (UMATCH_NONE);
    143 	id = usbd_get_interface_descriptor(uaa->iface);
    144 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
    145 		return (UMATCH_NONE);
    146 
    147 	err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
    148 	if (err)
    149 		return (UMATCH_NONE);
    150 
    151 	if (hid_is_collection(desc, size,
    152 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    153 		ret = UMATCH_IFACECLASS;
    154 	else
    155 		ret = UMATCH_NONE;
    156 
    157 	free(desc, M_TEMP);
    158 	return (ret);
    159 }
    160 
    161 USB_ATTACH(ums)
    162 {
    163 	USB_ATTACH_START(ums, sc, uaa);
    164 	usbd_interface_handle iface = uaa->iface;
    165 	usb_interface_descriptor_t *id;
    166 	usb_endpoint_descriptor_t *ed;
    167 	struct wsmousedev_attach_args a;
    168 	int size;
    169 	void *desc;
    170 	usbd_status err;
    171 	char devinfo[1024];
    172 	u_int32_t flags, quirks;
    173 	int i, wheel;
    174 	struct hid_location loc_btn;
    175 
    176 	sc->sc_udev = uaa->device;
    177 	sc->sc_iface = iface;
    178 	id = usbd_get_interface_descriptor(iface);
    179 	usbd_devinfo(uaa->device, 0, devinfo);
    180 	USB_ATTACH_SETUP;
    181 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    182 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    183 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    184 	if (ed == NULL) {
    185 		printf("%s: could not read endpoint descriptor\n",
    186 		       USBDEVNAME(sc->sc_dev));
    187 		USB_ATTACH_ERROR_RETURN;
    188 	}
    189 
    190 	DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
    191 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    192 		     " bInterval=%d\n",
    193 		     ed->bLength, ed->bDescriptorType,
    194 		     ed->bEndpointAddress & UE_ADDR,
    195 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    196 		     ed->bmAttributes & UE_XFERTYPE,
    197 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    198 
    199 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    200 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    201 		printf("%s: unexpected endpoint\n",
    202 		       USBDEVNAME(sc->sc_dev));
    203 		USB_ATTACH_ERROR_RETURN;
    204 	}
    205 
    206 	quirks = usbd_get_quirks(uaa->device)->uq_flags;
    207 	if (quirks & UQ_MS_REVZ)
    208 		sc->flags |= UMS_REVZ;
    209 	if (quirks & UQ_SPUR_BUT_UP)
    210 		sc->flags |= UMS_SPUR_BUT_UP;
    211 
    212 	err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
    213 	if (err)
    214 		USB_ATTACH_ERROR_RETURN;
    215 
    216 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    217 		       hid_input, &sc->sc_loc_x, &flags)) {
    218 		printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
    219 		USB_ATTACH_ERROR_RETURN;
    220 	}
    221 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    222 		printf("%s: X report 0x%04x not supported\n",
    223 		       USBDEVNAME(sc->sc_dev), flags);
    224 		USB_ATTACH_ERROR_RETURN;
    225 	}
    226 
    227 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    228 		       hid_input, &sc->sc_loc_y, &flags)) {
    229 		printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
    230 		USB_ATTACH_ERROR_RETURN;
    231 	}
    232 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    233 		printf("%s: Y report 0x%04x not supported\n",
    234 		       USBDEVNAME(sc->sc_dev), flags);
    235 		USB_ATTACH_ERROR_RETURN;
    236 	}
    237 
    238 	/* Try to guess the Z activator: first check Z, then WHEEL. */
    239 	wheel = 0;
    240 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    241 		       hid_input, &sc->sc_loc_z, &flags) ||
    242 	    (wheel = hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    243 						       HUG_WHEEL),
    244 		       hid_input, &sc->sc_loc_z, &flags))) {
    245 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    246 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    247 		} else {
    248 			sc->flags |= UMS_Z;
    249 			/* Wheels need the Z axis reversed. */
    250 			if (wheel)
    251 				sc->flags ^= UMS_REVZ;
    252 		}
    253 	}
    254 
    255 	/* figure out the number of buttons */
    256 	for (i = 1; i <= MAX_BUTTONS; i++)
    257 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    258 				hid_input, &loc_btn, 0))
    259 			break;
    260 	sc->nbuttons = i - 1;
    261 	sc->sc_loc_btn = malloc(sizeof(struct hid_location) * sc->nbuttons,
    262 				M_USBDEV, M_NOWAIT);
    263 	if (!sc->sc_loc_btn) {
    264 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    265 		USB_ATTACH_ERROR_RETURN;
    266 	}
    267 
    268 	printf("%s: %d button%s%s\n", USBDEVNAME(sc->sc_dev),
    269 	    sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
    270 	    sc->flags & UMS_Z ? " and Z dir." : "");
    271 
    272 	for (i = 1; i <= sc->nbuttons; i++)
    273 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    274 				hid_input, &sc->sc_loc_btn[i-1], 0);
    275 
    276 	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
    277 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
    278 	if (sc->sc_ibuf == NULL) {
    279 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    280 		free(sc->sc_loc_btn, M_USBDEV);
    281 		USB_ATTACH_ERROR_RETURN;
    282 	}
    283 
    284 	sc->sc_ep_addr = ed->bEndpointAddress;
    285 	free(desc, M_TEMP);
    286 
    287 #ifdef USB_DEBUG
    288 	DPRINTF(("ums_attach: sc=%p\n", sc));
    289 	DPRINTF(("ums_attach: X\t%d/%d\n",
    290 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    291 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    292 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
    293 	if (sc->flags & UMS_Z)
    294 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    295 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    296 	for (i = 1; i <= sc->nbuttons; i++) {
    297 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    298 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    299 	}
    300 	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
    301 #endif
    302 
    303 	a.accessops = &ums_accessops;
    304 	a.accesscookie = sc;
    305 
    306 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    307 			   USBDEV(sc->sc_dev));
    308 
    309 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    310 
    311 	USB_ATTACH_SUCCESS_RETURN;
    312 }
    313 
    314 int
    315 ums_activate(device_ptr_t self, enum devact act)
    316 {
    317 	struct ums_softc *sc = (struct ums_softc *)self;
    318 	int rv = 0;
    319 
    320 	switch (act) {
    321 	case DVACT_ACTIVATE:
    322 		return (EOPNOTSUPP);
    323 		break;
    324 
    325 	case DVACT_DEACTIVATE:
    326 		if (sc->sc_wsmousedev != NULL)
    327 			rv = config_deactivate(sc->sc_wsmousedev);
    328 		sc->sc_dying = 1;
    329 		break;
    330 	}
    331 	return (rv);
    332 }
    333 
    334 USB_DETACH(ums)
    335 {
    336 	USB_DETACH_START(ums, sc);
    337 	int rv = 0;
    338 
    339 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    340 
    341 #ifdef DIAGNOSTIC
    342 	if (sc->sc_intrpipe != NULL) {
    343 		printf("ums_disable: intr pipe still open\n");
    344 		usbd_abort_pipe(sc->sc_intrpipe);
    345 		usbd_close_pipe(sc->sc_intrpipe);
    346 		sc->sc_intrpipe = NULL;
    347 	}
    348 #endif
    349 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    350 	if (sc->sc_wsmousedev != NULL)
    351 		rv = config_detach(sc->sc_wsmousedev, flags);
    352 	if (rv == 0) {
    353 		free(sc->sc_loc_btn, M_USBDEV);
    354 		free(sc->sc_ibuf, M_USBDEV);
    355 	}
    356 
    357 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    358 			   USBDEV(sc->sc_dev));
    359 
    360 	return (rv);
    361 }
    362 
    363 void
    364 ums_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    365 {
    366 	struct ums_softc *sc = addr;
    367 	u_char *ibuf;
    368 	int dx, dy, dz;
    369 	u_int32_t buttons = 0;
    370 	int i;
    371 	int s;
    372 
    373 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    374 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    375 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    376 
    377 	if (status == USBD_CANCELLED)
    378 		return;
    379 
    380 	if (status) {
    381 		DPRINTF(("ums_intr: status=%d\n", status));
    382 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    383 		return;
    384 	}
    385 
    386 	ibuf = sc->sc_ibuf;
    387 	if (sc->sc_iid != 0) {
    388 		if (*ibuf++ != sc->sc_iid)
    389 			return;
    390 	}
    391 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    392 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    393 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    394 	if (sc->flags & UMS_REVZ)
    395 		dz = -dz;
    396 	for (i = 0; i < sc->nbuttons; i++)
    397 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    398 			buttons |= (1 << UMS_BUT(i));
    399 
    400 	if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) {
    401 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    402 			dx, dy, dz, buttons));
    403 		sc->sc_buttons = buttons;
    404 		if (sc->sc_wsmousedev != NULL) {
    405 			s = spltty();
    406 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
    407 				      WSMOUSE_INPUT_DELTA);
    408 			splx(s);
    409 		}
    410 	}
    411 }
    412 
    413 Static int
    414 ums_enable(void *v)
    415 {
    416 	struct ums_softc *sc = v;
    417 
    418 	usbd_status err;
    419 
    420 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    421 
    422 	if (sc->sc_dying)
    423 		return (EIO);
    424 
    425 	if (sc->sc_enabled)
    426 		return (EBUSY);
    427 
    428 	sc->sc_enabled = 1;
    429 	sc->sc_buttons = 0;
    430 
    431 	/* Set up interrupt pipe. */
    432 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    433 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    434 		  sc->sc_ibuf, sc->sc_isize, ums_intr, USBD_DEFAULT_INTERVAL);
    435 	if (err) {
    436 		DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
    437 			 err));
    438 		sc->sc_enabled = 0;
    439 		return (EIO);
    440 	}
    441 	return (0);
    442 }
    443 
    444 Static void
    445 ums_disable(void *v)
    446 {
    447 	struct ums_softc *sc = v;
    448 
    449 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    450 #ifdef DIAGNOSTIC
    451 	if (!sc->sc_enabled) {
    452 		printf("ums_disable: not enabled\n");
    453 		return;
    454 	}
    455 #endif
    456 
    457 	/* Disable interrupts. */
    458 	usbd_abort_pipe(sc->sc_intrpipe);
    459 	usbd_close_pipe(sc->sc_intrpipe);
    460 
    461 	sc->sc_enabled = 0;
    462 }
    463 
    464 Static int
    465 ums_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    466 
    467 {
    468 	switch (cmd) {
    469 	case WSMOUSEIO_GTYPE:
    470 		*(u_int *)data = WSMOUSE_TYPE_USB;
    471 		return (0);
    472 	}
    473 
    474 	return (-1);
    475 }
    476