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
27 #define ADB_BUSRESET 0x00
28 #define ADB_FLUSH 0x01
29 #define ADB_WRITEREG 0x08
30 #define ADB_READREG 0x0c
32 /* ADB device commands */
33 #define ADB_CMD_SELF_TEST 0xff
34 #define ADB_CMD_CHANGE_ID 0xfe
35 #define ADB_CMD_CHANGE_ID_AND_ACT 0xfd
36 #define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00
38 /* ADB default device IDs (upper 4 bits of ADB command byte) */
40 #define ADB_KEYBOARD 2
46 int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
52 devaddr = buf[0] >> 4;
53 if (buf[1] == ADB_BUSRESET) {
58 if (cmd == ADB_FLUSH) {
64 for(i = 0; i < s->nb_devices; i++) {
66 if (d->devaddr == devaddr) {
67 return d->devreq(d, obuf, buf, len);
73 int adb_poll(ADBBusState *s, uint8_t *obuf)
79 for(i = 0; i < s->nb_devices; i++) {
80 if (s->poll_index >= s->nb_devices)
82 d = &s->devices[s->poll_index];
83 olen = d->devreq(d, obuf, NULL, 0);
91 ADBDevice *adb_register_device(ADBBusState *s, int devaddr,
92 ADBDeviceRequest *devreq,
96 if (s->nb_devices >= MAX_ADB_DEVICES)
98 d = &s->devices[s->nb_devices++];
100 d->devaddr = devaddr;
106 /***************************************************************/
107 /* Keyboard ADB device */
109 typedef struct KBDState {
111 int rptr, wptr, count;
114 static const uint8_t pc_to_adb_keycode[256] = {
115 0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48,
116 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54, 0, 1,
117 2, 3, 5, 4, 38, 40, 37, 41, 39, 50, 56, 42, 6, 7, 8, 9,
118 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96,
119 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83,
120 84, 85, 82, 65, 0, 0, 10,103,111, 0, 0,110, 81, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123 0, 0, 0, 94, 0, 93, 0, 0, 0, 0, 0, 0,104,102, 0, 0,
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76,125, 0, 0,
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,105, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 75, 0, 0,124, 0, 0, 0, 0, 0, 0, 0,
127 0, 0, 0, 0, 0, 0, 0,115, 62,116, 0, 59, 0, 60, 0,119,
128 61,121,114,117, 0, 0, 0, 0, 0, 0, 0, 55,126, 0,127, 0,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
133 static void adb_kbd_put_keycode(void *opaque, int keycode)
135 ADBDevice *d = opaque;
136 KBDState *s = d->opaque;
138 if (s->count < sizeof(s->data)) {
139 s->data[s->wptr] = keycode;
140 if (++s->wptr == sizeof(s->data))
146 static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
148 static int ext_keycode;
149 KBDState *s = d->opaque;
150 int adb_keycode, keycode;
157 keycode = s->data[s->rptr];
158 if (++s->rptr == sizeof(s->data))
162 if (keycode == 0xe0) {
166 adb_keycode = pc_to_adb_keycode[keycode | 0x80];
168 adb_keycode = pc_to_adb_keycode[keycode & 0x7f];
169 obuf[0] = (d->devaddr << 4) | 0x0c;
170 obuf[1] = adb_keycode | (keycode & 0x80);
180 static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
181 const uint8_t *buf, int len)
186 return adb_kbd_poll(d, obuf);
200 case ADB_CMD_SELF_TEST:
202 case ADB_CMD_CHANGE_ID:
203 case ADB_CMD_CHANGE_ID_AND_ACT:
204 case ADB_CMD_CHANGE_ID_AND_ENABLE:
205 d->devaddr = buf[1] & 0xf;
208 /* XXX: check this */
209 d->devaddr = buf[1] & 0xf;
220 obuf[0] = 0x00; /* XXX: check this */
221 obuf[1] = 0x07; /* led status */
225 obuf[0] = d->handler;
226 obuf[1] = d->devaddr;
235 void adb_kbd_init(ADBBusState *bus)
239 s = qemu_mallocz(sizeof(KBDState));
240 d = adb_register_device(bus, ADB_KEYBOARD, adb_kbd_request, s);
242 qemu_add_kbd_event_handler(adb_kbd_put_keycode, d);
245 /***************************************************************/
246 /* Mouse ADB device */
248 typedef struct MouseState {
249 int buttons_state, last_buttons_state;
253 static void adb_mouse_event(void *opaque,
254 int dx1, int dy1, int dz1, int buttons_state)
256 ADBDevice *d = opaque;
257 MouseState *s = d->opaque;
262 s->buttons_state = buttons_state;
266 static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
268 MouseState *s = d->opaque;
271 if (s->last_buttons_state == s->buttons_state &&
272 s->dx == 0 && s->dy == 0)
289 s->last_buttons_state = s->buttons_state;
294 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
296 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
299 obuf[0] = (d->devaddr << 4) | 0x0c;
305 static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
306 const uint8_t *buf, int len)
311 return adb_mouse_poll(d, obuf);
324 case ADB_CMD_SELF_TEST:
326 case ADB_CMD_CHANGE_ID:
327 case ADB_CMD_CHANGE_ID_AND_ACT:
328 case ADB_CMD_CHANGE_ID_AND_ENABLE:
329 d->devaddr = buf[1] & 0xf;
332 /* XXX: check this */
333 d->devaddr = buf[1] & 0xf;
343 obuf[0] = d->handler;
344 obuf[1] = d->devaddr;
353 void adb_mouse_init(ADBBusState *bus)
358 s = qemu_mallocz(sizeof(MouseState));
359 d = adb_register_device(bus, ADB_MOUSE, adb_mouse_request, s);
361 qemu_add_mouse_event_handler(adb_mouse_event, d);