blob: ab8fe981d63ff073c92b97a7a3e3d3a009971025 [file] [log] [blame]
aurel32aa71cf82009-02-08 15:53:20 +00001/*
2 * QEMU Microsoft serial mouse emulation
3 *
4 * Copyright (c) 2008 Lubomir Rintel
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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell9c058332016-01-29 17:49:54 +000025#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020026#include "qemu/module.h"
Arwed Meyere0cf7f22022-09-11 20:18:38 +020027#include "qemu/fifo8.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040028#include "chardev/char.h"
Arwed Meyera39fe102022-09-11 20:18:36 +020029#include "chardev/char-serial.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010030#include "ui/console.h"
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020031#include "ui/input.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040032#include "qom/object.h"
aurel32aa71cf82009-02-08 15:53:20 +000033
Arwed Meyera39fe102022-09-11 20:18:36 +020034#define MSMOUSE_LO6(n) ((n) & 0x3f)
35#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
36#define MSMOUSE_PWR(cm) (cm & (CHR_TIOCM_RTS | CHR_TIOCM_DTR))
aurel32aa71cf82009-02-08 15:53:20 +000037
Arwed Meyer50d03d42022-09-11 20:18:39 +020038/* Serial PnP for 6 bit devices/mice sends all ASCII chars - 0x20 */
39#define M(c) (c - 0x20)
Arwed Meyere0cf7f22022-09-11 20:18:38 +020040/* Serial fifo size. */
41#define MSMOUSE_BUF_SZ 64
42
43/* Mouse ID: Send "M3" cause we behave like a 3 button logitech mouse. */
44const uint8_t mouse_id[] = {'M', '3'};
Arwed Meyer50d03d42022-09-11 20:18:39 +020045/*
46 * PnP start "(", PnP version (1.0), vendor ID, product ID, '\\',
47 * serial ID (omitted), '\\', MS class name, '\\', driver ID (omitted), '\\',
48 * product description, checksum, ")"
49 * Missing parts are inserted later.
50 */
51const uint8_t pnp_data[] = {M('('), 1, '$', M('Q'), M('M'), M('U'),
52 M('0'), M('0'), M('0'), M('1'),
53 M('\\'), M('\\'),
54 M('M'), M('O'), M('U'), M('S'), M('E'),
55 M('\\'), M('\\')};
Arwed Meyere0cf7f22022-09-11 20:18:38 +020056
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040057struct MouseChardev {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030058 Chardev parent;
Marc-André Lureau41ac54b2016-10-21 23:44:44 +030059
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020060 QemuInputHandlerState *hs;
Arwed Meyera39fe102022-09-11 20:18:36 +020061 int tiocm;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020062 int axis[INPUT_AXIS__MAX];
63 bool btns[INPUT_BUTTON__MAX];
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020064 bool btnc[INPUT_BUTTON__MAX];
Arwed Meyere0cf7f22022-09-11 20:18:38 +020065 Fifo8 outbuf;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040066};
67typedef struct MouseChardev MouseChardev;
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +020068
Marc-André Lureau777357d2016-12-07 18:39:10 +030069#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
Eduardo Habkost8110fa12020-08-31 17:07:33 -040070DECLARE_INSTANCE_CHECKER(MouseChardev, MOUSE_CHARDEV,
71 TYPE_CHARDEV_MSMOUSE)
Marc-André Lureau777357d2016-12-07 18:39:10 +030072
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030073static void msmouse_chr_accept_input(Chardev *chr)
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020074{
Marc-André Lureau777357d2016-12-07 18:39:10 +030075 MouseChardev *mouse = MOUSE_CHARDEV(chr);
Arwed Meyere0cf7f22022-09-11 20:18:38 +020076 uint32_t len, avail;
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020077
78 len = qemu_chr_be_can_write(chr);
Arwed Meyere0cf7f22022-09-11 20:18:38 +020079 avail = fifo8_num_used(&mouse->outbuf);
80 while (len > 0 && avail > 0) {
81 const uint8_t *buf;
82 uint32_t size;
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020083
Arwed Meyere0cf7f22022-09-11 20:18:38 +020084 buf = fifo8_pop_buf(&mouse->outbuf, MIN(len, avail), &size);
85 qemu_chr_be_write(chr, buf, size);
86 len = qemu_chr_be_can_write(chr);
87 avail -= size;
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020088 }
89}
90
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030091static void msmouse_queue_event(MouseChardev *mouse)
aurel32aa71cf82009-02-08 15:53:20 +000092{
aurel32aa71cf82009-02-08 15:53:20 +000093 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020094 int dx, dy, count = 3;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020095
96 dx = mouse->axis[INPUT_AXIS_X];
97 mouse->axis[INPUT_AXIS_X] = 0;
98
99 dy = mouse->axis[INPUT_AXIS_Y];
100 mouse->axis[INPUT_AXIS_Y] = 0;
aurel32aa71cf82009-02-08 15:53:20 +0000101
102 /* Movement deltas */
103 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
104 bytes[1] |= MSMOUSE_LO6(dx);
105 bytes[2] |= MSMOUSE_LO6(dy);
106
107 /* Buttons */
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200108 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
109 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +0200110 if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
111 mouse->btnc[INPUT_BUTTON_MIDDLE]) {
112 bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
113 mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
Arwed Meyere0cf7f22022-09-11 20:18:38 +0200114 count++;
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +0200115 }
aurel32aa71cf82009-02-08 15:53:20 +0000116
Arwed Meyere0cf7f22022-09-11 20:18:38 +0200117 if (fifo8_num_free(&mouse->outbuf) >= count) {
118 fifo8_push_all(&mouse->outbuf, bytes, count);
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +0200119 } else {
120 /* queue full -> drop event */
121 }
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200122}
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +0200123
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200124static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
125 InputEvent *evt)
126{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300127 MouseChardev *mouse = MOUSE_CHARDEV(dev);
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200128 InputMoveEvent *move;
129 InputBtnEvent *btn;
130
Arwed Meyera39fe102022-09-11 20:18:36 +0200131 /* Ignore events if serial mouse powered down. */
132 if (!MSMOUSE_PWR(mouse->tiocm)) {
133 return;
134 }
135
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200136 switch (evt->type) {
137 case INPUT_EVENT_KIND_REL:
138 move = evt->u.rel.data;
139 mouse->axis[move->axis] += move->value;
140 break;
141
142 case INPUT_EVENT_KIND_BTN:
143 btn = evt->u.btn.data;
144 mouse->btns[btn->button] = btn->down;
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +0200145 mouse->btnc[btn->button] = true;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200146 break;
147
148 default:
149 /* keep gcc happy */
150 break;
151 }
152}
153
154static void msmouse_input_sync(DeviceState *dev)
155{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300156 MouseChardev *mouse = MOUSE_CHARDEV(dev);
157 Chardev *chr = CHARDEV(dev);
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200158
Arwed Meyera39fe102022-09-11 20:18:36 +0200159 /* Ignore events if serial mouse powered down. */
160 if (!MSMOUSE_PWR(mouse->tiocm)) {
161 return;
162 }
163
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200164 msmouse_queue_event(mouse);
Marc-André Lureau41ac54b2016-10-21 23:44:44 +0300165 msmouse_chr_accept_input(chr);
aurel32aa71cf82009-02-08 15:53:20 +0000166}
167
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300168static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
aurel32aa71cf82009-02-08 15:53:20 +0000169{
170 /* Ignore writes to mouse port */
171 return len;
172}
173
Arwed Meyer50d03d42022-09-11 20:18:39 +0200174static QemuInputHandler msmouse_handler = {
175 .name = "QEMU Microsoft Mouse",
176 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
177 .event = msmouse_input_event,
178 .sync = msmouse_input_sync,
179};
180
Arwed Meyera39fe102022-09-11 20:18:36 +0200181static int msmouse_ioctl(Chardev *chr, int cmd, void *arg)
182{
183 MouseChardev *mouse = MOUSE_CHARDEV(chr);
Arwed Meyer50d03d42022-09-11 20:18:39 +0200184 int c, i, j;
185 uint8_t bytes[MSMOUSE_BUF_SZ / 2];
Arwed Meyera39fe102022-09-11 20:18:36 +0200186 int *targ = (int *)arg;
Arwed Meyer50d03d42022-09-11 20:18:39 +0200187 const uint8_t hexchr[16] = {M('0'), M('1'), M('2'), M('3'), M('4'), M('5'),
188 M('6'), M('7'), M('8'), M('9'), M('A'), M('B'),
189 M('C'), M('D'), M('E'), M('F')};
Arwed Meyera39fe102022-09-11 20:18:36 +0200190
191 switch (cmd) {
192 case CHR_IOCTL_SERIAL_SET_TIOCM:
193 c = mouse->tiocm;
194 mouse->tiocm = *(int *)arg;
195 if (MSMOUSE_PWR(mouse->tiocm)) {
196 if (!MSMOUSE_PWR(c)) {
197 /*
Arwed Meyer50d03d42022-09-11 20:18:39 +0200198 * Power on after reset: Send ID and PnP data
199 * No need to check fifo space as it is empty at this point.
Arwed Meyera39fe102022-09-11 20:18:36 +0200200 */
Arwed Meyere0cf7f22022-09-11 20:18:38 +0200201 fifo8_push_all(&mouse->outbuf, mouse_id, sizeof(mouse_id));
Arwed Meyer50d03d42022-09-11 20:18:39 +0200202 /* Add PnP data: */
203 fifo8_push_all(&mouse->outbuf, pnp_data, sizeof(pnp_data));
204 /*
205 * Add device description from qemu handler name.
206 * Make sure this all fits into the queue beforehand!
207 */
208 c = M(')');
209 for (i = 0; msmouse_handler.name[i]; i++) {
210 bytes[i] = M(msmouse_handler.name[i]);
211 c += bytes[i];
212 }
213 /* Calc more of checksum */
214 for (j = 0; j < sizeof(pnp_data); j++) {
215 c += pnp_data[j];
216 }
217 c &= 0xff;
218 bytes[i++] = hexchr[c >> 4];
219 bytes[i++] = hexchr[c & 0x0f];
220 bytes[i++] = M(')');
221 fifo8_push_all(&mouse->outbuf, bytes, i);
Arwed Meyera39fe102022-09-11 20:18:36 +0200222 /* Start sending data to serial. */
223 msmouse_chr_accept_input(chr);
224 }
225 break;
226 }
227 /*
228 * Reset mouse buffers on power down.
229 * Mouse won't send anything without power.
230 */
Arwed Meyere0cf7f22022-09-11 20:18:38 +0200231 fifo8_reset(&mouse->outbuf);
Arwed Meyera39fe102022-09-11 20:18:36 +0200232 memset(mouse->axis, 0, sizeof(mouse->axis));
233 memset(mouse->btns, false, sizeof(mouse->btns));
234 memset(mouse->btnc, false, sizeof(mouse->btns));
235 break;
236 case CHR_IOCTL_SERIAL_GET_TIOCM:
237 /* Remember line control status. */
238 *targ = mouse->tiocm;
239 break;
240 default:
241 return -ENOTSUP;
242 }
243 return 0;
244}
245
Marc-André Lureau8955e892016-12-08 16:34:44 +0300246static void char_msmouse_finalize(Object *obj)
aurel32aa71cf82009-02-08 15:53:20 +0000247{
Marc-André Lureau8955e892016-12-08 16:34:44 +0300248 MouseChardev *mouse = MOUSE_CHARDEV(obj);
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +0200249
Maksim Davydovfc0c1282022-08-25 19:52:47 +0300250 if (mouse->hs) {
251 qemu_input_handler_unregister(mouse->hs);
252 }
Arwed Meyere0cf7f22022-09-11 20:18:38 +0200253 fifo8_destroy(&mouse->outbuf);
aurel32aa71cf82009-02-08 15:53:20 +0000254}
255
Marc-André Lureau777357d2016-12-07 18:39:10 +0300256static void msmouse_chr_open(Chardev *chr,
257 ChardevBackend *backend,
258 bool *be_opened,
259 Error **errp)
aurel32aa71cf82009-02-08 15:53:20 +0000260{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300261 MouseChardev *mouse = MOUSE_CHARDEV(chr);
aurel32aa71cf82009-02-08 15:53:20 +0000262
Marc-André Lureau82878da2016-10-22 13:09:43 +0300263 *be_opened = false;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200264 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
265 &msmouse_handler);
Arwed Meyera39fe102022-09-11 20:18:36 +0200266 mouse->tiocm = 0;
Arwed Meyere0cf7f22022-09-11 20:18:38 +0200267 fifo8_create(&mouse->outbuf, MSMOUSE_BUF_SZ);
aurel32aa71cf82009-02-08 15:53:20 +0000268}
Anthony Liguori5ab82112013-03-05 23:21:31 +0530269
Marc-André Lureau777357d2016-12-07 18:39:10 +0300270static void char_msmouse_class_init(ObjectClass *oc, void *data)
271{
272 ChardevClass *cc = CHARDEV_CLASS(oc);
273
274 cc->open = msmouse_chr_open;
275 cc->chr_write = msmouse_chr_write;
276 cc->chr_accept_input = msmouse_chr_accept_input;
Arwed Meyera39fe102022-09-11 20:18:36 +0200277 cc->chr_ioctl = msmouse_ioctl;
Marc-André Lureau777357d2016-12-07 18:39:10 +0300278}
279
280static const TypeInfo char_msmouse_type_info = {
281 .name = TYPE_CHARDEV_MSMOUSE,
282 .parent = TYPE_CHARDEV,
283 .instance_size = sizeof(MouseChardev),
Marc-André Lureau8955e892016-12-08 16:34:44 +0300284 .instance_finalize = char_msmouse_finalize,
Marc-André Lureau777357d2016-12-07 18:39:10 +0300285 .class_init = char_msmouse_class_init,
286};
287
Anthony Liguori5ab82112013-03-05 23:21:31 +0530288static void register_types(void)
289{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300290 type_register_static(&char_msmouse_type_info);
Anthony Liguori5ab82112013-03-05 23:21:31 +0530291}
292
293type_init(register_types);