hid.c revision 281566
1/* 2 * hid.c 3 */ 4 5/*- 6 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 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 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $ 31 * $FreeBSD: stable/10/usr.sbin/bluetooth/bthidd/hid.c 281566 2015-04-15 22:02:52Z rakuco $ 32 */ 33 34#include <sys/consio.h> 35#include <sys/mouse.h> 36#include <sys/queue.h> 37#include <assert.h> 38#include <bluetooth.h> 39#include <dev/usb/usb.h> 40#include <dev/usb/usbhid.h> 41#include <errno.h> 42#include <stdio.h> 43#include <string.h> 44#include <syslog.h> 45#include <unistd.h> 46#include <usbhid.h> 47#include "bthid_config.h" 48#include "bthidd.h" 49#include "kbd.h" 50 51#undef min 52#define min(x, y) (((x) < (y))? (x) : (y)) 53 54#undef ASIZE 55#define ASIZE(a) (sizeof(a)/sizeof(a[0])) 56 57/* 58 * Process data from control channel 59 */ 60 61int32_t 62hid_control(bthid_session_p s, uint8_t *data, int32_t len) 63{ 64 assert(s != NULL); 65 assert(data != NULL); 66 assert(len > 0); 67 68 switch (data[0] >> 4) { 69 case 0: /* Handshake (response to command) */ 70 if (data[0] & 0xf) 71 syslog(LOG_ERR, "Got handshake message with error " \ 72 "response 0x%x from %s", 73 data[0], bt_ntoa(&s->bdaddr, NULL)); 74 break; 75 76 case 1: /* HID Control */ 77 switch (data[0] & 0xf) { 78 case 0: /* NOP */ 79 break; 80 81 case 1: /* Hard reset */ 82 case 2: /* Soft reset */ 83 syslog(LOG_WARNING, "Device %s requested %s reset", 84 bt_ntoa(&s->bdaddr, NULL), 85 ((data[0] & 0xf) == 1)? "hard" : "soft"); 86 break; 87 88 case 3: /* Suspend */ 89 syslog(LOG_NOTICE, "Device %s requested Suspend", 90 bt_ntoa(&s->bdaddr, NULL)); 91 break; 92 93 case 4: /* Exit suspend */ 94 syslog(LOG_NOTICE, "Device %s requested Exit Suspend", 95 bt_ntoa(&s->bdaddr, NULL)); 96 break; 97 98 case 5: /* Virtual cable unplug */ 99 syslog(LOG_NOTICE, "Device %s unplugged virtual cable", 100 bt_ntoa(&s->bdaddr, NULL)); 101 session_close(s); 102 break; 103 104 default: 105 syslog(LOG_WARNING, "Device %s sent unknown " \ 106 "HID_Control message 0x%x", 107 bt_ntoa(&s->bdaddr, NULL), data[0]); 108 break; 109 } 110 break; 111 112 default: 113 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \ 114 "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL)); 115 break; 116 } 117 118 return (0); 119} 120 121/* 122 * Process data from the interrupt channel 123 */ 124 125int32_t 126hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len) 127{ 128 hid_device_p hid_device; 129 hid_data_t d; 130 hid_item_t h; 131 int32_t report_id, usage, page, val, 132 mouse_x, mouse_y, mouse_z, mouse_butt, 133 mevents, kevents, i; 134 135 assert(s != NULL); 136 assert(s->srv != NULL); 137 assert(data != NULL); 138 139 if (len < 3) { 140 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \ 141 "channel from %s", len, bt_ntoa(&s->bdaddr, NULL)); 142 return (-1); 143 } 144 145 if (data[0] != 0xa1) { 146 syslog(LOG_ERR, "Got unexpected message 0x%x on " \ 147 "Interrupt channel from %s", 148 data[0], bt_ntoa(&s->bdaddr, NULL)); 149 return (-1); 150 } 151 152 report_id = data[1]; 153 data ++; 154 len --; 155 156 hid_device = get_hid_device(&s->bdaddr); 157 assert(hid_device != NULL); 158 159 mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0; 160 161 for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1); 162 hid_get_item(d, &h) > 0; ) { 163 if ((h.flags & HIO_CONST) || (h.report_ID != report_id) || 164 (h.kind != hid_input)) 165 continue; 166 167 page = HID_PAGE(h.usage); 168 val = hid_get_data(data, &h); 169 170 /* 171 * When the input field is an array and the usage is specified 172 * with a range instead of an ID, we have to derive the actual 173 * usage by using the item value as an index in the usage range 174 * list. 175 */ 176 if ((h.flags & HIO_VARIABLE)) { 177 usage = HID_USAGE(h.usage); 178 } else { 179 const uint32_t usage_offset = val - h.logical_minimum; 180 usage = HID_USAGE(h.usage_minimum + usage_offset); 181 } 182 183 switch (page) { 184 case HUP_GENERIC_DESKTOP: 185 switch (usage) { 186 case HUG_X: 187 mouse_x = val; 188 mevents ++; 189 break; 190 191 case HUG_Y: 192 mouse_y = val; 193 mevents ++; 194 break; 195 196 case HUG_WHEEL: 197 mouse_z = -val; 198 mevents ++; 199 break; 200 201 case HUG_SYSTEM_SLEEP: 202 if (val) 203 syslog(LOG_NOTICE, "Sleep button pressed"); 204 break; 205 } 206 break; 207 208 case HUP_KEYBOARD: 209 kevents ++; 210 211 if (h.flags & HIO_VARIABLE) { 212 if (val && usage < kbd_maxkey()) 213 bit_set(s->keys1, usage); 214 } else { 215 if (val && val < kbd_maxkey()) 216 bit_set(s->keys1, val); 217 218 for (i = 1; i < h.report_count; i++) { 219 h.pos += h.report_size; 220 val = hid_get_data(data, &h); 221 if (val && val < kbd_maxkey()) 222 bit_set(s->keys1, val); 223 } 224 } 225 break; 226 227 case HUP_BUTTON: 228 if (usage != 0) { 229 if (usage == 2) 230 usage = 3; 231 else if (usage == 3) 232 usage = 2; 233 234 mouse_butt |= (val << (usage - 1)); 235 mevents ++; 236 } 237 break; 238 239 case HUP_CONSUMER: 240 if (!val) 241 break; 242 243 switch (usage) { 244 case HUC_AC_PAN: 245 /* Horizontal scroll */ 246 if (val < 0) 247 mouse_butt |= (1 << 5); 248 else 249 mouse_butt |= (1 << 6); 250 251 mevents ++; 252 val = 0; 253 break; 254 255 case 0xb5: /* Scan Next Track */ 256 val = 0x19; 257 break; 258 259 case 0xb6: /* Scan Previous Track */ 260 val = 0x10; 261 break; 262 263 case 0xb7: /* Stop */ 264 val = 0x24; 265 break; 266 267 case 0xcd: /* Play/Pause */ 268 val = 0x22; 269 break; 270 271 case 0xe2: /* Mute */ 272 val = 0x20; 273 break; 274 275 case 0xe9: /* Volume Up */ 276 val = 0x30; 277 break; 278 279 case 0xea: /* Volume Down */ 280 val = 0x2E; 281 break; 282 283 case 0x183: /* Media Select */ 284 val = 0x6D; 285 break; 286 287 case 0x018a: /* Mail */ 288 val = 0x6C; 289 break; 290 291 case 0x192: /* Calculator */ 292 val = 0x21; 293 break; 294 295 case 0x194: /* My Computer */ 296 val = 0x6B; 297 break; 298 299 case 0x221: /* WWW Search */ 300 val = 0x65; 301 break; 302 303 case 0x223: /* WWW Home */ 304 val = 0x32; 305 break; 306 307 case 0x224: /* WWW Back */ 308 val = 0x6A; 309 break; 310 311 case 0x225: /* WWW Forward */ 312 val = 0x69; 313 break; 314 315 case 0x226: /* WWW Stop */ 316 val = 0x68; 317 break; 318 319 case 0x227: /* WWW Refresh */ 320 val = 0x67; 321 break; 322 323 case 0x22a: /* WWW Favorites */ 324 val = 0x66; 325 break; 326 327 default: 328 val = 0; 329 break; 330 } 331 332 /* XXX FIXME - UGLY HACK */ 333 if (val != 0) { 334 if (hid_device->keyboard) { 335 int32_t buf[4] = { 0xe0, val, 336 0xe0, val|0x80 }; 337 338 assert(s->vkbd != -1); 339 write(s->vkbd, buf, sizeof(buf)); 340 } else 341 syslog(LOG_ERR, "Keyboard events " \ 342 "received from non-keyboard " \ 343 "device %s. Please report", 344 bt_ntoa(&s->bdaddr, NULL)); 345 } 346 break; 347 348 case HUP_MICROSOFT: 349 switch (usage) { 350 case 0xfe01: 351 if (!hid_device->battery_power) 352 break; 353 354 switch (val) { 355 case 1: 356 syslog(LOG_INFO, "Battery is OK on %s", 357 bt_ntoa(&s->bdaddr, NULL)); 358 break; 359 360 case 2: 361 syslog(LOG_NOTICE, "Low battery on %s", 362 bt_ntoa(&s->bdaddr, NULL)); 363 break; 364 365 case 3: 366 syslog(LOG_WARNING, "Very low battery "\ 367 "on %s", 368 bt_ntoa(&s->bdaddr, NULL)); 369 break; 370 } 371 break; 372 } 373 break; 374 } 375 } 376 hid_end_parse(d); 377 378 /* 379 * XXX FIXME Feed keyboard events into kernel. 380 * The code below works, bit host also needs to track 381 * and handle repeat. 382 * 383 * Key repeat currently works in X, but not in console. 384 */ 385 386 if (kevents > 0) { 387 if (hid_device->keyboard) { 388 assert(s->vkbd != -1); 389 kbd_process_keys(s); 390 } else 391 syslog(LOG_ERR, "Keyboard events received from " \ 392 "non-keyboard device %s. Please report", 393 bt_ntoa(&s->bdaddr, NULL)); 394 } 395 396 /* 397 * XXX FIXME Feed mouse events into kernel. 398 * The code block below works, but it is not good enough. 399 * Need to track double-clicks etc. 400 * 401 * Double click currently works in X, but not in console. 402 */ 403 404 if (mevents > 0) { 405 struct mouse_info mi; 406 407 mi.operation = MOUSE_ACTION; 408 mi.u.data.x = mouse_x; 409 mi.u.data.y = mouse_y; 410 mi.u.data.z = mouse_z; 411 mi.u.data.buttons = mouse_butt; 412 413 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0) 414 syslog(LOG_ERR, "Could not process mouse events from " \ 415 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), 416 strerror(errno), errno); 417 } 418 419 return (0); 420} 421