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
26 #include "ui/console.h"
32 #define ADB_DPRINTF(fmt, ...) \
33 do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0)
35 #define ADB_DPRINTF(fmt, ...)
39 #define ADB_BUSRESET 0x00
40 #define ADB_FLUSH 0x01
41 #define ADB_WRITEREG 0x08
42 #define ADB_READREG 0x0c
44 /* ADB device commands */
45 #define ADB_CMD_SELF_TEST 0xff
46 #define ADB_CMD_CHANGE_ID 0xfe
47 #define ADB_CMD_CHANGE_ID_AND_ACT 0xfd
48 #define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00
50 /* ADB default device IDs (upper 4 bits of ADB command byte) */
51 #define ADB_DEVID_DONGLE 1
52 #define ADB_DEVID_KEYBOARD 2
53 #define ADB_DEVID_MOUSE 3
54 #define ADB_DEVID_TABLET 4
55 #define ADB_DEVID_MODEM 5
56 #define ADB_DEVID_MISC 7
59 #define ADB_RET_NOTPRESENT (-2)
61 static void adb_device_reset(ADBDevice
*d
)
63 qdev_reset_all(DEVICE(d
));
66 int adb_request(ADBBusState
*s
, uint8_t *obuf
, const uint8_t *buf
, int len
)
72 if (cmd
== ADB_BUSRESET
) {
73 for(i
= 0; i
< s
->nb_devices
; i
++) {
79 devaddr
= buf
[0] >> 4;
80 for(i
= 0; i
< s
->nb_devices
; i
++) {
82 if (d
->devaddr
== devaddr
) {
83 ADBDeviceClass
*adc
= ADB_DEVICE_GET_CLASS(d
);
84 return adc
->devreq(d
, obuf
, buf
, len
);
87 return ADB_RET_NOTPRESENT
;
90 /* XXX: move that to cuda ? */
91 int adb_poll(ADBBusState
*s
, uint8_t *obuf
)
98 for(i
= 0; i
< s
->nb_devices
; i
++) {
99 if (s
->poll_index
>= s
->nb_devices
)
101 d
= s
->devices
[s
->poll_index
];
102 buf
[0] = ADB_READREG
| (d
->devaddr
<< 4);
103 olen
= adb_request(s
, obuf
+ 1, buf
, 1);
104 /* if there is data, we poll again the same device */
115 static const TypeInfo adb_bus_type_info
= {
116 .name
= TYPE_ADB_BUS
,
118 .instance_size
= sizeof(ADBBusState
),
121 static void adb_device_realizefn(DeviceState
*dev
, Error
**errp
)
123 ADBDevice
*d
= ADB_DEVICE(dev
);
124 ADBBusState
*bus
= ADB_BUS(qdev_get_parent_bus(dev
));
126 if (bus
->nb_devices
>= MAX_ADB_DEVICES
) {
130 bus
->devices
[bus
->nb_devices
++] = d
;
133 static void adb_device_class_init(ObjectClass
*oc
, void *data
)
135 DeviceClass
*dc
= DEVICE_CLASS(oc
);
137 dc
->realize
= adb_device_realizefn
;
138 dc
->bus_type
= TYPE_ADB_BUS
;
141 static const TypeInfo adb_device_type_info
= {
142 .name
= TYPE_ADB_DEVICE
,
143 .parent
= TYPE_DEVICE
,
144 .instance_size
= sizeof(ADBDevice
),
146 .class_init
= adb_device_class_init
,
149 /***************************************************************/
150 /* Keyboard ADB device */
152 #define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD)
154 typedef struct KBDState
{
156 ADBDevice parent_obj
;
160 int rptr
, wptr
, count
;
163 #define ADB_KEYBOARD_CLASS(class) \
164 OBJECT_CLASS_CHECK(ADBKeyboardClass, (class), TYPE_ADB_KEYBOARD)
165 #define ADB_KEYBOARD_GET_CLASS(obj) \
166 OBJECT_GET_CLASS(ADBKeyboardClass, (obj), TYPE_ADB_KEYBOARD)
168 typedef struct ADBKeyboardClass
{
170 ADBDeviceClass parent_class
;
173 DeviceRealize parent_realize
;
176 static const uint8_t pc_to_adb_keycode
[256] = {
177 0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48,
178 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54, 0, 1,
179 2, 3, 5, 4, 38, 40, 37, 41, 39, 50, 56, 42, 6, 7, 8, 9,
180 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96,
181 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83,
182 84, 85, 82, 65, 0, 0, 10,103,111, 0, 0,110, 81, 0, 0, 0,
183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 94, 0, 93, 0, 0, 0, 0, 0, 0,104,102, 0, 0,
186 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76,125, 0, 0,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,105, 0, 0, 0, 0, 0,
188 0, 0, 0, 0, 0, 75, 0, 0,124, 0, 0, 0, 0, 0, 0, 0,
189 0, 0, 0, 0, 0, 0, 0,115, 62,116, 0, 59, 0, 60, 0,119,
190 61,121,114,117, 0, 0, 0, 0, 0, 0, 0, 55,126, 0,127, 0,
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192 0, 0, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195 static void adb_kbd_put_keycode(void *opaque
, int keycode
)
197 KBDState
*s
= opaque
;
199 if (s
->count
< sizeof(s
->data
)) {
200 s
->data
[s
->wptr
] = keycode
;
201 if (++s
->wptr
== sizeof(s
->data
))
207 static int adb_kbd_poll(ADBDevice
*d
, uint8_t *obuf
)
209 static int ext_keycode
;
210 KBDState
*s
= ADB_KEYBOARD(d
);
211 int adb_keycode
, keycode
;
218 keycode
= s
->data
[s
->rptr
];
219 if (++s
->rptr
== sizeof(s
->data
))
223 if (keycode
== 0xe0) {
227 adb_keycode
= pc_to_adb_keycode
[keycode
| 0x80];
229 adb_keycode
= pc_to_adb_keycode
[keycode
& 0x7f];
230 obuf
[0] = adb_keycode
| (keycode
& 0x80);
231 /* NOTE: could put a second keycode if needed */
241 static int adb_kbd_request(ADBDevice
*d
, uint8_t *obuf
,
242 const uint8_t *buf
, int len
)
244 KBDState
*s
= ADB_KEYBOARD(d
);
247 if ((buf
[0] & 0x0f) == ADB_FLUSH
) {
248 /* flush keyboard fifo */
249 s
->wptr
= s
->rptr
= s
->count
= 0;
264 case ADB_CMD_SELF_TEST
:
266 case ADB_CMD_CHANGE_ID
:
267 case ADB_CMD_CHANGE_ID_AND_ACT
:
268 case ADB_CMD_CHANGE_ID_AND_ENABLE
:
269 d
->devaddr
= buf
[1] & 0xf;
272 /* XXX: check this */
273 d
->devaddr
= buf
[1] & 0xf;
282 olen
= adb_kbd_poll(d
, obuf
);
287 obuf
[0] = 0x00; /* XXX: check this */
288 obuf
[1] = 0x07; /* led status */
292 obuf
[0] = d
->handler
;
293 obuf
[1] = d
->devaddr
;
302 static const VMStateDescription vmstate_adb_kbd
= {
305 .minimum_version_id
= 1,
306 .minimum_version_id_old
= 1,
307 .fields
= (VMStateField
[]) {
308 VMSTATE_BUFFER(data
, KBDState
),
309 VMSTATE_INT32(rptr
, KBDState
),
310 VMSTATE_INT32(wptr
, KBDState
),
311 VMSTATE_INT32(count
, KBDState
),
312 VMSTATE_END_OF_LIST()
316 static void adb_kbd_reset(DeviceState
*dev
)
318 ADBDevice
*d
= ADB_DEVICE(dev
);
319 KBDState
*s
= ADB_KEYBOARD(dev
);
322 d
->devaddr
= ADB_DEVID_KEYBOARD
;
323 memset(s
->data
, 0, sizeof(s
->data
));
329 static void adb_kbd_realizefn(DeviceState
*dev
, Error
**errp
)
331 ADBDevice
*d
= ADB_DEVICE(dev
);
332 ADBKeyboardClass
*akc
= ADB_KEYBOARD_GET_CLASS(dev
);
334 akc
->parent_realize(dev
, errp
);
336 qemu_add_kbd_event_handler(adb_kbd_put_keycode
, d
);
339 static void adb_kbd_initfn(Object
*obj
)
341 ADBDevice
*d
= ADB_DEVICE(obj
);
343 d
->devaddr
= ADB_DEVID_KEYBOARD
;
346 static void adb_kbd_class_init(ObjectClass
*oc
, void *data
)
348 DeviceClass
*dc
= DEVICE_CLASS(oc
);
349 ADBDeviceClass
*adc
= ADB_DEVICE_CLASS(oc
);
350 ADBKeyboardClass
*akc
= ADB_KEYBOARD_CLASS(oc
);
352 akc
->parent_realize
= dc
->realize
;
353 dc
->realize
= adb_kbd_realizefn
;
355 adc
->devreq
= adb_kbd_request
;
356 dc
->reset
= adb_kbd_reset
;
357 dc
->vmsd
= &vmstate_adb_kbd
;
360 static const TypeInfo adb_kbd_type_info
= {
361 .name
= TYPE_ADB_KEYBOARD
,
362 .parent
= TYPE_ADB_DEVICE
,
363 .instance_size
= sizeof(KBDState
),
364 .instance_init
= adb_kbd_initfn
,
365 .class_init
= adb_kbd_class_init
,
366 .class_size
= sizeof(ADBKeyboardClass
),
369 /***************************************************************/
370 /* Mouse ADB device */
372 #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE)
374 typedef struct MouseState
{
376 ADBDevice parent_obj
;
379 int buttons_state
, last_buttons_state
;
383 #define ADB_MOUSE_CLASS(class) \
384 OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE)
385 #define ADB_MOUSE_GET_CLASS(obj) \
386 OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE)
388 typedef struct ADBMouseClass
{
390 ADBDeviceClass parent_class
;
393 DeviceRealize parent_realize
;
396 static void adb_mouse_event(void *opaque
,
397 int dx1
, int dy1
, int dz1
, int buttons_state
)
399 MouseState
*s
= opaque
;
404 s
->buttons_state
= buttons_state
;
408 static int adb_mouse_poll(ADBDevice
*d
, uint8_t *obuf
)
410 MouseState
*s
= ADB_MOUSE(d
);
413 if (s
->last_buttons_state
== s
->buttons_state
&&
414 s
->dx
== 0 && s
->dy
== 0)
431 s
->last_buttons_state
= s
->buttons_state
;
436 if (!(s
->buttons_state
& MOUSE_EVENT_LBUTTON
))
438 if (!(s
->buttons_state
& MOUSE_EVENT_RBUTTON
))
446 static int adb_mouse_request(ADBDevice
*d
, uint8_t *obuf
,
447 const uint8_t *buf
, int len
)
449 MouseState
*s
= ADB_MOUSE(d
);
452 if ((buf
[0] & 0x0f) == ADB_FLUSH
) {
453 /* flush mouse fifo */
454 s
->buttons_state
= s
->last_buttons_state
;
466 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg
, buf
[1]);
472 case ADB_CMD_SELF_TEST
:
474 case ADB_CMD_CHANGE_ID
:
475 case ADB_CMD_CHANGE_ID_AND_ACT
:
476 case ADB_CMD_CHANGE_ID_AND_ENABLE
:
477 d
->devaddr
= buf
[1] & 0xf;
480 /* XXX: check this */
481 d
->devaddr
= buf
[1] & 0xf;
489 olen
= adb_mouse_poll(d
, obuf
);
494 obuf
[0] = d
->handler
;
495 obuf
[1] = d
->devaddr
;
499 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg
,
506 static void adb_mouse_reset(DeviceState
*dev
)
508 ADBDevice
*d
= ADB_DEVICE(dev
);
509 MouseState
*s
= ADB_MOUSE(dev
);
512 d
->devaddr
= ADB_DEVID_MOUSE
;
513 s
->last_buttons_state
= s
->buttons_state
= 0;
514 s
->dx
= s
->dy
= s
->dz
= 0;
517 static const VMStateDescription vmstate_adb_mouse
= {
520 .minimum_version_id
= 1,
521 .minimum_version_id_old
= 1,
522 .fields
= (VMStateField
[]) {
523 VMSTATE_INT32(buttons_state
, MouseState
),
524 VMSTATE_INT32(last_buttons_state
, MouseState
),
525 VMSTATE_INT32(dx
, MouseState
),
526 VMSTATE_INT32(dy
, MouseState
),
527 VMSTATE_INT32(dz
, MouseState
),
528 VMSTATE_END_OF_LIST()
532 static void adb_mouse_realizefn(DeviceState
*dev
, Error
**errp
)
534 MouseState
*s
= ADB_MOUSE(dev
);
535 ADBMouseClass
*amc
= ADB_MOUSE_GET_CLASS(dev
);
537 amc
->parent_realize(dev
, errp
);
539 qemu_add_mouse_event_handler(adb_mouse_event
, s
, 0, "QEMU ADB Mouse");
542 static void adb_mouse_initfn(Object
*obj
)
544 ADBDevice
*d
= ADB_DEVICE(obj
);
546 d
->devaddr
= ADB_DEVID_MOUSE
;
549 static void adb_mouse_class_init(ObjectClass
*oc
, void *data
)
551 DeviceClass
*dc
= DEVICE_CLASS(oc
);
552 ADBDeviceClass
*adc
= ADB_DEVICE_CLASS(oc
);
553 ADBMouseClass
*amc
= ADB_MOUSE_CLASS(oc
);
555 amc
->parent_realize
= dc
->realize
;
556 dc
->realize
= adb_mouse_realizefn
;
558 adc
->devreq
= adb_mouse_request
;
559 dc
->reset
= adb_mouse_reset
;
560 dc
->vmsd
= &vmstate_adb_mouse
;
563 static const TypeInfo adb_mouse_type_info
= {
564 .name
= TYPE_ADB_MOUSE
,
565 .parent
= TYPE_ADB_DEVICE
,
566 .instance_size
= sizeof(MouseState
),
567 .instance_init
= adb_mouse_initfn
,
568 .class_init
= adb_mouse_class_init
,
569 .class_size
= sizeof(ADBMouseClass
),
573 static void adb_register_types(void)
575 type_register_static(&adb_bus_type_info
);
576 type_register_static(&adb_device_type_info
);
577 type_register_static(&adb_kbd_type_info
);
578 type_register_static(&adb_mouse_type_info
);
581 type_init(adb_register_types
)