bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU ADB support |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 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 | */ |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 26 | #include "hw/input/adb.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 27 | #include "ui/console.h" |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 28 | #include "include/hw/input/adb-keys.h" |
| 29 | #include "ui/input.h" |
| 30 | #include "sysemu/sysemu.h" |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 31 | |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 32 | /* debug ADB */ |
| 33 | //#define DEBUG_ADB |
| 34 | |
| 35 | #ifdef DEBUG_ADB |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 36 | #define ADB_DPRINTF(fmt, ...) \ |
| 37 | do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0) |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 38 | #else |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 39 | #define ADB_DPRINTF(fmt, ...) |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 42 | /* ADB commands */ |
| 43 | #define ADB_BUSRESET 0x00 |
| 44 | #define ADB_FLUSH 0x01 |
| 45 | #define ADB_WRITEREG 0x08 |
| 46 | #define ADB_READREG 0x0c |
| 47 | |
| 48 | /* ADB device commands */ |
| 49 | #define ADB_CMD_SELF_TEST 0xff |
| 50 | #define ADB_CMD_CHANGE_ID 0xfe |
| 51 | #define ADB_CMD_CHANGE_ID_AND_ACT 0xfd |
| 52 | #define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00 |
| 53 | |
| 54 | /* ADB default device IDs (upper 4 bits of ADB command byte) */ |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 55 | #define ADB_DEVID_DONGLE 1 |
| 56 | #define ADB_DEVID_KEYBOARD 2 |
| 57 | #define ADB_DEVID_MOUSE 3 |
| 58 | #define ADB_DEVID_TABLET 4 |
| 59 | #define ADB_DEVID_MODEM 5 |
| 60 | #define ADB_DEVID_MISC 7 |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 61 | |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 62 | /* error codes */ |
| 63 | #define ADB_RET_NOTPRESENT (-2) |
| 64 | |
John Arbuckle | f366e72 | 2016-08-17 22:27:50 -0400 | [diff] [blame] | 65 | /* The adb keyboard doesn't have every key imaginable */ |
| 66 | #define NO_KEY 0xff |
| 67 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 68 | static void adb_device_reset(ADBDevice *d) |
| 69 | { |
| 70 | qdev_reset_all(DEVICE(d)); |
| 71 | } |
| 72 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 73 | int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 74 | { |
| 75 | ADBDevice *d; |
| 76 | int devaddr, cmd, i; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 77 | |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 78 | cmd = buf[0] & 0xf; |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 79 | if (cmd == ADB_BUSRESET) { |
| 80 | for(i = 0; i < s->nb_devices; i++) { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 81 | d = s->devices[i]; |
| 82 | adb_device_reset(d); |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 83 | } |
| 84 | return 0; |
| 85 | } |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 86 | devaddr = buf[0] >> 4; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 87 | for(i = 0; i < s->nb_devices; i++) { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 88 | d = s->devices[i]; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 89 | if (d->devaddr == devaddr) { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 90 | ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d); |
| 91 | return adc->devreq(d, obuf, buf, len); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 94 | return ADB_RET_NOTPRESENT; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 95 | } |
| 96 | |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 97 | /* XXX: move that to cuda ? */ |
Hervé Poussineau | 216c906 | 2016-02-07 21:34:08 +0100 | [diff] [blame] | 98 | int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask) |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 99 | { |
| 100 | ADBDevice *d; |
| 101 | int olen, i; |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 102 | uint8_t buf[1]; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 103 | |
| 104 | olen = 0; |
| 105 | for(i = 0; i < s->nb_devices; i++) { |
| 106 | if (s->poll_index >= s->nb_devices) |
| 107 | s->poll_index = 0; |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 108 | d = s->devices[s->poll_index]; |
Hervé Poussineau | 216c906 | 2016-02-07 21:34:08 +0100 | [diff] [blame] | 109 | if ((1 << d->devaddr) & poll_mask) { |
| 110 | buf[0] = ADB_READREG | (d->devaddr << 4); |
| 111 | olen = adb_request(s, obuf + 1, buf, 1); |
| 112 | /* if there is data, we poll again the same device */ |
| 113 | if (olen > 0) { |
| 114 | obuf[0] = buf[0]; |
| 115 | olen++; |
| 116 | break; |
| 117 | } |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 118 | } |
| 119 | s->poll_index++; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 120 | } |
| 121 | return olen; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Andreas Färber | 84ede32 | 2013-01-23 23:04:03 +0000 | [diff] [blame] | 124 | static const TypeInfo adb_bus_type_info = { |
| 125 | .name = TYPE_ADB_BUS, |
| 126 | .parent = TYPE_BUS, |
| 127 | .instance_size = sizeof(ADBBusState), |
| 128 | }; |
| 129 | |
Mark Cave-Ayland | e5dffaa | 2015-02-09 22:40:45 +0000 | [diff] [blame] | 130 | static const VMStateDescription vmstate_adb_device = { |
| 131 | .name = "adb_device", |
| 132 | .version_id = 0, |
| 133 | .minimum_version_id = 0, |
| 134 | .fields = (VMStateField[]) { |
| 135 | VMSTATE_INT32(devaddr, ADBDevice), |
| 136 | VMSTATE_INT32(handler, ADBDevice), |
| 137 | VMSTATE_END_OF_LIST() |
| 138 | } |
| 139 | }; |
| 140 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 141 | static void adb_device_realizefn(DeviceState *dev, Error **errp) |
| 142 | { |
| 143 | ADBDevice *d = ADB_DEVICE(dev); |
| 144 | ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev)); |
| 145 | |
| 146 | if (bus->nb_devices >= MAX_ADB_DEVICES) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | bus->devices[bus->nb_devices++] = d; |
| 151 | } |
| 152 | |
| 153 | static void adb_device_class_init(ObjectClass *oc, void *data) |
| 154 | { |
| 155 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 156 | |
| 157 | dc->realize = adb_device_realizefn; |
| 158 | dc->bus_type = TYPE_ADB_BUS; |
| 159 | } |
| 160 | |
| 161 | static const TypeInfo adb_device_type_info = { |
| 162 | .name = TYPE_ADB_DEVICE, |
| 163 | .parent = TYPE_DEVICE, |
| 164 | .instance_size = sizeof(ADBDevice), |
| 165 | .abstract = true, |
| 166 | .class_init = adb_device_class_init, |
| 167 | }; |
| 168 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 169 | /***************************************************************/ |
| 170 | /* Keyboard ADB device */ |
| 171 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 172 | #define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD) |
| 173 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 174 | typedef struct KBDState { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 175 | /*< private >*/ |
| 176 | ADBDevice parent_obj; |
| 177 | /*< public >*/ |
| 178 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 179 | uint8_t data[128]; |
| 180 | int rptr, wptr, count; |
| 181 | } KBDState; |
| 182 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 183 | #define ADB_KEYBOARD_CLASS(class) \ |
| 184 | OBJECT_CLASS_CHECK(ADBKeyboardClass, (class), TYPE_ADB_KEYBOARD) |
| 185 | #define ADB_KEYBOARD_GET_CLASS(obj) \ |
| 186 | OBJECT_GET_CLASS(ADBKeyboardClass, (obj), TYPE_ADB_KEYBOARD) |
| 187 | |
| 188 | typedef struct ADBKeyboardClass { |
| 189 | /*< private >*/ |
| 190 | ADBDeviceClass parent_class; |
| 191 | /*< public >*/ |
| 192 | |
| 193 | DeviceRealize parent_realize; |
| 194 | } ADBKeyboardClass; |
| 195 | |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 196 | int qcode_to_adb_keycode[] = { |
John Arbuckle | f366e72 | 2016-08-17 22:27:50 -0400 | [diff] [blame] | 197 | /* Make sure future additions are automatically set to NO_KEY */ |
| 198 | [0 ... 0xff] = NO_KEY, |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 199 | |
| 200 | [Q_KEY_CODE_SHIFT] = ADB_KEY_LEFT_SHIFT, |
| 201 | [Q_KEY_CODE_SHIFT_R] = ADB_KEY_RIGHT_SHIFT, |
| 202 | [Q_KEY_CODE_ALT] = ADB_KEY_LEFT_OPTION, |
| 203 | [Q_KEY_CODE_ALT_R] = ADB_KEY_RIGHT_OPTION, |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 204 | [Q_KEY_CODE_CTRL] = ADB_KEY_LEFT_CONTROL, |
| 205 | [Q_KEY_CODE_CTRL_R] = ADB_KEY_RIGHT_CONTROL, |
| 206 | [Q_KEY_CODE_META_L] = ADB_KEY_COMMAND, |
| 207 | [Q_KEY_CODE_META_R] = ADB_KEY_COMMAND, |
| 208 | [Q_KEY_CODE_SPC] = ADB_KEY_SPACEBAR, |
| 209 | |
| 210 | [Q_KEY_CODE_ESC] = ADB_KEY_ESC, |
| 211 | [Q_KEY_CODE_1] = ADB_KEY_1, |
| 212 | [Q_KEY_CODE_2] = ADB_KEY_2, |
| 213 | [Q_KEY_CODE_3] = ADB_KEY_3, |
| 214 | [Q_KEY_CODE_4] = ADB_KEY_4, |
| 215 | [Q_KEY_CODE_5] = ADB_KEY_5, |
| 216 | [Q_KEY_CODE_6] = ADB_KEY_6, |
| 217 | [Q_KEY_CODE_7] = ADB_KEY_7, |
| 218 | [Q_KEY_CODE_8] = ADB_KEY_8, |
| 219 | [Q_KEY_CODE_9] = ADB_KEY_9, |
| 220 | [Q_KEY_CODE_0] = ADB_KEY_0, |
| 221 | [Q_KEY_CODE_MINUS] = ADB_KEY_MINUS, |
| 222 | [Q_KEY_CODE_EQUAL] = ADB_KEY_EQUAL, |
| 223 | [Q_KEY_CODE_BACKSPACE] = ADB_KEY_DELETE, |
| 224 | [Q_KEY_CODE_TAB] = ADB_KEY_TAB, |
| 225 | [Q_KEY_CODE_Q] = ADB_KEY_Q, |
| 226 | [Q_KEY_CODE_W] = ADB_KEY_W, |
| 227 | [Q_KEY_CODE_E] = ADB_KEY_E, |
| 228 | [Q_KEY_CODE_R] = ADB_KEY_R, |
| 229 | [Q_KEY_CODE_T] = ADB_KEY_T, |
| 230 | [Q_KEY_CODE_Y] = ADB_KEY_Y, |
| 231 | [Q_KEY_CODE_U] = ADB_KEY_U, |
| 232 | [Q_KEY_CODE_I] = ADB_KEY_I, |
| 233 | [Q_KEY_CODE_O] = ADB_KEY_O, |
| 234 | [Q_KEY_CODE_P] = ADB_KEY_P, |
| 235 | [Q_KEY_CODE_BRACKET_LEFT] = ADB_KEY_LEFT_BRACKET, |
| 236 | [Q_KEY_CODE_BRACKET_RIGHT] = ADB_KEY_RIGHT_BRACKET, |
| 237 | [Q_KEY_CODE_RET] = ADB_KEY_RETURN, |
| 238 | [Q_KEY_CODE_A] = ADB_KEY_A, |
| 239 | [Q_KEY_CODE_S] = ADB_KEY_S, |
| 240 | [Q_KEY_CODE_D] = ADB_KEY_D, |
| 241 | [Q_KEY_CODE_F] = ADB_KEY_F, |
| 242 | [Q_KEY_CODE_G] = ADB_KEY_G, |
| 243 | [Q_KEY_CODE_H] = ADB_KEY_H, |
| 244 | [Q_KEY_CODE_J] = ADB_KEY_J, |
| 245 | [Q_KEY_CODE_K] = ADB_KEY_K, |
| 246 | [Q_KEY_CODE_L] = ADB_KEY_L, |
| 247 | [Q_KEY_CODE_SEMICOLON] = ADB_KEY_SEMICOLON, |
| 248 | [Q_KEY_CODE_APOSTROPHE] = ADB_KEY_APOSTROPHE, |
| 249 | [Q_KEY_CODE_GRAVE_ACCENT] = ADB_KEY_GRAVE_ACCENT, |
| 250 | [Q_KEY_CODE_BACKSLASH] = ADB_KEY_BACKSLASH, |
| 251 | [Q_KEY_CODE_Z] = ADB_KEY_Z, |
| 252 | [Q_KEY_CODE_X] = ADB_KEY_X, |
| 253 | [Q_KEY_CODE_C] = ADB_KEY_C, |
| 254 | [Q_KEY_CODE_V] = ADB_KEY_V, |
| 255 | [Q_KEY_CODE_B] = ADB_KEY_B, |
| 256 | [Q_KEY_CODE_N] = ADB_KEY_N, |
| 257 | [Q_KEY_CODE_M] = ADB_KEY_M, |
| 258 | [Q_KEY_CODE_COMMA] = ADB_KEY_COMMA, |
| 259 | [Q_KEY_CODE_DOT] = ADB_KEY_PERIOD, |
| 260 | [Q_KEY_CODE_SLASH] = ADB_KEY_FORWARD_SLASH, |
| 261 | [Q_KEY_CODE_ASTERISK] = ADB_KEY_KP_MULTIPLY, |
| 262 | [Q_KEY_CODE_CAPS_LOCK] = ADB_KEY_CAPS_LOCK, |
| 263 | |
| 264 | [Q_KEY_CODE_F1] = ADB_KEY_F1, |
| 265 | [Q_KEY_CODE_F2] = ADB_KEY_F2, |
| 266 | [Q_KEY_CODE_F3] = ADB_KEY_F3, |
| 267 | [Q_KEY_CODE_F4] = ADB_KEY_F4, |
| 268 | [Q_KEY_CODE_F5] = ADB_KEY_F5, |
| 269 | [Q_KEY_CODE_F6] = ADB_KEY_F6, |
| 270 | [Q_KEY_CODE_F7] = ADB_KEY_F7, |
| 271 | [Q_KEY_CODE_F8] = ADB_KEY_F8, |
| 272 | [Q_KEY_CODE_F9] = ADB_KEY_F9, |
| 273 | [Q_KEY_CODE_F10] = ADB_KEY_F10, |
| 274 | [Q_KEY_CODE_F11] = ADB_KEY_F11, |
| 275 | [Q_KEY_CODE_F12] = ADB_KEY_F12, |
John Arbuckle | 25c01db | 2016-08-17 22:27:49 -0400 | [diff] [blame] | 276 | [Q_KEY_CODE_PRINT] = ADB_KEY_F13, |
| 277 | [Q_KEY_CODE_SYSRQ] = ADB_KEY_F13, |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 278 | [Q_KEY_CODE_SCROLL_LOCK] = ADB_KEY_F14, |
John Arbuckle | 25c01db | 2016-08-17 22:27:49 -0400 | [diff] [blame] | 279 | [Q_KEY_CODE_PAUSE] = ADB_KEY_F15, |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 280 | |
| 281 | [Q_KEY_CODE_NUM_LOCK] = ADB_KEY_KP_CLEAR, |
John Arbuckle | 25c01db | 2016-08-17 22:27:49 -0400 | [diff] [blame] | 282 | [Q_KEY_CODE_KP_EQUALS] = ADB_KEY_KP_EQUAL, |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 283 | [Q_KEY_CODE_KP_DIVIDE] = ADB_KEY_KP_DIVIDE, |
| 284 | [Q_KEY_CODE_KP_MULTIPLY] = ADB_KEY_KP_MULTIPLY, |
| 285 | [Q_KEY_CODE_KP_SUBTRACT] = ADB_KEY_KP_SUBTRACT, |
| 286 | [Q_KEY_CODE_KP_ADD] = ADB_KEY_KP_PLUS, |
| 287 | [Q_KEY_CODE_KP_ENTER] = ADB_KEY_KP_ENTER, |
| 288 | [Q_KEY_CODE_KP_DECIMAL] = ADB_KEY_KP_PERIOD, |
| 289 | [Q_KEY_CODE_KP_0] = ADB_KEY_KP_0, |
| 290 | [Q_KEY_CODE_KP_1] = ADB_KEY_KP_1, |
| 291 | [Q_KEY_CODE_KP_2] = ADB_KEY_KP_2, |
| 292 | [Q_KEY_CODE_KP_3] = ADB_KEY_KP_3, |
| 293 | [Q_KEY_CODE_KP_4] = ADB_KEY_KP_4, |
| 294 | [Q_KEY_CODE_KP_5] = ADB_KEY_KP_5, |
| 295 | [Q_KEY_CODE_KP_6] = ADB_KEY_KP_6, |
| 296 | [Q_KEY_CODE_KP_7] = ADB_KEY_KP_7, |
| 297 | [Q_KEY_CODE_KP_8] = ADB_KEY_KP_8, |
| 298 | [Q_KEY_CODE_KP_9] = ADB_KEY_KP_9, |
| 299 | |
| 300 | [Q_KEY_CODE_UP] = ADB_KEY_UP, |
| 301 | [Q_KEY_CODE_DOWN] = ADB_KEY_DOWN, |
| 302 | [Q_KEY_CODE_LEFT] = ADB_KEY_LEFT, |
| 303 | [Q_KEY_CODE_RIGHT] = ADB_KEY_RIGHT, |
| 304 | |
John Arbuckle | 25c01db | 2016-08-17 22:27:49 -0400 | [diff] [blame] | 305 | [Q_KEY_CODE_HELP] = ADB_KEY_HELP, |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 306 | [Q_KEY_CODE_INSERT] = ADB_KEY_HELP, |
| 307 | [Q_KEY_CODE_DELETE] = ADB_KEY_FORWARD_DELETE, |
| 308 | [Q_KEY_CODE_HOME] = ADB_KEY_HOME, |
| 309 | [Q_KEY_CODE_END] = ADB_KEY_END, |
| 310 | [Q_KEY_CODE_PGUP] = ADB_KEY_PAGE_UP, |
| 311 | [Q_KEY_CODE_PGDN] = ADB_KEY_PAGE_DOWN, |
| 312 | |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 313 | [Q_KEY_CODE_POWER] = ADB_KEY_POWER |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | static void adb_kbd_put_keycode(void *opaque, int keycode) |
| 317 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 318 | KBDState *s = opaque; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 319 | |
| 320 | if (s->count < sizeof(s->data)) { |
| 321 | s->data[s->wptr] = keycode; |
| 322 | if (++s->wptr == sizeof(s->data)) |
| 323 | s->wptr = 0; |
| 324 | s->count++; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 328 | static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 329 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 330 | KBDState *s = ADB_KEYBOARD(d); |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 331 | int keycode; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 332 | int olen; |
| 333 | |
| 334 | olen = 0; |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 335 | if (s->count == 0) { |
| 336 | return 0; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 337 | } |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 338 | keycode = s->data[s->rptr]; |
| 339 | s->rptr++; |
| 340 | if (s->rptr == sizeof(s->data)) { |
| 341 | s->rptr = 0; |
| 342 | } |
| 343 | s->count--; |
| 344 | /* |
| 345 | * The power key is the only two byte value key, so it is a special case. |
| 346 | * Since 0x7f is not a used keycode for ADB we overload it to indicate the |
| 347 | * power button when we're storing keycodes in our internal buffer, and |
| 348 | * expand it out to two bytes when we send to the guest. |
| 349 | */ |
| 350 | if (keycode == 0x7f) { |
| 351 | obuf[0] = 0x7f; |
| 352 | obuf[1] = 0x7f; |
| 353 | olen = 2; |
| 354 | } else { |
| 355 | obuf[0] = keycode; |
| 356 | /* NOTE: the power key key-up is the two byte sequence 0xff 0xff; |
| 357 | * otherwise we could in theory send a second keycode in the second |
| 358 | * byte, but choose not to bother. |
| 359 | */ |
| 360 | obuf[1] = 0xff; |
| 361 | olen = 2; |
| 362 | } |
| 363 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 364 | return olen; |
| 365 | } |
| 366 | |
| 367 | static int adb_kbd_request(ADBDevice *d, uint8_t *obuf, |
| 368 | const uint8_t *buf, int len) |
| 369 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 370 | KBDState *s = ADB_KEYBOARD(d); |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 371 | int cmd, reg, olen; |
| 372 | |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 373 | if ((buf[0] & 0x0f) == ADB_FLUSH) { |
| 374 | /* flush keyboard fifo */ |
| 375 | s->wptr = s->rptr = s->count = 0; |
| 376 | return 0; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 377 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 378 | |
| 379 | cmd = buf[0] & 0xc; |
| 380 | reg = buf[0] & 0x3; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 381 | olen = 0; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 382 | switch(cmd) { |
| 383 | case ADB_WRITEREG: |
| 384 | switch(reg) { |
| 385 | case 2: |
| 386 | /* LED status */ |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 387 | break; |
| 388 | case 3: |
| 389 | switch(buf[2]) { |
| 390 | case ADB_CMD_SELF_TEST: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 391 | break; |
| 392 | case ADB_CMD_CHANGE_ID: |
| 393 | case ADB_CMD_CHANGE_ID_AND_ACT: |
| 394 | case ADB_CMD_CHANGE_ID_AND_ENABLE: |
| 395 | d->devaddr = buf[1] & 0xf; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 396 | break; |
| 397 | default: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 398 | d->devaddr = buf[1] & 0xf; |
Hervé Poussineau | a37eb9f | 2016-10-25 09:01:01 +0200 | [diff] [blame] | 399 | /* we support handlers: |
| 400 | * 1: Apple Standard Keyboard |
| 401 | * 2: Apple Extended Keyboard (LShift = RShift) |
| 402 | * 3: Apple Extended Keyboard (LShift != RShift) |
| 403 | */ |
| 404 | if (buf[2] == 1 || buf[2] == 2 || buf[2] == 3) { |
| 405 | d->handler = buf[2]; |
| 406 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | } |
| 410 | break; |
| 411 | case ADB_READREG: |
| 412 | switch(reg) { |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 413 | case 0: |
| 414 | olen = adb_kbd_poll(d, obuf); |
| 415 | break; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 416 | case 1: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 417 | break; |
| 418 | case 2: |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 419 | obuf[0] = 0x00; /* XXX: check this */ |
| 420 | obuf[1] = 0x07; /* led status */ |
| 421 | olen = 2; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 422 | break; |
| 423 | case 3: |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 424 | obuf[0] = d->handler; |
| 425 | obuf[1] = d->devaddr; |
| 426 | olen = 2; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | break; |
| 430 | } |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 431 | return olen; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 432 | } |
| 433 | |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 434 | /* This is where keyboard events enter this file */ |
| 435 | static void adb_keyboard_event(DeviceState *dev, QemuConsole *src, |
| 436 | InputEvent *evt) |
| 437 | { |
| 438 | KBDState *s = (KBDState *)dev; |
| 439 | int qcode, keycode; |
| 440 | |
| 441 | qcode = qemu_input_key_value_to_qcode(evt->u.key.data->key); |
| 442 | if (qcode >= ARRAY_SIZE(qcode_to_adb_keycode)) { |
| 443 | return; |
| 444 | } |
Hervé Poussineau | a37eb9f | 2016-10-25 09:01:01 +0200 | [diff] [blame] | 445 | /* FIXME: take handler into account when translating qcode */ |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 446 | keycode = qcode_to_adb_keycode[qcode]; |
John Arbuckle | f366e72 | 2016-08-17 22:27:50 -0400 | [diff] [blame] | 447 | if (keycode == NO_KEY) { /* We don't want to send this to the guest */ |
| 448 | ADB_DPRINTF("Ignoring NO_KEY\n"); |
| 449 | return; |
| 450 | } |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 451 | if (evt->u.key.data->down == false) { /* if key release event */ |
| 452 | keycode = keycode | 0x80; /* create keyboard break code */ |
| 453 | } |
| 454 | |
| 455 | adb_kbd_put_keycode(s, keycode); |
| 456 | } |
| 457 | |
Juan Quintela | 1f1f060 | 2010-12-01 21:54:04 +0100 | [diff] [blame] | 458 | static const VMStateDescription vmstate_adb_kbd = { |
| 459 | .name = "adb_kbd", |
Mark Cave-Ayland | e5dffaa | 2015-02-09 22:40:45 +0000 | [diff] [blame] | 460 | .version_id = 2, |
| 461 | .minimum_version_id = 2, |
Juan Quintela | 35d0845 | 2014-04-16 16:01:33 +0200 | [diff] [blame] | 462 | .fields = (VMStateField[]) { |
Mark Cave-Ayland | e5dffaa | 2015-02-09 22:40:45 +0000 | [diff] [blame] | 463 | VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice), |
Juan Quintela | 1f1f060 | 2010-12-01 21:54:04 +0100 | [diff] [blame] | 464 | VMSTATE_BUFFER(data, KBDState), |
| 465 | VMSTATE_INT32(rptr, KBDState), |
| 466 | VMSTATE_INT32(wptr, KBDState), |
| 467 | VMSTATE_INT32(count, KBDState), |
| 468 | VMSTATE_END_OF_LIST() |
| 469 | } |
| 470 | }; |
blueswir1 | 9b64997 | 2008-12-30 19:01:19 +0000 | [diff] [blame] | 471 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 472 | static void adb_kbd_reset(DeviceState *dev) |
bellard | 3988e89 | 2005-02-15 22:58:51 +0000 | [diff] [blame] | 473 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 474 | ADBDevice *d = ADB_DEVICE(dev); |
| 475 | KBDState *s = ADB_KEYBOARD(dev); |
bellard | 3988e89 | 2005-02-15 22:58:51 +0000 | [diff] [blame] | 476 | |
| 477 | d->handler = 1; |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 478 | d->devaddr = ADB_DEVID_KEYBOARD; |
| 479 | memset(s->data, 0, sizeof(s->data)); |
| 480 | s->rptr = 0; |
| 481 | s->wptr = 0; |
| 482 | s->count = 0; |
bellard | 3988e89 | 2005-02-15 22:58:51 +0000 | [diff] [blame] | 483 | } |
| 484 | |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 485 | static QemuInputHandler adb_keyboard_handler = { |
| 486 | .name = "QEMU ADB Keyboard", |
| 487 | .mask = INPUT_EVENT_MASK_KEY, |
| 488 | .event = adb_keyboard_event, |
| 489 | }; |
| 490 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 491 | static void adb_kbd_realizefn(DeviceState *dev, Error **errp) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 492 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 493 | ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev); |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 494 | akc->parent_realize(dev, errp); |
John Arbuckle | 5a1f497 | 2016-08-17 22:27:48 -0400 | [diff] [blame] | 495 | qemu_input_handler_register(dev, &adb_keyboard_handler); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 498 | static void adb_kbd_initfn(Object *obj) |
| 499 | { |
| 500 | ADBDevice *d = ADB_DEVICE(obj); |
| 501 | |
| 502 | d->devaddr = ADB_DEVID_KEYBOARD; |
| 503 | } |
| 504 | |
| 505 | static void adb_kbd_class_init(ObjectClass *oc, void *data) |
| 506 | { |
| 507 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 508 | ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc); |
| 509 | ADBKeyboardClass *akc = ADB_KEYBOARD_CLASS(oc); |
| 510 | |
| 511 | akc->parent_realize = dc->realize; |
| 512 | dc->realize = adb_kbd_realizefn; |
Laurent Vivier | 32f3a89 | 2015-09-26 18:22:03 +0200 | [diff] [blame] | 513 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 514 | |
| 515 | adc->devreq = adb_kbd_request; |
| 516 | dc->reset = adb_kbd_reset; |
| 517 | dc->vmsd = &vmstate_adb_kbd; |
| 518 | } |
| 519 | |
| 520 | static const TypeInfo adb_kbd_type_info = { |
| 521 | .name = TYPE_ADB_KEYBOARD, |
| 522 | .parent = TYPE_ADB_DEVICE, |
| 523 | .instance_size = sizeof(KBDState), |
| 524 | .instance_init = adb_kbd_initfn, |
| 525 | .class_init = adb_kbd_class_init, |
| 526 | .class_size = sizeof(ADBKeyboardClass), |
| 527 | }; |
| 528 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 529 | /***************************************************************/ |
| 530 | /* Mouse ADB device */ |
| 531 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 532 | #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE) |
| 533 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 534 | typedef struct MouseState { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 535 | /*< public >*/ |
| 536 | ADBDevice parent_obj; |
| 537 | /*< private >*/ |
| 538 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 539 | int buttons_state, last_buttons_state; |
| 540 | int dx, dy, dz; |
| 541 | } MouseState; |
| 542 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 543 | #define ADB_MOUSE_CLASS(class) \ |
| 544 | OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE) |
| 545 | #define ADB_MOUSE_GET_CLASS(obj) \ |
| 546 | OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE) |
| 547 | |
| 548 | typedef struct ADBMouseClass { |
| 549 | /*< public >*/ |
| 550 | ADBDeviceClass parent_class; |
| 551 | /*< private >*/ |
| 552 | |
| 553 | DeviceRealize parent_realize; |
| 554 | } ADBMouseClass; |
| 555 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 556 | static void adb_mouse_event(void *opaque, |
| 557 | int dx1, int dy1, int dz1, int buttons_state) |
| 558 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 559 | MouseState *s = opaque; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 560 | |
| 561 | s->dx += dx1; |
| 562 | s->dy += dy1; |
| 563 | s->dz += dz1; |
| 564 | s->buttons_state = buttons_state; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf) |
| 569 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 570 | MouseState *s = ADB_MOUSE(d); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 571 | int dx, dy; |
| 572 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 573 | if (s->last_buttons_state == s->buttons_state && |
| 574 | s->dx == 0 && s->dy == 0) |
| 575 | return 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 576 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 577 | dx = s->dx; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 578 | if (dx < -63) |
| 579 | dx = -63; |
| 580 | else if (dx > 63) |
| 581 | dx = 63; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 582 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 583 | dy = s->dy; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 584 | if (dy < -63) |
| 585 | dy = -63; |
| 586 | else if (dy > 63) |
| 587 | dy = 63; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 588 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 589 | s->dx -= dx; |
| 590 | s->dy -= dy; |
| 591 | s->last_buttons_state = s->buttons_state; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 592 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 593 | dx &= 0x7f; |
| 594 | dy &= 0x7f; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 595 | |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 596 | if (!(s->buttons_state & MOUSE_EVENT_LBUTTON)) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 597 | dy |= 0x80; |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 598 | if (!(s->buttons_state & MOUSE_EVENT_RBUTTON)) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 599 | dx |= 0x80; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 600 | |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 601 | obuf[0] = dy; |
| 602 | obuf[1] = dx; |
| 603 | return 2; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 604 | } |
| 605 | |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 606 | static int adb_mouse_request(ADBDevice *d, uint8_t *obuf, |
| 607 | const uint8_t *buf, int len) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 608 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 609 | MouseState *s = ADB_MOUSE(d); |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 610 | int cmd, reg, olen; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 611 | |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 612 | if ((buf[0] & 0x0f) == ADB_FLUSH) { |
| 613 | /* flush mouse fifo */ |
| 614 | s->buttons_state = s->last_buttons_state; |
| 615 | s->dx = 0; |
| 616 | s->dy = 0; |
| 617 | s->dz = 0; |
| 618 | return 0; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 619 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 620 | |
| 621 | cmd = buf[0] & 0xc; |
| 622 | reg = buf[0] & 0x3; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 623 | olen = 0; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 624 | switch(cmd) { |
| 625 | case ADB_WRITEREG: |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 626 | ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 627 | switch(reg) { |
| 628 | case 2: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 629 | break; |
| 630 | case 3: |
| 631 | switch(buf[2]) { |
| 632 | case ADB_CMD_SELF_TEST: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 633 | break; |
| 634 | case ADB_CMD_CHANGE_ID: |
| 635 | case ADB_CMD_CHANGE_ID_AND_ACT: |
| 636 | case ADB_CMD_CHANGE_ID_AND_ENABLE: |
| 637 | d->devaddr = buf[1] & 0xf; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 638 | break; |
| 639 | default: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 640 | d->devaddr = buf[1] & 0xf; |
Hervé Poussineau | a37eb9f | 2016-10-25 09:01:01 +0200 | [diff] [blame] | 641 | /* we support handlers: |
| 642 | * 0x01: Classic Apple Mouse Protocol / 100 cpi operations |
| 643 | * 0x02: Classic Apple Mouse Protocol / 200 cpi operations |
| 644 | * we don't support handlers (at least): |
| 645 | * 0x03: Mouse systems A3 trackball |
| 646 | * 0x04: Extended Apple Mouse Protocol |
| 647 | * 0x2f: Microspeed mouse |
| 648 | * 0x42: Macally |
| 649 | * 0x5f: Microspeed mouse |
| 650 | * 0x66: Microspeed mouse |
| 651 | */ |
| 652 | if (buf[2] == 1 || buf[2] == 2) { |
| 653 | d->handler = buf[2]; |
| 654 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | } |
| 658 | break; |
| 659 | case ADB_READREG: |
| 660 | switch(reg) { |
bellard | bec9d98 | 2004-07-12 20:15:26 +0000 | [diff] [blame] | 661 | case 0: |
| 662 | olen = adb_mouse_poll(d, obuf); |
| 663 | break; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 664 | case 1: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 665 | break; |
| 666 | case 3: |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 667 | obuf[0] = d->handler; |
| 668 | obuf[1] = d->devaddr; |
| 669 | olen = 2; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 670 | break; |
| 671 | } |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 672 | ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg, |
| 673 | obuf[0], obuf[1]); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 674 | break; |
| 675 | } |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 676 | return olen; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 679 | static void adb_mouse_reset(DeviceState *dev) |
bellard | 3988e89 | 2005-02-15 22:58:51 +0000 | [diff] [blame] | 680 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 681 | ADBDevice *d = ADB_DEVICE(dev); |
| 682 | MouseState *s = ADB_MOUSE(dev); |
bellard | 3988e89 | 2005-02-15 22:58:51 +0000 | [diff] [blame] | 683 | |
| 684 | d->handler = 2; |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 685 | d->devaddr = ADB_DEVID_MOUSE; |
| 686 | s->last_buttons_state = s->buttons_state = 0; |
| 687 | s->dx = s->dy = s->dz = 0; |
bellard | 3988e89 | 2005-02-15 22:58:51 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Juan Quintela | 2b2cd59 | 2010-12-01 21:56:35 +0100 | [diff] [blame] | 690 | static const VMStateDescription vmstate_adb_mouse = { |
| 691 | .name = "adb_mouse", |
Mark Cave-Ayland | e5dffaa | 2015-02-09 22:40:45 +0000 | [diff] [blame] | 692 | .version_id = 2, |
| 693 | .minimum_version_id = 2, |
Juan Quintela | 35d0845 | 2014-04-16 16:01:33 +0200 | [diff] [blame] | 694 | .fields = (VMStateField[]) { |
Mark Cave-Ayland | e5dffaa | 2015-02-09 22:40:45 +0000 | [diff] [blame] | 695 | VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device, |
| 696 | ADBDevice), |
Juan Quintela | 2b2cd59 | 2010-12-01 21:56:35 +0100 | [diff] [blame] | 697 | VMSTATE_INT32(buttons_state, MouseState), |
| 698 | VMSTATE_INT32(last_buttons_state, MouseState), |
| 699 | VMSTATE_INT32(dx, MouseState), |
| 700 | VMSTATE_INT32(dy, MouseState), |
| 701 | VMSTATE_INT32(dz, MouseState), |
| 702 | VMSTATE_END_OF_LIST() |
| 703 | } |
| 704 | }; |
blueswir1 | 9b64997 | 2008-12-30 19:01:19 +0000 | [diff] [blame] | 705 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 706 | static void adb_mouse_realizefn(DeviceState *dev, Error **errp) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 707 | { |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 708 | MouseState *s = ADB_MOUSE(dev); |
| 709 | ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 710 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 711 | amc->parent_realize(dev, errp); |
| 712 | |
| 713 | qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse"); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 714 | } |
Andreas Färber | 84ede32 | 2013-01-23 23:04:03 +0000 | [diff] [blame] | 715 | |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 716 | static void adb_mouse_initfn(Object *obj) |
| 717 | { |
| 718 | ADBDevice *d = ADB_DEVICE(obj); |
| 719 | |
| 720 | d->devaddr = ADB_DEVID_MOUSE; |
| 721 | } |
| 722 | |
| 723 | static void adb_mouse_class_init(ObjectClass *oc, void *data) |
| 724 | { |
| 725 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 726 | ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc); |
| 727 | ADBMouseClass *amc = ADB_MOUSE_CLASS(oc); |
| 728 | |
| 729 | amc->parent_realize = dc->realize; |
| 730 | dc->realize = adb_mouse_realizefn; |
Laurent Vivier | 32f3a89 | 2015-09-26 18:22:03 +0200 | [diff] [blame] | 731 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 732 | |
| 733 | adc->devreq = adb_mouse_request; |
| 734 | dc->reset = adb_mouse_reset; |
| 735 | dc->vmsd = &vmstate_adb_mouse; |
| 736 | } |
| 737 | |
| 738 | static const TypeInfo adb_mouse_type_info = { |
| 739 | .name = TYPE_ADB_MOUSE, |
| 740 | .parent = TYPE_ADB_DEVICE, |
| 741 | .instance_size = sizeof(MouseState), |
| 742 | .instance_init = adb_mouse_initfn, |
| 743 | .class_init = adb_mouse_class_init, |
| 744 | .class_size = sizeof(ADBMouseClass), |
| 745 | }; |
| 746 | |
Andreas Färber | 84ede32 | 2013-01-23 23:04:03 +0000 | [diff] [blame] | 747 | |
| 748 | static void adb_register_types(void) |
| 749 | { |
| 750 | type_register_static(&adb_bus_type_info); |
Andreas Färber | 2e4a7c9 | 2013-01-23 23:04:04 +0000 | [diff] [blame] | 751 | type_register_static(&adb_device_type_info); |
| 752 | type_register_static(&adb_kbd_type_info); |
| 753 | type_register_static(&adb_mouse_type_info); |
Andreas Färber | 84ede32 | 2013-01-23 23:04:03 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | type_init(adb_register_types) |