Blame view

drivers/macintosh/mac_hid.c 6.05 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /*
   * drivers/macintosh/mac_hid.c
   *
   * HID support stuff for Macintosh computers.
   *
   * Copyright (C) 2000 Franz Sirl.
   *
   * This file will soon be removed in favor of an uinput userspace tool.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
  #include <linux/init.h>
  #include <linux/proc_fs.h>
  #include <linux/sysctl.h>
  #include <linux/input.h>
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
15
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16

429722e19   Dmitry Torokhov   Input: Mac button...
17
  MODULE_LICENSE("GPL");
872758563   Olaf Hering   [POWERPC] move va...
18
  static int mouse_emulate_buttons;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
  static int mouse_button2_keycode = KEY_RIGHTCTRL;	/* right control key */
  static int mouse_button3_keycode = KEY_RIGHTALT;	/* right option key */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

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

429722e19   Dmitry Torokhov   Input: Mac button...
268
269
270
271
272
273
274
275
  static void __exit mac_hid_exit(void)
  {
  	unregister_sysctl_table(mac_hid_sysctl_header);
  
  	if (mouse_emulate_buttons)
  		mac_hid_stop_emulation();
  }
  module_exit(mac_hid_exit);