forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/drivers/hid/hid-core.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * HID support for Linux
34 *
....@@ -8,10 +9,6 @@
89 */
910
1011 /*
11
- * This program is free software; you can redistribute it and/or modify it
12
- * under the terms of the GNU General Public License as published by the Free
13
- * Software Foundation; either version 2 of the License, or (at your option)
14
- * any later version.
1512 */
1613
1714 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -125,6 +122,7 @@
125122 {
126123 struct hid_collection *collection;
127124 unsigned usage;
125
+ int collection_index;
128126
129127 usage = parser->local.usage[0];
130128
....@@ -167,11 +165,13 @@
167165 parser->collection_stack[parser->collection_stack_ptr++] =
168166 parser->device->maxcollection;
169167
170
- collection = parser->device->collection +
171
- parser->device->maxcollection++;
168
+ collection_index = parser->device->maxcollection++;
169
+ collection = parser->device->collection + collection_index;
172170 collection->type = type;
173171 collection->usage = usage;
174172 collection->level = parser->collection_stack_ptr - 1;
173
+ collection->parent_idx = (collection->level == 0) ? -1 :
174
+ parser->collection_stack[collection->level - 1];
175175
176176 if (type == HID_COLLECTION_APPLICATION)
177177 parser->device->maxapplication++;
....@@ -317,6 +317,7 @@
317317 field->usage[i].collection_index =
318318 parser->local.collection_index[j];
319319 field->usage[i].usage_index = i;
320
+ field->usage[i].resolution_multiplier = 1;
320321 }
321322
322323 field->maxusage = usages;
....@@ -433,7 +434,7 @@
433434
434435 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
435436 parser->global.report_size = item_udata(item);
436
- if (parser->global.report_size > 128) {
437
+ if (parser->global.report_size > 256) {
437438 hid_err(parser->device, "invalid report_size %d\n",
438439 parser->global.report_size);
439440 return -1;
....@@ -813,6 +814,13 @@
813814
814815 if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
815816 parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
817
+
818
+ if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR)
819
+ for (i = 0; i < parser->local.usage_index; i++)
820
+ if (parser->local.usage[i] ==
821
+ (HID_UP_GOOGLEVENDOR | 0x0001))
822
+ parser->device->group =
823
+ HID_GROUP_VIVALDI;
816824 }
817825
818826 static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
....@@ -919,7 +927,7 @@
919927 /**
920928 * hid_parse_report - parse device report
921929 *
922
- * @device: hid device
930
+ * @hid: hid device
923931 * @start: report start
924932 * @size: report size
925933 *
....@@ -944,7 +952,7 @@
944952 /**
945953 * hid_validate_values - validate existing device report's value indexes
946954 *
947
- * @device: hid device
955
+ * @hid: hid device
948956 * @type: which report type to examine
949957 * @id: which report ID to examine (0 for first)
950958 * @field_index: which report field to examine
....@@ -1003,6 +1011,169 @@
10031011 return report;
10041012 }
10051013 EXPORT_SYMBOL_GPL(hid_validate_values);
1014
+
1015
+static int hid_calculate_multiplier(struct hid_device *hid,
1016
+ struct hid_field *multiplier)
1017
+{
1018
+ int m;
1019
+ __s32 v = *multiplier->value;
1020
+ __s32 lmin = multiplier->logical_minimum;
1021
+ __s32 lmax = multiplier->logical_maximum;
1022
+ __s32 pmin = multiplier->physical_minimum;
1023
+ __s32 pmax = multiplier->physical_maximum;
1024
+
1025
+ /*
1026
+ * "Because OS implementations will generally divide the control's
1027
+ * reported count by the Effective Resolution Multiplier, designers
1028
+ * should take care not to establish a potential Effective
1029
+ * Resolution Multiplier of zero."
1030
+ * HID Usage Table, v1.12, Section 4.3.1, p31
1031
+ */
1032
+ if (lmax - lmin == 0)
1033
+ return 1;
1034
+ /*
1035
+ * Handling the unit exponent is left as an exercise to whoever
1036
+ * finds a device where that exponent is not 0.
1037
+ */
1038
+ m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
1039
+ if (unlikely(multiplier->unit_exponent != 0)) {
1040
+ hid_warn(hid,
1041
+ "unsupported Resolution Multiplier unit exponent %d\n",
1042
+ multiplier->unit_exponent);
1043
+ }
1044
+
1045
+ /* There are no devices with an effective multiplier > 255 */
1046
+ if (unlikely(m == 0 || m > 255 || m < -255)) {
1047
+ hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
1048
+ m = 1;
1049
+ }
1050
+
1051
+ return m;
1052
+}
1053
+
1054
+static void hid_apply_multiplier_to_field(struct hid_device *hid,
1055
+ struct hid_field *field,
1056
+ struct hid_collection *multiplier_collection,
1057
+ int effective_multiplier)
1058
+{
1059
+ struct hid_collection *collection;
1060
+ struct hid_usage *usage;
1061
+ int i;
1062
+
1063
+ /*
1064
+ * If multiplier_collection is NULL, the multiplier applies
1065
+ * to all fields in the report.
1066
+ * Otherwise, it is the Logical Collection the multiplier applies to
1067
+ * but our field may be in a subcollection of that collection.
1068
+ */
1069
+ for (i = 0; i < field->maxusage; i++) {
1070
+ usage = &field->usage[i];
1071
+
1072
+ collection = &hid->collection[usage->collection_index];
1073
+ while (collection->parent_idx != -1 &&
1074
+ collection != multiplier_collection)
1075
+ collection = &hid->collection[collection->parent_idx];
1076
+
1077
+ if (collection->parent_idx != -1 ||
1078
+ multiplier_collection == NULL)
1079
+ usage->resolution_multiplier = effective_multiplier;
1080
+
1081
+ }
1082
+}
1083
+
1084
+static void hid_apply_multiplier(struct hid_device *hid,
1085
+ struct hid_field *multiplier)
1086
+{
1087
+ struct hid_report_enum *rep_enum;
1088
+ struct hid_report *rep;
1089
+ struct hid_field *field;
1090
+ struct hid_collection *multiplier_collection;
1091
+ int effective_multiplier;
1092
+ int i;
1093
+
1094
+ /*
1095
+ * "The Resolution Multiplier control must be contained in the same
1096
+ * Logical Collection as the control(s) to which it is to be applied.
1097
+ * If no Resolution Multiplier is defined, then the Resolution
1098
+ * Multiplier defaults to 1. If more than one control exists in a
1099
+ * Logical Collection, the Resolution Multiplier is associated with
1100
+ * all controls in the collection. If no Logical Collection is
1101
+ * defined, the Resolution Multiplier is associated with all
1102
+ * controls in the report."
1103
+ * HID Usage Table, v1.12, Section 4.3.1, p30
1104
+ *
1105
+ * Thus, search from the current collection upwards until we find a
1106
+ * logical collection. Then search all fields for that same parent
1107
+ * collection. Those are the fields the multiplier applies to.
1108
+ *
1109
+ * If we have more than one multiplier, it will overwrite the
1110
+ * applicable fields later.
1111
+ */
1112
+ multiplier_collection = &hid->collection[multiplier->usage->collection_index];
1113
+ while (multiplier_collection->parent_idx != -1 &&
1114
+ multiplier_collection->type != HID_COLLECTION_LOGICAL)
1115
+ multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
1116
+
1117
+ effective_multiplier = hid_calculate_multiplier(hid, multiplier);
1118
+
1119
+ rep_enum = &hid->report_enum[HID_INPUT_REPORT];
1120
+ list_for_each_entry(rep, &rep_enum->report_list, list) {
1121
+ for (i = 0; i < rep->maxfield; i++) {
1122
+ field = rep->field[i];
1123
+ hid_apply_multiplier_to_field(hid, field,
1124
+ multiplier_collection,
1125
+ effective_multiplier);
1126
+ }
1127
+ }
1128
+}
1129
+
1130
+/*
1131
+ * hid_setup_resolution_multiplier - set up all resolution multipliers
1132
+ *
1133
+ * @device: hid device
1134
+ *
1135
+ * Search for all Resolution Multiplier Feature Reports and apply their
1136
+ * value to all matching Input items. This only updates the internal struct
1137
+ * fields.
1138
+ *
1139
+ * The Resolution Multiplier is applied by the hardware. If the multiplier
1140
+ * is anything other than 1, the hardware will send pre-multiplied events
1141
+ * so that the same physical interaction generates an accumulated
1142
+ * accumulated_value = value * * multiplier
1143
+ * This may be achieved by sending
1144
+ * - "value * multiplier" for each event, or
1145
+ * - "value" but "multiplier" times as frequently, or
1146
+ * - a combination of the above
1147
+ * The only guarantee is that the same physical interaction always generates
1148
+ * an accumulated 'value * multiplier'.
1149
+ *
1150
+ * This function must be called before any event processing and after
1151
+ * any SetRequest to the Resolution Multiplier.
1152
+ */
1153
+void hid_setup_resolution_multiplier(struct hid_device *hid)
1154
+{
1155
+ struct hid_report_enum *rep_enum;
1156
+ struct hid_report *rep;
1157
+ struct hid_usage *usage;
1158
+ int i, j;
1159
+
1160
+ rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1161
+ list_for_each_entry(rep, &rep_enum->report_list, list) {
1162
+ for (i = 0; i < rep->maxfield; i++) {
1163
+ /* Ignore if report count is out of bounds. */
1164
+ if (rep->field[i]->report_count < 1)
1165
+ continue;
1166
+
1167
+ for (j = 0; j < rep->field[i]->maxusage; j++) {
1168
+ usage = &rep->field[i]->usage[j];
1169
+ if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
1170
+ hid_apply_multiplier(hid,
1171
+ rep->field[i]);
1172
+ }
1173
+ }
1174
+ }
1175
+}
1176
+EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
10061177
10071178 /**
10081179 * hid_open_report - open a driver-specific device report
....@@ -1102,9 +1273,17 @@
11021273 hid_err(device, "unbalanced delimiter at end of report description\n");
11031274 goto err;
11041275 }
1276
+
1277
+ /*
1278
+ * fetch initial values in case the device's
1279
+ * default multiplier isn't the recommended 1
1280
+ */
1281
+ hid_setup_resolution_multiplier(device);
1282
+
11051283 kfree(parser->collection_stack);
11061284 vfree(parser);
11071285 device->status |= HID_STAT_PARSED;
1286
+
11081287 return 0;
11091288 }
11101289 }
....@@ -1130,6 +1309,9 @@
11301309 {
11311310 if (!value || !n)
11321311 return 0;
1312
+
1313
+ if (n > 32)
1314
+ n = 32;
11331315
11341316 switch (n) {
11351317 case 8: return ((__s8)value);
....@@ -1194,8 +1376,8 @@
11941376 unsigned offset, unsigned n)
11951377 {
11961378 if (n > 32) {
1197
- hid_warn(hid, "hid_field_extract() called with n (%d) > 32! (%s)\n",
1198
- n, current->comm);
1379
+ hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n",
1380
+ __func__, n, current->comm);
11991381 n = 32;
12001382 }
12011383
....@@ -1275,7 +1457,7 @@
12751457 * hid_match_report - check if driver's raw_event should be called
12761458 *
12771459 * @hid: hid device
1278
- * @report_type: type to match against
1460
+ * @report: hid report to match against
12791461 *
12801462 * compare hid->driver->report_table->report_type to report->type
12811463 */
....@@ -1527,7 +1709,7 @@
15271709 * Implement a generic .request() callback, using .raw_request()
15281710 * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
15291711 */
1530
-void __hid_request(struct hid_device *hid, struct hid_report *report,
1712
+int __hid_request(struct hid_device *hid, struct hid_report *report,
15311713 int reqtype)
15321714 {
15331715 char *buf;
....@@ -1536,7 +1718,7 @@
15361718
15371719 buf = hid_alloc_report_buf(report, GFP_KERNEL);
15381720 if (!buf)
1539
- return;
1721
+ return -ENOMEM;
15401722
15411723 len = hid_report_len(report);
15421724
....@@ -1553,8 +1735,11 @@
15531735 if (reqtype == HID_REQ_GET_REPORT)
15541736 hid_input_report(hid, report->type, buf, ret, 0);
15551737
1738
+ ret = 0;
1739
+
15561740 out:
15571741 kfree(buf);
1742
+ return ret;
15581743 }
15591744 EXPORT_SYMBOL_GPL(__hid_request);
15601745
....@@ -1951,7 +2136,7 @@
19512136
19522137 /**
19532138 * store_new_id - add a new HID device ID to this driver and re-probe devices
1954
- * @driver: target device driver
2139
+ * @drv: target device driver
19552140 * @buf: buffer for scanning device ID data
19562141 * @count: input size
19572142 *
....@@ -2443,11 +2628,7 @@
24432628 hid_quirks_exit(HID_BUS_ANY);
24442629 }
24452630
2446
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
2447
-rootfs_initcall(hid_init);
2448
-#else
24492631 module_init(hid_init);
2450
-#endif
24512632 module_exit(hid_exit);
24522633
24532634 MODULE_AUTHOR("Andreas Gal");