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