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