uhid.c 14 KB

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