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