hid-ovr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * Oculus VR driver for Linux
  3. *
  4. * Copyright (c) 2013 Lee Cooper <lee.cooper@oculusvr.com>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. /*
  13. * Driver for Oculus VR devices. Based on hidraw driver.
  14. */
  15. #include <linux/cdev.h>
  16. #include <linux/poll.h>
  17. #include <linux/sched.h>
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include <linux/hidraw.h>
  21. #include "hid-ids.h"
  22. #define USB_TRACKER_INTERFACE_PROTOCOL 0
  23. /* number of reports to buffer */
  24. #define OVR_HIDRAW_BUFFER_SIZE 64
  25. #define OVR_HIDRAW_MAX_DEVICES 8
  26. #define OVR_FIRST_MINOR 0
  27. static struct class *ovr_class;
  28. static struct hidraw *ovr_hidraw_table[OVR_HIDRAW_MAX_DEVICES];
  29. static DEFINE_MUTEX(minors_lock);
  30. static int ovr_major;
  31. static struct cdev ovr_cdev;
  32. static ssize_t ovr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  33. {
  34. struct hidraw_list *list = file->private_data;
  35. int ret = 0, len;
  36. DECLARE_WAITQUEUE(wait, current);
  37. mutex_lock(&list->read_mutex);
  38. while (ret == 0) {
  39. if (list->head == list->tail) {
  40. add_wait_queue(&list->hidraw->wait, &wait);
  41. set_current_state(TASK_INTERRUPTIBLE);
  42. while (list->head == list->tail) {
  43. if (signal_pending(current)) {
  44. ret = -ERESTARTSYS;
  45. break;
  46. }
  47. if (!list->hidraw->exist) {
  48. ret = -EIO;
  49. break;
  50. }
  51. if (file->f_flags & O_NONBLOCK) {
  52. ret = -EAGAIN;
  53. break;
  54. }
  55. /* allow O_NONBLOCK to work well from other threads */
  56. mutex_unlock(&list->read_mutex);
  57. schedule();
  58. mutex_lock(&list->read_mutex);
  59. set_current_state(TASK_INTERRUPTIBLE);
  60. }
  61. set_current_state(TASK_RUNNING);
  62. remove_wait_queue(&list->hidraw->wait, &wait);
  63. }
  64. if (ret)
  65. goto out;
  66. len = list->buffer[list->tail].len > count ?
  67. count : list->buffer[list->tail].len;
  68. if (list->buffer[list->tail].value) {
  69. if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
  70. ret = -EFAULT;
  71. goto out;
  72. }
  73. ret = len;
  74. }
  75. kfree(list->buffer[list->tail].value);
  76. list->buffer[list->tail].value = NULL;
  77. list->tail = (list->tail + 1) & (OVR_HIDRAW_BUFFER_SIZE - 1);
  78. }
  79. out:
  80. mutex_unlock(&list->read_mutex);
  81. return ret;
  82. }
  83. /* The first byte is expected to be a report number.
  84. * This function is to be called with the minors_lock mutex held */
  85. static ssize_t ovr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
  86. {
  87. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  88. struct hid_device *dev;
  89. __u8 *buf;
  90. int ret = 0;
  91. if (!ovr_hidraw_table[minor]) {
  92. ret = -ENODEV;
  93. goto out;
  94. }
  95. dev = ovr_hidraw_table[minor]->hid;
  96. if (!dev->hid_output_raw_report) {
  97. ret = -ENODEV;
  98. goto out;
  99. }
  100. if (count > HID_MAX_BUFFER_SIZE) {
  101. hid_warn(dev, "ovr - pid %d passed too large report\n",
  102. task_pid_nr(current));
  103. ret = -EINVAL;
  104. goto out;
  105. }
  106. if (count < 2) {
  107. hid_warn(dev, "ovr - pid %d passed too short report\n",
  108. task_pid_nr(current));
  109. ret = -EINVAL;
  110. goto out;
  111. }
  112. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  113. if (!buf) {
  114. ret = -ENOMEM;
  115. goto out;
  116. }
  117. if (copy_from_user(buf, buffer, count)) {
  118. ret = -EFAULT;
  119. goto out_free;
  120. }
  121. ret = dev->hid_output_raw_report(dev, buf, count, report_type);
  122. out_free:
  123. kfree(buf);
  124. out:
  125. return ret;
  126. }
  127. /* This function performs a Get_Report transfer over the control endpoint
  128. * per section 7.2.1 of the HID specification, version 1.1. The first byte
  129. * of buffer is the report number to request, or 0x0 if the defice does not
  130. * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
  131. * or HID_INPUT_REPORT. This function is to be called with the minors_lock
  132. * mutex held. */
  133. static ssize_t ovr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
  134. {
  135. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  136. struct hid_device *dev;
  137. __u8 *buf;
  138. int ret = 0, len;
  139. unsigned char report_number;
  140. dev = ovr_hidraw_table[minor]->hid;
  141. if (!dev->hid_get_raw_report) {
  142. ret = -ENODEV;
  143. goto out;
  144. }
  145. if (count > HID_MAX_BUFFER_SIZE) {
  146. printk(KERN_WARNING "ovr - hidraw: pid %d passed too large report\n",
  147. task_pid_nr(current));
  148. ret = -EINVAL;
  149. goto out;
  150. }
  151. if (count < 2) {
  152. printk(KERN_WARNING "ovr - hidraw: pid %d passed too short report\n",
  153. task_pid_nr(current));
  154. ret = -EINVAL;
  155. goto out;
  156. }
  157. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  158. if (!buf) {
  159. ret = -ENOMEM;
  160. goto out;
  161. }
  162. /* Read the first byte from the user. This is the report number,
  163. * which is passed to dev->hid_get_raw_report(). */
  164. if (copy_from_user(&report_number, buffer, 1)) {
  165. ret = -EFAULT;
  166. goto out_free;
  167. }
  168. ret = dev->hid_get_raw_report(dev, report_number, buf, count, report_type);
  169. if (ret < 0)
  170. goto out_free;
  171. len = (ret < count) ? ret : count;
  172. if (copy_to_user(buffer, buf, len)) {
  173. ret = -EFAULT;
  174. goto out_free;
  175. }
  176. ret = len;
  177. out_free:
  178. kfree(buf);
  179. out:
  180. return ret;
  181. }
  182. static unsigned int ovr_hidraw_poll(struct file *file, poll_table *wait)
  183. {
  184. struct hidraw_list *list = file->private_data;
  185. poll_wait(file, &list->hidraw->wait, wait);
  186. if (list->head != list->tail)
  187. return POLLIN | POLLRDNORM;
  188. if (!list->hidraw->exist)
  189. return POLLERR | POLLHUP;
  190. return 0;
  191. }
  192. static int ovr_hidraw_open(struct inode *inode, struct file *file)
  193. {
  194. unsigned int minor = iminor(inode);
  195. struct hidraw *dev;
  196. struct hidraw_list *list;
  197. int err = 0;
  198. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  199. err = -ENOMEM;
  200. goto out;
  201. }
  202. mutex_lock(&minors_lock);
  203. if (!ovr_hidraw_table[minor]) {
  204. err = -ENODEV;
  205. goto out_unlock;
  206. }
  207. list->hidraw = ovr_hidraw_table[minor];
  208. mutex_init(&list->read_mutex);
  209. list_add_tail(&list->node, &ovr_hidraw_table[minor]->list);
  210. file->private_data = list;
  211. dev = ovr_hidraw_table[minor];
  212. if (!dev->open++) {
  213. err = hid_hw_power(dev->hid, PM_HINT_FULLON);
  214. if (err < 0) {
  215. dev->open--;
  216. goto out_unlock;
  217. }
  218. err = hid_hw_open(dev->hid);
  219. if (err < 0) {
  220. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  221. dev->open--;
  222. }
  223. }
  224. out_unlock:
  225. mutex_unlock(&minors_lock);
  226. out:
  227. if (err < 0)
  228. kfree(list);
  229. return err;
  230. }
  231. static int ovr_hidraw_fasync(int fd, struct file *file, int on)
  232. {
  233. struct hidraw_list *list = file->private_data;
  234. return fasync_helper(fd, file, on, &list->fasync);
  235. }
  236. static int ovr_hidraw_release(struct inode * inode, struct file * file)
  237. {
  238. unsigned int minor = iminor(inode);
  239. struct hidraw *dev;
  240. struct hidraw_list *list = file->private_data;
  241. int ret;
  242. int i;
  243. mutex_lock(&minors_lock);
  244. if (!ovr_hidraw_table[minor]) {
  245. ret = -ENODEV;
  246. goto unlock;
  247. }
  248. list_del(&list->node);
  249. dev = ovr_hidraw_table[minor];
  250. if (!--dev->open) {
  251. if (list->hidraw->exist) {
  252. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  253. hid_hw_close(dev->hid);
  254. } else {
  255. kfree(list->hidraw);
  256. }
  257. }
  258. for (i = 0; i < OVR_HIDRAW_BUFFER_SIZE; ++i)
  259. kfree(list->buffer[i].value);
  260. kfree(list);
  261. ret = 0;
  262. unlock:
  263. mutex_unlock(&minors_lock);
  264. return ret;
  265. }
  266. int ovr_report_event(struct hid_device *hid, u8 *data, int len)
  267. {
  268. struct hidraw *dev = hid->hidovr;
  269. struct hidraw_list *list;
  270. int ret = 0;
  271. list_for_each_entry(list, &dev->list, node) {
  272. int new_head = (list->head + 1) & (OVR_HIDRAW_BUFFER_SIZE - 1);
  273. if (new_head == list->tail)
  274. continue;
  275. if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
  276. ret = -ENOMEM;
  277. break;
  278. }
  279. list->buffer[list->head].len = len;
  280. list->head = new_head;
  281. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  282. }
  283. wake_up_interruptible(&dev->wait);
  284. return ret;
  285. }
  286. int ovr_connect(struct hid_device *hid)
  287. {
  288. int minor, result;
  289. struct hidraw *dev;
  290. /* we accept any HID device, no matter the applications */
  291. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  292. if (!dev)
  293. return -ENOMEM;
  294. result = -EINVAL;
  295. mutex_lock(&minors_lock);
  296. for (minor = 0; minor < OVR_HIDRAW_MAX_DEVICES; minor++)
  297. {
  298. if (ovr_hidraw_table[minor])
  299. continue;
  300. ovr_hidraw_table[minor] = dev;
  301. result = 0;
  302. break;
  303. }
  304. if (result) {
  305. mutex_unlock(&minors_lock);
  306. kfree(dev);
  307. goto out;
  308. }
  309. dev->dev = device_create(ovr_class, &hid->dev, MKDEV(ovr_major, minor),
  310. NULL, "%s%d", "ovr", minor);
  311. if (IS_ERR(dev->dev)) {
  312. ovr_hidraw_table[minor] = NULL;
  313. mutex_unlock(&minors_lock);
  314. result = PTR_ERR(dev->dev);
  315. kfree(dev);
  316. goto out;
  317. }
  318. mutex_unlock(&minors_lock);
  319. init_waitqueue_head(&dev->wait);
  320. INIT_LIST_HEAD(&dev->list);
  321. dev->hid = hid;
  322. dev->minor = minor;
  323. dev->exist = 1;
  324. hid->hidovr = dev;
  325. out:
  326. return result;
  327. }
  328. void ovr_disconnect(struct hid_device *hid)
  329. {
  330. struct hidraw *hidraw = hid->hidovr;
  331. mutex_lock(&minors_lock);
  332. hidraw->exist = 0;
  333. device_destroy(ovr_class, MKDEV(ovr_major, hidraw->minor));
  334. ovr_hidraw_table[hidraw->minor] = NULL;
  335. if (hidraw->open) {
  336. hid_hw_close(hid);
  337. wake_up_interruptible(&hidraw->wait);
  338. } else {
  339. kfree(hidraw);
  340. }
  341. mutex_unlock(&minors_lock);
  342. }
  343. static long ovr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  344. {
  345. struct inode *inode = file->f_path.dentry->d_inode;
  346. unsigned int minor = iminor(inode);
  347. long ret = 0;
  348. struct hidraw *dev;
  349. void __user *user_arg = (void __user*) arg;
  350. mutex_lock(&minors_lock);
  351. dev = ovr_hidraw_table[minor];
  352. if (!dev) {
  353. ret = -ENODEV;
  354. goto out;
  355. }
  356. switch (cmd) {
  357. case HIDIOCGRDESCSIZE:
  358. if (put_user(dev->hid->rsize, (int __user *)arg))
  359. ret = -EFAULT;
  360. break;
  361. case HIDIOCGRDESC:
  362. {
  363. __u32 len;
  364. if (get_user(len, (int __user *)arg))
  365. ret = -EFAULT;
  366. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  367. ret = -EINVAL;
  368. else if (copy_to_user(user_arg + offsetof(
  369. struct hidraw_report_descriptor,
  370. value[0]),
  371. dev->hid->rdesc,
  372. min(dev->hid->rsize, len)))
  373. ret = -EFAULT;
  374. break;
  375. }
  376. case HIDIOCGRAWINFO:
  377. {
  378. struct hidraw_devinfo dinfo;
  379. dinfo.bustype = dev->hid->bus;
  380. dinfo.vendor = dev->hid->vendor;
  381. dinfo.product = dev->hid->product;
  382. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  383. ret = -EFAULT;
  384. break;
  385. }
  386. default:
  387. {
  388. struct hid_device *hid = dev->hid;
  389. if (_IOC_TYPE(cmd) != 'H') {
  390. ret = -EINVAL;
  391. break;
  392. }
  393. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
  394. int len = _IOC_SIZE(cmd);
  395. ret = ovr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
  396. break;
  397. }
  398. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
  399. int len = _IOC_SIZE(cmd);
  400. ret = ovr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
  401. break;
  402. }
  403. /* Begin Read-only ioctls. */
  404. if (_IOC_DIR(cmd) != _IOC_READ) {
  405. ret = -EINVAL;
  406. break;
  407. }
  408. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  409. int len = strlen(hid->name) + 1;
  410. if (len > _IOC_SIZE(cmd))
  411. len = _IOC_SIZE(cmd);
  412. ret = copy_to_user(user_arg, hid->name, len) ?
  413. -EFAULT : len;
  414. break;
  415. }
  416. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  417. int len = strlen(hid->phys) + 1;
  418. if (len > _IOC_SIZE(cmd))
  419. len = _IOC_SIZE(cmd);
  420. ret = copy_to_user(user_arg, hid->phys, len) ?
  421. -EFAULT : len;
  422. break;
  423. }
  424. }
  425. ret = -ENOTTY;
  426. }
  427. out:
  428. mutex_unlock(&minors_lock);
  429. return ret;
  430. }
  431. static const struct file_operations ovr_ops = {
  432. .owner = THIS_MODULE,
  433. .read = ovr_hidraw_read,
  434. .poll = ovr_hidraw_poll,
  435. .open = ovr_hidraw_open,
  436. .release = ovr_hidraw_release,
  437. .unlocked_ioctl = ovr_hidraw_ioctl,
  438. .fasync = ovr_hidraw_fasync,
  439. .llseek = noop_llseek,
  440. };
  441. static int ovr_probe(struct hid_device *hdev, const struct hid_device_id *id)
  442. {
  443. int retval;
  444. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  445. retval = hid_parse(hdev);
  446. if (retval) {
  447. hid_err(hdev, "ovr - parse failed\n");
  448. goto exit;
  449. }
  450. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  451. if (retval) {
  452. hid_err(hdev, "ovr - hw start failed\n");
  453. goto exit;
  454. }
  455. if (intf->cur_altsetting->desc.bInterfaceProtocol
  456. != USB_TRACKER_INTERFACE_PROTOCOL) {
  457. return 0;
  458. }
  459. retval = ovr_connect(hdev);
  460. if (retval) {
  461. hid_err(hdev, "ovr - Couldn't connect\n");
  462. goto exit_stop;
  463. }
  464. return 0;
  465. exit_stop:
  466. hid_hw_stop(hdev);
  467. exit:
  468. return retval;
  469. }
  470. static void ovr_remove(struct hid_device *hdev)
  471. {
  472. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  473. if (intf->cur_altsetting->desc.bInterfaceProtocol
  474. != USB_TRACKER_INTERFACE_PROTOCOL) {
  475. hid_hw_stop(hdev);
  476. return;
  477. }
  478. ovr_disconnect(hdev);
  479. hid_hw_stop(hdev);
  480. }
  481. static int ovr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
  482. {
  483. int retval = 0;
  484. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  485. if (intf->cur_altsetting->desc.bInterfaceProtocol
  486. != USB_TRACKER_INTERFACE_PROTOCOL) {
  487. return 0;
  488. }
  489. if (hdev->hidovr) {
  490. retval = ovr_report_event(hdev, data, size);
  491. }
  492. return retval;
  493. }
  494. static const struct hid_device_id ovr_devices[] = {
  495. { HID_USB_DEVICE(USB_VENDOR_ID_OVR, USB_DEVICE_ID_OVR_TRACKER) },
  496. { HID_USB_DEVICE(USB_VENDOR_ID_OVR, USB_DEVICE_ID_OVR_KTRACKER) },
  497. { HID_USB_DEVICE(USB_VENDOR_ID_OVR, USB_DEVICE_ID_OVR_LATENCY_TESTER) },
  498. { }
  499. };
  500. MODULE_DEVICE_TABLE(hid, ovr_devices);
  501. static struct hid_driver ovr_driver = {
  502. .name = "ovr",
  503. .id_table = ovr_devices,
  504. .probe = ovr_probe,
  505. .remove = ovr_remove,
  506. .raw_event = ovr_raw_event
  507. };
  508. static int __init ovr_init(void)
  509. {
  510. int retval;
  511. dev_t dev_id;
  512. ovr_class = class_create(THIS_MODULE, "ovr");
  513. if (IS_ERR(ovr_class)) {
  514. return PTR_ERR(ovr_class);
  515. }
  516. retval = hid_register_driver(&ovr_driver);
  517. if (retval < 0) {
  518. pr_warn("ovr_init - Can't register drive.\n");
  519. goto out_class;
  520. }
  521. retval = alloc_chrdev_region(&dev_id, OVR_FIRST_MINOR,
  522. OVR_HIDRAW_MAX_DEVICES, "ovr");
  523. if (retval < 0) {
  524. pr_warn("ovr_init - Can't allocate chrdev region.\n");
  525. goto out_register;
  526. }
  527. ovr_major = MAJOR(dev_id);
  528. cdev_init(&ovr_cdev, &ovr_ops);
  529. cdev_add(&ovr_cdev, dev_id, OVR_HIDRAW_MAX_DEVICES);
  530. return 0;
  531. out_register:
  532. hid_unregister_driver(&ovr_driver);
  533. out_class:
  534. class_destroy(ovr_class);
  535. return retval;
  536. }
  537. static void __exit ovr_exit(void)
  538. {
  539. dev_t dev_id = MKDEV(ovr_major, 0);
  540. cdev_del(&ovr_cdev);
  541. unregister_chrdev_region(dev_id, OVR_HIDRAW_MAX_DEVICES);
  542. hid_unregister_driver(&ovr_driver);
  543. class_destroy(ovr_class);
  544. }
  545. module_init(ovr_init);
  546. module_exit(ovr_exit);
  547. MODULE_AUTHOR("Lee Cooper");
  548. MODULE_DESCRIPTION("USB Oculus VR char device driver.");
  549. MODULE_LICENSE("GPL v2");