hidraw.c 10 KB

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