hidraw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 (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 (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->f_path.dentry->d_inode);
  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->f_path.dentry->d_inode);
  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 void drop_ref(struct hidraw *hidraw, int exists_bit)
  249. {
  250. if (exists_bit) {
  251. hid_hw_close(hidraw->hid);
  252. hidraw->exist = 0;
  253. if (hidraw->open)
  254. wake_up_interruptible(&hidraw->wait);
  255. } else {
  256. --hidraw->open;
  257. }
  258. if (!hidraw->open && !hidraw->exist) {
  259. device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
  260. hidraw_table[hidraw->minor] = NULL;
  261. kfree(hidraw);
  262. }
  263. }
  264. static int hidraw_release(struct inode * inode, struct file * file)
  265. {
  266. unsigned int minor = iminor(inode);
  267. struct hidraw_list *list = file->private_data;
  268. mutex_lock(&minors_lock);
  269. list_del(&list->node);
  270. kfree(list);
  271. drop_ref(hidraw_table[minor], 0);
  272. mutex_unlock(&minors_lock);
  273. return 0;
  274. }
  275. static long hidraw_ioctl(struct file *file, unsigned int cmd,
  276. unsigned long arg)
  277. {
  278. struct inode *inode = file->f_path.dentry->d_inode;
  279. unsigned int minor = iminor(inode);
  280. long ret = 0;
  281. struct hidraw *dev;
  282. void __user *user_arg = (void __user*) arg;
  283. mutex_lock(&minors_lock);
  284. dev = hidraw_table[minor];
  285. if (!dev) {
  286. ret = -ENODEV;
  287. goto out;
  288. }
  289. switch (cmd) {
  290. case HIDIOCGRDESCSIZE:
  291. if (put_user(dev->hid->rsize, (int __user *)arg))
  292. ret = -EFAULT;
  293. break;
  294. case HIDIOCGRDESC:
  295. {
  296. __u32 len;
  297. if (get_user(len, (int __user *)arg))
  298. ret = -EFAULT;
  299. else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
  300. ret = -EINVAL;
  301. else if (copy_to_user(user_arg + offsetof(
  302. struct hidraw_report_descriptor,
  303. value[0]),
  304. dev->hid->rdesc,
  305. min(dev->hid->rsize, len)))
  306. ret = -EFAULT;
  307. break;
  308. }
  309. case HIDIOCGRAWINFO:
  310. {
  311. struct hidraw_devinfo dinfo;
  312. dinfo.bustype = dev->hid->bus;
  313. dinfo.vendor = dev->hid->vendor;
  314. dinfo.product = dev->hid->product;
  315. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  316. ret = -EFAULT;
  317. break;
  318. }
  319. default:
  320. {
  321. struct hid_device *hid = dev->hid;
  322. if (_IOC_TYPE(cmd) != 'H') {
  323. ret = -EINVAL;
  324. break;
  325. }
  326. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
  327. int len = _IOC_SIZE(cmd);
  328. ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
  329. break;
  330. }
  331. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
  332. int len = _IOC_SIZE(cmd);
  333. ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
  334. break;
  335. }
  336. /* Begin Read-only ioctls. */
  337. if (_IOC_DIR(cmd) != _IOC_READ) {
  338. ret = -EINVAL;
  339. break;
  340. }
  341. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
  342. int len = strlen(hid->name) + 1;
  343. if (len > _IOC_SIZE(cmd))
  344. len = _IOC_SIZE(cmd);
  345. ret = copy_to_user(user_arg, hid->name, len) ?
  346. -EFAULT : len;
  347. break;
  348. }
  349. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
  350. int len = strlen(hid->phys) + 1;
  351. if (len > _IOC_SIZE(cmd))
  352. len = _IOC_SIZE(cmd);
  353. ret = copy_to_user(user_arg, hid->phys, len) ?
  354. -EFAULT : len;
  355. break;
  356. }
  357. }
  358. ret = -ENOTTY;
  359. }
  360. out:
  361. mutex_unlock(&minors_lock);
  362. return ret;
  363. }
  364. static const struct file_operations hidraw_ops = {
  365. .owner = THIS_MODULE,
  366. .read = hidraw_read,
  367. .write = hidraw_write,
  368. .poll = hidraw_poll,
  369. .open = hidraw_open,
  370. .release = hidraw_release,
  371. .unlocked_ioctl = hidraw_ioctl,
  372. #ifdef CONFIG_COMPAT
  373. .compat_ioctl = hidraw_ioctl,
  374. #endif
  375. .llseek = noop_llseek,
  376. };
  377. int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
  378. {
  379. struct hidraw *dev = hid->hidraw;
  380. struct hidraw_list *list;
  381. int ret = 0;
  382. list_for_each_entry(list, &dev->list, node) {
  383. int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
  384. if (new_head == list->tail)
  385. continue;
  386. if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
  387. ret = -ENOMEM;
  388. break;
  389. }
  390. list->buffer[list->head].len = len;
  391. list->head = new_head;
  392. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  393. }
  394. wake_up_interruptible(&dev->wait);
  395. return ret;
  396. }
  397. EXPORT_SYMBOL_GPL(hidraw_report_event);
  398. int hidraw_connect(struct hid_device *hid)
  399. {
  400. int minor, result;
  401. struct hidraw *dev;
  402. /* we accept any HID device, no matter the applications */
  403. dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
  404. if (!dev)
  405. return -ENOMEM;
  406. result = -EINVAL;
  407. mutex_lock(&minors_lock);
  408. for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
  409. if (hidraw_table[minor])
  410. continue;
  411. hidraw_table[minor] = dev;
  412. result = 0;
  413. break;
  414. }
  415. if (result) {
  416. mutex_unlock(&minors_lock);
  417. kfree(dev);
  418. goto out;
  419. }
  420. dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
  421. NULL, "%s%d", "hidraw", minor);
  422. if (IS_ERR(dev->dev)) {
  423. hidraw_table[minor] = NULL;
  424. mutex_unlock(&minors_lock);
  425. result = PTR_ERR(dev->dev);
  426. kfree(dev);
  427. goto out;
  428. }
  429. mutex_unlock(&minors_lock);
  430. init_waitqueue_head(&dev->wait);
  431. INIT_LIST_HEAD(&dev->list);
  432. dev->hid = hid;
  433. dev->minor = minor;
  434. dev->exist = 1;
  435. hid->hidraw = dev;
  436. out:
  437. return result;
  438. }
  439. EXPORT_SYMBOL_GPL(hidraw_connect);
  440. void hidraw_disconnect(struct hid_device *hid)
  441. {
  442. struct hidraw *hidraw = hid->hidraw;
  443. mutex_lock(&minors_lock);
  444. drop_ref(hidraw, 1);
  445. mutex_unlock(&minors_lock);
  446. }
  447. EXPORT_SYMBOL_GPL(hidraw_disconnect);
  448. int __init hidraw_init(void)
  449. {
  450. int result;
  451. dev_t dev_id;
  452. result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
  453. HIDRAW_MAX_DEVICES, "hidraw");
  454. hidraw_major = MAJOR(dev_id);
  455. if (result < 0) {
  456. pr_warn("can't get major number\n");
  457. goto out;
  458. }
  459. hidraw_class = class_create(THIS_MODULE, "hidraw");
  460. if (IS_ERR(hidraw_class)) {
  461. result = PTR_ERR(hidraw_class);
  462. goto error_cdev;
  463. }
  464. cdev_init(&hidraw_cdev, &hidraw_ops);
  465. result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
  466. if (result < 0)
  467. goto error_class;
  468. out:
  469. return result;
  470. error_class:
  471. class_destroy(hidraw_class);
  472. error_cdev:
  473. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  474. goto out;
  475. }
  476. void hidraw_exit(void)
  477. {
  478. dev_t dev_id = MKDEV(hidraw_major, 0);
  479. cdev_del(&hidraw_cdev);
  480. class_destroy(hidraw_class);
  481. unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
  482. }