Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.73.12.1
      1 /*	$NetBSD: ums.c,v 1.73.12.1 2009/05/13 17:21:35 jym 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.12.1 2009/05/13 17:21:35 jym 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 
     98 	int nbuttons;
     99 
    100 	u_int32_t sc_buttons;	/* mouse button status */
    101 	device_t sc_wsmousedev;
    102 
    103 	char			sc_dying;
    104 };
    105 
    106 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    107 #define MOUSE_FLAGS (HIO_RELATIVE)
    108 
    109 Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
    110 
    111 Static int	ums_enable(void *);
    112 Static void	ums_disable(void *);
    113 Static int	ums_ioctl(void *, u_long, void *, int, struct lwp * );
    114 
    115 const struct wsmouse_accessops ums_accessops = {
    116 	ums_enable,
    117 	ums_ioctl,
    118 	ums_disable,
    119 };
    120 
    121 int ums_match(device_t, cfdata_t, void *);
    122 void ums_attach(device_t, device_t, void *);
    123 void ums_childdet(device_t, device_t);
    124 int ums_detach(device_t, int);
    125 int ums_activate(device_t, enum devact);
    126 extern struct cfdriver ums_cd;
    127 CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
    128     ums_detach, ums_activate, NULL, ums_childdet);
    129 
    130 int
    131 ums_match(device_t parent, cfdata_t match, void *aux)
    132 {
    133 	struct uhidev_attach_arg *uha = aux;
    134 	int size;
    135 	void *desc;
    136 
    137 	uhidev_get_report_desc(uha->parent, &desc, &size);
    138 	if (!hid_is_collection(desc, size, uha->reportid,
    139 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    140 		return (UMATCH_NONE);
    141 
    142 	return (UMATCH_IFACECLASS);
    143 }
    144 
    145 void
    146 ums_attach(device_t parent, device_t self, void *aux)
    147 {
    148 	struct ums_softc *sc = device_private(self);
    149 	struct uhidev_attach_arg *uha = aux;
    150 	struct wsmousedev_attach_args a;
    151 	int size;
    152 	void *desc;
    153 	u_int32_t flags, quirks;
    154 	int i, wheel;
    155 	struct hid_location loc_btn;
    156 
    157 	aprint_naive("\n");
    158 
    159 	sc->sc_hdev.sc_dev = self;
    160 	sc->sc_hdev.sc_intr = ums_intr;
    161 	sc->sc_hdev.sc_parent = uha->parent;
    162 	sc->sc_hdev.sc_report_id = uha->reportid;
    163 
    164 	quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    165 	if (quirks & UQ_MS_REVZ)
    166 		sc->flags |= UMS_REVZ;
    167 	if (quirks & UQ_SPUR_BUT_UP)
    168 		sc->flags |= UMS_SPUR_BUT_UP;
    169 
    170 	uhidev_get_report_desc(uha->parent, &desc, &size);
    171 
    172 	if (!pmf_device_register(self, NULL, NULL))
    173 		aprint_error_dev(self, "couldn't establish power handler\n");
    174 
    175 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    176 	       uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
    177 		aprint_error("\n%s: mouse has no X report\n",
    178 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    179 		USB_ATTACH_ERROR_RETURN;
    180 	}
    181 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    182 		aprint_error("\n%s: X report 0x%04x not supported\n",
    183 		       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    184 		USB_ATTACH_ERROR_RETURN;
    185 	}
    186 
    187 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    188 	       uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
    189 		aprint_error("\n%s: mouse has no Y report\n",
    190 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    191 		USB_ATTACH_ERROR_RETURN;
    192 	}
    193 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    194 		aprint_error("\n%s: Y report 0x%04x not supported\n",
    195 		       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    196 		USB_ATTACH_ERROR_RETURN;
    197 	}
    198 
    199 	/* Try the wheel first as the Z activator since it's tradition. */
    200 	wheel = hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    201 						  HUG_WHEEL),
    202 			   uha->reportid, hid_input, &sc->sc_loc_z, &flags);
    203 	if (wheel) {
    204 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    205 			aprint_verbose("\n%s: Wheel report 0x%04x not "
    206 			    "supported\n", USBDEVNAME(sc->sc_hdev.sc_dev),
    207 			    flags);
    208 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    209 		} else {
    210 			sc->flags |= UMS_Z;
    211 			/* Wheels need the Z axis reversed. */
    212 			sc->flags ^= UMS_REVZ;
    213 		}
    214 		/*
    215 		 * We might have both a wheel and Z direction, if so put
    216 		 * put the Z on the W coordinate.
    217 		 */
    218 		if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    219 						      HUG_Z),
    220 			uha->reportid, hid_input, &sc->sc_loc_w, &flags)) {
    221 			if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    222 				aprint_verbose("\n%s: Z report 0x%04x not "
    223 				    "supported\n",
    224 				       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    225 				sc->sc_loc_w.size = 0;	/* Bad Z, ignore */
    226 			}
    227 		}
    228 	 } else if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    229 						      HUG_Z),
    230 		      uha->reportid, hid_input, &sc->sc_loc_z, &flags)) {
    231 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    232 			aprint_verbose("\n%s: Z report 0x%04x not supported\n",
    233 			       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    234 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    235 		} else {
    236 			sc->flags |= UMS_Z;
    237 		}
    238 	}
    239 
    240 
    241 	/* figure out the number of buttons */
    242 	for (i = 1; i <= MAX_BUTTONS; i++)
    243 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    244 			uha->reportid, hid_input, &loc_btn, 0))
    245 			break;
    246 	sc->nbuttons = i - 1;
    247 
    248 	aprint_normal(": %d button%s%s\n",
    249 	    sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
    250 	    sc->flags & UMS_Z ? " and Z dir." : "");
    251 
    252 	for (i = 1; i <= sc->nbuttons; i++)
    253 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    254 			   uha->reportid, hid_input,
    255 			   &sc->sc_loc_btn[i-1], 0);
    256 
    257 #ifdef USB_DEBUG
    258 	DPRINTF(("ums_attach: sc=%p\n", sc));
    259 	DPRINTF(("ums_attach: X\t%d/%d\n",
    260 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    261 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    262 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
    263 	if (sc->flags & UMS_Z)
    264 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    265 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    266 	for (i = 1; i <= sc->nbuttons; i++) {
    267 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    268 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    269 	}
    270 #endif
    271 
    272 	a.accessops = &ums_accessops;
    273 	a.accesscookie = sc;
    274 
    275 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    276 
    277 	USB_ATTACH_SUCCESS_RETURN;
    278 }
    279 
    280 int
    281 ums_activate(device_ptr_t self, enum devact act)
    282 {
    283 	struct ums_softc *sc = device_private(self);
    284 	int rv = 0;
    285 
    286 	switch (act) {
    287 	case DVACT_ACTIVATE:
    288 		return (EOPNOTSUPP);
    289 
    290 	case DVACT_DEACTIVATE:
    291 		if (sc->sc_wsmousedev != NULL)
    292 			rv = config_deactivate(sc->sc_wsmousedev);
    293 		sc->sc_dying = 1;
    294 		break;
    295 	}
    296 	return (rv);
    297 }
    298 
    299 void
    300 ums_childdet(device_t self, device_t child)
    301 {
    302 	struct ums_softc *sc = device_private(self);
    303 
    304 	KASSERT(sc->sc_wsmousedev == child);
    305 	sc->sc_wsmousedev = NULL;
    306 }
    307 
    308 int
    309 ums_detach(device_t self, int flags)
    310 {
    311 	struct ums_softc *sc = device_private(self);
    312 	int rv = 0;
    313 
    314 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    315 
    316 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    317 	if (sc->sc_wsmousedev != NULL)
    318 		rv = config_detach(sc->sc_wsmousedev, flags);
    319 
    320 	pmf_device_deregister(self);
    321 
    322 	return (rv);
    323 }
    324 
    325 void
    326 ums_intr(struct uhidev *addr, void *ibuf, u_int len)
    327 {
    328 	struct ums_softc *sc = (struct ums_softc *)addr;
    329 	int dx, dy, dz, dw;
    330 	u_int32_t buttons = 0;
    331 	int i;
    332 	int s;
    333 
    334 	DPRINTFN(5,("ums_intr: len=%d\n", len));
    335 
    336 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    337 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    338 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    339 	dw =  hid_get_data(ibuf, &sc->sc_loc_w);
    340 	if (sc->flags & UMS_REVZ)
    341 		dz = -dz;
    342 	for (i = 0; i < sc->nbuttons; i++)
    343 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    344 			buttons |= (1 << UMS_BUT(i));
    345 
    346 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
    347 	    buttons != sc->sc_buttons) {
    348 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
    349 			dx, dy, dz, dw, buttons));
    350 		sc->sc_buttons = buttons;
    351 		if (sc->sc_wsmousedev != NULL) {
    352 			s = spltty();
    353 			wsmouse_input(sc->sc_wsmousedev,
    354 					buttons,
    355 					dx, dy, dz, dw,
    356 					WSMOUSE_INPUT_DELTA);
    357 			splx(s);
    358 		}
    359 	}
    360 }
    361 
    362 Static int
    363 ums_enable(void *v)
    364 {
    365 	struct ums_softc *sc = v;
    366 
    367 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    368 
    369 	if (sc->sc_dying)
    370 		return (EIO);
    371 
    372 	if (sc->sc_enabled)
    373 		return (EBUSY);
    374 
    375 	sc->sc_enabled = 1;
    376 	sc->sc_buttons = 0;
    377 
    378 	return (uhidev_open(&sc->sc_hdev));
    379 }
    380 
    381 Static void
    382 ums_disable(void *v)
    383 {
    384 	struct ums_softc *sc = v;
    385 
    386 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    387 #ifdef DIAGNOSTIC
    388 	if (!sc->sc_enabled) {
    389 		printf("ums_disable: not enabled\n");
    390 		return;
    391 	}
    392 #endif
    393 
    394 	sc->sc_enabled = 0;
    395 	uhidev_close(&sc->sc_hdev);
    396 }
    397 
    398 Static int
    399 ums_ioctl(void *v, u_long cmd, void *data, int flag,
    400     struct lwp * p)
    401 
    402 {
    403 	switch (cmd) {
    404 	case WSMOUSEIO_GTYPE:
    405 		*(u_int *)data = WSMOUSE_TYPE_USB;
    406 		return (0);
    407 	}
    408 
    409 	return (EPASSTHROUGH);
    410 }
    411