hidraw.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/hid.h>
  30. #include <linux/mutex.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/hidraw.h>
  33. static int hidraw_major;
  34. static struct cdev hidraw_cdev;
  35. static struct class *hidraw_class;
  36. static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
  37. static DEFINE_SPINLOCK(minors_lock);
  38. static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  39. {
  40. struct hidraw_list *list = file->private_data;
  41. int ret = 0, len;
  42. char *report;
  43. DECLARE_WAITQUEUE(wait, current);
  44. while (ret == 0) {
  45. mutex_lock(&list->read_mutex);
  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. report = list->buffer[list->tail].value;
  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. /* FIXME: What stops hidraw_table going NULL */
  93. struct hid_device *dev = hidraw_table[minor]->hid;
  94. __u8 *buf;
  95. int ret = 0;
  96. if (!dev->hid_output_raw_report)
  97. return -ENODEV;
  98. if (count > HID_MIN_BUFFER_SIZE) {
  99. printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
  100. task_pid_nr(current));
  101. return -EINVAL;
  102. }
  103. if (count < 2) {
  104. printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
  105. task_pid_nr(current));
  106. return -EINVAL;
  107. }
  108. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  109. if (!buf)
  110. return -ENOMEM;
  111. if (copy_from_user(buf, buffer, count)) {
  112. ret = -EFAULT;
  113. goto out;
  114. }
  115. ret = dev->hid_output_raw_report(dev, buf, count);
  116. out:
  117. kfree(buf);
  118. return ret;
  119. }
  120. static unsigned int hidraw_poll(struct file *file, poll_table *wait)
  121. {
  122. struct hidraw_list *list = file->private_data;
  123. poll_wait(file, &list->hidraw->wait, wait);
  124. if (list->head != list->tail)
  125. return POLLIN | POLLRDNORM;
  126. if (!list->hidraw->exist)
  127. return POLLERR | POLLHUP;
  128. return 0;
  129. }
  130. static int hidraw_open(struct inode *inode, struct file *file)
  131. {
  132. unsigned int minor = iminor(inode);
  133. struct hidraw *dev;
  134. struct hidraw_list *list;
  135. int err = 0;
  136. lock_kernel();
  137. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  138. err = -ENOMEM;
  139. goto out;
  140. }
  141. spin_lock(&minors_lock);
  142. if (!hidraw_table[minor]) {
  143. printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
  144. minor);
  145. kfree(list);
  146. err = -ENODEV;
  147. goto out_unlock;
  148. }
  149. list->hidraw = hidraw_table[minor];
  150. mutex_init(&list->read_mutex);
  151. list_add_tail(&list->node, &hidraw_table[minor]->list);
  152. file->private_data = list;
  153. dev = hidraw_table[minor];
  154. if (!dev->open++)
  155. dev->hid->hid_open(dev->hid);
  156. out_unlock:
  157. spin_unlock(&minors_lock);
  158. out:
  159. unlock_kernel();
  160. return err;
  161. }
  162. static int hidraw_release(struct inode * inode, struct file * file)
  163. {
  164. unsigned int minor = iminor(inode);
  165. struct hidraw *dev;
  166. struct hidraw_list *list = file->private_data;
  167. if (!hidraw_table[minor]) {
  168. printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
  169. minor);
  170. return -ENODEV;
  171. }
  172. list_del(&list->node);
  173. dev = hidraw_table[minor];
  174. if (!dev->open--) {
  175. if (list->hidraw->exist)
  176. dev->hid->hid_close(dev->hid);
  177. else
  178. kfree(list->hidraw);
  179. }
  180. kfree(list);
  181. return 0;
  182. }
  183. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  184. unsigned long arg)
  185. {
  186. struct inode *inode = file->f_path.dentry->d_inode;
  187. unsigned int minor = iminor(inode);
  188. long ret = 0;
  189. /* FIXME: What stops hidraw_table going NULL */
  190. struct hidraw *dev = hidraw_table[minor];
  191. void __user *user_arg = (void __user*) arg;
  192. lock_kernel();
  193. switch (cmd) {
  194. case HIDIOCGRDESCSIZE:
  195. if (put_user(dev->hid->rsize, (int __user *)arg))
  196. ret = -EFAULT;
  197. break;
  198. case HIDIOCGRDESC:
  199. {
  200. __u32 len;
  201. if (get_user(len, (int __user *)arg))
  202. ret = -EFAULT;
  203. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  204. ret = -EINVAL;
  205. else if (copy_to_user(user_arg + offsetof(
  206. struct hidraw_report_descriptor,
  207. value[0]),
  208. dev->hid->rdesc,
  209. min(dev->hid->rsize, len)))
  210. ret = -EFAULT;
  211. break;
  212. }
  213. case HIDIOCGRAWINFO:
  214. {
  215. struct hidraw_devinfo dinfo;
  216. dinfo.bustype = dev->hid->bus;
  217. dinfo.vendor = dev->hid->vendor;
  218. dinfo.product = dev->hid->product;
  219. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  220. ret = -EFAULT;
  221. break;
  222. }
  223. default:
  224. ret = -ENOTTY;
  225. }
  226. return ret;
  227. }
  228. static const struct file_operations hidraw_ops = {
  229. .owner = THIS_MODULE,
  230. .read = hidraw_read,
  231. .write = hidraw_write,
  232. .poll = hidraw_poll,
  233. .open = hidraw_open,
  234. .release = hidraw_release,
  235. .unlocked_ioctl = hidraw_ioctl,
  236. };
  237. void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  238. {
  239. struct hidraw *dev = hid->hidraw;
  240. struct hidraw_list *list;
  241. list_for_each_entry(list, &dev->list, node) {
  242. list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
  243. list->buffer[list->head].len = len;
  244. list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  245. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  246. }
  247. wake_up_interruptible(&dev->wait);
  248. }
  249. EXPORT_SYMBOL_GPL(hidraw_report_event);
  250. int hidraw_connect(struct hid_device *hid)
  251. {
  252. int minor, result;
  253. struct hidraw *dev;
  254. /* TODO currently we accept any HID device. This should later
  255. * probably be fixed to accept only those devices which provide
  256. * non-input applications
  257. */
  258. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  259. if (!dev)
  260. return -ENOMEM;
  261. result = -EINVAL;
  262. spin_lock(&minors_lock);
  263. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  264. if (hidraw_table[minor])
  265. continue;
  266. hidraw_table[minor] = dev;
  267. result = 0;
  268. break;
  269. }
  270. spin_unlock(&minors_lock);
  271. if (result) {
  272. kfree(dev);
  273. goto out;
  274. }
  275. dev->dev = device_create_drvdata(hidraw_class, NULL,
  276. MKDEV(hidraw_major, minor), NULL,
  277. "%s%d", "hidraw", minor);
  278. if (IS_ERR(dev->dev)) {
  279. spin_lock(&minors_lock);
  280. hidraw_table[minor] = NULL;
  281. spin_unlock(&minors_lock);
  282. result = PTR_ERR(dev->dev);
  283. kfree(dev);
  284. goto out;
  285. }
  286. init_waitqueue_head(&dev->wait);
  287. INIT_LIST_HEAD(&dev->list);
  288. dev->hid = hid;
  289. dev->minor = minor;
  290. dev->exist = 1;
  291. hid->hidraw = dev;
  292. out:
  293. return result;
  294. }
  295. EXPORT_SYMBOL_GPL(hidraw_connect);
  296. void hidraw_disconnect(struct hid_device *hid)
  297. {
  298. struct hidraw *hidraw = hid->hidraw;
  299. hidraw->exist = 0;
  300. spin_lock(&minors_lock);
  301. hidraw_table[hidraw->minor] = NULL;
  302. spin_unlock(&minors_lock);
  303. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  304. if (hidraw->open) {
  305. hid->hid_close(hid);
  306. wake_up_interruptible(&hidraw->wait);
  307. } else {
  308. kfree(hidraw);
  309. }
  310. }
  311. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  312. int __init hidraw_init(void)
  313. {
  314. int result;
  315. dev_t dev_id;
  316. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  317. HIDRAW_MAX_DEVICES, "hidraw");
  318. hidraw_major = MAJOR(dev_id);
  319. if (result < 0) {
  320. printk(KERN_WARNING "hidraw: can't get major number\n");
  321. result = 0;
  322. goto out;
  323. }
  324. hidraw_class = class_create(THIS_MODULE, "hidraw");
  325. if (IS_ERR(hidraw_class)) {
  326. result = PTR_ERR(hidraw_class);
  327. unregister_chrdev(hidraw_major, "hidraw");
  328. goto out;
  329. }
  330. cdev_init(&hidraw_cdev, &hidraw_ops);
  331. cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  332. out:
  333. return result;
  334. }
  335. void __exit hidraw_exit(void)
  336. {
  337. dev_t dev_id = MKDEV(hidraw_major, 0);
  338. cdev_del(&hidraw_cdev);
  339. class_destroy(hidraw_class);
  340. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  341. }