hidraw.c 10 KB

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