hidraw.c 10 KB

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