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