hidraw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/smp_lock.h>
  34. #include <linux/hidraw.h>
  35. static int hidraw_major;
  36. static struct cdev hidraw_cdev;
  37. static struct class *hidraw_class;
  38. static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
  39. static DEFINE_MUTEX(minors_lock);
  40. static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  41. {
  42. struct hidraw_list *list = file->private_data;
  43. int ret = 0, len;
  44. char *report;
  45. DECLARE_WAITQUEUE(wait, current);
  46. mutex_lock(&list->read_mutex);
  47. while (ret == 0) {
  48. if (list->head == list->tail) {
  49. add_wait_queue(&list->hidraw->wait, &wait);
  50. set_current_state(TASK_INTERRUPTIBLE);
  51. while (list->head == list->tail) {
  52. if (file->f_flags & O_NONBLOCK) {
  53. ret = -EAGAIN;
  54. break;
  55. }
  56. if (signal_pending(current)) {
  57. ret = -ERESTARTSYS;
  58. break;
  59. }
  60. if (!list->hidraw->exist) {
  61. ret = -EIO;
  62. break;
  63. }
  64. /* allow O_NONBLOCK to work well from other threads */
  65. mutex_unlock(&list->read_mutex);
  66. schedule();
  67. mutex_lock(&list->read_mutex);
  68. set_current_state(TASK_INTERRUPTIBLE);
  69. }
  70. set_current_state(TASK_RUNNING);
  71. remove_wait_queue(&list->hidraw->wait, &wait);
  72. }
  73. if (ret)
  74. goto out;
  75. report = list->buffer[list->tail].value;
  76. len = list->buffer[list->tail].len > count ?
  77. count : list->buffer[list->tail].len;
  78. if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
  79. ret = -EFAULT;
  80. goto out;
  81. }
  82. ret += len;
  83. kfree(list->buffer[list->tail].value);
  84. list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
  85. }
  86. out:
  87. mutex_unlock(&list->read_mutex);
  88. return ret;
  89. }
  90. /* the first byte is expected to be a report number */
  91. static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  92. {
  93. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  94. /* FIXME: What stops hidraw_table going NULL */
  95. struct hid_device *dev = hidraw_table[minor]->hid;
  96. __u8 *buf;
  97. int ret = 0;
  98. if (!dev->hid_output_raw_report)
  99. return -ENODEV;
  100. if (count > HID_MAX_BUFFER_SIZE) {
  101. printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
  102. task_pid_nr(current));
  103. return -EINVAL;
  104. }
  105. if (count < 2) {
  106. printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
  107. task_pid_nr(current));
  108. return -EINVAL;
  109. }
  110. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  111. if (!buf)
  112. return -ENOMEM;
  113. if (copy_from_user(buf, buffer, count)) {
  114. ret = -EFAULT;
  115. goto out;
  116. }
  117. ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT);
  118. out:
  119. kfree(buf);
  120. return ret;
  121. }
  122. static unsigned int hidraw_poll(struct file *file, poll_table *wait)
  123. {
  124. struct hidraw_list *list = file->private_data;
  125. poll_wait(file, &list->hidraw->wait, wait);
  126. if (list->head != list->tail)
  127. return POLLIN | POLLRDNORM;
  128. if (!list->hidraw->exist)
  129. return POLLERR | POLLHUP;
  130. return 0;
  131. }
  132. static int hidraw_open(struct inode *inode, struct file *file)
  133. {
  134. unsigned int minor = iminor(inode);
  135. struct hidraw *dev;
  136. struct hidraw_list *list;
  137. int err = 0;
  138. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  139. err = -ENOMEM;
  140. goto out;
  141. }
  142. lock_kernel();
  143. mutex_lock(&minors_lock);
  144. if (!hidraw_table[minor]) {
  145. printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
  146. minor);
  147. kfree(list);
  148. err = -ENODEV;
  149. goto out_unlock;
  150. }
  151. list->hidraw = hidraw_table[minor];
  152. mutex_init(&list->read_mutex);
  153. list_add_tail(&list->node, &hidraw_table[minor]->list);
  154. file->private_data = list;
  155. dev = hidraw_table[minor];
  156. if (!dev->open++) {
  157. if (dev->hid->ll_driver->power) {
  158. err = dev->hid->ll_driver->power(dev->hid, PM_HINT_FULLON);
  159. if (err < 0)
  160. goto out_unlock;
  161. }
  162. err = dev->hid->ll_driver->open(dev->hid);
  163. if (err < 0) {
  164. if (dev->hid->ll_driver->power)
  165. dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
  166. dev->open--;
  167. }
  168. }
  169. out_unlock:
  170. mutex_unlock(&minors_lock);
  171. unlock_kernel();
  172. out:
  173. return err;
  174. }
  175. static int hidraw_release(struct inode * inode, struct file * file)
  176. {
  177. unsigned int minor = iminor(inode);
  178. struct hidraw *dev;
  179. struct hidraw_list *list = file->private_data;
  180. if (!hidraw_table[minor]) {
  181. printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
  182. minor);
  183. return -ENODEV;
  184. }
  185. list_del(&list->node);
  186. dev = hidraw_table[minor];
  187. if (!--dev->open) {
  188. if (list->hidraw->exist) {
  189. if (dev->hid->ll_driver->power)
  190. dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
  191. dev->hid->ll_driver->close(dev->hid);
  192. } else {
  193. kfree(list->hidraw);
  194. }
  195. }
  196. kfree(list);
  197. return 0;
  198. }
  199. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  200. unsigned long arg)
  201. {
  202. struct inode *inode = file->f_path.dentry->d_inode;
  203. unsigned int minor = iminor(inode);
  204. long ret = 0;
  205. /* FIXME: What stops hidraw_table going NULL */
  206. struct hidraw *dev = hidraw_table[minor];
  207. void __user *user_arg = (void __user*) arg;
  208. lock_kernel();
  209. switch (cmd) {
  210. case HIDIOCGRDESCSIZE:
  211. if (put_user(dev->hid->rsize, (int __user *)arg))
  212. ret = -EFAULT;
  213. break;
  214. case HIDIOCGRDESC:
  215. {
  216. __u32 len;
  217. if (get_user(len, (int __user *)arg))
  218. ret = -EFAULT;
  219. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  220. ret = -EINVAL;
  221. else if (copy_to_user(user_arg + offsetof(
  222. struct hidraw_report_descriptor,
  223. value[0]),
  224. dev->hid->rdesc,
  225. min(dev->hid->rsize, len)))
  226. ret = -EFAULT;
  227. break;
  228. }
  229. case HIDIOCGRAWINFO:
  230. {
  231. struct hidraw_devinfo dinfo;
  232. dinfo.bustype = dev->hid->bus;
  233. dinfo.vendor = dev->hid->vendor;
  234. dinfo.product = dev->hid->product;
  235. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  236. ret = -EFAULT;
  237. break;
  238. }
  239. default:
  240. {
  241. struct hid_device *hid = dev->hid;
  242. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
  243. ret = -EINVAL;
  244. break;
  245. }
  246. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  247. int len;
  248. if (!hid->name) {
  249. ret = 0;
  250. break;
  251. }
  252. len = strlen(hid->name) + 1;
  253. if (len > _IOC_SIZE(cmd))
  254. len = _IOC_SIZE(cmd);
  255. ret = copy_to_user(user_arg, hid->name, len) ?
  256. -EFAULT : len;
  257. break;
  258. }
  259. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  260. int len;
  261. if (!hid->phys) {
  262. ret = 0;
  263. break;
  264. }
  265. len = strlen(hid->phys) + 1;
  266. if (len > _IOC_SIZE(cmd))
  267. len = _IOC_SIZE(cmd);
  268. ret = copy_to_user(user_arg, hid->phys, len) ?
  269. -EFAULT : len;
  270. break;
  271. }
  272. }
  273. ret = -ENOTTY;
  274. }
  275. unlock_kernel();
  276. return ret;
  277. }
  278. static const struct file_operations hidraw_ops = {
  279. .owner = THIS_MODULE,
  280. .read = hidraw_read,
  281. .write = hidraw_write,
  282. .poll = hidraw_poll,
  283. .open = hidraw_open,
  284. .release = hidraw_release,
  285. .unlocked_ioctl = hidraw_ioctl,
  286. };
  287. void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  288. {
  289. struct hidraw *dev = hid->hidraw;
  290. struct hidraw_list *list;
  291. list_for_each_entry(list, &dev->list, node) {
  292. list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
  293. list->buffer[list->head].len = len;
  294. list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  295. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  296. }
  297. wake_up_interruptible(&dev->wait);
  298. }
  299. EXPORT_SYMBOL_GPL(hidraw_report_event);
  300. int hidraw_connect(struct hid_device *hid)
  301. {
  302. int minor, result;
  303. struct hidraw *dev;
  304. /* we accept any HID device, no matter the applications */
  305. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  306. if (!dev)
  307. return -ENOMEM;
  308. result = -EINVAL;
  309. mutex_lock(&minors_lock);
  310. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  311. if (hidraw_table[minor])
  312. continue;
  313. hidraw_table[minor] = dev;
  314. result = 0;
  315. break;
  316. }
  317. if (result) {
  318. mutex_unlock(&minors_lock);
  319. kfree(dev);
  320. goto out;
  321. }
  322. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  323. NULL, "%s%d", "hidraw", minor);
  324. if (IS_ERR(dev->dev)) {
  325. hidraw_table[minor] = NULL;
  326. mutex_unlock(&minors_lock);
  327. result = PTR_ERR(dev->dev);
  328. kfree(dev);
  329. goto out;
  330. }
  331. mutex_unlock(&minors_lock);
  332. init_waitqueue_head(&dev->wait);
  333. INIT_LIST_HEAD(&dev->list);
  334. dev->hid = hid;
  335. dev->minor = minor;
  336. dev->exist = 1;
  337. hid->hidraw = dev;
  338. out:
  339. return result;
  340. }
  341. EXPORT_SYMBOL_GPL(hidraw_connect);
  342. void hidraw_disconnect(struct hid_device *hid)
  343. {
  344. struct hidraw *hidraw = hid->hidraw;
  345. hidraw->exist = 0;
  346. mutex_lock(&minors_lock);
  347. hidraw_table[hidraw->minor] = NULL;
  348. mutex_unlock(&minors_lock);
  349. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  350. if (hidraw->open) {
  351. hid->ll_driver->close(hid);
  352. wake_up_interruptible(&hidraw->wait);
  353. } else {
  354. kfree(hidraw);
  355. }
  356. }
  357. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  358. int __init hidraw_init(void)
  359. {
  360. int result;
  361. dev_t dev_id;
  362. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  363. HIDRAW_MAX_DEVICES, "hidraw");
  364. hidraw_major = MAJOR(dev_id);
  365. if (result < 0) {
  366. printk(KERN_WARNING "hidraw: can't get major number\n");
  367. result = 0;
  368. goto out;
  369. }
  370. hidraw_class = class_create(THIS_MODULE, "hidraw");
  371. if (IS_ERR(hidraw_class)) {
  372. result = PTR_ERR(hidraw_class);
  373. unregister_chrdev(hidraw_major, "hidraw");
  374. goto out;
  375. }
  376. cdev_init(&hidraw_cdev, &hidraw_ops);
  377. cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  378. out:
  379. return result;
  380. }
  381. void hidraw_exit(void)
  382. {
  383. dev_t dev_id = MKDEV(hidraw_major, 0);
  384. cdev_del(&hidraw_cdev);
  385. class_destroy(hidraw_class);
  386. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  387. }