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