Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.32
      1 /*	$NetBSD: ums.c,v 1.32 1999/09/12 08:21:49 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 int
    321 ums_detach(self, flags)
    322 	device_ptr_t self;
    323 	int flags;
    324 {
    325 	struct ums_softc *sc = (struct ums_softc *)self;
    326 	int rv = 0;
    327 
    328 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    329 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    330 	if (sc->sc_wsmousedev)
    331 		rv = config_detach(sc->sc_wsmousedev, flags);
    332 	if (rv == 0) {
    333 		free(sc->sc_loc_btn, M_USBDEV);
    334 		free(sc->sc_ibuf, M_USBDEV);
    335 	}
    336 	return (rv);
    337 }
    338 
    339 void
    340 ums_intr(reqh, addr, status)
    341 	usbd_request_handle reqh;
    342 	usbd_private_handle addr;
    343 	usbd_status status;
    344 {
    345 	struct ums_softc *sc = addr;
    346 	u_char *ibuf;
    347 	int dx, dy, dz;
    348 	u_int32_t buttons = 0;
    349 	int i;
    350 	int s;
    351 
    352 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    353 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    354 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    355 
    356 	if (status == USBD_CANCELLED)
    357 		return;
    358 
    359 	if (status != USBD_NORMAL_COMPLETION) {
    360 		DPRINTF(("ums_intr: status=%d\n", status));
    361 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    362 		return;
    363 	}
    364 
    365 	ibuf = sc->sc_ibuf;
    366 	if (sc->sc_iid) {
    367 		if (*ibuf++ != sc->sc_iid)
    368 			return;
    369 	}
    370 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    371 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    372 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    373 	if (sc->revz)
    374 		dz = -dz;
    375 	for (i = 0; i < sc->nbuttons; i++)
    376 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    377 			buttons |= (1 << UMS_BUT(i));
    378 
    379 	if (dx || dy || dz || buttons != sc->sc_buttons) {
    380 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    381 			dx, dy, dz, buttons));
    382 		sc->sc_buttons = buttons;
    383 		if (sc->sc_wsmousedev) {
    384 			s = spltty();
    385 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
    386 			splx(s);
    387 		}
    388 	}
    389 }
    390 
    391 static int
    392 ums_enable(v)
    393 	void *v;
    394 {
    395 	struct ums_softc *sc = v;
    396 
    397 	usbd_status r;
    398 
    399 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    400 
    401 	if (sc->sc_dying)
    402 		return (EIO);
    403 
    404 	if (sc->sc_enabled)
    405 		return (EBUSY);
    406 
    407 	sc->sc_enabled = 1;
    408 	sc->sc_buttons = 0;
    409 
    410 	/* Set up interrupt pipe. */
    411 	r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    412 				USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    413 				sc->sc_ibuf, sc->sc_isize, ums_intr);
    414 	if (r != USBD_NORMAL_COMPLETION) {
    415 		DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
    416 			 r));
    417 		sc->sc_enabled = 0;
    418 		return (EIO);
    419 	}
    420 	return (0);
    421 }
    422 
    423 static void
    424 ums_disable(v)
    425 	void *v;
    426 {
    427 	struct ums_softc *sc = v;
    428 
    429 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    430 #ifdef DIAGNOSTIC
    431 	if (!sc->sc_enabled) {
    432 		printf("ums_disable: not enabled\n");
    433 		return;
    434 	}
    435 #endif
    436 
    437 	/* Disable interrupts. */
    438 	usbd_abort_pipe(sc->sc_intrpipe);
    439 	usbd_close_pipe(sc->sc_intrpipe);
    440 
    441 	sc->sc_enabled = 0;
    442 }
    443 
    444 static int
    445 ums_ioctl(v, cmd, data, flag, p)
    446 	void *v;
    447 	u_long cmd;
    448 	caddr_t data;
    449 	int flag;
    450 	struct proc *p;
    451 
    452 {
    453 	switch (cmd) {
    454 	case WSMOUSEIO_GTYPE:
    455 		*(u_int *)data = WSMOUSE_TYPE_USB;
    456 		return (0);
    457 	}
    458 
    459 	return (-1);
    460 }
    461 
    462