hidraw.c 10 KB

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