uhid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. /* blocking GET_REPORT support; state changes protected by qlock */
  40. struct mutex report_lock;
  41. wait_queue_head_t report_wait;
  42. bool report_running;
  43. u32 report_id;
  44. u32 report_type;
  45. struct uhid_event report_buf;
  46. };
  47. static struct miscdevice uhid_misc;
  48. static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
  49. {
  50. __u8 newhead;
  51. newhead = (uhid->head + 1) % UHID_BUFSIZE;
  52. if (newhead != uhid->tail) {
  53. uhid->outq[uhid->head] = ev;
  54. uhid->head = newhead;
  55. wake_up_interruptible(&uhid->waitq);
  56. } else {
  57. hid_warn(uhid->hid, "Output queue is full\n");
  58. kfree(ev);
  59. }
  60. }
  61. static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
  62. {
  63. unsigned long flags;
  64. struct uhid_event *ev;
  65. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  66. if (!ev)
  67. return -ENOMEM;
  68. ev->type = event;
  69. spin_lock_irqsave(&uhid->qlock, flags);
  70. uhid_queue(uhid, ev);
  71. spin_unlock_irqrestore(&uhid->qlock, flags);
  72. return 0;
  73. }
  74. static int uhid_hid_start(struct hid_device *hid)
  75. {
  76. struct uhid_device *uhid = hid->driver_data;
  77. struct uhid_event *ev;
  78. unsigned long flags;
  79. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  80. if (!ev)
  81. return -ENOMEM;
  82. ev->type = UHID_START;
  83. if (hid->report_enum[HID_FEATURE_REPORT].numbered)
  84. ev->u.start.dev_flags |= UHID_DEV_NUMBERED_FEATURE_REPORTS;
  85. if (hid->report_enum[HID_OUTPUT_REPORT].numbered)
  86. ev->u.start.dev_flags |= UHID_DEV_NUMBERED_OUTPUT_REPORTS;
  87. if (hid->report_enum[HID_INPUT_REPORT].numbered)
  88. ev->u.start.dev_flags |= UHID_DEV_NUMBERED_INPUT_REPORTS;
  89. spin_lock_irqsave(&uhid->qlock, flags);
  90. uhid_queue(uhid, ev);
  91. spin_unlock_irqrestore(&uhid->qlock, flags);
  92. return 0;
  93. }
  94. static void uhid_hid_stop(struct hid_device *hid)
  95. {
  96. struct uhid_device *uhid = hid->driver_data;
  97. hid->claimed = 0;
  98. uhid_queue_event(uhid, UHID_STOP);
  99. }
  100. static int uhid_hid_open(struct hid_device *hid)
  101. {
  102. struct uhid_device *uhid = hid->driver_data;
  103. return uhid_queue_event(uhid, UHID_OPEN);
  104. }
  105. static void uhid_hid_close(struct hid_device *hid)
  106. {
  107. struct uhid_device *uhid = hid->driver_data;
  108. uhid_queue_event(uhid, UHID_CLOSE);
  109. }
  110. static int uhid_hid_parse(struct hid_device *hid)
  111. {
  112. struct uhid_device *uhid = hid->driver_data;
  113. return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
  114. }
  115. /* must be called with report_lock held */
  116. static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
  117. struct uhid_event *ev,
  118. __u32 *report_id)
  119. {
  120. unsigned long flags;
  121. int ret;
  122. spin_lock_irqsave(&uhid->qlock, flags);
  123. *report_id = ++uhid->report_id;
  124. uhid->report_type = ev->type + 1;
  125. uhid->report_running = true;
  126. uhid_queue(uhid, ev);
  127. spin_unlock_irqrestore(&uhid->qlock, flags);
  128. ret = wait_event_interruptible_timeout(uhid->report_wait,
  129. !uhid->report_running || !uhid->running,
  130. 5 * HZ);
  131. if (!ret || !uhid->running || uhid->report_running)
  132. ret = -EIO;
  133. else if (ret < 0)
  134. ret = -ERESTARTSYS;
  135. else
  136. ret = 0;
  137. uhid->report_running = false;
  138. return ret;
  139. }
  140. static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
  141. const struct uhid_event *ev)
  142. {
  143. unsigned long flags;
  144. spin_lock_irqsave(&uhid->qlock, flags);
  145. /* id for old report; drop it silently */
  146. if (uhid->report_type != ev->type || uhid->report_id != id)
  147. goto unlock;
  148. if (!uhid->report_running)
  149. goto unlock;
  150. memcpy(&uhid->report_buf, ev, sizeof(*ev));
  151. uhid->report_running = false;
  152. wake_up_interruptible(&uhid->report_wait);
  153. unlock:
  154. spin_unlock_irqrestore(&uhid->qlock, flags);
  155. }
  156. static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
  157. u8 *buf, size_t count, u8 rtype)
  158. {
  159. struct uhid_device *uhid = hid->driver_data;
  160. struct uhid_get_report_reply_req *req;
  161. struct uhid_event *ev;
  162. int ret;
  163. if (!uhid->running)
  164. return -EIO;
  165. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  166. if (!ev)
  167. return -ENOMEM;
  168. ev->type = UHID_GET_REPORT;
  169. ev->u.get_report.rnum = rnum;
  170. ev->u.get_report.rtype = rtype;
  171. ret = mutex_lock_interruptible(&uhid->report_lock);
  172. if (ret) {
  173. kfree(ev);
  174. return ret;
  175. }
  176. /* this _always_ takes ownership of @ev */
  177. ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
  178. if (ret)
  179. goto unlock;
  180. req = &uhid->report_buf.u.get_report_reply;
  181. if (req->err) {
  182. ret = -EIO;
  183. } else {
  184. ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
  185. memcpy(buf, req->data, ret);
  186. }
  187. unlock:
  188. mutex_unlock(&uhid->report_lock);
  189. return ret;
  190. }
  191. static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
  192. const u8 *buf, size_t count, u8 rtype)
  193. {
  194. struct uhid_device *uhid = hid->driver_data;
  195. struct uhid_event *ev;
  196. int ret;
  197. if (!uhid->running || count > UHID_DATA_MAX)
  198. return -EIO;
  199. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  200. if (!ev)
  201. return -ENOMEM;
  202. ev->type = UHID_SET_REPORT;
  203. ev->u.set_report.rnum = rnum;
  204. ev->u.set_report.rtype = rtype;
  205. ev->u.set_report.size = count;
  206. memcpy(ev->u.set_report.data, buf, count);
  207. ret = mutex_lock_interruptible(&uhid->report_lock);
  208. if (ret) {
  209. kfree(ev);
  210. return ret;
  211. }
  212. /* this _always_ takes ownership of @ev */
  213. ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
  214. if (ret)
  215. goto unlock;
  216. if (uhid->report_buf.u.set_report_reply.err)
  217. ret = -EIO;
  218. else
  219. ret = count;
  220. unlock:
  221. mutex_unlock(&uhid->report_lock);
  222. return ret;
  223. }
  224. static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  225. __u8 *buf, size_t len, unsigned char rtype,
  226. int reqtype)
  227. {
  228. u8 u_rtype;
  229. switch (rtype) {
  230. case HID_FEATURE_REPORT:
  231. u_rtype = UHID_FEATURE_REPORT;
  232. break;
  233. case HID_OUTPUT_REPORT:
  234. u_rtype = UHID_OUTPUT_REPORT;
  235. break;
  236. case HID_INPUT_REPORT:
  237. u_rtype = UHID_INPUT_REPORT;
  238. break;
  239. default:
  240. return -EINVAL;
  241. }
  242. switch (reqtype) {
  243. case HID_REQ_GET_REPORT:
  244. return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
  245. case HID_REQ_SET_REPORT:
  246. return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
  247. default:
  248. return -EIO;
  249. }
  250. }
  251. static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
  252. unsigned char report_type)
  253. {
  254. struct uhid_device *uhid = hid->driver_data;
  255. __u8 rtype;
  256. unsigned long flags;
  257. struct uhid_event *ev;
  258. switch (report_type) {
  259. case HID_FEATURE_REPORT:
  260. rtype = UHID_FEATURE_REPORT;
  261. break;
  262. case HID_OUTPUT_REPORT:
  263. rtype = UHID_OUTPUT_REPORT;
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. if (count < 1 || count > UHID_DATA_MAX)
  269. return -EINVAL;
  270. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  271. if (!ev)
  272. return -ENOMEM;
  273. ev->type = UHID_OUTPUT;
  274. ev->u.output.size = count;
  275. ev->u.output.rtype = rtype;
  276. memcpy(ev->u.output.data, buf, count);
  277. spin_lock_irqsave(&uhid->qlock, flags);
  278. uhid_queue(uhid, ev);
  279. spin_unlock_irqrestore(&uhid->qlock, flags);
  280. return count;
  281. }
  282. static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
  283. size_t count)
  284. {
  285. return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
  286. }
  287. static struct hid_ll_driver uhid_hid_driver = {
  288. .start = uhid_hid_start,
  289. .stop = uhid_hid_stop,
  290. .open = uhid_hid_open,
  291. .close = uhid_hid_close,
  292. .parse = uhid_hid_parse,
  293. .raw_request = uhid_hid_raw_request,
  294. .output_report = uhid_hid_output_report,
  295. };
  296. #ifdef CONFIG_COMPAT
  297. /* Apparently we haven't stepped on these rakes enough times yet. */
  298. struct uhid_create_req_compat {
  299. __u8 name[128];
  300. __u8 phys[64];
  301. __u8 uniq[64];
  302. compat_uptr_t rd_data;
  303. __u16 rd_size;
  304. __u16 bus;
  305. __u32 vendor;
  306. __u32 product;
  307. __u32 version;
  308. __u32 country;
  309. } __attribute__((__packed__));
  310. static int uhid_event_from_user(const char __user *buffer, size_t len,
  311. struct uhid_event *event)
  312. {
  313. if (is_compat_task()) {
  314. u32 type;
  315. if (get_user(type, buffer))
  316. return -EFAULT;
  317. if (type == UHID_CREATE) {
  318. /*
  319. * This is our messed up request with compat pointer.
  320. * It is largish (more than 256 bytes) so we better
  321. * allocate it from the heap.
  322. */
  323. struct uhid_create_req_compat *compat;
  324. compat = kzalloc(sizeof(*compat), GFP_KERNEL);
  325. if (!compat)
  326. return -ENOMEM;
  327. buffer += sizeof(type);
  328. len -= sizeof(type);
  329. if (copy_from_user(compat, buffer,
  330. min(len, sizeof(*compat)))) {
  331. kfree(compat);
  332. return -EFAULT;
  333. }
  334. /* Shuffle the data over to proper structure */
  335. event->type = type;
  336. memcpy(event->u.create.name, compat->name,
  337. sizeof(compat->name));
  338. memcpy(event->u.create.phys, compat->phys,
  339. sizeof(compat->phys));
  340. memcpy(event->u.create.uniq, compat->uniq,
  341. sizeof(compat->uniq));
  342. event->u.create.rd_data = compat_ptr(compat->rd_data);
  343. event->u.create.rd_size = compat->rd_size;
  344. event->u.create.bus = compat->bus;
  345. event->u.create.vendor = compat->vendor;
  346. event->u.create.product = compat->product;
  347. event->u.create.version = compat->version;
  348. event->u.create.country = compat->country;
  349. kfree(compat);
  350. return 0;
  351. }
  352. /* All others can be copied directly */
  353. }
  354. if (copy_from_user(event, buffer, min(len, sizeof(*event))))
  355. return -EFAULT;
  356. return 0;
  357. }
  358. #else
  359. static int uhid_event_from_user(const char __user *buffer, size_t len,
  360. struct uhid_event *event)
  361. {
  362. if (copy_from_user(event, buffer, min(len, sizeof(*event))))
  363. return -EFAULT;
  364. return 0;
  365. }
  366. #endif
  367. static int uhid_dev_create2(struct uhid_device *uhid,
  368. const struct uhid_event *ev)
  369. {
  370. struct hid_device *hid;
  371. size_t rd_size, len;
  372. void *rd_data;
  373. int ret;
  374. if (uhid->running)
  375. return -EALREADY;
  376. rd_size = ev->u.create2.rd_size;
  377. if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
  378. return -EINVAL;
  379. rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
  380. if (!rd_data)
  381. return -ENOMEM;
  382. uhid->rd_size = rd_size;
  383. uhid->rd_data = rd_data;
  384. hid = hid_allocate_device();
  385. if (IS_ERR(hid)) {
  386. ret = PTR_ERR(hid);
  387. goto err_free;
  388. }
  389. len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
  390. strncpy(hid->name, ev->u.create2.name, len);
  391. len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
  392. strncpy(hid->phys, ev->u.create2.phys, len);
  393. len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
  394. strncpy(hid->uniq, ev->u.create2.uniq, len);
  395. hid->ll_driver = &uhid_hid_driver;
  396. hid->bus = ev->u.create2.bus;
  397. hid->vendor = ev->u.create2.vendor;
  398. hid->product = ev->u.create2.product;
  399. hid->version = ev->u.create2.version;
  400. hid->country = ev->u.create2.country;
  401. hid->driver_data = uhid;
  402. hid->dev.parent = uhid_misc.this_device;
  403. uhid->hid = hid;
  404. uhid->running = true;
  405. ret = hid_add_device(hid);
  406. if (ret) {
  407. hid_err(hid, "Cannot register HID device\n");
  408. goto err_hid;
  409. }
  410. return 0;
  411. err_hid:
  412. hid_destroy_device(hid);
  413. uhid->hid = NULL;
  414. uhid->running = false;
  415. err_free:
  416. kfree(uhid->rd_data);
  417. uhid->rd_data = NULL;
  418. uhid->rd_size = 0;
  419. return ret;
  420. }
  421. static int uhid_dev_create(struct uhid_device *uhid,
  422. struct uhid_event *ev)
  423. {
  424. struct uhid_create_req orig;
  425. orig = ev->u.create;
  426. if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
  427. return -EINVAL;
  428. if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
  429. return -EFAULT;
  430. memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
  431. memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
  432. memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
  433. ev->u.create2.rd_size = orig.rd_size;
  434. ev->u.create2.bus = orig.bus;
  435. ev->u.create2.vendor = orig.vendor;
  436. ev->u.create2.product = orig.product;
  437. ev->u.create2.version = orig.version;
  438. ev->u.create2.country = orig.country;
  439. return uhid_dev_create2(uhid, ev);
  440. }
  441. static int uhid_dev_destroy(struct uhid_device *uhid)
  442. {
  443. if (!uhid->running)
  444. return -EINVAL;
  445. uhid->running = false;
  446. wake_up_interruptible(&uhid->report_wait);
  447. hid_destroy_device(uhid->hid);
  448. kfree(uhid->rd_data);
  449. return 0;
  450. }
  451. static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
  452. {
  453. if (!uhid->running)
  454. return -EINVAL;
  455. hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
  456. min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
  457. return 0;
  458. }
  459. static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
  460. {
  461. if (!uhid->running)
  462. return -EINVAL;
  463. hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
  464. min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
  465. return 0;
  466. }
  467. static int uhid_dev_get_report_reply(struct uhid_device *uhid,
  468. struct uhid_event *ev)
  469. {
  470. if (!uhid->running)
  471. return -EINVAL;
  472. uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
  473. return 0;
  474. }
  475. static int uhid_dev_set_report_reply(struct uhid_device *uhid,
  476. struct uhid_event *ev)
  477. {
  478. if (!uhid->running)
  479. return -EINVAL;
  480. uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
  481. return 0;
  482. }
  483. static int uhid_char_open(struct inode *inode, struct file *file)
  484. {
  485. struct uhid_device *uhid;
  486. uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
  487. if (!uhid)
  488. return -ENOMEM;
  489. mutex_init(&uhid->devlock);
  490. mutex_init(&uhid->report_lock);
  491. spin_lock_init(&uhid->qlock);
  492. init_waitqueue_head(&uhid->waitq);
  493. init_waitqueue_head(&uhid->report_wait);
  494. uhid->running = false;
  495. file->private_data = uhid;
  496. nonseekable_open(inode, file);
  497. return 0;
  498. }
  499. static int uhid_char_release(struct inode *inode, struct file *file)
  500. {
  501. struct uhid_device *uhid = file->private_data;
  502. unsigned int i;
  503. uhid_dev_destroy(uhid);
  504. for (i = 0; i < UHID_BUFSIZE; ++i)
  505. kfree(uhid->outq[i]);
  506. kfree(uhid);
  507. return 0;
  508. }
  509. static ssize_t uhid_char_read(struct file *file, char __user *buffer,
  510. size_t count, loff_t *ppos)
  511. {
  512. struct uhid_device *uhid = file->private_data;
  513. int ret;
  514. unsigned long flags;
  515. size_t len;
  516. /* they need at least the "type" member of uhid_event */
  517. if (count < sizeof(__u32))
  518. return -EINVAL;
  519. try_again:
  520. if (file->f_flags & O_NONBLOCK) {
  521. if (uhid->head == uhid->tail)
  522. return -EAGAIN;
  523. } else {
  524. ret = wait_event_interruptible(uhid->waitq,
  525. uhid->head != uhid->tail);
  526. if (ret)
  527. return ret;
  528. }
  529. ret = mutex_lock_interruptible(&uhid->devlock);
  530. if (ret)
  531. return ret;
  532. if (uhid->head == uhid->tail) {
  533. mutex_unlock(&uhid->devlock);
  534. goto try_again;
  535. } else {
  536. len = min(count, sizeof(**uhid->outq));
  537. if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
  538. ret = -EFAULT;
  539. } else {
  540. kfree(uhid->outq[uhid->tail]);
  541. uhid->outq[uhid->tail] = NULL;
  542. spin_lock_irqsave(&uhid->qlock, flags);
  543. uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
  544. spin_unlock_irqrestore(&uhid->qlock, flags);
  545. }
  546. }
  547. mutex_unlock(&uhid->devlock);
  548. return ret ? ret : len;
  549. }
  550. static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
  551. size_t count, loff_t *ppos)
  552. {
  553. struct uhid_device *uhid = file->private_data;
  554. int ret;
  555. size_t len;
  556. /* we need at least the "type" member of uhid_event */
  557. if (count < sizeof(__u32))
  558. return -EINVAL;
  559. ret = mutex_lock_interruptible(&uhid->devlock);
  560. if (ret)
  561. return ret;
  562. memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
  563. len = min(count, sizeof(uhid->input_buf));
  564. ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
  565. if (ret)
  566. goto unlock;
  567. switch (uhid->input_buf.type) {
  568. case UHID_CREATE:
  569. ret = uhid_dev_create(uhid, &uhid->input_buf);
  570. break;
  571. case UHID_CREATE2:
  572. ret = uhid_dev_create2(uhid, &uhid->input_buf);
  573. break;
  574. case UHID_DESTROY:
  575. ret = uhid_dev_destroy(uhid);
  576. break;
  577. case UHID_INPUT:
  578. ret = uhid_dev_input(uhid, &uhid->input_buf);
  579. break;
  580. case UHID_INPUT2:
  581. ret = uhid_dev_input2(uhid, &uhid->input_buf);
  582. break;
  583. case UHID_GET_REPORT_REPLY:
  584. ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
  585. break;
  586. case UHID_SET_REPORT_REPLY:
  587. ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
  588. break;
  589. default:
  590. ret = -EOPNOTSUPP;
  591. }
  592. unlock:
  593. mutex_unlock(&uhid->devlock);
  594. /* return "count" not "len" to not confuse the caller */
  595. return ret ? ret : count;
  596. }
  597. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  598. {
  599. struct uhid_device *uhid = file->private_data;
  600. poll_wait(file, &uhid->waitq, wait);
  601. if (uhid->head != uhid->tail)
  602. return POLLIN | POLLRDNORM;
  603. return 0;
  604. }
  605. static const struct file_operations uhid_fops = {
  606. .owner = THIS_MODULE,
  607. .open = uhid_char_open,
  608. .release = uhid_char_release,
  609. .read = uhid_char_read,
  610. .write = uhid_char_write,
  611. .poll = uhid_char_poll,
  612. .llseek = no_llseek,
  613. };
  614. static struct miscdevice uhid_misc = {
  615. .fops = &uhid_fops,
  616. .minor = UHID_MINOR,
  617. .name = UHID_NAME,
  618. };
  619. static int __init uhid_init(void)
  620. {
  621. return misc_register(&uhid_misc);
  622. }
  623. static void __exit uhid_exit(void)
  624. {
  625. misc_deregister(&uhid_misc);
  626. }
  627. module_init(uhid_init);
  628. module_exit(uhid_exit);
  629. MODULE_LICENSE("GPL");
  630. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  631. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
  632. MODULE_ALIAS_MISCDEV(UHID_MINOR);
  633. MODULE_ALIAS("devname:" UHID_NAME);