xref: /openbmc/linux/drivers/hid/hid-apple.c (revision 2a598d0b)
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 };
348 
349 static bool apple_is_non_apple_keyboard(struct hid_device *hdev)
350 {
351 	int i;
352 
353 	for (i = 0; i < ARRAY_SIZE(non_apple_keyboards); i++) {
354 		char *non_apple = non_apple_keyboards[i].name;
355 
356 		if (strncmp(hdev->name, non_apple, strlen(non_apple)) == 0)
357 			return true;
358 	}
359 
360 	return false;
361 }
362 
363 static inline void apple_setup_key_translation(struct input_dev *input,
364 		const struct apple_key_translation *table)
365 {
366 	const struct apple_key_translation *trans;
367 
368 	for (trans = table; trans->from; trans++)
369 		set_bit(trans->to, input->keybit);
370 }
371 
372 static const struct apple_key_translation *apple_find_translation(
373 		const struct apple_key_translation *table, u16 from)
374 {
375 	const struct apple_key_translation *trans;
376 
377 	/* Look for the translation */
378 	for (trans = table; trans->from; trans++)
379 		if (trans->from == from)
380 			return trans;
381 
382 	return NULL;
383 }
384 
385 static void input_event_with_scancode(struct input_dev *input,
386 		__u8 type, __u16 code, unsigned int hid, __s32 value)
387 {
388 	if (type == EV_KEY &&
389 	    (!test_bit(code, input->key)) == value)
390 		input_event(input, EV_MSC, MSC_SCAN, hid);
391 	input_event(input, type, code, value);
392 }
393 
394 static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
395 		struct hid_usage *usage, __s32 value)
396 {
397 	struct apple_sc *asc = hid_get_drvdata(hid);
398 	const struct apple_key_translation *trans, *table;
399 	bool do_translate;
400 	u16 code = usage->code;
401 	unsigned int real_fnmode;
402 
403 	if (fnmode == 3) {
404 		real_fnmode = (asc->quirks & APPLE_IS_NON_APPLE) ? 2 : 1;
405 	} else {
406 		real_fnmode = fnmode;
407 	}
408 
409 	if (swap_fn_leftctrl) {
410 		trans = apple_find_translation(swapped_fn_leftctrl_keys, code);
411 
412 		if (trans)
413 			code = trans->to;
414 	}
415 
416 	if (iso_layout > 0 || (iso_layout < 0 && (asc->quirks & APPLE_ISO_TILDE_QUIRK) &&
417 			hid->country == HID_COUNTRY_INTERNATIONAL_ISO)) {
418 		trans = apple_find_translation(apple_iso_keyboard, code);
419 
420 		if (trans)
421 			code = trans->to;
422 	}
423 
424 	if (swap_opt_cmd) {
425 		if (swap_opt_cmd == 2)
426 			trans = apple_find_translation(swapped_option_cmd_left_keys, code);
427 		else
428 			trans = apple_find_translation(swapped_option_cmd_keys, code);
429 
430 		if (trans)
431 			code = trans->to;
432 	}
433 
434 	if (swap_ctrl_cmd) {
435 		trans = apple_find_translation(swapped_ctrl_cmd_keys, code);
436 
437 		if (trans)
438 			code = trans->to;
439 	}
440 
441 	if (code == KEY_FN)
442 		asc->fn_on = !!value;
443 
444 	if (real_fnmode) {
445 		if (hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI ||
446 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO ||
447 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS ||
448 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI ||
449 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO ||
450 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS ||
451 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI ||
452 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO ||
453 		    hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS)
454 			table = magic_keyboard_alu_fn_keys;
455 		else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015 ||
456 			 hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015)
457 			table = magic_keyboard_2015_fn_keys;
458 		else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 ||
459 			 hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 ||
460 			 hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021)
461 			table = apple2021_fn_keys;
462 		else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 ||
463 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 ||
464 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213)
465 				table = macbookpro_no_esc_fn_keys;
466 		else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K ||
467 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 ||
468 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F)
469 				table = macbookpro_dedicated_esc_fn_keys;
470 		else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K ||
471 			 hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K)
472 				table = apple_fn_keys;
473 		else if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
474 				hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
475 			table = macbookair_fn_keys;
476 		else if (hid->product < 0x21d || hid->product >= 0x300)
477 			table = powerbook_fn_keys;
478 		else
479 			table = apple_fn_keys;
480 
481 		trans = apple_find_translation(table, code);
482 
483 		if (trans) {
484 			bool from_is_set = test_bit(trans->from, input->key);
485 			bool to_is_set = test_bit(trans->to, input->key);
486 
487 			if (from_is_set)
488 				code = trans->from;
489 			else if (to_is_set)
490 				code = trans->to;
491 
492 			if (!(from_is_set || to_is_set)) {
493 				if (trans->flags & APPLE_FLAG_FKEY) {
494 					switch (real_fnmode) {
495 					case 1:
496 						do_translate = !asc->fn_on;
497 						break;
498 					case 2:
499 						do_translate = asc->fn_on;
500 						break;
501 					default:
502 						/* should never happen */
503 						do_translate = false;
504 					}
505 				} else {
506 					do_translate = asc->fn_on;
507 				}
508 
509 				if (do_translate)
510 					code = trans->to;
511 			}
512 		}
513 
514 		if (asc->quirks & APPLE_NUMLOCK_EMULATION &&
515 				(test_bit(code, asc->pressed_numlock) ||
516 				test_bit(LED_NUML, input->led))) {
517 			trans = apple_find_translation(powerbook_numlock_keys, code);
518 
519 			if (trans) {
520 				if (value)
521 					set_bit(code, asc->pressed_numlock);
522 				else
523 					clear_bit(code, asc->pressed_numlock);
524 
525 				code = trans->to;
526 			}
527 		}
528 	}
529 
530 	if (usage->code != code) {
531 		input_event_with_scancode(input, usage->type, code, usage->hid, value);
532 
533 		return 1;
534 	}
535 
536 	return 0;
537 }
538 
539 static int apple_event(struct hid_device *hdev, struct hid_field *field,
540 		struct hid_usage *usage, __s32 value)
541 {
542 	struct apple_sc *asc = hid_get_drvdata(hdev);
543 
544 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
545 			!usage->type)
546 		return 0;
547 
548 	if ((asc->quirks & APPLE_INVERT_HWHEEL) &&
549 			usage->code == REL_HWHEEL) {
550 		input_event_with_scancode(field->hidinput->input, usage->type,
551 				usage->code, usage->hid, -value);
552 		return 1;
553 	}
554 
555 	if ((asc->quirks & APPLE_HAS_FN) &&
556 			hidinput_apple_event(hdev, field->hidinput->input,
557 				usage, value))
558 		return 1;
559 
560 
561 	return 0;
562 }
563 
564 static int apple_fetch_battery(struct hid_device *hdev)
565 {
566 #ifdef CONFIG_HID_BATTERY_STRENGTH
567 	struct apple_sc *asc = hid_get_drvdata(hdev);
568 	struct hid_report_enum *report_enum;
569 	struct hid_report *report;
570 
571 	if (!(asc->quirks & APPLE_RDESC_BATTERY) || !hdev->battery)
572 		return -1;
573 
574 	report_enum = &hdev->report_enum[hdev->battery_report_type];
575 	report = report_enum->report_id_hash[hdev->battery_report_id];
576 
577 	if (!report || report->maxfield < 1)
578 		return -1;
579 
580 	if (hdev->battery_capacity == hdev->battery_max)
581 		return -1;
582 
583 	hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
584 	return 0;
585 #else
586 	return -1;
587 #endif
588 }
589 
590 static void apple_battery_timer_tick(struct timer_list *t)
591 {
592 	struct apple_sc *asc = from_timer(asc, t, battery_timer);
593 	struct hid_device *hdev = asc->hdev;
594 
595 	if (apple_fetch_battery(hdev) == 0) {
596 		mod_timer(&asc->battery_timer,
597 			  jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
598 	}
599 }
600 
601 /*
602  * MacBook JIS keyboard has wrong logical maximum
603  * Magic Keyboard JIS has wrong logical maximum
604  */
605 static __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
606 		unsigned int *rsize)
607 {
608 	struct apple_sc *asc = hid_get_drvdata(hdev);
609 
610 	if(*rsize >=71 && rdesc[70] == 0x65 && rdesc[64] == 0x65) {
611 		hid_info(hdev,
612 			 "fixing up Magic Keyboard JIS report descriptor\n");
613 		rdesc[64] = rdesc[70] = 0xe7;
614 	}
615 
616 	if ((asc->quirks & APPLE_RDESC_JIS) && *rsize >= 60 &&
617 			rdesc[53] == 0x65 && rdesc[59] == 0x65) {
618 		hid_info(hdev,
619 			 "fixing up MacBook JIS keyboard report descriptor\n");
620 		rdesc[53] = rdesc[59] = 0xe7;
621 	}
622 
623 	/*
624 	 * Change the usage from:
625 	 *   0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1)  0
626 	 *   0x09, 0x0b,       // Usage (Vendor Usage 0x0b)           3
627 	 * To:
628 	 *   0x05, 0x01,       // Usage Page (Generic Desktop)        0
629 	 *   0x09, 0x06,       // Usage (Keyboard)                    2
630 	 */
631 	if ((asc->quirks & APPLE_RDESC_BATTERY) && *rsize == 83 &&
632 	    rdesc[46] == 0x84 && rdesc[58] == 0x85) {
633 		hid_info(hdev,
634 			 "fixing up Magic Keyboard battery report descriptor\n");
635 		*rsize = *rsize - 1;
636 		rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL);
637 		if (!rdesc)
638 			return NULL;
639 
640 		rdesc[0] = 0x05;
641 		rdesc[1] = 0x01;
642 		rdesc[2] = 0x09;
643 		rdesc[3] = 0x06;
644 	}
645 
646 	return rdesc;
647 }
648 
649 static void apple_setup_input(struct input_dev *input)
650 {
651 	set_bit(KEY_NUMLOCK, input->keybit);
652 
653 	/* Enable all needed keys */
654 	apple_setup_key_translation(input, apple_fn_keys);
655 	apple_setup_key_translation(input, powerbook_fn_keys);
656 	apple_setup_key_translation(input, powerbook_numlock_keys);
657 	apple_setup_key_translation(input, apple_iso_keyboard);
658 	apple_setup_key_translation(input, magic_keyboard_alu_fn_keys);
659 	apple_setup_key_translation(input, magic_keyboard_2015_fn_keys);
660 	apple_setup_key_translation(input, apple2021_fn_keys);
661 	apple_setup_key_translation(input, macbookpro_no_esc_fn_keys);
662 	apple_setup_key_translation(input, macbookpro_dedicated_esc_fn_keys);
663 }
664 
665 static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
666 		struct hid_field *field, struct hid_usage *usage,
667 		unsigned long **bit, int *max)
668 {
669 	struct apple_sc *asc = hid_get_drvdata(hdev);
670 
671 	if (usage->hid == (HID_UP_CUSTOM | 0x0003) ||
672 			usage->hid == (HID_UP_MSVENDOR | 0x0003) ||
673 			usage->hid == (HID_UP_HPVENDOR2 | 0x0003)) {
674 		/* The fn key on Apple USB keyboards */
675 		set_bit(EV_REP, hi->input->evbit);
676 		hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_FN);
677 		asc->fn_found = true;
678 		apple_setup_input(hi->input);
679 		return 1;
680 	}
681 
682 	/* we want the hid layer to go through standard path (set and ignore) */
683 	return 0;
684 }
685 
686 static int apple_input_mapped(struct hid_device *hdev, struct hid_input *hi,
687 		struct hid_field *field, struct hid_usage *usage,
688 		unsigned long **bit, int *max)
689 {
690 	struct apple_sc *asc = hid_get_drvdata(hdev);
691 
692 	if (asc->quirks & APPLE_MIGHTYMOUSE) {
693 		if (usage->hid == HID_GD_Z)
694 			hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
695 		else if (usage->code == BTN_1)
696 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_2);
697 		else if (usage->code == BTN_2)
698 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_1);
699 	}
700 
701 	return 0;
702 }
703 
704 static int apple_input_configured(struct hid_device *hdev,
705 		struct hid_input *hidinput)
706 {
707 	struct apple_sc *asc = hid_get_drvdata(hdev);
708 
709 	if ((asc->quirks & APPLE_HAS_FN) && !asc->fn_found) {
710 		hid_info(hdev, "Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling\n");
711 		asc->quirks &= ~APPLE_HAS_FN;
712 	}
713 
714 	if (apple_is_non_apple_keyboard(hdev)) {
715 		hid_info(hdev, "Non-apple keyboard detected; function keys will default to fnmode=2 behavior\n");
716 		asc->quirks |= APPLE_IS_NON_APPLE;
717 	}
718 
719 	return 0;
720 }
721 
722 static bool apple_backlight_check_support(struct hid_device *hdev)
723 {
724 	int i;
725 	unsigned int hid;
726 	struct hid_report *report;
727 
728 	list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) {
729 		for (i = 0; i < report->maxfield; i++) {
730 			hid = report->field[i]->usage->hid;
731 			if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf)
732 				return true;
733 		}
734 	}
735 
736 	return false;
737 }
738 
739 static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate)
740 {
741 	int ret = 0;
742 	struct apple_backlight_set_report *rep;
743 
744 	rep = kmalloc(sizeof(*rep), GFP_KERNEL);
745 	if (rep == NULL)
746 		return -ENOMEM;
747 
748 	rep->report_id = 0xB0;
749 	rep->version = 1;
750 	rep->backlight = value;
751 	rep->rate = rate;
752 
753 	ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep),
754 				 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
755 
756 	kfree(rep);
757 	return ret;
758 }
759 
760 static int apple_backlight_led_set(struct led_classdev *led_cdev,
761 	enum led_brightness brightness)
762 {
763 	struct apple_sc_backlight *backlight = container_of(led_cdev,
764 							    struct apple_sc_backlight, cdev);
765 
766 	return apple_backlight_set(backlight->hdev, brightness, 0);
767 }
768 
769 static int apple_backlight_init(struct hid_device *hdev)
770 {
771 	int ret;
772 	struct apple_sc *asc = hid_get_drvdata(hdev);
773 	struct apple_backlight_config_report *rep;
774 
775 	if (!apple_backlight_check_support(hdev))
776 		return -EINVAL;
777 
778 	rep = kmalloc(0x200, GFP_KERNEL);
779 	if (rep == NULL)
780 		return -ENOMEM;
781 
782 	ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep),
783 				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
784 	if (ret < 0) {
785 		hid_err(hdev, "backlight request failed: %d\n", ret);
786 		goto cleanup_and_exit;
787 	}
788 	if (ret < 8 || rep->version != 1) {
789 		hid_err(hdev, "backlight config struct: bad version %i\n", rep->version);
790 		ret = -EINVAL;
791 		goto cleanup_and_exit;
792 	}
793 
794 	hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n",
795 		rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max);
796 
797 	asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL);
798 	if (!asc->backlight) {
799 		ret = -ENOMEM;
800 		goto cleanup_and_exit;
801 	}
802 
803 	asc->backlight->hdev = hdev;
804 	asc->backlight->cdev.name = "apple::kbd_backlight";
805 	asc->backlight->cdev.max_brightness = rep->backlight_on_max;
806 	asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set;
807 
808 	ret = apple_backlight_set(hdev, 0, 0);
809 	if (ret < 0) {
810 		hid_err(hdev, "backlight set request failed: %d\n", ret);
811 		goto cleanup_and_exit;
812 	}
813 
814 	ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev);
815 
816 cleanup_and_exit:
817 	kfree(rep);
818 	return ret;
819 }
820 
821 static int apple_probe(struct hid_device *hdev,
822 		const struct hid_device_id *id)
823 {
824 	unsigned long quirks = id->driver_data;
825 	struct apple_sc *asc;
826 	int ret;
827 
828 	asc = devm_kzalloc(&hdev->dev, sizeof(*asc), GFP_KERNEL);
829 	if (asc == NULL) {
830 		hid_err(hdev, "can't alloc apple descriptor\n");
831 		return -ENOMEM;
832 	}
833 
834 	asc->hdev = hdev;
835 	asc->quirks = quirks;
836 
837 	hid_set_drvdata(hdev, asc);
838 
839 	ret = hid_parse(hdev);
840 	if (ret) {
841 		hid_err(hdev, "parse failed\n");
842 		return ret;
843 	}
844 
845 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
846 	if (ret) {
847 		hid_err(hdev, "hw start failed\n");
848 		return ret;
849 	}
850 
851 	timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0);
852 	mod_timer(&asc->battery_timer,
853 		  jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
854 	apple_fetch_battery(hdev);
855 
856 	if (quirks & APPLE_BACKLIGHT_CTL)
857 		apple_backlight_init(hdev);
858 
859 	return 0;
860 }
861 
862 static void apple_remove(struct hid_device *hdev)
863 {
864 	struct apple_sc *asc = hid_get_drvdata(hdev);
865 
866 	del_timer_sync(&asc->battery_timer);
867 
868 	hid_hw_stop(hdev);
869 }
870 
871 static const struct hid_device_id apple_devices[] = {
872 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
873 		.driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
874 
875 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI),
876 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
877 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO),
878 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
879 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI),
880 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
881 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO),
882 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
883 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS),
884 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
885 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI),
886 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
887 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO),
888 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
889 			APPLE_ISO_TILDE_QUIRK },
890 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS),
891 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
892 			APPLE_RDESC_JIS },
893 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI),
894 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
895 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO),
896 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
897 			APPLE_ISO_TILDE_QUIRK },
898 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS),
899 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
900 			APPLE_RDESC_JIS },
901 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI),
902 		.driver_data = APPLE_HAS_FN },
903 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO),
904 		.driver_data = APPLE_HAS_FN },
905 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS),
906 		.driver_data = APPLE_HAS_FN },
907 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI),
908 		.driver_data = APPLE_HAS_FN },
909 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO),
910 		.driver_data = APPLE_HAS_FN },
911 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS),
912 		.driver_data = APPLE_HAS_FN },
913 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI),
914 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
915 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO),
916 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
917 			APPLE_ISO_TILDE_QUIRK },
918 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS),
919 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
920 			APPLE_RDESC_JIS },
921 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
922 		.driver_data = APPLE_HAS_FN },
923 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
924 		.driver_data = APPLE_HAS_FN },
925 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
926 		.driver_data = APPLE_HAS_FN },
927 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
928 		.driver_data = APPLE_HAS_FN },
929 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS),
930 		.driver_data = APPLE_HAS_FN },
931 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
932 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
933 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO),
934 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
935 			APPLE_ISO_TILDE_QUIRK },
936 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
937 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
938 			APPLE_ISO_TILDE_QUIRK },
939 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
940 				USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
941 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
942 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
943 				USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS),
944 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
945 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS),
946 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
947 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
948 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
949 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
950 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
951 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
952 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
953 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
954 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
955 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
956 		.driver_data = APPLE_HAS_FN },
957 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
958 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
959 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
960 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
961 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
962 		.driver_data = APPLE_HAS_FN },
963 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
964 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
965 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
966 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
967 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
968 		.driver_data = APPLE_HAS_FN },
969 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
970 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
971 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
972 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
973 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
974 		.driver_data = APPLE_HAS_FN },
975 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
976 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
977 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
978 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
979 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
980 		.driver_data = APPLE_HAS_FN },
981 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
982 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
983 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
984 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
985 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
986 		.driver_data = APPLE_HAS_FN },
987 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
988 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
989 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
990 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
991 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
992 		.driver_data = APPLE_HAS_FN },
993 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
994 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
995 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
996 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
997 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
998 		.driver_data = APPLE_HAS_FN },
999 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
1000 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1001 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
1002 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1003 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
1004 		.driver_data = APPLE_HAS_FN },
1005 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
1006 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1007 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
1008 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1009 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
1010 		.driver_data = APPLE_HAS_FN },
1011 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
1012 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1013 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
1014 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1015 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
1016 		.driver_data = APPLE_HAS_FN },
1017 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
1018 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1019 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
1020 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1021 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI),
1022 		.driver_data = APPLE_HAS_FN },
1023 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO),
1024 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1025 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS),
1026 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1027 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI),
1028 		.driver_data = APPLE_HAS_FN },
1029 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO),
1030 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1031 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
1032 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
1033 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K),
1034 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1035 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132),
1036 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1037 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680),
1038 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1039 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213),
1040 		.driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
1041 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K),
1042 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1043 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223),
1044 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1045 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K),
1046 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1047 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F),
1048 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1049 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
1050 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1051 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
1052 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
1053 			APPLE_ISO_TILDE_QUIRK },
1054 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS),
1055 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1056 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY),
1057 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1058 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY),
1059 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
1060 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
1061 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
1062 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
1063 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1064 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
1065 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
1066 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
1067 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1068 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
1069 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
1070 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
1071 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
1072 
1073 	{ }
1074 };
1075 MODULE_DEVICE_TABLE(hid, apple_devices);
1076 
1077 static struct hid_driver apple_driver = {
1078 	.name = "apple",
1079 	.id_table = apple_devices,
1080 	.report_fixup = apple_report_fixup,
1081 	.probe = apple_probe,
1082 	.remove = apple_remove,
1083 	.event = apple_event,
1084 	.input_mapping = apple_input_mapping,
1085 	.input_mapped = apple_input_mapped,
1086 	.input_configured = apple_input_configured,
1087 };
1088 module_hid_driver(apple_driver);
1089 
1090 MODULE_LICENSE("GPL");
1091