6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $
36 #include <sys/consio.h>
37 #include <sys/mouse.h>
38 #include <sys/queue.h>
40 #define L2CAP_SOCKET_CHECKED
41 #include <bluetooth.h>
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usbhid.h>
51 #include "bthid_config.h"
57 * Inoffical and unannounced report ids for Apple Mice and trackpad
59 #define TRACKPAD_REPORT_ID 0x28
60 #define AMM_REPORT_ID 0x29
61 #define BATT_STAT_REPORT_ID 0x30
62 #define BATT_STRENGTH_REPORT_ID 0x47
63 #define SURFACE_REPORT_ID 0x61
66 * Apple magic mouse (AMM) specific device state
68 #define AMM_MAX_BUTTONS 16
70 int y [AMM_MAX_BUTTONS];
74 #define MAGIC_MOUSE(D) (((D)->vendor_id == 0x5ac) && ((D)->product_id == 0x30d))
75 #define AMM_BASIC_BLOCK 5
76 #define AMM_FINGER_BLOCK 8
77 #define AMM_VALID_REPORT(L) (((L) >= AMM_BASIC_BLOCK) && \
78 ((L) <= 16*AMM_FINGER_BLOCK + AMM_BASIC_BLOCK) && \
79 ((L) % AMM_FINGER_BLOCK) == AMM_BASIC_BLOCK)
80 #define AMM_WHEEL_SPEED 100
83 * Probe for per-device initialisation
86 hid_initialise(bthid_session_p s)
88 hid_device_p hid_device = get_hid_device(&s->bdaddr);
90 if (hid_device && MAGIC_MOUSE(hid_device)) {
91 /* Magic report to enable trackpad on Apple's Magic Mouse */
92 static uint8_t rep[] = {0x53, 0xd7, 0x01};
94 if ((s->ctx = calloc(1, sizeof(struct apple_state))) == NULL)
96 write(s->ctrl, rep, 3);
101 * Process data from control channel
105 hid_control(bthid_session_p s, uint8_t *data, int32_t len)
108 assert(data != NULL);
111 switch (data[0] >> 4) {
112 case 0: /* Handshake (response to command) */
114 syslog(LOG_ERR, "Got handshake message with error " \
115 "response 0x%x from %s",
116 data[0], bt_ntoa(&s->bdaddr, NULL));
119 case 1: /* HID Control */
120 switch (data[0] & 0xf) {
124 case 1: /* Hard reset */
125 case 2: /* Soft reset */
126 syslog(LOG_WARNING, "Device %s requested %s reset",
127 bt_ntoa(&s->bdaddr, NULL),
128 ((data[0] & 0xf) == 1)? "hard" : "soft");
131 case 3: /* Suspend */
132 syslog(LOG_NOTICE, "Device %s requested Suspend",
133 bt_ntoa(&s->bdaddr, NULL));
136 case 4: /* Exit suspend */
137 syslog(LOG_NOTICE, "Device %s requested Exit Suspend",
138 bt_ntoa(&s->bdaddr, NULL));
141 case 5: /* Virtual cable unplug */
142 syslog(LOG_NOTICE, "Device %s unplugged virtual cable",
143 bt_ntoa(&s->bdaddr, NULL));
148 syslog(LOG_WARNING, "Device %s sent unknown " \
149 "HID_Control message 0x%x",
150 bt_ntoa(&s->bdaddr, NULL), data[0]);
156 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \
157 "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL));
165 * Process data from the interrupt channel
169 hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len)
171 hid_device_p hid_device;
174 int32_t report_id, usage, page, val,
175 mouse_x, mouse_y, mouse_z, mouse_t, mouse_butt,
179 assert(s->srv != NULL);
180 assert(data != NULL);
183 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \
184 "channel from %s", len, bt_ntoa(&s->bdaddr, NULL));
188 if (data[0] != 0xa1) {
189 syslog(LOG_ERR, "Got unexpected message 0x%x on " \
190 "Interrupt channel from %s",
191 data[0], bt_ntoa(&s->bdaddr, NULL));
199 hid_device = get_hid_device(&s->bdaddr);
200 assert(hid_device != NULL);
202 mouse_x = mouse_y = mouse_z = mouse_t = mouse_butt = 0;
203 mevents = kevents = 0;
205 for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1);
206 hid_get_item(d, &h) > 0; ) {
207 if ((h.flags & HIO_CONST) || (h.report_ID != report_id) ||
208 (h.kind != hid_input))
211 page = HID_PAGE(h.usage);
212 val = hid_get_data(data, &h);
215 * When the input field is an array and the usage is specified
216 * with a range instead of an ID, we have to derive the actual
217 * usage by using the item value as an index in the usage range
220 if ((h.flags & HIO_VARIABLE)) {
221 usage = HID_USAGE(h.usage);
223 const uint32_t usage_offset = val - h.logical_minimum;
224 usage = HID_USAGE(h.usage_minimum + usage_offset);
228 case HUP_GENERIC_DESKTOP:
245 case HUG_SYSTEM_SLEEP:
247 syslog(LOG_NOTICE, "Sleep button pressed");
255 if (h.flags & HIO_VARIABLE) {
256 if (val && usage < kbd_maxkey())
257 bit_set(s->keys1, usage);
259 if (val && val < kbd_maxkey())
260 bit_set(s->keys1, val);
262 for (i = 1; i < h.report_count; i++) {
263 h.pos += h.report_size;
264 val = hid_get_data(data, &h);
265 if (val && val < kbd_maxkey())
266 bit_set(s->keys1, val);
278 mouse_butt |= (val << (usage - 1));
284 if (hid_device->keyboard && s->srv->uinput) {
285 if (h.flags & HIO_VARIABLE) {
286 uinput_rep_cons(s->ukbd, usage, !!val);
289 uinput_rep_cons(s->ukbd,
291 if (uinput_rep_cons(s->ukbd, val, 1)
302 /* Horizontal scroll */
308 case 0xb5: /* Scan Next Track */
312 case 0xb6: /* Scan Previous Track */
316 case 0xb7: /* Stop */
320 case 0xcd: /* Play/Pause */
324 case 0xe2: /* Mute */
328 case 0xe9: /* Volume Up */
332 case 0xea: /* Volume Down */
336 case 0x183: /* Media Select */
340 case 0x018a: /* Mail */
344 case 0x192: /* Calculator */
348 case 0x194: /* My Computer */
352 case 0x221: /* WWW Search */
356 case 0x223: /* WWW Home */
360 case 0x224: /* WWW Back */
364 case 0x225: /* WWW Forward */
368 case 0x226: /* WWW Stop */
372 case 0x227: /* WWW Refresh */
376 case 0x22a: /* WWW Favorites */
385 /* XXX FIXME - UGLY HACK */
387 if (hid_device->keyboard) {
388 int32_t buf[4] = { 0xe0, val,
391 assert(s->vkbd != -1);
392 write(s->vkbd, buf, sizeof(buf));
394 syslog(LOG_ERR, "Keyboard events " \
395 "received from non-keyboard " \
396 "device %s. Please report",
397 bt_ntoa(&s->bdaddr, NULL));
404 if (!hid_device->battery_power)
409 syslog(LOG_INFO, "Battery is OK on %s",
410 bt_ntoa(&s->bdaddr, NULL));
414 syslog(LOG_NOTICE, "Low battery on %s",
415 bt_ntoa(&s->bdaddr, NULL));
419 syslog(LOG_WARNING, "Very low battery "\
421 bt_ntoa(&s->bdaddr, NULL));
432 * Apple adheres to no standards and sends reports it does
433 * not introduce in its hid descriptor for its magic mouse.
434 * Handle those reports here.
436 if (MAGIC_MOUSE(hid_device) && s->ctx) {
437 struct apple_state *c = (struct apple_state *)s->ctx;
438 int firm = 0, middle = 0;
441 data++, len--; /* Chomp report_id */
443 if (report_id != AMM_REPORT_ID || !AMM_VALID_REPORT(len))
444 goto check_middle_button;
447 * The basics. When touches are detected, no normal mouse
448 * reports are sent. Collect clicks and dx/dy
455 if ((v = data[0] + ((data[2] & 0x0C) << 6)))
456 mouse_x += ((int16_t)(v << 6)) >> 6, mevents++;
457 if ((v = data[1] + ((data[2] & 0x30) << 4)))
458 mouse_y += ((int16_t)(v << 6)) >> 6, mevents++;
461 * The hard part: accumulate touch events and emulate middle
463 for (data += AMM_BASIC_BLOCK, len -= AMM_BASIC_BLOCK;
464 len >= AMM_FINGER_BLOCK;
465 data += AMM_FINGER_BLOCK, len -= AMM_FINGER_BLOCK) {
466 int x, y, z, force, id;
468 v = data[0] | ((data[1] & 0xf) << 8);
469 x = ((int16_t)(v << 4)) >> 4;
471 v = (data[1] >> 4) | (data[2] << 4);
472 y = -(((int16_t)(v << 4)) >> 4);
474 force = data[5] & 0x3f;
475 id = 0xf & ((data[5] >> 6) | (data[6] << 2));
476 z = (y - c->y[id]) / AMM_WHEEL_SPEED;
478 switch ((data[7] >> 4) & 0x7) { /* Phase */
479 case 3: /* First touch */
482 case 4: /* Touch dragged */
485 c->y[id] += z * AMM_WHEEL_SPEED;
492 /* Count firm touches vs. firm+middle touches */
493 if (force >= 8 && ++firm && x > -350 && x < 350)
498 * If a new click is registered by mouse and there are firm
499 * touches which are all in center, make it a middle click
501 if (mouse_butt && !c->button_state && firm && middle == firm)
505 * If we're still clicking and have converted the click
506 * to a middle click, keep it middle clicking
509 if (mouse_butt && c->button_state == 0x2)
512 if (mouse_butt != c->button_state)
513 c->button_state = mouse_butt, mevents++;
517 * XXX FIXME Feed keyboard events into kernel.
518 * The code below works, bit host also needs to track
521 * Key repeat currently works in X, but not in console.
525 if (hid_device->keyboard) {
526 assert(s->vkbd != -1);
529 syslog(LOG_ERR, "Keyboard events received from " \
530 "non-keyboard device %s. Please report",
531 bt_ntoa(&s->bdaddr, NULL));
535 * XXX FIXME Feed mouse events into kernel.
536 * The code block below works, but it is not good enough.
537 * Need to track double-clicks etc.
539 * Double click currently works in X, but not in console.
543 struct mouse_info mi;
545 memset(&mi, 0, sizeof(mi));
546 mi.operation = MOUSE_ACTION;
547 mi.u.data.buttons = mouse_butt;
549 /* translate T-axis into button presses */
551 mi.u.data.buttons |= 1 << (mouse_t > 0 ? 6 : 5);
552 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0)
553 syslog(LOG_ERR, "Could not process mouse " \
554 "events from %s. %s (%d)",
555 bt_ntoa(&s->bdaddr, NULL),
556 strerror(errno), errno);
559 mi.u.data.x = mouse_x;
560 mi.u.data.y = mouse_y;
561 mi.u.data.z = mouse_z;
562 mi.u.data.buttons = mouse_butt;
564 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0)
565 syslog(LOG_ERR, "Could not process mouse events from " \
566 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
567 strerror(errno), errno);
569 if (hid_device->mouse && s->srv->uinput &&
570 uinput_rep_mouse(s->umouse, mouse_x, mouse_y, mouse_z,
571 mouse_t, mouse_butt, s->obutt) < 0)
572 syslog(LOG_ERR, "Could not process mouse events from " \
573 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
574 strerror(errno), errno);
575 s->obutt = mouse_butt;