Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.33
      1 /*	$NetBSD: ums.c,v 1.33 1999/10/13 08:10:57 augustss 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 (augustss (at) carlstedt.se) 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/data/usbhid10.pdf
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 #include <sys/device.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/tty.h>
     51 #include <sys/file.h>
     52 #include <sys/select.h>
     53 #include <sys/proc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/poll.h>
     56 
     57 #include <dev/usb/usb.h>
     58 #include <dev/usb/usbhid.h>
     59 
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdi_util.h>
     62 #include <dev/usb/usbdevs.h>
     63 #include <dev/usb/usb_quirks.h>
     64 #include <dev/usb/hid.h>
     65 
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wscons/wsmousevar.h>
     68 
     69 #ifdef USB_DEBUG
     70 #define DPRINTF(x)	if (umsdebug) logprintf x
     71 #define DPRINTFN(n,x)	if (umsdebug>(n)) logprintf x
     72 int	umsdebug = 0;
     73 #else
     74 #define DPRINTF(x)
     75 #define DPRINTFN(n,x)
     76 #endif
     77 
     78 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
     79 
     80 #define UMSUNIT(s)	(minor(s))
     81 
     82 #define PS2LBUTMASK	x01
     83 #define PS2RBUTMASK	x02
     84 #define PS2MBUTMASK	x04
     85 #define PS2BUTMASK 0x0f
     86 
     87 struct ums_softc {
     88 	USBBASEDEVICE sc_dev;		/* base device */
     89 	usbd_interface_handle sc_iface;	/* interface */
     90 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     91 	int sc_ep_addr;
     92 
     93 	u_char *sc_ibuf;
     94 	u_int8_t sc_iid;
     95 	int sc_isize;
     96 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
     97 	struct hid_location *sc_loc_btn;
     98 
     99 	int sc_enabled;
    100 
    101 	int flags;		/* device configuration */
    102 #define UMS_Z		0x01	/* z direction available */
    103 	int nbuttons;
    104 #define MAX_BUTTONS	31	/* chosen because sc_buttons is u_int32_t */
    105 
    106 	char revz;		/* Z-axis is reversed */
    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 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    118 
    119 static int	ums_enable __P((void *));
    120 static void	ums_disable __P((void *));
    121 static int	ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    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 USB_MATCH(ums)
    132 {
    133 	USB_MATCH_START(ums, uaa);
    134 	usb_interface_descriptor_t *id;
    135 	int size, ret;
    136 	void *desc;
    137 	usbd_status r;
    138 
    139 	if (!uaa->iface)
    140 		return (UMATCH_NONE);
    141 	id = usbd_get_interface_descriptor(uaa->iface);
    142 	if (!id || id->bInterfaceClass != UCLASS_HID)
    143 		return (UMATCH_NONE);
    144 
    145 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    146 	if (r != USBD_NORMAL_COMPLETION)
    147 		return (UMATCH_NONE);
    148 
    149 	if (hid_is_collection(desc, size,
    150 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    151 		ret = UMATCH_IFACECLASS;
    152 	else
    153 		ret = UMATCH_NONE;
    154 
    155 	free(desc, M_TEMP);
    156 	return (ret);
    157 }
    158 
    159 USB_ATTACH(ums)
    160 {
    161 	USB_ATTACH_START(ums, sc, uaa);
    162 	usbd_interface_handle iface = uaa->iface;
    163 	usb_interface_descriptor_t *id;
    164 	usb_endpoint_descriptor_t *ed;
    165 	struct wsmousedev_attach_args a;
    166 	int size;
    167 	void *desc;
    168 	usbd_status r;
    169 	char devinfo[1024];
    170 	u_int32_t flags;
    171 	int i;
    172 	struct hid_location loc_btn;
    173 
    174 	sc->sc_iface = iface;
    175 	id = usbd_get_interface_descriptor(iface);
    176 	usbd_devinfo(uaa->device, 0, devinfo);
    177 	USB_ATTACH_SETUP;
    178 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    179 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    180 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    181 	if (!ed) {
    182 		printf("%s: could not read endpoint descriptor\n",
    183 		       USBDEVNAME(sc->sc_dev));
    184 		USB_ATTACH_ERROR_RETURN;
    185 	}
    186 
    187 	DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
    188 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    189 		     " bInterval=%d\n",
    190 		     ed->bLength, ed->bDescriptorType,
    191 		     ed->bEndpointAddress & UE_ADDR,
    192 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    193 		     ed->bmAttributes & UE_XFERTYPE,
    194 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    195 
    196 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    197 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    198 		printf("%s: unexpected endpoint\n",
    199 		       USBDEVNAME(sc->sc_dev));
    200 		USB_ATTACH_ERROR_RETURN;
    201 	}
    202 
    203 	sc->revz = (usbd_get_quirks(uaa->device)->uq_flags & UQ_MS_REVZ) != 0;
    204 
    205 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    206 	if (r != USBD_NORMAL_COMPLETION)
    207 		USB_ATTACH_ERROR_RETURN;
    208 
    209 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    210 		       hid_input, &sc->sc_loc_x, &flags)) {
    211 		printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
    212 		USB_ATTACH_ERROR_RETURN;
    213 	}
    214 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    215 		printf("%s: X report 0x%04x not supported\n",
    216 		       USBDEVNAME(sc->sc_dev), flags);
    217 		USB_ATTACH_ERROR_RETURN;
    218 	}
    219 
    220 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    221 		       hid_input, &sc->sc_loc_y, &flags)) {
    222 		printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
    223 		USB_ATTACH_ERROR_RETURN;
    224 	}
    225 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    226 		printf("%s: Y report 0x%04x not supported\n",
    227 		       USBDEVNAME(sc->sc_dev), flags);
    228 		USB_ATTACH_ERROR_RETURN;
    229 	}
    230 
    231 	/* Try to guess the Z activator: first check Z, then WHEEL. */
    232 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    233 		       hid_input, &sc->sc_loc_z, &flags) ||
    234 	    hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    235 		       hid_input, &sc->sc_loc_z, &flags)) {
    236 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    237 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    238 		} else {
    239 			sc->flags |= UMS_Z;
    240 		}
    241 	}
    242 
    243 	/* figure out the number of buttons */
    244 	for (i = 1; i <= MAX_BUTTONS; i++)
    245 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    246 				hid_input, &loc_btn, 0))
    247 			break;
    248 	sc->nbuttons = i - 1;
    249 	sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
    250 				M_USBDEV, M_NOWAIT);
    251 	if (!sc->sc_loc_btn) {
    252 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    253 		USB_ATTACH_ERROR_RETURN;
    254 	}
    255 
    256 	printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
    257 	       sc->nbuttons, sc->flags & UMS_Z ? " and Z dir." : "");
    258 
    259 	for (i = 1; i <= sc->nbuttons; i++)
    260 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    261 				hid_input, &sc->sc_loc_btn[i-1], 0);
    262 
    263 	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
    264 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
    265 	if (!sc->sc_ibuf) {
    266 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    267 		free(sc->sc_loc_btn, M_USBDEV);
    268 		USB_ATTACH_ERROR_RETURN;
    269 	}
    270 
    271 	sc->sc_ep_addr = ed->bEndpointAddress;
    272 	free(desc, M_TEMP);
    273 
    274 #ifdef USB_DEBUG
    275 	DPRINTF(("ums_attach: sc=%p\n", sc));
    276 	DPRINTF(("ums_attach: X\t%d/%d\n",
    277 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    278 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    279 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    280 	if (sc->flags & UMS_Z)
    281 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    282 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    283 	for (i = 1; i <= sc->nbuttons; i++) {
    284 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    285 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    286 	}
    287 	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
    288 #endif
    289 
    290 	a.accessops = &ums_accessops;
    291 	a.accesscookie = sc;
    292 
    293 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    294 
    295 	USB_ATTACH_SUCCESS_RETURN;
    296 }
    297 
    298 int
    299 ums_activate(self, act)
    300 	device_ptr_t self;
    301 	enum devact act;
    302 {
    303 	struct ums_softc *sc = (struct ums_softc *)self;
    304 	int rv = 0;
    305 
    306 	switch (act) {
    307 	case DVACT_ACTIVATE:
    308 		return (EOPNOTSUPP);
    309 		break;
    310 
    311 	case DVACT_DEACTIVATE:
    312 		if (sc->sc_wsmousedev)
    313 			rv = config_deactivate(sc->sc_wsmousedev);
    314 		sc->sc_dying = 1;
    315 		break;
    316 	}
    317 	return (rv);
    318 }
    319 
    320 USB_DETACH(ums)
    321 {
    322 	USB_DETACH_START(ums, sc);
    323 	int rv = 0;
    324 
    325 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    326 
    327 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    328 	if (sc->sc_wsmousedev)
    329 		rv = config_detach(sc->sc_wsmousedev, flags);
    330 	if (rv == 0) {
    331 		free(sc->sc_loc_btn, M_USBDEV);
    332 		free(sc->sc_ibuf, M_USBDEV);
    333 	}
    334 	return (rv);
    335 }
    336 
    337 void
    338 ums_intr(reqh, addr, status)
    339 	usbd_request_handle reqh;
    340 	usbd_private_handle addr;
    341 	usbd_status status;
    342 {
    343 	struct ums_softc *sc = addr;
    344 	u_char *ibuf;
    345 	int dx, dy, dz;
    346 	u_int32_t buttons = 0;
    347 	int i;
    348 	int s;
    349 
    350 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    351 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    352 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    353 
    354 	if (status == USBD_CANCELLED)
    355 		return;
    356 
    357 	if (status != USBD_NORMAL_COMPLETION) {
    358 		DPRINTF(("ums_intr: status=%d\n", status));
    359 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    360 		return;
    361 	}
    362 
    363 	ibuf = sc->sc_ibuf;
    364 	if (sc->sc_iid) {
    365 		if (*ibuf++ != sc->sc_iid)
    366 			return;
    367 	}
    368 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    369 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    370 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    371 	if (sc->revz)
    372 		dz = -dz;
    373 	for (i = 0; i < sc->nbuttons; i++)
    374 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    375 			buttons |= (1 << UMS_BUT(i));
    376 
    377 	if (dx || dy || dz || buttons != sc->sc_buttons) {
    378 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    379 			dx, dy, dz, buttons));
    380 		sc->sc_buttons = buttons;
    381 		if (sc->sc_wsmousedev) {
    382 			s = spltty();
    383 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
    384 			splx(s);
    385 		}
    386 	}
    387 }
    388 
    389 static int
    390 ums_enable(v)
    391 	void *v;
    392 {
    393 	struct ums_softc *sc = v;
    394 
    395 	usbd_status r;
    396 
    397 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    398 
    399 	if (sc->sc_dying)
    400 		return (EIO);
    401 
    402 	if (sc->sc_enabled)
    403 		return (EBUSY);
    404 
    405 	sc->sc_enabled = 1;
    406 	sc->sc_buttons = 0;
    407 
    408 	/* Set up interrupt pipe. */
    409 	r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    410 				USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    411 				sc->sc_ibuf, sc->sc_isize, ums_intr);
    412 	if (r != USBD_NORMAL_COMPLETION) {
    413 		DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
    414 			 r));
    415 		sc->sc_enabled = 0;
    416 		return (EIO);
    417 	}
    418 	return (0);
    419 }
    420 
    421 static void
    422 ums_disable(v)
    423 	void *v;
    424 {
    425 	struct ums_softc *sc = v;
    426 
    427 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    428 #ifdef DIAGNOSTIC
    429 	if (!sc->sc_enabled) {
    430 		printf("ums_disable: not enabled\n");
    431 		return;
    432 	}
    433 #endif
    434 
    435 	/* Disable interrupts. */
    436 	usbd_abort_pipe(sc->sc_intrpipe);
    437 	usbd_close_pipe(sc->sc_intrpipe);
    438 
    439 	sc->sc_enabled = 0;
    440 }
    441 
    442 static int
    443 ums_ioctl(v, cmd, data, flag, p)
    444 	void *v;
    445 	u_long cmd;
    446 	caddr_t data;
    447 	int flag;
    448 	struct proc *p;
    449 
    450 {
    451 	switch (cmd) {
    452 	case WSMOUSEIO_GTYPE:
    453 		*(u_int *)data = WSMOUSE_TYPE_USB;
    454 		return (0);
    455 	}
    456 
    457 	return (-1);
    458 }
    459 
    460