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