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