102ac6454SAndrew Thompson /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 402ac6454SAndrew Thompson * Copyright (c) 1998 The NetBSD Foundation, Inc. 502ac6454SAndrew Thompson * All rights reserved. 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation 802ac6454SAndrew Thompson * by Lennart Augustsson (lennart@augustsson.net) at 902ac6454SAndrew Thompson * Carlstedt Research & Technology. 1002ac6454SAndrew Thompson * 1102ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 1202ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 1302ac6454SAndrew Thompson * are met: 1402ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1502ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1602ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1702ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1802ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1902ac6454SAndrew Thompson * 2002ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2102ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2202ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2302ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2402ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2502ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2602ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2702ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2802ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2902ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3002ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE. 3102ac6454SAndrew Thompson */ 3202ac6454SAndrew Thompson 3302ac6454SAndrew Thompson #include <sys/cdefs.h> 3402ac6454SAndrew Thompson /* 3502ac6454SAndrew Thompson * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 3602ac6454SAndrew Thompson */ 3702ac6454SAndrew Thompson 38c3af259aSOleksandr Tymoshenko #include "opt_evdev.h" 39c3af259aSOleksandr Tymoshenko 40ed6d949aSAndrew Thompson #include <sys/stdint.h> 41ed6d949aSAndrew Thompson #include <sys/stddef.h> 42ed6d949aSAndrew Thompson #include <sys/param.h> 43ed6d949aSAndrew Thompson #include <sys/queue.h> 44ed6d949aSAndrew Thompson #include <sys/types.h> 45ed6d949aSAndrew Thompson #include <sys/systm.h> 46ed6d949aSAndrew Thompson #include <sys/kernel.h> 47ed6d949aSAndrew Thompson #include <sys/bus.h> 48ed6d949aSAndrew Thompson #include <sys/module.h> 49ed6d949aSAndrew Thompson #include <sys/lock.h> 50ed6d949aSAndrew Thompson #include <sys/mutex.h> 51ed6d949aSAndrew Thompson #include <sys/condvar.h> 52ed6d949aSAndrew Thompson #include <sys/sysctl.h> 53ed6d949aSAndrew Thompson #include <sys/sx.h> 54ed6d949aSAndrew Thompson #include <sys/unistd.h> 55ed6d949aSAndrew Thompson #include <sys/callout.h> 56ed6d949aSAndrew Thompson #include <sys/malloc.h> 57ed6d949aSAndrew Thompson #include <sys/priv.h> 58ed6d949aSAndrew Thompson #include <sys/conf.h> 59ed6d949aSAndrew Thompson #include <sys/fcntl.h> 604ed0304cSAndrew Thompson #include <sys/sbuf.h> 61ed6d949aSAndrew Thompson 62eead9017SVladimir Kondratyev #include <dev/hid/hid.h> 63eead9017SVladimir Kondratyev 6402ac6454SAndrew Thompson #include <dev/usb/usb.h> 65ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 66ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 6702ac6454SAndrew Thompson #include <dev/usb/usbhid.h> 68ed6d949aSAndrew Thompson #include "usbdevs.h" 6902ac6454SAndrew Thompson 7002ac6454SAndrew Thompson #define USB_DEBUG_VAR ums_debug 7102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 7202ac6454SAndrew Thompson 7302ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 7402ac6454SAndrew Thompson 75a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 76c3af259aSOleksandr Tymoshenko #include <dev/evdev/input.h> 77c3af259aSOleksandr Tymoshenko #include <dev/evdev/evdev.h> 78c3af259aSOleksandr Tymoshenko #endif 79c3af259aSOleksandr Tymoshenko 8002ac6454SAndrew Thompson #include <sys/ioccom.h> 8102ac6454SAndrew Thompson #include <sys/filio.h> 8202ac6454SAndrew Thompson #include <sys/mouse.h> 8302ac6454SAndrew Thompson 84b850ecc1SAndrew Thompson #ifdef USB_DEBUG 8502ac6454SAndrew Thompson static int ums_debug = 0; 8602ac6454SAndrew Thompson 87f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 88f8d2b1f3SPawel Biernacki "USB ums"); 89ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN, 9002ac6454SAndrew Thompson &ums_debug, 0, "Debug level"); 9102ac6454SAndrew Thompson #endif 9202ac6454SAndrew Thompson 9302ac6454SAndrew Thompson #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE) 9402ac6454SAndrew Thompson #define MOUSE_FLAGS (HIO_RELATIVE) 9502ac6454SAndrew Thompson 9602ac6454SAndrew Thompson #define UMS_BUF_SIZE 8 /* bytes */ 9702ac6454SAndrew Thompson #define UMS_IFQ_MAXLEN 50 /* units */ 9802ac6454SAndrew Thompson #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */ 9902ac6454SAndrew Thompson #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i)) 100de967835SAndrew Thompson #define UMS_INFO_MAX 2 /* maximum number of HID sets */ 10102ac6454SAndrew Thompson 10202ac6454SAndrew Thompson enum { 10302ac6454SAndrew Thompson UMS_INTR_DT, 1043e5e312aSAndrew Thompson UMS_N_TRANSFER, 10502ac6454SAndrew Thompson }; 10602ac6454SAndrew Thompson 107de967835SAndrew Thompson struct ums_info { 10802ac6454SAndrew Thompson struct hid_location sc_loc_w; 10902ac6454SAndrew Thompson struct hid_location sc_loc_x; 11002ac6454SAndrew Thompson struct hid_location sc_loc_y; 11102ac6454SAndrew Thompson struct hid_location sc_loc_z; 11202ac6454SAndrew Thompson struct hid_location sc_loc_t; 11302ac6454SAndrew Thompson struct hid_location sc_loc_btn[UMS_BUTTON_MAX]; 11402ac6454SAndrew Thompson 11502ac6454SAndrew Thompson uint32_t sc_flags; 11602ac6454SAndrew Thompson #define UMS_FLAG_X_AXIS 0x0001 11702ac6454SAndrew Thompson #define UMS_FLAG_Y_AXIS 0x0002 11802ac6454SAndrew Thompson #define UMS_FLAG_Z_AXIS 0x0004 11902ac6454SAndrew Thompson #define UMS_FLAG_T_AXIS 0x0008 12002ac6454SAndrew Thompson #define UMS_FLAG_SBU 0x0010 /* spurious button up events */ 1213e5e312aSAndrew Thompson #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */ 1223e5e312aSAndrew Thompson #define UMS_FLAG_W_AXIS 0x0040 123ab4f740bSVladimir Kondratyev #define UMS_FLAG_VBTN 0x0080 /* Buttons in vendor usage page */ 12402ac6454SAndrew Thompson 125bf87f556SAndrew Thompson uint8_t sc_iid_w; 126bf87f556SAndrew Thompson uint8_t sc_iid_x; 127bf87f556SAndrew Thompson uint8_t sc_iid_y; 128bf87f556SAndrew Thompson uint8_t sc_iid_z; 129bf87f556SAndrew Thompson uint8_t sc_iid_t; 130bf87f556SAndrew Thompson uint8_t sc_iid_btn[UMS_BUTTON_MAX]; 131de967835SAndrew Thompson uint8_t sc_buttons; 132de967835SAndrew Thompson }; 133de967835SAndrew Thompson 134de967835SAndrew Thompson struct ums_softc { 135760bc48eSAndrew Thompson struct usb_fifo_sc sc_fifo; 136de967835SAndrew Thompson struct mtx sc_mtx; 137760bc48eSAndrew Thompson struct usb_callout sc_callout; 138de967835SAndrew Thompson struct ums_info sc_info[UMS_INFO_MAX]; 139de967835SAndrew Thompson 140de967835SAndrew Thompson mousehw_t sc_hw; 141de967835SAndrew Thompson mousemode_t sc_mode; 142de967835SAndrew Thompson mousestatus_t sc_status; 143de967835SAndrew Thompson 144760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UMS_N_TRANSFER]; 145de967835SAndrew Thompson 14681f82884SAlfred Perlstein int sc_pollrate; 14732e79f55SHans Petter Selasky int sc_fflags; 148a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 149c3af259aSOleksandr Tymoshenko int sc_evflags; 150c3af259aSOleksandr Tymoshenko #define UMS_EVDEV_OPENED 1 151c3af259aSOleksandr Tymoshenko #endif 15281f82884SAlfred Perlstein 153de967835SAndrew Thompson uint8_t sc_buttons; 154de967835SAndrew Thompson uint8_t sc_iid; 15502ac6454SAndrew Thompson uint8_t sc_temp[64]; 156c3af259aSOleksandr Tymoshenko 157a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 158c3af259aSOleksandr Tymoshenko struct evdev_dev *sc_evdev; 159c3af259aSOleksandr Tymoshenko #endif 16002ac6454SAndrew Thompson }; 16102ac6454SAndrew Thompson 16202ac6454SAndrew Thompson static void ums_put_queue_timeout(void *__sc); 16302ac6454SAndrew Thompson 164e0a69b51SAndrew Thompson static usb_callback_t ums_intr_callback; 16502ac6454SAndrew Thompson 16602ac6454SAndrew Thompson static device_probe_t ums_probe; 16702ac6454SAndrew Thompson static device_attach_t ums_attach; 16802ac6454SAndrew Thompson static device_detach_t ums_detach; 16902ac6454SAndrew Thompson 170c3af259aSOleksandr Tymoshenko static usb_fifo_cmd_t ums_fifo_start_read; 171c3af259aSOleksandr Tymoshenko static usb_fifo_cmd_t ums_fifo_stop_read; 172c3af259aSOleksandr Tymoshenko static usb_fifo_open_t ums_fifo_open; 173c3af259aSOleksandr Tymoshenko static usb_fifo_close_t ums_fifo_close; 174c3af259aSOleksandr Tymoshenko static usb_fifo_ioctl_t ums_fifo_ioctl; 17502ac6454SAndrew Thompson 176a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 177c3af259aSOleksandr Tymoshenko static evdev_open_t ums_ev_open; 178c3af259aSOleksandr Tymoshenko static evdev_close_t ums_ev_close; 179e012a280SOleksandr Tymoshenko static void ums_evdev_push(struct ums_softc *, int32_t, int32_t, 180e012a280SOleksandr Tymoshenko int32_t, int32_t, int32_t); 181c3af259aSOleksandr Tymoshenko #endif 182c3af259aSOleksandr Tymoshenko 183c3af259aSOleksandr Tymoshenko static void ums_start_rx(struct ums_softc *); 184c3af259aSOleksandr Tymoshenko static void ums_stop_rx(struct ums_softc *); 1854ed0304cSAndrew Thompson static void ums_put_queue(struct ums_softc *, int32_t, int32_t, 1864ed0304cSAndrew Thompson int32_t, int32_t, int32_t); 1874ed0304cSAndrew Thompson static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS); 18802ac6454SAndrew Thompson 189760bc48eSAndrew Thompson static struct usb_fifo_methods ums_fifo_methods = { 190c3af259aSOleksandr Tymoshenko .f_open = &ums_fifo_open, 191c3af259aSOleksandr Tymoshenko .f_close = &ums_fifo_close, 192c3af259aSOleksandr Tymoshenko .f_ioctl = &ums_fifo_ioctl, 193c3af259aSOleksandr Tymoshenko .f_start_read = &ums_fifo_start_read, 194c3af259aSOleksandr Tymoshenko .f_stop_read = &ums_fifo_stop_read, 19502ac6454SAndrew Thompson .basename[0] = "ums", 19602ac6454SAndrew Thompson }; 19702ac6454SAndrew Thompson 198a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 1995163e77fSOleksandr Tymoshenko static const struct evdev_methods ums_evdev_methods = { 200c3af259aSOleksandr Tymoshenko .ev_open = &ums_ev_open, 201c3af259aSOleksandr Tymoshenko .ev_close = &ums_ev_close, 202c3af259aSOleksandr Tymoshenko }; 203c3af259aSOleksandr Tymoshenko #endif 204c3af259aSOleksandr Tymoshenko 20502ac6454SAndrew Thompson static void 20602ac6454SAndrew Thompson ums_put_queue_timeout(void *__sc) 20702ac6454SAndrew Thompson { 20802ac6454SAndrew Thompson struct ums_softc *sc = __sc; 20902ac6454SAndrew Thompson 21002ac6454SAndrew Thompson mtx_assert(&sc->sc_mtx, MA_OWNED); 21102ac6454SAndrew Thompson 21202ac6454SAndrew Thompson ums_put_queue(sc, 0, 0, 0, 0, 0); 213e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 214e012a280SOleksandr Tymoshenko ums_evdev_push(sc, 0, 0, 0, 0, 0); 215e012a280SOleksandr Tymoshenko #endif 21602ac6454SAndrew Thompson } 21702ac6454SAndrew Thompson 21802ac6454SAndrew Thompson static void 219ed6d949aSAndrew Thompson ums_intr_callback(struct usb_xfer *xfer, usb_error_t error) 22002ac6454SAndrew Thompson { 221ed6d949aSAndrew Thompson struct ums_softc *sc = usbd_xfer_softc(xfer); 222de967835SAndrew Thompson struct ums_info *info = &sc->sc_info[0]; 223ed6d949aSAndrew Thompson struct usb_page_cache *pc; 22402ac6454SAndrew Thompson uint8_t *buf = sc->sc_temp; 22502ac6454SAndrew Thompson int32_t buttons = 0; 22681f82884SAlfred Perlstein int32_t buttons_found = 0; 227e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 228e012a280SOleksandr Tymoshenko int32_t buttons_reported = 0; 229e012a280SOleksandr Tymoshenko #endif 230de967835SAndrew Thompson int32_t dw = 0; 231de967835SAndrew Thompson int32_t dx = 0; 232de967835SAndrew Thompson int32_t dy = 0; 233de967835SAndrew Thompson int32_t dz = 0; 234de967835SAndrew Thompson int32_t dt = 0; 23502ac6454SAndrew Thompson uint8_t i; 236bf87f556SAndrew Thompson uint8_t id; 237ed6d949aSAndrew Thompson int len; 238ed6d949aSAndrew Thompson 239ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 24002ac6454SAndrew Thompson 24102ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 24202ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 24302ac6454SAndrew Thompson DPRINTFN(6, "sc=%p actlen=%d\n", sc, len); 24402ac6454SAndrew Thompson 2456d917491SHans Petter Selasky if (len > (int)sizeof(sc->sc_temp)) { 24602ac6454SAndrew Thompson DPRINTFN(6, "truncating large packet to %zu bytes\n", 24702ac6454SAndrew Thompson sizeof(sc->sc_temp)); 24802ac6454SAndrew Thompson len = sizeof(sc->sc_temp); 24902ac6454SAndrew Thompson } 2503e5e312aSAndrew Thompson if (len == 0) 25102ac6454SAndrew Thompson goto tr_setup; 2523e5e312aSAndrew Thompson 253ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 254ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, len); 25502ac6454SAndrew Thompson 25602ac6454SAndrew Thompson DPRINTFN(6, "data = %02x %02x %02x %02x " 25702ac6454SAndrew Thompson "%02x %02x %02x %02x\n", 25802ac6454SAndrew Thompson (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0, 25902ac6454SAndrew Thompson (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0, 26002ac6454SAndrew Thompson (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0, 26102ac6454SAndrew Thompson (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0); 26202ac6454SAndrew Thompson 26302ac6454SAndrew Thompson if (sc->sc_iid) { 264bf87f556SAndrew Thompson id = *buf; 26502ac6454SAndrew Thompson 26602ac6454SAndrew Thompson len--; 26702ac6454SAndrew Thompson buf++; 26802ac6454SAndrew Thompson 26902ac6454SAndrew Thompson } else { 270bf87f556SAndrew Thompson id = 0; 271de967835SAndrew Thompson if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) { 27202ac6454SAndrew Thompson if ((*buf == 0x14) || (*buf == 0x15)) { 27302ac6454SAndrew Thompson goto tr_setup; 27402ac6454SAndrew Thompson } 27502ac6454SAndrew Thompson } 27602ac6454SAndrew Thompson } 27702ac6454SAndrew Thompson 278de967835SAndrew Thompson repeat: 279de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_W_AXIS) && 280de967835SAndrew Thompson (id == info->sc_iid_w)) 281de967835SAndrew Thompson dw += hid_get_data(buf, len, &info->sc_loc_w); 28202ac6454SAndrew Thompson 283de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_X_AXIS) && 284de967835SAndrew Thompson (id == info->sc_iid_x)) 285de967835SAndrew Thompson dx += hid_get_data(buf, len, &info->sc_loc_x); 28602ac6454SAndrew Thompson 287de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_Y_AXIS) && 288de967835SAndrew Thompson (id == info->sc_iid_y)) 289e9517d24SPeter Grehan dy -= hid_get_data(buf, len, &info->sc_loc_y); 29002ac6454SAndrew Thompson 291de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_Z_AXIS) && 292de967835SAndrew Thompson (id == info->sc_iid_z)) { 293de967835SAndrew Thompson int32_t temp; 294de967835SAndrew Thompson temp = hid_get_data(buf, len, &info->sc_loc_z); 295de967835SAndrew Thompson if (info->sc_flags & UMS_FLAG_REVZ) 296de967835SAndrew Thompson temp = -temp; 297de967835SAndrew Thompson dz -= temp; 298de967835SAndrew Thompson } 29902ac6454SAndrew Thompson 300de967835SAndrew Thompson if ((info->sc_flags & UMS_FLAG_T_AXIS) && 301e2efc9beSHans Petter Selasky (id == info->sc_iid_t)) { 3027707483aSVladimir Kondratyev dt += hid_get_data(buf, len, &info->sc_loc_t); 303e2efc9beSHans Petter Selasky /* T-axis is translated into button presses */ 304e2efc9beSHans Petter Selasky buttons_found |= (1UL << 5) | (1UL << 6); 305e2efc9beSHans Petter Selasky } 306bf87f556SAndrew Thompson 307de967835SAndrew Thompson for (i = 0; i < info->sc_buttons; i++) { 30881f82884SAlfred Perlstein uint32_t mask; 30981f82884SAlfred Perlstein mask = 1UL << UMS_BUT(i); 31081f82884SAlfred Perlstein /* check for correct button ID */ 311de967835SAndrew Thompson if (id != info->sc_iid_btn[i]) 312bf87f556SAndrew Thompson continue; 31381f82884SAlfred Perlstein /* check for button pressed */ 31481f82884SAlfred Perlstein if (hid_get_data(buf, len, &info->sc_loc_btn[i])) 31581f82884SAlfred Perlstein buttons |= mask; 31681f82884SAlfred Perlstein /* register button mask */ 31781f82884SAlfred Perlstein buttons_found |= mask; 31802ac6454SAndrew Thompson } 31902ac6454SAndrew Thompson 320de967835SAndrew Thompson if (++info != &sc->sc_info[UMS_INFO_MAX]) 321de967835SAndrew Thompson goto repeat; 322de967835SAndrew Thompson 323*d6888e21SNiko Sonack /* keep old button value(s) for non-detected buttons */ 324*d6888e21SNiko Sonack buttons |= sc->sc_status.button & ~buttons_found; 325*d6888e21SNiko Sonack 326e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 327e012a280SOleksandr Tymoshenko buttons_reported = buttons; 328e012a280SOleksandr Tymoshenko #endif 32981f82884SAlfred Perlstein 33002ac6454SAndrew Thompson if (dx || dy || dz || dt || dw || 33102ac6454SAndrew Thompson (buttons != sc->sc_status.button)) { 33202ac6454SAndrew Thompson DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n", 33302ac6454SAndrew Thompson dx, dy, dz, dt, dw, buttons); 33402ac6454SAndrew Thompson 335300f96f1SAndrew Thompson /* translate T-axis into button presses until further */ 336d03403c4SHans Petter Selasky if (dt > 0) { 337d03403c4SHans Petter Selasky ums_put_queue(sc, 0, 0, 0, 0, buttons); 3387707483aSVladimir Kondratyev buttons |= 1UL << 6; 339d03403c4SHans Petter Selasky } else if (dt < 0) { 340d03403c4SHans Petter Selasky ums_put_queue(sc, 0, 0, 0, 0, buttons); 3417707483aSVladimir Kondratyev buttons |= 1UL << 5; 342d03403c4SHans Petter Selasky } 343300f96f1SAndrew Thompson 34402ac6454SAndrew Thompson sc->sc_status.button = buttons; 34502ac6454SAndrew Thompson sc->sc_status.dx += dx; 34602ac6454SAndrew Thompson sc->sc_status.dy += dy; 34702ac6454SAndrew Thompson sc->sc_status.dz += dz; 34802ac6454SAndrew Thompson /* 34902ac6454SAndrew Thompson * sc->sc_status.dt += dt; 35002ac6454SAndrew Thompson * no way to export this yet 35102ac6454SAndrew Thompson */ 35202ac6454SAndrew Thompson 35302ac6454SAndrew Thompson /* 3543e5e312aSAndrew Thompson * The Qtronix keyboard has a built in PS/2 3553e5e312aSAndrew Thompson * port for a mouse. The firmware once in a 3563e5e312aSAndrew Thompson * while posts a spurious button up 3573e5e312aSAndrew Thompson * event. This event we ignore by doing a 3583e5e312aSAndrew Thompson * timeout for 50 msecs. If we receive 3593e5e312aSAndrew Thompson * dx=dy=dz=buttons=0 before we add the event 3603e5e312aSAndrew Thompson * to the queue. In any other case we delete 3613e5e312aSAndrew Thompson * the timeout event. 36202ac6454SAndrew Thompson */ 363de967835SAndrew Thompson if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) && 36402ac6454SAndrew Thompson (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) && 36502ac6454SAndrew Thompson (dw == 0) && (buttons == 0)) { 366a593f6b8SAndrew Thompson usb_callout_reset(&sc->sc_callout, hz / 20, 36702ac6454SAndrew Thompson &ums_put_queue_timeout, sc); 36802ac6454SAndrew Thompson } else { 369a593f6b8SAndrew Thompson usb_callout_stop(&sc->sc_callout); 37002ac6454SAndrew Thompson 37102ac6454SAndrew Thompson ums_put_queue(sc, dx, dy, dz, dt, buttons); 372e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 373e012a280SOleksandr Tymoshenko ums_evdev_push(sc, dx, dy, dz, dt, 374e012a280SOleksandr Tymoshenko buttons_reported); 375e012a280SOleksandr Tymoshenko #endif 37602ac6454SAndrew Thompson } 37702ac6454SAndrew Thompson } 37802ac6454SAndrew Thompson case USB_ST_SETUP: 37902ac6454SAndrew Thompson tr_setup: 38002ac6454SAndrew Thompson /* check if we can put more data into the FIFO */ 381c3af259aSOleksandr Tymoshenko if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) { 382a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 38305ab03a3SVladimir Kondratyev if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0) 384c3af259aSOleksandr Tymoshenko break; 385c3af259aSOleksandr Tymoshenko #else 386c3af259aSOleksandr Tymoshenko break; 387c3af259aSOleksandr Tymoshenko #endif 388c3af259aSOleksandr Tymoshenko } 389c3af259aSOleksandr Tymoshenko 390ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 391a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 3923e5e312aSAndrew Thompson break; 39302ac6454SAndrew Thompson 39402ac6454SAndrew Thompson default: /* Error */ 395ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 3963e5e312aSAndrew Thompson /* try clear stall first */ 397ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 3983e5e312aSAndrew Thompson goto tr_setup; 39902ac6454SAndrew Thompson } 4003e5e312aSAndrew Thompson break; 40102ac6454SAndrew Thompson } 40202ac6454SAndrew Thompson } 40302ac6454SAndrew Thompson 404760bc48eSAndrew Thompson static const struct usb_config ums_config[UMS_N_TRANSFER] = { 40502ac6454SAndrew Thompson [UMS_INTR_DT] = { 40602ac6454SAndrew Thompson .type = UE_INTERRUPT, 40702ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 40802ac6454SAndrew Thompson .direction = UE_DIR_IN, 4094eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 4104eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 4114eae601eSAndrew Thompson .callback = &ums_intr_callback, 41202ac6454SAndrew Thompson }, 41302ac6454SAndrew Thompson }; 41402ac6454SAndrew Thompson 4159c916c62SHans Petter Selasky /* A match on these entries will load ums */ 4169c916c62SHans Petter Selasky static const STRUCT_USB_HOST_ID __used ums_devs[] = { 4179c916c62SHans Petter Selasky {USB_IFACE_CLASS(UICLASS_HID), 4189c916c62SHans Petter Selasky USB_IFACE_SUBCLASS(UISUBCLASS_BOOT), 4199c916c62SHans Petter Selasky USB_IFACE_PROTOCOL(UIPROTO_MOUSE),}, 4209c916c62SHans Petter Selasky }; 4219c916c62SHans Petter Selasky 42202ac6454SAndrew Thompson static int 42302ac6454SAndrew Thompson ums_probe(device_t dev) 42402ac6454SAndrew Thompson { 425760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 42602ac6454SAndrew Thompson void *d_ptr; 42760867f57SHans Petter Selasky int error; 42802ac6454SAndrew Thompson uint16_t d_len; 42902ac6454SAndrew Thompson 43002ac6454SAndrew Thompson DPRINTFN(11, "\n"); 43102ac6454SAndrew Thompson 432f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) 43302ac6454SAndrew Thompson return (ENXIO); 4343e5e312aSAndrew Thompson 435fd4f10ceSAndrew Thompson if (uaa->info.bInterfaceClass != UICLASS_HID) 43602ac6454SAndrew Thompson return (ENXIO); 4373e5e312aSAndrew Thompson 438d7dd1341SHans Petter Selasky if (usb_test_quirk(uaa, UQ_UMS_IGNORE)) 439d7dd1341SHans Petter Selasky return (ENXIO); 440d7dd1341SHans Petter Selasky 441fd4f10ceSAndrew Thompson if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 442fd4f10ceSAndrew Thompson (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE)) 443cd10bffaSAndriy Gapon return (BUS_PROBE_DEFAULT); 444fd4f10ceSAndrew Thompson 445a593f6b8SAndrew Thompson error = usbd_req_get_hid_desc(uaa->device, NULL, 44602ac6454SAndrew Thompson &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 44702ac6454SAndrew Thompson 4483e5e312aSAndrew Thompson if (error) 44902ac6454SAndrew Thompson return (ENXIO); 4503e5e312aSAndrew Thompson 45160867f57SHans Petter Selasky if (hid_is_mouse(d_ptr, d_len)) 45260867f57SHans Petter Selasky error = BUS_PROBE_DEFAULT; 45360867f57SHans Petter Selasky else 45460867f57SHans Petter Selasky error = ENXIO; 45560867f57SHans Petter Selasky 45602ac6454SAndrew Thompson free(d_ptr, M_TEMP); 45760867f57SHans Petter Selasky return (error); 45802ac6454SAndrew Thompson } 45902ac6454SAndrew Thompson 460de967835SAndrew Thompson static void 461de967835SAndrew Thompson ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf, 462de967835SAndrew Thompson uint16_t len, uint8_t index) 463de967835SAndrew Thompson { 464de967835SAndrew Thompson struct ums_info *info = &sc->sc_info[index]; 465de967835SAndrew Thompson uint32_t flags; 466de967835SAndrew Thompson uint8_t i; 467ee6e1a6fSAndrew Thompson uint8_t j; 468de967835SAndrew Thompson 469de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 470de967835SAndrew Thompson hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) { 471de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 472de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_X_AXIS; 473de967835SAndrew Thompson } 474de967835SAndrew Thompson } 475de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 476de967835SAndrew Thompson hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) { 477de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 478de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_Y_AXIS; 479de967835SAndrew Thompson } 480de967835SAndrew Thompson } 481de967835SAndrew Thompson /* Try the wheel first as the Z activator since it's tradition. */ 482de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 483de967835SAndrew Thompson HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags, 484de967835SAndrew Thompson &info->sc_iid_z) || 485de967835SAndrew Thompson hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 486de967835SAndrew Thompson HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags, 487de967835SAndrew Thompson &info->sc_iid_z)) { 488de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 489de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_Z_AXIS; 490de967835SAndrew Thompson } 491de967835SAndrew Thompson /* 492de967835SAndrew Thompson * We might have both a wheel and Z direction, if so put 493de967835SAndrew Thompson * put the Z on the W coordinate. 494de967835SAndrew Thompson */ 495de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 496de967835SAndrew Thompson HUG_Z), hid_input, index, &info->sc_loc_w, &flags, 497de967835SAndrew Thompson &info->sc_iid_w)) { 498de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 499de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_W_AXIS; 500de967835SAndrew Thompson } 501de967835SAndrew Thompson } 502de967835SAndrew Thompson } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 503de967835SAndrew Thompson HUG_Z), hid_input, index, &info->sc_loc_z, &flags, 504de967835SAndrew Thompson &info->sc_iid_z)) { 505de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 506de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_Z_AXIS; 507de967835SAndrew Thompson } 508de967835SAndrew Thompson } 509de967835SAndrew Thompson /* 510de967835SAndrew Thompson * The Microsoft Wireless Intellimouse 2.0 reports it's wheel 511de967835SAndrew Thompson * using 0x0048, which is HUG_TWHEEL, and seems to expect you 512de967835SAndrew Thompson * to know that the byte after the wheel is the tilt axis. 513de967835SAndrew Thompson * There are no other HID axis descriptors other than X,Y and 514de967835SAndrew Thompson * TWHEEL 515de967835SAndrew Thompson */ 516de967835SAndrew Thompson if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 517de967835SAndrew Thompson HUG_TWHEEL), hid_input, index, &info->sc_loc_t, 518de967835SAndrew Thompson &flags, &info->sc_iid_t)) { 519de967835SAndrew Thompson info->sc_loc_t.pos += 8; 520de967835SAndrew Thompson 521de967835SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 522de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_T_AXIS; 523de967835SAndrew Thompson } 524300f96f1SAndrew Thompson } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER, 525300f96f1SAndrew Thompson HUC_AC_PAN), hid_input, index, &info->sc_loc_t, 526300f96f1SAndrew Thompson &flags, &info->sc_iid_t)) { 527300f96f1SAndrew Thompson if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) 528300f96f1SAndrew Thompson info->sc_flags |= UMS_FLAG_T_AXIS; 529de967835SAndrew Thompson } 530de967835SAndrew Thompson /* figure out the number of buttons */ 531de967835SAndrew Thompson 532de967835SAndrew Thompson for (i = 0; i < UMS_BUTTON_MAX; i++) { 533de967835SAndrew Thompson if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)), 534de967835SAndrew Thompson hid_input, index, &info->sc_loc_btn[i], NULL, 535de967835SAndrew Thompson &info->sc_iid_btn[i])) { 536de967835SAndrew Thompson break; 537de967835SAndrew Thompson } 538de967835SAndrew Thompson } 539ee6e1a6fSAndrew Thompson 540ee6e1a6fSAndrew Thompson /* detect other buttons */ 541ab4f740bSVladimir Kondratyev if (info->sc_flags & UMS_FLAG_VBTN) { 5421f27b685SAndrew Thompson for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) { 543ab4f740bSVladimir Kondratyev if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, 544ab4f740bSVladimir Kondratyev (j + 1)), hid_input, index, &info->sc_loc_btn[i], 545ab4f740bSVladimir Kondratyev NULL, &info->sc_iid_btn[i])) { 546ee6e1a6fSAndrew Thompson break; 547ee6e1a6fSAndrew Thompson } 548ee6e1a6fSAndrew Thompson } 549ab4f740bSVladimir Kondratyev } 550ee6e1a6fSAndrew Thompson 551de967835SAndrew Thompson info->sc_buttons = i; 552de967835SAndrew Thompson 553de967835SAndrew Thompson if (i > sc->sc_buttons) 554de967835SAndrew Thompson sc->sc_buttons = i; 555de967835SAndrew Thompson 556de967835SAndrew Thompson if (info->sc_flags == 0) 557de967835SAndrew Thompson return; 558de967835SAndrew Thompson 559de967835SAndrew Thompson /* announce information about the mouse */ 560de967835SAndrew Thompson device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n", 561de967835SAndrew Thompson (info->sc_buttons), 562de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "", 563de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "", 564de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "", 565de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "", 566de967835SAndrew Thompson (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "", 567de967835SAndrew Thompson info->sc_iid_x); 568de967835SAndrew Thompson } 569de967835SAndrew Thompson 57002ac6454SAndrew Thompson static int 57102ac6454SAndrew Thompson ums_attach(device_t dev) 57202ac6454SAndrew Thompson { 573760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 57402ac6454SAndrew Thompson struct ums_softc *sc = device_get_softc(dev); 575de967835SAndrew Thompson struct ums_info *info; 57602ac6454SAndrew Thompson void *d_ptr = NULL; 5773e5e312aSAndrew Thompson int isize; 5783e5e312aSAndrew Thompson int err; 57902ac6454SAndrew Thompson uint16_t d_len; 58002ac6454SAndrew Thompson uint8_t i; 581b850ecc1SAndrew Thompson #ifdef USB_DEBUG 582de967835SAndrew Thompson uint8_t j; 583b850ecc1SAndrew Thompson #endif 58402ac6454SAndrew Thompson 58502ac6454SAndrew Thompson DPRINTFN(11, "sc=%p\n", sc); 58602ac6454SAndrew Thompson 587a593f6b8SAndrew Thompson device_set_usb_desc(dev); 58802ac6454SAndrew Thompson 58902ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE); 59002ac6454SAndrew Thompson 591a593f6b8SAndrew Thompson usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 59202ac6454SAndrew Thompson 59302ac6454SAndrew Thompson /* 59402ac6454SAndrew Thompson * Force the report (non-boot) protocol. 59502ac6454SAndrew Thompson * 59602ac6454SAndrew Thompson * Mice without boot protocol support may choose not to implement 59702ac6454SAndrew Thompson * Set_Protocol at all; Ignore any error. 59802ac6454SAndrew Thompson */ 599a593f6b8SAndrew Thompson err = usbd_req_set_protocol(uaa->device, NULL, 600de967835SAndrew Thompson uaa->info.bIfaceIndex, 1); 60102ac6454SAndrew Thompson 602a593f6b8SAndrew Thompson err = usbd_transfer_setup(uaa->device, 60302ac6454SAndrew Thompson &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config, 60402ac6454SAndrew Thompson UMS_N_TRANSFER, sc, &sc->sc_mtx); 60502ac6454SAndrew Thompson 60602ac6454SAndrew Thompson if (err) { 607a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 60802ac6454SAndrew Thompson goto detach; 60902ac6454SAndrew Thompson } 61081f82884SAlfred Perlstein 61181f82884SAlfred Perlstein /* Get HID descriptor */ 612a593f6b8SAndrew Thompson err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, 61302ac6454SAndrew Thompson &d_len, M_TEMP, uaa->info.bIfaceIndex); 61402ac6454SAndrew Thompson 61502ac6454SAndrew Thompson if (err) { 61602ac6454SAndrew Thompson device_printf(dev, "error reading report description\n"); 61702ac6454SAndrew Thompson goto detach; 61802ac6454SAndrew Thompson } 61902ac6454SAndrew Thompson 620eead9017SVladimir Kondratyev isize = hid_report_size_max(d_ptr, d_len, hid_input, &sc->sc_iid); 62102ac6454SAndrew Thompson 622ab4f740bSVladimir Kondratyev if (usb_test_quirk(uaa, UQ_MS_VENDOR_BTN)) 623ab4f740bSVladimir Kondratyev for (i = 0; i < UMS_INFO_MAX; i++) 624ab4f740bSVladimir Kondratyev sc->sc_info[i].sc_flags |= UMS_FLAG_VBTN; 625ab4f740bSVladimir Kondratyev 62602ac6454SAndrew Thompson /* 62702ac6454SAndrew Thompson * The Microsoft Wireless Notebook Optical Mouse seems to be in worse 62802ac6454SAndrew Thompson * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and 62902ac6454SAndrew Thompson * all of its other button positions are all off. It also reports that 63020733245SPedro F. Giffuni * it has two additional buttons and a tilt wheel. 63102ac6454SAndrew Thompson */ 632a593f6b8SAndrew Thompson if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) { 63381f82884SAlfred Perlstein sc->sc_iid = 0; 63481f82884SAlfred Perlstein 635de967835SAndrew Thompson info = &sc->sc_info[0]; 636de967835SAndrew Thompson info->sc_flags = (UMS_FLAG_X_AXIS | 63702ac6454SAndrew Thompson UMS_FLAG_Y_AXIS | 63802ac6454SAndrew Thompson UMS_FLAG_Z_AXIS | 63902ac6454SAndrew Thompson UMS_FLAG_SBU); 640de967835SAndrew Thompson info->sc_buttons = 3; 64102ac6454SAndrew Thompson isize = 5; 64202ac6454SAndrew Thompson /* 1st byte of descriptor report contains garbage */ 643de967835SAndrew Thompson info->sc_loc_x.pos = 16; 64481f82884SAlfred Perlstein info->sc_loc_x.size = 8; 645de967835SAndrew Thompson info->sc_loc_y.pos = 24; 64681f82884SAlfred Perlstein info->sc_loc_y.size = 8; 647de967835SAndrew Thompson info->sc_loc_z.pos = 32; 64881f82884SAlfred Perlstein info->sc_loc_z.size = 8; 649de967835SAndrew Thompson info->sc_loc_btn[0].pos = 8; 65081f82884SAlfred Perlstein info->sc_loc_btn[0].size = 1; 651de967835SAndrew Thompson info->sc_loc_btn[1].pos = 9; 65281f82884SAlfred Perlstein info->sc_loc_btn[1].size = 1; 653de967835SAndrew Thompson info->sc_loc_btn[2].pos = 10; 65481f82884SAlfred Perlstein info->sc_loc_btn[2].size = 1; 6553e5e312aSAndrew Thompson 656de967835SAndrew Thompson /* Announce device */ 657de967835SAndrew Thompson device_printf(dev, "3 buttons and [XYZ] " 658de967835SAndrew Thompson "coordinates ID=0\n"); 659de967835SAndrew Thompson 660de967835SAndrew Thompson } else { 661de967835SAndrew Thompson /* Search the HID descriptor and announce device */ 662de967835SAndrew Thompson for (i = 0; i < UMS_INFO_MAX; i++) { 663de967835SAndrew Thompson ums_hid_parse(sc, dev, d_ptr, d_len, i); 664de967835SAndrew Thompson } 66502ac6454SAndrew Thompson } 6663e5e312aSAndrew Thompson 667a593f6b8SAndrew Thompson if (usb_test_quirk(uaa, UQ_MS_REVZ)) { 668de967835SAndrew Thompson info = &sc->sc_info[0]; 66902ac6454SAndrew Thompson /* Some wheels need the Z axis reversed. */ 670de967835SAndrew Thompson info->sc_flags |= UMS_FLAG_REVZ; 67102ac6454SAndrew Thompson } 6726d917491SHans Petter Selasky if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) { 67302ac6454SAndrew Thompson DPRINTF("WARNING: report size, %d bytes, is larger " 674ed6d949aSAndrew Thompson "than interrupt size, %d bytes!\n", isize, 675ed6d949aSAndrew Thompson usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])); 67602ac6454SAndrew Thompson } 67702ac6454SAndrew Thompson free(d_ptr, M_TEMP); 67802ac6454SAndrew Thompson d_ptr = NULL; 67902ac6454SAndrew Thompson 680b850ecc1SAndrew Thompson #ifdef USB_DEBUG 681de967835SAndrew Thompson for (j = 0; j < UMS_INFO_MAX; j++) { 682de967835SAndrew Thompson info = &sc->sc_info[j]; 68302ac6454SAndrew Thompson 684de967835SAndrew Thompson DPRINTF("sc=%p, index=%d\n", sc, j); 685de967835SAndrew Thompson DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos, 686de967835SAndrew Thompson info->sc_loc_x.size, info->sc_iid_x); 687de967835SAndrew Thompson DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos, 688de967835SAndrew Thompson info->sc_loc_y.size, info->sc_iid_y); 689de967835SAndrew Thompson DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos, 690de967835SAndrew Thompson info->sc_loc_z.size, info->sc_iid_z); 691de967835SAndrew Thompson DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos, 692de967835SAndrew Thompson info->sc_loc_t.size, info->sc_iid_t); 693de967835SAndrew Thompson DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos, 694de967835SAndrew Thompson info->sc_loc_w.size, info->sc_iid_w); 695de967835SAndrew Thompson 696de967835SAndrew Thompson for (i = 0; i < info->sc_buttons; i++) { 697bf87f556SAndrew Thompson DPRINTF("B%d\t%d/%d id=%d\n", 698de967835SAndrew Thompson i + 1, info->sc_loc_btn[i].pos, 699de967835SAndrew Thompson info->sc_loc_btn[i].size, info->sc_iid_btn[i]); 700de967835SAndrew Thompson } 70102ac6454SAndrew Thompson } 70202ac6454SAndrew Thompson DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid); 70302ac6454SAndrew Thompson #endif 70402ac6454SAndrew Thompson 705a593f6b8SAndrew Thompson err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 70602ac6454SAndrew Thompson &ums_fifo_methods, &sc->sc_fifo, 7076d917491SHans Petter Selasky device_get_unit(dev), -1, uaa->info.bIfaceIndex, 708ee3e3ff5SAndrew Thompson UID_ROOT, GID_OPERATOR, 0644); 70932e79f55SHans Petter Selasky if (err) 71002ac6454SAndrew Thompson goto detach; 71132e79f55SHans Petter Selasky 712a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 713c3af259aSOleksandr Tymoshenko sc->sc_evdev = evdev_alloc(); 714c3af259aSOleksandr Tymoshenko evdev_set_name(sc->sc_evdev, device_get_desc(dev)); 715c3af259aSOleksandr Tymoshenko evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev)); 716c3af259aSOleksandr Tymoshenko evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor, 717c3af259aSOleksandr Tymoshenko uaa->info.idProduct, 0); 718c3af259aSOleksandr Tymoshenko evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device)); 719c3af259aSOleksandr Tymoshenko evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods); 720c3af259aSOleksandr Tymoshenko evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER); 721c3af259aSOleksandr Tymoshenko evdev_support_event(sc->sc_evdev, EV_SYN); 722c3af259aSOleksandr Tymoshenko evdev_support_event(sc->sc_evdev, EV_REL); 723c3af259aSOleksandr Tymoshenko evdev_support_event(sc->sc_evdev, EV_KEY); 724c3af259aSOleksandr Tymoshenko 725c3af259aSOleksandr Tymoshenko info = &sc->sc_info[0]; 726c3af259aSOleksandr Tymoshenko 727c3af259aSOleksandr Tymoshenko if (info->sc_flags & UMS_FLAG_X_AXIS) 728c3af259aSOleksandr Tymoshenko evdev_support_rel(sc->sc_evdev, REL_X); 729c3af259aSOleksandr Tymoshenko 730c3af259aSOleksandr Tymoshenko if (info->sc_flags & UMS_FLAG_Y_AXIS) 731c3af259aSOleksandr Tymoshenko evdev_support_rel(sc->sc_evdev, REL_Y); 732c3af259aSOleksandr Tymoshenko 733c3af259aSOleksandr Tymoshenko if (info->sc_flags & UMS_FLAG_Z_AXIS) 734c3af259aSOleksandr Tymoshenko evdev_support_rel(sc->sc_evdev, REL_WHEEL); 735c3af259aSOleksandr Tymoshenko 736c3af259aSOleksandr Tymoshenko if (info->sc_flags & UMS_FLAG_T_AXIS) 737c3af259aSOleksandr Tymoshenko evdev_support_rel(sc->sc_evdev, REL_HWHEEL); 738c3af259aSOleksandr Tymoshenko 739c3af259aSOleksandr Tymoshenko for (i = 0; i < info->sc_buttons; i++) 740c3af259aSOleksandr Tymoshenko evdev_support_key(sc->sc_evdev, BTN_MOUSE + i); 741c3af259aSOleksandr Tymoshenko 742e012a280SOleksandr Tymoshenko err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx); 743c3af259aSOleksandr Tymoshenko if (err) 744c3af259aSOleksandr Tymoshenko goto detach; 745c3af259aSOleksandr Tymoshenko #endif 746c3af259aSOleksandr Tymoshenko 7474ed0304cSAndrew Thompson SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 7484ed0304cSAndrew Thompson SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 749f8d2b1f3SPawel Biernacki OID_AUTO, "parseinfo", 750f8d2b1f3SPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 751f8d2b1f3SPawel Biernacki sc, 0, ums_sysctl_handler_parseinfo, "", 752f8d2b1f3SPawel Biernacki "Dump of parsed HID report descriptor"); 7534ed0304cSAndrew Thompson 75402ac6454SAndrew Thompson return (0); 75502ac6454SAndrew Thompson 75602ac6454SAndrew Thompson detach: 75702ac6454SAndrew Thompson if (d_ptr) { 75802ac6454SAndrew Thompson free(d_ptr, M_TEMP); 75902ac6454SAndrew Thompson } 76002ac6454SAndrew Thompson ums_detach(dev); 76102ac6454SAndrew Thompson return (ENOMEM); 76202ac6454SAndrew Thompson } 76302ac6454SAndrew Thompson 76402ac6454SAndrew Thompson static int 76502ac6454SAndrew Thompson ums_detach(device_t self) 76602ac6454SAndrew Thompson { 76702ac6454SAndrew Thompson struct ums_softc *sc = device_get_softc(self); 76802ac6454SAndrew Thompson 76902ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 77002ac6454SAndrew Thompson 771a593f6b8SAndrew Thompson usb_fifo_detach(&sc->sc_fifo); 77202ac6454SAndrew Thompson 773a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 774c3af259aSOleksandr Tymoshenko evdev_free(sc->sc_evdev); 775c3af259aSOleksandr Tymoshenko #endif 776c3af259aSOleksandr Tymoshenko 777a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER); 77802ac6454SAndrew Thompson 779a593f6b8SAndrew Thompson usb_callout_drain(&sc->sc_callout); 78002ac6454SAndrew Thompson 78102ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 78202ac6454SAndrew Thompson 78302ac6454SAndrew Thompson return (0); 78402ac6454SAndrew Thompson } 78502ac6454SAndrew Thompson 78602ac6454SAndrew Thompson static void 787c3af259aSOleksandr Tymoshenko ums_reset(struct ums_softc *sc) 78802ac6454SAndrew Thompson { 789c3af259aSOleksandr Tymoshenko 790c3af259aSOleksandr Tymoshenko /* reset all USB mouse parameters */ 791c3af259aSOleksandr Tymoshenko 792c3af259aSOleksandr Tymoshenko if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 793c3af259aSOleksandr Tymoshenko sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 794c3af259aSOleksandr Tymoshenko else 795c3af259aSOleksandr Tymoshenko sc->sc_hw.buttons = sc->sc_buttons; 796c3af259aSOleksandr Tymoshenko 797c3af259aSOleksandr Tymoshenko sc->sc_hw.iftype = MOUSE_IF_USB; 798c3af259aSOleksandr Tymoshenko sc->sc_hw.type = MOUSE_MOUSE; 799c3af259aSOleksandr Tymoshenko sc->sc_hw.model = MOUSE_MODEL_GENERIC; 800c3af259aSOleksandr Tymoshenko sc->sc_hw.hwid = 0; 801c3af259aSOleksandr Tymoshenko 802c3af259aSOleksandr Tymoshenko sc->sc_mode.protocol = MOUSE_PROTO_MSC; 803c3af259aSOleksandr Tymoshenko sc->sc_mode.rate = -1; 804c3af259aSOleksandr Tymoshenko sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; 805c3af259aSOleksandr Tymoshenko sc->sc_mode.accelfactor = 0; 806c3af259aSOleksandr Tymoshenko sc->sc_mode.level = 0; 807c3af259aSOleksandr Tymoshenko sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 808c3af259aSOleksandr Tymoshenko sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 809c3af259aSOleksandr Tymoshenko sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 810c3af259aSOleksandr Tymoshenko 811c3af259aSOleksandr Tymoshenko /* reset status */ 812c3af259aSOleksandr Tymoshenko 813c3af259aSOleksandr Tymoshenko sc->sc_status.flags = 0; 814c3af259aSOleksandr Tymoshenko sc->sc_status.button = 0; 815c3af259aSOleksandr Tymoshenko sc->sc_status.obutton = 0; 816c3af259aSOleksandr Tymoshenko sc->sc_status.dx = 0; 817c3af259aSOleksandr Tymoshenko sc->sc_status.dy = 0; 818c3af259aSOleksandr Tymoshenko sc->sc_status.dz = 0; 819c3af259aSOleksandr Tymoshenko /* sc->sc_status.dt = 0; */ 820c3af259aSOleksandr Tymoshenko } 821c3af259aSOleksandr Tymoshenko 822c3af259aSOleksandr Tymoshenko static void 823c3af259aSOleksandr Tymoshenko ums_start_rx(struct ums_softc *sc) 824c3af259aSOleksandr Tymoshenko { 82581f82884SAlfred Perlstein int rate; 82681f82884SAlfred Perlstein 82781f82884SAlfred Perlstein /* Check if we should override the default polling interval */ 82881f82884SAlfred Perlstein rate = sc->sc_pollrate; 82981f82884SAlfred Perlstein /* Range check rate */ 83081f82884SAlfred Perlstein if (rate > 1000) 83181f82884SAlfred Perlstein rate = 1000; 83281f82884SAlfred Perlstein /* Check for set rate */ 83381f82884SAlfred Perlstein if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) { 83481f82884SAlfred Perlstein DPRINTF("Setting pollrate = %d\n", rate); 83581f82884SAlfred Perlstein /* Stop current transfer, if any */ 83681f82884SAlfred Perlstein usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 83781f82884SAlfred Perlstein /* Set new interval */ 83881f82884SAlfred Perlstein usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate); 83981f82884SAlfred Perlstein /* Only set pollrate once */ 84081f82884SAlfred Perlstein sc->sc_pollrate = 0; 84181f82884SAlfred Perlstein } 84202ac6454SAndrew Thompson 843a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]); 84402ac6454SAndrew Thompson } 84502ac6454SAndrew Thompson 84602ac6454SAndrew Thompson static void 847c3af259aSOleksandr Tymoshenko ums_stop_rx(struct ums_softc *sc) 848c3af259aSOleksandr Tymoshenko { 849c3af259aSOleksandr Tymoshenko usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 850c3af259aSOleksandr Tymoshenko usb_callout_stop(&sc->sc_callout); 851c3af259aSOleksandr Tymoshenko } 852c3af259aSOleksandr Tymoshenko 853c3af259aSOleksandr Tymoshenko static void 854c3af259aSOleksandr Tymoshenko ums_fifo_start_read(struct usb_fifo *fifo) 85502ac6454SAndrew Thompson { 856ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 85702ac6454SAndrew Thompson 858c3af259aSOleksandr Tymoshenko ums_start_rx(sc); 859c3af259aSOleksandr Tymoshenko } 860c3af259aSOleksandr Tymoshenko 861c3af259aSOleksandr Tymoshenko static void 862c3af259aSOleksandr Tymoshenko ums_fifo_stop_read(struct usb_fifo *fifo) 863c3af259aSOleksandr Tymoshenko { 864c3af259aSOleksandr Tymoshenko struct ums_softc *sc = usb_fifo_softc(fifo); 865c3af259aSOleksandr Tymoshenko 86605ab03a3SVladimir Kondratyev #ifdef EVDEV_SUPPORT 86705ab03a3SVladimir Kondratyev if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0) 86805ab03a3SVladimir Kondratyev #endif 869c3af259aSOleksandr Tymoshenko ums_stop_rx(sc); 87002ac6454SAndrew Thompson } 87102ac6454SAndrew Thompson 87202ac6454SAndrew Thompson #if ((MOUSE_SYS_PACKETSIZE != 8) || \ 87302ac6454SAndrew Thompson (MOUSE_MSC_PACKETSIZE != 5)) 87402ac6454SAndrew Thompson #error "Software assumptions are not met. Please update code." 87502ac6454SAndrew Thompson #endif 87602ac6454SAndrew Thompson 87702ac6454SAndrew Thompson static void 87802ac6454SAndrew Thompson ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, 87902ac6454SAndrew Thompson int32_t dz, int32_t dt, int32_t buttons) 88002ac6454SAndrew Thompson { 88102ac6454SAndrew Thompson uint8_t buf[8]; 88202ac6454SAndrew Thompson 8834a0db5e2SVladimir Kondratyev #ifdef EVDEV_SUPPORT 8844a0db5e2SVladimir Kondratyev if (evdev_is_grabbed(sc->sc_evdev)) 8854a0db5e2SVladimir Kondratyev return; 8864a0db5e2SVladimir Kondratyev #endif 88702ac6454SAndrew Thompson if (1) { 88802ac6454SAndrew Thompson if (dx > 254) 88902ac6454SAndrew Thompson dx = 254; 89002ac6454SAndrew Thompson if (dx < -256) 89102ac6454SAndrew Thompson dx = -256; 89202ac6454SAndrew Thompson if (dy > 254) 89302ac6454SAndrew Thompson dy = 254; 89402ac6454SAndrew Thompson if (dy < -256) 89502ac6454SAndrew Thompson dy = -256; 89602ac6454SAndrew Thompson if (dz > 126) 89702ac6454SAndrew Thompson dz = 126; 89802ac6454SAndrew Thompson if (dz < -128) 89902ac6454SAndrew Thompson dz = -128; 90002ac6454SAndrew Thompson if (dt > 126) 90102ac6454SAndrew Thompson dt = 126; 90202ac6454SAndrew Thompson if (dt < -128) 90302ac6454SAndrew Thompson dt = -128; 90402ac6454SAndrew Thompson 90502ac6454SAndrew Thompson buf[0] = sc->sc_mode.syncmask[1]; 90602ac6454SAndrew Thompson buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS; 90702ac6454SAndrew Thompson buf[1] = dx >> 1; 90802ac6454SAndrew Thompson buf[2] = dy >> 1; 90902ac6454SAndrew Thompson buf[3] = dx - (dx >> 1); 91002ac6454SAndrew Thompson buf[4] = dy - (dy >> 1); 91102ac6454SAndrew Thompson 91202ac6454SAndrew Thompson if (sc->sc_mode.level == 1) { 91302ac6454SAndrew Thompson buf[5] = dz >> 1; 91402ac6454SAndrew Thompson buf[6] = dz - (dz >> 1); 91502ac6454SAndrew Thompson buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS); 91602ac6454SAndrew Thompson } 917a593f6b8SAndrew Thompson usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, 91802ac6454SAndrew Thompson sc->sc_mode.packetsize, 1); 919e012a280SOleksandr Tymoshenko } else { 920e012a280SOleksandr Tymoshenko DPRINTF("Buffer full, discarded packet\n"); 921e012a280SOleksandr Tymoshenko } 922e012a280SOleksandr Tymoshenko } 92302ac6454SAndrew Thompson 924a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 925e012a280SOleksandr Tymoshenko static void 926e012a280SOleksandr Tymoshenko ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy, 927e012a280SOleksandr Tymoshenko int32_t dz, int32_t dt, int32_t buttons) 928e012a280SOleksandr Tymoshenko { 929c3af259aSOleksandr Tymoshenko if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) { 930c3af259aSOleksandr Tymoshenko /* Push evdev event */ 931e012a280SOleksandr Tymoshenko evdev_push_rel(sc->sc_evdev, REL_X, dx); 932e012a280SOleksandr Tymoshenko evdev_push_rel(sc->sc_evdev, REL_Y, -dy); 933e012a280SOleksandr Tymoshenko evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz); 934e012a280SOleksandr Tymoshenko evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt); 935c3af259aSOleksandr Tymoshenko evdev_push_mouse_btn(sc->sc_evdev, 936c3af259aSOleksandr Tymoshenko (buttons & ~MOUSE_STDBUTTONS) | 937c3af259aSOleksandr Tymoshenko (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) | 938c3af259aSOleksandr Tymoshenko (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) | 939c3af259aSOleksandr Tymoshenko (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0)); 940c3af259aSOleksandr Tymoshenko evdev_sync(sc->sc_evdev); 941c3af259aSOleksandr Tymoshenko } 942e012a280SOleksandr Tymoshenko } 943c3af259aSOleksandr Tymoshenko #endif 94402ac6454SAndrew Thompson 94502ac6454SAndrew Thompson static void 94602ac6454SAndrew Thompson ums_reset_buf(struct ums_softc *sc) 94702ac6454SAndrew Thompson { 94832e79f55SHans Petter Selasky /* reset read queue, must be called locked */ 949a593f6b8SAndrew Thompson usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); 95002ac6454SAndrew Thompson } 95102ac6454SAndrew Thompson 952a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 95302ac6454SAndrew Thompson static int 954911aed94SVladimir Kondratyev ums_ev_open(struct evdev_dev *evdev) 955c3af259aSOleksandr Tymoshenko { 956911aed94SVladimir Kondratyev struct ums_softc *sc = evdev_get_softc(evdev); 957c3af259aSOleksandr Tymoshenko 958e012a280SOleksandr Tymoshenko mtx_assert(&sc->sc_mtx, MA_OWNED); 959c3af259aSOleksandr Tymoshenko 96005ab03a3SVladimir Kondratyev sc->sc_evflags |= UMS_EVDEV_OPENED; 961c3af259aSOleksandr Tymoshenko 962c3af259aSOleksandr Tymoshenko if (sc->sc_fflags == 0) { 963c3af259aSOleksandr Tymoshenko ums_reset(sc); 964c3af259aSOleksandr Tymoshenko } 96547791339SVladimir Kondratyev ums_start_rx(sc); 966c3af259aSOleksandr Tymoshenko 967c3af259aSOleksandr Tymoshenko return (0); 968c3af259aSOleksandr Tymoshenko } 969c3af259aSOleksandr Tymoshenko 970911aed94SVladimir Kondratyev static int 971911aed94SVladimir Kondratyev ums_ev_close(struct evdev_dev *evdev) 972c3af259aSOleksandr Tymoshenko { 973911aed94SVladimir Kondratyev struct ums_softc *sc = evdev_get_softc(evdev); 974c3af259aSOleksandr Tymoshenko 975e012a280SOleksandr Tymoshenko mtx_assert(&sc->sc_mtx, MA_OWNED); 976c3af259aSOleksandr Tymoshenko 97705ab03a3SVladimir Kondratyev sc->sc_evflags &= ~UMS_EVDEV_OPENED; 978c3af259aSOleksandr Tymoshenko 979c3af259aSOleksandr Tymoshenko if (sc->sc_fflags == 0) 980c3af259aSOleksandr Tymoshenko ums_stop_rx(sc); 981911aed94SVladimir Kondratyev 982911aed94SVladimir Kondratyev return (0); 983c3af259aSOleksandr Tymoshenko } 984c3af259aSOleksandr Tymoshenko #endif 985c3af259aSOleksandr Tymoshenko 986c3af259aSOleksandr Tymoshenko static int 987c3af259aSOleksandr Tymoshenko ums_fifo_open(struct usb_fifo *fifo, int fflags) 98802ac6454SAndrew Thompson { 989ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 99002ac6454SAndrew Thompson 99102ac6454SAndrew Thompson DPRINTFN(2, "\n"); 99202ac6454SAndrew Thompson 99332e79f55SHans Petter Selasky /* check for duplicate open, should not happen */ 99432e79f55SHans Petter Selasky if (sc->sc_fflags & fflags) 99532e79f55SHans Petter Selasky return (EBUSY); 99632e79f55SHans Petter Selasky 99732e79f55SHans Petter Selasky /* check for first open */ 998a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 99905ab03a3SVladimir Kondratyev if (sc->sc_fflags == 0 && (sc->sc_evflags & UMS_EVDEV_OPENED) == 0) 1000c3af259aSOleksandr Tymoshenko ums_reset(sc); 1001c3af259aSOleksandr Tymoshenko #else 1002c3af259aSOleksandr Tymoshenko if (sc->sc_fflags == 0) 1003c3af259aSOleksandr Tymoshenko ums_reset(sc); 1004c3af259aSOleksandr Tymoshenko #endif 100502ac6454SAndrew Thompson 100632e79f55SHans Petter Selasky if (fflags & FREAD) { 100732e79f55SHans Petter Selasky /* allocate RX buffer */ 1008a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo, 100902ac6454SAndrew Thompson UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) { 101002ac6454SAndrew Thompson return (ENOMEM); 101102ac6454SAndrew Thompson } 101202ac6454SAndrew Thompson } 101332e79f55SHans Petter Selasky 101432e79f55SHans Petter Selasky sc->sc_fflags |= fflags & (FREAD | FWRITE); 101502ac6454SAndrew Thompson return (0); 101602ac6454SAndrew Thompson } 101702ac6454SAndrew Thompson 101802ac6454SAndrew Thompson static void 1019c3af259aSOleksandr Tymoshenko ums_fifo_close(struct usb_fifo *fifo, int fflags) 102002ac6454SAndrew Thompson { 102132e79f55SHans Petter Selasky struct ums_softc *sc = usb_fifo_softc(fifo); 102232e79f55SHans Petter Selasky 102332e79f55SHans Petter Selasky DPRINTFN(2, "\n"); 102432e79f55SHans Petter Selasky 102532e79f55SHans Petter Selasky if (fflags & FREAD) 1026a593f6b8SAndrew Thompson usb_fifo_free_buffer(fifo); 102732e79f55SHans Petter Selasky 102832e79f55SHans Petter Selasky sc->sc_fflags &= ~(fflags & (FREAD | FWRITE)); 102902ac6454SAndrew Thompson } 103002ac6454SAndrew Thompson 103102ac6454SAndrew Thompson static int 1032c3af259aSOleksandr Tymoshenko ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 103302ac6454SAndrew Thompson { 1034ed6d949aSAndrew Thompson struct ums_softc *sc = usb_fifo_softc(fifo); 103502ac6454SAndrew Thompson mousemode_t mode; 103602ac6454SAndrew Thompson int error = 0; 103702ac6454SAndrew Thompson 103802ac6454SAndrew Thompson DPRINTFN(2, "\n"); 103902ac6454SAndrew Thompson 104002ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx); 104102ac6454SAndrew Thompson 104202ac6454SAndrew Thompson switch (cmd) { 104302ac6454SAndrew Thompson case MOUSE_GETHWINFO: 104402ac6454SAndrew Thompson *(mousehw_t *)addr = sc->sc_hw; 104502ac6454SAndrew Thompson break; 104602ac6454SAndrew Thompson 104702ac6454SAndrew Thompson case MOUSE_GETMODE: 104802ac6454SAndrew Thompson *(mousemode_t *)addr = sc->sc_mode; 104902ac6454SAndrew Thompson break; 105002ac6454SAndrew Thompson 105102ac6454SAndrew Thompson case MOUSE_SETMODE: 105202ac6454SAndrew Thompson mode = *(mousemode_t *)addr; 105302ac6454SAndrew Thompson 105402ac6454SAndrew Thompson if (mode.level == -1) { 105502ac6454SAndrew Thompson /* don't change the current setting */ 105602ac6454SAndrew Thompson } else if ((mode.level < 0) || (mode.level > 1)) { 105702ac6454SAndrew Thompson error = EINVAL; 105832e79f55SHans Petter Selasky break; 105902ac6454SAndrew Thompson } else { 106002ac6454SAndrew Thompson sc->sc_mode.level = mode.level; 106102ac6454SAndrew Thompson } 106202ac6454SAndrew Thompson 106381f82884SAlfred Perlstein /* store polling rate */ 106481f82884SAlfred Perlstein sc->sc_pollrate = mode.rate; 106581f82884SAlfred Perlstein 106602ac6454SAndrew Thompson if (sc->sc_mode.level == 0) { 106702ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 106802ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 106902ac6454SAndrew Thompson else 107002ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 107102ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_MSC; 107202ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 107302ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 107402ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 107502ac6454SAndrew Thompson } else if (sc->sc_mode.level == 1) { 107602ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 107702ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 107802ac6454SAndrew Thompson else 107902ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 108002ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 108102ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 108202ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 108302ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 108402ac6454SAndrew Thompson } 108502ac6454SAndrew Thompson ums_reset_buf(sc); 108602ac6454SAndrew Thompson break; 108702ac6454SAndrew Thompson 108802ac6454SAndrew Thompson case MOUSE_GETLEVEL: 108902ac6454SAndrew Thompson *(int *)addr = sc->sc_mode.level; 109002ac6454SAndrew Thompson break; 109102ac6454SAndrew Thompson 109202ac6454SAndrew Thompson case MOUSE_SETLEVEL: 109302ac6454SAndrew Thompson if (*(int *)addr < 0 || *(int *)addr > 1) { 109402ac6454SAndrew Thompson error = EINVAL; 109532e79f55SHans Petter Selasky break; 109602ac6454SAndrew Thompson } 109702ac6454SAndrew Thompson sc->sc_mode.level = *(int *)addr; 109802ac6454SAndrew Thompson 109902ac6454SAndrew Thompson if (sc->sc_mode.level == 0) { 110002ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 110102ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 110202ac6454SAndrew Thompson else 110302ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 110402ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_MSC; 110502ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 110602ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 110702ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 110802ac6454SAndrew Thompson } else if (sc->sc_mode.level == 1) { 110902ac6454SAndrew Thompson if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 111002ac6454SAndrew Thompson sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 111102ac6454SAndrew Thompson else 111202ac6454SAndrew Thompson sc->sc_hw.buttons = sc->sc_buttons; 111302ac6454SAndrew Thompson sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 111402ac6454SAndrew Thompson sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 111502ac6454SAndrew Thompson sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 111602ac6454SAndrew Thompson sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 111702ac6454SAndrew Thompson } 111802ac6454SAndrew Thompson ums_reset_buf(sc); 111902ac6454SAndrew Thompson break; 112002ac6454SAndrew Thompson 112102ac6454SAndrew Thompson case MOUSE_GETSTATUS:{ 112202ac6454SAndrew Thompson mousestatus_t *status = (mousestatus_t *)addr; 112302ac6454SAndrew Thompson 112402ac6454SAndrew Thompson *status = sc->sc_status; 112502ac6454SAndrew Thompson sc->sc_status.obutton = sc->sc_status.button; 112602ac6454SAndrew Thompson sc->sc_status.button = 0; 112702ac6454SAndrew Thompson sc->sc_status.dx = 0; 112802ac6454SAndrew Thompson sc->sc_status.dy = 0; 112902ac6454SAndrew Thompson sc->sc_status.dz = 0; 113002ac6454SAndrew Thompson /* sc->sc_status.dt = 0; */ 113102ac6454SAndrew Thompson 113202ac6454SAndrew Thompson if (status->dx || status->dy || status->dz /* || status->dt */ ) { 113302ac6454SAndrew Thompson status->flags |= MOUSE_POSCHANGED; 113402ac6454SAndrew Thompson } 113502ac6454SAndrew Thompson if (status->button != status->obutton) { 113602ac6454SAndrew Thompson status->flags |= MOUSE_BUTTONSCHANGED; 113702ac6454SAndrew Thompson } 113802ac6454SAndrew Thompson break; 113902ac6454SAndrew Thompson } 114002ac6454SAndrew Thompson default: 114102ac6454SAndrew Thompson error = ENOTTY; 114232e79f55SHans Petter Selasky break; 114302ac6454SAndrew Thompson } 114402ac6454SAndrew Thompson 114502ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx); 114602ac6454SAndrew Thompson return (error); 114702ac6454SAndrew Thompson } 114802ac6454SAndrew Thompson 11494ed0304cSAndrew Thompson static int 11504ed0304cSAndrew Thompson ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS) 11514ed0304cSAndrew Thompson { 11524ed0304cSAndrew Thompson struct ums_softc *sc = arg1; 11534ed0304cSAndrew Thompson struct ums_info *info; 11544ed0304cSAndrew Thompson struct sbuf *sb; 1155b635069cSHans Petter Selasky int i, j, err, had_output; 11564ed0304cSAndrew Thompson 11574ed0304cSAndrew Thompson sb = sbuf_new_auto(); 1158b635069cSHans Petter Selasky for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) { 11594ed0304cSAndrew Thompson info = &sc->sc_info[i]; 11604ed0304cSAndrew Thompson 11614ed0304cSAndrew Thompson /* Don't emit empty info */ 11624ed0304cSAndrew Thompson if ((info->sc_flags & 11634ed0304cSAndrew Thompson (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS | 11644ed0304cSAndrew Thompson UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 && 11654ed0304cSAndrew Thompson info->sc_buttons == 0) 11664ed0304cSAndrew Thompson continue; 11674ed0304cSAndrew Thompson 1168b635069cSHans Petter Selasky if (had_output) 1169b635069cSHans Petter Selasky sbuf_printf(sb, "\n"); 1170b635069cSHans Petter Selasky had_output = 1; 11714ed0304cSAndrew Thompson sbuf_printf(sb, "i%d:", i + 1); 11724ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_X_AXIS) 11734ed0304cSAndrew Thompson sbuf_printf(sb, " X:r%d, p%d, s%d;", 11744ed0304cSAndrew Thompson (int)info->sc_iid_x, 11754ed0304cSAndrew Thompson (int)info->sc_loc_x.pos, 11764ed0304cSAndrew Thompson (int)info->sc_loc_x.size); 11774ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_Y_AXIS) 11784ed0304cSAndrew Thompson sbuf_printf(sb, " Y:r%d, p%d, s%d;", 11794ed0304cSAndrew Thompson (int)info->sc_iid_y, 11804ed0304cSAndrew Thompson (int)info->sc_loc_y.pos, 11814ed0304cSAndrew Thompson (int)info->sc_loc_y.size); 11824ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_Z_AXIS) 11834ed0304cSAndrew Thompson sbuf_printf(sb, " Z:r%d, p%d, s%d;", 11844ed0304cSAndrew Thompson (int)info->sc_iid_z, 11854ed0304cSAndrew Thompson (int)info->sc_loc_z.pos, 11864ed0304cSAndrew Thompson (int)info->sc_loc_z.size); 11874ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_T_AXIS) 11884ed0304cSAndrew Thompson sbuf_printf(sb, " T:r%d, p%d, s%d;", 11894ed0304cSAndrew Thompson (int)info->sc_iid_t, 11904ed0304cSAndrew Thompson (int)info->sc_loc_t.pos, 11914ed0304cSAndrew Thompson (int)info->sc_loc_t.size); 11924ed0304cSAndrew Thompson if (info->sc_flags & UMS_FLAG_W_AXIS) 11934ed0304cSAndrew Thompson sbuf_printf(sb, " W:r%d, p%d, s%d;", 11944ed0304cSAndrew Thompson (int)info->sc_iid_w, 11954ed0304cSAndrew Thompson (int)info->sc_loc_w.pos, 11964ed0304cSAndrew Thompson (int)info->sc_loc_w.size); 11974ed0304cSAndrew Thompson 11984ed0304cSAndrew Thompson for (j = 0; j < info->sc_buttons; j++) { 11994ed0304cSAndrew Thompson sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1, 12004ed0304cSAndrew Thompson (int)info->sc_iid_btn[j], 12014ed0304cSAndrew Thompson (int)info->sc_loc_btn[j].pos, 12024ed0304cSAndrew Thompson (int)info->sc_loc_btn[j].size); 12034ed0304cSAndrew Thompson } 12044ed0304cSAndrew Thompson } 12054ed0304cSAndrew Thompson sbuf_finish(sb); 12064ed0304cSAndrew Thompson err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 12074ed0304cSAndrew Thompson sbuf_delete(sb); 12084ed0304cSAndrew Thompson 12094ed0304cSAndrew Thompson return (err); 12104ed0304cSAndrew Thompson } 12114ed0304cSAndrew Thompson 121202ac6454SAndrew Thompson static device_method_t ums_methods[] = { 121302ac6454SAndrew Thompson DEVMETHOD(device_probe, ums_probe), 121402ac6454SAndrew Thompson DEVMETHOD(device_attach, ums_attach), 121502ac6454SAndrew Thompson DEVMETHOD(device_detach, ums_detach), 121661bfd867SSofian Brabez 121761bfd867SSofian Brabez DEVMETHOD_END 121802ac6454SAndrew Thompson }; 121902ac6454SAndrew Thompson 122002ac6454SAndrew Thompson static driver_t ums_driver = { 122102ac6454SAndrew Thompson .name = "ums", 122202ac6454SAndrew Thompson .methods = ums_methods, 122302ac6454SAndrew Thompson .size = sizeof(struct ums_softc), 122402ac6454SAndrew Thompson }; 122502ac6454SAndrew Thompson 1226bc9372d7SJohn Baldwin DRIVER_MODULE(ums, uhub, ums_driver, NULL, NULL); 122702ac6454SAndrew Thompson MODULE_DEPEND(ums, usb, 1, 1, 1); 122867de2db2SVladimir Kondratyev MODULE_DEPEND(ums, hid, 1, 1, 1); 1229a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT 1230fa26e8edSOleksandr Tymoshenko MODULE_DEPEND(ums, evdev, 1, 1, 1); 1231fa26e8edSOleksandr Tymoshenko #endif 1232910cb8feSAndrew Thompson MODULE_VERSION(ums, 1); 1233f809f280SWarner Losh USB_PNP_HOST_INFO(ums_devs); 1234