hyperv-keyboard.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. if (info & IS_E1)
  142. serio_interrupt(kbd_dev->hv_serio,
  143. XTKBD_EMUL1, 0);
  144. scan_code = __le16_to_cpu(ks_msg->make_code);
  145. if (info & IS_BREAK)
  146. scan_code |= XTKBD_RELEASE;
  147. serio_interrupt(kbd_dev->hv_serio, scan_code, 0);
  148. }
  149. spin_unlock_irqrestore(&kbd_dev->lock, flags);
  150. break;
  151. default:
  152. dev_err(&hv_dev->device,
  153. "unhandled message type %d\n", msg_type);
  154. }
  155. }
  156. static void hv_kbd_handle_received_packet(struct hv_device *hv_dev,
  157. struct vmpacket_descriptor *desc,
  158. u32 bytes_recvd,
  159. u64 req_id)
  160. {
  161. struct synth_kbd_msg *msg;
  162. u32 msg_sz;
  163. switch (desc->type) {
  164. case VM_PKT_COMP:
  165. break;
  166. case VM_PKT_DATA_INBAND:
  167. /*
  168. * We have a packet that has "inband" data. The API used
  169. * for retrieving the packet guarantees that the complete
  170. * packet is read. So, minimally, we should be able to
  171. * parse the payload header safely (assuming that the host
  172. * can be trusted. Trusting the host seems to be a
  173. * reasonable assumption because in a virtualized
  174. * environment there is not whole lot you can do if you
  175. * don't trust the host.
  176. *
  177. * Nonetheless, let us validate if the host can be trusted
  178. * (in a trivial way). The interesting aspect of this
  179. * validation is how do you recover if we discover that the
  180. * host is not to be trusted? Simply dropping the packet, I
  181. * don't think is an appropriate recovery. In the interest
  182. * of failing fast, it may be better to crash the guest.
  183. * For now, I will just drop the packet!
  184. */
  185. msg_sz = bytes_recvd - (desc->offset8 << 3);
  186. if (msg_sz <= sizeof(struct synth_kbd_msg_hdr)) {
  187. /*
  188. * Drop the packet and hope
  189. * the problem magically goes away.
  190. */
  191. dev_err(&hv_dev->device,
  192. "Illegal packet (type: %d, tid: %llx, size: %d)\n",
  193. desc->type, req_id, msg_sz);
  194. break;
  195. }
  196. msg = (void *)desc + (desc->offset8 << 3);
  197. hv_kbd_on_receive(hv_dev, msg, msg_sz);
  198. break;
  199. default:
  200. dev_err(&hv_dev->device,
  201. "unhandled packet type %d, tid %llx len %d\n",
  202. desc->type, req_id, bytes_recvd);
  203. break;
  204. }
  205. }
  206. static void hv_kbd_on_channel_callback(void *context)
  207. {
  208. struct hv_device *hv_dev = context;
  209. void *buffer;
  210. int bufferlen = 0x100; /* Start with sensible size */
  211. u32 bytes_recvd;
  212. u64 req_id;
  213. int error;
  214. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  215. if (!buffer)
  216. return;
  217. while (1) {
  218. error = vmbus_recvpacket_raw(hv_dev->channel, buffer, bufferlen,
  219. &bytes_recvd, &req_id);
  220. switch (error) {
  221. case 0:
  222. if (bytes_recvd == 0) {
  223. kfree(buffer);
  224. return;
  225. }
  226. hv_kbd_handle_received_packet(hv_dev, buffer,
  227. bytes_recvd, req_id);
  228. break;
  229. case -ENOBUFS:
  230. kfree(buffer);
  231. /* Handle large packet */
  232. bufferlen = bytes_recvd;
  233. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  234. if (!buffer)
  235. return;
  236. break;
  237. }
  238. }
  239. }
  240. static int hv_kbd_connect_to_vsp(struct hv_device *hv_dev)
  241. {
  242. struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
  243. struct synth_kbd_protocol_request *request;
  244. struct synth_kbd_protocol_response *response;
  245. u32 proto_status;
  246. int error;
  247. request = &kbd_dev->protocol_req;
  248. memset(request, 0, sizeof(struct synth_kbd_protocol_request));
  249. request->header.type = __cpu_to_le32(SYNTH_KBD_PROTOCOL_REQUEST);
  250. request->version_requested.version = __cpu_to_le32(SYNTH_KBD_VERSION);
  251. error = vmbus_sendpacket(hv_dev->channel, request,
  252. sizeof(struct synth_kbd_protocol_request),
  253. (unsigned long)request,
  254. VM_PKT_DATA_INBAND,
  255. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  256. if (error)
  257. return error;
  258. if (!wait_for_completion_timeout(&kbd_dev->wait_event, 10 * HZ))
  259. return -ETIMEDOUT;
  260. response = &kbd_dev->protocol_resp;
  261. proto_status = __le32_to_cpu(response->proto_status);
  262. if (!(proto_status & PROTOCOL_ACCEPTED)) {
  263. dev_err(&hv_dev->device,
  264. "synth_kbd protocol request failed (version %d)\n",
  265. SYNTH_KBD_VERSION);
  266. return -ENODEV;
  267. }
  268. return 0;
  269. }
  270. static int hv_kbd_start(struct serio *serio)
  271. {
  272. struct hv_kbd_dev *kbd_dev = serio->port_data;
  273. unsigned long flags;
  274. spin_lock_irqsave(&kbd_dev->lock, flags);
  275. kbd_dev->started = true;
  276. spin_unlock_irqrestore(&kbd_dev->lock, flags);
  277. return 0;
  278. }
  279. static void hv_kbd_stop(struct serio *serio)
  280. {
  281. struct hv_kbd_dev *kbd_dev = serio->port_data;
  282. unsigned long flags;
  283. spin_lock_irqsave(&kbd_dev->lock, flags);
  284. kbd_dev->started = false;
  285. spin_unlock_irqrestore(&kbd_dev->lock, flags);
  286. }
  287. static int hv_kbd_probe(struct hv_device *hv_dev,
  288. const struct hv_vmbus_device_id *dev_id)
  289. {
  290. struct hv_kbd_dev *kbd_dev;
  291. struct serio *hv_serio;
  292. int error;
  293. kbd_dev = kzalloc(sizeof(struct hv_kbd_dev), GFP_KERNEL);
  294. hv_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  295. if (!kbd_dev || !hv_serio) {
  296. error = -ENOMEM;
  297. goto err_free_mem;
  298. }
  299. kbd_dev->hv_dev = hv_dev;
  300. kbd_dev->hv_serio = hv_serio;
  301. spin_lock_init(&kbd_dev->lock);
  302. init_completion(&kbd_dev->wait_event);
  303. hv_set_drvdata(hv_dev, kbd_dev);
  304. hv_serio->dev.parent = &hv_dev->device;
  305. hv_serio->id.type = SERIO_8042_XL;
  306. hv_serio->port_data = kbd_dev;
  307. strlcpy(hv_serio->name, dev_name(&hv_dev->device),
  308. sizeof(hv_serio->name));
  309. strlcpy(hv_serio->phys, dev_name(&hv_dev->device),
  310. sizeof(hv_serio->phys));
  311. hv_serio->start = hv_kbd_start;
  312. hv_serio->stop = hv_kbd_stop;
  313. error = vmbus_open(hv_dev->channel,
  314. KBD_VSC_SEND_RING_BUFFER_SIZE,
  315. KBD_VSC_RECV_RING_BUFFER_SIZE,
  316. NULL, 0,
  317. hv_kbd_on_channel_callback,
  318. hv_dev);
  319. if (error)
  320. goto err_free_mem;
  321. error = hv_kbd_connect_to_vsp(hv_dev);
  322. if (error)
  323. goto err_close_vmbus;
  324. serio_register_port(kbd_dev->hv_serio);
  325. return 0;
  326. err_close_vmbus:
  327. vmbus_close(hv_dev->channel);
  328. err_free_mem:
  329. kfree(hv_serio);
  330. kfree(kbd_dev);
  331. return error;
  332. }
  333. static int hv_kbd_remove(struct hv_device *hv_dev)
  334. {
  335. struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
  336. serio_unregister_port(kbd_dev->hv_serio);
  337. vmbus_close(hv_dev->channel);
  338. kfree(kbd_dev);
  339. hv_set_drvdata(hv_dev, NULL);
  340. return 0;
  341. }
  342. /*
  343. * Keyboard GUID
  344. * {f912ad6d-2b17-48ea-bd65-f927a61c7684}
  345. */
  346. #define HV_KBD_GUID \
  347. .guid = { \
  348. 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48, \
  349. 0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84 \
  350. }
  351. static const struct hv_vmbus_device_id id_table[] = {
  352. /* Keyboard guid */
  353. { HV_KBD_GUID, },
  354. { },
  355. };
  356. MODULE_DEVICE_TABLE(vmbus, id_table);
  357. static struct hv_driver hv_kbd_drv = {
  358. .name = KBUILD_MODNAME,
  359. .id_table = id_table,
  360. .probe = hv_kbd_probe,
  361. .remove = hv_kbd_remove,
  362. };
  363. static int __init hv_kbd_init(void)
  364. {
  365. return vmbus_driver_register(&hv_kbd_drv);
  366. }
  367. static void __exit hv_kbd_exit(void)
  368. {
  369. vmbus_driver_unregister(&hv_kbd_drv);
  370. }
  371. MODULE_LICENSE("GPL");
  372. module_init(hv_kbd_init);
  373. module_exit(hv_kbd_exit);