xref: /openbmc/linux/drivers/hid/hid-apple.c (revision 2f0754f2)
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  */
11 
12 /*
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/device.h>
18 #include <linux/hid.h>
19 #include <linux/jiffies.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/timer.h>
23 
24 #include "hid-ids.h"
25 
26 #define APPLE_RDESC_JIS		BIT(0)
27 #define APPLE_IGNORE_MOUSE	BIT(1)
28 #define APPLE_HAS_FN		BIT(2)
29 /* BIT(3) reserved, was: APPLE_HIDDEV */
30 #define APPLE_ISO_TILDE_QUIRK	BIT(4)
31 #define APPLE_MIGHTYMOUSE	BIT(5)
32 #define APPLE_INVERT_HWHEEL	BIT(6)
33 /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
34 #define APPLE_NUMLOCK_EMULATION	BIT(8)
35 #define APPLE_RDESC_BATTERY	BIT(9)
36 
37 #define APPLE_FLAG_FKEY		0x01
38 
39 #define HID_COUNTRY_INTERNATIONAL_ISO	13
40 #define APPLE_BATTERY_TIMEOUT_MS	60000
41 
42 static unsigned int fnmode = 1;
43 module_param(fnmode, uint, 0644);
44 MODULE_PARM_DESC(fnmode, "Mode of fn key on Apple keyboards (0 = disabled, "
45 		"[1] = fkeyslast, 2 = fkeysfirst)");
46 
47 static int iso_layout = -1;
48 module_param(iso_layout, int, 0644);
49 MODULE_PARM_DESC(iso_layout, "Swap the backtick/tilde and greater-than/less-than keys. "
50 		"([-1] = auto, 0 = disabled, 1 = enabled)");
51 
52 static unsigned int swap_opt_cmd;
53 module_param(swap_opt_cmd, uint, 0644);
54 MODULE_PARM_DESC(swap_opt_cmd, "Swap the Option (\"Alt\") and Command (\"Flag\") keys. "
55 		"(For people who want to keep Windows PC keyboard muscle memory. "
56 		"[0] = as-is, Mac layout. 1 = swapped, Windows layout.)");
57 
58 static unsigned int swap_fn_leftctrl;
59 module_param(swap_fn_leftctrl, uint, 0644);
60 MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
61 		"(For people who want to keep PC keyboard muscle memory. "
62 		"[0] = as-is, Mac layout, 1 = swapped, PC layout)");
63 
64 struct apple_sc {
65 	struct hid_device *hdev;
66 	unsigned long quirks;
67 	unsigned int fn_on;
68 	unsigned int fn_found;
69 	DECLARE_BITMAP(pressed_numlock, KEY_CNT);
70 	struct timer_list battery_timer;
71 };
72 
73 struct apple_key_translation {
74 	u16 from;
75 	u16 to;
76 	u8 flags;
77 };
78 
79 static const struct apple_key_translation apple2021_fn_keys[] = {
80 	{ KEY_BACKSPACE, KEY_DELETE },
81 	{ KEY_ENTER,	KEY_INSERT },
82 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
83 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
84 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
85 	{ KEY_F4,	KEY_SEARCH,         APPLE_FLAG_FKEY },
86 	{ KEY_F5,	KEY_MICMUTE,        APPLE_FLAG_FKEY },
87 	{ KEY_F6,	KEY_SLEEP,          APPLE_FLAG_FKEY },
88 	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
89 	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
90 	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
91 	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
92 	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
93 	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
94 	{ KEY_UP,	KEY_PAGEUP },
95 	{ KEY_DOWN,	KEY_PAGEDOWN },
96 	{ KEY_LEFT,	KEY_HOME },
97 	{ KEY_RIGHT,	KEY_END },
98 	{ }
99 };
100 
101 static const struct apple_key_translation macbookair_fn_keys[] = {
102 	{ KEY_BACKSPACE, KEY_DELETE },
103 	{ KEY_ENTER,	KEY_INSERT },
104 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
105 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
106 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
107 	{ KEY_F4,	KEY_DASHBOARD,      APPLE_FLAG_FKEY },
108 	{ KEY_F6,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
109 	{ KEY_F7,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
110 	{ KEY_F8,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
111 	{ KEY_F9,	KEY_MUTE,           APPLE_FLAG_FKEY },
112 	{ KEY_F10,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
113 	{ KEY_F11,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
114 	{ KEY_F12,	KEY_EJECTCD,        APPLE_FLAG_FKEY },
115 	{ KEY_UP,	KEY_PAGEUP },
116 	{ KEY_DOWN,	KEY_PAGEDOWN },
117 	{ KEY_LEFT,	KEY_HOME },
118 	{ KEY_RIGHT,	KEY_END },
119 	{ }
120 };
121 
122 static const struct apple_key_translation apple_fn_keys[] = {
123 	{ KEY_BACKSPACE, KEY_DELETE },
124 	{ KEY_ENTER,	KEY_INSERT },
125 	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
126 	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
127 	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
128 	{ KEY_F4,	KEY_DASHBOARD,      APPLE_FLAG_FKEY },
129 	{ KEY_F5,	KEY_KBDILLUMDOWN,   APPLE_FLAG_FKEY },
130 	{ KEY_F6,	KEY_KBDILLUMUP,     APPLE_FLAG_FKEY },
131 	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
132 	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
133 	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
134 	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
135 	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
136 	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
137 	{ KEY_UP,	KEY_PAGEUP },
138 	{ KEY_DOWN,	KEY_PAGEDOWN },
139 	{ KEY_LEFT,	KEY_HOME },
140 	{ KEY_RIGHT,	KEY_END },
141 	{ }
142 };
143 
144 static const struct apple_key_translation powerbook_fn_keys[] = {
145 	{ KEY_BACKSPACE, KEY_DELETE },
146 	{ KEY_F1,	KEY_BRIGHTNESSDOWN,     APPLE_FLAG_FKEY },
147 	{ KEY_F2,	KEY_BRIGHTNESSUP,       APPLE_FLAG_FKEY },
148 	{ KEY_F3,	KEY_MUTE,               APPLE_FLAG_FKEY },
149 	{ KEY_F4,	KEY_VOLUMEDOWN,         APPLE_FLAG_FKEY },
150 	{ KEY_F5,	KEY_VOLUMEUP,           APPLE_FLAG_FKEY },
151 	{ KEY_F6,	KEY_NUMLOCK,            APPLE_FLAG_FKEY },
152 	{ KEY_F7,	KEY_SWITCHVIDEOMODE,    APPLE_FLAG_FKEY },
153 	{ KEY_F8,	KEY_KBDILLUMTOGGLE,     APPLE_FLAG_FKEY },
154 	{ KEY_F9,	KEY_KBDILLUMDOWN,       APPLE_FLAG_FKEY },
155 	{ KEY_F10,	KEY_KBDILLUMUP,         APPLE_FLAG_FKEY },
156 	{ KEY_UP,	KEY_PAGEUP },
157 	{ KEY_DOWN,	KEY_PAGEDOWN },
158 	{ KEY_LEFT,	KEY_HOME },
159 	{ KEY_RIGHT,	KEY_END },
160 	{ }
161 };
162 
163 static const struct apple_key_translation powerbook_numlock_keys[] = {
164 	{ KEY_J,	KEY_KP1 },
165 	{ KEY_K,	KEY_KP2 },
166 	{ KEY_L,	KEY_KP3 },
167 	{ KEY_U,	KEY_KP4 },
168 	{ KEY_I,	KEY_KP5 },
169 	{ KEY_O,	KEY_KP6 },
170 	{ KEY_7,	KEY_KP7 },
171 	{ KEY_8,	KEY_KP8 },
172 	{ KEY_9,	KEY_KP9 },
173 	{ KEY_M,	KEY_KP0 },
174 	{ KEY_DOT,	KEY_KPDOT },
175 	{ KEY_SLASH,	KEY_KPPLUS },
176 	{ KEY_SEMICOLON, KEY_KPMINUS },
177 	{ KEY_P,	KEY_KPASTERISK },
178 	{ KEY_MINUS,	KEY_KPEQUAL },
179 	{ KEY_0,	KEY_KPSLASH },
180 	{ KEY_F6,	KEY_NUMLOCK },
181 	{ KEY_KPENTER,	KEY_KPENTER },
182 	{ KEY_BACKSPACE, KEY_BACKSPACE },
183 	{ }
184 };
185 
186 static const struct apple_key_translation apple_iso_keyboard[] = {
187 	{ KEY_GRAVE,	KEY_102ND },
188 	{ KEY_102ND,	KEY_GRAVE },
189 	{ }
190 };
191 
192 static const struct apple_key_translation swapped_option_cmd_keys[] = {
193 	{ KEY_LEFTALT,	KEY_LEFTMETA },
194 	{ KEY_LEFTMETA,	KEY_LEFTALT },
195 	{ KEY_RIGHTALT,	KEY_RIGHTMETA },
196 	{ KEY_RIGHTMETA,KEY_RIGHTALT },
197 	{ }
198 };
199 
200 static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
201 	{ KEY_FN, KEY_LEFTCTRL },
202 	{ }
203 };
204 
205 static const struct apple_key_translation *apple_find_translation(
206 		const struct apple_key_translation *table, u16 from)
207 {
208 	const struct apple_key_translation *trans;
209 
210 	/* Look for the translation */
211 	for (trans = table; trans->from; trans++)
212 		if (trans->from == from)
213 			return trans;
214 
215 	return NULL;
216 }
217 
218 static void input_event_with_scancode(struct input_dev *input,
219 		__u8 type, __u16 code, unsigned int hid, __s32 value)
220 {
221 	if (type == EV_KEY &&
222 	    (!test_bit(code, input->key)) == value)
223 		input_event(input, EV_MSC, MSC_SCAN, hid);
224 	input_event(input, type, code, value);
225 }
226 
227 static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
228 		struct hid_usage *usage, __s32 value)
229 {
230 	struct apple_sc *asc = hid_get_drvdata(hid);
231 	const struct apple_key_translation *trans, *table;
232 	bool do_translate;
233 	u16 code = 0;
234 
235 	u16 fn_keycode = (swap_fn_leftctrl) ? (KEY_LEFTCTRL) : (KEY_FN);
236 
237 	if (usage->code == fn_keycode) {
238 		asc->fn_on = !!value;
239 		input_event_with_scancode(input, usage->type, KEY_FN,
240 				usage->hid, value);
241 		return 1;
242 	}
243 
244 	if (fnmode) {
245 		if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 ||
246 		    hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 ||
247 		    hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021)
248 			table = apple2021_fn_keys;
249 		else if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
250 				hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
251 			table = macbookair_fn_keys;
252 		else if (hid->product < 0x21d || hid->product >= 0x300)
253 			table = powerbook_fn_keys;
254 		else
255 			table = apple_fn_keys;
256 
257 		trans = apple_find_translation (table, usage->code);
258 
259 		if (trans) {
260 			if (test_bit(trans->from, input->key))
261 				code = trans->from;
262 			else if (test_bit(trans->to, input->key))
263 				code = trans->to;
264 
265 			if (!code) {
266 				if (trans->flags & APPLE_FLAG_FKEY) {
267 					switch (fnmode) {
268 					case 1:
269 						do_translate = !asc->fn_on;
270 						break;
271 					case 2:
272 						do_translate = asc->fn_on;
273 						break;
274 					default:
275 						/* should never happen */
276 						do_translate = false;
277 					}
278 				} else {
279 					do_translate = asc->fn_on;
280 				}
281 
282 				code = do_translate ? trans->to : trans->from;
283 			}
284 
285 			input_event_with_scancode(input, usage->type, code,
286 					usage->hid, value);
287 			return 1;
288 		}
289 
290 		if (asc->quirks & APPLE_NUMLOCK_EMULATION &&
291 				(test_bit(usage->code, asc->pressed_numlock) ||
292 				test_bit(LED_NUML, input->led))) {
293 			trans = apple_find_translation(powerbook_numlock_keys,
294 					usage->code);
295 
296 			if (trans) {
297 				if (value)
298 					set_bit(usage->code,
299 							asc->pressed_numlock);
300 				else
301 					clear_bit(usage->code,
302 							asc->pressed_numlock);
303 
304 				input_event_with_scancode(input, usage->type,
305 						trans->to, usage->hid, value);
306 			}
307 
308 			return 1;
309 		}
310 	}
311 
312 	if (iso_layout > 0 || (iso_layout < 0 && (asc->quirks & APPLE_ISO_TILDE_QUIRK) &&
313 			hid->country == HID_COUNTRY_INTERNATIONAL_ISO)) {
314 		trans = apple_find_translation(apple_iso_keyboard, usage->code);
315 		if (trans) {
316 			input_event_with_scancode(input, usage->type,
317 					trans->to, usage->hid, value);
318 			return 1;
319 		}
320 	}
321 
322 	if (swap_opt_cmd) {
323 		trans = apple_find_translation(swapped_option_cmd_keys, usage->code);
324 		if (trans) {
325 			input_event_with_scancode(input, usage->type,
326 					trans->to, usage->hid, value);
327 			return 1;
328 		}
329 	}
330 
331 	if (swap_fn_leftctrl) {
332 		trans = apple_find_translation(swapped_fn_leftctrl_keys, usage->code);
333 		if (trans) {
334 			input_event_with_scancode(input, usage->type,
335 					trans->to, usage->hid, value);
336 			return 1;
337 		}
338 	}
339 
340 	return 0;
341 }
342 
343 static int apple_event(struct hid_device *hdev, struct hid_field *field,
344 		struct hid_usage *usage, __s32 value)
345 {
346 	struct apple_sc *asc = hid_get_drvdata(hdev);
347 
348 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
349 			!usage->type)
350 		return 0;
351 
352 	if ((asc->quirks & APPLE_INVERT_HWHEEL) &&
353 			usage->code == REL_HWHEEL) {
354 		input_event_with_scancode(field->hidinput->input, usage->type,
355 				usage->code, usage->hid, -value);
356 		return 1;
357 	}
358 
359 	if ((asc->quirks & APPLE_HAS_FN) &&
360 			hidinput_apple_event(hdev, field->hidinput->input,
361 				usage, value))
362 		return 1;
363 
364 
365 	return 0;
366 }
367 
368 static int apple_fetch_battery(struct hid_device *hdev)
369 {
370 #ifdef CONFIG_HID_BATTERY_STRENGTH
371 	struct apple_sc *asc = hid_get_drvdata(hdev);
372 	struct hid_report_enum *report_enum;
373 	struct hid_report *report;
374 
375 	if (!(asc->quirks & APPLE_RDESC_BATTERY) || !hdev->battery)
376 		return -1;
377 
378 	report_enum = &hdev->report_enum[hdev->battery_report_type];
379 	report = report_enum->report_id_hash[hdev->battery_report_id];
380 
381 	if (!report || report->maxfield < 1)
382 		return -1;
383 
384 	if (hdev->battery_capacity == hdev->battery_max)
385 		return -1;
386 
387 	hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
388 	return 0;
389 #else
390 	return -1;
391 #endif
392 }
393 
394 static void apple_battery_timer_tick(struct timer_list *t)
395 {
396 	struct apple_sc *asc = from_timer(asc, t, battery_timer);
397 	struct hid_device *hdev = asc->hdev;
398 
399 	if (apple_fetch_battery(hdev) == 0) {
400 		mod_timer(&asc->battery_timer,
401 			  jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
402 	}
403 }
404 
405 /*
406  * MacBook JIS keyboard has wrong logical maximum
407  * Magic Keyboard JIS has wrong logical maximum
408  */
409 static __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
410 		unsigned int *rsize)
411 {
412 	struct apple_sc *asc = hid_get_drvdata(hdev);
413 
414 	if(*rsize >=71 && rdesc[70] == 0x65 && rdesc[64] == 0x65) {
415 		hid_info(hdev,
416 			 "fixing up Magic Keyboard JIS report descriptor\n");
417 		rdesc[64] = rdesc[70] = 0xe7;
418 	}
419 
420 	if ((asc->quirks & APPLE_RDESC_JIS) && *rsize >= 60 &&
421 			rdesc[53] == 0x65 && rdesc[59] == 0x65) {
422 		hid_info(hdev,
423 			 "fixing up MacBook JIS keyboard report descriptor\n");
424 		rdesc[53] = rdesc[59] = 0xe7;
425 	}
426 
427 	/*
428 	 * Change the usage from:
429 	 *   0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1)  0
430 	 *   0x09, 0x0b,       // Usage (Vendor Usage 0x0b)           3
431 	 * To:
432 	 *   0x05, 0x01,       // Usage Page (Generic Desktop)        0
433 	 *   0x09, 0x06,       // Usage (Keyboard)                    2
434 	 */
435 	if ((asc->quirks & APPLE_RDESC_BATTERY) && *rsize == 83 &&
436 	    rdesc[46] == 0x84 && rdesc[58] == 0x85) {
437 		hid_info(hdev,
438 			 "fixing up Magic Keyboard battery report descriptor\n");
439 		*rsize = *rsize - 1;
440 		rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL);
441 		if (!rdesc)
442 			return NULL;
443 
444 		rdesc[0] = 0x05;
445 		rdesc[1] = 0x01;
446 		rdesc[2] = 0x09;
447 		rdesc[3] = 0x06;
448 	}
449 
450 	return rdesc;
451 }
452 
453 static void apple_setup_input(struct input_dev *input)
454 {
455 	const struct apple_key_translation *trans;
456 
457 	set_bit(KEY_NUMLOCK, input->keybit);
458 
459 	/* Enable all needed keys */
460 	for (trans = apple_fn_keys; trans->from; trans++)
461 		set_bit(trans->to, input->keybit);
462 
463 	for (trans = powerbook_fn_keys; trans->from; trans++)
464 		set_bit(trans->to, input->keybit);
465 
466 	for (trans = powerbook_numlock_keys; trans->from; trans++)
467 		set_bit(trans->to, input->keybit);
468 
469 	for (trans = apple_iso_keyboard; trans->from; trans++)
470 		set_bit(trans->to, input->keybit);
471 
472 	for (trans = apple2021_fn_keys; trans->from; trans++)
473 		set_bit(trans->to, input->keybit);
474 
475 	if (swap_fn_leftctrl) {
476 		for (trans = swapped_fn_leftctrl_keys; trans->from; trans++)
477 			set_bit(trans->to, input->keybit);
478 	}
479 }
480 
481 static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
482 		struct hid_field *field, struct hid_usage *usage,
483 		unsigned long **bit, int *max)
484 {
485 	struct apple_sc *asc = hid_get_drvdata(hdev);
486 
487 	if (usage->hid == (HID_UP_CUSTOM | 0x0003) ||
488 			usage->hid == (HID_UP_MSVENDOR | 0x0003) ||
489 			usage->hid == (HID_UP_HPVENDOR2 | 0x0003)) {
490 		/* The fn key on Apple USB keyboards */
491 		set_bit(EV_REP, hi->input->evbit);
492 		hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_FN);
493 		asc->fn_found = true;
494 		apple_setup_input(hi->input);
495 		return 1;
496 	}
497 
498 	/* we want the hid layer to go through standard path (set and ignore) */
499 	return 0;
500 }
501 
502 static int apple_input_mapped(struct hid_device *hdev, struct hid_input *hi,
503 		struct hid_field *field, struct hid_usage *usage,
504 		unsigned long **bit, int *max)
505 {
506 	struct apple_sc *asc = hid_get_drvdata(hdev);
507 
508 	if (asc->quirks & APPLE_MIGHTYMOUSE) {
509 		if (usage->hid == HID_GD_Z)
510 			hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
511 		else if (usage->code == BTN_1)
512 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_2);
513 		else if (usage->code == BTN_2)
514 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_1);
515 	}
516 
517 	return 0;
518 }
519 
520 static int apple_input_configured(struct hid_device *hdev,
521 		struct hid_input *hidinput)
522 {
523 	struct apple_sc *asc = hid_get_drvdata(hdev);
524 
525 	if ((asc->quirks & APPLE_HAS_FN) && !asc->fn_found) {
526 		hid_info(hdev, "Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling\n");
527 		asc->quirks &= ~APPLE_HAS_FN;
528 	}
529 
530 	return 0;
531 }
532 
533 static int apple_probe(struct hid_device *hdev,
534 		const struct hid_device_id *id)
535 {
536 	unsigned long quirks = id->driver_data;
537 	struct apple_sc *asc;
538 	int ret;
539 
540 	asc = devm_kzalloc(&hdev->dev, sizeof(*asc), GFP_KERNEL);
541 	if (asc == NULL) {
542 		hid_err(hdev, "can't alloc apple descriptor\n");
543 		return -ENOMEM;
544 	}
545 
546 	asc->hdev = hdev;
547 	asc->quirks = quirks;
548 
549 	hid_set_drvdata(hdev, asc);
550 
551 	ret = hid_parse(hdev);
552 	if (ret) {
553 		hid_err(hdev, "parse failed\n");
554 		return ret;
555 	}
556 
557 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
558 	if (ret) {
559 		hid_err(hdev, "hw start failed\n");
560 		return ret;
561 	}
562 
563 	timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0);
564 	mod_timer(&asc->battery_timer,
565 		  jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
566 	apple_fetch_battery(hdev);
567 
568 	return 0;
569 }
570 
571 static void apple_remove(struct hid_device *hdev)
572 {
573 	struct apple_sc *asc = hid_get_drvdata(hdev);
574 
575 	del_timer_sync(&asc->battery_timer);
576 
577 	hid_hw_stop(hdev);
578 }
579 
580 static const struct hid_device_id apple_devices[] = {
581 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
582 		.driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
583 
584 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI),
585 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
586 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO),
587 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
588 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI),
589 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
590 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO),
591 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
592 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS),
593 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
594 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI),
595 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
596 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO),
597 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
598 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS),
599 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
600 			APPLE_RDESC_JIS },
601 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI),
602 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
603 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO),
604 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
605 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS),
606 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
607 			APPLE_RDESC_JIS },
608 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI),
609 		.driver_data = APPLE_HAS_FN },
610 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO),
611 		.driver_data = APPLE_HAS_FN },
612 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS),
613 		.driver_data = APPLE_HAS_FN },
614 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI),
615 		.driver_data = APPLE_HAS_FN },
616 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO),
617 		.driver_data = APPLE_HAS_FN },
618 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS),
619 		.driver_data = APPLE_HAS_FN },
620 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI),
621 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
622 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO),
623 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
624 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS),
625 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
626 			APPLE_RDESC_JIS },
627 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
628 		.driver_data = APPLE_HAS_FN },
629 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
630 		.driver_data = APPLE_HAS_FN },
631 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
632 		.driver_data = APPLE_HAS_FN },
633 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
634 		.driver_data = APPLE_HAS_FN },
635 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS),
636 		.driver_data = APPLE_HAS_FN },
637 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
638 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
639 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO),
640 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
641 			APPLE_ISO_TILDE_QUIRK },
642 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
643 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
644 			APPLE_ISO_TILDE_QUIRK },
645 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
646 				USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
647 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
648 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
649 				USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS),
650 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
651 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS),
652 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
653 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
654 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
655 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
656 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
657 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
658 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
659 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
660 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
661 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
662 		.driver_data = APPLE_HAS_FN },
663 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
664 		.driver_data = APPLE_HAS_FN },
665 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
666 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
667 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
668 		.driver_data = APPLE_HAS_FN },
669 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
670 		.driver_data = APPLE_HAS_FN },
671 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
672 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
673 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
674 		.driver_data = APPLE_HAS_FN },
675 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
676 		.driver_data = APPLE_HAS_FN },
677 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
678 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
679 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
680 		.driver_data = APPLE_HAS_FN },
681 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
682 		.driver_data = APPLE_HAS_FN },
683 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
684 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
685 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
686 		.driver_data = APPLE_HAS_FN },
687 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
688 		.driver_data = APPLE_HAS_FN },
689 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
690 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
691 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
692 		.driver_data = APPLE_HAS_FN },
693 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
694 		.driver_data = APPLE_HAS_FN },
695 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
696 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
697 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
698 		.driver_data = APPLE_HAS_FN },
699 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
700 		.driver_data = APPLE_HAS_FN },
701 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
702 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
703 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
704 		.driver_data = APPLE_HAS_FN },
705 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
706 		.driver_data = APPLE_HAS_FN },
707 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
708 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
709 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
710 		.driver_data = APPLE_HAS_FN },
711 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
712 		.driver_data = APPLE_HAS_FN },
713 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
714 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
715 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
716 		.driver_data = APPLE_HAS_FN },
717 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
718 		.driver_data = APPLE_HAS_FN },
719 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
720 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
721 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
722 		.driver_data = APPLE_HAS_FN },
723 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
724 		.driver_data = APPLE_HAS_FN },
725 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
726 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
727 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI),
728 		.driver_data = APPLE_HAS_FN },
729 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO),
730 		.driver_data = APPLE_HAS_FN },
731 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS),
732 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
733 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI),
734 		.driver_data = APPLE_HAS_FN },
735 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO),
736 		.driver_data = APPLE_HAS_FN },
737 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
738 		.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
739 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
740 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
741 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
742 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
743 			APPLE_ISO_TILDE_QUIRK },
744 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS),
745 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
746 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY),
747 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
748 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY),
749 		.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
750 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
751 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
752 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
753 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
754 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
755 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
756 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
757 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
758 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
759 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
760 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
761 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
762 
763 	{ }
764 };
765 MODULE_DEVICE_TABLE(hid, apple_devices);
766 
767 static struct hid_driver apple_driver = {
768 	.name = "apple",
769 	.id_table = apple_devices,
770 	.report_fixup = apple_report_fixup,
771 	.probe = apple_probe,
772 	.remove = apple_remove,
773 	.event = apple_event,
774 	.input_mapping = apple_input_mapping,
775 	.input_mapped = apple_input_mapped,
776 	.input_configured = apple_input_configured,
777 };
778 module_hid_driver(apple_driver);
779 
780 MODULE_LICENSE("GPL");
781