blob: 8fcbac7f50ce92d651137019e1d7e6ca997b2c7a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Slaby8c19a512008-06-18 23:36:49 +02002/*
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
Jiri Slaby8c19a512008-06-18 23:36:49 +02009 * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
10 */
11
12/*
Jiri Slaby8c19a512008-06-18 23:36:49 +020013 */
14
Joe Perches4291ee32010-12-09 19:29:03 -080015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Jiri Slaby8c19a512008-06-18 23:36:49 +020017#include <linux/device.h>
18#include <linux/hid.h>
José Expósito6e143292021-11-18 08:29:55 +010019#include <linux/jiffies.h>
Jiri Slaby8c19a512008-06-18 23:36:49 +020020#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
José Expósito6e143292021-11-18 08:29:55 +010022#include <linux/timer.h>
Jiri Slaby8c19a512008-06-18 23:36:49 +020023
24#include "hid-ids.h"
25
José Expósito7f52ece2021-11-18 08:29:54 +010026#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)
José Expósito6e143292021-11-18 08:29:55 +010035#define APPLE_RDESC_BATTERY BIT(9)
Jiri Slaby8c19a512008-06-18 23:36:49 +020036
37#define APPLE_FLAG_FKEY 0x01
38
Alex Henriebd77a0f2017-06-02 09:28:39 -060039#define HID_COUNTRY_INTERNATIONAL_ISO 13
José Expósito6e143292021-11-18 08:29:55 +010040#define APPLE_BATTERY_TIMEOUT_MS 60000
Alex Henriebd77a0f2017-06-02 09:28:39 -060041
Jiri Slaby8c19a512008-06-18 23:36:49 +020042static unsigned int fnmode = 1;
43module_param(fnmode, uint, 0644);
44MODULE_PARM_DESC(fnmode, "Mode of fn key on Apple keyboards (0 = disabled, "
45 "[1] = fkeyslast, 2 = fkeysfirst)");
46
Alex Henried58cf342021-10-19 01:12:32 -060047static int iso_layout = -1;
48module_param(iso_layout, int, 0644);
49MODULE_PARM_DESC(iso_layout, "Swap the backtick/tilde and greater-than/less-than keys. "
50 "([-1] = auto, 0 = disabled, 1 = enabled)");
Stefan Glasenhardt54a65932010-01-05 23:30:30 +010051
Jiri Kosinaa94c79b2013-10-14 16:07:42 +020052static unsigned int swap_opt_cmd;
Nanno Langstraat43c83142013-10-14 16:07:15 +020053module_param(swap_opt_cmd, uint, 0644);
54MODULE_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
free5lot346338e2020-05-15 13:14:00 +070058static unsigned int swap_fn_leftctrl;
59module_param(swap_fn_leftctrl, uint, 0644);
60MODULE_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
Jiri Slaby8c19a512008-06-18 23:36:49 +020064struct apple_sc {
José Expósito6e143292021-11-18 08:29:55 +010065 struct hid_device *hdev;
Jiri Slaby8c19a512008-06-18 23:36:49 +020066 unsigned long quirks;
67 unsigned int fn_on;
Hans de Goedea5d81642020-06-20 14:32:29 +020068 unsigned int fn_found;
Jiri Slaby8c19a512008-06-18 23:36:49 +020069 DECLARE_BITMAP(pressed_numlock, KEY_CNT);
José Expósito6e143292021-11-18 08:29:55 +010070 struct timer_list battery_timer;
Jiri Slaby8c19a512008-06-18 23:36:49 +020071};
72
73struct apple_key_translation {
74 u16 from;
75 u16 to;
76 u8 flags;
77};
78
Benjamin Berg531cb562021-11-08 13:50:38 +010079static 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
Edgar (gimli) Hucek99b9f752010-11-03 10:36:18 -0400101static 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
Paul Collinsb2ddd542009-03-07 17:07:15 +1300122static const struct apple_key_translation apple_fn_keys[] = {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200123 { KEY_BACKSPACE, KEY_DELETE },
Henrik Rydberg437184a2008-11-04 13:31:38 +0100124 { KEY_ENTER, KEY_INSERT },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200125 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
126 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
Henrik Rydberg437184a2008-11-04 13:31:38 +0100127 { KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY },
128 { KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200129 { 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
Paul Collinsb2ddd542009-03-07 17:07:15 +1300144static const struct apple_key_translation powerbook_fn_keys[] = {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200145 { 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
Paul Collinsb2ddd542009-03-07 17:07:15 +1300163static const struct apple_key_translation powerbook_numlock_keys[] = {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200164 { 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
Paul Collinsb2ddd542009-03-07 17:07:15 +1300186static const struct apple_key_translation apple_iso_keyboard[] = {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200187 { KEY_GRAVE, KEY_102ND },
188 { KEY_102ND, KEY_GRAVE },
189 { }
190};
191
Nanno Langstraat43c83142013-10-14 16:07:15 +0200192static 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
free5lot346338e2020-05-15 13:14:00 +0700200static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
201 { KEY_FN, KEY_LEFTCTRL },
202 { }
203};
204
Paul Collinsb2ddd542009-03-07 17:07:15 +1300205static const struct apple_key_translation *apple_find_translation(
206 const struct apple_key_translation *table, u16 from)
Jiri Slaby8c19a512008-06-18 23:36:49 +0200207{
Paul Collinsb2ddd542009-03-07 17:07:15 +1300208 const struct apple_key_translation *trans;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200209
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
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200218static 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
Jiri Slaby8c19a512008-06-18 23:36:49 +0200227static 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);
Jiri Kosinaa4bc6922010-11-03 10:42:02 -0400231 const struct apple_key_translation *trans, *table;
Joao Morenoaec256d2019-09-03 16:46:32 +0200232 bool do_translate;
233 u16 code = 0;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200234
free5lot346338e2020-05-15 13:14:00 +0700235 u16 fn_keycode = (swap_fn_leftctrl) ? (KEY_LEFTCTRL) : (KEY_FN);
236
237 if (usage->code == fn_keycode) {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200238 asc->fn_on = !!value;
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200239 input_event_with_scancode(input, usage->type, KEY_FN,
240 usage->hid, value);
Jiri Slaby8c19a512008-06-18 23:36:49 +0200241 return 1;
242 }
243
244 if (fnmode) {
Benjamin Berg531cb562021-11-08 13:50:38 +0100245 if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021)
246 table = apple2021_fn_keys;
247 else if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
Jiri Kosinaa4bc6922010-11-03 10:42:02 -0400248 hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
249 table = macbookair_fn_keys;
250 else if (hid->product < 0x21d || hid->product >= 0x300)
251 table = powerbook_fn_keys;
252 else
253 table = apple_fn_keys;
254
255 trans = apple_find_translation (table, usage->code);
Edgar (gimli) Hucek99b9f752010-11-03 10:36:18 -0400256
Jiri Slaby8c19a512008-06-18 23:36:49 +0200257 if (trans) {
Joao Morenoaec256d2019-09-03 16:46:32 +0200258 if (test_bit(trans->from, input->key))
259 code = trans->from;
260 else if (test_bit(trans->to, input->key))
261 code = trans->to;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200262
Joao Morenoaec256d2019-09-03 16:46:32 +0200263 if (!code) {
264 if (trans->flags & APPLE_FLAG_FKEY) {
265 switch (fnmode) {
266 case 1:
267 do_translate = !asc->fn_on;
268 break;
269 case 2:
270 do_translate = asc->fn_on;
271 break;
272 default:
273 /* should never happen */
274 do_translate = false;
275 }
276 } else {
277 do_translate = asc->fn_on;
278 }
Jiri Slaby8c19a512008-06-18 23:36:49 +0200279
Joao Morenoaec256d2019-09-03 16:46:32 +0200280 code = do_translate ? trans->to : trans->from;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200281 }
Joao Morenoaec256d2019-09-03 16:46:32 +0200282
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200283 input_event_with_scancode(input, usage->type, code,
284 usage->hid, value);
Joao Morenoaec256d2019-09-03 16:46:32 +0200285 return 1;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200286 }
287
288 if (asc->quirks & APPLE_NUMLOCK_EMULATION &&
289 (test_bit(usage->code, asc->pressed_numlock) ||
290 test_bit(LED_NUML, input->led))) {
291 trans = apple_find_translation(powerbook_numlock_keys,
292 usage->code);
293
294 if (trans) {
295 if (value)
296 set_bit(usage->code,
297 asc->pressed_numlock);
298 else
299 clear_bit(usage->code,
300 asc->pressed_numlock);
301
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200302 input_event_with_scancode(input, usage->type,
303 trans->to, usage->hid, value);
Jiri Slaby8c19a512008-06-18 23:36:49 +0200304 }
305
306 return 1;
307 }
308 }
309
Alex Henried58cf342021-10-19 01:12:32 -0600310 if (iso_layout > 0 || (iso_layout < 0 && (asc->quirks & APPLE_ISO_TILDE_QUIRK) &&
311 hid->country == HID_COUNTRY_INTERNATIONAL_ISO)) {
312 trans = apple_find_translation(apple_iso_keyboard, usage->code);
313 if (trans) {
314 input_event_with_scancode(input, usage->type,
315 trans->to, usage->hid, value);
316 return 1;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200317 }
318 }
319
Nanno Langstraat43c83142013-10-14 16:07:15 +0200320 if (swap_opt_cmd) {
321 trans = apple_find_translation(swapped_option_cmd_keys, usage->code);
322 if (trans) {
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200323 input_event_with_scancode(input, usage->type,
324 trans->to, usage->hid, value);
Nanno Langstraat43c83142013-10-14 16:07:15 +0200325 return 1;
326 }
327 }
328
free5lot346338e2020-05-15 13:14:00 +0700329 if (swap_fn_leftctrl) {
330 trans = apple_find_translation(swapped_fn_leftctrl_keys, usage->code);
331 if (trans) {
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200332 input_event_with_scancode(input, usage->type,
333 trans->to, usage->hid, value);
free5lot346338e2020-05-15 13:14:00 +0700334 return 1;
335 }
336 }
337
Jiri Slaby8c19a512008-06-18 23:36:49 +0200338 return 0;
339}
340
341static int apple_event(struct hid_device *hdev, struct hid_field *field,
342 struct hid_usage *usage, __s32 value)
343{
344 struct apple_sc *asc = hid_get_drvdata(hdev);
345
346 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
347 !usage->type)
348 return 0;
349
350 if ((asc->quirks & APPLE_INVERT_HWHEEL) &&
351 usage->code == REL_HWHEEL) {
Vincent Lefevre3b41fb42021-07-22 03:25:44 +0200352 input_event_with_scancode(field->hidinput->input, usage->type,
353 usage->code, usage->hid, -value);
Jiri Slaby8c19a512008-06-18 23:36:49 +0200354 return 1;
355 }
356
357 if ((asc->quirks & APPLE_HAS_FN) &&
358 hidinput_apple_event(hdev, field->hidinput->input,
359 usage, value))
360 return 1;
361
362
363 return 0;
364}
365
José Expósito6e143292021-11-18 08:29:55 +0100366static int apple_fetch_battery(struct hid_device *hdev)
367{
368#ifdef CONFIG_HID_BATTERY_STRENGTH
369 struct apple_sc *asc = hid_get_drvdata(hdev);
370 struct hid_report_enum *report_enum;
371 struct hid_report *report;
372
373 if (!(asc->quirks & APPLE_RDESC_BATTERY) || !hdev->battery)
374 return -1;
375
376 report_enum = &hdev->report_enum[hdev->battery_report_type];
377 report = report_enum->report_id_hash[hdev->battery_report_id];
378
379 if (!report || report->maxfield < 1)
380 return -1;
381
382 if (hdev->battery_capacity == hdev->battery_max)
383 return -1;
384
385 hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
386 return 0;
387#else
388 return -1;
389#endif
390}
391
392static void apple_battery_timer_tick(struct timer_list *t)
393{
394 struct apple_sc *asc = from_timer(asc, t, battery_timer);
395 struct hid_device *hdev = asc->hdev;
396
397 if (apple_fetch_battery(hdev) == 0) {
398 mod_timer(&asc->battery_timer,
399 jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
400 }
401}
402
Jiri Slaby8c19a512008-06-18 23:36:49 +0200403/*
404 * MacBook JIS keyboard has wrong logical maximum
Mizuho Mori67fd71b2021-07-29 20:03:25 +0900405 * Magic Keyboard JIS has wrong logical maximum
Jiri Slaby8c19a512008-06-18 23:36:49 +0200406 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400407static __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
408 unsigned int *rsize)
Jiri Slaby8c19a512008-06-18 23:36:49 +0200409{
410 struct apple_sc *asc = hid_get_drvdata(hdev);
411
Mizuho Mori67fd71b2021-07-29 20:03:25 +0900412 if(*rsize >=71 && rdesc[70] == 0x65 && rdesc[64] == 0x65) {
413 hid_info(hdev,
414 "fixing up Magic Keyboard JIS report descriptor\n");
415 rdesc[64] = rdesc[70] = 0xe7;
416 }
417
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400418 if ((asc->quirks & APPLE_RDESC_JIS) && *rsize >= 60 &&
Jiri Slaby8c19a512008-06-18 23:36:49 +0200419 rdesc[53] == 0x65 && rdesc[59] == 0x65) {
Joe Perches4291ee32010-12-09 19:29:03 -0800420 hid_info(hdev,
421 "fixing up MacBook JIS keyboard report descriptor\n");
Jiri Slaby8c19a512008-06-18 23:36:49 +0200422 rdesc[53] = rdesc[59] = 0xe7;
423 }
José Expósito6e143292021-11-18 08:29:55 +0100424
425 /*
426 * Change the usage from:
427 * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 0
428 * 0x09, 0x0b, // Usage (Vendor Usage 0x0b) 3
429 * To:
430 * 0x05, 0x01, // Usage Page (Generic Desktop) 0
431 * 0x09, 0x06, // Usage (Keyboard) 2
432 */
433 if ((asc->quirks & APPLE_RDESC_BATTERY) && *rsize == 83 &&
434 rdesc[46] == 0x84 && rdesc[58] == 0x85) {
435 hid_info(hdev,
436 "fixing up Magic Keyboard battery report descriptor\n");
437 *rsize = *rsize - 1;
438 rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL);
439 if (!rdesc)
440 return NULL;
441
442 rdesc[0] = 0x05;
443 rdesc[1] = 0x01;
444 rdesc[2] = 0x09;
445 rdesc[3] = 0x06;
446 }
447
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400448 return rdesc;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200449}
450
451static void apple_setup_input(struct input_dev *input)
452{
Paul Collinsb2ddd542009-03-07 17:07:15 +1300453 const struct apple_key_translation *trans;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200454
455 set_bit(KEY_NUMLOCK, input->keybit);
456
457 /* Enable all needed keys */
458 for (trans = apple_fn_keys; trans->from; trans++)
459 set_bit(trans->to, input->keybit);
460
461 for (trans = powerbook_fn_keys; trans->from; trans++)
462 set_bit(trans->to, input->keybit);
463
464 for (trans = powerbook_numlock_keys; trans->from; trans++)
465 set_bit(trans->to, input->keybit);
466
467 for (trans = apple_iso_keyboard; trans->from; trans++)
468 set_bit(trans->to, input->keybit);
free5lot346338e2020-05-15 13:14:00 +0700469
Benjamin Berg531cb562021-11-08 13:50:38 +0100470 for (trans = apple2021_fn_keys; trans->from; trans++)
471 set_bit(trans->to, input->keybit);
472
free5lot346338e2020-05-15 13:14:00 +0700473 if (swap_fn_leftctrl) {
474 for (trans = swapped_fn_leftctrl_keys; trans->from; trans++)
475 set_bit(trans->to, input->keybit);
476 }
Jiri Slaby8c19a512008-06-18 23:36:49 +0200477}
478
479static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
480 struct hid_field *field, struct hid_usage *usage,
481 unsigned long **bit, int *max)
482{
Hans de Goedea5d81642020-06-20 14:32:29 +0200483 struct apple_sc *asc = hid_get_drvdata(hdev);
484
Sean O'Brienee345492018-08-27 13:02:15 -0700485 if (usage->hid == (HID_UP_CUSTOM | 0x0003) ||
Mansour Behabadie433be92020-01-29 17:26:31 +1100486 usage->hid == (HID_UP_MSVENDOR | 0x0003) ||
487 usage->hid == (HID_UP_HPVENDOR2 | 0x0003)) {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200488 /* The fn key on Apple USB keyboards */
489 set_bit(EV_REP, hi->input->evbit);
490 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_FN);
Hans de Goedea5d81642020-06-20 14:32:29 +0200491 asc->fn_found = true;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200492 apple_setup_input(hi->input);
493 return 1;
494 }
495
496 /* we want the hid layer to go through standard path (set and ignore) */
497 return 0;
498}
499
500static int apple_input_mapped(struct hid_device *hdev, struct hid_input *hi,
501 struct hid_field *field, struct hid_usage *usage,
502 unsigned long **bit, int *max)
503{
504 struct apple_sc *asc = hid_get_drvdata(hdev);
505
506 if (asc->quirks & APPLE_MIGHTYMOUSE) {
507 if (usage->hid == HID_GD_Z)
508 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
509 else if (usage->code == BTN_1)
510 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_2);
511 else if (usage->code == BTN_2)
512 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_1);
513 }
514
515 return 0;
516}
517
Hans de Goedea5d81642020-06-20 14:32:29 +0200518static int apple_input_configured(struct hid_device *hdev,
519 struct hid_input *hidinput)
520{
521 struct apple_sc *asc = hid_get_drvdata(hdev);
522
523 if ((asc->quirks & APPLE_HAS_FN) && !asc->fn_found) {
524 hid_info(hdev, "Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling\n");
José Expósitoa5fe7862021-11-18 08:29:53 +0100525 asc->quirks &= ~APPLE_HAS_FN;
Hans de Goedea5d81642020-06-20 14:32:29 +0200526 }
527
528 return 0;
529}
530
Jiri Slaby8c19a512008-06-18 23:36:49 +0200531static int apple_probe(struct hid_device *hdev,
532 const struct hid_device_id *id)
533{
534 unsigned long quirks = id->driver_data;
535 struct apple_sc *asc;
536 int ret;
537
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200538 asc = devm_kzalloc(&hdev->dev, sizeof(*asc), GFP_KERNEL);
Jiri Slaby8c19a512008-06-18 23:36:49 +0200539 if (asc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800540 hid_err(hdev, "can't alloc apple descriptor\n");
Jiri Slaby8c19a512008-06-18 23:36:49 +0200541 return -ENOMEM;
542 }
543
José Expósito6e143292021-11-18 08:29:55 +0100544 asc->hdev = hdev;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200545 asc->quirks = quirks;
546
547 hid_set_drvdata(hdev, asc);
548
Jiri Slaby8c19a512008-06-18 23:36:49 +0200549 ret = hid_parse(hdev);
550 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800551 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200552 return ret;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200553 }
554
Alexander F. Lentcc840942021-09-16 15:38:40 -0400555 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby8c19a512008-06-18 23:36:49 +0200556 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800557 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200558 return ret;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200559 }
560
José Expósito6e143292021-11-18 08:29:55 +0100561 timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0);
562 mod_timer(&asc->battery_timer,
563 jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
564 apple_fetch_battery(hdev);
565
Jiri Slaby8c19a512008-06-18 23:36:49 +0200566 return 0;
Jiri Slaby8c19a512008-06-18 23:36:49 +0200567}
568
José Expósito6e143292021-11-18 08:29:55 +0100569static void apple_remove(struct hid_device *hdev)
570{
571 struct apple_sc *asc = hid_get_drvdata(hdev);
572
573 del_timer_sync(&asc->battery_timer);
574
575 hid_hw_stop(hdev);
576}
577
Jiri Slaby8c19a512008-06-18 23:36:49 +0200578static const struct hid_device_id apple_devices[] = {
Jiri Slaby8c19a512008-06-18 23:36:49 +0200579 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
580 .driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
581
582 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200583 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200584 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200585 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200586 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200587 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200588 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600589 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200590 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200591 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200592 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200593 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200594 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600595 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200596 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS),
597 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200598 APPLE_RDESC_JIS },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200599 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200600 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200601 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600602 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200603 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS),
604 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200605 APPLE_RDESC_JIS },
Ryan Finniefef3f572009-03-05 10:18:01 +0100606 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI),
607 .driver_data = APPLE_HAS_FN },
608 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600609 .driver_data = APPLE_HAS_FN },
Ryan Finniefef3f572009-03-05 10:18:01 +0100610 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS),
611 .driver_data = APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200612 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI),
613 .driver_data = APPLE_HAS_FN },
614 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600615 .driver_data = APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200616 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS),
617 .driver_data = APPLE_HAS_FN },
618 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200619 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200620 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600621 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200622 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS),
623 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200624 APPLE_RDESC_JIS },
Dan Bastone4a4c8792011-07-31 07:40:49 -0400625 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
626 .driver_data = APPLE_HAS_FN },
Haochen Tongebe0b42a2021-07-18 01:04:31 +0800627 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ANSI),
628 .driver_data = APPLE_HAS_FN },
Dan Bastone4a4c8792011-07-31 07:40:49 -0400629 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600630 .driver_data = APPLE_HAS_FN },
BALATON Zoltan4c945422020-07-20 22:53:46 +0200631 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_ISO),
632 .driver_data = APPLE_HAS_FN },
Dan Bastone4a4c8792011-07-31 07:40:49 -0400633 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_REVB_JIS),
634 .driver_data = APPLE_HAS_FN },
Jan Scholzee8a1a02008-11-26 15:33:45 +0100635 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
Jiri Slaby8c19a512008-06-18 23:36:49 +0200636 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jan Scholzee8a1a02008-11-26 15:33:45 +0100637 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO),
Alex Henried58cf342021-10-19 01:12:32 -0600638 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
639 APPLE_ISO_TILDE_QUIRK },
Andreas Kristad734bc2011-10-28 18:50:39 +0200640 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
Alex Henried58cf342021-10-19 01:12:32 -0600641 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
642 APPLE_ISO_TILDE_QUIRK },
Alexey Kaminsky0a97e1e2012-04-23 18:02:18 +0200643 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
644 USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
645 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Huei-Horng Yobd4a7ce2014-02-06 17:40:34 +0800646 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
647 USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS),
648 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jan Scholzee8a1a02008-11-26 15:33:45 +0100649 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS),
Jiri Slaby8c19a512008-06-18 23:36:49 +0200650 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Alex Henrie371a9dc2021-10-08 01:37:00 -0600651 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
José Expósito6e143292021-11-18 08:29:55 +0100652 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
Alex Henrie371a9dc2021-10-08 01:37:00 -0600653 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015),
Alex Henried58cf342021-10-19 01:12:32 -0600654 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
Alex Henrie371a9dc2021-10-08 01:37:00 -0600655 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
José Expósito6e143292021-11-18 08:29:55 +0100656 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY },
Alex Henrie371a9dc2021-10-08 01:37:00 -0600657 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015),
Alex Henried58cf342021-10-19 01:12:32 -0600658 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200659 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200660 .driver_data = APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200661 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600662 .driver_data = APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200663 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200664 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200665 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200666 .driver_data = APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200667 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600668 .driver_data = APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200669 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200670 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Henrik Rydberga96d6ef2008-11-04 20:03:45 +0100671 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI),
672 .driver_data = APPLE_HAS_FN },
673 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600674 .driver_data = APPLE_HAS_FN },
Henrik Rydberga96d6ef2008-11-04 20:03:45 +0100675 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS),
676 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Edgar (gimli) Hucek99b9f752010-11-03 10:36:18 -0400677 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI),
678 .driver_data = APPLE_HAS_FN },
679 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600680 .driver_data = APPLE_HAS_FN },
Edgar (gimli) Hucek99b9f752010-11-03 10:36:18 -0400681 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4_JIS),
682 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
683 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ANSI),
684 .driver_data = APPLE_HAS_FN },
685 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600686 .driver_data = APPLE_HAS_FN },
Edgar (gimli) Hucek99b9f752010-11-03 10:36:18 -0400687 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS),
688 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Andy Botting47340bd2011-03-12 20:27:22 -0800689 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ANSI),
690 .driver_data = APPLE_HAS_FN },
691 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600692 .driver_data = APPLE_HAS_FN },
Andy Botting47340bd2011-03-12 20:27:22 -0800693 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5_JIS),
694 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Joshua V. Dillon5d922ba2011-08-05 12:05:22 -0700695 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ANSI),
696 .driver_data = APPLE_HAS_FN },
697 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600698 .driver_data = APPLE_HAS_FN },
Joshua V. Dillon5d922ba2011-08-05 12:05:22 -0700699 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6_JIS),
700 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Nobuhiro Iwamatsud762cc22011-10-01 15:54:53 +0900701 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ANSI),
702 .driver_data = APPLE_HAS_FN },
703 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600704 .driver_data = APPLE_HAS_FN },
Nobuhiro Iwamatsud762cc22011-10-01 15:54:53 +0900705 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING6A_JIS),
706 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Gökçen Eraslan213f9da2011-10-22 22:39:06 +0300707 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ANSI),
708 .driver_data = APPLE_HAS_FN },
709 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600710 .driver_data = APPLE_HAS_FN },
Gökçen Eraslan213f9da2011-10-22 22:39:06 +0300711 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING5A_JIS),
712 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Ryan Bourgeoisb2e6ad72012-07-10 09:43:33 -0700713 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ANSI),
714 .driver_data = APPLE_HAS_FN },
715 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600716 .driver_data = APPLE_HAS_FN },
Ryan Bourgeoisb2e6ad72012-07-10 09:43:33 -0700717 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7_JIS),
718 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Dirk Hohndel8d80da92012-10-30 11:11:48 -0700719 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ANSI),
720 .driver_data = APPLE_HAS_FN },
721 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600722 .driver_data = APPLE_HAS_FN },
Dirk Hohndel8d80da92012-10-30 11:11:48 -0700723 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING7A_JIS),
724 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Dmitry Torokhov9d9a04e2013-07-01 11:46:27 -0700725 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ANSI),
726 .driver_data = APPLE_HAS_FN },
727 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600728 .driver_data = APPLE_HAS_FN },
Dmitry Torokhov9d9a04e2013-07-01 11:46:27 -0700729 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING8_JIS),
730 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Henrik Rydberga4a2c542015-07-24 14:45:05 -0700731 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI),
732 .driver_data = APPLE_HAS_FN },
733 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO),
Alex Henriebd77a0f2017-06-02 09:28:39 -0600734 .driver_data = APPLE_HAS_FN },
Henrik Rydberga4a2c542015-07-24 14:45:05 -0700735 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
736 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
Christian Schuerer-Waldheim23aeb612010-01-06 14:49:57 +0100737 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
738 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
739 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
Alex Henried58cf342021-10-19 01:12:32 -0600740 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
741 APPLE_ISO_TILDE_QUIRK },
Christian Schuerer-Waldheim23aeb612010-01-06 14:49:57 +0100742 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS),
743 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200744 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200745 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200746 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY),
Jiri Slabyb4d8e472008-10-22 14:47:18 +0200747 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
Alex Henrie0cd3be52021-10-08 01:37:01 -0600748 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
Alex Henried58cf342021-10-19 01:12:32 -0600749 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
Alex Henrie0cd3be52021-10-08 01:37:01 -0600750 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
Alex Henried58cf342021-10-19 01:12:32 -0600751 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
Alex Henrie9f92d612021-12-01 23:16:50 -0700752 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
753 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
754 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
755 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
Alex Henrieb2dcade2021-12-01 23:16:51 -0700756 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
757 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
758 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
759 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
Jiri Slaby8c19a512008-06-18 23:36:49 +0200760
Jiri Slaby8c19a512008-06-18 23:36:49 +0200761 { }
762};
763MODULE_DEVICE_TABLE(hid, apple_devices);
764
765static struct hid_driver apple_driver = {
766 .name = "apple",
767 .id_table = apple_devices,
768 .report_fixup = apple_report_fixup,
769 .probe = apple_probe,
José Expósito6e143292021-11-18 08:29:55 +0100770 .remove = apple_remove,
Jiri Slaby8c19a512008-06-18 23:36:49 +0200771 .event = apple_event,
772 .input_mapping = apple_input_mapping,
773 .input_mapped = apple_input_mapped,
Hans de Goedea5d81642020-06-20 14:32:29 +0200774 .input_configured = apple_input_configured,
Jiri Slaby8c19a512008-06-18 23:36:49 +0200775};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700776module_hid_driver(apple_driver);
Jiri Slaby8c19a512008-06-18 23:36:49 +0200777
Jiri Slaby8c19a512008-06-18 23:36:49 +0200778MODULE_LICENSE("GPL");