vboxguest_linux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * vboxguest linux pci driver, char-dev and input-device code,
  4. *
  5. * Copyright (C) 2006-2016 Oracle Corporation
  6. */
  7. #include <linux/input.h>
  8. #include <linux/kernel.h>
  9. #include <linux/miscdevice.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/poll.h>
  13. #include <linux/vbox_utils.h>
  14. #include "vboxguest_core.h"
  15. /** The device name. */
  16. #define DEVICE_NAME "vboxguest"
  17. /** The device name for the device node open to everyone. */
  18. #define DEVICE_NAME_USER "vboxuser"
  19. /** VirtualBox PCI vendor ID. */
  20. #define VBOX_VENDORID 0x80ee
  21. /** VMMDev PCI card product ID. */
  22. #define VMMDEV_DEVICEID 0xcafe
  23. /** Mutex protecting the global vbg_gdev pointer used by vbg_get/put_gdev. */
  24. static DEFINE_MUTEX(vbg_gdev_mutex);
  25. /** Global vbg_gdev pointer used by vbg_get/put_gdev. */
  26. static struct vbg_dev *vbg_gdev;
  27. static int vbg_misc_device_open(struct inode *inode, struct file *filp)
  28. {
  29. struct vbg_session *session;
  30. struct vbg_dev *gdev;
  31. /* misc_open sets filp->private_data to our misc device */
  32. gdev = container_of(filp->private_data, struct vbg_dev, misc_device);
  33. session = vbg_core_open_session(gdev, false);
  34. if (IS_ERR(session))
  35. return PTR_ERR(session);
  36. filp->private_data = session;
  37. return 0;
  38. }
  39. static int vbg_misc_device_user_open(struct inode *inode, struct file *filp)
  40. {
  41. struct vbg_session *session;
  42. struct vbg_dev *gdev;
  43. /* misc_open sets filp->private_data to our misc device */
  44. gdev = container_of(filp->private_data, struct vbg_dev,
  45. misc_device_user);
  46. session = vbg_core_open_session(gdev, false);
  47. if (IS_ERR(session))
  48. return PTR_ERR(session);
  49. filp->private_data = session;
  50. return 0;
  51. }
  52. /**
  53. * Close device.
  54. * Return: 0 on success, negated errno on failure.
  55. * @inode: Pointer to inode info structure.
  56. * @filp: Associated file pointer.
  57. */
  58. static int vbg_misc_device_close(struct inode *inode, struct file *filp)
  59. {
  60. vbg_core_close_session(filp->private_data);
  61. filp->private_data = NULL;
  62. return 0;
  63. }
  64. /**
  65. * Device I/O Control entry point.
  66. * Return: 0 on success, negated errno on failure.
  67. * @filp: Associated file pointer.
  68. * @req: The request specified to ioctl().
  69. * @arg: The argument specified to ioctl().
  70. */
  71. static long vbg_misc_device_ioctl(struct file *filp, unsigned int req,
  72. unsigned long arg)
  73. {
  74. struct vbg_session *session = filp->private_data;
  75. size_t returned_size, size;
  76. struct vbg_ioctl_hdr hdr;
  77. bool is_vmmdev_req;
  78. int ret = 0;
  79. void *buf;
  80. if (copy_from_user(&hdr, (void *)arg, sizeof(hdr)))
  81. return -EFAULT;
  82. if (hdr.version != VBG_IOCTL_HDR_VERSION)
  83. return -EINVAL;
  84. if (hdr.size_in < sizeof(hdr) ||
  85. (hdr.size_out && hdr.size_out < sizeof(hdr)))
  86. return -EINVAL;
  87. size = max(hdr.size_in, hdr.size_out);
  88. if (_IOC_SIZE(req) && _IOC_SIZE(req) != size)
  89. return -EINVAL;
  90. if (size > SZ_16M)
  91. return -E2BIG;
  92. /*
  93. * IOCTL_VMMDEV_REQUEST needs the buffer to be below 4G to avoid
  94. * the need for a bounce-buffer and another copy later on.
  95. */
  96. is_vmmdev_req = (req & ~IOCSIZE_MASK) == VBG_IOCTL_VMMDEV_REQUEST(0) ||
  97. req == VBG_IOCTL_VMMDEV_REQUEST_BIG;
  98. if (is_vmmdev_req)
  99. buf = vbg_req_alloc(size, VBG_IOCTL_HDR_TYPE_DEFAULT);
  100. else
  101. buf = kmalloc(size, GFP_KERNEL);
  102. if (!buf)
  103. return -ENOMEM;
  104. *((struct vbg_ioctl_hdr *)buf) = hdr;
  105. if (copy_from_user(buf + sizeof(hdr), (void *)arg + sizeof(hdr),
  106. hdr.size_in - sizeof(hdr))) {
  107. ret = -EFAULT;
  108. goto out;
  109. }
  110. if (hdr.size_in < size)
  111. memset(buf + hdr.size_in, 0, size - hdr.size_in);
  112. ret = vbg_core_ioctl(session, req, buf);
  113. if (ret)
  114. goto out;
  115. returned_size = ((struct vbg_ioctl_hdr *)buf)->size_out;
  116. if (returned_size > size) {
  117. vbg_debug("%s: too much output data %zu > %zu\n",
  118. __func__, returned_size, size);
  119. returned_size = size;
  120. }
  121. if (copy_to_user((void *)arg, buf, returned_size) != 0)
  122. ret = -EFAULT;
  123. out:
  124. if (is_vmmdev_req)
  125. vbg_req_free(buf, size);
  126. else
  127. kfree(buf);
  128. return ret;
  129. }
  130. /** The file_operations structures. */
  131. static const struct file_operations vbg_misc_device_fops = {
  132. .owner = THIS_MODULE,
  133. .open = vbg_misc_device_open,
  134. .release = vbg_misc_device_close,
  135. .unlocked_ioctl = vbg_misc_device_ioctl,
  136. #ifdef CONFIG_COMPAT
  137. .compat_ioctl = vbg_misc_device_ioctl,
  138. #endif
  139. };
  140. static const struct file_operations vbg_misc_device_user_fops = {
  141. .owner = THIS_MODULE,
  142. .open = vbg_misc_device_user_open,
  143. .release = vbg_misc_device_close,
  144. .unlocked_ioctl = vbg_misc_device_ioctl,
  145. #ifdef CONFIG_COMPAT
  146. .compat_ioctl = vbg_misc_device_ioctl,
  147. #endif
  148. };
  149. /**
  150. * Called when the input device is first opened.
  151. *
  152. * Sets up absolute mouse reporting.
  153. */
  154. static int vbg_input_open(struct input_dev *input)
  155. {
  156. struct vbg_dev *gdev = input_get_drvdata(input);
  157. u32 feat = VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE | VMMDEV_MOUSE_NEW_PROTOCOL;
  158. int ret;
  159. ret = vbg_core_set_mouse_status(gdev, feat);
  160. if (ret)
  161. return ret;
  162. return 0;
  163. }
  164. /**
  165. * Called if all open handles to the input device are closed.
  166. *
  167. * Disables absolute reporting.
  168. */
  169. static void vbg_input_close(struct input_dev *input)
  170. {
  171. struct vbg_dev *gdev = input_get_drvdata(input);
  172. vbg_core_set_mouse_status(gdev, 0);
  173. }
  174. /**
  175. * Creates the kernel input device.
  176. *
  177. * Return: 0 on success, negated errno on failure.
  178. */
  179. static int vbg_create_input_device(struct vbg_dev *gdev)
  180. {
  181. struct input_dev *input;
  182. input = devm_input_allocate_device(gdev->dev);
  183. if (!input)
  184. return -ENOMEM;
  185. input->id.bustype = BUS_PCI;
  186. input->id.vendor = VBOX_VENDORID;
  187. input->id.product = VMMDEV_DEVICEID;
  188. input->open = vbg_input_open;
  189. input->close = vbg_input_close;
  190. input->dev.parent = gdev->dev;
  191. input->name = "VirtualBox mouse integration";
  192. input_set_abs_params(input, ABS_X, VMMDEV_MOUSE_RANGE_MIN,
  193. VMMDEV_MOUSE_RANGE_MAX, 0, 0);
  194. input_set_abs_params(input, ABS_Y, VMMDEV_MOUSE_RANGE_MIN,
  195. VMMDEV_MOUSE_RANGE_MAX, 0, 0);
  196. input_set_capability(input, EV_KEY, BTN_MOUSE);
  197. input_set_drvdata(input, gdev);
  198. gdev->input = input;
  199. return input_register_device(gdev->input);
  200. }
  201. static ssize_t host_version_show(struct device *dev,
  202. struct device_attribute *attr, char *buf)
  203. {
  204. struct vbg_dev *gdev = dev_get_drvdata(dev);
  205. return sprintf(buf, "%s\n", gdev->host_version);
  206. }
  207. static ssize_t host_features_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. struct vbg_dev *gdev = dev_get_drvdata(dev);
  211. return sprintf(buf, "%#x\n", gdev->host_features);
  212. }
  213. static DEVICE_ATTR_RO(host_version);
  214. static DEVICE_ATTR_RO(host_features);
  215. /**
  216. * Does the PCI detection and init of the device.
  217. *
  218. * Return: 0 on success, negated errno on failure.
  219. */
  220. static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
  221. {
  222. struct device *dev = &pci->dev;
  223. resource_size_t io, io_len, mmio, mmio_len;
  224. struct vmmdev_memory *vmmdev;
  225. struct vbg_dev *gdev;
  226. int ret;
  227. gdev = devm_kzalloc(dev, sizeof(*gdev), GFP_KERNEL);
  228. if (!gdev)
  229. return -ENOMEM;
  230. ret = pci_enable_device(pci);
  231. if (ret != 0) {
  232. vbg_err("vboxguest: Error enabling device: %d\n", ret);
  233. return ret;
  234. }
  235. ret = -ENODEV;
  236. io = pci_resource_start(pci, 0);
  237. io_len = pci_resource_len(pci, 0);
  238. if (!io || !io_len) {
  239. vbg_err("vboxguest: Error IO-port resource (0) is missing\n");
  240. goto err_disable_pcidev;
  241. }
  242. if (devm_request_region(dev, io, io_len, DEVICE_NAME) == NULL) {
  243. vbg_err("vboxguest: Error could not claim IO resource\n");
  244. ret = -EBUSY;
  245. goto err_disable_pcidev;
  246. }
  247. mmio = pci_resource_start(pci, 1);
  248. mmio_len = pci_resource_len(pci, 1);
  249. if (!mmio || !mmio_len) {
  250. vbg_err("vboxguest: Error MMIO resource (1) is missing\n");
  251. goto err_disable_pcidev;
  252. }
  253. if (devm_request_mem_region(dev, mmio, mmio_len, DEVICE_NAME) == NULL) {
  254. vbg_err("vboxguest: Error could not claim MMIO resource\n");
  255. ret = -EBUSY;
  256. goto err_disable_pcidev;
  257. }
  258. vmmdev = devm_ioremap(dev, mmio, mmio_len);
  259. if (!vmmdev) {
  260. vbg_err("vboxguest: Error ioremap failed; MMIO addr=%pap size=%pap\n",
  261. &mmio, &mmio_len);
  262. goto err_disable_pcidev;
  263. }
  264. /* Validate MMIO region version and size. */
  265. if (vmmdev->version != VMMDEV_MEMORY_VERSION ||
  266. vmmdev->size < 32 || vmmdev->size > mmio_len) {
  267. vbg_err("vboxguest: Bogus VMMDev memory; version=%08x (expected %08x) size=%d (expected <= %d)\n",
  268. vmmdev->version, VMMDEV_MEMORY_VERSION,
  269. vmmdev->size, (int)mmio_len);
  270. goto err_disable_pcidev;
  271. }
  272. gdev->io_port = io;
  273. gdev->mmio = vmmdev;
  274. gdev->dev = dev;
  275. gdev->misc_device.minor = MISC_DYNAMIC_MINOR;
  276. gdev->misc_device.name = DEVICE_NAME;
  277. gdev->misc_device.fops = &vbg_misc_device_fops;
  278. gdev->misc_device_user.minor = MISC_DYNAMIC_MINOR;
  279. gdev->misc_device_user.name = DEVICE_NAME_USER;
  280. gdev->misc_device_user.fops = &vbg_misc_device_user_fops;
  281. ret = vbg_core_init(gdev, VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
  282. if (ret)
  283. goto err_disable_pcidev;
  284. ret = vbg_create_input_device(gdev);
  285. if (ret) {
  286. vbg_err("vboxguest: Error creating input device: %d\n", ret);
  287. goto err_vbg_core_exit;
  288. }
  289. ret = devm_request_irq(dev, pci->irq, vbg_core_isr, IRQF_SHARED,
  290. DEVICE_NAME, gdev);
  291. if (ret) {
  292. vbg_err("vboxguest: Error requesting irq: %d\n", ret);
  293. goto err_vbg_core_exit;
  294. }
  295. ret = misc_register(&gdev->misc_device);
  296. if (ret) {
  297. vbg_err("vboxguest: Error misc_register %s failed: %d\n",
  298. DEVICE_NAME, ret);
  299. goto err_vbg_core_exit;
  300. }
  301. ret = misc_register(&gdev->misc_device_user);
  302. if (ret) {
  303. vbg_err("vboxguest: Error misc_register %s failed: %d\n",
  304. DEVICE_NAME_USER, ret);
  305. goto err_unregister_misc_device;
  306. }
  307. mutex_lock(&vbg_gdev_mutex);
  308. if (!vbg_gdev)
  309. vbg_gdev = gdev;
  310. else
  311. ret = -EBUSY;
  312. mutex_unlock(&vbg_gdev_mutex);
  313. if (ret) {
  314. vbg_err("vboxguest: Error more then 1 vbox guest pci device\n");
  315. goto err_unregister_misc_device_user;
  316. }
  317. pci_set_drvdata(pci, gdev);
  318. device_create_file(dev, &dev_attr_host_version);
  319. device_create_file(dev, &dev_attr_host_features);
  320. vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
  321. gdev->misc_device.minor, pci->irq, gdev->io_port,
  322. &mmio, &mmio_len);
  323. return 0;
  324. err_unregister_misc_device_user:
  325. misc_deregister(&gdev->misc_device_user);
  326. err_unregister_misc_device:
  327. misc_deregister(&gdev->misc_device);
  328. err_vbg_core_exit:
  329. vbg_core_exit(gdev);
  330. err_disable_pcidev:
  331. pci_disable_device(pci);
  332. return ret;
  333. }
  334. static void vbg_pci_remove(struct pci_dev *pci)
  335. {
  336. struct vbg_dev *gdev = pci_get_drvdata(pci);
  337. mutex_lock(&vbg_gdev_mutex);
  338. vbg_gdev = NULL;
  339. mutex_unlock(&vbg_gdev_mutex);
  340. device_remove_file(gdev->dev, &dev_attr_host_features);
  341. device_remove_file(gdev->dev, &dev_attr_host_version);
  342. misc_deregister(&gdev->misc_device_user);
  343. misc_deregister(&gdev->misc_device);
  344. vbg_core_exit(gdev);
  345. pci_disable_device(pci);
  346. }
  347. struct vbg_dev *vbg_get_gdev(void)
  348. {
  349. mutex_lock(&vbg_gdev_mutex);
  350. /*
  351. * Note on success we keep the mutex locked until vbg_put_gdev(),
  352. * this stops vbg_pci_remove from removing the device from underneath
  353. * vboxsf. vboxsf will only hold a reference for a short while.
  354. */
  355. if (vbg_gdev)
  356. return vbg_gdev;
  357. mutex_unlock(&vbg_gdev_mutex);
  358. return ERR_PTR(-ENODEV);
  359. }
  360. EXPORT_SYMBOL(vbg_get_gdev);
  361. void vbg_put_gdev(struct vbg_dev *gdev)
  362. {
  363. WARN_ON(gdev != vbg_gdev);
  364. mutex_unlock(&vbg_gdev_mutex);
  365. }
  366. EXPORT_SYMBOL(vbg_put_gdev);
  367. /**
  368. * Callback for mouse events.
  369. *
  370. * This is called at the end of the ISR, after leaving the event spinlock, if
  371. * VMMDEV_EVENT_MOUSE_POSITION_CHANGED was raised by the host.
  372. *
  373. * @gdev: The device extension.
  374. */
  375. void vbg_linux_mouse_event(struct vbg_dev *gdev)
  376. {
  377. int rc;
  378. /* Report events to the kernel input device */
  379. gdev->mouse_status_req->mouse_features = 0;
  380. gdev->mouse_status_req->pointer_pos_x = 0;
  381. gdev->mouse_status_req->pointer_pos_y = 0;
  382. rc = vbg_req_perform(gdev, gdev->mouse_status_req);
  383. if (rc >= 0) {
  384. input_report_abs(gdev->input, ABS_X,
  385. gdev->mouse_status_req->pointer_pos_x);
  386. input_report_abs(gdev->input, ABS_Y,
  387. gdev->mouse_status_req->pointer_pos_y);
  388. input_sync(gdev->input);
  389. }
  390. }
  391. static const struct pci_device_id vbg_pci_ids[] = {
  392. { .vendor = VBOX_VENDORID, .device = VMMDEV_DEVICEID },
  393. {}
  394. };
  395. MODULE_DEVICE_TABLE(pci, vbg_pci_ids);
  396. static struct pci_driver vbg_pci_driver = {
  397. .name = DEVICE_NAME,
  398. .id_table = vbg_pci_ids,
  399. .probe = vbg_pci_probe,
  400. .remove = vbg_pci_remove,
  401. };
  402. module_pci_driver(vbg_pci_driver);
  403. MODULE_AUTHOR("Oracle Corporation");
  404. MODULE_DESCRIPTION("Oracle VM VirtualBox Guest Additions for Linux Module");
  405. MODULE_LICENSE("GPL");