uhid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * User-space I/O driver support for HID subsystem
  3. * Copyright (c) 2012 David Herrmann
  4. */
  5. /*
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/compat.h>
  13. #include <linux/device.h>
  14. #include <linux/fs.h>
  15. #include <linux/hid.h>
  16. #include <linux/input.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/poll.h>
  21. #include <linux/sched.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/uhid.h>
  24. #include <linux/wait.h>
  25. #define UHID_NAME "uhid"
  26. #define UHID_BUFSIZE 32
  27. struct uhid_device {
  28. struct mutex devlock;
  29. bool running;
  30. __u8 *rd_data;
  31. uint rd_size;
  32. struct hid_device *hid;
  33. struct uhid_event input_buf;
  34. wait_queue_head_t waitq;
  35. spinlock_t qlock;
  36. __u8 head;
  37. __u8 tail;
  38. struct uhid_event *outq[UHID_BUFSIZE];
  39. struct mutex report_lock;
  40. wait_queue_head_t report_wait;
  41. atomic_t report_done;
  42. atomic_t report_id;
  43. struct uhid_event report_buf;
  44. };
  45. static struct miscdevice uhid_misc;
  46. static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
  47. {
  48. __u8 newhead;
  49. newhead = (uhid->head + 1) % UHID_BUFSIZE;
  50. if (newhead != uhid->tail) {
  51. uhid->outq[uhid->head] = ev;
  52. uhid->head = newhead;
  53. wake_up_interruptible(&uhid->waitq);
  54. } else {
  55. hid_warn(uhid->hid, "Output queue is full\n");
  56. kfree(ev);
  57. }
  58. }
  59. static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
  60. {
  61. unsigned long flags;
  62. struct uhid_event *ev;
  63. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  64. if (!ev)
  65. return -ENOMEM;
  66. ev->type = event;
  67. spin_lock_irqsave(&uhid->qlock, flags);
  68. uhid_queue(uhid, ev);
  69. spin_unlock_irqrestore(&uhid->qlock, flags);
  70. return 0;
  71. }
  72. static int uhid_hid_start(struct hid_device *hid)
  73. {
  74. struct uhid_device *uhid = hid->driver_data;
  75. return uhid_queue_event(uhid, UHID_START);
  76. }
  77. static void uhid_hid_stop(struct hid_device *hid)
  78. {
  79. struct uhid_device *uhid = hid->driver_data;
  80. hid->claimed = 0;
  81. uhid_queue_event(uhid, UHID_STOP);
  82. }
  83. static int uhid_hid_open(struct hid_device *hid)
  84. {
  85. struct uhid_device *uhid = hid->driver_data;
  86. return uhid_queue_event(uhid, UHID_OPEN);
  87. }
  88. static void uhid_hid_close(struct hid_device *hid)
  89. {
  90. struct uhid_device *uhid = hid->driver_data;
  91. uhid_queue_event(uhid, UHID_CLOSE);
  92. }
  93. static int uhid_hid_parse(struct hid_device *hid)
  94. {
  95. struct uhid_device *uhid = hid->driver_data;
  96. return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
  97. }
  98. static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
  99. __u8 *buf, size_t count, unsigned char rtype)
  100. {
  101. struct uhid_device *uhid = hid->driver_data;
  102. __u8 report_type;
  103. struct uhid_event *ev;
  104. unsigned long flags;
  105. int ret;
  106. size_t uninitialized_var(len);
  107. struct uhid_feature_answer_req *req;
  108. if (!uhid->running)
  109. return -EIO;
  110. switch (rtype) {
  111. case HID_FEATURE_REPORT:
  112. report_type = UHID_FEATURE_REPORT;
  113. break;
  114. case HID_OUTPUT_REPORT:
  115. report_type = UHID_OUTPUT_REPORT;
  116. break;
  117. case HID_INPUT_REPORT:
  118. report_type = UHID_INPUT_REPORT;
  119. break;
  120. default:
  121. return -EINVAL;
  122. }
  123. ret = mutex_lock_interruptible(&uhid->report_lock);
  124. if (ret)
  125. return ret;
  126. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  127. if (!ev) {
  128. ret = -ENOMEM;
  129. goto unlock;
  130. }
  131. spin_lock_irqsave(&uhid->qlock, flags);
  132. ev->type = UHID_FEATURE;
  133. ev->u.feature.id = atomic_inc_return(&uhid->report_id);
  134. ev->u.feature.rnum = rnum;
  135. ev->u.feature.rtype = report_type;
  136. atomic_set(&uhid->report_done, 0);
  137. uhid_queue(uhid, ev);
  138. spin_unlock_irqrestore(&uhid->qlock, flags);
  139. ret = wait_event_interruptible_timeout(uhid->report_wait,
  140. atomic_read(&uhid->report_done), 5 * HZ);
  141. /*
  142. * Make sure "uhid->running" is cleared on shutdown before
  143. * "uhid->report_done" is set.
  144. */
  145. smp_rmb();
  146. if (!ret || !uhid->running) {
  147. ret = -EIO;
  148. } else if (ret < 0) {
  149. ret = -ERESTARTSYS;
  150. } else {
  151. spin_lock_irqsave(&uhid->qlock, flags);
  152. req = &uhid->report_buf.u.feature_answer;
  153. if (req->err) {
  154. ret = -EIO;
  155. } else {
  156. ret = 0;
  157. len = min(count,
  158. min_t(size_t, req->size, UHID_DATA_MAX));
  159. memcpy(buf, req->data, len);
  160. }
  161. spin_unlock_irqrestore(&uhid->qlock, flags);
  162. }
  163. atomic_set(&uhid->report_done, 1);
  164. unlock:
  165. mutex_unlock(&uhid->report_lock);
  166. return ret ? ret : len;
  167. }
  168. static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
  169. unsigned char report_type)
  170. {
  171. struct uhid_device *uhid = hid->driver_data;
  172. __u8 rtype;
  173. unsigned long flags;
  174. struct uhid_event *ev;
  175. switch (report_type) {
  176. case HID_FEATURE_REPORT:
  177. rtype = UHID_FEATURE_REPORT;
  178. break;
  179. case HID_OUTPUT_REPORT:
  180. rtype = UHID_OUTPUT_REPORT;
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. if (count < 1 || count > UHID_DATA_MAX)
  186. return -EINVAL;
  187. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  188. if (!ev)
  189. return -ENOMEM;
  190. ev->type = UHID_OUTPUT;
  191. ev->u.output.size = count;
  192. ev->u.output.rtype = rtype;
  193. memcpy(ev->u.output.data, buf, count);
  194. spin_lock_irqsave(&uhid->qlock, flags);
  195. uhid_queue(uhid, ev);
  196. spin_unlock_irqrestore(&uhid->qlock, flags);
  197. return count;
  198. }
  199. static struct hid_ll_driver uhid_hid_driver = {
  200. .start = uhid_hid_start,
  201. .stop = uhid_hid_stop,
  202. .open = uhid_hid_open,
  203. .close = uhid_hid_close,
  204. .parse = uhid_hid_parse,
  205. };
  206. #ifdef CONFIG_COMPAT
  207. /* Apparently we haven't stepped on these rakes enough times yet. */
  208. struct uhid_create_req_compat {
  209. __u8 name[128];
  210. __u8 phys[64];
  211. __u8 uniq[64];
  212. compat_uptr_t rd_data;
  213. __u16 rd_size;
  214. __u16 bus;
  215. __u32 vendor;
  216. __u32 product;
  217. __u32 version;
  218. __u32 country;
  219. } __attribute__((__packed__));
  220. static int uhid_event_from_user(const char __user *buffer, size_t len,
  221. struct uhid_event *event)
  222. {
  223. if (is_compat_task()) {
  224. u32 type;
  225. if (get_user(type, buffer))
  226. return -EFAULT;
  227. if (type == UHID_CREATE) {
  228. /*
  229. * This is our messed up request with compat pointer.
  230. * It is largish (more than 256 bytes) so we better
  231. * allocate it from the heap.
  232. */
  233. struct uhid_create_req_compat *compat;
  234. compat = kmalloc(sizeof(*compat), GFP_KERNEL);
  235. if (!compat)
  236. return -ENOMEM;
  237. buffer += sizeof(type);
  238. len -= sizeof(type);
  239. if (copy_from_user(compat, buffer,
  240. min(len, sizeof(*compat)))) {
  241. kfree(compat);
  242. return -EFAULT;
  243. }
  244. /* Shuffle the data over to proper structure */
  245. event->type = type;
  246. memcpy(event->u.create.name, compat->name,
  247. sizeof(compat->name));
  248. memcpy(event->u.create.phys, compat->phys,
  249. sizeof(compat->phys));
  250. memcpy(event->u.create.uniq, compat->uniq,
  251. sizeof(compat->uniq));
  252. event->u.create.rd_data = compat_ptr(compat->rd_data);
  253. event->u.create.rd_size = compat->rd_size;
  254. event->u.create.bus = compat->bus;
  255. event->u.create.vendor = compat->vendor;
  256. event->u.create.product = compat->product;
  257. event->u.create.version = compat->version;
  258. event->u.create.country = compat->country;
  259. kfree(compat);
  260. return 0;
  261. }
  262. /* All others can be copied directly */
  263. }
  264. if (copy_from_user(event, buffer, min(len, sizeof(*event))))
  265. return -EFAULT;
  266. return 0;
  267. }
  268. #else
  269. static int uhid_event_from_user(const char __user *buffer, size_t len,
  270. struct uhid_event *event)
  271. {
  272. if (copy_from_user(event, buffer, min(len, sizeof(*event))))
  273. return -EFAULT;
  274. return 0;
  275. }
  276. #endif
  277. static int uhid_dev_create(struct uhid_device *uhid,
  278. const struct uhid_event *ev)
  279. {
  280. struct hid_device *hid;
  281. int ret;
  282. if (uhid->running)
  283. return -EALREADY;
  284. uhid->rd_size = ev->u.create.rd_size;
  285. if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
  286. return -EINVAL;
  287. uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
  288. if (!uhid->rd_data)
  289. return -ENOMEM;
  290. if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
  291. uhid->rd_size)) {
  292. ret = -EFAULT;
  293. goto err_free;
  294. }
  295. hid = hid_allocate_device();
  296. if (IS_ERR(hid)) {
  297. ret = PTR_ERR(hid);
  298. goto err_free;
  299. }
  300. strncpy(hid->name, ev->u.create.name, 127);
  301. hid->name[127] = 0;
  302. strncpy(hid->phys, ev->u.create.phys, 63);
  303. hid->phys[63] = 0;
  304. strncpy(hid->uniq, ev->u.create.uniq, 63);
  305. hid->uniq[63] = 0;
  306. hid->ll_driver = &uhid_hid_driver;
  307. hid->hid_get_raw_report = uhid_hid_get_raw;
  308. hid->hid_output_raw_report = uhid_hid_output_raw;
  309. hid->bus = ev->u.create.bus;
  310. hid->vendor = ev->u.create.vendor;
  311. hid->product = ev->u.create.product;
  312. hid->version = ev->u.create.version;
  313. hid->country = ev->u.create.country;
  314. hid->driver_data = uhid;
  315. hid->dev.parent = uhid_misc.this_device;
  316. uhid->hid = hid;
  317. uhid->running = true;
  318. ret = hid_add_device(hid);
  319. if (ret) {
  320. hid_err(hid, "Cannot register HID device\n");
  321. goto err_hid;
  322. }
  323. return 0;
  324. err_hid:
  325. hid_destroy_device(hid);
  326. uhid->hid = NULL;
  327. uhid->running = false;
  328. err_free:
  329. kfree(uhid->rd_data);
  330. return ret;
  331. }
  332. static int uhid_dev_destroy(struct uhid_device *uhid)
  333. {
  334. if (!uhid->running)
  335. return -EINVAL;
  336. /* clear "running" before setting "report_done" */
  337. uhid->running = false;
  338. smp_wmb();
  339. atomic_set(&uhid->report_done, 1);
  340. wake_up_interruptible(&uhid->report_wait);
  341. hid_destroy_device(uhid->hid);
  342. kfree(uhid->rd_data);
  343. return 0;
  344. }
  345. static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
  346. {
  347. if (!uhid->running)
  348. return -EINVAL;
  349. hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
  350. min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
  351. return 0;
  352. }
  353. static int uhid_dev_feature_answer(struct uhid_device *uhid,
  354. struct uhid_event *ev)
  355. {
  356. unsigned long flags;
  357. if (!uhid->running)
  358. return -EINVAL;
  359. spin_lock_irqsave(&uhid->qlock, flags);
  360. /* id for old report; drop it silently */
  361. if (atomic_read(&uhid->report_id) != ev->u.feature_answer.id)
  362. goto unlock;
  363. if (atomic_read(&uhid->report_done))
  364. goto unlock;
  365. memcpy(&uhid->report_buf, ev, sizeof(*ev));
  366. atomic_set(&uhid->report_done, 1);
  367. wake_up_interruptible(&uhid->report_wait);
  368. unlock:
  369. spin_unlock_irqrestore(&uhid->qlock, flags);
  370. return 0;
  371. }
  372. static int uhid_char_open(struct inode *inode, struct file *file)
  373. {
  374. struct uhid_device *uhid;
  375. uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
  376. if (!uhid)
  377. return -ENOMEM;
  378. mutex_init(&uhid->devlock);
  379. mutex_init(&uhid->report_lock);
  380. spin_lock_init(&uhid->qlock);
  381. init_waitqueue_head(&uhid->waitq);
  382. init_waitqueue_head(&uhid->report_wait);
  383. uhid->running = false;
  384. atomic_set(&uhid->report_done, 1);
  385. file->private_data = uhid;
  386. nonseekable_open(inode, file);
  387. return 0;
  388. }
  389. static int uhid_char_release(struct inode *inode, struct file *file)
  390. {
  391. struct uhid_device *uhid = file->private_data;
  392. unsigned int i;
  393. uhid_dev_destroy(uhid);
  394. for (i = 0; i < UHID_BUFSIZE; ++i)
  395. kfree(uhid->outq[i]);
  396. kfree(uhid);
  397. return 0;
  398. }
  399. static ssize_t uhid_char_read(struct file *file, char __user *buffer,
  400. size_t count, loff_t *ppos)
  401. {
  402. struct uhid_device *uhid = file->private_data;
  403. int ret;
  404. unsigned long flags;
  405. size_t len;
  406. /* they need at least the "type" member of uhid_event */
  407. if (count < sizeof(__u32))
  408. return -EINVAL;
  409. try_again:
  410. if (file->f_flags & O_NONBLOCK) {
  411. if (uhid->head == uhid->tail)
  412. return -EAGAIN;
  413. } else {
  414. ret = wait_event_interruptible(uhid->waitq,
  415. uhid->head != uhid->tail);
  416. if (ret)
  417. return ret;
  418. }
  419. ret = mutex_lock_interruptible(&uhid->devlock);
  420. if (ret)
  421. return ret;
  422. if (uhid->head == uhid->tail) {
  423. mutex_unlock(&uhid->devlock);
  424. goto try_again;
  425. } else {
  426. len = min(count, sizeof(**uhid->outq));
  427. if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
  428. ret = -EFAULT;
  429. } else {
  430. kfree(uhid->outq[uhid->tail]);
  431. uhid->outq[uhid->tail] = NULL;
  432. spin_lock_irqsave(&uhid->qlock, flags);
  433. uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
  434. spin_unlock_irqrestore(&uhid->qlock, flags);
  435. }
  436. }
  437. mutex_unlock(&uhid->devlock);
  438. return ret ? ret : len;
  439. }
  440. static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
  441. size_t count, loff_t *ppos)
  442. {
  443. struct uhid_device *uhid = file->private_data;
  444. int ret;
  445. size_t len;
  446. /* we need at least the "type" member of uhid_event */
  447. if (count < sizeof(__u32))
  448. return -EINVAL;
  449. ret = mutex_lock_interruptible(&uhid->devlock);
  450. if (ret)
  451. return ret;
  452. memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
  453. len = min(count, sizeof(uhid->input_buf));
  454. ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
  455. if (ret)
  456. goto unlock;
  457. switch (uhid->input_buf.type) {
  458. case UHID_CREATE:
  459. ret = uhid_dev_create(uhid, &uhid->input_buf);
  460. break;
  461. case UHID_DESTROY:
  462. ret = uhid_dev_destroy(uhid);
  463. break;
  464. case UHID_INPUT:
  465. ret = uhid_dev_input(uhid, &uhid->input_buf);
  466. break;
  467. case UHID_FEATURE_ANSWER:
  468. ret = uhid_dev_feature_answer(uhid, &uhid->input_buf);
  469. break;
  470. default:
  471. ret = -EOPNOTSUPP;
  472. }
  473. unlock:
  474. mutex_unlock(&uhid->devlock);
  475. /* return "count" not "len" to not confuse the caller */
  476. return ret ? ret : count;
  477. }
  478. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  479. {
  480. struct uhid_device *uhid = file->private_data;
  481. poll_wait(file, &uhid->waitq, wait);
  482. if (uhid->head != uhid->tail)
  483. return POLLIN | POLLRDNORM;
  484. return 0;
  485. }
  486. static const struct file_operations uhid_fops = {
  487. .owner = THIS_MODULE,
  488. .open = uhid_char_open,
  489. .release = uhid_char_release,
  490. .read = uhid_char_read,
  491. .write = uhid_char_write,
  492. .poll = uhid_char_poll,
  493. .llseek = no_llseek,
  494. };
  495. static struct miscdevice uhid_misc = {
  496. .fops = &uhid_fops,
  497. .minor = MISC_DYNAMIC_MINOR,
  498. .name = UHID_NAME,
  499. };
  500. static int __init uhid_init(void)
  501. {
  502. return misc_register(&uhid_misc);
  503. }
  504. static void __exit uhid_exit(void)
  505. {
  506. misc_deregister(&uhid_misc);
  507. }
  508. module_init(uhid_init);
  509. module_exit(uhid_exit);
  510. MODULE_LICENSE("GPL");
  511. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  512. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
  513. MODULE_ALIAS("devname:" UHID_NAME);