Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  ION iCade input driver
0004  *
0005  *  Copyright (c) 2012 Bastien Nocera <hadess@hadess.net>
0006  *  Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
0007  */
0008 
0009 /*
0010  */
0011 
0012 #include <linux/device.h>
0013 #include <linux/hid.h>
0014 #include <linux/module.h>
0015 
0016 #include "hid-ids.h"
0017 
0018 /*
0019  *   ↑      A C Y L
0020  *  ← →
0021  *   ↓      B X Z R
0022  *
0023  *
0024  *  UP ON,OFF  = w,e
0025  *  RT ON,OFF  = d,c
0026  *  DN ON,OFF  = x,z
0027  *  LT ON,OFF  = a,q
0028  *  A  ON,OFF  = y,t
0029  *  B  ON,OFF  = h,r
0030  *  C  ON,OFF  = u,f
0031  *  X  ON,OFF  = j,n
0032  *  Y  ON,OFF  = i,m
0033  *  Z  ON,OFF  = k,p
0034  *  L  ON,OFF  = o,g
0035  *  R  ON,OFF  = l,v
0036  */
0037 
0038 /* The translation code uses HID usage instead of input layer
0039  * keys. This code generates a lookup table that makes
0040  * translation quick.
0041  *
0042  * #include <linux/input.h>
0043  * #include <stdio.h>
0044  * #include <assert.h>
0045  *
0046  * #define unk     KEY_UNKNOWN
0047  *
0048  * < copy of hid_keyboard[] from hid-input.c >
0049  *
0050  * struct icade_key_translation {
0051  *     int         from;
0052  *     const char *to;
0053  *     int         press;
0054  * };
0055  *
0056  * static const struct icade_key_translation icade_keys[] = {
0057  *    { KEY_W,        "KEY_UP",         1 },
0058  *    { KEY_E,        "KEY_UP",         0 },
0059  *    { KEY_D,        "KEY_RIGHT",      1 },
0060  *    { KEY_C,        "KEY_RIGHT",      0 },
0061  *    { KEY_X,        "KEY_DOWN",       1 },
0062  *    { KEY_Z,        "KEY_DOWN",       0 },
0063  *    { KEY_A,        "KEY_LEFT",       1 },
0064  *    { KEY_Q,        "KEY_LEFT",       0 },
0065  *    { KEY_Y,        "BTN_A",          1 },
0066  *    { KEY_T,        "BTN_A",          0 },
0067  *    { KEY_H,        "BTN_B",          1 },
0068  *    { KEY_R,        "BTN_B",          0 },
0069  *    { KEY_U,        "BTN_C",          1 },
0070  *    { KEY_F,        "BTN_C",          0 },
0071  *    { KEY_J,        "BTN_X",          1 },
0072  *    { KEY_N,        "BTN_X",          0 },
0073  *    { KEY_I,        "BTN_Y",          1 },
0074  *    { KEY_M,        "BTN_Y",          0 },
0075  *    { KEY_K,        "BTN_Z",          1 },
0076  *    { KEY_P,        "BTN_Z",          0 },
0077  *    { KEY_O,        "BTN_THUMBL",     1 },
0078  *    { KEY_G,        "BTN_THUMBL",     0 },
0079  *    { KEY_L,        "BTN_THUMBR",     1 },
0080  *    { KEY_V,        "BTN_THUMBR",     0 },
0081  *
0082  *    { }
0083  * };
0084  *
0085  * static int
0086  * usage_for_key (int key)
0087  * {
0088  *     int i;
0089  *     for (i = 0; i < 256; i++) {
0090  *     if (hid_keyboard[i] == key)
0091  *         return i;
0092  *     }
0093  *     assert(0);
0094  * }
0095  *
0096  * int main (int argc, char **argv)
0097  * {
0098  *     const struct icade_key_translation *trans;
0099  *     int max_usage = 0;
0100  *
0101  *     for (trans = icade_keys; trans->from; trans++) {
0102  *         int usage = usage_for_key (trans->from);
0103  *         max_usage = usage > max_usage ? usage : max_usage;
0104  *     }
0105  *
0106  *     printf ("#define ICADE_MAX_USAGE %d\n\n", max_usage);
0107  *     printf ("struct icade_key {\n");
0108  *     printf ("\tu16 to;\n");
0109  *     printf ("\tu8 press:1;\n");
0110  *     printf ("};\n\n");
0111  *     printf ("static const struct icade_key "
0112  *             "icade_usage_table[%d] = {\n", max_usage + 1);
0113  *     for (trans = icade_keys; trans->from; trans++) {
0114  *         printf ("\t[%d] = { %s, %d },\n",
0115  *                 usage_for_key (trans->from), trans->to, trans->press);
0116  *     }
0117  *     printf ("};\n");
0118  *
0119  *     return 0;
0120  * }
0121  */
0122 
0123 #define ICADE_MAX_USAGE 29
0124 
0125 struct icade_key {
0126     u16 to;
0127     u8 press:1;
0128 };
0129 
0130 static const struct icade_key icade_usage_table[30] = {
0131     [26] = { KEY_UP, 1 },
0132     [8] = { KEY_UP, 0 },
0133     [7] = { KEY_RIGHT, 1 },
0134     [6] = { KEY_RIGHT, 0 },
0135     [27] = { KEY_DOWN, 1 },
0136     [29] = { KEY_DOWN, 0 },
0137     [4] = { KEY_LEFT, 1 },
0138     [20] = { KEY_LEFT, 0 },
0139     [28] = { BTN_A, 1 },
0140     [23] = { BTN_A, 0 },
0141     [11] = { BTN_B, 1 },
0142     [21] = { BTN_B, 0 },
0143     [24] = { BTN_C, 1 },
0144     [9] = { BTN_C, 0 },
0145     [13] = { BTN_X, 1 },
0146     [17] = { BTN_X, 0 },
0147     [12] = { BTN_Y, 1 },
0148     [16] = { BTN_Y, 0 },
0149     [14] = { BTN_Z, 1 },
0150     [19] = { BTN_Z, 0 },
0151     [18] = { BTN_THUMBL, 1 },
0152     [10] = { BTN_THUMBL, 0 },
0153     [15] = { BTN_THUMBR, 1 },
0154     [25] = { BTN_THUMBR, 0 },
0155 };
0156 
0157 static const struct icade_key *icade_find_translation(u16 from)
0158 {
0159     if (from > ICADE_MAX_USAGE)
0160         return NULL;
0161     return &icade_usage_table[from];
0162 }
0163 
0164 static int icade_event(struct hid_device *hdev, struct hid_field *field,
0165         struct hid_usage *usage, __s32 value)
0166 {
0167     const struct icade_key *trans;
0168 
0169     if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
0170             !usage->type)
0171         return 0;
0172 
0173     /* We ignore the fake key up, and act only on key down */
0174     if (!value)
0175         return 1;
0176 
0177     trans = icade_find_translation(usage->hid & HID_USAGE);
0178 
0179     if (!trans)
0180         return 1;
0181 
0182     input_event(field->hidinput->input, usage->type,
0183             trans->to, trans->press);
0184 
0185     return 1;
0186 }
0187 
0188 static int icade_input_mapping(struct hid_device *hdev, struct hid_input *hi,
0189         struct hid_field *field, struct hid_usage *usage,
0190         unsigned long **bit, int *max)
0191 {
0192     const struct icade_key *trans;
0193 
0194     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_KEYBOARD) {
0195         trans = icade_find_translation(usage->hid & HID_USAGE);
0196 
0197         if (!trans)
0198             return -1;
0199 
0200         hid_map_usage(hi, usage, bit, max, EV_KEY, trans->to);
0201         set_bit(trans->to, hi->input->keybit);
0202 
0203         return 1;
0204     }
0205 
0206     /* ignore others */
0207     return -1;
0208 
0209 }
0210 
0211 static int icade_input_mapped(struct hid_device *hdev, struct hid_input *hi,
0212         struct hid_field *field, struct hid_usage *usage,
0213         unsigned long **bit, int *max)
0214 {
0215     if (usage->type == EV_KEY)
0216         set_bit(usage->type, hi->input->evbit);
0217 
0218     return -1;
0219 }
0220 
0221 static const struct hid_device_id icade_devices[] = {
0222     { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ION, USB_DEVICE_ID_ICADE) },
0223 
0224     { }
0225 };
0226 MODULE_DEVICE_TABLE(hid, icade_devices);
0227 
0228 static struct hid_driver icade_driver = {
0229     .name = "icade",
0230     .id_table = icade_devices,
0231     .event = icade_event,
0232     .input_mapped = icade_input_mapped,
0233     .input_mapping = icade_input_mapping,
0234 };
0235 module_hid_driver(icade_driver);
0236 
0237 MODULE_LICENSE("GPL");
0238 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
0239 MODULE_DESCRIPTION("ION iCade input driver");