hid-hyperv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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_event(&input_dev->device->device, 0);
  257. break;
  258. default:
  259. pr_err("unsupported hid msg type - type %d len %d",
  260. hid_msg->header.type, hid_msg->header.size);
  261. break;
  262. }
  263. }
  264. static void mousevsc_on_channel_callback(void *context)
  265. {
  266. const int packet_size = 0x100;
  267. int ret;
  268. struct hv_device *device = context;
  269. u32 bytes_recvd;
  270. u64 req_id;
  271. struct vmpacket_descriptor *desc;
  272. unsigned char *buffer;
  273. int bufferlen = packet_size;
  274. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  275. if (!buffer)
  276. return;
  277. do {
  278. ret = vmbus_recvpacket_raw(device->channel, buffer,
  279. bufferlen, &bytes_recvd, &req_id);
  280. switch (ret) {
  281. case 0:
  282. if (bytes_recvd <= 0) {
  283. kfree(buffer);
  284. return;
  285. }
  286. desc = (struct vmpacket_descriptor *)buffer;
  287. switch (desc->type) {
  288. case VM_PKT_COMP:
  289. break;
  290. case VM_PKT_DATA_INBAND:
  291. mousevsc_on_receive(device, desc);
  292. break;
  293. default:
  294. pr_err("unhandled packet type %d, tid %llx len %d\n",
  295. desc->type, req_id, bytes_recvd);
  296. break;
  297. }
  298. break;
  299. case -ENOBUFS:
  300. kfree(buffer);
  301. /* Handle large packet */
  302. bufferlen = bytes_recvd;
  303. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  304. if (!buffer)
  305. return;
  306. break;
  307. }
  308. } while (1);
  309. }
  310. static int mousevsc_connect_to_vsp(struct hv_device *device)
  311. {
  312. int ret = 0;
  313. unsigned long t;
  314. struct mousevsc_dev *input_dev = hv_get_drvdata(device);
  315. struct mousevsc_prt_msg *request;
  316. struct mousevsc_prt_msg *response;
  317. request = &input_dev->protocol_req;
  318. memset(request, 0, sizeof(struct mousevsc_prt_msg));
  319. request->type = PIPE_MESSAGE_DATA;
  320. request->size = sizeof(struct synthhid_protocol_request);
  321. request->request.header.type = SYNTH_HID_PROTOCOL_REQUEST;
  322. request->request.header.size = sizeof(unsigned int);
  323. request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
  324. ret = vmbus_sendpacket(device->channel, request,
  325. sizeof(struct pipe_prt_msg) -
  326. sizeof(unsigned char) +
  327. sizeof(struct synthhid_protocol_request),
  328. (unsigned long)request,
  329. VM_PKT_DATA_INBAND,
  330. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  331. if (ret)
  332. goto cleanup;
  333. t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
  334. if (!t) {
  335. ret = -ETIMEDOUT;
  336. goto cleanup;
  337. }
  338. response = &input_dev->protocol_resp;
  339. if (!response->response.approved) {
  340. pr_err("synthhid protocol request failed (version %d)\n",
  341. SYNTHHID_INPUT_VERSION);
  342. ret = -ENODEV;
  343. goto cleanup;
  344. }
  345. t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
  346. if (!t) {
  347. ret = -ETIMEDOUT;
  348. goto cleanup;
  349. }
  350. /*
  351. * We should have gotten the device attr, hid desc and report
  352. * desc at this point
  353. */
  354. ret = input_dev->dev_info_status;
  355. cleanup:
  356. return ret;
  357. }
  358. static int mousevsc_hid_parse(struct hid_device *hid)
  359. {
  360. struct hv_device *dev = hid_get_drvdata(hid);
  361. struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
  362. return hid_parse_report(hid, input_dev->report_desc,
  363. input_dev->report_desc_size);
  364. }
  365. static int mousevsc_hid_open(struct hid_device *hid)
  366. {
  367. return 0;
  368. }
  369. static int mousevsc_hid_start(struct hid_device *hid)
  370. {
  371. return 0;
  372. }
  373. static void mousevsc_hid_close(struct hid_device *hid)
  374. {
  375. }
  376. static void mousevsc_hid_stop(struct hid_device *hid)
  377. {
  378. }
  379. static int mousevsc_hid_raw_request(struct hid_device *hid,
  380. unsigned char report_num,
  381. __u8 *buf, size_t len,
  382. unsigned char rtype,
  383. int reqtype)
  384. {
  385. return 0;
  386. }
  387. static struct hid_ll_driver mousevsc_ll_driver = {
  388. .parse = mousevsc_hid_parse,
  389. .open = mousevsc_hid_open,
  390. .close = mousevsc_hid_close,
  391. .start = mousevsc_hid_start,
  392. .stop = mousevsc_hid_stop,
  393. .raw_request = mousevsc_hid_raw_request,
  394. };
  395. static struct hid_driver mousevsc_hid_driver;
  396. static int mousevsc_probe(struct hv_device *device,
  397. const struct hv_vmbus_device_id *dev_id)
  398. {
  399. int ret;
  400. struct mousevsc_dev *input_dev;
  401. struct hid_device *hid_dev;
  402. input_dev = mousevsc_alloc_device(device);
  403. if (!input_dev)
  404. return -ENOMEM;
  405. ret = vmbus_open(device->channel,
  406. INPUTVSC_SEND_RING_BUFFER_SIZE,
  407. INPUTVSC_RECV_RING_BUFFER_SIZE,
  408. NULL,
  409. 0,
  410. mousevsc_on_channel_callback,
  411. device
  412. );
  413. if (ret)
  414. goto probe_err0;
  415. ret = mousevsc_connect_to_vsp(device);
  416. if (ret)
  417. goto probe_err1;
  418. /* workaround SA-167 */
  419. if (input_dev->report_desc[14] == 0x25)
  420. input_dev->report_desc[14] = 0x29;
  421. hid_dev = hid_allocate_device();
  422. if (IS_ERR(hid_dev)) {
  423. ret = PTR_ERR(hid_dev);
  424. goto probe_err1;
  425. }
  426. hid_dev->ll_driver = &mousevsc_ll_driver;
  427. hid_dev->driver = &mousevsc_hid_driver;
  428. hid_dev->bus = BUS_VIRTUAL;
  429. hid_dev->vendor = input_dev->hid_dev_info.vendor;
  430. hid_dev->product = input_dev->hid_dev_info.product;
  431. hid_dev->version = input_dev->hid_dev_info.version;
  432. input_dev->hid_device = hid_dev;
  433. sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
  434. hid_set_drvdata(hid_dev, device);
  435. ret = hid_add_device(hid_dev);
  436. if (ret)
  437. goto probe_err1;
  438. ret = hid_parse(hid_dev);
  439. if (ret) {
  440. hid_err(hid_dev, "parse failed\n");
  441. goto probe_err2;
  442. }
  443. ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
  444. if (ret) {
  445. hid_err(hid_dev, "hw start failed\n");
  446. goto probe_err2;
  447. }
  448. device_init_wakeup(&device->device, true);
  449. input_dev->connected = true;
  450. input_dev->init_complete = true;
  451. return ret;
  452. probe_err2:
  453. hid_destroy_device(hid_dev);
  454. probe_err1:
  455. vmbus_close(device->channel);
  456. probe_err0:
  457. mousevsc_free_device(input_dev);
  458. return ret;
  459. }
  460. static int mousevsc_remove(struct hv_device *dev)
  461. {
  462. struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
  463. device_init_wakeup(&dev->device, false);
  464. vmbus_close(dev->channel);
  465. hid_hw_stop(input_dev->hid_device);
  466. hid_destroy_device(input_dev->hid_device);
  467. mousevsc_free_device(input_dev);
  468. return 0;
  469. }
  470. static const struct hv_vmbus_device_id id_table[] = {
  471. /* Mouse guid */
  472. { HV_MOUSE_GUID, },
  473. { },
  474. };
  475. MODULE_DEVICE_TABLE(vmbus, id_table);
  476. static struct hv_driver mousevsc_drv = {
  477. .name = KBUILD_MODNAME,
  478. .id_table = id_table,
  479. .probe = mousevsc_probe,
  480. .remove = mousevsc_remove,
  481. };
  482. static int __init mousevsc_init(void)
  483. {
  484. return vmbus_driver_register(&mousevsc_drv);
  485. }
  486. static void __exit mousevsc_exit(void)
  487. {
  488. vmbus_driver_unregister(&mousevsc_drv);
  489. }
  490. MODULE_LICENSE("GPL");
  491. module_init(mousevsc_init);
  492. module_exit(mousevsc_exit);