hid.c revision 207812
1128032Stjr/* 2123222Stjr * hid.c 3123222Stjr */ 4123222Stjr 5123222Stjr/*- 6123222Stjr * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7123222Stjr * All rights reserved. 8123222Stjr * 9123222Stjr * Redistribution and use in source and binary forms, with or without 10123222Stjr * modification, are permitted provided that the following conditions 11123222Stjr * are met: 12123222Stjr * 1. Redistributions of source code must retain the above copyright 13123222Stjr * notice, this list of conditions and the following disclaimer. 14123222Stjr * 2. Redistributions in binary form must reproduce the above copyright 15123222Stjr * notice, this list of conditions and the following disclaimer in the 16123222Stjr * documentation and/or other materials provided with the distribution. 17123222Stjr * 18123222Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19123222Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20123222Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21123222Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22123222Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23123222Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24123222Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25123222Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26123222Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27123222Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28123222Stjr * SUCH DAMAGE. 29123222Stjr * 30123222Stjr * $Id: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $ 31123222Stjr * $FreeBSD: head/usr.sbin/bluetooth/bthidd/hid.c 207812 2010-05-09 09:20:25Z kaiw $ 32123222Stjr */ 33123222Stjr 34123222Stjr#include <sys/consio.h> 35123222Stjr#include <sys/mouse.h> 36128102Stjr#include <sys/queue.h> 37123222Stjr#include <assert.h> 38123222Stjr#include <bluetooth.h> 39123222Stjr#include <dev/usb/usb.h> 40123222Stjr#include <dev/usb/usbhid.h> 41123222Stjr#include <errno.h> 42123222Stjr#include <stdio.h> 43123222Stjr#include <string.h> 44123222Stjr#include <syslog.h> 45123222Stjr#include <unistd.h> 46123222Stjr#include <usbhid.h> 47123222Stjr#include "bthid_config.h" 48123222Stjr#include "bthidd.h" 49123222Stjr#include "kbd.h" 50123222Stjr 51123222Stjr#undef min 52123222Stjr#define min(x, y) (((x) < (y))? (x) : (y)) 53128032Stjr 54128032Stjr#undef ASIZE 55123222Stjr#define ASIZE(a) (sizeof(a)/sizeof(a[0])) 56123222Stjr 57123222Stjr/* 58123222Stjr * Process data from control channel 59123222Stjr */ 60123222Stjr 61123222Stjrint32_t 62123222Stjrhid_control(bthid_session_p s, uint8_t *data, int32_t len) 63123222Stjr{ 64123222Stjr assert(s != NULL); 65123222Stjr assert(data != NULL); 66123222Stjr assert(len > 0); 67123222Stjr 68123222Stjr switch (data[0] >> 4) { 69123222Stjr case 0: /* Handshake (response to command) */ 70123222Stjr if (data[0] & 0xf) 71123222Stjr syslog(LOG_ERR, "Got handshake message with error " \ 72123222Stjr "response 0x%x from %s", 73128032Stjr data[0], bt_ntoa(&s->bdaddr, NULL)); 74128032Stjr break; 75123222Stjr 76128032Stjr case 1: /* HID Control */ 77123222Stjr switch (data[0] & 0xf) { 78128102Stjr case 0: /* NOP */ 79128102Stjr break; 80128102Stjr 81128102Stjr case 1: /* Hard reset */ 82123222Stjr case 2: /* Soft reset */ 83128102Stjr syslog(LOG_WARNING, "Device %s requested %s reset", 84123222Stjr bt_ntoa(&s->bdaddr, NULL), 85128032Stjr ((data[0] & 0xf) == 1)? "hard" : "soft"); 86131608Sru break; 87128032Stjr 88123222Stjr case 3: /* Suspend */ 89131472Sru syslog(LOG_NOTICE, "Device %s requested Suspend", 90123222Stjr bt_ntoa(&s->bdaddr, NULL)); 91123222Stjr break; 92123222Stjr 93123222Stjr case 4: /* Exit suspend */ 94123222Stjr syslog(LOG_NOTICE, "Device %s requested Exit Suspend", 95128032Stjr bt_ntoa(&s->bdaddr, NULL)); 96128032Stjr break; 97123222Stjr 98123222Stjr case 5: /* Virtual cable unplug */ 99123222Stjr syslog(LOG_NOTICE, "Device %s unplugged virtual cable", 100128032Stjr bt_ntoa(&s->bdaddr, NULL)); 101128032Stjr session_close(s); 102123222Stjr break; 103123222Stjr 104123222Stjr default: 105123222Stjr syslog(LOG_WARNING, "Device %s sent unknown " \ 106128032Stjr "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 continue; 165 166 page = HID_PAGE(h.usage); 167 usage = HID_USAGE(h.usage); 168 val = hid_get_data(data, &h); 169 170 switch (page) { 171 case HUP_GENERIC_DESKTOP: 172 switch (usage) { 173 case HUG_X: 174 mouse_x = val; 175 mevents ++; 176 break; 177 178 case HUG_Y: 179 mouse_y = val; 180 mevents ++; 181 break; 182 183 case HUG_WHEEL: 184 mouse_z = -val; 185 mevents ++; 186 break; 187 188 case HUG_SYSTEM_SLEEP: 189 if (val) 190 syslog(LOG_NOTICE, "Sleep button pressed"); 191 break; 192 } 193 break; 194 195 case HUP_KEYBOARD: 196 kevents ++; 197 198 if (h.flags & HIO_VARIABLE) { 199 if (val && usage < kbd_maxkey()) 200 bit_set(s->keys1, usage); 201 } else { 202 if (val && val < kbd_maxkey()) 203 bit_set(s->keys1, val); 204 205 for (i = 1; i < h.report_count; i++) { 206 h.pos += h.report_size; 207 val = hid_get_data(data, &h); 208 if (val && val < kbd_maxkey()) 209 bit_set(s->keys1, val); 210 } 211 } 212 break; 213 214 case HUP_BUTTON: 215 if (usage != 0) { 216 if (usage == 2) 217 usage = 3; 218 else if (usage == 3) 219 usage = 2; 220 221 mouse_butt |= (val << (usage - 1)); 222 mevents ++; 223 } 224 break; 225 226 case HUP_CONSUMER: 227 if (!val) 228 break; 229 230 switch (usage) { 231 case 0xb5: /* Scan Next Track */ 232 val = 0x19; 233 break; 234 235 case 0xb6: /* Scan Previous Track */ 236 val = 0x10; 237 break; 238 239 case 0xb7: /* Stop */ 240 val = 0x24; 241 break; 242 243 case 0xcd: /* Play/Pause */ 244 val = 0x22; 245 break; 246 247 case 0xe2: /* Mute */ 248 val = 0x20; 249 break; 250 251 case 0xe9: /* Volume Up */ 252 val = 0x30; 253 break; 254 255 case 0xea: /* Volume Down */ 256 val = 0x2E; 257 break; 258 259 case 0x183: /* Media Select */ 260 val = 0x6D; 261 break; 262 263 case 0x018a: /* Mail */ 264 val = 0x6C; 265 break; 266 267 case 0x192: /* Calculator */ 268 val = 0x21; 269 break; 270 271 case 0x194: /* My Computer */ 272 val = 0x6B; 273 break; 274 275 case 0x221: /* WWW Search */ 276 val = 0x65; 277 break; 278 279 case 0x223: /* WWW Home */ 280 val = 0x32; 281 break; 282 283 case 0x224: /* WWW Back */ 284 val = 0x6A; 285 break; 286 287 case 0x225: /* WWW Forward */ 288 val = 0x69; 289 break; 290 291 case 0x226: /* WWW Stop */ 292 val = 0x68; 293 break; 294 295 case 0x227: /* WWW Refresh */ 296 val = 0x67; 297 break; 298 299 case 0x22a: /* WWW Favorites */ 300 val = 0x66; 301 break; 302 303 default: 304 val = 0; 305 break; 306 } 307 308 /* XXX FIXME - UGLY HACK */ 309 if (val != 0) { 310 if (hid_device->keyboard) { 311 int32_t buf[4] = { 0xe0, val, 312 0xe0, val|0x80 }; 313 314 assert(s->vkbd != -1); 315 write(s->vkbd, buf, sizeof(buf)); 316 } else 317 syslog(LOG_ERR, "Keyboard events " \ 318 "received from non-keyboard " \ 319 "device %s. Please report", 320 bt_ntoa(&s->bdaddr, NULL)); 321 } 322 break; 323 324 case HUP_MICROSOFT: 325 switch (usage) { 326 case 0xfe01: 327 if (!hid_device->battery_power) 328 break; 329 330 switch (val) { 331 case 1: 332 syslog(LOG_INFO, "Battery is OK on %s", 333 bt_ntoa(&s->bdaddr, NULL)); 334 break; 335 336 case 2: 337 syslog(LOG_NOTICE, "Low battery on %s", 338 bt_ntoa(&s->bdaddr, NULL)); 339 break; 340 341 case 3: 342 syslog(LOG_WARNING, "Very low battery "\ 343 "on %s", 344 bt_ntoa(&s->bdaddr, NULL)); 345 break; 346 } 347 break; 348 } 349 break; 350 } 351 } 352 hid_end_parse(d); 353 354 /* 355 * XXX FIXME Feed keyboard events into kernel. 356 * The code below works, bit host also needs to track 357 * and handle repeat. 358 * 359 * Key repeat currently works in X, but not in console. 360 */ 361 362 if (kevents > 0) { 363 if (hid_device->keyboard) { 364 assert(s->vkbd != -1); 365 kbd_process_keys(s); 366 } else 367 syslog(LOG_ERR, "Keyboard events received from " \ 368 "non-keyboard device %s. Please report", 369 bt_ntoa(&s->bdaddr, NULL)); 370 } 371 372 /* 373 * XXX FIXME Feed mouse events into kernel. 374 * The code block below works, but it is not good enough. 375 * Need to track double-clicks etc. 376 * 377 * Double click currently works in X, but not in console. 378 */ 379 380 if (mevents > 0) { 381 struct mouse_info mi; 382 383 mi.operation = MOUSE_ACTION; 384 mi.u.data.x = mouse_x; 385 mi.u.data.y = mouse_y; 386 mi.u.data.z = mouse_z; 387 mi.u.data.buttons = mouse_butt; 388 389 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0) 390 syslog(LOG_ERR, "Could not process mouse events from " \ 391 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), 392 strerror(errno), errno); 393 } 394 395 return (0); 396} 397