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
25 #include "hw/input/adb.h"
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 const VMStateDescription vmstate_adb_device
= {
122 .name
= "adb_device",
124 .minimum_version_id
= 0,
125 .fields
= (VMStateField
[]) {
126 VMSTATE_INT32(devaddr
, ADBDevice
),
127 VMSTATE_INT32(handler
, ADBDevice
),
128 VMSTATE_END_OF_LIST()
132 static void adb_device_realizefn(DeviceState
*dev
, Error
**errp
)
134 ADBDevice
*d
= ADB_DEVICE(dev
);
135 ADBBusState
*bus
= ADB_BUS(qdev_get_parent_bus(dev
));
137 if (bus
->nb_devices
>= MAX_ADB_DEVICES
) {
141 bus
->devices
[bus
->nb_devices
++] = d
;
144 static void adb_device_class_init(ObjectClass
*oc
, void *data
)
146 DeviceClass
*dc
= DEVICE_CLASS(oc
);
148 dc
->realize
= adb_device_realizefn
;
149 dc
->bus_type
= TYPE_ADB_BUS
;
152 static const TypeInfo adb_device_type_info
= {
153 .name
= TYPE_ADB_DEVICE
,
154 .parent
= TYPE_DEVICE
,
155 .instance_size
= sizeof(ADBDevice
),
157 .class_init
= adb_device_class_init
,
160 /***************************************************************/
161 /* Keyboard ADB device */
163 #define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD)
165 typedef struct KBDState
{
167 ADBDevice parent_obj
;
171 int rptr
, wptr
, count
;
174 #define ADB_KEYBOARD_CLASS(class) \
175 OBJECT_CLASS_CHECK(ADBKeyboardClass, (class), TYPE_ADB_KEYBOARD)
176 #define ADB_KEYBOARD_GET_CLASS(obj) \
177 OBJECT_GET_CLASS(ADBKeyboardClass, (obj), TYPE_ADB_KEYBOARD)
179 typedef struct ADBKeyboardClass
{
181 ADBDeviceClass parent_class
;
184 DeviceRealize parent_realize
;
187 static const uint8_t pc_to_adb_keycode
[256] = {
188 0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48,
189 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54, 0, 1,
190 2, 3, 5, 4, 38, 40, 37, 41, 39, 50, 56, 42, 6, 7, 8, 9,
191 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96,
192 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83,
193 84, 85, 82, 65, 0, 0, 10,103,111, 0, 0,110, 81, 0, 0, 0,
194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196 0, 0, 0, 94, 0, 93, 0, 0, 0, 0, 0, 0,104,102, 0, 0,
197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76,125, 0, 0,
198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,105, 0, 0, 0, 0, 0,
199 0, 0, 0, 0, 0, 75, 0, 0,124, 0, 0, 0, 0, 0, 0, 0,
200 0, 0, 0, 0, 0, 0, 0,115, 62,116, 0, 59, 0, 60, 0,119,
201 61,121,114,117, 0, 0, 0, 0, 0, 0, 0, 55,126, 0,127, 0,
202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203 0, 0, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206 static void adb_kbd_put_keycode(void *opaque
, int keycode
)
208 KBDState
*s
= opaque
;
210 if (s
->count
< sizeof(s
->data
)) {
211 s
->data
[s
->wptr
] = keycode
;
212 if (++s
->wptr
== sizeof(s
->data
))
218 static int adb_kbd_poll(ADBDevice
*d
, uint8_t *obuf
)
220 static int ext_keycode
;
221 KBDState
*s
= ADB_KEYBOARD(d
);
222 int adb_keycode
, keycode
;
229 keycode
= s
->data
[s
->rptr
];
230 if (++s
->rptr
== sizeof(s
->data
))
234 if (keycode
== 0xe0) {
238 adb_keycode
= pc_to_adb_keycode
[keycode
| 0x80];
240 adb_keycode
= pc_to_adb_keycode
[keycode
& 0x7f];
241 obuf
[0] = adb_keycode
| (keycode
& 0x80);
242 /* NOTE: could put a second keycode if needed */
252 static int adb_kbd_request(ADBDevice
*d
, uint8_t *obuf
,
253 const uint8_t *buf
, int len
)
255 KBDState
*s
= ADB_KEYBOARD(d
);
258 if ((buf
[0] & 0x0f) == ADB_FLUSH
) {
259 /* flush keyboard fifo */
260 s
->wptr
= s
->rptr
= s
->count
= 0;
275 case ADB_CMD_SELF_TEST
:
277 case ADB_CMD_CHANGE_ID
:
278 case ADB_CMD_CHANGE_ID_AND_ACT
:
279 case ADB_CMD_CHANGE_ID_AND_ENABLE
:
280 d
->devaddr
= buf
[1] & 0xf;
283 /* XXX: check this */
284 d
->devaddr
= buf
[1] & 0xf;
293 olen
= adb_kbd_poll(d
, obuf
);
298 obuf
[0] = 0x00; /* XXX: check this */
299 obuf
[1] = 0x07; /* led status */
303 obuf
[0] = d
->handler
;
304 obuf
[1] = d
->devaddr
;
313 static const VMStateDescription vmstate_adb_kbd
= {
316 .minimum_version_id
= 2,
317 .fields
= (VMStateField
[]) {
318 VMSTATE_STRUCT(parent_obj
, KBDState
, 0, vmstate_adb_device
, ADBDevice
),
319 VMSTATE_BUFFER(data
, KBDState
),
320 VMSTATE_INT32(rptr
, KBDState
),
321 VMSTATE_INT32(wptr
, KBDState
),
322 VMSTATE_INT32(count
, KBDState
),
323 VMSTATE_END_OF_LIST()
327 static void adb_kbd_reset(DeviceState
*dev
)
329 ADBDevice
*d
= ADB_DEVICE(dev
);
330 KBDState
*s
= ADB_KEYBOARD(dev
);
333 d
->devaddr
= ADB_DEVID_KEYBOARD
;
334 memset(s
->data
, 0, sizeof(s
->data
));
340 static void adb_kbd_realizefn(DeviceState
*dev
, Error
**errp
)
342 ADBDevice
*d
= ADB_DEVICE(dev
);
343 ADBKeyboardClass
*akc
= ADB_KEYBOARD_GET_CLASS(dev
);
345 akc
->parent_realize(dev
, errp
);
347 qemu_add_kbd_event_handler(adb_kbd_put_keycode
, d
);
350 static void adb_kbd_initfn(Object
*obj
)
352 ADBDevice
*d
= ADB_DEVICE(obj
);
354 d
->devaddr
= ADB_DEVID_KEYBOARD
;
357 static void adb_kbd_class_init(ObjectClass
*oc
, void *data
)
359 DeviceClass
*dc
= DEVICE_CLASS(oc
);
360 ADBDeviceClass
*adc
= ADB_DEVICE_CLASS(oc
);
361 ADBKeyboardClass
*akc
= ADB_KEYBOARD_CLASS(oc
);
363 akc
->parent_realize
= dc
->realize
;
364 dc
->realize
= adb_kbd_realizefn
;
366 adc
->devreq
= adb_kbd_request
;
367 dc
->reset
= adb_kbd_reset
;
368 dc
->vmsd
= &vmstate_adb_kbd
;
371 static const TypeInfo adb_kbd_type_info
= {
372 .name
= TYPE_ADB_KEYBOARD
,
373 .parent
= TYPE_ADB_DEVICE
,
374 .instance_size
= sizeof(KBDState
),
375 .instance_init
= adb_kbd_initfn
,
376 .class_init
= adb_kbd_class_init
,
377 .class_size
= sizeof(ADBKeyboardClass
),
380 /***************************************************************/
381 /* Mouse ADB device */
383 #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE)
385 typedef struct MouseState
{
387 ADBDevice parent_obj
;
390 int buttons_state
, last_buttons_state
;
394 #define ADB_MOUSE_CLASS(class) \
395 OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE)
396 #define ADB_MOUSE_GET_CLASS(obj) \
397 OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE)
399 typedef struct ADBMouseClass
{
401 ADBDeviceClass parent_class
;
404 DeviceRealize parent_realize
;
407 static void adb_mouse_event(void *opaque
,
408 int dx1
, int dy1
, int dz1
, int buttons_state
)
410 MouseState
*s
= opaque
;
415 s
->buttons_state
= buttons_state
;
419 static int adb_mouse_poll(ADBDevice
*d
, uint8_t *obuf
)
421 MouseState
*s
= ADB_MOUSE(d
);
424 if (s
->last_buttons_state
== s
->buttons_state
&&
425 s
->dx
== 0 && s
->dy
== 0)
442 s
->last_buttons_state
= s
->buttons_state
;
447 if (!(s
->buttons_state
& MOUSE_EVENT_LBUTTON
))
449 if (!(s
->buttons_state
& MOUSE_EVENT_RBUTTON
))
457 static int adb_mouse_request(ADBDevice
*d
, uint8_t *obuf
,
458 const uint8_t *buf
, int len
)
460 MouseState
*s
= ADB_MOUSE(d
);
463 if ((buf
[0] & 0x0f) == ADB_FLUSH
) {
464 /* flush mouse fifo */
465 s
->buttons_state
= s
->last_buttons_state
;
477 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg
, buf
[1]);
483 case ADB_CMD_SELF_TEST
:
485 case ADB_CMD_CHANGE_ID
:
486 case ADB_CMD_CHANGE_ID_AND_ACT
:
487 case ADB_CMD_CHANGE_ID_AND_ENABLE
:
488 d
->devaddr
= buf
[1] & 0xf;
491 /* XXX: check this */
492 d
->devaddr
= buf
[1] & 0xf;
500 olen
= adb_mouse_poll(d
, obuf
);
505 obuf
[0] = d
->handler
;
506 obuf
[1] = d
->devaddr
;
510 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg
,
517 static void adb_mouse_reset(DeviceState
*dev
)
519 ADBDevice
*d
= ADB_DEVICE(dev
);
520 MouseState
*s
= ADB_MOUSE(dev
);
523 d
->devaddr
= ADB_DEVID_MOUSE
;
524 s
->last_buttons_state
= s
->buttons_state
= 0;
525 s
->dx
= s
->dy
= s
->dz
= 0;
528 static const VMStateDescription vmstate_adb_mouse
= {
531 .minimum_version_id
= 2,
532 .fields
= (VMStateField
[]) {
533 VMSTATE_STRUCT(parent_obj
, MouseState
, 0, vmstate_adb_device
,
535 VMSTATE_INT32(buttons_state
, MouseState
),
536 VMSTATE_INT32(last_buttons_state
, MouseState
),
537 VMSTATE_INT32(dx
, MouseState
),
538 VMSTATE_INT32(dy
, MouseState
),
539 VMSTATE_INT32(dz
, MouseState
),
540 VMSTATE_END_OF_LIST()
544 static void adb_mouse_realizefn(DeviceState
*dev
, Error
**errp
)
546 MouseState
*s
= ADB_MOUSE(dev
);
547 ADBMouseClass
*amc
= ADB_MOUSE_GET_CLASS(dev
);
549 amc
->parent_realize(dev
, errp
);
551 qemu_add_mouse_event_handler(adb_mouse_event
, s
, 0, "QEMU ADB Mouse");
554 static void adb_mouse_initfn(Object
*obj
)
556 ADBDevice
*d
= ADB_DEVICE(obj
);
558 d
->devaddr
= ADB_DEVID_MOUSE
;
561 static void adb_mouse_class_init(ObjectClass
*oc
, void *data
)
563 DeviceClass
*dc
= DEVICE_CLASS(oc
);
564 ADBDeviceClass
*adc
= ADB_DEVICE_CLASS(oc
);
565 ADBMouseClass
*amc
= ADB_MOUSE_CLASS(oc
);
567 amc
->parent_realize
= dc
->realize
;
568 dc
->realize
= adb_mouse_realizefn
;
570 adc
->devreq
= adb_mouse_request
;
571 dc
->reset
= adb_mouse_reset
;
572 dc
->vmsd
= &vmstate_adb_mouse
;
575 static const TypeInfo adb_mouse_type_info
= {
576 .name
= TYPE_ADB_MOUSE
,
577 .parent
= TYPE_ADB_DEVICE
,
578 .instance_size
= sizeof(MouseState
),
579 .instance_init
= adb_mouse_initfn
,
580 .class_init
= adb_mouse_class_init
,
581 .class_size
= sizeof(ADBMouseClass
),
585 static void adb_register_types(void)
587 type_register_static(&adb_bus_type_info
);
588 type_register_static(&adb_device_type_info
);
589 type_register_static(&adb_kbd_type_info
);
590 type_register_static(&adb_mouse_type_info
);
593 type_init(adb_register_types
)