vkbd.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * vkbd.c
  3. */
  4. #include <linux/init.h>
  5. #include <linux/input.h>
  6. #include <linux/module.h>
  7. #include <linux/spinlock.h>
  8. #include "vinput.h"
  9. #define VINPUT_KBD "vkbd"
  10. #define VINPUT_RELEASE 0
  11. #define VINPUT_PRESS 1
  12. static unsigned short vkeymap[KEY_MAX];
  13. static int vinput_vkbd_init(struct vinput *vinput)
  14. {
  15. int i;
  16. /* Set up the input bitfield */
  17. vinput->input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  18. vinput->input->keycodesize = sizeof(unsigned short);
  19. vinput->input->keycodemax = KEY_MAX;
  20. vinput->input->keycode = vkeymap;
  21. for (i = 0; i < KEY_MAX; i++)
  22. set_bit(vkeymap[i], vinput->input->keybit);
  23. /* vinput will help us allocate new input device structure via
  24. * input_allocate_device(). So, we can register it straightforwardly.
  25. */
  26. return input_register_device(vinput->input);
  27. }
  28. static int vinput_vkbd_read(struct vinput *vinput, char *buff, int len)
  29. {
  30. spin_lock(&vinput->lock);
  31. len = snprintf(buff, len, "%+ld\n", vinput->last_entry);
  32. spin_unlock(&vinput->lock);
  33. return len;
  34. }
  35. static int vinput_vkbd_send(struct vinput *vinput, char *buff, int len)
  36. {
  37. int ret;
  38. long key = 0;
  39. short type = VINPUT_PRESS;
  40. /* Determine which event was received (press or release)
  41. * and store the state.
  42. */
  43. if (buff[0] == '+')
  44. ret = kstrtol(buff + 1, 10, &key);
  45. else
  46. ret = kstrtol(buff, 10, &key);
  47. if (ret)
  48. dev_err(&vinput->dev, "error during kstrtol: -%d\n", ret);
  49. spin_lock(&vinput->lock);
  50. vinput->last_entry = key;
  51. spin_unlock(&vinput->lock);
  52. if (key < 0) {
  53. type = VINPUT_RELEASE;
  54. key = -key;
  55. }
  56. dev_info(&vinput->dev, "Event %s code %ld\n",
  57. (type == VINPUT_RELEASE) ? "VINPUT_RELEASE" : "VINPUT_PRESS", key);
  58. /* Report the state received to input subsystem. */
  59. input_report_key(vinput->input, key, type);
  60. /* Tell input subsystem that it finished the report. */
  61. input_sync(vinput->input);
  62. return len;
  63. }
  64. static struct vinput_ops vkbd_ops = {
  65. .init = vinput_vkbd_init,
  66. .send = vinput_vkbd_send,
  67. .read = vinput_vkbd_read,
  68. };
  69. static struct vinput_device vkbd_dev = {
  70. .name = VINPUT_KBD,
  71. .ops = &vkbd_ops,
  72. };
  73. static int __init vkbd_init(void)
  74. {
  75. int i;
  76. for (i = 0; i < KEY_MAX; i++)
  77. vkeymap[i] = i;
  78. return vinput_register(&vkbd_dev);
  79. }
  80. static void __exit vkbd_end(void)
  81. {
  82. vinput_unregister(&vkbd_dev);
  83. }
  84. module_init(vkbd_init);
  85. module_exit(vkbd_end);
  86. MODULE_LICENSE("GPL");
  87. MODULE_DESCRIPTION("Emulate keyboard input events through /dev/vinput");