uinput.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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_each_set_bit(cnt, dev->absbit, ABS_CNT) {
  266. int min, max;
  267. min = input_abs_get_min(dev, cnt);
  268. max = input_abs_get_max(dev, cnt);
  269. if ((min != 0 || max != 0) && max <= min) {
  270. printk(KERN_DEBUG
  271. "%s: invalid abs[%02x] min:%d max:%d\n",
  272. UINPUT_NAME, cnt,
  273. input_abs_get_min(dev, cnt),
  274. input_abs_get_max(dev, cnt));
  275. return -EINVAL;
  276. }
  277. if (input_abs_get_flat(dev, cnt) >
  278. input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
  279. printk(KERN_DEBUG
  280. "%s: abs_flat #%02x out of range: %d "
  281. "(min:%d/max:%d)\n",
  282. UINPUT_NAME, cnt,
  283. input_abs_get_flat(dev, cnt),
  284. input_abs_get_min(dev, cnt),
  285. input_abs_get_max(dev, cnt));
  286. return -EINVAL;
  287. }
  288. }
  289. if (test_bit(ABS_MT_SLOT, dev->absbit)) {
  290. nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
  291. input_mt_init_slots(dev, nslot, 0);
  292. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  293. input_set_events_per_packet(dev, 60);
  294. }
  295. return 0;
  296. }
  297. static int uinput_allocate_device(struct uinput_device *udev)
  298. {
  299. udev->dev = input_allocate_device();
  300. if (!udev->dev)
  301. return -ENOMEM;
  302. udev->dev->event = uinput_dev_event;
  303. input_set_drvdata(udev->dev, udev);
  304. return 0;
  305. }
  306. static int uinput_setup_device(struct uinput_device *udev,
  307. const char __user *buffer, size_t count)
  308. {
  309. struct uinput_user_dev *user_dev;
  310. struct input_dev *dev;
  311. int i;
  312. int retval;
  313. if (count != sizeof(struct uinput_user_dev))
  314. return -EINVAL;
  315. if (!udev->dev) {
  316. retval = uinput_allocate_device(udev);
  317. if (retval)
  318. return retval;
  319. }
  320. dev = udev->dev;
  321. user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
  322. if (IS_ERR(user_dev))
  323. return PTR_ERR(user_dev);
  324. udev->ff_effects_max = user_dev->ff_effects_max;
  325. /* Ensure name is filled in */
  326. if (!user_dev->name[0]) {
  327. retval = -EINVAL;
  328. goto exit;
  329. }
  330. kfree(dev->name);
  331. dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
  332. GFP_KERNEL);
  333. if (!dev->name) {
  334. retval = -ENOMEM;
  335. goto exit;
  336. }
  337. dev->id.bustype = user_dev->id.bustype;
  338. dev->id.vendor = user_dev->id.vendor;
  339. dev->id.product = user_dev->id.product;
  340. dev->id.version = user_dev->id.version;
  341. for (i = 0; i < ABS_CNT; i++) {
  342. input_abs_set_max(dev, i, user_dev->absmax[i]);
  343. input_abs_set_min(dev, i, user_dev->absmin[i]);
  344. input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
  345. input_abs_set_flat(dev, i, user_dev->absflat[i]);
  346. }
  347. retval = uinput_validate_absbits(dev);
  348. if (retval < 0)
  349. goto exit;
  350. udev->state = UIST_SETUP_COMPLETE;
  351. retval = count;
  352. exit:
  353. kfree(user_dev);
  354. return retval;
  355. }
  356. static ssize_t uinput_inject_events(struct uinput_device *udev,
  357. const char __user *buffer, size_t count)
  358. {
  359. struct input_event ev;
  360. size_t bytes = 0;
  361. if (count != 0 && count < input_event_size())
  362. return -EINVAL;
  363. while (bytes + input_event_size() <= count) {
  364. /*
  365. * Note that even if some events were fetched successfully
  366. * we are still going to return EFAULT instead of partial
  367. * count to let userspace know that it got it's buffers
  368. * all wrong.
  369. */
  370. if (input_event_from_user(buffer + bytes, &ev))
  371. return -EFAULT;
  372. input_event(udev->dev, ev.type, ev.code, ev.value);
  373. bytes += input_event_size();
  374. }
  375. return bytes;
  376. }
  377. static ssize_t uinput_write(struct file *file, const char __user *buffer,
  378. size_t count, loff_t *ppos)
  379. {
  380. struct uinput_device *udev = file->private_data;
  381. int retval;
  382. if (count == 0)
  383. return 0;
  384. retval = mutex_lock_interruptible(&udev->mutex);
  385. if (retval)
  386. return retval;
  387. retval = udev->state == UIST_CREATED ?
  388. uinput_inject_events(udev, buffer, count) :
  389. uinput_setup_device(udev, buffer, count);
  390. mutex_unlock(&udev->mutex);
  391. return retval;
  392. }
  393. static bool uinput_fetch_next_event(struct uinput_device *udev,
  394. struct input_event *event)
  395. {
  396. bool have_event;
  397. spin_lock_irq(&udev->dev->event_lock);
  398. have_event = udev->head != udev->tail;
  399. if (have_event) {
  400. *event = udev->buff[udev->tail];
  401. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  402. }
  403. spin_unlock_irq(&udev->dev->event_lock);
  404. return have_event;
  405. }
  406. static ssize_t uinput_events_to_user(struct uinput_device *udev,
  407. char __user *buffer, size_t count)
  408. {
  409. struct input_event event;
  410. size_t read = 0;
  411. while (read + input_event_size() <= count &&
  412. uinput_fetch_next_event(udev, &event)) {
  413. if (input_event_to_user(buffer + read, &event))
  414. return -EFAULT;
  415. read += input_event_size();
  416. }
  417. return read;
  418. }
  419. static ssize_t uinput_read(struct file *file, char __user *buffer,
  420. size_t count, loff_t *ppos)
  421. {
  422. struct uinput_device *udev = file->private_data;
  423. ssize_t retval;
  424. if (count != 0 && count < input_event_size())
  425. return -EINVAL;
  426. do {
  427. retval = mutex_lock_interruptible(&udev->mutex);
  428. if (retval)
  429. return retval;
  430. if (udev->state != UIST_CREATED)
  431. retval = -ENODEV;
  432. else if (udev->head == udev->tail &&
  433. (file->f_flags & O_NONBLOCK))
  434. retval = -EAGAIN;
  435. else
  436. retval = uinput_events_to_user(udev, buffer, count);
  437. mutex_unlock(&udev->mutex);
  438. if (retval || count == 0)
  439. break;
  440. if (!(file->f_flags & O_NONBLOCK))
  441. retval = wait_event_interruptible(udev->waitq,
  442. udev->head != udev->tail ||
  443. udev->state != UIST_CREATED);
  444. } while (retval == 0);
  445. return retval;
  446. }
  447. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  448. {
  449. struct uinput_device *udev = file->private_data;
  450. poll_wait(file, &udev->waitq, wait);
  451. if (udev->head != udev->tail)
  452. return POLLIN | POLLRDNORM;
  453. return 0;
  454. }
  455. static int uinput_release(struct inode *inode, struct file *file)
  456. {
  457. struct uinput_device *udev = file->private_data;
  458. uinput_destroy_device(udev);
  459. kfree(udev);
  460. return 0;
  461. }
  462. #ifdef CONFIG_COMPAT
  463. struct uinput_ff_upload_compat {
  464. __u32 request_id;
  465. __s32 retval;
  466. struct ff_effect_compat effect;
  467. struct ff_effect_compat old;
  468. };
  469. static int uinput_ff_upload_to_user(char __user *buffer,
  470. const struct uinput_ff_upload *ff_up)
  471. {
  472. if (INPUT_COMPAT_TEST) {
  473. struct uinput_ff_upload_compat ff_up_compat;
  474. ff_up_compat.request_id = ff_up->request_id;
  475. ff_up_compat.retval = ff_up->retval;
  476. /*
  477. * It so happens that the pointer that gives us the trouble
  478. * is the last field in the structure. Since we don't support
  479. * custom waveforms in uinput anyway we can just copy the whole
  480. * thing (to the compat size) and ignore the pointer.
  481. */
  482. memcpy(&ff_up_compat.effect, &ff_up->effect,
  483. sizeof(struct ff_effect_compat));
  484. memcpy(&ff_up_compat.old, &ff_up->old,
  485. sizeof(struct ff_effect_compat));
  486. if (copy_to_user(buffer, &ff_up_compat,
  487. sizeof(struct uinput_ff_upload_compat)))
  488. return -EFAULT;
  489. } else {
  490. if (copy_to_user(buffer, ff_up,
  491. sizeof(struct uinput_ff_upload)))
  492. return -EFAULT;
  493. }
  494. return 0;
  495. }
  496. static int uinput_ff_upload_from_user(const char __user *buffer,
  497. struct uinput_ff_upload *ff_up)
  498. {
  499. if (INPUT_COMPAT_TEST) {
  500. struct uinput_ff_upload_compat ff_up_compat;
  501. if (copy_from_user(&ff_up_compat, buffer,
  502. sizeof(struct uinput_ff_upload_compat)))
  503. return -EFAULT;
  504. ff_up->request_id = ff_up_compat.request_id;
  505. ff_up->retval = ff_up_compat.retval;
  506. memcpy(&ff_up->effect, &ff_up_compat.effect,
  507. sizeof(struct ff_effect_compat));
  508. memcpy(&ff_up->old, &ff_up_compat.old,
  509. sizeof(struct ff_effect_compat));
  510. } else {
  511. if (copy_from_user(ff_up, buffer,
  512. sizeof(struct uinput_ff_upload)))
  513. return -EFAULT;
  514. }
  515. return 0;
  516. }
  517. #else
  518. static int uinput_ff_upload_to_user(char __user *buffer,
  519. const struct uinput_ff_upload *ff_up)
  520. {
  521. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  522. return -EFAULT;
  523. return 0;
  524. }
  525. static int uinput_ff_upload_from_user(const char __user *buffer,
  526. struct uinput_ff_upload *ff_up)
  527. {
  528. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  529. return -EFAULT;
  530. return 0;
  531. }
  532. #endif
  533. #define uinput_set_bit(_arg, _bit, _max) \
  534. ({ \
  535. int __ret = 0; \
  536. if (udev->state == UIST_CREATED) \
  537. __ret = -EINVAL; \
  538. else if ((_arg) > (_max)) \
  539. __ret = -EINVAL; \
  540. else set_bit((_arg), udev->dev->_bit); \
  541. __ret; \
  542. })
  543. static int uinput_str_to_user(void __user *dest, const char *str,
  544. unsigned int maxlen)
  545. {
  546. char __user *p = dest;
  547. int len, ret;
  548. if (!str)
  549. return -ENOENT;
  550. if (maxlen == 0)
  551. return -EINVAL;
  552. len = strlen(str) + 1;
  553. if (len > maxlen)
  554. len = maxlen;
  555. ret = copy_to_user(p, str, len);
  556. if (ret)
  557. return -EFAULT;
  558. /* force terminating '\0' */
  559. ret = put_user(0, p + len - 1);
  560. return ret ? -EFAULT : len;
  561. }
  562. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  563. unsigned long arg, void __user *p)
  564. {
  565. int retval;
  566. struct uinput_device *udev = file->private_data;
  567. struct uinput_ff_upload ff_up;
  568. struct uinput_ff_erase ff_erase;
  569. struct uinput_request *req;
  570. char *phys;
  571. const char *name;
  572. unsigned int size;
  573. retval = mutex_lock_interruptible(&udev->mutex);
  574. if (retval)
  575. return retval;
  576. if (!udev->dev) {
  577. retval = uinput_allocate_device(udev);
  578. if (retval)
  579. goto out;
  580. }
  581. switch (cmd) {
  582. case UI_GET_VERSION:
  583. if (put_user(UINPUT_VERSION,
  584. (unsigned int __user *)p))
  585. retval = -EFAULT;
  586. goto out;
  587. case UI_DEV_CREATE:
  588. retval = uinput_create_device(udev);
  589. goto out;
  590. case UI_DEV_DESTROY:
  591. uinput_destroy_device(udev);
  592. goto out;
  593. case UI_SET_EVBIT:
  594. retval = uinput_set_bit(arg, evbit, EV_MAX);
  595. goto out;
  596. case UI_SET_KEYBIT:
  597. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  598. goto out;
  599. case UI_SET_RELBIT:
  600. retval = uinput_set_bit(arg, relbit, REL_MAX);
  601. goto out;
  602. case UI_SET_ABSBIT:
  603. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  604. goto out;
  605. case UI_SET_MSCBIT:
  606. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  607. goto out;
  608. case UI_SET_LEDBIT:
  609. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  610. goto out;
  611. case UI_SET_SNDBIT:
  612. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  613. goto out;
  614. case UI_SET_FFBIT:
  615. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  616. goto out;
  617. case UI_SET_SWBIT:
  618. retval = uinput_set_bit(arg, swbit, SW_MAX);
  619. goto out;
  620. case UI_SET_PROPBIT:
  621. retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
  622. goto out;
  623. case UI_SET_PHYS:
  624. if (udev->state == UIST_CREATED) {
  625. retval = -EINVAL;
  626. goto out;
  627. }
  628. phys = strndup_user(p, 1024);
  629. if (IS_ERR(phys)) {
  630. retval = PTR_ERR(phys);
  631. goto out;
  632. }
  633. kfree(udev->dev->phys);
  634. udev->dev->phys = phys;
  635. goto out;
  636. case UI_BEGIN_FF_UPLOAD:
  637. retval = uinput_ff_upload_from_user(p, &ff_up);
  638. if (retval)
  639. goto out;
  640. req = uinput_request_find(udev, ff_up.request_id);
  641. if (!req || req->code != UI_FF_UPLOAD ||
  642. !req->u.upload.effect) {
  643. retval = -EINVAL;
  644. goto out;
  645. }
  646. ff_up.retval = 0;
  647. ff_up.effect = *req->u.upload.effect;
  648. if (req->u.upload.old)
  649. ff_up.old = *req->u.upload.old;
  650. else
  651. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  652. retval = uinput_ff_upload_to_user(p, &ff_up);
  653. goto out;
  654. case UI_BEGIN_FF_ERASE:
  655. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  656. retval = -EFAULT;
  657. goto out;
  658. }
  659. req = uinput_request_find(udev, ff_erase.request_id);
  660. if (!req || req->code != UI_FF_ERASE) {
  661. retval = -EINVAL;
  662. goto out;
  663. }
  664. ff_erase.retval = 0;
  665. ff_erase.effect_id = req->u.effect_id;
  666. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  667. retval = -EFAULT;
  668. goto out;
  669. }
  670. goto out;
  671. case UI_END_FF_UPLOAD:
  672. retval = uinput_ff_upload_from_user(p, &ff_up);
  673. if (retval)
  674. goto out;
  675. req = uinput_request_find(udev, ff_up.request_id);
  676. if (!req || req->code != UI_FF_UPLOAD ||
  677. !req->u.upload.effect) {
  678. retval = -EINVAL;
  679. goto out;
  680. }
  681. req->retval = ff_up.retval;
  682. uinput_request_done(udev, req);
  683. goto out;
  684. case UI_END_FF_ERASE:
  685. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  686. retval = -EFAULT;
  687. goto out;
  688. }
  689. req = uinput_request_find(udev, ff_erase.request_id);
  690. if (!req || req->code != UI_FF_ERASE) {
  691. retval = -EINVAL;
  692. goto out;
  693. }
  694. req->retval = ff_erase.retval;
  695. uinput_request_done(udev, req);
  696. goto out;
  697. }
  698. size = _IOC_SIZE(cmd);
  699. /* Now check variable-length commands */
  700. switch (cmd & ~IOCSIZE_MASK) {
  701. case UI_GET_SYSNAME(0):
  702. if (udev->state != UIST_CREATED) {
  703. retval = -ENOENT;
  704. goto out;
  705. }
  706. name = dev_name(&udev->dev->dev);
  707. retval = uinput_str_to_user(p, name, size);
  708. goto out;
  709. }
  710. retval = -EINVAL;
  711. out:
  712. mutex_unlock(&udev->mutex);
  713. return retval;
  714. }
  715. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  716. {
  717. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  718. }
  719. #ifdef CONFIG_COMPAT
  720. #define UI_SET_PHYS_COMPAT _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
  721. static long uinput_compat_ioctl(struct file *file,
  722. unsigned int cmd, unsigned long arg)
  723. {
  724. if (cmd == UI_SET_PHYS_COMPAT)
  725. cmd = UI_SET_PHYS;
  726. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  727. }
  728. #endif
  729. static const struct file_operations uinput_fops = {
  730. .owner = THIS_MODULE,
  731. .open = uinput_open,
  732. .release = uinput_release,
  733. .read = uinput_read,
  734. .write = uinput_write,
  735. .poll = uinput_poll,
  736. .unlocked_ioctl = uinput_ioctl,
  737. #ifdef CONFIG_COMPAT
  738. .compat_ioctl = uinput_compat_ioctl,
  739. #endif
  740. .llseek = no_llseek,
  741. };
  742. static struct miscdevice uinput_misc = {
  743. .fops = &uinput_fops,
  744. .minor = UINPUT_MINOR,
  745. .name = UINPUT_NAME,
  746. };
  747. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  748. MODULE_ALIAS("devname:" UINPUT_NAME);
  749. static int __init uinput_init(void)
  750. {
  751. return misc_register(&uinput_misc);
  752. }
  753. static void __exit uinput_exit(void)
  754. {
  755. misc_deregister(&uinput_misc);
  756. }
  757. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  758. MODULE_DESCRIPTION("User level driver support for input subsystem");
  759. MODULE_LICENSE("GPL");
  760. MODULE_VERSION("0.3");
  761. module_init(uinput_init);
  762. module_exit(uinput_exit);