hid-hyperv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. };
  138. static struct mousevsc_dev *mousevsc_alloc_device(struct hv_device *device)
  139. {
  140. struct mousevsc_dev *input_dev;
  141. input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
  142. if (!input_dev)
  143. return NULL;
  144. input_dev->device = device;
  145. hv_set_drvdata(device, input_dev);
  146. init_completion(&input_dev->wait_event);
  147. input_dev->init_complete = false;
  148. return input_dev;
  149. }
  150. static void mousevsc_free_device(struct mousevsc_dev *device)
  151. {
  152. kfree(device->hid_desc);
  153. kfree(device->report_desc);
  154. hv_set_drvdata(device->device, NULL);
  155. kfree(device);
  156. }
  157. static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
  158. struct synthhid_device_info *device_info)
  159. {
  160. int ret = 0;
  161. struct hid_descriptor *desc;
  162. struct mousevsc_prt_msg ack;
  163. input_device->dev_info_status = -ENOMEM;
  164. input_device->hid_dev_info = device_info->hid_dev_info;
  165. desc = &device_info->hid_descriptor;
  166. if (desc->bLength == 0)
  167. goto cleanup;
  168. input_device->hid_desc = kzalloc(desc->bLength, GFP_ATOMIC);
  169. if (!input_device->hid_desc)
  170. goto cleanup;
  171. memcpy(input_device->hid_desc, desc, desc->bLength);
  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. pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
  214. (packet->offset8 << 3));
  215. if (pipe_msg->type != PIPE_MESSAGE_DATA)
  216. return;
  217. hid_msg = (struct synthhid_msg *)pipe_msg->data;
  218. switch (hid_msg->header.type) {
  219. case SYNTH_HID_PROTOCOL_RESPONSE:
  220. /*
  221. * While it will be impossible for us to protect against
  222. * malicious/buggy hypervisor/host, add a check here to
  223. * ensure we don't corrupt memory.
  224. */
  225. if ((pipe_msg->size + sizeof(struct pipe_prt_msg)
  226. - sizeof(unsigned char))
  227. > sizeof(struct mousevsc_prt_msg)) {
  228. WARN_ON(1);
  229. break;
  230. }
  231. memcpy(&input_dev->protocol_resp, pipe_msg,
  232. pipe_msg->size + sizeof(struct pipe_prt_msg) -
  233. sizeof(unsigned char));
  234. complete(&input_dev->wait_event);
  235. break;
  236. case SYNTH_HID_INITIAL_DEVICE_INFO:
  237. WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
  238. /*
  239. * Parse out the device info into device attr,
  240. * hid desc and report desc
  241. */
  242. mousevsc_on_receive_device_info(input_dev,
  243. (struct synthhid_device_info *)pipe_msg->data);
  244. break;
  245. case SYNTH_HID_INPUT_REPORT:
  246. input_report =
  247. (struct synthhid_input_report *)pipe_msg->data;
  248. if (!input_dev->init_complete)
  249. break;
  250. hid_input_report(input_dev->hid_device,
  251. HID_INPUT_REPORT, input_report->buffer,
  252. input_report->header.size, 1);
  253. break;
  254. default:
  255. pr_err("unsupported hid msg type - type %d len %d",
  256. hid_msg->header.type, hid_msg->header.size);
  257. break;
  258. }
  259. }
  260. static void mousevsc_on_channel_callback(void *context)
  261. {
  262. const int packet_size = 0x100;
  263. int ret;
  264. struct hv_device *device = context;
  265. u32 bytes_recvd;
  266. u64 req_id;
  267. struct vmpacket_descriptor *desc;
  268. unsigned char *buffer;
  269. int bufferlen = packet_size;
  270. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  271. if (!buffer)
  272. return;
  273. do {
  274. ret = vmbus_recvpacket_raw(device->channel, buffer,
  275. bufferlen, &bytes_recvd, &req_id);
  276. switch (ret) {
  277. case 0:
  278. if (bytes_recvd <= 0) {
  279. kfree(buffer);
  280. return;
  281. }
  282. desc = (struct vmpacket_descriptor *)buffer;
  283. switch (desc->type) {
  284. case VM_PKT_COMP:
  285. break;
  286. case VM_PKT_DATA_INBAND:
  287. mousevsc_on_receive(device, desc);
  288. break;
  289. default:
  290. pr_err("unhandled packet type %d, tid %llx len %d\n",
  291. desc->type, req_id, bytes_recvd);
  292. break;
  293. }
  294. break;
  295. case -ENOBUFS:
  296. kfree(buffer);
  297. /* Handle large packet */
  298. bufferlen = bytes_recvd;
  299. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  300. if (!buffer)
  301. return;
  302. break;
  303. }
  304. } while (1);
  305. }
  306. static int mousevsc_connect_to_vsp(struct hv_device *device)
  307. {
  308. int ret = 0;
  309. int t;
  310. struct mousevsc_dev *input_dev = hv_get_drvdata(device);
  311. struct mousevsc_prt_msg *request;
  312. struct mousevsc_prt_msg *response;
  313. request = &input_dev->protocol_req;
  314. memset(request, 0, sizeof(struct mousevsc_prt_msg));
  315. request->type = PIPE_MESSAGE_DATA;
  316. request->size = sizeof(struct synthhid_protocol_request);
  317. request->request.header.type = SYNTH_HID_PROTOCOL_REQUEST;
  318. request->request.header.size = sizeof(unsigned int);
  319. request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
  320. ret = vmbus_sendpacket(device->channel, request,
  321. sizeof(struct pipe_prt_msg) -
  322. sizeof(unsigned char) +
  323. sizeof(struct synthhid_protocol_request),
  324. (unsigned long)request,
  325. VM_PKT_DATA_INBAND,
  326. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  327. if (ret)
  328. goto cleanup;
  329. t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
  330. if (!t) {
  331. ret = -ETIMEDOUT;
  332. goto cleanup;
  333. }
  334. response = &input_dev->protocol_resp;
  335. if (!response->response.approved) {
  336. pr_err("synthhid protocol request failed (version %d)\n",
  337. SYNTHHID_INPUT_VERSION);
  338. ret = -ENODEV;
  339. goto cleanup;
  340. }
  341. t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
  342. if (!t) {
  343. ret = -ETIMEDOUT;
  344. goto cleanup;
  345. }
  346. /*
  347. * We should have gotten the device attr, hid desc and report
  348. * desc at this point
  349. */
  350. ret = input_dev->dev_info_status;
  351. cleanup:
  352. return ret;
  353. }
  354. static int mousevsc_hid_open(struct hid_device *hid)
  355. {
  356. return 0;
  357. }
  358. static int mousevsc_hid_start(struct hid_device *hid)
  359. {
  360. return 0;
  361. }
  362. static void mousevsc_hid_close(struct hid_device *hid)
  363. {
  364. }
  365. static void mousevsc_hid_stop(struct hid_device *hid)
  366. {
  367. }
  368. static struct hid_ll_driver mousevsc_ll_driver = {
  369. .open = mousevsc_hid_open,
  370. .close = mousevsc_hid_close,
  371. .start = mousevsc_hid_start,
  372. .stop = mousevsc_hid_stop,
  373. };
  374. static struct hid_driver mousevsc_hid_driver;
  375. static int mousevsc_probe(struct hv_device *device,
  376. const struct hv_vmbus_device_id *dev_id)
  377. {
  378. int ret;
  379. struct mousevsc_dev *input_dev;
  380. struct hid_device *hid_dev;
  381. input_dev = mousevsc_alloc_device(device);
  382. if (!input_dev)
  383. return -ENOMEM;
  384. ret = vmbus_open(device->channel,
  385. INPUTVSC_SEND_RING_BUFFER_SIZE,
  386. INPUTVSC_RECV_RING_BUFFER_SIZE,
  387. NULL,
  388. 0,
  389. mousevsc_on_channel_callback,
  390. device
  391. );
  392. if (ret)
  393. goto probe_err0;
  394. ret = mousevsc_connect_to_vsp(device);
  395. if (ret)
  396. goto probe_err1;
  397. /* workaround SA-167 */
  398. if (input_dev->report_desc[14] == 0x25)
  399. input_dev->report_desc[14] = 0x29;
  400. hid_dev = hid_allocate_device();
  401. if (IS_ERR(hid_dev)) {
  402. ret = PTR_ERR(hid_dev);
  403. goto probe_err1;
  404. }
  405. hid_dev->ll_driver = &mousevsc_ll_driver;
  406. hid_dev->driver = &mousevsc_hid_driver;
  407. hid_dev->bus = BUS_VIRTUAL;
  408. hid_dev->vendor = input_dev->hid_dev_info.vendor;
  409. hid_dev->product = input_dev->hid_dev_info.product;
  410. hid_dev->version = input_dev->hid_dev_info.version;
  411. input_dev->hid_device = hid_dev;
  412. sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
  413. ret = hid_add_device(hid_dev);
  414. if (ret)
  415. goto probe_err1;
  416. ret = hid_parse_report(hid_dev, input_dev->report_desc,
  417. input_dev->report_desc_size);
  418. if (ret) {
  419. hid_err(hid_dev, "parse failed\n");
  420. goto probe_err2;
  421. }
  422. ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
  423. if (ret) {
  424. hid_err(hid_dev, "hw start failed\n");
  425. goto probe_err2;
  426. }
  427. input_dev->connected = true;
  428. input_dev->init_complete = true;
  429. return ret;
  430. probe_err2:
  431. hid_destroy_device(hid_dev);
  432. probe_err1:
  433. vmbus_close(device->channel);
  434. probe_err0:
  435. mousevsc_free_device(input_dev);
  436. return ret;
  437. }
  438. static int mousevsc_remove(struct hv_device *dev)
  439. {
  440. struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
  441. vmbus_close(dev->channel);
  442. hid_hw_stop(input_dev->hid_device);
  443. hid_destroy_device(input_dev->hid_device);
  444. mousevsc_free_device(input_dev);
  445. return 0;
  446. }
  447. static const struct hv_vmbus_device_id id_table[] = {
  448. /* Mouse guid */
  449. { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
  450. 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
  451. { },
  452. };
  453. MODULE_DEVICE_TABLE(vmbus, id_table);
  454. static struct hv_driver mousevsc_drv = {
  455. .name = KBUILD_MODNAME,
  456. .id_table = id_table,
  457. .probe = mousevsc_probe,
  458. .remove = mousevsc_remove,
  459. };
  460. static int __init mousevsc_init(void)
  461. {
  462. return vmbus_driver_register(&mousevsc_drv);
  463. }
  464. static void __exit mousevsc_exit(void)
  465. {
  466. vmbus_driver_unregister(&mousevsc_drv);
  467. }
  468. MODULE_LICENSE("GPL");
  469. MODULE_VERSION(HV_DRV_VERSION);
  470. module_init(mousevsc_init);
  471. module_exit(mousevsc_exit);