^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2013, Microsoft Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hyperv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Current version 1.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define SYNTH_KBD_VERSION_MAJOR 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define SYNTH_KBD_VERSION_MINOR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define SYNTH_KBD_VERSION (SYNTH_KBD_VERSION_MINOR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) (SYNTH_KBD_VERSION_MAJOR << 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Message types in the synthetic input protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) enum synth_kbd_msg_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) SYNTH_KBD_PROTOCOL_REQUEST = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) SYNTH_KBD_PROTOCOL_RESPONSE = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) SYNTH_KBD_EVENT = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) SYNTH_KBD_LED_INDICATORS = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Basic message structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct synth_kbd_msg_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __le32 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct synth_kbd_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct synth_kbd_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) char data[]; /* Enclosed message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) union synth_kbd_version {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Protocol messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct synth_kbd_protocol_request {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct synth_kbd_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) union synth_kbd_version version_requested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define PROTOCOL_ACCEPTED BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct synth_kbd_protocol_response {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct synth_kbd_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) __le32 proto_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define IS_UNICODE BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define IS_BREAK BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define IS_E0 BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define IS_E1 BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct synth_kbd_keystroke {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct synth_kbd_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __le16 make_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) __le16 reserved0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __le32 info; /* Additional information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define HK_MAXIMUM_MESSAGE_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define KBD_VSC_SEND_RING_BUFFER_SIZE VMBUS_RING_SIZE(36 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define KBD_VSC_RECV_RING_BUFFER_SIZE VMBUS_RING_SIZE(36 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define XTKBD_EMUL0 0xe0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define XTKBD_EMUL1 0xe1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define XTKBD_RELEASE 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * Represents a keyboard device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct hv_kbd_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct hv_device *hv_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct serio *hv_serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct synth_kbd_protocol_request protocol_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct synth_kbd_protocol_response protocol_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Synchronize the request/response if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct completion wait_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) spinlock_t lock; /* protects 'started' field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) bool started;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void hv_kbd_on_receive(struct hv_device *hv_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct synth_kbd_msg *msg, u32 msg_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct synth_kbd_keystroke *ks_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) u32 msg_type = __le32_to_cpu(msg->header.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u32 info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u16 scan_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) switch (msg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) case SYNTH_KBD_PROTOCOL_RESPONSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Validate the information provided by the host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * If the host is giving us a bogus packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * drop the packet (hoping the problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * goes away).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (msg_length < sizeof(struct synth_kbd_protocol_response)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_err(&hv_dev->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) "Illegal protocol response packet (len: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) msg_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) memcpy(&kbd_dev->protocol_resp, msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) sizeof(struct synth_kbd_protocol_response));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) complete(&kbd_dev->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) case SYNTH_KBD_EVENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Validate the information provided by the host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * If the host is giving us a bogus packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * drop the packet (hoping the problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * goes away).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (msg_length < sizeof(struct synth_kbd_keystroke)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_err(&hv_dev->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) "Illegal keyboard event packet (len: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) msg_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ks_msg = (struct synth_kbd_keystroke *)msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) info = __le32_to_cpu(ks_msg->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Inject the information through the serio interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) spin_lock_irqsave(&kbd_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (kbd_dev->started) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (info & IS_E0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) serio_interrupt(kbd_dev->hv_serio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) XTKBD_EMUL0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (info & IS_E1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) serio_interrupt(kbd_dev->hv_serio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) XTKBD_EMUL1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) scan_code = __le16_to_cpu(ks_msg->make_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (info & IS_BREAK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) scan_code |= XTKBD_RELEASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) serio_interrupt(kbd_dev->hv_serio, scan_code, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) spin_unlock_irqrestore(&kbd_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Only trigger a wakeup on key down, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * "echo freeze > /sys/power/state" can't really enter the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * state because the Enter-UP can trigger a wakeup at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!(info & IS_BREAK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pm_wakeup_hard_event(&hv_dev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) dev_err(&hv_dev->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) "unhandled message type %d\n", msg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static void hv_kbd_handle_received_packet(struct hv_device *hv_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct vmpacket_descriptor *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) u32 bytes_recvd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) u64 req_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct synth_kbd_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) u32 msg_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) switch (desc->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) case VM_PKT_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) case VM_PKT_DATA_INBAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * We have a packet that has "inband" data. The API used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * for retrieving the packet guarantees that the complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * packet is read. So, minimally, we should be able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * parse the payload header safely (assuming that the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * can be trusted. Trusting the host seems to be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * reasonable assumption because in a virtualized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * environment there is not whole lot you can do if you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * don't trust the host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Nonetheless, let us validate if the host can be trusted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * (in a trivial way). The interesting aspect of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * validation is how do you recover if we discover that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * host is not to be trusted? Simply dropping the packet, I
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * don't think is an appropriate recovery. In the interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * of failing fast, it may be better to crash the guest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * For now, I will just drop the packet!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) msg_sz = bytes_recvd - (desc->offset8 << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (msg_sz <= sizeof(struct synth_kbd_msg_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * Drop the packet and hope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * the problem magically goes away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev_err(&hv_dev->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) "Illegal packet (type: %d, tid: %llx, size: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) desc->type, req_id, msg_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) msg = (void *)desc + (desc->offset8 << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) hv_kbd_on_receive(hv_dev, msg, msg_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dev_err(&hv_dev->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) "unhandled packet type %d, tid %llx len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) desc->type, req_id, bytes_recvd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static void hv_kbd_on_channel_callback(void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct vmpacket_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct hv_device *hv_dev = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) u32 bytes_recvd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) u64 req_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) foreach_vmbus_pkt(desc, hv_dev->channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) bytes_recvd = desc->len8 * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) req_id = desc->trans_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) hv_kbd_handle_received_packet(hv_dev, desc, bytes_recvd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) req_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int hv_kbd_connect_to_vsp(struct hv_device *hv_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct synth_kbd_protocol_request *request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct synth_kbd_protocol_response *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) u32 proto_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) reinit_completion(&kbd_dev->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) request = &kbd_dev->protocol_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) memset(request, 0, sizeof(struct synth_kbd_protocol_request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) request->header.type = __cpu_to_le32(SYNTH_KBD_PROTOCOL_REQUEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) request->version_requested.version = __cpu_to_le32(SYNTH_KBD_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) error = vmbus_sendpacket(hv_dev->channel, request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) sizeof(struct synth_kbd_protocol_request),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) (unsigned long)request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) VM_PKT_DATA_INBAND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (!wait_for_completion_timeout(&kbd_dev->wait_event, 10 * HZ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) response = &kbd_dev->protocol_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) proto_status = __le32_to_cpu(response->proto_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!(proto_status & PROTOCOL_ACCEPTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) dev_err(&hv_dev->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) "synth_kbd protocol request failed (version %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) SYNTH_KBD_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static int hv_kbd_start(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct hv_kbd_dev *kbd_dev = serio->port_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) spin_lock_irqsave(&kbd_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) kbd_dev->started = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) spin_unlock_irqrestore(&kbd_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static void hv_kbd_stop(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct hv_kbd_dev *kbd_dev = serio->port_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) spin_lock_irqsave(&kbd_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) kbd_dev->started = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) spin_unlock_irqrestore(&kbd_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static int hv_kbd_probe(struct hv_device *hv_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) const struct hv_vmbus_device_id *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct hv_kbd_dev *kbd_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct serio *hv_serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) kbd_dev = kzalloc(sizeof(struct hv_kbd_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) hv_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!kbd_dev || !hv_serio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) kbd_dev->hv_dev = hv_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) kbd_dev->hv_serio = hv_serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) spin_lock_init(&kbd_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) init_completion(&kbd_dev->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) hv_set_drvdata(hv_dev, kbd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) hv_serio->dev.parent = &hv_dev->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) hv_serio->id.type = SERIO_8042_XL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) hv_serio->port_data = kbd_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) strlcpy(hv_serio->name, dev_name(&hv_dev->device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) sizeof(hv_serio->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) strlcpy(hv_serio->phys, dev_name(&hv_dev->device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) sizeof(hv_serio->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) hv_serio->start = hv_kbd_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) hv_serio->stop = hv_kbd_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) error = vmbus_open(hv_dev->channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) KBD_VSC_SEND_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) KBD_VSC_RECV_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) hv_kbd_on_channel_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) error = hv_kbd_connect_to_vsp(hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto err_close_vmbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) serio_register_port(kbd_dev->hv_serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) device_init_wakeup(&hv_dev->device, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) err_close_vmbus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) vmbus_close(hv_dev->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) kfree(hv_serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) kfree(kbd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int hv_kbd_remove(struct hv_device *hv_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) serio_unregister_port(kbd_dev->hv_serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) vmbus_close(hv_dev->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) kfree(kbd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) hv_set_drvdata(hv_dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static int hv_kbd_suspend(struct hv_device *hv_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) vmbus_close(hv_dev->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static int hv_kbd_resume(struct hv_device *hv_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ret = vmbus_open(hv_dev->channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) KBD_VSC_SEND_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) KBD_VSC_RECV_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) hv_kbd_on_channel_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ret = hv_kbd_connect_to_vsp(hv_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static const struct hv_vmbus_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /* Keyboard guid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) { HV_KBD_GUID, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) MODULE_DEVICE_TABLE(vmbus, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static struct hv_driver hv_kbd_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .name = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .id_table = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .probe = hv_kbd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) .remove = hv_kbd_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) .suspend = hv_kbd_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .resume = hv_kbd_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) .probe_type = PROBE_PREFER_ASYNCHRONOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static int __init hv_kbd_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return vmbus_driver_register(&hv_kbd_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static void __exit hv_kbd_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) vmbus_driver_unregister(&hv_kbd_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Keyboard Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) module_init(hv_kbd_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) module_exit(hv_kbd_exit);