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