xref: /openbmc/qemu/hw/input/adb-mouse.c (revision dbb9e0f4)
1 /*
2  * QEMU ADB mouse support
3  *
4  * Copyright (c) 2004 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 #include "qemu/osdep.h"
25 #include "ui/console.h"
26 #include "hw/input/adb.h"
27 #include "adb-internal.h"
28 #include "trace.h"
29 
30 #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE)
31 
32 typedef struct MouseState {
33     /*< public >*/
34     ADBDevice parent_obj;
35     /*< private >*/
36 
37     int buttons_state, last_buttons_state;
38     int dx, dy, dz;
39 } MouseState;
40 
41 #define ADB_MOUSE_CLASS(class) \
42     OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE)
43 #define ADB_MOUSE_GET_CLASS(obj) \
44     OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE)
45 
46 typedef struct ADBMouseClass {
47     /*< public >*/
48     ADBDeviceClass parent_class;
49     /*< private >*/
50 
51     DeviceRealize parent_realize;
52 } ADBMouseClass;
53 
54 static void adb_mouse_event(void *opaque,
55                             int dx1, int dy1, int dz1, int buttons_state)
56 {
57     MouseState *s = opaque;
58 
59     s->dx += dx1;
60     s->dy += dy1;
61     s->dz += dz1;
62     s->buttons_state = buttons_state;
63 }
64 
65 
66 static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
67 {
68     MouseState *s = ADB_MOUSE(d);
69     int dx, dy;
70 
71     if (s->last_buttons_state == s->buttons_state &&
72         s->dx == 0 && s->dy == 0) {
73         return 0;
74     }
75 
76     dx = s->dx;
77     if (dx < -63) {
78         dx = -63;
79     } else if (dx > 63) {
80         dx = 63;
81     }
82 
83     dy = s->dy;
84     if (dy < -63) {
85         dy = -63;
86     } else if (dy > 63) {
87         dy = 63;
88     }
89 
90     s->dx -= dx;
91     s->dy -= dy;
92     s->last_buttons_state = s->buttons_state;
93 
94     dx &= 0x7f;
95     dy &= 0x7f;
96 
97     if (!(s->buttons_state & MOUSE_EVENT_LBUTTON)) {
98         dy |= 0x80;
99     }
100     if (!(s->buttons_state & MOUSE_EVENT_RBUTTON)) {
101         dx |= 0x80;
102     }
103 
104     obuf[0] = dy;
105     obuf[1] = dx;
106     return 2;
107 }
108 
109 static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
110                              const uint8_t *buf, int len)
111 {
112     MouseState *s = ADB_MOUSE(d);
113     int cmd, reg, olen;
114 
115     if ((buf[0] & 0x0f) == ADB_FLUSH) {
116         /* flush mouse fifo */
117         s->buttons_state = s->last_buttons_state;
118         s->dx = 0;
119         s->dy = 0;
120         s->dz = 0;
121         return 0;
122     }
123 
124     cmd = buf[0] & 0xc;
125     reg = buf[0] & 0x3;
126     olen = 0;
127     switch (cmd) {
128     case ADB_WRITEREG:
129         trace_adb_mouse_writereg(reg, buf[1]);
130         switch (reg) {
131         case 2:
132             break;
133         case 3:
134             switch (buf[2]) {
135             case ADB_CMD_SELF_TEST:
136                 break;
137             case ADB_CMD_CHANGE_ID:
138             case ADB_CMD_CHANGE_ID_AND_ACT:
139             case ADB_CMD_CHANGE_ID_AND_ENABLE:
140                 d->devaddr = buf[1] & 0xf;
141                 break;
142             default:
143                 d->devaddr = buf[1] & 0xf;
144                 /* we support handlers:
145                  * 0x01: Classic Apple Mouse Protocol / 100 cpi operations
146                  * 0x02: Classic Apple Mouse Protocol / 200 cpi operations
147                  * we don't support handlers (at least):
148                  * 0x03: Mouse systems A3 trackball
149                  * 0x04: Extended Apple Mouse Protocol
150                  * 0x2f: Microspeed mouse
151                  * 0x42: Macally
152                  * 0x5f: Microspeed mouse
153                  * 0x66: Microspeed mouse
154                  */
155                 if (buf[2] == 1 || buf[2] == 2) {
156                     d->handler = buf[2];
157                 }
158                 break;
159             }
160         }
161         break;
162     case ADB_READREG:
163         switch (reg) {
164         case 0:
165             olen = adb_mouse_poll(d, obuf);
166             break;
167         case 1:
168             break;
169         case 3:
170             obuf[0] = d->handler;
171             obuf[1] = d->devaddr;
172             olen = 2;
173             break;
174         }
175         trace_adb_mouse_readreg(reg, obuf[0], obuf[1]);
176         break;
177     }
178     return olen;
179 }
180 
181 static void adb_mouse_reset(DeviceState *dev)
182 {
183     ADBDevice *d = ADB_DEVICE(dev);
184     MouseState *s = ADB_MOUSE(dev);
185 
186     d->handler = 2;
187     d->devaddr = ADB_DEVID_MOUSE;
188     s->last_buttons_state = s->buttons_state = 0;
189     s->dx = s->dy = s->dz = 0;
190 }
191 
192 static const VMStateDescription vmstate_adb_mouse = {
193     .name = "adb_mouse",
194     .version_id = 2,
195     .minimum_version_id = 2,
196     .fields = (VMStateField[]) {
197         VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device,
198                        ADBDevice),
199         VMSTATE_INT32(buttons_state, MouseState),
200         VMSTATE_INT32(last_buttons_state, MouseState),
201         VMSTATE_INT32(dx, MouseState),
202         VMSTATE_INT32(dy, MouseState),
203         VMSTATE_INT32(dz, MouseState),
204         VMSTATE_END_OF_LIST()
205     }
206 };
207 
208 static void adb_mouse_realizefn(DeviceState *dev, Error **errp)
209 {
210     MouseState *s = ADB_MOUSE(dev);
211     ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev);
212 
213     amc->parent_realize(dev, errp);
214 
215     qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse");
216 }
217 
218 static void adb_mouse_initfn(Object *obj)
219 {
220     ADBDevice *d = ADB_DEVICE(obj);
221 
222     d->devaddr = ADB_DEVID_MOUSE;
223 }
224 
225 static void adb_mouse_class_init(ObjectClass *oc, void *data)
226 {
227     DeviceClass *dc = DEVICE_CLASS(oc);
228     ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
229     ADBMouseClass *amc = ADB_MOUSE_CLASS(oc);
230 
231     device_class_set_parent_realize(dc, adb_mouse_realizefn,
232                                     &amc->parent_realize);
233     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
234 
235     adc->devreq = adb_mouse_request;
236     dc->reset = adb_mouse_reset;
237     dc->vmsd = &vmstate_adb_mouse;
238 }
239 
240 static const TypeInfo adb_mouse_type_info = {
241     .name = TYPE_ADB_MOUSE,
242     .parent = TYPE_ADB_DEVICE,
243     .instance_size = sizeof(MouseState),
244     .instance_init = adb_mouse_initfn,
245     .class_init = adb_mouse_class_init,
246     .class_size = sizeof(ADBMouseClass),
247 };
248 
249 static void adb_mouse_register_types(void)
250 {
251     type_register_static(&adb_mouse_type_info);
252 }
253 
254 type_init(adb_mouse_register_types)
255