32 #include <linux/poll.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
39 #include <linux/uinput.h>
41 #include "../input-compat.h"
43 static int uinput_dev_event(
struct input_dev *
dev,
85 if (
id >= UINPUT_NUM_REQUESTS)
91 static int uinput_request_reserve_slot(
struct uinput_device *udev,
96 uinput_request_alloc_id(udev, request));
123 init_completion(&request->
done);
141 error = uinput_request_reserve_slot(udev, request);
145 error = uinput_request_send(udev, request);
147 uinput_request_done(udev, request);
159 static void uinput_flush_requests(
struct uinput_device *udev)
170 uinput_request_done(udev, request);
177 static void uinput_dev_set_gain(
struct input_dev *
dev,
u16 gain)
182 static void uinput_dev_set_autocenter(
struct input_dev *
dev,
u16 magnitude)
189 return uinput_dev_event(dev,
EV_FF, effect_id, value);
192 static int uinput_dev_upload_effect(
struct input_dev *
dev,
214 return uinput_request_submit(udev, &request);
217 static int uinput_dev_erase_effect(
struct input_dev *dev,
int effect_id)
228 return uinput_request_submit(udev, &request);
231 static void uinput_destroy_device(
struct uinput_device *udev)
234 struct input_dev *dev = udev->
dev;
243 uinput_flush_requests(udev);
244 input_unregister_device(dev);
246 input_free_device(dev);
256 struct input_dev *dev = udev->
dev;
269 dev->ff->upload = uinput_dev_upload_effect;
270 dev->ff->erase = uinput_dev_erase_effect;
271 dev->ff->playback = uinput_dev_playback;
272 dev->ff->set_gain = uinput_dev_set_gain;
273 dev->ff->set_autocenter = uinput_dev_set_autocenter;
276 error = input_register_device(udev->
dev);
284 fail2: input_ff_destroy(dev);
285 fail1: uinput_destroy_device(udev);
309 static int uinput_validate_absbits(
struct input_dev *dev)
314 for (cnt = 0; cnt <
ABS_CNT; cnt++) {
319 min = input_abs_get_min(dev, cnt);
320 max = input_abs_get_max(dev, cnt);
322 if ((min != 0 || max != 0) && max <= min) {
324 "%s: invalid abs[%02x] min:%d max:%d\n",
326 input_abs_get_min(dev, cnt),
327 input_abs_get_max(dev, cnt));
332 if (input_abs_get_flat(dev, cnt) >
333 input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
335 "%s: abs_flat #%02x out of range: %d "
338 input_abs_get_flat(dev, cnt),
339 input_abs_get_min(dev, cnt),
340 input_abs_get_max(dev, cnt));
348 static int uinput_allocate_device(
struct uinput_device *udev)
350 udev->
dev = input_allocate_device();
354 udev->
dev->event = uinput_dev_event;
355 input_set_drvdata(udev->
dev, udev);
364 struct input_dev *
dev;
372 retval = uinput_allocate_device(udev);
380 if (IS_ERR(user_dev))
381 return PTR_ERR(user_dev);
386 if (!user_dev->
name[0]) {
399 dev->id.bustype = user_dev->
id.bustype;
400 dev->id.vendor = user_dev->
id.vendor;
401 dev->id.product = user_dev->
id.product;
402 dev->id.version = user_dev->
id.version;
404 for (i = 0; i <
ABS_CNT; i++) {
405 input_abs_set_max(dev, i, user_dev->
absmax[i]);
406 input_abs_set_min(dev, i, user_dev->
absmin[i]);
407 input_abs_set_fuzz(dev, i, user_dev->
absfuzz[i]);
408 input_abs_set_flat(dev, i, user_dev->
absflat[i]);
414 retval = uinput_validate_absbits(dev);
418 int nslot = input_abs_get_max(dev,
ABS_MT_SLOT) + 1;
421 input_set_events_per_packet(dev, 60);
434 const char __user *buffer,
size_t count)
438 if (count < input_event_size())
446 return input_event_size();
449 static ssize_t uinput_write(
struct file *file,
const char __user *buffer,
450 size_t count, loff_t *ppos)
463 uinput_inject_event(udev, buffer, count) :
464 uinput_setup_device(udev, buffer, count);
471 static bool uinput_fetch_next_event(
struct uinput_device *udev,
476 spin_lock_irq(&udev->
dev->event_lock);
478 have_event = udev->
head != udev->
tail;
484 spin_unlock_irq(&udev->
dev->event_lock);
490 char __user *buffer,
size_t count)
495 while (read + input_event_size() <= count &&
496 uinput_fetch_next_event(udev, &event)) {
501 read += input_event_size();
507 static ssize_t uinput_read(
struct file *file,
char __user *buffer,
508 size_t count, loff_t *ppos)
513 if (count != 0 && count < input_event_size())
523 else if (udev->
head == udev->
tail &&
527 retval = uinput_events_to_user(udev, buffer, count);
531 if (retval || count == 0)
538 }
while (retval == 0);
543 static unsigned int uinput_poll(
struct file *file,
poll_table *
wait)
547 poll_wait(file, &udev->
waitq, wait);
555 static int uinput_release(
struct inode *inode,
struct file *file)
559 uinput_destroy_device(udev);
566 struct uinput_ff_upload_compat {
569 struct ff_effect_compat effect;
570 struct ff_effect_compat old;
573 static int uinput_ff_upload_to_user(
char __user *buffer,
576 if (INPUT_COMPAT_TEST) {
577 struct uinput_ff_upload_compat ff_up_compat;
580 ff_up_compat.retval = ff_up->
retval;
588 sizeof(
struct ff_effect_compat));
590 sizeof(
struct ff_effect_compat));
593 sizeof(
struct uinput_ff_upload_compat)))
604 static int uinput_ff_upload_from_user(
const char __user *buffer,
607 if (INPUT_COMPAT_TEST) {
608 struct uinput_ff_upload_compat ff_up_compat;
611 sizeof(
struct uinput_ff_upload_compat)))
615 ff_up->
retval = ff_up_compat.retval;
617 sizeof(
struct ff_effect_compat));
619 sizeof(
struct ff_effect_compat));
632 static int uinput_ff_upload_to_user(
char __user *buffer,
641 static int uinput_ff_upload_from_user(
const char __user *buffer,
652 #define uinput_set_bit(_arg, _bit, _max) \
655 if (udev->state == UIST_CREATED) \
657 else if ((_arg) > (_max)) \
659 else set_bit((_arg), udev->dev->_bit); \
663 static long uinput_ioctl_handler(
struct file *file,
unsigned int cmd,
664 unsigned long arg,
void __user *
p)
678 retval = uinput_allocate_device(udev);
685 retval = uinput_create_device(udev);
689 uinput_destroy_device(udev);
740 retval = PTR_ERR(phys);
749 retval = uinput_ff_upload_from_user(p, &ff_up);
753 req = uinput_request_find(udev, ff_up.
request_id);
767 retval = uinput_ff_upload_to_user(p, &ff_up);
776 req = uinput_request_find(udev, ff_erase.
request_id);
792 retval = uinput_ff_upload_from_user(p, &ff_up);
796 req = uinput_request_find(udev, ff_up.
request_id);
804 uinput_request_done(udev, req);
813 req = uinput_request_find(udev, ff_erase.
request_id);
820 uinput_request_done(udev, req);
832 static long uinput_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
834 return uinput_ioctl_handler(file, cmd, arg, (
void __user *)arg);
838 static long uinput_compat_ioctl(
struct file *file,
839 unsigned int cmd,
unsigned long arg)
841 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
848 .release = uinput_release,
850 .write = uinput_write,
852 .unlocked_ioctl = uinput_ioctl,
854 .compat_ioctl = uinput_compat_ioctl,
860 .fops = &uinput_fops,
867 static int __init uinput_init(
void)
872 static void __exit uinput_exit(
void)