hid.c revision 156965
1/* 2 * hid.c 3 * 4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: hid.c,v 1.4 2004/11/17 21:59:42 max Exp $ 29 * $FreeBSD: head/usr.sbin/bluetooth/bthidd/hid.c 156965 2006-03-21 18:42:52Z emax $ 30 */ 31 32#include <sys/consio.h> 33#include <sys/mouse.h> 34#include <sys/queue.h> 35#include <assert.h> 36#include <bluetooth.h> 37#include <errno.h> 38#include <dev/usb/usb.h> 39#include <dev/usb/usbhid.h> 40#include <stdio.h> 41#include <string.h> 42#include <syslog.h> 43#include <unistd.h> 44#include <usbhid.h> 45#include "bthidd.h" 46#include "bthid_config.h" 47#include "kbd.h" 48 49#undef min 50#define min(x, y) (((x) < (y))? (x) : (y)) 51 52#undef ASIZE 53#define ASIZE(a) (sizeof(a)/sizeof(a[0])) 54 55#undef HID_BUT 56#define HID_BUT(i) ((i) < 3 ? (((i) ^ 3) % 3) : (i)) 57 58/* 59 * Process data from control channel 60 */ 61 62int 63hid_control(bthid_session_p s, char *data, int len) 64{ 65 assert(s != NULL); 66 assert(data != NULL); 67 assert(len > 0); 68 69 switch (data[0] >> 4) { 70 case 0: /* Handshake (response to command) */ 71 if (data[0] & 0xf) 72 syslog(LOG_ERR, "Got handshake message with error " \ 73 "response 0x%x from %s", 74 data[0], bt_ntoa(&s->bdaddr, NULL)); 75 break; 76 77 case 1: /* HID Control */ 78 switch (data[0] & 0xf) { 79 case 0: /* NOP */ 80 break; 81 82 case 1: /* Hard reset */ 83 case 2: /* Soft reset */ 84 syslog(LOG_WARNING, "Device %s requested %s reset", 85 bt_ntoa(&s->bdaddr, NULL), 86 ((data[0] & 0xf) == 1)? "hard" : "soft"); 87 break; 88 89 case 3: /* Suspend */ 90 syslog(LOG_NOTICE, "Device %s requested Suspend", 91 bt_ntoa(&s->bdaddr, NULL)); 92 break; 93 94 case 4: /* Exit suspend */ 95 syslog(LOG_NOTICE, "Device %s requested Exit Suspend", 96 bt_ntoa(&s->bdaddr, NULL)); 97 break; 98 99 case 5: /* Virtual cable unplug */ 100 syslog(LOG_NOTICE, "Device %s unplugged virtual cable", 101 bt_ntoa(&s->bdaddr, NULL)); 102 session_close(s); 103 break; 104 105 default: 106 syslog(LOG_WARNING, "Device %s sent unknown " \ 107 "HID_Control message 0x%x", 108 bt_ntoa(&s->bdaddr, NULL), data[0]); 109 break; 110 } 111 break; 112 113 default: 114 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \ 115 "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL)); 116 break; 117 } 118 119 return (0); 120} 121 122/* 123 * Process data from the interrupt channel 124 */ 125 126int 127hid_interrupt(bthid_session_p s, char *data, int len) 128{ 129 hid_device_p hid_device = NULL; 130 hid_data_t d; 131 hid_item_t h; 132 int report_id, usage, page, val, 133 mouse_x, mouse_y, mouse_z, mouse_butt, 134 mevents, kevents; 135 136 assert(s != NULL); 137 assert(s->srv != NULL); 138 assert(data != NULL); 139 140 if (len < 3) { 141 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \ 142 "channel from %s", len, bt_ntoa(&s->bdaddr, NULL)); 143 return (-1); 144 } 145 146 if ((unsigned char) data[0] != 0xa1) { 147 syslog(LOG_ERR, "Got unexpected message 0x%x on " \ 148 "Interrupt channel from %s", 149 data[0], bt_ntoa(&s->bdaddr, NULL)); 150 return (-1); 151 } 152 153 report_id = data[1]; 154 data += 2; 155 len -= 2; 156 157 hid_device = get_hid_device(&s->bdaddr); 158 assert(hid_device != NULL); 159 160 mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0; 161 162 for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1); 163 hid_get_item(d, &h) > 0; ) { 164 if ((h.flags & HIO_CONST) || (h.report_ID != report_id)) 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->srv->keys, usage); 202 } else { 203 if (val && val < kbd_maxkey()) 204 bit_set(s->srv->keys, val); 205 206 data ++; 207 len --; 208 209 len = min(len, h.report_size); 210 while (len > 0) { 211 val = hid_get_data(data, &h); 212 if (val && val < kbd_maxkey()) 213 bit_set(s->srv->keys, val); 214 215 data ++; 216 len --; 217 } 218 } 219 break; 220 221 case HUP_BUTTON: 222 mouse_butt |= (val << HID_BUT(usage - 1)); 223 mevents ++; 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 int buf[4] = { 0xe0, val, 0xe0, val|0x80 }; 311 312 write(s->srv->vkbd, buf, sizeof(buf)); 313 } 314 break; 315 316 case HUP_MICROSOFT: 317 switch (usage) { 318 case 0xfe01: 319 if (!hid_device->battery_power) 320 break; 321 322 switch (val) { 323 case 1: 324 syslog(LOG_INFO, "Battery is OK on %s", 325 bt_ntoa(&s->bdaddr, NULL)); 326 break; 327 328 case 2: 329 syslog(LOG_NOTICE, "Low battery on %s", 330 bt_ntoa(&s->bdaddr, NULL)); 331 break; 332 333 case 3: 334 syslog(LOG_WARNING, "Very low battery "\ 335 "on %s", 336 bt_ntoa(&s->bdaddr, NULL)); 337 break; 338 } 339 break; 340 } 341 break; 342 } 343 } 344 hid_end_parse(d); 345 346 /* Feed keyboard events into kernel */ 347 if (kevents > 0) 348 kbd_process_keys(s); 349 350 /* 351 * XXX FIXME Feed mouse events into kernel. 352 * The code block below works, but it is not good enough. 353 * Need to track double-clicks etc. 354 */ 355 356 if (mevents > 0) { 357 struct mouse_info mi; 358 359 mi.operation = MOUSE_ACTION; 360 mi.u.data.x = mouse_x; 361 mi.u.data.y = mouse_y; 362 mi.u.data.z = mouse_z; 363 mi.u.data.buttons = mouse_butt; 364 365 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0) 366 syslog(LOG_ERR, "Could not process mouse events from " \ 367 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), 368 strerror(errno), errno); 369 } 370 371 return (0); 372} 373 374