uinput.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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_done(struct uinput_device *udev,
  90. struct uinput_request *request)
  91. {
  92. /* Mark slot as available */
  93. udev->requests[request->id] = NULL;
  94. wake_up(&udev->requests_waitq);
  95. complete(&request->done);
  96. }
  97. static int uinput_request_send(struct uinput_device *udev,
  98. struct uinput_request *request)
  99. {
  100. int retval;
  101. retval = mutex_lock_interruptible(&udev->mutex);
  102. if (retval)
  103. return retval;
  104. if (udev->state != UIST_CREATED) {
  105. retval = -ENODEV;
  106. goto out;
  107. }
  108. init_completion(&request->done);
  109. /*
  110. * Tell our userspace application about this new request
  111. * by queueing an input event.
  112. */
  113. uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
  114. out:
  115. mutex_unlock(&udev->mutex);
  116. return retval;
  117. }
  118. static int uinput_request_submit(struct uinput_device *udev,
  119. struct uinput_request *request)
  120. {
  121. int error;
  122. error = uinput_request_reserve_slot(udev, request);
  123. if (error)
  124. return error;
  125. error = uinput_request_send(udev, request);
  126. if (error) {
  127. uinput_request_done(udev, request);
  128. return error;
  129. }
  130. wait_for_completion(&request->done);
  131. return request->retval;
  132. }
  133. /*
  134. * Fail all outstanding requests so handlers don't wait for the userspace
  135. * to finish processing them.
  136. */
  137. static void uinput_flush_requests(struct uinput_device *udev)
  138. {
  139. struct uinput_request *request;
  140. int i;
  141. spin_lock(&udev->requests_lock);
  142. for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
  143. request = udev->requests[i];
  144. if (request) {
  145. request->retval = -ENODEV;
  146. uinput_request_done(udev, request);
  147. }
  148. }
  149. spin_unlock(&udev->requests_lock);
  150. }
  151. static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
  152. {
  153. uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
  154. }
  155. static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
  156. {
  157. uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
  158. }
  159. static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
  160. {
  161. return uinput_dev_event(dev, EV_FF, effect_id, value);
  162. }
  163. static int uinput_dev_upload_effect(struct input_dev *dev,
  164. struct ff_effect *effect,
  165. struct ff_effect *old)
  166. {
  167. struct uinput_device *udev = input_get_drvdata(dev);
  168. struct uinput_request request;
  169. /*
  170. * uinput driver does not currently support periodic effects with
  171. * custom waveform since it does not have a way to pass buffer of
  172. * samples (custom_data) to userspace. If ever there is a device
  173. * supporting custom waveforms we would need to define an additional
  174. * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
  175. */
  176. if (effect->type == FF_PERIODIC &&
  177. effect->u.periodic.waveform == FF_CUSTOM)
  178. return -EINVAL;
  179. request.code = UI_FF_UPLOAD;
  180. request.u.upload.effect = effect;
  181. request.u.upload.old = old;
  182. return uinput_request_submit(udev, &request);
  183. }
  184. static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
  185. {
  186. struct uinput_device *udev = input_get_drvdata(dev);
  187. struct uinput_request request;
  188. if (!test_bit(EV_FF, dev->evbit))
  189. return -ENOSYS;
  190. request.code = UI_FF_ERASE;
  191. request.u.effect_id = effect_id;
  192. return uinput_request_submit(udev, &request);
  193. }
  194. static void uinput_destroy_device(struct uinput_device *udev)
  195. {
  196. const char *name, *phys;
  197. struct input_dev *dev = udev->dev;
  198. enum uinput_state old_state = udev->state;
  199. udev->state = UIST_NEW_DEVICE;
  200. if (dev) {
  201. name = dev->name;
  202. phys = dev->phys;
  203. if (old_state == UIST_CREATED) {
  204. uinput_flush_requests(udev);
  205. input_unregister_device(dev);
  206. } else {
  207. input_free_device(dev);
  208. }
  209. kfree(name);
  210. kfree(phys);
  211. udev->dev = NULL;
  212. }
  213. }
  214. static int uinput_create_device(struct uinput_device *udev)
  215. {
  216. struct input_dev *dev = udev->dev;
  217. int error;
  218. if (udev->state != UIST_SETUP_COMPLETE) {
  219. printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
  220. return -EINVAL;
  221. }
  222. if (udev->ff_effects_max) {
  223. error = input_ff_create(dev, udev->ff_effects_max);
  224. if (error)
  225. goto fail1;
  226. dev->ff->upload = uinput_dev_upload_effect;
  227. dev->ff->erase = uinput_dev_erase_effect;
  228. dev->ff->playback = uinput_dev_playback;
  229. dev->ff->set_gain = uinput_dev_set_gain;
  230. dev->ff->set_autocenter = uinput_dev_set_autocenter;
  231. }
  232. error = input_register_device(udev->dev);
  233. if (error)
  234. goto fail2;
  235. udev->state = UIST_CREATED;
  236. return 0;
  237. fail2: input_ff_destroy(dev);
  238. fail1: uinput_destroy_device(udev);
  239. return error;
  240. }
  241. static int uinput_open(struct inode *inode, struct file *file)
  242. {
  243. struct uinput_device *newdev;
  244. newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
  245. if (!newdev)
  246. return -ENOMEM;
  247. mutex_init(&newdev->mutex);
  248. spin_lock_init(&newdev->requests_lock);
  249. init_waitqueue_head(&newdev->requests_waitq);
  250. init_waitqueue_head(&newdev->waitq);
  251. newdev->state = UIST_NEW_DEVICE;
  252. file->private_data = newdev;
  253. nonseekable_open(inode, file);
  254. return 0;
  255. }
  256. static int uinput_validate_absbits(struct input_dev *dev)
  257. {
  258. unsigned int cnt;
  259. int nslot;
  260. if (!test_bit(EV_ABS, dev->evbit))
  261. return 0;
  262. /*
  263. * Check if absmin/absmax/absfuzz/absflat are sane.
  264. */
  265. for (cnt = 0; cnt < ABS_CNT; cnt++) {
  266. int min, max;
  267. if (!test_bit(cnt, dev->absbit))
  268. continue;
  269. min = input_abs_get_min(dev, cnt);
  270. max = input_abs_get_max(dev, cnt);
  271. if ((min != 0 || max != 0) && max <= min) {
  272. printk(KERN_DEBUG
  273. "%s: invalid abs[%02x] min:%d max:%d\n",
  274. UINPUT_NAME, cnt,
  275. input_abs_get_min(dev, cnt),
  276. input_abs_get_max(dev, cnt));
  277. return -EINVAL;
  278. }
  279. if (input_abs_get_flat(dev, cnt) >
  280. input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
  281. printk(KERN_DEBUG
  282. "%s: abs_flat #%02x out of range: %d "
  283. "(min:%d/max:%d)\n",
  284. UINPUT_NAME, cnt,
  285. input_abs_get_flat(dev, cnt),
  286. input_abs_get_min(dev, cnt),
  287. input_abs_get_max(dev, cnt));
  288. return -EINVAL;
  289. }
  290. }
  291. if (test_bit(ABS_MT_SLOT, dev->absbit)) {
  292. nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
  293. input_mt_init_slots(dev, nslot, 0);
  294. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  295. input_set_events_per_packet(dev, 60);
  296. }
  297. return 0;
  298. }
  299. static int uinput_allocate_device(struct uinput_device *udev)
  300. {
  301. udev->dev = input_allocate_device();
  302. if (!udev->dev)
  303. return -ENOMEM;
  304. udev->dev->event = uinput_dev_event;
  305. input_set_drvdata(udev->dev, udev);
  306. return 0;
  307. }
  308. static int uinput_setup_device(struct uinput_device *udev,
  309. const char __user *buffer, size_t count)
  310. {
  311. struct uinput_user_dev *user_dev;
  312. struct input_dev *dev;
  313. int i;
  314. int retval;
  315. if (count != sizeof(struct uinput_user_dev))
  316. return -EINVAL;
  317. if (!udev->dev) {
  318. retval = uinput_allocate_device(udev);
  319. if (retval)
  320. return retval;
  321. }
  322. dev = udev->dev;
  323. user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
  324. if (IS_ERR(user_dev))
  325. return PTR_ERR(user_dev);
  326. udev->ff_effects_max = user_dev->ff_effects_max;
  327. /* Ensure name is filled in */
  328. if (!user_dev->name[0]) {
  329. retval = -EINVAL;
  330. goto exit;
  331. }
  332. kfree(dev->name);
  333. dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
  334. GFP_KERNEL);
  335. if (!dev->name) {
  336. retval = -ENOMEM;
  337. goto exit;
  338. }
  339. dev->id.bustype = user_dev->id.bustype;
  340. dev->id.vendor = user_dev->id.vendor;
  341. dev->id.product = user_dev->id.product;
  342. dev->id.version = user_dev->id.version;
  343. for (i = 0; i < ABS_CNT; i++) {
  344. input_abs_set_max(dev, i, user_dev->absmax[i]);
  345. input_abs_set_min(dev, i, user_dev->absmin[i]);
  346. input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
  347. input_abs_set_flat(dev, i, user_dev->absflat[i]);
  348. }
  349. retval = uinput_validate_absbits(dev);
  350. if (retval < 0)
  351. goto exit;
  352. udev->state = UIST_SETUP_COMPLETE;
  353. retval = count;
  354. exit:
  355. kfree(user_dev);
  356. return retval;
  357. }
  358. static ssize_t uinput_inject_events(struct uinput_device *udev,
  359. const char __user *buffer, size_t count)
  360. {
  361. struct input_event ev;
  362. size_t bytes = 0;
  363. if (count != 0 && count < input_event_size())
  364. return -EINVAL;
  365. while (bytes + input_event_size() <= count) {
  366. /*
  367. * Note that even if some events were fetched successfully
  368. * we are still going to return EFAULT instead of partial
  369. * count to let userspace know that it got it's buffers
  370. * all wrong.
  371. */
  372. if (input_event_from_user(buffer + bytes, &ev))
  373. return -EFAULT;
  374. input_event(udev->dev, ev.type, ev.code, ev.value);
  375. bytes += input_event_size();
  376. }
  377. return bytes;
  378. }
  379. static ssize_t uinput_write(struct file *file, const char __user *buffer,
  380. size_t count, loff_t *ppos)
  381. {
  382. struct uinput_device *udev = file->private_data;
  383. int retval;
  384. if (count == 0)
  385. return 0;
  386. retval = mutex_lock_interruptible(&udev->mutex);
  387. if (retval)
  388. return retval;
  389. retval = udev->state == UIST_CREATED ?
  390. uinput_inject_events(udev, buffer, count) :
  391. uinput_setup_device(udev, buffer, count);
  392. mutex_unlock(&udev->mutex);
  393. return retval;
  394. }
  395. static bool uinput_fetch_next_event(struct uinput_device *udev,
  396. struct input_event *event)
  397. {
  398. bool have_event;
  399. spin_lock_irq(&udev->dev->event_lock);
  400. have_event = udev->head != udev->tail;
  401. if (have_event) {
  402. *event = udev->buff[udev->tail];
  403. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  404. }
  405. spin_unlock_irq(&udev->dev->event_lock);
  406. return have_event;
  407. }
  408. static ssize_t uinput_events_to_user(struct uinput_device *udev,
  409. char __user *buffer, size_t count)
  410. {
  411. struct input_event event;
  412. size_t read = 0;
  413. while (read + input_event_size() <= count &&
  414. uinput_fetch_next_event(udev, &event)) {
  415. if (input_event_to_user(buffer + read, &event))
  416. return -EFAULT;
  417. read += input_event_size();
  418. }
  419. return read;
  420. }
  421. static ssize_t uinput_read(struct file *file, char __user *buffer,
  422. size_t count, loff_t *ppos)
  423. {
  424. struct uinput_device *udev = file->private_data;
  425. ssize_t retval;
  426. if (count != 0 && count < input_event_size())
  427. return -EINVAL;
  428. do {
  429. retval = mutex_lock_interruptible(&udev->mutex);
  430. if (retval)
  431. return retval;
  432. if (udev->state != UIST_CREATED)
  433. retval = -ENODEV;
  434. else if (udev->head == udev->tail &&
  435. (file->f_flags & O_NONBLOCK))
  436. retval = -EAGAIN;
  437. else
  438. retval = uinput_events_to_user(udev, buffer, count);
  439. mutex_unlock(&udev->mutex);
  440. if (retval || count == 0)
  441. break;
  442. if (!(file->f_flags & O_NONBLOCK))
  443. retval = wait_event_interruptible(udev->waitq,
  444. udev->head != udev->tail ||
  445. udev->state != UIST_CREATED);
  446. } while (retval == 0);
  447. return retval;
  448. }
  449. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  450. {
  451. struct uinput_device *udev = file->private_data;
  452. poll_wait(file, &udev->waitq, wait);
  453. if (udev->head != udev->tail)
  454. return POLLIN | POLLRDNORM;
  455. return 0;
  456. }
  457. static int uinput_release(struct inode *inode, struct file *file)
  458. {
  459. struct uinput_device *udev = file->private_data;
  460. uinput_destroy_device(udev);
  461. kfree(udev);
  462. return 0;
  463. }
  464. #ifdef CONFIG_COMPAT
  465. struct uinput_ff_upload_compat {
  466. __u32 request_id;
  467. __s32 retval;
  468. struct ff_effect_compat effect;
  469. struct ff_effect_compat old;
  470. };
  471. static int uinput_ff_upload_to_user(char __user *buffer,
  472. const struct uinput_ff_upload *ff_up)
  473. {
  474. if (INPUT_COMPAT_TEST) {
  475. struct uinput_ff_upload_compat ff_up_compat;
  476. ff_up_compat.request_id = ff_up->request_id;
  477. ff_up_compat.retval = ff_up->retval;
  478. /*
  479. * It so happens that the pointer that gives us the trouble
  480. * is the last field in the structure. Since we don't support
  481. * custom waveforms in uinput anyway we can just copy the whole
  482. * thing (to the compat size) and ignore the pointer.
  483. */
  484. memcpy(&ff_up_compat.effect, &ff_up->effect,
  485. sizeof(struct ff_effect_compat));
  486. memcpy(&ff_up_compat.old, &ff_up->old,
  487. sizeof(struct ff_effect_compat));
  488. if (copy_to_user(buffer, &ff_up_compat,
  489. sizeof(struct uinput_ff_upload_compat)))
  490. return -EFAULT;
  491. } else {
  492. if (copy_to_user(buffer, ff_up,
  493. sizeof(struct uinput_ff_upload)))
  494. return -EFAULT;
  495. }
  496. return 0;
  497. }
  498. static int uinput_ff_upload_from_user(const char __user *buffer,
  499. struct uinput_ff_upload *ff_up)
  500. {
  501. if (INPUT_COMPAT_TEST) {
  502. struct uinput_ff_upload_compat ff_up_compat;
  503. if (copy_from_user(&ff_up_compat, buffer,
  504. sizeof(struct uinput_ff_upload_compat)))
  505. return -EFAULT;
  506. ff_up->request_id = ff_up_compat.request_id;
  507. ff_up->retval = ff_up_compat.retval;
  508. memcpy(&ff_up->effect, &ff_up_compat.effect,
  509. sizeof(struct ff_effect_compat));
  510. memcpy(&ff_up->old, &ff_up_compat.old,
  511. sizeof(struct ff_effect_compat));
  512. } else {
  513. if (copy_from_user(ff_up, buffer,
  514. sizeof(struct uinput_ff_upload)))
  515. return -EFAULT;
  516. }
  517. return 0;
  518. }
  519. #else
  520. static int uinput_ff_upload_to_user(char __user *buffer,
  521. const struct uinput_ff_upload *ff_up)
  522. {
  523. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  524. return -EFAULT;
  525. return 0;
  526. }
  527. static int uinput_ff_upload_from_user(const char __user *buffer,
  528. struct uinput_ff_upload *ff_up)
  529. {
  530. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  531. return -EFAULT;
  532. return 0;
  533. }
  534. #endif
  535. #define uinput_set_bit(_arg, _bit, _max) \
  536. ({ \
  537. int __ret = 0; \
  538. if (udev->state == UIST_CREATED) \
  539. __ret = -EINVAL; \
  540. else if ((_arg) > (_max)) \
  541. __ret = -EINVAL; \
  542. else set_bit((_arg), udev->dev->_bit); \
  543. __ret; \
  544. })
  545. static int uinput_str_to_user(void __user *dest, const char *str,
  546. unsigned int maxlen)
  547. {
  548. char __user *p = dest;
  549. int len, ret;
  550. if (!str)
  551. return -ENOENT;
  552. if (maxlen == 0)
  553. return -EINVAL;
  554. len = strlen(str) + 1;
  555. if (len > maxlen)
  556. len = maxlen;
  557. ret = copy_to_user(p, str, len);
  558. if (ret)
  559. return -EFAULT;
  560. /* force terminating '\0' */
  561. ret = put_user(0, p + len - 1);
  562. return ret ? -EFAULT : len;
  563. }
  564. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  565. unsigned long arg, void __user *p)
  566. {
  567. int retval;
  568. struct uinput_device *udev = file->private_data;
  569. struct uinput_ff_upload ff_up;
  570. struct uinput_ff_erase ff_erase;
  571. struct uinput_request *req;
  572. char *phys;
  573. const char *name;
  574. unsigned int size;
  575. retval = mutex_lock_interruptible(&udev->mutex);
  576. if (retval)
  577. return retval;
  578. if (!udev->dev) {
  579. retval = uinput_allocate_device(udev);
  580. if (retval)
  581. goto out;
  582. }
  583. switch (cmd) {
  584. case UI_GET_VERSION:
  585. if (put_user(UINPUT_VERSION,
  586. (unsigned int __user *)p))
  587. retval = -EFAULT;
  588. goto out;
  589. case UI_DEV_CREATE:
  590. retval = uinput_create_device(udev);
  591. goto out;
  592. case UI_DEV_DESTROY:
  593. uinput_destroy_device(udev);
  594. goto out;
  595. case UI_SET_EVBIT:
  596. retval = uinput_set_bit(arg, evbit, EV_MAX);
  597. goto out;
  598. case UI_SET_KEYBIT:
  599. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  600. goto out;
  601. case UI_SET_RELBIT:
  602. retval = uinput_set_bit(arg, relbit, REL_MAX);
  603. goto out;
  604. case UI_SET_ABSBIT:
  605. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  606. goto out;
  607. case UI_SET_MSCBIT:
  608. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  609. goto out;
  610. case UI_SET_LEDBIT:
  611. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  612. goto out;
  613. case UI_SET_SNDBIT:
  614. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  615. goto out;
  616. case UI_SET_FFBIT:
  617. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  618. goto out;
  619. case UI_SET_SWBIT:
  620. retval = uinput_set_bit(arg, swbit, SW_MAX);
  621. goto out;
  622. case UI_SET_PROPBIT:
  623. retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
  624. goto out;
  625. case UI_SET_PHYS:
  626. if (udev->state == UIST_CREATED) {
  627. retval = -EINVAL;
  628. goto out;
  629. }
  630. phys = strndup_user(p, 1024);
  631. if (IS_ERR(phys)) {
  632. retval = PTR_ERR(phys);
  633. goto out;
  634. }
  635. kfree(udev->dev->phys);
  636. udev->dev->phys = phys;
  637. goto out;
  638. case UI_BEGIN_FF_UPLOAD:
  639. retval = uinput_ff_upload_from_user(p, &ff_up);
  640. if (retval)
  641. goto out;
  642. req = uinput_request_find(udev, ff_up.request_id);
  643. if (!req || req->code != UI_FF_UPLOAD ||
  644. !req->u.upload.effect) {
  645. retval = -EINVAL;
  646. goto out;
  647. }
  648. ff_up.retval = 0;
  649. ff_up.effect = *req->u.upload.effect;
  650. if (req->u.upload.old)
  651. ff_up.old = *req->u.upload.old;
  652. else
  653. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  654. retval = uinput_ff_upload_to_user(p, &ff_up);
  655. goto out;
  656. case UI_BEGIN_FF_ERASE:
  657. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  658. retval = -EFAULT;
  659. goto out;
  660. }
  661. req = uinput_request_find(udev, ff_erase.request_id);
  662. if (!req || req->code != UI_FF_ERASE) {
  663. retval = -EINVAL;
  664. goto out;
  665. }
  666. ff_erase.retval = 0;
  667. ff_erase.effect_id = req->u.effect_id;
  668. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  669. retval = -EFAULT;
  670. goto out;
  671. }
  672. goto out;
  673. case UI_END_FF_UPLOAD:
  674. retval = uinput_ff_upload_from_user(p, &ff_up);
  675. if (retval)
  676. goto out;
  677. req = uinput_request_find(udev, ff_up.request_id);
  678. if (!req || req->code != UI_FF_UPLOAD ||
  679. !req->u.upload.effect) {
  680. retval = -EINVAL;
  681. goto out;
  682. }
  683. req->retval = ff_up.retval;
  684. uinput_request_done(udev, req);
  685. goto out;
  686. case UI_END_FF_ERASE:
  687. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  688. retval = -EFAULT;
  689. goto out;
  690. }
  691. req = uinput_request_find(udev, ff_erase.request_id);
  692. if (!req || req->code != UI_FF_ERASE) {
  693. retval = -EINVAL;
  694. goto out;
  695. }
  696. req->retval = ff_erase.retval;
  697. uinput_request_done(udev, req);
  698. goto out;
  699. }
  700. size = _IOC_SIZE(cmd);
  701. /* Now check variable-length commands */
  702. switch (cmd & ~IOCSIZE_MASK) {
  703. case UI_GET_SYSNAME(0):
  704. if (udev->state != UIST_CREATED) {
  705. retval = -ENOENT;
  706. goto out;
  707. }
  708. name = dev_name(&udev->dev->dev);
  709. retval = uinput_str_to_user(p, name, size);
  710. goto out;
  711. }
  712. retval = -EINVAL;
  713. out:
  714. mutex_unlock(&udev->mutex);
  715. return retval;
  716. }
  717. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  718. {
  719. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  720. }
  721. #ifdef CONFIG_COMPAT
  722. static long uinput_compat_ioctl(struct file *file,
  723. unsigned int cmd, unsigned long arg)
  724. {
  725. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  726. }
  727. #endif
  728. static const struct file_operations uinput_fops = {
  729. .owner = THIS_MODULE,
  730. .open = uinput_open,
  731. .release = uinput_release,
  732. .read = uinput_read,
  733. .write = uinput_write,
  734. .poll = uinput_poll,
  735. .unlocked_ioctl = uinput_ioctl,
  736. #ifdef CONFIG_COMPAT
  737. .compat_ioctl = uinput_compat_ioctl,
  738. #endif
  739. .llseek = no_llseek,
  740. };
  741. static struct miscdevice uinput_misc = {
  742. .fops = &uinput_fops,
  743. .minor = UINPUT_MINOR,
  744. .name = UINPUT_NAME,
  745. };
  746. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  747. MODULE_ALIAS("devname:" UINPUT_NAME);
  748. static int __init uinput_init(void)
  749. {
  750. return misc_register(&uinput_misc);
  751. }
  752. static void __exit uinput_exit(void)
  753. {
  754. misc_deregister(&uinput_misc);
  755. }
  756. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  757. MODULE_DESCRIPTION("User level driver support for input subsystem");
  758. MODULE_LICENSE("GPL");
  759. MODULE_VERSION("0.3");
  760. module_init(uinput_init);
  761. module_exit(uinput_exit);