hidraw.c 13 KB

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