Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Helpers for ChromeOS HID Vivaldi keyboards
0004  *
0005  * Copyright (C) 2022 Google, Inc
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/hid.h>
0010 #include <linux/input/vivaldi-fmap.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/types.h>
0014 
0015 #include "hid-vivaldi-common.h"
0016 
0017 #define MIN_FN_ROW_KEY 1
0018 #define MAX_FN_ROW_KEY VIVALDI_MAX_FUNCTION_ROW_KEYS
0019 #define HID_VD_FN_ROW_PHYSMAP 0x00000001
0020 #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
0021 
0022 /**
0023  * vivaldi_feature_mapping - Fill out vivaldi keymap data exposed via HID
0024  * @hdev: HID device to parse
0025  * @field: HID field to parse
0026  * @usage: HID usage to parse
0027  *
0028  * Note: this function assumes that driver data attached to @hdev contains an
0029  * instance of &struct vivaldi_data at the very beginning.
0030  */
0031 void vivaldi_feature_mapping(struct hid_device *hdev,
0032                  struct hid_field *field, struct hid_usage *usage)
0033 {
0034     struct vivaldi_data *data = hid_get_drvdata(hdev);
0035     struct hid_report *report = field->report;
0036     u8 *report_data, *buf;
0037     u32 report_len;
0038     unsigned int fn_key;
0039     int ret;
0040 
0041     if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
0042         (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
0043         return;
0044 
0045     fn_key = usage->hid & HID_USAGE;
0046     if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
0047         return;
0048 
0049     if (fn_key > data->num_function_row_keys)
0050         data->num_function_row_keys = fn_key;
0051 
0052     report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
0053     if (!report_data)
0054         return;
0055 
0056     report_len = hid_report_len(report);
0057     if (!report->id) {
0058         /*
0059          * hid_hw_raw_request() will stuff report ID (which will be 0)
0060          * into the first byte of the buffer even for unnumbered
0061          * reports, so we need to account for this to avoid getting
0062          * -EOVERFLOW in return.
0063          * Note that hid_alloc_report_buf() adds 7 bytes to the size
0064          * so we can safely say that we have space for an extra byte.
0065          */
0066         report_len++;
0067     }
0068 
0069     ret = hid_hw_raw_request(hdev, report->id, report_data,
0070                  report_len, HID_FEATURE_REPORT,
0071                  HID_REQ_GET_REPORT);
0072     if (ret < 0) {
0073         dev_warn(&hdev->dev, "failed to fetch feature %d\n",
0074              field->report->id);
0075         goto out;
0076     }
0077 
0078     if (!report->id) {
0079         /*
0080          * Undo the damage from hid_hw_raw_request() for unnumbered
0081          * reports.
0082          */
0083         report_data++;
0084         report_len--;
0085     }
0086 
0087     ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
0088                    report_len, 0);
0089     if (ret) {
0090         dev_warn(&hdev->dev, "failed to report feature %d\n",
0091              field->report->id);
0092         goto out;
0093     }
0094 
0095     data->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
0096         field->value[usage->usage_index];
0097 
0098 out:
0099     kfree(buf);
0100 }
0101 EXPORT_SYMBOL_GPL(vivaldi_feature_mapping);
0102 
0103 static ssize_t function_row_physmap_show(struct device *dev,
0104                      struct device_attribute *attr,
0105                      char *buf)
0106 {
0107     struct hid_device *hdev = to_hid_device(dev);
0108     struct vivaldi_data *data = hid_get_drvdata(hdev);
0109 
0110     return vivaldi_function_row_physmap_show(data, buf);
0111 }
0112 
0113 static DEVICE_ATTR_RO(function_row_physmap);
0114 static struct attribute *vivaldi_sysfs_attrs[] = {
0115     &dev_attr_function_row_physmap.attr,
0116     NULL
0117 };
0118 
0119 static const struct attribute_group vivaldi_attribute_group = {
0120     .attrs = vivaldi_sysfs_attrs,
0121 };
0122 
0123 /**
0124  * vivaldi_input_configured - Complete initialization of device using vivaldi map
0125  * @hdev: HID device to which vivaldi attributes should be attached
0126  * @hidinput: HID input device (unused)
0127  */
0128 int vivaldi_input_configured(struct hid_device *hdev,
0129                  struct hid_input *hidinput)
0130 {
0131     struct vivaldi_data *data = hid_get_drvdata(hdev);
0132 
0133     if (!data->num_function_row_keys)
0134         return 0;
0135 
0136     return devm_device_add_group(&hdev->dev, &vivaldi_attribute_group);
0137 }
0138 EXPORT_SYMBOL_GPL(vivaldi_input_configured);
0139 
0140 MODULE_LICENSE("GPL");