hyperv-keyboard.c 10 KB

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