Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.73.8.2
      1 /*	$NetBSD: ums.c,v 1.73.8.2 2009/11/27 09:49:42 sborrill 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.73.8.2 2009/11/27 09:49:42 sborrill Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/device.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/tty.h>
     47 #include <sys/file.h>
     48 #include <sys/select.h>
     49 #include <sys/proc.h>
     50 #include <sys/vnode.h>
     51 #include <sys/poll.h>
     52 
     53 #include <dev/usb/usb.h>
     54 #include <dev/usb/usbhid.h>
     55 
     56 #include <dev/usb/usbdi.h>
     57 #include <dev/usb/usbdi_util.h>
     58 #include <dev/usb/usbdevs.h>
     59 #include <dev/usb/usb_quirks.h>
     60 #include <dev/usb/uhidev.h>
     61 #include <dev/usb/hid.h>
     62 
     63 #include <dev/wscons/wsconsio.h>
     64 #include <dev/wscons/wsmousevar.h>
     65 
     66 #ifdef USB_DEBUG
     67 #define DPRINTF(x)	if (umsdebug) logprintf x
     68 #define DPRINTFN(n,x)	if (umsdebug>(n)) logprintf x
     69 int	umsdebug = 0;
     70 #else
     71 #define DPRINTF(x)
     72 #define DPRINTFN(n,x)
     73 #endif
     74 
     75 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
     76 
     77 #define UMSUNIT(s)	(minor(s))
     78 
     79 #define PS2LBUTMASK	x01
     80 #define PS2RBUTMASK	x02
     81 #define PS2MBUTMASK	x04
     82 #define PS2BUTMASK 0x0f
     83 
     84 #define MAX_BUTTONS	31	/* must not exceed size of sc_buttons */
     85 
     86 struct ums_softc {
     87 	struct uhidev sc_hdev;
     88 
     89 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_w;
     90 	struct hid_location sc_loc_btn[MAX_BUTTONS];
     91 
     92 	int sc_enabled;
     93 
     94 	int flags;		/* device configuration */
     95 #define UMS_Z		0x01	/* z direction available */
     96 #define UMS_SPUR_BUT_UP	0x02	/* spurious button up events */
     97 #define UMS_REVZ	0x04	/* Z-axis is reversed */
     98 #define UMS_W		0x08	/* w direction/tilt available */
     99 #define UMS_ABS		0x10	/* absolute position, touchpanel */
    100 
    101 	int nbuttons;
    102 
    103 	u_int32_t sc_buttons;	/* mouse button status */
    104 	device_t sc_wsmousedev;
    105 
    106 	char			sc_dying;
    107 };
    108 
    109 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    110 
    111 Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
    112 
    113 Static int	ums_enable(void *);
    114 Static void	ums_disable(void *);
    115 Static int	ums_ioctl(void *, u_long, void *, int, struct lwp * );
    116 
    117 const struct wsmouse_accessops ums_accessops = {
    118 	ums_enable,
    119 	ums_ioctl,
    120 	ums_disable,
    121 };
    122 
    123 int ums_match(device_t, cfdata_t, void *);
    124 void ums_attach(device_t, device_t, void *);
    125 void ums_childdet(device_t, device_t);
    126 int ums_detach(device_t, int);
    127 int ums_activate(device_t, enum devact);
    128 extern struct cfdriver ums_cd;
    129 CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
    130     ums_detach, ums_activate, NULL, ums_childdet);
    131 
    132 int
    133 ums_match(device_t parent, cfdata_t match, void *aux)
    134 {
    135 	struct uhidev_attach_arg *uha = aux;
    136 	int size;
    137 	void *desc;
    138 
    139 	uhidev_get_report_desc(uha->parent, &desc, &size);
    140 	if (!hid_is_collection(desc, size, uha->reportid,
    141 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    142 		return (UMATCH_NONE);
    143 
    144 	return (UMATCH_IFACECLASS);
    145 }
    146 
    147 void
    148 ums_attach(device_t parent, device_t self, void *aux)
    149 {
    150 	struct ums_softc *sc = device_private(self);
    151 	struct uhidev_attach_arg *uha = aux;
    152 	struct wsmousedev_attach_args a;
    153 	int size;
    154 	void *desc;
    155 	u_int32_t flags, quirks;
    156 	int i, hl;
    157 	struct hid_location *zloc;
    158 	struct hid_location loc_btn;
    159 
    160 	aprint_naive("\n");
    161 
    162 	sc->sc_hdev.sc_dev = self;
    163 	sc->sc_hdev.sc_intr = ums_intr;
    164 	sc->sc_hdev.sc_parent = uha->parent;
    165 	sc->sc_hdev.sc_report_id = uha->reportid;
    166 
    167 	quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    168 	if (quirks & UQ_MS_REVZ)
    169 		sc->flags |= UMS_REVZ;
    170 	if (quirks & UQ_SPUR_BUT_UP)
    171 		sc->flags |= UMS_SPUR_BUT_UP;
    172 
    173 	uhidev_get_report_desc(uha->parent, &desc, &size);
    174 
    175 	if (!pmf_device_register(self, NULL, NULL))
    176 		aprint_error_dev(self, "couldn't establish power handler\n");
    177 
    178 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    179 	       uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
    180 		aprint_error("\n%s: mouse has no X report\n",
    181 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    182 		USB_ATTACH_ERROR_RETURN;
    183 	}
    184 	switch (flags & MOUSE_FLAGS_MASK) {
    185 	case 0:
    186 		sc->flags |= UMS_ABS;
    187 		break;
    188 	case HIO_RELATIVE:
    189 		break;
    190 	default:
    191 		aprint_error("\n%s: X report 0x%04x not supported\n",
    192 		       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    193 		USB_ATTACH_ERROR_RETURN;
    194 	}
    195 
    196 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    197 	       uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
    198 		aprint_error("\n%s: mouse has no Y report\n",
    199 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    200 		USB_ATTACH_ERROR_RETURN;
    201 	}
    202 	switch (flags & MOUSE_FLAGS_MASK) {
    203 	case 0:
    204 		sc->flags |= UMS_ABS;
    205 		break;
    206 	case HIO_RELATIVE:
    207 		break;
    208 	default:
    209 		aprint_error("\n%s: Y report 0x%04x not supported\n",
    210 		       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    211 		USB_ATTACH_ERROR_RETURN;
    212 	}
    213 
    214 	/* Try the wheel first as the Z activator since it's tradition. */
    215 	hl = hid_locate(desc,
    216 			size,
    217 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    218 			uha->reportid,
    219 			hid_input,
    220 			&sc->sc_loc_z,
    221 			&flags);
    222 
    223 	zloc = &sc->sc_loc_z;
    224 	if (hl) {
    225 		if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) {
    226 			aprint_verbose("\n%s: Wheel report 0x%04x not "
    227 			    "supported\n", USBDEVNAME(sc->sc_hdev.sc_dev),
    228 			    flags);
    229 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    230 		} else {
    231 			sc->flags |= UMS_Z;
    232 			/* Wheels need the Z axis reversed. */
    233 			sc->flags ^= UMS_REVZ;
    234 			/* Put Z on the W coordinate */
    235 			zloc = &sc->sc_loc_w;
    236 		}
    237 	}
    238 
    239 	hl = hid_locate(desc,
    240 			size,
    241 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    242 			uha->reportid,
    243 			hid_input,
    244 			zloc,
    245 			&flags);
    246 
    247 	/*
    248 	 * The horizontal component of the scrollball can also be given by
    249 	 * Application Control Pan in the Consumer page, so if we didnt see
    250 	 * any Z then check that.
    251 	 */
    252 	if (!hl) {
    253 		hl = hid_locate(desc,
    254 				size,
    255 				HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
    256 				uha->reportid,
    257 				hid_input,
    258 				zloc,
    259 				&flags);
    260 	}
    261 
    262 	if (hl) {
    263 		if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) {
    264 			aprint_verbose("\n%s: Z report 0x%04x not supported\n",
    265 			       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    266 			zloc->size = 0;	/* Bad Z coord, ignore it */
    267 		} else {
    268 			if (sc->flags & UMS_Z)
    269 				sc->flags |= UMS_W;
    270 			else
    271 				sc->flags |= UMS_Z;
    272 		}
    273 	}
    274 
    275 	/*
    276 	 * The Microsoft Wireless Laser Mouse 6000 v2.0 reports a bad
    277 	 * position for the wheel and wheel tilt controls -- should be
    278 	 * in bytes 3 & 4 of the report.  Fix this if necessary.
    279 	 */
    280 	if (uha->uaa->vendor == USB_VENDOR_MICROSOFT &&
    281 	    uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR) {
    282 		if ((sc->flags & UMS_Z) && sc->sc_loc_z.pos == 0)
    283 			sc->sc_loc_z.pos = 24;
    284 		if ((sc->flags & UMS_W) && sc->sc_loc_w.pos == 0)
    285 			sc->sc_loc_w.pos = sc->sc_loc_z.pos + 8;
    286 	}
    287 
    288 	/* figure out the number of buttons */
    289 	for (i = 1; i <= MAX_BUTTONS; i++)
    290 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    291 			uha->reportid, hid_input, &loc_btn, 0))
    292 			break;
    293 	sc->nbuttons = i - 1;
    294 
    295 	aprint_normal(": %d button%s%s%s%s\n",
    296 	    sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
    297 	    sc->flags & UMS_W ? ", W" : "",
    298 	    sc->flags & UMS_Z ? " and Z dir" : "",
    299 	    sc->flags & UMS_W ? "s" : "");
    300 
    301 	for (i = 1; i <= sc->nbuttons; i++)
    302 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    303 			   uha->reportid, hid_input,
    304 			   &sc->sc_loc_btn[i-1], 0);
    305 
    306 #ifdef USB_DEBUG
    307 	DPRINTF(("ums_attach: sc=%p\n", sc));
    308 	DPRINTF(("ums_attach: X\t%d/%d\n",
    309 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    310 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    311 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
    312 	if (sc->flags & UMS_Z)
    313 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    314 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    315 	if (sc->flags & UMS_W)
    316 		DPRINTF(("ums_attach: W\t%d/%d\n",
    317 			 sc->sc_loc_w.pos, sc->sc_loc_w.size));
    318 	for (i = 1; i <= sc->nbuttons; i++) {
    319 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    320 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    321 	}
    322 #endif
    323 
    324 	a.accessops = &ums_accessops;
    325 	a.accesscookie = sc;
    326 
    327 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    328 
    329 	USB_ATTACH_SUCCESS_RETURN;
    330 }
    331 
    332 int
    333 ums_activate(device_ptr_t self, enum devact act)
    334 {
    335 	struct ums_softc *sc = device_private(self);
    336 	int rv = 0;
    337 
    338 	switch (act) {
    339 	case DVACT_ACTIVATE:
    340 		return (EOPNOTSUPP);
    341 
    342 	case DVACT_DEACTIVATE:
    343 		if (sc->sc_wsmousedev != NULL)
    344 			rv = config_deactivate(sc->sc_wsmousedev);
    345 		sc->sc_dying = 1;
    346 		break;
    347 	}
    348 	return (rv);
    349 }
    350 
    351 void
    352 ums_childdet(device_t self, device_t child)
    353 {
    354 	struct ums_softc *sc = device_private(self);
    355 
    356 	KASSERT(sc->sc_wsmousedev == child);
    357 	sc->sc_wsmousedev = NULL;
    358 }
    359 
    360 int
    361 ums_detach(device_t self, int flags)
    362 {
    363 	struct ums_softc *sc = device_private(self);
    364 	int rv = 0;
    365 
    366 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    367 
    368 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    369 	if (sc->sc_wsmousedev != NULL)
    370 		rv = config_detach(sc->sc_wsmousedev, flags);
    371 
    372 	pmf_device_deregister(self);
    373 
    374 	return (rv);
    375 }
    376 
    377 void
    378 ums_intr(struct uhidev *addr, void *ibuf, u_int len)
    379 {
    380 	struct ums_softc *sc = (struct ums_softc *)addr;
    381 	int dx, dy, dz, dw;
    382 	u_int32_t buttons = 0;
    383 	int i, flags, s;
    384 
    385 	DPRINTFN(5,("ums_intr: len=%d\n", len));
    386 
    387 	flags = WSMOUSE_INPUT_DELTA;	/* equals 0 */
    388 
    389 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    390 	if (sc->flags & UMS_ABS) {
    391 		flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    392 		dy = hid_get_data(ibuf, &sc->sc_loc_y);
    393 	} else
    394 		dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    395 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    396 	dw =  hid_get_data(ibuf, &sc->sc_loc_w);
    397 
    398 	if (sc->flags & UMS_REVZ)
    399 		dz = -dz;
    400 	for (i = 0; i < sc->nbuttons; i++)
    401 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    402 			buttons |= (1 << UMS_BUT(i));
    403 
    404 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
    405 	    buttons != sc->sc_buttons) {
    406 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
    407 			dx, dy, dz, dw, buttons));
    408 		sc->sc_buttons = buttons;
    409 		if (sc->sc_wsmousedev != NULL) {
    410 			s = spltty();
    411 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
    412 			    dw, flags);
    413 			splx(s);
    414 		}
    415 	}
    416 }
    417 
    418 Static int
    419 ums_enable(void *v)
    420 {
    421 	struct ums_softc *sc = v;
    422 
    423 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    424 
    425 	if (sc->sc_dying)
    426 		return (EIO);
    427 
    428 	if (sc->sc_enabled)
    429 		return (EBUSY);
    430 
    431 	sc->sc_enabled = 1;
    432 	sc->sc_buttons = 0;
    433 
    434 	return (uhidev_open(&sc->sc_hdev));
    435 }
    436 
    437 Static void
    438 ums_disable(void *v)
    439 {
    440 	struct ums_softc *sc = v;
    441 
    442 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    443 #ifdef DIAGNOSTIC
    444 	if (!sc->sc_enabled) {
    445 		printf("ums_disable: not enabled\n");
    446 		return;
    447 	}
    448 #endif
    449 
    450 	sc->sc_enabled = 0;
    451 	uhidev_close(&sc->sc_hdev);
    452 }
    453 
    454 Static int
    455 ums_ioctl(void *v, u_long cmd, void *data, int flag,
    456     struct lwp * p)
    457 
    458 {
    459 	struct ums_softc *sc = v;
    460 
    461 	switch (cmd) {
    462 	case WSMOUSEIO_GTYPE:
    463 		if (sc->flags & UMS_ABS)
    464 			*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    465 		else
    466 			*(u_int *)data = WSMOUSE_TYPE_USB;
    467 		return (0);
    468 	}
    469 
    470 	return (EPASSTHROUGH);
    471 }
    472