^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * HID support for Vivaldi Keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2020 Google LLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Sean O'Brien <seobrien@chromium.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define MIN_FN_ROW_KEY 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define MAX_FN_ROW_KEY 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define HID_VD_FN_ROW_PHYSMAP 0x00000001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static struct hid_driver hid_vivaldi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct vivaldi_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) u32 function_row_physmap[MAX_FN_ROW_KEY - MIN_FN_ROW_KEY + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int max_function_row_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static ssize_t function_row_physmap_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) ssize_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!drvdata->max_function_row_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) for (i = 0; i < drvdata->max_function_row_key; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) size += sprintf(buf + size, "%02X ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) drvdata->function_row_physmap[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) size += sprintf(buf + size, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) DEVICE_ATTR_RO(function_row_physmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static struct attribute *sysfs_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) &dev_attr_function_row_physmap.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static const struct attribute_group input_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .attrs = sysfs_attrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int vivaldi_probe(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct vivaldi_data *drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!drvdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) hid_set_drvdata(hdev, drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static void vivaldi_feature_mapping(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct hid_usage *usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct hid_report *report = field->report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int fn_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) u32 report_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) u8 *report_data, *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fn_key = (usage->hid & HID_USAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (fn_key > drvdata->max_function_row_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) drvdata->max_function_row_key = fn_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!report_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) report_len = hid_report_len(report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!report->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * hid_hw_raw_request() will stuff report ID (which will be 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * into the first byte of the buffer even for unnumbered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * reports, so we need to account for this to avoid getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * -EOVERFLOW in return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Note that hid_alloc_report_buf() adds 7 bytes to the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * so we can safely say that we have space for an extra byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) report_len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = hid_hw_raw_request(hdev, report->id, report_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) report_len, HID_FEATURE_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) HID_REQ_GET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dev_warn(&hdev->dev, "failed to fetch feature %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) field->report->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!report->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * Undo the damage from hid_hw_raw_request() for unnumbered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * reports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) report_data++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) report_len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) report_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_warn(&hdev->dev, "failed to report feature %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) field->report->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) drvdata->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) field->value[usage->usage_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int vivaldi_input_configured(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct hid_input *hidinput)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return devm_device_add_group(&hdev->dev, &input_attribute_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static const struct hid_device_id vivaldi_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) { HID_DEVICE(HID_BUS_ANY, HID_GROUP_VIVALDI, HID_ANY_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) HID_ANY_ID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) MODULE_DEVICE_TABLE(hid, vivaldi_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct hid_driver hid_vivaldi = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .name = "hid-vivaldi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .id_table = vivaldi_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .probe = vivaldi_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .feature_mapping = vivaldi_feature_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .input_configured = vivaldi_input_configured,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) module_hid_driver(hid_vivaldi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_AUTHOR("Sean O'Brien");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_DESCRIPTION("HID vivaldi driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_LICENSE("GPL");