Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.67
      1 /*	$NetBSD: ums.c,v 1.67 2006/11/16 01:33:27 christos 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/devclass_docs/HID1_11.pdf
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.67 2006/11/16 01:33:27 christos Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 #include <sys/device.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/tty.h>
     54 #include <sys/file.h>
     55 #include <sys/select.h>
     56 #include <sys/proc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/poll.h>
     59 
     60 #include <dev/usb/usb.h>
     61 #include <dev/usb/usbhid.h>
     62 
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 #include <dev/usb/usbdevs.h>
     66 #include <dev/usb/usb_quirks.h>
     67 #include <dev/usb/uhidev.h>
     68 #include <dev/usb/hid.h>
     69 
     70 #include <dev/wscons/wsconsio.h>
     71 #include <dev/wscons/wsmousevar.h>
     72 
     73 #ifdef USB_DEBUG
     74 #define DPRINTF(x)	if (umsdebug) logprintf x
     75 #define DPRINTFN(n,x)	if (umsdebug>(n)) logprintf x
     76 int	umsdebug = 0;
     77 #else
     78 #define DPRINTF(x)
     79 #define DPRINTFN(n,x)
     80 #endif
     81 
     82 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
     83 
     84 #define UMSUNIT(s)	(minor(s))
     85 
     86 #define PS2LBUTMASK	x01
     87 #define PS2RBUTMASK	x02
     88 #define PS2MBUTMASK	x04
     89 #define PS2BUTMASK 0x0f
     90 
     91 #define MAX_BUTTONS	31	/* must not exceed size of sc_buttons */
     92 
     93 struct ums_softc {
     94 	struct uhidev sc_hdev;
     95 
     96 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_w;
     97 	struct hid_location sc_loc_btn[MAX_BUTTONS];
     98 
     99 	int sc_enabled;
    100 
    101 	int flags;		/* device configuration */
    102 #define UMS_Z		0x01	/* z direction available */
    103 #define UMS_SPUR_BUT_UP	0x02	/* spurious button up events */
    104 #define UMS_REVZ	0x04	/* Z-axis is reversed */
    105 
    106 	int nbuttons;
    107 
    108 	u_int32_t sc_buttons;	/* mouse button status */
    109 	struct device *sc_wsmousedev;
    110 
    111 	char			sc_dying;
    112 };
    113 
    114 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    115 #define MOUSE_FLAGS (HIO_RELATIVE)
    116 
    117 Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
    118 
    119 Static int	ums_enable(void *);
    120 Static void	ums_disable(void *);
    121 Static int	ums_ioctl(void *, u_long, caddr_t, int, struct lwp * );
    122 
    123 const struct wsmouse_accessops ums_accessops = {
    124 	ums_enable,
    125 	ums_ioctl,
    126 	ums_disable,
    127 };
    128 
    129 USB_DECLARE_DRIVER(ums);
    130 
    131 int
    132 ums_match(struct device *parent, struct cfdata *match,
    133     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(struct device *parent, struct device *self, void *aux)
    149 {
    150 	struct ums_softc *sc = (struct ums_softc *)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, wheel;
    157 	struct hid_location loc_btn;
    158 
    159 	sc->sc_hdev.sc_intr = ums_intr;
    160 	sc->sc_hdev.sc_parent = uha->parent;
    161 	sc->sc_hdev.sc_report_id = uha->reportid;
    162 
    163 	quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    164 	if (quirks & UQ_MS_REVZ)
    165 		sc->flags |= UMS_REVZ;
    166 	if (quirks & UQ_SPUR_BUT_UP)
    167 		sc->flags |= UMS_SPUR_BUT_UP;
    168 
    169 	uhidev_get_report_desc(uha->parent, &desc, &size);
    170 
    171 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    172 	       uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
    173 		printf("\n%s: mouse has no X report\n",
    174 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    175 		USB_ATTACH_ERROR_RETURN;
    176 	}
    177 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    178 		printf("\n%s: X report 0x%04x not supported\n",
    179 		       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    180 		USB_ATTACH_ERROR_RETURN;
    181 	}
    182 
    183 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    184 	       uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
    185 		printf("\n%s: mouse has no Y report\n",
    186 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    187 		USB_ATTACH_ERROR_RETURN;
    188 	}
    189 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    190 		printf("\n%s: Y report 0x%04x not supported\n",
    191 		       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    192 		USB_ATTACH_ERROR_RETURN;
    193 	}
    194 
    195 	/* Try the wheel first as the Z activator since it's tradition. */
    196 	wheel = hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    197 						  HUG_WHEEL),
    198 			   uha->reportid, hid_input, &sc->sc_loc_z, &flags);
    199 	if (wheel) {
    200 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    201 			printf("\n%s: Wheel report 0x%04x not supported\n",
    202 			       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    203 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    204 		} else {
    205 			sc->flags |= UMS_Z;
    206 			/* Wheels need the Z axis reversed. */
    207 			sc->flags ^= UMS_REVZ;
    208 		}
    209 		/*
    210 		 * We might have both a wheel and Z direction, if so put
    211 		 * put the Z on the W coordinate.
    212 		 */
    213 		if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    214 						      HUG_Z),
    215 			uha->reportid, hid_input, &sc->sc_loc_w, &flags)) {
    216 			if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    217 				printf("\n%s: Z report 0x%04x not supported\n",
    218 				       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    219 				sc->sc_loc_w.size = 0;	/* Bad Z, ignore */
    220 			}
    221 		}
    222 	 } else if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    223 						      HUG_Z),
    224 		      uha->reportid, hid_input, &sc->sc_loc_z, &flags)) {
    225 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    226 			printf("\n%s: Z report 0x%04x not supported\n",
    227 			       USBDEVNAME(sc->sc_hdev.sc_dev), flags);
    228 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    229 		} else {
    230 			sc->flags |= UMS_Z;
    231 		}
    232 	}
    233 
    234 
    235 	/* figure out the number of buttons */
    236 	for (i = 1; i <= MAX_BUTTONS; i++)
    237 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    238 			uha->reportid, hid_input, &loc_btn, 0))
    239 			break;
    240 	sc->nbuttons = i - 1;
    241 
    242 	printf(": %d button%s%s\n",
    243 	    sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
    244 	    sc->flags & UMS_Z ? " and Z dir." : "");
    245 
    246 	for (i = 1; i <= sc->nbuttons; i++)
    247 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    248 			   uha->reportid, hid_input,
    249 			   &sc->sc_loc_btn[i-1], 0);
    250 
    251 #ifdef USB_DEBUG
    252 	DPRINTF(("ums_attach: sc=%p\n", sc));
    253 	DPRINTF(("ums_attach: X\t%d/%d\n",
    254 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    255 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    256 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
    257 	if (sc->flags & UMS_Z)
    258 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    259 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    260 	for (i = 1; i <= sc->nbuttons; i++) {
    261 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    262 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    263 	}
    264 #endif
    265 
    266 	a.accessops = &ums_accessops;
    267 	a.accesscookie = sc;
    268 
    269 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    270 
    271 	USB_ATTACH_SUCCESS_RETURN;
    272 }
    273 
    274 int
    275 ums_activate(device_ptr_t self, enum devact act)
    276 {
    277 	struct ums_softc *sc = (struct ums_softc *)self;
    278 	int rv = 0;
    279 
    280 	switch (act) {
    281 	case DVACT_ACTIVATE:
    282 		return (EOPNOTSUPP);
    283 
    284 	case DVACT_DEACTIVATE:
    285 		if (sc->sc_wsmousedev != NULL)
    286 			rv = config_deactivate(sc->sc_wsmousedev);
    287 		sc->sc_dying = 1;
    288 		break;
    289 	}
    290 	return (rv);
    291 }
    292 
    293 int
    294 ums_detach(struct device *self, int flags)
    295 {
    296 	struct ums_softc *sc = (struct ums_softc *)self;
    297 	int rv = 0;
    298 
    299 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    300 
    301 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    302 	if (sc->sc_wsmousedev != NULL)
    303 		rv = config_detach(sc->sc_wsmousedev, flags);
    304 
    305 	return (rv);
    306 }
    307 
    308 void
    309 ums_intr(struct uhidev *addr, void *ibuf, u_int len)
    310 {
    311 	struct ums_softc *sc = (struct ums_softc *)addr;
    312 	int dx, dy, dz, dw;
    313 	u_int32_t buttons = 0;
    314 	int i;
    315 	int s;
    316 
    317 	DPRINTFN(5,("ums_intr: len=%d\n", len));
    318 
    319 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    320 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    321 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    322 	dw =  hid_get_data(ibuf, &sc->sc_loc_w);
    323 	if (sc->flags & UMS_REVZ)
    324 		dz = -dz;
    325 	for (i = 0; i < sc->nbuttons; i++)
    326 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    327 			buttons |= (1 << UMS_BUT(i));
    328 
    329 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
    330 	    buttons != sc->sc_buttons) {
    331 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
    332 			dx, dy, dz, dw, buttons));
    333 		sc->sc_buttons = buttons;
    334 		if (sc->sc_wsmousedev != NULL) {
    335 			s = spltty();
    336 			wsmouse_input(sc->sc_wsmousedev,
    337 					buttons,
    338 					dx, dy, dz, dw,
    339 					WSMOUSE_INPUT_DELTA);
    340 			splx(s);
    341 		}
    342 	}
    343 }
    344 
    345 Static int
    346 ums_enable(void *v)
    347 {
    348 	struct ums_softc *sc = v;
    349 
    350 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    351 
    352 	if (sc->sc_dying)
    353 		return (EIO);
    354 
    355 	if (sc->sc_enabled)
    356 		return (EBUSY);
    357 
    358 	sc->sc_enabled = 1;
    359 	sc->sc_buttons = 0;
    360 
    361 	return (uhidev_open(&sc->sc_hdev));
    362 }
    363 
    364 Static void
    365 ums_disable(void *v)
    366 {
    367 	struct ums_softc *sc = v;
    368 
    369 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    370 #ifdef DIAGNOSTIC
    371 	if (!sc->sc_enabled) {
    372 		printf("ums_disable: not enabled\n");
    373 		return;
    374 	}
    375 #endif
    376 
    377 	sc->sc_enabled = 0;
    378 	uhidev_close(&sc->sc_hdev);
    379 }
    380 
    381 Static int
    382 ums_ioctl(void *v, u_long cmd, caddr_t data, int flag,
    383     struct lwp * p)
    384 
    385 {
    386 	switch (cmd) {
    387 	case WSMOUSEIO_GTYPE:
    388 		*(u_int *)data = WSMOUSE_TYPE_USB;
    389 		return (0);
    390 	}
    391 
    392 	return (EPASSTHROUGH);
    393 }
    394