2009-02-08 16:53:20 +01:00
|
|
|
/*
|
|
|
|
* QEMU Microsoft serial mouse emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Lubomir Rintel
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 16:35:07 +02:00
|
|
|
|
2016-01-29 18:49:54 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2022-09-11 20:18:38 +02:00
|
|
|
#include "qemu/fifo8.h"
|
2017-01-26 14:19:46 +01:00
|
|
|
#include "chardev/char.h"
|
2022-09-11 20:18:36 +02:00
|
|
|
#include "chardev/char-serial.h"
|
2012-11-28 12:06:30 +01:00
|
|
|
#include "ui/console.h"
|
2016-07-04 11:42:54 +02:00
|
|
|
#include "ui/input.h"
|
2020-09-03 22:43:22 +02:00
|
|
|
#include "qom/object.h"
|
2009-02-08 16:53:20 +01:00
|
|
|
|
2022-09-11 20:18:36 +02:00
|
|
|
#define MSMOUSE_LO6(n) ((n) & 0x3f)
|
|
|
|
#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
|
|
|
|
#define MSMOUSE_PWR(cm) (cm & (CHR_TIOCM_RTS | CHR_TIOCM_DTR))
|
2009-02-08 16:53:20 +01:00
|
|
|
|
2022-09-11 20:18:39 +02:00
|
|
|
/* Serial PnP for 6 bit devices/mice sends all ASCII chars - 0x20 */
|
|
|
|
#define M(c) (c - 0x20)
|
2022-09-11 20:18:38 +02:00
|
|
|
/* Serial fifo size. */
|
|
|
|
#define MSMOUSE_BUF_SZ 64
|
|
|
|
|
|
|
|
/* Mouse ID: Send "M3" cause we behave like a 3 button logitech mouse. */
|
|
|
|
const uint8_t mouse_id[] = {'M', '3'};
|
2022-09-11 20:18:39 +02:00
|
|
|
/*
|
|
|
|
* PnP start "(", PnP version (1.0), vendor ID, product ID, '\\',
|
|
|
|
* serial ID (omitted), '\\', MS class name, '\\', driver ID (omitted), '\\',
|
|
|
|
* product description, checksum, ")"
|
|
|
|
* Missing parts are inserted later.
|
|
|
|
*/
|
|
|
|
const uint8_t pnp_data[] = {M('('), 1, '$', M('Q'), M('M'), M('U'),
|
|
|
|
M('0'), M('0'), M('0'), M('1'),
|
|
|
|
M('\\'), M('\\'),
|
|
|
|
M('M'), M('O'), M('U'), M('S'), M('E'),
|
|
|
|
M('\\'), M('\\')};
|
2022-09-11 20:18:38 +02:00
|
|
|
|
2020-09-03 22:43:22 +02:00
|
|
|
struct MouseChardev {
|
2016-12-07 14:20:22 +01:00
|
|
|
Chardev parent;
|
2016-10-21 22:44:44 +02:00
|
|
|
|
2016-07-04 11:42:54 +02:00
|
|
|
QemuInputHandlerState *hs;
|
2022-09-11 20:18:36 +02:00
|
|
|
int tiocm;
|
2016-07-04 11:42:54 +02:00
|
|
|
int axis[INPUT_AXIS__MAX];
|
|
|
|
bool btns[INPUT_BUTTON__MAX];
|
2016-07-04 11:42:55 +02:00
|
|
|
bool btnc[INPUT_BUTTON__MAX];
|
2022-09-11 20:18:38 +02:00
|
|
|
Fifo8 outbuf;
|
2020-09-03 22:43:22 +02:00
|
|
|
};
|
|
|
|
typedef struct MouseChardev MouseChardev;
|
2016-07-04 11:42:52 +02:00
|
|
|
|
2016-12-07 16:39:10 +01:00
|
|
|
#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
|
2020-08-31 23:07:33 +02:00
|
|
|
DECLARE_INSTANCE_CHECKER(MouseChardev, MOUSE_CHARDEV,
|
|
|
|
TYPE_CHARDEV_MSMOUSE)
|
2016-12-07 16:39:10 +01:00
|
|
|
|
2016-12-07 14:20:22 +01:00
|
|
|
static void msmouse_chr_accept_input(Chardev *chr)
|
2016-07-04 11:42:53 +02:00
|
|
|
{
|
2016-12-07 16:39:10 +01:00
|
|
|
MouseChardev *mouse = MOUSE_CHARDEV(chr);
|
2022-09-11 20:18:38 +02:00
|
|
|
uint32_t len, avail;
|
2016-07-04 11:42:53 +02:00
|
|
|
|
|
|
|
len = qemu_chr_be_can_write(chr);
|
2022-09-11 20:18:38 +02:00
|
|
|
avail = fifo8_num_used(&mouse->outbuf);
|
|
|
|
while (len > 0 && avail > 0) {
|
|
|
|
const uint8_t *buf;
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
buf = fifo8_pop_buf(&mouse->outbuf, MIN(len, avail), &size);
|
|
|
|
qemu_chr_be_write(chr, buf, size);
|
|
|
|
len = qemu_chr_be_can_write(chr);
|
|
|
|
avail -= size;
|
2016-07-04 11:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:20:22 +01:00
|
|
|
static void msmouse_queue_event(MouseChardev *mouse)
|
2009-02-08 16:53:20 +01:00
|
|
|
{
|
|
|
|
unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
|
2016-07-04 11:42:55 +02:00
|
|
|
int dx, dy, count = 3;
|
2016-07-04 11:42:54 +02:00
|
|
|
|
|
|
|
dx = mouse->axis[INPUT_AXIS_X];
|
|
|
|
mouse->axis[INPUT_AXIS_X] = 0;
|
|
|
|
|
|
|
|
dy = mouse->axis[INPUT_AXIS_Y];
|
|
|
|
mouse->axis[INPUT_AXIS_Y] = 0;
|
2009-02-08 16:53:20 +01:00
|
|
|
|
|
|
|
/* Movement deltas */
|
|
|
|
bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
|
|
|
|
bytes[1] |= MSMOUSE_LO6(dx);
|
|
|
|
bytes[2] |= MSMOUSE_LO6(dy);
|
|
|
|
|
|
|
|
/* Buttons */
|
2016-07-04 11:42:54 +02:00
|
|
|
bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
|
|
|
|
bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
|
2016-07-04 11:42:55 +02:00
|
|
|
if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
|
|
|
|
mouse->btnc[INPUT_BUTTON_MIDDLE]) {
|
|
|
|
bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
|
|
|
|
mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
|
2022-09-11 20:18:38 +02:00
|
|
|
count++;
|
2016-07-04 11:42:55 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 20:18:38 +02:00
|
|
|
if (fifo8_num_free(&mouse->outbuf) >= count) {
|
|
|
|
fifo8_push_all(&mouse->outbuf, bytes, count);
|
2016-07-04 11:42:53 +02:00
|
|
|
} else {
|
|
|
|
/* queue full -> drop event */
|
|
|
|
}
|
2016-07-04 11:42:54 +02:00
|
|
|
}
|
2016-07-04 11:42:53 +02:00
|
|
|
|
2016-07-04 11:42:54 +02:00
|
|
|
static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
|
|
|
|
InputEvent *evt)
|
|
|
|
{
|
2016-12-07 16:39:10 +01:00
|
|
|
MouseChardev *mouse = MOUSE_CHARDEV(dev);
|
2016-07-04 11:42:54 +02:00
|
|
|
InputMoveEvent *move;
|
|
|
|
InputBtnEvent *btn;
|
|
|
|
|
2022-09-11 20:18:36 +02:00
|
|
|
/* Ignore events if serial mouse powered down. */
|
|
|
|
if (!MSMOUSE_PWR(mouse->tiocm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-04 11:42:54 +02:00
|
|
|
switch (evt->type) {
|
|
|
|
case INPUT_EVENT_KIND_REL:
|
|
|
|
move = evt->u.rel.data;
|
|
|
|
mouse->axis[move->axis] += move->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INPUT_EVENT_KIND_BTN:
|
|
|
|
btn = evt->u.btn.data;
|
|
|
|
mouse->btns[btn->button] = btn->down;
|
2016-07-04 11:42:55 +02:00
|
|
|
mouse->btnc[btn->button] = true;
|
2016-07-04 11:42:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* keep gcc happy */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msmouse_input_sync(DeviceState *dev)
|
|
|
|
{
|
2016-12-07 16:39:10 +01:00
|
|
|
MouseChardev *mouse = MOUSE_CHARDEV(dev);
|
|
|
|
Chardev *chr = CHARDEV(dev);
|
2016-07-04 11:42:54 +02:00
|
|
|
|
2022-09-11 20:18:36 +02:00
|
|
|
/* Ignore events if serial mouse powered down. */
|
|
|
|
if (!MSMOUSE_PWR(mouse->tiocm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-04 11:42:54 +02:00
|
|
|
msmouse_queue_event(mouse);
|
2016-10-21 22:44:44 +02:00
|
|
|
msmouse_chr_accept_input(chr);
|
2009-02-08 16:53:20 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 14:20:22 +01:00
|
|
|
static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
|
2009-02-08 16:53:20 +01:00
|
|
|
{
|
|
|
|
/* Ignore writes to mouse port */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:05:00 +02:00
|
|
|
static const QemuInputHandler msmouse_handler = {
|
2022-09-11 20:18:39 +02:00
|
|
|
.name = "QEMU Microsoft Mouse",
|
|
|
|
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
|
|
|
|
.event = msmouse_input_event,
|
|
|
|
.sync = msmouse_input_sync,
|
|
|
|
};
|
|
|
|
|
2022-09-11 20:18:36 +02:00
|
|
|
static int msmouse_ioctl(Chardev *chr, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
MouseChardev *mouse = MOUSE_CHARDEV(chr);
|
2022-09-11 20:18:39 +02:00
|
|
|
int c, i, j;
|
|
|
|
uint8_t bytes[MSMOUSE_BUF_SZ / 2];
|
2022-09-11 20:18:36 +02:00
|
|
|
int *targ = (int *)arg;
|
2022-09-11 20:18:39 +02:00
|
|
|
const uint8_t hexchr[16] = {M('0'), M('1'), M('2'), M('3'), M('4'), M('5'),
|
|
|
|
M('6'), M('7'), M('8'), M('9'), M('A'), M('B'),
|
|
|
|
M('C'), M('D'), M('E'), M('F')};
|
2022-09-11 20:18:36 +02:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CHR_IOCTL_SERIAL_SET_TIOCM:
|
|
|
|
c = mouse->tiocm;
|
|
|
|
mouse->tiocm = *(int *)arg;
|
|
|
|
if (MSMOUSE_PWR(mouse->tiocm)) {
|
|
|
|
if (!MSMOUSE_PWR(c)) {
|
|
|
|
/*
|
2022-09-11 20:18:39 +02:00
|
|
|
* Power on after reset: Send ID and PnP data
|
|
|
|
* No need to check fifo space as it is empty at this point.
|
2022-09-11 20:18:36 +02:00
|
|
|
*/
|
2022-09-11 20:18:38 +02:00
|
|
|
fifo8_push_all(&mouse->outbuf, mouse_id, sizeof(mouse_id));
|
2022-09-11 20:18:39 +02:00
|
|
|
/* Add PnP data: */
|
|
|
|
fifo8_push_all(&mouse->outbuf, pnp_data, sizeof(pnp_data));
|
|
|
|
/*
|
|
|
|
* Add device description from qemu handler name.
|
|
|
|
* Make sure this all fits into the queue beforehand!
|
|
|
|
*/
|
|
|
|
c = M(')');
|
|
|
|
for (i = 0; msmouse_handler.name[i]; i++) {
|
|
|
|
bytes[i] = M(msmouse_handler.name[i]);
|
|
|
|
c += bytes[i];
|
|
|
|
}
|
|
|
|
/* Calc more of checksum */
|
|
|
|
for (j = 0; j < sizeof(pnp_data); j++) {
|
|
|
|
c += pnp_data[j];
|
|
|
|
}
|
|
|
|
c &= 0xff;
|
|
|
|
bytes[i++] = hexchr[c >> 4];
|
|
|
|
bytes[i++] = hexchr[c & 0x0f];
|
|
|
|
bytes[i++] = M(')');
|
|
|
|
fifo8_push_all(&mouse->outbuf, bytes, i);
|
2022-09-11 20:18:36 +02:00
|
|
|
/* Start sending data to serial. */
|
|
|
|
msmouse_chr_accept_input(chr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Reset mouse buffers on power down.
|
|
|
|
* Mouse won't send anything without power.
|
|
|
|
*/
|
2022-09-11 20:18:38 +02:00
|
|
|
fifo8_reset(&mouse->outbuf);
|
2022-09-11 20:18:36 +02:00
|
|
|
memset(mouse->axis, 0, sizeof(mouse->axis));
|
|
|
|
memset(mouse->btns, false, sizeof(mouse->btns));
|
|
|
|
memset(mouse->btnc, false, sizeof(mouse->btns));
|
|
|
|
break;
|
|
|
|
case CHR_IOCTL_SERIAL_GET_TIOCM:
|
|
|
|
/* Remember line control status. */
|
|
|
|
*targ = mouse->tiocm;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-08 14:34:44 +01:00
|
|
|
static void char_msmouse_finalize(Object *obj)
|
2009-02-08 16:53:20 +01:00
|
|
|
{
|
2016-12-08 14:34:44 +01:00
|
|
|
MouseChardev *mouse = MOUSE_CHARDEV(obj);
|
2016-07-04 11:42:52 +02:00
|
|
|
|
2022-08-25 18:52:47 +02:00
|
|
|
if (mouse->hs) {
|
|
|
|
qemu_input_handler_unregister(mouse->hs);
|
|
|
|
}
|
2022-09-11 20:18:38 +02:00
|
|
|
fifo8_destroy(&mouse->outbuf);
|
2009-02-08 16:53:20 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 16:39:10 +01:00
|
|
|
static void msmouse_chr_open(Chardev *chr,
|
|
|
|
ChardevBackend *backend,
|
|
|
|
bool *be_opened,
|
|
|
|
Error **errp)
|
2009-02-08 16:53:20 +01:00
|
|
|
{
|
2016-12-07 16:39:10 +01:00
|
|
|
MouseChardev *mouse = MOUSE_CHARDEV(chr);
|
2009-02-08 16:53:20 +01:00
|
|
|
|
2016-10-22 12:09:43 +02:00
|
|
|
*be_opened = false;
|
2016-07-04 11:42:54 +02:00
|
|
|
mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
|
|
|
|
&msmouse_handler);
|
2022-09-11 20:18:36 +02:00
|
|
|
mouse->tiocm = 0;
|
2022-09-11 20:18:38 +02:00
|
|
|
fifo8_create(&mouse->outbuf, MSMOUSE_BUF_SZ);
|
2016-12-07 16:39:10 +01:00
|
|
|
}
|
2016-07-04 11:42:52 +02:00
|
|
|
|
2016-12-07 16:39:10 +01:00
|
|
|
static void char_msmouse_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
ChardevClass *cc = CHARDEV_CLASS(oc);
|
2009-02-08 16:53:20 +01:00
|
|
|
|
2016-12-07 16:39:10 +01:00
|
|
|
cc->open = msmouse_chr_open;
|
|
|
|
cc->chr_write = msmouse_chr_write;
|
|
|
|
cc->chr_accept_input = msmouse_chr_accept_input;
|
2022-09-11 20:18:36 +02:00
|
|
|
cc->chr_ioctl = msmouse_ioctl;
|
2009-02-08 16:53:20 +01:00
|
|
|
}
|
2013-03-05 18:51:31 +01:00
|
|
|
|
2016-12-07 16:39:10 +01:00
|
|
|
static const TypeInfo char_msmouse_type_info = {
|
|
|
|
.name = TYPE_CHARDEV_MSMOUSE,
|
|
|
|
.parent = TYPE_CHARDEV,
|
|
|
|
.instance_size = sizeof(MouseChardev),
|
2016-12-08 14:34:44 +01:00
|
|
|
.instance_finalize = char_msmouse_finalize,
|
2016-12-07 16:39:10 +01:00
|
|
|
.class_init = char_msmouse_class_init,
|
|
|
|
};
|
|
|
|
|
2013-03-05 18:51:31 +01:00
|
|
|
static void register_types(void)
|
|
|
|
{
|
2016-12-07 16:39:10 +01:00
|
|
|
type_register_static(&char_msmouse_type_info);
|
2013-03-05 18:51:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|