hidraw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * HID raw devices, giving access to raw HID events.
  3. *
  4. * In comparison to hiddev, this device does not process the
  5. * hid events at all (no parsing, no lookups). This lets applications
  6. * to work on raw hid events as they want to, and avoids a need to
  7. * use a transport-specific userspace libhid/libusb libraries.
  8. *
  9. * Copyright (c) 2007-2014 Jiri Kosina
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/fs.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/cdev.h>
  27. #include <linux/poll.h>
  28. #include <linux/device.h>
  29. #include <linux/major.h>
  30. #include <linux/slab.h>
  31. #include <linux/hid.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sched.h>
  34. #include <linux/string.h>
  35. #include <linux/hidraw.h>
  36. static int hidraw_major;
  37. static struct cdev hidraw_cdev;
  38. static struct class *hidraw_class;
  39. static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
  40. static DEFINE_MUTEX(minors_lock);
  41. static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  42. {
  43. struct hidraw_list *list = file->private_data;
  44. int ret = 0, len;
  45. DECLARE_WAITQUEUE(wait, current);
  46. mutex_lock(&list->read_mutex);
  47. while (ret == 0) {
  48. if (list->head == list->tail) {
  49. add_wait_queue(&list->hidraw->wait, &wait);
  50. set_current_state(TASK_INTERRUPTIBLE);
  51. while (list->head == list->tail) {
  52. if (signal_pending(current)) {
  53. ret = -ERESTARTSYS;
  54. break;
  55. }
  56. if (!list->hidraw->exist) {
  57. ret = -EIO;
  58. break;
  59. }
  60. if (file->f_flags & O_NONBLOCK) {
  61. ret = -EAGAIN;
  62. break;
  63. }
  64. /* allow O_NONBLOCK to work well from other threads */
  65. mutex_unlock(&list->read_mutex);
  66. schedule();
  67. mutex_lock(&list->read_mutex);
  68. set_current_state(TASK_INTERRUPTIBLE);
  69. }
  70. set_current_state(TASK_RUNNING);
  71. remove_wait_queue(&list->hidraw->wait, &wait);
  72. }
  73. if (ret)
  74. goto out;
  75. len = list->buffer[list->tail].len > count ?
  76. count : list->buffer[list->tail].len;
  77. if (list->buffer[list->tail].value) {
  78. if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
  79. ret = -EFAULT;
  80. goto out;
  81. }
  82. ret = len;
  83. }
  84. kfree(list->buffer[list->tail].value);
  85. list->buffer[list->tail].value = NULL;
  86. list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
  87. }
  88. out:
  89. mutex_unlock(&list->read_mutex);
  90. return ret;
  91. }
  92. /*
  93. * The first byte of the report buffer is expected to be a report number.
  94. *
  95. * This function is to be called with the minors_lock mutex held.
  96. */
  97. static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
  98. {
  99. unsigned int minor = iminor(file_inode(file));
  100. struct hid_device *dev;
  101. __u8 *buf;
  102. int ret = 0;
  103. if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
  104. ret = -ENODEV;
  105. goto out;
  106. }
  107. dev = hidraw_table[minor]->hid;
  108. if (count > HID_MAX_BUFFER_SIZE) {
  109. hid_warn(dev, "pid %d passed too large report\n",
  110. task_pid_nr(current));
  111. ret = -EINVAL;
  112. goto out;
  113. }
  114. if (count < 2) {
  115. hid_warn(dev, "pid %d passed too short report\n",
  116. task_pid_nr(current));
  117. ret = -EINVAL;
  118. goto out;
  119. }
  120. buf = memdup_user(buffer, count);
  121. if (IS_ERR(buf)) {
  122. ret = PTR_ERR(buf);
  123. goto out;
  124. }
  125. if ((report_type == HID_OUTPUT_REPORT) &&
  126. !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
  127. ret = hid_hw_output_report(dev, buf, count);
  128. /*
  129. * compatibility with old implementation of USB-HID and I2C-HID:
  130. * if the device does not support receiving output reports,
  131. * on an interrupt endpoint, fallback to SET_REPORT HID command.
  132. */
  133. if (ret != -ENOSYS)
  134. goto out_free;
  135. }
  136. ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
  137. HID_REQ_SET_REPORT);
  138. out_free:
  139. kfree(buf);
  140. out:
  141. return ret;
  142. }
  143. static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  144. {
  145. ssize_t ret;
  146. mutex_lock(&minors_lock);
  147. ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
  148. mutex_unlock(&minors_lock);
  149. return ret;
  150. }
  151. /*
  152. * This function performs a Get_Report transfer over the control endpoint
  153. * per section 7.2.1 of the HID specification, version 1.1. The first byte
  154. * of buffer is the report number to request, or 0x0 if the defice does not
  155. * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
  156. * or HID_INPUT_REPORT.
  157. *
  158. * This function is to be called with the minors_lock mutex held.
  159. */
  160. static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
  161. {
  162. unsigned int minor = iminor(file_inode(file));
  163. struct hid_device *dev;
  164. __u8 *buf;
  165. int ret = 0, len;
  166. unsigned char report_number;
  167. if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
  168. ret = -ENODEV;
  169. goto out;
  170. }
  171. dev = hidraw_table[minor]->hid;
  172. if (!dev->ll_driver->raw_request) {
  173. ret = -ENODEV;
  174. goto out;
  175. }
  176. if (count > HID_MAX_BUFFER_SIZE) {
  177. printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
  178. task_pid_nr(current));
  179. ret = -EINVAL;
  180. goto out;
  181. }
  182. if (count < 2) {
  183. printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
  184. task_pid_nr(current));
  185. ret = -EINVAL;
  186. goto out;
  187. }
  188. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  189. if (!buf) {
  190. ret = -ENOMEM;
  191. goto out;
  192. }
  193. /*
  194. * Read the first byte from the user. This is the report number,
  195. * which is passed to hid_hw_raw_request().
  196. */
  197. if (copy_from_user(&report_number, buffer, 1)) {
  198. ret = -EFAULT;
  199. goto out_free;
  200. }
  201. ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
  202. HID_REQ_GET_REPORT);
  203. if (ret < 0)
  204. goto out_free;
  205. len = (ret < count) ? ret : count;
  206. if (copy_to_user(buffer, buf, len)) {
  207. ret = -EFAULT;
  208. goto out_free;
  209. }
  210. ret = len;
  211. out_free:
  212. kfree(buf);
  213. out:
  214. return ret;
  215. }
  216. static unsigned int hidraw_poll(struct file *file, poll_table *wait)
  217. {
  218. struct hidraw_list *list = file->private_data;
  219. poll_wait(file, &list->hidraw->wait, wait);
  220. if (list->head != list->tail)
  221. return POLLIN | POLLRDNORM;
  222. if (!list->hidraw->exist)
  223. return POLLERR | POLLHUP;
  224. return 0;
  225. }
  226. static int hidraw_open(struct inode *inode, struct file *file)
  227. {
  228. unsigned int minor = iminor(inode);
  229. struct hidraw *dev;
  230. struct hidraw_list *list;
  231. unsigned long flags;
  232. int err = 0;
  233. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  234. err = -ENOMEM;
  235. goto out;
  236. }
  237. mutex_lock(&minors_lock);
  238. if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
  239. err = -ENODEV;
  240. goto out_unlock;
  241. }
  242. dev = hidraw_table[minor];
  243. if (!dev->open++) {
  244. err = hid_hw_power(dev->hid, PM_HINT_FULLON);
  245. if (err < 0) {
  246. dev->open--;
  247. goto out_unlock;
  248. }
  249. err = hid_hw_open(dev->hid);
  250. if (err < 0) {
  251. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  252. dev->open--;
  253. goto out_unlock;
  254. }
  255. }
  256. list->hidraw = hidraw_table[minor];
  257. mutex_init(&list->read_mutex);
  258. spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
  259. list_add_tail(&list->node, &hidraw_table[minor]->list);
  260. spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
  261. file->private_data = list;
  262. out_unlock:
  263. mutex_unlock(&minors_lock);
  264. out:
  265. if (err < 0)
  266. kfree(list);
  267. return err;
  268. }
  269. static int hidraw_fasync(int fd, struct file *file, int on)
  270. {
  271. struct hidraw_list *list = file->private_data;
  272. return fasync_helper(fd, file, on, &list->fasync);
  273. }
  274. static void drop_ref(struct hidraw *hidraw, int exists_bit)
  275. {
  276. if (exists_bit) {
  277. hidraw->exist = 0;
  278. if (hidraw->open) {
  279. hid_hw_close(hidraw->hid);
  280. wake_up_interruptible(&hidraw->wait);
  281. }
  282. device_destroy(hidraw_class,
  283. MKDEV(hidraw_major, hidraw->minor));
  284. } else {
  285. --hidraw->open;
  286. }
  287. if (!hidraw->open) {
  288. if (!hidraw->exist) {
  289. hidraw_table[hidraw->minor] = NULL;
  290. kfree(hidraw);
  291. } else {
  292. /* close device for last reader */
  293. hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
  294. hid_hw_close(hidraw->hid);
  295. }
  296. }
  297. }
  298. static int hidraw_release(struct inode * inode, struct file * file)
  299. {
  300. unsigned int minor = iminor(inode);
  301. struct hidraw_list *list = file->private_data;
  302. unsigned long flags;
  303. mutex_lock(&minors_lock);
  304. spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
  305. list_del(&list->node);
  306. spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
  307. kfree(list);
  308. drop_ref(hidraw_table[minor], 0);
  309. mutex_unlock(&minors_lock);
  310. return 0;
  311. }
  312. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  313. unsigned long arg)
  314. {
  315. struct inode *inode = file_inode(file);
  316. unsigned int minor = iminor(inode);
  317. long ret = 0;
  318. struct hidraw *dev;
  319. void __user *user_arg = (void __user*) arg;
  320. mutex_lock(&minors_lock);
  321. dev = hidraw_table[minor];
  322. if (!dev) {
  323. ret = -ENODEV;
  324. goto out;
  325. }
  326. switch (cmd) {
  327. case HIDIOCGRDESCSIZE:
  328. if (put_user(dev->hid->rsize, (int __user *)arg))
  329. ret = -EFAULT;
  330. break;
  331. case HIDIOCGRDESC:
  332. {
  333. __u32 len;
  334. if (get_user(len, (int __user *)arg))
  335. ret = -EFAULT;
  336. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  337. ret = -EINVAL;
  338. else if (copy_to_user(user_arg + offsetof(
  339. struct hidraw_report_descriptor,
  340. value[0]),
  341. dev->hid->rdesc,
  342. min(dev->hid->rsize, len)))
  343. ret = -EFAULT;
  344. break;
  345. }
  346. case HIDIOCGRAWINFO:
  347. {
  348. struct hidraw_devinfo dinfo;
  349. dinfo.bustype = dev->hid->bus;
  350. dinfo.vendor = dev->hid->vendor;
  351. dinfo.product = dev->hid->product;
  352. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  353. ret = -EFAULT;
  354. break;
  355. }
  356. default:
  357. {
  358. struct hid_device *hid = dev->hid;
  359. if (_IOC_TYPE(cmd) != 'H') {
  360. ret = -EINVAL;
  361. break;
  362. }
  363. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
  364. int len = _IOC_SIZE(cmd);
  365. ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
  366. break;
  367. }
  368. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
  369. int len = _IOC_SIZE(cmd);
  370. ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
  371. break;
  372. }
  373. /* Begin Read-only ioctls. */
  374. if (_IOC_DIR(cmd) != _IOC_READ) {
  375. ret = -EINVAL;
  376. break;
  377. }
  378. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  379. int len = strlen(hid->name) + 1;
  380. if (len > _IOC_SIZE(cmd))
  381. len = _IOC_SIZE(cmd);
  382. ret = copy_to_user(user_arg, hid->name, len) ?
  383. -EFAULT : len;
  384. break;
  385. }
  386. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  387. int len = strlen(hid->phys) + 1;
  388. if (len > _IOC_SIZE(cmd))
  389. len = _IOC_SIZE(cmd);
  390. ret = copy_to_user(user_arg, hid->phys, len) ?
  391. -EFAULT : len;
  392. break;
  393. }
  394. }
  395. ret = -ENOTTY;
  396. }
  397. out:
  398. mutex_unlock(&minors_lock);
  399. return ret;
  400. }
  401. static const struct file_operations hidraw_ops = {
  402. .owner = THIS_MODULE,
  403. .read = hidraw_read,
  404. .write = hidraw_write,
  405. .poll = hidraw_poll,
  406. .open = hidraw_open,
  407. .release = hidraw_release,
  408. .unlocked_ioctl = hidraw_ioctl,
  409. .fasync = hidraw_fasync,
  410. #ifdef CONFIG_COMPAT
  411. .compat_ioctl = hidraw_ioctl,
  412. #endif
  413. .llseek = noop_llseek,
  414. };
  415. int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  416. {
  417. struct hidraw *dev = hid->hidraw;
  418. struct hidraw_list *list;
  419. int ret = 0;
  420. unsigned long flags;
  421. spin_lock_irqsave(&dev->list_lock, flags);
  422. list_for_each_entry(list, &dev->list, node) {
  423. int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  424. if (new_head == list->tail)
  425. continue;
  426. if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
  427. ret = -ENOMEM;
  428. break;
  429. }
  430. list->buffer[list->head].len = len;
  431. list->head = new_head;
  432. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  433. }
  434. spin_unlock_irqrestore(&dev->list_lock, flags);
  435. wake_up_interruptible(&dev->wait);
  436. return ret;
  437. }
  438. EXPORT_SYMBOL_GPL(hidraw_report_event);
  439. int hidraw_connect(struct hid_device *hid)
  440. {
  441. int minor, result;
  442. struct hidraw *dev;
  443. /* we accept any HID device, all applications */
  444. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  445. if (!dev)
  446. return -ENOMEM;
  447. result = -EINVAL;
  448. mutex_lock(&minors_lock);
  449. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  450. if (hidraw_table[minor])
  451. continue;
  452. hidraw_table[minor] = dev;
  453. result = 0;
  454. break;
  455. }
  456. if (result) {
  457. mutex_unlock(&minors_lock);
  458. kfree(dev);
  459. goto out;
  460. }
  461. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  462. NULL, "%s%d", "hidraw", minor);
  463. if (IS_ERR(dev->dev)) {
  464. hidraw_table[minor] = NULL;
  465. mutex_unlock(&minors_lock);
  466. result = PTR_ERR(dev->dev);
  467. kfree(dev);
  468. goto out;
  469. }
  470. init_waitqueue_head(&dev->wait);
  471. spin_lock_init(&dev->list_lock);
  472. INIT_LIST_HEAD(&dev->list);
  473. dev->hid = hid;
  474. dev->minor = minor;
  475. dev->exist = 1;
  476. hid->hidraw = dev;
  477. mutex_unlock(&minors_lock);
  478. out:
  479. return result;
  480. }
  481. EXPORT_SYMBOL_GPL(hidraw_connect);
  482. void hidraw_disconnect(struct hid_device *hid)
  483. {
  484. struct hidraw *hidraw = hid->hidraw;
  485. mutex_lock(&minors_lock);
  486. drop_ref(hidraw, 1);
  487. mutex_unlock(&minors_lock);
  488. }
  489. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  490. int __init hidraw_init(void)
  491. {
  492. int result;
  493. dev_t dev_id;
  494. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  495. HIDRAW_MAX_DEVICES, "hidraw");
  496. if (result < 0) {
  497. pr_warn("can't get major number\n");
  498. goto out;
  499. }
  500. hidraw_major = MAJOR(dev_id);
  501. hidraw_class = class_create(THIS_MODULE, "hidraw");
  502. if (IS_ERR(hidraw_class)) {
  503. result = PTR_ERR(hidraw_class);
  504. goto error_cdev;
  505. }
  506. cdev_init(&hidraw_cdev, &hidraw_ops);
  507. result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  508. if (result < 0)
  509. goto error_class;
  510. printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
  511. out:
  512. return result;
  513. error_class:
  514. class_destroy(hidraw_class);
  515. error_cdev:
  516. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  517. goto out;
  518. }
  519. void hidraw_exit(void)
  520. {
  521. dev_t dev_id = MKDEV(hidraw_major, 0);
  522. cdev_del(&hidraw_cdev);
  523. class_destroy(hidraw_class);
  524. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  525. }