hidraw.c 12 KB

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