uhid.c 18 KB

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