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