hid-hyperv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Copyright (c) 2009, Citrix Systems, Inc.
  3. * Copyright (c) 2010, Microsoft Corporation.
  4. * Copyright (c) 2011, Novell Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/completion.h>
  19. #include <linux/input.h>
  20. #include <linux/hid.h>
  21. #include <linux/hiddev.h>
  22. #include <linux/hyperv.h>
  23. struct hv_input_dev_info {
  24. unsigned int size;
  25. unsigned short vendor;
  26. unsigned short product;
  27. unsigned short version;
  28. unsigned short reserved[11];
  29. };
  30. /* The maximum size of a synthetic input message. */
  31. #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
  32. /*
  33. * Current version
  34. *
  35. * History:
  36. * Beta, RC < 2008/1/22 1,0
  37. * RC > 2008/1/22 2,0
  38. */
  39. #define SYNTHHID_INPUT_VERSION_MAJOR 2
  40. #define SYNTHHID_INPUT_VERSION_MINOR 0
  41. #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
  42. (SYNTHHID_INPUT_VERSION_MAJOR << 16))
  43. #pragma pack(push, 1)
  44. /*
  45. * Message types in the synthetic input protocol
  46. */
  47. enum synthhid_msg_type {
  48. SYNTH_HID_PROTOCOL_REQUEST,
  49. SYNTH_HID_PROTOCOL_RESPONSE,
  50. SYNTH_HID_INITIAL_DEVICE_INFO,
  51. SYNTH_HID_INITIAL_DEVICE_INFO_ACK,
  52. SYNTH_HID_INPUT_REPORT,
  53. SYNTH_HID_MAX
  54. };
  55. /*
  56. * Basic message structures.
  57. */
  58. struct synthhid_msg_hdr {
  59. enum synthhid_msg_type type;
  60. u32 size;
  61. };
  62. struct synthhid_msg {
  63. struct synthhid_msg_hdr header;
  64. char data[1]; /* Enclosed message */
  65. };
  66. union synthhid_version {
  67. struct {
  68. u16 minor_version;
  69. u16 major_version;
  70. };
  71. u32 version;
  72. };
  73. /*
  74. * Protocol messages
  75. */
  76. struct synthhid_protocol_request {
  77. struct synthhid_msg_hdr header;
  78. union synthhid_version version_requested;
  79. };
  80. struct synthhid_protocol_response {
  81. struct synthhid_msg_hdr header;
  82. union synthhid_version version_requested;
  83. unsigned char approved;
  84. };
  85. struct synthhid_device_info {
  86. struct synthhid_msg_hdr header;
  87. struct hv_input_dev_info hid_dev_info;
  88. struct hid_descriptor hid_descriptor;
  89. };
  90. struct synthhid_device_info_ack {
  91. struct synthhid_msg_hdr header;
  92. unsigned char reserved;
  93. };
  94. struct synthhid_input_report {
  95. struct synthhid_msg_hdr header;
  96. char buffer[1];
  97. };
  98. #pragma pack(pop)
  99. #define INPUTVSC_SEND_RING_BUFFER_SIZE (10*PAGE_SIZE)
  100. #define INPUTVSC_RECV_RING_BUFFER_SIZE (10*PAGE_SIZE)
  101. enum pipe_prot_msg_type {
  102. PIPE_MESSAGE_INVALID,
  103. PIPE_MESSAGE_DATA,
  104. PIPE_MESSAGE_MAXIMUM
  105. };
  106. struct pipe_prt_msg {
  107. enum pipe_prot_msg_type type;
  108. u32 size;
  109. char data[1];
  110. };
  111. struct mousevsc_prt_msg {
  112. enum pipe_prot_msg_type type;
  113. u32 size;
  114. union {
  115. struct synthhid_protocol_request request;
  116. struct synthhid_protocol_response response;
  117. struct synthhid_device_info_ack ack;
  118. };
  119. };
  120. /*
  121. * Represents an mousevsc device
  122. */
  123. struct mousevsc_dev {
  124. struct hv_device *device;
  125. bool init_complete;
  126. bool connected;
  127. struct mousevsc_prt_msg protocol_req;
  128. struct mousevsc_prt_msg protocol_resp;
  129. /* Synchronize the request/response if needed */
  130. struct completion wait_event;
  131. int dev_info_status;
  132. struct hid_descriptor *hid_desc;
  133. unsigned char *report_desc;
  134. u32 report_desc_size;
  135. struct hv_input_dev_info hid_dev_info;
  136. struct hid_device *hid_device;
  137. u8 input_buf[HID_MAX_BUFFER_SIZE];
  138. };
  139. static struct mousevsc_dev *mousevsc_alloc_device(struct hv_device *device)
  140. {
  141. struct mousevsc_dev *input_dev;
  142. input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
  143. if (!input_dev)
  144. return NULL;
  145. input_dev->device = device;
  146. hv_set_drvdata(device, input_dev);
  147. init_completion(&input_dev->wait_event);
  148. input_dev->init_complete = false;
  149. return input_dev;
  150. }
  151. static void mousevsc_free_device(struct mousevsc_dev *device)
  152. {
  153. kfree(device->hid_desc);
  154. kfree(device->report_desc);
  155. hv_set_drvdata(device->device, NULL);
  156. kfree(device);
  157. }
  158. static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
  159. struct synthhid_device_info *device_info)
  160. {
  161. int ret = 0;
  162. struct hid_descriptor *desc;
  163. struct mousevsc_prt_msg ack;
  164. input_device->dev_info_status = -ENOMEM;
  165. input_device->hid_dev_info = device_info->hid_dev_info;
  166. desc = &device_info->hid_descriptor;
  167. if (desc->bLength == 0)
  168. goto cleanup;
  169. input_device->hid_desc = kmemdup(desc, desc->bLength, GFP_ATOMIC);
  170. if (!input_device->hid_desc)
  171. goto cleanup;
  172. input_device->report_desc_size = desc->desc[0].wDescriptorLength;
  173. if (input_device->report_desc_size == 0) {
  174. input_device->dev_info_status = -EINVAL;
  175. goto cleanup;
  176. }
  177. input_device->report_desc = kzalloc(input_device->report_desc_size,
  178. GFP_ATOMIC);
  179. if (!input_device->report_desc) {
  180. input_device->dev_info_status = -ENOMEM;
  181. goto cleanup;
  182. }
  183. memcpy(input_device->report_desc,
  184. ((unsigned char *)desc) + desc->bLength,
  185. desc->desc[0].wDescriptorLength);
  186. /* Send the ack */
  187. memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
  188. ack.type = PIPE_MESSAGE_DATA;
  189. ack.size = sizeof(struct synthhid_device_info_ack);
  190. ack.ack.header.type = SYNTH_HID_INITIAL_DEVICE_INFO_ACK;
  191. ack.ack.header.size = 1;
  192. ack.ack.reserved = 0;
  193. ret = vmbus_sendpacket(input_device->device->channel,
  194. &ack,
  195. sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
  196. sizeof(struct synthhid_device_info_ack),
  197. (unsigned long)&ack,
  198. VM_PKT_DATA_INBAND,
  199. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  200. if (!ret)
  201. input_device->dev_info_status = 0;
  202. cleanup:
  203. complete(&input_device->wait_event);
  204. return;
  205. }
  206. static void mousevsc_on_receive(struct hv_device *device,
  207. struct vmpacket_descriptor *packet)
  208. {
  209. struct pipe_prt_msg *pipe_msg;
  210. struct synthhid_msg *hid_msg;
  211. struct mousevsc_dev *input_dev = hv_get_drvdata(device);
  212. struct synthhid_input_report *input_report;
  213. size_t len;
  214. pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
  215. (packet->offset8 << 3));
  216. if (pipe_msg->type != PIPE_MESSAGE_DATA)
  217. return;
  218. hid_msg = (struct synthhid_msg *)pipe_msg->data;
  219. switch (hid_msg->header.type) {
  220. case SYNTH_HID_PROTOCOL_RESPONSE:
  221. /*
  222. * While it will be impossible for us to protect against
  223. * malicious/buggy hypervisor/host, add a check here to
  224. * ensure we don't corrupt memory.
  225. */
  226. if ((pipe_msg->size + sizeof(struct pipe_prt_msg)
  227. - sizeof(unsigned char))
  228. > sizeof(struct mousevsc_prt_msg)) {
  229. WARN_ON(1);
  230. break;
  231. }
  232. memcpy(&input_dev->protocol_resp, pipe_msg,
  233. pipe_msg->size + sizeof(struct pipe_prt_msg) -
  234. sizeof(unsigned char));
  235. complete(&input_dev->wait_event);
  236. break;
  237. case SYNTH_HID_INITIAL_DEVICE_INFO:
  238. WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
  239. /*
  240. * Parse out the device info into device attr,
  241. * hid desc and report desc
  242. */
  243. mousevsc_on_receive_device_info(input_dev,
  244. (struct synthhid_device_info *)pipe_msg->data);
  245. break;
  246. case SYNTH_HID_INPUT_REPORT:
  247. input_report =
  248. (struct synthhid_input_report *)pipe_msg->data;
  249. if (!input_dev->init_complete)
  250. break;
  251. len = min(input_report->header.size,
  252. (u32)sizeof(input_dev->input_buf));
  253. memcpy(input_dev->input_buf, input_report->buffer, len);
  254. hid_input_report(input_dev->hid_device, HID_INPUT_REPORT,
  255. input_dev->input_buf, len, 1);
  256. pm_wakeup_hard_event(&input_dev->device->device);
  257. break;
  258. default:
  259. pr_err("unsupported hid msg type - type %d len %d\n",
  260. hid_msg->header.type, hid_msg->header.size);
  261. break;
  262. }
  263. }
  264. static void mousevsc_on_channel_callback(void *context)
  265. {
  266. struct hv_device *device = context;
  267. struct vmpacket_descriptor *desc;
  268. foreach_vmbus_pkt(desc, device->channel) {
  269. switch (desc->type) {
  270. case VM_PKT_COMP:
  271. break;
  272. case VM_PKT_DATA_INBAND:
  273. mousevsc_on_receive(device, desc);
  274. break;
  275. default:
  276. pr_err("Unhandled packet type %d, tid %llx len %d\n",
  277. desc->type, desc->trans_id, desc->len8 * 8);
  278. break;
  279. }
  280. }
  281. }
  282. static int mousevsc_connect_to_vsp(struct hv_device *device)
  283. {
  284. int ret = 0;
  285. unsigned long t;
  286. struct mousevsc_dev *input_dev = hv_get_drvdata(device);
  287. struct mousevsc_prt_msg *request;
  288. struct mousevsc_prt_msg *response;
  289. request = &input_dev->protocol_req;
  290. memset(request, 0, sizeof(struct mousevsc_prt_msg));
  291. request->type = PIPE_MESSAGE_DATA;
  292. request->size = sizeof(struct synthhid_protocol_request);
  293. request->request.header.type = SYNTH_HID_PROTOCOL_REQUEST;
  294. request->request.header.size = sizeof(unsigned int);
  295. request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
  296. ret = vmbus_sendpacket(device->channel, request,
  297. sizeof(struct pipe_prt_msg) -
  298. sizeof(unsigned char) +
  299. sizeof(struct synthhid_protocol_request),
  300. (unsigned long)request,
  301. VM_PKT_DATA_INBAND,
  302. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  303. if (ret)
  304. goto cleanup;
  305. t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
  306. if (!t) {
  307. ret = -ETIMEDOUT;
  308. goto cleanup;
  309. }
  310. response = &input_dev->protocol_resp;
  311. if (!response->response.approved) {
  312. pr_err("synthhid protocol request failed (version %d)\n",
  313. SYNTHHID_INPUT_VERSION);
  314. ret = -ENODEV;
  315. goto cleanup;
  316. }
  317. t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
  318. if (!t) {
  319. ret = -ETIMEDOUT;
  320. goto cleanup;
  321. }
  322. /*
  323. * We should have gotten the device attr, hid desc and report
  324. * desc at this point
  325. */
  326. ret = input_dev->dev_info_status;
  327. cleanup:
  328. return ret;
  329. }
  330. static int mousevsc_hid_parse(struct hid_device *hid)
  331. {
  332. struct hv_device *dev = hid_get_drvdata(hid);
  333. struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
  334. return hid_parse_report(hid, input_dev->report_desc,
  335. input_dev->report_desc_size);
  336. }
  337. static int mousevsc_hid_open(struct hid_device *hid)
  338. {
  339. return 0;
  340. }
  341. static int mousevsc_hid_start(struct hid_device *hid)
  342. {
  343. return 0;
  344. }
  345. static void mousevsc_hid_close(struct hid_device *hid)
  346. {
  347. }
  348. static void mousevsc_hid_stop(struct hid_device *hid)
  349. {
  350. }
  351. static int mousevsc_hid_raw_request(struct hid_device *hid,
  352. unsigned char report_num,
  353. __u8 *buf, size_t len,
  354. unsigned char rtype,
  355. int reqtype)
  356. {
  357. return 0;
  358. }
  359. static struct hid_ll_driver mousevsc_ll_driver = {
  360. .parse = mousevsc_hid_parse,
  361. .open = mousevsc_hid_open,
  362. .close = mousevsc_hid_close,
  363. .start = mousevsc_hid_start,
  364. .stop = mousevsc_hid_stop,
  365. .raw_request = mousevsc_hid_raw_request,
  366. };
  367. static struct hid_driver mousevsc_hid_driver;
  368. static int mousevsc_probe(struct hv_device *device,
  369. const struct hv_vmbus_device_id *dev_id)
  370. {
  371. int ret;
  372. struct mousevsc_dev *input_dev;
  373. struct hid_device *hid_dev;
  374. input_dev = mousevsc_alloc_device(device);
  375. if (!input_dev)
  376. return -ENOMEM;
  377. ret = vmbus_open(device->channel,
  378. INPUTVSC_SEND_RING_BUFFER_SIZE,
  379. INPUTVSC_RECV_RING_BUFFER_SIZE,
  380. NULL,
  381. 0,
  382. mousevsc_on_channel_callback,
  383. device
  384. );
  385. if (ret)
  386. goto probe_err0;
  387. ret = mousevsc_connect_to_vsp(device);
  388. if (ret)
  389. goto probe_err1;
  390. /* workaround SA-167 */
  391. if (input_dev->report_desc[14] == 0x25)
  392. input_dev->report_desc[14] = 0x29;
  393. hid_dev = hid_allocate_device();
  394. if (IS_ERR(hid_dev)) {
  395. ret = PTR_ERR(hid_dev);
  396. goto probe_err1;
  397. }
  398. hid_dev->ll_driver = &mousevsc_ll_driver;
  399. hid_dev->driver = &mousevsc_hid_driver;
  400. hid_dev->bus = BUS_VIRTUAL;
  401. hid_dev->vendor = input_dev->hid_dev_info.vendor;
  402. hid_dev->product = input_dev->hid_dev_info.product;
  403. hid_dev->version = input_dev->hid_dev_info.version;
  404. input_dev->hid_device = hid_dev;
  405. sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
  406. hid_set_drvdata(hid_dev, device);
  407. ret = hid_add_device(hid_dev);
  408. if (ret)
  409. goto probe_err1;
  410. ret = hid_parse(hid_dev);
  411. if (ret) {
  412. hid_err(hid_dev, "parse failed\n");
  413. goto probe_err2;
  414. }
  415. ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
  416. if (ret) {
  417. hid_err(hid_dev, "hw start failed\n");
  418. goto probe_err2;
  419. }
  420. device_init_wakeup(&device->device, true);
  421. input_dev->connected = true;
  422. input_dev->init_complete = true;
  423. return ret;
  424. probe_err2:
  425. hid_destroy_device(hid_dev);
  426. probe_err1:
  427. vmbus_close(device->channel);
  428. probe_err0:
  429. mousevsc_free_device(input_dev);
  430. return ret;
  431. }
  432. static int mousevsc_remove(struct hv_device *dev)
  433. {
  434. struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
  435. device_init_wakeup(&dev->device, false);
  436. vmbus_close(dev->channel);
  437. hid_hw_stop(input_dev->hid_device);
  438. hid_destroy_device(input_dev->hid_device);
  439. mousevsc_free_device(input_dev);
  440. return 0;
  441. }
  442. static const struct hv_vmbus_device_id id_table[] = {
  443. /* Mouse guid */
  444. { HV_MOUSE_GUID, },
  445. { },
  446. };
  447. MODULE_DEVICE_TABLE(vmbus, id_table);
  448. static struct hv_driver mousevsc_drv = {
  449. .name = KBUILD_MODNAME,
  450. .id_table = id_table,
  451. .probe = mousevsc_probe,
  452. .remove = mousevsc_remove,
  453. .driver = {
  454. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  455. },
  456. };
  457. static int __init mousevsc_init(void)
  458. {
  459. return vmbus_driver_register(&mousevsc_drv);
  460. }
  461. static void __exit mousevsc_exit(void)
  462. {
  463. vmbus_driver_unregister(&mousevsc_drv);
  464. }
  465. MODULE_LICENSE("GPL");
  466. module_init(mousevsc_init);
  467. module_exit(mousevsc_exit);