hid.c revision 137868
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 137868 2004-11-18 18:05:15Z 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/*
56 * Process data from control channel
57 */
58
59int
60hid_control(bthid_session_p s, char *data, int len)
61{
62	assert(s != NULL);
63	assert(data != NULL);
64	assert(len > 0);
65
66	switch (data[0] >> 4) {
67        case 0: /* Handshake (response to command) */
68		if (data[0] & 0xf)
69			syslog(LOG_ERR, "Got handshake message with error " \
70				"response 0x%x from %s",
71				data[0], bt_ntoa(&s->bdaddr, NULL));
72		break;
73
74	case 1: /* HID Control */
75		switch (data[0] & 0xf) {
76		case 0: /* NOP */
77			break;
78
79		case 1: /* Hard reset */
80		case 2: /* Soft reset */
81			syslog(LOG_WARNING, "Device %s requested %s reset",
82				bt_ntoa(&s->bdaddr, NULL),
83				((data[0] & 0xf) == 1)? "hard" : "soft");
84			break;
85
86		case 3: /* Suspend */
87			syslog(LOG_NOTICE, "Device %s requested Suspend",
88				bt_ntoa(&s->bdaddr, NULL));
89			break;
90
91		case 4: /* Exit suspend */
92			syslog(LOG_NOTICE, "Device %s requested Exit Suspend",
93				bt_ntoa(&s->bdaddr, NULL));
94			break;
95
96		case 5: /* Virtual cable unplug */
97			syslog(LOG_NOTICE, "Device %s unplugged virtual cable",
98				bt_ntoa(&s->bdaddr, NULL));
99			session_close(s);
100			break;
101
102		default:
103			syslog(LOG_WARNING, "Device %s sent unknown " \
104                                "HID_Control message 0x%x",
105				bt_ntoa(&s->bdaddr, NULL), data[0]);
106			break;
107		}
108		break;
109
110	default:
111		syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \
112			"channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL));
113		break;
114	}
115
116	return (0);
117}
118
119/*
120 * Process data from the interrupt channel
121 */
122
123int
124hid_interrupt(bthid_session_p s, char *data, int len)
125{
126	hid_device_p	hid_device = NULL;
127	hid_data_t	d;
128	hid_item_t	h;
129	int		report_id, usage, page, val,
130			mouse_x, mouse_y, mouse_z, mouse_butt,
131			mevents, kevents;
132
133	assert(s != NULL);
134	assert(s->srv != NULL);
135	assert(data != NULL);
136
137	if (len < 3) {
138		syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \
139			"channel from %s", len, bt_ntoa(&s->bdaddr, NULL));
140		return (-1);
141	}
142
143	if ((unsigned char) data[0] != 0xa1) {
144		syslog(LOG_ERR, "Got unexpected message 0x%x on " \
145			"Interrupt channel from %s",
146			data[0], bt_ntoa(&s->bdaddr, NULL));
147		return (-1);
148	}
149
150	report_id = data[1];
151	data += 2;
152	len -= 2;
153
154	hid_device = get_hid_device(&s->bdaddr);
155	assert(hid_device != NULL);
156
157	mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0;
158
159	for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1);
160	     hid_get_item(d, &h) > 0; ) {
161		if ((h.flags & HIO_CONST) || (h.report_ID != report_id))
162			continue;
163
164		page = HID_PAGE(h.usage);
165		usage = HID_USAGE(h.usage);
166		val = hid_get_data(data, &h);
167
168		switch (page) {
169		case HUP_GENERIC_DESKTOP:
170			switch (usage) {
171			case HUG_X:
172				mouse_x = val;
173				mevents ++;
174				break;
175
176			case HUG_Y:
177				mouse_y = val;
178				mevents ++;
179				break;
180
181			case HUG_WHEEL:
182				mouse_z = -val;
183				mevents ++;
184				break;
185
186			case HUG_SYSTEM_SLEEP:
187				if (val)
188					syslog(LOG_NOTICE, "Sleep button pressed");
189				break;
190			}
191			break;
192
193		case HUP_KEYBOARD:
194			kevents ++;
195
196			if (h.flags & HIO_VARIABLE) {
197				if (val && usage < kbd_maxkey())
198					bit_set(s->srv->keys, usage);
199			} else {
200				if (val && val < kbd_maxkey())
201					bit_set(s->srv->keys, val);
202
203				data ++;
204				len --;
205
206				len = min(len, h.report_size);
207				while (len > 0) {
208					val = hid_get_data(data, &h);
209					if (val && val < kbd_maxkey())
210						bit_set(s->srv->keys, val);
211
212					data ++;
213					len --;
214				}
215			}
216			break;
217
218		case HUP_BUTTON:
219			mouse_butt |= (val << (usage - 1));
220			mevents ++;
221			break;
222
223		case HUP_CONSUMER:
224			if (!val)
225				break;
226
227			switch (usage) {
228			case 0xb5: /* Scan Next Track */
229				val = 0x19;
230				break;
231
232			case 0xb6: /* Scan Previous Track */
233				val = 0x10;
234				break;
235
236			case 0xb7: /* Stop */
237				val = 0x24;
238				break;
239
240			case 0xcd: /* Play/Pause */
241				val = 0x22;
242				break;
243
244			case 0xe2: /* Mute */
245				val = 0x20;
246				break;
247
248			case 0xe9: /* Volume Up */
249				val = 0x30;
250				break;
251
252			case 0xea: /* Volume Down */
253				val = 0x2E;
254				break;
255
256			case 0x183: /* Media Select */
257				val = 0x6D;
258				break;
259
260			case 0x018a: /* Mail */
261				val = 0x6C;
262				break;
263
264			case 0x192: /* Calculator */
265				val = 0x21;
266				break;
267
268			case 0x194: /* My Computer */
269				val = 0x6B;
270				break;
271
272			case 0x221: /* WWW Search */
273				val = 0x65;
274				break;
275
276			case 0x223: /* WWW Home */
277				val = 0x32;
278				break;
279
280			case 0x224: /* WWW Back */
281				val = 0x6A;
282				break;
283
284			case 0x225: /* WWW Forward */
285				val = 0x69;
286				break;
287
288			case 0x226: /* WWW Stop */
289				val = 0x68;
290				break;
291
292			case 0227: /* WWW Refresh */
293				val = 0x67;
294				break;
295
296			case 0x22a: /* WWW Favorites */
297				val = 0x66;
298				break;
299
300			default:
301				val = 0;
302				break;
303			}
304
305			/* XXX FIXME - UGLY HACK */
306			if (val != 0) {
307				int	buf[4] = { 0xe0, val, 0xe0, val|0x80 };
308
309				write(s->srv->vkbd, buf, sizeof(buf));
310			}
311			break;
312
313		case HUP_MICROSOFT:
314			switch (usage) {
315			case 0xfe01:
316				if (!hid_device->battery_power)
317					break;
318
319				switch (val) {
320				case 1:
321					syslog(LOG_INFO, "Battery is OK on %s",
322						bt_ntoa(&s->bdaddr, NULL));
323					break;
324
325				case 2:
326					syslog(LOG_NOTICE, "Low battery on %s",
327						bt_ntoa(&s->bdaddr, NULL));
328					break;
329
330				case 3:
331					syslog(LOG_WARNING, "Very low battery "\
332                                                "on %s",
333						bt_ntoa(&s->bdaddr, NULL));
334					break;
335                                }
336				break;
337			}
338			break;
339		}
340	}
341	hid_end_parse(d);
342
343	/* Feed keyboard events into kernel */
344	if (kevents > 0)
345		kbd_process_keys(s);
346
347	/*
348	 * XXX FIXME Feed mouse events into kernel.
349	 * The code block below works, but it is not good enough.
350	 * Need to track double-clicks etc.
351	 */
352
353	if (mevents > 0) {
354		struct mouse_info	mi;
355
356		mi.operation = MOUSE_ACTION;
357		mi.u.data.x = mouse_x;
358		mi.u.data.y = mouse_y;
359		mi.u.data.z = mouse_z;
360		mi.u.data.buttons = mouse_butt;
361
362		if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0)
363			syslog(LOG_ERR, "Could not process mouse events from " \
364				"%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
365				strerror(errno), errno);
366	}
367
368	return (0);
369}
370
371