hyperv-keyboard.c 11 KB

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