hidraw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 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 (file->f_flags & O_NONBLOCK) {
  52. ret = -EAGAIN;
  53. break;
  54. }
  55. if (signal_pending(current)) {
  56. ret = -ERESTARTSYS;
  57. break;
  58. }
  59. if (!list->hidraw->exist) {
  60. ret = -EIO;
  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 (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
  77. ret = -EFAULT;
  78. goto out;
  79. }
  80. ret += len;
  81. kfree(list->buffer[list->tail].value);
  82. list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
  83. }
  84. out:
  85. mutex_unlock(&list->read_mutex);
  86. return ret;
  87. }
  88. /* the first byte is expected to be a report number */
  89. static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  90. {
  91. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  92. struct hid_device *dev;
  93. __u8 *buf;
  94. int ret = 0;
  95. mutex_lock(&minors_lock);
  96. if (!hidraw_table[minor]) {
  97. ret = -ENODEV;
  98. goto out;
  99. }
  100. dev = hidraw_table[minor]->hid;
  101. if (!dev->hid_output_raw_report) {
  102. ret = -ENODEV;
  103. goto out;
  104. }
  105. if (count > HID_MAX_BUFFER_SIZE) {
  106. hid_warn(dev, "pid %d passed too large report\n",
  107. task_pid_nr(current));
  108. ret = -EINVAL;
  109. goto out;
  110. }
  111. if (count < 2) {
  112. hid_warn(dev, "pid %d passed too short report\n",
  113. task_pid_nr(current));
  114. ret = -EINVAL;
  115. goto out;
  116. }
  117. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  118. if (!buf) {
  119. ret = -ENOMEM;
  120. goto out;
  121. }
  122. if (copy_from_user(buf, buffer, count)) {
  123. ret = -EFAULT;
  124. goto out_free;
  125. }
  126. ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT);
  127. out_free:
  128. kfree(buf);
  129. out:
  130. mutex_unlock(&minors_lock);
  131. return ret;
  132. }
  133. static unsigned int hidraw_poll(struct file *file, poll_table *wait)
  134. {
  135. struct hidraw_list *list = file->private_data;
  136. poll_wait(file, &list->hidraw->wait, wait);
  137. if (list->head != list->tail)
  138. return POLLIN | POLLRDNORM;
  139. if (!list->hidraw->exist)
  140. return POLLERR | POLLHUP;
  141. return 0;
  142. }
  143. static int hidraw_open(struct inode *inode, struct file *file)
  144. {
  145. unsigned int minor = iminor(inode);
  146. struct hidraw *dev;
  147. struct hidraw_list *list;
  148. int err = 0;
  149. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  150. err = -ENOMEM;
  151. goto out;
  152. }
  153. mutex_lock(&minors_lock);
  154. if (!hidraw_table[minor]) {
  155. kfree(list);
  156. err = -ENODEV;
  157. goto out_unlock;
  158. }
  159. list->hidraw = hidraw_table[minor];
  160. mutex_init(&list->read_mutex);
  161. list_add_tail(&list->node, &hidraw_table[minor]->list);
  162. file->private_data = list;
  163. dev = hidraw_table[minor];
  164. if (!dev->open++) {
  165. err = hid_hw_power(dev->hid, PM_HINT_FULLON);
  166. if (err < 0)
  167. goto out_unlock;
  168. err = hid_hw_open(dev->hid);
  169. if (err < 0) {
  170. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  171. dev->open--;
  172. }
  173. }
  174. out_unlock:
  175. mutex_unlock(&minors_lock);
  176. out:
  177. return err;
  178. }
  179. static int hidraw_release(struct inode * inode, struct file * file)
  180. {
  181. unsigned int minor = iminor(inode);
  182. struct hidraw *dev;
  183. struct hidraw_list *list = file->private_data;
  184. int ret;
  185. mutex_lock(&minors_lock);
  186. if (!hidraw_table[minor]) {
  187. ret = -ENODEV;
  188. goto unlock;
  189. }
  190. list_del(&list->node);
  191. dev = hidraw_table[minor];
  192. if (!--dev->open) {
  193. if (list->hidraw->exist) {
  194. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  195. hid_hw_close(dev->hid);
  196. } else {
  197. kfree(list->hidraw);
  198. }
  199. }
  200. kfree(list);
  201. ret = 0;
  202. unlock:
  203. mutex_unlock(&minors_lock);
  204. return ret;
  205. }
  206. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  207. unsigned long arg)
  208. {
  209. struct inode *inode = file->f_path.dentry->d_inode;
  210. unsigned int minor = iminor(inode);
  211. long ret = 0;
  212. struct hidraw *dev;
  213. void __user *user_arg = (void __user*) arg;
  214. mutex_lock(&minors_lock);
  215. dev = hidraw_table[minor];
  216. if (!dev) {
  217. ret = -ENODEV;
  218. goto out;
  219. }
  220. switch (cmd) {
  221. case HIDIOCGRDESCSIZE:
  222. if (put_user(dev->hid->rsize, (int __user *)arg))
  223. ret = -EFAULT;
  224. break;
  225. case HIDIOCGRDESC:
  226. {
  227. __u32 len;
  228. if (get_user(len, (int __user *)arg))
  229. ret = -EFAULT;
  230. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  231. ret = -EINVAL;
  232. else if (copy_to_user(user_arg + offsetof(
  233. struct hidraw_report_descriptor,
  234. value[0]),
  235. dev->hid->rdesc,
  236. min(dev->hid->rsize, len)))
  237. ret = -EFAULT;
  238. break;
  239. }
  240. case HIDIOCGRAWINFO:
  241. {
  242. struct hidraw_devinfo dinfo;
  243. dinfo.bustype = dev->hid->bus;
  244. dinfo.vendor = dev->hid->vendor;
  245. dinfo.product = dev->hid->product;
  246. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  247. ret = -EFAULT;
  248. break;
  249. }
  250. default:
  251. {
  252. struct hid_device *hid = dev->hid;
  253. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
  254. ret = -EINVAL;
  255. break;
  256. }
  257. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  258. int len;
  259. if (!hid->name) {
  260. ret = 0;
  261. break;
  262. }
  263. len = strlen(hid->name) + 1;
  264. if (len > _IOC_SIZE(cmd))
  265. len = _IOC_SIZE(cmd);
  266. ret = copy_to_user(user_arg, hid->name, len) ?
  267. -EFAULT : len;
  268. break;
  269. }
  270. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  271. int len;
  272. if (!hid->phys) {
  273. ret = 0;
  274. break;
  275. }
  276. len = strlen(hid->phys) + 1;
  277. if (len > _IOC_SIZE(cmd))
  278. len = _IOC_SIZE(cmd);
  279. ret = copy_to_user(user_arg, hid->phys, len) ?
  280. -EFAULT : len;
  281. break;
  282. }
  283. }
  284. ret = -ENOTTY;
  285. }
  286. out:
  287. mutex_unlock(&minors_lock);
  288. return ret;
  289. }
  290. static const struct file_operations hidraw_ops = {
  291. .owner = THIS_MODULE,
  292. .read = hidraw_read,
  293. .write = hidraw_write,
  294. .poll = hidraw_poll,
  295. .open = hidraw_open,
  296. .release = hidraw_release,
  297. .unlocked_ioctl = hidraw_ioctl,
  298. #ifdef CONFIG_COMPAT
  299. .compat_ioctl = hidraw_ioctl,
  300. #endif
  301. .llseek = noop_llseek,
  302. };
  303. void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  304. {
  305. struct hidraw *dev = hid->hidraw;
  306. struct hidraw_list *list;
  307. list_for_each_entry(list, &dev->list, node) {
  308. list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
  309. list->buffer[list->head].len = len;
  310. list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  311. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  312. }
  313. wake_up_interruptible(&dev->wait);
  314. }
  315. EXPORT_SYMBOL_GPL(hidraw_report_event);
  316. int hidraw_connect(struct hid_device *hid)
  317. {
  318. int minor, result;
  319. struct hidraw *dev;
  320. /* we accept any HID device, no matter the applications */
  321. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  322. if (!dev)
  323. return -ENOMEM;
  324. result = -EINVAL;
  325. mutex_lock(&minors_lock);
  326. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  327. if (hidraw_table[minor])
  328. continue;
  329. hidraw_table[minor] = dev;
  330. result = 0;
  331. break;
  332. }
  333. if (result) {
  334. mutex_unlock(&minors_lock);
  335. kfree(dev);
  336. goto out;
  337. }
  338. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  339. NULL, "%s%d", "hidraw", minor);
  340. if (IS_ERR(dev->dev)) {
  341. hidraw_table[minor] = NULL;
  342. mutex_unlock(&minors_lock);
  343. result = PTR_ERR(dev->dev);
  344. kfree(dev);
  345. goto out;
  346. }
  347. mutex_unlock(&minors_lock);
  348. init_waitqueue_head(&dev->wait);
  349. INIT_LIST_HEAD(&dev->list);
  350. dev->hid = hid;
  351. dev->minor = minor;
  352. dev->exist = 1;
  353. hid->hidraw = dev;
  354. out:
  355. return result;
  356. }
  357. EXPORT_SYMBOL_GPL(hidraw_connect);
  358. void hidraw_disconnect(struct hid_device *hid)
  359. {
  360. struct hidraw *hidraw = hid->hidraw;
  361. hidraw->exist = 0;
  362. mutex_lock(&minors_lock);
  363. hidraw_table[hidraw->minor] = NULL;
  364. mutex_unlock(&minors_lock);
  365. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  366. if (hidraw->open) {
  367. hid_hw_close(hid);
  368. wake_up_interruptible(&hidraw->wait);
  369. } else {
  370. kfree(hidraw);
  371. }
  372. }
  373. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  374. int __init hidraw_init(void)
  375. {
  376. int result;
  377. dev_t dev_id;
  378. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  379. HIDRAW_MAX_DEVICES, "hidraw");
  380. hidraw_major = MAJOR(dev_id);
  381. if (result < 0) {
  382. pr_warn("can't get major number\n");
  383. result = 0;
  384. goto out;
  385. }
  386. hidraw_class = class_create(THIS_MODULE, "hidraw");
  387. if (IS_ERR(hidraw_class)) {
  388. result = PTR_ERR(hidraw_class);
  389. unregister_chrdev(hidraw_major, "hidraw");
  390. goto out;
  391. }
  392. cdev_init(&hidraw_cdev, &hidraw_ops);
  393. cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  394. out:
  395. return result;
  396. }
  397. void hidraw_exit(void)
  398. {
  399. dev_t dev_id = MKDEV(hidraw_major, 0);
  400. cdev_del(&hidraw_cdev);
  401. class_destroy(hidraw_class);
  402. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  403. }