Import QEMU upstream snapshot d2e570c

Upstream: https://gitlab.com/qemu-project/qemu.git

Upstream-Commit: d2e570cc0f97b936902a5b1b86b73c0f5998b475
This commit is contained in:
2026-08-31 02:15:30 +02:00
commit cf256aa081
11315 changed files with 3598369 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
config ADB
bool
config PCKBD
bool
select PS2
depends on ISA_BUS
config PL050
bool
select PS2
config PS2
bool
config STELLARIS_GAMEPAD
bool
config VIRTIO_INPUT
bool
default y
depends on VIRTIO
config VIRTIO_INPUT_HOST
bool
default y
depends on VIRTIO_INPUT && LINUX
config VHOST_USER_INPUT
bool
default y
depends on VIRTIO_INPUT && VHOST_USER
config LASIPS2
select PS2
+53
View File
@@ -0,0 +1,53 @@
/*
* QEMU ADB support
*
* Copyright (c) 2004 Fabrice Bellard
*
* 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.
*/
#ifndef HW_INPUT_ADB_INTERNAL_H
#define HW_INPUT_ADB_INTERNAL_H
/* ADB commands */
#define ADB_BUSRESET 0x00
#define ADB_FLUSH 0x01
#define ADB_WRITEREG 0x08
#define ADB_READREG 0x0c
/* ADB device commands */
#define ADB_CMD_SELF_TEST 0xff
#define ADB_CMD_CHANGE_ID 0xfe
#define ADB_CMD_CHANGE_ID_AND_ACT 0xfd
#define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00
/* ADB default device IDs (upper 4 bits of ADB command byte) */
#define ADB_DEVID_DONGLE 1
#define ADB_DEVID_KEYBOARD 2
#define ADB_DEVID_MOUSE 3
#define ADB_DEVID_TABLET 4
#define ADB_DEVID_MODEM 5
#define ADB_DEVID_MISC 7
extern const VMStateDescription vmstate_adb_device;
#endif
+421
View File
@@ -0,0 +1,421 @@
/*
* QEMU ADB keyboard support
*
* Copyright (c) 2004 Fabrice Bellard
*
* 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.
*/
#include "qemu/osdep.h"
#include "hw/input/adb.h"
#include "migration/vmstate.h"
#include "qemu/module.h"
#include "standard-headers/linux/input-event-codes.h"
#include "ui/input.h"
#include "hw/input/adb-keys.h"
#include "adb-internal.h"
#include "trace.h"
#include "qom/object.h"
OBJECT_DECLARE_TYPE(KBDState, ADBKeyboardClass, ADB_KEYBOARD)
struct KBDState {
/*< private >*/
ADBDevice parent_obj;
/*< public >*/
QemuInputHandlerState *hs;
uint8_t data[128];
int rptr, wptr, count;
};
struct ADBKeyboardClass {
/*< private >*/
ADBDeviceClass parent_class;
/*< public >*/
DeviceRealize parent_realize;
DeviceUnrealize parent_unrealize;
};
/* The adb keyboard doesn't have every key imaginable */
#define NO_KEY 0xff
int linux_to_adb_keycode[] = {
/* Make sure future additions are automatically set to NO_KEY */
[0 ... KEY_MAX] = NO_KEY,
[KEY_LEFTSHIFT] = ADB_KEY_LEFT_SHIFT,
[KEY_RIGHTSHIFT] = ADB_KEY_RIGHT_SHIFT,
[KEY_LEFTALT] = ADB_KEY_LEFT_OPTION,
[KEY_RIGHTALT] = ADB_KEY_RIGHT_OPTION,
[KEY_LEFTCTRL] = ADB_KEY_LEFT_CONTROL,
[KEY_RIGHTCTRL] = ADB_KEY_RIGHT_CONTROL,
[KEY_LEFTMETA] = ADB_KEY_COMMAND,
[KEY_RIGHTMETA] = ADB_KEY_COMMAND,
[KEY_SPACE] = ADB_KEY_SPACEBAR,
[KEY_ESC] = ADB_KEY_ESC,
[KEY_1] = ADB_KEY_1,
[KEY_2] = ADB_KEY_2,
[KEY_3] = ADB_KEY_3,
[KEY_4] = ADB_KEY_4,
[KEY_5] = ADB_KEY_5,
[KEY_6] = ADB_KEY_6,
[KEY_7] = ADB_KEY_7,
[KEY_8] = ADB_KEY_8,
[KEY_9] = ADB_KEY_9,
[KEY_0] = ADB_KEY_0,
[KEY_MINUS] = ADB_KEY_MINUS,
[KEY_EQUAL] = ADB_KEY_EQUAL,
[KEY_BACKSPACE] = ADB_KEY_DELETE,
[KEY_TAB] = ADB_KEY_TAB,
[KEY_Q] = ADB_KEY_Q,
[KEY_W] = ADB_KEY_W,
[KEY_E] = ADB_KEY_E,
[KEY_R] = ADB_KEY_R,
[KEY_T] = ADB_KEY_T,
[KEY_Y] = ADB_KEY_Y,
[KEY_U] = ADB_KEY_U,
[KEY_I] = ADB_KEY_I,
[KEY_O] = ADB_KEY_O,
[KEY_P] = ADB_KEY_P,
[KEY_LEFTBRACE] = ADB_KEY_LEFT_BRACKET,
[KEY_RIGHTBRACE] = ADB_KEY_RIGHT_BRACKET,
[KEY_ENTER] = ADB_KEY_RETURN,
[KEY_A] = ADB_KEY_A,
[KEY_S] = ADB_KEY_S,
[KEY_D] = ADB_KEY_D,
[KEY_F] = ADB_KEY_F,
[KEY_G] = ADB_KEY_G,
[KEY_H] = ADB_KEY_H,
[KEY_J] = ADB_KEY_J,
[KEY_K] = ADB_KEY_K,
[KEY_L] = ADB_KEY_L,
[KEY_SEMICOLON] = ADB_KEY_SEMICOLON,
[KEY_APOSTROPHE] = ADB_KEY_APOSTROPHE,
[KEY_GRAVE] = ADB_KEY_GRAVE_ACCENT,
[KEY_BACKSLASH] = ADB_KEY_BACKSLASH,
[KEY_Z] = ADB_KEY_Z,
[KEY_X] = ADB_KEY_X,
[KEY_C] = ADB_KEY_C,
[KEY_V] = ADB_KEY_V,
[KEY_B] = ADB_KEY_B,
[KEY_N] = ADB_KEY_N,
[KEY_M] = ADB_KEY_M,
[KEY_COMMA] = ADB_KEY_COMMA,
[KEY_DOT] = ADB_KEY_PERIOD,
[KEY_SLASH] = ADB_KEY_FORWARD_SLASH,
[KEY_CAPSLOCK] = ADB_KEY_CAPS_LOCK,
[KEY_F1] = ADB_KEY_F1,
[KEY_F2] = ADB_KEY_F2,
[KEY_F3] = ADB_KEY_F3,
[KEY_F4] = ADB_KEY_F4,
[KEY_F5] = ADB_KEY_F5,
[KEY_F6] = ADB_KEY_F6,
[KEY_F7] = ADB_KEY_F7,
[KEY_F8] = ADB_KEY_F8,
[KEY_F9] = ADB_KEY_F9,
[KEY_F10] = ADB_KEY_F10,
[KEY_F11] = ADB_KEY_F11,
[KEY_F12] = ADB_KEY_F12,
[KEY_SYSRQ] = ADB_KEY_F13,
[KEY_SCROLLLOCK] = ADB_KEY_F14,
[KEY_PAUSE] = ADB_KEY_F15,
[KEY_NUMLOCK] = ADB_KEY_KP_CLEAR,
[KEY_KPEQUAL] = ADB_KEY_KP_EQUAL,
[KEY_KPSLASH] = ADB_KEY_KP_DIVIDE,
[KEY_KPASTERISK] = ADB_KEY_KP_MULTIPLY,
[KEY_KPMINUS] = ADB_KEY_KP_SUBTRACT,
[KEY_KPPLUS] = ADB_KEY_KP_PLUS,
[KEY_KPENTER] = ADB_KEY_KP_ENTER,
[KEY_KPDOT] = ADB_KEY_KP_PERIOD,
[KEY_KP0] = ADB_KEY_KP_0,
[KEY_KP1] = ADB_KEY_KP_1,
[KEY_KP2] = ADB_KEY_KP_2,
[KEY_KP3] = ADB_KEY_KP_3,
[KEY_KP4] = ADB_KEY_KP_4,
[KEY_KP5] = ADB_KEY_KP_5,
[KEY_KP6] = ADB_KEY_KP_6,
[KEY_KP7] = ADB_KEY_KP_7,
[KEY_KP8] = ADB_KEY_KP_8,
[KEY_KP9] = ADB_KEY_KP_9,
[KEY_UP] = ADB_KEY_UP,
[KEY_DOWN] = ADB_KEY_DOWN,
[KEY_LEFT] = ADB_KEY_LEFT,
[KEY_RIGHT] = ADB_KEY_RIGHT,
[KEY_HELP] = ADB_KEY_HELP,
[KEY_INSERT] = ADB_KEY_HELP,
[KEY_DELETE] = ADB_KEY_FORWARD_DELETE,
[KEY_HOME] = ADB_KEY_HOME,
[KEY_END] = ADB_KEY_END,
[KEY_PAGEUP] = ADB_KEY_PAGE_UP,
[KEY_PAGEDOWN] = ADB_KEY_PAGE_DOWN,
[KEY_POWER] = ADB_KEY_POWER
};
static void adb_kbd_put_keycode(void *opaque, int keycode)
{
KBDState *s = opaque;
if (s->count < sizeof(s->data)) {
s->data[s->wptr] = keycode;
if (++s->wptr == sizeof(s->data)) {
s->wptr = 0;
}
s->count++;
}
}
static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
{
KBDState *s = ADB_KEYBOARD(d);
int keycode;
if (s->count == 0) {
return 0;
}
keycode = s->data[s->rptr];
s->rptr++;
if (s->rptr == sizeof(s->data)) {
s->rptr = 0;
}
s->count--;
/*
* The power key is the only two byte value key, so it is a special case.
* Since 0x7f is not a used keycode for ADB we overload it to indicate the
* power button when we're storing keycodes in our internal buffer, and
* expand it out to two bytes when we send to the guest.
*/
if (keycode == 0x7f) {
obuf[0] = 0x7f;
obuf[1] = 0x7f;
} else {
obuf[0] = keycode;
/* NOTE: the power key key-up is the two byte sequence 0xff 0xff;
* otherwise we could in theory send a second keycode in the second
* byte, but choose not to bother.
*/
obuf[1] = 0xff;
}
return 2;
}
static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
const uint8_t *buf, int len)
{
KBDState *s = ADB_KEYBOARD(d);
int cmd, reg, olen;
if ((buf[0] & 0x0f) == ADB_FLUSH) {
/* flush keyboard fifo */
s->wptr = s->rptr = s->count = 0;
return 0;
}
cmd = buf[0] & 0xc;
reg = buf[0] & 0x3;
olen = 0;
switch (cmd) {
case ADB_WRITEREG:
trace_adb_device_kbd_writereg(reg, buf[1]);
switch (reg) {
case 2:
/* LED status */
break;
case 3:
switch (buf[2]) {
case ADB_CMD_SELF_TEST:
break;
case ADB_CMD_CHANGE_ID:
case ADB_CMD_CHANGE_ID_AND_ACT:
case ADB_CMD_CHANGE_ID_AND_ENABLE:
d->devaddr = buf[1] & 0xf;
trace_adb_device_kbd_request_change_addr(d->devaddr);
break;
default:
d->devaddr = buf[1] & 0xf;
/*
* we support handlers:
* 1: Apple Standard Keyboard
* 2: Apple Extended Keyboard (LShift = RShift)
* 3: Apple Extended Keyboard (LShift != RShift)
*/
if (buf[2] == 1 || buf[2] == 2 || buf[2] == 3) {
d->handler = buf[2];
}
trace_adb_device_kbd_request_change_addr_and_handler(
d->devaddr, d->handler);
break;
}
}
break;
case ADB_READREG:
switch (reg) {
case 0:
olen = adb_kbd_poll(d, obuf);
break;
case 1:
break;
case 2:
obuf[0] = 0x00; /* XXX: check this */
obuf[1] = 0x07; /* led status */
olen = 2;
break;
case 3:
obuf[0] = d->devaddr;
obuf[1] = d->handler;
olen = 2;
break;
}
trace_adb_device_kbd_readreg(reg, obuf[0], obuf[1]);
break;
}
return olen;
}
static bool adb_kbd_has_data(ADBDevice *d)
{
KBDState *s = ADB_KEYBOARD(d);
return s->count > 0;
}
/* This is where keyboard events enter this file */
static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
QemuInputEvent *evt)
{
KBDState *s = (KBDState *)dev;
int keycode;
if (evt->key.key >= ARRAY_SIZE(linux_to_adb_keycode)) {
return;
}
/* FIXME: take handler into account when translating evt->key.key */
keycode = linux_to_adb_keycode[evt->key.key];
if (keycode == NO_KEY) { /* We don't want to send this to the guest */
trace_adb_device_kbd_no_key();
return;
}
if (evt->key.down == false) { /* if key release event */
keycode = keycode | 0x80; /* create keyboard break code */
}
adb_kbd_put_keycode(s, keycode);
}
static const VMStateDescription vmstate_adb_kbd = {
.name = "adb_kbd",
.version_id = 2,
.minimum_version_id = 2,
.fields = (const VMStateField[]) {
VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice),
VMSTATE_BUFFER(data, KBDState),
VMSTATE_INT32(rptr, KBDState),
VMSTATE_INT32(wptr, KBDState),
VMSTATE_INT32(count, KBDState),
VMSTATE_END_OF_LIST()
}
};
static void adb_kbd_reset(DeviceState *dev)
{
ADBDevice *d = ADB_DEVICE(dev);
KBDState *s = ADB_KEYBOARD(dev);
d->handler = 1;
d->devaddr = ADB_DEVID_KEYBOARD;
memset(s->data, 0, sizeof(s->data));
s->rptr = 0;
s->wptr = 0;
s->count = 0;
}
static const QemuInputHandler adb_keyboard_handler = {
.name = "QEMU ADB Keyboard",
.mask = INPUT_EVENT_MASK_KEY,
.event = adb_keyboard_event,
};
static void adb_kbd_realizefn(DeviceState *dev, Error **errp)
{
KBDState *s = ADB_KEYBOARD(dev);
ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev);
akc->parent_realize(dev, errp);
s->hs = qemu_input_handler_register(dev, &adb_keyboard_handler);
}
static void adb_kbd_unrealizefn(DeviceState *dev)
{
KBDState *s = ADB_KEYBOARD(dev);
ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev);
g_clear_pointer(&s->hs, qemu_input_handler_unregister);
akc->parent_unrealize(dev);
}
static void adb_kbd_initfn(Object *obj)
{
ADBDevice *d = ADB_DEVICE(obj);
d->devaddr = ADB_DEVID_KEYBOARD;
}
static void adb_kbd_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
ADBKeyboardClass *akc = ADB_KEYBOARD_CLASS(oc);
device_class_set_parent_realize(dc, adb_kbd_realizefn,
&akc->parent_realize);
device_class_set_parent_unrealize(dc, adb_kbd_unrealizefn,
&akc->parent_unrealize);
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
adc->devreq = adb_kbd_request;
adc->devhasdata = adb_kbd_has_data;
device_class_set_legacy_reset(dc, adb_kbd_reset);
dc->vmsd = &vmstate_adb_kbd;
}
static const TypeInfo adb_kbd_type_info = {
.name = TYPE_ADB_KEYBOARD,
.parent = TYPE_ADB_DEVICE,
.instance_size = sizeof(KBDState),
.instance_init = adb_kbd_initfn,
.class_init = adb_kbd_class_init,
.class_size = sizeof(ADBKeyboardClass),
};
static void adb_kbd_register_types(void)
{
type_register_static(&adb_kbd_type_info);
}
type_init(adb_kbd_register_types)
+316
View File
@@ -0,0 +1,316 @@
/*
* QEMU ADB mouse support
*
* Copyright (c) 2004 Fabrice Bellard
*
* 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.
*/
#include "qemu/osdep.h"
#include "ui/console.h"
#include "hw/input/adb.h"
#include "migration/vmstate.h"
#include "qemu/module.h"
#include "adb-internal.h"
#include "trace.h"
#include "qom/object.h"
OBJECT_DECLARE_TYPE(MouseState, ADBMouseClass, ADB_MOUSE)
struct MouseState {
/*< public >*/
ADBDevice parent_obj;
/*< private >*/
QemuInputHandlerState *hs;
int buttons_state, last_buttons_state;
int dx, dy, dz;
};
struct ADBMouseClass {
/*< public >*/
ADBDeviceClass parent_class;
/*< private >*/
DeviceRealize parent_realize;
};
#define ADB_MOUSE_BUTTON_LEFT 0x01
#define ADB_MOUSE_BUTTON_RIGHT 0x02
static void adb_mouse_handle_event(DeviceState *dev, QemuConsole *src,
QemuInputEvent *evt)
{
MouseState *s = (MouseState *)dev;
static const int bmap[INPUT_BUTTON__MAX] = {
[INPUT_BUTTON_LEFT] = ADB_MOUSE_BUTTON_LEFT,
[INPUT_BUTTON_RIGHT] = ADB_MOUSE_BUTTON_RIGHT,
};
switch (evt->type) {
case INPUT_EVENT_KIND_REL:
if (evt->rel.axis == INPUT_AXIS_X) {
s->dx += evt->rel.value;
} else if (evt->rel.axis == INPUT_AXIS_Y) {
s->dy += evt->rel.value;
}
break;
case INPUT_EVENT_KIND_BTN:
if (bmap[evt->btn.button]) {
if (evt->btn.down) {
s->buttons_state |= bmap[evt->btn.button];
} else {
s->buttons_state &= ~bmap[evt->btn.button];
}
}
break;
default:
/* keep gcc happy */
break;
}
}
static const QemuInputHandler adb_mouse_handler = {
.name = "QEMU ADB Mouse",
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
.event = adb_mouse_handle_event,
/*
* We do not need the .sync handler because unlike e.g. PS/2 where async
* mouse events are sent over the serial port, an ADB mouse is constantly
* polled by the host via the adb_mouse_poll() callback.
*/
};
static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
{
MouseState *s = ADB_MOUSE(d);
int dx, dy;
if (s->last_buttons_state == s->buttons_state &&
s->dx == 0 && s->dy == 0) {
return 0;
}
dx = s->dx;
if (dx < -63) {
dx = -63;
} else if (dx > 63) {
dx = 63;
}
dy = s->dy;
if (dy < -63) {
dy = -63;
} else if (dy > 63) {
dy = 63;
}
s->dx -= dx;
s->dy -= dy;
s->last_buttons_state = s->buttons_state;
dx &= 0x7f;
dy &= 0x7f;
if (!(s->buttons_state & ADB_MOUSE_BUTTON_LEFT)) {
dy |= 0x80;
}
if (!(s->buttons_state & ADB_MOUSE_BUTTON_RIGHT)) {
dx |= 0x80;
}
obuf[0] = dy;
obuf[1] = dx;
return 2;
}
static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
const uint8_t *buf, int len)
{
MouseState *s = ADB_MOUSE(d);
int cmd, reg, olen;
if ((buf[0] & 0x0f) == ADB_FLUSH) {
/* flush mouse fifo */
s->buttons_state = s->last_buttons_state;
s->dx = 0;
s->dy = 0;
s->dz = 0;
trace_adb_device_mouse_flush();
return 0;
}
cmd = buf[0] & 0xc;
reg = buf[0] & 0x3;
olen = 0;
switch (cmd) {
case ADB_WRITEREG:
trace_adb_device_mouse_writereg(reg, buf[1]);
switch (reg) {
case 2:
break;
case 3:
/*
* MacOS 9 has a bug in its ADB driver whereby after configuring
* the ADB bus devices it sends another write of invalid length
* to reg 3. Make sure we ignore it to prevent an address clash
* with the previous device.
*/
if (len != 3) {
return 0;
}
switch (buf[2]) {
case ADB_CMD_SELF_TEST:
break;
case ADB_CMD_CHANGE_ID:
case ADB_CMD_CHANGE_ID_AND_ACT:
case ADB_CMD_CHANGE_ID_AND_ENABLE:
d->devaddr = buf[1] & 0xf;
trace_adb_device_mouse_request_change_addr(d->devaddr);
break;
default:
d->devaddr = buf[1] & 0xf;
/*
* we support handlers:
* 0x01: Classic Apple Mouse Protocol / 100 cpi operations
* 0x02: Classic Apple Mouse Protocol / 200 cpi operations
* we don't support handlers (at least):
* 0x03: Mouse systems A3 trackball
* 0x04: Extended Apple Mouse Protocol
* 0x2f: Microspeed mouse
* 0x42: Macally
* 0x5f: Microspeed mouse
* 0x66: Microspeed mouse
*/
if (buf[2] == 1 || buf[2] == 2) {
d->handler = buf[2];
}
trace_adb_device_mouse_request_change_addr_and_handler(
d->devaddr, d->handler);
break;
}
}
break;
case ADB_READREG:
switch (reg) {
case 0:
olen = adb_mouse_poll(d, obuf);
break;
case 1:
break;
case 3:
obuf[0] = d->devaddr;
obuf[1] = d->handler;
olen = 2;
break;
}
trace_adb_device_mouse_readreg(reg, obuf[0], obuf[1]);
break;
}
return olen;
}
static bool adb_mouse_has_data(ADBDevice *d)
{
MouseState *s = ADB_MOUSE(d);
return !(s->last_buttons_state == s->buttons_state &&
s->dx == 0 && s->dy == 0);
}
static void adb_mouse_reset(DeviceState *dev)
{
ADBDevice *d = ADB_DEVICE(dev);
MouseState *s = ADB_MOUSE(dev);
d->handler = 2;
d->devaddr = ADB_DEVID_MOUSE;
s->last_buttons_state = s->buttons_state = 0;
s->dx = s->dy = s->dz = 0;
}
static const VMStateDescription vmstate_adb_mouse = {
.name = "adb_mouse",
.version_id = 2,
.minimum_version_id = 2,
.fields = (const VMStateField[]) {
VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device,
ADBDevice),
VMSTATE_INT32(buttons_state, MouseState),
VMSTATE_INT32(last_buttons_state, MouseState),
VMSTATE_INT32(dx, MouseState),
VMSTATE_INT32(dy, MouseState),
VMSTATE_INT32(dz, MouseState),
VMSTATE_END_OF_LIST()
}
};
static void adb_mouse_realizefn(DeviceState *dev, Error **errp)
{
MouseState *s = ADB_MOUSE(dev);
ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev);
amc->parent_realize(dev, errp);
s->hs = qemu_input_handler_register(dev, &adb_mouse_handler);
}
static void adb_mouse_initfn(Object *obj)
{
ADBDevice *d = ADB_DEVICE(obj);
d->devaddr = ADB_DEVID_MOUSE;
}
static void adb_mouse_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
ADBMouseClass *amc = ADB_MOUSE_CLASS(oc);
device_class_set_parent_realize(dc, adb_mouse_realizefn,
&amc->parent_realize);
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
adc->devreq = adb_mouse_request;
adc->devhasdata = adb_mouse_has_data;
device_class_set_legacy_reset(dc, adb_mouse_reset);
dc->vmsd = &vmstate_adb_mouse;
}
static const TypeInfo adb_mouse_type_info = {
.name = TYPE_ADB_MOUSE,
.parent = TYPE_ADB_DEVICE,
.instance_size = sizeof(MouseState),
.instance_init = adb_mouse_initfn,
.class_init = adb_mouse_class_init,
.class_size = sizeof(ADBMouseClass),
};
static void adb_mouse_register_types(void)
{
type_register_static(&adb_mouse_type_info);
}
type_init(adb_mouse_register_types)
+325
View File
@@ -0,0 +1,325 @@
/*
* QEMU ADB support
*
* Copyright (c) 2004 Fabrice Bellard
*
* 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.
*/
#include "qemu/osdep.h"
#include "hw/input/adb.h"
#include "hw/core/qdev-properties.h"
#include "migration/vmstate.h"
#include "qemu/module.h"
#include "qemu/timer.h"
#include "adb-internal.h"
#include "trace.h"
/* error codes */
#define ADB_RET_NOTPRESENT (-2)
static const char *adb_commands[] = {
"RESET", "FLUSH", "(Reserved 0x2)", "(Reserved 0x3)",
"Reserved (0x4)", "(Reserved 0x5)", "(Reserved 0x6)", "(Reserved 0x7)",
"LISTEN r0", "LISTEN r1", "LISTEN r2", "LISTEN r3",
"TALK r0", "TALK r1", "TALK r2", "TALK r3",
};
static void adb_device_reset(ADBDevice *d)
{
device_cold_reset(DEVICE(d));
}
static int do_adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf,
int len)
{
ADBDevice *d;
ADBDeviceClass *adc;
int devaddr, cmd, olen, i;
cmd = buf[0] & 0xf;
if (cmd == ADB_BUSRESET) {
for (i = 0; i < s->nb_devices; i++) {
d = s->devices[i];
adb_device_reset(d);
}
s->status = 0;
return 0;
}
s->pending = 0;
for (i = 0; i < s->nb_devices; i++) {
d = s->devices[i];
adc = ADB_DEVICE_GET_CLASS(d);
if (adc->devhasdata(d)) {
s->pending |= (1 << d->devaddr);
}
}
s->status = 0;
devaddr = buf[0] >> 4;
for (i = 0; i < s->nb_devices; i++) {
d = s->devices[i];
adc = ADB_DEVICE_GET_CLASS(d);
if (d->devaddr == devaddr) {
olen = adc->devreq(d, obuf, buf, len);
if (!olen) {
s->status |= ADB_STATUS_BUSTIMEOUT;
}
return olen;
}
}
s->status |= ADB_STATUS_BUSTIMEOUT;
return ADB_RET_NOTPRESENT;
}
int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
{
int ret;
trace_adb_bus_request(buf[0] >> 4, adb_commands[buf[0] & 0xf], len);
assert(s->autopoll_blocked);
ret = do_adb_request(s, obuf, buf, len);
trace_adb_bus_request_done(buf[0] >> 4, adb_commands[buf[0] & 0xf], ret);
return ret;
}
int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
{
ADBDevice *d;
int olen, i;
uint8_t buf[1];
olen = 0;
for (i = 0; i < s->nb_devices; i++) {
if (s->poll_index >= s->nb_devices) {
s->poll_index = 0;
}
d = s->devices[s->poll_index];
if ((1 << d->devaddr) & poll_mask) {
buf[0] = ADB_READREG | (d->devaddr << 4);
olen = do_adb_request(s, obuf + 1, buf, 1);
/* if there is data, we poll again the same device */
if (olen > 0) {
s->status |= ADB_STATUS_POLLREPLY;
obuf[0] = buf[0];
olen++;
return olen;
}
}
s->poll_index++;
}
return olen;
}
void adb_set_autopoll_enabled(ADBBusState *s, bool enabled)
{
if (s->autopoll_enabled != enabled) {
s->autopoll_enabled = enabled;
if (s->autopoll_enabled) {
timer_mod(s->autopoll_timer,
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
s->autopoll_rate_ms);
} else {
timer_del(s->autopoll_timer);
}
}
}
void adb_set_autopoll_rate_ms(ADBBusState *s, int rate_ms)
{
s->autopoll_rate_ms = rate_ms;
if (s->autopoll_enabled) {
timer_mod(s->autopoll_timer,
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
s->autopoll_rate_ms);
}
}
void adb_set_autopoll_mask(ADBBusState *s, uint16_t mask)
{
if (s->autopoll_mask != mask) {
s->autopoll_mask = mask;
if (s->autopoll_enabled && s->autopoll_mask) {
timer_mod(s->autopoll_timer,
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
s->autopoll_rate_ms);
} else {
timer_del(s->autopoll_timer);
}
}
}
void adb_autopoll_block(ADBBusState *s)
{
s->autopoll_blocked = true;
trace_adb_bus_autopoll_block(s->autopoll_blocked);
if (s->autopoll_enabled) {
timer_del(s->autopoll_timer);
}
}
void adb_autopoll_unblock(ADBBusState *s)
{
s->autopoll_blocked = false;
trace_adb_bus_autopoll_block(s->autopoll_blocked);
if (s->autopoll_enabled) {
timer_mod(s->autopoll_timer,
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
s->autopoll_rate_ms);
}
}
static void adb_autopoll(void *opaque)
{
ADBBusState *s = opaque;
if (!s->autopoll_blocked) {
trace_adb_bus_autopoll_cb(s->autopoll_mask);
s->autopoll_cb(s->autopoll_cb_opaque);
trace_adb_bus_autopoll_cb_done(s->autopoll_mask);
}
timer_mod(s->autopoll_timer,
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
s->autopoll_rate_ms);
}
void adb_register_autopoll_callback(ADBBusState *s, void (*cb)(void *opaque),
void *opaque)
{
s->autopoll_cb = cb;
s->autopoll_cb_opaque = opaque;
}
static const VMStateDescription vmstate_adb_bus = {
.name = "adb_bus",
.version_id = 0,
.minimum_version_id = 0,
.fields = (const VMStateField[]) {
VMSTATE_TIMER_PTR(autopoll_timer, ADBBusState),
VMSTATE_BOOL(autopoll_enabled, ADBBusState),
VMSTATE_UINT8(autopoll_rate_ms, ADBBusState),
VMSTATE_UINT16(autopoll_mask, ADBBusState),
VMSTATE_BOOL(autopoll_blocked, ADBBusState),
VMSTATE_END_OF_LIST()
}
};
static void adb_bus_reset_hold(Object *obj, ResetType type)
{
ADBBusState *adb_bus = ADB_BUS(obj);
adb_bus->autopoll_enabled = false;
adb_bus->autopoll_mask = 0xffff;
adb_bus->autopoll_rate_ms = 20;
}
static void adb_bus_realize(BusState *qbus, Error **errp)
{
ADBBusState *adb_bus = ADB_BUS(qbus);
adb_bus->autopoll_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, adb_autopoll,
adb_bus);
vmstate_register_any(NULL, &vmstate_adb_bus, adb_bus);
}
static void adb_bus_unrealize(BusState *qbus)
{
ADBBusState *adb_bus = ADB_BUS(qbus);
timer_del(adb_bus->autopoll_timer);
vmstate_unregister(NULL, &vmstate_adb_bus, adb_bus);
}
static void adb_bus_class_init(ObjectClass *klass, const void *data)
{
BusClass *k = BUS_CLASS(klass);
ResettableClass *rc = RESETTABLE_CLASS(klass);
k->realize = adb_bus_realize;
k->unrealize = adb_bus_unrealize;
rc->phases.hold = adb_bus_reset_hold;
}
static const TypeInfo adb_bus_type_info = {
.name = TYPE_ADB_BUS,
.parent = TYPE_BUS,
.instance_size = sizeof(ADBBusState),
.class_init = adb_bus_class_init,
};
const VMStateDescription vmstate_adb_device = {
.name = "adb_device",
.version_id = 0,
.minimum_version_id = 0,
.fields = (const VMStateField[]) {
VMSTATE_INT32(devaddr, ADBDevice),
VMSTATE_INT32(handler, ADBDevice),
VMSTATE_END_OF_LIST()
}
};
static void adb_device_realizefn(DeviceState *dev, Error **errp)
{
ADBDevice *d = ADB_DEVICE(dev);
ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
if (bus->nb_devices >= MAX_ADB_DEVICES) {
return;
}
bus->devices[bus->nb_devices++] = d;
}
static void adb_device_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
dc->realize = adb_device_realizefn;
dc->bus_type = TYPE_ADB_BUS;
}
static const TypeInfo adb_device_type_info = {
.name = TYPE_ADB_DEVICE,
.parent = TYPE_DEVICE,
.class_size = sizeof(ADBDeviceClass),
.instance_size = sizeof(ADBDevice),
.abstract = true,
.class_init = adb_device_class_init,
};
static void adb_register_types(void)
{
type_register_static(&adb_bus_type_info);
type_register_static(&adb_device_type_info);
}
type_init(adb_register_types)
+621
View File
@@ -0,0 +1,621 @@
/*
* QEMU HID devices
*
* Copyright (c) 2005 Fabrice Bellard
* Copyright (c) 2007 OpenMoko, Inc. ([email protected])
*
* 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.
*/
#include "qemu/osdep.h"
#include "ui/console.h"
#include "ui/input.h"
#include "qemu/timer.h"
#include "hw/input/hid.h"
#include "migration/vmstate.h"
#include "trace.h"
#define HID_USAGE_ERROR_ROLLOVER 0x01
#define HID_USAGE_POSTFAIL 0x02
#define HID_USAGE_ERROR_UNDEFINED 0x03
/* Indices are QEMU keycodes, values are from HID Usage Table. Indices
* above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d. */
static const uint8_t hid_usage_keys[0x100] = {
0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
0xe2, 0x2c, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
0x5a, 0x5b, 0x62, 0x63, 0x46, 0x00, 0x64, 0x44,
0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
0x88, 0x00, 0x00, 0x87, 0x00, 0x00, 0x00, 0x00,
0x00, 0x8a, 0x00, 0x8b, 0x00, 0x89, 0xe7, 0x65,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x48, 0x4a,
0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x66, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
bool hid_has_events(HIDState *hs)
{
return hs->n > 0 || hs->idle_pending;
}
static void hid_idle_timer(void *opaque)
{
HIDState *hs = opaque;
hs->idle_pending = true;
hs->event(hs);
}
static void hid_del_idle_timer(HIDState *hs)
{
if (hs->idle_timer) {
timer_free(hs->idle_timer);
hs->idle_timer = NULL;
}
}
void hid_set_next_idle(HIDState *hs)
{
if (hs->idle) {
uint64_t expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
NANOSECONDS_PER_SECOND * hs->idle * 4 / 1000;
if (!hs->idle_timer) {
hs->idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hid_idle_timer, hs);
}
timer_mod_ns(hs->idle_timer, expire_time);
} else {
hid_del_idle_timer(hs);
}
}
static void hid_pointer_event(DeviceState *dev, QemuConsole *src,
QemuInputEvent *evt)
{
static const int bmap[INPUT_BUTTON__MAX] = {
[INPUT_BUTTON_LEFT] = 0x01,
[INPUT_BUTTON_RIGHT] = 0x02,
[INPUT_BUTTON_MIDDLE] = 0x04,
[INPUT_BUTTON_SIDE] = 0x08,
[INPUT_BUTTON_EXTRA] = 0x10,
};
HIDState *hs = (HIDState *)dev;
HIDPointerEvent *e;
assert(hs->n < QUEUE_LENGTH);
e = &hs->ptr.queue[(hs->head + hs->n) & QUEUE_MASK];
switch (evt->type) {
case INPUT_EVENT_KIND_REL:
if (evt->rel.axis == INPUT_AXIS_X) {
e->xdx += evt->rel.value;
} else if (evt->rel.axis == INPUT_AXIS_Y) {
e->ydy += evt->rel.value;
}
break;
case INPUT_EVENT_KIND_ABS:
if (evt->abs.axis == INPUT_AXIS_X) {
e->xdx = evt->abs.value;
} else if (evt->abs.axis == INPUT_AXIS_Y) {
e->ydy = evt->abs.value;
}
break;
case INPUT_EVENT_KIND_BTN:
if (evt->btn.down) {
e->buttons_state |= bmap[evt->btn.button];
if (evt->btn.button == INPUT_BUTTON_WHEEL_UP) {
e->dz--;
} else if (evt->btn.button == INPUT_BUTTON_WHEEL_DOWN) {
e->dz++;
}
} else {
e->buttons_state &= ~bmap[evt->btn.button];
}
break;
default:
/* keep gcc happy */
break;
}
}
static void hid_pointer_sync(DeviceState *dev)
{
HIDState *hs = (HIDState *)dev;
HIDPointerEvent *prev, *curr, *next;
bool event_compression = false;
if (hs->n == QUEUE_LENGTH-1) {
/*
* Queue full. We are losing information, but we at least
* keep track of most recent button state.
*/
return;
}
prev = &hs->ptr.queue[(hs->head + hs->n - 1) & QUEUE_MASK];
curr = &hs->ptr.queue[(hs->head + hs->n) & QUEUE_MASK];
next = &hs->ptr.queue[(hs->head + hs->n + 1) & QUEUE_MASK];
if (hs->n > 0) {
/*
* No button state change between previous and current event
* (and previous wasn't seen by the guest yet), so there is
* motion information only and we can combine the two event
* into one.
*/
if (curr->buttons_state == prev->buttons_state) {
event_compression = true;
}
}
if (event_compression) {
/* add current motion to previous, clear current */
if (hs->kind == HID_MOUSE) {
prev->xdx += curr->xdx;
curr->xdx = 0;
prev->ydy += curr->ydy;
curr->ydy = 0;
} else {
prev->xdx = curr->xdx;
prev->ydy = curr->ydy;
}
prev->dz += curr->dz;
curr->dz = 0;
} else {
/* prepare next (clear rel, copy abs + btns) */
if (hs->kind == HID_MOUSE) {
next->xdx = 0;
next->ydy = 0;
} else {
next->xdx = curr->xdx;
next->ydy = curr->ydy;
}
next->dz = 0;
next->buttons_state = curr->buttons_state;
/* make current guest visible, notify guest */
hs->n++;
hs->event(hs);
}
}
static void hid_keyboard_event(DeviceState *dev, QemuConsole *src,
QemuInputEvent *evt)
{
HIDState *hs = (HIDState *)dev;
int scancodes[3], i, count;
int slot;
count = qemu_input_linux_to_scancode(evt->key.key, evt->key.down,
scancodes);
if (hs->n + count > QUEUE_LENGTH) {
trace_hid_kbd_queue_full();
return;
}
for (i = 0; i < count; i++) {
slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
hs->kbd.keycodes[slot] = scancodes[i];
}
hs->event(hs);
}
static void hid_keyboard_process_keycode(HIDState *hs)
{
uint8_t hid_code, index, key;
int i, keycode, slot;
if (hs->n == 0) {
return;
}
slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
keycode = hs->kbd.keycodes[slot];
if (!hs->n) {
trace_hid_kbd_queue_empty();
}
key = keycode & 0x7f;
index = key | ((hs->kbd.modifiers & (1 << 8)) >> 1);
hid_code = hid_usage_keys[index];
hs->kbd.modifiers &= ~(1 << 8);
switch (hid_code) {
case 0x00:
return;
case 0xe0:
assert(key == 0x1d);
if (hs->kbd.modifiers & (1 << 9)) {
/* The hid_codes for the 0xe1/0x1d scancode sequence are 0xe9/0xe0.
* Here we're processing the second hid_code. By dropping bit 9
* and setting bit 8, the scancode after 0x1d will access the
* second half of the table.
*/
hs->kbd.modifiers ^= (1 << 8) | (1 << 9);
return;
}
/* fall through to process Ctrl_L */
case 0xe1 ... 0xe7:
/* Ctrl_L/Ctrl_R, Shift_L/Shift_R, Alt_L/Alt_R, Win_L/Win_R.
* Handle releases here, or fall through to process presses.
*/
if (keycode & (1 << 7)) {
hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
return;
}
/* fall through */
case 0xe8 ... 0xe9:
/* USB modifiers are just 1 byte long. Bits 8 and 9 of
* hs->kbd.modifiers implement a state machine that detects the
* 0xe0 and 0xe1/0x1d sequences. These bits do not follow the
* usual rules where bit 7 marks released keys; they are cleared
* elsewhere in the function as the state machine dictates.
*/
hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
return;
case 0xea ... 0xef:
abort();
default:
break;
}
if (keycode & (1 << 7)) {
for (i = hs->kbd.keys - 1; i >= 0; i--) {
if (hs->kbd.key[i] == hid_code) {
hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
hs->kbd.key[hs->kbd.keys] = 0x00;
break;
}
}
if (i < 0) {
return;
}
} else {
for (i = hs->kbd.keys - 1; i >= 0; i--) {
if (hs->kbd.key[i] == hid_code) {
break;
}
}
if (i < 0) {
if (hs->kbd.keys < sizeof(hs->kbd.key)) {
hs->kbd.key[hs->kbd.keys++] = hid_code;
}
} else {
return;
}
}
}
static inline int int_clamp(int val, int vmin, int vmax)
{
if (val < vmin) {
return vmin;
} else if (val > vmax) {
return vmax;
} else {
return val;
}
}
void hid_pointer_activate(HIDState *hs)
{
if (!hs->ptr.mouse_grabbed) {
qemu_input_handler_activate(hs->s);
hs->ptr.mouse_grabbed = 1;
}
}
int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
{
int dx, dy, dz, l;
int index;
HIDPointerEvent *e;
hs->idle_pending = false;
hid_pointer_activate(hs);
/* When the buffer is empty, return the last event. Relative
movements will all be zero. */
index = (hs->n ? hs->head : hs->head - 1);
e = &hs->ptr.queue[index & QUEUE_MASK];
if (hs->kind == HID_MOUSE) {
dx = int_clamp(e->xdx, -127, 127);
dy = int_clamp(e->ydy, -127, 127);
e->xdx -= dx;
e->ydy -= dy;
} else {
dx = e->xdx;
dy = e->ydy;
}
dz = int_clamp(e->dz, -127, 127);
e->dz -= dz;
if (hs->n &&
!e->dz &&
(hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
/* that deals with this event */
QUEUE_INCR(hs->head);
hs->n--;
}
/* Appears we have to invert the wheel direction */
dz = 0 - dz;
l = 0;
switch (hs->kind) {
case HID_MOUSE:
if (len > l) {
buf[l++] = e->buttons_state;
}
if (len > l) {
buf[l++] = dx;
}
if (len > l) {
buf[l++] = dy;
}
if (len > l) {
buf[l++] = dz;
}
break;
case HID_TABLET:
if (len > l) {
buf[l++] = e->buttons_state;
}
if (len > l) {
buf[l++] = dx & 0xff;
}
if (len > l) {
buf[l++] = dx >> 8;
}
if (len > l) {
buf[l++] = dy & 0xff;
}
if (len > l) {
buf[l++] = dy >> 8;
}
if (len > l) {
buf[l++] = dz;
}
break;
default:
abort();
}
return l;
}
int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
{
hs->idle_pending = false;
if (len < 2) {
return 0;
}
hid_keyboard_process_keycode(hs);
buf[0] = hs->kbd.modifiers & 0xff;
buf[1] = 0;
if (hs->kbd.keys > 6) {
memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
} else {
memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
}
return MIN(8, len);
}
int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
{
if (len > 0) {
int ledstate = 0;
/* 0x01: Num Lock LED
* 0x02: Caps Lock LED
* 0x04: Scroll Lock LED
* 0x08: Compose LED
* 0x10: Kana LED */
hs->kbd.leds = buf[0];
if (hs->kbd.leds & 0x04) {
ledstate |= QEMU_SCROLL_LOCK_LED;
}
if (hs->kbd.leds & 0x01) {
ledstate |= QEMU_NUM_LOCK_LED;
}
if (hs->kbd.leds & 0x02) {
ledstate |= QEMU_CAPS_LOCK_LED;
}
qemu_input_handler_set_leds_mask(hs->s, ledstate);
return 1;
}
return 0;
}
void hid_reset(HIDState *hs)
{
switch (hs->kind) {
case HID_KEYBOARD:
memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
hs->kbd.keys = 0;
hs->kbd.modifiers = 0;
break;
case HID_MOUSE:
case HID_TABLET:
memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
break;
}
hs->head = 0;
hs->n = 0;
hs->protocol = 1;
hs->idle = 0;
hs->idle_pending = false;
hid_del_idle_timer(hs);
}
void hid_free(HIDState *hs)
{
qemu_input_handler_unregister(hs->s);
hid_del_idle_timer(hs);
}
static const QemuInputHandler hid_keyboard_handler = {
.name = "QEMU HID Keyboard",
.mask = INPUT_EVENT_MASK_KEY,
.event = hid_keyboard_event,
};
static const QemuInputHandler hid_mouse_handler = {
.name = "QEMU HID Mouse",
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
.event = hid_pointer_event,
.sync = hid_pointer_sync,
};
static const QemuInputHandler hid_tablet_handler = {
.name = "QEMU HID Tablet",
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
.event = hid_pointer_event,
.sync = hid_pointer_sync,
};
void hid_init(HIDState *hs, int kind, HIDEventFunc event)
{
hs->kind = kind;
hs->event = event;
if (hs->kind == HID_KEYBOARD) {
hs->s = qemu_input_handler_register((DeviceState *)hs,
&hid_keyboard_handler);
qemu_input_handler_activate(hs->s);
} else if (hs->kind == HID_MOUSE) {
hs->s = qemu_input_handler_register((DeviceState *)hs,
&hid_mouse_handler);
} else if (hs->kind == HID_TABLET) {
hs->s = qemu_input_handler_register((DeviceState *)hs,
&hid_tablet_handler);
}
}
static int hid_post_load(void *opaque, int version_id)
{
HIDState *s = opaque;
hid_set_next_idle(s);
if (s->n == QUEUE_LENGTH && (s->kind == HID_TABLET ||
s->kind == HID_MOUSE)) {
/*
* Handle ptr device migration from old qemu with full queue.
*
* Throw away everything but the last event, so we propagate
* at least the current button state to the guest. Also keep
* current position for the tablet, signal "no motion" for the
* mouse.
*/
HIDPointerEvent evt;
evt = s->ptr.queue[(s->head+s->n) & QUEUE_MASK];
if (s->kind == HID_MOUSE) {
evt.xdx = 0;
evt.ydy = 0;
}
s->ptr.queue[0] = evt;
s->head = 0;
s->n = 1;
}
return 0;
}
static const VMStateDescription vmstate_hid_ptr_queue = {
.name = "HIDPointerEventQueue",
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_INT32(xdx, HIDPointerEvent),
VMSTATE_INT32(ydy, HIDPointerEvent),
VMSTATE_INT32(dz, HIDPointerEvent),
VMSTATE_INT32(buttons_state, HIDPointerEvent),
VMSTATE_END_OF_LIST()
}
};
const VMStateDescription vmstate_hid_ptr_device = {
.name = "HIDPointerDevice",
.version_id = 1,
.minimum_version_id = 1,
.post_load = hid_post_load,
.fields = (const VMStateField[]) {
VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
vmstate_hid_ptr_queue, HIDPointerEvent),
VMSTATE_UINT32(head, HIDState),
VMSTATE_UINT32(n, HIDState),
VMSTATE_INT32(protocol, HIDState),
VMSTATE_UINT8(idle, HIDState),
VMSTATE_END_OF_LIST(),
}
};
const VMStateDescription vmstate_hid_keyboard_device = {
.name = "HIDKeyboardDevice",
.version_id = 1,
.minimum_version_id = 1,
.post_load = hid_post_load,
.fields = (const VMStateField[]) {
VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
VMSTATE_UINT32(head, HIDState),
VMSTATE_UINT32(n, HIDState),
VMSTATE_UINT16(kbd.modifiers, HIDState),
VMSTATE_UINT8(kbd.leds, HIDState),
VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
VMSTATE_INT32(kbd.keys, HIDState),
VMSTATE_INT32(protocol, HIDState),
VMSTATE_UINT8(idle, HIDState),
VMSTATE_END_OF_LIST(),
}
};
+475
View File
@@ -0,0 +1,475 @@
/*
* QEMU HP Lasi PS/2 interface emulation
*
* Copyright (c) 2019 Sven Schnelle
*
* 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.
*/
#include "qemu/osdep.h"
#include "qemu/log.h"
#include "hw/core/qdev-properties.h"
#include "hw/core/sysbus.h"
#include "hw/input/ps2.h"
#include "hw/input/lasips2.h"
#include "exec/hwaddr.h"
#include "trace.h"
#include "system/address-spaces.h"
#include "migration/vmstate.h"
#include "hw/core/irq.h"
#include "qapi/error.h"
static const VMStateDescription vmstate_lasips2_port = {
.name = "lasips2-port",
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_UINT8(control, LASIPS2Port),
VMSTATE_UINT8(buf, LASIPS2Port),
VMSTATE_BOOL(loopback_rbne, LASIPS2Port),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_lasips2 = {
.name = "lasips2",
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_UINT8(int_status, LASIPS2State),
VMSTATE_STRUCT(kbd_port.parent_obj, LASIPS2State, 1,
vmstate_lasips2_port, LASIPS2Port),
VMSTATE_STRUCT(mouse_port.parent_obj, LASIPS2State, 1,
vmstate_lasips2_port, LASIPS2Port),
VMSTATE_END_OF_LIST()
}
};
typedef enum {
REG_PS2_ID = 0,
REG_PS2_RCVDATA = 4,
REG_PS2_CONTROL = 8,
REG_PS2_STATUS = 12,
} lasips2_read_reg_t;
typedef enum {
REG_PS2_RESET = 0,
REG_PS2_XMTDATA = 4,
} lasips2_write_reg_t;
typedef enum {
LASIPS2_CONTROL_ENABLE = 0x01,
LASIPS2_CONTROL_LOOPBACK = 0x02,
LASIPS2_CONTROL_DIAG = 0x20,
LASIPS2_CONTROL_DATDIR = 0x40,
LASIPS2_CONTROL_CLKDIR = 0x80,
} lasips2_control_reg_t;
typedef enum {
LASIPS2_STATUS_RBNE = 0x01,
LASIPS2_STATUS_TBNE = 0x02,
LASIPS2_STATUS_TERR = 0x04,
LASIPS2_STATUS_PERR = 0x08,
LASIPS2_STATUS_CMPINTR = 0x10,
LASIPS2_STATUS_DATSHD = 0x40,
LASIPS2_STATUS_CLKSHD = 0x80,
} lasips2_status_reg_t;
static const char *lasips2_read_reg_name(uint64_t addr)
{
switch (addr & 0xc) {
case REG_PS2_ID:
return " PS2_ID";
case REG_PS2_RCVDATA:
return " PS2_RCVDATA";
case REG_PS2_CONTROL:
return " PS2_CONTROL";
case REG_PS2_STATUS:
return " PS2_STATUS";
default:
return "";
}
}
static const char *lasips2_write_reg_name(uint64_t addr)
{
switch (addr & 0x0c) {
case REG_PS2_RESET:
return " PS2_RESET";
case REG_PS2_XMTDATA:
return " PS2_XMTDATA";
case REG_PS2_CONTROL:
return " PS2_CONTROL";
default:
return "";
}
}
static void lasips2_update_irq(LASIPS2State *s)
{
int level = s->int_status ? 1 : 0;
trace_lasips2_intr(level);
qemu_set_irq(s->irq, level);
}
static void lasips2_set_irq(void *opaque, int n, int level)
{
LASIPS2State *s = LASIPS2(opaque);
if (level) {
s->int_status |= BIT(n);
} else {
s->int_status &= ~BIT(n);
}
lasips2_update_irq(s);
}
static void lasips2_reg_write(void *opaque, hwaddr addr, uint64_t val,
unsigned size)
{
LASIPS2Port *lp = LASIPS2_PORT(opaque);
trace_lasips2_reg_write(size, lp->id, addr,
lasips2_write_reg_name(addr), val);
switch (addr & 0xc) {
case REG_PS2_CONTROL:
lp->control = val;
break;
case REG_PS2_XMTDATA:
if (lp->control & LASIPS2_CONTROL_LOOPBACK) {
lp->buf = val;
lp->loopback_rbne = true;
qemu_set_irq(lp->irq, 1);
break;
}
if (lp->id) {
ps2_write_mouse(PS2_MOUSE_DEVICE(lp->ps2dev), val);
} else {
ps2_write_keyboard(PS2_KBD_DEVICE(lp->ps2dev), val);
}
break;
case REG_PS2_RESET:
break;
default:
qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
__func__, addr);
break;
}
}
static uint64_t lasips2_reg_read(void *opaque, hwaddr addr, unsigned size)
{
LASIPS2Port *lp = LASIPS2_PORT(opaque);
uint64_t ret = 0;
switch (addr & 0xc) {
case REG_PS2_ID:
ret = lp->id;
break;
case REG_PS2_RCVDATA:
if (lp->control & LASIPS2_CONTROL_LOOPBACK) {
lp->loopback_rbne = false;
qemu_set_irq(lp->irq, 0);
ret = lp->buf;
break;
}
ret = ps2_read_data(lp->ps2dev);
break;
case REG_PS2_CONTROL:
ret = lp->control;
break;
case REG_PS2_STATUS:
ret = LASIPS2_STATUS_DATSHD | LASIPS2_STATUS_CLKSHD;
if (lp->control & LASIPS2_CONTROL_DIAG) {
if (!(lp->control & LASIPS2_CONTROL_DATDIR)) {
ret &= ~LASIPS2_STATUS_DATSHD;
}
if (!(lp->control & LASIPS2_CONTROL_CLKDIR)) {
ret &= ~LASIPS2_STATUS_CLKSHD;
}
}
if (lp->control & LASIPS2_CONTROL_LOOPBACK) {
if (lp->loopback_rbne) {
ret |= LASIPS2_STATUS_RBNE;
}
} else {
if (!ps2_queue_empty(lp->ps2dev)) {
ret |= LASIPS2_STATUS_RBNE;
}
}
if (lp->lasips2->int_status) {
ret |= LASIPS2_STATUS_CMPINTR;
}
break;
default:
qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
__func__, addr);
break;
}
trace_lasips2_reg_read(size, lp->id, addr,
lasips2_read_reg_name(addr), ret);
return ret;
}
static const MemoryRegionOps lasips2_reg_ops = {
.read = lasips2_reg_read,
.write = lasips2_reg_write,
.impl = {
.min_access_size = 1,
.max_access_size = 4,
},
.endianness = DEVICE_BIG_ENDIAN,
};
static void lasips2_realize(DeviceState *dev, Error **errp)
{
LASIPS2State *s = LASIPS2(dev);
LASIPS2Port *lp;
lp = LASIPS2_PORT(&s->kbd_port);
if (!(qdev_realize(DEVICE(lp), NULL, errp))) {
return;
}
qdev_connect_gpio_out(DEVICE(lp), 0,
qdev_get_gpio_in_named(dev, "lasips2-port-input-irq",
lp->id));
lp = LASIPS2_PORT(&s->mouse_port);
if (!(qdev_realize(DEVICE(lp), NULL, errp))) {
return;
}
qdev_connect_gpio_out(DEVICE(lp), 0,
qdev_get_gpio_in_named(dev, "lasips2-port-input-irq",
lp->id));
}
static void lasips2_init(Object *obj)
{
LASIPS2State *s = LASIPS2(obj);
LASIPS2Port *lp;
object_initialize_child(obj, "lasips2-kbd-port", &s->kbd_port,
TYPE_LASIPS2_KBD_PORT);
object_initialize_child(obj, "lasips2-mouse-port", &s->mouse_port,
TYPE_LASIPS2_MOUSE_PORT);
lp = LASIPS2_PORT(&s->kbd_port);
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &lp->reg);
lp = LASIPS2_PORT(&s->mouse_port);
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &lp->reg);
sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
qdev_init_gpio_in_named(DEVICE(obj), lasips2_set_irq,
"lasips2-port-input-irq", 2);
}
static void lasips2_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
dc->realize = lasips2_realize;
dc->vmsd = &vmstate_lasips2;
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
}
static const TypeInfo lasips2_info = {
.name = TYPE_LASIPS2,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_init = lasips2_init,
.instance_size = sizeof(LASIPS2State),
.class_init = lasips2_class_init,
};
static void lasips2_port_set_irq(void *opaque, int n, int level)
{
LASIPS2Port *s = LASIPS2_PORT(opaque);
qemu_set_irq(s->irq, level);
}
static void lasips2_port_realize(DeviceState *dev, Error **errp)
{
LASIPS2Port *s = LASIPS2_PORT(dev);
qdev_connect_gpio_out(DEVICE(s->ps2dev), PS2_DEVICE_IRQ,
qdev_get_gpio_in_named(dev, "ps2-input-irq", 0));
}
static void lasips2_port_init(Object *obj)
{
LASIPS2Port *s = LASIPS2_PORT(obj);
qdev_init_gpio_out(DEVICE(obj), &s->irq, 1);
qdev_init_gpio_in_named(DEVICE(obj), lasips2_port_set_irq,
"ps2-input-irq", 1);
}
static void lasips2_port_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
/*
* The PS/2 mouse port is integreal part of LASI and can not be
* created by users without LASI.
*/
dc->user_creatable = false;
dc->realize = lasips2_port_realize;
}
static const TypeInfo lasips2_port_info = {
.name = TYPE_LASIPS2_PORT,
.parent = TYPE_DEVICE,
.instance_init = lasips2_port_init,
.instance_size = sizeof(LASIPS2Port),
.class_init = lasips2_port_class_init,
.class_size = sizeof(LASIPS2PortDeviceClass),
.abstract = true,
};
static void lasips2_kbd_port_realize(DeviceState *dev, Error **errp)
{
LASIPS2KbdPort *s = LASIPS2_KBD_PORT(dev);
LASIPS2Port *lp = LASIPS2_PORT(dev);
LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_GET_CLASS(lp);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->kbd), errp)) {
return;
}
lp->ps2dev = PS2_DEVICE(&s->kbd);
lpdc->parent_realize(dev, errp);
}
static void lasips2_kbd_port_init(Object *obj)
{
LASIPS2KbdPort *s = LASIPS2_KBD_PORT(obj);
LASIPS2Port *lp = LASIPS2_PORT(obj);
memory_region_init_io(&lp->reg, obj, &lasips2_reg_ops, lp, "lasips2-kbd",
0x100);
object_initialize_child(obj, "kbd", &s->kbd, TYPE_PS2_KBD_DEVICE);
lp->id = 0;
lp->lasips2 = container_of(s, LASIPS2State, kbd_port);
}
static void lasips2_kbd_port_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_CLASS(klass);
/*
* The PS/2 keyboard port is integreal part of LASI and can not be
* created by users without LASI.
*/
dc->user_creatable = false;
device_class_set_parent_realize(dc, lasips2_kbd_port_realize,
&lpdc->parent_realize);
}
static const TypeInfo lasips2_kbd_port_info = {
.name = TYPE_LASIPS2_KBD_PORT,
.parent = TYPE_LASIPS2_PORT,
.instance_size = sizeof(LASIPS2KbdPort),
.instance_init = lasips2_kbd_port_init,
.class_init = lasips2_kbd_port_class_init,
};
static void lasips2_mouse_port_realize(DeviceState *dev, Error **errp)
{
LASIPS2MousePort *s = LASIPS2_MOUSE_PORT(dev);
LASIPS2Port *lp = LASIPS2_PORT(dev);
LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_GET_CLASS(lp);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->mouse), errp)) {
return;
}
lp->ps2dev = PS2_DEVICE(&s->mouse);
lpdc->parent_realize(dev, errp);
}
static void lasips2_mouse_port_init(Object *obj)
{
LASIPS2MousePort *s = LASIPS2_MOUSE_PORT(obj);
LASIPS2Port *lp = LASIPS2_PORT(obj);
memory_region_init_io(&lp->reg, obj, &lasips2_reg_ops, lp, "lasips2-mouse",
0x100);
object_initialize_child(obj, "mouse", &s->mouse, TYPE_PS2_MOUSE_DEVICE);
lp->id = 1;
lp->lasips2 = container_of(s, LASIPS2State, mouse_port);
}
static void lasips2_mouse_port_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_CLASS(klass);
device_class_set_parent_realize(dc, lasips2_mouse_port_realize,
&lpdc->parent_realize);
}
static const TypeInfo lasips2_mouse_port_info = {
.name = TYPE_LASIPS2_MOUSE_PORT,
.parent = TYPE_LASIPS2_PORT,
.instance_size = sizeof(LASIPS2MousePort),
.instance_init = lasips2_mouse_port_init,
.class_init = lasips2_mouse_port_class_init,
};
static void lasips2_register_types(void)
{
type_register_static(&lasips2_info);
type_register_static(&lasips2_port_info);
type_register_static(&lasips2_kbd_port_info);
type_register_static(&lasips2_mouse_port_info);
}
type_init(lasips2_register_types)
+12
View File
@@ -0,0 +1,12 @@
system_ss.add(files('hid.c'))
system_ss.add(when: 'CONFIG_ADB', if_true: files('adb.c', 'adb-mouse.c', 'adb-kbd.c'))
system_ss.add(when: 'CONFIG_PCKBD', if_true: files('pckbd.c'))
system_ss.add(when: 'CONFIG_PL050', if_true: files('pl050.c'))
system_ss.add(when: 'CONFIG_PS2', if_true: files('ps2.c'))
system_ss.add(when: 'CONFIG_STELLARIS_GAMEPAD', if_true: files('stellaris_gamepad.c'))
system_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-input.c'))
system_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-input-hid.c'))
system_ss.add(when: 'CONFIG_VIRTIO_INPUT_HOST', if_true: files('virtio-input-host.c'))
system_ss.add(when: 'CONFIG_LASIPS2', if_true: files('lasips2.c'))
+973
View File
@@ -0,0 +1,973 @@
/*
* QEMU PC keyboard emulation
*
* Copyright (c) 2003 Fabrice Bellard
*
* 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.
*/
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "qemu/log.h"
#include "qemu/timer.h"
#include "qapi/error.h"
#include "hw/isa/isa.h"
#include "migration/vmstate.h"
#include "hw/acpi/acpi_aml_interface.h"
#include "hw/input/ps2.h"
#include "hw/core/irq.h"
#include "hw/input/i8042.h"
#include "hw/core/qdev-properties.h"
#include "system/reset.h"
#include "system/runstate.h"
#include "trace.h"
/* Keyboard Controller Commands */
/* Read mode bits */
#define KBD_CCMD_READ_MODE 0x20
/* Write mode bits */
#define KBD_CCMD_WRITE_MODE 0x60
/* Get controller version */
#define KBD_CCMD_GET_VERSION 0xA1
/* Disable mouse interface */
#define KBD_CCMD_MOUSE_DISABLE 0xA7
/* Enable mouse interface */
#define KBD_CCMD_MOUSE_ENABLE 0xA8
/* Mouse interface test */
#define KBD_CCMD_TEST_MOUSE 0xA9
/* Controller self test */
#define KBD_CCMD_SELF_TEST 0xAA
/* Keyboard interface test */
#define KBD_CCMD_KBD_TEST 0xAB
/* Keyboard interface disable */
#define KBD_CCMD_KBD_DISABLE 0xAD
/* Keyboard interface enable */
#define KBD_CCMD_KBD_ENABLE 0xAE
/* read input port */
#define KBD_CCMD_READ_INPORT 0xC0
/* read output port */
#define KBD_CCMD_READ_OUTPORT 0xD0
/* write output port */
#define KBD_CCMD_WRITE_OUTPORT 0xD1
#define KBD_CCMD_WRITE_OBUF 0xD2
/* Write to output buffer as if initiated by the auxiliary device */
#define KBD_CCMD_WRITE_AUX_OBUF 0xD3
/* Write the following byte to the mouse */
#define KBD_CCMD_WRITE_MOUSE 0xD4
/* HP vectra only ? */
#define KBD_CCMD_DISABLE_A20 0xDD
/* HP vectra only ? */
#define KBD_CCMD_ENABLE_A20 0xDF
/* Pulse bits 3-0 of the output port P2. */
#define KBD_CCMD_PULSE_BITS_3_0 0xF0
/* Pulse bit 0 of the output port P2 = CPU reset. */
#define KBD_CCMD_RESET 0xFE
/* Pulse no bits of the output port P2. */
#define KBD_CCMD_NO_OP 0xFF
/* Status Register Bits */
/* Keyboard output buffer full */
#define KBD_STAT_OBF 0x01
/* Keyboard input buffer full */
#define KBD_STAT_IBF 0x02
/* Self test successful */
#define KBD_STAT_SELFTEST 0x04
/* Last write was a command write (0=data) */
#define KBD_STAT_CMD 0x08
/* Zero if keyboard locked */
#define KBD_STAT_UNLOCKED 0x10
/* Mouse output buffer full */
#define KBD_STAT_MOUSE_OBF 0x20
/* General receive/xmit timeout */
#define KBD_STAT_GTO 0x40
/* Parity error */
#define KBD_STAT_PERR 0x80
/* Controller Mode Register Bits */
/* Keyboard data generate IRQ1 */
#define KBD_MODE_KBD_INT 0x01
/* Mouse data generate IRQ12 */
#define KBD_MODE_MOUSE_INT 0x02
/* The system flag (?) */
#define KBD_MODE_SYS 0x04
/* The keylock doesn't affect the keyboard if set */
#define KBD_MODE_NO_KEYLOCK 0x08
/* Disable keyboard interface */
#define KBD_MODE_DISABLE_KBD 0x10
/* Disable mouse interface */
#define KBD_MODE_DISABLE_MOUSE 0x20
/* Scan code conversion to PC format */
#define KBD_MODE_KCC 0x40
#define KBD_MODE_RFU 0x80
/* Output Port Bits */
#define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */
#define KBD_OUT_A20 0x02 /* x86 only */
#define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */
#define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */
/*
* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
* We make the default value of the outport include these four bits,
* so that the subsection is rarely necessary.
*/
#define KBD_OUT_ONES 0xcc
#define KBD_PENDING_KBD_COMPAT 0x01
#define KBD_PENDING_AUX_COMPAT 0x02
#define KBD_PENDING_CTRL_KBD 0x04
#define KBD_PENDING_CTRL_AUX 0x08
#define KBD_PENDING_KBD KBD_MODE_DISABLE_KBD /* 0x10 */
#define KBD_PENDING_AUX KBD_MODE_DISABLE_MOUSE /* 0x20 */
#define KBD_MIGR_TIMER_PENDING 0x1
#define KBD_OBSRC_KBD 0x01
#define KBD_OBSRC_MOUSE 0x02
#define KBD_OBSRC_CTRL 0x04
/*
* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
* incorrect, but it avoids having to simulate exact delays
*/
static void kbd_update_irq_lines(KBDState *s)
{
int irq_kbd_level, irq_mouse_level;
irq_kbd_level = 0;
irq_mouse_level = 0;
if (s->status & KBD_STAT_OBF) {
if (s->status & KBD_STAT_MOUSE_OBF) {
if (s->mode & KBD_MODE_MOUSE_INT) {
irq_mouse_level = 1;
}
} else {
if ((s->mode & KBD_MODE_KBD_INT) &&
!(s->mode & KBD_MODE_DISABLE_KBD)) {
irq_kbd_level = 1;
}
}
}
qemu_set_irq(s->irqs[I8042_KBD_IRQ], irq_kbd_level);
qemu_set_irq(s->irqs[I8042_MOUSE_IRQ], irq_mouse_level);
}
static void kbd_deassert_irq(KBDState *s)
{
s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
kbd_update_irq_lines(s);
}
static uint8_t kbd_pending(KBDState *s)
{
if (s->extended_state) {
return s->pending & (~s->mode | ~(KBD_PENDING_KBD | KBD_PENDING_AUX));
} else {
return s->pending;
}
}
/* update irq and KBD_STAT_[MOUSE_]OBF */
static void kbd_update_irq(KBDState *s)
{
uint8_t pending = kbd_pending(s);
s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
if (pending) {
s->status |= KBD_STAT_OBF;
s->outport |= KBD_OUT_OBF;
if (pending & KBD_PENDING_CTRL_KBD) {
s->obsrc = KBD_OBSRC_CTRL;
} else if (pending & KBD_PENDING_CTRL_AUX) {
s->status |= KBD_STAT_MOUSE_OBF;
s->outport |= KBD_OUT_MOUSE_OBF;
s->obsrc = KBD_OBSRC_CTRL;
} else if (pending & KBD_PENDING_KBD) {
s->obsrc = KBD_OBSRC_KBD;
} else {
s->status |= KBD_STAT_MOUSE_OBF;
s->outport |= KBD_OUT_MOUSE_OBF;
s->obsrc = KBD_OBSRC_MOUSE;
}
}
kbd_update_irq_lines(s);
}
static void kbd_safe_update_irq(KBDState *s)
{
/*
* with KBD_STAT_OBF set, a call to kbd_read_data() will eventually call
* kbd_update_irq()
*/
if (s->status & KBD_STAT_OBF) {
return;
}
/* the throttle timer is pending and will call kbd_update_irq() */
if (s->throttle_timer && timer_pending(s->throttle_timer)) {
return;
}
if (kbd_pending(s)) {
kbd_update_irq(s);
}
}
static void kbd_update_kbd_irq(void *opaque, int level)
{
KBDState *s = opaque;
if (level) {
s->pending |= KBD_PENDING_KBD;
} else {
s->pending &= ~KBD_PENDING_KBD;
}
kbd_safe_update_irq(s);
}
static void kbd_update_aux_irq(void *opaque, int level)
{
KBDState *s = opaque;
if (level) {
s->pending |= KBD_PENDING_AUX;
} else {
s->pending &= ~KBD_PENDING_AUX;
}
kbd_safe_update_irq(s);
}
static void kbd_throttle_timeout(void *opaque)
{
KBDState *s = opaque;
if (kbd_pending(s)) {
kbd_update_irq(s);
}
}
static uint64_t kbd_read_status(void *opaque, hwaddr addr,
unsigned size)
{
KBDState *s = opaque;
int val;
val = s->status;
trace_pckbd_kbd_read_status(val);
return val;
}
static void kbd_queue(KBDState *s, int b, int aux)
{
if (s->extended_state) {
s->cbdata = b;
s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX;
s->pending |= aux ? KBD_PENDING_CTRL_AUX : KBD_PENDING_CTRL_KBD;
kbd_safe_update_irq(s);
} else {
ps2_queue(aux ? PS2_DEVICE(&s->ps2mouse) : PS2_DEVICE(&s->ps2kbd), b);
}
}
static uint8_t kbd_dequeue(KBDState *s)
{
uint8_t b = s->cbdata;
s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX;
if (kbd_pending(s)) {
kbd_update_irq(s);
}
return b;
}
static void outport_write(KBDState *s, uint32_t val)
{
trace_pckbd_outport_write(val);
s->outport = val;
qemu_set_irq(s->a20_out, (val >> 1) & 1);
if (!(val & 1)) {
qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
}
}
static void kbd_write_command(void *opaque, hwaddr addr,
uint64_t val, unsigned size)
{
KBDState *s = opaque;
trace_pckbd_kbd_write_command(val);
/*
* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
* low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
* command specify the output port bits to be pulsed.
* 0: Bit should be pulsed. 1: Bit should not be modified.
* The only useful version of this command is pulsing bit 0,
* which does a CPU reset.
*/
if ((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
if (!(val & 1)) {
val = KBD_CCMD_RESET;
} else {
val = KBD_CCMD_NO_OP;
}
}
switch (val) {
case KBD_CCMD_READ_MODE:
kbd_queue(s, s->mode, 0);
break;
case KBD_CCMD_WRITE_MODE:
case KBD_CCMD_WRITE_OBUF:
case KBD_CCMD_WRITE_AUX_OBUF:
case KBD_CCMD_WRITE_MOUSE:
case KBD_CCMD_WRITE_OUTPORT:
s->write_cmd = val;
break;
case KBD_CCMD_MOUSE_DISABLE:
s->mode |= KBD_MODE_DISABLE_MOUSE;
break;
case KBD_CCMD_MOUSE_ENABLE:
s->mode &= ~KBD_MODE_DISABLE_MOUSE;
kbd_safe_update_irq(s);
break;
case KBD_CCMD_TEST_MOUSE:
kbd_queue(s, 0x00, 0);
break;
case KBD_CCMD_SELF_TEST:
s->status |= KBD_STAT_SELFTEST;
kbd_queue(s, 0x55, 0);
break;
case KBD_CCMD_KBD_TEST:
kbd_queue(s, 0x00, 0);
break;
case KBD_CCMD_KBD_DISABLE:
s->mode |= KBD_MODE_DISABLE_KBD;
break;
case KBD_CCMD_KBD_ENABLE:
s->mode &= ~KBD_MODE_DISABLE_KBD;
kbd_safe_update_irq(s);
break;
case KBD_CCMD_READ_INPORT:
kbd_queue(s, 0x80, 0);
break;
case KBD_CCMD_READ_OUTPORT:
kbd_queue(s, s->outport, 0);
break;
case KBD_CCMD_ENABLE_A20:
qemu_irq_raise(s->a20_out);
s->outport |= KBD_OUT_A20;
break;
case KBD_CCMD_DISABLE_A20:
qemu_irq_lower(s->a20_out);
s->outport &= ~KBD_OUT_A20;
break;
case KBD_CCMD_RESET:
qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
break;
case KBD_CCMD_NO_OP:
/* ignore that */
break;
default:
qemu_log_mask(LOG_GUEST_ERROR,
"unsupported keyboard cmd=0x%02" PRIx64 "\n", val);
break;
}
}
static uint64_t kbd_read_data(void *opaque, hwaddr addr,
unsigned size)
{
KBDState *s = opaque;
if (s->status & KBD_STAT_OBF) {
kbd_deassert_irq(s);
if (s->obsrc & KBD_OBSRC_KBD) {
if (s->throttle_timer) {
timer_mod(s->throttle_timer,
qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 1000);
}
s->obdata = ps2_read_data(PS2_DEVICE(&s->ps2kbd));
} else if (s->obsrc & KBD_OBSRC_MOUSE) {
s->obdata = ps2_read_data(PS2_DEVICE(&s->ps2mouse));
} else if (s->obsrc & KBD_OBSRC_CTRL) {
s->obdata = kbd_dequeue(s);
}
}
trace_pckbd_kbd_read_data(s->obdata);
return s->obdata;
}
static void kbd_write_data(void *opaque, hwaddr addr,
uint64_t val, unsigned size)
{
KBDState *s = opaque;
trace_pckbd_kbd_write_data(val);
switch (s->write_cmd) {
case 0:
ps2_write_keyboard(&s->ps2kbd, val);
/* sending data to the keyboard reenables PS/2 communication */
s->mode &= ~KBD_MODE_DISABLE_KBD;
kbd_safe_update_irq(s);
break;
case KBD_CCMD_WRITE_MODE:
s->mode = val;
ps2_keyboard_set_translation(&s->ps2kbd,
(s->mode & KBD_MODE_KCC) != 0);
/*
* a write to the mode byte interrupt enable flags directly updates
* the irq lines
*/
kbd_update_irq_lines(s);
/*
* a write to the mode byte disable interface flags may raise
* an irq if there is pending data in the PS/2 queues.
*/
kbd_safe_update_irq(s);
break;
case KBD_CCMD_WRITE_OBUF:
kbd_queue(s, val, 0);
break;
case KBD_CCMD_WRITE_AUX_OBUF:
kbd_queue(s, val, 1);
break;
case KBD_CCMD_WRITE_OUTPORT:
outport_write(s, val);
break;
case KBD_CCMD_WRITE_MOUSE:
ps2_write_mouse(&s->ps2mouse, val);
/* sending data to the mouse reenables PS/2 communication */
s->mode &= ~KBD_MODE_DISABLE_MOUSE;
kbd_safe_update_irq(s);
break;
default:
break;
}
s->write_cmd = 0;
}
static void kbd_reset(void *opaque)
{
KBDState *s = opaque;
s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES;
s->pending = 0;
kbd_deassert_irq(s);
if (s->throttle_timer) {
timer_del(s->throttle_timer);
}
}
static uint8_t kbd_outport_default(KBDState *s)
{
return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES
| (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0)
| (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0);
}
static int kbd_outport_post_load(void *opaque, int version_id)
{
KBDState *s = opaque;
s->outport_present = true;
return 0;
}
static bool kbd_outport_needed(void *opaque)
{
KBDState *s = opaque;
return s->outport != kbd_outport_default(s);
}
static const VMStateDescription vmstate_kbd_outport = {
.name = "pckbd_outport",
.version_id = 1,
.minimum_version_id = 1,
.post_load = kbd_outport_post_load,
.needed = kbd_outport_needed,
.fields = (const VMStateField[]) {
VMSTATE_UINT8(outport, KBDState),
VMSTATE_END_OF_LIST()
}
};
static int kbd_extended_state_pre_save(void *opaque)
{
KBDState *s = opaque;
s->migration_flags = 0;
if (s->throttle_timer && timer_pending(s->throttle_timer)) {
s->migration_flags |= KBD_MIGR_TIMER_PENDING;
}
return 0;
}
static int kbd_extended_state_post_load(void *opaque, int version_id)
{
KBDState *s = opaque;
if (s->migration_flags & KBD_MIGR_TIMER_PENDING) {
kbd_throttle_timeout(s);
}
s->extended_state_loaded = true;
return 0;
}
static bool kbd_extended_state_needed(void *opaque)
{
KBDState *s = opaque;
return s->extended_state;
}
static const VMStateDescription vmstate_kbd_extended_state = {
.name = "pckbd/extended_state",
.post_load = kbd_extended_state_post_load,
.pre_save = kbd_extended_state_pre_save,
.needed = kbd_extended_state_needed,
.fields = (const VMStateField[]) {
VMSTATE_UINT32(migration_flags, KBDState),
VMSTATE_UINT32(obsrc, KBDState),
VMSTATE_UINT8(obdata, KBDState),
VMSTATE_UINT8(cbdata, KBDState),
VMSTATE_END_OF_LIST()
}
};
static int kbd_pre_save(void *opaque)
{
KBDState *s = opaque;
if (s->extended_state) {
s->pending_tmp = s->pending;
} else {
s->pending_tmp = 0;
if (s->pending & KBD_PENDING_KBD) {
s->pending_tmp |= KBD_PENDING_KBD_COMPAT;
}
if (s->pending & KBD_PENDING_AUX) {
s->pending_tmp |= KBD_PENDING_AUX_COMPAT;
}
}
return 0;
}
static int kbd_pre_load(void *opaque)
{
KBDState *s = opaque;
s->outport_present = false;
s->extended_state_loaded = false;
return 0;
}
static int kbd_post_load(void *opaque, int version_id)
{
KBDState *s = opaque;
if (!s->outport_present) {
s->outport = kbd_outport_default(s);
}
s->pending = s->pending_tmp;
if (!s->extended_state_loaded) {
s->obsrc = s->status & KBD_STAT_OBF ?
(s->status & KBD_STAT_MOUSE_OBF ? KBD_OBSRC_MOUSE : KBD_OBSRC_KBD) :
0;
if (s->pending & KBD_PENDING_KBD_COMPAT) {
s->pending |= KBD_PENDING_KBD;
}
if (s->pending & KBD_PENDING_AUX_COMPAT) {
s->pending |= KBD_PENDING_AUX;
}
}
/* clear all unused flags */
s->pending &= KBD_PENDING_CTRL_KBD | KBD_PENDING_CTRL_AUX |
KBD_PENDING_KBD | KBD_PENDING_AUX;
return 0;
}
static const VMStateDescription vmstate_kbd = {
.name = "pckbd",
.version_id = 3,
.minimum_version_id = 3,
.pre_load = kbd_pre_load,
.post_load = kbd_post_load,
.pre_save = kbd_pre_save,
.fields = (const VMStateField[]) {
VMSTATE_UINT8(write_cmd, KBDState),
VMSTATE_UINT8(status, KBDState),
VMSTATE_UINT8(mode, KBDState),
VMSTATE_UINT8(pending_tmp, KBDState),
VMSTATE_END_OF_LIST()
},
.subsections = (const VMStateDescription * const []) {
&vmstate_kbd_outport,
&vmstate_kbd_extended_state,
NULL
}
};
/* Memory mapped interface */
static uint64_t kbd_mm_readfn(void *opaque, hwaddr addr, unsigned size)
{
KBDState *s = opaque;
if (addr & s->mask) {
return kbd_read_status(s, 0, 1) & 0xff;
} else {
return kbd_read_data(s, 0, 1) & 0xff;
}
}
static void kbd_mm_writefn(void *opaque, hwaddr addr,
uint64_t value, unsigned size)
{
KBDState *s = opaque;
if (addr & s->mask) {
kbd_write_command(s, 0, value & 0xff, 1);
} else {
kbd_write_data(s, 0, value & 0xff, 1);
}
}
static const MemoryRegionOps i8042_mmio_ops = {
.read = kbd_mm_readfn,
.write = kbd_mm_writefn,
.valid.min_access_size = 1,
.valid.max_access_size = 4,
.endianness = DEVICE_NATIVE_ENDIAN,
};
static void i8042_mmio_set_kbd_irq(void *opaque, int n, int level)
{
MMIOKBDState *s = I8042_MMIO(opaque);
KBDState *ks = &s->kbd;
kbd_update_kbd_irq(ks, level);
}
static void i8042_mmio_set_mouse_irq(void *opaque, int n, int level)
{
MMIOKBDState *s = I8042_MMIO(opaque);
KBDState *ks = &s->kbd;
kbd_update_aux_irq(ks, level);
}
static void i8042_mmio_reset(DeviceState *dev)
{
MMIOKBDState *s = I8042_MMIO(dev);
KBDState *ks = &s->kbd;
kbd_reset(ks);
}
static void i8042_mmio_realize(DeviceState *dev, Error **errp)
{
MMIOKBDState *s = I8042_MMIO(dev);
KBDState *ks = &s->kbd;
memory_region_init_io(&s->region, OBJECT(dev), &i8042_mmio_ops, ks,
"i8042", s->size);
sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->region);
if (!sysbus_realize(SYS_BUS_DEVICE(&ks->ps2kbd), errp)) {
return;
}
if (!sysbus_realize(SYS_BUS_DEVICE(&ks->ps2mouse), errp)) {
return;
}
qdev_connect_gpio_out(DEVICE(&ks->ps2kbd), PS2_DEVICE_IRQ,
qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq",
0));
qdev_connect_gpio_out(DEVICE(&ks->ps2mouse), PS2_DEVICE_IRQ,
qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq",
0));
}
static void i8042_mmio_init(Object *obj)
{
MMIOKBDState *s = I8042_MMIO(obj);
KBDState *ks = &s->kbd;
ks->extended_state = true;
object_initialize_child(obj, "ps2kbd", &ks->ps2kbd, TYPE_PS2_KBD_DEVICE);
object_initialize_child(obj, "ps2mouse", &ks->ps2mouse,
TYPE_PS2_MOUSE_DEVICE);
qdev_init_gpio_out(DEVICE(obj), ks->irqs, 2);
qdev_init_gpio_in_named(DEVICE(obj), i8042_mmio_set_kbd_irq,
"ps2-kbd-input-irq", 1);
qdev_init_gpio_in_named(DEVICE(obj), i8042_mmio_set_mouse_irq,
"ps2-mouse-input-irq", 1);
}
static const Property i8042_mmio_properties[] = {
DEFINE_PROP_UINT64("mask", MMIOKBDState, kbd.mask, UINT64_MAX),
DEFINE_PROP_UINT32("size", MMIOKBDState, size, -1),
};
static const VMStateDescription vmstate_kbd_mmio = {
.name = "pckbd-mmio",
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_STRUCT(kbd, MMIOKBDState, 0, vmstate_kbd, KBDState),
VMSTATE_END_OF_LIST()
}
};
static void i8042_mmio_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
dc->realize = i8042_mmio_realize;
device_class_set_legacy_reset(dc, i8042_mmio_reset);
dc->vmsd = &vmstate_kbd_mmio;
device_class_set_props(dc, i8042_mmio_properties);
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
}
static const TypeInfo i8042_mmio_info = {
.name = TYPE_I8042_MMIO,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_init = i8042_mmio_init,
.instance_size = sizeof(MMIOKBDState),
.class_init = i8042_mmio_class_init
};
void i8042_isa_mouse_fake_event(ISAKBDState *isa)
{
KBDState *s = &isa->kbd;
ps2_mouse_fake_event(&s->ps2mouse);
}
static const VMStateDescription vmstate_kbd_isa = {
.name = "pckbd",
.version_id = 3,
.minimum_version_id = 3,
.fields = (const VMStateField[]) {
VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
VMSTATE_END_OF_LIST()
}
};
static const MemoryRegionOps i8042_data_ops = {
.read = kbd_read_data,
.write = kbd_write_data,
.impl = {
.min_access_size = 1,
.max_access_size = 1,
},
.endianness = DEVICE_LITTLE_ENDIAN,
};
static const MemoryRegionOps i8042_cmd_ops = {
.read = kbd_read_status,
.write = kbd_write_command,
.impl = {
.min_access_size = 1,
.max_access_size = 1,
},
.endianness = DEVICE_LITTLE_ENDIAN,
};
static void i8042_set_kbd_irq(void *opaque, int n, int level)
{
ISAKBDState *s = I8042(opaque);
KBDState *ks = &s->kbd;
kbd_update_kbd_irq(ks, level);
}
static void i8042_set_mouse_irq(void *opaque, int n, int level)
{
ISAKBDState *s = I8042(opaque);
KBDState *ks = &s->kbd;
kbd_update_aux_irq(ks, level);
}
static void i8042_reset(DeviceState *dev)
{
ISAKBDState *s = I8042(dev);
KBDState *ks = &s->kbd;
kbd_reset(ks);
}
static void i8042_initfn(Object *obj)
{
ISAKBDState *isa_s = I8042(obj);
KBDState *s = &isa_s->kbd;
memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s,
"i8042-data", 1);
memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s,
"i8042-cmd", 1);
object_initialize_child(obj, "ps2kbd", &s->ps2kbd, TYPE_PS2_KBD_DEVICE);
object_initialize_child(obj, "ps2mouse", &s->ps2mouse,
TYPE_PS2_MOUSE_DEVICE);
qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, I8042_A20_LINE, 1);
qdev_init_gpio_out(DEVICE(obj), s->irqs, 2);
qdev_init_gpio_in_named(DEVICE(obj), i8042_set_kbd_irq,
"ps2-kbd-input-irq", 1);
qdev_init_gpio_in_named(DEVICE(obj), i8042_set_mouse_irq,
"ps2-mouse-input-irq", 1);
}
static void i8042_realizefn(DeviceState *dev, Error **errp)
{
ISADevice *isadev = ISA_DEVICE(dev);
ISAKBDState *isa_s = I8042(dev);
KBDState *s = &isa_s->kbd;
if (isa_s->kbd_irq >= ISA_NUM_IRQS) {
error_setg(errp, "Maximum value for \"kbd-irq\" is: %u",
ISA_NUM_IRQS - 1);
return;
}
if (isa_s->mouse_irq >= ISA_NUM_IRQS) {
error_setg(errp, "Maximum value for \"mouse-irq\" is: %u",
ISA_NUM_IRQS - 1);
return;
}
isa_connect_gpio_out(isadev, I8042_KBD_IRQ, isa_s->kbd_irq);
isa_connect_gpio_out(isadev, I8042_MOUSE_IRQ, isa_s->mouse_irq);
isa_register_ioport(isadev, isa_s->io + 0, 0x60);
isa_register_ioport(isadev, isa_s->io + 1, 0x64);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->ps2kbd), errp)) {
return;
}
qdev_connect_gpio_out(DEVICE(&s->ps2kbd), PS2_DEVICE_IRQ,
qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq",
0));
if (!sysbus_realize(SYS_BUS_DEVICE(&s->ps2mouse), errp)) {
return;
}
qdev_connect_gpio_out(DEVICE(&s->ps2mouse), PS2_DEVICE_IRQ,
qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq",
0));
if (isa_s->kbd_throttle && !isa_s->kbd.extended_state) {
warn_report(TYPE_I8042 ": can't enable kbd-throttle without"
" extended-state, disabling kbd-throttle");
} else if (isa_s->kbd_throttle) {
s->throttle_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
kbd_throttle_timeout, s);
}
}
static void i8042_build_aml(AcpiDevAmlIf *adev, Aml *scope)
{
ISAKBDState *isa_s = I8042(adev);
Aml *kbd;
Aml *mou;
Aml *crs;
crs = aml_resource_template();
aml_append(crs, aml_io(AML_DECODE16, 0x0060, 0x0060, 0x01, 0x01));
aml_append(crs, aml_io(AML_DECODE16, 0x0064, 0x0064, 0x01, 0x01));
aml_append(crs, aml_irq_no_flags(isa_s->kbd_irq));
kbd = aml_device("KBD");
aml_append(kbd, aml_name_decl("_HID", aml_eisaid("PNP0303")));
aml_append(kbd, aml_name_decl("_STA", aml_int(0xf)));
aml_append(kbd, aml_name_decl("_CRS", crs));
crs = aml_resource_template();
aml_append(crs, aml_irq_no_flags(isa_s->mouse_irq));
mou = aml_device("MOU");
aml_append(mou, aml_name_decl("_HID", aml_eisaid("PNP0F13")));
aml_append(mou, aml_name_decl("_STA", aml_int(0xf)));
aml_append(mou, aml_name_decl("_CRS", crs));
aml_append(scope, kbd);
aml_append(scope, mou);
}
static const Property i8042_properties[] = {
DEFINE_PROP_BOOL("extended-state", ISAKBDState, kbd.extended_state, true),
DEFINE_PROP_BOOL("kbd-throttle", ISAKBDState, kbd_throttle, false),
DEFINE_PROP_UINT8("kbd-irq", ISAKBDState, kbd_irq, 1),
DEFINE_PROP_UINT8("mouse-irq", ISAKBDState, mouse_irq, 12),
};
static void i8042_class_initfn(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
device_class_set_props(dc, i8042_properties);
device_class_set_legacy_reset(dc, i8042_reset);
dc->realize = i8042_realizefn;
dc->vmsd = &vmstate_kbd_isa;
adevc->build_dev_aml = i8042_build_aml;
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
}
static const TypeInfo i8042_info = {
.name = TYPE_I8042,
.parent = TYPE_ISA_DEVICE,
.instance_size = sizeof(ISAKBDState),
.instance_init = i8042_initfn,
.class_init = i8042_class_initfn,
.interfaces = (const InterfaceInfo[]) {
{ TYPE_ACPI_DEV_AML_IF },
{ },
},
};
static void i8042_register_types(void)
{
type_register_static(&i8042_info);
type_register_static(&i8042_mmio_info);
}
type_init(i8042_register_types)
+278
View File
@@ -0,0 +1,278 @@
/*
* Arm PrimeCell PL050 Keyboard / Mouse Interface
*
* Copyright (c) 2006-2007 CodeSourcery.
* Written by Paul Brook
*
* This code is licensed under the GPL.
*/
/*
* QEMU interface:
* + sysbus MMIO region 0: MemoryRegion defining the PL050 registers
* + Named GPIO input "ps2-input-irq": set to 1 if the downstream PS2 device
* has asserted its irq
* + sysbus IRQ 0: PL050 output irq
*/
#include "qemu/osdep.h"
#include "hw/core/sysbus.h"
#include "migration/vmstate.h"
#include "hw/input/ps2.h"
#include "hw/input/pl050.h"
#include "hw/core/irq.h"
#include "qemu/log.h"
#include "qemu/module.h"
#include "qom/object.h"
static const VMStateDescription vmstate_pl050 = {
.name = "pl050",
.version_id = 2,
.minimum_version_id = 2,
.fields = (const VMStateField[]) {
VMSTATE_UINT32(cr, PL050State),
VMSTATE_UINT32(clk, PL050State),
VMSTATE_UINT32(last, PL050State),
VMSTATE_INT32(pending, PL050State),
VMSTATE_END_OF_LIST()
}
};
#define PL050_TXEMPTY (1 << 6)
#define PL050_TXBUSY (1 << 5)
#define PL050_RXFULL (1 << 4)
#define PL050_RXBUSY (1 << 3)
#define PL050_RXPARITY (1 << 2)
#define PL050_KMIC (1 << 1)
#define PL050_KMID (1 << 0)
static const unsigned char pl050_id[] = {
0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
};
static void pl050_update_irq(PL050State *s)
{
int level = (s->pending && (s->cr & 0x10) != 0)
|| (s->cr & 0x08) != 0;
qemu_set_irq(s->irq, level);
}
static void pl050_set_irq(void *opaque, int n, int level)
{
PL050State *s = (PL050State *)opaque;
s->pending = level;
pl050_update_irq(s);
}
static uint64_t pl050_read(void *opaque, hwaddr offset,
unsigned size)
{
PL050State *s = (PL050State *)opaque;
if (offset >= 0xfe0 && offset < 0x1000) {
return pl050_id[(offset - 0xfe0) >> 2];
}
switch (offset >> 2) {
case 0: /* KMICR */
return s->cr;
case 1: /* KMISTAT */
{
uint8_t val;
uint32_t stat;
val = s->last;
val = val ^ (val >> 4);
val = val ^ (val >> 2);
val = (val ^ (val >> 1)) & 1;
stat = PL050_TXEMPTY;
if (val) {
stat |= PL050_RXPARITY;
}
if (s->pending) {
stat |= PL050_RXFULL;
}
return stat;
}
case 2: /* KMIDATA */
if (s->pending) {
s->last = ps2_read_data(s->ps2dev);
}
return s->last;
case 3: /* KMICLKDIV */
return s->clk;
case 4: /* KMIIR */
return s->pending | 2;
default:
qemu_log_mask(LOG_GUEST_ERROR,
"pl050_read: Bad offset %x\n", (int)offset);
return 0;
}
}
static void pl050_write(void *opaque, hwaddr offset,
uint64_t value, unsigned size)
{
PL050State *s = (PL050State *)opaque;
switch (offset >> 2) {
case 0: /* KMICR */
s->cr = value;
pl050_update_irq(s);
/* ??? Need to implement the enable/disable bit. */
break;
case 2: /* KMIDATA */
/* ??? This should toggle the TX interrupt line. */
/* ??? This means kbd/mouse can block each other. */
if (s->is_mouse) {
ps2_write_mouse(PS2_MOUSE_DEVICE(s->ps2dev), value);
} else {
ps2_write_keyboard(PS2_KBD_DEVICE(s->ps2dev), value);
}
break;
case 3: /* KMICLKDIV */
s->clk = value;
return;
default:
qemu_log_mask(LOG_GUEST_ERROR,
"pl050_write: Bad offset %x\n", (int)offset);
}
}
static const MemoryRegionOps pl050_ops = {
.read = pl050_read,
.write = pl050_write,
.endianness = DEVICE_NATIVE_ENDIAN,
};
static void pl050_realize(DeviceState *dev, Error **errp)
{
PL050State *s = PL050(dev);
qdev_connect_gpio_out(DEVICE(s->ps2dev), PS2_DEVICE_IRQ,
qdev_get_gpio_in_named(dev, "ps2-input-irq", 0));
}
static void pl050_kbd_realize(DeviceState *dev, Error **errp)
{
PL050DeviceClass *pdc = PL050_GET_CLASS(dev);
PL050KbdState *s = PL050_KBD_DEVICE(dev);
PL050State *ps = PL050(dev);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->kbd), errp)) {
return;
}
ps->ps2dev = PS2_DEVICE(&s->kbd);
pdc->parent_realize(dev, errp);
}
static void pl050_kbd_init(Object *obj)
{
PL050KbdState *s = PL050_KBD_DEVICE(obj);
PL050State *ps = PL050(obj);
ps->is_mouse = false;
object_initialize_child(obj, "kbd", &s->kbd, TYPE_PS2_KBD_DEVICE);
}
static void pl050_mouse_realize(DeviceState *dev, Error **errp)
{
PL050DeviceClass *pdc = PL050_GET_CLASS(dev);
PL050MouseState *s = PL050_MOUSE_DEVICE(dev);
PL050State *ps = PL050(dev);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->mouse), errp)) {
return;
}
ps->ps2dev = PS2_DEVICE(&s->mouse);
pdc->parent_realize(dev, errp);
}
static void pl050_mouse_init(Object *obj)
{
PL050MouseState *s = PL050_MOUSE_DEVICE(obj);
PL050State *ps = PL050(obj);
ps->is_mouse = true;
object_initialize_child(obj, "mouse", &s->mouse, TYPE_PS2_MOUSE_DEVICE);
}
static void pl050_kbd_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
PL050DeviceClass *pdc = PL050_CLASS(oc);
device_class_set_parent_realize(dc, pl050_kbd_realize,
&pdc->parent_realize);
}
static const TypeInfo pl050_kbd_info = {
.name = TYPE_PL050_KBD_DEVICE,
.parent = TYPE_PL050,
.instance_init = pl050_kbd_init,
.instance_size = sizeof(PL050KbdState),
.class_init = pl050_kbd_class_init,
};
static void pl050_mouse_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
PL050DeviceClass *pdc = PL050_CLASS(oc);
device_class_set_parent_realize(dc, pl050_mouse_realize,
&pdc->parent_realize);
}
static const TypeInfo pl050_mouse_info = {
.name = TYPE_PL050_MOUSE_DEVICE,
.parent = TYPE_PL050,
.instance_init = pl050_mouse_init,
.instance_size = sizeof(PL050MouseState),
.class_init = pl050_mouse_class_init,
};
static void pl050_init(Object *obj)
{
PL050State *s = PL050(obj);
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
memory_region_init_io(&s->iomem, obj, &pl050_ops, s, "pl050", 0x1000);
sysbus_init_mmio(sbd, &s->iomem);
sysbus_init_irq(sbd, &s->irq);
qdev_init_gpio_in_named(DEVICE(obj), pl050_set_irq, "ps2-input-irq", 1);
}
static void pl050_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
dc->realize = pl050_realize;
dc->vmsd = &vmstate_pl050;
}
static const TypeInfo pl050_type_info = {
.name = TYPE_PL050,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_init = pl050_init,
.instance_size = sizeof(PL050State),
.class_init = pl050_class_init,
.class_size = sizeof(PL050DeviceClass),
.abstract = true,
.class_init = pl050_class_init,
};
static void pl050_register_types(void)
{
type_register_static(&pl050_type_info);
type_register_static(&pl050_kbd_info);
type_register_static(&pl050_mouse_info);
}
type_init(pl050_register_types)
+1353
View File
File diff suppressed because it is too large Load Diff
+107
View File
@@ -0,0 +1,107 @@
/*
* Gamepad style buttons connected to IRQ/GPIO lines
*
* Copyright (c) 2007 CodeSourcery.
* Written by Paul Brook
*
* This code is licensed under the GPL.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "hw/input/stellaris_gamepad.h"
#include "hw/core/irq.h"
#include "hw/core/qdev-properties.h"
#include "migration/vmstate.h"
#include "ui/console.h"
static void stellaris_gamepad_event(DeviceState *dev, QemuConsole *src,
QemuInputEvent *evt)
{
StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
int qcode = qemu_input_linux_to_qcode(evt->key.key);
int i;
for (i = 0; i < s->num_buttons; i++) {
if (s->keycodes[i] == qcode && s->pressed[i] != evt->key.down) {
s->pressed[i] = evt->key.down;
qemu_set_irq(s->irqs[i], evt->key.down);
}
}
}
static const VMStateDescription vmstate_stellaris_gamepad = {
.name = "stellaris_gamepad",
.version_id = 4,
.minimum_version_id = 4,
.fields = (const VMStateField[]) {
VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
0, vmstate_info_uint8, uint8_t),
VMSTATE_END_OF_LIST()
}
};
static const QemuInputHandler stellaris_gamepad_handler = {
.name = "Stellaris Gamepad",
.mask = INPUT_EVENT_MASK_KEY,
.event = stellaris_gamepad_event,
};
static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)
{
StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
if (s->num_buttons == 0) {
error_setg(errp, "keycodes property array must be set");
return;
}
s->irqs = g_new0(qemu_irq, s->num_buttons);
s->pressed = g_new0(uint8_t, s->num_buttons);
qdev_init_gpio_out(dev, s->irqs, s->num_buttons);
s->hs = qemu_input_handler_register(dev, &stellaris_gamepad_handler);
}
static void stellaris_gamepad_unrealize(DeviceState *dev)
{
StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
g_clear_pointer(&s->irqs, g_free);
g_clear_pointer(&s->pressed, g_free);
g_clear_pointer(&s->hs, qemu_input_handler_unregister);
}
static void stellaris_gamepad_reset_enter(Object *obj, ResetType type)
{
StellarisGamepad *s = STELLARIS_GAMEPAD(obj);
memset(s->pressed, 0, s->num_buttons * sizeof(uint8_t));
}
static const Property stellaris_gamepad_properties[] = {
DEFINE_PROP_ARRAY("keycodes", StellarisGamepad, num_buttons,
keycodes, qdev_prop_uint32, uint32_t),
};
static void stellaris_gamepad_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
ResettableClass *rc = RESETTABLE_CLASS(klass);
rc->phases.enter = stellaris_gamepad_reset_enter;
dc->realize = stellaris_gamepad_realize;
dc->unrealize = stellaris_gamepad_unrealize;
dc->vmsd = &vmstate_stellaris_gamepad;
device_class_set_props(dc, stellaris_gamepad_properties);
}
static const TypeInfo stellaris_gamepad_info[] = {
{
.name = TYPE_STELLARIS_GAMEPAD,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(StellarisGamepad),
.class_init = stellaris_gamepad_class_init,
},
};
DEFINE_TYPES(stellaris_gamepad_info);
+55
View File
@@ -0,0 +1,55 @@
# See docs/devel/tracing.rst for syntax documentation.
# adb-kbd.c
adb_device_kbd_no_key(void) "Ignoring NO_KEY"
adb_device_kbd_writereg(int reg, uint8_t val) "reg %d val 0x%2.2x"
adb_device_kbd_readreg(int reg, uint8_t val0, uint8_t val1) "reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x"
adb_device_kbd_request_change_addr(int devaddr) "change addr to 0x%x"
adb_device_kbd_request_change_addr_and_handler(int devaddr, int handler) "change addr and handler to 0x%x, 0x%x"
# adb-mouse.c
adb_device_mouse_flush(void) "flush"
adb_device_mouse_writereg(int reg, uint8_t val) "reg %d val 0x%2.2x"
adb_device_mouse_readreg(int reg, uint8_t val0, uint8_t val1) "reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x"
adb_device_mouse_request_change_addr(int devaddr) "change addr to 0x%x"
adb_device_mouse_request_change_addr_and_handler(int devaddr, int handler) "change addr and handler to 0x%x, 0x%x"
# adb.c
adb_bus_request(uint8_t addr, const char *cmd, int size) "device 0x%x %s cmdsize=%d"
adb_bus_request_done(uint8_t addr, const char *cmd, int size) "device 0x%x %s replysize=%d"
adb_bus_autopoll_block(bool blocked) "blocked: %d"
adb_bus_autopoll_cb(uint16_t mask) "executing autopoll_cb with autopoll mask 0x%x"
adb_bus_autopoll_cb_done(uint16_t mask) "done executing autopoll_cb with autopoll mask 0x%x"
# pckbd.c
pckbd_kbd_read_data(uint32_t val) "0x%02x"
pckbd_kbd_read_status(int status) "0x%02x"
pckbd_outport_write(uint32_t val) "0x%02x"
pckbd_kbd_write_command(uint64_t val) "0x%02"PRIx64
pckbd_kbd_write_data(uint64_t val) "0x%02"PRIx64
# ps2.c
ps2_put_keycode(void *opaque, int keycode) "%p keycode 0x%02x"
ps2_keyboard_event(void *opaque, unsigned int lnx, int down, unsigned int modifier, unsigned int modifiers, int set, int xlate) "%p lnx %u down %d modifier 0x%x modifiers 0x%x set %d xlate %d"
ps2_read_data(void *opaque) "%p"
ps2_set_ledstate(void *s, int ledstate) "%p ledstate %d"
ps2_reset_keyboard(void *s) "%p"
ps2_write_keyboard(void *opaque, int val) "%p val %d"
ps2_keyboard_set_translation(void *opaque, int mode) "%p mode %d"
ps2_mouse_send_packet(void *s, int dx1, int dy1, int dz1, int b) "%p x %d y %d z %d bs 0x%x"
ps2_mouse_fake_event(void *opaque) "%p"
ps2_write_mouse(void *opaque, int val) "%p val %d"
ps2_kbd_reset(void *opaque) "%p"
ps2_mouse_reset(void *opaque) "%p"
# hid.c
hid_kbd_queue_full(void) "queue full"
hid_kbd_queue_empty(void) "queue empty"
# virtio-input.c
virtio_input_queue_full(void) "queue full"
# lasips2.c
lasips2_reg_read(unsigned int size, int id, uint64_t addr, const char *name, uint64_t val) "%u %d addr 0x%"PRIx64 "%s -> 0x%"PRIx64
lasips2_reg_write(unsigned int size, int id, uint64_t addr, const char *name, uint64_t val) "%u %d addr 0x%"PRIx64 "%s <- 0x%"PRIx64
lasips2_intr(unsigned int val) "%d"
+1
View File
@@ -0,0 +1 @@
#include "trace/trace-hw_input.h"
+543
View File
@@ -0,0 +1,543 @@
/*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu/iov.h"
#include "qemu/log.h"
#include "qemu/module.h"
#include "hw/virtio/virtio.h"
#include "hw/core/qdev-properties.h"
#include "hw/virtio/virtio-input.h"
#include "ui/input.h"
#include "ui/console.h"
#include "standard-headers/linux/input.h"
#define VIRTIO_ID_NAME_KEYBOARD "QEMU Virtio Keyboard"
#define VIRTIO_ID_NAME_MOUSE "QEMU Virtio Mouse"
#define VIRTIO_ID_NAME_TABLET "QEMU Virtio Tablet"
#define VIRTIO_ID_NAME_MULTITOUCH "QEMU Virtio MultiTouch"
/* ----------------------------------------------------------------- */
static const unsigned short keymap_button[INPUT_BUTTON__MAX] = {
[INPUT_BUTTON_LEFT] = BTN_LEFT,
[INPUT_BUTTON_RIGHT] = BTN_RIGHT,
[INPUT_BUTTON_MIDDLE] = BTN_MIDDLE,
[INPUT_BUTTON_WHEEL_UP] = BTN_GEAR_UP,
[INPUT_BUTTON_WHEEL_DOWN] = BTN_GEAR_DOWN,
[INPUT_BUTTON_SIDE] = BTN_SIDE,
[INPUT_BUTTON_EXTRA] = BTN_EXTRA,
[INPUT_BUTTON_TOUCH] = BTN_TOUCH,
};
static const unsigned short axismap_rel[INPUT_AXIS__MAX] = {
[INPUT_AXIS_X] = REL_X,
[INPUT_AXIS_Y] = REL_Y,
};
static const unsigned short axismap_abs[INPUT_AXIS__MAX] = {
[INPUT_AXIS_X] = ABS_X,
[INPUT_AXIS_Y] = ABS_Y,
};
static const unsigned short axismap_tch[INPUT_AXIS__MAX] = {
[INPUT_AXIS_X] = ABS_MT_POSITION_X,
[INPUT_AXIS_Y] = ABS_MT_POSITION_Y,
};
/* ----------------------------------------------------------------- */
static void virtio_input_extend_config(VirtIOInput *vinput,
const unsigned short *map,
size_t mapsize,
uint8_t select, uint8_t subsel)
{
virtio_input_config ext;
int i, bit, byte, bmax = 0;
memset(&ext, 0, sizeof(ext));
for (i = 0; i < mapsize; i++) {
bit = map[i];
if (!bit) {
continue;
}
byte = bit / 8;
bit = bit % 8;
ext.u.bitmap[byte] |= (1 << bit);
if (bmax < byte+1) {
bmax = byte+1;
}
}
ext.select = select;
ext.subsel = subsel;
ext.size = bmax;
virtio_input_add_config(vinput, &ext);
}
static void virtio_input_handle_event(DeviceState *dev, QemuConsole *src,
QemuInputEvent *evt)
{
VirtIOInput *vinput = VIRTIO_INPUT(dev);
virtio_input_event event;
switch (evt->type) {
case INPUT_EVENT_KIND_KEY:
event.type = cpu_to_le16(EV_KEY);
event.code = cpu_to_le16(evt->key.key);
event.value = cpu_to_le32(evt->key.down ? 1 : 0);
virtio_input_send(vinput, &event);
break;
case INPUT_EVENT_KIND_BTN:
if ((evt->btn.button == INPUT_BUTTON_WHEEL_UP ||
evt->btn.button == INPUT_BUTTON_WHEEL_DOWN) &&
evt->btn.down) {
event.type = cpu_to_le16(EV_REL);
event.code = cpu_to_le16(REL_WHEEL);
event.value = cpu_to_le32(evt->btn.button == INPUT_BUTTON_WHEEL_UP
? 1 : -1);
virtio_input_send(vinput, &event);
} else if (keymap_button[evt->btn.button]) {
event.type = cpu_to_le16(EV_KEY);
event.code = cpu_to_le16(keymap_button[evt->btn.button]);
event.value = cpu_to_le32(evt->btn.down ? 1 : 0);
virtio_input_send(vinput, &event);
} else {
if (evt->btn.down) {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: unmapped button: %d [%s]\n", __func__,
evt->btn.button, InputButton_str(evt->btn.button));
}
}
break;
case INPUT_EVENT_KIND_REL:
event.type = cpu_to_le16(EV_REL);
event.code = cpu_to_le16(axismap_rel[evt->rel.axis]);
event.value = cpu_to_le32(evt->rel.value);
virtio_input_send(vinput, &event);
break;
case INPUT_EVENT_KIND_ABS:
event.type = cpu_to_le16(EV_ABS);
event.code = cpu_to_le16(axismap_abs[evt->abs.axis]);
event.value = cpu_to_le32(evt->abs.value);
virtio_input_send(vinput, &event);
break;
case INPUT_EVENT_KIND_MTT:
if (evt->mtt.type == INPUT_MULTI_TOUCH_TYPE_DATA) {
event.type = cpu_to_le16(EV_ABS);
event.code = cpu_to_le16(axismap_tch[evt->mtt.axis]);
event.value = cpu_to_le32(evt->mtt.value);
virtio_input_send(vinput, &event);
} else {
event.type = cpu_to_le16(EV_ABS);
event.code = cpu_to_le16(ABS_MT_SLOT);
event.value = cpu_to_le32(evt->mtt.slot);
virtio_input_send(vinput, &event);
event.type = cpu_to_le16(EV_ABS);
event.code = cpu_to_le16(ABS_MT_TRACKING_ID);
event.value = cpu_to_le32(evt->mtt.tracking_id);
virtio_input_send(vinput, &event);
}
break;
default:
/* keep gcc happy */
break;
}
}
static void virtio_input_handle_sync(DeviceState *dev)
{
VirtIOInput *vinput = VIRTIO_INPUT(dev);
virtio_input_event event = {
.type = cpu_to_le16(EV_SYN),
.code = cpu_to_le16(SYN_REPORT),
.value = 0,
};
virtio_input_send(vinput, &event);
}
static void virtio_input_hid_realize(DeviceState *dev, Error **errp)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(dev);
vhid->hs = qemu_input_handler_register(dev, vhid->handler);
if (vhid->display && vhid->hs) {
qemu_input_handler_bind(vhid->hs, vhid->display, vhid->head, NULL);
}
}
static void virtio_input_hid_unrealize(DeviceState *dev)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(dev);
qemu_input_handler_unregister(vhid->hs);
}
static void virtio_input_hid_change_active(VirtIOInput *vinput)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput);
if (vinput->active) {
qemu_input_handler_activate(vhid->hs);
} else {
qemu_input_handler_deactivate(vhid->hs);
}
}
static void virtio_input_hid_handle_status(VirtIOInput *vinput,
virtio_input_event *event)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput);
int ledbit = 0;
switch (le16_to_cpu(event->type)) {
case EV_LED:
if (event->code == LED_NUML) {
ledbit = QEMU_NUM_LOCK_LED;
} else if (event->code == LED_CAPSL) {
ledbit = QEMU_CAPS_LOCK_LED;
} else if (event->code == LED_SCROLLL) {
ledbit = QEMU_SCROLL_LOCK_LED;
}
if (event->value) {
vhid->ledstate |= ledbit;
} else {
vhid->ledstate &= ~ledbit;
}
qemu_input_handler_set_leds_mask(vhid->hs, vhid->ledstate);
break;
default:
qemu_log_mask(LOG_GUEST_ERROR,
"%s: unknown type %d\n", __func__,
le16_to_cpu(event->type));
break;
}
}
static const Property virtio_input_hid_properties[] = {
DEFINE_PROP_STRING("display", VirtIOInputHID, display),
DEFINE_PROP_UINT32("head", VirtIOInputHID, head, 0),
};
static void virtio_input_hid_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
device_class_set_props(dc, virtio_input_hid_properties);
vic->realize = virtio_input_hid_realize;
vic->unrealize = virtio_input_hid_unrealize;
vic->change_active = virtio_input_hid_change_active;
vic->handle_status = virtio_input_hid_handle_status;
}
static const TypeInfo virtio_input_hid_info = {
.name = TYPE_VIRTIO_INPUT_HID,
.parent = TYPE_VIRTIO_INPUT,
.instance_size = sizeof(VirtIOInputHID),
.class_init = virtio_input_hid_class_init,
.abstract = true,
};
/* ----------------------------------------------------------------- */
static const QemuInputHandler virtio_keyboard_handler = {
.name = VIRTIO_ID_NAME_KEYBOARD,
.mask = INPUT_EVENT_MASK_KEY,
.event = virtio_input_handle_event,
.sync = virtio_input_handle_sync,
};
static struct virtio_input_config virtio_keyboard_config[] = {
{
.select = VIRTIO_INPUT_CFG_ID_NAME,
.size = sizeof(VIRTIO_ID_NAME_KEYBOARD),
.u.string = VIRTIO_ID_NAME_KEYBOARD,
},{
.select = VIRTIO_INPUT_CFG_ID_DEVIDS,
.size = sizeof(struct virtio_input_devids),
.u.ids = {
.bustype = const_le16(BUS_VIRTUAL),
.vendor = const_le16(0x0627), /* same we use for usb hid devices */
.product = const_le16(0x0001),
.version = const_le16(0x0001),
},
},{
.select = VIRTIO_INPUT_CFG_EV_BITS,
.subsel = EV_REP,
.size = 1,
},{
.select = VIRTIO_INPUT_CFG_EV_BITS,
.subsel = EV_LED,
.size = 1,
.u.bitmap = {
(1 << LED_NUML) | (1 << LED_CAPSL) | (1 << LED_SCROLLL),
},
},
{ /* end of list */ },
};
static void virtio_keyboard_init(Object *obj)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
VirtIOInput *vinput = VIRTIO_INPUT(obj);
virtio_input_config ext = {
.select = VIRTIO_INPUT_CFG_EV_BITS,
.subsel = EV_KEY,
.size = DIV_ROUND_UP(KEY_REPLY, 8)
};
unsigned int i;
vhid->handler = &virtio_keyboard_handler;
virtio_input_init_config(vinput, virtio_keyboard_config);
/*
* Cover as many keys as possible since we cannot tell what keys the host
* supports. This follows Linux xen-kbdfront's approach:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/diff/drivers/input/xen-kbdfront.c?id=4ee36dc08e5c4d16d078f59acd6d9d536f9718dd
*
* Stop before KEY_REPLY for migration compatibility.
*/
for (i = KEY_ESC; i < KEY_REPLY; i++) {
ext.u.bitmap[i / 8] |= (1 << (i % 8));
}
virtio_input_add_config(vinput, &ext);
}
static const TypeInfo virtio_keyboard_info = {
.name = TYPE_VIRTIO_KEYBOARD,
.parent = TYPE_VIRTIO_INPUT_HID,
.instance_size = sizeof(VirtIOInputHID),
.instance_init = virtio_keyboard_init,
};
/* ----------------------------------------------------------------- */
static const QemuInputHandler virtio_mouse_handler = {
.name = VIRTIO_ID_NAME_MOUSE,
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
.event = virtio_input_handle_event,
.sync = virtio_input_handle_sync,
};
static struct virtio_input_config virtio_mouse_config[] = {
{
.select = VIRTIO_INPUT_CFG_ID_NAME,
.size = sizeof(VIRTIO_ID_NAME_MOUSE),
.u.string = VIRTIO_ID_NAME_MOUSE,
},{
.select = VIRTIO_INPUT_CFG_ID_DEVIDS,
.size = sizeof(struct virtio_input_devids),
.u.ids = {
.bustype = const_le16(BUS_VIRTUAL),
.vendor = const_le16(0x0627), /* same we use for usb hid devices */
.product = const_le16(0x0002),
.version = const_le16(0x0002),
},
},{
.select = VIRTIO_INPUT_CFG_EV_BITS,
.subsel = EV_REL,
.size = 2,
.u.bitmap = {
(1 << REL_X) | (1 << REL_Y),
(1 << (REL_WHEEL - 8))
},
},
{ /* end of list */ },
};
static void virtio_mouse_init(Object *obj)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
VirtIOInput *vinput = VIRTIO_INPUT(obj);
vhid->handler = &virtio_mouse_handler;
virtio_input_init_config(vinput, virtio_mouse_config);
virtio_input_extend_config(vinput, keymap_button,
ARRAY_SIZE(keymap_button),
VIRTIO_INPUT_CFG_EV_BITS, EV_KEY);
}
static const TypeInfo virtio_mouse_info = {
.name = TYPE_VIRTIO_MOUSE,
.parent = TYPE_VIRTIO_INPUT_HID,
.instance_size = sizeof(VirtIOInputHID),
.instance_init = virtio_mouse_init,
};
/* ----------------------------------------------------------------- */
static const QemuInputHandler virtio_tablet_handler = {
.name = VIRTIO_ID_NAME_TABLET,
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
.event = virtio_input_handle_event,
.sync = virtio_input_handle_sync,
};
static struct virtio_input_config virtio_tablet_config[] = {
{
.select = VIRTIO_INPUT_CFG_ID_NAME,
.size = sizeof(VIRTIO_ID_NAME_TABLET),
.u.string = VIRTIO_ID_NAME_TABLET,
},{
.select = VIRTIO_INPUT_CFG_ID_DEVIDS,
.size = sizeof(struct virtio_input_devids),
.u.ids = {
.bustype = const_le16(BUS_VIRTUAL),
.vendor = const_le16(0x0627), /* same we use for usb hid devices */
.product = const_le16(0x0003),
.version = const_le16(0x0002),
},
},{
.select = VIRTIO_INPUT_CFG_EV_BITS,
.subsel = EV_ABS,
.size = 1,
.u.bitmap = {
(1 << ABS_X) | (1 << ABS_Y),
},
},{
.select = VIRTIO_INPUT_CFG_EV_BITS,
.subsel = EV_REL,
.size = 2,
.u.bitmap = {
0,
(1 << (REL_WHEEL - 8))
},
},{
.select = VIRTIO_INPUT_CFG_ABS_INFO,
.subsel = ABS_X,
.size = sizeof(virtio_input_absinfo),
.u.abs.min = const_le32(INPUT_EVENT_ABS_MIN),
.u.abs.max = const_le32(INPUT_EVENT_ABS_MAX),
},{
.select = VIRTIO_INPUT_CFG_ABS_INFO,
.subsel = ABS_Y,
.size = sizeof(virtio_input_absinfo),
.u.abs.min = const_le32(INPUT_EVENT_ABS_MIN),
.u.abs.max = const_le32(INPUT_EVENT_ABS_MAX),
},
{ /* end of list */ },
};
static void virtio_tablet_init(Object *obj)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
VirtIOInput *vinput = VIRTIO_INPUT(obj);
vhid->handler = &virtio_tablet_handler;
virtio_input_init_config(vinput, virtio_tablet_config);
virtio_input_extend_config(vinput, keymap_button,
ARRAY_SIZE(keymap_button),
VIRTIO_INPUT_CFG_EV_BITS, EV_KEY);
}
static const TypeInfo virtio_tablet_info = {
.name = TYPE_VIRTIO_TABLET,
.parent = TYPE_VIRTIO_INPUT_HID,
.instance_size = sizeof(VirtIOInputHID),
.instance_init = virtio_tablet_init,
};
/* ----------------------------------------------------------------- */
static const QemuInputHandler virtio_multitouch_handler = {
.name = VIRTIO_ID_NAME_MULTITOUCH,
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_MTT,
.event = virtio_input_handle_event,
.sync = virtio_input_handle_sync,
};
static struct virtio_input_config virtio_multitouch_config[] = {
{
.select = VIRTIO_INPUT_CFG_ID_NAME,
.size = sizeof(VIRTIO_ID_NAME_MULTITOUCH),
.u.string = VIRTIO_ID_NAME_MULTITOUCH,
},{
.select = VIRTIO_INPUT_CFG_ID_DEVIDS,
.size = sizeof(struct virtio_input_devids),
.u.ids = {
.bustype = const_le16(BUS_VIRTUAL),
.vendor = const_le16(0x0627), /* same we use for usb hid devices */
.product = const_le16(0x0003),
.version = const_le16(0x0001),
},
},{
.select = VIRTIO_INPUT_CFG_ABS_INFO,
.subsel = ABS_MT_SLOT,
.size = sizeof(virtio_input_absinfo),
.u.abs.min = const_le32(INPUT_EVENT_SLOTS_MIN),
.u.abs.max = const_le32(INPUT_EVENT_SLOTS_MAX),
},{
.select = VIRTIO_INPUT_CFG_ABS_INFO,
.subsel = ABS_MT_TRACKING_ID,
.size = sizeof(virtio_input_absinfo),
.u.abs.min = const_le32(INPUT_EVENT_SLOTS_MIN),
.u.abs.max = const_le32(INPUT_EVENT_SLOTS_MAX),
},{
.select = VIRTIO_INPUT_CFG_ABS_INFO,
.subsel = ABS_MT_POSITION_X,
.size = sizeof(virtio_input_absinfo),
.u.abs.min = const_le32(INPUT_EVENT_ABS_MIN),
.u.abs.max = const_le32(INPUT_EVENT_ABS_MAX),
},{
.select = VIRTIO_INPUT_CFG_ABS_INFO,
.subsel = ABS_MT_POSITION_Y,
.size = sizeof(virtio_input_absinfo),
.u.abs.min = const_le32(INPUT_EVENT_ABS_MIN),
.u.abs.max = const_le32(INPUT_EVENT_ABS_MAX),
},
{ /* end of list */ },
};
static void virtio_multitouch_init(Object *obj)
{
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
VirtIOInput *vinput = VIRTIO_INPUT(obj);
unsigned short abs_props[] = {
INPUT_PROP_DIRECT,
};
unsigned short abs_bits[] = {
ABS_MT_SLOT,
ABS_MT_TRACKING_ID,
ABS_MT_POSITION_X,
ABS_MT_POSITION_Y,
};
vhid->handler = &virtio_multitouch_handler;
virtio_input_init_config(vinput, virtio_multitouch_config);
virtio_input_extend_config(vinput, keymap_button,
ARRAY_SIZE(keymap_button),
VIRTIO_INPUT_CFG_EV_BITS, EV_KEY);
virtio_input_extend_config(vinput, abs_props,
ARRAY_SIZE(abs_props),
VIRTIO_INPUT_CFG_PROP_BITS, 0);
virtio_input_extend_config(vinput, abs_bits,
ARRAY_SIZE(abs_bits),
VIRTIO_INPUT_CFG_EV_BITS, EV_ABS);
}
static const TypeInfo virtio_multitouch_info = {
.name = TYPE_VIRTIO_MULTITOUCH,
.parent = TYPE_VIRTIO_INPUT_HID,
.instance_size = sizeof(VirtIOInputHID),
.instance_init = virtio_multitouch_init,
};
/* ----------------------------------------------------------------- */
static void virtio_register_types(void)
{
type_register_static(&virtio_input_hid_info);
type_register_static(&virtio_keyboard_info);
type_register_static(&virtio_mouse_info);
type_register_static(&virtio_tablet_info);
type_register_static(&virtio_multitouch_info);
}
type_init(virtio_register_types)
+260
View File
@@ -0,0 +1,260 @@
/*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/module.h"
#include "qemu/sockets.h"
#include "hw/virtio/virtio.h"
#include "hw/core/qdev-properties.h"
#include "hw/virtio/virtio-input.h"
#include <sys/ioctl.h>
#include "standard-headers/linux/input.h"
/* ----------------------------------------------------------------- */
static struct virtio_input_config virtio_input_host_config[] = {
{ /* empty list */ },
};
static void virtio_input_host_event(void *opaque)
{
VirtIOInputHost *vih = opaque;
VirtIOInput *vinput = VIRTIO_INPUT(vih);
struct virtio_input_event virtio;
struct input_event evdev;
int rc;
for (;;) {
rc = read(vih->fd, &evdev, sizeof(evdev));
if (rc != sizeof(evdev)) {
break;
}
virtio.type = cpu_to_le16(evdev.type);
virtio.code = cpu_to_le16(evdev.code);
virtio.value = cpu_to_le32(evdev.value);
virtio_input_send(vinput, &virtio);
}
}
static void virtio_input_bits_config(VirtIOInputHost *vih,
int type, int count)
{
virtio_input_config bits;
int rc, i, size = 0;
memset(&bits, 0, sizeof(bits));
rc = ioctl(vih->fd, EVIOCGBIT(type, count/8), bits.u.bitmap);
if (rc < 0) {
return;
}
for (i = 0; i < count/8; i++) {
if (bits.u.bitmap[i]) {
size = i+1;
}
}
if (size == 0) {
return;
}
bits.select = VIRTIO_INPUT_CFG_EV_BITS;
bits.subsel = type;
bits.size = size;
virtio_input_add_config(VIRTIO_INPUT(vih), &bits);
}
static void virtio_input_abs_config(VirtIOInputHost *vih, int axis)
{
virtio_input_config config;
struct input_absinfo absinfo;
int rc;
rc = ioctl(vih->fd, EVIOCGABS(axis), &absinfo);
if (rc < 0) {
return;
}
memset(&config, 0, sizeof(config));
config.select = VIRTIO_INPUT_CFG_ABS_INFO;
config.subsel = axis;
config.size = sizeof(virtio_input_absinfo);
config.u.abs.min = cpu_to_le32(absinfo.minimum);
config.u.abs.max = cpu_to_le32(absinfo.maximum);
config.u.abs.fuzz = cpu_to_le32(absinfo.fuzz);
config.u.abs.flat = cpu_to_le32(absinfo.flat);
config.u.abs.res = cpu_to_le32(absinfo.resolution);
virtio_input_add_config(VIRTIO_INPUT(vih), &config);
}
static void virtio_input_host_realize(DeviceState *dev, Error **errp)
{
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
VirtIOInput *vinput = VIRTIO_INPUT(dev);
virtio_input_config id, *abs;
struct input_id ids;
int rc, ver, i, axis;
uint8_t byte;
if (!vih->evdev) {
error_setg(errp, "evdev property is required");
return;
}
vih->fd = open(vih->evdev, O_RDWR);
if (vih->fd < 0) {
error_setg_file_open(errp, errno, vih->evdev);
return;
}
if (!qemu_set_blocking(vih->fd, false, errp)) {
goto err_close;
}
rc = ioctl(vih->fd, EVIOCGVERSION, &ver);
if (rc < 0) {
error_setg(errp, "%s: is not an evdev device", vih->evdev);
goto err_close;
}
rc = ioctl(vih->fd, EVIOCGRAB, 1);
if (rc < 0) {
error_setg_errno(errp, errno, "%s: failed to get exclusive access",
vih->evdev);
goto err_close;
}
memset(&id, 0, sizeof(id));
ioctl(vih->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string);
id.select = VIRTIO_INPUT_CFG_ID_NAME;
id.size = strlen(id.u.string);
virtio_input_add_config(vinput, &id);
if (ioctl(vih->fd, EVIOCGID, &ids) == 0) {
memset(&id, 0, sizeof(id));
id.select = VIRTIO_INPUT_CFG_ID_DEVIDS;
id.size = sizeof(struct virtio_input_devids);
id.u.ids.bustype = cpu_to_le16(ids.bustype);
id.u.ids.vendor = cpu_to_le16(ids.vendor);
id.u.ids.product = cpu_to_le16(ids.product);
id.u.ids.version = cpu_to_le16(ids.version);
virtio_input_add_config(vinput, &id);
}
virtio_input_bits_config(vih, EV_KEY, KEY_CNT);
virtio_input_bits_config(vih, EV_REL, REL_CNT);
virtio_input_bits_config(vih, EV_ABS, ABS_CNT);
virtio_input_bits_config(vih, EV_MSC, MSC_CNT);
virtio_input_bits_config(vih, EV_SW, SW_CNT);
virtio_input_bits_config(vih, EV_LED, LED_CNT);
abs = virtio_input_find_config(VIRTIO_INPUT(vih),
VIRTIO_INPUT_CFG_EV_BITS, EV_ABS);
if (abs) {
for (i = 0; i < abs->size; i++) {
byte = abs->u.bitmap[i];
axis = 8 * i;
while (byte) {
if (byte & 1) {
virtio_input_abs_config(vih, axis);
}
axis++;
byte >>= 1;
}
}
}
qemu_set_fd_handler(vih->fd, virtio_input_host_event, NULL, vih);
return;
err_close:
close(vih->fd);
vih->fd = -1;
}
static void virtio_input_host_unrealize(DeviceState *dev)
{
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
if (vih->fd > 0) {
qemu_set_fd_handler(vih->fd, NULL, NULL, NULL);
close(vih->fd);
}
}
static void virtio_input_host_handle_status(VirtIOInput *vinput,
virtio_input_event *event)
{
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(vinput);
struct input_event evdev;
struct timeval tval;
int rc;
if (gettimeofday(&tval, NULL)) {
perror("virtio_input_host_handle_status: gettimeofday");
return;
}
evdev.input_event_sec = tval.tv_sec;
evdev.input_event_usec = tval.tv_usec;
evdev.type = le16_to_cpu(event->type);
evdev.code = le16_to_cpu(event->code);
evdev.value = le32_to_cpu(event->value);
rc = write(vih->fd, &evdev, sizeof(evdev));
if (rc == -1) {
perror("virtio_input_host_handle_status: write");
}
}
static const VMStateDescription vmstate_virtio_input_host = {
.name = "virtio-input-host",
.unmigratable = 1,
};
static const Property virtio_input_host_properties[] = {
DEFINE_PROP_STRING("evdev", VirtIOInputHost, evdev),
};
static void virtio_input_host_class_init(ObjectClass *klass, const void *data)
{
VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
DeviceClass *dc = DEVICE_CLASS(klass);
dc->vmsd = &vmstate_virtio_input_host;
device_class_set_props(dc, virtio_input_host_properties);
vic->realize = virtio_input_host_realize;
vic->unrealize = virtio_input_host_unrealize;
vic->handle_status = virtio_input_host_handle_status;
}
static void virtio_input_host_init(Object *obj)
{
VirtIOInput *vinput = VIRTIO_INPUT(obj);
virtio_input_init_config(vinput, virtio_input_host_config);
}
static const TypeInfo virtio_input_host_info = {
.name = TYPE_VIRTIO_INPUT_HOST,
.parent = TYPE_VIRTIO_INPUT,
.instance_size = sizeof(VirtIOInputHost),
.instance_init = virtio_input_host_init,
.class_init = virtio_input_host_class_init,
};
/* ----------------------------------------------------------------- */
static void virtio_register_types(void)
{
type_register_static(&virtio_input_host_info);
}
type_init(virtio_register_types)
+342
View File
@@ -0,0 +1,342 @@
/*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/iov.h"
#include "qemu/module.h"
#include "trace.h"
#include "hw/virtio/virtio.h"
#include "hw/core/qdev-properties.h"
#include "hw/virtio/virtio-input.h"
#include "standard-headers/linux/input.h"
#define VIRTIO_INPUT_VM_VERSION 1
/* ----------------------------------------------------------------- */
void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
{
VirtQueueElement *elem;
int i, len;
if (!vinput->active) {
return;
}
/* queue up events ... */
if (vinput->qindex == vinput->qsize) {
vinput->qsize++;
vinput->queue = g_realloc(vinput->queue, vinput->qsize *
sizeof(vinput->queue[0]));
}
vinput->queue[vinput->qindex++].event = *event;
/* ... until we see a report sync ... */
if (event->type != cpu_to_le16(EV_SYN) ||
event->code != cpu_to_le16(SYN_REPORT)) {
return;
}
/* ... then check available space ... */
for (i = 0; i < vinput->qindex; i++) {
elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement));
if (!elem) {
while (--i >= 0) {
virtqueue_unpop(vinput->evt, vinput->queue[i].elem, 0);
}
vinput->qindex = 0;
trace_virtio_input_queue_full();
return;
}
vinput->queue[i].elem = elem;
}
/* ... and finally pass them to the guest */
for (i = 0; i < vinput->qindex; i++) {
elem = vinput->queue[i].elem;
len = iov_from_buf(elem->in_sg, elem->in_num,
0, &vinput->queue[i].event, sizeof(virtio_input_event));
virtqueue_push(vinput->evt, elem, len);
g_free(elem);
}
virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);
vinput->qindex = 0;
}
static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq)
{
/* nothing */
}
static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq)
{
VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
VirtIOInput *vinput = VIRTIO_INPUT(vdev);
virtio_input_event event;
VirtQueueElement *elem;
int len;
for (;;) {
elem = virtqueue_pop(vinput->sts, sizeof(VirtQueueElement));
if (!elem) {
break;
}
memset(&event, 0, sizeof(event));
len = iov_to_buf(elem->out_sg, elem->out_num,
0, &event, sizeof(event));
if (vic->handle_status) {
vic->handle_status(vinput, &event);
}
virtqueue_push(vinput->sts, elem, len);
g_free(elem);
}
virtio_notify(vdev, vinput->sts);
}
virtio_input_config *virtio_input_find_config(VirtIOInput *vinput,
uint8_t select,
uint8_t subsel)
{
VirtIOInputConfig *cfg;
QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
if (select == cfg->config.select &&
subsel == cfg->config.subsel) {
return &cfg->config;
}
}
return NULL;
}
void virtio_input_add_config(VirtIOInput *vinput,
virtio_input_config *config)
{
VirtIOInputConfig *cfg;
if (virtio_input_find_config(vinput, config->select, config->subsel)) {
/* should not happen */
fprintf(stderr, "%s: duplicate config: %d/%d\n",
__func__, config->select, config->subsel);
abort();
}
cfg = g_new0(VirtIOInputConfig, 1);
cfg->config = *config;
QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node);
}
void virtio_input_init_config(VirtIOInput *vinput,
virtio_input_config *config)
{
int i = 0;
QTAILQ_INIT(&vinput->cfg_list);
while (config[i].select) {
virtio_input_add_config(vinput, config + i);
i++;
}
}
void virtio_input_idstr_config(VirtIOInput *vinput,
uint8_t select, const char *string)
{
virtio_input_config id;
if (!string) {
return;
}
memset(&id, 0, sizeof(id));
id.select = select;
id.size = snprintf(id.u.string, sizeof(id.u.string), "%s", string);
virtio_input_add_config(vinput, &id);
}
static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
{
VirtIOInput *vinput = VIRTIO_INPUT(vdev);
virtio_input_config *config;
config = virtio_input_find_config(vinput, vinput->cfg_select,
vinput->cfg_subsel);
if (config) {
memcpy(config_data, config, vinput->cfg_size);
} else {
memset(config_data, 0, vinput->cfg_size);
}
}
static void virtio_input_set_config(VirtIODevice *vdev,
const uint8_t *config_data)
{
VirtIOInput *vinput = VIRTIO_INPUT(vdev);
virtio_input_config *config = (virtio_input_config *)config_data;
vinput->cfg_select = config->select;
vinput->cfg_subsel = config->subsel;
virtio_notify_config(vdev);
}
static uint64_t virtio_input_get_features(VirtIODevice *vdev, uint64_t f,
Error **errp)
{
return f;
}
static int virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
{
VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
VirtIOInput *vinput = VIRTIO_INPUT(vdev);
if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
if (!vinput->active) {
vinput->active = true;
if (vic->change_active) {
vic->change_active(vinput);
}
}
}
return 0;
}
static void virtio_input_reset(VirtIODevice *vdev)
{
VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
VirtIOInput *vinput = VIRTIO_INPUT(vdev);
if (vinput->active) {
vinput->active = false;
if (vic->change_active) {
vic->change_active(vinput);
}
}
}
static int virtio_input_post_load(void *opaque, int version_id)
{
VirtIOInput *vinput = opaque;
VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vinput);
VirtIODevice *vdev = VIRTIO_DEVICE(vinput);
vinput->active = vdev->status & VIRTIO_CONFIG_S_DRIVER_OK;
if (vic->change_active) {
vic->change_active(vinput);
}
return 0;
}
static void virtio_input_device_realize(DeviceState *dev, Error **errp)
{
VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
VirtIOInput *vinput = VIRTIO_INPUT(dev);
VirtIOInputConfig *cfg;
Error *local_err = NULL;
if (vic->realize) {
vic->realize(dev, &local_err);
if (local_err) {
error_propagate(errp, local_err);
return;
}
}
virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
vinput->serial);
QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
if (vinput->cfg_size < cfg->config.size) {
vinput->cfg_size = cfg->config.size;
}
}
vinput->cfg_size += 8;
assert(vinput->cfg_size <= sizeof(virtio_input_config));
virtio_init(vdev, VIRTIO_ID_INPUT, vinput->cfg_size);
vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
}
static void virtio_input_finalize(Object *obj)
{
VirtIOInput *vinput = VIRTIO_INPUT(obj);
VirtIOInputConfig *cfg, *next;
QTAILQ_FOREACH_SAFE(cfg, &vinput->cfg_list, node, next) {
QTAILQ_REMOVE(&vinput->cfg_list, cfg, node);
g_free(cfg);
}
g_free(vinput->queue);
}
static void virtio_input_device_unrealize(DeviceState *dev)
{
VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
VirtIOInput *vinput = VIRTIO_INPUT(dev);
if (vic->unrealize) {
vic->unrealize(dev);
}
virtio_delete_queue(vinput->evt);
virtio_delete_queue(vinput->sts);
virtio_cleanup(vdev);
}
static const VMStateDescription vmstate_virtio_input = {
.name = "virtio-input",
.minimum_version_id = VIRTIO_INPUT_VM_VERSION,
.version_id = VIRTIO_INPUT_VM_VERSION,
.fields = (const VMStateField[]) {
VMSTATE_VIRTIO_DEVICE,
VMSTATE_END_OF_LIST()
},
.post_load = virtio_input_post_load,
};
static const Property virtio_input_properties[] = {
DEFINE_PROP_STRING("serial", VirtIOInput, serial),
};
static void virtio_input_class_init(ObjectClass *klass, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
device_class_set_props(dc, virtio_input_properties);
dc->vmsd = &vmstate_virtio_input;
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
vdc->realize = virtio_input_device_realize;
vdc->unrealize = virtio_input_device_unrealize;
vdc->get_config = virtio_input_get_config;
vdc->set_config = virtio_input_set_config;
vdc->get_features = virtio_input_get_features;
vdc->set_status = virtio_input_set_status;
vdc->reset = virtio_input_reset;
}
static const TypeInfo virtio_input_info = {
.name = TYPE_VIRTIO_INPUT,
.parent = TYPE_VIRTIO_DEVICE,
.instance_size = sizeof(VirtIOInput),
.class_size = sizeof(VirtIOInputClass),
.class_init = virtio_input_class_init,
.abstract = true,
.instance_finalize = virtio_input_finalize,
};
/* ----------------------------------------------------------------- */
static void virtio_register_types(void)
{
type_register_static(&virtio_input_info);
}
type_init(virtio_register_types)