hidraw.c 14 KB

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