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. #include <linux/fs.h>
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/cdev.h>
  26. #include <linux/poll.h>
  27. #include <linux/device.h>
  28. #include <linux/major.h>
  29. #include <linux/slab.h>
  30. #include <linux/hid.h>
  31. #include <linux/mutex.h>
  32. #include <linux/sched.h>
  33. #include <linux/hidraw.h>
  34. static int hidraw_major;
  35. static struct cdev hidraw_cdev;
  36. static struct class *hidraw_class;
  37. static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
  38. static DEFINE_MUTEX(minors_lock);
  39. static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  40. {
  41. struct hidraw_list *list = file->private_data;
  42. int ret = 0, len;
  43. DECLARE_WAITQUEUE(wait, current);
  44. mutex_lock(&list->read_mutex);
  45. while (ret == 0) {
  46. if (list->head == list->tail) {
  47. add_wait_queue(&list->hidraw->wait, &wait);
  48. set_current_state(TASK_INTERRUPTIBLE);
  49. while (list->head == list->tail) {
  50. if (file->f_flags & O_NONBLOCK) {
  51. ret = -EAGAIN;
  52. break;
  53. }
  54. if (signal_pending(current)) {
  55. ret = -ERESTARTSYS;
  56. break;
  57. }
  58. if (!list->hidraw->exist) {
  59. ret = -EIO;
  60. break;
  61. }
  62. /* allow O_NONBLOCK to work well from other threads */
  63. mutex_unlock(&list->read_mutex);
  64. schedule();
  65. mutex_lock(&list->read_mutex);
  66. set_current_state(TASK_INTERRUPTIBLE);
  67. }
  68. set_current_state(TASK_RUNNING);
  69. remove_wait_queue(&list->hidraw->wait, &wait);
  70. }
  71. if (ret)
  72. goto out;
  73. len = list->buffer[list->tail].len > count ?
  74. count : list->buffer[list->tail].len;
  75. if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
  76. ret = -EFAULT;
  77. goto out;
  78. }
  79. ret += len;
  80. kfree(list->buffer[list->tail].value);
  81. list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
  82. }
  83. out:
  84. mutex_unlock(&list->read_mutex);
  85. return ret;
  86. }
  87. /* the first byte is expected to be a report number */
  88. static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  89. {
  90. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  91. struct hid_device *dev;
  92. __u8 *buf;
  93. int ret = 0;
  94. mutex_lock(&minors_lock);
  95. if (!hidraw_table[minor]) {
  96. ret = -ENODEV;
  97. goto out;
  98. }
  99. dev = hidraw_table[minor]->hid;
  100. if (!dev->hid_output_raw_report) {
  101. ret = -ENODEV;
  102. goto out;
  103. }
  104. if (count > HID_MAX_BUFFER_SIZE) {
  105. printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
  106. task_pid_nr(current));
  107. ret = -EINVAL;
  108. goto out;
  109. }
  110. if (count < 2) {
  111. printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
  112. task_pid_nr(current));
  113. ret = -EINVAL;
  114. goto out;
  115. }
  116. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  117. if (!buf) {
  118. ret = -ENOMEM;
  119. goto out;
  120. }
  121. if (copy_from_user(buf, buffer, count)) {
  122. ret = -EFAULT;
  123. goto out_free;
  124. }
  125. ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT);
  126. out_free:
  127. kfree(buf);
  128. out:
  129. mutex_unlock(&minors_lock);
  130. return ret;
  131. }
  132. static unsigned int hidraw_poll(struct file *file, poll_table *wait)
  133. {
  134. struct hidraw_list *list = file->private_data;
  135. poll_wait(file, &list->hidraw->wait, wait);
  136. if (list->head != list->tail)
  137. return POLLIN | POLLRDNORM;
  138. if (!list->hidraw->exist)
  139. return POLLERR | POLLHUP;
  140. return 0;
  141. }
  142. static int hidraw_open(struct inode *inode, struct file *file)
  143. {
  144. unsigned int minor = iminor(inode);
  145. struct hidraw *dev;
  146. struct hidraw_list *list;
  147. int err = 0;
  148. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  149. err = -ENOMEM;
  150. goto out;
  151. }
  152. mutex_lock(&minors_lock);
  153. if (!hidraw_table[minor]) {
  154. kfree(list);
  155. err = -ENODEV;
  156. goto out_unlock;
  157. }
  158. list->hidraw = hidraw_table[minor];
  159. mutex_init(&list->read_mutex);
  160. list_add_tail(&list->node, &hidraw_table[minor]->list);
  161. file->private_data = list;
  162. dev = hidraw_table[minor];
  163. if (!dev->open++) {
  164. if (dev->hid->ll_driver->power) {
  165. err = dev->hid->ll_driver->power(dev->hid, PM_HINT_FULLON);
  166. if (err < 0)
  167. goto out_unlock;
  168. }
  169. err = dev->hid->ll_driver->open(dev->hid);
  170. if (err < 0) {
  171. if (dev->hid->ll_driver->power)
  172. dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
  173. dev->open--;
  174. }
  175. }
  176. out_unlock:
  177. mutex_unlock(&minors_lock);
  178. out:
  179. return err;
  180. }
  181. static int hidraw_release(struct inode * inode, struct file * file)
  182. {
  183. unsigned int minor = iminor(inode);
  184. struct hidraw *dev;
  185. struct hidraw_list *list = file->private_data;
  186. int ret;
  187. mutex_lock(&minors_lock);
  188. if (!hidraw_table[minor]) {
  189. ret = -ENODEV;
  190. goto unlock;
  191. }
  192. list_del(&list->node);
  193. dev = hidraw_table[minor];
  194. if (!--dev->open) {
  195. if (list->hidraw->exist) {
  196. if (dev->hid->ll_driver->power)
  197. dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
  198. dev->hid->ll_driver->close(dev->hid);
  199. } else {
  200. kfree(list->hidraw);
  201. }
  202. }
  203. kfree(list);
  204. ret = 0;
  205. unlock:
  206. mutex_unlock(&minors_lock);
  207. return ret;
  208. }
  209. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  210. unsigned long arg)
  211. {
  212. struct inode *inode = file->f_path.dentry->d_inode;
  213. unsigned int minor = iminor(inode);
  214. long ret = 0;
  215. struct hidraw *dev;
  216. void __user *user_arg = (void __user*) arg;
  217. mutex_lock(&minors_lock);
  218. dev = hidraw_table[minor];
  219. if (!dev) {
  220. ret = -ENODEV;
  221. goto out;
  222. }
  223. switch (cmd) {
  224. case HIDIOCGRDESCSIZE:
  225. if (put_user(dev->hid->rsize, (int __user *)arg))
  226. ret = -EFAULT;
  227. break;
  228. case HIDIOCGRDESC:
  229. {
  230. __u32 len;
  231. if (get_user(len, (int __user *)arg))
  232. ret = -EFAULT;
  233. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  234. ret = -EINVAL;
  235. else if (copy_to_user(user_arg + offsetof(
  236. struct hidraw_report_descriptor,
  237. value[0]),
  238. dev->hid->rdesc,
  239. min(dev->hid->rsize, len)))
  240. ret = -EFAULT;
  241. break;
  242. }
  243. case HIDIOCGRAWINFO:
  244. {
  245. struct hidraw_devinfo dinfo;
  246. dinfo.bustype = dev->hid->bus;
  247. dinfo.vendor = dev->hid->vendor;
  248. dinfo.product = dev->hid->product;
  249. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  250. ret = -EFAULT;
  251. break;
  252. }
  253. default:
  254. {
  255. struct hid_device *hid = dev->hid;
  256. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
  257. ret = -EINVAL;
  258. break;
  259. }
  260. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  261. int len;
  262. if (!hid->name) {
  263. ret = 0;
  264. break;
  265. }
  266. len = strlen(hid->name) + 1;
  267. if (len > _IOC_SIZE(cmd))
  268. len = _IOC_SIZE(cmd);
  269. ret = copy_to_user(user_arg, hid->name, len) ?
  270. -EFAULT : len;
  271. break;
  272. }
  273. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  274. int len;
  275. if (!hid->phys) {
  276. ret = 0;
  277. break;
  278. }
  279. len = strlen(hid->phys) + 1;
  280. if (len > _IOC_SIZE(cmd))
  281. len = _IOC_SIZE(cmd);
  282. ret = copy_to_user(user_arg, hid->phys, len) ?
  283. -EFAULT : len;
  284. break;
  285. }
  286. }
  287. ret = -ENOTTY;
  288. }
  289. out:
  290. mutex_unlock(&minors_lock);
  291. return ret;
  292. }
  293. static const struct file_operations hidraw_ops = {
  294. .owner = THIS_MODULE,
  295. .read = hidraw_read,
  296. .write = hidraw_write,
  297. .poll = hidraw_poll,
  298. .open = hidraw_open,
  299. .release = hidraw_release,
  300. .unlocked_ioctl = hidraw_ioctl,
  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->ll_driver->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. printk(KERN_WARNING "hidraw: 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. }