6 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $
34 #include <sys/consio.h>
35 #include <sys/mouse.h>
36 #include <sys/queue.h>
38 #define L2CAP_SOCKET_CHECKED
39 #include <bluetooth.h>
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbhid.h>
48 #include "bthid_config.h"
53 * Process data from control channel
57 hid_control(bthid_session_p s, uint8_t *data, int32_t len)
63 switch (data[0] >> 4) {
64 case 0: /* Handshake (response to command) */
66 syslog(LOG_ERR, "Got handshake message with error " \
67 "response 0x%x from %s",
68 data[0], bt_ntoa(&s->bdaddr, NULL));
71 case 1: /* HID Control */
72 switch (data[0] & 0xf) {
76 case 1: /* Hard reset */
77 case 2: /* Soft reset */
78 syslog(LOG_WARNING, "Device %s requested %s reset",
79 bt_ntoa(&s->bdaddr, NULL),
80 ((data[0] & 0xf) == 1)? "hard" : "soft");
84 syslog(LOG_NOTICE, "Device %s requested Suspend",
85 bt_ntoa(&s->bdaddr, NULL));
88 case 4: /* Exit suspend */
89 syslog(LOG_NOTICE, "Device %s requested Exit Suspend",
90 bt_ntoa(&s->bdaddr, NULL));
93 case 5: /* Virtual cable unplug */
94 syslog(LOG_NOTICE, "Device %s unplugged virtual cable",
95 bt_ntoa(&s->bdaddr, NULL));
100 syslog(LOG_WARNING, "Device %s sent unknown " \
101 "HID_Control message 0x%x",
102 bt_ntoa(&s->bdaddr, NULL), data[0]);
108 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \
109 "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL));
117 * Process data from the interrupt channel
121 hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len)
123 hid_device_p hid_device;
126 int32_t report_id, usage, page, val,
127 mouse_x, mouse_y, mouse_z, mouse_butt,
131 assert(s->srv != NULL);
132 assert(data != NULL);
135 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \
136 "channel from %s", len, bt_ntoa(&s->bdaddr, NULL));
140 if (data[0] != 0xa1) {
141 syslog(LOG_ERR, "Got unexpected message 0x%x on " \
142 "Interrupt channel from %s",
143 data[0], bt_ntoa(&s->bdaddr, NULL));
151 hid_device = get_hid_device(&s->bdaddr);
152 assert(hid_device != NULL);
154 mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0;
156 for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1);
157 hid_get_item(d, &h) > 0; ) {
158 if ((h.flags & HIO_CONST) || (h.report_ID != report_id) ||
159 (h.kind != hid_input))
162 page = HID_PAGE(h.usage);
163 val = hid_get_data(data, &h);
166 * When the input field is an array and the usage is specified
167 * with a range instead of an ID, we have to derive the actual
168 * usage by using the item value as an index in the usage range
171 if ((h.flags & HIO_VARIABLE)) {
172 usage = HID_USAGE(h.usage);
174 const uint32_t usage_offset = val - h.logical_minimum;
175 usage = HID_USAGE(h.usage_minimum + usage_offset);
179 case HUP_GENERIC_DESKTOP:
196 case HUG_SYSTEM_SLEEP:
198 syslog(LOG_NOTICE, "Sleep button pressed");
206 if (h.flags & HIO_VARIABLE) {
207 if (val && usage < kbd_maxkey())
208 bit_set(s->keys1, usage);
210 if (val && val < kbd_maxkey())
211 bit_set(s->keys1, val);
213 for (i = 1; i < h.report_count; i++) {
214 h.pos += h.report_size;
215 val = hid_get_data(data, &h);
216 if (val && val < kbd_maxkey())
217 bit_set(s->keys1, val);
229 mouse_butt |= (val << (usage - 1));
240 /* Horizontal scroll */
242 mouse_butt |= (1 << 5);
244 mouse_butt |= (1 << 6);
250 case 0xb5: /* Scan Next Track */
254 case 0xb6: /* Scan Previous Track */
258 case 0xb7: /* Stop */
262 case 0xcd: /* Play/Pause */
266 case 0xe2: /* Mute */
270 case 0xe9: /* Volume Up */
274 case 0xea: /* Volume Down */
278 case 0x183: /* Media Select */
282 case 0x018a: /* Mail */
286 case 0x192: /* Calculator */
290 case 0x194: /* My Computer */
294 case 0x221: /* WWW Search */
298 case 0x223: /* WWW Home */
302 case 0x224: /* WWW Back */
306 case 0x225: /* WWW Forward */
310 case 0x226: /* WWW Stop */
314 case 0x227: /* WWW Refresh */
318 case 0x22a: /* WWW Favorites */
327 /* XXX FIXME - UGLY HACK */
329 if (hid_device->keyboard) {
330 int32_t buf[4] = { 0xe0, val,
333 assert(s->vkbd != -1);
334 write(s->vkbd, buf, sizeof(buf));
336 syslog(LOG_ERR, "Keyboard events " \
337 "received from non-keyboard " \
338 "device %s. Please report",
339 bt_ntoa(&s->bdaddr, NULL));
346 if (!hid_device->battery_power)
351 syslog(LOG_INFO, "Battery is OK on %s",
352 bt_ntoa(&s->bdaddr, NULL));
356 syslog(LOG_NOTICE, "Low battery on %s",
357 bt_ntoa(&s->bdaddr, NULL));
361 syslog(LOG_WARNING, "Very low battery "\
363 bt_ntoa(&s->bdaddr, NULL));
374 * XXX FIXME Feed keyboard events into kernel.
375 * The code below works, bit host also needs to track
378 * Key repeat currently works in X, but not in console.
382 if (hid_device->keyboard) {
383 assert(s->vkbd != -1);
386 syslog(LOG_ERR, "Keyboard events received from " \
387 "non-keyboard device %s. Please report",
388 bt_ntoa(&s->bdaddr, NULL));
392 * XXX FIXME Feed mouse events into kernel.
393 * The code block below works, but it is not good enough.
394 * Need to track double-clicks etc.
396 * Double click currently works in X, but not in console.
400 struct mouse_info mi;
402 mi.operation = MOUSE_ACTION;
403 mi.u.data.x = mouse_x;
404 mi.u.data.y = mouse_y;
405 mi.u.data.z = mouse_z;
406 mi.u.data.buttons = mouse_butt;
408 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0)
409 syslog(LOG_ERR, "Could not process mouse events from " \
410 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
411 strerror(errno), errno);