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