1 /* $NetBSD: btms.c,v 1.1 2006/06/19 15:44:45 gdamore Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Itronix Inc. 5 * All rights reserved. 6 * 7 * Written by Iain Hibbert for Itronix Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of Itronix Inc. may not be used to endorse 18 * or promote products derived from this software without specific 19 * prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 * ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * based on dev/usb/ums.c 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: btms.c,v 1.1 2006/06/19 15:44:45 gdamore Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/conf.h> 43 #include <sys/device.h> 44 #include <sys/proc.h> 45 #include <sys/systm.h> 46 47 #include <netbt/bluetooth.h> 48 49 #include <dev/bluetooth/bthid.h> 50 #include <dev/bluetooth/bthidev.h> 51 52 #include <dev/usb/hid.h> 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usbhid.h> 55 56 #include <dev/wscons/wsconsio.h> 57 #include <dev/wscons/wsmousevar.h> 58 59 #define MAX_BUTTONS 31 60 #define BUTTON(n) (1 << (((n) == 1 || (n) == 2) ? 3 - (n) : (n))) 61 #define NOTMOUSE(f) (((f) & (HIO_CONST | HIO_RELATIVE)) != HIO_RELATIVE) 62 63 struct btms_softc { 64 struct bthidev sc_hidev; /* device+ */ 65 66 struct device *sc_wsmouse; /* child */ 67 int sc_enabled; 68 uint16_t sc_flags; 69 70 /* locators */ 71 struct hid_location sc_loc_x; 72 struct hid_location sc_loc_y; 73 struct hid_location sc_loc_z; 74 struct hid_location sc_loc_w; 75 struct hid_location sc_loc_button[MAX_BUTTONS]; 76 77 int sc_num_buttons; 78 uint32_t sc_buttons; 79 }; 80 81 /* sc_flags */ 82 #define BTMS_REVZ (1 << 0) /* reverse Z direction */ 83 #define BTMS_HASZ (1 << 1) /* has Z direction */ 84 85 /* autoconf(9) methods */ 86 static int btms_match(struct device *, struct cfdata *, void *); 87 static void btms_attach(struct device *, struct device *, void *); 88 static int btms_detach(struct device *, int); 89 90 CFATTACH_DECL(btms, sizeof(struct btms_softc), 91 btms_match, btms_attach, btms_detach, NULL); 92 93 /* wsmouse(4) accessops */ 94 static int btms_wsmouse_enable(void *); 95 static int btms_wsmouse_ioctl(void *, unsigned long, caddr_t, int, struct lwp *); 96 static void btms_wsmouse_disable(void *); 97 98 static const struct wsmouse_accessops btms_wsmouse_accessops = { 99 btms_wsmouse_enable, 100 btms_wsmouse_ioctl, 101 btms_wsmouse_disable, 102 }; 103 104 /* bthid methods */ 105 static void btms_input(struct bthidev *, uint8_t *, int); 106 107 /***************************************************************************** 108 * 109 * btms autoconf(9) routines 110 */ 111 112 static int 113 btms_match(struct device *parent, struct cfdata *match, void *aux) 114 { 115 struct bthidev_attach_args *ba = aux; 116 117 if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id, 118 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) 119 return 1; 120 121 return 0; 122 } 123 124 static void 125 btms_attach(struct device *parent, struct device *self, void *aux) 126 { 127 struct btms_softc *sc = (struct btms_softc *)self; 128 struct bthidev_attach_args *ba = aux; 129 struct wsmousedev_attach_args wsma; 130 struct hid_location *zloc; 131 uint32_t flags; 132 int i, hl; 133 134 ba->ba_input = btms_input; 135 136 /* control the horizontal */ 137 hl = hid_locate(ba->ba_desc, 138 ba->ba_dlen, 139 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 140 ba->ba_id, 141 hid_input, 142 &sc->sc_loc_x, 143 &flags); 144 145 if (hl == 0 || NOTMOUSE(flags)) { 146 printf("\n%s: X report 0x%04x not supported\n", 147 sc->sc_hidev.sc_dev.dv_xname, flags); 148 149 return; 150 } 151 152 /* control the vertical */ 153 hl = hid_locate(ba->ba_desc, 154 ba->ba_dlen, 155 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 156 ba->ba_id, 157 hid_input, 158 &sc->sc_loc_y, 159 &flags); 160 161 if (hl == 0 || NOTMOUSE(flags)) { 162 printf("\n%s: Y report 0x%04x not supported\n", 163 sc->sc_hidev.sc_dev.dv_xname, flags); 164 165 return; 166 } 167 168 /* Try the wheel first as the Z activator since it's tradition. */ 169 hl = hid_locate(ba->ba_desc, 170 ba->ba_dlen, 171 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL), 172 ba->ba_id, 173 hid_input, 174 &sc->sc_loc_z, 175 &flags); 176 177 zloc = &sc->sc_loc_z; 178 if (hl) { 179 if (NOTMOUSE(flags)) { 180 printf("\n%s: Wheel report 0x%04x not supported\n", 181 sc->sc_hidev.sc_dev.dv_xname, flags); 182 183 /* ignore Bad Z coord */ 184 sc->sc_loc_z.size = 0; 185 } else { 186 sc->sc_flags |= BTMS_HASZ; 187 /* Wheels need the Z axis reversed. */ 188 sc->sc_flags ^= BTMS_REVZ; 189 /* Put Z on the W coordinate */ 190 zloc = &sc->sc_loc_w; 191 } 192 } 193 194 hl = hid_locate(ba->ba_desc, 195 ba->ba_dlen, 196 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z), 197 ba->ba_id, 198 hid_input, 199 zloc, 200 &flags); 201 202 if (hl) { 203 if (NOTMOUSE(flags)) 204 zloc->size = 0; /* ignore Z */ 205 else 206 sc->sc_flags |= BTMS_HASZ; 207 } 208 209 for (i = 1 ; i <= MAX_BUTTONS ; i++) { 210 hl = hid_locate(ba->ba_desc, 211 ba->ba_dlen, 212 HID_USAGE2(HUP_BUTTON, i), 213 ba->ba_id, 214 hid_input, 215 &sc->sc_loc_button[i - 1], 216 NULL); 217 218 if (hl == 0) 219 break; 220 } 221 sc->sc_num_buttons = i - 1; 222 223 aprint_normal(": %d button%s%s.\n", 224 sc->sc_num_buttons, 225 sc->sc_num_buttons == 1 ? "" : "s", 226 sc->sc_flags & BTMS_HASZ ? " and Z dir" : ""); 227 228 wsma.accessops = &btms_wsmouse_accessops; 229 wsma.accesscookie = sc; 230 231 sc->sc_wsmouse = config_found((struct device *)sc, &wsma, wsmousedevprint); 232 } 233 234 static int 235 btms_detach(struct device *self, int flags) 236 { 237 struct btms_softc *sc = (struct btms_softc *)self; 238 int err = 0; 239 240 if (sc->sc_wsmouse != NULL) { 241 err = config_detach(sc->sc_wsmouse, flags); 242 sc->sc_wsmouse = NULL; 243 } 244 245 return err; 246 } 247 248 /***************************************************************************** 249 * 250 * wsmouse(4) accessops 251 */ 252 253 static int 254 btms_wsmouse_enable(void *self) 255 { 256 struct btms_softc *sc = (struct btms_softc *)self; 257 258 if (sc->sc_enabled) 259 return EBUSY; 260 261 sc->sc_enabled = 1; 262 return 0; 263 } 264 265 static int 266 btms_wsmouse_ioctl(void *self, unsigned long cmd, caddr_t data, int flag, struct lwp *l) 267 { 268 /* struct btms_softc *sc = (struct btms_softc *)self; */ 269 270 switch (cmd) { 271 case WSMOUSEIO_GTYPE: 272 *(uint *)data = WSMOUSE_TYPE_BLUETOOTH; 273 break; 274 275 default: 276 return EPASSTHROUGH; 277 } 278 279 return 0; 280 } 281 282 static void 283 btms_wsmouse_disable(void *self) 284 { 285 struct btms_softc *sc = (struct btms_softc *)self; 286 287 sc->sc_enabled = 0; 288 } 289 290 /***************************************************************************** 291 * 292 * btms input routine, called from our parent 293 */ 294 295 static void 296 btms_input(struct bthidev *self, uint8_t *data, int len) 297 { 298 struct btms_softc *sc = (struct btms_softc *)self; 299 int dx, dy, dz, dw; 300 uint32_t buttons; 301 int i, s; 302 303 if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0) 304 return; 305 306 dx = hid_get_data(data, &sc->sc_loc_x); 307 dy = -hid_get_data(data, &sc->sc_loc_y); 308 dz = hid_get_data(data, &sc->sc_loc_z); 309 dw = hid_get_data(data, &sc->sc_loc_w); 310 311 if (sc->sc_flags & BTMS_REVZ) 312 dz = -dz; 313 314 buttons = 0; 315 for (i = 0 ; i < sc->sc_num_buttons ; i++) 316 if (hid_get_data(data, &sc->sc_loc_button[i])) 317 buttons |= BUTTON(i); 318 319 if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) { 320 sc->sc_buttons = buttons; 321 322 s = spltty(); 323 wsmouse_input_xyzw(sc->sc_wsmouse, buttons, 324 dx, dy, dz, dw, 325 WSMOUSE_INPUT_DELTA); 326 splx(s); 327 } 328 } 329