tests/qapi-schema: Improve coverage of bogus member docs
[qemu/ar7.git] / hw / input / adb.c
blob43d3205472795fc0440af1b92367b2ed8e390d95
1 /*
2 * QEMU ADB support
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "hw/hw.h"
26 #include "hw/input/adb.h"
27 #include "ui/console.h"
28 #include "include/hw/input/adb-keys.h"
29 #include "ui/input.h"
30 #include "sysemu/sysemu.h"
32 /* debug ADB */
33 //#define DEBUG_ADB
35 #ifdef DEBUG_ADB
36 #define ADB_DPRINTF(fmt, ...) \
37 do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0)
38 #else
39 #define ADB_DPRINTF(fmt, ...)
40 #endif
42 /* ADB commands */
43 #define ADB_BUSRESET 0x00
44 #define ADB_FLUSH 0x01
45 #define ADB_WRITEREG 0x08
46 #define ADB_READREG 0x0c
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
54 /* ADB default device IDs (upper 4 bits of ADB command byte) */
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
62 /* error codes */
63 #define ADB_RET_NOTPRESENT (-2)
65 /* The adb keyboard doesn't have every key imaginable */
66 #define NO_KEY 0xff
68 static void adb_device_reset(ADBDevice *d)
70 qdev_reset_all(DEVICE(d));
73 int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
75 ADBDevice *d;
76 int devaddr, cmd, i;
78 cmd = buf[0] & 0xf;
79 if (cmd == ADB_BUSRESET) {
80 for(i = 0; i < s->nb_devices; i++) {
81 d = s->devices[i];
82 adb_device_reset(d);
84 return 0;
86 devaddr = buf[0] >> 4;
87 for(i = 0; i < s->nb_devices; i++) {
88 d = s->devices[i];
89 if (d->devaddr == devaddr) {
90 ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d);
91 return adc->devreq(d, obuf, buf, len);
94 return ADB_RET_NOTPRESENT;
97 /* XXX: move that to cuda ? */
98 int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
100 ADBDevice *d;
101 int olen, i;
102 uint8_t buf[1];
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;
108 d = s->devices[s->poll_index];
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;
119 s->poll_index++;
121 return olen;
124 static const TypeInfo adb_bus_type_info = {
125 .name = TYPE_ADB_BUS,
126 .parent = TYPE_BUS,
127 .instance_size = sizeof(ADBBusState),
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()
141 static void adb_device_realizefn(DeviceState *dev, Error **errp)
143 ADBDevice *d = ADB_DEVICE(dev);
144 ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
146 if (bus->nb_devices >= MAX_ADB_DEVICES) {
147 return;
150 bus->devices[bus->nb_devices++] = d;
153 static void adb_device_class_init(ObjectClass *oc, void *data)
155 DeviceClass *dc = DEVICE_CLASS(oc);
157 dc->realize = adb_device_realizefn;
158 dc->bus_type = TYPE_ADB_BUS;
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,
169 /***************************************************************/
170 /* Keyboard ADB device */
172 #define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD)
174 typedef struct KBDState {
175 /*< private >*/
176 ADBDevice parent_obj;
177 /*< public >*/
179 uint8_t data[128];
180 int rptr, wptr, count;
181 } KBDState;
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)
188 typedef struct ADBKeyboardClass {
189 /*< private >*/
190 ADBDeviceClass parent_class;
191 /*< public >*/
193 DeviceRealize parent_realize;
194 } ADBKeyboardClass;
196 int qcode_to_adb_keycode[] = {
197 /* Make sure future additions are automatically set to NO_KEY */
198 [0 ... 0xff] = NO_KEY,
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,
204 [Q_KEY_CODE_ALTGR] = ADB_KEY_RIGHT_OPTION,
205 [Q_KEY_CODE_CTRL] = ADB_KEY_LEFT_CONTROL,
206 [Q_KEY_CODE_CTRL_R] = ADB_KEY_RIGHT_CONTROL,
207 [Q_KEY_CODE_META_L] = ADB_KEY_COMMAND,
208 [Q_KEY_CODE_META_R] = ADB_KEY_COMMAND,
209 [Q_KEY_CODE_SPC] = ADB_KEY_SPACEBAR,
211 [Q_KEY_CODE_ESC] = ADB_KEY_ESC,
212 [Q_KEY_CODE_1] = ADB_KEY_1,
213 [Q_KEY_CODE_2] = ADB_KEY_2,
214 [Q_KEY_CODE_3] = ADB_KEY_3,
215 [Q_KEY_CODE_4] = ADB_KEY_4,
216 [Q_KEY_CODE_5] = ADB_KEY_5,
217 [Q_KEY_CODE_6] = ADB_KEY_6,
218 [Q_KEY_CODE_7] = ADB_KEY_7,
219 [Q_KEY_CODE_8] = ADB_KEY_8,
220 [Q_KEY_CODE_9] = ADB_KEY_9,
221 [Q_KEY_CODE_0] = ADB_KEY_0,
222 [Q_KEY_CODE_MINUS] = ADB_KEY_MINUS,
223 [Q_KEY_CODE_EQUAL] = ADB_KEY_EQUAL,
224 [Q_KEY_CODE_BACKSPACE] = ADB_KEY_DELETE,
225 [Q_KEY_CODE_TAB] = ADB_KEY_TAB,
226 [Q_KEY_CODE_Q] = ADB_KEY_Q,
227 [Q_KEY_CODE_W] = ADB_KEY_W,
228 [Q_KEY_CODE_E] = ADB_KEY_E,
229 [Q_KEY_CODE_R] = ADB_KEY_R,
230 [Q_KEY_CODE_T] = ADB_KEY_T,
231 [Q_KEY_CODE_Y] = ADB_KEY_Y,
232 [Q_KEY_CODE_U] = ADB_KEY_U,
233 [Q_KEY_CODE_I] = ADB_KEY_I,
234 [Q_KEY_CODE_O] = ADB_KEY_O,
235 [Q_KEY_CODE_P] = ADB_KEY_P,
236 [Q_KEY_CODE_BRACKET_LEFT] = ADB_KEY_LEFT_BRACKET,
237 [Q_KEY_CODE_BRACKET_RIGHT] = ADB_KEY_RIGHT_BRACKET,
238 [Q_KEY_CODE_RET] = ADB_KEY_RETURN,
239 [Q_KEY_CODE_A] = ADB_KEY_A,
240 [Q_KEY_CODE_S] = ADB_KEY_S,
241 [Q_KEY_CODE_D] = ADB_KEY_D,
242 [Q_KEY_CODE_F] = ADB_KEY_F,
243 [Q_KEY_CODE_G] = ADB_KEY_G,
244 [Q_KEY_CODE_H] = ADB_KEY_H,
245 [Q_KEY_CODE_J] = ADB_KEY_J,
246 [Q_KEY_CODE_K] = ADB_KEY_K,
247 [Q_KEY_CODE_L] = ADB_KEY_L,
248 [Q_KEY_CODE_SEMICOLON] = ADB_KEY_SEMICOLON,
249 [Q_KEY_CODE_APOSTROPHE] = ADB_KEY_APOSTROPHE,
250 [Q_KEY_CODE_GRAVE_ACCENT] = ADB_KEY_GRAVE_ACCENT,
251 [Q_KEY_CODE_BACKSLASH] = ADB_KEY_BACKSLASH,
252 [Q_KEY_CODE_Z] = ADB_KEY_Z,
253 [Q_KEY_CODE_X] = ADB_KEY_X,
254 [Q_KEY_CODE_C] = ADB_KEY_C,
255 [Q_KEY_CODE_V] = ADB_KEY_V,
256 [Q_KEY_CODE_B] = ADB_KEY_B,
257 [Q_KEY_CODE_N] = ADB_KEY_N,
258 [Q_KEY_CODE_M] = ADB_KEY_M,
259 [Q_KEY_CODE_COMMA] = ADB_KEY_COMMA,
260 [Q_KEY_CODE_DOT] = ADB_KEY_PERIOD,
261 [Q_KEY_CODE_SLASH] = ADB_KEY_FORWARD_SLASH,
262 [Q_KEY_CODE_ASTERISK] = ADB_KEY_KP_MULTIPLY,
263 [Q_KEY_CODE_CAPS_LOCK] = ADB_KEY_CAPS_LOCK,
265 [Q_KEY_CODE_F1] = ADB_KEY_F1,
266 [Q_KEY_CODE_F2] = ADB_KEY_F2,
267 [Q_KEY_CODE_F3] = ADB_KEY_F3,
268 [Q_KEY_CODE_F4] = ADB_KEY_F4,
269 [Q_KEY_CODE_F5] = ADB_KEY_F5,
270 [Q_KEY_CODE_F6] = ADB_KEY_F6,
271 [Q_KEY_CODE_F7] = ADB_KEY_F7,
272 [Q_KEY_CODE_F8] = ADB_KEY_F8,
273 [Q_KEY_CODE_F9] = ADB_KEY_F9,
274 [Q_KEY_CODE_F10] = ADB_KEY_F10,
275 [Q_KEY_CODE_F11] = ADB_KEY_F11,
276 [Q_KEY_CODE_F12] = ADB_KEY_F12,
277 [Q_KEY_CODE_PRINT] = ADB_KEY_F13,
278 [Q_KEY_CODE_SYSRQ] = ADB_KEY_F13,
279 [Q_KEY_CODE_SCROLL_LOCK] = ADB_KEY_F14,
280 [Q_KEY_CODE_PAUSE] = ADB_KEY_F15,
282 [Q_KEY_CODE_NUM_LOCK] = ADB_KEY_KP_CLEAR,
283 [Q_KEY_CODE_KP_EQUALS] = ADB_KEY_KP_EQUAL,
284 [Q_KEY_CODE_KP_DIVIDE] = ADB_KEY_KP_DIVIDE,
285 [Q_KEY_CODE_KP_MULTIPLY] = ADB_KEY_KP_MULTIPLY,
286 [Q_KEY_CODE_KP_SUBTRACT] = ADB_KEY_KP_SUBTRACT,
287 [Q_KEY_CODE_KP_ADD] = ADB_KEY_KP_PLUS,
288 [Q_KEY_CODE_KP_ENTER] = ADB_KEY_KP_ENTER,
289 [Q_KEY_CODE_KP_DECIMAL] = ADB_KEY_KP_PERIOD,
290 [Q_KEY_CODE_KP_0] = ADB_KEY_KP_0,
291 [Q_KEY_CODE_KP_1] = ADB_KEY_KP_1,
292 [Q_KEY_CODE_KP_2] = ADB_KEY_KP_2,
293 [Q_KEY_CODE_KP_3] = ADB_KEY_KP_3,
294 [Q_KEY_CODE_KP_4] = ADB_KEY_KP_4,
295 [Q_KEY_CODE_KP_5] = ADB_KEY_KP_5,
296 [Q_KEY_CODE_KP_6] = ADB_KEY_KP_6,
297 [Q_KEY_CODE_KP_7] = ADB_KEY_KP_7,
298 [Q_KEY_CODE_KP_8] = ADB_KEY_KP_8,
299 [Q_KEY_CODE_KP_9] = ADB_KEY_KP_9,
301 [Q_KEY_CODE_UP] = ADB_KEY_UP,
302 [Q_KEY_CODE_DOWN] = ADB_KEY_DOWN,
303 [Q_KEY_CODE_LEFT] = ADB_KEY_LEFT,
304 [Q_KEY_CODE_RIGHT] = ADB_KEY_RIGHT,
306 [Q_KEY_CODE_HELP] = ADB_KEY_HELP,
307 [Q_KEY_CODE_INSERT] = ADB_KEY_HELP,
308 [Q_KEY_CODE_DELETE] = ADB_KEY_FORWARD_DELETE,
309 [Q_KEY_CODE_HOME] = ADB_KEY_HOME,
310 [Q_KEY_CODE_END] = ADB_KEY_END,
311 [Q_KEY_CODE_PGUP] = ADB_KEY_PAGE_UP,
312 [Q_KEY_CODE_PGDN] = ADB_KEY_PAGE_DOWN,
314 [Q_KEY_CODE_POWER] = ADB_KEY_POWER
317 static void adb_kbd_put_keycode(void *opaque, int keycode)
319 KBDState *s = opaque;
321 if (s->count < sizeof(s->data)) {
322 s->data[s->wptr] = keycode;
323 if (++s->wptr == sizeof(s->data))
324 s->wptr = 0;
325 s->count++;
329 static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
331 KBDState *s = ADB_KEYBOARD(d);
332 int keycode;
333 int olen;
335 olen = 0;
336 if (s->count == 0) {
337 return 0;
339 keycode = s->data[s->rptr];
340 s->rptr++;
341 if (s->rptr == sizeof(s->data)) {
342 s->rptr = 0;
344 s->count--;
346 * The power key is the only two byte value key, so it is a special case.
347 * Since 0x7f is not a used keycode for ADB we overload it to indicate the
348 * power button when we're storing keycodes in our internal buffer, and
349 * expand it out to two bytes when we send to the guest.
351 if (keycode == 0x7f) {
352 obuf[0] = 0x7f;
353 obuf[1] = 0x7f;
354 olen = 2;
355 } else {
356 obuf[0] = keycode;
357 /* NOTE: the power key key-up is the two byte sequence 0xff 0xff;
358 * otherwise we could in theory send a second keycode in the second
359 * byte, but choose not to bother.
361 obuf[1] = 0xff;
362 olen = 2;
365 return olen;
368 static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
369 const uint8_t *buf, int len)
371 KBDState *s = ADB_KEYBOARD(d);
372 int cmd, reg, olen;
374 if ((buf[0] & 0x0f) == ADB_FLUSH) {
375 /* flush keyboard fifo */
376 s->wptr = s->rptr = s->count = 0;
377 return 0;
380 cmd = buf[0] & 0xc;
381 reg = buf[0] & 0x3;
382 olen = 0;
383 switch(cmd) {
384 case ADB_WRITEREG:
385 switch(reg) {
386 case 2:
387 /* LED status */
388 break;
389 case 3:
390 switch(buf[2]) {
391 case ADB_CMD_SELF_TEST:
392 break;
393 case ADB_CMD_CHANGE_ID:
394 case ADB_CMD_CHANGE_ID_AND_ACT:
395 case ADB_CMD_CHANGE_ID_AND_ENABLE:
396 d->devaddr = buf[1] & 0xf;
397 break;
398 default:
399 d->devaddr = buf[1] & 0xf;
400 /* we support handlers:
401 * 1: Apple Standard Keyboard
402 * 2: Apple Extended Keyboard (LShift = RShift)
403 * 3: Apple Extended Keyboard (LShift != RShift)
405 if (buf[2] == 1 || buf[2] == 2 || buf[2] == 3) {
406 d->handler = buf[2];
408 break;
411 break;
412 case ADB_READREG:
413 switch(reg) {
414 case 0:
415 olen = adb_kbd_poll(d, obuf);
416 break;
417 case 1:
418 break;
419 case 2:
420 obuf[0] = 0x00; /* XXX: check this */
421 obuf[1] = 0x07; /* led status */
422 olen = 2;
423 break;
424 case 3:
425 obuf[0] = d->handler;
426 obuf[1] = d->devaddr;
427 olen = 2;
428 break;
430 break;
432 return olen;
435 /* This is where keyboard events enter this file */
436 static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
437 InputEvent *evt)
439 KBDState *s = (KBDState *)dev;
440 int qcode, keycode;
442 qcode = qemu_input_key_value_to_qcode(evt->u.key.data->key);
443 if (qcode >= ARRAY_SIZE(qcode_to_adb_keycode)) {
444 return;
446 /* FIXME: take handler into account when translating qcode */
447 keycode = qcode_to_adb_keycode[qcode];
448 if (keycode == NO_KEY) { /* We don't want to send this to the guest */
449 ADB_DPRINTF("Ignoring NO_KEY\n");
450 return;
452 if (evt->u.key.data->down == false) { /* if key release event */
453 keycode = keycode | 0x80; /* create keyboard break code */
456 adb_kbd_put_keycode(s, keycode);
459 static const VMStateDescription vmstate_adb_kbd = {
460 .name = "adb_kbd",
461 .version_id = 2,
462 .minimum_version_id = 2,
463 .fields = (VMStateField[]) {
464 VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice),
465 VMSTATE_BUFFER(data, KBDState),
466 VMSTATE_INT32(rptr, KBDState),
467 VMSTATE_INT32(wptr, KBDState),
468 VMSTATE_INT32(count, KBDState),
469 VMSTATE_END_OF_LIST()
473 static void adb_kbd_reset(DeviceState *dev)
475 ADBDevice *d = ADB_DEVICE(dev);
476 KBDState *s = ADB_KEYBOARD(dev);
478 d->handler = 1;
479 d->devaddr = ADB_DEVID_KEYBOARD;
480 memset(s->data, 0, sizeof(s->data));
481 s->rptr = 0;
482 s->wptr = 0;
483 s->count = 0;
486 static QemuInputHandler adb_keyboard_handler = {
487 .name = "QEMU ADB Keyboard",
488 .mask = INPUT_EVENT_MASK_KEY,
489 .event = adb_keyboard_event,
492 static void adb_kbd_realizefn(DeviceState *dev, Error **errp)
494 ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev);
495 akc->parent_realize(dev, errp);
496 qemu_input_handler_register(dev, &adb_keyboard_handler);
499 static void adb_kbd_initfn(Object *obj)
501 ADBDevice *d = ADB_DEVICE(obj);
503 d->devaddr = ADB_DEVID_KEYBOARD;
506 static void adb_kbd_class_init(ObjectClass *oc, void *data)
508 DeviceClass *dc = DEVICE_CLASS(oc);
509 ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
510 ADBKeyboardClass *akc = ADB_KEYBOARD_CLASS(oc);
512 akc->parent_realize = dc->realize;
513 dc->realize = adb_kbd_realizefn;
514 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
516 adc->devreq = adb_kbd_request;
517 dc->reset = adb_kbd_reset;
518 dc->vmsd = &vmstate_adb_kbd;
521 static const TypeInfo adb_kbd_type_info = {
522 .name = TYPE_ADB_KEYBOARD,
523 .parent = TYPE_ADB_DEVICE,
524 .instance_size = sizeof(KBDState),
525 .instance_init = adb_kbd_initfn,
526 .class_init = adb_kbd_class_init,
527 .class_size = sizeof(ADBKeyboardClass),
530 /***************************************************************/
531 /* Mouse ADB device */
533 #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE)
535 typedef struct MouseState {
536 /*< public >*/
537 ADBDevice parent_obj;
538 /*< private >*/
540 int buttons_state, last_buttons_state;
541 int dx, dy, dz;
542 } MouseState;
544 #define ADB_MOUSE_CLASS(class) \
545 OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE)
546 #define ADB_MOUSE_GET_CLASS(obj) \
547 OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE)
549 typedef struct ADBMouseClass {
550 /*< public >*/
551 ADBDeviceClass parent_class;
552 /*< private >*/
554 DeviceRealize parent_realize;
555 } ADBMouseClass;
557 static void adb_mouse_event(void *opaque,
558 int dx1, int dy1, int dz1, int buttons_state)
560 MouseState *s = opaque;
562 s->dx += dx1;
563 s->dy += dy1;
564 s->dz += dz1;
565 s->buttons_state = buttons_state;
569 static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
571 MouseState *s = ADB_MOUSE(d);
572 int dx, dy;
574 if (s->last_buttons_state == s->buttons_state &&
575 s->dx == 0 && s->dy == 0)
576 return 0;
578 dx = s->dx;
579 if (dx < -63)
580 dx = -63;
581 else if (dx > 63)
582 dx = 63;
584 dy = s->dy;
585 if (dy < -63)
586 dy = -63;
587 else if (dy > 63)
588 dy = 63;
590 s->dx -= dx;
591 s->dy -= dy;
592 s->last_buttons_state = s->buttons_state;
594 dx &= 0x7f;
595 dy &= 0x7f;
597 if (!(s->buttons_state & MOUSE_EVENT_LBUTTON))
598 dy |= 0x80;
599 if (!(s->buttons_state & MOUSE_EVENT_RBUTTON))
600 dx |= 0x80;
602 obuf[0] = dy;
603 obuf[1] = dx;
604 return 2;
607 static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
608 const uint8_t *buf, int len)
610 MouseState *s = ADB_MOUSE(d);
611 int cmd, reg, olen;
613 if ((buf[0] & 0x0f) == ADB_FLUSH) {
614 /* flush mouse fifo */
615 s->buttons_state = s->last_buttons_state;
616 s->dx = 0;
617 s->dy = 0;
618 s->dz = 0;
619 return 0;
622 cmd = buf[0] & 0xc;
623 reg = buf[0] & 0x3;
624 olen = 0;
625 switch(cmd) {
626 case ADB_WRITEREG:
627 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]);
628 switch(reg) {
629 case 2:
630 break;
631 case 3:
632 switch(buf[2]) {
633 case ADB_CMD_SELF_TEST:
634 break;
635 case ADB_CMD_CHANGE_ID:
636 case ADB_CMD_CHANGE_ID_AND_ACT:
637 case ADB_CMD_CHANGE_ID_AND_ENABLE:
638 d->devaddr = buf[1] & 0xf;
639 break;
640 default:
641 d->devaddr = buf[1] & 0xf;
642 /* we support handlers:
643 * 0x01: Classic Apple Mouse Protocol / 100 cpi operations
644 * 0x02: Classic Apple Mouse Protocol / 200 cpi operations
645 * we don't support handlers (at least):
646 * 0x03: Mouse systems A3 trackball
647 * 0x04: Extended Apple Mouse Protocol
648 * 0x2f: Microspeed mouse
649 * 0x42: Macally
650 * 0x5f: Microspeed mouse
651 * 0x66: Microspeed mouse
653 if (buf[2] == 1 || buf[2] == 2) {
654 d->handler = buf[2];
656 break;
659 break;
660 case ADB_READREG:
661 switch(reg) {
662 case 0:
663 olen = adb_mouse_poll(d, obuf);
664 break;
665 case 1:
666 break;
667 case 3:
668 obuf[0] = d->handler;
669 obuf[1] = d->devaddr;
670 olen = 2;
671 break;
673 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg,
674 obuf[0], obuf[1]);
675 break;
677 return olen;
680 static void adb_mouse_reset(DeviceState *dev)
682 ADBDevice *d = ADB_DEVICE(dev);
683 MouseState *s = ADB_MOUSE(dev);
685 d->handler = 2;
686 d->devaddr = ADB_DEVID_MOUSE;
687 s->last_buttons_state = s->buttons_state = 0;
688 s->dx = s->dy = s->dz = 0;
691 static const VMStateDescription vmstate_adb_mouse = {
692 .name = "adb_mouse",
693 .version_id = 2,
694 .minimum_version_id = 2,
695 .fields = (VMStateField[]) {
696 VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device,
697 ADBDevice),
698 VMSTATE_INT32(buttons_state, MouseState),
699 VMSTATE_INT32(last_buttons_state, MouseState),
700 VMSTATE_INT32(dx, MouseState),
701 VMSTATE_INT32(dy, MouseState),
702 VMSTATE_INT32(dz, MouseState),
703 VMSTATE_END_OF_LIST()
707 static void adb_mouse_realizefn(DeviceState *dev, Error **errp)
709 MouseState *s = ADB_MOUSE(dev);
710 ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev);
712 amc->parent_realize(dev, errp);
714 qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse");
717 static void adb_mouse_initfn(Object *obj)
719 ADBDevice *d = ADB_DEVICE(obj);
721 d->devaddr = ADB_DEVID_MOUSE;
724 static void adb_mouse_class_init(ObjectClass *oc, void *data)
726 DeviceClass *dc = DEVICE_CLASS(oc);
727 ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
728 ADBMouseClass *amc = ADB_MOUSE_CLASS(oc);
730 amc->parent_realize = dc->realize;
731 dc->realize = adb_mouse_realizefn;
732 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
734 adc->devreq = adb_mouse_request;
735 dc->reset = adb_mouse_reset;
736 dc->vmsd = &vmstate_adb_mouse;
739 static const TypeInfo adb_mouse_type_info = {
740 .name = TYPE_ADB_MOUSE,
741 .parent = TYPE_ADB_DEVICE,
742 .instance_size = sizeof(MouseState),
743 .instance_init = adb_mouse_initfn,
744 .class_init = adb_mouse_class_init,
745 .class_size = sizeof(ADBMouseClass),
749 static void adb_register_types(void)
751 type_register_static(&adb_bus_type_info);
752 type_register_static(&adb_device_type_info);
753 type_register_static(&adb_kbd_type_info);
754 type_register_static(&adb_mouse_type_info);
757 type_init(adb_register_types)