HID: Map MS Presenter 8000 bottom-side buttons
[safe/jmp/linux-2.6] / drivers / hid / hid-input.c
index 60de16a..3a18ec4 100644 (file)
@@ -53,7 +53,7 @@ static const unsigned char hid_keyboard[256] = {
        115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
        122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
-       unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
+       unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
         29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
@@ -86,6 +86,11 @@ static const struct {
 #define map_abs_clear(c)       do { map_abs(c); clear_bit(c, bit); } while (0)
 #define map_key_clear(c)       do { map_key(c); clear_bit(c, bit); } while (0)
 
+/* hardware needing special handling due to colliding MSVENDOR page usages */
+#define IS_CHICONY_TACTICAL_PAD(x) (x->vendor == 0x04f2 && device->product == 0x0418)
+#define IS_MS_KB(x) (x->vendor == 0x045e && (x->product == 0x00db || x->product == 0x00f9))
+#define IS_MS_PRESENTER_8000(x) (x->vendor == 0x045e && x->product == 0x0713)
+
 #ifdef CONFIG_USB_HIDINPUT_POWERBOOK
 
 struct hidinput_key_translation {
@@ -293,9 +298,9 @@ static struct hid_usage *hidinput_find_key(struct hid_device *hid,
 static int hidinput_getkeycode(struct input_dev *dev, int scancode,
                                int *keycode)
 {
-       struct hid_device *hid = dev->private;
+       struct hid_device *hid = input_get_drvdata(dev);
        struct hid_usage *usage;
-       
+
        usage = hidinput_find_key(hid, scancode, 0);
        if (usage) {
                *keycode = usage->code;
@@ -307,31 +312,29 @@ static int hidinput_getkeycode(struct input_dev *dev, int scancode,
 static int hidinput_setkeycode(struct input_dev *dev, int scancode,
                                int keycode)
 {
-       struct hid_device *hid = dev->private;
+       struct hid_device *hid = input_get_drvdata(dev);
        struct hid_usage *usage;
        int old_keycode;
-       
+
        if (keycode < 0 || keycode > KEY_MAX)
                return -EINVAL;
-       
+
        usage = hidinput_find_key(hid, scancode, 0);
        if (usage) {
                old_keycode = usage->code;
                usage->code = keycode;
-               
+
                clear_bit(old_keycode, dev->keybit);
                set_bit(usage->code, dev->keybit);
-#ifdef CONFIG_HID_DEBUG
-               printk (KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
-#endif
+               dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
                /* Set the keybit for the old keycode if the old keycode is used
                 * by another key */
                if (hidinput_find_key (hid, 0, old_keycode))
                        set_bit(old_keycode, dev->keybit);
-               
+
                return 0;
        }
-       
+
        return -EINVAL;
 }
 
@@ -346,15 +349,20 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 
        field->hidinput = hidinput;
 
-#ifdef CONFIG_HID_DEBUG
-       printk(KERN_DEBUG "Mapping: ");
+       dbg_hid("Mapping: ");
        hid_resolv_usage(usage->hid);
-       printk(" ---> ");
-#endif
+       dbg_hid_line(" ---> ");
 
        if (field->flags & HID_MAIN_ITEM_CONSTANT)
                goto ignore;
 
+       /* only LED usages are supported in output fields */
+       if (field->report_type == HID_OUTPUT_REPORT &&
+                       (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
+               dbg_hid_line(" [non-LED output field] ");
+               goto ignore;
+       }
+
        switch (usage->hid & HID_USAGE_PAGE) {
 
                case HID_UP_UNDEFINED:
@@ -599,6 +607,7 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
                                case 0x0f6: map_key_clear(KEY_NEXT);            break;
                                case 0x0fa: map_key_clear(KEY_BACK);            break;
 
+                               case 0x182: map_key_clear(KEY_BOOKMARKS);       break;
                                case 0x183: map_key_clear(KEY_CONFIG);          break;
                                case 0x184: map_key_clear(KEY_WORDPROCESSOR);   break;
                                case 0x185: map_key_clear(KEY_EDITOR);          break;
@@ -615,9 +624,13 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
                                case 0x192: map_key_clear(KEY_CALC);            break;
                                case 0x194: map_key_clear(KEY_FILE);            break;
                                case 0x196: map_key_clear(KEY_WWW);             break;
+                               case 0x19c: map_key_clear(KEY_LOGOFF);          break;
                                case 0x19e: map_key_clear(KEY_COFFEE);          break;
                                case 0x1a6: map_key_clear(KEY_HELP);            break;
                                case 0x1a7: map_key_clear(KEY_DOCUMENTS);       break;
+                               case 0x1ab: map_key_clear(KEY_SPELLCHECK);      break;
+                               case 0x1b6: map_key_clear(KEY_MEDIA);           break;
+                               case 0x1b7: map_key_clear(KEY_SOUND);           break;
                                case 0x1bc: map_key_clear(KEY_MESSENGER);       break;
                                case 0x1bd: map_key_clear(KEY_INFO);            break;
                                case 0x201: map_key_clear(KEY_NEW);             break;
@@ -724,8 +737,15 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 
                case HID_UP_MSVENDOR:
 
-                       /* special case - Chicony Chicony KU-0418 tactical pad */
-                       if (device->vendor == 0x04f2 && device->product == 0x0418) {
+                       /* Unfortunately, there are multiple devices which
+                        * emit usages from MSVENDOR page that require different
+                        * handling. If this list grows too much in the future,
+                        * more general handling will have to be introduced here
+                        * (i.e. another blacklist).
+                        */
+
+                       /* Chicony Chicony KU-0418 tactical pad */
+                       if (IS_CHICONY_TACTICAL_PAD(device)) {
                                set_bit(EV_REP, input->evbit);
                                switch(usage->hid & HID_USAGE) {
                                        case 0xff01: map_key_clear(BTN_1);              break;
@@ -741,9 +761,40 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
                                        case 0xff0b: map_key_clear(BTN_B);              break;
                                        default:    goto ignore;
                                }
-                       } else {
+
+                       /* Microsoft Natural Ergonomic Keyboard 4000 */
+                       } else if (IS_MS_KB(device)) {
+                               switch(usage->hid & HID_USAGE) {
+                                       case 0xfd06:
+                                               map_key_clear(KEY_CHAT);
+                                               break;
+                                       case 0xfd07:
+                                               map_key_clear(KEY_PHONE);
+                                               break;
+                                       case 0xff05:
+                                               set_bit(EV_REP, input->evbit);
+                                               map_key_clear(KEY_F13);
+                                               set_bit(KEY_F14, input->keybit);
+                                               set_bit(KEY_F15, input->keybit);
+                                               set_bit(KEY_F16, input->keybit);
+                                               set_bit(KEY_F17, input->keybit);
+                                               set_bit(KEY_F18, input->keybit);
+                                       default:        goto ignore;
+                               }
+
+                       /* Microsoft Wireless Notebook Presenter Mouse 8000 */
+                       } else if (IS_MS_PRESENTER_8000(device)) {
+                               set_bit(EV_REP, input->evbit);
+                               switch(usage->hid & HID_USAGE) {
+                                       /* Useful mappings of bottom-side keys for presentations */
+                                       case 0xfd08: map_key_clear(KEY_RIGHT);          break;
+                                       case 0xfd09: map_key_clear(KEY_LEFT);           break;
+                                       case 0xfd0b: map_key_clear(KEY_PAUSE);          break;
+                                       case 0xfd0f: map_key_clear(KEY_F5);             break;
+                                       default:    goto ignore;
+                               }
+                       } else
                                goto ignore;
-                       }
                        break;
 
                case HID_UP_CUSTOM: /* Reported on Logitech and Powerbook USB keyboards */
@@ -882,23 +933,36 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
                        field->dpad = usage->code;
        }
 
+       /* for those devices which produce Consumer volume usage as relative,
+        * we emulate pressing volumeup/volumedown appropriate number of times
+        * in hidinput_hid_event()
+        */
+       if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
+                       (usage->code == ABS_VOLUME)) {
+               set_bit(KEY_VOLUMEUP, input->keybit);
+               set_bit(KEY_VOLUMEDOWN, input->keybit);
+       }
+
+       if (usage->type == EV_KEY) {
+               set_bit(EV_MSC, input->evbit);
+               set_bit(MSC_SCAN, input->mscbit);
+       }
+
        hid_resolv_event(usage->type, usage->code);
-#ifdef CONFIG_HID_DEBUG
-       printk("\n");
-#endif
+
+       dbg_hid_line("\n");
+
        return;
 
 ignore:
-#ifdef CONFIG_HID_DEBUG
-       printk("IGNORED\n");
-#endif
+       dbg_hid_line("IGNORED\n");
        return;
 }
 
 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
 {
        struct input_dev *input;
-       int *quirks = &hid->quirks;
+       unsigned *quirks = &hid->quirks;
 
        if (!field->hidinput)
                return;
@@ -960,18 +1024,56 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
        }
 
        if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
-               dbg("Maximum Effects - %d",value);
+               dbg_hid("Maximum Effects - %d\n",value);
                return;
        }
 
        if (usage->hid == (HID_UP_PID | 0x7fUL)) {
-               dbg("PID Pool Report\n");
+               dbg_hid("PID Pool Report\n");
                return;
        }
 
        if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
                return;
 
+       if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
+                       (usage->code == ABS_VOLUME)) {
+               int count = abs(value);
+               int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
+               int i;
+
+               for (i = 0; i < count; i++) {
+                       input_event(input, EV_KEY, direction, 1);
+                       input_sync(input);
+                       input_event(input, EV_KEY, direction, 0);
+                       input_sync(input);
+               }
+               return;
+       }
+
+       /* Handling MS keyboards special buttons */
+       if (IS_MS_KB(hid) && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
+               int key = 0;
+               static int last_key = 0;
+               switch (value) {
+                       case 0x01: key = KEY_F14; break;
+                       case 0x02: key = KEY_F15; break;
+                       case 0x04: key = KEY_F16; break;
+                       case 0x08: key = KEY_F17; break;
+                       case 0x10: key = KEY_F18; break;
+                       default: break;
+               }
+               if (key) {
+                       input_event(input, usage->type, key, 1);
+                       last_key = key;
+               } else {
+                       input_event(input, usage->type, last_key, 0);
+               }
+       }
+       /* report the usage code as scancode if the key status has changed */
+       if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
+               input_event(input, EV_MSC, MSC_SCAN, usage->hid);
+
        input_event(input, usage->type, usage->code, value);
 
        if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
@@ -1032,6 +1134,9 @@ int hidinput_connect(struct hid_device *hid)
        int i, j, k;
        int max_report_type = HID_OUTPUT_REPORT;
 
+       if (hid->quirks & HID_QUIRK_IGNORE_HIDINPUT)
+               return -1;
+
        INIT_LIST_HEAD(&hid->inputs);
 
        for (i = 0; i < hid->maxcollection; i++)
@@ -1058,8 +1163,8 @@ int hidinput_connect(struct hid_device *hid)
                                if (!hidinput || !input_dev) {
                                        kfree(hidinput);
                                        input_free_device(input_dev);
-                                       err("Out of memory during hid input probe");
-                                       return -1;
+                                       err_hid("Out of memory during hid input probe");
+                                       goto out_unwind;
                                }
 
                                input_set_drvdata(input_dev, hid);
@@ -1093,15 +1198,25 @@ int hidinput_connect(struct hid_device *hid)
                                 * UGCI) cram a lot of unrelated inputs into the
                                 * same interface. */
                                hidinput->report = report;
-                               input_register_device(hidinput->input);
+                               if (input_register_device(hidinput->input))
+                                       goto out_cleanup;
                                hidinput = NULL;
                        }
                }
 
-       if (hidinput)
-               input_register_device(hidinput->input);
+       if (hidinput && input_register_device(hidinput->input))
+               goto out_cleanup;
 
        return 0;
+
+out_cleanup:
+       input_free_device(hidinput->input);
+       kfree(hidinput);
+out_unwind:
+       /* unwind the ones we already registered */
+       hidinput_disconnect(hid);
+
+       return -1;
 }
 EXPORT_SYMBOL_GPL(hidinput_connect);