hid.c revision 212335
1219820Sjeff/* 2219820Sjeff * hid.c 3219820Sjeff */ 4219820Sjeff 5219820Sjeff/*- 6219820Sjeff * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7219820Sjeff * All rights reserved. 8219820Sjeff * 9219820Sjeff * Redistribution and use in source and binary forms, with or without 10219820Sjeff * modification, are permitted provided that the following conditions 11219820Sjeff * are met: 12219820Sjeff * 1. Redistributions of source code must retain the above copyright 13219820Sjeff * notice, this list of conditions and the following disclaimer. 14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 15219820Sjeff * notice, this list of conditions and the following disclaimer in the 16219820Sjeff * documentation and/or other materials provided with the distribution. 17219820Sjeff * 18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28219820Sjeff * SUCH DAMAGE. 29219820Sjeff * 30219820Sjeff * $Id: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $ 31219820Sjeff * $FreeBSD: head/usr.sbin/bluetooth/bthidd/hid.c 212335 2010-09-08 20:00:27Z emax $ 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#include <sys/consio.h> 35219820Sjeff#include <sys/mouse.h> 36219820Sjeff#include <sys/queue.h> 37219820Sjeff#include <assert.h> 38219820Sjeff#include <bluetooth.h> 39219820Sjeff#include <dev/usb/usb.h> 40219820Sjeff#include <dev/usb/usbhid.h> 41219820Sjeff#include <errno.h> 42219820Sjeff#include <stdio.h> 43219820Sjeff#include <string.h> 44219820Sjeff#include <syslog.h> 45219820Sjeff#include <unistd.h> 46219820Sjeff#include <usbhid.h> 47219820Sjeff#include "bthid_config.h" 48219820Sjeff#include "bthidd.h" 49219820Sjeff#include "kbd.h" 50219820Sjeff 51219820Sjeff#undef min 52219820Sjeff#define min(x, y) (((x) < (y))? (x) : (y)) 53219820Sjeff 54219820Sjeff#undef ASIZE 55219820Sjeff#define ASIZE(a) (sizeof(a)/sizeof(a[0])) 56219820Sjeff 57219820Sjeff/* 58219820Sjeff * Process data from control channel 59219820Sjeff */ 60219820Sjeff 61219820Sjeffint32_t 62219820Sjeffhid_control(bthid_session_p s, uint8_t *data, int32_t len) 63219820Sjeff{ 64219820Sjeff assert(s != NULL); 65219820Sjeff assert(data != NULL); 66219820Sjeff assert(len > 0); 67219820Sjeff 68219820Sjeff switch (data[0] >> 4) { 69219820Sjeff case 0: /* Handshake (response to command) */ 70219820Sjeff if (data[0] & 0xf) 71219820Sjeff syslog(LOG_ERR, "Got handshake message with error " \ 72219820Sjeff "response 0x%x from %s", 73219820Sjeff data[0], bt_ntoa(&s->bdaddr, NULL)); 74219820Sjeff break; 75219820Sjeff 76219820Sjeff case 1: /* HID Control */ 77219820Sjeff switch (data[0] & 0xf) { 78219820Sjeff case 0: /* NOP */ 79219820Sjeff break; 80219820Sjeff 81219820Sjeff case 1: /* Hard reset */ 82219820Sjeff case 2: /* Soft reset */ 83219820Sjeff syslog(LOG_WARNING, "Device %s requested %s reset", 84219820Sjeff bt_ntoa(&s->bdaddr, NULL), 85219820Sjeff ((data[0] & 0xf) == 1)? "hard" : "soft"); 86219820Sjeff break; 87219820Sjeff 88219820Sjeff case 3: /* Suspend */ 89219820Sjeff syslog(LOG_NOTICE, "Device %s requested Suspend", 90219820Sjeff bt_ntoa(&s->bdaddr, NULL)); 91219820Sjeff break; 92219820Sjeff 93219820Sjeff case 4: /* Exit suspend */ 94219820Sjeff syslog(LOG_NOTICE, "Device %s requested Exit Suspend", 95219820Sjeff bt_ntoa(&s->bdaddr, NULL)); 96219820Sjeff break; 97219820Sjeff 98219820Sjeff case 5: /* Virtual cable unplug */ 99219820Sjeff syslog(LOG_NOTICE, "Device %s unplugged virtual cable", 100219820Sjeff bt_ntoa(&s->bdaddr, NULL)); 101219820Sjeff session_close(s); 102219820Sjeff break; 103219820Sjeff 104219820Sjeff default: 105219820Sjeff syslog(LOG_WARNING, "Device %s sent unknown " \ 106219820Sjeff "HID_Control message 0x%x", 107219820Sjeff bt_ntoa(&s->bdaddr, NULL), data[0]); 108219820Sjeff break; 109219820Sjeff } 110219820Sjeff break; 111219820Sjeff 112219820Sjeff default: 113219820Sjeff syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \ 114219820Sjeff "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL)); 115219820Sjeff break; 116219820Sjeff } 117219820Sjeff 118219820Sjeff return (0); 119219820Sjeff} 120219820Sjeff 121219820Sjeff/* 122219820Sjeff * Process data from the interrupt channel 123219820Sjeff */ 124219820Sjeff 125219820Sjeffint32_t 126219820Sjeffhid_interrupt(bthid_session_p s, uint8_t *data, int32_t len) 127219820Sjeff{ 128219820Sjeff hid_device_p hid_device; 129219820Sjeff hid_data_t d; 130219820Sjeff hid_item_t h; 131219820Sjeff int32_t report_id, usage, page, val, 132219820Sjeff mouse_x, mouse_y, mouse_z, mouse_butt, 133219820Sjeff mevents, kevents, i; 134219820Sjeff 135219820Sjeff assert(s != NULL); 136219820Sjeff assert(s->srv != NULL); 137219820Sjeff assert(data != NULL); 138219820Sjeff 139219820Sjeff if (len < 3) { 140219820Sjeff syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \ 141219820Sjeff "channel from %s", len, bt_ntoa(&s->bdaddr, NULL)); 142219820Sjeff return (-1); 143219820Sjeff } 144219820Sjeff 145219820Sjeff if (data[0] != 0xa1) { 146219820Sjeff syslog(LOG_ERR, "Got unexpected message 0x%x on " \ 147219820Sjeff "Interrupt channel from %s", 148219820Sjeff data[0], bt_ntoa(&s->bdaddr, NULL)); 149219820Sjeff return (-1); 150219820Sjeff } 151219820Sjeff 152219820Sjeff report_id = data[1]; 153219820Sjeff data ++; 154219820Sjeff len --; 155219820Sjeff 156219820Sjeff hid_device = get_hid_device(&s->bdaddr); 157219820Sjeff assert(hid_device != NULL); 158219820Sjeff 159219820Sjeff mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0; 160219820Sjeff 161219820Sjeff for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1); 162219820Sjeff hid_get_item(d, &h) > 0; ) { 163219820Sjeff if ((h.flags & HIO_CONST) || (h.report_ID != report_id) || 164219820Sjeff (h.kind != hid_input)) 165 continue; 166 167 page = HID_PAGE(h.usage); 168 usage = HID_USAGE(h.usage); 169 val = hid_get_data(data, &h); 170 171 switch (page) { 172 case HUP_GENERIC_DESKTOP: 173 switch (usage) { 174 case HUG_X: 175 mouse_x = val; 176 mevents ++; 177 break; 178 179 case HUG_Y: 180 mouse_y = val; 181 mevents ++; 182 break; 183 184 case HUG_WHEEL: 185 mouse_z = -val; 186 mevents ++; 187 break; 188 189 case HUG_SYSTEM_SLEEP: 190 if (val) 191 syslog(LOG_NOTICE, "Sleep button pressed"); 192 break; 193 } 194 break; 195 196 case HUP_KEYBOARD: 197 kevents ++; 198 199 if (h.flags & HIO_VARIABLE) { 200 if (val && usage < kbd_maxkey()) 201 bit_set(s->keys1, usage); 202 } else { 203 if (val && val < kbd_maxkey()) 204 bit_set(s->keys1, val); 205 206 for (i = 1; i < h.report_count; i++) { 207 h.pos += h.report_size; 208 val = hid_get_data(data, &h); 209 if (val && val < kbd_maxkey()) 210 bit_set(s->keys1, val); 211 } 212 } 213 break; 214 215 case HUP_BUTTON: 216 if (usage != 0) { 217 if (usage == 2) 218 usage = 3; 219 else if (usage == 3) 220 usage = 2; 221 222 mouse_butt |= (val << (usage - 1)); 223 mevents ++; 224 } 225 break; 226 227 case HUP_CONSUMER: 228 if (!val) 229 break; 230 231 switch (usage) { 232 case 0xb5: /* Scan Next Track */ 233 val = 0x19; 234 break; 235 236 case 0xb6: /* Scan Previous Track */ 237 val = 0x10; 238 break; 239 240 case 0xb7: /* Stop */ 241 val = 0x24; 242 break; 243 244 case 0xcd: /* Play/Pause */ 245 val = 0x22; 246 break; 247 248 case 0xe2: /* Mute */ 249 val = 0x20; 250 break; 251 252 case 0xe9: /* Volume Up */ 253 val = 0x30; 254 break; 255 256 case 0xea: /* Volume Down */ 257 val = 0x2E; 258 break; 259 260 case 0x183: /* Media Select */ 261 val = 0x6D; 262 break; 263 264 case 0x018a: /* Mail */ 265 val = 0x6C; 266 break; 267 268 case 0x192: /* Calculator */ 269 val = 0x21; 270 break; 271 272 case 0x194: /* My Computer */ 273 val = 0x6B; 274 break; 275 276 case 0x221: /* WWW Search */ 277 val = 0x65; 278 break; 279 280 case 0x223: /* WWW Home */ 281 val = 0x32; 282 break; 283 284 case 0x224: /* WWW Back */ 285 val = 0x6A; 286 break; 287 288 case 0x225: /* WWW Forward */ 289 val = 0x69; 290 break; 291 292 case 0x226: /* WWW Stop */ 293 val = 0x68; 294 break; 295 296 case 0x227: /* WWW Refresh */ 297 val = 0x67; 298 break; 299 300 case 0x22a: /* WWW Favorites */ 301 val = 0x66; 302 break; 303 304 default: 305 val = 0; 306 break; 307 } 308 309 /* XXX FIXME - UGLY HACK */ 310 if (val != 0) { 311 if (hid_device->keyboard) { 312 int32_t buf[4] = { 0xe0, val, 313 0xe0, val|0x80 }; 314 315 assert(s->vkbd != -1); 316 write(s->vkbd, buf, sizeof(buf)); 317 } else 318 syslog(LOG_ERR, "Keyboard events " \ 319 "received from non-keyboard " \ 320 "device %s. Please report", 321 bt_ntoa(&s->bdaddr, NULL)); 322 } 323 break; 324 325 case HUP_MICROSOFT: 326 switch (usage) { 327 case 0xfe01: 328 if (!hid_device->battery_power) 329 break; 330 331 switch (val) { 332 case 1: 333 syslog(LOG_INFO, "Battery is OK on %s", 334 bt_ntoa(&s->bdaddr, NULL)); 335 break; 336 337 case 2: 338 syslog(LOG_NOTICE, "Low battery on %s", 339 bt_ntoa(&s->bdaddr, NULL)); 340 break; 341 342 case 3: 343 syslog(LOG_WARNING, "Very low battery "\ 344 "on %s", 345 bt_ntoa(&s->bdaddr, NULL)); 346 break; 347 } 348 break; 349 } 350 break; 351 } 352 } 353 hid_end_parse(d); 354 355 /* 356 * XXX FIXME Feed keyboard events into kernel. 357 * The code below works, bit host also needs to track 358 * and handle repeat. 359 * 360 * Key repeat currently works in X, but not in console. 361 */ 362 363 if (kevents > 0) { 364 if (hid_device->keyboard) { 365 assert(s->vkbd != -1); 366 kbd_process_keys(s); 367 } else 368 syslog(LOG_ERR, "Keyboard events received from " \ 369 "non-keyboard device %s. Please report", 370 bt_ntoa(&s->bdaddr, NULL)); 371 } 372 373 /* 374 * XXX FIXME Feed mouse events into kernel. 375 * The code block below works, but it is not good enough. 376 * Need to track double-clicks etc. 377 * 378 * Double click currently works in X, but not in console. 379 */ 380 381 if (mevents > 0) { 382 struct mouse_info mi; 383 384 mi.operation = MOUSE_ACTION; 385 mi.u.data.x = mouse_x; 386 mi.u.data.y = mouse_y; 387 mi.u.data.z = mouse_z; 388 mi.u.data.buttons = mouse_butt; 389 390 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0) 391 syslog(LOG_ERR, "Could not process mouse events from " \ 392 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), 393 strerror(errno), errno); 394 } 395 396 return (0); 397} 398