hidraw.c 14 KB

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