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