xref: /linux/drivers/hid/hid-apple.c (revision 9f7861c56b51b84d30114e7fea9d744a9d5ba9b7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  USB HID quirks support for Linux
4  *
5  *  Copyright (c) 1999 Andreas Gal
6  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
8  *  Copyright (c) 2006-2007 Jiri Kosina
9  *  Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
10  *  Copyright (c) 2019 Paul Pawlowski <paul@mrarm.io>
11  */
12 
13 /*
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/device.h>
19 #include <linux/hid.h>
20 #include <linux/jiffies.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/leds.h>
26 
27 #include "hid-ids.h"
28 
29 #define APPLE_RDESC_JIS		BIT(0)
30 #define APPLE_IGNORE_MOUSE	BIT(1)
31 #define APPLE_HAS_FN		BIT(2)
32 /* BIT(3) reserved, was: APPLE_HIDDEV */
33 #define APPLE_ISO_TILDE_QUIRK	BIT(4)
34 #define APPLE_MIGHTYMOUSE	BIT(5)
35 #define APPLE_INVERT_HWHEEL	BIT(6)
36 /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
37 #define APPLE_NUMLOCK_EMULATION	BIT(8)
38 #define APPLE_RDESC_BATTERY	BIT(9)
39 #define APPLE_BACKLIGHT_CTL	BIT(10)
40 #define APPLE_IS_NON_APPLE	BIT(11)
41 
42 #define APPLE_FLAG_FKEY		0x01
43 
44 #define HID_COUNTRY_INTERNATIONAL_ISO	13
45 #define APPLE_BATTERY_TIMEOUT_MS	60000
46 
47 static unsigned int fnmode = 3;
48 module_param(fnmode, uint, 0644);
49 MODULE_PARM_DESC(fnmode, "Mode of fn key on Apple keyboards (0 = disabled, "
50 		"1 = fkeyslast, 2 = fkeysfirst, [3] = auto)");
51 
52 static int iso_layout = -1;
53 module_param(iso_layout, int, 0644);
54 MODULE_PARM_DESC(iso_layout, "Swap the backtick/tilde and greater-than/less-than keys. "
55 		"([-1] = auto, 0 = disabled, 1 = enabled)");
56 
57 static unsigned int swap_opt_cmd;
58 module_param(swap_opt_cmd, uint, 0644);
59 MODULE_PARM_DESC(swap_opt_cmd, "Swap the Option (\"Alt\") and Command (\"Flag\") keys. "
60 		"(For people who want to keep Windows PC keyboard muscle memory. "
61 		"[0] = as-is, Mac layout. 1 = swapped, Windows layout., 2 = swapped, Swap only left side)");
62 
63 static unsigned int swap_ctrl_cmd;
64 module_param(swap_ctrl_cmd, uint, 0644);
65 MODULE_PARM_DESC(swap_ctrl_cmd, "Swap the Control (\"Ctrl\") and Command (\"Flag\") keys. "
66 		"(For people who are used to Mac shortcuts involving Command instead of Control. "
67 		"[0] = No change. 1 = Swapped.)");
68 
69 static unsigned int swap_fn_leftctrl;
70 module_param(swap_fn_leftctrl, uint, 0644);
71 MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
72 		"(For people who want to keep PC keyboard muscle memory. "
73 		"[0] = as-is, Mac layout, 1 = swapped, PC layout)");
74 
75 struct apple_non_apple_keyboard {
76 	char *name;
77 };
78 
79 struct apple_sc_backlight {
80 	struct led_classdev cdev;
81 	struct hid_device *hdev;
82 	unsigned short backlight_off, backlight_on_min, backlight_on_max;
83 };
84 
85 struct apple_sc {
86 	struct hid_device *hdev;
87 	unsigned long quirks;
88 	unsigned int fn_on;
89 	unsigned int fn_found;
90 	DECLARE_BITMAP(pressed_numlock, KEY_CNT);
91 	struct timer_list battery_timer;
92 	struct apple_sc_backlight *backlight;
93 };
94 
95 struct apple_key_translation {
96 	u16 from;
97 	u16 to;
98 	u8 flags;
99 };
100 
101 static const struct apple_key_translation magic_keyboard_alu_fn_keys[] = {
102 	{ KEY_BACKSPACE, KEY_DELETE },
103 	{ KEY_ENTER,	KEY_INSERT },
104 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
105 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
106 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
107 	{ KEY_F4,	KEY_DASHBOARD,      APPLE_FLAG_FKEY },
108 	{ KEY_F6,	KEY_NUMLOCK,        APPLE_FLAG_FKEY },
109 	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
110 	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
111 	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
112 	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
113 	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
114 	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
115 	{ KEY_UP,	KEY_PAGEUP },
116 	{ KEY_DOWN,	KEY_PAGEDOWN },
117 	{ KEY_LEFT,	KEY_HOME },
118 	{ KEY_RIGHT,	KEY_END },
119 	{ }
120 };
121 
122 static const struct apple_key_translation magic_keyboard_2015_fn_keys[] = {
123 	{ KEY_BACKSPACE, KEY_DELETE },
124 	{ KEY_ENTER,	KEY_INSERT },
125 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
126 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
127 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
128 	{ KEY_F4,	KEY_DASHBOARD,      APPLE_FLAG_FKEY },
129 	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
130 	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
131 	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
132 	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
133 	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
134 	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
135 	{ KEY_UP,	KEY_PAGEUP },
136 	{ KEY_DOWN,	KEY_PAGEDOWN },
137 	{ KEY_LEFT,	KEY_HOME },
138 	{ KEY_RIGHT,	KEY_END },
139 	{ }
140 };
141 
142 struct apple_backlight_config_report {
143 	u8 report_id;
144 	u8 version;
145 	u16 backlight_off, backlight_on_min, backlight_on_max;
146 };
147 
148 struct apple_backlight_set_report {
149 	u8 report_id;
150 	u8 version;
151 	u16 backlight;
152 	u16 rate;
153 };
154 
155 
156 static const struct apple_key_translation apple2021_fn_keys[] = {
157 	{ KEY_BACKSPACE, KEY_DELETE },
158 	{ KEY_ENTER,	KEY_INSERT },
159 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
160 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
161 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
162 	{ KEY_F4,	KEY_SEARCH,         APPLE_FLAG_FKEY },
163 	{ KEY_F5,	KEY_MICMUTE,        APPLE_FLAG_FKEY },
164 	{ KEY_F6,	KEY_SLEEP,          APPLE_FLAG_FKEY },
165 	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
166 	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
167 	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
168 	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
169 	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
170 	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
171 	{ KEY_UP,	KEY_PAGEUP },
172 	{ KEY_DOWN,	KEY_PAGEDOWN },
173 	{ KEY_LEFT,	KEY_HOME },
174 	{ KEY_RIGHT,	KEY_END },
175 	{ }
176 };
177 
178 static const struct apple_key_translation macbookair_fn_keys[] = {
179 	{ KEY_BACKSPACE, KEY_DELETE },
180 	{ KEY_ENTER,	KEY_INSERT },
181 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
182 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
183 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
184 	{ KEY_F4,	KEY_DASHBOARD,      APPLE_FLAG_FKEY },
185 	{ KEY_F6,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
186 	{ KEY_F7,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
187 	{ KEY_F8,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
188 	{ KEY_F9,	KEY_MUTE,           APPLE_FLAG_FKEY },
189 	{ KEY_F10,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
190 	{ KEY_F11,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
191 	{ KEY_F12,	KEY_EJECTCD,        APPLE_FLAG_FKEY },
192 	{ KEY_UP,	KEY_PAGEUP },
193 	{ KEY_DOWN,	KEY_PAGEDOWN },
194 	{ KEY_LEFT,	KEY_HOME },
195 	{ KEY_RIGHT,	KEY_END },
196 	{ }
197 };
198 
199 static const struct apple_key_translation macbookpro_no_esc_fn_keys[] = {
200 	{ KEY_BACKSPACE, KEY_DELETE },
201 	{ KEY_ENTER,	KEY_INSERT },
202 	{ KEY_GRAVE,	KEY_ESC },
203 	{ KEY_1,	KEY_F1 },
204 	{ KEY_2,	KEY_F2 },
205 	{ KEY_3,	KEY_F3 },
206 	{ KEY_4,	KEY_F4 },
207 	{ KEY_5,	KEY_F5 },
208 	{ KEY_6,	KEY_F6 },
209 	{ KEY_7,	KEY_F7 },
210 	{ KEY_8,	KEY_F8 },
211 	{ KEY_9,	KEY_F9 },
212 	{ KEY_0,	KEY_F10 },
213 	{ KEY_MINUS,	KEY_F11 },
214 	{ KEY_EQUAL,	KEY_F12 },
215 	{ KEY_UP,	KEY_PAGEUP },
216 	{ KEY_DOWN,	KEY_PAGEDOWN },
217 	{ KEY_LEFT,	KEY_HOME },
218 	{ KEY_RIGHT,	KEY_END },
219 	{ }
220 };
221 
222 static const struct apple_key_translation macbookpro_dedicated_esc_fn_keys[] = {
223 	{ KEY_BACKSPACE, KEY_DELETE },
224 	{ KEY_ENTER,	KEY_INSERT },
225 	{ KEY_1,	KEY_F1 },
226 	{ KEY_2,	KEY_F2 },
227 	{ KEY_3,	KEY_F3 },
228 	{ KEY_4,	KEY_F4 },
229 	{ KEY_5,	KEY_F5 },
230 	{ KEY_6,	KEY_F6 },
231 	{ KEY_7,	KEY_F7 },
232 	{ KEY_8,	KEY_F8 },
233 	{ KEY_9,	KEY_F9 },
234 	{ KEY_0,	KEY_F10 },
235 	{ KEY_MINUS,	KEY_F11 },
236 	{ KEY_EQUAL,	KEY_F12 },
237 	{ KEY_UP,	KEY_PAGEUP },
238 	{ KEY_DOWN,	KEY_PAGEDOWN },
239 	{ KEY_LEFT,	KEY_HOME },
240 	{ KEY_RIGHT,	KEY_END },
241 	{ }
242 };
243 
244 static const struct apple_key_translation apple_fn_keys[] = {
245 	{ KEY_BACKSPACE, KEY_DELETE },
246 	{ KEY_ENTER,	KEY_INSERT },
247 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
248 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
249 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
250 	{ KEY_F4,	KEY_DASHBOARD,      APPLE_FLAG_FKEY },
251 	{ KEY_F5,	KEY_KBDILLUMDOWN,   APPLE_FLAG_FKEY },
252 	{ KEY_F6,	KEY_KBDILLUMUP,     APPLE_FLAG_FKEY },
253 	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
254 	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
255 	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
256 	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
257 	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
258 	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
259 	{ KEY_UP,	KEY_PAGEUP },
260 	{ KEY_DOWN,	KEY_PAGEDOWN },
261 	{ KEY_LEFT,	KEY_HOME },
262 	{ KEY_RIGHT,	KEY_END },
263 	{ }
264 };
265 
266 static const struct apple_key_translation powerbook_fn_keys[] = {
267 	{ KEY_BACKSPACE, KEY_DELETE },
268 	{ KEY_F1,	KEY_BRIGHTNESSDOWN,     APPLE_FLAG_FKEY },
269 	{ KEY_F2,	KEY_BRIGHTNESSUP,       APPLE_FLAG_FKEY },
270 	{ KEY_F3,	KEY_MUTE,               APPLE_FLAG_FKEY },
271 	{ KEY_F4,	KEY_VOLUMEDOWN,         APPLE_FLAG_FKEY },
272 	{ KEY_F5,	KEY_VOLUMEUP,           APPLE_FLAG_FKEY },
273 	{ KEY_F6,	KEY_NUMLOCK,            APPLE_FLAG_FKEY },
274 	{ KEY_F7,	KEY_SWITCHVIDEOMODE,    APPLE_FLAG_FKEY },
275 	{ KEY_F8,	KEY_KBDILLUMTOGGLE,     APPLE_FLAG_FKEY },
276 	{ KEY_F9,	KEY_KBDILLUMDOWN,       APPLE_FLAG_FKEY },
277 	{ KEY_F10,	KEY_KBDILLUMUP,         APPLE_FLAG_FKEY },
278 	{ KEY_UP,	KEY_PAGEUP },
279 	{ KEY_DOWN,	KEY_PAGEDOWN },
280 	{ KEY_LEFT,	KEY_HOME },
281 	{ KEY_RIGHT,	KEY_END },
282 	{ }
283 };
284 
285 static const struct apple_key_translation powerbook_numlock_keys[] = {
286 	{ KEY_J,	KEY_KP1 },
287 	{ KEY_K,	KEY_KP2 },
288 	{ KEY_L,	KEY_KP3 },
289 	{ KEY_U,	KEY_KP4 },
290 	{ KEY_I,	KEY_KP5 },
291 	{ KEY_O,	KEY_KP6 },
292 	{ KEY_7,	KEY_KP7 },
293 	{ KEY_8,	KEY_KP8 },
294 	{ KEY_9,	KEY_KP9 },
295 	{ KEY_M,	KEY_KP0 },
296 	{ KEY_DOT,	KEY_KPDOT },
297 	{ KEY_SLASH,	KEY_KPPLUS },
298 	{ KEY_SEMICOLON, KEY_KPMINUS },
299 	{ KEY_P,	KEY_KPASTERISK },
300 	{ KEY_MINUS,	KEY_KPEQUAL },
301 	{ KEY_0,	KEY_KPSLASH },
302 	{ KEY_F6,	KEY_NUMLOCK },
303 	{ KEY_KPENTER,	KEY_KPENTER },
304 	{ KEY_BACKSPACE, KEY_BACKSPACE },
305 	{ }
306 };
307 
308 static const struct apple_key_translation apple_iso_keyboard[] = {
309 	{ KEY_GRAVE,	KEY_102ND },
310 	{ KEY_102ND,	KEY_GRAVE },
311 	{ }
312 };
313 
314 static const struct apple_key_translation swapped_option_cmd_keys[] = {
315 	{ KEY_LEFTALT,	KEY_LEFTMETA },
316 	{ KEY_LEFTMETA,	KEY_LEFTALT },
317 	{ KEY_RIGHTALT,	KEY_RIGHTMETA },
318 	{ KEY_RIGHTMETA, KEY_RIGHTALT },
319 	{ }
320 };
321 
322 static const struct apple_key_translation swapped_option_cmd_left_keys[] = {
323 	{ KEY_LEFTALT,	KEY_LEFTMETA },
324 	{ KEY_LEFTMETA,	KEY_LEFTALT },
325 	{ }
326 };
327 
328 static const struct apple_key_translation swapped_ctrl_cmd_keys[] = {
329 	{ KEY_LEFTCTRL,	KEY_LEFTMETA },
330 	{ KEY_LEFTMETA,	KEY_LEFTCTRL },
331 	{ KEY_RIGHTCTRL, KEY_RIGHTMETA },
332 	{ KEY_RIGHTMETA, KEY_RIGHTCTRL },
333 	{ }
334 };
335 
336 static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
337 	{ KEY_FN, KEY_LEFTCTRL },
338 	{ KEY_LEFTCTRL, KEY_FN },
339 	{ }
340 };
341 
342 static const struct apple_non_apple_keyboard non_apple_keyboards[] = {
343 	{ "SONiX USB DEVICE" },
344 	{ "Keychron" },
345 	{ "AONE" },
346 	{ "GANSS" },
347 	{ "Hailuck" },
348 	{ "Jamesdonkey" },
349 	{ "A3R" },
350 	{ "hfd.cn" },
351 	{ "WKB603" },
352 };
353 
354 static bool apple_is_non_apple_keyboard(struct hid_device *hdev)
355 {
356 	int i;
357 
358 	for (i = 0; i < ARRAY_SIZE(non_apple_keyboards); i++) {
359 		char *non_apple = non_apple_keyboards[i].name;
360 
361 		if (strncmp(hdev->name, non_apple, strlen(non_apple)) == 0)
362 			return true;
363 	}
364 
365 	return false;
366 }
367 
368 static inline void apple_setup_key_translation(struct input_dev *input,
369 		const struct apple_key_translation *table)
370 {
371 	const struct apple_key_translation *trans;
372 
373 	for (trans = table; trans->from; trans++)
374 		set_bit(trans->to, input->keybit);
375 }
376 
377 static const struct apple_key_translation *apple_find_translation(
378 		const struct apple_key_translation *table, u16 from)
379 {
380 	const struct apple_key_translation *trans;
381 
382 	/* Look for the translation */
383 	for (trans = table; trans->from; trans++)
384 		if (trans->from == from)
385 			return trans;
386 
387 	return NULL;
388 }
389 
390 static void input_event_with_scancode(struct input_dev *input,
391 		__u8 type, __u16 code, unsigned int hid, __s32 value)
392 {
393 	if (type == EV_KEY &&
394 	    (!test_bit(code, input->key)) == value)
395 		input_event(input, EV_MSC, MSC_SCAN, hid);
396 	input_event(input, type, code, value);
397 }
398 
399 static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
400 		struct hid_usage *usage, __s32 value)
401 {
402 	struct apple_sc *asc = hid_get_drvdata(hid);
403 	const struct apple_key_translation *trans, *table;
404 	bool do_translate;
405 	u16 code = usage->code;
406 	unsigned int real_fnmode;
407 
408 	if (fnmode == 3) {
409 		real_fnmode = (asc->quirks & APPLE_IS_NON_APPLE) ? 2 : 1;
410 	} else {
411 		real_fnmode = fnmode;
412 	}
413 
414 	if (swap_fn_leftctrl) {
415 		trans = apple_find_translation(swapped_fn_leftctrl_keys, code);
416 
417 		if (trans)
418 			code = trans->to;
419 	}
420 
421 	if (iso_layout > 0 || (iso_layout < 0 && (asc->quirks & APPLE_ISO_TILDE_QUIRK) &&
422 			hid->country == HID_COUNTRY_INTERNATIONAL_ISO)) {
423 		trans = apple_find_translation(apple_iso_keyboard, code);
424 
425 		if (trans)
426 			code = trans->to;
427 	}
428 
429 	if (swap_opt_cmd) {
430 		if (swap_opt_cmd == 2)
431 			trans = apple_find_translation(swapped_option_cmd_left_keys, code);
432 		else
433 			trans = apple_find_translation(swapped_option_cmd_keys, code);
434 
435 		if (trans)
436 			code = trans->to;
437 	}
438 
439 	if (swap_ctrl_cmd) {
440 		trans = apple_find_translation(swapped_ctrl_cmd_keys, code);
441 
442 		if (trans)
443 			code = trans->to;
444 	}
445 
446 	if (code == KEY_FN)
447 		asc->fn_on = !!value;
448 
449 	if (real_fnmode) {
450 		if (hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI ||
451 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO ||
452 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS ||
453 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI ||
454 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO ||
455 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS ||
456 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI ||
457 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO ||
458 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS)
459 			table = magic_keyboard_alu_fn_keys;
460 		else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015 ||
461 			 hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015)
462 			table = magic_keyboard_2015_fn_keys;
463 		else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 ||
464 			 hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 ||
465 			 hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021)
466 			table = apple2021_fn_keys;
467 		else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 ||
468 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 ||
469 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213)
470 				table = macbookpro_no_esc_fn_keys;
471 		else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K ||
472 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 ||
473 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F)
474 				table = macbookpro_dedicated_esc_fn_keys;
475 		else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K ||
476 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K)
477 				table = apple_fn_keys;
478 		else if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
479 				hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
480 			table = macbookair_fn_keys;
481 		else if (hid->product < 0x21d || hid->product >= 0x300)
482 			table = powerbook_fn_keys;
483 		else
484 			table = apple_fn_keys;
485 
486 		trans = apple_find_translation(table, code);
487 
488 		if (trans) {
489 			bool from_is_set = test_bit(trans->from, input->key);
490 			bool to_is_set = test_bit(trans->to, input->key);
491 
492 			if (from_is_set)
493 				code = trans->from;
494 			else if (to_is_set)
495 				code = trans->to;
496 
497 			if (!(from_is_set || to_is_set)) {
498 				if (trans->flags & APPLE_FLAG_FKEY) {
499 					switch (real_fnmode) {
500 					case 1:
501 						do_translate = !asc->fn_on;
502 						break;
503 					case 2:
504 						do_translate = asc->fn_on;
505 						break;
506 					default:
507 						/* should never happen */
508 						do_translate = false;
509 					}
510 				} else {
511 					do_translate = asc->fn_on;
512 				}
513 
514 				if (do_translate)
515 					code = trans->to;
516 			}
517 		}
518 
519 		if (asc->quirks & APPLE_NUMLOCK_EMULATION &&
520 				(test_bit(code, asc->pressed_numlock) ||
521 				test_bit(LED_NUML, input->led))) {
522 			trans = apple_find_translation(powerbook_numlock_keys, code);
523 
524 			if (trans) {
525 				if (value)
526 					set_bit(code, asc->pressed_numlock);
527 				else
528 					clear_bit(code, asc->pressed_numlock);
529 
530 				code = trans->to;
531 			}
532 		}
533 	}
534 
535 	if (usage->code != code) {
536 		input_event_with_scancode(input, usage->type, code, usage->hid, value);
537 
538 		return 1;
539 	}
540 
541 	return 0;
542 }
543 
544 static int apple_event(struct hid_device *hdev, struct hid_field *field,
545 		struct hid_usage *usage, __s32 value)
546 {
547 	struct apple_sc *asc = hid_get_drvdata(hdev);
548 
549 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
550 			!usage->type)
551 		return 0;
552 
553 	if ((asc->quirks & APPLE_INVERT_HWHEEL) &&
554 			usage->code == REL_HWHEEL) {
555 		input_event_with_scancode(field->hidinput->input, usage->type,
556 				usage->code, usage->hid, -value);
557 		return 1;
558 	}
559 
560 	if ((asc->quirks & APPLE_HAS_FN) &&
561 			hidinput_apple_event(hdev, field->hidinput->input,
562 				usage, value))
563 		return 1;
564 
565 
566 	return 0;
567 }
568 
569 static int apple_fetch_battery(struct hid_device *hdev)
570 {
571 #ifdef CONFIG_HID_BATTERY_STRENGTH
572 	struct apple_sc *asc = hid_get_drvdata(hdev);
573 	struct hid_report_enum *report_enum;
574 	struct hid_report *report;
575 
576 	if (!(asc->quirks & APPLE_RDESC_BATTERY) || !hdev->battery)
577 		return -1;
578 
579 	report_enum = &hdev->report_enum[hdev->battery_report_type];
580 	report = report_enum->report_id_hash[hdev->battery_report_id];
581 
582 	if (!report || report->maxfield < 1)
583 		return -1;
584 
585 	if (hdev->battery_capacity == hdev->battery_max)
586 		return -1;
587 
588 	hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
589 	return 0;
590 #else
591 	return -1;
592 #endif
593 }
594 
595 static void apple_battery_timer_tick(struct timer_list *t)
596 {
597 	struct apple_sc *asc = from_timer(asc, t, battery_timer);
598 	struct hid_device *hdev = asc->hdev;
599 
600 	if (apple_fetch_battery(hdev) == 0) {
601 		mod_timer(&asc->battery_timer,
602 			  jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
603 	}
604 }
605 
606 /*
607  * MacBook JIS keyboard has wrong logical maximum
608  * Magic Keyboard JIS has wrong logical maximum
609  */
610 static __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
611 		unsigned int *rsize)
612 {
613 	struct apple_sc *asc = hid_get_drvdata(hdev);
614 
615 	if(*rsize >=71 && rdesc[70] == 0x65 && rdesc[64] == 0x65) {
616 		hid_info(hdev,
617 			 "fixing up Magic Keyboard JIS report descriptor\n");
618 		rdesc[64] = rdesc[70] = 0xe7;
619 	}
620 
621 	if ((asc->quirks & APPLE_RDESC_JIS) && *rsize >= 60 &&
622 			rdesc[53] == 0x65 && rdesc[59] == 0x65) {
623 		hid_info(hdev,
624 			 "fixing up MacBook JIS keyboard report descriptor\n");
625 		rdesc[53] = rdesc[59] = 0xe7;
626 	}
627 
628 	/*
629 	 * Change the usage from:
630 	 *   0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1)  0
631 	 *   0x09, 0x0b,       // Usage (Vendor Usage 0x0b)           3
632 	 * To:
633 	 *   0x05, 0x01,       // Usage Page (Generic Desktop)        0
634 	 *   0x09, 0x06,       // Usage (Keyboard)                    2
635 	 */
636 	if ((asc->quirks & APPLE_RDESC_BATTERY) && *rsize == 83 &&
637 	    rdesc[46] == 0x84 && rdesc[58] == 0x85) {
638 		hid_info(hdev,
639 			 "fixing up Magic Keyboard battery report descriptor\n");
640 		*rsize = *rsize - 1;
641 		rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL);
642 		if (!rdesc)
643 			return NULL;
644 
645 		rdesc[0] = 0x05;
646 		rdesc[1] = 0x01;
647 		rdesc[2] = 0x09;
648 		rdesc[3] = 0x06;
649 	}
650 
651 	return rdesc;
652 }
653 
654 static void apple_setup_input(struct input_dev *input)
655 {
656 	set_bit(KEY_NUMLOCK, input->keybit);
657 
658 	/* Enable all needed keys */
659 	apple_setup_key_translation(input, apple_fn_keys);
660 	apple_setup_key_translation(input, powerbook_fn_keys);
661 	apple_setup_key_translation(input, powerbook_numlock_keys);
662 	apple_setup_key_translation(input, apple_iso_keyboard);
663 	apple_setup_key_translation(input, magic_keyboard_alu_fn_keys);
664 	apple_setup_key_translation(input, magic_keyboard_2015_fn_keys);
665 	apple_setup_key_translation(input, apple2021_fn_keys);
666 	apple_setup_key_translation(input, macbookpro_no_esc_fn_keys);
667 	apple_setup_key_translation(input, macbookpro_dedicated_esc_fn_keys);
668 }
669 
670 static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
671 		struct hid_field *field, struct hid_usage *usage,
672 		unsigned long **bit, int *max)
673 {
674 	struct apple_sc *asc = hid_get_drvdata(hdev);
675 
676 	if (usage->hid == (HID_UP_CUSTOM | 0x0003) ||
677 			usage->hid == (HID_UP_MSVENDOR | 0x0003) ||
678 			usage->hid == (HID_UP_HPVENDOR2 | 0x0003)) {
679 		/* The fn key on Apple USB keyboards */
680 		set_bit(EV_REP, hi->input->evbit);
681 		hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_FN);
682 		asc->fn_found = true;
683 		apple_setup_input(hi->input);
684 		return 1;
685 	}
686 
687 	/* we want the hid layer to go through standard path (set and ignore) */
688 	return 0;
689 }
690 
691 static int apple_input_mapped(struct hid_device *hdev, struct hid_input *hi,
692 		struct hid_field *field, struct hid_usage *usage,
693 		unsigned long **bit, int *max)
694 {
695 	struct apple_sc *asc = hid_get_drvdata(hdev);
696 
697 	if (asc->quirks & APPLE_MIGHTYMOUSE) {
698 		if (usage->hid == HID_GD_Z)
699 			hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
700 		else if (usage->code == BTN_1)
701 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_2);
702 		else if (usage->code == BTN_2)
703 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_1);
704 	}
705 
706 	return 0;
707 }
708 
709 static int apple_input_configured(struct hid_device *hdev,
710 		struct hid_input *hidinput)
711 {
712 	struct apple_sc *asc = hid_get_drvdata(hdev);
713 
714 	if ((asc->quirks & APPLE_HAS_FN) && !asc->fn_found) {
715 		hid_info(hdev, "Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling\n");
716 		asc->quirks &= ~APPLE_HAS_FN;
717 	}
718 
719 	if (apple_is_non_apple_keyboard(hdev)) {
720 		hid_info(hdev, "Non-apple keyboard detected; function keys will default to fnmode=2 behavior\n");
721 		asc->quirks |= APPLE_IS_NON_APPLE;
722 	}
723 
724 	return 0;
725 }
726 
727 static bool apple_backlight_check_support(struct hid_device *hdev)
728 {
729 	int i;
730 	unsigned int hid;
731 	struct hid_report *report;
732 
733 	list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) {
734 		for (i = 0; i < report->maxfield; i++) {
735 			hid = report->field[i]->usage->hid;
736 			if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf)
737 				return true;
738 		}
739 	}
740 
741 	return false;
742 }
743 
744 static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate)
745 {
746 	int ret = 0;
747 	struct apple_backlight_set_report *rep;
748 
749 	rep = kmalloc(sizeof(*rep), GFP_KERNEL);
750 	if (rep == NULL)
751 		return -ENOMEM;
752 
753 	rep->report_id = 0xB0;
754 	rep->version = 1;
755 	rep->backlight = value;
756 	rep->rate = rate;
757 
758 	ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep),
759 				 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
760 
761 	kfree(rep);
762 	return ret;
763 }
764 
765 static int apple_backlight_led_set(struct led_classdev *led_cdev,
766 	enum led_brightness brightness)
767 {
768 	struct apple_sc_backlight *backlight = container_of(led_cdev,
769 							    struct apple_sc_backlight, cdev);
770 
771 	return apple_backlight_set(backlight->hdev, brightness, 0);
772 }
773 
774 static int apple_backlight_init(struct hid_device *hdev)
775 {
776 	int ret;
777 	struct apple_sc *asc = hid_get_drvdata(hdev);
778 	struct apple_backlight_config_report *rep;
779 
780 	if (!apple_backlight_check_support(hdev))
781 		return -EINVAL;
782 
783 	rep = kmalloc(0x200, GFP_KERNEL);
784 	if (rep == NULL)
785 		return -ENOMEM;
786 
787 	ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep),
788 				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
789 	if (ret < 0) {
790 		hid_err(hdev, "backlight request failed: %d\n", ret);
791 		goto cleanup_and_exit;
792 	}
793 	if (ret < 8 || rep->version != 1) {
794 		hid_err(hdev, "backlight config struct: bad version %i\n", rep->version);
795 		ret = -EINVAL;
796 		goto cleanup_and_exit;
797 	}
798 
799 	hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n",
800 		rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max);
801 
802 	asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL);
803 	if (!asc->backlight) {
804 		ret = -ENOMEM;
805 		goto cleanup_and_exit;
806 	}
807 
808 	asc->backlight->hdev = hdev;
809 	asc->backlight->cdev.name = "apple::kbd_backlight";
810 	asc->backlight->cdev.max_brightness = rep->backlight_on_max;
811 	asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set;
812 
813 	ret = apple_backlight_set(hdev, 0, 0);
814 	if (ret < 0) {
815 		hid_err(hdev, "backlight set request failed: %d\n", ret);
816 		goto cleanup_and_exit;
817 	}
818 
819 	ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev);
820 
821 cleanup_and_exit:
822 	kfree(rep);
823 	return ret;
824 }
825 
826 static int apple_probe(struct hid_device *hdev,
827 		const struct hid_device_id *id)
828 {
829 	unsigned long quirks = id->driver_data;
830 	struct apple_sc *asc;
831 	int ret;
832 
833 	asc = devm_kzalloc(&hdev->dev, sizeof(*asc), GFP_KERNEL);
834 	if (asc == NULL) {
835 		hid_err(hdev, "can't alloc apple descriptor\n");
836 		return -ENOMEM;
837 	}
838 
839 	asc->hdev = hdev;
840 	asc->quirks = quirks;
841 
842 	hid_set_drvdata(hdev, asc);
843 
844 	ret = hid_parse(hdev);
845 	if (ret) {
846 		hid_err(hdev, "parse failed\n");
847 		return ret;
848 	}
849 
850 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
851 	if (ret) {
852 		hid_err(hdev, "hw start failed\n");
853 		return ret;
854 	}
855 
856 	timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0);
857 	mod_timer(&asc->battery_timer,
858 		  jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
859 	apple_fetch_battery(hdev);
860 
861 	if (quirks & APPLE_BACKLIGHT_CTL)
862 		apple_backlight_init(hdev);
863 
864 	return 0;
865 }
866 
867 static void apple_remove(struct hid_device *hdev)
868 {
869 	struct apple_sc *asc = hid_get_drvdata(hdev);
870 
871 	del_timer_sync(&asc->battery_timer);
872 
873 	hid_hw_stop(hdev);
874 }
875 
876 static const struct hid_device_id apple_devices[] = {
877 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
878 		.driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
879 
880 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI),
881 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
882 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO),
883 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
884 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI),
885 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
886 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO),
887 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
888 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS),
889 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
890 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI),
891 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
892 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO),
893 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
894 			APPLE_ISO_TILDE_QUIRK },
895 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS),
896 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
897 			APPLE_RDESC_JIS },
898 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI),
899 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
900 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO),
901 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
902 			APPLE_ISO_TILDE_QUIRK },
903 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS),
904 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
905 			APPLE_RDESC_JIS },
906 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI),
907 		.driver_data = APPLE_HAS_FN },
908 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO),
909 		.driver_data = APPLE_HAS_FN },
910 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS),
911 		.driver_data = APPLE_HAS_FN },
912 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI),
913 		.driver_data = APPLE_HAS_FN },
914 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO),
915 		.driver_data = APPLE_HAS_FN },
916 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS),
917 		.driver_data = APPLE_HAS_FN },
918 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI),
919 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
920 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO),
921 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
922 			APPLE_ISO_TILDE_QUIRK },
923 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS),
924 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
925 			APPLE_RDESC_JIS },
926 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
927 		.driver_data = APPLE_HAS_FN },
928 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
929 		.driver_data = APPLE_HAS_FN },
930 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
931 		.driver_data = APPLE_HAS_FN },
932 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
933 		.driver_data = APPLE_HAS_FN },
934 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS),
935 		.driver_data = APPLE_HAS_FN },
936 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
937 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
938 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO),
939 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
940 			APPLE_ISO_TILDE_QUIRK },
941 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
942 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
943 			APPLE_ISO_TILDE_QUIRK },
944 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
945 				USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
946 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
947 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
948 				USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS),
949 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
950 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS),
951 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
952 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
953 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
954 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
955 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
956 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
957 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
958 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
959 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
960 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
961 		.driver_data = APPLE_HAS_FN },
962 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
963 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
964 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
965 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
966 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
967 		.driver_data = APPLE_HAS_FN },
968 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
969 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
970 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
971 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
972 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
973 		.driver_data = APPLE_HAS_FN },
974 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
975 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
976 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
977 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
978 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
979 		.driver_data = APPLE_HAS_FN },
980 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
981 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
982 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
983 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
984 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
985 		.driver_data = APPLE_HAS_FN },
986 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
987 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
988 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
989 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
990 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
991 		.driver_data = APPLE_HAS_FN },
992 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
993 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
994 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
995 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
996 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
997 		.driver_data = APPLE_HAS_FN },
998 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
999 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1000 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
1001 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1002 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
1003 		.driver_data = APPLE_HAS_FN },
1004 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
1005 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1006 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
1007 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1008 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
1009 		.driver_data = APPLE_HAS_FN },
1010 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
1011 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1012 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
1013 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1014 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
1015 		.driver_data = APPLE_HAS_FN },
1016 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
1017 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1018 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
1019 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1020 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
1021 		.driver_data = APPLE_HAS_FN },
1022 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
1023 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1024 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
1025 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1026 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI),
1027 		.driver_data = APPLE_HAS_FN },
1028 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO),
1029 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1030 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS),
1031 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1032 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI),
1033 		.driver_data = APPLE_HAS_FN },
1034 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO),
1035 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1036 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
1037 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1038 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K),
1039 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1040 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132),
1041 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1042 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680),
1043 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1044 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213),
1045 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1046 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K),
1047 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1048 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223),
1049 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1050 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K),
1051 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1052 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F),
1053 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1054 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
1055 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1056 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
1057 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
1058 			APPLE_ISO_TILDE_QUIRK },
1059 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS),
1060 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1061 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY),
1062 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1063 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY),
1064 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1065 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
1066 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
1067 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
1068 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1069 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
1070 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
1071 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
1072 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1073 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
1074 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
1075 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
1076 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1077 
1078 	{ }
1079 };
1080 MODULE_DEVICE_TABLE(hid, apple_devices);
1081 
1082 static struct hid_driver apple_driver = {
1083 	.name = "apple",
1084 	.id_table = apple_devices,
1085 	.report_fixup = apple_report_fixup,
1086 	.probe = apple_probe,
1087 	.remove = apple_remove,
1088 	.event = apple_event,
1089 	.input_mapping = apple_input_mapping,
1090 	.input_mapped = apple_input_mapped,
1091 	.input_configured = apple_input_configured,
1092 };
1093 module_hid_driver(apple_driver);
1094 
1095 MODULE_LICENSE("GPL");
1096