blob: fcca3a8eb947bf44114af30c5d8423c8f25dfdb4 [file] [log] [blame]
bellard267002c2004-06-03 18:46:20 +00001/*
2 * QEMU ADB support
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard267002c2004-06-03 18:46:20 +00004 * Copyright (c) 2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard267002c2004-06-03 18:46:20 +00006 * 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 Maydell04308912016-01-26 18:17:30 +000024#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010026#include "hw/input/adb.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
John Arbuckle5a1f4972016-08-17 22:27:48 -040028#include "include/hw/input/adb-keys.h"
29#include "ui/input.h"
30#include "sysemu/sysemu.h"
bellard267002c2004-06-03 18:46:20 +000031
blueswir1ea026b22008-12-24 09:38:16 +000032/* debug ADB */
33//#define DEBUG_ADB
34
35#ifdef DEBUG_ADB
Blue Swirl001faf32009-05-13 17:53:17 +000036#define ADB_DPRINTF(fmt, ...) \
37do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0)
blueswir1ea026b22008-12-24 09:38:16 +000038#else
Blue Swirl001faf32009-05-13 17:53:17 +000039#define ADB_DPRINTF(fmt, ...)
blueswir1ea026b22008-12-24 09:38:16 +000040#endif
41
bellard267002c2004-06-03 18:46:20 +000042/* 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ärber2e4a7c92013-01-23 23:04:04 +000055#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
bellard267002c2004-06-03 18:46:20 +000061
bellardbec9d982004-07-12 20:15:26 +000062/* error codes */
63#define ADB_RET_NOTPRESENT (-2)
64
John Arbucklef366e722016-08-17 22:27:50 -040065/* The adb keyboard doesn't have every key imaginable */
66#define NO_KEY 0xff
67
Andreas Färber2e4a7c92013-01-23 23:04:04 +000068static void adb_device_reset(ADBDevice *d)
69{
70 qdev_reset_all(DEVICE(d));
71}
72
bellarde2733d22004-06-21 22:46:10 +000073int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
bellard267002c2004-06-03 18:46:20 +000074{
75 ADBDevice *d;
76 int devaddr, cmd, i;
bellard267002c2004-06-03 18:46:20 +000077
bellard819e7122004-06-21 16:47:13 +000078 cmd = buf[0] & 0xf;
bellardbec9d982004-07-12 20:15:26 +000079 if (cmd == ADB_BUSRESET) {
80 for(i = 0; i < s->nb_devices; i++) {
Andreas Färber2e4a7c92013-01-23 23:04:04 +000081 d = s->devices[i];
82 adb_device_reset(d);
bellardbec9d982004-07-12 20:15:26 +000083 }
84 return 0;
85 }
bellard819e7122004-06-21 16:47:13 +000086 devaddr = buf[0] >> 4;
bellard267002c2004-06-03 18:46:20 +000087 for(i = 0; i < s->nb_devices; i++) {
Andreas Färber2e4a7c92013-01-23 23:04:04 +000088 d = s->devices[i];
bellard267002c2004-06-03 18:46:20 +000089 if (d->devaddr == devaddr) {
Andreas Färber2e4a7c92013-01-23 23:04:04 +000090 ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d);
91 return adc->devreq(d, obuf, buf, len);
bellard267002c2004-06-03 18:46:20 +000092 }
93 }
bellardbec9d982004-07-12 20:15:26 +000094 return ADB_RET_NOTPRESENT;
bellarde2733d22004-06-21 22:46:10 +000095}
96
bellardbec9d982004-07-12 20:15:26 +000097/* XXX: move that to cuda ? */
Hervé Poussineau216c9062016-02-07 21:34:08 +010098int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
bellarde2733d22004-06-21 22:46:10 +000099{
100 ADBDevice *d;
101 int olen, i;
bellardbec9d982004-07-12 20:15:26 +0000102 uint8_t buf[1];
bellarde2733d22004-06-21 22:46:10 +0000103
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ärber2e4a7c92013-01-23 23:04:04 +0000108 d = s->devices[s->poll_index];
Hervé Poussineau216c9062016-02-07 21:34:08 +0100109 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 }
bellardbec9d982004-07-12 20:15:26 +0000118 }
119 s->poll_index++;
bellarde2733d22004-06-21 22:46:10 +0000120 }
121 return olen;
bellard267002c2004-06-03 18:46:20 +0000122}
123
Andreas Färber84ede322013-01-23 23:04:03 +0000124static const TypeInfo adb_bus_type_info = {
125 .name = TYPE_ADB_BUS,
126 .parent = TYPE_BUS,
127 .instance_size = sizeof(ADBBusState),
128};
129
Mark Cave-Aylande5dffaa2015-02-09 22:40:45 +0000130static 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ärber2e4a7c92013-01-23 23:04:04 +0000141static 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
153static 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
161static 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
bellard267002c2004-06-03 18:46:20 +0000169/***************************************************************/
170/* Keyboard ADB device */
171
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000172#define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD)
173
bellarde2733d22004-06-21 22:46:10 +0000174typedef struct KBDState {
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000175 /*< private >*/
176 ADBDevice parent_obj;
177 /*< public >*/
178
bellarde2733d22004-06-21 22:46:10 +0000179 uint8_t data[128];
180 int rptr, wptr, count;
181} KBDState;
182
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000183#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
188typedef struct ADBKeyboardClass {
189 /*< private >*/
190 ADBDeviceClass parent_class;
191 /*< public >*/
192
193 DeviceRealize parent_realize;
194} ADBKeyboardClass;
195
John Arbuckle5a1f4972016-08-17 22:27:48 -0400196int qcode_to_adb_keycode[] = {
John Arbucklef366e722016-08-17 22:27:50 -0400197 /* Make sure future additions are automatically set to NO_KEY */
198 [0 ... 0xff] = NO_KEY,
John Arbuckle5a1f4972016-08-17 22:27:48 -0400199
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 Arbuckle5a1f4972016-08-17 22:27:48 -0400204 [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 Arbuckle25c01db2016-08-17 22:27:49 -0400276 [Q_KEY_CODE_PRINT] = ADB_KEY_F13,
277 [Q_KEY_CODE_SYSRQ] = ADB_KEY_F13,
John Arbuckle5a1f4972016-08-17 22:27:48 -0400278 [Q_KEY_CODE_SCROLL_LOCK] = ADB_KEY_F14,
John Arbuckle25c01db2016-08-17 22:27:49 -0400279 [Q_KEY_CODE_PAUSE] = ADB_KEY_F15,
John Arbuckle5a1f4972016-08-17 22:27:48 -0400280
281 [Q_KEY_CODE_NUM_LOCK] = ADB_KEY_KP_CLEAR,
John Arbuckle25c01db2016-08-17 22:27:49 -0400282 [Q_KEY_CODE_KP_EQUALS] = ADB_KEY_KP_EQUAL,
John Arbuckle5a1f4972016-08-17 22:27:48 -0400283 [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 Arbuckle25c01db2016-08-17 22:27:49 -0400305 [Q_KEY_CODE_HELP] = ADB_KEY_HELP,
John Arbuckle5a1f4972016-08-17 22:27:48 -0400306 [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 Arbuckle5a1f4972016-08-17 22:27:48 -0400313 [Q_KEY_CODE_POWER] = ADB_KEY_POWER
bellard267002c2004-06-03 18:46:20 +0000314};
315
316static void adb_kbd_put_keycode(void *opaque, int keycode)
317{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000318 KBDState *s = opaque;
bellarde2733d22004-06-21 22:46:10 +0000319
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++;
bellard267002c2004-06-03 18:46:20 +0000325 }
326}
327
bellarde2733d22004-06-21 22:46:10 +0000328static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
bellard267002c2004-06-03 18:46:20 +0000329{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000330 KBDState *s = ADB_KEYBOARD(d);
John Arbuckle5a1f4972016-08-17 22:27:48 -0400331 int keycode;
bellarde2733d22004-06-21 22:46:10 +0000332 int olen;
333
334 olen = 0;
John Arbuckle5a1f4972016-08-17 22:27:48 -0400335 if (s->count == 0) {
336 return 0;
bellarde2733d22004-06-21 22:46:10 +0000337 }
John Arbuckle5a1f4972016-08-17 22:27:48 -0400338 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
bellarde2733d22004-06-21 22:46:10 +0000364 return olen;
365}
366
367static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
368 const uint8_t *buf, int len)
369{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000370 KBDState *s = ADB_KEYBOARD(d);
bellarde2733d22004-06-21 22:46:10 +0000371 int cmd, reg, olen;
372
bellardbec9d982004-07-12 20:15:26 +0000373 if ((buf[0] & 0x0f) == ADB_FLUSH) {
374 /* flush keyboard fifo */
375 s->wptr = s->rptr = s->count = 0;
376 return 0;
bellarde2733d22004-06-21 22:46:10 +0000377 }
bellard267002c2004-06-03 18:46:20 +0000378
379 cmd = buf[0] & 0xc;
380 reg = buf[0] & 0x3;
bellarde2733d22004-06-21 22:46:10 +0000381 olen = 0;
bellard267002c2004-06-03 18:46:20 +0000382 switch(cmd) {
383 case ADB_WRITEREG:
384 switch(reg) {
385 case 2:
386 /* LED status */
bellard267002c2004-06-03 18:46:20 +0000387 break;
388 case 3:
389 switch(buf[2]) {
390 case ADB_CMD_SELF_TEST:
bellard267002c2004-06-03 18:46:20 +0000391 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;
bellard267002c2004-06-03 18:46:20 +0000396 break;
397 default:
bellard267002c2004-06-03 18:46:20 +0000398 d->devaddr = buf[1] & 0xf;
Hervé Poussineaua37eb9f2016-10-25 09:01:01 +0200399 /* 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 }
bellard267002c2004-06-03 18:46:20 +0000407 break;
408 }
409 }
410 break;
411 case ADB_READREG:
412 switch(reg) {
bellardbec9d982004-07-12 20:15:26 +0000413 case 0:
414 olen = adb_kbd_poll(d, obuf);
415 break;
bellard267002c2004-06-03 18:46:20 +0000416 case 1:
bellard267002c2004-06-03 18:46:20 +0000417 break;
418 case 2:
bellarde2733d22004-06-21 22:46:10 +0000419 obuf[0] = 0x00; /* XXX: check this */
420 obuf[1] = 0x07; /* led status */
421 olen = 2;
bellard267002c2004-06-03 18:46:20 +0000422 break;
423 case 3:
bellarde2733d22004-06-21 22:46:10 +0000424 obuf[0] = d->handler;
425 obuf[1] = d->devaddr;
426 olen = 2;
bellard267002c2004-06-03 18:46:20 +0000427 break;
428 }
429 break;
430 }
bellarde2733d22004-06-21 22:46:10 +0000431 return olen;
bellard267002c2004-06-03 18:46:20 +0000432}
433
John Arbuckle5a1f4972016-08-17 22:27:48 -0400434/* This is where keyboard events enter this file */
435static 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é Poussineaua37eb9f2016-10-25 09:01:01 +0200445 /* FIXME: take handler into account when translating qcode */
John Arbuckle5a1f4972016-08-17 22:27:48 -0400446 keycode = qcode_to_adb_keycode[qcode];
John Arbucklef366e722016-08-17 22:27:50 -0400447 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 Arbuckle5a1f4972016-08-17 22:27:48 -0400451 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 Quintela1f1f0602010-12-01 21:54:04 +0100458static const VMStateDescription vmstate_adb_kbd = {
459 .name = "adb_kbd",
Mark Cave-Aylande5dffaa2015-02-09 22:40:45 +0000460 .version_id = 2,
461 .minimum_version_id = 2,
Juan Quintela35d08452014-04-16 16:01:33 +0200462 .fields = (VMStateField[]) {
Mark Cave-Aylande5dffaa2015-02-09 22:40:45 +0000463 VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice),
Juan Quintela1f1f0602010-12-01 21:54:04 +0100464 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};
blueswir19b649972008-12-30 19:01:19 +0000471
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000472static void adb_kbd_reset(DeviceState *dev)
bellard3988e892005-02-15 22:58:51 +0000473{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000474 ADBDevice *d = ADB_DEVICE(dev);
475 KBDState *s = ADB_KEYBOARD(dev);
bellard3988e892005-02-15 22:58:51 +0000476
477 d->handler = 1;
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000478 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;
bellard3988e892005-02-15 22:58:51 +0000483}
484
John Arbuckle5a1f4972016-08-17 22:27:48 -0400485static QemuInputHandler adb_keyboard_handler = {
486 .name = "QEMU ADB Keyboard",
487 .mask = INPUT_EVENT_MASK_KEY,
488 .event = adb_keyboard_event,
489};
490
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000491static void adb_kbd_realizefn(DeviceState *dev, Error **errp)
bellard267002c2004-06-03 18:46:20 +0000492{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000493 ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev);
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000494 akc->parent_realize(dev, errp);
John Arbuckle5a1f4972016-08-17 22:27:48 -0400495 qemu_input_handler_register(dev, &adb_keyboard_handler);
bellard267002c2004-06-03 18:46:20 +0000496}
497
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000498static void adb_kbd_initfn(Object *obj)
499{
500 ADBDevice *d = ADB_DEVICE(obj);
501
502 d->devaddr = ADB_DEVID_KEYBOARD;
503}
504
505static 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 Vivier32f3a892015-09-26 18:22:03 +0200513 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000514
515 adc->devreq = adb_kbd_request;
516 dc->reset = adb_kbd_reset;
517 dc->vmsd = &vmstate_adb_kbd;
518}
519
520static 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
bellard267002c2004-06-03 18:46:20 +0000529/***************************************************************/
530/* Mouse ADB device */
531
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000532#define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE)
533
bellarde2733d22004-06-21 22:46:10 +0000534typedef struct MouseState {
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000535 /*< public >*/
536 ADBDevice parent_obj;
537 /*< private >*/
538
bellarde2733d22004-06-21 22:46:10 +0000539 int buttons_state, last_buttons_state;
540 int dx, dy, dz;
541} MouseState;
542
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000543#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
548typedef struct ADBMouseClass {
549 /*< public >*/
550 ADBDeviceClass parent_class;
551 /*< private >*/
552
553 DeviceRealize parent_realize;
554} ADBMouseClass;
555
bellard267002c2004-06-03 18:46:20 +0000556static void adb_mouse_event(void *opaque,
557 int dx1, int dy1, int dz1, int buttons_state)
558{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000559 MouseState *s = opaque;
bellarde2733d22004-06-21 22:46:10 +0000560
561 s->dx += dx1;
562 s->dy += dy1;
563 s->dz += dz1;
564 s->buttons_state = buttons_state;
565}
566
567
568static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
569{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000570 MouseState *s = ADB_MOUSE(d);
bellard267002c2004-06-03 18:46:20 +0000571 int dx, dy;
572
bellarde2733d22004-06-21 22:46:10 +0000573 if (s->last_buttons_state == s->buttons_state &&
574 s->dx == 0 && s->dy == 0)
575 return 0;
ths3b46e622007-09-17 08:09:54 +0000576
bellarde2733d22004-06-21 22:46:10 +0000577 dx = s->dx;
bellard267002c2004-06-03 18:46:20 +0000578 if (dx < -63)
579 dx = -63;
580 else if (dx > 63)
581 dx = 63;
ths3b46e622007-09-17 08:09:54 +0000582
bellarde2733d22004-06-21 22:46:10 +0000583 dy = s->dy;
bellard267002c2004-06-03 18:46:20 +0000584 if (dy < -63)
585 dy = -63;
586 else if (dy > 63)
587 dy = 63;
ths3b46e622007-09-17 08:09:54 +0000588
bellarde2733d22004-06-21 22:46:10 +0000589 s->dx -= dx;
590 s->dy -= dy;
591 s->last_buttons_state = s->buttons_state;
ths3b46e622007-09-17 08:09:54 +0000592
bellard267002c2004-06-03 18:46:20 +0000593 dx &= 0x7f;
594 dy &= 0x7f;
ths3b46e622007-09-17 08:09:54 +0000595
bellardbec9d982004-07-12 20:15:26 +0000596 if (!(s->buttons_state & MOUSE_EVENT_LBUTTON))
bellard267002c2004-06-03 18:46:20 +0000597 dy |= 0x80;
bellardbec9d982004-07-12 20:15:26 +0000598 if (!(s->buttons_state & MOUSE_EVENT_RBUTTON))
bellard267002c2004-06-03 18:46:20 +0000599 dx |= 0x80;
ths3b46e622007-09-17 08:09:54 +0000600
bellardbec9d982004-07-12 20:15:26 +0000601 obuf[0] = dy;
602 obuf[1] = dx;
603 return 2;
bellard267002c2004-06-03 18:46:20 +0000604}
605
bellarde2733d22004-06-21 22:46:10 +0000606static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
607 const uint8_t *buf, int len)
bellard267002c2004-06-03 18:46:20 +0000608{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000609 MouseState *s = ADB_MOUSE(d);
bellarde2733d22004-06-21 22:46:10 +0000610 int cmd, reg, olen;
ths3b46e622007-09-17 08:09:54 +0000611
bellardbec9d982004-07-12 20:15:26 +0000612 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;
bellarde2733d22004-06-21 22:46:10 +0000619 }
bellard267002c2004-06-03 18:46:20 +0000620
621 cmd = buf[0] & 0xc;
622 reg = buf[0] & 0x3;
bellarde2733d22004-06-21 22:46:10 +0000623 olen = 0;
bellard267002c2004-06-03 18:46:20 +0000624 switch(cmd) {
625 case ADB_WRITEREG:
blueswir1ea026b22008-12-24 09:38:16 +0000626 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]);
bellard267002c2004-06-03 18:46:20 +0000627 switch(reg) {
628 case 2:
bellard267002c2004-06-03 18:46:20 +0000629 break;
630 case 3:
631 switch(buf[2]) {
632 case ADB_CMD_SELF_TEST:
bellard267002c2004-06-03 18:46:20 +0000633 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;
bellard267002c2004-06-03 18:46:20 +0000638 break;
639 default:
bellard267002c2004-06-03 18:46:20 +0000640 d->devaddr = buf[1] & 0xf;
Hervé Poussineaua37eb9f2016-10-25 09:01:01 +0200641 /* 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 }
bellard267002c2004-06-03 18:46:20 +0000655 break;
656 }
657 }
658 break;
659 case ADB_READREG:
660 switch(reg) {
bellardbec9d982004-07-12 20:15:26 +0000661 case 0:
662 olen = adb_mouse_poll(d, obuf);
663 break;
bellard267002c2004-06-03 18:46:20 +0000664 case 1:
bellard267002c2004-06-03 18:46:20 +0000665 break;
666 case 3:
bellarde2733d22004-06-21 22:46:10 +0000667 obuf[0] = d->handler;
668 obuf[1] = d->devaddr;
669 olen = 2;
bellard267002c2004-06-03 18:46:20 +0000670 break;
671 }
blueswir1ea026b22008-12-24 09:38:16 +0000672 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg,
673 obuf[0], obuf[1]);
bellard267002c2004-06-03 18:46:20 +0000674 break;
675 }
bellarde2733d22004-06-21 22:46:10 +0000676 return olen;
bellard267002c2004-06-03 18:46:20 +0000677}
678
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000679static void adb_mouse_reset(DeviceState *dev)
bellard3988e892005-02-15 22:58:51 +0000680{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000681 ADBDevice *d = ADB_DEVICE(dev);
682 MouseState *s = ADB_MOUSE(dev);
bellard3988e892005-02-15 22:58:51 +0000683
684 d->handler = 2;
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000685 d->devaddr = ADB_DEVID_MOUSE;
686 s->last_buttons_state = s->buttons_state = 0;
687 s->dx = s->dy = s->dz = 0;
bellard3988e892005-02-15 22:58:51 +0000688}
689
Juan Quintela2b2cd592010-12-01 21:56:35 +0100690static const VMStateDescription vmstate_adb_mouse = {
691 .name = "adb_mouse",
Mark Cave-Aylande5dffaa2015-02-09 22:40:45 +0000692 .version_id = 2,
693 .minimum_version_id = 2,
Juan Quintela35d08452014-04-16 16:01:33 +0200694 .fields = (VMStateField[]) {
Mark Cave-Aylande5dffaa2015-02-09 22:40:45 +0000695 VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device,
696 ADBDevice),
Juan Quintela2b2cd592010-12-01 21:56:35 +0100697 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};
blueswir19b649972008-12-30 19:01:19 +0000705
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000706static void adb_mouse_realizefn(DeviceState *dev, Error **errp)
bellard267002c2004-06-03 18:46:20 +0000707{
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000708 MouseState *s = ADB_MOUSE(dev);
709 ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev);
bellard267002c2004-06-03 18:46:20 +0000710
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000711 amc->parent_realize(dev, errp);
712
713 qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse");
bellard267002c2004-06-03 18:46:20 +0000714}
Andreas Färber84ede322013-01-23 23:04:03 +0000715
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000716static void adb_mouse_initfn(Object *obj)
717{
718 ADBDevice *d = ADB_DEVICE(obj);
719
720 d->devaddr = ADB_DEVID_MOUSE;
721}
722
723static 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 Vivier32f3a892015-09-26 18:22:03 +0200731 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000732
733 adc->devreq = adb_mouse_request;
734 dc->reset = adb_mouse_reset;
735 dc->vmsd = &vmstate_adb_mouse;
736}
737
738static 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ärber84ede322013-01-23 23:04:03 +0000747
748static void adb_register_types(void)
749{
750 type_register_static(&adb_bus_type_info);
Andreas Färber2e4a7c92013-01-23 23:04:04 +0000751 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ärber84ede322013-01-23 23:04:03 +0000754}
755
756type_init(adb_register_types)