uhid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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/device.h>
  13. #include <linux/fs.h>
  14. #include <linux/hid.h>
  15. #include <linux/input.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/poll.h>
  20. #include <linux/sched.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/uhid.h>
  23. #include <linux/wait.h>
  24. #include <linux/fb.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. bool lcd_is_on = true;
  47. static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
  48. {
  49. __u8 newhead;
  50. newhead = (uhid->head + 1) % UHID_BUFSIZE;
  51. if (newhead != uhid->tail) {
  52. uhid->outq[uhid->head] = ev;
  53. uhid->head = newhead;
  54. wake_up_interruptible(&uhid->waitq);
  55. } else {
  56. hid_warn(uhid->hid, "Output queue is full\n");
  57. kfree(ev);
  58. }
  59. }
  60. static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
  61. {
  62. unsigned long flags;
  63. struct uhid_event *ev;
  64. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  65. if (!ev)
  66. return -ENOMEM;
  67. ev->type = event;
  68. spin_lock_irqsave(&uhid->qlock, flags);
  69. uhid_queue(uhid, ev);
  70. spin_unlock_irqrestore(&uhid->qlock, flags);
  71. return 0;
  72. }
  73. static int uhid_hid_start(struct hid_device *hid)
  74. {
  75. struct uhid_device *uhid = hid->driver_data;
  76. return uhid_queue_event(uhid, UHID_START);
  77. }
  78. static void uhid_hid_stop(struct hid_device *hid)
  79. {
  80. struct uhid_device *uhid = hid->driver_data;
  81. hid->claimed = 0;
  82. uhid_queue_event(uhid, UHID_STOP);
  83. }
  84. static int uhid_hid_open(struct hid_device *hid)
  85. {
  86. struct uhid_device *uhid = hid->driver_data;
  87. return uhid_queue_event(uhid, UHID_OPEN);
  88. }
  89. static void uhid_hid_close(struct hid_device *hid)
  90. {
  91. struct uhid_device *uhid = hid->driver_data;
  92. uhid_queue_event(uhid, UHID_CLOSE);
  93. }
  94. static int uhid_hid_input(struct input_dev *input, unsigned int type,
  95. unsigned int code, int value)
  96. {
  97. struct hid_device *hid = input_get_drvdata(input);
  98. struct uhid_device *uhid = hid->driver_data;
  99. unsigned long flags;
  100. struct uhid_event *ev;
  101. struct hid_field *field;
  102. struct hid_report *report;
  103. int offset;
  104. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  105. if (!ev)
  106. return -ENOMEM;
  107. switch (type) {
  108. case EV_LED:
  109. if(!lcd_is_on){
  110. dbg_hid("uhid_hid_input lcd is off, don't report LED event\n");
  111. kfree(ev);
  112. return -1;
  113. }
  114. offset = hidinput_find_field(hid, type, code, &field);
  115. if (offset == -1) {
  116. hid_warn(input, "event field not found\n");
  117. kfree(ev);
  118. return -1;
  119. }
  120. hid_set_field(field, offset, value);
  121. report = field->report;
  122. ev->type = UHID_OUTPUT;
  123. ev->u.output.rtype = UHID_OUTPUT_REPORT;
  124. hid_output_report(report, ev->u.output.data);
  125. ev->u.output.size = ((report->size - 1) >> 3) + 1 +
  126. (report->id > 0);
  127. break;
  128. default:
  129. ev->type = UHID_OUTPUT_EV;
  130. ev->u.output_ev.type = type;
  131. ev->u.output_ev.code = code;
  132. ev->u.output_ev.value = value;
  133. }
  134. spin_lock_irqsave(&uhid->qlock, flags);
  135. uhid_queue(uhid, ev);
  136. spin_unlock_irqrestore(&uhid->qlock, flags);
  137. return 0;
  138. }
  139. static int fb_state_change(struct notifier_block *nb,
  140. unsigned long val, void *data)
  141. {
  142. struct fb_event *evdata = data;
  143. unsigned int blank;
  144. dbg_hid("fb_state_change");
  145. if (val != FB_EVENT_BLANK)
  146. return 0;
  147. blank = *(int *)evdata->data;
  148. switch (blank) {
  149. case FB_BLANK_POWERDOWN:
  150. lcd_is_on = false;
  151. break;
  152. case FB_BLANK_UNBLANK:
  153. lcd_is_on = true;
  154. break;
  155. default:
  156. break;
  157. }
  158. return NOTIFY_OK;
  159. }
  160. static struct notifier_block fb_block = {
  161. .notifier_call = fb_state_change,
  162. };
  163. static int uhid_hid_parse(struct hid_device *hid)
  164. {
  165. struct uhid_device *uhid = hid->driver_data;
  166. return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
  167. }
  168. static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
  169. __u8 *buf, size_t count, unsigned char rtype)
  170. {
  171. struct uhid_device *uhid = hid->driver_data;
  172. __u8 report_type;
  173. struct uhid_event *ev;
  174. unsigned long flags;
  175. int ret;
  176. size_t uninitialized_var(len);
  177. struct uhid_feature_answer_req *req;
  178. if (!uhid->running)
  179. return -EIO;
  180. switch (rtype) {
  181. case HID_FEATURE_REPORT:
  182. report_type = UHID_FEATURE_REPORT;
  183. break;
  184. case HID_OUTPUT_REPORT:
  185. report_type = UHID_OUTPUT_REPORT;
  186. break;
  187. case HID_INPUT_REPORT:
  188. report_type = UHID_INPUT_REPORT;
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. ret = mutex_lock_interruptible(&uhid->report_lock);
  194. if (ret)
  195. return ret;
  196. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  197. if (!ev) {
  198. ret = -ENOMEM;
  199. goto unlock;
  200. }
  201. spin_lock_irqsave(&uhid->qlock, flags);
  202. ev->type = UHID_FEATURE;
  203. ev->u.feature.id = atomic_inc_return(&uhid->report_id);
  204. ev->u.feature.rnum = rnum;
  205. ev->u.feature.rtype = report_type;
  206. atomic_set(&uhid->report_done, 0);
  207. uhid_queue(uhid, ev);
  208. spin_unlock_irqrestore(&uhid->qlock, flags);
  209. ret = wait_event_interruptible_timeout(uhid->report_wait,
  210. atomic_read(&uhid->report_done), 5 * HZ);
  211. /*
  212. * Make sure "uhid->running" is cleared on shutdown before
  213. * "uhid->report_done" is set.
  214. */
  215. smp_rmb();
  216. if (!ret || !uhid->running) {
  217. ret = -EIO;
  218. } else if (ret < 0) {
  219. ret = -ERESTARTSYS;
  220. } else {
  221. spin_lock_irqsave(&uhid->qlock, flags);
  222. req = &uhid->report_buf.u.feature_answer;
  223. if (req->err) {
  224. ret = -EIO;
  225. } else {
  226. ret = 0;
  227. len = min(count,
  228. min_t(size_t, req->size, UHID_DATA_MAX));
  229. memcpy(buf, req->data, len);
  230. }
  231. spin_unlock_irqrestore(&uhid->qlock, flags);
  232. }
  233. atomic_set(&uhid->report_done, 1);
  234. unlock:
  235. mutex_unlock(&uhid->report_lock);
  236. return ret ? ret : len;
  237. }
  238. static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
  239. unsigned char report_type)
  240. {
  241. struct uhid_device *uhid = hid->driver_data;
  242. __u8 rtype;
  243. unsigned long flags;
  244. struct uhid_event *ev;
  245. switch (report_type) {
  246. case HID_FEATURE_REPORT:
  247. rtype = UHID_FEATURE_REPORT;
  248. break;
  249. case HID_OUTPUT_REPORT:
  250. rtype = UHID_OUTPUT_REPORT;
  251. break;
  252. default:
  253. return -EINVAL;
  254. }
  255. if (count < 1 || count > UHID_DATA_MAX)
  256. return -EINVAL;
  257. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  258. if (!ev)
  259. return -ENOMEM;
  260. ev->type = UHID_OUTPUT;
  261. ev->u.output.size = count;
  262. ev->u.output.rtype = rtype;
  263. memcpy(ev->u.output.data, buf, count);
  264. spin_lock_irqsave(&uhid->qlock, flags);
  265. uhid_queue(uhid, ev);
  266. spin_unlock_irqrestore(&uhid->qlock, flags);
  267. return count;
  268. }
  269. static struct hid_ll_driver uhid_hid_driver = {
  270. .start = uhid_hid_start,
  271. .stop = uhid_hid_stop,
  272. .open = uhid_hid_open,
  273. .close = uhid_hid_close,
  274. .hidinput_input_event = uhid_hid_input,
  275. .parse = uhid_hid_parse,
  276. };
  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. if (copy_from_user(&uhid->input_buf, buffer, len)) {
  455. ret = -EFAULT;
  456. goto unlock;
  457. }
  458. switch (uhid->input_buf.type) {
  459. case UHID_CREATE:
  460. ret = uhid_dev_create(uhid, &uhid->input_buf);
  461. break;
  462. case UHID_DESTROY:
  463. ret = uhid_dev_destroy(uhid);
  464. break;
  465. case UHID_INPUT:
  466. ret = uhid_dev_input(uhid, &uhid->input_buf);
  467. break;
  468. case UHID_FEATURE_ANSWER:
  469. ret = uhid_dev_feature_answer(uhid, &uhid->input_buf);
  470. break;
  471. default:
  472. ret = -EOPNOTSUPP;
  473. }
  474. unlock:
  475. mutex_unlock(&uhid->devlock);
  476. /* return "count" not "len" to not confuse the caller */
  477. return ret ? ret : count;
  478. }
  479. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  480. {
  481. struct uhid_device *uhid = file->private_data;
  482. poll_wait(file, &uhid->waitq, wait);
  483. if (uhid->head != uhid->tail)
  484. return POLLIN | POLLRDNORM;
  485. return 0;
  486. }
  487. static const struct file_operations uhid_fops = {
  488. .owner = THIS_MODULE,
  489. .open = uhid_char_open,
  490. .release = uhid_char_release,
  491. .read = uhid_char_read,
  492. .write = uhid_char_write,
  493. .poll = uhid_char_poll,
  494. .llseek = no_llseek,
  495. };
  496. static struct miscdevice uhid_misc = {
  497. .fops = &uhid_fops,
  498. .minor = MISC_DYNAMIC_MINOR,
  499. .name = UHID_NAME,
  500. };
  501. static int __init uhid_init(void)
  502. {
  503. fb_register_client(&fb_block);
  504. return misc_register(&uhid_misc);
  505. }
  506. static void __exit uhid_exit(void)
  507. {
  508. fb_unregister_client(&fb_block);
  509. misc_deregister(&uhid_misc);
  510. }
  511. module_init(uhid_init);
  512. module_exit(uhid_exit);
  513. MODULE_LICENSE("GPL");
  514. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  515. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");