uinput.c 20 KB

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