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