]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/bluetooth/bthidd/kbd.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / bluetooth / bthidd / kbd.c
1 /*
2  * kbd.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: kbd.c,v 1.4 2006/09/07 21:06:53 max Exp $
31  * $FreeBSD$
32  */
33
34 #include <sys/consio.h>
35 #include <sys/ioctl.h>
36 #include <sys/kbio.h>
37 #include <sys/queue.h>
38 #include <sys/wait.h>
39 #include <assert.h>
40 #include <bluetooth.h>
41 #include <dev/usb/usb.h>
42 #include <dev/usb/usbhid.h>
43 #include <dev/vkbd/vkbd_var.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <usbhid.h>
54 #include "bthid_config.h"
55 #include "bthidd.h"
56 #include "kbd.h"
57
58 static void     kbd_write(bitstr_t *m, int32_t fb, int32_t make, int32_t fd);
59 static int32_t  kbd_xlate(int32_t code, int32_t make, int32_t *b, int32_t const *eob);
60
61 /*
62  * HID code to PS/2 set 1 code translation table.
63  *
64  * http://www.microsoft.com/whdc/device/input/Scancode.mspx
65  *
66  * The table only contains "make" (key pressed) codes.
67  * The "break" (key released) code is generated as "make" | 0x80
68  */
69
70 #define E0PREFIX        (1 << 31)
71 #define NOBREAK         (1 << 30)
72 #define CODEMASK        (~(E0PREFIX|NOBREAK))
73
74 static int32_t const    x[] =
75 {
76 /*==================================================*/
77 /* Name                   HID code    Make     Break*/
78 /*==================================================*/
79 /* No Event                     00 */ -1,   /* None */
80 /* Overrun Error                01 */ NOBREAK|0xFF, /* None */
81 /* POST Fail                    02 */ NOBREAK|0xFC, /* None */
82 /* ErrorUndefined               03 */ -1,   /* Unassigned */
83 /* a A                          04 */ 0x1E, /* 9E */
84 /* b B                          05 */ 0x30, /* B0 */
85 /* c C                          06 */ 0x2E, /* AE */
86 /* d D                          07 */ 0x20, /* A0 */
87 /* e E                          08 */ 0x12, /* 92 */
88 /* f F                          09 */ 0x21, /* A1 */
89 /* g G                          0A */ 0x22, /* A2 */
90 /* h H                          0B */ 0x23, /* A3 */
91 /* i I                          0C */ 0x17, /* 97 */
92 /* j J                          0D */ 0x24, /* A4 */
93 /* k K                          0E */ 0x25, /* A5 */
94 /* l L                          0F */ 0x26, /* A6 */
95 /* m M                          10 */ 0x32, /* B2 */
96 /* n N                          11 */ 0x31, /* B1 */
97 /* o O                          12 */ 0x18, /* 98 */
98 /* p P                          13 */ 0x19, /* 99 */
99 /* q Q                          14 */ 0x10, /* 90 */
100 /* r R                          15 */ 0x13, /* 93 */
101 /* s S                          16 */ 0x1F, /* 9F */
102 /* t T                          17 */ 0x14, /* 94 */
103 /* u U                          18 */ 0x16, /* 96 */
104 /* v V                          19 */ 0x2F, /* AF */
105 /* w W                          1A */ 0x11, /* 91 */
106 /* x X                          1B */ 0x2D, /* AD */
107 /* y Y                          1C */ 0x15, /* 95 */
108 /* z Z                          1D */ 0x2C, /* AC */
109 /* 1 !                          1E */ 0x02, /* 82 */
110 /* 2 @                          1F */ 0x03, /* 83 */
111 /* 3 #                          20 */ 0x04, /* 84 */
112 /* 4 $                          21 */ 0x05, /* 85 */
113 /* 5 %                          22 */ 0x06, /* 86 */
114 /* 6 ^                          23 */ 0x07, /* 87 */
115 /* 7 &                          24 */ 0x08, /* 88 */
116 /* 8 *                          25 */ 0x09, /* 89 */
117 /* 9 (                          26 */ 0x0A, /* 8A */
118 /* 0 )                          27 */ 0x0B, /* 8B */
119 /* Return                       28 */ 0x1C, /* 9C */
120 /* Escape                       29 */ 0x01, /* 81 */
121 /* Backspace                    2A */ 0x0E, /* 8E */
122 /* Tab                          2B */ 0x0F, /* 8F */
123 /* Space                        2C */ 0x39, /* B9 */
124 /* - _                          2D */ 0x0C, /* 8C */
125 /* = +                          2E */ 0x0D, /* 8D */
126 /* [ {                          2F */ 0x1A, /* 9A */
127 /* ] }                          30 */ 0x1B, /* 9B */
128 /* \ |                          31 */ 0x2B, /* AB */
129 /* Europe 1                     32 */ 0x2B, /* AB */
130 /* ; :                          33 */ 0x27, /* A7 */
131 /* " '                          34 */ 0x28, /* A8 */
132 /* ` ~                          35 */ 0x29, /* A9 */
133 /* comma <                      36 */ 0x33, /* B3 */
134 /* . >                          37 */ 0x34, /* B4 */
135 /* / ?                          38 */ 0x35, /* B5 */
136 /* Caps Lock                    39 */ 0x3A, /* BA */
137 /* F1                           3A */ 0x3B, /* BB */
138 /* F2                           3B */ 0x3C, /* BC */
139 /* F3                           3C */ 0x3D, /* BD */
140 /* F4                           3D */ 0x3E, /* BE */
141 /* F5                           3E */ 0x3F, /* BF */
142 /* F6                           3F */ 0x40, /* C0 */
143 /* F7                           40 */ 0x41, /* C1 */
144 /* F8                           41 */ 0x42, /* C2 */
145 /* F9                           42 */ 0x43, /* C3 */
146 /* F10                          43 */ 0x44, /* C4 */
147 /* F11                          44 */ 0x57, /* D7 */
148 /* F12                          45 */ 0x58, /* D8 */
149 /* Print Screen                 46 */ E0PREFIX|0x37, /* E0 B7 */
150 /* Scroll Lock                  47 */ 0x46, /* C6 */
151 #if 0
152 /* Break (Ctrl-Pause)           48 */ E0 46 E0 C6, /* None */
153 /* Pause                        48 */ E1 1D 45 E1 9D C5, /* None */
154 #else
155 /* Break (Ctrl-Pause)/Pause     48 */ NOBREAK /* Special case */, /* None */
156 #endif
157 /* Insert                       49 */ E0PREFIX|0x52, /* E0 D2 */
158 /* Home                         4A */ E0PREFIX|0x47, /* E0 C7 */
159 /* Page Up                      4B */ E0PREFIX|0x49, /* E0 C9 */
160 /* Delete                       4C */ E0PREFIX|0x53, /* E0 D3 */
161 /* End                          4D */ E0PREFIX|0x4F, /* E0 CF */
162 /* Page Down                    4E */ E0PREFIX|0x51, /* E0 D1 */
163 /* Right Arrow                  4F */ E0PREFIX|0x4D, /* E0 CD */
164 /* Left Arrow                   50 */ E0PREFIX|0x4B, /* E0 CB */
165 /* Down Arrow                   51 */ E0PREFIX|0x50, /* E0 D0 */
166 /* Up Arrow                     52 */ E0PREFIX|0x48, /* E0 C8 */
167 /* Num Lock                     53 */ 0x45, /* C5 */
168 /* Keypad /                     54 */ E0PREFIX|0x35, /* E0 B5 */
169 /* Keypad *                     55 */ 0x37, /* B7 */
170 /* Keypad -                     56 */ 0x4A, /* CA */
171 /* Keypad +                     57 */ 0x4E, /* CE */
172 /* Keypad Enter                 58 */ E0PREFIX|0x1C, /* E0 9C */
173 /* Keypad 1 End                 59 */ 0x4F, /* CF */
174 /* Keypad 2 Down                5A */ 0x50, /* D0 */
175 /* Keypad 3 PageDn              5B */ 0x51, /* D1 */
176 /* Keypad 4 Left                5C */ 0x4B, /* CB */
177 /* Keypad 5                     5D */ 0x4C, /* CC */
178 /* Keypad 6 Right               5E */ 0x4D, /* CD */
179 /* Keypad 7 Home                5F */ 0x47, /* C7 */
180 /* Keypad 8 Up                  60 */ 0x48, /* C8 */
181 /* Keypad 9 PageUp              61 */ 0x49, /* C9 */
182 /* Keypad 0 Insert              62 */ 0x52, /* D2 */
183 /* Keypad . Delete              63 */ 0x53, /* D3 */
184 /* Europe 2                     64 */ 0x56, /* D6 */
185 /* App                          65 */ E0PREFIX|0x5D, /* E0 DD */
186 /* Keyboard Power               66 */ E0PREFIX|0x5E, /* E0 DE */
187 /* Keypad =                     67 */ 0x59, /* D9 */
188 /* F13                          68 */ 0x64, /* E4 */
189 /* F14                          69 */ 0x65, /* E5 */
190 /* F15                          6A */ 0x66, /* E6 */
191 /* F16                          6B */ 0x67, /* E7 */
192 /* F17                          6C */ 0x68, /* E8 */
193 /* F18                          6D */ 0x69, /* E9 */
194 /* F19                          6E */ 0x6A, /* EA */
195 /* F20                          6F */ 0x6B, /* EB */
196 /* F21                          70 */ 0x6C, /* EC */
197 /* F22                          71 */ 0x6D, /* ED */
198 /* F23                          72 */ 0x6E, /* EE */
199 /* F24                          73 */ 0x76, /* F6 */
200 /* Keyboard Execute             74 */ -1,   /* Unassigned */
201 /* Keyboard Help                75 */ -1,   /* Unassigned */
202 /* Keyboard Menu                76 */ -1,   /* Unassigned */
203 /* Keyboard Select              77 */ -1,   /* Unassigned */
204 /* Keyboard Stop                78 */ -1,   /* Unassigned */
205 /* Keyboard Again               79 */ -1,   /* Unassigned */
206 /* Keyboard Undo                7A */ -1,   /* Unassigned */
207 /* Keyboard Cut                 7B */ -1,   /* Unassigned */
208 /* Keyboard Copy                7C */ -1,   /* Unassigned */
209 /* Keyboard Paste               7D */ -1,   /* Unassigned */
210 /* Keyboard Find                7E */ -1,   /* Unassigned */
211 /* Keyboard Mute                7F */ -1,   /* Unassigned */
212 /* Keyboard Volume Up           80 */ -1,   /* Unassigned */
213 /* Keyboard Volume Dn           81 */ -1,   /* Unassigned */
214 /* Keyboard Locking Caps Lock   82 */ -1,   /* Unassigned */
215 /* Keyboard Locking Num Lock    83 */ -1,   /* Unassigned */
216 /* Keyboard Locking Scroll Lock 84 */ -1,   /* Unassigned */
217 /* Keypad comma                 85 */ 0x7E, /* FE */
218 /* Keyboard Equal Sign          86 */ -1,   /* Unassigned */
219 /* Keyboard Int'l 1             87 */ 0x73, /* F3 */
220 /* Keyboard Int'l 2             88 */ 0x70, /* F0 */
221 /* Keyboard Int'l 2             89 */ 0x7D, /* FD */
222 /* Keyboard Int'l 4             8A */ 0x79, /* F9 */
223 /* Keyboard Int'l 5             8B */ 0x7B, /* FB */
224 /* Keyboard Int'l 6             8C */ 0x5C, /* DC */
225 /* Keyboard Int'l 7             8D */ -1,   /* Unassigned */
226 /* Keyboard Int'l 8             8E */ -1,   /* Unassigned */
227 /* Keyboard Int'l 9             8F */ -1,   /* Unassigned */
228 /* Keyboard Lang 1              90 */ NOBREAK|0xF2, /* None */
229 /* Keyboard Lang 2              91 */ NOBREAK|0xF1, /* None */
230 /* Keyboard Lang 3              92 */ 0x78, /* F8 */
231 /* Keyboard Lang 4              93 */ 0x77, /* F7 */
232 /* Keyboard Lang 5              94 */ 0x76, /* F6 */
233 /* Keyboard Lang 6              95 */ -1,   /* Unassigned */
234 /* Keyboard Lang 7              96 */ -1,   /* Unassigned */
235 /* Keyboard Lang 8              97 */ -1,   /* Unassigned */
236 /* Keyboard Lang 9              98 */ -1,   /* Unassigned */
237 /* Keyboard Alternate Erase     99 */ -1,   /* Unassigned */
238 /* Keyboard SysReq/Attention    9A */ -1,   /* Unassigned */
239 /* Keyboard Cancel              9B */ -1,   /* Unassigned */
240 /* Keyboard Clear               9C */ -1,   /* Unassigned */
241 /* Keyboard Prior               9D */ -1,   /* Unassigned */
242 /* Keyboard Return              9E */ -1,   /* Unassigned */
243 /* Keyboard Separator           9F */ -1,   /* Unassigned */
244 /* Keyboard Out                 A0 */ -1,   /* Unassigned */
245 /* Keyboard Oper                A1 */ -1,   /* Unassigned */
246 /* Keyboard Clear/Again         A2 */ -1,   /* Unassigned */
247 /* Keyboard CrSel/Props         A3 */ -1,   /* Unassigned */
248 /* Keyboard ExSel               A4 */ -1,   /* Unassigned */
249 /* Reserved                     A5 */ -1,   /* Reserved */
250 /* Reserved                     A6 */ -1,   /* Reserved */
251 /* Reserved                     A7 */ -1,   /* Reserved */
252 /* Reserved                     A8 */ -1,   /* Reserved */
253 /* Reserved                     A9 */ -1,   /* Reserved */
254 /* Reserved                     AA */ -1,   /* Reserved */
255 /* Reserved                     AB */ -1,   /* Reserved */
256 /* Reserved                     AC */ -1,   /* Reserved */
257 /* Reserved                     AD */ -1,   /* Reserved */
258 /* Reserved                     AE */ -1,   /* Reserved */
259 /* Reserved                     AF */ -1,   /* Reserved */
260 /* Reserved                     B0 */ -1,   /* Reserved */
261 /* Reserved                     B1 */ -1,   /* Reserved */
262 /* Reserved                     B2 */ -1,   /* Reserved */
263 /* Reserved                     B3 */ -1,   /* Reserved */
264 /* Reserved                     B4 */ -1,   /* Reserved */
265 /* Reserved                     B5 */ -1,   /* Reserved */
266 /* Reserved                     B6 */ -1,   /* Reserved */
267 /* Reserved                     B7 */ -1,   /* Reserved */
268 /* Reserved                     B8 */ -1,   /* Reserved */
269 /* Reserved                     B9 */ -1,   /* Reserved */
270 /* Reserved                     BA */ -1,   /* Reserved */
271 /* Reserved                     BB */ -1,   /* Reserved */
272 /* Reserved                     BC */ -1,   /* Reserved */
273 /* Reserved                     BD */ -1,   /* Reserved */
274 /* Reserved                     BE */ -1,   /* Reserved */
275 /* Reserved                     BF */ -1,   /* Reserved */
276 /* Reserved                     C0 */ -1,   /* Reserved */
277 /* Reserved                     C1 */ -1,   /* Reserved */
278 /* Reserved                     C2 */ -1,   /* Reserved */
279 /* Reserved                     C3 */ -1,   /* Reserved */
280 /* Reserved                     C4 */ -1,   /* Reserved */
281 /* Reserved                     C5 */ -1,   /* Reserved */
282 /* Reserved                     C6 */ -1,   /* Reserved */
283 /* Reserved                     C7 */ -1,   /* Reserved */
284 /* Reserved                     C8 */ -1,   /* Reserved */
285 /* Reserved                     C9 */ -1,   /* Reserved */
286 /* Reserved                     CA */ -1,   /* Reserved */
287 /* Reserved                     CB */ -1,   /* Reserved */
288 /* Reserved                     CC */ -1,   /* Reserved */
289 /* Reserved                     CD */ -1,   /* Reserved */
290 /* Reserved                     CE */ -1,   /* Reserved */
291 /* Reserved                     CF */ -1,   /* Reserved */
292 /* Reserved                     D0 */ -1,   /* Reserved */
293 /* Reserved                     D1 */ -1,   /* Reserved */
294 /* Reserved                     D2 */ -1,   /* Reserved */
295 /* Reserved                     D3 */ -1,   /* Reserved */
296 /* Reserved                     D4 */ -1,   /* Reserved */
297 /* Reserved                     D5 */ -1,   /* Reserved */
298 /* Reserved                     D6 */ -1,   /* Reserved */
299 /* Reserved                     D7 */ -1,   /* Reserved */
300 /* Reserved                     D8 */ -1,   /* Reserved */
301 /* Reserved                     D9 */ -1,   /* Reserved */
302 /* Reserved                     DA */ -1,   /* Reserved */
303 /* Reserved                     DB */ -1,   /* Reserved */
304 /* Reserved                     DC */ -1,   /* Reserved */
305 /* Reserved                     DD */ -1,   /* Reserved */
306 /* Reserved                     DE */ -1,   /* Reserved */
307 /* Reserved                     DF */ -1,   /* Reserved */
308 /* Left Control                 E0 */ 0x1D, /* 9D */
309 /* Left Shift                   E1 */ 0x2A, /* AA */
310 /* Left Alt                     E2 */ 0x38, /* B8 */
311 /* Left GUI                     E3 */ E0PREFIX|0x5B, /* E0 DB */
312 /* Right Control                E4 */ E0PREFIX|0x1D, /* E0 9D */
313 /* Right Shift                  E5 */ 0x36, /* B6 */
314 /* Right Alt                    E6 */ E0PREFIX|0x38, /* E0 B8 */
315 /* Right GUI                    E7 */ E0PREFIX|0x5C  /* E0 DC */
316 };
317
318 #define xsize   ((int32_t)(sizeof(x)/sizeof(x[0])))
319
320 /*
321  * Get a max HID keycode (aligned)
322  */
323
324 int32_t
325 kbd_maxkey(void)
326 {
327         return (xsize);
328 }
329
330 /*
331  * Process keys
332  */
333
334 int32_t
335 kbd_process_keys(bthid_session_p s)
336 {
337         bitstr_t        diff[bitstr_size(xsize)];
338         int32_t         f1, f2, i;
339
340         assert(s != NULL);
341         assert(s->srv != NULL);
342
343         /* Check if the new keys have been pressed */
344         bit_ffs(s->keys1, xsize, &f1);
345
346         /* Check if old keys still pressed */
347         bit_ffs(s->keys2, xsize, &f2);
348
349         if (f1 == -1) {
350                 /* no new key pressed */
351                 if (f2 != -1) {
352                         /* release old keys */
353                         kbd_write(s->keys2, f2, 0, s->vkbd);
354                         memset(s->keys2, 0, bitstr_size(xsize));
355                 }
356
357                 return (0);
358         }
359
360         if (f2 == -1) {
361                 /* no old keys, but new keys pressed */
362                 assert(f1 != -1);
363                 
364                 memcpy(s->keys2, s->keys1, bitstr_size(xsize));
365                 kbd_write(s->keys1, f1, 1, s->vkbd);
366                 memset(s->keys1, 0, bitstr_size(xsize));
367
368                 return (0);
369         }
370
371         /* new keys got pressed, old keys got released */
372         memset(diff, 0, bitstr_size(xsize));
373
374         for (i = f2; i < xsize; i ++) {
375                 if (bit_test(s->keys2, i)) {
376                         if (!bit_test(s->keys1, i)) {
377                                 bit_clear(s->keys2, i);
378                                 bit_set(diff, i);
379                         }
380                 }
381         }
382
383         for (i = f1; i < xsize; i++) {
384                 if (bit_test(s->keys1, i)) {
385                         if (!bit_test(s->keys2, i))
386                                 bit_set(s->keys2, i);
387                         else
388                                 bit_clear(s->keys1, i);
389                 }
390         }
391
392         bit_ffs(diff, xsize, &f2);
393         if (f2 > 0)
394                 kbd_write(diff, f2, 0, s->vkbd);
395
396         bit_ffs(s->keys1, xsize, &f1);
397         if (f1 > 0) {
398                 kbd_write(s->keys1, f1, 1, s->vkbd);
399                 memset(s->keys1, 0, bitstr_size(xsize));
400         }
401
402         return (0);
403 }
404
405 /*
406  * Translate given keymap and write keyscodes
407  */ 
408
409 static void
410 kbd_write(bitstr_t *m, int32_t fb, int32_t make, int32_t fd)
411 {
412         int32_t i, *b, *eob, n, buf[64];
413
414         b = buf;
415         eob = b + sizeof(buf)/sizeof(buf[0]);
416         i = fb;
417
418         while (i < xsize) {
419                 if (bit_test(m, i)) {
420                         n = kbd_xlate(i, make, b, eob);
421                         if (n == -1) {
422                                 write(fd, buf, (b - buf) * sizeof(buf[0]));
423                                 b = buf;
424                                 continue;
425                         }
426
427                         b += n;
428                 }
429
430                 i ++;
431         }
432
433         if (b != buf)
434                 write(fd, buf, (b - buf) * sizeof(buf[0]));
435 }
436
437 /*
438  * Translate HID code into PS/2 code and put codes into buffer b.
439  * Returns the number of codes put in b. Return -1 if buffer has not
440  * enough space.
441  */
442
443 #undef  PUT
444 #define PUT(c, n, b, eob)       \
445 do {                            \
446         if ((b) >= (eob))       \
447                 return (-1);    \
448         *(b) = (c);             \
449         (b) ++;                 \
450         (n) ++;                 \
451 } while (0)
452
453 static int32_t
454 kbd_xlate(int32_t code, int32_t make, int32_t *b, int32_t const *eob)
455 {
456         int32_t c, n;
457
458         n = 0;
459
460         if (code >= xsize)
461                 return (0); /* HID code is not in the table */
462
463         /* Handle special case - Pause/Break */
464         if (code == 0x48) {
465                 if (!make)
466                         return (0); /* No break code */
467
468 #if 0
469 XXX FIXME
470                 if (ctrl_is_pressed) {
471                         /* Break (Ctrl-Pause) */
472                         PUT(0xe0, n, b, eob);
473                         PUT(0x46, n, b, eob);
474                         PUT(0xe0, n, b, eob);
475                         PUT(0xc6, n, b, eob);
476                 } else {
477                         /* Pause */
478                         PUT(0xe1, n, b, eob);
479                         PUT(0x1d, n, b, eob);
480                         PUT(0x45, n, b, eob);
481                         PUT(0xe1, n, b, eob);
482                         PUT(0x9d, n, b, eob);
483                         PUT(0xc5, n, b, eob);
484                 }
485 #endif
486
487                 return (n);
488         }
489
490         if ((c = x[code]) == -1)
491                 return (0); /* HID code translation is not defined */
492
493         if (make) {
494                 if (c & E0PREFIX)
495                         PUT(0xe0, n, b, eob);
496
497                 PUT((c & CODEMASK), n, b, eob);
498         } else if (!(c & NOBREAK)) {
499                 if (c & E0PREFIX)
500                         PUT(0xe0, n, b, eob);
501
502                 PUT((0x80|(c & CODEMASK)), n, b, eob);
503         }
504
505         return (n);
506 }
507
508 /*
509  * Process status change from vkbd(4)
510  */
511
512 int32_t
513 kbd_status_changed(bthid_session_p s, uint8_t *data, int32_t len)
514 {
515         vkbd_status_t   st;
516         uint8_t         leds, report_id;
517         hid_device_p    hid_device;
518         hid_data_t      d;
519         hid_item_t      h;
520
521         assert(s != NULL);
522         assert(len == sizeof(vkbd_status_t));
523
524         memcpy(&st, data, sizeof(st));
525         leds = 0;
526         report_id = NO_REPORT_ID;
527
528         hid_device = get_hid_device(&s->bdaddr);
529         assert(hid_device != NULL);
530
531         for (d = hid_start_parse(hid_device->desc, 1 << hid_output, -1);
532              hid_get_item(d, &h) > 0; ) {
533                 if (HID_PAGE(h.usage) == HUP_LEDS) {
534                         if (report_id == NO_REPORT_ID)
535                                 report_id = h.report_ID;
536                         else if (h.report_ID != report_id)
537                                 syslog(LOG_WARNING, "Output HID report IDs " \
538                                         "for %s do not match: %d vs. %d. " \
539                                         "Please report",
540                                         bt_ntoa(&s->bdaddr, NULL),
541                                         h.report_ID, report_id);
542                         
543                         switch(HID_USAGE(h.usage)) {
544                         case 0x01: /* Num Lock LED */
545                                 if (st.leds & LED_NUM)
546                                         hid_set_data(&leds, &h, 1);
547                                 break;
548
549                         case 0x02: /* Caps Lock LED */
550                                 if (st.leds & LED_CAP)
551                                         hid_set_data(&leds, &h, 1);
552                                 break;
553
554                         case 0x03: /* Scroll Lock LED */
555                                 if (st.leds & LED_SCR)
556                                         hid_set_data(&leds, &h, 1);
557                                 break;
558
559                         /* XXX add other LEDs ? */
560                         }
561                 }
562         }
563         hid_end_parse(d);
564
565         data[0] = 0xa2; /* DATA output (HID output report) */
566
567         if (report_id != NO_REPORT_ID) {
568                 data[1] = report_id;
569                 data[2] = leds;
570                 len = 3;
571         } else {
572                 data[1] = leds;
573                 len = 2;
574         }
575
576         write(s->intr, data, len);
577
578         return (0);
579 }
580