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 #include <bluetooth.h>
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbhid.h>
47 #include "bthid_config.h"
52 * Process data from control channel
56 hid_control(bthid_session_p s, uint8_t *data, int32_t len)
62 switch (data[0] >> 4) {
63 case 0: /* Handshake (response to command) */
65 syslog(LOG_ERR, "Got handshake message with error " \
66 "response 0x%x from %s",
67 data[0], bt_ntoa(&s->bdaddr, NULL));
70 case 1: /* HID Control */
71 switch (data[0] & 0xf) {
75 case 1: /* Hard reset */
76 case 2: /* Soft reset */
77 syslog(LOG_WARNING, "Device %s requested %s reset",
78 bt_ntoa(&s->bdaddr, NULL),
79 ((data[0] & 0xf) == 1)? "hard" : "soft");
83 syslog(LOG_NOTICE, "Device %s requested Suspend",
84 bt_ntoa(&s->bdaddr, NULL));
87 case 4: /* Exit suspend */
88 syslog(LOG_NOTICE, "Device %s requested Exit Suspend",
89 bt_ntoa(&s->bdaddr, NULL));
92 case 5: /* Virtual cable unplug */
93 syslog(LOG_NOTICE, "Device %s unplugged virtual cable",
94 bt_ntoa(&s->bdaddr, NULL));
99 syslog(LOG_WARNING, "Device %s sent unknown " \
100 "HID_Control message 0x%x",
101 bt_ntoa(&s->bdaddr, NULL), data[0]);
107 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \
108 "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL));
116 * Process data from the interrupt channel
120 hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len)
122 hid_device_p hid_device;
125 int32_t report_id, usage, page, val,
126 mouse_x, mouse_y, mouse_z, mouse_butt,
130 assert(s->srv != NULL);
131 assert(data != NULL);
134 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \
135 "channel from %s", len, bt_ntoa(&s->bdaddr, NULL));
139 if (data[0] != 0xa1) {
140 syslog(LOG_ERR, "Got unexpected message 0x%x on " \
141 "Interrupt channel from %s",
142 data[0], bt_ntoa(&s->bdaddr, NULL));
150 hid_device = get_hid_device(&s->bdaddr);
151 assert(hid_device != NULL);
153 mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0;
155 for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1);
156 hid_get_item(d, &h) > 0; ) {
157 if ((h.flags & HIO_CONST) || (h.report_ID != report_id) ||
158 (h.kind != hid_input))
161 page = HID_PAGE(h.usage);
162 val = hid_get_data(data, &h);
165 * When the input field is an array and the usage is specified
166 * with a range instead of an ID, we have to derive the actual
167 * usage by using the item value as an index in the usage range
170 if ((h.flags & HIO_VARIABLE)) {
171 usage = HID_USAGE(h.usage);
173 const uint32_t usage_offset = val - h.logical_minimum;
174 usage = HID_USAGE(h.usage_minimum + usage_offset);
178 case HUP_GENERIC_DESKTOP:
195 case HUG_SYSTEM_SLEEP:
197 syslog(LOG_NOTICE, "Sleep button pressed");
205 if (h.flags & HIO_VARIABLE) {
206 if (val && usage < kbd_maxkey())
207 bit_set(s->keys1, usage);
209 if (val && val < kbd_maxkey())
210 bit_set(s->keys1, val);
212 for (i = 1; i < h.report_count; i++) {
213 h.pos += h.report_size;
214 val = hid_get_data(data, &h);
215 if (val && val < kbd_maxkey())
216 bit_set(s->keys1, val);
228 mouse_butt |= (val << (usage - 1));
239 /* Horizontal scroll */
241 mouse_butt |= (1 << 5);
243 mouse_butt |= (1 << 6);
249 case 0xb5: /* Scan Next Track */
253 case 0xb6: /* Scan Previous Track */
257 case 0xb7: /* Stop */
261 case 0xcd: /* Play/Pause */
265 case 0xe2: /* Mute */
269 case 0xe9: /* Volume Up */
273 case 0xea: /* Volume Down */
277 case 0x183: /* Media Select */
281 case 0x018a: /* Mail */
285 case 0x192: /* Calculator */
289 case 0x194: /* My Computer */
293 case 0x221: /* WWW Search */
297 case 0x223: /* WWW Home */
301 case 0x224: /* WWW Back */
305 case 0x225: /* WWW Forward */
309 case 0x226: /* WWW Stop */
313 case 0x227: /* WWW Refresh */
317 case 0x22a: /* WWW Favorites */
326 /* XXX FIXME - UGLY HACK */
328 if (hid_device->keyboard) {
329 int32_t buf[4] = { 0xe0, val,
332 assert(s->vkbd != -1);
333 write(s->vkbd, buf, sizeof(buf));
335 syslog(LOG_ERR, "Keyboard events " \
336 "received from non-keyboard " \
337 "device %s. Please report",
338 bt_ntoa(&s->bdaddr, NULL));
345 if (!hid_device->battery_power)
350 syslog(LOG_INFO, "Battery is OK on %s",
351 bt_ntoa(&s->bdaddr, NULL));
355 syslog(LOG_NOTICE, "Low battery on %s",
356 bt_ntoa(&s->bdaddr, NULL));
360 syslog(LOG_WARNING, "Very low battery "\
362 bt_ntoa(&s->bdaddr, NULL));
373 * XXX FIXME Feed keyboard events into kernel.
374 * The code below works, bit host also needs to track
377 * Key repeat currently works in X, but not in console.
381 if (hid_device->keyboard) {
382 assert(s->vkbd != -1);
385 syslog(LOG_ERR, "Keyboard events received from " \
386 "non-keyboard device %s. Please report",
387 bt_ntoa(&s->bdaddr, NULL));
391 * XXX FIXME Feed mouse events into kernel.
392 * The code block below works, but it is not good enough.
393 * Need to track double-clicks etc.
395 * Double click currently works in X, but not in console.
399 struct mouse_info mi;
401 mi.operation = MOUSE_ACTION;
402 mi.u.data.x = mouse_x;
403 mi.u.data.y = mouse_y;
404 mi.u.data.z = mouse_z;
405 mi.u.data.buttons = mouse_butt;
407 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0)
408 syslog(LOG_ERR, "Could not process mouse events from " \
409 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
410 strerror(errno), errno);