root / hw / adb.c @ 124964b5
History | View | Annotate | Download (12.3 kB)
1 | 267002cd | bellard | /*
|
---|---|---|---|
2 | 267002cd | bellard | * QEMU ADB support
|
3 | 5fafdf24 | ths | *
|
4 | 267002cd | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 267002cd | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 267002cd | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 267002cd | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 267002cd | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 267002cd | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 267002cd | bellard | * furnished to do so, subject to the following conditions:
|
12 | 267002cd | bellard | *
|
13 | 267002cd | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 267002cd | bellard | * all copies or substantial portions of the Software.
|
15 | 267002cd | bellard | *
|
16 | 267002cd | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 267002cd | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 267002cd | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 267002cd | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 267002cd | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 267002cd | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 267002cd | bellard | * THE SOFTWARE.
|
23 | 267002cd | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "ppc_mac.h" |
26 | 87ecb68b | pbrook | #include "console.h" |
27 | 267002cd | bellard | |
28 | ea026b2f | blueswir1 | /* debug ADB */
|
29 | ea026b2f | blueswir1 | //#define DEBUG_ADB
|
30 | ea026b2f | blueswir1 | |
31 | ea026b2f | blueswir1 | #ifdef DEBUG_ADB
|
32 | 001faf32 | Blue Swirl | #define ADB_DPRINTF(fmt, ...) \
|
33 | 001faf32 | Blue Swirl | do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0) |
34 | ea026b2f | blueswir1 | #else
|
35 | 001faf32 | Blue Swirl | #define ADB_DPRINTF(fmt, ...)
|
36 | ea026b2f | blueswir1 | #endif
|
37 | ea026b2f | blueswir1 | |
38 | 267002cd | bellard | /* ADB commands */
|
39 | 267002cd | bellard | #define ADB_BUSRESET 0x00 |
40 | 267002cd | bellard | #define ADB_FLUSH 0x01 |
41 | 267002cd | bellard | #define ADB_WRITEREG 0x08 |
42 | 267002cd | bellard | #define ADB_READREG 0x0c |
43 | 267002cd | bellard | |
44 | 267002cd | bellard | /* ADB device commands */
|
45 | 267002cd | bellard | #define ADB_CMD_SELF_TEST 0xff |
46 | 267002cd | bellard | #define ADB_CMD_CHANGE_ID 0xfe |
47 | 267002cd | bellard | #define ADB_CMD_CHANGE_ID_AND_ACT 0xfd |
48 | 267002cd | bellard | #define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00 |
49 | 267002cd | bellard | |
50 | 267002cd | bellard | /* ADB default device IDs (upper 4 bits of ADB command byte) */
|
51 | 267002cd | bellard | #define ADB_DONGLE 1 |
52 | 267002cd | bellard | #define ADB_KEYBOARD 2 |
53 | 267002cd | bellard | #define ADB_MOUSE 3 |
54 | 267002cd | bellard | #define ADB_TABLET 4 |
55 | 267002cd | bellard | #define ADB_MODEM 5 |
56 | 267002cd | bellard | #define ADB_MISC 7 |
57 | 267002cd | bellard | |
58 | bec9d989 | bellard | /* error codes */
|
59 | bec9d989 | bellard | #define ADB_RET_NOTPRESENT (-2) |
60 | bec9d989 | bellard | |
61 | e2733d20 | bellard | int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len) |
62 | 267002cd | bellard | { |
63 | 267002cd | bellard | ADBDevice *d; |
64 | 267002cd | bellard | int devaddr, cmd, i;
|
65 | 267002cd | bellard | |
66 | 819e712b | bellard | cmd = buf[0] & 0xf; |
67 | bec9d989 | bellard | if (cmd == ADB_BUSRESET) {
|
68 | bec9d989 | bellard | for(i = 0; i < s->nb_devices; i++) { |
69 | bec9d989 | bellard | d = &s->devices[i]; |
70 | bec9d989 | bellard | if (d->devreset) {
|
71 | bec9d989 | bellard | d->devreset(d); |
72 | bec9d989 | bellard | } |
73 | bec9d989 | bellard | } |
74 | bec9d989 | bellard | return 0; |
75 | 267002cd | bellard | } |
76 | bec9d989 | bellard | devaddr = buf[0] >> 4; |
77 | 267002cd | bellard | for(i = 0; i < s->nb_devices; i++) { |
78 | 267002cd | bellard | d = &s->devices[i]; |
79 | 267002cd | bellard | if (d->devaddr == devaddr) {
|
80 | e2733d20 | bellard | return d->devreq(d, obuf, buf, len);
|
81 | 267002cd | bellard | } |
82 | 267002cd | bellard | } |
83 | bec9d989 | bellard | return ADB_RET_NOTPRESENT;
|
84 | e2733d20 | bellard | } |
85 | e2733d20 | bellard | |
86 | bec9d989 | bellard | /* XXX: move that to cuda ? */
|
87 | e2733d20 | bellard | int adb_poll(ADBBusState *s, uint8_t *obuf)
|
88 | e2733d20 | bellard | { |
89 | e2733d20 | bellard | ADBDevice *d; |
90 | e2733d20 | bellard | int olen, i;
|
91 | bec9d989 | bellard | uint8_t buf[1];
|
92 | e2733d20 | bellard | |
93 | e2733d20 | bellard | olen = 0;
|
94 | e2733d20 | bellard | for(i = 0; i < s->nb_devices; i++) { |
95 | e2733d20 | bellard | if (s->poll_index >= s->nb_devices)
|
96 | e2733d20 | bellard | s->poll_index = 0;
|
97 | e2733d20 | bellard | d = &s->devices[s->poll_index]; |
98 | bec9d989 | bellard | buf[0] = ADB_READREG | (d->devaddr << 4); |
99 | bec9d989 | bellard | olen = adb_request(s, obuf + 1, buf, 1); |
100 | bec9d989 | bellard | /* if there is data, we poll again the same device */
|
101 | bec9d989 | bellard | if (olen > 0) { |
102 | bec9d989 | bellard | obuf[0] = buf[0]; |
103 | bec9d989 | bellard | olen++; |
104 | e2733d20 | bellard | break;
|
105 | bec9d989 | bellard | } |
106 | bec9d989 | bellard | s->poll_index++; |
107 | e2733d20 | bellard | } |
108 | e2733d20 | bellard | return olen;
|
109 | 267002cd | bellard | } |
110 | 267002cd | bellard | |
111 | 5fafdf24 | ths | ADBDevice *adb_register_device(ADBBusState *s, int devaddr,
|
112 | 5fafdf24 | ths | ADBDeviceRequest *devreq, |
113 | 5fafdf24 | ths | ADBDeviceReset *devreset, |
114 | 267002cd | bellard | void *opaque)
|
115 | 267002cd | bellard | { |
116 | 267002cd | bellard | ADBDevice *d; |
117 | 267002cd | bellard | if (s->nb_devices >= MAX_ADB_DEVICES)
|
118 | 267002cd | bellard | return NULL; |
119 | 267002cd | bellard | d = &s->devices[s->nb_devices++]; |
120 | 267002cd | bellard | d->bus = s; |
121 | 267002cd | bellard | d->devaddr = devaddr; |
122 | e2733d20 | bellard | d->devreq = devreq; |
123 | bec9d989 | bellard | d->devreset = devreset; |
124 | 267002cd | bellard | d->opaque = opaque; |
125 | a08d4367 | Jan Kiszka | qemu_register_reset((QEMUResetHandler *)devreset, d); |
126 | 267002cd | bellard | return d;
|
127 | 267002cd | bellard | } |
128 | 267002cd | bellard | |
129 | 267002cd | bellard | /***************************************************************/
|
130 | 267002cd | bellard | /* Keyboard ADB device */
|
131 | 267002cd | bellard | |
132 | e2733d20 | bellard | typedef struct KBDState { |
133 | e2733d20 | bellard | uint8_t data[128];
|
134 | e2733d20 | bellard | int rptr, wptr, count;
|
135 | e2733d20 | bellard | } KBDState; |
136 | e2733d20 | bellard | |
137 | 267002cd | bellard | static const uint8_t pc_to_adb_keycode[256] = { |
138 | 267002cd | bellard | 0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48, |
139 | 267002cd | bellard | 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54, 0, 1, |
140 | 267002cd | bellard | 2, 3, 5, 4, 38, 40, 37, 41, 39, 50, 56, 42, 6, 7, 8, 9, |
141 | 267002cd | bellard | 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96, |
142 | 267002cd | bellard | 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83, |
143 | e2733d20 | bellard | 84, 85, 82, 65, 0, 0, 10,103,111, 0, 0,110, 81, 0, 0, 0, |
144 | 267002cd | bellard | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
145 | 267002cd | bellard | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
146 | e2733d20 | bellard | 0, 0, 0, 94, 0, 93, 0, 0, 0, 0, 0, 0,104,102, 0, 0, |
147 | e2733d20 | bellard | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76,125, 0, 0, |
148 | e2733d20 | bellard | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,105, 0, 0, 0, 0, 0, |
149 | e2733d20 | bellard | 0, 0, 0, 0, 0, 75, 0, 0,124, 0, 0, 0, 0, 0, 0, 0, |
150 | e2733d20 | bellard | 0, 0, 0, 0, 0, 0, 0,115, 62,116, 0, 59, 0, 60, 0,119, |
151 | e2733d20 | bellard | 61,121,114,117, 0, 0, 0, 0, 0, 0, 0, 55,126, 0,127, 0, |
152 | 267002cd | bellard | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
153 | e2733d20 | bellard | 0, 0, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
154 | 267002cd | bellard | }; |
155 | 267002cd | bellard | |
156 | 267002cd | bellard | static void adb_kbd_put_keycode(void *opaque, int keycode) |
157 | 267002cd | bellard | { |
158 | 267002cd | bellard | ADBDevice *d = opaque; |
159 | e2733d20 | bellard | KBDState *s = d->opaque; |
160 | e2733d20 | bellard | |
161 | e2733d20 | bellard | if (s->count < sizeof(s->data)) { |
162 | e2733d20 | bellard | s->data[s->wptr] = keycode; |
163 | e2733d20 | bellard | if (++s->wptr == sizeof(s->data)) |
164 | e2733d20 | bellard | s->wptr = 0;
|
165 | e2733d20 | bellard | s->count++; |
166 | 267002cd | bellard | } |
167 | 267002cd | bellard | } |
168 | 267002cd | bellard | |
169 | e2733d20 | bellard | static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf) |
170 | 267002cd | bellard | { |
171 | e2733d20 | bellard | static int ext_keycode; |
172 | e2733d20 | bellard | KBDState *s = d->opaque; |
173 | e2733d20 | bellard | int adb_keycode, keycode;
|
174 | e2733d20 | bellard | int olen;
|
175 | e2733d20 | bellard | |
176 | e2733d20 | bellard | olen = 0;
|
177 | e2733d20 | bellard | for(;;) {
|
178 | e2733d20 | bellard | if (s->count == 0) |
179 | e2733d20 | bellard | break;
|
180 | e2733d20 | bellard | keycode = s->data[s->rptr]; |
181 | e2733d20 | bellard | if (++s->rptr == sizeof(s->data)) |
182 | e2733d20 | bellard | s->rptr = 0;
|
183 | e2733d20 | bellard | s->count--; |
184 | e2733d20 | bellard | |
185 | e2733d20 | bellard | if (keycode == 0xe0) { |
186 | e2733d20 | bellard | ext_keycode = 1;
|
187 | e2733d20 | bellard | } else {
|
188 | e2733d20 | bellard | if (ext_keycode)
|
189 | e2733d20 | bellard | adb_keycode = pc_to_adb_keycode[keycode | 0x80];
|
190 | e2733d20 | bellard | else
|
191 | e2733d20 | bellard | adb_keycode = pc_to_adb_keycode[keycode & 0x7f];
|
192 | bec9d989 | bellard | obuf[0] = adb_keycode | (keycode & 0x80); |
193 | bec9d989 | bellard | /* NOTE: could put a second keycode if needed */
|
194 | bec9d989 | bellard | obuf[1] = 0xff; |
195 | bec9d989 | bellard | olen = 2;
|
196 | e2733d20 | bellard | ext_keycode = 0;
|
197 | e2733d20 | bellard | break;
|
198 | e2733d20 | bellard | } |
199 | e2733d20 | bellard | } |
200 | e2733d20 | bellard | return olen;
|
201 | e2733d20 | bellard | } |
202 | e2733d20 | bellard | |
203 | e2733d20 | bellard | static int adb_kbd_request(ADBDevice *d, uint8_t *obuf, |
204 | e2733d20 | bellard | const uint8_t *buf, int len) |
205 | e2733d20 | bellard | { |
206 | bec9d989 | bellard | KBDState *s = d->opaque; |
207 | e2733d20 | bellard | int cmd, reg, olen;
|
208 | e2733d20 | bellard | |
209 | bec9d989 | bellard | if ((buf[0] & 0x0f) == ADB_FLUSH) { |
210 | bec9d989 | bellard | /* flush keyboard fifo */
|
211 | bec9d989 | bellard | s->wptr = s->rptr = s->count = 0;
|
212 | bec9d989 | bellard | return 0; |
213 | e2733d20 | bellard | } |
214 | 267002cd | bellard | |
215 | 267002cd | bellard | cmd = buf[0] & 0xc; |
216 | 267002cd | bellard | reg = buf[0] & 0x3; |
217 | e2733d20 | bellard | olen = 0;
|
218 | 267002cd | bellard | switch(cmd) {
|
219 | 267002cd | bellard | case ADB_WRITEREG:
|
220 | 267002cd | bellard | switch(reg) {
|
221 | 267002cd | bellard | case 2: |
222 | 267002cd | bellard | /* LED status */
|
223 | 267002cd | bellard | break;
|
224 | 267002cd | bellard | case 3: |
225 | 267002cd | bellard | switch(buf[2]) { |
226 | 267002cd | bellard | case ADB_CMD_SELF_TEST:
|
227 | 267002cd | bellard | break;
|
228 | 267002cd | bellard | case ADB_CMD_CHANGE_ID:
|
229 | 267002cd | bellard | case ADB_CMD_CHANGE_ID_AND_ACT:
|
230 | 267002cd | bellard | case ADB_CMD_CHANGE_ID_AND_ENABLE:
|
231 | 267002cd | bellard | d->devaddr = buf[1] & 0xf; |
232 | 267002cd | bellard | break;
|
233 | 267002cd | bellard | default:
|
234 | 267002cd | bellard | /* XXX: check this */
|
235 | 267002cd | bellard | d->devaddr = buf[1] & 0xf; |
236 | 267002cd | bellard | d->handler = buf[2];
|
237 | 267002cd | bellard | break;
|
238 | 267002cd | bellard | } |
239 | 267002cd | bellard | } |
240 | 267002cd | bellard | break;
|
241 | 267002cd | bellard | case ADB_READREG:
|
242 | 267002cd | bellard | switch(reg) {
|
243 | bec9d989 | bellard | case 0: |
244 | bec9d989 | bellard | olen = adb_kbd_poll(d, obuf); |
245 | bec9d989 | bellard | break;
|
246 | 267002cd | bellard | case 1: |
247 | 267002cd | bellard | break;
|
248 | 267002cd | bellard | case 2: |
249 | e2733d20 | bellard | obuf[0] = 0x00; /* XXX: check this */ |
250 | e2733d20 | bellard | obuf[1] = 0x07; /* led status */ |
251 | e2733d20 | bellard | olen = 2;
|
252 | 267002cd | bellard | break;
|
253 | 267002cd | bellard | case 3: |
254 | e2733d20 | bellard | obuf[0] = d->handler;
|
255 | e2733d20 | bellard | obuf[1] = d->devaddr;
|
256 | e2733d20 | bellard | olen = 2;
|
257 | 267002cd | bellard | break;
|
258 | 267002cd | bellard | } |
259 | 267002cd | bellard | break;
|
260 | 267002cd | bellard | } |
261 | e2733d20 | bellard | return olen;
|
262 | 267002cd | bellard | } |
263 | 267002cd | bellard | |
264 | 9b64997f | blueswir1 | static void adb_kbd_save(QEMUFile *f, void *opaque) |
265 | 9b64997f | blueswir1 | { |
266 | 9b64997f | blueswir1 | KBDState *s = (KBDState *)opaque; |
267 | 9b64997f | blueswir1 | |
268 | 9b64997f | blueswir1 | qemu_put_buffer(f, s->data, sizeof(s->data));
|
269 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->rptr); |
270 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->wptr); |
271 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->count); |
272 | 9b64997f | blueswir1 | } |
273 | 9b64997f | blueswir1 | |
274 | 9b64997f | blueswir1 | static int adb_kbd_load(QEMUFile *f, void *opaque, int version_id) |
275 | 9b64997f | blueswir1 | { |
276 | 9b64997f | blueswir1 | KBDState *s = (KBDState *)opaque; |
277 | 9b64997f | blueswir1 | |
278 | 9b64997f | blueswir1 | if (version_id != 1) |
279 | 9b64997f | blueswir1 | return -EINVAL;
|
280 | 9b64997f | blueswir1 | |
281 | 9b64997f | blueswir1 | qemu_get_buffer(f, s->data, sizeof(s->data));
|
282 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->rptr); |
283 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->wptr); |
284 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->count); |
285 | 9b64997f | blueswir1 | |
286 | 9b64997f | blueswir1 | return 0; |
287 | 9b64997f | blueswir1 | } |
288 | 9b64997f | blueswir1 | |
289 | 3988e897 | bellard | static int adb_kbd_reset(ADBDevice *d) |
290 | 3988e897 | bellard | { |
291 | 3988e897 | bellard | KBDState *s = d->opaque; |
292 | 3988e897 | bellard | |
293 | 3988e897 | bellard | d->handler = 1;
|
294 | 3988e897 | bellard | d->devaddr = ADB_KEYBOARD; |
295 | 3988e897 | bellard | memset(s, 0, sizeof(KBDState)); |
296 | 3988e897 | bellard | |
297 | 3988e897 | bellard | return 0; |
298 | 3988e897 | bellard | } |
299 | 3988e897 | bellard | |
300 | 267002cd | bellard | void adb_kbd_init(ADBBusState *bus)
|
301 | 267002cd | bellard | { |
302 | 267002cd | bellard | ADBDevice *d; |
303 | e2733d20 | bellard | KBDState *s; |
304 | e2733d20 | bellard | s = qemu_mallocz(sizeof(KBDState));
|
305 | 3988e897 | bellard | d = adb_register_device(bus, ADB_KEYBOARD, adb_kbd_request, |
306 | 3988e897 | bellard | adb_kbd_reset, s); |
307 | 267002cd | bellard | qemu_add_kbd_event_handler(adb_kbd_put_keycode, d); |
308 | 9b64997f | blueswir1 | register_savevm("adb_kbd", -1, 1, adb_kbd_save, |
309 | 9b64997f | blueswir1 | adb_kbd_load, s); |
310 | 267002cd | bellard | } |
311 | 267002cd | bellard | |
312 | 267002cd | bellard | /***************************************************************/
|
313 | 267002cd | bellard | /* Mouse ADB device */
|
314 | 267002cd | bellard | |
315 | e2733d20 | bellard | typedef struct MouseState { |
316 | e2733d20 | bellard | int buttons_state, last_buttons_state;
|
317 | e2733d20 | bellard | int dx, dy, dz;
|
318 | e2733d20 | bellard | } MouseState; |
319 | e2733d20 | bellard | |
320 | 267002cd | bellard | static void adb_mouse_event(void *opaque, |
321 | 267002cd | bellard | int dx1, int dy1, int dz1, int buttons_state) |
322 | 267002cd | bellard | { |
323 | 267002cd | bellard | ADBDevice *d = opaque; |
324 | e2733d20 | bellard | MouseState *s = d->opaque; |
325 | e2733d20 | bellard | |
326 | e2733d20 | bellard | s->dx += dx1; |
327 | e2733d20 | bellard | s->dy += dy1; |
328 | e2733d20 | bellard | s->dz += dz1; |
329 | e2733d20 | bellard | s->buttons_state = buttons_state; |
330 | e2733d20 | bellard | } |
331 | e2733d20 | bellard | |
332 | e2733d20 | bellard | |
333 | e2733d20 | bellard | static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf) |
334 | e2733d20 | bellard | { |
335 | e2733d20 | bellard | MouseState *s = d->opaque; |
336 | 267002cd | bellard | int dx, dy;
|
337 | 267002cd | bellard | |
338 | e2733d20 | bellard | if (s->last_buttons_state == s->buttons_state &&
|
339 | e2733d20 | bellard | s->dx == 0 && s->dy == 0) |
340 | e2733d20 | bellard | return 0; |
341 | 3b46e624 | ths | |
342 | e2733d20 | bellard | dx = s->dx; |
343 | 267002cd | bellard | if (dx < -63) |
344 | 267002cd | bellard | dx = -63;
|
345 | 267002cd | bellard | else if (dx > 63) |
346 | 267002cd | bellard | dx = 63;
|
347 | 3b46e624 | ths | |
348 | e2733d20 | bellard | dy = s->dy; |
349 | 267002cd | bellard | if (dy < -63) |
350 | 267002cd | bellard | dy = -63;
|
351 | 267002cd | bellard | else if (dy > 63) |
352 | 267002cd | bellard | dy = 63;
|
353 | 3b46e624 | ths | |
354 | e2733d20 | bellard | s->dx -= dx; |
355 | e2733d20 | bellard | s->dy -= dy; |
356 | e2733d20 | bellard | s->last_buttons_state = s->buttons_state; |
357 | 3b46e624 | ths | |
358 | 267002cd | bellard | dx &= 0x7f;
|
359 | 267002cd | bellard | dy &= 0x7f;
|
360 | 3b46e624 | ths | |
361 | bec9d989 | bellard | if (!(s->buttons_state & MOUSE_EVENT_LBUTTON))
|
362 | 267002cd | bellard | dy |= 0x80;
|
363 | bec9d989 | bellard | if (!(s->buttons_state & MOUSE_EVENT_RBUTTON))
|
364 | 267002cd | bellard | dx |= 0x80;
|
365 | 3b46e624 | ths | |
366 | bec9d989 | bellard | obuf[0] = dy;
|
367 | bec9d989 | bellard | obuf[1] = dx;
|
368 | bec9d989 | bellard | return 2; |
369 | 267002cd | bellard | } |
370 | 267002cd | bellard | |
371 | e2733d20 | bellard | static int adb_mouse_request(ADBDevice *d, uint8_t *obuf, |
372 | e2733d20 | bellard | const uint8_t *buf, int len) |
373 | 267002cd | bellard | { |
374 | bec9d989 | bellard | MouseState *s = d->opaque; |
375 | e2733d20 | bellard | int cmd, reg, olen;
|
376 | 3b46e624 | ths | |
377 | bec9d989 | bellard | if ((buf[0] & 0x0f) == ADB_FLUSH) { |
378 | bec9d989 | bellard | /* flush mouse fifo */
|
379 | bec9d989 | bellard | s->buttons_state = s->last_buttons_state; |
380 | bec9d989 | bellard | s->dx = 0;
|
381 | bec9d989 | bellard | s->dy = 0;
|
382 | bec9d989 | bellard | s->dz = 0;
|
383 | bec9d989 | bellard | return 0; |
384 | e2733d20 | bellard | } |
385 | 267002cd | bellard | |
386 | 267002cd | bellard | cmd = buf[0] & 0xc; |
387 | 267002cd | bellard | reg = buf[0] & 0x3; |
388 | e2733d20 | bellard | olen = 0;
|
389 | 267002cd | bellard | switch(cmd) {
|
390 | 267002cd | bellard | case ADB_WRITEREG:
|
391 | ea026b2f | blueswir1 | ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]); |
392 | 267002cd | bellard | switch(reg) {
|
393 | 267002cd | bellard | case 2: |
394 | 267002cd | bellard | break;
|
395 | 267002cd | bellard | case 3: |
396 | 267002cd | bellard | switch(buf[2]) { |
397 | 267002cd | bellard | case ADB_CMD_SELF_TEST:
|
398 | 267002cd | bellard | break;
|
399 | 267002cd | bellard | case ADB_CMD_CHANGE_ID:
|
400 | 267002cd | bellard | case ADB_CMD_CHANGE_ID_AND_ACT:
|
401 | 267002cd | bellard | case ADB_CMD_CHANGE_ID_AND_ENABLE:
|
402 | 267002cd | bellard | d->devaddr = buf[1] & 0xf; |
403 | 267002cd | bellard | break;
|
404 | 267002cd | bellard | default:
|
405 | 267002cd | bellard | /* XXX: check this */
|
406 | 267002cd | bellard | d->devaddr = buf[1] & 0xf; |
407 | 267002cd | bellard | break;
|
408 | 267002cd | bellard | } |
409 | 267002cd | bellard | } |
410 | 267002cd | bellard | break;
|
411 | 267002cd | bellard | case ADB_READREG:
|
412 | 267002cd | bellard | switch(reg) {
|
413 | bec9d989 | bellard | case 0: |
414 | bec9d989 | bellard | olen = adb_mouse_poll(d, obuf); |
415 | bec9d989 | bellard | break;
|
416 | 267002cd | bellard | case 1: |
417 | 267002cd | bellard | break;
|
418 | 267002cd | bellard | case 3: |
419 | e2733d20 | bellard | obuf[0] = d->handler;
|
420 | e2733d20 | bellard | obuf[1] = d->devaddr;
|
421 | e2733d20 | bellard | olen = 2;
|
422 | 267002cd | bellard | break;
|
423 | 267002cd | bellard | } |
424 | ea026b2f | blueswir1 | ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg,
|
425 | ea026b2f | blueswir1 | obuf[0], obuf[1]); |
426 | 267002cd | bellard | break;
|
427 | 267002cd | bellard | } |
428 | e2733d20 | bellard | return olen;
|
429 | 267002cd | bellard | } |
430 | 267002cd | bellard | |
431 | 3988e897 | bellard | static int adb_mouse_reset(ADBDevice *d) |
432 | 3988e897 | bellard | { |
433 | 3988e897 | bellard | MouseState *s = d->opaque; |
434 | 3988e897 | bellard | |
435 | 3988e897 | bellard | d->handler = 2;
|
436 | 3988e897 | bellard | d->devaddr = ADB_MOUSE; |
437 | 3988e897 | bellard | memset(s, 0, sizeof(MouseState)); |
438 | 3988e897 | bellard | |
439 | 3988e897 | bellard | return 0; |
440 | 3988e897 | bellard | } |
441 | 3988e897 | bellard | |
442 | 9b64997f | blueswir1 | static void adb_mouse_save(QEMUFile *f, void *opaque) |
443 | 9b64997f | blueswir1 | { |
444 | 9b64997f | blueswir1 | MouseState *s = (MouseState *)opaque; |
445 | 9b64997f | blueswir1 | |
446 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->buttons_state); |
447 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->last_buttons_state); |
448 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->dx); |
449 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->dy); |
450 | 9b64997f | blueswir1 | qemu_put_sbe32s(f, &s->dz); |
451 | 9b64997f | blueswir1 | } |
452 | 9b64997f | blueswir1 | |
453 | 9b64997f | blueswir1 | static int adb_mouse_load(QEMUFile *f, void *opaque, int version_id) |
454 | 9b64997f | blueswir1 | { |
455 | 9b64997f | blueswir1 | MouseState *s = (MouseState *)opaque; |
456 | 9b64997f | blueswir1 | |
457 | 9b64997f | blueswir1 | if (version_id != 1) |
458 | 9b64997f | blueswir1 | return -EINVAL;
|
459 | 9b64997f | blueswir1 | |
460 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->buttons_state); |
461 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->last_buttons_state); |
462 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->dx); |
463 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->dy); |
464 | 9b64997f | blueswir1 | qemu_get_sbe32s(f, &s->dz); |
465 | 9b64997f | blueswir1 | |
466 | 9b64997f | blueswir1 | return 0; |
467 | 9b64997f | blueswir1 | } |
468 | 9b64997f | blueswir1 | |
469 | 267002cd | bellard | void adb_mouse_init(ADBBusState *bus)
|
470 | 267002cd | bellard | { |
471 | 267002cd | bellard | ADBDevice *d; |
472 | e2733d20 | bellard | MouseState *s; |
473 | 267002cd | bellard | |
474 | e2733d20 | bellard | s = qemu_mallocz(sizeof(MouseState));
|
475 | 3988e897 | bellard | d = adb_register_device(bus, ADB_MOUSE, adb_mouse_request, |
476 | 3988e897 | bellard | adb_mouse_reset, s); |
477 | 455204eb | ths | qemu_add_mouse_event_handler(adb_mouse_event, d, 0, "QEMU ADB Mouse"); |
478 | 9b64997f | blueswir1 | register_savevm("adb_mouse", -1, 1, adb_mouse_save, |
479 | 9b64997f | blueswir1 | adb_mouse_load, s); |
480 | 267002cd | bellard | } |