hidraw.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. err = dev->hid->ll_driver->open(dev->hid);
  156. if (err < 0)
  157. dev->open--;
  158. }
  159. out_unlock:
  160. mutex_unlock(&minors_lock);
  161. unlock_kernel();
  162. out:
  163. return err;
  164. }
  165. static int hidraw_release(struct inode * inode, struct file * file)
  166. {
  167. unsigned int minor = iminor(inode);
  168. struct hidraw *dev;
  169. struct hidraw_list *list = file->private_data;
  170. if (!hidraw_table[minor]) {
  171. printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
  172. minor);
  173. return -ENODEV;
  174. }
  175. list_del(&list->node);
  176. dev = hidraw_table[minor];
  177. if (!--dev->open) {
  178. if (list->hidraw->exist)
  179. dev->hid->ll_driver->close(dev->hid);
  180. else
  181. kfree(list->hidraw);
  182. }
  183. kfree(list);
  184. return 0;
  185. }
  186. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  187. unsigned long arg)
  188. {
  189. struct inode *inode = file->f_path.dentry->d_inode;
  190. unsigned int minor = iminor(inode);
  191. long ret = 0;
  192. /* FIXME: What stops hidraw_table going NULL */
  193. struct hidraw *dev = hidraw_table[minor];
  194. void __user *user_arg = (void __user*) arg;
  195. lock_kernel();
  196. switch (cmd) {
  197. case HIDIOCGRDESCSIZE:
  198. if (put_user(dev->hid->rsize, (int __user *)arg))
  199. ret = -EFAULT;
  200. break;
  201. case HIDIOCGRDESC:
  202. {
  203. __u32 len;
  204. if (get_user(len, (int __user *)arg))
  205. ret = -EFAULT;
  206. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  207. ret = -EINVAL;
  208. else if (copy_to_user(user_arg + offsetof(
  209. struct hidraw_report_descriptor,
  210. value[0]),
  211. dev->hid->rdesc,
  212. min(dev->hid->rsize, len)))
  213. ret = -EFAULT;
  214. break;
  215. }
  216. case HIDIOCGRAWINFO:
  217. {
  218. struct hidraw_devinfo dinfo;
  219. dinfo.bustype = dev->hid->bus;
  220. dinfo.vendor = dev->hid->vendor;
  221. dinfo.product = dev->hid->product;
  222. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  223. ret = -EFAULT;
  224. break;
  225. }
  226. default:
  227. {
  228. struct hid_device *hid = dev->hid;
  229. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
  230. ret = -EINVAL;
  231. break;
  232. }
  233. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  234. int len;
  235. if (!hid->name)
  236. return 0;
  237. len = strlen(hid->name) + 1;
  238. if (len > _IOC_SIZE(cmd))
  239. len = _IOC_SIZE(cmd);
  240. ret = copy_to_user(user_arg, hid->name, len) ?
  241. -EFAULT : len;
  242. break;
  243. }
  244. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  245. int len;
  246. if (!hid->phys)
  247. return 0;
  248. len = strlen(hid->phys) + 1;
  249. if (len > _IOC_SIZE(cmd))
  250. len = _IOC_SIZE(cmd);
  251. ret = copy_to_user(user_arg, hid->phys, len) ?
  252. -EFAULT : len;
  253. break;
  254. }
  255. }
  256. ret = -ENOTTY;
  257. }
  258. unlock_kernel();
  259. return ret;
  260. }
  261. static const struct file_operations hidraw_ops = {
  262. .owner = THIS_MODULE,
  263. .read = hidraw_read,
  264. .write = hidraw_write,
  265. .poll = hidraw_poll,
  266. .open = hidraw_open,
  267. .release = hidraw_release,
  268. .unlocked_ioctl = hidraw_ioctl,
  269. };
  270. void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  271. {
  272. struct hidraw *dev = hid->hidraw;
  273. struct hidraw_list *list;
  274. list_for_each_entry(list, &dev->list, node) {
  275. list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
  276. list->buffer[list->head].len = len;
  277. list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  278. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  279. }
  280. wake_up_interruptible(&dev->wait);
  281. }
  282. EXPORT_SYMBOL_GPL(hidraw_report_event);
  283. int hidraw_connect(struct hid_device *hid)
  284. {
  285. int minor, result;
  286. struct hidraw *dev;
  287. /* TODO currently we accept any HID device. This should later
  288. * probably be fixed to accept only those devices which provide
  289. * non-input applications
  290. */
  291. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  292. if (!dev)
  293. return -ENOMEM;
  294. result = -EINVAL;
  295. mutex_lock(&minors_lock);
  296. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  297. if (hidraw_table[minor])
  298. continue;
  299. hidraw_table[minor] = dev;
  300. result = 0;
  301. break;
  302. }
  303. if (result) {
  304. mutex_unlock(&minors_lock);
  305. kfree(dev);
  306. goto out;
  307. }
  308. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  309. NULL, "%s%d", "hidraw", minor);
  310. if (IS_ERR(dev->dev)) {
  311. hidraw_table[minor] = NULL;
  312. mutex_unlock(&minors_lock);
  313. result = PTR_ERR(dev->dev);
  314. kfree(dev);
  315. goto out;
  316. }
  317. mutex_unlock(&minors_lock);
  318. init_waitqueue_head(&dev->wait);
  319. INIT_LIST_HEAD(&dev->list);
  320. dev->hid = hid;
  321. dev->minor = minor;
  322. dev->exist = 1;
  323. hid->hidraw = dev;
  324. out:
  325. return result;
  326. }
  327. EXPORT_SYMBOL_GPL(hidraw_connect);
  328. void hidraw_disconnect(struct hid_device *hid)
  329. {
  330. struct hidraw *hidraw = hid->hidraw;
  331. hidraw->exist = 0;
  332. mutex_lock(&minors_lock);
  333. hidraw_table[hidraw->minor] = NULL;
  334. mutex_unlock(&minors_lock);
  335. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  336. if (hidraw->open) {
  337. hid->ll_driver->close(hid);
  338. wake_up_interruptible(&hidraw->wait);
  339. } else {
  340. kfree(hidraw);
  341. }
  342. }
  343. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  344. int __init hidraw_init(void)
  345. {
  346. int result;
  347. dev_t dev_id;
  348. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  349. HIDRAW_MAX_DEVICES, "hidraw");
  350. hidraw_major = MAJOR(dev_id);
  351. if (result < 0) {
  352. printk(KERN_WARNING "hidraw: can't get major number\n");
  353. result = 0;
  354. goto out;
  355. }
  356. hidraw_class = class_create(THIS_MODULE, "hidraw");
  357. if (IS_ERR(hidraw_class)) {
  358. result = PTR_ERR(hidraw_class);
  359. unregister_chrdev(hidraw_major, "hidraw");
  360. goto out;
  361. }
  362. cdev_init(&hidraw_cdev, &hidraw_ops);
  363. cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  364. out:
  365. return result;
  366. }
  367. void hidraw_exit(void)
  368. {
  369. dev_t dev_id = MKDEV(hidraw_major, 0);
  370. cdev_del(&hidraw_cdev);
  371. class_destroy(hidraw_class);
  372. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  373. }