1 /* SPDX-License-Identifier: GPL-2.0 */
3 * vboxguest linux pci driver, char-dev and input-device code,
5 * Copyright (C) 2006-2016 Oracle Corporation
8 #include <linux/input.h>
9 #include <linux/kernel.h>
10 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/poll.h>
14 #include <linux/vbox_utils.h>
15 #include "vboxguest_core.h"
17 /** The device name. */
18 #define DEVICE_NAME "vboxguest"
19 /** The device name for the device node open to everyone. */
20 #define DEVICE_NAME_USER "vboxuser"
21 /** VirtualBox PCI vendor ID. */
22 #define VBOX_VENDORID 0x80ee
23 /** VMMDev PCI card product ID. */
24 #define VMMDEV_DEVICEID 0xcafe
26 /** Mutex protecting the global vbg_gdev pointer used by vbg_get/put_gdev. */
27 static DEFINE_MUTEX(vbg_gdev_mutex
);
28 /** Global vbg_gdev pointer used by vbg_get/put_gdev. */
29 static struct vbg_dev
*vbg_gdev
;
31 static int vbg_misc_device_open(struct inode
*inode
, struct file
*filp
)
33 struct vbg_session
*session
;
36 /* misc_open sets filp->private_data to our misc device */
37 gdev
= container_of(filp
->private_data
, struct vbg_dev
, misc_device
);
39 session
= vbg_core_open_session(gdev
, false);
41 return PTR_ERR(session
);
43 filp
->private_data
= session
;
47 static int vbg_misc_device_user_open(struct inode
*inode
, struct file
*filp
)
49 struct vbg_session
*session
;
52 /* misc_open sets filp->private_data to our misc device */
53 gdev
= container_of(filp
->private_data
, struct vbg_dev
,
56 session
= vbg_core_open_session(gdev
, false);
58 return PTR_ERR(session
);
60 filp
->private_data
= session
;
66 * Return: 0 on success, negated errno on failure.
67 * @inode: Pointer to inode info structure.
68 * @filp: Associated file pointer.
70 static int vbg_misc_device_close(struct inode
*inode
, struct file
*filp
)
72 vbg_core_close_session(filp
->private_data
);
73 filp
->private_data
= NULL
;
78 * Device I/O Control entry point.
79 * Return: 0 on success, negated errno on failure.
80 * @filp: Associated file pointer.
81 * @req: The request specified to ioctl().
82 * @arg: The argument specified to ioctl().
84 static long vbg_misc_device_ioctl(struct file
*filp
, unsigned int req
,
87 struct vbg_session
*session
= filp
->private_data
;
88 size_t returned_size
, size
;
89 struct vbg_ioctl_hdr hdr
;
93 if (copy_from_user(&hdr
, (void *)arg
, sizeof(hdr
)))
96 if (hdr
.version
!= VBG_IOCTL_HDR_VERSION
)
99 if (hdr
.size_in
< sizeof(hdr
) ||
100 (hdr
.size_out
&& hdr
.size_out
< sizeof(hdr
)))
103 size
= max(hdr
.size_in
, hdr
.size_out
);
104 if (_IOC_SIZE(req
) && _IOC_SIZE(req
) != size
)
109 /* __GFP_DMA32 because IOCTL_VMMDEV_REQUEST passes this to the host */
110 buf
= kmalloc(size
, GFP_KERNEL
| __GFP_DMA32
);
114 if (copy_from_user(buf
, (void *)arg
, hdr
.size_in
)) {
118 if (hdr
.size_in
< size
)
119 memset(buf
+ hdr
.size_in
, 0, size
- hdr
.size_in
);
121 ret
= vbg_core_ioctl(session
, req
, buf
);
125 returned_size
= ((struct vbg_ioctl_hdr
*)buf
)->size_out
;
126 if (returned_size
> size
) {
127 vbg_debug("%s: too much output data %zu > %zu\n",
128 __func__
, returned_size
, size
);
129 returned_size
= size
;
131 if (copy_to_user((void *)arg
, buf
, returned_size
) != 0)
140 /** The file_operations structures. */
141 static const struct file_operations vbg_misc_device_fops
= {
142 .owner
= THIS_MODULE
,
143 .open
= vbg_misc_device_open
,
144 .release
= vbg_misc_device_close
,
145 .unlocked_ioctl
= vbg_misc_device_ioctl
,
147 .compat_ioctl
= vbg_misc_device_ioctl
,
150 static const struct file_operations vbg_misc_device_user_fops
= {
151 .owner
= THIS_MODULE
,
152 .open
= vbg_misc_device_user_open
,
153 .release
= vbg_misc_device_close
,
154 .unlocked_ioctl
= vbg_misc_device_ioctl
,
156 .compat_ioctl
= vbg_misc_device_ioctl
,
161 * Called when the input device is first opened.
163 * Sets up absolute mouse reporting.
165 static int vbg_input_open(struct input_dev
*input
)
167 struct vbg_dev
*gdev
= input_get_drvdata(input
);
168 u32 feat
= VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
| VMMDEV_MOUSE_NEW_PROTOCOL
;
171 ret
= vbg_core_set_mouse_status(gdev
, feat
);
179 * Called if all open handles to the input device are closed.
181 * Disables absolute reporting.
183 static void vbg_input_close(struct input_dev
*input
)
185 struct vbg_dev
*gdev
= input_get_drvdata(input
);
187 vbg_core_set_mouse_status(gdev
, 0);
191 * Creates the kernel input device.
193 * Return: 0 on success, negated errno on failure.
195 static int vbg_create_input_device(struct vbg_dev
*gdev
)
197 struct input_dev
*input
;
199 input
= devm_input_allocate_device(gdev
->dev
);
203 input
->id
.bustype
= BUS_PCI
;
204 input
->id
.vendor
= VBOX_VENDORID
;
205 input
->id
.product
= VMMDEV_DEVICEID
;
206 input
->open
= vbg_input_open
;
207 input
->close
= vbg_input_close
;
208 input
->dev
.parent
= gdev
->dev
;
209 input
->name
= "VirtualBox mouse integration";
211 input_set_abs_params(input
, ABS_X
, VMMDEV_MOUSE_RANGE_MIN
,
212 VMMDEV_MOUSE_RANGE_MAX
, 0, 0);
213 input_set_abs_params(input
, ABS_Y
, VMMDEV_MOUSE_RANGE_MIN
,
214 VMMDEV_MOUSE_RANGE_MAX
, 0, 0);
215 input_set_capability(input
, EV_KEY
, BTN_MOUSE
);
216 input_set_drvdata(input
, gdev
);
220 return input_register_device(gdev
->input
);
223 static ssize_t
host_version_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct vbg_dev
*gdev
= dev_get_drvdata(dev
);
228 return sprintf(buf
, "%s\n", gdev
->host_version
);
231 static ssize_t
host_features_show(struct device
*dev
,
232 struct device_attribute
*attr
, char *buf
)
234 struct vbg_dev
*gdev
= dev_get_drvdata(dev
);
236 return sprintf(buf
, "%#x\n", gdev
->host_features
);
239 static DEVICE_ATTR_RO(host_version
);
240 static DEVICE_ATTR_RO(host_features
);
243 * Does the PCI detection and init of the device.
245 * Return: 0 on success, negated errno on failure.
247 static int vbg_pci_probe(struct pci_dev
*pci
, const struct pci_device_id
*id
)
249 struct device
*dev
= &pci
->dev
;
250 resource_size_t io
, io_len
, mmio
, mmio_len
;
251 struct vmmdev_memory
*vmmdev
;
252 struct vbg_dev
*gdev
;
255 gdev
= devm_kzalloc(dev
, sizeof(*gdev
), GFP_KERNEL
);
259 ret
= pci_enable_device(pci
);
261 vbg_err("vboxguest: Error enabling device: %d\n", ret
);
267 io
= pci_resource_start(pci
, 0);
268 io_len
= pci_resource_len(pci
, 0);
269 if (!io
|| !io_len
) {
270 vbg_err("vboxguest: Error IO-port resource (0) is missing\n");
271 goto err_disable_pcidev
;
273 if (devm_request_region(dev
, io
, io_len
, DEVICE_NAME
) == NULL
) {
274 vbg_err("vboxguest: Error could not claim IO resource\n");
276 goto err_disable_pcidev
;
279 mmio
= pci_resource_start(pci
, 1);
280 mmio_len
= pci_resource_len(pci
, 1);
281 if (!mmio
|| !mmio_len
) {
282 vbg_err("vboxguest: Error MMIO resource (1) is missing\n");
283 goto err_disable_pcidev
;
286 if (devm_request_mem_region(dev
, mmio
, mmio_len
, DEVICE_NAME
) == NULL
) {
287 vbg_err("vboxguest: Error could not claim MMIO resource\n");
289 goto err_disable_pcidev
;
292 vmmdev
= devm_ioremap(dev
, mmio
, mmio_len
);
294 vbg_err("vboxguest: Error ioremap failed; MMIO addr=%pap size=%pap\n",
296 goto err_disable_pcidev
;
299 /* Validate MMIO region version and size. */
300 if (vmmdev
->version
!= VMMDEV_MEMORY_VERSION
||
301 vmmdev
->size
< 32 || vmmdev
->size
> mmio_len
) {
302 vbg_err("vboxguest: Bogus VMMDev memory; version=%08x (expected %08x) size=%d (expected <= %d)\n",
303 vmmdev
->version
, VMMDEV_MEMORY_VERSION
,
304 vmmdev
->size
, (int)mmio_len
);
305 goto err_disable_pcidev
;
311 gdev
->misc_device
.minor
= MISC_DYNAMIC_MINOR
;
312 gdev
->misc_device
.name
= DEVICE_NAME
;
313 gdev
->misc_device
.fops
= &vbg_misc_device_fops
;
314 gdev
->misc_device_user
.minor
= MISC_DYNAMIC_MINOR
;
315 gdev
->misc_device_user
.name
= DEVICE_NAME_USER
;
316 gdev
->misc_device_user
.fops
= &vbg_misc_device_user_fops
;
318 ret
= vbg_core_init(gdev
, VMMDEV_EVENT_MOUSE_POSITION_CHANGED
);
320 goto err_disable_pcidev
;
322 ret
= vbg_create_input_device(gdev
);
324 vbg_err("vboxguest: Error creating input device: %d\n", ret
);
325 goto err_vbg_core_exit
;
328 ret
= devm_request_irq(dev
, pci
->irq
, vbg_core_isr
, IRQF_SHARED
,
331 vbg_err("vboxguest: Error requesting irq: %d\n", ret
);
332 goto err_vbg_core_exit
;
335 ret
= misc_register(&gdev
->misc_device
);
337 vbg_err("vboxguest: Error misc_register %s failed: %d\n",
339 goto err_vbg_core_exit
;
342 ret
= misc_register(&gdev
->misc_device_user
);
344 vbg_err("vboxguest: Error misc_register %s failed: %d\n",
345 DEVICE_NAME_USER
, ret
);
346 goto err_unregister_misc_device
;
349 mutex_lock(&vbg_gdev_mutex
);
354 mutex_unlock(&vbg_gdev_mutex
);
357 vbg_err("vboxguest: Error more then 1 vbox guest pci device\n");
358 goto err_unregister_misc_device_user
;
361 pci_set_drvdata(pci
, gdev
);
362 device_create_file(dev
, &dev_attr_host_version
);
363 device_create_file(dev
, &dev_attr_host_features
);
365 vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
366 gdev
->misc_device
.minor
, pci
->irq
, gdev
->io_port
,
371 err_unregister_misc_device_user
:
372 misc_deregister(&gdev
->misc_device_user
);
373 err_unregister_misc_device
:
374 misc_deregister(&gdev
->misc_device
);
378 pci_disable_device(pci
);
383 static void vbg_pci_remove(struct pci_dev
*pci
)
385 struct vbg_dev
*gdev
= pci_get_drvdata(pci
);
387 mutex_lock(&vbg_gdev_mutex
);
389 mutex_unlock(&vbg_gdev_mutex
);
391 device_remove_file(gdev
->dev
, &dev_attr_host_features
);
392 device_remove_file(gdev
->dev
, &dev_attr_host_version
);
393 misc_deregister(&gdev
->misc_device_user
);
394 misc_deregister(&gdev
->misc_device
);
396 pci_disable_device(pci
);
399 struct vbg_dev
*vbg_get_gdev(void)
401 mutex_lock(&vbg_gdev_mutex
);
404 * Note on success we keep the mutex locked until vbg_put_gdev(),
405 * this stops vbg_pci_remove from removing the device from underneath
406 * vboxsf. vboxsf will only hold a reference for a short while.
411 mutex_unlock(&vbg_gdev_mutex
);
412 return ERR_PTR(-ENODEV
);
414 EXPORT_SYMBOL(vbg_get_gdev
);
416 void vbg_put_gdev(struct vbg_dev
*gdev
)
418 WARN_ON(gdev
!= vbg_gdev
);
419 mutex_unlock(&vbg_gdev_mutex
);
421 EXPORT_SYMBOL(vbg_put_gdev
);
424 * Callback for mouse events.
426 * This is called at the end of the ISR, after leaving the event spinlock, if
427 * VMMDEV_EVENT_MOUSE_POSITION_CHANGED was raised by the host.
429 * @gdev: The device extension.
431 void vbg_linux_mouse_event(struct vbg_dev
*gdev
)
435 /* Report events to the kernel input device */
436 gdev
->mouse_status_req
->mouse_features
= 0;
437 gdev
->mouse_status_req
->pointer_pos_x
= 0;
438 gdev
->mouse_status_req
->pointer_pos_y
= 0;
439 rc
= vbg_req_perform(gdev
, gdev
->mouse_status_req
);
441 input_report_abs(gdev
->input
, ABS_X
,
442 gdev
->mouse_status_req
->pointer_pos_x
);
443 input_report_abs(gdev
->input
, ABS_Y
,
444 gdev
->mouse_status_req
->pointer_pos_y
);
445 input_sync(gdev
->input
);
449 static const struct pci_device_id vbg_pci_ids
[] = {
450 { .vendor
= VBOX_VENDORID
, .device
= VMMDEV_DEVICEID
},
453 MODULE_DEVICE_TABLE(pci
, vbg_pci_ids
);
455 static struct pci_driver vbg_pci_driver
= {
457 .id_table
= vbg_pci_ids
,
458 .probe
= vbg_pci_probe
,
459 .remove
= vbg_pci_remove
,
462 module_pci_driver(vbg_pci_driver
);
464 MODULE_AUTHOR("Oracle Corporation");
465 MODULE_DESCRIPTION("Oracle VM VirtualBox Guest Additions for Linux Module");
466 MODULE_LICENSE("GPL");