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