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 #define min(x, y) (((x) < (y))? (x) : (y))
55 #define ASIZE(a) (sizeof(a)/sizeof(a[0]))
58 * Process data from control channel
62 hid_control(bthid_session_p s, uint8_t *data, int32_t len)
68 switch (data[0] >> 4) {
69 case 0: /* Handshake (response to command) */
71 syslog(LOG_ERR, "Got handshake message with error " \
72 "response 0x%x from %s",
73 data[0], bt_ntoa(&s->bdaddr, NULL));
76 case 1: /* HID Control */
77 switch (data[0] & 0xf) {
81 case 1: /* Hard reset */
82 case 2: /* Soft reset */
83 syslog(LOG_WARNING, "Device %s requested %s reset",
84 bt_ntoa(&s->bdaddr, NULL),
85 ((data[0] & 0xf) == 1)? "hard" : "soft");
89 syslog(LOG_NOTICE, "Device %s requested Suspend",
90 bt_ntoa(&s->bdaddr, NULL));
93 case 4: /* Exit suspend */
94 syslog(LOG_NOTICE, "Device %s requested Exit Suspend",
95 bt_ntoa(&s->bdaddr, NULL));
98 case 5: /* Virtual cable unplug */
99 syslog(LOG_NOTICE, "Device %s unplugged virtual cable",
100 bt_ntoa(&s->bdaddr, NULL));
105 syslog(LOG_WARNING, "Device %s sent unknown " \
106 "HID_Control message 0x%x",
107 bt_ntoa(&s->bdaddr, NULL), data[0]);
113 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \
114 "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL));
122 * Process data from the interrupt channel
126 hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len)
128 hid_device_p hid_device;
131 int32_t report_id, usage, page, val,
132 mouse_x, mouse_y, mouse_z, mouse_butt,
136 assert(s->srv != NULL);
137 assert(data != NULL);
140 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \
141 "channel from %s", len, bt_ntoa(&s->bdaddr, NULL));
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));
156 hid_device = get_hid_device(&s->bdaddr);
157 assert(hid_device != NULL);
159 mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0;
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))
166 page = HID_PAGE(h.usage);
167 usage = HID_USAGE(h.usage);
168 val = hid_get_data(data, &h);
171 case HUP_GENERIC_DESKTOP:
188 case HUG_SYSTEM_SLEEP:
190 syslog(LOG_NOTICE, "Sleep button pressed");
198 if (h.flags & HIO_VARIABLE) {
199 if (val && usage < kbd_maxkey())
200 bit_set(s->keys1, usage);
202 if (val && val < kbd_maxkey())
203 bit_set(s->keys1, val);
208 len = min(len, h.report_size);
210 val = hid_get_data(data, &h);
211 if (val && val < kbd_maxkey())
212 bit_set(s->keys1, val);
227 mouse_butt |= (val << (usage - 1));
237 case 0xb5: /* Scan Next Track */
241 case 0xb6: /* Scan Previous Track */
245 case 0xb7: /* Stop */
249 case 0xcd: /* Play/Pause */
253 case 0xe2: /* Mute */
257 case 0xe9: /* Volume Up */
261 case 0xea: /* Volume Down */
265 case 0x183: /* Media Select */
269 case 0x018a: /* Mail */
273 case 0x192: /* Calculator */
277 case 0x194: /* My Computer */
281 case 0x221: /* WWW Search */
285 case 0x223: /* WWW Home */
289 case 0x224: /* WWW Back */
293 case 0x225: /* WWW Forward */
297 case 0x226: /* WWW Stop */
301 case 0x227: /* WWW Refresh */
305 case 0x22a: /* WWW Favorites */
314 /* XXX FIXME - UGLY HACK */
316 if (hid_device->keyboard) {
317 int32_t buf[4] = { 0xe0, val,
320 assert(s->vkbd != -1);
321 write(s->vkbd, buf, sizeof(buf));
323 syslog(LOG_ERR, "Keyboard events " \
324 "received from non-keyboard " \
325 "device %s. Please report",
326 bt_ntoa(&s->bdaddr, NULL));
333 if (!hid_device->battery_power)
338 syslog(LOG_INFO, "Battery is OK on %s",
339 bt_ntoa(&s->bdaddr, NULL));
343 syslog(LOG_NOTICE, "Low battery on %s",
344 bt_ntoa(&s->bdaddr, NULL));
348 syslog(LOG_WARNING, "Very low battery "\
350 bt_ntoa(&s->bdaddr, NULL));
361 * XXX FIXME Feed keyboard events into kernel.
362 * The code below works, bit host also needs to track
365 * Key repeat currently works in X, but not in console.
369 if (hid_device->keyboard) {
370 assert(s->vkbd != -1);
373 syslog(LOG_ERR, "Keyboard events received from " \
374 "non-keyboard device %s. Please report",
375 bt_ntoa(&s->bdaddr, NULL));
379 * XXX FIXME Feed mouse events into kernel.
380 * The code block below works, but it is not good enough.
381 * Need to track double-clicks etc.
383 * Double click currently works in X, but not in console.
387 struct mouse_info mi;
389 mi.operation = MOUSE_ACTION;
390 mi.u.data.x = mouse_x;
391 mi.u.data.y = mouse_y;
392 mi.u.data.z = mouse_z;
393 mi.u.data.buttons = mouse_butt;
395 if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0)
396 syslog(LOG_ERR, "Could not process mouse events from " \
397 "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
398 strerror(errno), errno);