102ac6454SAndrew Thompson /*- 202ac6454SAndrew Thompson * Copyright (c) 1998 The NetBSD Foundation, Inc. 302ac6454SAndrew Thompson * All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation 602ac6454SAndrew Thompson * by Lennart Augustsson (lennart@augustsson.net) at 702ac6454SAndrew Thompson * Carlstedt Research & Technology. 802ac6454SAndrew Thompson * 902ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 1002ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 1102ac6454SAndrew Thompson * are met: 1202ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1402ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1502ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1602ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1702ac6454SAndrew Thompson * 1802ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1902ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2002ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2102ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2202ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2302ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2402ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2502ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2602ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2702ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2802ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE. 2902ac6454SAndrew Thompson */ 3002ac6454SAndrew Thompson 3102ac6454SAndrew Thompson #include <sys/cdefs.h> 3202ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 3302ac6454SAndrew Thompson 3402ac6454SAndrew Thompson /* 3502ac6454SAndrew Thompson * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 3602ac6454SAndrew Thompson */ 3702ac6454SAndrew Thompson 38ed6d949aSAndrew Thompson #include <sys/stdint.h> 39ed6d949aSAndrew Thompson #include <sys/stddef.h> 40ed6d949aSAndrew Thompson #include <sys/param.h> 41ed6d949aSAndrew Thompson #include <sys/queue.h> 42ed6d949aSAndrew Thompson #include <sys/types.h> 43ed6d949aSAndrew Thompson #include <sys/systm.h> 44ed6d949aSAndrew Thompson #include <sys/kernel.h> 45ed6d949aSAndrew Thompson #include <sys/bus.h> 46ed6d949aSAndrew Thompson #include <sys/module.h> 47ed6d949aSAndrew Thompson #include <sys/lock.h> 48ed6d949aSAndrew Thompson #include <sys/mutex.h> 49ed6d949aSAndrew Thompson #include <sys/condvar.h> 50ed6d949aSAndrew Thompson #include <sys/sysctl.h> 51ed6d949aSAndrew Thompson #include <sys/sx.h> 52ed6d949aSAndrew Thompson #include <sys/unistd.h> 53ed6d949aSAndrew Thompson #include <sys/callout.h> 54ed6d949aSAndrew Thompson #include <sys/malloc.h> 55ed6d949aSAndrew Thompson #include <sys/priv.h> 56ed6d949aSAndrew Thompson #include <sys/conf.h> 57ed6d949aSAndrew Thompson #include <sys/fcntl.h> 584ed0304cSAndrew Thompson #include <sys/sbuf.h> 59ed6d949aSAndrew Thompson 6002ac6454SAndrew Thompson #include <dev/usb/usb.h> 61ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 62ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 6302ac6454SAndrew Thompson #include <dev/usb/usbhid.h> 64ed6d949aSAndrew Thompson #include "usbdevs.h" 6502ac6454SAndrew Thompson 6602ac6454SAndrew Thompson #define USB_DEBUG_VAR ums_debug 6702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6802ac6454SAndrew Thompson 6902ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 7002ac6454SAndrew Thompson 7102ac6454SAndrew Thompson #include <sys/ioccom.h> 7202ac6454SAndrew Thompson #include <sys/filio.h> 7302ac6454SAndrew Thompson #include <sys/tty.h> 7402ac6454SAndrew Thompson #include <sys/mouse.h> 7502ac6454SAndrew Thompson 76b850ecc1SAndrew Thompson #ifdef USB_DEBUG 7702ac6454SAndrew Thompson static int ums_debug = 0; 7802ac6454SAndrew Thompson 799360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums"); 809360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW, 8102ac6454SAndrew Thompson &ums_debug, 0, "Debug level"); 8202ac6454SAndrew Thompson #endif 8302ac6454SAndrew Thompson 8402ac6454SAndrew Thompson #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE) 8502ac6454SAndrew Thompson #define MOUSE_FLAGS (HIO_RELATIVE) 8602ac6454SAndrew Thompson 8702ac6454SAndrew Thompson #define UMS_BUF_SIZE 8 /* bytes */ 8802ac6454SAndrew Thompson #define UMS_IFQ_MAXLEN 50 /* units */ 8902ac6454SAndrew Thompson #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */ 9002ac6454SAndrew Thompson #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i)) 91de967835SAndrew Thompson #define UMS_INFO_MAX 2 /* maximum number of HID sets */ 9202ac6454SAndrew Thompson 9302ac6454SAndrew Thompson enum { 9402ac6454SAndrew Thompson UMS_INTR_DT, 953e5e312aSAndrew Thompson UMS_N_TRANSFER, 9602ac6454SAndrew Thompson }; 9702ac6454SAndrew Thompson 98de967835SAndrew Thompson struct ums_info { 9902ac6454SAndrew Thompson struct hid_location sc_loc_w; 10002ac6454SAndrew Thompson struct hid_location sc_loc_x; 10102ac6454SAndrew Thompson struct hid_location sc_loc_y; 10202ac6454SAndrew Thompson struct hid_location sc_loc_z; 10302ac6454SAndrew Thompson struct hid_location sc_loc_t; 10402ac6454SAndrew Thompson struct hid_location sc_loc_btn[UMS_BUTTON_MAX]; 10502ac6454SAndrew Thompson 10602ac6454SAndrew Thompson uint32_t sc_flags; 10702ac6454SAndrew Thompson #define UMS_FLAG_X_AXIS 0x0001 10802ac6454SAndrew Thompson #define UMS_FLAG_Y_AXIS 0x0002 10902ac6454SAndrew Thompson #define UMS_FLAG_Z_AXIS 0x0004 11002ac6454SAndrew Thompson #define UMS_FLAG_T_AXIS 0x0008 11102ac6454SAndrew Thompson #define UMS_FLAG_SBU 0x0010 /* spurious button up events */ 1123e5e312aSAndrew Thompson #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */ 1133e5e312aSAndrew Thompson #define UMS_FLAG_W_AXIS 0x0040 11402ac6454SAndrew Thompson 115bf87f556SAndrew Thompson uint8_t sc_iid_w; 116bf87f556SAndrew Thompson uint8_t sc_iid_x; 117bf87f556SAndrew Thompson uint8_t sc_iid_y; 118bf87f556SAndrew Thompson uint8_t sc_iid_z; 119bf87f556SAndrew Thompson uint8_t sc_iid_t; 120bf87f556SAndrew Thompson uint8_t sc_iid_btn[UMS_BUTTON_MAX]; 121de967835SAndrew Thompson uint8_t sc_buttons; 122de967835SAndrew Thompson }; 123de967835SAndrew Thompson 124de967835SAndrew Thompson struct ums_softc { 125760bc48eSAndrew Thompson struct usb_fifo_sc sc_fifo; 126de967835SAndrew Thompson struct mtx sc_mtx; 127760bc48eSAndrew Thompson struct usb_callout sc_callout; 128de967835SAndrew Thompson struct ums_info sc_info[UMS_INFO_MAX]; 129de967835SAndrew Thompson 130de967835SAndrew Thompson mousehw_t sc_hw; 131de967835SAndrew Thompson mousemode_t sc_mode; 132de967835SAndrew Thompson mousestatus_t sc_status; 133de967835SAndrew Thompson 134760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UMS_N_TRANSFER]; 135de967835SAndrew Thompson 13681f82884SAlfred Perlstein int sc_pollrate; 13781f82884SAlfred Perlstein 138de967835SAndrew Thompson uint8_t sc_buttons; 139de967835SAndrew Thompson uint8_t sc_iid; 14002ac6454SAndrew Thompson uint8_t sc_temp[64]; 14102ac6454SAndrew Thompson }; 14202ac6454SAndrew Thompson 14302ac6454SAndrew Thompson static void ums_put_queue_timeout(void *__sc); 14402ac6454SAndrew Thompson 145e0a69b51SAndrew Thompson static usb_callback_t ums_intr_callback; 14602ac6454SAndrew Thompson 14702ac6454SAndrew Thompson static device_probe_t ums_probe; 14802ac6454SAndrew Thompson static device_attach_t ums_attach; 14902ac6454SAndrew Thompson static device_detach_t ums_detach; 15002ac6454SAndrew Thompson 151e0a69b51SAndrew Thompson static usb_fifo_cmd_t ums_start_read; 152e0a69b51SAndrew Thompson static usb_fifo_cmd_t ums_stop_read; 153e0a69b51SAndrew Thompson static usb_fifo_open_t ums_open; 154e0a69b51SAndrew Thompson static usb_fifo_close_t ums_close; 155e0a69b51SAndrew Thompson static usb_fifo_ioctl_t ums_ioctl; 15602ac6454SAndrew Thompson 1574ed0304cSAndrew Thompson static void ums_put_queue(struct ums_softc *, int32_t, int32_t, 1584ed0304cSAndrew Thompson int32_t, int32_t, int32_t); 1594ed0304cSAndrew Thompson static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS); 16002ac6454SAndrew Thompson 161760bc48eSAndrew Thompson static struct usb_fifo_methods ums_fifo_methods = { 16202ac6454SAndrew Thompson .f_open = &ums_open, 16302ac6454SAndrew Thompson .f_close = &ums_close, 16402ac6454SAndrew Thompson .f_ioctl = &ums_ioctl, 16502ac6454SAndrew Thompson .f_start_read = &ums_start_read, 16602ac6454SAndrew Thompson .f_stop_read = &ums_stop_read, 16702ac6454SAndrew Thompson .basename[0] = "ums", 16802ac6454SAndrew Thompson }; 16902ac6454SAndrew Thompson 17002ac6454SAndrew Thompson static void 17102ac6454SAndrew Thompson ums_put_queue_timeout(void *__sc) 17202ac6454SAndrew Thompson { 17302ac6454SAndrew Thompson struct ums_softc *sc = __sc; 17402ac6454SAndrew Thompson 17502ac6454SAndrew Thompson mtx_assert(&sc->sc_mtx, MA_OWNED); 17602ac6454SAndrew Thompson 17702ac6454SAndrew Thompson ums_put_queue(sc, 0, 0, 0, 0, 0); 17802ac6454SAndrew Thompson } 17902ac6454SAndrew Thompson 18002ac6454SAndrew Thompson static void 181ed6d949aSAndrew Thompson ums_intr_callback(struct usb_xfer *xfer, usb_error_t error) 18202ac6454SAndrew Thompson { 183ed6d949aSAndrew Thompson struct ums_softc *sc = usbd_xfer_softc(xfer); 184de967835SAndrew Thompson struct ums_info *info = &sc->sc_info[0]; 185ed6d949aSAndrew Thompson struct usb_page_cache *pc; 18602ac6454SAndrew Thompson uint8_t *buf = sc->sc_temp; 18702ac6454SAndrew Thompson int32_t buttons = 0; 18881f82884SAlfred Perlstein int32_t buttons_found = 0; 189de967835SAndrew Thompson int32_t dw = 0; 190de967835SAndrew Thompson int32_t dx = 0; 191de967835SAndrew Thompson int32_t dy = 0; 192de967835SAndrew Thompson int32_t dz = 0; 193de967835SAndrew Thompson int32_t dt = 0; 19402ac6454SAndrew Thompson uint8_t i; 195bf87f556SAndrew Thompson uint8_t id; 196ed6d949aSAndrew Thompson int len; 197ed6d949aSAndrew Thompson 198ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 19902ac6454SAndrew Thompson 20002ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 20102ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 20202ac6454SAndrew Thompson DPRINTFN(6, "sc=%p actlen=%d\n", sc, len); 20302ac6454SAndrew Thompson 20402ac6454SAndrew Thompson if (len > sizeof(sc->sc_temp)) { 20502ac6454SAndrew Thompson DPRINTFN(6, "truncating large packet to %zu bytes\n", 20602ac6454SAndrew Thompson sizeof(sc->sc_temp)); 20702ac6454SAndrew Thompson len = sizeof(sc->sc_temp); 20802ac6454SAndrew Thompson } 2093e5e312aSAndrew Thompson if (len == 0) 21002ac6454SAndrew Thompson goto tr_setup; 2113e5e312aSAndrew Thompson 212ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 213ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, len); 21402ac6454SAndrew Thompson 21502ac6454SAndrew Thompson DPRINTFN(6, "data = %02x %02x %02x %02x " 21602ac6454SAndrew Thompson "%02x %02x %02x %02x\n", 21702ac6454SAndrew Thompson (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0, 21802ac6454SAndrew Thompson (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0, 21902ac6454SAndrew Thompson (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0, 22002ac6454SAndrew Thompson (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0); 22102ac6454SAndrew Thompson 22202ac6454SAndrew Thompson if (sc->sc_iid) { 223bf87f556SAndrew Thompson id = *buf; 22402ac6454SAndrew Thompson 22502ac6454SAndrew Thompson len--; 22602ac6454SAndrew Thompson buf++; 22702ac6454SAndrew Thompson 22802ac6454SAndrew Thompson } else { 229bf87f556SAndrew Thompson id = 0; 230de967835SAndrew Thompson if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) { 23102ac6454SAndrew Thompson if ((*buf == 0x14) || (*buf == 0x15)) { 23202ac6454SAndrew Thompson goto tr_setup; 23302ac6454SAndrew Thompson } 23402ac6454SAndrew Thompson } 23502ac6454SAndrew Thompson } 23602ac6454SAndrew Thompson 237de967835SAndrew Thompson repeat: 238de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_W_AXIS) && 239de967835SAndrew Thompson (id == info->sc_iid_w)) 240de967835SAndrew Thompson dw += hid_get_data(buf, len, &info->sc_loc_w); 24102ac6454SAndrew Thompson 242de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_X_AXIS) && 243de967835SAndrew Thompson (id == info->sc_iid_x)) 244de967835SAndrew Thompson dx += hid_get_data(buf, len, &info->sc_loc_x); 24502ac6454SAndrew Thompson 246de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_Y_AXIS) && 247de967835SAndrew Thompson (id == info->sc_iid_y)) 248de967835SAndrew Thompson dy = -hid_get_data(buf, len, &info->sc_loc_y); 24902ac6454SAndrew Thompson 250de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_Z_AXIS) && 251de967835SAndrew Thompson (id == info->sc_iid_z)) { 252de967835SAndrew Thompson int32_t temp; 253de967835SAndrew Thompson temp = hid_get_data(buf, len, &info->sc_loc_z); 254de967835SAndrew Thompson if (info->sc_flags & UMS_FLAG_REVZ) 255de967835SAndrew Thompson temp = -temp; 256de967835SAndrew Thompson dz -= temp; 257de967835SAndrew Thompson } 25802ac6454SAndrew Thompson 259de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_T_AXIS) && 260de967835SAndrew Thompson (id == info->sc_iid_t)) 261de967835SAndrew Thompson dt -= hid_get_data(buf, len, &info->sc_loc_t); 262bf87f556SAndrew Thompson 263de967835SAndrew Thompson for (i = 0; i < info->sc_buttons; i++) { 26481f82884SAlfred Perlstein uint32_t mask; 26581f82884SAlfred Perlstein mask = 1UL << UMS_BUT(i); 26681f82884SAlfred Perlstein /* check for correct button ID */ 267de967835SAndrew Thompson if (id != info->sc_iid_btn[i]) 268bf87f556SAndrew Thompson continue; 26981f82884SAlfred Perlstein /* check for button pressed */ 27081f82884SAlfred Perlstein if (hid_get_data(buf, len, &info->sc_loc_btn[i])) 27181f82884SAlfred Perlstein buttons |= mask; 27281f82884SAlfred Perlstein /* register button mask */ 27381f82884SAlfred Perlstein buttons_found |= mask; 27402ac6454SAndrew Thompson } 27502ac6454SAndrew Thompson 276de967835SAndrew Thompson if (++info != &sc->sc_info[UMS_INFO_MAX]) 277de967835SAndrew Thompson goto repeat; 278de967835SAndrew Thompson 27981f82884SAlfred Perlstein /* keep old button value(s) for non-detected buttons */ 28081f82884SAlfred Perlstein buttons |= sc->sc_status.button & ~buttons_found; 28181f82884SAlfred Perlstein 28202ac6454SAndrew Thompson if (dx || dy || dz || dt || dw || 28302ac6454SAndrew Thompson (buttons != sc->sc_status.button)) { 28402ac6454SAndrew Thompson 28502ac6454SAndrew Thompson DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n", 28602ac6454SAndrew Thompson dx, dy, dz, dt, dw, buttons); 28702ac6454SAndrew Thompson 288300f96f1SAndrew Thompson /* translate T-axis into button presses until further */ 289300f96f1SAndrew Thompson if (dt > 0) 290300f96f1SAndrew Thompson buttons |= 1UL << 3; 291300f96f1SAndrew Thompson else if (dt < 0) 292300f96f1SAndrew Thompson buttons |= 1UL << 4; 293300f96f1SAndrew Thompson 29402ac6454SAndrew Thompson sc->sc_status.button = buttons; 29502ac6454SAndrew Thompson sc->sc_status.dx += dx; 29602ac6454SAndrew Thompson sc->sc_status.dy += dy; 29702ac6454SAndrew Thompson sc->sc_status.dz += dz; 29802ac6454SAndrew Thompson /* 29902ac6454SAndrew Thompson * sc->sc_status.dt += dt; 30002ac6454SAndrew Thompson * no way to export this yet 30102ac6454SAndrew Thompson */ 30202ac6454SAndrew Thompson 30302ac6454SAndrew Thompson /* 3043e5e312aSAndrew Thompson * The Qtronix keyboard has a built in PS/2 3053e5e312aSAndrew Thompson * port for a mouse. The firmware once in a 3063e5e312aSAndrew Thompson * while posts a spurious button up 3073e5e312aSAndrew Thompson * event. This event we ignore by doing a 3083e5e312aSAndrew Thompson * timeout for 50 msecs. If we receive 3093e5e312aSAndrew Thompson * dx=dy=dz=buttons=0 before we add the event 3103e5e312aSAndrew Thompson * to the queue. In any other case we delete 3113e5e312aSAndrew Thompson * the timeout event. 31202ac6454SAndrew Thompson */ 313de967835SAndrew Thompson if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) && 31402ac6454SAndrew Thompson (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) && 31502ac6454SAndrew Thompson (dw == 0) && (buttons == 0)) { 31602ac6454SAndrew Thompson 317a593f6b8SAndrew Thompson usb_callout_reset(&sc->sc_callout, hz / 20, 31802ac6454SAndrew Thompson &ums_put_queue_timeout, sc); 31902ac6454SAndrew Thompson } else { 32002ac6454SAndrew Thompson 321a593f6b8SAndrew Thompson usb_callout_stop(&sc->sc_callout); 32202ac6454SAndrew Thompson 32302ac6454SAndrew Thompson ums_put_queue(sc, dx, dy, dz, dt, buttons); 32402ac6454SAndrew Thompson } 32502ac6454SAndrew Thompson } 32602ac6454SAndrew Thompson case USB_ST_SETUP: 32702ac6454SAndrew Thompson tr_setup: 32802ac6454SAndrew Thompson /* check if we can put more data into the FIFO */ 329a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max( 33002ac6454SAndrew Thompson sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 331ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 332a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 33302ac6454SAndrew Thompson } 3343e5e312aSAndrew Thompson break; 33502ac6454SAndrew Thompson 33602ac6454SAndrew Thompson default: /* Error */ 337ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 3383e5e312aSAndrew Thompson /* try clear stall first */ 339ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 3403e5e312aSAndrew Thompson goto tr_setup; 34102ac6454SAndrew Thompson } 3423e5e312aSAndrew Thompson break; 34302ac6454SAndrew Thompson } 34402ac6454SAndrew Thompson } 34502ac6454SAndrew Thompson 346760bc48eSAndrew Thompson static const struct usb_config ums_config[UMS_N_TRANSFER] = { 34702ac6454SAndrew Thompson 34802ac6454SAndrew Thompson [UMS_INTR_DT] = { 34902ac6454SAndrew Thompson .type = UE_INTERRUPT, 35002ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 35102ac6454SAndrew Thompson .direction = UE_DIR_IN, 3524eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 3534eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 3544eae601eSAndrew Thompson .callback = &ums_intr_callback, 35502ac6454SAndrew Thompson }, 35602ac6454SAndrew Thompson }; 35702ac6454SAndrew Thompson 35802ac6454SAndrew Thompson static int 35902ac6454SAndrew Thompson ums_probe(device_t dev) 36002ac6454SAndrew Thompson { 361760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 36202ac6454SAndrew Thompson void *d_ptr; 3633e5e312aSAndrew Thompson int error; 36402ac6454SAndrew Thompson uint16_t d_len; 36502ac6454SAndrew Thompson 36602ac6454SAndrew Thompson DPRINTFN(11, "\n"); 36702ac6454SAndrew Thompson 368f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) 36902ac6454SAndrew Thompson return (ENXIO); 3703e5e312aSAndrew Thompson 371fd4f10ceSAndrew Thompson if (uaa->info.bInterfaceClass != UICLASS_HID) 37202ac6454SAndrew Thompson return (ENXIO); 3733e5e312aSAndrew Thompson 374fd4f10ceSAndrew Thompson if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 375fd4f10ceSAndrew Thompson (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE)) 376*cd10bffaSAndriy Gapon return (BUS_PROBE_DEFAULT); 377fd4f10ceSAndrew Thompson 378a593f6b8SAndrew Thompson error = usbd_req_get_hid_desc(uaa->device, NULL, 37902ac6454SAndrew Thompson &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 38002ac6454SAndrew Thompson 3813e5e312aSAndrew Thompson if (error) 38202ac6454SAndrew Thompson return (ENXIO); 3833e5e312aSAndrew Thompson 38402ac6454SAndrew Thompson if (hid_is_collection(d_ptr, d_len, 3853e5e312aSAndrew Thompson HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) 386*cd10bffaSAndriy Gapon error = BUS_PROBE_DEFAULT; 3873e5e312aSAndrew Thompson else 38802ac6454SAndrew Thompson error = ENXIO; 38902ac6454SAndrew Thompson 39002ac6454SAndrew Thompson free(d_ptr, M_TEMP); 39102ac6454SAndrew Thompson return (error); 39202ac6454SAndrew Thompson } 39302ac6454SAndrew Thompson 394de967835SAndrew Thompson static void 395de967835SAndrew Thompson ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf, 396de967835SAndrew Thompson uint16_t len, uint8_t index) 397de967835SAndrew Thompson { 398de967835SAndrew Thompson struct ums_info *info = &sc->sc_info[index]; 399de967835SAndrew Thompson uint32_t flags; 400de967835SAndrew Thompson uint8_t i; 401ee6e1a6fSAndrew Thompson uint8_t j; 402de967835SAndrew Thompson 403de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 404de967835SAndrew Thompson hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) { 405de967835SAndrew Thompson 406de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 407de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_X_AXIS; 408de967835SAndrew Thompson } 409de967835SAndrew Thompson } 410de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 411de967835SAndrew Thompson hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) { 412de967835SAndrew Thompson 413de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 414de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_Y_AXIS; 415de967835SAndrew Thompson } 416de967835SAndrew Thompson } 417de967835SAndrew Thompson /* Try the wheel first as the Z activator since it's tradition. */ 418de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 419de967835SAndrew Thompson HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags, 420de967835SAndrew Thompson &info->sc_iid_z) || 421de967835SAndrew Thompson hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 422de967835SAndrew Thompson HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags, 423de967835SAndrew Thompson &info->sc_iid_z)) { 424de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 425de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_Z_AXIS; 426de967835SAndrew Thompson } 427de967835SAndrew Thompson /* 428de967835SAndrew Thompson * We might have both a wheel and Z direction, if so put 429de967835SAndrew Thompson * put the Z on the W coordinate. 430de967835SAndrew Thompson */ 431de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 432de967835SAndrew Thompson HUG_Z), hid_input, index, &info->sc_loc_w, &flags, 433de967835SAndrew Thompson &info->sc_iid_w)) { 434de967835SAndrew Thompson 435de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 436de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_W_AXIS; 437de967835SAndrew Thompson } 438de967835SAndrew Thompson } 439de967835SAndrew Thompson } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 440de967835SAndrew Thompson HUG_Z), hid_input, index, &info->sc_loc_z, &flags, 441de967835SAndrew Thompson &info->sc_iid_z)) { 442de967835SAndrew Thompson 443de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 444de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_Z_AXIS; 445de967835SAndrew Thompson } 446de967835SAndrew Thompson } 447de967835SAndrew Thompson /* 448de967835SAndrew Thompson * The Microsoft Wireless Intellimouse 2.0 reports it's wheel 449de967835SAndrew Thompson * using 0x0048, which is HUG_TWHEEL, and seems to expect you 450de967835SAndrew Thompson * to know that the byte after the wheel is the tilt axis. 451de967835SAndrew Thompson * There are no other HID axis descriptors other than X,Y and 452de967835SAndrew Thompson * TWHEEL 453de967835SAndrew Thompson */ 454de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 455de967835SAndrew Thompson HUG_TWHEEL), hid_input, index, &info->sc_loc_t, 456de967835SAndrew Thompson &flags, &info->sc_iid_t)) { 457de967835SAndrew Thompson 458de967835SAndrew Thompson info->sc_loc_t.pos += 8; 459de967835SAndrew Thompson 460de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 461de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_T_AXIS; 462de967835SAndrew Thompson } 463300f96f1SAndrew Thompson } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER, 464300f96f1SAndrew Thompson HUC_AC_PAN), hid_input, index, &info->sc_loc_t, 465300f96f1SAndrew Thompson &flags, &info->sc_iid_t)) { 466300f96f1SAndrew Thompson 467300f96f1SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) 468300f96f1SAndrew Thompson info->sc_flags |= UMS_FLAG_T_AXIS; 469de967835SAndrew Thompson } 470de967835SAndrew Thompson /* figure out the number of buttons */ 471de967835SAndrew Thompson 472de967835SAndrew Thompson for (i = 0; i < UMS_BUTTON_MAX; i++) { 473de967835SAndrew Thompson if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)), 474de967835SAndrew Thompson hid_input, index, &info->sc_loc_btn[i], NULL, 475de967835SAndrew Thompson &info->sc_iid_btn[i])) { 476de967835SAndrew Thompson break; 477de967835SAndrew Thompson } 478de967835SAndrew Thompson } 479ee6e1a6fSAndrew Thompson 480ee6e1a6fSAndrew Thompson /* detect other buttons */ 481ee6e1a6fSAndrew Thompson 4821f27b685SAndrew Thompson for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) { 483ee6e1a6fSAndrew Thompson if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)), 484ee6e1a6fSAndrew Thompson hid_input, index, &info->sc_loc_btn[i], NULL, 485ee6e1a6fSAndrew Thompson &info->sc_iid_btn[i])) { 486ee6e1a6fSAndrew Thompson break; 487ee6e1a6fSAndrew Thompson } 488ee6e1a6fSAndrew Thompson } 489ee6e1a6fSAndrew Thompson 490de967835SAndrew Thompson info->sc_buttons = i; 491de967835SAndrew Thompson 492de967835SAndrew Thompson if (i > sc->sc_buttons) 493de967835SAndrew Thompson sc->sc_buttons = i; 494de967835SAndrew Thompson 495de967835SAndrew Thompson if (info->sc_flags == 0) 496de967835SAndrew Thompson return; 497de967835SAndrew Thompson 498de967835SAndrew Thompson /* announce information about the mouse */ 499de967835SAndrew Thompson device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n", 500de967835SAndrew Thompson (info->sc_buttons), 501de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "", 502de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "", 503de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "", 504de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "", 505de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "", 506de967835SAndrew Thompson info->sc_iid_x); 507de967835SAndrew Thompson } 508de967835SAndrew Thompson 50902ac6454SAndrew Thompson static int 51002ac6454SAndrew Thompson ums_attach(device_t dev) 51102ac6454SAndrew Thompson { 512760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 51302ac6454SAndrew Thompson struct ums_softc *sc = device_get_softc(dev); 514de967835SAndrew Thompson struct ums_info *info; 51502ac6454SAndrew Thompson void *d_ptr = NULL; 5163e5e312aSAndrew Thompson int isize; 5173e5e312aSAndrew Thompson int err; 51802ac6454SAndrew Thompson uint16_t d_len; 51902ac6454SAndrew Thompson uint8_t i; 520b850ecc1SAndrew Thompson #ifdef USB_DEBUG 521de967835SAndrew Thompson uint8_t j; 522b850ecc1SAndrew Thompson #endif 52302ac6454SAndrew Thompson 52402ac6454SAndrew Thompson DPRINTFN(11, "sc=%p\n", sc); 52502ac6454SAndrew Thompson 526a593f6b8SAndrew Thompson device_set_usb_desc(dev); 52702ac6454SAndrew Thompson 52802ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE); 52902ac6454SAndrew Thompson 530a593f6b8SAndrew Thompson usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 53102ac6454SAndrew Thompson 53202ac6454SAndrew Thompson /* 53302ac6454SAndrew Thompson * Force the report (non-boot) protocol. 53402ac6454SAndrew Thompson * 53502ac6454SAndrew Thompson * Mice without boot protocol support may choose not to implement 53602ac6454SAndrew Thompson * Set_Protocol at all; Ignore any error. 53702ac6454SAndrew Thompson */ 538a593f6b8SAndrew Thompson err = usbd_req_set_protocol(uaa->device, NULL, 539de967835SAndrew Thompson uaa->info.bIfaceIndex, 1); 54002ac6454SAndrew Thompson 541a593f6b8SAndrew Thompson err = usbd_transfer_setup(uaa->device, 54202ac6454SAndrew Thompson &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config, 54302ac6454SAndrew Thompson UMS_N_TRANSFER, sc, &sc->sc_mtx); 54402ac6454SAndrew Thompson 54502ac6454SAndrew Thompson if (err) { 546a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 54702ac6454SAndrew Thompson goto detach; 54802ac6454SAndrew Thompson } 54981f82884SAlfred Perlstein 55081f82884SAlfred Perlstein /* Get HID descriptor */ 551a593f6b8SAndrew Thompson err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, 55202ac6454SAndrew Thompson &d_len, M_TEMP, uaa->info.bIfaceIndex); 55302ac6454SAndrew Thompson 55402ac6454SAndrew Thompson if (err) { 55502ac6454SAndrew Thompson device_printf(dev, "error reading report description\n"); 55602ac6454SAndrew Thompson goto detach; 55702ac6454SAndrew Thompson } 55802ac6454SAndrew Thompson 559bf87f556SAndrew Thompson isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid); 56002ac6454SAndrew Thompson 56102ac6454SAndrew Thompson /* 56202ac6454SAndrew Thompson * The Microsoft Wireless Notebook Optical Mouse seems to be in worse 56302ac6454SAndrew Thompson * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and 56402ac6454SAndrew Thompson * all of its other button positions are all off. It also reports that 56502ac6454SAndrew Thompson * it has two addional buttons and a tilt wheel. 56602ac6454SAndrew Thompson */ 567a593f6b8SAndrew Thompson if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) { 56881f82884SAlfred Perlstein 56981f82884SAlfred Perlstein sc->sc_iid = 0; 57081f82884SAlfred Perlstein 571de967835SAndrew Thompson info = &sc->sc_info[0]; 572de967835SAndrew Thompson info->sc_flags = (UMS_FLAG_X_AXIS | 57302ac6454SAndrew Thompson UMS_FLAG_Y_AXIS | 57402ac6454SAndrew Thompson UMS_FLAG_Z_AXIS | 57502ac6454SAndrew Thompson UMS_FLAG_SBU); 576de967835SAndrew Thompson info->sc_buttons = 3; 57702ac6454SAndrew Thompson isize = 5; 57802ac6454SAndrew Thompson /* 1st byte of descriptor report contains garbage */ 579de967835SAndrew Thompson info->sc_loc_x.pos = 16; 58081f82884SAlfred Perlstein info->sc_loc_x.size = 8; 581de967835SAndrew Thompson info->sc_loc_y.pos = 24; 58281f82884SAlfred Perlstein info->sc_loc_y.size = 8; 583de967835SAndrew Thompson info->sc_loc_z.pos = 32; 58481f82884SAlfred Perlstein info->sc_loc_z.size = 8; 585de967835SAndrew Thompson info->sc_loc_btn[0].pos = 8; 58681f82884SAlfred Perlstein info->sc_loc_btn[0].size = 1; 587de967835SAndrew Thompson info->sc_loc_btn[1].pos = 9; 58881f82884SAlfred Perlstein info->sc_loc_btn[1].size = 1; 589de967835SAndrew Thompson info->sc_loc_btn[2].pos = 10; 59081f82884SAlfred Perlstein info->sc_loc_btn[2].size = 1; 5913e5e312aSAndrew Thompson 592de967835SAndrew Thompson /* Announce device */ 593de967835SAndrew Thompson device_printf(dev, "3 buttons and [XYZ] " 594de967835SAndrew Thompson "coordinates ID=0\n"); 595de967835SAndrew Thompson 596de967835SAndrew Thompson } else { 597de967835SAndrew Thompson /* Search the HID descriptor and announce device */ 598de967835SAndrew Thompson for (i = 0; i < UMS_INFO_MAX; i++) { 599de967835SAndrew Thompson ums_hid_parse(sc, dev, d_ptr, d_len, i); 600de967835SAndrew Thompson } 60102ac6454SAndrew Thompson } 6023e5e312aSAndrew Thompson 603a593f6b8SAndrew Thompson if (usb_test_quirk(uaa, UQ_MS_REVZ)) { 604de967835SAndrew Thompson info = &sc->sc_info[0]; 60502ac6454SAndrew Thompson /* Some wheels need the Z axis reversed. */ 606de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_REVZ; 60702ac6454SAndrew Thompson } 608ed6d949aSAndrew Thompson if (isize > usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) { 60902ac6454SAndrew Thompson DPRINTF("WARNING: report size, %d bytes, is larger " 610ed6d949aSAndrew Thompson "than interrupt size, %d bytes!\n", isize, 611ed6d949aSAndrew Thompson usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])); 61202ac6454SAndrew Thompson } 61302ac6454SAndrew Thompson free(d_ptr, M_TEMP); 61402ac6454SAndrew Thompson d_ptr = NULL; 61502ac6454SAndrew Thompson 616b850ecc1SAndrew Thompson #ifdef USB_DEBUG 617de967835SAndrew Thompson for (j = 0; j < UMS_INFO_MAX; j++) { 618de967835SAndrew Thompson info = &sc->sc_info[j]; 61902ac6454SAndrew Thompson 620de967835SAndrew Thompson DPRINTF("sc=%p, index=%d\n", sc, j); 621de967835SAndrew Thompson DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos, 622de967835SAndrew Thompson info->sc_loc_x.size, info->sc_iid_x); 623de967835SAndrew Thompson DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos, 624de967835SAndrew Thompson info->sc_loc_y.size, info->sc_iid_y); 625de967835SAndrew Thompson DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos, 626de967835SAndrew Thompson info->sc_loc_z.size, info->sc_iid_z); 627de967835SAndrew Thompson DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos, 628de967835SAndrew Thompson info->sc_loc_t.size, info->sc_iid_t); 629de967835SAndrew Thompson DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos, 630de967835SAndrew Thompson info->sc_loc_w.size, info->sc_iid_w); 631de967835SAndrew Thompson 632de967835SAndrew Thompson for (i = 0; i < info->sc_buttons; i++) { 633bf87f556SAndrew Thompson DPRINTF("B%d\t%d/%d id=%d\n", 634de967835SAndrew Thompson i + 1, info->sc_loc_btn[i].pos, 635de967835SAndrew Thompson info->sc_loc_btn[i].size, info->sc_iid_btn[i]); 636de967835SAndrew Thompson } 63702ac6454SAndrew Thompson } 63802ac6454SAndrew Thompson DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid); 63902ac6454SAndrew Thompson #endif 64002ac6454SAndrew Thompson 64102ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 64202ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 64302ac6454SAndrew Thompson else 64402ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 64502ac6454SAndrew Thompson 64602ac6454SAndrew Thompson sc->sc_hw.iftype = MOUSE_IF_USB; 64702ac6454SAndrew Thompson sc->sc_hw.type = MOUSE_MOUSE; 64802ac6454SAndrew Thompson sc->sc_hw.model = MOUSE_MODEL_GENERIC; 64902ac6454SAndrew Thompson sc->sc_hw.hwid = 0; 65002ac6454SAndrew Thompson 65102ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_MSC; 65202ac6454SAndrew Thompson sc->sc_mode.rate = -1; 65302ac6454SAndrew Thompson sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; 65402ac6454SAndrew Thompson sc->sc_mode.accelfactor = 0; 65502ac6454SAndrew Thompson sc->sc_mode.level = 0; 65602ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 65702ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 65802ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 65902ac6454SAndrew Thompson 660a593f6b8SAndrew Thompson err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 66102ac6454SAndrew Thompson &ums_fifo_methods, &sc->sc_fifo, 662de967835SAndrew Thompson device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex, 663ee3e3ff5SAndrew Thompson UID_ROOT, GID_OPERATOR, 0644); 66402ac6454SAndrew Thompson if (err) { 66502ac6454SAndrew Thompson goto detach; 66602ac6454SAndrew Thompson } 6674ed0304cSAndrew Thompson SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6684ed0304cSAndrew Thompson SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 6694ed0304cSAndrew Thompson OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD, 6704ed0304cSAndrew Thompson sc, 0, ums_sysctl_handler_parseinfo, 671b635069cSHans Petter Selasky "", "Dump of parsed HID report descriptor"); 6724ed0304cSAndrew Thompson 67302ac6454SAndrew Thompson return (0); 67402ac6454SAndrew Thompson 67502ac6454SAndrew Thompson detach: 67602ac6454SAndrew Thompson if (d_ptr) { 67702ac6454SAndrew Thompson free(d_ptr, M_TEMP); 67802ac6454SAndrew Thompson } 67902ac6454SAndrew Thompson ums_detach(dev); 68002ac6454SAndrew Thompson return (ENOMEM); 68102ac6454SAndrew Thompson } 68202ac6454SAndrew Thompson 68302ac6454SAndrew Thompson static int 68402ac6454SAndrew Thompson ums_detach(device_t self) 68502ac6454SAndrew Thompson { 68602ac6454SAndrew Thompson struct ums_softc *sc = device_get_softc(self); 68702ac6454SAndrew Thompson 68802ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 68902ac6454SAndrew Thompson 690a593f6b8SAndrew Thompson usb_fifo_detach(&sc->sc_fifo); 69102ac6454SAndrew Thompson 692a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER); 69302ac6454SAndrew Thompson 694a593f6b8SAndrew Thompson usb_callout_drain(&sc->sc_callout); 69502ac6454SAndrew Thompson 69602ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 69702ac6454SAndrew Thompson 69802ac6454SAndrew Thompson return (0); 69902ac6454SAndrew Thompson } 70002ac6454SAndrew Thompson 70102ac6454SAndrew Thompson static void 702760bc48eSAndrew Thompson ums_start_read(struct usb_fifo *fifo) 70302ac6454SAndrew Thompson { 704ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 70581f82884SAlfred Perlstein int rate; 70681f82884SAlfred Perlstein 70781f82884SAlfred Perlstein /* Check if we should override the default polling interval */ 70881f82884SAlfred Perlstein rate = sc->sc_pollrate; 70981f82884SAlfred Perlstein /* Range check rate */ 71081f82884SAlfred Perlstein if (rate > 1000) 71181f82884SAlfred Perlstein rate = 1000; 71281f82884SAlfred Perlstein /* Check for set rate */ 71381f82884SAlfred Perlstein if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) { 71481f82884SAlfred Perlstein DPRINTF("Setting pollrate = %d\n", rate); 71581f82884SAlfred Perlstein /* Stop current transfer, if any */ 71681f82884SAlfred Perlstein usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 71781f82884SAlfred Perlstein /* Set new interval */ 71881f82884SAlfred Perlstein usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate); 71981f82884SAlfred Perlstein /* Only set pollrate once */ 72081f82884SAlfred Perlstein sc->sc_pollrate = 0; 72181f82884SAlfred Perlstein } 72202ac6454SAndrew Thompson 723a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]); 72402ac6454SAndrew Thompson } 72502ac6454SAndrew Thompson 72602ac6454SAndrew Thompson static void 727760bc48eSAndrew Thompson ums_stop_read(struct usb_fifo *fifo) 72802ac6454SAndrew Thompson { 729ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 73002ac6454SAndrew Thompson 731a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 732a593f6b8SAndrew Thompson usb_callout_stop(&sc->sc_callout); 73302ac6454SAndrew Thompson } 73402ac6454SAndrew Thompson 73502ac6454SAndrew Thompson 73602ac6454SAndrew Thompson #if ((MOUSE_SYS_PACKETSIZE != 8) || \ 73702ac6454SAndrew Thompson (MOUSE_MSC_PACKETSIZE != 5)) 73802ac6454SAndrew Thompson #error "Software assumptions are not met. Please update code." 73902ac6454SAndrew Thompson #endif 74002ac6454SAndrew Thompson 74102ac6454SAndrew Thompson static void 74202ac6454SAndrew Thompson ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, 74302ac6454SAndrew Thompson int32_t dz, int32_t dt, int32_t buttons) 74402ac6454SAndrew Thompson { 74502ac6454SAndrew Thompson uint8_t buf[8]; 74602ac6454SAndrew Thompson 74702ac6454SAndrew Thompson if (1) { 74802ac6454SAndrew Thompson 74902ac6454SAndrew Thompson if (dx > 254) 75002ac6454SAndrew Thompson dx = 254; 75102ac6454SAndrew Thompson if (dx < -256) 75202ac6454SAndrew Thompson dx = -256; 75302ac6454SAndrew Thompson if (dy > 254) 75402ac6454SAndrew Thompson dy = 254; 75502ac6454SAndrew Thompson if (dy < -256) 75602ac6454SAndrew Thompson dy = -256; 75702ac6454SAndrew Thompson if (dz > 126) 75802ac6454SAndrew Thompson dz = 126; 75902ac6454SAndrew Thompson if (dz < -128) 76002ac6454SAndrew Thompson dz = -128; 76102ac6454SAndrew Thompson if (dt > 126) 76202ac6454SAndrew Thompson dt = 126; 76302ac6454SAndrew Thompson if (dt < -128) 76402ac6454SAndrew Thompson dt = -128; 76502ac6454SAndrew Thompson 76602ac6454SAndrew Thompson buf[0] = sc->sc_mode.syncmask[1]; 76702ac6454SAndrew Thompson buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS; 76802ac6454SAndrew Thompson buf[1] = dx >> 1; 76902ac6454SAndrew Thompson buf[2] = dy >> 1; 77002ac6454SAndrew Thompson buf[3] = dx - (dx >> 1); 77102ac6454SAndrew Thompson buf[4] = dy - (dy >> 1); 77202ac6454SAndrew Thompson 77302ac6454SAndrew Thompson if (sc->sc_mode.level == 1) { 77402ac6454SAndrew Thompson buf[5] = dz >> 1; 77502ac6454SAndrew Thompson buf[6] = dz - (dz >> 1); 77602ac6454SAndrew Thompson buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS); 77702ac6454SAndrew Thompson } 778a593f6b8SAndrew Thompson usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, 77902ac6454SAndrew Thompson sc->sc_mode.packetsize, 1); 78002ac6454SAndrew Thompson 78102ac6454SAndrew Thompson } else { 78202ac6454SAndrew Thompson DPRINTF("Buffer full, discarded packet\n"); 78302ac6454SAndrew Thompson } 78402ac6454SAndrew Thompson } 78502ac6454SAndrew Thompson 78602ac6454SAndrew Thompson static void 78702ac6454SAndrew Thompson ums_reset_buf(struct ums_softc *sc) 78802ac6454SAndrew Thompson { 78902ac6454SAndrew Thompson /* reset read queue */ 790a593f6b8SAndrew Thompson usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); 79102ac6454SAndrew Thompson } 79202ac6454SAndrew Thompson 79302ac6454SAndrew Thompson static int 794760bc48eSAndrew Thompson ums_open(struct usb_fifo *fifo, int fflags) 79502ac6454SAndrew Thompson { 796ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 79702ac6454SAndrew Thompson 79802ac6454SAndrew Thompson DPRINTFN(2, "\n"); 79902ac6454SAndrew Thompson 80002ac6454SAndrew Thompson if (fflags & FREAD) { 80102ac6454SAndrew Thompson 80202ac6454SAndrew Thompson /* reset status */ 80302ac6454SAndrew Thompson 80402ac6454SAndrew Thompson sc->sc_status.flags = 0; 80502ac6454SAndrew Thompson sc->sc_status.button = 0; 80602ac6454SAndrew Thompson sc->sc_status.obutton = 0; 80702ac6454SAndrew Thompson sc->sc_status.dx = 0; 80802ac6454SAndrew Thompson sc->sc_status.dy = 0; 80902ac6454SAndrew Thompson sc->sc_status.dz = 0; 81002ac6454SAndrew Thompson /* sc->sc_status.dt = 0; */ 81102ac6454SAndrew Thompson 812a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo, 81302ac6454SAndrew Thompson UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) { 81402ac6454SAndrew Thompson return (ENOMEM); 81502ac6454SAndrew Thompson } 81602ac6454SAndrew Thompson } 81702ac6454SAndrew Thompson return (0); 81802ac6454SAndrew Thompson } 81902ac6454SAndrew Thompson 82002ac6454SAndrew Thompson static void 821760bc48eSAndrew Thompson ums_close(struct usb_fifo *fifo, int fflags) 82202ac6454SAndrew Thompson { 82302ac6454SAndrew Thompson if (fflags & FREAD) { 824a593f6b8SAndrew Thompson usb_fifo_free_buffer(fifo); 82502ac6454SAndrew Thompson } 82602ac6454SAndrew Thompson } 82702ac6454SAndrew Thompson 82802ac6454SAndrew Thompson static int 829760bc48eSAndrew Thompson ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 83002ac6454SAndrew Thompson { 831ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 83202ac6454SAndrew Thompson mousemode_t mode; 83302ac6454SAndrew Thompson int error = 0; 83402ac6454SAndrew Thompson 83502ac6454SAndrew Thompson DPRINTFN(2, "\n"); 83602ac6454SAndrew Thompson 83702ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx); 83802ac6454SAndrew Thompson 83902ac6454SAndrew Thompson switch (cmd) { 84002ac6454SAndrew Thompson case MOUSE_GETHWINFO: 84102ac6454SAndrew Thompson *(mousehw_t *)addr = sc->sc_hw; 84202ac6454SAndrew Thompson break; 84302ac6454SAndrew Thompson 84402ac6454SAndrew Thompson case MOUSE_GETMODE: 84502ac6454SAndrew Thompson *(mousemode_t *)addr = sc->sc_mode; 84602ac6454SAndrew Thompson break; 84702ac6454SAndrew Thompson 84802ac6454SAndrew Thompson case MOUSE_SETMODE: 84902ac6454SAndrew Thompson mode = *(mousemode_t *)addr; 85002ac6454SAndrew Thompson 85102ac6454SAndrew Thompson if (mode.level == -1) { 85202ac6454SAndrew Thompson /* don't change the current setting */ 85302ac6454SAndrew Thompson } else if ((mode.level < 0) || (mode.level > 1)) { 85402ac6454SAndrew Thompson error = EINVAL; 85502ac6454SAndrew Thompson goto done; 85602ac6454SAndrew Thompson } else { 85702ac6454SAndrew Thompson sc->sc_mode.level = mode.level; 85802ac6454SAndrew Thompson } 85902ac6454SAndrew Thompson 86081f82884SAlfred Perlstein /* store polling rate */ 86181f82884SAlfred Perlstein sc->sc_pollrate = mode.rate; 86281f82884SAlfred Perlstein 86302ac6454SAndrew Thompson if (sc->sc_mode.level == 0) { 86402ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 86502ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 86602ac6454SAndrew Thompson else 86702ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 86802ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_MSC; 86902ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 87002ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 87102ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 87202ac6454SAndrew Thompson } else if (sc->sc_mode.level == 1) { 87302ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 87402ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 87502ac6454SAndrew Thompson else 87602ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 87702ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 87802ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 87902ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 88002ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 88102ac6454SAndrew Thompson } 88202ac6454SAndrew Thompson ums_reset_buf(sc); 88302ac6454SAndrew Thompson break; 88402ac6454SAndrew Thompson 88502ac6454SAndrew Thompson case MOUSE_GETLEVEL: 88602ac6454SAndrew Thompson *(int *)addr = sc->sc_mode.level; 88702ac6454SAndrew Thompson break; 88802ac6454SAndrew Thompson 88902ac6454SAndrew Thompson case MOUSE_SETLEVEL: 89002ac6454SAndrew Thompson if (*(int *)addr < 0 || *(int *)addr > 1) { 89102ac6454SAndrew Thompson error = EINVAL; 89202ac6454SAndrew Thompson goto done; 89302ac6454SAndrew Thompson } 89402ac6454SAndrew Thompson sc->sc_mode.level = *(int *)addr; 89502ac6454SAndrew Thompson 89602ac6454SAndrew Thompson if (sc->sc_mode.level == 0) { 89702ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 89802ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 89902ac6454SAndrew Thompson else 90002ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 90102ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_MSC; 90202ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 90302ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 90402ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 90502ac6454SAndrew Thompson } else if (sc->sc_mode.level == 1) { 90602ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 90702ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 90802ac6454SAndrew Thompson else 90902ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 91002ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 91102ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 91202ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 91302ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 91402ac6454SAndrew Thompson } 91502ac6454SAndrew Thompson ums_reset_buf(sc); 91602ac6454SAndrew Thompson break; 91702ac6454SAndrew Thompson 91802ac6454SAndrew Thompson case MOUSE_GETSTATUS:{ 91902ac6454SAndrew Thompson mousestatus_t *status = (mousestatus_t *)addr; 92002ac6454SAndrew Thompson 92102ac6454SAndrew Thompson *status = sc->sc_status; 92202ac6454SAndrew Thompson sc->sc_status.obutton = sc->sc_status.button; 92302ac6454SAndrew Thompson sc->sc_status.button = 0; 92402ac6454SAndrew Thompson sc->sc_status.dx = 0; 92502ac6454SAndrew Thompson sc->sc_status.dy = 0; 92602ac6454SAndrew Thompson sc->sc_status.dz = 0; 92702ac6454SAndrew Thompson /* sc->sc_status.dt = 0; */ 92802ac6454SAndrew Thompson 92902ac6454SAndrew Thompson if (status->dx || status->dy || status->dz /* || status->dt */ ) { 93002ac6454SAndrew Thompson status->flags |= MOUSE_POSCHANGED; 93102ac6454SAndrew Thompson } 93202ac6454SAndrew Thompson if (status->button != status->obutton) { 93302ac6454SAndrew Thompson status->flags |= MOUSE_BUTTONSCHANGED; 93402ac6454SAndrew Thompson } 93502ac6454SAndrew Thompson break; 93602ac6454SAndrew Thompson } 93702ac6454SAndrew Thompson default: 93802ac6454SAndrew Thompson error = ENOTTY; 93902ac6454SAndrew Thompson } 94002ac6454SAndrew Thompson 94102ac6454SAndrew Thompson done: 94202ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx); 94302ac6454SAndrew Thompson return (error); 94402ac6454SAndrew Thompson } 94502ac6454SAndrew Thompson 9464ed0304cSAndrew Thompson static int 9474ed0304cSAndrew Thompson ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS) 9484ed0304cSAndrew Thompson { 9494ed0304cSAndrew Thompson struct ums_softc *sc = arg1; 9504ed0304cSAndrew Thompson struct ums_info *info; 9514ed0304cSAndrew Thompson struct sbuf *sb; 952b635069cSHans Petter Selasky int i, j, err, had_output; 9534ed0304cSAndrew Thompson 9544ed0304cSAndrew Thompson sb = sbuf_new_auto(); 955b635069cSHans Petter Selasky for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) { 9564ed0304cSAndrew Thompson info = &sc->sc_info[i]; 9574ed0304cSAndrew Thompson 9584ed0304cSAndrew Thompson /* Don't emit empty info */ 9594ed0304cSAndrew Thompson if ((info->sc_flags & 9604ed0304cSAndrew Thompson (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS | 9614ed0304cSAndrew Thompson UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 && 9624ed0304cSAndrew Thompson info->sc_buttons == 0) 9634ed0304cSAndrew Thompson continue; 9644ed0304cSAndrew Thompson 965b635069cSHans Petter Selasky if (had_output) 966b635069cSHans Petter Selasky sbuf_printf(sb, "\n"); 967b635069cSHans Petter Selasky had_output = 1; 9684ed0304cSAndrew Thompson sbuf_printf(sb, "i%d:", i + 1); 9694ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_X_AXIS) 9704ed0304cSAndrew Thompson sbuf_printf(sb, " X:r%d, p%d, s%d;", 9714ed0304cSAndrew Thompson (int)info->sc_iid_x, 9724ed0304cSAndrew Thompson (int)info->sc_loc_x.pos, 9734ed0304cSAndrew Thompson (int)info->sc_loc_x.size); 9744ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_Y_AXIS) 9754ed0304cSAndrew Thompson sbuf_printf(sb, " Y:r%d, p%d, s%d;", 9764ed0304cSAndrew Thompson (int)info->sc_iid_y, 9774ed0304cSAndrew Thompson (int)info->sc_loc_y.pos, 9784ed0304cSAndrew Thompson (int)info->sc_loc_y.size); 9794ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_Z_AXIS) 9804ed0304cSAndrew Thompson sbuf_printf(sb, " Z:r%d, p%d, s%d;", 9814ed0304cSAndrew Thompson (int)info->sc_iid_z, 9824ed0304cSAndrew Thompson (int)info->sc_loc_z.pos, 9834ed0304cSAndrew Thompson (int)info->sc_loc_z.size); 9844ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_T_AXIS) 9854ed0304cSAndrew Thompson sbuf_printf(sb, " T:r%d, p%d, s%d;", 9864ed0304cSAndrew Thompson (int)info->sc_iid_t, 9874ed0304cSAndrew Thompson (int)info->sc_loc_t.pos, 9884ed0304cSAndrew Thompson (int)info->sc_loc_t.size); 9894ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_W_AXIS) 9904ed0304cSAndrew Thompson sbuf_printf(sb, " W:r%d, p%d, s%d;", 9914ed0304cSAndrew Thompson (int)info->sc_iid_w, 9924ed0304cSAndrew Thompson (int)info->sc_loc_w.pos, 9934ed0304cSAndrew Thompson (int)info->sc_loc_w.size); 9944ed0304cSAndrew Thompson 9954ed0304cSAndrew Thompson for (j = 0; j < info->sc_buttons; j++) { 9964ed0304cSAndrew Thompson sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1, 9974ed0304cSAndrew Thompson (int)info->sc_iid_btn[j], 9984ed0304cSAndrew Thompson (int)info->sc_loc_btn[j].pos, 9994ed0304cSAndrew Thompson (int)info->sc_loc_btn[j].size); 10004ed0304cSAndrew Thompson } 10014ed0304cSAndrew Thompson } 10024ed0304cSAndrew Thompson sbuf_finish(sb); 10034ed0304cSAndrew Thompson err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 10044ed0304cSAndrew Thompson sbuf_delete(sb); 10054ed0304cSAndrew Thompson 10064ed0304cSAndrew Thompson return (err); 10074ed0304cSAndrew Thompson } 10084ed0304cSAndrew Thompson 100902ac6454SAndrew Thompson static devclass_t ums_devclass; 101002ac6454SAndrew Thompson 101102ac6454SAndrew Thompson static device_method_t ums_methods[] = { 101202ac6454SAndrew Thompson DEVMETHOD(device_probe, ums_probe), 101302ac6454SAndrew Thompson DEVMETHOD(device_attach, ums_attach), 101402ac6454SAndrew Thompson DEVMETHOD(device_detach, ums_detach), 101502ac6454SAndrew Thompson {0, 0} 101602ac6454SAndrew Thompson }; 101702ac6454SAndrew Thompson 101802ac6454SAndrew Thompson static driver_t ums_driver = { 101902ac6454SAndrew Thompson .name = "ums", 102002ac6454SAndrew Thompson .methods = ums_methods, 102102ac6454SAndrew Thompson .size = sizeof(struct ums_softc), 102202ac6454SAndrew Thompson }; 102302ac6454SAndrew Thompson 10249aef556dSAndrew Thompson DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0); 102502ac6454SAndrew Thompson MODULE_DEPEND(ums, usb, 1, 1, 1); 1026910cb8feSAndrew Thompson MODULE_VERSION(ums, 1); 1027