uhid.c 19 KB

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