Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  HID driver for Cougar 500k Gaming Keyboard
0004  *
0005  *  Copyright (c) 2018 Daniel M. Lambea <dmlambea@gmail.com>
0006  */
0007 
0008 #include <linux/hid.h>
0009 #include <linux/module.h>
0010 #include <linux/printk.h>
0011 
0012 #include "hid-ids.h"
0013 
0014 MODULE_AUTHOR("Daniel M. Lambea <dmlambea@gmail.com>");
0015 MODULE_DESCRIPTION("Cougar 500k Gaming Keyboard");
0016 MODULE_LICENSE("GPL");
0017 MODULE_INFO(key_mappings, "G1-G6 are mapped to F13-F18");
0018 
0019 static bool g6_is_space = true;
0020 MODULE_PARM_DESC(g6_is_space,
0021     "If true, G6 programmable key sends SPACE instead of F18 (default=true)");
0022 
0023 #define COUGAR_VENDOR_USAGE 0xff00ff00
0024 
0025 #define COUGAR_FIELD_CODE   1
0026 #define COUGAR_FIELD_ACTION 2
0027 
0028 #define COUGAR_KEY_G1       0x83
0029 #define COUGAR_KEY_G2       0x84
0030 #define COUGAR_KEY_G3       0x85
0031 #define COUGAR_KEY_G4       0x86
0032 #define COUGAR_KEY_G5       0x87
0033 #define COUGAR_KEY_G6       0x78
0034 #define COUGAR_KEY_FN       0x0d
0035 #define COUGAR_KEY_MR       0x6f
0036 #define COUGAR_KEY_M1       0x80
0037 #define COUGAR_KEY_M2       0x81
0038 #define COUGAR_KEY_M3       0x82
0039 #define COUGAR_KEY_LEDS     0x67
0040 #define COUGAR_KEY_LOCK     0x6e
0041 
0042 
0043 /* Default key mappings. The special key COUGAR_KEY_G6 is defined first
0044  * because it is more frequent to use the spacebar rather than any other
0045  * special keys. Depending on the value of the parameter 'g6_is_space',
0046  * the mapping will be updated in the probe function.
0047  */
0048 static unsigned char cougar_mapping[][2] = {
0049     { COUGAR_KEY_G6,   KEY_SPACE },
0050     { COUGAR_KEY_G1,   KEY_F13 },
0051     { COUGAR_KEY_G2,   KEY_F14 },
0052     { COUGAR_KEY_G3,   KEY_F15 },
0053     { COUGAR_KEY_G4,   KEY_F16 },
0054     { COUGAR_KEY_G5,   KEY_F17 },
0055     { COUGAR_KEY_LOCK, KEY_SCREENLOCK },
0056 /* The following keys are handled by the hardware itself, so no special
0057  * treatment is required:
0058     { COUGAR_KEY_FN, KEY_RESERVED },
0059     { COUGAR_KEY_MR, KEY_RESERVED },
0060     { COUGAR_KEY_M1, KEY_RESERVED },
0061     { COUGAR_KEY_M2, KEY_RESERVED },
0062     { COUGAR_KEY_M3, KEY_RESERVED },
0063     { COUGAR_KEY_LEDS, KEY_RESERVED },
0064 */
0065     { 0, 0 },
0066 };
0067 
0068 struct cougar_shared {
0069     struct list_head list;
0070     struct kref kref;
0071     bool enabled;
0072     struct hid_device *dev;
0073     struct input_dev *input;
0074 };
0075 
0076 struct cougar {
0077     bool special_intf;
0078     struct cougar_shared *shared;
0079 };
0080 
0081 static LIST_HEAD(cougar_udev_list);
0082 static DEFINE_MUTEX(cougar_udev_list_lock);
0083 
0084 /**
0085  * cougar_fix_g6_mapping - configure the mapping for key G6/Spacebar
0086  */
0087 static void cougar_fix_g6_mapping(void)
0088 {
0089     int i;
0090 
0091     for (i = 0; cougar_mapping[i][0]; i++) {
0092         if (cougar_mapping[i][0] == COUGAR_KEY_G6) {
0093             cougar_mapping[i][1] =
0094                 g6_is_space ? KEY_SPACE : KEY_F18;
0095             pr_info("cougar: G6 mapped to %s\n",
0096                 g6_is_space ? "space" : "F18");
0097             return;
0098         }
0099     }
0100     pr_warn("cougar: no mappings defined for G6/spacebar");
0101 }
0102 
0103 /*
0104  * Constant-friendly rdesc fixup for mouse interface
0105  */
0106 static __u8 *cougar_report_fixup(struct hid_device *hdev, __u8 *rdesc,
0107                  unsigned int *rsize)
0108 {
0109     if (rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
0110         (rdesc[115] | rdesc[116] << 8) >= HID_MAX_USAGES) {
0111         hid_info(hdev,
0112             "usage count exceeds max: fixing up report descriptor\n");
0113         rdesc[115] = ((HID_MAX_USAGES-1) & 0xff);
0114         rdesc[116] = ((HID_MAX_USAGES-1) >> 8);
0115     }
0116     return rdesc;
0117 }
0118 
0119 static struct cougar_shared *cougar_get_shared_data(struct hid_device *hdev)
0120 {
0121     struct cougar_shared *shared;
0122 
0123     /* Try to find an already-probed interface from the same device */
0124     list_for_each_entry(shared, &cougar_udev_list, list) {
0125         if (hid_compare_device_paths(hdev, shared->dev, '/')) {
0126             kref_get(&shared->kref);
0127             return shared;
0128         }
0129     }
0130     return NULL;
0131 }
0132 
0133 static void cougar_release_shared_data(struct kref *kref)
0134 {
0135     struct cougar_shared *shared = container_of(kref,
0136                             struct cougar_shared, kref);
0137 
0138     mutex_lock(&cougar_udev_list_lock);
0139     list_del(&shared->list);
0140     mutex_unlock(&cougar_udev_list_lock);
0141 
0142     kfree(shared);
0143 }
0144 
0145 static void cougar_remove_shared_data(void *resource)
0146 {
0147     struct cougar *cougar = resource;
0148 
0149     if (cougar->shared) {
0150         kref_put(&cougar->shared->kref, cougar_release_shared_data);
0151         cougar->shared = NULL;
0152     }
0153 }
0154 
0155 /*
0156  * Bind the device group's shared data to this cougar struct.
0157  * If no shared data exists for this group, create and initialize it.
0158  */
0159 static int cougar_bind_shared_data(struct hid_device *hdev,
0160                    struct cougar *cougar)
0161 {
0162     struct cougar_shared *shared;
0163     int error = 0;
0164 
0165     mutex_lock(&cougar_udev_list_lock);
0166 
0167     shared = cougar_get_shared_data(hdev);
0168     if (!shared) {
0169         shared = kzalloc(sizeof(*shared), GFP_KERNEL);
0170         if (!shared) {
0171             error = -ENOMEM;
0172             goto out;
0173         }
0174 
0175         kref_init(&shared->kref);
0176         shared->dev = hdev;
0177         list_add_tail(&shared->list, &cougar_udev_list);
0178     }
0179 
0180     cougar->shared = shared;
0181 
0182     error = devm_add_action_or_reset(&hdev->dev, cougar_remove_shared_data, cougar);
0183     if (error) {
0184         mutex_unlock(&cougar_udev_list_lock);
0185         return error;
0186     }
0187 
0188 out:
0189     mutex_unlock(&cougar_udev_list_lock);
0190     return error;
0191 }
0192 
0193 static int cougar_probe(struct hid_device *hdev,
0194             const struct hid_device_id *id)
0195 {
0196     struct cougar *cougar;
0197     struct hid_input *next, *hidinput = NULL;
0198     unsigned int connect_mask;
0199     int error;
0200 
0201     cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
0202     if (!cougar)
0203         return -ENOMEM;
0204     hid_set_drvdata(hdev, cougar);
0205 
0206     error = hid_parse(hdev);
0207     if (error) {
0208         hid_err(hdev, "parse failed\n");
0209         return error;
0210     }
0211 
0212     if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
0213         cougar->special_intf = true;
0214         connect_mask = HID_CONNECT_HIDRAW;
0215     } else
0216         connect_mask = HID_CONNECT_DEFAULT;
0217 
0218     error = hid_hw_start(hdev, connect_mask);
0219     if (error) {
0220         hid_err(hdev, "hw start failed\n");
0221         return error;
0222     }
0223 
0224     error = cougar_bind_shared_data(hdev, cougar);
0225     if (error)
0226         goto fail_stop_and_cleanup;
0227 
0228     /* The custom vendor interface will use the hid_input registered
0229      * for the keyboard interface, in order to send translated key codes
0230      * to it.
0231      */
0232     if (hdev->collection->usage == HID_GD_KEYBOARD) {
0233         list_for_each_entry_safe(hidinput, next, &hdev->inputs, list) {
0234             if (hidinput->registered && hidinput->input != NULL) {
0235                 cougar->shared->input = hidinput->input;
0236                 cougar->shared->enabled = true;
0237                 break;
0238             }
0239         }
0240     } else if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
0241         /* Preinit the mapping table */
0242         cougar_fix_g6_mapping();
0243         error = hid_hw_open(hdev);
0244         if (error)
0245             goto fail_stop_and_cleanup;
0246     }
0247     return 0;
0248 
0249 fail_stop_and_cleanup:
0250     hid_hw_stop(hdev);
0251     return error;
0252 }
0253 
0254 /*
0255  * Convert events from vendor intf to input key events
0256  */
0257 static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
0258                 u8 *data, int size)
0259 {
0260     struct cougar *cougar;
0261     struct cougar_shared *shared;
0262     unsigned char code, action;
0263     int i;
0264 
0265     cougar = hid_get_drvdata(hdev);
0266     shared = cougar->shared;
0267     if (!cougar->special_intf || !shared)
0268         return 0;
0269 
0270     if (!shared->enabled || !shared->input)
0271         return -EPERM;
0272 
0273     code = data[COUGAR_FIELD_CODE];
0274     action = data[COUGAR_FIELD_ACTION];
0275     for (i = 0; cougar_mapping[i][0]; i++) {
0276         if (code == cougar_mapping[i][0]) {
0277             input_event(shared->input, EV_KEY,
0278                     cougar_mapping[i][1], action);
0279             input_sync(shared->input);
0280             return -EPERM;
0281         }
0282     }
0283     /* Avoid warnings on the same unmapped key twice */
0284     if (action != 0)
0285         hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
0286     return -EPERM;
0287 }
0288 
0289 static void cougar_remove(struct hid_device *hdev)
0290 {
0291     struct cougar *cougar = hid_get_drvdata(hdev);
0292 
0293     if (cougar) {
0294         /* Stop the vendor intf to process more events */
0295         if (cougar->shared)
0296             cougar->shared->enabled = false;
0297         if (cougar->special_intf)
0298             hid_hw_close(hdev);
0299     }
0300     hid_hw_stop(hdev);
0301 }
0302 
0303 static int cougar_param_set_g6_is_space(const char *val,
0304                     const struct kernel_param *kp)
0305 {
0306     int ret;
0307 
0308     ret = param_set_bool(val, kp);
0309     if (ret)
0310         return ret;
0311 
0312     cougar_fix_g6_mapping();
0313 
0314     return 0;
0315 }
0316 
0317 static const struct kernel_param_ops cougar_g6_is_space_ops = {
0318     .set    = cougar_param_set_g6_is_space,
0319     .get    = param_get_bool,
0320 };
0321 module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
0322 
0323 static const struct hid_device_id cougar_id_table[] = {
0324     { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
0325              USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD) },
0326     { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
0327              USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD) },
0328     {}
0329 };
0330 MODULE_DEVICE_TABLE(hid, cougar_id_table);
0331 
0332 static struct hid_driver cougar_driver = {
0333     .name           = "cougar",
0334     .id_table       = cougar_id_table,
0335     .report_fixup       = cougar_report_fixup,
0336     .probe          = cougar_probe,
0337     .remove         = cougar_remove,
0338     .raw_event      = cougar_raw_event,
0339 };
0340 
0341 module_hid_driver(cougar_driver);