mac_hid.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * drivers/macintosh/mac_hid.c
  3. *
  4. * HID support stuff for Macintosh computers.
  5. *
  6. * Copyright (C) 2000 Franz Sirl.
  7. *
  8. * This file will soon be removed in favor of an uinput userspace tool.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/sysctl.h>
  13. #include <linux/input.h>
  14. #include <linux/module.h>
  15. #include <linux/kbd_kern.h>
  16. static struct input_dev *emumousebtn;
  17. static int emumousebtn_input_register(void);
  18. static int mouse_emulate_buttons;
  19. static int mouse_button2_keycode = KEY_RIGHTCTRL; /* right control key */
  20. static int mouse_button3_keycode = KEY_RIGHTALT; /* right option key */
  21. static int mouse_last_keycode;
  22. #if defined(CONFIG_SYSCTL)
  23. /* file(s) in /proc/sys/dev/mac_hid */
  24. static ctl_table mac_hid_files[] = {
  25. {
  26. .ctl_name = DEV_MAC_HID_MOUSE_BUTTON_EMULATION,
  27. .procname = "mouse_button_emulation",
  28. .data = &mouse_emulate_buttons,
  29. .maxlen = sizeof(int),
  30. .mode = 0644,
  31. .proc_handler = &proc_dointvec,
  32. },
  33. {
  34. .ctl_name = DEV_MAC_HID_MOUSE_BUTTON2_KEYCODE,
  35. .procname = "mouse_button2_keycode",
  36. .data = &mouse_button2_keycode,
  37. .maxlen = sizeof(int),
  38. .mode = 0644,
  39. .proc_handler = &proc_dointvec,
  40. },
  41. {
  42. .ctl_name = DEV_MAC_HID_MOUSE_BUTTON3_KEYCODE,
  43. .procname = "mouse_button3_keycode",
  44. .data = &mouse_button3_keycode,
  45. .maxlen = sizeof(int),
  46. .mode = 0644,
  47. .proc_handler = &proc_dointvec,
  48. },
  49. { .ctl_name = 0 }
  50. };
  51. /* dir in /proc/sys/dev */
  52. static ctl_table mac_hid_dir[] = {
  53. {
  54. .ctl_name = DEV_MAC_HID,
  55. .procname = "mac_hid",
  56. .maxlen = 0,
  57. .mode = 0555,
  58. .child = mac_hid_files,
  59. },
  60. { .ctl_name = 0 }
  61. };
  62. /* /proc/sys/dev itself, in case that is not there yet */
  63. static ctl_table mac_hid_root_dir[] = {
  64. {
  65. .ctl_name = CTL_DEV,
  66. .procname = "dev",
  67. .maxlen = 0,
  68. .mode = 0555,
  69. .child = mac_hid_dir,
  70. },
  71. { .ctl_name = 0 }
  72. };
  73. static struct ctl_table_header *mac_hid_sysctl_header;
  74. #endif /* endif CONFIG_SYSCTL */
  75. int mac_hid_mouse_emulate_buttons(int caller, unsigned int keycode, int down)
  76. {
  77. switch (caller) {
  78. case 1:
  79. /* Called from keyboard.c */
  80. if (mouse_emulate_buttons
  81. && (keycode == mouse_button2_keycode
  82. || keycode == mouse_button3_keycode)) {
  83. if (mouse_emulate_buttons == 1) {
  84. input_report_key(emumousebtn,
  85. keycode == mouse_button2_keycode ? BTN_MIDDLE : BTN_RIGHT,
  86. down);
  87. input_sync(emumousebtn);
  88. return 1;
  89. }
  90. mouse_last_keycode = down ? keycode : 0;
  91. }
  92. break;
  93. }
  94. return 0;
  95. }
  96. static struct lock_class_key emumousebtn_event_class;
  97. static struct lock_class_key emumousebtn_mutex_class;
  98. static int emumousebtn_input_register(void)
  99. {
  100. int ret;
  101. emumousebtn = input_allocate_device();
  102. if (!emumousebtn)
  103. return -ENOMEM;
  104. lockdep_set_class(&emumousebtn->event_lock, &emumousebtn_event_class);
  105. lockdep_set_class(&emumousebtn->mutex, &emumousebtn_mutex_class);
  106. emumousebtn->name = "Macintosh mouse button emulation";
  107. emumousebtn->id.bustype = BUS_ADB;
  108. emumousebtn->id.vendor = 0x0001;
  109. emumousebtn->id.product = 0x0001;
  110. emumousebtn->id.version = 0x0100;
  111. emumousebtn->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  112. emumousebtn->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  113. BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  114. emumousebtn->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  115. ret = input_register_device(emumousebtn);
  116. if (ret)
  117. input_free_device(emumousebtn);
  118. return ret;
  119. }
  120. static int __init mac_hid_init(void)
  121. {
  122. int err;
  123. err = emumousebtn_input_register();
  124. if (err)
  125. return err;
  126. #if defined(CONFIG_SYSCTL)
  127. mac_hid_sysctl_header = register_sysctl_table(mac_hid_root_dir);
  128. #endif /* CONFIG_SYSCTL */
  129. return 0;
  130. }
  131. device_initcall(mac_hid_init);