uhid.c 18 KB

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