xref: /openbmc/qemu/hw/input/adb-kbd.c (revision ad9e5aa2)
1 /*
2  * QEMU ADB keyboard support
3  *
4  * Copyright (c) 2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "hw/input/adb.h"
27 #include "migration/vmstate.h"
28 #include "qemu/module.h"
29 #include "ui/input.h"
30 #include "hw/input/adb-keys.h"
31 #include "adb-internal.h"
32 #include "trace.h"
33 
34 #define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD)
35 
36 typedef struct KBDState {
37     /*< private >*/
38     ADBDevice parent_obj;
39     /*< public >*/
40 
41     uint8_t data[128];
42     int rptr, wptr, count;
43 } KBDState;
44 
45 #define ADB_KEYBOARD_CLASS(class) \
46     OBJECT_CLASS_CHECK(ADBKeyboardClass, (class), TYPE_ADB_KEYBOARD)
47 #define ADB_KEYBOARD_GET_CLASS(obj) \
48     OBJECT_GET_CLASS(ADBKeyboardClass, (obj), TYPE_ADB_KEYBOARD)
49 
50 typedef struct ADBKeyboardClass {
51     /*< private >*/
52     ADBDeviceClass parent_class;
53     /*< public >*/
54 
55     DeviceRealize parent_realize;
56 } ADBKeyboardClass;
57 
58 /* The adb keyboard doesn't have every key imaginable */
59 #define NO_KEY 0xff
60 
61 int qcode_to_adb_keycode[] = {
62      /* Make sure future additions are automatically set to NO_KEY */
63     [0 ... 0xff]               = NO_KEY,
64 
65     [Q_KEY_CODE_SHIFT]         = ADB_KEY_LEFT_SHIFT,
66     [Q_KEY_CODE_SHIFT_R]       = ADB_KEY_RIGHT_SHIFT,
67     [Q_KEY_CODE_ALT]           = ADB_KEY_LEFT_OPTION,
68     [Q_KEY_CODE_ALT_R]         = ADB_KEY_RIGHT_OPTION,
69     [Q_KEY_CODE_CTRL]          = ADB_KEY_LEFT_CONTROL,
70     [Q_KEY_CODE_CTRL_R]        = ADB_KEY_RIGHT_CONTROL,
71     [Q_KEY_CODE_META_L]        = ADB_KEY_COMMAND,
72     [Q_KEY_CODE_META_R]        = ADB_KEY_COMMAND,
73     [Q_KEY_CODE_SPC]           = ADB_KEY_SPACEBAR,
74 
75     [Q_KEY_CODE_ESC]           = ADB_KEY_ESC,
76     [Q_KEY_CODE_1]             = ADB_KEY_1,
77     [Q_KEY_CODE_2]             = ADB_KEY_2,
78     [Q_KEY_CODE_3]             = ADB_KEY_3,
79     [Q_KEY_CODE_4]             = ADB_KEY_4,
80     [Q_KEY_CODE_5]             = ADB_KEY_5,
81     [Q_KEY_CODE_6]             = ADB_KEY_6,
82     [Q_KEY_CODE_7]             = ADB_KEY_7,
83     [Q_KEY_CODE_8]             = ADB_KEY_8,
84     [Q_KEY_CODE_9]             = ADB_KEY_9,
85     [Q_KEY_CODE_0]             = ADB_KEY_0,
86     [Q_KEY_CODE_MINUS]         = ADB_KEY_MINUS,
87     [Q_KEY_CODE_EQUAL]         = ADB_KEY_EQUAL,
88     [Q_KEY_CODE_BACKSPACE]     = ADB_KEY_DELETE,
89     [Q_KEY_CODE_TAB]           = ADB_KEY_TAB,
90     [Q_KEY_CODE_Q]             = ADB_KEY_Q,
91     [Q_KEY_CODE_W]             = ADB_KEY_W,
92     [Q_KEY_CODE_E]             = ADB_KEY_E,
93     [Q_KEY_CODE_R]             = ADB_KEY_R,
94     [Q_KEY_CODE_T]             = ADB_KEY_T,
95     [Q_KEY_CODE_Y]             = ADB_KEY_Y,
96     [Q_KEY_CODE_U]             = ADB_KEY_U,
97     [Q_KEY_CODE_I]             = ADB_KEY_I,
98     [Q_KEY_CODE_O]             = ADB_KEY_O,
99     [Q_KEY_CODE_P]             = ADB_KEY_P,
100     [Q_KEY_CODE_BRACKET_LEFT]  = ADB_KEY_LEFT_BRACKET,
101     [Q_KEY_CODE_BRACKET_RIGHT] = ADB_KEY_RIGHT_BRACKET,
102     [Q_KEY_CODE_RET]           = ADB_KEY_RETURN,
103     [Q_KEY_CODE_A]             = ADB_KEY_A,
104     [Q_KEY_CODE_S]             = ADB_KEY_S,
105     [Q_KEY_CODE_D]             = ADB_KEY_D,
106     [Q_KEY_CODE_F]             = ADB_KEY_F,
107     [Q_KEY_CODE_G]             = ADB_KEY_G,
108     [Q_KEY_CODE_H]             = ADB_KEY_H,
109     [Q_KEY_CODE_J]             = ADB_KEY_J,
110     [Q_KEY_CODE_K]             = ADB_KEY_K,
111     [Q_KEY_CODE_L]             = ADB_KEY_L,
112     [Q_KEY_CODE_SEMICOLON]     = ADB_KEY_SEMICOLON,
113     [Q_KEY_CODE_APOSTROPHE]    = ADB_KEY_APOSTROPHE,
114     [Q_KEY_CODE_GRAVE_ACCENT]  = ADB_KEY_GRAVE_ACCENT,
115     [Q_KEY_CODE_BACKSLASH]     = ADB_KEY_BACKSLASH,
116     [Q_KEY_CODE_Z]             = ADB_KEY_Z,
117     [Q_KEY_CODE_X]             = ADB_KEY_X,
118     [Q_KEY_CODE_C]             = ADB_KEY_C,
119     [Q_KEY_CODE_V]             = ADB_KEY_V,
120     [Q_KEY_CODE_B]             = ADB_KEY_B,
121     [Q_KEY_CODE_N]             = ADB_KEY_N,
122     [Q_KEY_CODE_M]             = ADB_KEY_M,
123     [Q_KEY_CODE_COMMA]         = ADB_KEY_COMMA,
124     [Q_KEY_CODE_DOT]           = ADB_KEY_PERIOD,
125     [Q_KEY_CODE_SLASH]         = ADB_KEY_FORWARD_SLASH,
126     [Q_KEY_CODE_ASTERISK]      = ADB_KEY_KP_MULTIPLY,
127     [Q_KEY_CODE_CAPS_LOCK]     = ADB_KEY_CAPS_LOCK,
128 
129     [Q_KEY_CODE_F1]            = ADB_KEY_F1,
130     [Q_KEY_CODE_F2]            = ADB_KEY_F2,
131     [Q_KEY_CODE_F3]            = ADB_KEY_F3,
132     [Q_KEY_CODE_F4]            = ADB_KEY_F4,
133     [Q_KEY_CODE_F5]            = ADB_KEY_F5,
134     [Q_KEY_CODE_F6]            = ADB_KEY_F6,
135     [Q_KEY_CODE_F7]            = ADB_KEY_F7,
136     [Q_KEY_CODE_F8]            = ADB_KEY_F8,
137     [Q_KEY_CODE_F9]            = ADB_KEY_F9,
138     [Q_KEY_CODE_F10]           = ADB_KEY_F10,
139     [Q_KEY_CODE_F11]           = ADB_KEY_F11,
140     [Q_KEY_CODE_F12]           = ADB_KEY_F12,
141     [Q_KEY_CODE_PRINT]         = ADB_KEY_F13,
142     [Q_KEY_CODE_SYSRQ]         = ADB_KEY_F13,
143     [Q_KEY_CODE_SCROLL_LOCK]   = ADB_KEY_F14,
144     [Q_KEY_CODE_PAUSE]         = ADB_KEY_F15,
145 
146     [Q_KEY_CODE_NUM_LOCK]      = ADB_KEY_KP_CLEAR,
147     [Q_KEY_CODE_KP_EQUALS]     = ADB_KEY_KP_EQUAL,
148     [Q_KEY_CODE_KP_DIVIDE]     = ADB_KEY_KP_DIVIDE,
149     [Q_KEY_CODE_KP_MULTIPLY]   = ADB_KEY_KP_MULTIPLY,
150     [Q_KEY_CODE_KP_SUBTRACT]   = ADB_KEY_KP_SUBTRACT,
151     [Q_KEY_CODE_KP_ADD]        = ADB_KEY_KP_PLUS,
152     [Q_KEY_CODE_KP_ENTER]      = ADB_KEY_KP_ENTER,
153     [Q_KEY_CODE_KP_DECIMAL]    = ADB_KEY_KP_PERIOD,
154     [Q_KEY_CODE_KP_0]          = ADB_KEY_KP_0,
155     [Q_KEY_CODE_KP_1]          = ADB_KEY_KP_1,
156     [Q_KEY_CODE_KP_2]          = ADB_KEY_KP_2,
157     [Q_KEY_CODE_KP_3]          = ADB_KEY_KP_3,
158     [Q_KEY_CODE_KP_4]          = ADB_KEY_KP_4,
159     [Q_KEY_CODE_KP_5]          = ADB_KEY_KP_5,
160     [Q_KEY_CODE_KP_6]          = ADB_KEY_KP_6,
161     [Q_KEY_CODE_KP_7]          = ADB_KEY_KP_7,
162     [Q_KEY_CODE_KP_8]          = ADB_KEY_KP_8,
163     [Q_KEY_CODE_KP_9]          = ADB_KEY_KP_9,
164 
165     [Q_KEY_CODE_UP]            = ADB_KEY_UP,
166     [Q_KEY_CODE_DOWN]          = ADB_KEY_DOWN,
167     [Q_KEY_CODE_LEFT]          = ADB_KEY_LEFT,
168     [Q_KEY_CODE_RIGHT]         = ADB_KEY_RIGHT,
169 
170     [Q_KEY_CODE_HELP]          = ADB_KEY_HELP,
171     [Q_KEY_CODE_INSERT]        = ADB_KEY_HELP,
172     [Q_KEY_CODE_DELETE]        = ADB_KEY_FORWARD_DELETE,
173     [Q_KEY_CODE_HOME]          = ADB_KEY_HOME,
174     [Q_KEY_CODE_END]           = ADB_KEY_END,
175     [Q_KEY_CODE_PGUP]          = ADB_KEY_PAGE_UP,
176     [Q_KEY_CODE_PGDN]          = ADB_KEY_PAGE_DOWN,
177 
178     [Q_KEY_CODE_POWER]         = ADB_KEY_POWER
179 };
180 
181 static void adb_kbd_put_keycode(void *opaque, int keycode)
182 {
183     KBDState *s = opaque;
184 
185     if (s->count < sizeof(s->data)) {
186         s->data[s->wptr] = keycode;
187         if (++s->wptr == sizeof(s->data)) {
188             s->wptr = 0;
189         }
190         s->count++;
191     }
192 }
193 
194 static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
195 {
196     KBDState *s = ADB_KEYBOARD(d);
197     int keycode;
198 
199     if (s->count == 0) {
200         return 0;
201     }
202     keycode = s->data[s->rptr];
203     s->rptr++;
204     if (s->rptr == sizeof(s->data)) {
205         s->rptr = 0;
206     }
207     s->count--;
208     /*
209      * The power key is the only two byte value key, so it is a special case.
210      * Since 0x7f is not a used keycode for ADB we overload it to indicate the
211      * power button when we're storing keycodes in our internal buffer, and
212      * expand it out to two bytes when we send to the guest.
213      */
214     if (keycode == 0x7f) {
215         obuf[0] = 0x7f;
216         obuf[1] = 0x7f;
217     } else {
218         obuf[0] = keycode;
219         /* NOTE: the power key key-up is the two byte sequence 0xff 0xff;
220          * otherwise we could in theory send a second keycode in the second
221          * byte, but choose not to bother.
222          */
223         obuf[1] = 0xff;
224     }
225 
226     return 2;
227 }
228 
229 static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
230                            const uint8_t *buf, int len)
231 {
232     KBDState *s = ADB_KEYBOARD(d);
233     int cmd, reg, olen;
234 
235     if ((buf[0] & 0x0f) == ADB_FLUSH) {
236         /* flush keyboard fifo */
237         s->wptr = s->rptr = s->count = 0;
238         return 0;
239     }
240 
241     cmd = buf[0] & 0xc;
242     reg = buf[0] & 0x3;
243     olen = 0;
244     switch (cmd) {
245     case ADB_WRITEREG:
246         trace_adb_device_kbd_writereg(reg, buf[1]);
247         switch (reg) {
248         case 2:
249             /* LED status */
250             break;
251         case 3:
252             switch (buf[2]) {
253             case ADB_CMD_SELF_TEST:
254                 break;
255             case ADB_CMD_CHANGE_ID:
256             case ADB_CMD_CHANGE_ID_AND_ACT:
257             case ADB_CMD_CHANGE_ID_AND_ENABLE:
258                 d->devaddr = buf[1] & 0xf;
259                 trace_adb_device_kbd_request_change_addr(d->devaddr);
260                 break;
261             default:
262                 d->devaddr = buf[1] & 0xf;
263                 /*
264                  * we support handlers:
265                  * 1: Apple Standard Keyboard
266                  * 2: Apple Extended Keyboard (LShift = RShift)
267                  * 3: Apple Extended Keyboard (LShift != RShift)
268                  */
269                 if (buf[2] == 1 || buf[2] == 2 || buf[2] == 3) {
270                     d->handler = buf[2];
271                 }
272 
273                 trace_adb_device_kbd_request_change_addr_and_handler(
274                     d->devaddr, d->handler);
275                 break;
276             }
277         }
278         break;
279     case ADB_READREG:
280         switch (reg) {
281         case 0:
282             olen = adb_kbd_poll(d, obuf);
283             break;
284         case 1:
285             break;
286         case 2:
287             obuf[0] = 0x00; /* XXX: check this */
288             obuf[1] = 0x07; /* led status */
289             olen = 2;
290             break;
291         case 3:
292             obuf[0] = d->devaddr;
293             obuf[1] = d->handler;
294             olen = 2;
295             break;
296         }
297         trace_adb_device_kbd_readreg(reg, obuf[0], obuf[1]);
298         break;
299     }
300     return olen;
301 }
302 
303 static bool adb_kbd_has_data(ADBDevice *d)
304 {
305     KBDState *s = ADB_KEYBOARD(d);
306 
307     return s->count > 0;
308 }
309 
310 /* This is where keyboard events enter this file */
311 static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
312                                InputEvent *evt)
313 {
314     KBDState *s = (KBDState *)dev;
315     int qcode, keycode;
316 
317     qcode = qemu_input_key_value_to_qcode(evt->u.key.data->key);
318     if (qcode >= ARRAY_SIZE(qcode_to_adb_keycode)) {
319         return;
320     }
321     /* FIXME: take handler into account when translating qcode */
322     keycode = qcode_to_adb_keycode[qcode];
323     if (keycode == NO_KEY) {  /* We don't want to send this to the guest */
324         trace_adb_device_kbd_no_key();
325         return;
326     }
327     if (evt->u.key.data->down == false) { /* if key release event */
328         keycode = keycode | 0x80;   /* create keyboard break code */
329     }
330 
331     adb_kbd_put_keycode(s, keycode);
332 }
333 
334 static const VMStateDescription vmstate_adb_kbd = {
335     .name = "adb_kbd",
336     .version_id = 2,
337     .minimum_version_id = 2,
338     .fields = (VMStateField[]) {
339         VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice),
340         VMSTATE_BUFFER(data, KBDState),
341         VMSTATE_INT32(rptr, KBDState),
342         VMSTATE_INT32(wptr, KBDState),
343         VMSTATE_INT32(count, KBDState),
344         VMSTATE_END_OF_LIST()
345     }
346 };
347 
348 static void adb_kbd_reset(DeviceState *dev)
349 {
350     ADBDevice *d = ADB_DEVICE(dev);
351     KBDState *s = ADB_KEYBOARD(dev);
352 
353     d->handler = 1;
354     d->devaddr = ADB_DEVID_KEYBOARD;
355     memset(s->data, 0, sizeof(s->data));
356     s->rptr = 0;
357     s->wptr = 0;
358     s->count = 0;
359 }
360 
361 static QemuInputHandler adb_keyboard_handler = {
362     .name  = "QEMU ADB Keyboard",
363     .mask  = INPUT_EVENT_MASK_KEY,
364     .event = adb_keyboard_event,
365 };
366 
367 static void adb_kbd_realizefn(DeviceState *dev, Error **errp)
368 {
369     ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev);
370     akc->parent_realize(dev, errp);
371     qemu_input_handler_register(dev, &adb_keyboard_handler);
372 }
373 
374 static void adb_kbd_initfn(Object *obj)
375 {
376     ADBDevice *d = ADB_DEVICE(obj);
377 
378     d->devaddr = ADB_DEVID_KEYBOARD;
379 }
380 
381 static void adb_kbd_class_init(ObjectClass *oc, void *data)
382 {
383     DeviceClass *dc = DEVICE_CLASS(oc);
384     ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
385     ADBKeyboardClass *akc = ADB_KEYBOARD_CLASS(oc);
386 
387     device_class_set_parent_realize(dc, adb_kbd_realizefn,
388                                     &akc->parent_realize);
389     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
390 
391     adc->devreq = adb_kbd_request;
392     adc->devhasdata = adb_kbd_has_data;
393     dc->reset = adb_kbd_reset;
394     dc->vmsd = &vmstate_adb_kbd;
395 }
396 
397 static const TypeInfo adb_kbd_type_info = {
398     .name = TYPE_ADB_KEYBOARD,
399     .parent = TYPE_ADB_DEVICE,
400     .instance_size = sizeof(KBDState),
401     .instance_init = adb_kbd_initfn,
402     .class_init = adb_kbd_class_init,
403     .class_size = sizeof(ADBKeyboardClass),
404 };
405 
406 static void adb_kbd_register_types(void)
407 {
408     type_register_static(&adb_kbd_type_info);
409 }
410 
411 type_init(adb_kbd_register_types)
412