hidraw.c 13 KB

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