uinput.c 24 KB

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