hidraw.c 12 KB

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