]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/bluetooth/bthidd/hid.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / bluetooth / bthidd / hid.c
1 /*
2  * hid.c
3  */
4
5 /*-
6  * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $Id: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $
31  * $FreeBSD$
32  */
33
34 #include <sys/consio.h>
35 #include <sys/mouse.h>
36 #include <sys/queue.h>
37 #include <assert.h>
38 #include <bluetooth.h>
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbhid.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
46 #include <usbhid.h>
47 #include "bthid_config.h"
48 #include "bthidd.h"
49 #include "kbd.h"
50
51 /*
52  * Process data from control channel
53  */
54
55 int32_t
56 hid_control(bthid_session_p s, uint8_t *data, int32_t len)
57 {
58         assert(s != NULL);
59         assert(data != NULL);
60         assert(len > 0);
61
62         switch (data[0] >> 4) {
63         case 0: /* Handshake (response to command) */
64                 if (data[0] & 0xf)
65                         syslog(LOG_ERR, "Got handshake message with error " \
66                                 "response 0x%x from %s",
67                                 data[0], bt_ntoa(&s->bdaddr, NULL));
68                 break;
69
70         case 1: /* HID Control */
71                 switch (data[0] & 0xf) {
72                 case 0: /* NOP */
73                         break;
74
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");
80                         break;
81
82                 case 3: /* Suspend */
83                         syslog(LOG_NOTICE, "Device %s requested Suspend",
84                                 bt_ntoa(&s->bdaddr, NULL));
85                         break;
86
87                 case 4: /* Exit suspend */
88                         syslog(LOG_NOTICE, "Device %s requested Exit Suspend",
89                                 bt_ntoa(&s->bdaddr, NULL));
90                         break;
91
92                 case 5: /* Virtual cable unplug */
93                         syslog(LOG_NOTICE, "Device %s unplugged virtual cable",
94                                 bt_ntoa(&s->bdaddr, NULL));
95                         session_close(s);
96                         break;
97
98                 default:
99                         syslog(LOG_WARNING, "Device %s sent unknown " \
100                                 "HID_Control message 0x%x",
101                                 bt_ntoa(&s->bdaddr, NULL), data[0]);
102                         break;
103                 }
104                 break;
105
106         default:
107                 syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \
108                         "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL));
109                 break;
110         }
111
112         return (0);
113 }
114
115 /*
116  * Process data from the interrupt channel
117  */
118
119 int32_t
120 hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len)
121 {
122         hid_device_p    hid_device;
123         hid_data_t      d;
124         hid_item_t      h;
125         int32_t         report_id, usage, page, val,
126                         mouse_x, mouse_y, mouse_z, mouse_butt,
127                         mevents, kevents, i;
128
129         assert(s != NULL);
130         assert(s->srv != NULL);
131         assert(data != NULL);
132
133         if (len < 3) {
134                 syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \
135                         "channel from %s", len, bt_ntoa(&s->bdaddr, NULL));
136                 return (-1);
137         }
138
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));
143                 return (-1);
144         }
145
146         report_id = data[1];
147         data ++;
148         len --;
149
150         hid_device = get_hid_device(&s->bdaddr);
151         assert(hid_device != NULL);
152
153         mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0;
154
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))
159                         continue;
160
161                 page = HID_PAGE(h.usage);
162                 val = hid_get_data(data, &h);
163
164                 /*
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
168                  * list.
169                  */
170                 if ((h.flags & HIO_VARIABLE)) {
171                         usage = HID_USAGE(h.usage);
172                 } else {
173                         const uint32_t usage_offset = val - h.logical_minimum;
174                         usage = HID_USAGE(h.usage_minimum + usage_offset);
175                 }
176
177                 switch (page) {
178                 case HUP_GENERIC_DESKTOP:
179                         switch (usage) {
180                         case HUG_X:
181                                 mouse_x = val;
182                                 mevents ++;
183                                 break;
184
185                         case HUG_Y:
186                                 mouse_y = val;
187                                 mevents ++;
188                                 break;
189
190                         case HUG_WHEEL:
191                                 mouse_z = -val;
192                                 mevents ++;
193                                 break;
194
195                         case HUG_SYSTEM_SLEEP:
196                                 if (val)
197                                         syslog(LOG_NOTICE, "Sleep button pressed");
198                                 break;
199                         }
200                         break;
201
202                 case HUP_KEYBOARD:
203                         kevents ++;
204
205                         if (h.flags & HIO_VARIABLE) {
206                                 if (val && usage < kbd_maxkey())
207                                         bit_set(s->keys1, usage);
208                         } else {
209                                 if (val && val < kbd_maxkey())
210                                         bit_set(s->keys1, val);
211
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);
217                                 }
218                         }
219                         break;
220
221                 case HUP_BUTTON:
222                         if (usage != 0) {
223                                 if (usage == 2)
224                                         usage = 3;
225                                 else if (usage == 3)
226                                         usage = 2;
227                                 
228                                 mouse_butt |= (val << (usage - 1));
229                                 mevents ++;
230                         }
231                         break;
232
233                 case HUP_CONSUMER:
234                         if (!val)
235                                 break;
236
237                         switch (usage) {
238                         case HUC_AC_PAN:
239                                 /* Horizontal scroll */
240                                 if (val < 0)
241                                         mouse_butt |= (1 << 5);
242                                 else
243                                         mouse_butt |= (1 << 6);
244
245                                 mevents ++;
246                                 val = 0;
247                                 break;
248
249                         case 0xb5: /* Scan Next Track */
250                                 val = 0x19;
251                                 break;
252
253                         case 0xb6: /* Scan Previous Track */
254                                 val = 0x10;
255                                 break;
256
257                         case 0xb7: /* Stop */
258                                 val = 0x24;
259                                 break;
260
261                         case 0xcd: /* Play/Pause */
262                                 val = 0x22;
263                                 break;
264
265                         case 0xe2: /* Mute */
266                                 val = 0x20;
267                                 break;
268
269                         case 0xe9: /* Volume Up */
270                                 val = 0x30;
271                                 break;
272
273                         case 0xea: /* Volume Down */
274                                 val = 0x2E;
275                                 break;
276
277                         case 0x183: /* Media Select */
278                                 val = 0x6D;
279                                 break;
280
281                         case 0x018a: /* Mail */
282                                 val = 0x6C;
283                                 break;
284
285                         case 0x192: /* Calculator */
286                                 val = 0x21;
287                                 break;
288
289                         case 0x194: /* My Computer */
290                                 val = 0x6B;
291                                 break;
292
293                         case 0x221: /* WWW Search */
294                                 val = 0x65;
295                                 break;
296
297                         case 0x223: /* WWW Home */
298                                 val = 0x32;
299                                 break;
300
301                         case 0x224: /* WWW Back */
302                                 val = 0x6A;
303                                 break;
304
305                         case 0x225: /* WWW Forward */
306                                 val = 0x69;
307                                 break;
308
309                         case 0x226: /* WWW Stop */
310                                 val = 0x68;
311                                 break;
312
313                         case 0x227: /* WWW Refresh */
314                                 val = 0x67;
315                                 break;
316
317                         case 0x22a: /* WWW Favorites */
318                                 val = 0x66;
319                                 break;
320
321                         default:
322                                 val = 0;
323                                 break;
324                         }
325
326                         /* XXX FIXME - UGLY HACK */
327                         if (val != 0) {
328                                 if (hid_device->keyboard) {
329                                         int32_t buf[4] = { 0xe0, val,
330                                                            0xe0, val|0x80 };
331
332                                         assert(s->vkbd != -1);
333                                         write(s->vkbd, buf, sizeof(buf));
334                                 } else
335                                         syslog(LOG_ERR, "Keyboard events " \
336                                                 "received from non-keyboard " \
337                                                 "device %s. Please report",
338                                                 bt_ntoa(&s->bdaddr, NULL));
339                         }
340                         break;
341
342                 case HUP_MICROSOFT:
343                         switch (usage) {
344                         case 0xfe01:
345                                 if (!hid_device->battery_power)
346                                         break;
347
348                                 switch (val) {
349                                 case 1:
350                                         syslog(LOG_INFO, "Battery is OK on %s",
351                                                 bt_ntoa(&s->bdaddr, NULL));
352                                         break;
353
354                                 case 2:
355                                         syslog(LOG_NOTICE, "Low battery on %s",
356                                                 bt_ntoa(&s->bdaddr, NULL));
357                                         break;
358
359                                 case 3:
360                                         syslog(LOG_WARNING, "Very low battery "\
361                                                 "on %s",
362                                                 bt_ntoa(&s->bdaddr, NULL));
363                                         break;
364                                 }
365                                 break;
366                         }
367                         break;
368                 }
369         }
370         hid_end_parse(d);
371
372         /*
373          * XXX FIXME Feed keyboard events into kernel.
374          * The code below works, bit host also needs to track
375          * and handle repeat.
376          *
377          * Key repeat currently works in X, but not in console.
378          */
379
380         if (kevents > 0) {
381                 if (hid_device->keyboard) {
382                         assert(s->vkbd != -1);
383                         kbd_process_keys(s);
384                 } else
385                         syslog(LOG_ERR, "Keyboard events received from " \
386                                 "non-keyboard device %s. Please report",
387                                 bt_ntoa(&s->bdaddr, NULL));
388         }
389
390         /* 
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.
394          *
395          * Double click currently works in X, but not in console.
396          */
397
398         if (mevents > 0) {
399                 struct mouse_info       mi;
400
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;
406
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);
411         }
412
413         return (0);
414 }