hidraw.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_MUTEX(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_MAX_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. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  137. err = -ENOMEM;
  138. goto out;
  139. }
  140. lock_kernel();
  141. mutex_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. err = dev->hid->ll_driver->open(dev->hid);
  156. if (err < 0)
  157. dev->open--;
  158. }
  159. out_unlock:
  160. mutex_unlock(&minors_lock);
  161. unlock_kernel();
  162. out:
  163. return err;
  164. }
  165. static int hidraw_release(struct inode * inode, struct file * file)
  166. {
  167. unsigned int minor = iminor(inode);
  168. struct hidraw *dev;
  169. struct hidraw_list *list = file->private_data;
  170. if (!hidraw_table[minor]) {
  171. printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
  172. minor);
  173. return -ENODEV;
  174. }
  175. list_del(&list->node);
  176. dev = hidraw_table[minor];
  177. if (!dev->open--) {
  178. if (list->hidraw->exist)
  179. dev->hid->ll_driver->close(dev->hid);
  180. else
  181. kfree(list->hidraw);
  182. }
  183. kfree(list);
  184. return 0;
  185. }
  186. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  187. unsigned long arg)
  188. {
  189. struct inode *inode = file->f_path.dentry->d_inode;
  190. unsigned int minor = iminor(inode);
  191. long ret = 0;
  192. /* FIXME: What stops hidraw_table going NULL */
  193. struct hidraw *dev = hidraw_table[minor];
  194. void __user *user_arg = (void __user*) arg;
  195. lock_kernel();
  196. switch (cmd) {
  197. case HIDIOCGRDESCSIZE:
  198. if (put_user(dev->hid->rsize, (int __user *)arg))
  199. ret = -EFAULT;
  200. break;
  201. case HIDIOCGRDESC:
  202. {
  203. __u32 len;
  204. if (get_user(len, (int __user *)arg))
  205. ret = -EFAULT;
  206. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  207. ret = -EINVAL;
  208. else if (copy_to_user(user_arg + offsetof(
  209. struct hidraw_report_descriptor,
  210. value[0]),
  211. dev->hid->rdesc,
  212. min(dev->hid->rsize, len)))
  213. ret = -EFAULT;
  214. break;
  215. }
  216. case HIDIOCGRAWINFO:
  217. {
  218. struct hidraw_devinfo dinfo;
  219. dinfo.bustype = dev->hid->bus;
  220. dinfo.vendor = dev->hid->vendor;
  221. dinfo.product = dev->hid->product;
  222. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  223. ret = -EFAULT;
  224. break;
  225. }
  226. default:
  227. ret = -ENOTTY;
  228. }
  229. unlock_kernel();
  230. return ret;
  231. }
  232. static const struct file_operations hidraw_ops = {
  233. .owner = THIS_MODULE,
  234. .read = hidraw_read,
  235. .write = hidraw_write,
  236. .poll = hidraw_poll,
  237. .open = hidraw_open,
  238. .release = hidraw_release,
  239. .unlocked_ioctl = hidraw_ioctl,
  240. };
  241. void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  242. {
  243. struct hidraw *dev = hid->hidraw;
  244. struct hidraw_list *list;
  245. list_for_each_entry(list, &dev->list, node) {
  246. list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
  247. list->buffer[list->head].len = len;
  248. list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  249. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  250. }
  251. wake_up_interruptible(&dev->wait);
  252. }
  253. EXPORT_SYMBOL_GPL(hidraw_report_event);
  254. int hidraw_connect(struct hid_device *hid)
  255. {
  256. int minor, result;
  257. struct hidraw *dev;
  258. /* TODO currently we accept any HID device. This should later
  259. * probably be fixed to accept only those devices which provide
  260. * non-input applications
  261. */
  262. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  263. if (!dev)
  264. return -ENOMEM;
  265. result = -EINVAL;
  266. mutex_lock(&minors_lock);
  267. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  268. if (hidraw_table[minor])
  269. continue;
  270. hidraw_table[minor] = dev;
  271. result = 0;
  272. break;
  273. }
  274. if (result) {
  275. mutex_unlock(&minors_lock);
  276. kfree(dev);
  277. goto out;
  278. }
  279. dev->dev = device_create(hidraw_class, NULL, MKDEV(hidraw_major, minor),
  280. NULL, "%s%d", "hidraw", minor);
  281. if (IS_ERR(dev->dev)) {
  282. hidraw_table[minor] = NULL;
  283. mutex_unlock(&minors_lock);
  284. result = PTR_ERR(dev->dev);
  285. kfree(dev);
  286. goto out;
  287. }
  288. mutex_unlock(&minors_lock);
  289. init_waitqueue_head(&dev->wait);
  290. INIT_LIST_HEAD(&dev->list);
  291. dev->hid = hid;
  292. dev->minor = minor;
  293. dev->exist = 1;
  294. hid->hidraw = dev;
  295. out:
  296. return result;
  297. }
  298. EXPORT_SYMBOL_GPL(hidraw_connect);
  299. void hidraw_disconnect(struct hid_device *hid)
  300. {
  301. struct hidraw *hidraw = hid->hidraw;
  302. hidraw->exist = 0;
  303. mutex_lock(&minors_lock);
  304. hidraw_table[hidraw->minor] = NULL;
  305. mutex_unlock(&minors_lock);
  306. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  307. if (hidraw->open) {
  308. hid->ll_driver->close(hid);
  309. wake_up_interruptible(&hidraw->wait);
  310. } else {
  311. kfree(hidraw);
  312. }
  313. }
  314. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  315. int __init hidraw_init(void)
  316. {
  317. int result;
  318. dev_t dev_id;
  319. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  320. HIDRAW_MAX_DEVICES, "hidraw");
  321. hidraw_major = MAJOR(dev_id);
  322. if (result < 0) {
  323. printk(KERN_WARNING "hidraw: can't get major number\n");
  324. result = 0;
  325. goto out;
  326. }
  327. hidraw_class = class_create(THIS_MODULE, "hidraw");
  328. if (IS_ERR(hidraw_class)) {
  329. result = PTR_ERR(hidraw_class);
  330. unregister_chrdev(hidraw_major, "hidraw");
  331. goto out;
  332. }
  333. cdev_init(&hidraw_cdev, &hidraw_ops);
  334. cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  335. out:
  336. return result;
  337. }
  338. void hidraw_exit(void)
  339. {
  340. dev_t dev_id = MKDEV(hidraw_major, 0);
  341. cdev_del(&hidraw_cdev);
  342. class_destroy(hidraw_class);
  343. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  344. }