Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) 2009, Citrix Systems, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Copyright (c) 2010, Microsoft Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (c) 2011, Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/hiddev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/hyperv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) struct hv_input_dev_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	unsigned short vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	unsigned short product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned short version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned short reserved[11];
^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) /* The maximum size of a synthetic input message. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Current version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * History:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Beta, RC < 2008/1/22        1,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * RC > 2008/1/22              2,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define SYNTHHID_INPUT_VERSION_MAJOR	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define SYNTHHID_INPUT_VERSION_MINOR	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define SYNTHHID_INPUT_VERSION		(SYNTHHID_INPUT_VERSION_MINOR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 					 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
^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) #pragma pack(push, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * Message types in the synthetic input protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) enum synthhid_msg_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	SYNTH_HID_PROTOCOL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	SYNTH_HID_PROTOCOL_RESPONSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	SYNTH_HID_INITIAL_DEVICE_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	SYNTH_HID_INITIAL_DEVICE_INFO_ACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	SYNTH_HID_INPUT_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	SYNTH_HID_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * Basic message structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) struct synthhid_msg_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	enum synthhid_msg_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) struct synthhid_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct synthhid_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	char data[1]; /* Enclosed message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) union synthhid_version {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		u16 minor_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		u16 major_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u32 version;
^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)  * Protocol messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) struct synthhid_protocol_request {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct synthhid_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	union synthhid_version version_requested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) struct synthhid_protocol_response {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct synthhid_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	union synthhid_version version_requested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned char approved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) struct synthhid_device_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct synthhid_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct hv_input_dev_info hid_dev_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct hid_descriptor hid_descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) struct synthhid_device_info_ack {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct synthhid_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	unsigned char reserved;
^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) struct synthhid_input_report {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct synthhid_msg_hdr header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	char buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #pragma pack(pop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define INPUTVSC_SEND_RING_BUFFER_SIZE	VMBUS_RING_SIZE(36 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define INPUTVSC_RECV_RING_BUFFER_SIZE	VMBUS_RING_SIZE(36 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) enum pipe_prot_msg_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	PIPE_MESSAGE_INVALID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	PIPE_MESSAGE_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	PIPE_MESSAGE_MAXIMUM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct pipe_prt_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	enum pipe_prot_msg_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	char data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct  mousevsc_prt_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	enum pipe_prot_msg_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		struct synthhid_protocol_request request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		struct synthhid_protocol_response response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		struct synthhid_device_info_ack ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * Represents an mousevsc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct mousevsc_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct hv_device	*device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	bool			init_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	bool			connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct mousevsc_prt_msg	protocol_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct mousevsc_prt_msg	protocol_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/* Synchronize the request/response if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct completion	wait_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int			dev_info_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct hid_descriptor	*hid_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	unsigned char		*report_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	u32			report_desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct hv_input_dev_info hid_dev_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct hid_device       *hid_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	u8			input_buf[HID_MAX_BUFFER_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static struct mousevsc_dev *mousevsc_alloc_device(struct hv_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct mousevsc_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	input_dev->device = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	hv_set_drvdata(device, input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	init_completion(&input_dev->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	input_dev->init_complete = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void mousevsc_free_device(struct mousevsc_dev *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	kfree(device->hid_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	kfree(device->report_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	hv_set_drvdata(device->device, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	kfree(device);
^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) static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				struct synthhid_device_info *device_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct hid_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct mousevsc_prt_msg ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	input_device->dev_info_status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	input_device->hid_dev_info = device_info->hid_dev_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	desc = &device_info->hid_descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (desc->bLength == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/* The pointer is not NULL when we resume from hibernation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	kfree(input_device->hid_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	input_device->hid_desc = kmemdup(desc, desc->bLength, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (!input_device->hid_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	input_device->report_desc_size = desc->desc[0].wDescriptorLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (input_device->report_desc_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		input_device->dev_info_status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/* The pointer is not NULL when we resume from hibernation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	kfree(input_device->report_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	input_device->report_desc = kzalloc(input_device->report_desc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 					  GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (!input_device->report_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		input_device->dev_info_status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	memcpy(input_device->report_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	       ((unsigned char *)desc) + desc->bLength,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	       desc->desc[0].wDescriptorLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* Send the ack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	ack.type = PIPE_MESSAGE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ack.size = sizeof(struct synthhid_device_info_ack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ack.ack.header.type = SYNTH_HID_INITIAL_DEVICE_INFO_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	ack.ack.header.size = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	ack.ack.reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	ret = vmbus_sendpacket(input_device->device->channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			&ack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			sizeof(struct synthhid_device_info_ack),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			(unsigned long)&ack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			VM_PKT_DATA_INBAND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		input_device->dev_info_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	complete(&input_device->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static void mousevsc_on_receive(struct hv_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				struct vmpacket_descriptor *packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct pipe_prt_msg *pipe_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct synthhid_msg *hid_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct mousevsc_dev *input_dev = hv_get_drvdata(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct synthhid_input_report *input_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 						(packet->offset8 << 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (pipe_msg->type != PIPE_MESSAGE_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	hid_msg = (struct synthhid_msg *)pipe_msg->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	switch (hid_msg->header.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	case SYNTH_HID_PROTOCOL_RESPONSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		 * While it will be impossible for us to protect against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		 * malicious/buggy hypervisor/host, add a check here to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		 * ensure we don't corrupt memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		if ((pipe_msg->size + sizeof(struct pipe_prt_msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			- sizeof(unsigned char))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			> sizeof(struct mousevsc_prt_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		memcpy(&input_dev->protocol_resp, pipe_msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		       pipe_msg->size + sizeof(struct pipe_prt_msg) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		       sizeof(unsigned char));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		complete(&input_dev->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	case SYNTH_HID_INITIAL_DEVICE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		 * Parse out the device info into device attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		 * hid desc and report desc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		mousevsc_on_receive_device_info(input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			(struct synthhid_device_info *)pipe_msg->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	case SYNTH_HID_INPUT_REPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		input_report =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			(struct synthhid_input_report *)pipe_msg->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		if (!input_dev->init_complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		len = min(input_report->header.size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			  (u32)sizeof(input_dev->input_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		memcpy(input_dev->input_buf, input_report->buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		hid_input_report(input_dev->hid_device, HID_INPUT_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 				 input_dev->input_buf, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		pm_wakeup_hard_event(&input_dev->device->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		pr_err("unsupported hid msg type - type %d len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		       hid_msg->header.type, hid_msg->header.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void mousevsc_on_channel_callback(void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct hv_device *device = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct vmpacket_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	foreach_vmbus_pkt(desc, device->channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		switch (desc->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		case VM_PKT_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		case VM_PKT_DATA_INBAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			mousevsc_on_receive(device, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			pr_err("Unhandled packet type %d, tid %llx len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			       desc->type, desc->trans_id, desc->len8 * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int mousevsc_connect_to_vsp(struct hv_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	unsigned long t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	struct mousevsc_dev *input_dev = hv_get_drvdata(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct mousevsc_prt_msg *request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct mousevsc_prt_msg *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	reinit_completion(&input_dev->wait_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	request = &input_dev->protocol_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	memset(request, 0, sizeof(struct mousevsc_prt_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	request->type = PIPE_MESSAGE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	request->size = sizeof(struct synthhid_protocol_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	request->request.header.type = SYNTH_HID_PROTOCOL_REQUEST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	request->request.header.size = sizeof(unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	ret = vmbus_sendpacket(device->channel, request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				sizeof(struct pipe_prt_msg) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				sizeof(unsigned char) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				sizeof(struct synthhid_protocol_request),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				(unsigned long)request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 				VM_PKT_DATA_INBAND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	response = &input_dev->protocol_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (!response->response.approved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		pr_err("synthhid protocol request failed (version %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		       SYNTHHID_INPUT_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		goto cleanup;
^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) 	t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^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) 	 * We should have gotten the device attr, hid desc and report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	 * desc at this point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	ret = input_dev->dev_info_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static int mousevsc_hid_parse(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct hv_device *dev = hid_get_drvdata(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return hid_parse_report(hid, input_dev->report_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 				input_dev->report_desc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static int mousevsc_hid_open(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static int mousevsc_hid_start(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static void mousevsc_hid_close(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static void mousevsc_hid_stop(struct hid_device *hid)
^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 mousevsc_hid_raw_request(struct hid_device *hid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 				    unsigned char report_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 				    __u8 *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 				    unsigned char rtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 				    int reqtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static struct hid_ll_driver mousevsc_ll_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	.parse = mousevsc_hid_parse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	.open = mousevsc_hid_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	.close = mousevsc_hid_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	.start = mousevsc_hid_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	.stop = mousevsc_hid_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	.raw_request = mousevsc_hid_raw_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static struct hid_driver mousevsc_hid_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static int mousevsc_probe(struct hv_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			const struct hv_vmbus_device_id *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	struct mousevsc_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	struct hid_device *hid_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	input_dev = mousevsc_alloc_device(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	ret = vmbus_open(device->channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		INPUTVSC_SEND_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		INPUTVSC_RECV_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		mousevsc_on_channel_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		goto probe_err0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	ret = mousevsc_connect_to_vsp(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		goto probe_err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	/* workaround SA-167 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (input_dev->report_desc[14] == 0x25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		input_dev->report_desc[14] = 0x29;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	hid_dev = hid_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (IS_ERR(hid_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		ret = PTR_ERR(hid_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		goto probe_err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	hid_dev->ll_driver = &mousevsc_ll_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	hid_dev->driver = &mousevsc_hid_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	hid_dev->bus = BUS_VIRTUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	hid_dev->vendor = input_dev->hid_dev_info.vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	hid_dev->product = input_dev->hid_dev_info.product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	hid_dev->version = input_dev->hid_dev_info.version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	input_dev->hid_device = hid_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	hid_set_drvdata(hid_dev, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	ret = hid_add_device(hid_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		goto probe_err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	ret = hid_parse(hid_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		hid_err(hid_dev, "parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		goto probe_err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		hid_err(hid_dev, "hw start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		goto probe_err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	device_init_wakeup(&device->device, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	input_dev->connected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	input_dev->init_complete = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) probe_err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	hid_destroy_device(hid_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) probe_err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	vmbus_close(device->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) probe_err0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	mousevsc_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static int mousevsc_remove(struct hv_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	device_init_wakeup(&dev->device, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	vmbus_close(dev->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	hid_hw_stop(input_dev->hid_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	hid_destroy_device(input_dev->hid_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	mousevsc_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static int mousevsc_suspend(struct hv_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	vmbus_close(dev->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static int mousevsc_resume(struct hv_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	ret = vmbus_open(dev->channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			 INPUTVSC_SEND_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			 INPUTVSC_RECV_RING_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			 NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			 mousevsc_on_channel_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			 dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	ret = mousevsc_connect_to_vsp(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static const struct hv_vmbus_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	/* Mouse guid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	{ HV_MOUSE_GUID, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) MODULE_DEVICE_TABLE(vmbus, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static struct  hv_driver mousevsc_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	.name = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	.id_table = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	.probe = mousevsc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	.remove = mousevsc_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	.suspend = mousevsc_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	.resume = mousevsc_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) static int __init mousevsc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return vmbus_driver_register(&mousevsc_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static void __exit mousevsc_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	vmbus_driver_unregister(&mousevsc_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic HID Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) module_init(mousevsc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) module_exit(mousevsc_exit);