hidraw.c 10 KB

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