hidraw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/fs.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/cdev.h>
  27. #include <linux/poll.h>
  28. #include <linux/device.h>
  29. #include <linux/major.h>
  30. #include <linux/slab.h>
  31. #include <linux/hid.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sched.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 (signal_pending(current)) {
  52. ret = -ERESTARTSYS;
  53. break;
  54. }
  55. if (!list->hidraw->exist) {
  56. ret = -EIO;
  57. break;
  58. }
  59. if (file->f_flags & O_NONBLOCK) {
  60. ret = -EAGAIN;
  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 (list->buffer[list->tail].value) {
  77. if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
  78. ret = -EFAULT;
  79. goto out;
  80. }
  81. ret = len;
  82. }
  83. kfree(list->buffer[list->tail].value);
  84. list->buffer[list->tail].value = NULL;
  85. list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
  86. }
  87. out:
  88. mutex_unlock(&list->read_mutex);
  89. return ret;
  90. }
  91. /* The first byte is expected to be a report number.
  92. * This function is to be called with the minors_lock mutex held */
  93. static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
  94. {
  95. unsigned int minor = iminor(file_inode(file));
  96. struct hid_device *dev;
  97. __u8 *buf;
  98. int ret = 0;
  99. if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
  100. ret = -ENODEV;
  101. goto out;
  102. }
  103. dev = hidraw_table[minor]->hid;
  104. if (!dev->hid_output_raw_report) {
  105. ret = -ENODEV;
  106. goto out;
  107. }
  108. if (count > HID_MAX_BUFFER_SIZE) {
  109. hid_warn(dev, "pid %d passed too large report\n",
  110. task_pid_nr(current));
  111. ret = -EINVAL;
  112. goto out;
  113. }
  114. if (count < 2) {
  115. hid_warn(dev, "pid %d passed too short report\n",
  116. task_pid_nr(current));
  117. ret = -EINVAL;
  118. goto out;
  119. }
  120. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  121. if (!buf) {
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. if (copy_from_user(buf, buffer, count)) {
  126. ret = -EFAULT;
  127. goto out_free;
  128. }
  129. ret = dev->hid_output_raw_report(dev, buf, count, report_type);
  130. out_free:
  131. kfree(buf);
  132. out:
  133. return ret;
  134. }
  135. /* the first byte is expected to be a report number */
  136. static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  137. {
  138. ssize_t ret;
  139. mutex_lock(&minors_lock);
  140. ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
  141. mutex_unlock(&minors_lock);
  142. return ret;
  143. }
  144. /* This function performs a Get_Report transfer over the control endpoint
  145. * per section 7.2.1 of the HID specification, version 1.1. The first byte
  146. * of buffer is the report number to request, or 0x0 if the defice does not
  147. * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
  148. * or HID_INPUT_REPORT. This function is to be called with the minors_lock
  149. * mutex held. */
  150. static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
  151. {
  152. unsigned int minor = iminor(file_inode(file));
  153. struct hid_device *dev;
  154. __u8 *buf;
  155. int ret = 0, len;
  156. unsigned char report_number;
  157. dev = hidraw_table[minor]->hid;
  158. if (!dev->hid_get_raw_report) {
  159. ret = -ENODEV;
  160. goto out;
  161. }
  162. if (count > HID_MAX_BUFFER_SIZE) {
  163. printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
  164. task_pid_nr(current));
  165. ret = -EINVAL;
  166. goto out;
  167. }
  168. if (count < 2) {
  169. printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
  170. task_pid_nr(current));
  171. ret = -EINVAL;
  172. goto out;
  173. }
  174. buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
  175. if (!buf) {
  176. ret = -ENOMEM;
  177. goto out;
  178. }
  179. /* Read the first byte from the user. This is the report number,
  180. * which is passed to dev->hid_get_raw_report(). */
  181. if (copy_from_user(&report_number, buffer, 1)) {
  182. ret = -EFAULT;
  183. goto out_free;
  184. }
  185. ret = dev->hid_get_raw_report(dev, report_number, buf, count, report_type);
  186. if (ret < 0)
  187. goto out_free;
  188. len = (ret < count) ? ret : count;
  189. if (copy_to_user(buffer, buf, len)) {
  190. ret = -EFAULT;
  191. goto out_free;
  192. }
  193. ret = len;
  194. out_free:
  195. kfree(buf);
  196. out:
  197. return ret;
  198. }
  199. static unsigned int hidraw_poll(struct file *file, poll_table *wait)
  200. {
  201. struct hidraw_list *list = file->private_data;
  202. poll_wait(file, &list->hidraw->wait, wait);
  203. if (list->head != list->tail)
  204. return POLLIN | POLLRDNORM;
  205. if (!list->hidraw->exist)
  206. return POLLERR | POLLHUP;
  207. return 0;
  208. }
  209. static int hidraw_open(struct inode *inode, struct file *file)
  210. {
  211. unsigned int minor = iminor(inode);
  212. struct hidraw *dev;
  213. struct hidraw_list *list;
  214. int err = 0;
  215. if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
  216. err = -ENOMEM;
  217. goto out;
  218. }
  219. mutex_lock(&minors_lock);
  220. if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
  221. err = -ENODEV;
  222. goto out_unlock;
  223. }
  224. list->hidraw = hidraw_table[minor];
  225. mutex_init(&list->read_mutex);
  226. list_add_tail(&list->node, &hidraw_table[minor]->list);
  227. file->private_data = list;
  228. dev = hidraw_table[minor];
  229. if (!dev->open++) {
  230. err = hid_hw_power(dev->hid, PM_HINT_FULLON);
  231. if (err < 0) {
  232. dev->open--;
  233. goto out_unlock;
  234. }
  235. err = hid_hw_open(dev->hid);
  236. if (err < 0) {
  237. hid_hw_power(dev->hid, PM_HINT_NORMAL);
  238. dev->open--;
  239. }
  240. }
  241. out_unlock:
  242. mutex_unlock(&minors_lock);
  243. out:
  244. if (err < 0)
  245. kfree(list);
  246. return err;
  247. }
  248. static int hidraw_fasync(int fd, struct file *file, int on)
  249. {
  250. struct hidraw_list *list = file->private_data;
  251. return fasync_helper(fd, file, on, &list->fasync);
  252. }
  253. static void drop_ref(struct hidraw *hidraw, int exists_bit)
  254. {
  255. if (exists_bit) {
  256. hid_hw_close(hidraw->hid);
  257. hidraw->exist = 0;
  258. if (hidraw->open)
  259. wake_up_interruptible(&hidraw->wait);
  260. } else {
  261. --hidraw->open;
  262. }
  263. if (!hidraw->open && !hidraw->exist) {
  264. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  265. hidraw_table[hidraw->minor] = NULL;
  266. kfree(hidraw);
  267. }
  268. }
  269. static int hidraw_release(struct inode * inode, struct file * file)
  270. {
  271. unsigned int minor = iminor(inode);
  272. struct hidraw_list *list = file->private_data;
  273. mutex_lock(&minors_lock);
  274. list_del(&list->node);
  275. kfree(list);
  276. drop_ref(hidraw_table[minor], 0);
  277. mutex_unlock(&minors_lock);
  278. return 0;
  279. }
  280. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  281. unsigned long arg)
  282. {
  283. struct inode *inode = file_inode(file);
  284. unsigned int minor = iminor(inode);
  285. long ret = 0;
  286. struct hidraw *dev;
  287. void __user *user_arg = (void __user*) arg;
  288. mutex_lock(&minors_lock);
  289. dev = hidraw_table[minor];
  290. if (!dev) {
  291. ret = -ENODEV;
  292. goto out;
  293. }
  294. switch (cmd) {
  295. case HIDIOCGRDESCSIZE:
  296. if (put_user(dev->hid->rsize, (int __user *)arg))
  297. ret = -EFAULT;
  298. break;
  299. case HIDIOCGRDESC:
  300. {
  301. __u32 len;
  302. if (get_user(len, (int __user *)arg))
  303. ret = -EFAULT;
  304. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  305. ret = -EINVAL;
  306. else if (copy_to_user(user_arg + offsetof(
  307. struct hidraw_report_descriptor,
  308. value[0]),
  309. dev->hid->rdesc,
  310. min(dev->hid->rsize, len)))
  311. ret = -EFAULT;
  312. break;
  313. }
  314. case HIDIOCGRAWINFO:
  315. {
  316. struct hidraw_devinfo dinfo;
  317. dinfo.bustype = dev->hid->bus;
  318. dinfo.vendor = dev->hid->vendor;
  319. dinfo.product = dev->hid->product;
  320. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  321. ret = -EFAULT;
  322. break;
  323. }
  324. default:
  325. {
  326. struct hid_device *hid = dev->hid;
  327. if (_IOC_TYPE(cmd) != 'H') {
  328. ret = -EINVAL;
  329. break;
  330. }
  331. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
  332. int len = _IOC_SIZE(cmd);
  333. ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
  334. break;
  335. }
  336. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
  337. int len = _IOC_SIZE(cmd);
  338. ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
  339. break;
  340. }
  341. /* Begin Read-only ioctls. */
  342. if (_IOC_DIR(cmd) != _IOC_READ) {
  343. ret = -EINVAL;
  344. break;
  345. }
  346. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  347. int len = strlen(hid->name) + 1;
  348. if (len > _IOC_SIZE(cmd))
  349. len = _IOC_SIZE(cmd);
  350. ret = copy_to_user(user_arg, hid->name, len) ?
  351. -EFAULT : len;
  352. break;
  353. }
  354. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  355. int len = strlen(hid->phys) + 1;
  356. if (len > _IOC_SIZE(cmd))
  357. len = _IOC_SIZE(cmd);
  358. ret = copy_to_user(user_arg, hid->phys, len) ?
  359. -EFAULT : len;
  360. break;
  361. }
  362. }
  363. ret = -ENOTTY;
  364. }
  365. out:
  366. mutex_unlock(&minors_lock);
  367. return ret;
  368. }
  369. static const struct file_operations hidraw_ops = {
  370. .owner = THIS_MODULE,
  371. .read = hidraw_read,
  372. .write = hidraw_write,
  373. .poll = hidraw_poll,
  374. .open = hidraw_open,
  375. .release = hidraw_release,
  376. .unlocked_ioctl = hidraw_ioctl,
  377. .fasync = hidraw_fasync,
  378. #ifdef CONFIG_COMPAT
  379. .compat_ioctl = hidraw_ioctl,
  380. #endif
  381. .llseek = noop_llseek,
  382. };
  383. int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  384. {
  385. struct hidraw *dev = hid->hidraw;
  386. struct hidraw_list *list;
  387. int ret = 0;
  388. list_for_each_entry(list, &dev->list, node) {
  389. int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  390. if (new_head == list->tail)
  391. continue;
  392. if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
  393. ret = -ENOMEM;
  394. break;
  395. }
  396. list->buffer[list->head].len = len;
  397. list->head = new_head;
  398. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  399. }
  400. wake_up_interruptible(&dev->wait);
  401. return ret;
  402. }
  403. EXPORT_SYMBOL_GPL(hidraw_report_event);
  404. int hidraw_connect(struct hid_device *hid)
  405. {
  406. int minor, result;
  407. struct hidraw *dev;
  408. /* we accept any HID device, no matter the applications */
  409. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  410. if (!dev)
  411. return -ENOMEM;
  412. result = -EINVAL;
  413. mutex_lock(&minors_lock);
  414. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  415. if (hidraw_table[minor])
  416. continue;
  417. hidraw_table[minor] = dev;
  418. result = 0;
  419. break;
  420. }
  421. if (result) {
  422. mutex_unlock(&minors_lock);
  423. kfree(dev);
  424. goto out;
  425. }
  426. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  427. NULL, "%s%d", "hidraw", minor);
  428. if (IS_ERR(dev->dev)) {
  429. hidraw_table[minor] = NULL;
  430. mutex_unlock(&minors_lock);
  431. result = PTR_ERR(dev->dev);
  432. kfree(dev);
  433. goto out;
  434. }
  435. mutex_unlock(&minors_lock);
  436. init_waitqueue_head(&dev->wait);
  437. INIT_LIST_HEAD(&dev->list);
  438. dev->hid = hid;
  439. dev->minor = minor;
  440. dev->exist = 1;
  441. hid->hidraw = dev;
  442. out:
  443. return result;
  444. }
  445. EXPORT_SYMBOL_GPL(hidraw_connect);
  446. void hidraw_disconnect(struct hid_device *hid)
  447. {
  448. struct hidraw *hidraw = hid->hidraw;
  449. mutex_lock(&minors_lock);
  450. drop_ref(hidraw, 1);
  451. mutex_unlock(&minors_lock);
  452. }
  453. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  454. int __init hidraw_init(void)
  455. {
  456. int result;
  457. dev_t dev_id;
  458. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  459. HIDRAW_MAX_DEVICES, "hidraw");
  460. hidraw_major = MAJOR(dev_id);
  461. if (result < 0) {
  462. pr_warn("can't get major number\n");
  463. goto out;
  464. }
  465. hidraw_class = class_create(THIS_MODULE, "hidraw");
  466. if (IS_ERR(hidraw_class)) {
  467. result = PTR_ERR(hidraw_class);
  468. goto error_cdev;
  469. }
  470. cdev_init(&hidraw_cdev, &hidraw_ops);
  471. result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  472. if (result < 0)
  473. goto error_class;
  474. printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
  475. out:
  476. return result;
  477. error_class:
  478. class_destroy(hidraw_class);
  479. error_cdev:
  480. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  481. goto out;
  482. }
  483. void hidraw_exit(void)
  484. {
  485. dev_t dev_id = MKDEV(hidraw_major, 0);
  486. cdev_del(&hidraw_cdev);
  487. class_destroy(hidraw_class);
  488. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  489. }