hidraw.c 14 KB

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