hyperv-keyboard.c 10 KB

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