| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* drivers/macintosh/mac_hid.c |
| 4 |
* |
| 5 |
* HID support stuff for Macintosh computers. |
| 6 |
* |
| 7 |
* Copyright (C) 2000 Franz Sirl. |
| 8 |
* |
| 9 |
* This file will soon be removed in favor of an uinput userspace tool. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/init.h> |
| 13 |
#include <linux/proc_fs.h> |
| 14 |
#include <linux/sysctl.h> |
| 15 |
#include <linux/input.h> |
| 16 |
#include <linux/module.h> |
| 17 |
#include <linux/slab.h> |
| 18 |
|
| 19 |
MODULE_DESCRIPTION("Mouse button 2+3 emulation"); |
| 20 |
MODULE_LICENSE("GPL"); |
| 21 |
|
| 22 |
static int mouse_emulate_buttons; |
| 23 |
static int mouse_button2_keycode = KEY_RIGHTCTRL; /* right control key */ |
| 24 |
static int mouse_button3_keycode = KEY_RIGHTALT; /* right option key */ |
| 25 |
|
| 26 |
static struct input_dev *mac_hid_emumouse_dev; |
| 27 |
|
| 28 |
static DEFINE_MUTEX(mac_hid_emumouse_mutex); |
| 29 |
|
| 30 |
static int mac_hid_create_emumouse(void) |
| 31 |
{ |
| 32 |
static struct lock_class_key mac_hid_emumouse_dev_event_class; |
| 33 |
static struct lock_class_key mac_hid_emumouse_dev_mutex_class; |
| 34 |
int err; |
| 35 |
|
| 36 |
mac_hid_emumouse_dev = input_allocate_device(); |
| 37 |
if (!mac_hid_emumouse_dev) |
| 38 |
return -ENOMEM; |
| 39 |
|
| 40 |
lockdep_set_class(&mac_hid_emumouse_dev->event_lock, |
| 41 |
&mac_hid_emumouse_dev_event_class); |
| 42 |
lockdep_set_class(&mac_hid_emumouse_dev->mutex, |
| 43 |
&mac_hid_emumouse_dev_mutex_class); |
| 44 |
|
| 45 |
mac_hid_emumouse_dev->name = "Macintosh mouse button emulation"; |
| 46 |
mac_hid_emumouse_dev->id.bustype = BUS_ADB; |
| 47 |
mac_hid_emumouse_dev->id.vendor = 0x0001; |
| 48 |
mac_hid_emumouse_dev->id.product = 0x0001; |
| 49 |
mac_hid_emumouse_dev->id.version = 0x0100; |
| 50 |
|
| 51 |
mac_hid_emumouse_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 52 |
mac_hid_emumouse_dev->keybit[BIT_WORD(BTN_MOUSE)] = |
| 53 |
BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT); |
| 54 |
mac_hid_emumouse_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 55 |
|
| 56 |
err = input_register_device(mac_hid_emumouse_dev); |
| 57 |
if (err) { |
| 58 |
input_free_device(mac_hid_emumouse_dev); |
| 59 |
mac_hid_emumouse_dev = NULL; |
| 60 |
return err; |
| 61 |
} |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static void mac_hid_destroy_emumouse(void) |
| 67 |
{ |
| 68 |
input_unregister_device(mac_hid_emumouse_dev); |
| 69 |
mac_hid_emumouse_dev = NULL; |
| 70 |
} |
| 71 |
|
| 72 |
static bool mac_hid_emumouse_filter(struct input_handle *handle, |
| 73 |
unsigned int type, unsigned int code, |
| 74 |
int value) |
| 75 |
{ |
| 76 |
unsigned int btn; |
| 77 |
|
| 78 |
if (type != EV_KEY) |
| 79 |
return false; |
| 80 |
|
| 81 |
if (code == mouse_button2_keycode) |
| 82 |
btn = BTN_MIDDLE; |
| 83 |
else if (code == mouse_button3_keycode) |
| 84 |
btn = BTN_RIGHT; |
| 85 |
else |
| 86 |
return false; |
| 87 |
|
| 88 |
input_report_key(mac_hid_emumouse_dev, btn, value); |
| 89 |
input_sync(mac_hid_emumouse_dev); |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
static int mac_hid_emumouse_connect(struct input_handler *handler, |
| 95 |
struct input_dev *dev, |
| 96 |
const struct input_device_id *id) |
| 97 |
{ |
| 98 |
struct input_handle *handle; |
| 99 |
int error; |
| 100 |
|
| 101 |
/* Don't bind to ourselves */ |
| 102 |
if (dev == mac_hid_emumouse_dev) |
| 103 |
return -ENODEV; |
| 104 |
|
| 105 |
handle = kzalloc_obj(struct input_handle); |
| 106 |
if (!handle) |
| 107 |
return -ENOMEM; |
| 108 |
|
| 109 |
handle->dev = dev; |
| 110 |
handle->handler = handler; |
| 111 |
handle->name = "mac-button-emul"; |
| 112 |
|
| 113 |
error = input_register_handle(handle); |
| 114 |
if (error) { |
| 115 |
printk(KERN_ERR |
| 116 |
"mac_hid: Failed to register button emulation handle, " |
| 117 |
"error %d\n", error); |
| 118 |
goto err_free; |
| 119 |
} |
| 120 |
|
| 121 |
error = input_open_device(handle); |
| 122 |
if (error) { |
| 123 |
printk(KERN_ERR |
| 124 |
"mac_hid: Failed to open input device, error %d\n", |
| 125 |
error); |
| 126 |
goto err_unregister; |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
|
| 131 |
err_unregister: |
| 132 |
input_unregister_handle(handle); |
| 133 |
err_free: |
| 134 |
kfree(handle); |
| 135 |
return error; |
| 136 |
} |
| 137 |
|
| 138 |
static void mac_hid_emumouse_disconnect(struct input_handle *handle) |
| 139 |
{ |
| 140 |
input_close_device(handle); |
| 141 |
input_unregister_handle(handle); |
| 142 |
kfree(handle); |
| 143 |
} |
| 144 |
|
| 145 |
static const struct input_device_id mac_hid_emumouse_ids[] = { |
| 146 |
{ |
| 147 |
.flags = INPUT_DEVICE_ID_MATCH_EVBIT, |
| 148 |
.evbit = { BIT_MASK(EV_KEY) }, |
| 149 |
}, |
| 150 |
{ }, |
| 151 |
}; |
| 152 |
|
| 153 |
MODULE_DEVICE_TABLE(input, mac_hid_emumouse_ids); |
| 154 |
|
| 155 |
static struct input_handler mac_hid_emumouse_handler = { |
| 156 |
.filter = mac_hid_emumouse_filter, |
| 157 |
.connect = mac_hid_emumouse_connect, |
| 158 |
.disconnect = mac_hid_emumouse_disconnect, |
| 159 |
.name = "mac-button-emul", |
| 160 |
.id_table = mac_hid_emumouse_ids, |
| 161 |
}; |
| 162 |
|
| 163 |
static int mac_hid_start_emulation(void) |
| 164 |
{ |
| 165 |
int err; |
| 166 |
|
| 167 |
err = mac_hid_create_emumouse(); |
| 168 |
if (err) |
| 169 |
return err; |
| 170 |
|
| 171 |
err = input_register_handler(&mac_hid_emumouse_handler); |
| 172 |
if (err) { |
| 173 |
mac_hid_destroy_emumouse(); |
| 174 |
return err; |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static void mac_hid_stop_emulation(void) |
| 181 |
{ |
| 182 |
input_unregister_handler(&mac_hid_emumouse_handler); |
| 183 |
mac_hid_destroy_emumouse(); |
| 184 |
} |
| 185 |
|
| 186 |
static int mac_hid_toggle_emumouse(const struct ctl_table *table, int write, |
| 187 |
void *buffer, size_t *lenp, loff_t *ppos) |
| 188 |
{ |
| 189 |
int *valp = table->data; |
| 190 |
int old_val; |
| 191 |
int rc; |
| 192 |
|
| 193 |
rc = mutex_lock_killable(&mac_hid_emumouse_mutex); |
| 194 |
if (rc) |
| 195 |
return rc; |
| 196 |
|
| 197 |
old_val = *valp; |
| 198 |
rc = proc_dointvec(table, write, buffer, lenp, ppos); |
| 199 |
|
| 200 |
if (rc == 0 && write && *valp != old_val) { |
| 201 |
if (*valp == 1) |
| 202 |
rc = mac_hid_start_emulation(); |
| 203 |
else if (*valp == 0) |
| 204 |
mac_hid_stop_emulation(); |
| 205 |
else |
| 206 |
rc = -EINVAL; |
| 207 |
} |
| 208 |
|
| 209 |
/* Restore the old value in case of error */ |
| 210 |
if (rc) |
| 211 |
*valp = old_val; |
| 212 |
|
| 213 |
mutex_unlock(&mac_hid_emumouse_mutex); |
| 214 |
|
| 215 |
return rc; |
| 216 |
} |
| 217 |
|
| 218 |
/* file(s) in /proc/sys/dev/mac_hid */ |
| 219 |
static const struct ctl_table mac_hid_files[] = { |
| 220 |
{ |
| 221 |
.procname = "mouse_button_emulation", |
| 222 |
.data = &mouse_emulate_buttons, |
| 223 |
.maxlen = sizeof(int), |
| 224 |
.mode = 0644, |
| 225 |
.proc_handler = mac_hid_toggle_emumouse, |
| 226 |
}, |
| 227 |
{ |
| 228 |
.procname = "mouse_button2_keycode", |
| 229 |
.data = &mouse_button2_keycode, |
| 230 |
.maxlen = sizeof(int), |
| 231 |
.mode = 0644, |
| 232 |
.proc_handler = proc_dointvec, |
| 233 |
}, |
| 234 |
{ |
| 235 |
.procname = "mouse_button3_keycode", |
| 236 |
.data = &mouse_button3_keycode, |
| 237 |
.maxlen = sizeof(int), |
| 238 |
.mode = 0644, |
| 239 |
.proc_handler = proc_dointvec, |
| 240 |
}, |
| 241 |
}; |
| 242 |
|
| 243 |
static struct ctl_table_header *mac_hid_sysctl_header; |
| 244 |
|
| 245 |
static int __init mac_hid_init(void) |
| 246 |
{ |
| 247 |
mac_hid_sysctl_header = register_sysctl("dev/mac_hid", mac_hid_files); |
| 248 |
if (!mac_hid_sysctl_header) |
| 249 |
return -ENOMEM; |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
module_init(mac_hid_init); |
| 254 |
|
| 255 |
static void __exit mac_hid_exit(void) |
| 256 |
{ |
| 257 |
unregister_sysctl_table(mac_hid_sysctl_header); |
| 258 |
|
| 259 |
if (mouse_emulate_buttons) |
| 260 |
mac_hid_stop_emulation(); |
| 261 |
} |
| 262 |
module_exit(mac_hid_exit); |