uhid.c 18 KB

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