Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/proc_fs.h> |
| 13 | #include <linux/sysctl.h> |
| 14 | #include <linux/input.h> |
| 15 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Dmitry Torokhov | 429722e | 2010-01-30 01:44:20 -0800 | [diff] [blame] | 18 | MODULE_LICENSE("GPL"); |
| 19 | |
Olaf Hering | 8727585 | 2007-02-10 21:35:12 +0100 | [diff] [blame] | 20 | static int mouse_emulate_buttons; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | static int mouse_button2_keycode = KEY_RIGHTCTRL; /* right control key */ |
| 22 | static int mouse_button3_keycode = KEY_RIGHTALT; /* right option key */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Dmitry Torokhov | 99b089c | 2010-01-30 00:53:29 -0800 | [diff] [blame] | 24 | static struct input_dev *mac_hid_emumouse_dev; |
| 25 | |
| 26 | static int mac_hid_create_emumouse(void) |
| 27 | { |
| 28 | static struct lock_class_key mac_hid_emumouse_dev_event_class; |
| 29 | static struct lock_class_key mac_hid_emumouse_dev_mutex_class; |
| 30 | int err; |
| 31 | |
| 32 | mac_hid_emumouse_dev = input_allocate_device(); |
| 33 | if (!mac_hid_emumouse_dev) |
| 34 | return -ENOMEM; |
| 35 | |
| 36 | lockdep_set_class(&mac_hid_emumouse_dev->event_lock, |
| 37 | &mac_hid_emumouse_dev_event_class); |
| 38 | lockdep_set_class(&mac_hid_emumouse_dev->mutex, |
| 39 | &mac_hid_emumouse_dev_mutex_class); |
| 40 | |
| 41 | mac_hid_emumouse_dev->name = "Macintosh mouse button emulation"; |
| 42 | mac_hid_emumouse_dev->id.bustype = BUS_ADB; |
| 43 | mac_hid_emumouse_dev->id.vendor = 0x0001; |
| 44 | mac_hid_emumouse_dev->id.product = 0x0001; |
| 45 | mac_hid_emumouse_dev->id.version = 0x0100; |
| 46 | |
| 47 | mac_hid_emumouse_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 48 | mac_hid_emumouse_dev->keybit[BIT_WORD(BTN_MOUSE)] = |
| 49 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT); |
| 50 | mac_hid_emumouse_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 51 | |
| 52 | err = input_register_device(mac_hid_emumouse_dev); |
| 53 | if (err) { |
| 54 | input_free_device(mac_hid_emumouse_dev); |
| 55 | mac_hid_emumouse_dev = NULL; |
| 56 | return err; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void mac_hid_destroy_emumouse(void) |
| 63 | { |
| 64 | input_unregister_device(mac_hid_emumouse_dev); |
| 65 | mac_hid_emumouse_dev = NULL; |
| 66 | } |
| 67 | |
| 68 | static bool mac_hid_emumouse_filter(struct input_handle *handle, |
| 69 | unsigned int type, unsigned int code, |
| 70 | int value) |
| 71 | { |
| 72 | unsigned int btn; |
| 73 | |
| 74 | if (type != EV_KEY) |
| 75 | return false; |
| 76 | |
| 77 | if (code == mouse_button2_keycode) |
| 78 | btn = BTN_MIDDLE; |
| 79 | else if (code == mouse_button3_keycode) |
| 80 | btn = BTN_RIGHT; |
| 81 | else |
| 82 | return false; |
| 83 | |
| 84 | input_report_key(mac_hid_emumouse_dev, btn, value); |
| 85 | input_sync(mac_hid_emumouse_dev); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static int mac_hid_emumouse_connect(struct input_handler *handler, |
| 91 | struct input_dev *dev, |
| 92 | const struct input_device_id *id) |
| 93 | { |
| 94 | struct input_handle *handle; |
| 95 | int error; |
| 96 | |
| 97 | /* Don't bind to ourselves */ |
| 98 | if (dev == mac_hid_emumouse_dev) |
| 99 | return -ENODEV; |
| 100 | |
| 101 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); |
| 102 | if (!handle) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | handle->dev = dev; |
| 106 | handle->handler = handler; |
| 107 | handle->name = "mac-button-emul"; |
| 108 | |
| 109 | error = input_register_handle(handle); |
| 110 | if (error) { |
| 111 | printk(KERN_ERR |
| 112 | "mac_hid: Failed to register button emulation handle, " |
| 113 | "error %d\n", error); |
| 114 | goto err_free; |
| 115 | } |
| 116 | |
| 117 | error = input_open_device(handle); |
| 118 | if (error) { |
| 119 | printk(KERN_ERR |
| 120 | "mac_hid: Failed to open input device, error %d\n", |
| 121 | error); |
| 122 | goto err_unregister; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | |
| 127 | err_unregister: |
| 128 | input_unregister_handle(handle); |
| 129 | err_free: |
| 130 | kfree(handle); |
| 131 | return error; |
| 132 | } |
| 133 | |
| 134 | static void mac_hid_emumouse_disconnect(struct input_handle *handle) |
| 135 | { |
| 136 | input_close_device(handle); |
| 137 | input_unregister_handle(handle); |
| 138 | kfree(handle); |
| 139 | } |
| 140 | |
| 141 | static const struct input_device_id mac_hid_emumouse_ids[] = { |
| 142 | { |
| 143 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT, |
| 144 | .evbit = { BIT_MASK(EV_KEY) }, |
| 145 | }, |
| 146 | { }, |
| 147 | }; |
| 148 | |
| 149 | MODULE_DEVICE_TABLE(input, mac_hid_emumouse_ids); |
| 150 | |
| 151 | static struct input_handler mac_hid_emumouse_handler = { |
| 152 | .filter = mac_hid_emumouse_filter, |
| 153 | .connect = mac_hid_emumouse_connect, |
| 154 | .disconnect = mac_hid_emumouse_disconnect, |
| 155 | .name = "mac-button-emul", |
| 156 | .id_table = mac_hid_emumouse_ids, |
| 157 | }; |
| 158 | |
| 159 | static int mac_hid_start_emulation(void) |
| 160 | { |
| 161 | int err; |
| 162 | |
| 163 | err = mac_hid_create_emumouse(); |
| 164 | if (err) |
| 165 | return err; |
| 166 | |
| 167 | err = input_register_handler(&mac_hid_emumouse_handler); |
| 168 | if (err) { |
| 169 | mac_hid_destroy_emumouse(); |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static void mac_hid_stop_emulation(void) |
| 177 | { |
| 178 | input_unregister_handler(&mac_hid_emumouse_handler); |
| 179 | mac_hid_destroy_emumouse(); |
| 180 | } |
| 181 | |
| 182 | static int mac_hid_toggle_emumouse(ctl_table *table, int write, |
| 183 | void __user *buffer, size_t *lenp, |
| 184 | loff_t *ppos) |
| 185 | { |
| 186 | int *valp = table->data; |
| 187 | int old_val = *valp; |
| 188 | int rc; |
| 189 | |
| 190 | rc = proc_dointvec(table, write, buffer, lenp, ppos); |
| 191 | |
| 192 | if (rc == 0 && write && *valp != old_val) { |
| 193 | if (*valp == 1) |
| 194 | rc = mac_hid_start_emulation(); |
| 195 | else if (*valp == 0) |
| 196 | mac_hid_stop_emulation(); |
| 197 | else |
| 198 | rc = -EINVAL; |
| 199 | } |
| 200 | |
| 201 | /* Restore the old value in case of error */ |
| 202 | if (rc) |
| 203 | *valp = old_val; |
| 204 | |
| 205 | return rc; |
| 206 | } |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | /* file(s) in /proc/sys/dev/mac_hid */ |
Adrian Bunk | f3e5d2b | 2007-03-08 09:41:07 +1100 | [diff] [blame] | 209 | static ctl_table mac_hid_files[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | .procname = "mouse_button_emulation", |
| 212 | .data = &mouse_emulate_buttons, |
| 213 | .maxlen = sizeof(int), |
| 214 | .mode = 0644, |
Dmitry Torokhov | 99b089c | 2010-01-30 00:53:29 -0800 | [diff] [blame] | 215 | .proc_handler = mac_hid_toggle_emumouse, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | }, |
| 217 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | .procname = "mouse_button2_keycode", |
| 219 | .data = &mouse_button2_keycode, |
| 220 | .maxlen = sizeof(int), |
| 221 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 222 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | }, |
| 224 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | .procname = "mouse_button3_keycode", |
| 226 | .data = &mouse_button3_keycode, |
| 227 | .maxlen = sizeof(int), |
| 228 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 229 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 231 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | /* dir in /proc/sys/dev */ |
Adrian Bunk | f3e5d2b | 2007-03-08 09:41:07 +1100 | [diff] [blame] | 235 | static ctl_table mac_hid_dir[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | .procname = "mac_hid", |
| 238 | .maxlen = 0, |
| 239 | .mode = 0555, |
| 240 | .child = mac_hid_files, |
| 241 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 242 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | /* /proc/sys/dev itself, in case that is not there yet */ |
Adrian Bunk | f3e5d2b | 2007-03-08 09:41:07 +1100 | [diff] [blame] | 246 | static ctl_table mac_hid_root_dir[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | .procname = "dev", |
| 249 | .maxlen = 0, |
| 250 | .mode = 0555, |
| 251 | .child = mac_hid_dir, |
| 252 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 253 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | static struct ctl_table_header *mac_hid_sysctl_header; |
| 257 | |
Adrian Bunk | f3e5d2b | 2007-03-08 09:41:07 +1100 | [diff] [blame] | 258 | static int __init mac_hid_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 260 | mac_hid_sysctl_header = register_sysctl_table(mac_hid_root_dir); |
Dmitry Torokhov | 99b089c | 2010-01-30 00:53:29 -0800 | [diff] [blame] | 261 | if (!mac_hid_sysctl_header) |
| 262 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | return 0; |
| 265 | } |
Dmitry Torokhov | 429722e | 2010-01-30 01:44:20 -0800 | [diff] [blame] | 266 | module_init(mac_hid_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Dmitry Torokhov | 429722e | 2010-01-30 01:44:20 -0800 | [diff] [blame] | 268 | static void __exit mac_hid_exit(void) |
| 269 | { |
| 270 | unregister_sysctl_table(mac_hid_sysctl_header); |
| 271 | |
| 272 | if (mouse_emulate_buttons) |
| 273 | mac_hid_stop_emulation(); |
| 274 | } |
| 275 | module_exit(mac_hid_exit); |