hyperv-keyboard.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. unsigned long flags;
  89. u32 msg_type = __le32_to_cpu(msg->header.type);
  90. u32 info;
  91. u16 scan_code;
  92. switch (msg_type) {
  93. case SYNTH_KBD_PROTOCOL_RESPONSE:
  94. /*
  95. * Validate the information provided by the host.
  96. * If the host is giving us a bogus packet,
  97. * drop the packet (hoping the problem
  98. * goes away).
  99. */
  100. if (msg_length < sizeof(struct synth_kbd_protocol_response)) {
  101. dev_err(&hv_dev->device,
  102. "Illegal protocol response packet (len: %d)\n",
  103. msg_length);
  104. break;
  105. }
  106. memcpy(&kbd_dev->protocol_resp, msg,
  107. sizeof(struct synth_kbd_protocol_response));
  108. complete(&kbd_dev->wait_event);
  109. break;
  110. case SYNTH_KBD_EVENT:
  111. /*
  112. * Validate the information provided by the host.
  113. * If the host is giving us a bogus packet,
  114. * drop the packet (hoping the problem
  115. * goes away).
  116. */
  117. if (msg_length < sizeof(struct synth_kbd_keystroke)) {
  118. dev_err(&hv_dev->device,
  119. "Illegal keyboard event packet (len: %d)\n",
  120. msg_length);
  121. break;
  122. }
  123. ks_msg = (struct synth_kbd_keystroke *)msg;
  124. info = __le32_to_cpu(ks_msg->info);
  125. /*
  126. * Inject the information through the serio interrupt.
  127. */
  128. spin_lock_irqsave(&kbd_dev->lock, flags);
  129. if (kbd_dev->started) {
  130. if (info & IS_E0)
  131. serio_interrupt(kbd_dev->hv_serio,
  132. XTKBD_EMUL0, 0);
  133. if (info & IS_E1)
  134. serio_interrupt(kbd_dev->hv_serio,
  135. XTKBD_EMUL1, 0);
  136. scan_code = __le16_to_cpu(ks_msg->make_code);
  137. if (info & IS_BREAK)
  138. scan_code |= XTKBD_RELEASE;
  139. serio_interrupt(kbd_dev->hv_serio, scan_code, 0);
  140. }
  141. spin_unlock_irqrestore(&kbd_dev->lock, flags);
  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. unsigned long flags;
  253. spin_lock_irqsave(&kbd_dev->lock, flags);
  254. kbd_dev->started = true;
  255. spin_unlock_irqrestore(&kbd_dev->lock, flags);
  256. return 0;
  257. }
  258. static void hv_kbd_stop(struct serio *serio)
  259. {
  260. struct hv_kbd_dev *kbd_dev = serio->port_data;
  261. unsigned long flags;
  262. spin_lock_irqsave(&kbd_dev->lock, flags);
  263. kbd_dev->started = false;
  264. spin_unlock_irqrestore(&kbd_dev->lock, flags);
  265. }
  266. static int hv_kbd_probe(struct hv_device *hv_dev,
  267. const struct hv_vmbus_device_id *dev_id)
  268. {
  269. struct hv_kbd_dev *kbd_dev;
  270. struct serio *hv_serio;
  271. int error;
  272. kbd_dev = kzalloc(sizeof(struct hv_kbd_dev), GFP_KERNEL);
  273. hv_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  274. if (!kbd_dev || !hv_serio) {
  275. error = -ENOMEM;
  276. goto err_free_mem;
  277. }
  278. kbd_dev->hv_dev = hv_dev;
  279. kbd_dev->hv_serio = hv_serio;
  280. spin_lock_init(&kbd_dev->lock);
  281. init_completion(&kbd_dev->wait_event);
  282. hv_set_drvdata(hv_dev, kbd_dev);
  283. hv_serio->dev.parent = &hv_dev->device;
  284. hv_serio->id.type = SERIO_8042_XL;
  285. hv_serio->port_data = kbd_dev;
  286. strlcpy(hv_serio->name, dev_name(&hv_dev->device),
  287. sizeof(hv_serio->name));
  288. strlcpy(hv_serio->phys, dev_name(&hv_dev->device),
  289. sizeof(hv_serio->phys));
  290. hv_serio->start = hv_kbd_start;
  291. hv_serio->stop = hv_kbd_stop;
  292. error = vmbus_open(hv_dev->channel,
  293. KBD_VSC_SEND_RING_BUFFER_SIZE,
  294. KBD_VSC_RECV_RING_BUFFER_SIZE,
  295. NULL, 0,
  296. hv_kbd_on_channel_callback,
  297. hv_dev);
  298. if (error)
  299. goto err_free_mem;
  300. error = hv_kbd_connect_to_vsp(hv_dev);
  301. if (error)
  302. goto err_close_vmbus;
  303. serio_register_port(kbd_dev->hv_serio);
  304. device_init_wakeup(&hv_dev->device, true);
  305. return 0;
  306. err_close_vmbus:
  307. vmbus_close(hv_dev->channel);
  308. err_free_mem:
  309. kfree(hv_serio);
  310. kfree(kbd_dev);
  311. return error;
  312. }
  313. static int hv_kbd_remove(struct hv_device *hv_dev)
  314. {
  315. struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
  316. serio_unregister_port(kbd_dev->hv_serio);
  317. vmbus_close(hv_dev->channel);
  318. kfree(kbd_dev);
  319. hv_set_drvdata(hv_dev, NULL);
  320. return 0;
  321. }
  322. static int hv_kbd_suspend(struct hv_device *hv_dev)
  323. {
  324. vmbus_close(hv_dev->channel);
  325. return 0;
  326. }
  327. static int hv_kbd_resume(struct hv_device *hv_dev)
  328. {
  329. int ret;
  330. ret = vmbus_open(hv_dev->channel,
  331. KBD_VSC_SEND_RING_BUFFER_SIZE,
  332. KBD_VSC_RECV_RING_BUFFER_SIZE,
  333. NULL, 0,
  334. hv_kbd_on_channel_callback,
  335. hv_dev);
  336. if (ret == 0)
  337. ret = hv_kbd_connect_to_vsp(hv_dev);
  338. return ret;
  339. }
  340. static const struct hv_vmbus_device_id id_table[] = {
  341. /* Keyboard guid */
  342. { HV_KBD_GUID, },
  343. { },
  344. };
  345. MODULE_DEVICE_TABLE(vmbus, id_table);
  346. static struct hv_driver hv_kbd_drv = {
  347. .name = KBUILD_MODNAME,
  348. .id_table = id_table,
  349. .probe = hv_kbd_probe,
  350. .remove = hv_kbd_remove,
  351. .suspend = hv_kbd_suspend,
  352. .resume = hv_kbd_resume,
  353. .driver = {
  354. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  355. },
  356. };
  357. static int __init hv_kbd_init(void)
  358. {
  359. return vmbus_driver_register(&hv_kbd_drv);
  360. }
  361. static void __exit hv_kbd_exit(void)
  362. {
  363. vmbus_driver_unregister(&hv_kbd_drv);
  364. }
  365. MODULE_LICENSE("GPL");
  366. MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Keyboard Driver");
  367. module_init(hv_kbd_init);
  368. module_exit(hv_kbd_exit);