hidraw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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/signal.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, 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 __poll_t hidraw_poll(struct file *file, poll_table *wait)
  217. {
  218. struct hidraw_list *list = file->private_data;
  219. __poll_t mask = EPOLLOUT | EPOLLWRNORM; /* hidraw is always writable */
  220. poll_wait(file, &list->hidraw->wait, wait);
  221. if (list->head != list->tail)
  222. mask |= EPOLLIN | EPOLLRDNORM;
  223. if (!list->hidraw->exist)
  224. mask |= EPOLLERR | EPOLLHUP;
  225. return mask;
  226. }
  227. static int hidraw_open(struct inode *inode, struct file *file)
  228. {
  229. unsigned int minor = iminor(inode);
  230. struct hidraw *dev;
  231. struct hidraw_list *list;
  232. unsigned long flags;
  233. int err = 0;
  234. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  235. err = -ENOMEM;
  236. goto out;
  237. }
  238. mutex_lock(&minors_lock);
  239. if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
  240. err = -ENODEV;
  241. goto out_unlock;
  242. }
  243. dev = hidraw_table[minor];
  244. if (!dev->open++) {
  245. err = hid_hw_power(dev->hid, PM_HINT_FULLON);
  246. if (err < 0) {
  247. dev->open--;
  248. goto out_unlock;
  249. }
  250. err = hid_hw_open(dev->hid);
  251. if (err < 0) {
  252. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  253. dev->open--;
  254. goto out_unlock;
  255. }
  256. }
  257. list->hidraw = hidraw_table[minor];
  258. mutex_init(&list->read_mutex);
  259. spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
  260. list_add_tail(&list->node, &hidraw_table[minor]->list);
  261. spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
  262. file->private_data = list;
  263. out_unlock:
  264. mutex_unlock(&minors_lock);
  265. out:
  266. if (err < 0)
  267. kfree(list);
  268. return err;
  269. }
  270. static int hidraw_fasync(int fd, struct file *file, int on)
  271. {
  272. struct hidraw_list *list = file->private_data;
  273. return fasync_helper(fd, file, on, &list->fasync);
  274. }
  275. static void drop_ref(struct hidraw *hidraw, int exists_bit)
  276. {
  277. if (exists_bit) {
  278. hidraw->exist = 0;
  279. if (hidraw->open) {
  280. hid_hw_close(hidraw->hid);
  281. wake_up_interruptible(&hidraw->wait);
  282. }
  283. device_destroy(hidraw_class,
  284. MKDEV(hidraw_major, hidraw->minor));
  285. } else {
  286. --hidraw->open;
  287. }
  288. if (!hidraw->open) {
  289. if (!hidraw->exist) {
  290. hidraw_table[hidraw->minor] = NULL;
  291. kfree(hidraw);
  292. } else {
  293. /* close device for last reader */
  294. hid_hw_close(hidraw->hid);
  295. hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
  296. }
  297. }
  298. }
  299. static int hidraw_release(struct inode * inode, struct file * file)
  300. {
  301. unsigned int minor = iminor(inode);
  302. struct hidraw_list *list = file->private_data;
  303. unsigned long flags;
  304. mutex_lock(&minors_lock);
  305. spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
  306. list_del(&list->node);
  307. spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
  308. kfree(list);
  309. drop_ref(hidraw_table[minor], 0);
  310. mutex_unlock(&minors_lock);
  311. return 0;
  312. }
  313. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  314. unsigned long arg)
  315. {
  316. struct inode *inode = file_inode(file);
  317. unsigned int minor = iminor(inode);
  318. long ret = 0;
  319. struct hidraw *dev;
  320. void __user *user_arg = (void __user*) arg;
  321. mutex_lock(&minors_lock);
  322. dev = hidraw_table[minor];
  323. if (!dev || !dev->exist) {
  324. ret = -ENODEV;
  325. goto out;
  326. }
  327. switch (cmd) {
  328. case HIDIOCGRDESCSIZE:
  329. if (put_user(dev->hid->rsize, (int __user *)arg))
  330. ret = -EFAULT;
  331. break;
  332. case HIDIOCGRDESC:
  333. {
  334. __u32 len;
  335. if (get_user(len, (int __user *)arg))
  336. ret = -EFAULT;
  337. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  338. ret = -EINVAL;
  339. else if (copy_to_user(user_arg + offsetof(
  340. struct hidraw_report_descriptor,
  341. value[0]),
  342. dev->hid->rdesc,
  343. min(dev->hid->rsize, len)))
  344. ret = -EFAULT;
  345. break;
  346. }
  347. case HIDIOCGRAWINFO:
  348. {
  349. struct hidraw_devinfo dinfo;
  350. dinfo.bustype = dev->hid->bus;
  351. dinfo.vendor = dev->hid->vendor;
  352. dinfo.product = dev->hid->product;
  353. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  354. ret = -EFAULT;
  355. break;
  356. }
  357. default:
  358. {
  359. struct hid_device *hid = dev->hid;
  360. if (_IOC_TYPE(cmd) != 'H') {
  361. ret = -EINVAL;
  362. break;
  363. }
  364. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
  365. int len = _IOC_SIZE(cmd);
  366. ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
  367. break;
  368. }
  369. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
  370. int len = _IOC_SIZE(cmd);
  371. ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
  372. break;
  373. }
  374. /* Begin Read-only ioctls. */
  375. if (_IOC_DIR(cmd) != _IOC_READ) {
  376. ret = -EINVAL;
  377. break;
  378. }
  379. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  380. int len = strlen(hid->name) + 1;
  381. if (len > _IOC_SIZE(cmd))
  382. len = _IOC_SIZE(cmd);
  383. ret = copy_to_user(user_arg, hid->name, len) ?
  384. -EFAULT : len;
  385. break;
  386. }
  387. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  388. int len = strlen(hid->phys) + 1;
  389. if (len > _IOC_SIZE(cmd))
  390. len = _IOC_SIZE(cmd);
  391. ret = copy_to_user(user_arg, hid->phys, len) ?
  392. -EFAULT : len;
  393. break;
  394. }
  395. }
  396. ret = -ENOTTY;
  397. }
  398. out:
  399. mutex_unlock(&minors_lock);
  400. return ret;
  401. }
  402. static const struct file_operations hidraw_ops = {
  403. .owner = THIS_MODULE,
  404. .read = hidraw_read,
  405. .write = hidraw_write,
  406. .poll = hidraw_poll,
  407. .open = hidraw_open,
  408. .release = hidraw_release,
  409. .unlocked_ioctl = hidraw_ioctl,
  410. .fasync = hidraw_fasync,
  411. #ifdef CONFIG_COMPAT
  412. .compat_ioctl = hidraw_ioctl,
  413. #endif
  414. .llseek = noop_llseek,
  415. };
  416. int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  417. {
  418. struct hidraw *dev = hid->hidraw;
  419. struct hidraw_list *list;
  420. int ret = 0;
  421. unsigned long flags;
  422. spin_lock_irqsave(&dev->list_lock, flags);
  423. list_for_each_entry(list, &dev->list, node) {
  424. int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  425. if (new_head == list->tail)
  426. continue;
  427. if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
  428. ret = -ENOMEM;
  429. break;
  430. }
  431. list->buffer[list->head].len = len;
  432. list->head = new_head;
  433. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  434. }
  435. spin_unlock_irqrestore(&dev->list_lock, flags);
  436. wake_up_interruptible(&dev->wait);
  437. return ret;
  438. }
  439. EXPORT_SYMBOL_GPL(hidraw_report_event);
  440. int hidraw_connect(struct hid_device *hid)
  441. {
  442. int minor, result;
  443. struct hidraw *dev;
  444. /* we accept any HID device, all applications */
  445. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  446. if (!dev)
  447. return -ENOMEM;
  448. result = -EINVAL;
  449. mutex_lock(&minors_lock);
  450. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  451. if (hidraw_table[minor])
  452. continue;
  453. hidraw_table[minor] = dev;
  454. result = 0;
  455. break;
  456. }
  457. if (result) {
  458. mutex_unlock(&minors_lock);
  459. kfree(dev);
  460. goto out;
  461. }
  462. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  463. NULL, "%s%d", "hidraw", minor);
  464. if (IS_ERR(dev->dev)) {
  465. hidraw_table[minor] = NULL;
  466. mutex_unlock(&minors_lock);
  467. result = PTR_ERR(dev->dev);
  468. kfree(dev);
  469. goto out;
  470. }
  471. init_waitqueue_head(&dev->wait);
  472. spin_lock_init(&dev->list_lock);
  473. INIT_LIST_HEAD(&dev->list);
  474. dev->hid = hid;
  475. dev->minor = minor;
  476. dev->exist = 1;
  477. hid->hidraw = dev;
  478. mutex_unlock(&minors_lock);
  479. out:
  480. return result;
  481. }
  482. EXPORT_SYMBOL_GPL(hidraw_connect);
  483. void hidraw_disconnect(struct hid_device *hid)
  484. {
  485. struct hidraw *hidraw = hid->hidraw;
  486. mutex_lock(&minors_lock);
  487. drop_ref(hidraw, 1);
  488. mutex_unlock(&minors_lock);
  489. }
  490. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  491. int __init hidraw_init(void)
  492. {
  493. int result;
  494. dev_t dev_id;
  495. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  496. HIDRAW_MAX_DEVICES, "hidraw");
  497. if (result < 0) {
  498. pr_warn("can't get major number\n");
  499. goto out;
  500. }
  501. hidraw_major = MAJOR(dev_id);
  502. hidraw_class = class_create(THIS_MODULE, "hidraw");
  503. if (IS_ERR(hidraw_class)) {
  504. result = PTR_ERR(hidraw_class);
  505. goto error_cdev;
  506. }
  507. cdev_init(&hidraw_cdev, &hidraw_ops);
  508. result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  509. if (result < 0)
  510. goto error_class;
  511. printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
  512. out:
  513. return result;
  514. error_class:
  515. class_destroy(hidraw_class);
  516. error_cdev:
  517. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  518. goto out;
  519. }
  520. void hidraw_exit(void)
  521. {
  522. dev_t dev_id = MKDEV(hidraw_major, 0);
  523. cdev_del(&hidraw_cdev);
  524. class_destroy(hidraw_class);
  525. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  526. }