2 * USB HID quirks support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
27 #define APPLE_RDESC_JIS 0x0001
28 #define APPLE_IGNORE_MOUSE 0x0002
29 #define APPLE_HAS_FN 0x0004
30 #define APPLE_HIDDEV 0x0008
31 #define APPLE_ISO_KEYBOARD 0x0010
32 #define APPLE_MIGHTYMOUSE 0x0020
33 #define APPLE_INVERT_HWHEEL 0x0040
34 #define APPLE_IGNORE_HIDINPUT 0x0080
35 #define APPLE_NUMLOCK_EMULATION 0x0100
37 #define APPLE_FLAG_FKEY 0x01
39 static unsigned int fnmode
= 1;
40 module_param(fnmode
, uint
, 0644);
41 MODULE_PARM_DESC(fnmode
, "Mode of fn key on Apple keyboards (0 = disabled, "
42 "[1] = fkeyslast, 2 = fkeysfirst)");
44 static unsigned int iso_layout
= 1;
45 module_param(iso_layout
, uint
, 0644);
46 MODULE_PARM_DESC(iso_layout
, "Enable/Disable hardcoded ISO-layout of the keyboard. "
47 "(0 = disabled, [1] = enabled)");
52 DECLARE_BITMAP(pressed_fn
, KEY_CNT
);
53 DECLARE_BITMAP(pressed_numlock
, KEY_CNT
);
56 struct apple_key_translation
{
62 static const struct apple_key_translation apple_fn_keys
[] = {
63 { KEY_BACKSPACE
, KEY_DELETE
},
64 { KEY_ENTER
, KEY_INSERT
},
65 { KEY_F1
, KEY_BRIGHTNESSDOWN
, APPLE_FLAG_FKEY
},
66 { KEY_F2
, KEY_BRIGHTNESSUP
, APPLE_FLAG_FKEY
},
67 { KEY_F3
, KEY_SCALE
, APPLE_FLAG_FKEY
},
68 { KEY_F4
, KEY_DASHBOARD
, APPLE_FLAG_FKEY
},
69 { KEY_F5
, KEY_KBDILLUMDOWN
, APPLE_FLAG_FKEY
},
70 { KEY_F6
, KEY_KBDILLUMUP
, APPLE_FLAG_FKEY
},
71 { KEY_F7
, KEY_PREVIOUSSONG
, APPLE_FLAG_FKEY
},
72 { KEY_F8
, KEY_PLAYPAUSE
, APPLE_FLAG_FKEY
},
73 { KEY_F9
, KEY_NEXTSONG
, APPLE_FLAG_FKEY
},
74 { KEY_F10
, KEY_MUTE
, APPLE_FLAG_FKEY
},
75 { KEY_F11
, KEY_VOLUMEDOWN
, APPLE_FLAG_FKEY
},
76 { KEY_F12
, KEY_VOLUMEUP
, APPLE_FLAG_FKEY
},
77 { KEY_UP
, KEY_PAGEUP
},
78 { KEY_DOWN
, KEY_PAGEDOWN
},
79 { KEY_LEFT
, KEY_HOME
},
80 { KEY_RIGHT
, KEY_END
},
84 static const struct apple_key_translation powerbook_fn_keys
[] = {
85 { KEY_BACKSPACE
, KEY_DELETE
},
86 { KEY_F1
, KEY_BRIGHTNESSDOWN
, APPLE_FLAG_FKEY
},
87 { KEY_F2
, KEY_BRIGHTNESSUP
, APPLE_FLAG_FKEY
},
88 { KEY_F3
, KEY_MUTE
, APPLE_FLAG_FKEY
},
89 { KEY_F4
, KEY_VOLUMEDOWN
, APPLE_FLAG_FKEY
},
90 { KEY_F5
, KEY_VOLUMEUP
, APPLE_FLAG_FKEY
},
91 { KEY_F6
, KEY_NUMLOCK
, APPLE_FLAG_FKEY
},
92 { KEY_F7
, KEY_SWITCHVIDEOMODE
, APPLE_FLAG_FKEY
},
93 { KEY_F8
, KEY_KBDILLUMTOGGLE
, APPLE_FLAG_FKEY
},
94 { KEY_F9
, KEY_KBDILLUMDOWN
, APPLE_FLAG_FKEY
},
95 { KEY_F10
, KEY_KBDILLUMUP
, APPLE_FLAG_FKEY
},
96 { KEY_UP
, KEY_PAGEUP
},
97 { KEY_DOWN
, KEY_PAGEDOWN
},
98 { KEY_LEFT
, KEY_HOME
},
99 { KEY_RIGHT
, KEY_END
},
103 static const struct apple_key_translation powerbook_numlock_keys
[] = {
114 { KEY_DOT
, KEY_KPDOT
},
115 { KEY_SLASH
, KEY_KPPLUS
},
116 { KEY_SEMICOLON
, KEY_KPMINUS
},
117 { KEY_P
, KEY_KPASTERISK
},
118 { KEY_MINUS
, KEY_KPEQUAL
},
119 { KEY_0
, KEY_KPSLASH
},
120 { KEY_F6
, KEY_NUMLOCK
},
121 { KEY_KPENTER
, KEY_KPENTER
},
122 { KEY_BACKSPACE
, KEY_BACKSPACE
},
126 static const struct apple_key_translation apple_iso_keyboard
[] = {
127 { KEY_GRAVE
, KEY_102ND
},
128 { KEY_102ND
, KEY_GRAVE
},
132 static const struct apple_key_translation
*apple_find_translation(
133 const struct apple_key_translation
*table
, u16 from
)
135 const struct apple_key_translation
*trans
;
137 /* Look for the translation */
138 for (trans
= table
; trans
->from
; trans
++)
139 if (trans
->from
== from
)
145 static int hidinput_apple_event(struct hid_device
*hid
, struct input_dev
*input
,
146 struct hid_usage
*usage
, __s32 value
)
148 struct apple_sc
*asc
= hid_get_drvdata(hid
);
149 const struct apple_key_translation
*trans
;
151 if (usage
->code
== KEY_FN
) {
152 asc
->fn_on
= !!value
;
153 input_event(input
, usage
->type
, usage
->code
, value
);
160 trans
= apple_find_translation((hid
->product
< 0x21d ||
161 hid
->product
>= 0x300) ?
162 powerbook_fn_keys
: apple_fn_keys
,
165 if (test_bit(usage
->code
, asc
->pressed_fn
))
167 else if (trans
->flags
& APPLE_FLAG_FKEY
)
168 do_translate
= (fnmode
== 2 && asc
->fn_on
) ||
169 (fnmode
== 1 && !asc
->fn_on
);
171 do_translate
= asc
->fn_on
;
175 set_bit(usage
->code
, asc
->pressed_fn
);
177 clear_bit(usage
->code
, asc
->pressed_fn
);
179 input_event(input
, usage
->type
, trans
->to
,
186 if (asc
->quirks
& APPLE_NUMLOCK_EMULATION
&&
187 (test_bit(usage
->code
, asc
->pressed_numlock
) ||
188 test_bit(LED_NUML
, input
->led
))) {
189 trans
= apple_find_translation(powerbook_numlock_keys
,
195 asc
->pressed_numlock
);
197 clear_bit(usage
->code
,
198 asc
->pressed_numlock
);
200 input_event(input
, usage
->type
, trans
->to
,
209 if (asc
->quirks
& APPLE_ISO_KEYBOARD
) {
210 trans
= apple_find_translation(apple_iso_keyboard
, usage
->code
);
212 input_event(input
, usage
->type
, trans
->to
, value
);
221 static int apple_event(struct hid_device
*hdev
, struct hid_field
*field
,
222 struct hid_usage
*usage
, __s32 value
)
224 struct apple_sc
*asc
= hid_get_drvdata(hdev
);
226 if (!(hdev
->claimed
& HID_CLAIMED_INPUT
) || !field
->hidinput
||
230 if ((asc
->quirks
& APPLE_INVERT_HWHEEL
) &&
231 usage
->code
== REL_HWHEEL
) {
232 input_event(field
->hidinput
->input
, usage
->type
, usage
->code
,
237 if ((asc
->quirks
& APPLE_HAS_FN
) &&
238 hidinput_apple_event(hdev
, field
->hidinput
->input
,
247 * MacBook JIS keyboard has wrong logical maximum
249 static void apple_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
252 struct apple_sc
*asc
= hid_get_drvdata(hdev
);
254 if ((asc
->quirks
& APPLE_RDESC_JIS
) && rsize
>= 60 &&
255 rdesc
[53] == 0x65 && rdesc
[59] == 0x65) {
256 dev_info(&hdev
->dev
, "fixing up MacBook JIS keyboard report "
258 rdesc
[53] = rdesc
[59] = 0xe7;
262 static void apple_setup_input(struct input_dev
*input
)
264 const struct apple_key_translation
*trans
;
266 set_bit(KEY_NUMLOCK
, input
->keybit
);
268 /* Enable all needed keys */
269 for (trans
= apple_fn_keys
; trans
->from
; trans
++)
270 set_bit(trans
->to
, input
->keybit
);
272 for (trans
= powerbook_fn_keys
; trans
->from
; trans
++)
273 set_bit(trans
->to
, input
->keybit
);
275 for (trans
= powerbook_numlock_keys
; trans
->from
; trans
++)
276 set_bit(trans
->to
, input
->keybit
);
278 for (trans
= apple_iso_keyboard
; trans
->from
; trans
++)
279 set_bit(trans
->to
, input
->keybit
);
282 static int apple_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
283 struct hid_field
*field
, struct hid_usage
*usage
,
284 unsigned long **bit
, int *max
)
286 if (usage
->hid
== (HID_UP_CUSTOM
| 0x0003)) {
287 /* The fn key on Apple USB keyboards */
288 set_bit(EV_REP
, hi
->input
->evbit
);
289 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, KEY_FN
);
290 apple_setup_input(hi
->input
);
294 /* we want the hid layer to go through standard path (set and ignore) */
298 static int apple_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
299 struct hid_field
*field
, struct hid_usage
*usage
,
300 unsigned long **bit
, int *max
)
302 struct apple_sc
*asc
= hid_get_drvdata(hdev
);
304 if (asc
->quirks
& APPLE_MIGHTYMOUSE
) {
305 if (usage
->hid
== HID_GD_Z
)
306 hid_map_usage(hi
, usage
, bit
, max
, EV_REL
, REL_HWHEEL
);
307 else if (usage
->code
== BTN_1
)
308 hid_map_usage(hi
, usage
, bit
, max
, EV_KEY
, BTN_2
);
309 else if (usage
->code
== BTN_2
)
310 hid_map_usage(hi
, usage
, bit
, max
, EV_KEY
, BTN_1
);
316 static int apple_probe(struct hid_device
*hdev
,
317 const struct hid_device_id
*id
)
319 unsigned long quirks
= id
->driver_data
;
320 struct apple_sc
*asc
;
321 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
324 asc
= kzalloc(sizeof(*asc
), GFP_KERNEL
);
326 dev_err(&hdev
->dev
, "can't alloc apple descriptor\n");
330 asc
->quirks
= quirks
;
332 hid_set_drvdata(hdev
, asc
);
334 ret
= hid_parse(hdev
);
336 dev_err(&hdev
->dev
, "parse failed\n");
340 if (quirks
& APPLE_HIDDEV
)
341 connect_mask
|= HID_CONNECT_HIDDEV_FORCE
;
342 if (quirks
& APPLE_IGNORE_HIDINPUT
)
343 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
345 ret
= hid_hw_start(hdev
, connect_mask
);
347 dev_err(&hdev
->dev
, "hw start failed\n");
357 static void apple_remove(struct hid_device
*hdev
)
360 kfree(hid_get_drvdata(hdev
));
363 static const struct hid_device_id apple_devices
[] = {
364 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ATV_IRCONTROL
),
365 .driver_data
= APPLE_HIDDEV
| APPLE_IGNORE_HIDINPUT
},
366 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_IRCONTROL4
),
367 .driver_data
= APPLE_HIDDEV
| APPLE_IGNORE_HIDINPUT
},
368 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_MIGHTYMOUSE
),
369 .driver_data
= APPLE_MIGHTYMOUSE
| APPLE_INVERT_HWHEEL
},
371 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI
),
372 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
373 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO
),
374 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
375 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER_ANSI
),
376 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
377 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER_ISO
),
378 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
379 APPLE_ISO_KEYBOARD
},
380 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER_JIS
),
381 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
382 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER3_ANSI
),
383 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
384 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER3_ISO
),
385 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
386 APPLE_ISO_KEYBOARD
},
387 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER3_JIS
),
388 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
390 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER4_ANSI
),
391 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
392 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER4_ISO
),
393 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
394 APPLE_ISO_KEYBOARD
},
395 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER4_JIS
),
396 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
398 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI
),
399 .driver_data
= APPLE_HAS_FN
},
400 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_MINI_ISO
),
401 .driver_data
= APPLE_HAS_FN
| APPLE_ISO_KEYBOARD
},
402 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_MINI_JIS
),
403 .driver_data
= APPLE_HAS_FN
},
404 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_ANSI
),
405 .driver_data
= APPLE_HAS_FN
},
406 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_ISO
),
407 .driver_data
= APPLE_HAS_FN
| APPLE_ISO_KEYBOARD
},
408 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_JIS
),
409 .driver_data
= APPLE_HAS_FN
},
410 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI
),
411 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
412 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO
),
413 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
414 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS
),
415 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
417 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI
),
418 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
419 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO
),
420 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
421 APPLE_ISO_KEYBOARD
},
422 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS
),
423 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
424 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI
),
425 .driver_data
= APPLE_HAS_FN
},
426 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING_ISO
),
427 .driver_data
= APPLE_HAS_FN
| APPLE_ISO_KEYBOARD
},
428 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING_JIS
),
429 .driver_data
= APPLE_HAS_FN
| APPLE_RDESC_JIS
},
430 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI
),
431 .driver_data
= APPLE_HAS_FN
},
432 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO
),
433 .driver_data
= APPLE_HAS_FN
| APPLE_ISO_KEYBOARD
},
434 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS
),
435 .driver_data
= APPLE_HAS_FN
| APPLE_RDESC_JIS
},
436 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING3_ANSI
),
437 .driver_data
= APPLE_HAS_FN
},
438 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING3_ISO
),
439 .driver_data
= APPLE_HAS_FN
| APPLE_ISO_KEYBOARD
},
440 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_WELLSPRING3_JIS
),
441 .driver_data
= APPLE_HAS_FN
| APPLE_RDESC_JIS
},
442 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI
),
443 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
444 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO
),
445 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
|
446 APPLE_ISO_KEYBOARD
},
447 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS
),
448 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
449 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY
),
450 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
451 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE
, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY
),
452 .driver_data
= APPLE_NUMLOCK_EMULATION
| APPLE_HAS_FN
},
456 MODULE_DEVICE_TABLE(hid
, apple_devices
);
458 static struct hid_driver apple_driver
= {
460 .id_table
= apple_devices
,
461 .report_fixup
= apple_report_fixup
,
462 .probe
= apple_probe
,
463 .remove
= apple_remove
,
464 .event
= apple_event
,
465 .input_mapping
= apple_input_mapping
,
466 .input_mapped
= apple_input_mapped
,
469 static int __init
apple_init(void)
473 ret
= hid_register_driver(&apple_driver
);
475 printk(KERN_ERR
"can't register apple driver\n");
480 static void __exit
apple_exit(void)
482 hid_unregister_driver(&apple_driver
);
485 module_init(apple_init
);
486 module_exit(apple_exit
);
487 MODULE_LICENSE("GPL");