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
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) */
52 #define ADB_KEYBOARD 2
59 #define ADB_RET_NOTPRESENT (-2)
61 int adb_request(ADBBusState
*s
, uint8_t *obuf
, const uint8_t *buf
, int len
)
67 if (cmd
== ADB_BUSRESET
) {
68 for(i
= 0; i
< s
->nb_devices
; i
++) {
76 devaddr
= buf
[0] >> 4;
77 for(i
= 0; i
< s
->nb_devices
; i
++) {
79 if (d
->devaddr
== devaddr
) {
80 return d
->devreq(d
, obuf
, buf
, len
);
83 return ADB_RET_NOTPRESENT
;
86 /* XXX: move that to cuda ? */
87 int adb_poll(ADBBusState
*s
, uint8_t *obuf
)
94 for(i
= 0; i
< s
->nb_devices
; i
++) {
95 if (s
->poll_index
>= s
->nb_devices
)
97 d
= &s
->devices
[s
->poll_index
];
98 buf
[0] = ADB_READREG
| (d
->devaddr
<< 4);
99 olen
= adb_request(s
, obuf
+ 1, buf
, 1);
100 /* if there is data, we poll again the same device */
111 ADBDevice
*adb_register_device(ADBBusState
*s
, int devaddr
,
112 ADBDeviceRequest
*devreq
,
113 ADBDeviceReset
*devreset
,
117 if (s
->nb_devices
>= MAX_ADB_DEVICES
)
119 d
= &s
->devices
[s
->nb_devices
++];
121 d
->devaddr
= devaddr
;
123 d
->devreset
= devreset
;
125 qemu_register_reset((QEMUResetHandler
*)devreset
, d
);
130 /***************************************************************/
131 /* Keyboard ADB device */
133 typedef struct KBDState
{
135 int rptr
, wptr
, count
;
138 static const uint8_t pc_to_adb_keycode
[256] = {
139 0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48,
140 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54, 0, 1,
141 2, 3, 5, 4, 38, 40, 37, 41, 39, 50, 56, 42, 6, 7, 8, 9,
142 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96,
143 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83,
144 84, 85, 82, 65, 0, 0, 10,103,111, 0, 0,110, 81, 0, 0, 0,
145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 94, 0, 93, 0, 0, 0, 0, 0, 0,104,102, 0, 0,
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76,125, 0, 0,
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,105, 0, 0, 0, 0, 0,
150 0, 0, 0, 0, 0, 75, 0, 0,124, 0, 0, 0, 0, 0, 0, 0,
151 0, 0, 0, 0, 0, 0, 0,115, 62,116, 0, 59, 0, 60, 0,119,
152 61,121,114,117, 0, 0, 0, 0, 0, 0, 0, 55,126, 0,127, 0,
153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
154 0, 0, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 static void adb_kbd_put_keycode(void *opaque
, int keycode
)
159 ADBDevice
*d
= opaque
;
160 KBDState
*s
= d
->opaque
;
162 if (s
->count
< sizeof(s
->data
)) {
163 s
->data
[s
->wptr
] = keycode
;
164 if (++s
->wptr
== sizeof(s
->data
))
170 static int adb_kbd_poll(ADBDevice
*d
, uint8_t *obuf
)
172 static int ext_keycode
;
173 KBDState
*s
= d
->opaque
;
174 int adb_keycode
, keycode
;
181 keycode
= s
->data
[s
->rptr
];
182 if (++s
->rptr
== sizeof(s
->data
))
186 if (keycode
== 0xe0) {
190 adb_keycode
= pc_to_adb_keycode
[keycode
| 0x80];
192 adb_keycode
= pc_to_adb_keycode
[keycode
& 0x7f];
193 obuf
[0] = adb_keycode
| (keycode
& 0x80);
194 /* NOTE: could put a second keycode if needed */
204 static int adb_kbd_request(ADBDevice
*d
, uint8_t *obuf
,
205 const uint8_t *buf
, int len
)
207 KBDState
*s
= d
->opaque
;
210 if ((buf
[0] & 0x0f) == ADB_FLUSH
) {
211 /* flush keyboard fifo */
212 s
->wptr
= s
->rptr
= s
->count
= 0;
227 case ADB_CMD_SELF_TEST
:
229 case ADB_CMD_CHANGE_ID
:
230 case ADB_CMD_CHANGE_ID_AND_ACT
:
231 case ADB_CMD_CHANGE_ID_AND_ENABLE
:
232 d
->devaddr
= buf
[1] & 0xf;
235 /* XXX: check this */
236 d
->devaddr
= buf
[1] & 0xf;
245 olen
= adb_kbd_poll(d
, obuf
);
250 obuf
[0] = 0x00; /* XXX: check this */
251 obuf
[1] = 0x07; /* led status */
255 obuf
[0] = d
->handler
;
256 obuf
[1] = d
->devaddr
;
265 static void adb_kbd_save(QEMUFile
*f
, void *opaque
)
267 KBDState
*s
= (KBDState
*)opaque
;
269 qemu_put_buffer(f
, s
->data
, sizeof(s
->data
));
270 qemu_put_sbe32s(f
, &s
->rptr
);
271 qemu_put_sbe32s(f
, &s
->wptr
);
272 qemu_put_sbe32s(f
, &s
->count
);
275 static int adb_kbd_load(QEMUFile
*f
, void *opaque
, int version_id
)
277 KBDState
*s
= (KBDState
*)opaque
;
282 qemu_get_buffer(f
, s
->data
, sizeof(s
->data
));
283 qemu_get_sbe32s(f
, &s
->rptr
);
284 qemu_get_sbe32s(f
, &s
->wptr
);
285 qemu_get_sbe32s(f
, &s
->count
);
290 static int adb_kbd_reset(ADBDevice
*d
)
292 KBDState
*s
= d
->opaque
;
295 d
->devaddr
= ADB_KEYBOARD
;
296 memset(s
, 0, sizeof(KBDState
));
301 void adb_kbd_init(ADBBusState
*bus
)
305 s
= qemu_mallocz(sizeof(KBDState
));
306 d
= adb_register_device(bus
, ADB_KEYBOARD
, adb_kbd_request
,
308 qemu_add_kbd_event_handler(adb_kbd_put_keycode
, d
);
309 register_savevm("adb_kbd", -1, 1, adb_kbd_save
,
313 /***************************************************************/
314 /* Mouse ADB device */
316 typedef struct MouseState
{
317 int buttons_state
, last_buttons_state
;
321 static void adb_mouse_event(void *opaque
,
322 int dx1
, int dy1
, int dz1
, int buttons_state
)
324 ADBDevice
*d
= opaque
;
325 MouseState
*s
= d
->opaque
;
330 s
->buttons_state
= buttons_state
;
334 static int adb_mouse_poll(ADBDevice
*d
, uint8_t *obuf
)
336 MouseState
*s
= d
->opaque
;
339 if (s
->last_buttons_state
== s
->buttons_state
&&
340 s
->dx
== 0 && s
->dy
== 0)
357 s
->last_buttons_state
= s
->buttons_state
;
362 if (!(s
->buttons_state
& MOUSE_EVENT_LBUTTON
))
364 if (!(s
->buttons_state
& MOUSE_EVENT_RBUTTON
))
372 static int adb_mouse_request(ADBDevice
*d
, uint8_t *obuf
,
373 const uint8_t *buf
, int len
)
375 MouseState
*s
= d
->opaque
;
378 if ((buf
[0] & 0x0f) == ADB_FLUSH
) {
379 /* flush mouse fifo */
380 s
->buttons_state
= s
->last_buttons_state
;
392 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg
, buf
[1]);
398 case ADB_CMD_SELF_TEST
:
400 case ADB_CMD_CHANGE_ID
:
401 case ADB_CMD_CHANGE_ID_AND_ACT
:
402 case ADB_CMD_CHANGE_ID_AND_ENABLE
:
403 d
->devaddr
= buf
[1] & 0xf;
406 /* XXX: check this */
407 d
->devaddr
= buf
[1] & 0xf;
415 olen
= adb_mouse_poll(d
, obuf
);
420 obuf
[0] = d
->handler
;
421 obuf
[1] = d
->devaddr
;
425 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg
,
432 static int adb_mouse_reset(ADBDevice
*d
)
434 MouseState
*s
= d
->opaque
;
437 d
->devaddr
= ADB_MOUSE
;
438 memset(s
, 0, sizeof(MouseState
));
443 static void adb_mouse_save(QEMUFile
*f
, void *opaque
)
445 MouseState
*s
= (MouseState
*)opaque
;
447 qemu_put_sbe32s(f
, &s
->buttons_state
);
448 qemu_put_sbe32s(f
, &s
->last_buttons_state
);
449 qemu_put_sbe32s(f
, &s
->dx
);
450 qemu_put_sbe32s(f
, &s
->dy
);
451 qemu_put_sbe32s(f
, &s
->dz
);
454 static int adb_mouse_load(QEMUFile
*f
, void *opaque
, int version_id
)
456 MouseState
*s
= (MouseState
*)opaque
;
461 qemu_get_sbe32s(f
, &s
->buttons_state
);
462 qemu_get_sbe32s(f
, &s
->last_buttons_state
);
463 qemu_get_sbe32s(f
, &s
->dx
);
464 qemu_get_sbe32s(f
, &s
->dy
);
465 qemu_get_sbe32s(f
, &s
->dz
);
470 void adb_mouse_init(ADBBusState
*bus
)
475 s
= qemu_mallocz(sizeof(MouseState
));
476 d
= adb_register_device(bus
, ADB_MOUSE
, adb_mouse_request
,
478 qemu_add_mouse_event_handler(adb_mouse_event
, d
, 0, "QEMU ADB Mouse");
479 register_savevm("adb_mouse", -1, 1, adb_mouse_save
,