uinput.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * User level driver support for input subsystem
  4. *
  5. * Heavily based on evdev.c by Vojtech Pavlik
  6. *
  7. * Author: Aristeu Sergio Rozanski Filho <[email protected]>
  8. *
  9. * Changes/Revisions:
  10. * 0.4 01/09/2014 (Benjamin Tissoires <[email protected]>)
  11. * - add UI_GET_SYSNAME ioctl
  12. * 0.3 09/04/2006 (Anssi Hannula <[email protected]>)
  13. * - updated ff support for the changes in kernel interface
  14. * - added MODULE_VERSION
  15. * 0.2 16/10/2004 (Micah Dowty <[email protected]>)
  16. * - added force feedback support
  17. * - added UI_SET_PHYS
  18. * 0.1 20/06/2002
  19. * - first public version
  20. */
  21. #include <uapi/linux/uinput.h>
  22. #include <linux/poll.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/overflow.h>
  30. #include <linux/input/mt.h>
  31. #include "../input-compat.h"
  32. #define UINPUT_NAME "uinput"
  33. #define UINPUT_BUFFER_SIZE 16
  34. #define UINPUT_NUM_REQUESTS 16
  35. #define UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS 10
  36. enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED };
  37. struct uinput_request {
  38. unsigned int id;
  39. unsigned int code; /* UI_FF_UPLOAD, UI_FF_ERASE */
  40. int retval;
  41. struct completion done;
  42. union {
  43. unsigned int effect_id;
  44. struct {
  45. struct ff_effect *effect;
  46. struct ff_effect *old;
  47. } upload;
  48. } u;
  49. };
  50. struct uinput_device {
  51. struct input_dev *dev;
  52. struct mutex mutex;
  53. enum uinput_state state;
  54. wait_queue_head_t waitq;
  55. unsigned char ready;
  56. unsigned char head;
  57. unsigned char tail;
  58. struct input_event buff[UINPUT_BUFFER_SIZE];
  59. unsigned int ff_effects_max;
  60. struct uinput_request *requests[UINPUT_NUM_REQUESTS];
  61. wait_queue_head_t requests_waitq;
  62. spinlock_t requests_lock;
  63. };
  64. static int uinput_dev_event(struct input_dev *dev,
  65. unsigned int type, unsigned int code, int value)
  66. {
  67. struct uinput_device *udev = input_get_drvdata(dev);
  68. struct timespec64 ts;
  69. ktime_get_ts64(&ts);
  70. udev->buff[udev->head] = (struct input_event) {
  71. .input_event_sec = ts.tv_sec,
  72. .input_event_usec = ts.tv_nsec / NSEC_PER_USEC,
  73. .type = type,
  74. .code = code,
  75. .value = value,
  76. };
  77. udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
  78. wake_up_interruptible(&udev->waitq);
  79. return 0;
  80. }
  81. /* Atomically allocate an ID for the given request. Returns 0 on success. */
  82. static bool uinput_request_alloc_id(struct uinput_device *udev,
  83. struct uinput_request *request)
  84. {
  85. unsigned int id;
  86. bool reserved = false;
  87. spin_lock(&udev->requests_lock);
  88. for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
  89. if (!udev->requests[id]) {
  90. request->id = id;
  91. udev->requests[id] = request;
  92. reserved = true;
  93. break;
  94. }
  95. }
  96. spin_unlock(&udev->requests_lock);
  97. return reserved;
  98. }
  99. static struct uinput_request *uinput_request_find(struct uinput_device *udev,
  100. unsigned int id)
  101. {
  102. /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
  103. if (id >= UINPUT_NUM_REQUESTS)
  104. return NULL;
  105. return udev->requests[id];
  106. }
  107. static int uinput_request_reserve_slot(struct uinput_device *udev,
  108. struct uinput_request *request)
  109. {
  110. /* Allocate slot. If none are available right away, wait. */
  111. return wait_event_interruptible(udev->requests_waitq,
  112. uinput_request_alloc_id(udev, request));
  113. }
  114. static void uinput_request_release_slot(struct uinput_device *udev,
  115. unsigned int id)
  116. {
  117. /* Mark slot as available */
  118. spin_lock(&udev->requests_lock);
  119. udev->requests[id] = NULL;
  120. spin_unlock(&udev->requests_lock);
  121. wake_up(&udev->requests_waitq);
  122. }
  123. static int uinput_request_send(struct uinput_device *udev,
  124. struct uinput_request *request)
  125. {
  126. int retval;
  127. retval = mutex_lock_interruptible(&udev->mutex);
  128. if (retval)
  129. return retval;
  130. if (udev->state != UIST_CREATED) {
  131. retval = -ENODEV;
  132. goto out;
  133. }
  134. init_completion(&request->done);
  135. /*
  136. * Tell our userspace application about this new request
  137. * by queueing an input event.
  138. */
  139. uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
  140. out:
  141. mutex_unlock(&udev->mutex);
  142. return retval;
  143. }
  144. static int uinput_request_submit(struct uinput_device *udev,
  145. struct uinput_request *request)
  146. {
  147. int retval;
  148. retval = uinput_request_reserve_slot(udev, request);
  149. if (retval)
  150. return retval;
  151. retval = uinput_request_send(udev, request);
  152. if (retval)
  153. goto out;
  154. if (!wait_for_completion_timeout(&request->done, 30 * HZ)) {
  155. retval = -ETIMEDOUT;
  156. goto out;
  157. }
  158. retval = request->retval;
  159. out:
  160. uinput_request_release_slot(udev, request->id);
  161. return retval;
  162. }
  163. /*
  164. * Fail all outstanding requests so handlers don't wait for the userspace
  165. * to finish processing them.
  166. */
  167. static void uinput_flush_requests(struct uinput_device *udev)
  168. {
  169. struct uinput_request *request;
  170. int i;
  171. spin_lock(&udev->requests_lock);
  172. for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
  173. request = udev->requests[i];
  174. if (request) {
  175. request->retval = -ENODEV;
  176. complete(&request->done);
  177. }
  178. }
  179. spin_unlock(&udev->requests_lock);
  180. }
  181. static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
  182. {
  183. uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
  184. }
  185. static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
  186. {
  187. uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
  188. }
  189. static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
  190. {
  191. return uinput_dev_event(dev, EV_FF, effect_id, value);
  192. }
  193. static int uinput_dev_upload_effect(struct input_dev *dev,
  194. struct ff_effect *effect,
  195. struct ff_effect *old)
  196. {
  197. struct uinput_device *udev = input_get_drvdata(dev);
  198. struct uinput_request request;
  199. /*
  200. * uinput driver does not currently support periodic effects with
  201. * custom waveform since it does not have a way to pass buffer of
  202. * samples (custom_data) to userspace. If ever there is a device
  203. * supporting custom waveforms we would need to define an additional
  204. * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
  205. */
  206. if (effect->type == FF_PERIODIC &&
  207. effect->u.periodic.waveform == FF_CUSTOM)
  208. return -EINVAL;
  209. request.code = UI_FF_UPLOAD;
  210. request.u.upload.effect = effect;
  211. request.u.upload.old = old;
  212. return uinput_request_submit(udev, &request);
  213. }
  214. static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
  215. {
  216. struct uinput_device *udev = input_get_drvdata(dev);
  217. struct uinput_request request;
  218. if (!test_bit(EV_FF, dev->evbit))
  219. return -ENOSYS;
  220. request.code = UI_FF_ERASE;
  221. request.u.effect_id = effect_id;
  222. return uinput_request_submit(udev, &request);
  223. }
  224. static int uinput_dev_flush(struct input_dev *dev, struct file *file)
  225. {
  226. /*
  227. * If we are called with file == NULL that means we are tearing
  228. * down the device, and therefore we can not handle FF erase
  229. * requests: either we are handling UI_DEV_DESTROY (and holding
  230. * the udev->mutex), or the file descriptor is closed and there is
  231. * nobody on the other side anymore.
  232. */
  233. return file ? input_ff_flush(dev, file) : 0;
  234. }
  235. static void uinput_destroy_device(struct uinput_device *udev)
  236. {
  237. const char *name, *phys;
  238. struct input_dev *dev = udev->dev;
  239. enum uinput_state old_state = udev->state;
  240. udev->state = UIST_NEW_DEVICE;
  241. if (dev) {
  242. name = dev->name;
  243. phys = dev->phys;
  244. if (old_state == UIST_CREATED) {
  245. uinput_flush_requests(udev);
  246. input_unregister_device(dev);
  247. } else {
  248. input_free_device(dev);
  249. }
  250. kfree(name);
  251. kfree(phys);
  252. udev->dev = NULL;
  253. }
  254. }
  255. static int uinput_create_device(struct uinput_device *udev)
  256. {
  257. struct input_dev *dev = udev->dev;
  258. int error, nslot;
  259. if (udev->state != UIST_SETUP_COMPLETE) {
  260. printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
  261. return -EINVAL;
  262. }
  263. if (test_bit(EV_ABS, dev->evbit)) {
  264. input_alloc_absinfo(dev);
  265. if (!dev->absinfo) {
  266. error = -EINVAL;
  267. goto fail1;
  268. }
  269. if (test_bit(ABS_MT_SLOT, dev->absbit)) {
  270. nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
  271. error = input_mt_init_slots(dev, nslot, 0);
  272. if (error)
  273. goto fail1;
  274. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  275. input_set_events_per_packet(dev, 60);
  276. }
  277. }
  278. if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
  279. printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
  280. UINPUT_NAME);
  281. error = -EINVAL;
  282. goto fail1;
  283. }
  284. if (udev->ff_effects_max) {
  285. error = input_ff_create(dev, udev->ff_effects_max);
  286. if (error)
  287. goto fail1;
  288. dev->ff->upload = uinput_dev_upload_effect;
  289. dev->ff->erase = uinput_dev_erase_effect;
  290. dev->ff->playback = uinput_dev_playback;
  291. dev->ff->set_gain = uinput_dev_set_gain;
  292. dev->ff->set_autocenter = uinput_dev_set_autocenter;
  293. /*
  294. * The standard input_ff_flush() implementation does
  295. * not quite work for uinput as we can't reasonably
  296. * handle FF requests during device teardown.
  297. */
  298. dev->flush = uinput_dev_flush;
  299. }
  300. dev->event = uinput_dev_event;
  301. input_set_drvdata(udev->dev, udev);
  302. error = input_register_device(udev->dev);
  303. if (error)
  304. goto fail2;
  305. udev->state = UIST_CREATED;
  306. return 0;
  307. fail2: input_ff_destroy(dev);
  308. fail1: uinput_destroy_device(udev);
  309. return error;
  310. }
  311. static int uinput_open(struct inode *inode, struct file *file)
  312. {
  313. struct uinput_device *newdev;
  314. newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
  315. if (!newdev)
  316. return -ENOMEM;
  317. mutex_init(&newdev->mutex);
  318. spin_lock_init(&newdev->requests_lock);
  319. init_waitqueue_head(&newdev->requests_waitq);
  320. init_waitqueue_head(&newdev->waitq);
  321. newdev->state = UIST_NEW_DEVICE;
  322. file->private_data = newdev;
  323. stream_open(inode, file);
  324. return 0;
  325. }
  326. static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
  327. const struct input_absinfo *abs)
  328. {
  329. int min, max, range;
  330. min = abs->minimum;
  331. max = abs->maximum;
  332. if ((min != 0 || max != 0) && max < min) {
  333. printk(KERN_DEBUG
  334. "%s: invalid abs[%02x] min:%d max:%d\n",
  335. UINPUT_NAME, code, min, max);
  336. return -EINVAL;
  337. }
  338. if (!check_sub_overflow(max, min, &range) && abs->flat > range) {
  339. printk(KERN_DEBUG
  340. "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
  341. UINPUT_NAME, code, abs->flat, min, max);
  342. return -EINVAL;
  343. }
  344. return 0;
  345. }
  346. static int uinput_validate_absbits(struct input_dev *dev)
  347. {
  348. unsigned int cnt;
  349. int error;
  350. if (!test_bit(EV_ABS, dev->evbit))
  351. return 0;
  352. /*
  353. * Check if absmin/absmax/absfuzz/absflat are sane.
  354. */
  355. for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
  356. if (!dev->absinfo)
  357. return -EINVAL;
  358. error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
  359. if (error)
  360. return error;
  361. }
  362. return 0;
  363. }
  364. static int uinput_dev_setup(struct uinput_device *udev,
  365. struct uinput_setup __user *arg)
  366. {
  367. struct uinput_setup setup;
  368. struct input_dev *dev;
  369. if (udev->state == UIST_CREATED)
  370. return -EINVAL;
  371. if (copy_from_user(&setup, arg, sizeof(setup)))
  372. return -EFAULT;
  373. if (!setup.name[0])
  374. return -EINVAL;
  375. dev = udev->dev;
  376. dev->id = setup.id;
  377. udev->ff_effects_max = setup.ff_effects_max;
  378. kfree(dev->name);
  379. dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
  380. if (!dev->name)
  381. return -ENOMEM;
  382. udev->state = UIST_SETUP_COMPLETE;
  383. return 0;
  384. }
  385. static int uinput_abs_setup(struct uinput_device *udev,
  386. struct uinput_setup __user *arg, size_t size)
  387. {
  388. struct uinput_abs_setup setup = {};
  389. struct input_dev *dev;
  390. int error;
  391. if (size > sizeof(setup))
  392. return -E2BIG;
  393. if (udev->state == UIST_CREATED)
  394. return -EINVAL;
  395. if (copy_from_user(&setup, arg, size))
  396. return -EFAULT;
  397. if (setup.code > ABS_MAX)
  398. return -ERANGE;
  399. dev = udev->dev;
  400. error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
  401. if (error)
  402. return error;
  403. input_alloc_absinfo(dev);
  404. if (!dev->absinfo)
  405. return -ENOMEM;
  406. set_bit(setup.code, dev->absbit);
  407. dev->absinfo[setup.code] = setup.absinfo;
  408. return 0;
  409. }
  410. /* legacy setup via write() */
  411. static int uinput_setup_device_legacy(struct uinput_device *udev,
  412. const char __user *buffer, size_t count)
  413. {
  414. struct uinput_user_dev *user_dev;
  415. struct input_dev *dev;
  416. int i;
  417. int retval;
  418. if (count != sizeof(struct uinput_user_dev))
  419. return -EINVAL;
  420. if (!udev->dev) {
  421. udev->dev = input_allocate_device();
  422. if (!udev->dev)
  423. return -ENOMEM;
  424. }
  425. dev = udev->dev;
  426. user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
  427. if (IS_ERR(user_dev))
  428. return PTR_ERR(user_dev);
  429. udev->ff_effects_max = user_dev->ff_effects_max;
  430. /* Ensure name is filled in */
  431. if (!user_dev->name[0]) {
  432. retval = -EINVAL;
  433. goto exit;
  434. }
  435. kfree(dev->name);
  436. dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
  437. GFP_KERNEL);
  438. if (!dev->name) {
  439. retval = -ENOMEM;
  440. goto exit;
  441. }
  442. dev->id.bustype = user_dev->id.bustype;
  443. dev->id.vendor = user_dev->id.vendor;
  444. dev->id.product = user_dev->id.product;
  445. dev->id.version = user_dev->id.version;
  446. for (i = 0; i < ABS_CNT; i++) {
  447. input_abs_set_max(dev, i, user_dev->absmax[i]);
  448. input_abs_set_min(dev, i, user_dev->absmin[i]);
  449. input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
  450. input_abs_set_flat(dev, i, user_dev->absflat[i]);
  451. }
  452. retval = uinput_validate_absbits(dev);
  453. if (retval < 0)
  454. goto exit;
  455. udev->state = UIST_SETUP_COMPLETE;
  456. retval = count;
  457. exit:
  458. kfree(user_dev);
  459. return retval;
  460. }
  461. /*
  462. * Returns true if the given timestamp is valid (i.e., if all the following
  463. * conditions are satisfied), false otherwise.
  464. * 1) given timestamp is positive
  465. * 2) it's within the allowed offset before the current time
  466. * 3) it's not in the future
  467. */
  468. static bool is_valid_timestamp(const ktime_t timestamp)
  469. {
  470. ktime_t zero_time;
  471. ktime_t current_time;
  472. ktime_t min_time;
  473. ktime_t offset;
  474. zero_time = ktime_set(0, 0);
  475. if (ktime_compare(zero_time, timestamp) >= 0)
  476. return false;
  477. current_time = ktime_get();
  478. offset = ktime_set(UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS, 0);
  479. min_time = ktime_sub(current_time, offset);
  480. if (ktime_after(min_time, timestamp) || ktime_after(timestamp, current_time))
  481. return false;
  482. return true;
  483. }
  484. static ssize_t uinput_inject_events(struct uinput_device *udev,
  485. const char __user *buffer, size_t count)
  486. {
  487. struct input_event ev;
  488. size_t bytes = 0;
  489. ktime_t timestamp;
  490. if (count != 0 && count < input_event_size())
  491. return -EINVAL;
  492. while (bytes + input_event_size() <= count) {
  493. /*
  494. * Note that even if some events were fetched successfully
  495. * we are still going to return EFAULT instead of partial
  496. * count to let userspace know that it got it's buffers
  497. * all wrong.
  498. */
  499. if (input_event_from_user(buffer + bytes, &ev))
  500. return -EFAULT;
  501. timestamp = ktime_set(ev.input_event_sec, ev.input_event_usec * NSEC_PER_USEC);
  502. if (is_valid_timestamp(timestamp))
  503. input_set_timestamp(udev->dev, timestamp);
  504. input_event(udev->dev, ev.type, ev.code, ev.value);
  505. bytes += input_event_size();
  506. cond_resched();
  507. }
  508. return bytes;
  509. }
  510. static ssize_t uinput_write(struct file *file, const char __user *buffer,
  511. size_t count, loff_t *ppos)
  512. {
  513. struct uinput_device *udev = file->private_data;
  514. int retval;
  515. if (count == 0)
  516. return 0;
  517. retval = mutex_lock_interruptible(&udev->mutex);
  518. if (retval)
  519. return retval;
  520. retval = udev->state == UIST_CREATED ?
  521. uinput_inject_events(udev, buffer, count) :
  522. uinput_setup_device_legacy(udev, buffer, count);
  523. mutex_unlock(&udev->mutex);
  524. return retval;
  525. }
  526. static bool uinput_fetch_next_event(struct uinput_device *udev,
  527. struct input_event *event)
  528. {
  529. bool have_event;
  530. spin_lock_irq(&udev->dev->event_lock);
  531. have_event = udev->head != udev->tail;
  532. if (have_event) {
  533. *event = udev->buff[udev->tail];
  534. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  535. }
  536. spin_unlock_irq(&udev->dev->event_lock);
  537. return have_event;
  538. }
  539. static ssize_t uinput_events_to_user(struct uinput_device *udev,
  540. char __user *buffer, size_t count)
  541. {
  542. struct input_event event;
  543. size_t read = 0;
  544. while (read + input_event_size() <= count &&
  545. uinput_fetch_next_event(udev, &event)) {
  546. if (input_event_to_user(buffer + read, &event))
  547. return -EFAULT;
  548. read += input_event_size();
  549. }
  550. return read;
  551. }
  552. static ssize_t uinput_read(struct file *file, char __user *buffer,
  553. size_t count, loff_t *ppos)
  554. {
  555. struct uinput_device *udev = file->private_data;
  556. ssize_t retval;
  557. if (count != 0 && count < input_event_size())
  558. return -EINVAL;
  559. do {
  560. retval = mutex_lock_interruptible(&udev->mutex);
  561. if (retval)
  562. return retval;
  563. if (udev->state != UIST_CREATED)
  564. retval = -ENODEV;
  565. else if (udev->head == udev->tail &&
  566. (file->f_flags & O_NONBLOCK))
  567. retval = -EAGAIN;
  568. else
  569. retval = uinput_events_to_user(udev, buffer, count);
  570. mutex_unlock(&udev->mutex);
  571. if (retval || count == 0)
  572. break;
  573. if (!(file->f_flags & O_NONBLOCK))
  574. retval = wait_event_interruptible(udev->waitq,
  575. udev->head != udev->tail ||
  576. udev->state != UIST_CREATED);
  577. } while (retval == 0);
  578. return retval;
  579. }
  580. static __poll_t uinput_poll(struct file *file, poll_table *wait)
  581. {
  582. struct uinput_device *udev = file->private_data;
  583. __poll_t mask = EPOLLOUT | EPOLLWRNORM; /* uinput is always writable */
  584. poll_wait(file, &udev->waitq, wait);
  585. if (udev->head != udev->tail)
  586. mask |= EPOLLIN | EPOLLRDNORM;
  587. return mask;
  588. }
  589. static int uinput_release(struct inode *inode, struct file *file)
  590. {
  591. struct uinput_device *udev = file->private_data;
  592. uinput_destroy_device(udev);
  593. kfree(udev);
  594. return 0;
  595. }
  596. #ifdef CONFIG_COMPAT
  597. struct uinput_ff_upload_compat {
  598. __u32 request_id;
  599. __s32 retval;
  600. struct ff_effect_compat effect;
  601. struct ff_effect_compat old;
  602. };
  603. static int uinput_ff_upload_to_user(char __user *buffer,
  604. const struct uinput_ff_upload *ff_up)
  605. {
  606. if (in_compat_syscall()) {
  607. struct uinput_ff_upload_compat ff_up_compat;
  608. ff_up_compat.request_id = ff_up->request_id;
  609. ff_up_compat.retval = ff_up->retval;
  610. /*
  611. * It so happens that the pointer that gives us the trouble
  612. * is the last field in the structure. Since we don't support
  613. * custom waveforms in uinput anyway we can just copy the whole
  614. * thing (to the compat size) and ignore the pointer.
  615. */
  616. memcpy(&ff_up_compat.effect, &ff_up->effect,
  617. sizeof(struct ff_effect_compat));
  618. memcpy(&ff_up_compat.old, &ff_up->old,
  619. sizeof(struct ff_effect_compat));
  620. if (copy_to_user(buffer, &ff_up_compat,
  621. sizeof(struct uinput_ff_upload_compat)))
  622. return -EFAULT;
  623. } else {
  624. if (copy_to_user(buffer, ff_up,
  625. sizeof(struct uinput_ff_upload)))
  626. return -EFAULT;
  627. }
  628. return 0;
  629. }
  630. static int uinput_ff_upload_from_user(const char __user *buffer,
  631. struct uinput_ff_upload *ff_up)
  632. {
  633. if (in_compat_syscall()) {
  634. struct uinput_ff_upload_compat ff_up_compat;
  635. if (copy_from_user(&ff_up_compat, buffer,
  636. sizeof(struct uinput_ff_upload_compat)))
  637. return -EFAULT;
  638. ff_up->request_id = ff_up_compat.request_id;
  639. ff_up->retval = ff_up_compat.retval;
  640. memcpy(&ff_up->effect, &ff_up_compat.effect,
  641. sizeof(struct ff_effect_compat));
  642. memcpy(&ff_up->old, &ff_up_compat.old,
  643. sizeof(struct ff_effect_compat));
  644. } else {
  645. if (copy_from_user(ff_up, buffer,
  646. sizeof(struct uinput_ff_upload)))
  647. return -EFAULT;
  648. }
  649. return 0;
  650. }
  651. #else
  652. static int uinput_ff_upload_to_user(char __user *buffer,
  653. const struct uinput_ff_upload *ff_up)
  654. {
  655. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  656. return -EFAULT;
  657. return 0;
  658. }
  659. static int uinput_ff_upload_from_user(const char __user *buffer,
  660. struct uinput_ff_upload *ff_up)
  661. {
  662. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  663. return -EFAULT;
  664. return 0;
  665. }
  666. #endif
  667. #define uinput_set_bit(_arg, _bit, _max) \
  668. ({ \
  669. int __ret = 0; \
  670. if (udev->state == UIST_CREATED) \
  671. __ret = -EINVAL; \
  672. else if ((_arg) > (_max)) \
  673. __ret = -EINVAL; \
  674. else set_bit((_arg), udev->dev->_bit); \
  675. __ret; \
  676. })
  677. static int uinput_str_to_user(void __user *dest, const char *str,
  678. unsigned int maxlen)
  679. {
  680. char __user *p = dest;
  681. int len, ret;
  682. if (!str)
  683. return -ENOENT;
  684. if (maxlen == 0)
  685. return -EINVAL;
  686. len = strlen(str) + 1;
  687. if (len > maxlen)
  688. len = maxlen;
  689. ret = copy_to_user(p, str, len);
  690. if (ret)
  691. return -EFAULT;
  692. /* force terminating '\0' */
  693. ret = put_user(0, p + len - 1);
  694. return ret ? -EFAULT : len;
  695. }
  696. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  697. unsigned long arg, void __user *p)
  698. {
  699. int retval;
  700. struct uinput_device *udev = file->private_data;
  701. struct uinput_ff_upload ff_up;
  702. struct uinput_ff_erase ff_erase;
  703. struct uinput_request *req;
  704. char *phys;
  705. const char *name;
  706. unsigned int size;
  707. retval = mutex_lock_interruptible(&udev->mutex);
  708. if (retval)
  709. return retval;
  710. if (!udev->dev) {
  711. udev->dev = input_allocate_device();
  712. if (!udev->dev) {
  713. retval = -ENOMEM;
  714. goto out;
  715. }
  716. }
  717. switch (cmd) {
  718. case UI_GET_VERSION:
  719. if (put_user(UINPUT_VERSION, (unsigned int __user *)p))
  720. retval = -EFAULT;
  721. goto out;
  722. case UI_DEV_CREATE:
  723. retval = uinput_create_device(udev);
  724. goto out;
  725. case UI_DEV_DESTROY:
  726. uinput_destroy_device(udev);
  727. goto out;
  728. case UI_DEV_SETUP:
  729. retval = uinput_dev_setup(udev, p);
  730. goto out;
  731. /* UI_ABS_SETUP is handled in the variable size ioctls */
  732. case UI_SET_EVBIT:
  733. retval = uinput_set_bit(arg, evbit, EV_MAX);
  734. goto out;
  735. case UI_SET_KEYBIT:
  736. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  737. goto out;
  738. case UI_SET_RELBIT:
  739. retval = uinput_set_bit(arg, relbit, REL_MAX);
  740. goto out;
  741. case UI_SET_ABSBIT:
  742. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  743. goto out;
  744. case UI_SET_MSCBIT:
  745. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  746. goto out;
  747. case UI_SET_LEDBIT:
  748. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  749. goto out;
  750. case UI_SET_SNDBIT:
  751. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  752. goto out;
  753. case UI_SET_FFBIT:
  754. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  755. goto out;
  756. case UI_SET_SWBIT:
  757. retval = uinput_set_bit(arg, swbit, SW_MAX);
  758. goto out;
  759. case UI_SET_PROPBIT:
  760. retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
  761. goto out;
  762. case UI_SET_PHYS:
  763. if (udev->state == UIST_CREATED) {
  764. retval = -EINVAL;
  765. goto out;
  766. }
  767. phys = strndup_user(p, 1024);
  768. if (IS_ERR(phys)) {
  769. retval = PTR_ERR(phys);
  770. goto out;
  771. }
  772. kfree(udev->dev->phys);
  773. udev->dev->phys = phys;
  774. goto out;
  775. case UI_BEGIN_FF_UPLOAD:
  776. retval = uinput_ff_upload_from_user(p, &ff_up);
  777. if (retval)
  778. goto out;
  779. req = uinput_request_find(udev, ff_up.request_id);
  780. if (!req || req->code != UI_FF_UPLOAD ||
  781. !req->u.upload.effect) {
  782. retval = -EINVAL;
  783. goto out;
  784. }
  785. ff_up.retval = 0;
  786. ff_up.effect = *req->u.upload.effect;
  787. if (req->u.upload.old)
  788. ff_up.old = *req->u.upload.old;
  789. else
  790. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  791. retval = uinput_ff_upload_to_user(p, &ff_up);
  792. goto out;
  793. case UI_BEGIN_FF_ERASE:
  794. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  795. retval = -EFAULT;
  796. goto out;
  797. }
  798. req = uinput_request_find(udev, ff_erase.request_id);
  799. if (!req || req->code != UI_FF_ERASE) {
  800. retval = -EINVAL;
  801. goto out;
  802. }
  803. ff_erase.retval = 0;
  804. ff_erase.effect_id = req->u.effect_id;
  805. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  806. retval = -EFAULT;
  807. goto out;
  808. }
  809. goto out;
  810. case UI_END_FF_UPLOAD:
  811. retval = uinput_ff_upload_from_user(p, &ff_up);
  812. if (retval)
  813. goto out;
  814. req = uinput_request_find(udev, ff_up.request_id);
  815. if (!req || req->code != UI_FF_UPLOAD ||
  816. !req->u.upload.effect) {
  817. retval = -EINVAL;
  818. goto out;
  819. }
  820. req->retval = ff_up.retval;
  821. complete(&req->done);
  822. goto out;
  823. case UI_END_FF_ERASE:
  824. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  825. retval = -EFAULT;
  826. goto out;
  827. }
  828. req = uinput_request_find(udev, ff_erase.request_id);
  829. if (!req || req->code != UI_FF_ERASE) {
  830. retval = -EINVAL;
  831. goto out;
  832. }
  833. req->retval = ff_erase.retval;
  834. complete(&req->done);
  835. goto out;
  836. }
  837. size = _IOC_SIZE(cmd);
  838. /* Now check variable-length commands */
  839. switch (cmd & ~IOCSIZE_MASK) {
  840. case UI_GET_SYSNAME(0):
  841. if (udev->state != UIST_CREATED) {
  842. retval = -ENOENT;
  843. goto out;
  844. }
  845. name = dev_name(&udev->dev->dev);
  846. retval = uinput_str_to_user(p, name, size);
  847. goto out;
  848. case UI_ABS_SETUP & ~IOCSIZE_MASK:
  849. retval = uinput_abs_setup(udev, p, size);
  850. goto out;
  851. }
  852. retval = -EINVAL;
  853. out:
  854. mutex_unlock(&udev->mutex);
  855. return retval;
  856. }
  857. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  858. {
  859. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  860. }
  861. #ifdef CONFIG_COMPAT
  862. /*
  863. * These IOCTLs change their size and thus their numbers between
  864. * 32 and 64 bits.
  865. */
  866. #define UI_SET_PHYS_COMPAT \
  867. _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
  868. #define UI_BEGIN_FF_UPLOAD_COMPAT \
  869. _IOWR(UINPUT_IOCTL_BASE, 200, struct uinput_ff_upload_compat)
  870. #define UI_END_FF_UPLOAD_COMPAT \
  871. _IOW(UINPUT_IOCTL_BASE, 201, struct uinput_ff_upload_compat)
  872. static long uinput_compat_ioctl(struct file *file,
  873. unsigned int cmd, unsigned long arg)
  874. {
  875. switch (cmd) {
  876. case UI_SET_PHYS_COMPAT:
  877. cmd = UI_SET_PHYS;
  878. break;
  879. case UI_BEGIN_FF_UPLOAD_COMPAT:
  880. cmd = UI_BEGIN_FF_UPLOAD;
  881. break;
  882. case UI_END_FF_UPLOAD_COMPAT:
  883. cmd = UI_END_FF_UPLOAD;
  884. break;
  885. }
  886. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  887. }
  888. #endif
  889. static const struct file_operations uinput_fops = {
  890. .owner = THIS_MODULE,
  891. .open = uinput_open,
  892. .release = uinput_release,
  893. .read = uinput_read,
  894. .write = uinput_write,
  895. .poll = uinput_poll,
  896. .unlocked_ioctl = uinput_ioctl,
  897. #ifdef CONFIG_COMPAT
  898. .compat_ioctl = uinput_compat_ioctl,
  899. #endif
  900. .llseek = no_llseek,
  901. };
  902. static struct miscdevice uinput_misc = {
  903. .fops = &uinput_fops,
  904. .minor = UINPUT_MINOR,
  905. .name = UINPUT_NAME,
  906. };
  907. module_misc_device(uinput_misc);
  908. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  909. MODULE_ALIAS("devname:" UINPUT_NAME);
  910. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  911. MODULE_DESCRIPTION("User level driver support for input subsystem");
  912. MODULE_LICENSE("GPL");