linux/drivers/hid/hid-vivaldi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * HID support for Vivaldi Keyboard
   4 *
   5 * Copyright 2020 Google LLC.
   6 * Author: Sean O'Brien <seobrien@chromium.org>
   7 */
   8
   9#include <linux/hid.h>
  10#include <linux/module.h>
  11
  12#define MIN_FN_ROW_KEY  1
  13#define MAX_FN_ROW_KEY  24
  14#define HID_VD_FN_ROW_PHYSMAP 0x00000001
  15#define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
  16
  17static struct hid_driver hid_vivaldi;
  18
  19struct vivaldi_data {
  20        u32 function_row_physmap[MAX_FN_ROW_KEY - MIN_FN_ROW_KEY + 1];
  21        int max_function_row_key;
  22};
  23
  24static ssize_t function_row_physmap_show(struct device *dev,
  25                                         struct device_attribute *attr,
  26                                         char *buf)
  27{
  28        struct hid_device *hdev = to_hid_device(dev);
  29        struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
  30        ssize_t size = 0;
  31        int i;
  32
  33        if (!drvdata->max_function_row_key)
  34                return 0;
  35
  36        for (i = 0; i < drvdata->max_function_row_key; i++)
  37                size += sprintf(buf + size, "%02X ",
  38                                drvdata->function_row_physmap[i]);
  39        size += sprintf(buf + size, "\n");
  40        return size;
  41}
  42
  43DEVICE_ATTR_RO(function_row_physmap);
  44static struct attribute *sysfs_attrs[] = {
  45        &dev_attr_function_row_physmap.attr,
  46        NULL
  47};
  48
  49static const struct attribute_group input_attribute_group = {
  50        .attrs = sysfs_attrs
  51};
  52
  53static int vivaldi_probe(struct hid_device *hdev,
  54                         const struct hid_device_id *id)
  55{
  56        struct vivaldi_data *drvdata;
  57        int ret;
  58
  59        drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  60        hid_set_drvdata(hdev, drvdata);
  61
  62        ret = hid_parse(hdev);
  63        if (ret)
  64                return ret;
  65
  66        return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  67}
  68
  69static void vivaldi_feature_mapping(struct hid_device *hdev,
  70                                    struct hid_field *field,
  71                                    struct hid_usage *usage)
  72{
  73        struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
  74        int fn_key;
  75        int ret;
  76        u32 report_len;
  77        u8 *buf;
  78
  79        if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
  80            (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
  81                return;
  82
  83        fn_key = (usage->hid & HID_USAGE);
  84        if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
  85                return;
  86        if (fn_key > drvdata->max_function_row_key)
  87                drvdata->max_function_row_key = fn_key;
  88
  89        buf = hid_alloc_report_buf(field->report, GFP_KERNEL);
  90        if (!buf)
  91                return;
  92
  93        report_len = hid_report_len(field->report);
  94        ret = hid_hw_raw_request(hdev, field->report->id, buf,
  95                                 report_len, HID_FEATURE_REPORT,
  96                                 HID_REQ_GET_REPORT);
  97        if (ret < 0) {
  98                dev_warn(&hdev->dev, "failed to fetch feature %d\n",
  99                         field->report->id);
 100                goto out;
 101        }
 102
 103        ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
 104                                   report_len, 0);
 105        if (ret) {
 106                dev_warn(&hdev->dev, "failed to report feature %d\n",
 107                         field->report->id);
 108                goto out;
 109        }
 110
 111        drvdata->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
 112            field->value[usage->usage_index];
 113
 114out:
 115        kfree(buf);
 116}
 117
 118static int vivaldi_input_configured(struct hid_device *hdev,
 119                                    struct hid_input *hidinput)
 120{
 121        return sysfs_create_group(&hdev->dev.kobj, &input_attribute_group);
 122}
 123
 124static const struct hid_device_id vivaldi_table[] = {
 125        { HID_DEVICE(HID_BUS_ANY, HID_GROUP_VIVALDI, HID_ANY_ID,
 126                     HID_ANY_ID) },
 127        { }
 128};
 129
 130MODULE_DEVICE_TABLE(hid, vivaldi_table);
 131
 132static struct hid_driver hid_vivaldi = {
 133        .name = "hid-vivaldi",
 134        .id_table = vivaldi_table,
 135        .probe = vivaldi_probe,
 136        .feature_mapping = vivaldi_feature_mapping,
 137        .input_configured = vivaldi_input_configured,
 138};
 139
 140module_hid_driver(hid_vivaldi);
 141
 142MODULE_AUTHOR("Sean O'Brien");
 143MODULE_DESCRIPTION("HID vivaldi driver");
 144MODULE_LICENSE("GPL");
 145