hidraw.c 10 KB

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