hidraw.c 9.0 KB

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