Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hid-apple.c
Go to the documentation of this file.
1 /*
2  * USB HID quirks support for Linux
3  *
4  * Copyright (c) 1999 Andreas Gal
5  * Copyright (c) 2000-2005 Vojtech Pavlik <[email protected]>
6  * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
7  * Copyright (c) 2006-2007 Jiri Kosina
8  * Copyright (c) 2008 Jiri Slaby <[email protected]>
9  */
10 
11 /*
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 2 of the License, or (at your option)
15  * any later version.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/device.h>
21 #include <linux/hid.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/usb.h>
25 
26 #include "hid-ids.h"
27 
28 #define APPLE_RDESC_JIS 0x0001
29 #define APPLE_IGNORE_MOUSE 0x0002
30 #define APPLE_HAS_FN 0x0004
31 #define APPLE_HIDDEV 0x0008
32 #define APPLE_ISO_KEYBOARD 0x0010
33 #define APPLE_MIGHTYMOUSE 0x0020
34 #define APPLE_INVERT_HWHEEL 0x0040
35 #define APPLE_IGNORE_HIDINPUT 0x0080
36 #define APPLE_NUMLOCK_EMULATION 0x0100
37 
38 #define APPLE_FLAG_FKEY 0x01
39 
40 static unsigned int fnmode = 1;
41 module_param(fnmode, uint, 0644);
42 MODULE_PARM_DESC(fnmode, "Mode of fn key on Apple keyboards (0 = disabled, "
43  "[1] = fkeyslast, 2 = fkeysfirst)");
44 
45 static unsigned int iso_layout = 1;
46 module_param(iso_layout, uint, 0644);
47 MODULE_PARM_DESC(iso_layout, "Enable/Disable hardcoded ISO-layout of the keyboard. "
48  "(0 = disabled, [1] = enabled)");
49 
50 struct apple_sc {
51  unsigned long quirks;
52  unsigned int fn_on;
53  DECLARE_BITMAP(pressed_fn, KEY_CNT);
54  DECLARE_BITMAP(pressed_numlock, KEY_CNT);
55 };
56 
61 };
62 
63 static const struct apple_key_translation macbookair_fn_keys[] = {
65  { KEY_ENTER, KEY_INSERT },
77  { KEY_UP, KEY_PAGEUP },
79  { KEY_LEFT, KEY_HOME },
80  { KEY_RIGHT, KEY_END },
81  { }
82 };
83 
84 static const struct apple_key_translation apple_fn_keys[] = {
86  { KEY_ENTER, KEY_INSERT },
99  { KEY_UP, KEY_PAGEUP },
100  { KEY_DOWN, KEY_PAGEDOWN },
101  { KEY_LEFT, KEY_HOME },
102  { KEY_RIGHT, KEY_END },
103  { }
104 };
105 
106 static const struct apple_key_translation powerbook_fn_keys[] = {
118  { KEY_UP, KEY_PAGEUP },
119  { KEY_DOWN, KEY_PAGEDOWN },
120  { KEY_LEFT, KEY_HOME },
121  { KEY_RIGHT, KEY_END },
122  { }
123 };
124 
125 static const struct apple_key_translation powerbook_numlock_keys[] = {
126  { KEY_J, KEY_KP1 },
127  { KEY_K, KEY_KP2 },
128  { KEY_L, KEY_KP3 },
129  { KEY_U, KEY_KP4 },
130  { KEY_I, KEY_KP5 },
131  { KEY_O, KEY_KP6 },
132  { KEY_7, KEY_KP7 },
133  { KEY_8, KEY_KP8 },
134  { KEY_9, KEY_KP9 },
135  { KEY_M, KEY_KP0 },
136  { KEY_DOT, KEY_KPDOT },
137  { KEY_SLASH, KEY_KPPLUS },
139  { KEY_P, KEY_KPASTERISK },
140  { KEY_MINUS, KEY_KPEQUAL },
141  { KEY_0, KEY_KPSLASH },
142  { KEY_F6, KEY_NUMLOCK },
145  { }
146 };
147 
148 static const struct apple_key_translation apple_iso_keyboard[] = {
149  { KEY_GRAVE, KEY_102ND },
150  { KEY_102ND, KEY_GRAVE },
151  { }
152 };
153 
154 static const struct apple_key_translation *apple_find_translation(
155  const struct apple_key_translation *table, u16 from)
156 {
157  const struct apple_key_translation *trans;
158 
159  /* Look for the translation */
160  for (trans = table; trans->from; trans++)
161  if (trans->from == from)
162  return trans;
163 
164  return NULL;
165 }
166 
167 static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
168  struct hid_usage *usage, __s32 value)
169 {
170  struct apple_sc *asc = hid_get_drvdata(hid);
171  const struct apple_key_translation *trans, *table;
172 
173  if (usage->code == KEY_FN) {
174  asc->fn_on = !!value;
175  input_event(input, usage->type, usage->code, value);
176  return 1;
177  }
178 
179  if (fnmode) {
180  int do_translate;
181 
184  table = macbookair_fn_keys;
185  else if (hid->product < 0x21d || hid->product >= 0x300)
186  table = powerbook_fn_keys;
187  else
188  table = apple_fn_keys;
189 
190  trans = apple_find_translation (table, usage->code);
191 
192  if (trans) {
193  if (test_bit(usage->code, asc->pressed_fn))
194  do_translate = 1;
195  else if (trans->flags & APPLE_FLAG_FKEY)
196  do_translate = (fnmode == 2 && asc->fn_on) ||
197  (fnmode == 1 && !asc->fn_on);
198  else
199  do_translate = asc->fn_on;
200 
201  if (do_translate) {
202  if (value)
203  set_bit(usage->code, asc->pressed_fn);
204  else
205  clear_bit(usage->code, asc->pressed_fn);
206 
207  input_event(input, usage->type, trans->to,
208  value);
209 
210  return 1;
211  }
212  }
213 
214  if (asc->quirks & APPLE_NUMLOCK_EMULATION &&
215  (test_bit(usage->code, asc->pressed_numlock) ||
216  test_bit(LED_NUML, input->led))) {
217  trans = apple_find_translation(powerbook_numlock_keys,
218  usage->code);
219 
220  if (trans) {
221  if (value)
222  set_bit(usage->code,
223  asc->pressed_numlock);
224  else
225  clear_bit(usage->code,
226  asc->pressed_numlock);
227 
228  input_event(input, usage->type, trans->to,
229  value);
230  }
231 
232  return 1;
233  }
234  }
235 
236  if (iso_layout) {
237  if (asc->quirks & APPLE_ISO_KEYBOARD) {
238  trans = apple_find_translation(apple_iso_keyboard, usage->code);
239  if (trans) {
240  input_event(input, usage->type, trans->to, value);
241  return 1;
242  }
243  }
244  }
245 
246  return 0;
247 }
248 
249 static int apple_event(struct hid_device *hdev, struct hid_field *field,
250  struct hid_usage *usage, __s32 value)
251 {
252  struct apple_sc *asc = hid_get_drvdata(hdev);
253 
254  if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
255  !usage->type)
256  return 0;
257 
258  if ((asc->quirks & APPLE_INVERT_HWHEEL) &&
259  usage->code == REL_HWHEEL) {
260  input_event(field->hidinput->input, usage->type, usage->code,
261  -value);
262  return 1;
263  }
264 
265  if ((asc->quirks & APPLE_HAS_FN) &&
266  hidinput_apple_event(hdev, field->hidinput->input,
267  usage, value))
268  return 1;
269 
270 
271  return 0;
272 }
273 
274 /*
275  * MacBook JIS keyboard has wrong logical maximum
276  */
277 static __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
278  unsigned int *rsize)
279 {
280  struct apple_sc *asc = hid_get_drvdata(hdev);
281 
282  if ((asc->quirks & APPLE_RDESC_JIS) && *rsize >= 60 &&
283  rdesc[53] == 0x65 && rdesc[59] == 0x65) {
284  hid_info(hdev,
285  "fixing up MacBook JIS keyboard report descriptor\n");
286  rdesc[53] = rdesc[59] = 0xe7;
287  }
288  return rdesc;
289 }
290 
291 static void apple_setup_input(struct input_dev *input)
292 {
293  const struct apple_key_translation *trans;
294 
295  set_bit(KEY_NUMLOCK, input->keybit);
296 
297  /* Enable all needed keys */
298  for (trans = apple_fn_keys; trans->from; trans++)
299  set_bit(trans->to, input->keybit);
300 
301  for (trans = powerbook_fn_keys; trans->from; trans++)
302  set_bit(trans->to, input->keybit);
303 
304  for (trans = powerbook_numlock_keys; trans->from; trans++)
305  set_bit(trans->to, input->keybit);
306 
307  for (trans = apple_iso_keyboard; trans->from; trans++)
308  set_bit(trans->to, input->keybit);
309 }
310 
311 static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
312  struct hid_field *field, struct hid_usage *usage,
313  unsigned long **bit, int *max)
314 {
315  if (usage->hid == (HID_UP_CUSTOM | 0x0003)) {
316  /* The fn key on Apple USB keyboards */
317  set_bit(EV_REP, hi->input->evbit);
318  hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_FN);
319  apple_setup_input(hi->input);
320  return 1;
321  }
322 
323  /* we want the hid layer to go through standard path (set and ignore) */
324  return 0;
325 }
326 
327 static int apple_input_mapped(struct hid_device *hdev, struct hid_input *hi,
328  struct hid_field *field, struct hid_usage *usage,
329  unsigned long **bit, int *max)
330 {
331  struct apple_sc *asc = hid_get_drvdata(hdev);
332 
333  if (asc->quirks & APPLE_MIGHTYMOUSE) {
334  if (usage->hid == HID_GD_Z)
335  hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
336  else if (usage->code == BTN_1)
337  hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_2);
338  else if (usage->code == BTN_2)
339  hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_1);
340  }
341 
342  return 0;
343 }
344 
345 static int apple_probe(struct hid_device *hdev,
346  const struct hid_device_id *id)
347 {
348  unsigned long quirks = id->driver_data;
349  struct apple_sc *asc;
350  unsigned int connect_mask = HID_CONNECT_DEFAULT;
351  int ret;
352 
353  asc = kzalloc(sizeof(*asc), GFP_KERNEL);
354  if (asc == NULL) {
355  hid_err(hdev, "can't alloc apple descriptor\n");
356  return -ENOMEM;
357  }
358 
359  asc->quirks = quirks;
360 
361  hid_set_drvdata(hdev, asc);
362 
363  ret = hid_parse(hdev);
364  if (ret) {
365  hid_err(hdev, "parse failed\n");
366  goto err_free;
367  }
368 
369  if (quirks & APPLE_HIDDEV)
370  connect_mask |= HID_CONNECT_HIDDEV_FORCE;
371  if (quirks & APPLE_IGNORE_HIDINPUT)
372  connect_mask &= ~HID_CONNECT_HIDINPUT;
373 
374  ret = hid_hw_start(hdev, connect_mask);
375  if (ret) {
376  hid_err(hdev, "hw start failed\n");
377  goto err_free;
378  }
379 
380  return 0;
381 err_free:
382  kfree(asc);
383  return ret;
384 }
385 
386 static void apple_remove(struct hid_device *hdev)
387 {
388  hid_hw_stop(hdev);
389  kfree(hid_get_drvdata(hdev));
390 }
391 
392 static const struct hid_device_id apple_devices[] = {
394  .driver_data = APPLE_HIDDEV | APPLE_IGNORE_HIDINPUT },
396  .driver_data = APPLE_HIDDEV | APPLE_IGNORE_HIDINPUT },
398  .driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
399 
401  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
403  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
405  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
407  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
410  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
412  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
414  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
417  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
418  APPLE_RDESC_JIS },
420  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
422  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
425  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
426  APPLE_RDESC_JIS },
428  .driver_data = APPLE_HAS_FN },
430  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
432  .driver_data = APPLE_HAS_FN },
434  .driver_data = APPLE_HAS_FN },
436  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
438  .driver_data = APPLE_HAS_FN },
440  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
442  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
444  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
445  APPLE_RDESC_JIS },
447  .driver_data = APPLE_HAS_FN },
449  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
451  .driver_data = APPLE_HAS_FN },
453  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
455  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
458  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
462  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
464  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
466  .driver_data = APPLE_HAS_FN },
468  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
470  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
472  .driver_data = APPLE_HAS_FN },
474  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
476  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
478  .driver_data = APPLE_HAS_FN },
480  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
482  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
484  .driver_data = APPLE_HAS_FN },
486  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
488  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
490  .driver_data = APPLE_HAS_FN },
492  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
494  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
496  .driver_data = APPLE_HAS_FN },
498  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
500  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
502  .driver_data = APPLE_HAS_FN },
504  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
506  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
508  .driver_data = APPLE_HAS_FN },
510  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
512  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
514  .driver_data = APPLE_HAS_FN },
516  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
518  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
520  .driver_data = APPLE_HAS_FN },
522  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
524  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
526  .driver_data = APPLE_HAS_FN },
528  .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
530  .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
532  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
534  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
537  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
539  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
541  .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
542 
543  { }
544 };
545 MODULE_DEVICE_TABLE(hid, apple_devices);
546 
547 static struct hid_driver apple_driver = {
548  .name = "apple",
549  .id_table = apple_devices,
550  .report_fixup = apple_report_fixup,
551  .probe = apple_probe,
552  .remove = apple_remove,
553  .event = apple_event,
554  .input_mapping = apple_input_mapping,
555  .input_mapped = apple_input_mapped,
556 };
557 
558 static int __init apple_init(void)
559 {
560  int ret;
561 
562  ret = hid_register_driver(&apple_driver);
563  if (ret)
564  pr_err("can't register apple driver\n");
565 
566  return ret;
567 }
568 
569 static void __exit apple_exit(void)
570 {
571  hid_unregister_driver(&apple_driver);
572 }
573 
574 module_init(apple_init);
575 module_exit(apple_exit);
576 MODULE_LICENSE("GPL");