Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'hid-for-linus-2025093001' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid

Pull HID updates from Benjamin Tissoires:

- haptic touchpad support (Angela Czubak and Jonathan Denose)

- support for audio jack handling on DualSense Playstation controllers
(Cristian Ciocaltea)

- allow HID-BPF to rebind a driver to hid-multitouch (Benjamin
Tissoires)

- rework hidraw ioctls to make them safer (and tested) (Benjamin
Tissoires)

- various PIDFF and universal-PIDFF fixes/improvements (Tomasz Pakuła)

- better configuration of Intel QuickI2C through ACPI (Xinpeng Sun)

- other assorted cleanups and fixes

* tag 'hid-for-linus-2025093001' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid: (58 commits)
HID: playstation: Switch to scoped_guard() in {dualsense|dualshock4}_output_worker()
HID: playstation: Silence sparse warnings for locking context imbalances
HID: playstation: Update SP preamp gain comment line
HID: intel-thc-hid: intel-quicki2c: support ACPI config for advanced features
HID: core: Change hid_driver to use a const char* for name
HID: hidraw: tighten ioctl command parsing
selftests/hid: hidraw: forge wrong ioctls and tests them
selftests/hid: hidraw: add more coverage for hidraw ioctls
selftests/hid: update vmtest.sh for virtme-ng
HID: playstation: Support DualSense audio jack event reporting
HID: playstation: Support DualSense audio jack hotplug detection
HID: playstation: Redefine DualSense input report status field
HID: playstation: Prefer kzalloc(sizeof(*buf)...)
HID: playstation: Document spinlock_t usage
HID: playstation: Fix all alignment and line length issues
HID: playstation: Correct spelling in comment sections
HID: playstation: Replace uint{32,16,8}_t with u{32,16,8}
HID: playstation: Simplify locking with guard() and scoped_guard()
HID: playstation: Add spaces around arithmetic operators
HID: playstation: Make use of bitfield macros
...

+3166 -1317
+14
Documentation/input/event-codes.rst
··· 400 400 All other axes retain their meaning. A device must not mix 401 401 regular directional axes and accelerometer axes on the same event node. 402 402 403 + INPUT_PROP_HAPTIC_TOUCHPAD 404 + -------------------------- 405 + 406 + The INPUT_PROP_HAPTIC_TOUCHPAD property indicates that device: 407 + - supports simple haptic auto and manual triggering 408 + - can differentiate between at least 5 fingers 409 + - uses correct resolution for the X/Y (units and value) 410 + - reports correct force per touch, and correct units for them (newtons or grams) 411 + - follows the MT protocol type B 412 + 413 + Summing up, such devices follow the MS spec for input devices in 414 + Win8 and Win8.1, and in addition support the Simple haptic controller HID table, 415 + and report correct units for the pressure. 416 + 403 417 Guidelines 404 418 ========== 405 419
+11
drivers/hid/Kconfig
··· 92 92 93 93 If unsure, say Y. 94 94 95 + config HID_HAPTIC 96 + tristate "Haptic touchpad support" 97 + default n 98 + help 99 + Support for touchpads with force sensors and haptic actuators instead of a 100 + traditional button. 101 + Adds extra parsing and FF device for the hid multitouch driver. 102 + It can be used for Elan 2703 haptic touchpad. 103 + 104 + If unsure, say N. 105 + 95 106 menu "Special HID drivers" 96 107 97 108 config HID_A4TECH
+1
drivers/hid/Makefile
··· 4 4 # 5 5 hid-y := hid-core.o hid-input.o hid-quirks.o 6 6 hid-$(CONFIG_DEBUG_FS) += hid-debug.o 7 + hid-$(CONFIG_HID_HAPTIC) += hid-haptic.o 7 8 8 9 obj-$(CONFIG_HID_BPF) += bpf/ 9 10
+3 -3
drivers/hid/hid-asus.c
··· 1388 1388 USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2), 1389 1389 QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD }, 1390 1390 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1391 - USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD3), 1392 - QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD }, 1393 - { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, 1394 1391 USB_DEVICE_ID_ASUSTEK_ROG_Z13_LIGHTBAR), 1395 1392 QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD }, 1396 1393 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, ··· 1416 1419 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard 1417 1420 * part, while letting hid-multitouch.c handle the touchpad. 1418 1421 */ 1422 + { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1423 + USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_ROG_Z13_FOLIO), 1424 + QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD }, 1419 1425 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 1420 1426 USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) }, 1421 1427 { }
+36 -8
drivers/hid/hid-core.c
··· 944 944 hid->group = HID_GROUP_GENERIC; 945 945 946 946 /* 947 + * In case we are re-scanning after a BPF has been loaded, 948 + * we need to use the bpf report descriptor, not the original one. 949 + */ 950 + if (hid->bpf_rdesc && hid->bpf_rsize) { 951 + start = hid->bpf_rdesc; 952 + end = start + hid->bpf_rsize; 953 + } 954 + 955 + /* 947 956 * The parsing is simpler than the one in hid_open_report() as we should 948 957 * be robust against hid errors. Those errors will be raised by 949 958 * hid_open_report() anyway. ··· 2717 2708 return !hid_ignore_special_drivers && !(hdev->quirks & HID_QUIRK_IGNORE_SPECIAL_DRIVER); 2718 2709 } 2719 2710 2711 + static void hid_set_group(struct hid_device *hdev) 2712 + { 2713 + int ret; 2714 + 2715 + if (hid_ignore_special_drivers) { 2716 + hdev->group = HID_GROUP_GENERIC; 2717 + } else if (!hdev->group && 2718 + !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) { 2719 + ret = hid_scan_report(hdev); 2720 + if (ret) 2721 + hid_warn(hdev, "bad device descriptor (%d)\n", ret); 2722 + } 2723 + } 2724 + 2720 2725 static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv) 2721 2726 { 2722 2727 const struct hid_device_id *id; 2723 2728 int ret; 2724 2729 2725 2730 if (!hdev->bpf_rsize) { 2731 + /* we keep a reference to the currently scanned report descriptor */ 2732 + const __u8 *original_rdesc = hdev->bpf_rdesc; 2733 + 2734 + if (!original_rdesc) 2735 + original_rdesc = hdev->dev_rdesc; 2736 + 2726 2737 /* in case a bpf program gets detached, we need to free the old one */ 2727 2738 hid_free_bpf_rdesc(hdev); 2728 2739 ··· 2752 2723 /* call_hid_bpf_rdesc_fixup will always return a valid pointer */ 2753 2724 hdev->bpf_rdesc = call_hid_bpf_rdesc_fixup(hdev, hdev->dev_rdesc, 2754 2725 &hdev->bpf_rsize); 2726 + 2727 + /* the report descriptor changed, we need to re-scan it */ 2728 + if (original_rdesc != hdev->bpf_rdesc) { 2729 + hdev->group = 0; 2730 + hid_set_group(hdev); 2731 + } 2755 2732 } 2756 2733 2757 2734 if (!hid_check_device_match(hdev, hdrv, &id)) ··· 2938 2903 /* 2939 2904 * Scan generic devices for group information 2940 2905 */ 2941 - if (hid_ignore_special_drivers) { 2942 - hdev->group = HID_GROUP_GENERIC; 2943 - } else if (!hdev->group && 2944 - !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) { 2945 - ret = hid_scan_report(hdev); 2946 - if (ret) 2947 - hid_warn(hdev, "bad device descriptor (%d)\n", ret); 2948 - } 2906 + hid_set_group(hdev); 2949 2907 2950 2908 hdev->id = atomic_inc_return(&id); 2951 2909
+580
drivers/hid/hid-haptic.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * HID Haptic support for Linux 4 + * 5 + * Copyright (c) 2021 Angela Czubak <acz@semihalf.com> 6 + */ 7 + 8 + #include <linux/input/mt.h> 9 + #include <linux/module.h> 10 + 11 + #include "hid-haptic.h" 12 + 13 + void hid_haptic_feature_mapping(struct hid_device *hdev, 14 + struct hid_haptic_device *haptic, 15 + struct hid_field *field, struct hid_usage *usage) 16 + { 17 + u16 usage_hid; 18 + 19 + if (usage->hid == HID_HP_AUTOTRIGGER) { 20 + if (usage->usage_index >= field->report_count) { 21 + dev_err(&hdev->dev, 22 + "HID_HP_AUTOTRIGGER out of range\n"); 23 + return; 24 + } 25 + 26 + hid_device_io_start(hdev); 27 + hid_hw_request(hdev, field->report, HID_REQ_GET_REPORT); 28 + hid_hw_wait(hdev); 29 + hid_device_io_stop(hdev); 30 + haptic->default_auto_trigger = 31 + field->value[usage->usage_index]; 32 + haptic->auto_trigger_report = field->report; 33 + } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ORDINAL) { 34 + usage_hid = usage->hid & HID_USAGE; 35 + switch (field->logical) { 36 + case HID_HP_WAVEFORMLIST: 37 + if (usage_hid > haptic->max_waveform_id) 38 + haptic->max_waveform_id = usage_hid; 39 + break; 40 + case HID_HP_DURATIONLIST: 41 + if (usage_hid > haptic->max_duration_id) 42 + haptic->max_duration_id = usage_hid; 43 + break; 44 + default: 45 + break; 46 + } 47 + } 48 + } 49 + EXPORT_SYMBOL_GPL(hid_haptic_feature_mapping); 50 + 51 + bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic, 52 + struct hid_input *hi, struct hid_field *field) 53 + { 54 + if (field->unit == HID_UNIT_GRAM || field->unit == HID_UNIT_NEWTON) { 55 + haptic->force_logical_minimum = field->logical_minimum; 56 + haptic->force_physical_minimum = field->physical_minimum; 57 + haptic->force_resolution = input_abs_get_res(hi->input, 58 + ABS_MT_PRESSURE); 59 + return true; 60 + } 61 + return false; 62 + } 63 + EXPORT_SYMBOL_GPL(hid_haptic_check_pressure_unit); 64 + 65 + int hid_haptic_input_mapping(struct hid_device *hdev, 66 + struct hid_haptic_device *haptic, 67 + struct hid_input *hi, 68 + struct hid_field *field, struct hid_usage *usage, 69 + unsigned long **bit, int *max) 70 + { 71 + if (usage->hid == HID_HP_MANUALTRIGGER) { 72 + haptic->manual_trigger_report = field->report; 73 + /* we don't really want to map these fields */ 74 + return -1; 75 + } 76 + 77 + return 0; 78 + } 79 + EXPORT_SYMBOL_GPL(hid_haptic_input_mapping); 80 + 81 + int hid_haptic_input_configured(struct hid_device *hdev, 82 + struct hid_haptic_device *haptic, 83 + struct hid_input *hi) 84 + { 85 + 86 + if (hi->application == HID_DG_TOUCHPAD) { 87 + if (haptic->auto_trigger_report && 88 + haptic->manual_trigger_report) { 89 + __set_bit(INPUT_PROP_HAPTIC_TOUCHPAD, hi->input->propbit); 90 + return 1; 91 + } 92 + return 0; 93 + } 94 + return -1; 95 + } 96 + EXPORT_SYMBOL_GPL(hid_haptic_input_configured); 97 + 98 + static void parse_auto_trigger_field(struct hid_haptic_device *haptic, 99 + struct hid_field *field) 100 + { 101 + int count = field->report_count; 102 + int n; 103 + u16 usage_hid; 104 + 105 + for (n = 0; n < count; n++) { 106 + switch (field->usage[n].hid & HID_USAGE_PAGE) { 107 + case HID_UP_ORDINAL: 108 + usage_hid = field->usage[n].hid & HID_USAGE; 109 + switch (field->logical) { 110 + case HID_HP_WAVEFORMLIST: 111 + haptic->hid_usage_map[usage_hid] = field->value[n]; 112 + if (field->value[n] == 113 + (HID_HP_WAVEFORMPRESS & HID_USAGE)) { 114 + haptic->press_ordinal = usage_hid; 115 + } else if (field->value[n] == 116 + (HID_HP_WAVEFORMRELEASE & HID_USAGE)) { 117 + haptic->release_ordinal = usage_hid; 118 + } 119 + break; 120 + case HID_HP_DURATIONLIST: 121 + haptic->duration_map[usage_hid] = 122 + field->value[n]; 123 + break; 124 + default: 125 + break; 126 + } 127 + break; 128 + case HID_UP_HAPTIC: 129 + switch (field->usage[n].hid) { 130 + case HID_HP_WAVEFORMVENDORID: 131 + haptic->vendor_id = field->value[n]; 132 + break; 133 + case HID_HP_WAVEFORMVENDORPAGE: 134 + haptic->vendor_page = field->value[n]; 135 + break; 136 + default: 137 + break; 138 + } 139 + break; 140 + default: 141 + /* Should not really happen */ 142 + break; 143 + } 144 + } 145 + } 146 + 147 + static void fill_effect_buf(struct hid_haptic_device *haptic, 148 + struct ff_haptic_effect *effect, 149 + struct hid_haptic_effect *haptic_effect, 150 + int waveform_ordinal) 151 + { 152 + struct hid_report *rep = haptic->manual_trigger_report; 153 + struct hid_usage *usage; 154 + struct hid_field *field; 155 + s32 value; 156 + int i, j; 157 + u8 *buf = haptic_effect->report_buf; 158 + 159 + mutex_lock(&haptic->manual_trigger_mutex); 160 + for (i = 0; i < rep->maxfield; i++) { 161 + field = rep->field[i]; 162 + /* Ignore if report count is out of bounds. */ 163 + if (field->report_count < 1) 164 + continue; 165 + 166 + for (j = 0; j < field->maxusage; j++) { 167 + usage = &field->usage[j]; 168 + 169 + switch (usage->hid) { 170 + case HID_HP_INTENSITY: 171 + if (effect->intensity > 100) { 172 + value = field->logical_maximum; 173 + } else { 174 + value = field->logical_minimum + 175 + effect->intensity * 176 + (field->logical_maximum - 177 + field->logical_minimum) / 100; 178 + } 179 + break; 180 + case HID_HP_REPEATCOUNT: 181 + value = effect->repeat_count; 182 + break; 183 + case HID_HP_RETRIGGERPERIOD: 184 + value = effect->retrigger_period; 185 + break; 186 + case HID_HP_MANUALTRIGGER: 187 + value = waveform_ordinal; 188 + break; 189 + default: 190 + break; 191 + } 192 + 193 + field->value[j] = value; 194 + } 195 + } 196 + 197 + hid_output_report(rep, buf); 198 + mutex_unlock(&haptic->manual_trigger_mutex); 199 + } 200 + 201 + static void switch_mode(struct hid_device *hdev, struct hid_haptic_device *haptic, 202 + int mode) 203 + { 204 + struct hid_report *rep = haptic->auto_trigger_report; 205 + struct hid_field *field; 206 + s32 value; 207 + int i, j; 208 + 209 + if (mode == HID_HAPTIC_MODE_HOST) 210 + value = HID_HAPTIC_ORDINAL_WAVEFORMSTOP; 211 + else 212 + value = haptic->default_auto_trigger; 213 + 214 + mutex_lock(&haptic->auto_trigger_mutex); 215 + for (i = 0; i < rep->maxfield; i++) { 216 + field = rep->field[i]; 217 + /* Ignore if report count is out of bounds. */ 218 + if (field->report_count < 1) 219 + continue; 220 + 221 + for (j = 0; j < field->maxusage; j++) { 222 + if (field->usage[j].hid == HID_HP_AUTOTRIGGER) 223 + field->value[j] = value; 224 + } 225 + } 226 + 227 + /* send the report */ 228 + hid_hw_request(hdev, rep, HID_REQ_SET_REPORT); 229 + mutex_unlock(&haptic->auto_trigger_mutex); 230 + haptic->mode = mode; 231 + } 232 + 233 + static int hid_haptic_upload_effect(struct input_dev *dev, struct ff_effect *effect, 234 + struct ff_effect *old) 235 + { 236 + struct hid_device *hdev = input_get_drvdata(dev); 237 + struct ff_device *ff = dev->ff; 238 + struct hid_haptic_device *haptic = ff->private; 239 + int i, ordinal = 0; 240 + bool switch_modes = false; 241 + 242 + /* If vendor range, check vendor id and page */ 243 + if (effect->u.haptic.hid_usage >= (HID_HP_VENDORWAVEFORMMIN & HID_USAGE) && 244 + effect->u.haptic.hid_usage <= (HID_HP_VENDORWAVEFORMMAX & HID_USAGE) && 245 + (effect->u.haptic.vendor_id != haptic->vendor_id || 246 + effect->u.haptic.vendor_waveform_page != haptic->vendor_page)) 247 + return -EINVAL; 248 + 249 + /* Check hid_usage */ 250 + for (i = 1; i <= haptic->max_waveform_id; i++) { 251 + if (haptic->hid_usage_map[i] == effect->u.haptic.hid_usage) { 252 + ordinal = i; 253 + break; 254 + } 255 + } 256 + if (ordinal < 1) 257 + return -EINVAL; 258 + 259 + /* Fill the buffer for the effect id */ 260 + fill_effect_buf(haptic, &effect->u.haptic, &haptic->effect[effect->id], 261 + ordinal); 262 + 263 + if (effect->u.haptic.hid_usage == (HID_HP_WAVEFORMPRESS & HID_USAGE) || 264 + effect->u.haptic.hid_usage == (HID_HP_WAVEFORMRELEASE & HID_USAGE)) 265 + switch_modes = true; 266 + 267 + /* If device is in autonomous mode, and the uploaded effect signals userspace 268 + * wants control of the device, change modes 269 + */ 270 + if (switch_modes && haptic->mode == HID_HAPTIC_MODE_DEVICE) 271 + switch_mode(hdev, haptic, HID_HAPTIC_MODE_HOST); 272 + 273 + return 0; 274 + } 275 + 276 + static int play_effect(struct hid_device *hdev, struct hid_haptic_device *haptic, 277 + struct hid_haptic_effect *effect) 278 + { 279 + int ret; 280 + 281 + ret = hid_hw_output_report(hdev, effect->report_buf, 282 + haptic->manual_trigger_report_len); 283 + if (ret < 0) { 284 + ret = hid_hw_raw_request(hdev, 285 + haptic->manual_trigger_report->id, 286 + effect->report_buf, 287 + haptic->manual_trigger_report_len, 288 + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 289 + } 290 + 291 + return ret; 292 + } 293 + 294 + static void haptic_work_handler(struct work_struct *work) 295 + { 296 + 297 + struct hid_haptic_effect *effect = container_of(work, 298 + struct hid_haptic_effect, 299 + work); 300 + struct input_dev *dev = effect->input_dev; 301 + struct hid_device *hdev = input_get_drvdata(dev); 302 + struct hid_haptic_device *haptic = dev->ff->private; 303 + 304 + mutex_lock(&haptic->manual_trigger_mutex); 305 + if (effect != &haptic->stop_effect) 306 + play_effect(hdev, haptic, &haptic->stop_effect); 307 + 308 + play_effect(hdev, haptic, effect); 309 + mutex_unlock(&haptic->manual_trigger_mutex); 310 + 311 + } 312 + 313 + static int hid_haptic_playback(struct input_dev *dev, int effect_id, int value) 314 + { 315 + struct hid_haptic_device *haptic = dev->ff->private; 316 + 317 + if (value) 318 + queue_work(haptic->wq, &haptic->effect[effect_id].work); 319 + else 320 + queue_work(haptic->wq, &haptic->stop_effect.work); 321 + 322 + return 0; 323 + } 324 + 325 + static void effect_set_default(struct ff_effect *effect) 326 + { 327 + effect->type = FF_HAPTIC; 328 + effect->id = -1; 329 + effect->u.haptic.hid_usage = HID_HP_WAVEFORMNONE & HID_USAGE; 330 + effect->u.haptic.intensity = 100; 331 + effect->u.haptic.retrigger_period = 0; 332 + effect->u.haptic.repeat_count = 0; 333 + } 334 + 335 + static int hid_haptic_erase(struct input_dev *dev, int effect_id) 336 + { 337 + struct hid_haptic_device *haptic = dev->ff->private; 338 + struct hid_device *hdev = input_get_drvdata(dev); 339 + struct ff_effect effect; 340 + int ordinal; 341 + 342 + effect_set_default(&effect); 343 + 344 + if (effect.u.haptic.hid_usage == (HID_HP_WAVEFORMRELEASE & HID_USAGE)) { 345 + ordinal = haptic->release_ordinal; 346 + if (!ordinal) { 347 + ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE; 348 + if (haptic->mode == HID_HAPTIC_MODE_HOST) 349 + switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE); 350 + } else 351 + effect.u.haptic.hid_usage = HID_HP_WAVEFORMRELEASE & HID_USAGE; 352 + 353 + fill_effect_buf(haptic, &effect.u.haptic, &haptic->effect[effect_id], 354 + ordinal); 355 + } else if (effect.u.haptic.hid_usage == (HID_HP_WAVEFORMPRESS & HID_USAGE)) { 356 + ordinal = haptic->press_ordinal; 357 + if (!ordinal) { 358 + ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE; 359 + if (haptic->mode == HID_HAPTIC_MODE_HOST) 360 + switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE); 361 + } 362 + else 363 + effect.u.haptic.hid_usage = HID_HP_WAVEFORMPRESS & HID_USAGE; 364 + 365 + fill_effect_buf(haptic, &effect.u.haptic, &haptic->effect[effect_id], 366 + ordinal); 367 + } 368 + 369 + return 0; 370 + } 371 + 372 + static void hid_haptic_destroy(struct ff_device *ff) 373 + { 374 + struct hid_haptic_device *haptic = ff->private; 375 + struct hid_device *hdev = haptic->hdev; 376 + int r; 377 + 378 + if (hdev) 379 + put_device(&hdev->dev); 380 + 381 + kfree(haptic->stop_effect.report_buf); 382 + haptic->stop_effect.report_buf = NULL; 383 + 384 + if (haptic->effect) { 385 + for (r = 0; r < ff->max_effects; r++) 386 + kfree(haptic->effect[r].report_buf); 387 + kfree(haptic->effect); 388 + } 389 + haptic->effect = NULL; 390 + 391 + destroy_workqueue(haptic->wq); 392 + haptic->wq = NULL; 393 + 394 + kfree(haptic->duration_map); 395 + haptic->duration_map = NULL; 396 + 397 + kfree(haptic->hid_usage_map); 398 + haptic->hid_usage_map = NULL; 399 + 400 + module_put(THIS_MODULE); 401 + } 402 + 403 + int hid_haptic_init(struct hid_device *hdev, 404 + struct hid_haptic_device **haptic_ptr) 405 + { 406 + struct hid_haptic_device *haptic = *haptic_ptr; 407 + struct input_dev *dev = NULL; 408 + struct hid_input *hidinput; 409 + struct ff_device *ff; 410 + int ret = 0, r; 411 + struct ff_haptic_effect stop_effect = { 412 + .hid_usage = HID_HP_WAVEFORMSTOP & HID_USAGE, 413 + }; 414 + const char *prefix = "hid-haptic"; 415 + char *name; 416 + int (*flush)(struct input_dev *dev, struct file *file); 417 + int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); 418 + 419 + haptic->hdev = hdev; 420 + haptic->max_waveform_id = max(2u, haptic->max_waveform_id); 421 + haptic->max_duration_id = max(2u, haptic->max_duration_id); 422 + 423 + haptic->hid_usage_map = kcalloc(haptic->max_waveform_id + 1, 424 + sizeof(u16), GFP_KERNEL); 425 + if (!haptic->hid_usage_map) { 426 + ret = -ENOMEM; 427 + goto exit; 428 + } 429 + haptic->duration_map = kcalloc(haptic->max_duration_id + 1, 430 + sizeof(u32), GFP_KERNEL); 431 + if (!haptic->duration_map) { 432 + ret = -ENOMEM; 433 + goto usage_map; 434 + } 435 + 436 + if (haptic->max_waveform_id != haptic->max_duration_id) 437 + dev_warn(&hdev->dev, 438 + "Haptic duration and waveform lists have different max id (%u and %u).\n", 439 + haptic->max_duration_id, haptic->max_waveform_id); 440 + 441 + haptic->hid_usage_map[HID_HAPTIC_ORDINAL_WAVEFORMNONE] = 442 + HID_HP_WAVEFORMNONE & HID_USAGE; 443 + haptic->hid_usage_map[HID_HAPTIC_ORDINAL_WAVEFORMSTOP] = 444 + HID_HP_WAVEFORMSTOP & HID_USAGE; 445 + 446 + mutex_init(&haptic->auto_trigger_mutex); 447 + for (r = 0; r < haptic->auto_trigger_report->maxfield; r++) 448 + parse_auto_trigger_field(haptic, haptic->auto_trigger_report->field[r]); 449 + 450 + list_for_each_entry(hidinput, &hdev->inputs, list) { 451 + if (hidinput->application == HID_DG_TOUCHPAD) { 452 + dev = hidinput->input; 453 + break; 454 + } 455 + } 456 + 457 + if (!dev) { 458 + dev_err(&hdev->dev, "Failed to find the input device\n"); 459 + ret = -ENODEV; 460 + goto duration_map; 461 + } 462 + 463 + haptic->input_dev = dev; 464 + haptic->manual_trigger_report_len = 465 + hid_report_len(haptic->manual_trigger_report); 466 + mutex_init(&haptic->manual_trigger_mutex); 467 + name = kmalloc(strlen(prefix) + strlen(hdev->name) + 2, GFP_KERNEL); 468 + if (name) { 469 + sprintf(name, "%s %s", prefix, hdev->name); 470 + haptic->wq = create_singlethread_workqueue(name); 471 + kfree(name); 472 + } 473 + if (!haptic->wq) { 474 + ret = -ENOMEM; 475 + goto duration_map; 476 + } 477 + haptic->effect = kcalloc(FF_MAX_EFFECTS, 478 + sizeof(struct hid_haptic_effect), GFP_KERNEL); 479 + if (!haptic->effect) { 480 + ret = -ENOMEM; 481 + goto output_queue; 482 + } 483 + for (r = 0; r < FF_MAX_EFFECTS; r++) { 484 + haptic->effect[r].report_buf = 485 + hid_alloc_report_buf(haptic->manual_trigger_report, 486 + GFP_KERNEL); 487 + if (!haptic->effect[r].report_buf) { 488 + dev_err(&hdev->dev, 489 + "Failed to allocate a buffer for an effect.\n"); 490 + ret = -ENOMEM; 491 + goto buffer_free; 492 + } 493 + haptic->effect[r].input_dev = dev; 494 + INIT_WORK(&haptic->effect[r].work, haptic_work_handler); 495 + } 496 + haptic->stop_effect.report_buf = 497 + hid_alloc_report_buf(haptic->manual_trigger_report, 498 + GFP_KERNEL); 499 + if (!haptic->stop_effect.report_buf) { 500 + dev_err(&hdev->dev, 501 + "Failed to allocate a buffer for stop effect.\n"); 502 + ret = -ENOMEM; 503 + goto buffer_free; 504 + } 505 + haptic->stop_effect.input_dev = dev; 506 + INIT_WORK(&haptic->stop_effect.work, haptic_work_handler); 507 + fill_effect_buf(haptic, &stop_effect, &haptic->stop_effect, 508 + HID_HAPTIC_ORDINAL_WAVEFORMSTOP); 509 + 510 + input_set_capability(dev, EV_FF, FF_HAPTIC); 511 + 512 + flush = dev->flush; 513 + event = dev->event; 514 + ret = input_ff_create(dev, FF_MAX_EFFECTS); 515 + if (ret) { 516 + dev_err(&hdev->dev, "Failed to create ff device.\n"); 517 + goto stop_buffer_free; 518 + } 519 + 520 + ff = dev->ff; 521 + ff->private = haptic; 522 + ff->upload = hid_haptic_upload_effect; 523 + ff->playback = hid_haptic_playback; 524 + ff->erase = hid_haptic_erase; 525 + ff->destroy = hid_haptic_destroy; 526 + if (!try_module_get(THIS_MODULE)) { 527 + dev_err(&hdev->dev, "Failed to increase module count.\n"); 528 + goto input_free; 529 + } 530 + if (!get_device(&hdev->dev)) { 531 + dev_err(&hdev->dev, "Failed to get hdev device.\n"); 532 + module_put(THIS_MODULE); 533 + goto input_free; 534 + } 535 + return 0; 536 + 537 + input_free: 538 + input_ff_destroy(dev); 539 + /* Do not let double free happen, input_ff_destroy will call 540 + * hid_haptic_destroy. 541 + */ 542 + *haptic_ptr = NULL; 543 + /* Restore dev flush and event */ 544 + dev->flush = flush; 545 + dev->event = event; 546 + return ret; 547 + stop_buffer_free: 548 + kfree(haptic->stop_effect.report_buf); 549 + haptic->stop_effect.report_buf = NULL; 550 + buffer_free: 551 + while (--r >= 0) 552 + kfree(haptic->effect[r].report_buf); 553 + kfree(haptic->effect); 554 + haptic->effect = NULL; 555 + output_queue: 556 + destroy_workqueue(haptic->wq); 557 + haptic->wq = NULL; 558 + duration_map: 559 + kfree(haptic->duration_map); 560 + haptic->duration_map = NULL; 561 + usage_map: 562 + kfree(haptic->hid_usage_map); 563 + haptic->hid_usage_map = NULL; 564 + exit: 565 + return ret; 566 + } 567 + EXPORT_SYMBOL_GPL(hid_haptic_init); 568 + 569 + void hid_haptic_pressure_reset(struct hid_haptic_device *haptic) 570 + { 571 + haptic->pressure_sum = 0; 572 + } 573 + EXPORT_SYMBOL_GPL(hid_haptic_pressure_reset); 574 + 575 + void hid_haptic_pressure_increase(struct hid_haptic_device *haptic, 576 + __s32 pressure) 577 + { 578 + haptic->pressure_sum += pressure; 579 + } 580 + EXPORT_SYMBOL_GPL(hid_haptic_pressure_increase);
+127
drivers/hid/hid-haptic.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * HID Haptic support for Linux 4 + * 5 + * Copyright (c) 2021 Angela Czubak <acz@semihalf.com> 6 + */ 7 + 8 + #include <linux/hid.h> 9 + 10 + #define HID_HAPTIC_ORDINAL_WAVEFORMNONE 1 11 + #define HID_HAPTIC_ORDINAL_WAVEFORMSTOP 2 12 + 13 + #define HID_HAPTIC_MODE_DEVICE 0 14 + #define HID_HAPTIC_MODE_HOST 1 15 + 16 + struct hid_haptic_effect { 17 + u8 *report_buf; 18 + struct input_dev *input_dev; 19 + struct work_struct work; 20 + struct list_head control; 21 + struct mutex control_mutex; 22 + }; 23 + 24 + struct hid_haptic_effect_node { 25 + struct list_head node; 26 + struct file *file; 27 + }; 28 + 29 + struct hid_haptic_device { 30 + struct input_dev *input_dev; 31 + struct hid_device *hdev; 32 + struct hid_report *auto_trigger_report; 33 + struct mutex auto_trigger_mutex; 34 + struct workqueue_struct *wq; 35 + struct hid_report *manual_trigger_report; 36 + struct mutex manual_trigger_mutex; 37 + size_t manual_trigger_report_len; 38 + int pressed_state; 39 + s32 pressure_sum; 40 + s32 force_logical_minimum; 41 + s32 force_physical_minimum; 42 + s32 force_resolution; 43 + u32 mode; 44 + u32 default_auto_trigger; 45 + u32 vendor_page; 46 + u32 vendor_id; 47 + u32 max_waveform_id; 48 + u32 max_duration_id; 49 + u16 *hid_usage_map; 50 + u32 *duration_map; 51 + u16 press_ordinal; 52 + u16 release_ordinal; 53 + struct hid_haptic_effect *effect; 54 + struct hid_haptic_effect stop_effect; 55 + }; 56 + 57 + #if IS_ENABLED(CONFIG_HID_HAPTIC) 58 + void hid_haptic_feature_mapping(struct hid_device *hdev, 59 + struct hid_haptic_device *haptic, 60 + struct hid_field *field, struct hid_usage 61 + *usage); 62 + bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic, 63 + struct hid_input *hi, struct hid_field *field); 64 + int hid_haptic_input_mapping(struct hid_device *hdev, 65 + struct hid_haptic_device *haptic, 66 + struct hid_input *hi, 67 + struct hid_field *field, struct hid_usage *usage, 68 + unsigned long **bit, int *max); 69 + int hid_haptic_input_configured(struct hid_device *hdev, 70 + struct hid_haptic_device *haptic, 71 + struct hid_input *hi); 72 + int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device **haptic_ptr); 73 + void hid_haptic_handle_press_release(struct hid_haptic_device *haptic); 74 + void hid_haptic_pressure_reset(struct hid_haptic_device *haptic); 75 + void hid_haptic_pressure_increase(struct hid_haptic_device *haptic, 76 + __s32 pressure); 77 + #else 78 + static inline 79 + void hid_haptic_feature_mapping(struct hid_device *hdev, 80 + struct hid_haptic_device *haptic, 81 + struct hid_field *field, struct hid_usage 82 + *usage) 83 + {} 84 + static inline 85 + bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic, 86 + struct hid_input *hi, struct hid_field *field) 87 + { 88 + return false; 89 + } 90 + static inline 91 + int hid_haptic_input_mapping(struct hid_device *hdev, 92 + struct hid_haptic_device *haptic, 93 + struct hid_input *hi, 94 + struct hid_field *field, struct hid_usage *usage, 95 + unsigned long **bit, int *max) 96 + { 97 + return 0; 98 + } 99 + static inline 100 + int hid_haptic_input_configured(struct hid_device *hdev, 101 + struct hid_haptic_device *haptic, 102 + struct hid_input *hi) 103 + { 104 + return 0; 105 + } 106 + static inline 107 + void hid_haptic_reset(struct hid_device *hdev, struct hid_haptic_device *haptic) 108 + {} 109 + static inline 110 + int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device **haptic_ptr) 111 + { 112 + return 0; 113 + } 114 + static inline 115 + void hid_haptic_handle_press_release(struct hid_haptic_device *haptic) {} 116 + static inline 117 + bool hid_haptic_handle_input(struct hid_haptic_device *haptic) 118 + { 119 + return false; 120 + } 121 + static inline 122 + void hid_haptic_pressure_reset(struct hid_haptic_device *haptic) {} 123 + static inline 124 + void hid_haptic_pressure_increase(struct hid_haptic_device *haptic, 125 + __s32 pressure) 126 + {} 127 + #endif
+3 -1
drivers/hid/hid-ids.h
··· 223 223 #define USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3 0x1822 224 224 #define USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD 0x1866 225 225 #define USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2 0x19b6 226 - #define USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD3 0x1a30 226 + #define USB_DEVICE_ID_ASUSTEK_ROG_Z13_FOLIO 0x1a30 227 227 #define USB_DEVICE_ID_ASUSTEK_ROG_Z13_LIGHTBAR 0x18c6 228 228 #define USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY 0x1abe 229 229 #define USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X 0x1b4c ··· 1296 1296 1297 1297 #define USB_VENDOR_ID_STEELSERIES 0x1038 1298 1298 #define USB_DEVICE_ID_STEELSERIES_SRWS1 0x1410 1299 + #define USB_DEVICE_ID_STEELSERIES_ARCTIS_1 0x12b6 1300 + #define USB_DEVICE_ID_STEELSERIES_ARCTIS_9 0x12c2 1299 1301 1300 1302 #define USB_VENDOR_ID_SUN 0x0430 1301 1303 #define USB_DEVICE_ID_RARITAN_KVM_DONGLE 0xcdab
+16 -2
drivers/hid/hid-input.c
··· 303 303 } 304 304 break; 305 305 306 + case ABS_PRESSURE: 307 + case ABS_MT_PRESSURE: 308 + if (field->unit == HID_UNIT_NEWTON) { 309 + /* Convert to grams, 1 newton is 101.97 grams */ 310 + prev = physical_extents; 311 + physical_extents *= 10197; 312 + if (physical_extents < prev) 313 + return 0; 314 + unit_exponent -= 2; 315 + } else if (field->unit != HID_UNIT_GRAM) { 316 + return 0; 317 + } 318 + break; 306 319 default: 307 320 return 0; 308 321 } ··· 696 683 if (field->report_count < 1) 697 684 goto ignore; 698 685 699 - /* only LED usages are supported in output fields */ 686 + /* only LED and HAPTIC usages are supported in output fields */ 700 687 if (field->report_type == HID_OUTPUT_REPORT && 701 - (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) { 688 + (usage->hid & HID_USAGE_PAGE) != HID_UP_LED && 689 + (usage->hid & HID_USAGE_PAGE) != HID_UP_HAPTIC) { 702 690 goto ignore; 703 691 } 704 692
+47
drivers/hid/hid-multitouch.c
··· 49 49 50 50 #include "hid-ids.h" 51 51 52 + #include "hid-haptic.h" 53 + 52 54 /* quirks to control the device */ 53 55 #define MT_QUIRK_NOT_SEEN_MEANS_UP BIT(0) 54 56 #define MT_QUIRK_SLOT_IS_CONTACTID BIT(1) ··· 170 168 struct mt_device { 171 169 struct mt_class mtclass; /* our mt device class */ 172 170 struct timer_list release_timer; /* to release sticky fingers */ 171 + struct hid_haptic_device *haptic; /* haptic related configuration */ 173 172 struct hid_device *hdev; /* hid_device we're attached to */ 174 173 unsigned long mt_io_flags; /* mt flags (MT_IO_FLAGS_*) */ 175 174 __u8 inputmode_value; /* InputMode HID feature value */ 176 175 __u8 maxcontacts; 177 176 bool is_buttonpad; /* is this device a button pad? */ 177 + bool is_haptic_touchpad; /* is this device a haptic touchpad? */ 178 178 bool serial_maybe; /* need to check for serial protocol */ 179 179 180 180 struct list_head applications; ··· 537 533 mt_get_feature(hdev, field->report); 538 534 break; 539 535 } 536 + 537 + hid_haptic_feature_mapping(hdev, td->haptic, field, usage); 540 538 } 541 539 542 540 static void set_abs(struct input_dev *input, unsigned int code, ··· 894 888 case HID_DG_TIPPRESSURE: 895 889 set_abs(hi->input, ABS_MT_PRESSURE, field, 896 890 cls->sn_pressure); 891 + td->is_haptic_touchpad = 892 + hid_haptic_check_pressure_unit(td->haptic, 893 + hi, field); 897 894 MT_STORE_FIELD(p); 898 895 return 1; 899 896 case HID_DG_SCANTIME: ··· 1017 1008 1018 1009 app->num_received = 0; 1019 1010 app->left_button_state = 0; 1011 + if (td->is_haptic_touchpad) 1012 + hid_haptic_pressure_reset(td->haptic); 1020 1013 1021 1014 if (test_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags)) 1022 1015 set_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags); ··· 1175 1164 major = major >> 1; 1176 1165 minor = minor >> 1; 1177 1166 } 1167 + 1168 + if (td->is_haptic_touchpad) 1169 + hid_haptic_pressure_increase(td->haptic, *slot->p); 1178 1170 1179 1171 x = hdev->quirks & HID_QUIRK_X_INVERT ? 1180 1172 input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->x : ··· 1380 1366 if (cls->is_indirect) 1381 1367 app->mt_flags |= INPUT_MT_POINTER; 1382 1368 1369 + if (td->is_haptic_touchpad) 1370 + app->mt_flags |= INPUT_MT_TOTAL_FORCE; 1371 + 1383 1372 if (app->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) 1384 1373 app->mt_flags |= INPUT_MT_DROP_UNUSED; 1385 1374 ··· 1418 1401 struct mt_device *td = hid_get_drvdata(hdev); 1419 1402 struct mt_application *application; 1420 1403 struct mt_report_data *rdata; 1404 + int ret; 1421 1405 1422 1406 rdata = mt_find_report_data(td, field->report); 1423 1407 if (!rdata) { ··· 1480 1462 */ 1481 1463 if (field->physical == HID_DG_STYLUS) 1482 1464 hi->application = HID_DG_STYLUS; 1465 + 1466 + ret = hid_haptic_input_mapping(hdev, td->haptic, hi, field, usage, bit, 1467 + max); 1468 + if (ret != 0) 1469 + return ret; 1483 1470 1484 1471 /* let hid-core decide for the others */ 1485 1472 return 0; ··· 1708 1685 struct hid_report *report; 1709 1686 int ret; 1710 1687 1688 + if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 || 1689 + td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT)) { 1690 + if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0) 1691 + td->is_haptic_touchpad = false; 1692 + } else { 1693 + td->is_haptic_touchpad = false; 1694 + } 1695 + 1711 1696 list_for_each_entry(report, &hi->reports, hidinput_list) { 1712 1697 rdata = mt_find_report_data(td, report); 1713 1698 if (!rdata) { ··· 1858 1827 dev_err(&hdev->dev, "cannot allocate multitouch data\n"); 1859 1828 return -ENOMEM; 1860 1829 } 1830 + td->haptic = devm_kzalloc(&hdev->dev, sizeof(*(td->haptic)), GFP_KERNEL); 1831 + if (!td->haptic) 1832 + return -ENOMEM; 1833 + 1834 + td->haptic->hdev = hdev; 1861 1835 td->hdev = hdev; 1862 1836 td->mtclass = *mtclass; 1863 1837 td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN; ··· 1930 1894 hdev->name); 1931 1895 1932 1896 mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL); 1897 + 1898 + if (td->is_haptic_touchpad) { 1899 + if (hid_haptic_init(hdev, &td->haptic)) { 1900 + dev_warn(&hdev->dev, "Cannot allocate haptic for %s\n", 1901 + hdev->name); 1902 + td->is_haptic_touchpad = false; 1903 + devm_kfree(&hdev->dev, td->haptic); 1904 + } 1905 + } else { 1906 + devm_kfree(&hdev->dev, td->haptic); 1907 + } 1933 1908 1934 1909 return 0; 1935 1910 }
+606 -481
drivers/hid/hid-playstation.c
··· 5 5 * Copyright (c) 2020-2022 Sony Interactive Entertainment 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/bits.h> 10 + #include <linux/cleanup.h> 9 11 #include <linux/crc32.h> 10 12 #include <linux/device.h> 11 13 #include <linux/hid.h> ··· 38 36 struct ps_device { 39 37 struct list_head list; 40 38 struct hid_device *hdev; 41 - spinlock_t lock; 39 + spinlock_t lock; /* Sync between event handler and workqueue */ 42 40 43 - uint32_t player_id; 41 + u32 player_id; 44 42 45 43 struct power_supply_desc battery_desc; 46 44 struct power_supply *battery; 47 - uint8_t battery_capacity; 45 + u8 battery_capacity; 48 46 int battery_status; 49 47 50 48 const char *input_dev_name; /* Name of primary input device. */ 51 - uint8_t mac_address[6]; /* Note: stored in little endian order. */ 52 - uint32_t hw_version; 53 - uint32_t fw_version; 49 + u8 mac_address[6]; /* Note: stored in little endian order. */ 50 + u32 hw_version; 51 + u32 fw_version; 54 52 55 53 int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size); 56 54 void (*remove)(struct ps_device *dev); ··· 112 110 #define DS_BUTTONS2_TOUCHPAD BIT(1) 113 111 #define DS_BUTTONS2_MIC_MUTE BIT(2) 114 112 115 - /* Status field of DualSense input report. */ 116 - #define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0) 117 - #define DS_STATUS_CHARGING GENMASK(7, 4) 118 - #define DS_STATUS_CHARGING_SHIFT 4 113 + /* Status fields of DualSense input report. */ 114 + #define DS_STATUS0_BATTERY_CAPACITY GENMASK(3, 0) 115 + #define DS_STATUS0_CHARGING GENMASK(7, 4) 116 + #define DS_STATUS1_HP_DETECT BIT(0) 117 + #define DS_STATUS1_MIC_DETECT BIT(1) 118 + #define DS_STATUS1_JACK_DETECT (DS_STATUS1_HP_DETECT | DS_STATUS1_MIC_DETECT) 119 + #define DS_STATUS1_MIC_MUTE BIT(2) 119 120 120 121 /* Feature version from DualSense Firmware Info report. */ 121 - #define DS_FEATURE_VERSION(major, minor) ((major & 0xff) << 8 | (minor & 0xff)) 122 - 122 + #define DS_FEATURE_VERSION_MINOR GENMASK(7, 0) 123 + #define DS_FEATURE_VERSION_MAJOR GENMASK(15, 8) 124 + #define DS_FEATURE_VERSION(major, minor) (FIELD_PREP(DS_FEATURE_VERSION_MAJOR, major) | \ 125 + FIELD_PREP(DS_FEATURE_VERSION_MINOR, minor)) 123 126 /* 124 127 * Status of a DualSense touch point contact. 125 128 * Contact IDs, with highest bit set are 'inactive' 126 129 * and any associated data is then invalid. 127 130 */ 128 - #define DS_TOUCH_POINT_INACTIVE BIT(7) 131 + #define DS_TOUCH_POINT_INACTIVE BIT(7) 132 + #define DS_TOUCH_POINT_X_LO GENMASK(7, 0) 133 + #define DS_TOUCH_POINT_X_HI GENMASK(11, 8) 134 + #define DS_TOUCH_POINT_X(hi, lo) (FIELD_PREP(DS_TOUCH_POINT_X_HI, hi) | \ 135 + FIELD_PREP(DS_TOUCH_POINT_X_LO, lo)) 136 + #define DS_TOUCH_POINT_Y_LO GENMASK(3, 0) 137 + #define DS_TOUCH_POINT_Y_HI GENMASK(11, 4) 138 + #define DS_TOUCH_POINT_Y(hi, lo) (FIELD_PREP(DS_TOUCH_POINT_Y_HI, hi) | \ 139 + FIELD_PREP(DS_TOUCH_POINT_Y_LO, lo)) 129 140 130 141 /* Magic value required in tag field of Bluetooth output report. */ 131 - #define DS_OUTPUT_TAG 0x10 142 + #define DS_OUTPUT_TAG 0x10 143 + #define DS_OUTPUT_SEQ_TAG GENMASK(3, 0) 144 + #define DS_OUTPUT_SEQ_NO GENMASK(7, 4) 132 145 /* Flags for DualSense output report. */ 133 - #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0) 134 - #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1) 135 - #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0) 136 - #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1) 137 - #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2) 138 - #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3) 139 - #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4) 140 - #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1) 141 - #define DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2 BIT(2) 142 - #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4) 143 - #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1) 146 + #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0) 147 + #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1) 148 + #define DS_OUTPUT_VALID_FLAG0_SPEAKER_VOLUME_ENABLE BIT(5) 149 + #define DS_OUTPUT_VALID_FLAG0_MIC_VOLUME_ENABLE BIT(6) 150 + #define DS_OUTPUT_VALID_FLAG0_AUDIO_CONTROL_ENABLE BIT(7) 151 + #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0) 152 + #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1) 153 + #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2) 154 + #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3) 155 + #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4) 156 + #define DS_OUTPUT_VALID_FLAG1_AUDIO_CONTROL2_ENABLE BIT(7) 157 + #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1) 158 + #define DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2 BIT(2) 159 + #define DS_OUTPUT_AUDIO_FLAGS_OUTPUT_PATH_SEL GENMASK(5, 4) 160 + #define DS_OUTPUT_AUDIO_FLAGS2_SP_PREAMP_GAIN GENMASK(2, 0) 161 + #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4) 162 + #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1) 144 163 145 164 /* DualSense hardware limits */ 146 165 #define DS_ACC_RES_PER_G 8192 147 - #define DS_ACC_RANGE (4*DS_ACC_RES_PER_G) 166 + #define DS_ACC_RANGE (4 * DS_ACC_RES_PER_G) 148 167 #define DS_GYRO_RES_PER_DEG_S 1024 149 - #define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S) 168 + #define DS_GYRO_RANGE (2048 * DS_GYRO_RES_PER_DEG_S) 150 169 #define DS_TOUCHPAD_WIDTH 1920 151 170 #define DS_TOUCHPAD_HEIGHT 1080 152 171 ··· 176 153 struct input_dev *gamepad; 177 154 struct input_dev *sensors; 178 155 struct input_dev *touchpad; 156 + struct input_dev *jack; 179 157 180 158 /* Update version is used as a feature/capability version. */ 181 - uint16_t update_version; 159 + u16 update_version; 182 160 183 161 /* Calibration data for accelerometer and gyroscope. */ 184 162 struct ps_calibration_data accel_calib_data[3]; ··· 187 163 188 164 /* Timestamp for sensor data */ 189 165 bool sensor_timestamp_initialized; 190 - uint32_t prev_sensor_timestamp; 191 - uint32_t sensor_timestamp_us; 166 + u32 prev_sensor_timestamp; 167 + u32 sensor_timestamp_us; 192 168 193 169 /* Compatible rumble state */ 194 170 bool use_vibration_v2; 195 171 bool update_rumble; 196 - uint8_t motor_left; 197 - uint8_t motor_right; 172 + u8 motor_left; 173 + u8 motor_right; 198 174 199 175 /* RGB lightbar */ 200 176 struct led_classdev_mc lightbar; 201 177 bool update_lightbar; 202 - uint8_t lightbar_red; 203 - uint8_t lightbar_green; 204 - uint8_t lightbar_blue; 178 + u8 lightbar_red; 179 + u8 lightbar_green; 180 + u8 lightbar_blue; 181 + 182 + /* Audio Jack plugged state */ 183 + u8 plugged_state; 184 + u8 prev_plugged_state; 185 + bool prev_plugged_state_valid; 205 186 206 187 /* Microphone */ 207 188 bool update_mic_mute; ··· 215 186 216 187 /* Player leds */ 217 188 bool update_player_leds; 218 - uint8_t player_leds_state; 189 + u8 player_leds_state; 219 190 struct led_classdev player_leds[5]; 220 191 221 192 struct work_struct output_worker; 222 193 bool output_worker_initialized; 223 194 void *output_report_dmabuf; 224 - uint8_t output_seq; /* Sequence number for output report. */ 195 + u8 output_seq; /* Sequence number for output report. */ 225 196 }; 226 197 227 198 struct dualsense_touch_point { 228 - uint8_t contact; 229 - uint8_t x_lo; 230 - uint8_t x_hi:4, y_lo:4; 231 - uint8_t y_hi; 199 + u8 contact; 200 + u8 x_lo; 201 + u8 x_hi:4, y_lo:4; 202 + u8 y_hi; 232 203 } __packed; 233 204 static_assert(sizeof(struct dualsense_touch_point) == 4); 234 205 235 206 /* Main DualSense input report excluding any BT/USB specific headers. */ 236 207 struct dualsense_input_report { 237 - uint8_t x, y; 238 - uint8_t rx, ry; 239 - uint8_t z, rz; 240 - uint8_t seq_number; 241 - uint8_t buttons[4]; 242 - uint8_t reserved[4]; 208 + u8 x, y; 209 + u8 rx, ry; 210 + u8 z, rz; 211 + u8 seq_number; 212 + u8 buttons[4]; 213 + u8 reserved[4]; 243 214 244 215 /* Motion sensors */ 245 216 __le16 gyro[3]; /* x, y, z */ 246 217 __le16 accel[3]; /* x, y, z */ 247 218 __le32 sensor_timestamp; 248 - uint8_t reserved2; 219 + u8 reserved2; 249 220 250 221 /* Touchpad */ 251 222 struct dualsense_touch_point points[2]; 252 223 253 - uint8_t reserved3[12]; 254 - uint8_t status; 255 - uint8_t reserved4[10]; 224 + u8 reserved3[12]; 225 + u8 status[3]; 226 + u8 reserved4[8]; 256 227 } __packed; 257 228 /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */ 258 229 static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1); 259 230 260 231 /* Common data between DualSense BT/USB main output report. */ 261 232 struct dualsense_output_report_common { 262 - uint8_t valid_flag0; 263 - uint8_t valid_flag1; 233 + u8 valid_flag0; 234 + u8 valid_flag1; 264 235 265 236 /* For DualShock 4 compatibility mode. */ 266 - uint8_t motor_right; 267 - uint8_t motor_left; 237 + u8 motor_right; 238 + u8 motor_left; 268 239 269 240 /* Audio controls */ 270 - uint8_t reserved[4]; 271 - uint8_t mute_button_led; 241 + u8 headphone_volume; /* 0x0 - 0x7f */ 242 + u8 speaker_volume; /* 0x0 - 0xff */ 243 + u8 mic_volume; /* 0x0 - 0x40 */ 244 + u8 audio_control; 245 + u8 mute_button_led; 272 246 273 - uint8_t power_save_control; 274 - uint8_t reserved2[28]; 247 + u8 power_save_control; 248 + u8 reserved2[27]; 249 + u8 audio_control2; 275 250 276 251 /* LEDs and lightbar */ 277 - uint8_t valid_flag2; 278 - uint8_t reserved3[2]; 279 - uint8_t lightbar_setup; 280 - uint8_t led_brightness; 281 - uint8_t player_leds; 282 - uint8_t lightbar_red; 283 - uint8_t lightbar_green; 284 - uint8_t lightbar_blue; 252 + u8 valid_flag2; 253 + u8 reserved3[2]; 254 + u8 lightbar_setup; 255 + u8 led_brightness; 256 + u8 player_leds; 257 + u8 lightbar_red; 258 + u8 lightbar_green; 259 + u8 lightbar_blue; 285 260 } __packed; 286 261 static_assert(sizeof(struct dualsense_output_report_common) == 47); 287 262 288 263 struct dualsense_output_report_bt { 289 - uint8_t report_id; /* 0x31 */ 290 - uint8_t seq_tag; 291 - uint8_t tag; 264 + u8 report_id; /* 0x31 */ 265 + u8 seq_tag; 266 + u8 tag; 292 267 struct dualsense_output_report_common common; 293 - uint8_t reserved[24]; 268 + u8 reserved[24]; 294 269 __le32 crc32; 295 270 } __packed; 296 271 static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE); 297 272 298 273 struct dualsense_output_report_usb { 299 - uint8_t report_id; /* 0x02 */ 274 + u8 report_id; /* 0x02 */ 300 275 struct dualsense_output_report_common common; 301 - uint8_t reserved[15]; 276 + u8 reserved[15]; 302 277 } __packed; 303 278 static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE); 304 279 ··· 312 279 * This structure hide the differences between the two to simplify sending output reports. 313 280 */ 314 281 struct dualsense_output_report { 315 - uint8_t *data; /* Start of data */ 316 - uint8_t len; /* Size of output report */ 282 + u8 *data; /* Start of data */ 283 + u8 len; /* Size of output report */ 317 284 318 285 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */ 319 286 struct dualsense_output_report_bt *bt; ··· 348 315 * Contact IDs, with highest bit set are 'inactive' 349 316 * and any associated data is then invalid. 350 317 */ 351 - #define DS4_TOUCH_POINT_INACTIVE BIT(7) 318 + #define DS4_TOUCH_POINT_INACTIVE BIT(7) 319 + #define DS4_TOUCH_POINT_X(hi, lo) DS_TOUCH_POINT_X(hi, lo) 320 + #define DS4_TOUCH_POINT_Y(hi, lo) DS_TOUCH_POINT_Y(hi, lo) 352 321 353 322 /* Status field of DualShock4 input report. */ 354 323 #define DS4_STATUS0_BATTERY_CAPACITY GENMASK(3, 0) ··· 358 323 /* Battery status within batery_status field. */ 359 324 #define DS4_BATTERY_STATUS_FULL 11 360 325 /* Status1 bit2 contains dongle connection state: 361 - * 0 = connectd 326 + * 0 = connected 362 327 * 1 = disconnected 363 328 */ 364 329 #define DS4_STATUS1_DONGLE_STATE BIT(2) ··· 384 349 385 350 /* DualShock4 hardware limits */ 386 351 #define DS4_ACC_RES_PER_G 8192 387 - #define DS4_ACC_RANGE (4*DS_ACC_RES_PER_G) 352 + #define DS4_ACC_RANGE (4 * DS_ACC_RES_PER_G) 388 353 #define DS4_GYRO_RES_PER_DEG_S 1024 389 - #define DS4_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S) 354 + #define DS4_GYRO_RANGE (2048 * DS_GYRO_RES_PER_DEG_S) 390 355 #define DS4_LIGHTBAR_MAX_BLINK 255 /* 255 centiseconds */ 391 356 #define DS4_TOUCHPAD_WIDTH 1920 392 357 #define DS4_TOUCHPAD_HEIGHT 942 ··· 415 380 416 381 /* Timestamp for sensor data */ 417 382 bool sensor_timestamp_initialized; 418 - uint32_t prev_sensor_timestamp; 419 - uint32_t sensor_timestamp_us; 383 + u32 prev_sensor_timestamp; 384 + u32 sensor_timestamp_us; 420 385 421 386 /* Bluetooth poll interval */ 422 387 bool update_bt_poll_interval; 423 - uint8_t bt_poll_interval; 388 + u8 bt_poll_interval; 424 389 425 390 bool update_rumble; 426 - uint8_t motor_left; 427 - uint8_t motor_right; 391 + u8 motor_left; 392 + u8 motor_right; 428 393 429 394 /* Lightbar leds */ 430 395 bool update_lightbar; 431 396 bool update_lightbar_blink; 432 397 bool lightbar_enabled; /* For use by global LED control. */ 433 - uint8_t lightbar_red; 434 - uint8_t lightbar_green; 435 - uint8_t lightbar_blue; 436 - uint8_t lightbar_blink_on; /* In increments of 10ms. */ 437 - uint8_t lightbar_blink_off; /* In increments of 10ms. */ 398 + u8 lightbar_red; 399 + u8 lightbar_green; 400 + u8 lightbar_blue; 401 + u8 lightbar_blink_on; /* In increments of 10ms. */ 402 + u8 lightbar_blink_off; /* In increments of 10ms. */ 438 403 struct led_classdev lightbar_leds[4]; 439 404 440 405 struct work_struct output_worker; ··· 443 408 }; 444 409 445 410 struct dualshock4_touch_point { 446 - uint8_t contact; 447 - uint8_t x_lo; 448 - uint8_t x_hi:4, y_lo:4; 449 - uint8_t y_hi; 411 + u8 contact; 412 + u8 x_lo; 413 + u8 x_hi:4, y_lo:4; 414 + u8 y_hi; 450 415 } __packed; 451 416 static_assert(sizeof(struct dualshock4_touch_point) == 4); 452 417 453 418 struct dualshock4_touch_report { 454 - uint8_t timestamp; 419 + u8 timestamp; 455 420 struct dualshock4_touch_point points[2]; 456 421 } __packed; 457 422 static_assert(sizeof(struct dualshock4_touch_report) == 9); 458 423 459 424 /* Main DualShock4 input report excluding any BT/USB specific headers. */ 460 425 struct dualshock4_input_report_common { 461 - uint8_t x, y; 462 - uint8_t rx, ry; 463 - uint8_t buttons[3]; 464 - uint8_t z, rz; 426 + u8 x, y; 427 + u8 rx, ry; 428 + u8 buttons[3]; 429 + u8 z, rz; 465 430 466 431 /* Motion sensors */ 467 432 __le16 sensor_timestamp; 468 - uint8_t sensor_temperature; 433 + u8 sensor_temperature; 469 434 __le16 gyro[3]; /* x, y, z */ 470 435 __le16 accel[3]; /* x, y, z */ 471 - uint8_t reserved2[5]; 436 + u8 reserved2[5]; 472 437 473 - uint8_t status[2]; 474 - uint8_t reserved3; 438 + u8 status[2]; 439 + u8 reserved3; 475 440 } __packed; 476 441 static_assert(sizeof(struct dualshock4_input_report_common) == 32); 477 442 478 443 struct dualshock4_input_report_usb { 479 - uint8_t report_id; /* 0x01 */ 444 + u8 report_id; /* 0x01 */ 480 445 struct dualshock4_input_report_common common; 481 - uint8_t num_touch_reports; 446 + u8 num_touch_reports; 482 447 struct dualshock4_touch_report touch_reports[3]; 483 - uint8_t reserved[3]; 448 + u8 reserved[3]; 484 449 } __packed; 485 450 static_assert(sizeof(struct dualshock4_input_report_usb) == DS4_INPUT_REPORT_USB_SIZE); 486 451 487 452 struct dualshock4_input_report_bt { 488 - uint8_t report_id; /* 0x11 */ 489 - uint8_t reserved[2]; 453 + u8 report_id; /* 0x11 */ 454 + u8 reserved[2]; 490 455 struct dualshock4_input_report_common common; 491 - uint8_t num_touch_reports; 456 + u8 num_touch_reports; 492 457 struct dualshock4_touch_report touch_reports[4]; /* BT has 4 compared to 3 for USB */ 493 - uint8_t reserved2[2]; 458 + u8 reserved2[2]; 494 459 __le32 crc32; 495 460 } __packed; 496 461 static_assert(sizeof(struct dualshock4_input_report_bt) == DS4_INPUT_REPORT_BT_SIZE); 497 462 498 463 /* Common data between Bluetooth and USB DualShock4 output reports. */ 499 464 struct dualshock4_output_report_common { 500 - uint8_t valid_flag0; 501 - uint8_t valid_flag1; 465 + u8 valid_flag0; 466 + u8 valid_flag1; 502 467 503 - uint8_t reserved; 468 + u8 reserved; 504 469 505 - uint8_t motor_right; 506 - uint8_t motor_left; 470 + u8 motor_right; 471 + u8 motor_left; 507 472 508 - uint8_t lightbar_red; 509 - uint8_t lightbar_green; 510 - uint8_t lightbar_blue; 511 - uint8_t lightbar_blink_on; 512 - uint8_t lightbar_blink_off; 473 + u8 lightbar_red; 474 + u8 lightbar_green; 475 + u8 lightbar_blue; 476 + u8 lightbar_blink_on; 477 + u8 lightbar_blink_off; 513 478 } __packed; 514 479 515 480 struct dualshock4_output_report_usb { 516 - uint8_t report_id; /* 0x5 */ 481 + u8 report_id; /* 0x5 */ 517 482 struct dualshock4_output_report_common common; 518 - uint8_t reserved[21]; 483 + u8 reserved[21]; 519 484 } __packed; 520 485 static_assert(sizeof(struct dualshock4_output_report_usb) == DS4_OUTPUT_REPORT_USB_SIZE); 521 486 522 487 struct dualshock4_output_report_bt { 523 - uint8_t report_id; /* 0x11 */ 524 - uint8_t hw_control; 525 - uint8_t audio_control; 488 + u8 report_id; /* 0x11 */ 489 + u8 hw_control; 490 + u8 audio_control; 526 491 struct dualshock4_output_report_common common; 527 - uint8_t reserved[61]; 492 + u8 reserved[61]; 528 493 __le32 crc32; 529 494 } __packed; 530 495 static_assert(sizeof(struct dualshock4_output_report_bt) == DS4_OUTPUT_REPORT_BT_SIZE); ··· 535 500 * This structure hide the differences between the two to simplify sending output reports. 536 501 */ 537 502 struct dualshock4_output_report { 538 - uint8_t *data; /* Start of data */ 539 - uint8_t len; /* Size of output report */ 503 + u8 *data; /* Start of data */ 504 + u8 len; /* Size of output report */ 540 505 541 506 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */ 542 507 struct dualshock4_output_report_bt *bt; ··· 575 540 static int dualshock4_get_calibration_data(struct dualshock4 *ds4); 576 541 static inline void dualsense_schedule_work(struct dualsense *ds); 577 542 static inline void dualshock4_schedule_work(struct dualshock4 *ds4); 578 - static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue); 543 + static void dualsense_set_lightbar(struct dualsense *ds, u8 red, u8 green, u8 blue); 579 544 static void dualshock4_set_default_lightbar_colors(struct dualshock4 *ds4); 580 545 581 546 /* ··· 587 552 { 588 553 struct ps_device *entry; 589 554 590 - mutex_lock(&ps_devices_lock); 555 + guard(mutex)(&ps_devices_lock); 556 + 591 557 list_for_each_entry(entry, &ps_devices_list, list) { 592 558 if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) { 593 559 hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n", 594 - dev->mac_address); 595 - mutex_unlock(&ps_devices_lock); 560 + dev->mac_address); 596 561 return -EEXIST; 597 562 } 598 563 } 599 564 600 565 list_add_tail(&dev->list, &ps_devices_list); 601 - mutex_unlock(&ps_devices_lock); 602 566 return 0; 603 567 } 604 568 605 569 static int ps_devices_list_remove(struct ps_device *dev) 606 570 { 607 - mutex_lock(&ps_devices_lock); 571 + guard(mutex)(&ps_devices_lock); 572 + 608 573 list_del(&dev->list); 609 - mutex_unlock(&ps_devices_lock); 610 574 return 0; 611 575 } 612 576 ··· 627 593 dev->player_id = U32_MAX; 628 594 } 629 595 630 - static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix) 596 + static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, 597 + const char *name_suffix) 631 598 { 632 599 struct input_dev *input_dev; 633 600 ··· 643 608 input_dev->uniq = hdev->uniq; 644 609 645 610 if (name_suffix) { 646 - input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name, 647 - name_suffix); 611 + input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", 612 + hdev->name, name_suffix); 648 613 if (!input_dev->name) 649 614 return ERR_PTR(-ENOMEM); 650 615 } else { ··· 664 629 }; 665 630 666 631 static int ps_battery_get_property(struct power_supply *psy, 667 - enum power_supply_property psp, 668 - union power_supply_propval *val) 632 + enum power_supply_property psp, 633 + union power_supply_propval *val) 669 634 { 670 635 struct ps_device *dev = power_supply_get_drvdata(psy); 671 - uint8_t battery_capacity; 636 + u8 battery_capacity; 672 637 int battery_status; 673 - unsigned long flags; 674 638 int ret = 0; 675 639 676 - spin_lock_irqsave(&dev->lock, flags); 677 - battery_capacity = dev->battery_capacity; 678 - battery_status = dev->battery_status; 679 - spin_unlock_irqrestore(&dev->lock, flags); 640 + scoped_guard(spinlock_irqsave, &dev->lock) { 641 + battery_capacity = dev->battery_capacity; 642 + battery_status = dev->battery_status; 643 + } 680 644 681 645 switch (psp) { 682 646 case POWER_SUPPLY_PROP_STATUS: ··· 709 675 dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props); 710 676 dev->battery_desc.get_property = ps_battery_get_property; 711 677 dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL, 712 - "ps-controller-battery-%pMR", dev->mac_address); 678 + "ps-controller-battery-%pMR", dev->mac_address); 713 679 if (!dev->battery_desc.name) 714 680 return -ENOMEM; 715 681 ··· 731 697 } 732 698 733 699 /* Compute crc32 of HID data and compare against expected CRC. */ 734 - static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc) 700 + static bool ps_check_crc32(u8 seed, u8 *data, size_t len, u32 report_crc) 735 701 { 736 - uint32_t crc; 702 + u32 crc; 737 703 738 704 crc = crc32_le(0xFFFFFFFF, &seed, 1); 739 705 crc = ~crc32_le(crc, data, len); ··· 741 707 return crc == report_crc; 742 708 } 743 709 744 - static struct input_dev *ps_gamepad_create(struct hid_device *hdev, 745 - int (*play_effect)(struct input_dev *, void *, struct ff_effect *)) 710 + static struct input_dev * 711 + ps_gamepad_create(struct hid_device *hdev, 712 + int (*play_effect)(struct input_dev *, void *, struct ff_effect *)) 746 713 { 747 714 struct input_dev *gamepad; 748 715 unsigned int i; ··· 780 745 return gamepad; 781 746 } 782 747 783 - static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size, 784 - bool check_crc) 748 + static int ps_get_report(struct hid_device *hdev, u8 report_id, u8 *buf, 749 + size_t size, bool check_crc) 785 750 { 786 751 int ret; 787 752 ··· 804 769 805 770 if (hdev->bus == BUS_BLUETOOTH && check_crc) { 806 771 /* Last 4 bytes contains crc32. */ 807 - uint8_t crc_offset = size - 4; 808 - uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]); 772 + u8 crc_offset = size - 4; 773 + u32 report_crc = get_unaligned_le32(&buf[crc_offset]); 809 774 810 775 if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) { 811 776 hid_err(hdev, "CRC check failed for reportID=%d\n", report_id); ··· 817 782 } 818 783 819 784 static int ps_led_register(struct ps_device *ps_dev, struct led_classdev *led, 820 - const struct ps_led_info *led_info) 785 + const struct ps_led_info *led_info) 821 786 { 822 787 int ret; 823 788 824 789 if (led_info->name) { 825 - led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, 826 - "%s:%s:%s", ps_dev->input_dev_name, led_info->color, led_info->name); 790 + led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, "%s:%s:%s", 791 + ps_dev->input_dev_name, led_info->color, 792 + led_info->name); 827 793 } else { 828 - /* Backwards compatible mode for hid-sony, but not compliant with LED class spec. */ 829 - led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, 830 - "%s:%s", ps_dev->input_dev_name, led_info->color); 794 + /* Backwards compatible mode for hid-sony, but not compliant 795 + * with LED class spec. 796 + */ 797 + led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, "%s:%s", 798 + ps_dev->input_dev_name, led_info->color); 831 799 } 832 800 833 801 if (!led->name) ··· 854 816 855 817 /* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ 856 818 static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev, 857 - int (*brightness_set)(struct led_classdev *, enum led_brightness)) 819 + int (*brightness_set)(struct led_classdev *, enum led_brightness)) 858 820 { 859 821 struct hid_device *hdev = ps_dev->hdev; 860 822 struct mc_subled *mc_led_info; ··· 875 837 876 838 led_cdev = &lightbar_mc_dev->led_cdev; 877 839 led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s:rgb:indicator", 878 - ps_dev->input_dev_name); 840 + ps_dev->input_dev_name); 879 841 if (!led_cdev->name) 880 842 return -ENOMEM; 881 843 led_cdev->brightness = 255; ··· 891 853 return 0; 892 854 } 893 855 894 - static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res, 895 - int gyro_range, int gyro_res) 856 + static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, 857 + int accel_res, int gyro_range, int gyro_res) 896 858 { 897 859 struct input_dev *sensors; 898 860 int ret; ··· 928 890 return sensors; 929 891 } 930 892 931 - static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height, 932 - unsigned int num_contacts) 893 + static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, 894 + int height, unsigned int num_contacts) 933 895 { 934 896 struct input_dev *touchpad; 935 897 int ret; ··· 956 918 return touchpad; 957 919 } 958 920 921 + static struct input_dev *ps_headset_jack_create(struct hid_device *hdev) 922 + { 923 + struct input_dev *jack; 924 + int ret; 925 + 926 + jack = ps_allocate_input_dev(hdev, "Headset Jack"); 927 + if (IS_ERR(jack)) 928 + return ERR_CAST(jack); 929 + 930 + input_set_capability(jack, EV_SW, SW_HEADPHONE_INSERT); 931 + input_set_capability(jack, EV_SW, SW_MICROPHONE_INSERT); 932 + 933 + ret = input_register_device(jack); 934 + if (ret) 935 + return ERR_PTR(ret); 936 + 937 + return jack; 938 + } 939 + 959 940 static ssize_t firmware_version_show(struct device *dev, 960 - struct device_attribute 961 - *attr, char *buf) 941 + struct device_attribute *attr, char *buf) 962 942 { 963 943 struct hid_device *hdev = to_hid_device(dev); 964 944 struct ps_device *ps_dev = hid_get_drvdata(hdev); ··· 987 931 static DEVICE_ATTR_RO(firmware_version); 988 932 989 933 static ssize_t hardware_version_show(struct device *dev, 990 - struct device_attribute 991 - *attr, char *buf) 934 + struct device_attribute *attr, char *buf) 992 935 { 993 936 struct hid_device *hdev = to_hid_device(dev); 994 937 struct ps_device *ps_dev = hid_get_drvdata(hdev); ··· 1018 963 int range_2g; 1019 964 int ret = 0; 1020 965 int i; 1021 - uint8_t *buf; 966 + u8 *buf; 1022 967 1023 968 buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); 1024 969 if (!buf) 1025 970 return -ENOMEM; 1026 971 1027 972 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf, 1028 - DS_FEATURE_REPORT_CALIBRATION_SIZE, true); 973 + DS_FEATURE_REPORT_CALIBRATION_SIZE, true); 1029 974 if (ret) { 1030 975 hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret); 1031 976 goto err_free; ··· 1056 1001 speed_2x = (gyro_speed_plus + gyro_speed_minus); 1057 1002 ds->gyro_calib_data[0].abs_code = ABS_RX; 1058 1003 ds->gyro_calib_data[0].bias = 0; 1059 - ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; 1004 + ds->gyro_calib_data[0].sens_numer = speed_2x * DS_GYRO_RES_PER_DEG_S; 1060 1005 ds->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) + 1061 1006 abs(gyro_pitch_minus - gyro_pitch_bias); 1062 1007 1063 1008 ds->gyro_calib_data[1].abs_code = ABS_RY; 1064 1009 ds->gyro_calib_data[1].bias = 0; 1065 - ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; 1010 + ds->gyro_calib_data[1].sens_numer = speed_2x * DS_GYRO_RES_PER_DEG_S; 1066 1011 ds->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) + 1067 1012 abs(gyro_yaw_minus - gyro_yaw_bias); 1068 1013 1069 1014 ds->gyro_calib_data[2].abs_code = ABS_RZ; 1070 1015 ds->gyro_calib_data[2].bias = 0; 1071 - ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; 1016 + ds->gyro_calib_data[2].sens_numer = speed_2x * DS_GYRO_RES_PER_DEG_S; 1072 1017 ds->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) + 1073 1018 abs(gyro_roll_minus - gyro_roll_bias); 1074 1019 ··· 1079 1024 */ 1080 1025 for (i = 0; i < ARRAY_SIZE(ds->gyro_calib_data); i++) { 1081 1026 if (ds->gyro_calib_data[i].sens_denom == 0) { 1082 - hid_warn(hdev, "Invalid gyro calibration data for axis (%d), disabling calibration.", 1083 - ds->gyro_calib_data[i].abs_code); 1027 + hid_warn(hdev, 1028 + "Invalid gyro calibration data for axis (%d), disabling calibration.", 1029 + ds->gyro_calib_data[i].abs_code); 1084 1030 ds->gyro_calib_data[i].bias = 0; 1085 1031 ds->gyro_calib_data[i].sens_numer = DS_GYRO_RANGE; 1086 1032 ds->gyro_calib_data[i].sens_denom = S16_MAX; ··· 1095 1039 range_2g = acc_x_plus - acc_x_minus; 1096 1040 ds->accel_calib_data[0].abs_code = ABS_X; 1097 1041 ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2; 1098 - ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G; 1042 + ds->accel_calib_data[0].sens_numer = 2 * DS_ACC_RES_PER_G; 1099 1043 ds->accel_calib_data[0].sens_denom = range_2g; 1100 1044 1101 1045 range_2g = acc_y_plus - acc_y_minus; 1102 1046 ds->accel_calib_data[1].abs_code = ABS_Y; 1103 1047 ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2; 1104 - ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G; 1048 + ds->accel_calib_data[1].sens_numer = 2 * DS_ACC_RES_PER_G; 1105 1049 ds->accel_calib_data[1].sens_denom = range_2g; 1106 1050 1107 1051 range_2g = acc_z_plus - acc_z_minus; 1108 1052 ds->accel_calib_data[2].abs_code = ABS_Z; 1109 1053 ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2; 1110 - ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G; 1054 + ds->accel_calib_data[2].sens_numer = 2 * DS_ACC_RES_PER_G; 1111 1055 ds->accel_calib_data[2].sens_denom = range_2g; 1112 1056 1113 1057 /* ··· 1117 1061 */ 1118 1062 for (i = 0; i < ARRAY_SIZE(ds->accel_calib_data); i++) { 1119 1063 if (ds->accel_calib_data[i].sens_denom == 0) { 1120 - hid_warn(hdev, "Invalid accelerometer calibration data for axis (%d), disabling calibration.", 1121 - ds->accel_calib_data[i].abs_code); 1064 + hid_warn(hdev, 1065 + "Invalid accelerometer calibration data for axis (%d), disabling calibration.", 1066 + ds->accel_calib_data[i].abs_code); 1122 1067 ds->accel_calib_data[i].bias = 0; 1123 1068 ds->accel_calib_data[i].sens_numer = DS_ACC_RANGE; 1124 1069 ds->accel_calib_data[i].sens_denom = S16_MAX; ··· 1131 1074 return ret; 1132 1075 } 1133 1076 1134 - 1135 1077 static int dualsense_get_firmware_info(struct dualsense *ds) 1136 1078 { 1137 - uint8_t *buf; 1079 + u8 *buf; 1138 1080 int ret; 1139 1081 1140 1082 buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL); ··· 1141 1085 return -ENOMEM; 1142 1086 1143 1087 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf, 1144 - DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, true); 1088 + DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, true); 1145 1089 if (ret) { 1146 1090 hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret); 1147 1091 goto err_free; ··· 1166 1110 1167 1111 static int dualsense_get_mac_address(struct dualsense *ds) 1168 1112 { 1169 - uint8_t *buf; 1113 + u8 *buf; 1170 1114 int ret = 0; 1171 1115 1172 1116 buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL); ··· 1174 1118 return -ENOMEM; 1175 1119 1176 1120 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf, 1177 - DS_FEATURE_REPORT_PAIRING_INFO_SIZE, true); 1121 + DS_FEATURE_REPORT_PAIRING_INFO_SIZE, true); 1178 1122 if (ret) { 1179 1123 hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret); 1180 1124 goto err_free; ··· 1188 1132 } 1189 1133 1190 1134 static int dualsense_lightbar_set_brightness(struct led_classdev *cdev, 1191 - enum led_brightness brightness) 1135 + enum led_brightness brightness) 1192 1136 { 1193 1137 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev); 1194 1138 struct dualsense *ds = container_of(mc_cdev, struct dualsense, lightbar); 1195 - uint8_t red, green, blue; 1139 + u8 red, green, blue; 1196 1140 1197 1141 led_mc_calc_color_components(mc_cdev, brightness); 1198 1142 red = mc_cdev->subled_info[0].brightness; ··· 1215 1159 { 1216 1160 struct hid_device *hdev = to_hid_device(led->dev->parent); 1217 1161 struct dualsense *ds = hid_get_drvdata(hdev); 1218 - unsigned long flags; 1219 1162 unsigned int led_index; 1220 1163 1221 - spin_lock_irqsave(&ds->base.lock, flags); 1164 + scoped_guard(spinlock_irqsave, &ds->base.lock) { 1165 + led_index = led - ds->player_leds; 1166 + if (value == LED_OFF) 1167 + ds->player_leds_state &= ~BIT(led_index); 1168 + else 1169 + ds->player_leds_state |= BIT(led_index); 1222 1170 1223 - led_index = led - ds->player_leds; 1224 - if (value == LED_OFF) 1225 - ds->player_leds_state &= ~BIT(led_index); 1226 - else 1227 - ds->player_leds_state |= BIT(led_index); 1228 - 1229 - ds->update_player_leds = true; 1230 - spin_unlock_irqrestore(&ds->base.lock, flags); 1171 + ds->update_player_leds = true; 1172 + } 1231 1173 1232 1174 dualsense_schedule_work(ds); 1233 1175 1234 1176 return 0; 1235 1177 } 1236 1178 1237 - static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp, 1238 - void *buf) 1179 + static void dualsense_init_output_report(struct dualsense *ds, 1180 + struct dualsense_output_report *rp, void *buf) 1239 1181 { 1240 1182 struct hid_device *hdev = ds->base.hdev; 1241 1183 ··· 1248 1194 * Highest 4-bit is a sequence number, which needs to be increased 1249 1195 * every report. Lowest 4-bit is tag and can be zero for now. 1250 1196 */ 1251 - bt->seq_tag = (ds->output_seq << 4) | 0x0; 1197 + bt->seq_tag = FIELD_PREP(DS_OUTPUT_SEQ_NO, ds->output_seq) | 1198 + FIELD_PREP(DS_OUTPUT_SEQ_TAG, 0x0); 1252 1199 if (++ds->output_seq == 16) 1253 1200 ds->output_seq = 0; 1254 1201 ··· 1274 1219 1275 1220 static inline void dualsense_schedule_work(struct dualsense *ds) 1276 1221 { 1277 - unsigned long flags; 1278 - 1279 - spin_lock_irqsave(&ds->base.lock, flags); 1280 - if (ds->output_worker_initialized) 1281 - schedule_work(&ds->output_worker); 1282 - spin_unlock_irqrestore(&ds->base.lock, flags); 1222 + /* Using scoped_guard() instead of guard() to make sparse happy */ 1223 + scoped_guard(spinlock_irqsave, &ds->base.lock) 1224 + if (ds->output_worker_initialized) 1225 + schedule_work(&ds->output_worker); 1283 1226 } 1284 1227 1285 1228 /* ··· 1285 1232 * for Bluetooth reports. 1286 1233 */ 1287 1234 static void dualsense_send_output_report(struct dualsense *ds, 1288 - struct dualsense_output_report *report) 1235 + struct dualsense_output_report *report) 1289 1236 { 1290 1237 struct hid_device *hdev = ds->base.hdev; 1291 1238 1292 1239 /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */ 1293 1240 if (report->bt) { 1294 - uint32_t crc; 1295 - uint8_t seed = PS_OUTPUT_CRC32_SEED; 1241 + u32 crc; 1242 + u8 seed = PS_OUTPUT_CRC32_SEED; 1296 1243 1297 1244 crc = crc32_le(0xFFFFFFFF, &seed, 1); 1298 1245 crc = ~crc32_le(crc, report->data, report->len - 4); ··· 1308 1255 struct dualsense *ds = container_of(work, struct dualsense, output_worker); 1309 1256 struct dualsense_output_report report; 1310 1257 struct dualsense_output_report_common *common; 1311 - unsigned long flags; 1312 1258 1313 1259 dualsense_init_output_report(ds, &report, ds->output_report_dmabuf); 1314 1260 common = report.common; 1315 1261 1316 - spin_lock_irqsave(&ds->base.lock, flags); 1317 - 1318 - if (ds->update_rumble) { 1319 - /* Select classic rumble style haptics and enable it. */ 1320 - common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT; 1321 - if (ds->use_vibration_v2) 1322 - common->valid_flag2 |= DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2; 1323 - else 1324 - common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION; 1325 - common->motor_left = ds->motor_left; 1326 - common->motor_right = ds->motor_right; 1327 - ds->update_rumble = false; 1328 - } 1329 - 1330 - if (ds->update_lightbar) { 1331 - common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE; 1332 - common->lightbar_red = ds->lightbar_red; 1333 - common->lightbar_green = ds->lightbar_green; 1334 - common->lightbar_blue = ds->lightbar_blue; 1335 - 1336 - ds->update_lightbar = false; 1337 - } 1338 - 1339 - if (ds->update_player_leds) { 1340 - common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE; 1341 - common->player_leds = ds->player_leds_state; 1342 - 1343 - ds->update_player_leds = false; 1344 - } 1345 - 1346 - if (ds->update_mic_mute) { 1347 - common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE; 1348 - common->mute_button_led = ds->mic_muted; 1349 - 1350 - if (ds->mic_muted) { 1351 - /* Disable microphone */ 1352 - common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE; 1353 - common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE; 1354 - } else { 1355 - /* Enable microphone */ 1356 - common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE; 1357 - common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE; 1262 + scoped_guard(spinlock_irqsave, &ds->base.lock) { 1263 + if (ds->update_rumble) { 1264 + /* Select classic rumble style haptics and enable it. */ 1265 + common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT; 1266 + if (ds->use_vibration_v2) 1267 + common->valid_flag2 |= DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2; 1268 + else 1269 + common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION; 1270 + common->motor_left = ds->motor_left; 1271 + common->motor_right = ds->motor_right; 1272 + ds->update_rumble = false; 1358 1273 } 1359 1274 1360 - ds->update_mic_mute = false; 1361 - } 1275 + if (ds->update_lightbar) { 1276 + common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE; 1277 + common->lightbar_red = ds->lightbar_red; 1278 + common->lightbar_green = ds->lightbar_green; 1279 + common->lightbar_blue = ds->lightbar_blue; 1362 1280 1363 - spin_unlock_irqrestore(&ds->base.lock, flags); 1281 + ds->update_lightbar = false; 1282 + } 1283 + 1284 + if (ds->update_player_leds) { 1285 + common->valid_flag1 |= 1286 + DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE; 1287 + common->player_leds = ds->player_leds_state; 1288 + 1289 + ds->update_player_leds = false; 1290 + } 1291 + 1292 + if (ds->plugged_state != ds->prev_plugged_state) { 1293 + u8 val = ds->plugged_state & DS_STATUS1_HP_DETECT; 1294 + 1295 + if (val != (ds->prev_plugged_state & DS_STATUS1_HP_DETECT)) { 1296 + common->valid_flag0 = DS_OUTPUT_VALID_FLAG0_AUDIO_CONTROL_ENABLE; 1297 + /* 1298 + * _--------> Output path setup in audio_flag0 1299 + * / _------> Headphone (HP) Left channel sink 1300 + * | / _----> Headphone (HP) Right channel sink 1301 + * | | / _--> Internal Speaker (SP) sink 1302 + * | | | / 1303 + * | | | | L/R - Left/Right channel source 1304 + * 0 L-R X X - Unrouted (muted) channel source 1305 + * 1 L-L X 1306 + * 2 L-L R 1307 + * 3 X-X R 1308 + */ 1309 + if (val) { 1310 + /* Mute SP and route L+R channels to HP */ 1311 + common->audio_control = 0; 1312 + } else { 1313 + /* Mute HP and route R channel to SP */ 1314 + common->audio_control = 1315 + FIELD_PREP(DS_OUTPUT_AUDIO_FLAGS_OUTPUT_PATH_SEL, 1316 + 0x3); 1317 + /* 1318 + * Set SP hardware volume to 100%. 1319 + * Note the accepted range seems to be [0x3d..0x64] 1320 + */ 1321 + common->valid_flag0 |= 1322 + DS_OUTPUT_VALID_FLAG0_SPEAKER_VOLUME_ENABLE; 1323 + common->speaker_volume = 0x64; 1324 + /* Set SP preamp gain to +6dB */ 1325 + common->valid_flag1 = 1326 + DS_OUTPUT_VALID_FLAG1_AUDIO_CONTROL2_ENABLE; 1327 + common->audio_control2 = 1328 + FIELD_PREP(DS_OUTPUT_AUDIO_FLAGS2_SP_PREAMP_GAIN, 1329 + 0x2); 1330 + } 1331 + 1332 + input_report_switch(ds->jack, SW_HEADPHONE_INSERT, val); 1333 + } 1334 + 1335 + val = ds->plugged_state & DS_STATUS1_MIC_DETECT; 1336 + if (val != (ds->prev_plugged_state & DS_STATUS1_MIC_DETECT)) 1337 + input_report_switch(ds->jack, SW_MICROPHONE_INSERT, val); 1338 + 1339 + input_sync(ds->jack); 1340 + ds->prev_plugged_state = ds->plugged_state; 1341 + } 1342 + 1343 + if (ds->update_mic_mute) { 1344 + common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE; 1345 + common->mute_button_led = ds->mic_muted; 1346 + 1347 + if (ds->mic_muted) { 1348 + /* Disable microphone */ 1349 + common->valid_flag1 |= 1350 + DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE; 1351 + common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE; 1352 + } else { 1353 + /* Enable microphone */ 1354 + common->valid_flag1 |= 1355 + DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE; 1356 + common->power_save_control &= 1357 + ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE; 1358 + } 1359 + 1360 + ds->update_mic_mute = false; 1361 + } 1362 + } 1364 1363 1365 1364 dualsense_send_output_report(ds, &report); 1366 1365 } 1367 1366 1368 1367 static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report, 1369 - u8 *data, int size) 1368 + u8 *data, int size) 1370 1369 { 1371 1370 struct hid_device *hdev = ps_dev->hdev; 1372 1371 struct dualsense *ds = container_of(ps_dev, struct dualsense, base); 1373 1372 struct dualsense_input_report *ds_report; 1374 - uint8_t battery_data, battery_capacity, charging_status, value; 1373 + u8 battery_data, battery_capacity, charging_status, value; 1375 1374 int battery_status; 1376 - uint32_t sensor_timestamp; 1375 + u32 sensor_timestamp; 1377 1376 bool btn_mic_state; 1378 - unsigned long flags; 1379 1377 int i; 1380 1378 1381 1379 /* ··· 1435 1331 * the full report using reportID 49. 1436 1332 */ 1437 1333 if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB && 1438 - size == DS_INPUT_REPORT_USB_SIZE) { 1334 + size == DS_INPUT_REPORT_USB_SIZE) { 1439 1335 ds_report = (struct dualsense_input_report *)&data[1]; 1440 1336 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT && 1441 - size == DS_INPUT_REPORT_BT_SIZE) { 1337 + size == DS_INPUT_REPORT_BT_SIZE) { 1442 1338 /* Last 4 bytes of input report contain crc32 */ 1443 - uint32_t report_crc = get_unaligned_le32(&data[size - 4]); 1339 + u32 report_crc = get_unaligned_le32(&data[size - 4]); 1444 1340 1445 1341 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) { 1446 1342 hid_err(hdev, "DualSense input CRC's check failed\n"); ··· 1488 1384 */ 1489 1385 btn_mic_state = !!(ds_report->buttons[2] & DS_BUTTONS2_MIC_MUTE); 1490 1386 if (btn_mic_state && !ds->last_btn_mic_state) { 1491 - spin_lock_irqsave(&ps_dev->lock, flags); 1492 - ds->update_mic_mute = true; 1493 - ds->mic_muted = !ds->mic_muted; /* toggle */ 1494 - spin_unlock_irqrestore(&ps_dev->lock, flags); 1387 + scoped_guard(spinlock_irqsave, &ps_dev->lock) { 1388 + ds->update_mic_mute = true; 1389 + ds->mic_muted = !ds->mic_muted; /* toggle */ 1390 + } 1495 1391 1496 1392 /* Schedule updating of microphone state at hardware level. */ 1497 1393 dualsense_schedule_work(ds); 1498 1394 } 1499 1395 ds->last_btn_mic_state = btn_mic_state; 1396 + 1397 + /* 1398 + * Parse HP/MIC plugged state data for USB use case, since Bluetooth 1399 + * audio is currently not supported. 1400 + */ 1401 + if (hdev->bus == BUS_USB) { 1402 + value = ds_report->status[1] & DS_STATUS1_JACK_DETECT; 1403 + 1404 + if (!ds->prev_plugged_state_valid) { 1405 + /* Initial handling of the plugged state report */ 1406 + scoped_guard(spinlock_irqsave, &ps_dev->lock) { 1407 + ds->plugged_state = (~value) & DS_STATUS1_JACK_DETECT; 1408 + ds->prev_plugged_state_valid = true; 1409 + } 1410 + } 1411 + 1412 + if (value != ds->plugged_state) { 1413 + scoped_guard(spinlock_irqsave, &ps_dev->lock) { 1414 + ds->prev_plugged_state = ds->plugged_state; 1415 + ds->plugged_state = value; 1416 + } 1417 + 1418 + /* Schedule audio routing towards active endpoint. */ 1419 + dualsense_schedule_work(ds); 1420 + } 1421 + } 1500 1422 1501 1423 /* Parse and calibrate gyroscope data. */ 1502 1424 for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) { ··· 1549 1419 ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3); 1550 1420 ds->sensor_timestamp_initialized = true; 1551 1421 } else { 1552 - uint32_t delta; 1422 + u32 delta; 1553 1423 1554 1424 if (ds->prev_sensor_timestamp > sensor_timestamp) 1555 1425 delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1); ··· 1569 1439 input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active); 1570 1440 1571 1441 if (active) { 1572 - int x = (point->x_hi << 8) | point->x_lo; 1573 - int y = (point->y_hi << 4) | point->y_lo; 1574 - 1575 - input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x); 1576 - input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y); 1442 + input_report_abs(ds->touchpad, ABS_MT_POSITION_X, 1443 + DS_TOUCH_POINT_X(point->x_hi, point->x_lo)); 1444 + input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, 1445 + DS_TOUCH_POINT_Y(point->y_hi, point->y_lo)); 1577 1446 } 1578 1447 } 1579 1448 input_mt_sync_frame(ds->touchpad); 1580 1449 input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD); 1581 1450 input_sync(ds->touchpad); 1582 1451 1583 - battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY; 1584 - charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT; 1452 + battery_data = FIELD_GET(DS_STATUS0_BATTERY_CAPACITY, ds_report->status[0]); 1453 + charging_status = FIELD_GET(DS_STATUS0_CHARGING, ds_report->status[0]); 1585 1454 1586 1455 switch (charging_status) { 1587 1456 case 0x0: ··· 1610 1481 battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 1611 1482 } 1612 1483 1613 - spin_lock_irqsave(&ps_dev->lock, flags); 1614 - ps_dev->battery_capacity = battery_capacity; 1615 - ps_dev->battery_status = battery_status; 1616 - spin_unlock_irqrestore(&ps_dev->lock, flags); 1484 + scoped_guard(spinlock_irqsave, &ps_dev->lock) { 1485 + ps_dev->battery_capacity = battery_capacity; 1486 + ps_dev->battery_status = battery_status; 1487 + } 1617 1488 1618 1489 return 0; 1619 1490 } ··· 1622 1493 { 1623 1494 struct hid_device *hdev = input_get_drvdata(dev); 1624 1495 struct dualsense *ds = hid_get_drvdata(hdev); 1625 - unsigned long flags; 1626 1496 1627 1497 if (effect->type != FF_RUMBLE) 1628 1498 return 0; 1629 1499 1630 - spin_lock_irqsave(&ds->base.lock, flags); 1631 - ds->update_rumble = true; 1632 - ds->motor_left = effect->u.rumble.strong_magnitude / 256; 1633 - ds->motor_right = effect->u.rumble.weak_magnitude / 256; 1634 - spin_unlock_irqrestore(&ds->base.lock, flags); 1500 + scoped_guard(spinlock_irqsave, &ds->base.lock) { 1501 + ds->update_rumble = true; 1502 + ds->motor_left = effect->u.rumble.strong_magnitude / 256; 1503 + ds->motor_right = effect->u.rumble.weak_magnitude / 256; 1504 + } 1635 1505 1636 1506 dualsense_schedule_work(ds); 1637 1507 return 0; ··· 1639 1511 static void dualsense_remove(struct ps_device *ps_dev) 1640 1512 { 1641 1513 struct dualsense *ds = container_of(ps_dev, struct dualsense, base); 1642 - unsigned long flags; 1643 1514 1644 - spin_lock_irqsave(&ds->base.lock, flags); 1645 - ds->output_worker_initialized = false; 1646 - spin_unlock_irqrestore(&ds->base.lock, flags); 1515 + scoped_guard(spinlock_irqsave, &ds->base.lock) 1516 + ds->output_worker_initialized = false; 1647 1517 1648 1518 cancel_work_sync(&ds->output_worker); 1649 1519 } ··· 1649 1523 static int dualsense_reset_leds(struct dualsense *ds) 1650 1524 { 1651 1525 struct dualsense_output_report report; 1652 - uint8_t *buf; 1526 + struct dualsense_output_report_bt *buf; 1653 1527 1654 - buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL); 1528 + buf = kzalloc(sizeof(*buf), GFP_KERNEL); 1655 1529 if (!buf) 1656 1530 return -ENOMEM; 1657 1531 ··· 1671 1545 return 0; 1672 1546 } 1673 1547 1674 - static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue) 1548 + static void dualsense_set_lightbar(struct dualsense *ds, u8 red, u8 green, u8 blue) 1675 1549 { 1676 - unsigned long flags; 1677 - 1678 - spin_lock_irqsave(&ds->base.lock, flags); 1679 - ds->update_lightbar = true; 1680 - ds->lightbar_red = red; 1681 - ds->lightbar_green = green; 1682 - ds->lightbar_blue = blue; 1683 - spin_unlock_irqrestore(&ds->base.lock, flags); 1550 + scoped_guard(spinlock_irqsave, &ds->base.lock) { 1551 + ds->update_lightbar = true; 1552 + ds->lightbar_red = red; 1553 + ds->lightbar_green = green; 1554 + ds->lightbar_blue = blue; 1555 + } 1684 1556 1685 1557 dualsense_schedule_work(ds); 1686 1558 } ··· 1699 1575 BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0) 1700 1576 }; 1701 1577 1702 - uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids); 1578 + u8 player_id = ds->base.player_id % ARRAY_SIZE(player_ids); 1703 1579 1704 1580 ds->update_player_leds = true; 1705 1581 ds->player_leds_state = player_ids[player_id]; ··· 1710 1586 { 1711 1587 struct dualsense *ds; 1712 1588 struct ps_device *ps_dev; 1713 - uint8_t max_output_report_size; 1589 + u8 max_output_report_size; 1714 1590 int i, ret; 1715 1591 1716 1592 static const struct ps_led_info player_leds_info[] = { ··· 1799 1675 ps_dev->input_dev_name = dev_name(&ds->gamepad->dev); 1800 1676 1801 1677 ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G, 1802 - DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S); 1678 + DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S); 1803 1679 if (IS_ERR(ds->sensors)) { 1804 1680 ret = PTR_ERR(ds->sensors); 1805 1681 goto err; ··· 1809 1685 if (IS_ERR(ds->touchpad)) { 1810 1686 ret = PTR_ERR(ds->touchpad); 1811 1687 goto err; 1688 + } 1689 + 1690 + /* Bluetooth audio is currently not supported. */ 1691 + if (hdev->bus == BUS_USB) { 1692 + ds->jack = ps_headset_jack_create(hdev); 1693 + if (IS_ERR(ds->jack)) { 1694 + ret = PTR_ERR(ds->jack); 1695 + goto err; 1696 + } 1812 1697 } 1813 1698 1814 1699 ret = ps_device_register_battery(ps_dev); ··· 1862 1729 * can change behavior. 1863 1730 */ 1864 1731 hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n", 1865 - ds->base.hw_version, ds->base.fw_version); 1732 + ds->base.hw_version, ds->base.fw_version); 1866 1733 1867 1734 return &ds->base; 1868 1735 ··· 1874 1741 static void dualshock4_dongle_calibration_work(struct work_struct *work) 1875 1742 { 1876 1743 struct dualshock4 *ds4 = container_of(work, struct dualshock4, dongle_hotplug_worker); 1877 - unsigned long flags; 1878 1744 enum dualshock4_dongle_state dongle_state; 1879 1745 int ret; 1880 1746 ··· 1885 1753 * DS4 hotplug is detect from sony_raw_event as any issues 1886 1754 * are likely resolved then (the dongle is quite stupid). 1887 1755 */ 1888 - hid_err(ds4->base.hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n"); 1756 + hid_err(ds4->base.hdev, 1757 + "DualShock 4 USB dongle: calibration failed, disabling device\n"); 1889 1758 dongle_state = DONGLE_DISABLED; 1890 1759 } else { 1891 1760 hid_info(ds4->base.hdev, "DualShock 4 USB dongle: calibration completed\n"); 1892 1761 dongle_state = DONGLE_CONNECTED; 1893 1762 } 1894 1763 1895 - spin_lock_irqsave(&ds4->base.lock, flags); 1896 - ds4->dongle_state = dongle_state; 1897 - spin_unlock_irqrestore(&ds4->base.lock, flags); 1764 + scoped_guard(spinlock_irqsave, &ds4->base.lock) 1765 + ds4->dongle_state = dongle_state; 1898 1766 } 1899 1767 1900 1768 static int dualshock4_get_calibration_data(struct dualshock4 *ds4) ··· 1911 1779 int range_2g; 1912 1780 int ret = 0; 1913 1781 int i; 1914 - uint8_t *buf; 1782 + u8 *buf; 1915 1783 1916 1784 if (ds4->base.hdev->bus == BUS_USB) { 1917 1785 int retries; ··· 1930 1798 */ 1931 1799 for (retries = 0; retries < 3; retries++) { 1932 1800 ret = ps_get_report(hdev, DS4_FEATURE_REPORT_CALIBRATION, buf, 1933 - DS4_FEATURE_REPORT_CALIBRATION_SIZE, true); 1801 + DS4_FEATURE_REPORT_CALIBRATION_SIZE, true); 1934 1802 if (ret) { 1935 1803 if (retries < 2) { 1936 - hid_warn(hdev, "Retrying DualShock 4 get calibration report (0x02) request\n"); 1804 + hid_warn(hdev, 1805 + "Retrying DualShock 4 get calibration report (0x02) request\n"); 1937 1806 continue; 1938 1807 } 1939 1808 1940 - hid_warn(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret); 1809 + hid_warn(hdev, 1810 + "Failed to retrieve DualShock4 calibration info: %d\n", 1811 + ret); 1941 1812 ret = -EILSEQ; 1942 1813 goto transfer_failed; 1943 1814 } else { ··· 1955 1820 } 1956 1821 1957 1822 ret = ps_get_report(hdev, DS4_FEATURE_REPORT_CALIBRATION_BT, buf, 1958 - DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE, true); 1823 + DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE, true); 1959 1824 1960 1825 if (ret) { 1961 1826 hid_warn(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret); ··· 2002 1867 speed_2x = (gyro_speed_plus + gyro_speed_minus); 2003 1868 ds4->gyro_calib_data[0].abs_code = ABS_RX; 2004 1869 ds4->gyro_calib_data[0].bias = 0; 2005 - ds4->gyro_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; 1870 + ds4->gyro_calib_data[0].sens_numer = speed_2x * DS4_GYRO_RES_PER_DEG_S; 2006 1871 ds4->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) + 2007 1872 abs(gyro_pitch_minus - gyro_pitch_bias); 2008 1873 2009 1874 ds4->gyro_calib_data[1].abs_code = ABS_RY; 2010 1875 ds4->gyro_calib_data[1].bias = 0; 2011 - ds4->gyro_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; 1876 + ds4->gyro_calib_data[1].sens_numer = speed_2x * DS4_GYRO_RES_PER_DEG_S; 2012 1877 ds4->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) + 2013 1878 abs(gyro_yaw_minus - gyro_yaw_bias); 2014 1879 2015 1880 ds4->gyro_calib_data[2].abs_code = ABS_RZ; 2016 1881 ds4->gyro_calib_data[2].bias = 0; 2017 - ds4->gyro_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; 1882 + ds4->gyro_calib_data[2].sens_numer = speed_2x * DS4_GYRO_RES_PER_DEG_S; 2018 1883 ds4->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) + 2019 1884 abs(gyro_roll_minus - gyro_roll_bias); 2020 1885 ··· 2025 1890 range_2g = acc_x_plus - acc_x_minus; 2026 1891 ds4->accel_calib_data[0].abs_code = ABS_X; 2027 1892 ds4->accel_calib_data[0].bias = acc_x_plus - range_2g / 2; 2028 - ds4->accel_calib_data[0].sens_numer = 2*DS4_ACC_RES_PER_G; 1893 + ds4->accel_calib_data[0].sens_numer = 2 * DS4_ACC_RES_PER_G; 2029 1894 ds4->accel_calib_data[0].sens_denom = range_2g; 2030 1895 2031 1896 range_2g = acc_y_plus - acc_y_minus; 2032 1897 ds4->accel_calib_data[1].abs_code = ABS_Y; 2033 1898 ds4->accel_calib_data[1].bias = acc_y_plus - range_2g / 2; 2034 - ds4->accel_calib_data[1].sens_numer = 2*DS4_ACC_RES_PER_G; 1899 + ds4->accel_calib_data[1].sens_numer = 2 * DS4_ACC_RES_PER_G; 2035 1900 ds4->accel_calib_data[1].sens_denom = range_2g; 2036 1901 2037 1902 range_2g = acc_z_plus - acc_z_minus; 2038 1903 ds4->accel_calib_data[2].abs_code = ABS_Z; 2039 1904 ds4->accel_calib_data[2].bias = acc_z_plus - range_2g / 2; 2040 - ds4->accel_calib_data[2].sens_numer = 2*DS4_ACC_RES_PER_G; 1905 + ds4->accel_calib_data[2].sens_numer = 2 * DS4_ACC_RES_PER_G; 2041 1906 ds4->accel_calib_data[2].sens_denom = range_2g; 2042 1907 2043 1908 transfer_failed: ··· 2049 1914 for (i = 0; i < ARRAY_SIZE(ds4->gyro_calib_data); i++) { 2050 1915 if (ds4->gyro_calib_data[i].sens_denom == 0) { 2051 1916 ds4->gyro_calib_data[i].abs_code = ABS_RX + i; 2052 - hid_warn(hdev, "Invalid gyro calibration data for axis (%d), disabling calibration.", 2053 - ds4->gyro_calib_data[i].abs_code); 1917 + hid_warn(hdev, 1918 + "Invalid gyro calibration data for axis (%d), disabling calibration.", 1919 + ds4->gyro_calib_data[i].abs_code); 2054 1920 ds4->gyro_calib_data[i].bias = 0; 2055 1921 ds4->gyro_calib_data[i].sens_numer = DS4_GYRO_RANGE; 2056 1922 ds4->gyro_calib_data[i].sens_denom = S16_MAX; ··· 2066 1930 for (i = 0; i < ARRAY_SIZE(ds4->accel_calib_data); i++) { 2067 1931 if (ds4->accel_calib_data[i].sens_denom == 0) { 2068 1932 ds4->accel_calib_data[i].abs_code = ABS_X + i; 2069 - hid_warn(hdev, "Invalid accelerometer calibration data for axis (%d), disabling calibration.", 2070 - ds4->accel_calib_data[i].abs_code); 1933 + hid_warn(hdev, 1934 + "Invalid accelerometer calibration data for axis (%d), disabling calibration.", 1935 + ds4->accel_calib_data[i].abs_code); 2071 1936 ds4->accel_calib_data[i].bias = 0; 2072 1937 ds4->accel_calib_data[i].sens_numer = DS4_ACC_RANGE; 2073 1938 ds4->accel_calib_data[i].sens_denom = S16_MAX; ··· 2080 1943 2081 1944 static int dualshock4_get_firmware_info(struct dualshock4 *ds4) 2082 1945 { 2083 - uint8_t *buf; 1946 + u8 *buf; 2084 1947 int ret; 2085 1948 2086 1949 buf = kzalloc(DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL); ··· 2091 1954 * lacks CRC support, so must be disabled in ps_get_report. 2092 1955 */ 2093 1956 ret = ps_get_report(ds4->base.hdev, DS4_FEATURE_REPORT_FIRMWARE_INFO, buf, 2094 - DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, false); 1957 + DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, false); 2095 1958 if (ret) { 2096 1959 hid_err(ds4->base.hdev, "Failed to retrieve DualShock4 firmware info: %d\n", ret); 2097 1960 goto err_free; ··· 2108 1971 static int dualshock4_get_mac_address(struct dualshock4 *ds4) 2109 1972 { 2110 1973 struct hid_device *hdev = ds4->base.hdev; 2111 - uint8_t *buf; 1974 + u8 *buf; 2112 1975 int ret = 0; 2113 1976 2114 1977 if (hdev->bus == BUS_USB) { ··· 2117 1980 return -ENOMEM; 2118 1981 2119 1982 ret = ps_get_report(hdev, DS4_FEATURE_REPORT_PAIRING_INFO, buf, 2120 - DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, false); 1983 + DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, false); 2121 1984 if (ret) { 2122 1985 hid_err(hdev, "Failed to retrieve DualShock4 pairing info: %d\n", ret); 2123 1986 goto err_free; ··· 2130 1993 return -EINVAL; 2131 1994 2132 1995 ret = sscanf(hdev->uniq, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 2133 - &ds4->base.mac_address[5], &ds4->base.mac_address[4], 2134 - &ds4->base.mac_address[3], &ds4->base.mac_address[2], 2135 - &ds4->base.mac_address[1], &ds4->base.mac_address[0]); 1996 + &ds4->base.mac_address[5], &ds4->base.mac_address[4], 1997 + &ds4->base.mac_address[3], &ds4->base.mac_address[2], 1998 + &ds4->base.mac_address[1], &ds4->base.mac_address[0]); 2136 1999 2137 2000 if (ret != sizeof(ds4->base.mac_address)) 2138 2001 return -EINVAL; ··· 2167 2030 } 2168 2031 2169 2032 static int dualshock4_led_set_blink(struct led_classdev *led, unsigned long *delay_on, 2170 - unsigned long *delay_off) 2033 + unsigned long *delay_off) 2171 2034 { 2172 2035 struct hid_device *hdev = to_hid_device(led->dev->parent); 2173 2036 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 2174 - unsigned long flags; 2175 2037 2176 - spin_lock_irqsave(&ds4->base.lock, flags); 2038 + scoped_guard(spinlock_irqsave, &ds4->base.lock) { 2039 + if (!*delay_on && !*delay_off) { 2040 + /* Default to 1 Hz (50 centiseconds on, 50 centiseconds off). */ 2041 + ds4->lightbar_blink_on = 50; 2042 + ds4->lightbar_blink_off = 50; 2043 + } else { 2044 + /* Blink delays in centiseconds. */ 2045 + ds4->lightbar_blink_on = min_t(unsigned long, *delay_on / 10, 2046 + DS4_LIGHTBAR_MAX_BLINK); 2047 + ds4->lightbar_blink_off = min_t(unsigned long, *delay_off / 10, 2048 + DS4_LIGHTBAR_MAX_BLINK); 2049 + } 2177 2050 2178 - if (!*delay_on && !*delay_off) { 2179 - /* Default to 1 Hz (50 centiseconds on, 50 centiseconds off). */ 2180 - ds4->lightbar_blink_on = 50; 2181 - ds4->lightbar_blink_off = 50; 2182 - } else { 2183 - /* Blink delays in centiseconds. */ 2184 - ds4->lightbar_blink_on = min_t(unsigned long, *delay_on/10, DS4_LIGHTBAR_MAX_BLINK); 2185 - ds4->lightbar_blink_off = min_t(unsigned long, *delay_off/10, DS4_LIGHTBAR_MAX_BLINK); 2051 + ds4->update_lightbar_blink = true; 2186 2052 } 2187 - 2188 - ds4->update_lightbar_blink = true; 2189 - 2190 - spin_unlock_irqrestore(&ds4->base.lock, flags); 2191 2053 2192 2054 dualshock4_schedule_work(ds4); 2193 2055 ··· 2201 2065 { 2202 2066 struct hid_device *hdev = to_hid_device(led->dev->parent); 2203 2067 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 2204 - unsigned long flags; 2205 2068 unsigned int led_index; 2206 2069 2207 - spin_lock_irqsave(&ds4->base.lock, flags); 2070 + scoped_guard(spinlock_irqsave, &ds4->base.lock) { 2071 + led_index = led - ds4->lightbar_leds; 2072 + switch (led_index) { 2073 + case 0: 2074 + ds4->lightbar_red = value; 2075 + break; 2076 + case 1: 2077 + ds4->lightbar_green = value; 2078 + break; 2079 + case 2: 2080 + ds4->lightbar_blue = value; 2081 + break; 2082 + case 3: 2083 + ds4->lightbar_enabled = !!value; 2208 2084 2209 - led_index = led - ds4->lightbar_leds; 2210 - switch (led_index) { 2211 - case 0: 2212 - ds4->lightbar_red = value; 2213 - break; 2214 - case 1: 2215 - ds4->lightbar_green = value; 2216 - break; 2217 - case 2: 2218 - ds4->lightbar_blue = value; 2219 - break; 2220 - case 3: 2221 - ds4->lightbar_enabled = !!value; 2222 - 2223 - /* brightness = 0 also cancels blinking in Linux. */ 2224 - if (!ds4->lightbar_enabled) { 2225 - ds4->lightbar_blink_off = 0; 2226 - ds4->lightbar_blink_on = 0; 2227 - ds4->update_lightbar_blink = true; 2085 + /* brightness = 0 also cancels blinking in Linux. */ 2086 + if (!ds4->lightbar_enabled) { 2087 + ds4->lightbar_blink_off = 0; 2088 + ds4->lightbar_blink_on = 0; 2089 + ds4->update_lightbar_blink = true; 2090 + } 2228 2091 } 2092 + 2093 + ds4->update_lightbar = true; 2229 2094 } 2230 - 2231 - ds4->update_lightbar = true; 2232 - 2233 - spin_unlock_irqrestore(&ds4->base.lock, flags); 2234 2095 2235 2096 dualshock4_schedule_work(ds4); 2236 2097 ··· 2235 2102 } 2236 2103 2237 2104 static void dualshock4_init_output_report(struct dualshock4 *ds4, 2238 - struct dualshock4_output_report *rp, void *buf) 2105 + struct dualshock4_output_report *rp, void *buf) 2239 2106 { 2240 2107 struct hid_device *hdev = ds4->base.hdev; 2241 2108 ··· 2269 2136 struct dualshock4 *ds4 = container_of(work, struct dualshock4, output_worker); 2270 2137 struct dualshock4_output_report report; 2271 2138 struct dualshock4_output_report_common *common; 2272 - unsigned long flags; 2273 2139 2274 2140 dualshock4_init_output_report(ds4, &report, ds4->output_report_dmabuf); 2275 2141 common = report.common; 2276 2142 2277 - spin_lock_irqsave(&ds4->base.lock, flags); 2278 - 2279 - /* 2280 - * Some 3rd party gamepads expect updates to rumble and lightbar 2281 - * together, and setting one may cancel the other. 2282 - * 2283 - * Let's maximise compatibility by always sending rumble and lightbar 2284 - * updates together, even when only one has been scheduled, resulting 2285 - * in: 2286 - * 2287 - * ds4->valid_flag0 >= 0x03 2288 - * 2289 - * Hopefully this will maximise compatibility with third-party pads. 2290 - * 2291 - * Any further update bits, such as 0x04 for lightbar blinking, will 2292 - * be or'd on top of this like before. 2293 - */ 2294 - if (ds4->update_rumble || ds4->update_lightbar) { 2295 - ds4->update_rumble = true; /* 0x01 */ 2296 - ds4->update_lightbar = true; /* 0x02 */ 2297 - } 2298 - 2299 - if (ds4->update_rumble) { 2300 - /* Select classic rumble style haptics and enable it. */ 2301 - common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_MOTOR; 2302 - common->motor_left = ds4->motor_left; 2303 - common->motor_right = ds4->motor_right; 2304 - ds4->update_rumble = false; 2305 - } 2306 - 2307 - if (ds4->update_lightbar) { 2308 - common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED; 2309 - /* Comptabile behavior with hid-sony, which used a dummy global LED to 2310 - * allow enabling/disabling the lightbar. The global LED maps to 2311 - * lightbar_enabled. 2143 + scoped_guard(spinlock_irqsave, &ds4->base.lock) { 2144 + /* 2145 + * Some 3rd party gamepads expect updates to rumble and lightbar 2146 + * together, and setting one may cancel the other. 2147 + * 2148 + * Let's maximise compatibility by always sending rumble and lightbar 2149 + * updates together, even when only one has been scheduled, resulting 2150 + * in: 2151 + * 2152 + * ds4->valid_flag0 >= 0x03 2153 + * 2154 + * Hopefully this will maximise compatibility with third-party pads. 2155 + * 2156 + * Any further update bits, such as 0x04 for lightbar blinking, will 2157 + * be or'd on top of this like before. 2312 2158 */ 2313 - common->lightbar_red = ds4->lightbar_enabled ? ds4->lightbar_red : 0; 2314 - common->lightbar_green = ds4->lightbar_enabled ? ds4->lightbar_green : 0; 2315 - common->lightbar_blue = ds4->lightbar_enabled ? ds4->lightbar_blue : 0; 2316 - ds4->update_lightbar = false; 2317 - } 2159 + if (ds4->update_rumble || ds4->update_lightbar) { 2160 + ds4->update_rumble = true; /* 0x01 */ 2161 + ds4->update_lightbar = true; /* 0x02 */ 2162 + } 2318 2163 2319 - if (ds4->update_lightbar_blink) { 2320 - common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED_BLINK; 2321 - common->lightbar_blink_on = ds4->lightbar_blink_on; 2322 - common->lightbar_blink_off = ds4->lightbar_blink_off; 2323 - ds4->update_lightbar_blink = false; 2324 - } 2164 + if (ds4->update_rumble) { 2165 + /* Select classic rumble style haptics and enable it. */ 2166 + common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_MOTOR; 2167 + common->motor_left = ds4->motor_left; 2168 + common->motor_right = ds4->motor_right; 2169 + ds4->update_rumble = false; 2170 + } 2325 2171 2326 - spin_unlock_irqrestore(&ds4->base.lock, flags); 2172 + if (ds4->update_lightbar) { 2173 + common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED; 2174 + /* Compatible behavior with hid-sony, which used a dummy global LED to 2175 + * allow enabling/disabling the lightbar. The global LED maps to 2176 + * lightbar_enabled. 2177 + */ 2178 + common->lightbar_red = ds4->lightbar_enabled ? ds4->lightbar_red : 0; 2179 + common->lightbar_green = ds4->lightbar_enabled ? ds4->lightbar_green : 0; 2180 + common->lightbar_blue = ds4->lightbar_enabled ? ds4->lightbar_blue : 0; 2181 + ds4->update_lightbar = false; 2182 + } 2183 + 2184 + if (ds4->update_lightbar_blink) { 2185 + common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED_BLINK; 2186 + common->lightbar_blink_on = ds4->lightbar_blink_on; 2187 + common->lightbar_blink_off = ds4->lightbar_blink_off; 2188 + ds4->update_lightbar_blink = false; 2189 + } 2190 + } 2327 2191 2328 2192 /* Bluetooth packets need additional flags as well as a CRC in the last 4 bytes. */ 2329 2193 if (report.bt) { 2330 - uint32_t crc; 2331 - uint8_t seed = PS_OUTPUT_CRC32_SEED; 2194 + u32 crc; 2195 + u8 seed = PS_OUTPUT_CRC32_SEED; 2332 2196 2333 2197 /* Hardware control flags need to set to let the device know 2334 2198 * there is HID data as well as CRC. ··· 2347 2217 } 2348 2218 2349 2219 static int dualshock4_parse_report(struct ps_device *ps_dev, struct hid_report *report, 2350 - u8 *data, int size) 2220 + u8 *data, int size) 2351 2221 { 2352 2222 struct hid_device *hdev = ps_dev->hdev; 2353 2223 struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base); 2354 2224 struct dualshock4_input_report_common *ds4_report; 2355 2225 struct dualshock4_touch_report *touch_reports; 2356 - uint8_t battery_capacity, num_touch_reports, value; 2226 + u8 battery_capacity, num_touch_reports, value; 2357 2227 int battery_status, i, j; 2358 - uint16_t sensor_timestamp; 2359 - unsigned long flags; 2228 + u16 sensor_timestamp; 2360 2229 bool is_minimal = false; 2361 2230 2362 2231 /* ··· 2364 2235 * the full report using reportID 17. 2365 2236 */ 2366 2237 if (hdev->bus == BUS_USB && report->id == DS4_INPUT_REPORT_USB && 2367 - size == DS4_INPUT_REPORT_USB_SIZE) { 2368 - struct dualshock4_input_report_usb *usb = (struct dualshock4_input_report_usb *)data; 2238 + size == DS4_INPUT_REPORT_USB_SIZE) { 2239 + struct dualshock4_input_report_usb *usb = 2240 + (struct dualshock4_input_report_usb *)data; 2369 2241 2370 2242 ds4_report = &usb->common; 2371 2243 num_touch_reports = usb->num_touch_reports; 2372 2244 touch_reports = usb->touch_reports; 2373 2245 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS4_INPUT_REPORT_BT && 2374 - size == DS4_INPUT_REPORT_BT_SIZE) { 2246 + size == DS4_INPUT_REPORT_BT_SIZE) { 2375 2247 struct dualshock4_input_report_bt *bt = (struct dualshock4_input_report_bt *)data; 2376 - uint32_t report_crc = get_unaligned_le32(&bt->crc32); 2248 + u32 report_crc = get_unaligned_le32(&bt->crc32); 2377 2249 2378 2250 /* Last 4 bytes of input report contains CRC. */ 2379 2251 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) { ··· 2455 2325 /* Convert timestamp (in 5.33us unit) to timestamp_us */ 2456 2326 sensor_timestamp = le16_to_cpu(ds4_report->sensor_timestamp); 2457 2327 if (!ds4->sensor_timestamp_initialized) { 2458 - ds4->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp*16, 3); 2328 + ds4->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp * 16, 3); 2459 2329 ds4->sensor_timestamp_initialized = true; 2460 2330 } else { 2461 - uint16_t delta; 2331 + u16 delta; 2462 2332 2463 2333 if (ds4->prev_sensor_timestamp > sensor_timestamp) 2464 2334 delta = (U16_MAX - ds4->prev_sensor_timestamp + sensor_timestamp + 1); 2465 2335 else 2466 2336 delta = sensor_timestamp - ds4->prev_sensor_timestamp; 2467 - ds4->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta*16, 3); 2337 + ds4->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta * 16, 3); 2468 2338 } 2469 2339 ds4->prev_sensor_timestamp = sensor_timestamp; 2470 2340 input_event(ds4->sensors, EV_MSC, MSC_TIMESTAMP, ds4->sensor_timestamp_us); ··· 2481 2351 input_mt_report_slot_state(ds4->touchpad, MT_TOOL_FINGER, active); 2482 2352 2483 2353 if (active) { 2484 - int x = (point->x_hi << 8) | point->x_lo; 2485 - int y = (point->y_hi << 4) | point->y_lo; 2486 - 2487 - input_report_abs(ds4->touchpad, ABS_MT_POSITION_X, x); 2488 - input_report_abs(ds4->touchpad, ABS_MT_POSITION_Y, y); 2354 + input_report_abs(ds4->touchpad, ABS_MT_POSITION_X, 2355 + DS4_TOUCH_POINT_X(point->x_hi, point->x_lo)); 2356 + input_report_abs(ds4->touchpad, ABS_MT_POSITION_Y, 2357 + DS4_TOUCH_POINT_Y(point->y_hi, point->y_lo)); 2489 2358 } 2490 2359 } 2491 2360 input_mt_sync_frame(ds4->touchpad); ··· 2503 2374 * - 15: charge error 2504 2375 */ 2505 2376 if (ds4_report->status[0] & DS4_STATUS0_CABLE_STATE) { 2506 - uint8_t battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY; 2377 + u8 battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY; 2507 2378 2508 2379 if (battery_data < 10) { 2509 2380 /* Take the mid-point for each battery capacity value, ··· 2524 2395 battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 2525 2396 } 2526 2397 } else { 2527 - uint8_t battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY; 2398 + u8 battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY; 2528 2399 2529 2400 if (battery_data < 10) 2530 2401 battery_capacity = battery_data * 10 + 5; ··· 2534 2405 battery_status = POWER_SUPPLY_STATUS_DISCHARGING; 2535 2406 } 2536 2407 2537 - spin_lock_irqsave(&ps_dev->lock, flags); 2538 - ps_dev->battery_capacity = battery_capacity; 2539 - ps_dev->battery_status = battery_status; 2540 - spin_unlock_irqrestore(&ps_dev->lock, flags); 2408 + scoped_guard(spinlock_irqsave, &ps_dev->lock) { 2409 + ps_dev->battery_capacity = battery_capacity; 2410 + ps_dev->battery_status = battery_status; 2411 + } 2541 2412 2542 2413 return 0; 2543 2414 } 2544 2415 2545 2416 static int dualshock4_dongle_parse_report(struct ps_device *ps_dev, struct hid_report *report, 2546 - u8 *data, int size) 2417 + u8 *data, int size) 2547 2418 { 2548 2419 struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base); 2549 2420 bool connected = false; ··· 2554 2425 * parsing code. 2555 2426 */ 2556 2427 if (data[0] == DS4_INPUT_REPORT_USB && size == DS4_INPUT_REPORT_USB_SIZE) { 2557 - struct dualshock4_input_report_common *ds4_report = (struct dualshock4_input_report_common *)&data[1]; 2558 - unsigned long flags; 2428 + struct dualshock4_input_report_common *ds4_report = 2429 + (struct dualshock4_input_report_common *)&data[1]; 2559 2430 2560 2431 connected = ds4_report->status[1] & DS4_STATUS1_DONGLE_STATE ? false : true; 2561 2432 ··· 2564 2435 2565 2436 dualshock4_set_default_lightbar_colors(ds4); 2566 2437 2567 - spin_lock_irqsave(&ps_dev->lock, flags); 2568 - ds4->dongle_state = DONGLE_CALIBRATING; 2569 - spin_unlock_irqrestore(&ps_dev->lock, flags); 2438 + scoped_guard(spinlock_irqsave, &ps_dev->lock) 2439 + ds4->dongle_state = DONGLE_CALIBRATING; 2570 2440 2571 2441 schedule_work(&ds4->dongle_hotplug_worker); 2572 2442 ··· 2577 2449 ds4->dongle_state == DONGLE_DISABLED) && !connected) { 2578 2450 hid_info(ps_dev->hdev, "DualShock 4 USB dongle: controller disconnected\n"); 2579 2451 2580 - spin_lock_irqsave(&ps_dev->lock, flags); 2581 - ds4->dongle_state = DONGLE_DISCONNECTED; 2582 - spin_unlock_irqrestore(&ps_dev->lock, flags); 2452 + scoped_guard(spinlock_irqsave, &ps_dev->lock) 2453 + ds4->dongle_state = DONGLE_DISCONNECTED; 2583 2454 2584 2455 /* Return 0, so hidraw can get the report. */ 2585 2456 return 0; ··· 2600 2473 { 2601 2474 struct hid_device *hdev = input_get_drvdata(dev); 2602 2475 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 2603 - unsigned long flags; 2604 2476 2605 2477 if (effect->type != FF_RUMBLE) 2606 2478 return 0; 2607 2479 2608 - spin_lock_irqsave(&ds4->base.lock, flags); 2609 - ds4->update_rumble = true; 2610 - ds4->motor_left = effect->u.rumble.strong_magnitude / 256; 2611 - ds4->motor_right = effect->u.rumble.weak_magnitude / 256; 2612 - spin_unlock_irqrestore(&ds4->base.lock, flags); 2480 + scoped_guard(spinlock_irqsave, &ds4->base.lock) { 2481 + ds4->update_rumble = true; 2482 + ds4->motor_left = effect->u.rumble.strong_magnitude / 256; 2483 + ds4->motor_right = effect->u.rumble.weak_magnitude / 256; 2484 + } 2613 2485 2614 2486 dualshock4_schedule_work(ds4); 2615 2487 return 0; ··· 2617 2491 static void dualshock4_remove(struct ps_device *ps_dev) 2618 2492 { 2619 2493 struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base); 2620 - unsigned long flags; 2621 2494 2622 - spin_lock_irqsave(&ds4->base.lock, flags); 2623 - ds4->output_worker_initialized = false; 2624 - spin_unlock_irqrestore(&ds4->base.lock, flags); 2495 + scoped_guard(spinlock_irqsave, &ds4->base.lock) 2496 + ds4->output_worker_initialized = false; 2625 2497 2626 2498 cancel_work_sync(&ds4->output_worker); 2627 2499 ··· 2629 2505 2630 2506 static inline void dualshock4_schedule_work(struct dualshock4 *ds4) 2631 2507 { 2632 - unsigned long flags; 2633 - 2634 - spin_lock_irqsave(&ds4->base.lock, flags); 2635 - if (ds4->output_worker_initialized) 2636 - schedule_work(&ds4->output_worker); 2637 - spin_unlock_irqrestore(&ds4->base.lock, flags); 2508 + /* Using scoped_guard() instead of guard() to make sparse happy */ 2509 + scoped_guard(spinlock_irqsave, &ds4->base.lock) 2510 + if (ds4->output_worker_initialized) 2511 + schedule_work(&ds4->output_worker); 2638 2512 } 2639 2513 2640 - static void dualshock4_set_bt_poll_interval(struct dualshock4 *ds4, uint8_t interval) 2514 + static void dualshock4_set_bt_poll_interval(struct dualshock4 *ds4, u8 interval) 2641 2515 { 2642 2516 ds4->bt_poll_interval = interval; 2643 2517 ds4->update_bt_poll_interval = true; ··· 2655 2533 { 0x20, 0x00, 0x20 } /* Pink */ 2656 2534 }; 2657 2535 2658 - uint8_t player_id = ds4->base.player_id % ARRAY_SIZE(player_colors); 2536 + u8 player_id = ds4->base.player_id % ARRAY_SIZE(player_colors); 2659 2537 2660 2538 ds4->lightbar_enabled = true; 2661 2539 ds4->lightbar_red = player_colors[player_id][0]; ··· 2670 2548 { 2671 2549 struct dualshock4 *ds4; 2672 2550 struct ps_device *ps_dev; 2673 - uint8_t max_output_report_size; 2551 + u8 max_output_report_size; 2674 2552 int i, ret; 2675 2553 2676 2554 /* The DualShock4 has an RGB lightbar, which the original hid-sony driver ··· 2683 2561 * existing applications (e.g. Android). Nothing matches against MAC address. 2684 2562 */ 2685 2563 static const struct ps_led_info lightbar_leds_info[] = { 2686 - { NULL, "red", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, 2687 - { NULL, "green", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, 2688 - { NULL, "blue", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, 2689 - { NULL, "global", 1, dualshock4_led_get_brightness, dualshock4_led_set_brightness, 2690 - dualshock4_led_set_blink }, 2564 + { NULL, "red", 255, dualshock4_led_get_brightness, 2565 + dualshock4_led_set_brightness }, 2566 + { NULL, "green", 255, dualshock4_led_get_brightness, 2567 + dualshock4_led_set_brightness }, 2568 + { NULL, "blue", 255, dualshock4_led_get_brightness, 2569 + dualshock4_led_set_brightness }, 2570 + { NULL, "global", 1, dualshock4_led_get_brightness, 2571 + dualshock4_led_set_brightness, dualshock4_led_set_blink }, 2691 2572 }; 2692 2573 2693 2574 ds4 = devm_kzalloc(&hdev->dev, sizeof(*ds4), GFP_KERNEL); ··· 2760 2635 ps_dev->input_dev_name = dev_name(&ds4->gamepad->dev); 2761 2636 2762 2637 ds4->sensors = ps_sensors_create(hdev, DS4_ACC_RANGE, DS4_ACC_RES_PER_G, 2763 - DS4_GYRO_RANGE, DS4_GYRO_RES_PER_DEG_S); 2638 + DS4_GYRO_RANGE, DS4_GYRO_RES_PER_DEG_S); 2764 2639 if (IS_ERR(ds4->sensors)) { 2765 2640 ret = PTR_ERR(ds4->sensors); 2766 2641 goto err; ··· 2799 2674 * can change behavior. 2800 2675 */ 2801 2676 hid_info(hdev, "Registered DualShock4 controller hw_version=0x%08x fw_version=0x%08x\n", 2802 - ds4->base.hw_version, ds4->base.fw_version); 2677 + ds4->base.hw_version, ds4->base.fw_version); 2803 2678 return &ds4->base; 2804 2679 2805 2680 err: ··· 2808 2683 } 2809 2684 2810 2685 static int ps_raw_event(struct hid_device *hdev, struct hid_report *report, 2811 - u8 *data, int size) 2686 + u8 *data, int size) 2812 2687 { 2813 2688 struct ps_device *dev = hid_get_drvdata(hdev); 2814 2689
+2
drivers/hid/hid-quirks.c
··· 695 695 #endif 696 696 #if IS_ENABLED(CONFIG_HID_STEELSERIES) 697 697 { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) }, 698 + { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_ARCTIS_1) }, 699 + { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_ARCTIS_9) }, 698 700 #endif 699 701 #if IS_ENABLED(CONFIG_HID_SUNPLUS) 700 702 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
+38 -70
drivers/hid/hid-steelseries.c
··· 249 249 { 250 250 int ret, i; 251 251 struct led_classdev *led; 252 + struct steelseries_srws1_data *drv_data; 252 253 size_t name_sz; 253 254 char *name; 254 255 255 - struct steelseries_srws1_data *drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL); 256 - 256 + drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL); 257 257 if (drv_data == NULL) { 258 258 hid_err(hdev, "can't alloc SRW-S1 memory\n"); 259 259 return -ENOMEM; ··· 264 264 ret = hid_parse(hdev); 265 265 if (ret) { 266 266 hid_err(hdev, "parse failed\n"); 267 - goto err_free; 267 + goto err; 268 268 } 269 269 270 270 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 16)) { 271 271 ret = -ENODEV; 272 - goto err_free; 272 + goto err; 273 273 } 274 274 275 275 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 276 276 if (ret) { 277 277 hid_err(hdev, "hw start failed\n"); 278 - goto err_free; 278 + goto err; 279 279 } 280 280 281 281 /* register led subsystem */ ··· 288 288 name_sz = strlen(hdev->uniq) + 16; 289 289 290 290 /* 'ALL', for setting all LEDs simultaneously */ 291 - led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 291 + led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 292 292 if (!led) { 293 293 hid_err(hdev, "can't allocate memory for LED ALL\n"); 294 - goto err_led; 294 + goto out; 295 295 } 296 296 297 297 name = (void *)(&led[1]); ··· 303 303 led->brightness_set = steelseries_srws1_led_all_set_brightness; 304 304 305 305 drv_data->led[SRWS1_NUMBER_LEDS] = led; 306 - ret = led_classdev_register(&hdev->dev, led); 307 - if (ret) 308 - goto err_led; 306 + ret = devm_led_classdev_register(&hdev->dev, led); 307 + if (ret) { 308 + hid_err(hdev, "failed to register LED %d. Aborting.\n", SRWS1_NUMBER_LEDS); 309 + goto out; /* let the driver continue without LEDs */ 310 + } 309 311 310 312 /* Each individual LED */ 311 313 for (i = 0; i < SRWS1_NUMBER_LEDS; i++) { 312 - led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 314 + led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 313 315 if (!led) { 314 316 hid_err(hdev, "can't allocate memory for LED %d\n", i); 315 - goto err_led; 317 + break; 316 318 } 317 319 318 320 name = (void *)(&led[1]); ··· 326 324 led->brightness_set = steelseries_srws1_led_set_brightness; 327 325 328 326 drv_data->led[i] = led; 329 - ret = led_classdev_register(&hdev->dev, led); 327 + ret = devm_led_classdev_register(&hdev->dev, led); 330 328 331 329 if (ret) { 332 330 hid_err(hdev, "failed to register LED %d. Aborting.\n", i); 333 - err_led: 334 - /* Deregister all LEDs (if any) */ 335 - for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) { 336 - led = drv_data->led[i]; 337 - drv_data->led[i] = NULL; 338 - if (!led) 339 - continue; 340 - led_classdev_unregister(led); 341 - kfree(led); 342 - } 343 - goto out; /* but let the driver continue without LEDs */ 331 + break; /* but let the driver continue without LEDs */ 344 332 } 345 333 } 346 334 out: 347 335 return 0; 348 - err_free: 349 - kfree(drv_data); 336 + err: 350 337 return ret; 351 - } 352 - 353 - static void steelseries_srws1_remove(struct hid_device *hdev) 354 - { 355 - int i; 356 - struct led_classdev *led; 357 - 358 - struct steelseries_srws1_data *drv_data = hid_get_drvdata(hdev); 359 - 360 - if (drv_data) { 361 - /* Deregister LEDs (if any) */ 362 - for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) { 363 - led = drv_data->led[i]; 364 - drv_data->led[i] = NULL; 365 - if (!led) 366 - continue; 367 - led_classdev_unregister(led); 368 - kfree(led); 369 - } 370 - 371 - } 372 - 373 - hid_hw_stop(hdev); 374 - kfree(drv_data); 375 338 } 376 339 #endif 377 340 ··· 372 405 373 406 static void steelseries_headset_fetch_battery(struct hid_device *hdev) 374 407 { 375 - struct steelseries_device *sd = hid_get_drvdata(hdev); 376 408 int ret = 0; 377 409 378 - if (sd->quirks & STEELSERIES_ARCTIS_1) 410 + if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_1) 379 411 ret = steelseries_headset_request_battery(hdev, 380 412 arctis_1_battery_request, sizeof(arctis_1_battery_request)); 381 - else if (sd->quirks & STEELSERIES_ARCTIS_9) 413 + else if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_9) 382 414 ret = steelseries_headset_request_battery(hdev, 383 415 arctis_9_battery_request, sizeof(arctis_9_battery_request)); 384 416 ··· 533 567 struct steelseries_device *sd; 534 568 int ret; 535 569 536 - sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL); 537 - if (!sd) 538 - return -ENOMEM; 539 - hid_set_drvdata(hdev, sd); 540 - sd->hdev = hdev; 541 - sd->quirks = id->driver_data; 542 - 543 - if (sd->quirks & STEELSERIES_SRWS1) { 570 + if (hdev->product == USB_DEVICE_ID_STEELSERIES_SRWS1) { 544 571 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \ 545 572 (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES)) 546 573 return steelseries_srws1_probe(hdev, id); ··· 541 582 return -ENODEV; 542 583 #endif 543 584 } 585 + 586 + sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL); 587 + if (!sd) 588 + return -ENOMEM; 589 + hid_set_drvdata(hdev, sd); 590 + sd->hdev = hdev; 591 + sd->quirks = id->driver_data; 544 592 545 593 ret = hid_parse(hdev); 546 594 if (ret) ··· 576 610 577 611 static void steelseries_remove(struct hid_device *hdev) 578 612 { 579 - struct steelseries_device *sd = hid_get_drvdata(hdev); 613 + struct steelseries_device *sd; 580 614 unsigned long flags; 581 615 582 - if (sd->quirks & STEELSERIES_SRWS1) { 616 + if (hdev->product == USB_DEVICE_ID_STEELSERIES_SRWS1) { 583 617 #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \ 584 618 (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES)) 585 - steelseries_srws1_remove(hdev); 619 + hid_hw_stop(hdev); 586 620 #endif 587 621 return; 588 622 } 623 + 624 + sd = hid_get_drvdata(hdev); 589 625 590 626 spin_lock_irqsave(&sd->lock, flags); 591 627 sd->removed = true; ··· 635 667 unsigned long flags; 636 668 637 669 /* Not a headset */ 638 - if (sd->quirks & STEELSERIES_SRWS1) 670 + if (hdev->product == USB_DEVICE_ID_STEELSERIES_SRWS1) 639 671 return 0; 640 672 641 - if (sd->quirks & STEELSERIES_ARCTIS_1) { 673 + if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_1) { 642 674 hid_dbg(sd->hdev, 643 675 "Parsing raw event for Arctis 1 headset (%*ph)\n", size, read_buf); 644 676 if (size < ARCTIS_1_BATTERY_RESPONSE_LEN || ··· 656 688 } 657 689 } 658 690 659 - if (sd->quirks & STEELSERIES_ARCTIS_9) { 691 + if (hdev->product == USB_DEVICE_ID_STEELSERIES_ARCTIS_9) { 660 692 hid_dbg(sd->hdev, 661 693 "Parsing raw event for Arctis 9 headset (%*ph)\n", size, read_buf); 662 694 if (size < ARCTIS_9_BATTERY_RESPONSE_LEN) { ··· 725 757 .driver_data = STEELSERIES_SRWS1 }, 726 758 727 759 { /* SteelSeries Arctis 1 Wireless for XBox */ 728 - HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, 0x12b6), 729 - .driver_data = STEELSERIES_ARCTIS_1 }, 760 + HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_ARCTIS_1), 761 + .driver_data = STEELSERIES_ARCTIS_1 }, 730 762 731 763 { /* SteelSeries Arctis 9 Wireless for XBox */ 732 - HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, 0x12c2), 764 + HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_ARCTIS_9), 733 765 .driver_data = STEELSERIES_ARCTIS_9 }, 734 766 735 767 { }
+5 -5
drivers/hid/hid-uclogic-params.c
··· 20 20 #include <linux/ctype.h> 21 21 #include <linux/string.h> 22 22 #include <linux/unaligned.h> 23 + #include <linux/string_choices.h> 23 24 24 25 /** 25 26 * uclogic_params_pen_inrange_to_str() - Convert a pen in-range reporting type ··· 60 59 size_t i; 61 60 62 61 hid_dbg(hdev, "\t.usage_invalid = %s\n", 63 - (pen->usage_invalid ? "true" : "false")); 62 + str_true_false(pen->usage_invalid)); 64 63 hid_dbg(hdev, "\t.desc_ptr = %p\n", pen->desc_ptr); 65 64 hid_dbg(hdev, "\t.desc_size = %u\n", pen->desc_size); 66 65 hid_dbg(hdev, "\t.id = %u\n", pen->id); ··· 75 74 hid_dbg(hdev, "\t.inrange = %s\n", 76 75 uclogic_params_pen_inrange_to_str(pen->inrange)); 77 76 hid_dbg(hdev, "\t.fragmented_hires = %s\n", 78 - (pen->fragmented_hires ? "true" : "false")); 77 + str_true_false(pen->fragmented_hires)); 79 78 hid_dbg(hdev, "\t.tilt_y_flipped = %s\n", 80 - (pen->tilt_y_flipped ? "true" : "false")); 79 + str_true_false(pen->tilt_y_flipped)); 81 80 } 82 81 83 82 /** ··· 120 119 { 121 120 size_t i; 122 121 123 - hid_dbg(hdev, ".invalid = %s\n", 124 - params->invalid ? "true" : "false"); 122 + hid_dbg(hdev, ".invalid = %s\n", str_true_false(params->invalid)); 125 123 hid_dbg(hdev, ".desc_ptr = %p\n", params->desc_ptr); 126 124 hid_dbg(hdev, ".desc_size = %u\n", params->desc_size); 127 125 hid_dbg(hdev, ".pen = {\n");
+30 -29
drivers/hid/hid-universal-pidff.c
··· 8 8 * Copyright (c) 2024, 2025 Tomasz Pakuła 9 9 */ 10 10 11 - #include <linux/device.h> 12 - #include <linux/hid.h> 13 - #include <linux/module.h> 14 - #include <linux/input-event-codes.h> 15 11 #include "hid-ids.h" 16 12 #include "usbhid/hid-pidff.h" 13 + #include <linux/device.h> 14 + #include <linux/hid.h> 15 + #include <linux/input-event-codes.h> 16 + #include <linux/module.h> 17 17 18 18 #define JOY_RANGE (BTN_DEAD - BTN_JOYSTICK + 1) 19 19 ··· 21 21 * Map buttons manually to extend the default joystick button limit 22 22 */ 23 23 static int universal_pidff_input_mapping(struct hid_device *hdev, 24 - struct hid_input *hi, struct hid_field *field, struct hid_usage *usage, 25 - unsigned long **bit, int *max) 24 + struct hid_input *hi, 25 + struct hid_field *field, 26 + struct hid_usage *usage, 27 + unsigned long **bit, int *max) 26 28 { 27 29 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) 28 30 return 0; ··· 128 126 if (!test_bit(axis, input->absbit)) 129 127 continue; 130 128 131 - input_set_abs_params(input, axis, 132 - input->absinfo[axis].minimum, 133 - input->absinfo[axis].maximum, 134 - axis == ABS_X ? 0 : 8, 0); 129 + input_set_abs_params(input, axis, input->absinfo[axis].minimum, 130 + input->absinfo[axis].maximum, 131 + axis == ABS_X ? 0 : 8, 0); 135 132 } 136 133 137 134 /* Remove fuzz and deadzone from the second joystick axis */ 138 135 if (hdev->vendor == USB_VENDOR_ID_FFBEAST && 139 136 hdev->product == USB_DEVICE_ID_FFBEAST_JOYSTICK) 140 137 input_set_abs_params(input, ABS_Y, 141 - input->absinfo[ABS_Y].minimum, 142 - input->absinfo[ABS_Y].maximum, 0, 0); 138 + input->absinfo[ABS_Y].minimum, 139 + input->absinfo[ABS_Y].maximum, 0, 0); 143 140 144 141 return 0; 145 142 } 146 143 147 144 static const struct hid_device_id universal_pidff_devices[] = { 148 145 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R3), 149 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 146 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 150 147 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R3_2), 151 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 148 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 152 149 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R5), 153 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 150 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 154 151 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R5_2), 155 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 152 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 156 153 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R9), 157 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 154 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 158 155 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R9_2), 159 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 156 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 160 157 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R12), 161 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 158 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 162 159 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R12_2), 163 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 160 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 164 161 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R16_R21), 165 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 162 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 166 163 { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R16_R21_2), 167 - .driver_data = HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION }, 164 + .driver_data = HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION }, 168 165 { HID_USB_DEVICE(USB_VENDOR_ID_CAMMUS, USB_DEVICE_ID_CAMMUS_C5) }, 169 166 { HID_USB_DEVICE(USB_VENDOR_ID_CAMMUS, USB_DEVICE_ID_CAMMUS_C12) }, 170 167 { HID_USB_DEVICE(USB_VENDOR_ID_VRS, USB_DEVICE_ID_VRS_DFP), 171 - .driver_data = HID_PIDFF_QUIRK_PERMISSIVE_CONTROL }, 168 + .driver_data = HID_PIDFF_QUIRK_PERMISSIVE_CONTROL }, 172 169 { HID_USB_DEVICE(USB_VENDOR_ID_FFBEAST, USB_DEVICE_ID_FFBEAST_JOYSTICK), }, 173 170 { HID_USB_DEVICE(USB_VENDOR_ID_FFBEAST, USB_DEVICE_ID_FFBEAST_RUDDER), }, 174 171 { HID_USB_DEVICE(USB_VENDOR_ID_FFBEAST, USB_DEVICE_ID_FFBEAST_WHEEL) }, 175 172 { HID_USB_DEVICE(USB_VENDOR_ID_LITE_STAR, USB_DEVICE_ID_PXN_V10), 176 - .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 173 + .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 177 174 { HID_USB_DEVICE(USB_VENDOR_ID_LITE_STAR, USB_DEVICE_ID_PXN_V12), 178 - .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 175 + .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 179 176 { HID_USB_DEVICE(USB_VENDOR_ID_LITE_STAR, USB_DEVICE_ID_PXN_V12_LITE), 180 - .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 177 + .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 181 178 { HID_USB_DEVICE(USB_VENDOR_ID_LITE_STAR, USB_DEVICE_ID_PXN_V12_LITE_2), 182 - .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 179 + .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 183 180 { HID_USB_DEVICE(USB_VENDOR_ID_LITE_STAR, USB_DEVICE_ID_LITE_STAR_GT987), 184 - .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 181 + .driver_data = HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY }, 185 182 { HID_USB_DEVICE(USB_VENDOR_ID_ASETEK, USB_DEVICE_ID_ASETEK_INVICTA) }, 186 183 { HID_USB_DEVICE(USB_VENDOR_ID_ASETEK, USB_DEVICE_ID_ASETEK_FORTE) }, 187 184 { HID_USB_DEVICE(USB_VENDOR_ID_ASETEK, USB_DEVICE_ID_ASETEK_LA_PRIMA) }, 188 185 { HID_USB_DEVICE(USB_VENDOR_ID_ASETEK, USB_DEVICE_ID_ASETEK_TONY_KANAAN) }, 189 - { } 186 + {} 190 187 }; 191 188 MODULE_DEVICE_TABLE(hid, universal_pidff_devices); 192 189
+141 -121
drivers/hid/hidraw.c
··· 394 394 return 0; 395 395 } 396 396 397 - static long hidraw_ioctl(struct file *file, unsigned int cmd, 398 - unsigned long arg) 397 + static long hidraw_fixed_size_ioctl(struct file *file, struct hidraw *dev, unsigned int cmd, 398 + void __user *arg) 399 + { 400 + struct hid_device *hid = dev->hid; 401 + 402 + switch (cmd) { 403 + case HIDIOCGRDESCSIZE: 404 + if (put_user(hid->rsize, (int __user *)arg)) 405 + return -EFAULT; 406 + break; 407 + 408 + case HIDIOCGRDESC: 409 + { 410 + __u32 len; 411 + 412 + if (get_user(len, (int __user *)arg)) 413 + return -EFAULT; 414 + 415 + if (len > HID_MAX_DESCRIPTOR_SIZE - 1) 416 + return -EINVAL; 417 + 418 + if (copy_to_user(arg + offsetof( 419 + struct hidraw_report_descriptor, 420 + value[0]), 421 + hid->rdesc, 422 + min(hid->rsize, len))) 423 + return -EFAULT; 424 + 425 + break; 426 + } 427 + case HIDIOCGRAWINFO: 428 + { 429 + struct hidraw_devinfo dinfo; 430 + 431 + dinfo.bustype = hid->bus; 432 + dinfo.vendor = hid->vendor; 433 + dinfo.product = hid->product; 434 + if (copy_to_user(arg, &dinfo, sizeof(dinfo))) 435 + return -EFAULT; 436 + break; 437 + } 438 + case HIDIOCREVOKE: 439 + { 440 + struct hidraw_list *list = file->private_data; 441 + 442 + if (arg) 443 + return -EINVAL; 444 + 445 + return hidraw_revoke(list); 446 + } 447 + default: 448 + /* 449 + * None of the above ioctls can return -EAGAIN, so 450 + * use it as a marker that we need to check variable 451 + * length ioctls. 452 + */ 453 + return -EAGAIN; 454 + } 455 + 456 + return 0; 457 + } 458 + 459 + static long hidraw_rw_variable_size_ioctl(struct file *file, struct hidraw *dev, unsigned int cmd, 460 + void __user *user_arg) 461 + { 462 + int len = _IOC_SIZE(cmd); 463 + 464 + switch (cmd & ~IOCSIZE_MASK) { 465 + case HIDIOCSFEATURE(0): 466 + return hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT); 467 + case HIDIOCGFEATURE(0): 468 + return hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT); 469 + case HIDIOCSINPUT(0): 470 + return hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT); 471 + case HIDIOCGINPUT(0): 472 + return hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT); 473 + case HIDIOCSOUTPUT(0): 474 + return hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT); 475 + case HIDIOCGOUTPUT(0): 476 + return hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT); 477 + } 478 + 479 + return -EINVAL; 480 + } 481 + 482 + static long hidraw_ro_variable_size_ioctl(struct file *file, struct hidraw *dev, unsigned int cmd, 483 + void __user *user_arg) 484 + { 485 + struct hid_device *hid = dev->hid; 486 + int len = _IOC_SIZE(cmd); 487 + int field_len; 488 + 489 + switch (cmd & ~IOCSIZE_MASK) { 490 + case HIDIOCGRAWNAME(0): 491 + field_len = strlen(hid->name) + 1; 492 + if (len > field_len) 493 + len = field_len; 494 + return copy_to_user(user_arg, hid->name, len) ? -EFAULT : len; 495 + case HIDIOCGRAWPHYS(0): 496 + field_len = strlen(hid->phys) + 1; 497 + if (len > field_len) 498 + len = field_len; 499 + return copy_to_user(user_arg, hid->phys, len) ? -EFAULT : len; 500 + case HIDIOCGRAWUNIQ(0): 501 + field_len = strlen(hid->uniq) + 1; 502 + if (len > field_len) 503 + len = field_len; 504 + return copy_to_user(user_arg, hid->uniq, len) ? -EFAULT : len; 505 + } 506 + 507 + return -EINVAL; 508 + } 509 + 510 + static long hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 399 511 { 400 512 struct inode *inode = file_inode(file); 401 513 unsigned int minor = iminor(inode); 402 - long ret = 0; 403 514 struct hidraw *dev; 404 515 struct hidraw_list *list = file->private_data; 405 - void __user *user_arg = (void __user*) arg; 516 + void __user *user_arg = (void __user *)arg; 517 + int ret; 406 518 407 519 down_read(&minors_rwsem); 408 520 dev = hidraw_table[minor]; ··· 523 411 goto out; 524 412 } 525 413 526 - switch (cmd) { 527 - case HIDIOCGRDESCSIZE: 528 - if (put_user(dev->hid->rsize, (int __user *)arg)) 529 - ret = -EFAULT; 530 - break; 531 - 532 - case HIDIOCGRDESC: 533 - { 534 - __u32 len; 535 - 536 - if (get_user(len, (int __user *)arg)) 537 - ret = -EFAULT; 538 - else if (len > HID_MAX_DESCRIPTOR_SIZE - 1) 539 - ret = -EINVAL; 540 - else if (copy_to_user(user_arg + offsetof( 541 - struct hidraw_report_descriptor, 542 - value[0]), 543 - dev->hid->rdesc, 544 - min(dev->hid->rsize, len))) 545 - ret = -EFAULT; 546 - break; 547 - } 548 - case HIDIOCGRAWINFO: 549 - { 550 - struct hidraw_devinfo dinfo; 551 - 552 - dinfo.bustype = dev->hid->bus; 553 - dinfo.vendor = dev->hid->vendor; 554 - dinfo.product = dev->hid->product; 555 - if (copy_to_user(user_arg, &dinfo, sizeof(dinfo))) 556 - ret = -EFAULT; 557 - break; 558 - } 559 - case HIDIOCREVOKE: 560 - { 561 - if (user_arg) 562 - ret = -EINVAL; 563 - else 564 - ret = hidraw_revoke(list); 565 - break; 566 - } 567 - default: 568 - { 569 - struct hid_device *hid = dev->hid; 570 - if (_IOC_TYPE(cmd) != 'H') { 571 - ret = -EINVAL; 572 - break; 573 - } 574 - 575 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) { 576 - int len = _IOC_SIZE(cmd); 577 - ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT); 578 - break; 579 - } 580 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) { 581 - int len = _IOC_SIZE(cmd); 582 - ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT); 583 - break; 584 - } 585 - 586 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSINPUT(0))) { 587 - int len = _IOC_SIZE(cmd); 588 - ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT); 589 - break; 590 - } 591 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGINPUT(0))) { 592 - int len = _IOC_SIZE(cmd); 593 - ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT); 594 - break; 595 - } 596 - 597 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSOUTPUT(0))) { 598 - int len = _IOC_SIZE(cmd); 599 - ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT); 600 - break; 601 - } 602 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGOUTPUT(0))) { 603 - int len = _IOC_SIZE(cmd); 604 - ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT); 605 - break; 606 - } 607 - 608 - /* Begin Read-only ioctls. */ 609 - if (_IOC_DIR(cmd) != _IOC_READ) { 610 - ret = -EINVAL; 611 - break; 612 - } 613 - 614 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) { 615 - int len = strlen(hid->name) + 1; 616 - if (len > _IOC_SIZE(cmd)) 617 - len = _IOC_SIZE(cmd); 618 - ret = copy_to_user(user_arg, hid->name, len) ? 619 - -EFAULT : len; 620 - break; 621 - } 622 - 623 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) { 624 - int len = strlen(hid->phys) + 1; 625 - if (len > _IOC_SIZE(cmd)) 626 - len = _IOC_SIZE(cmd); 627 - ret = copy_to_user(user_arg, hid->phys, len) ? 628 - -EFAULT : len; 629 - break; 630 - } 631 - 632 - if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) { 633 - int len = strlen(hid->uniq) + 1; 634 - if (len > _IOC_SIZE(cmd)) 635 - len = _IOC_SIZE(cmd); 636 - ret = copy_to_user(user_arg, hid->uniq, len) ? 637 - -EFAULT : len; 638 - break; 639 - } 640 - } 641 - 642 - ret = -ENOTTY; 414 + if (_IOC_TYPE(cmd) != 'H') { 415 + ret = -EINVAL; 416 + goto out; 643 417 } 418 + 419 + if (_IOC_NR(cmd) > HIDIOCTL_LAST || _IOC_NR(cmd) == 0) { 420 + ret = -ENOTTY; 421 + goto out; 422 + } 423 + 424 + ret = hidraw_fixed_size_ioctl(file, dev, cmd, user_arg); 425 + if (ret != -EAGAIN) 426 + goto out; 427 + 428 + switch (_IOC_DIR(cmd)) { 429 + case (_IOC_READ | _IOC_WRITE): 430 + ret = hidraw_rw_variable_size_ioctl(file, dev, cmd, user_arg); 431 + break; 432 + case _IOC_READ: 433 + ret = hidraw_ro_variable_size_ioctl(file, dev, cmd, user_arg); 434 + break; 435 + default: 436 + /* Any other IOC_DIR is wrong */ 437 + ret = -EINVAL; 438 + } 439 + 644 440 out: 645 441 up_read(&minors_rwsem); 646 442 return ret;
+8
drivers/hid/i2c-hid/i2c-hid-acpi.c
··· 76 76 return hid_descriptor_address; 77 77 } 78 78 79 + static void i2c_hid_acpi_restore_sequence(struct i2chid_ops *ops) 80 + { 81 + struct i2c_hid_acpi *ihid_acpi = container_of(ops, struct i2c_hid_acpi, ops); 82 + 83 + i2c_hid_acpi_get_descriptor(ihid_acpi); 84 + } 85 + 79 86 static void i2c_hid_acpi_shutdown_tail(struct i2chid_ops *ops) 80 87 { 81 88 struct i2c_hid_acpi *ihid_acpi = container_of(ops, struct i2c_hid_acpi, ops); ··· 103 96 104 97 ihid_acpi->adev = ACPI_COMPANION(dev); 105 98 ihid_acpi->ops.shutdown_tail = i2c_hid_acpi_shutdown_tail; 99 + ihid_acpi->ops.restore_sequence = i2c_hid_acpi_restore_sequence; 106 100 107 101 ret = i2c_hid_acpi_get_descriptor(ihid_acpi); 108 102 if (ret < 0)
+27 -1
drivers/hid/i2c-hid/i2c-hid-core.c
··· 961 961 ihid->ops->shutdown_tail(ihid->ops); 962 962 } 963 963 964 + static void i2c_hid_core_restore_sequence(struct i2c_hid *ihid) 965 + { 966 + if (!ihid->ops->restore_sequence) 967 + return; 968 + 969 + ihid->ops->restore_sequence(ihid->ops); 970 + } 971 + 964 972 static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff) 965 973 { 966 974 struct i2c_client *client = ihid->client; ··· 1378 1370 return i2c_hid_core_resume(ihid); 1379 1371 } 1380 1372 1373 + static int i2c_hid_core_pm_restore(struct device *dev) 1374 + { 1375 + struct i2c_client *client = to_i2c_client(dev); 1376 + struct i2c_hid *ihid = i2c_get_clientdata(client); 1377 + 1378 + if (ihid->is_panel_follower) 1379 + return 0; 1380 + 1381 + i2c_hid_core_restore_sequence(ihid); 1382 + 1383 + return i2c_hid_core_resume(ihid); 1384 + } 1385 + 1381 1386 const struct dev_pm_ops i2c_hid_core_pm = { 1382 - SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume) 1387 + .suspend = pm_sleep_ptr(i2c_hid_core_pm_suspend), 1388 + .resume = pm_sleep_ptr(i2c_hid_core_pm_resume), 1389 + .freeze = pm_sleep_ptr(i2c_hid_core_pm_suspend), 1390 + .thaw = pm_sleep_ptr(i2c_hid_core_pm_resume), 1391 + .poweroff = pm_sleep_ptr(i2c_hid_core_pm_suspend), 1392 + .restore = pm_sleep_ptr(i2c_hid_core_pm_restore), 1383 1393 }; 1384 1394 EXPORT_SYMBOL_GPL(i2c_hid_core_pm); 1385 1395
+2
drivers/hid/i2c-hid/i2c-hid.h
··· 27 27 * @power_up: do sequencing to power up the device. 28 28 * @power_down: do sequencing to power down the device. 29 29 * @shutdown_tail: called at the end of shutdown. 30 + * @restore_sequence: hibernation restore sequence. 30 31 */ 31 32 struct i2chid_ops { 32 33 int (*power_up)(struct i2chid_ops *ops); 33 34 void (*power_down)(struct i2chid_ops *ops); 34 35 void (*shutdown_tail)(struct i2chid_ops *ops); 36 + void (*restore_sequence)(struct i2chid_ops *ops); 35 37 }; 36 38 37 39 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
+9 -8
drivers/hid/intel-ish-hid/ipc/ipc.c
··· 498 498 { 499 499 uint32_t reset_id; 500 500 unsigned long flags; 501 + int ret; 501 502 502 503 /* Read reset ID */ 503 504 reset_id = ish_reg_read(dev, IPC_REG_ISH2HOST_MSG) & 0xFFFF; ··· 511 510 /* ISHTP notification in IPC_RESET */ 512 511 ishtp_reset_handler(dev); 513 512 514 - if (!ish_is_input_ready(dev)) 515 - timed_wait_for_timeout(dev, WAIT_FOR_INPUT_RDY, 516 - TIME_SLICE_FOR_INPUT_RDY_MS, TIMEOUT_FOR_INPUT_RDY_MS); 517 - 513 + ret = timed_wait_for_timeout(dev, WAIT_FOR_INPUT_RDY, 514 + TIME_SLICE_FOR_INPUT_RDY_MS, 515 + TIMEOUT_FOR_INPUT_RDY_MS); 518 516 /* ISH FW is dead */ 519 - if (!ish_is_input_ready(dev)) 517 + if (ret) 520 518 return -EPIPE; 521 519 522 520 /* Send clock sync at once after reset */ ··· 531 531 sizeof(uint32_t)); 532 532 533 533 /* Wait for ISH FW'es ILUP and ISHTP_READY */ 534 - timed_wait_for_timeout(dev, WAIT_FOR_FW_RDY, 535 - TIME_SLICE_FOR_FW_RDY_MS, TIMEOUT_FOR_FW_RDY_MS); 536 - if (!ishtp_fw_is_ready(dev)) { 534 + ret = timed_wait_for_timeout(dev, WAIT_FOR_FW_RDY, 535 + TIME_SLICE_FOR_FW_RDY_MS, 536 + TIMEOUT_FOR_FW_RDY_MS); 537 + if (ret) { 537 538 /* ISH FW is dead */ 538 539 uint32_t ish_status; 539 540
+31 -8
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
··· 23 23 24 24 static struct quicki2c_ddata ptl_ddata = { 25 25 .max_detect_size = MAX_RX_DETECT_SIZE_PTL, 26 + .max_interrupt_delay = MAX_RX_INTERRUPT_DELAY, 26 27 }; 27 28 28 29 /* THC QuickI2C ACPI method to get device properties */ ··· 199 198 qcdev->i2c_clock_lcnt = i2c_config.HMLX; 200 199 } else { 201 200 return -EOPNOTSUPP; 201 + } 202 + 203 + if (qcdev->ddata) { 204 + qcdev->i2c_max_frame_size_enable = i2c_config.FSEN; 205 + qcdev->i2c_int_delay_enable = i2c_config.INDE; 206 + 207 + if (i2c_config.FSVL <= qcdev->ddata->max_detect_size) 208 + qcdev->i2c_max_frame_size = i2c_config.FSVL; 209 + else 210 + qcdev->i2c_max_frame_size = qcdev->ddata->max_detect_size; 211 + 212 + if (i2c_config.INDV <= qcdev->ddata->max_interrupt_delay) 213 + qcdev->i2c_int_delay = i2c_config.INDV; 214 + else 215 + qcdev->i2c_int_delay = qcdev->ddata->max_interrupt_delay; 202 216 } 203 217 204 218 return 0; ··· 457 441 * max input length <= THC detect capability, enable the feature with device 458 442 * max input length. 459 443 */ 460 - if (qcdev->ddata->max_detect_size >= 461 - le16_to_cpu(qcdev->dev_desc.max_input_len)) { 462 - thc_i2c_set_rx_max_size(qcdev->thc_hw, 463 - le16_to_cpu(qcdev->dev_desc.max_input_len)); 444 + if (qcdev->i2c_max_frame_size_enable) { 445 + if (qcdev->i2c_max_frame_size >= 446 + le16_to_cpu(qcdev->dev_desc.max_input_len)) { 447 + thc_i2c_set_rx_max_size(qcdev->thc_hw, 448 + le16_to_cpu(qcdev->dev_desc.max_input_len)); 449 + } else { 450 + dev_warn(qcdev->dev, 451 + "Max frame size is smaller than hid max input length!"); 452 + thc_i2c_set_rx_max_size(qcdev->thc_hw, 453 + le16_to_cpu(qcdev->i2c_max_frame_size)); 454 + } 464 455 thc_i2c_rx_max_size_enable(qcdev->thc_hw, true); 465 456 } 466 457 467 458 /* If platform supports interrupt delay feature, enable it with given delay */ 468 - if (qcdev->ddata->interrupt_delay) { 459 + if (qcdev->i2c_int_delay_enable) { 469 460 thc_i2c_set_rx_int_delay(qcdev->thc_hw, 470 - qcdev->ddata->interrupt_delay); 461 + qcdev->i2c_int_delay * 10); 471 462 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, true); 472 463 } 473 464 } ··· 487 464 */ 488 465 static void quicki2c_dma_adv_disable(struct quicki2c_device *qcdev) 489 466 { 490 - if (qcdev->ddata->max_detect_size) 467 + if (qcdev->i2c_max_frame_size_enable) 491 468 thc_i2c_rx_max_size_enable(qcdev->thc_hw, false); 492 469 493 - if (qcdev->ddata->interrupt_delay) 470 + if (qcdev->i2c_int_delay_enable) 494 471 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, false); 495 472 } 496 473
+22 -2
drivers/hid/intel-thc-hid/intel-quicki2c/quicki2c-dev.h
··· 40 40 41 41 /* PTL Max packet size detection capability is 255 Bytes */ 42 42 #define MAX_RX_DETECT_SIZE_PTL 255 43 + /* Max interrupt delay capability is 2.56ms */ 44 + #define MAX_RX_INTERRUPT_DELAY 256 43 45 44 46 /* Default interrupt delay is 1ms, suitable for most devices */ 45 47 #define DEFAULT_INTERRUPT_DELAY_US (1 * USEC_PER_MSEC) ··· 105 103 * @HMTD: High Speed Mode Plus (3.4Mbits/sec) Serial Data Line Transmit HOLD Period 106 104 * @HMRD: High Speed Mode Plus (3.4Mbits/sec) Serial Data Line Receive HOLD Period 107 105 * @HMSL: Maximum length (in ic_clk_cycles) of suppressed spikes in High Speed Mode 106 + * @FSEN: Maximum Frame Size Feature Enable Control 107 + * @FSVL: Maximum Frame Size Value (unit in Bytes) 108 + * @INDE: Interrupt Delay Feature Enable Control 109 + * @INDV: Interrupt Delay Value (unit in 10 us) 108 110 * 109 111 * Those properties get from QUICKI2C_ACPI_METHOD_NAME_ISUB method, used for 110 112 * I2C timing configure. ··· 135 129 u64 HMTD; 136 130 u64 HMRD; 137 131 u64 HMSL; 132 + 133 + u64 FSEN; 134 + u64 FSVL; 135 + u64 INDE; 136 + u64 INDV; 138 137 u8 reserved; 139 138 }; 140 139 141 140 /** 142 141 * struct quicki2c_ddata - Driver specific data for quicki2c device 143 142 * @max_detect_size: Identify max packet size detect for rx 144 - * @interrupt_delay: Identify interrupt detect delay for rx 143 + * @interrupt_delay: Identify max interrupt detect delay for rx 145 144 */ 146 145 struct quicki2c_ddata { 147 146 u32 max_detect_size; 148 - u32 interrupt_delay; 147 + u32 max_interrupt_delay; 149 148 }; 150 149 151 150 struct device; ··· 183 172 * @report_len: The length of input/output report packet 184 173 * @reset_ack_wq: Workqueue for waiting reset response from device 185 174 * @reset_ack: Indicate reset response received or not 175 + * @i2c_max_frame_size_enable: Indicate max frame size feature enabled or not 176 + * @i2c_max_frame_size: Max RX frame size (unit in Bytes) 177 + * @i2c_int_delay_enable: Indicate interrupt delay feature enabled or not 178 + * @i2c_int_delay: Interrupt detection delay value (unit in 10 us) 186 179 */ 187 180 struct quicki2c_device { 188 181 struct device *dev; ··· 217 202 218 203 wait_queue_head_t reset_ack_wq; 219 204 bool reset_ack; 205 + 206 + u32 i2c_max_frame_size_enable; 207 + u32 i2c_max_frame_size; 208 + u32 i2c_int_delay_enable; 209 + u32 i2c_int_delay; 220 210 }; 221 211 222 212 #endif /* _QUICKI2C_DEV_H_ */
+2 -1
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
··· 4 4 #include <linux/bitfield.h> 5 5 #include <linux/math.h> 6 6 #include <linux/regmap.h> 7 + #include <linux/string_choices.h> 7 8 8 9 #include "intel-thc-dev.h" 9 10 #include "intel-thc-hw.h" ··· 665 664 if (ret) { 666 665 dev_err_once(dev->dev, 667 666 "Timeout while waiting THC idle, target quiesce state = %s\n", 668 - int_quiesce ? "true" : "false"); 667 + str_true_false(int_quiesce)); 669 668 return ret; 670 669 } 671 670
+414 -302
drivers/hid/usbhid/hid-pidff.c
··· 9 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 10 11 11 #include "hid-pidff.h" 12 + #include <linux/hid.h> 12 13 #include <linux/input.h> 14 + #include <linux/minmax.h> 13 15 #include <linux/slab.h> 14 16 #include <linux/usb.h> 15 - #include <linux/hid.h> 16 - #include <linux/minmax.h> 17 - 18 17 19 18 #define PID_EFFECTS_MAX 64 20 19 #define PID_INFINITE U16_MAX ··· 32 33 #define PID_DEVICE_CONTROL 6 33 34 #define PID_CREATE_NEW_EFFECT 7 34 35 35 - #define PID_REQUIRED_REPORTS 7 36 + #define PID_REQUIRED_REPORTS 8 36 37 37 38 #define PID_SET_ENVELOPE 8 38 39 #define PID_SET_CONDITION 9 ··· 50 51 51 52 /* PID special fields */ 52 53 #define PID_EFFECT_TYPE 0x25 54 + #define PID_AXES_ENABLE 0x55 53 55 #define PID_DIRECTION 0x57 54 56 #define PID_EFFECT_OPERATION_ARRAY 0x78 55 57 #define PID_BLOCK_LOAD_STATUS 0x8b ··· 141 141 #define PID_BLOCK_LOAD_SUCCESS 0 142 142 #define PID_BLOCK_LOAD_FULL 1 143 143 #define PID_BLOCK_LOAD_ERROR 2 144 - static const u8 pidff_block_load_status[] = { 0x8c, 0x8d, 0x8e}; 144 + static const u8 pidff_block_load_status[] = { 0x8c, 0x8d, 0x8e }; 145 145 146 146 #define PID_EFFECT_START 0 147 147 #define PID_EFFECT_STOP 1 148 148 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b }; 149 149 150 - /* Polar direction 90 degrees (East) */ 151 - #define PIDFF_FIXED_WHEEL_DIRECTION 0x4000 150 + #define PID_DIRECTION_NORTH 0x0000 151 + #define PID_DIRECTION_EAST 0x4000 152 + #define PID_DIRECTION_SOUTH 0x8000 153 + #define PID_DIRECTION_WEST 0xc000 154 + 155 + #define PIDFF_FIXED_WHEEL_DIRECTION PID_DIRECTION_EAST 156 + 157 + /* AXES_ENABLE and DIRECTION axes */ 158 + enum pid_axes { 159 + PID_AXIS_X, 160 + PID_AXIS_Y, 161 + PID_AXIS_Z, 162 + PID_AXIS_RX, 163 + PID_AXIS_RY, 164 + PID_AXIS_RZ, 165 + PID_AXIS_SLIDER, 166 + PID_AXIS_DIAL, 167 + PID_AXIS_WHEEL, 168 + PID_AXES_COUNT, 169 + }; 170 + static const u8 pidff_direction_axis[] = { 171 + HID_USAGE & HID_GD_X, 172 + HID_USAGE & HID_GD_Y, 173 + HID_USAGE & HID_GD_Z, 174 + HID_USAGE & HID_GD_RX, 175 + HID_USAGE & HID_GD_RY, 176 + HID_USAGE & HID_GD_RZ, 177 + HID_USAGE & HID_GD_SLIDER, 178 + HID_USAGE & HID_GD_DIAL, 179 + HID_USAGE & HID_GD_WHEEL, 180 + }; 152 181 153 182 struct pidff_usage { 154 183 struct hid_field *field; 155 184 s32 *value; 156 185 }; 157 186 187 + struct pidff_effect { 188 + int pid_id; 189 + int is_infinite; 190 + unsigned int loop_count; 191 + }; 192 + 158 193 struct pidff_device { 159 194 struct hid_device *hid; 160 195 161 - struct hid_report *reports[sizeof(pidff_reports)]; 196 + struct hid_report *reports[ARRAY_SIZE(pidff_reports)]; 162 197 163 - struct pidff_usage set_effect[sizeof(pidff_set_effect)]; 164 - struct pidff_usage set_envelope[sizeof(pidff_set_envelope)]; 165 - struct pidff_usage set_condition[sizeof(pidff_set_condition)]; 166 - struct pidff_usage set_periodic[sizeof(pidff_set_periodic)]; 167 - struct pidff_usage set_constant[sizeof(pidff_set_constant)]; 168 - struct pidff_usage set_ramp[sizeof(pidff_set_ramp)]; 198 + struct pidff_usage set_effect[ARRAY_SIZE(pidff_set_effect)]; 199 + struct pidff_usage set_envelope[ARRAY_SIZE(pidff_set_envelope)]; 200 + struct pidff_usage set_condition[ARRAY_SIZE(pidff_set_condition)]; 201 + struct pidff_usage set_periodic[ARRAY_SIZE(pidff_set_periodic)]; 202 + struct pidff_usage set_constant[ARRAY_SIZE(pidff_set_constant)]; 203 + struct pidff_usage set_ramp[ARRAY_SIZE(pidff_set_ramp)]; 169 204 170 - struct pidff_usage device_gain[sizeof(pidff_device_gain)]; 171 - struct pidff_usage block_load[sizeof(pidff_block_load)]; 172 - struct pidff_usage pool[sizeof(pidff_pool)]; 173 - struct pidff_usage effect_operation[sizeof(pidff_effect_operation)]; 174 - struct pidff_usage block_free[sizeof(pidff_block_free)]; 205 + struct pidff_usage device_gain[ARRAY_SIZE(pidff_device_gain)]; 206 + struct pidff_usage block_load[ARRAY_SIZE(pidff_block_load)]; 207 + struct pidff_usage pool[ARRAY_SIZE(pidff_pool)]; 208 + struct pidff_usage effect_operation[ARRAY_SIZE(pidff_effect_operation)]; 209 + struct pidff_usage block_free[ARRAY_SIZE(pidff_block_free)]; 210 + 211 + struct pidff_effect effect[PID_EFFECTS_MAX]; 175 212 176 213 /* 177 214 * Special field is a field that is not composed of ··· 221 184 /* Special fields in set_effect */ 222 185 struct hid_field *set_effect_type; 223 186 struct hid_field *effect_direction; 187 + struct hid_field *axes_enable; 224 188 225 189 /* Special field in device_control */ 226 190 struct hid_field *device_control; ··· 232 194 /* Special field in effect_operation */ 233 195 struct hid_field *effect_operation_status; 234 196 235 - int control_id[sizeof(pidff_device_control)]; 236 - int type_id[sizeof(pidff_effect_types)]; 237 - int status_id[sizeof(pidff_block_load_status)]; 238 - int operation_id[sizeof(pidff_effect_operation_status)]; 239 - 240 - int pid_id[PID_EFFECTS_MAX]; 197 + int control_id[ARRAY_SIZE(pidff_device_control)]; 198 + int type_id[ARRAY_SIZE(pidff_effect_types)]; 199 + int status_id[ARRAY_SIZE(pidff_block_load_status)]; 200 + int operation_id[ARRAY_SIZE(pidff_effect_operation_status)]; 201 + int direction_axis_id[ARRAY_SIZE(pidff_direction_axis)]; 241 202 242 203 u32 quirks; 243 204 u8 effect_count; 205 + u8 axis_count; 244 206 }; 207 + 208 + static int pidff_is_effect_conditional(struct ff_effect *effect) 209 + { 210 + return effect->type == FF_SPRING || 211 + effect->type == FF_DAMPER || 212 + effect->type == FF_INERTIA || 213 + effect->type == FF_FRICTION; 214 + } 215 + 216 + static int pidff_is_duration_infinite(u16 duration) 217 + { 218 + return duration == FF_INFINITE || duration == PID_INFINITE; 219 + } 220 + 221 + /* 222 + * Get PID effect index from FF effect type. 223 + * Return 0 if invalid. 224 + */ 225 + static int pidff_effect_ff_to_pid(struct ff_effect *effect) 226 + { 227 + switch (effect->type) { 228 + case FF_CONSTANT: 229 + return PID_CONSTANT; 230 + case FF_RAMP: 231 + return PID_RAMP; 232 + case FF_SPRING: 233 + return PID_SPRING; 234 + case FF_DAMPER: 235 + return PID_DAMPER; 236 + case FF_INERTIA: 237 + return PID_INERTIA; 238 + case FF_FRICTION: 239 + return PID_FRICTION; 240 + case FF_PERIODIC: 241 + switch (effect->u.periodic.waveform) { 242 + case FF_SQUARE: 243 + return PID_SQUARE; 244 + case FF_TRIANGLE: 245 + return PID_TRIANGLE; 246 + case FF_SINE: 247 + return PID_SINE; 248 + case FF_SAW_UP: 249 + return PID_SAW_UP; 250 + case FF_SAW_DOWN: 251 + return PID_SAW_DOWN; 252 + } 253 + } 254 + pr_err("invalid effect type\n"); 255 + return -EINVAL; 256 + } 257 + 258 + /* 259 + * Get effect id in the device descriptor. 260 + * Return 0 if invalid. 261 + */ 262 + static int pidff_get_effect_type_id(struct pidff_device *pidff, 263 + struct ff_effect *effect) 264 + { 265 + int id = pidff_effect_ff_to_pid(effect); 266 + 267 + if (id < 0) 268 + return 0; 269 + 270 + if (effect->type == FF_PERIODIC && 271 + pidff->quirks & HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY) 272 + id = PID_SINE; 273 + 274 + return pidff->type_id[id]; 275 + } 245 276 246 277 /* 247 278 * Clamp value for a given field ··· 326 219 static int pidff_rescale(int i, int max, struct hid_field *field) 327 220 { 328 221 return i * (field->logical_maximum - field->logical_minimum) / max + 329 - field->logical_minimum; 222 + field->logical_minimum; 330 223 } 331 224 332 225 /* ··· 372 265 else { 373 266 if (value < 0) 374 267 usage->value[0] = 375 - pidff_rescale(-value, -S16_MIN, usage->field); 268 + pidff_rescale(-value, -S16_MIN, usage->field); 376 269 else 377 270 usage->value[0] = 378 - pidff_rescale(value, S16_MAX, usage->field); 271 + pidff_rescale(value, S16_MAX, usage->field); 379 272 } 380 273 pr_debug("calculated from %d to %d\n", value, usage->value[0]); 381 274 } 382 275 383 276 static void pidff_set_time(struct pidff_usage *usage, u16 time) 384 277 { 385 - usage->value[0] = pidff_clamp( 386 - pidff_rescale_time(time, usage->field), usage->field); 278 + usage->value[0] = pidff_clamp(pidff_rescale_time(time, usage->field), 279 + usage->field); 387 280 } 388 281 389 282 static void pidff_set_duration(struct pidff_usage *usage, u16 duration) 390 283 { 391 - /* Infinite value conversion from Linux API -> PID */ 392 - if (duration == FF_INFINITE) 393 - duration = PID_INFINITE; 394 - 395 284 /* PID defines INFINITE as the max possible value for duration field */ 396 - if (duration == PID_INFINITE) { 285 + if (pidff_is_duration_infinite(duration)) { 397 286 usage->value[0] = (1U << usage->field->report_size) - 1; 398 287 return; 399 288 } 400 289 401 290 pidff_set_time(usage, duration); 291 + } 292 + 293 + static void pidff_set_effect_direction(struct pidff_device *pidff, 294 + struct ff_effect *effect) 295 + { 296 + u16 direction = effect->direction; 297 + int direction_enable = 1; 298 + 299 + /* Use fixed direction if needed */ 300 + if (pidff->quirks & HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION && 301 + pidff_is_effect_conditional(effect)) 302 + direction = PIDFF_FIXED_WHEEL_DIRECTION; 303 + 304 + pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = direction_enable; 305 + pidff->effect_direction->value[0] = 306 + pidff_rescale(direction, U16_MAX, pidff->effect_direction); 307 + 308 + if (direction_enable) 309 + return; 310 + 311 + /* 312 + * For use with improved FFB API 313 + * We want to read the selected axes and their direction from the effect 314 + * struct and only enable those. For now, enable all axes. 315 + * 316 + */ 317 + for (int i = 0; i < PID_AXES_COUNT; i++) { 318 + /* HID index starts with 1 */ 319 + int index = pidff->direction_axis_id[i] - 1; 320 + 321 + if (index < 0) 322 + continue; 323 + 324 + pidff->axes_enable->value[index] = 1; 325 + pidff->effect_direction->value[index] = pidff_rescale( 326 + direction, U16_MAX, pidff->effect_direction); 327 + } 402 328 } 403 329 404 330 /* ··· 453 313 pidff->set_envelope[PID_FADE_LEVEL].field); 454 314 455 315 pidff_set_time(&pidff->set_envelope[PID_ATTACK_TIME], 456 - envelope->attack_length); 316 + envelope->attack_length); 457 317 pidff_set_time(&pidff->set_envelope[PID_FADE_TIME], 458 - envelope->fade_length); 459 - 460 - hid_dbg(pidff->hid, "attack %u => %d\n", 461 - envelope->attack_level, 462 - pidff->set_envelope[PID_ATTACK_LEVEL].value[0]); 318 + envelope->fade_length); 463 319 464 320 hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE], 465 - HID_REQ_SET_REPORT); 321 + HID_REQ_SET_REPORT); 466 322 } 467 323 468 324 /* ··· 467 331 static int pidff_needs_set_envelope(struct ff_envelope *envelope, 468 332 struct ff_envelope *old) 469 333 { 470 - bool needs_new_envelope; 334 + int needs_new_envelope; 471 335 472 336 needs_new_envelope = envelope->attack_level != 0 || 473 337 envelope->fade_level != 0 || ··· 475 339 envelope->fade_length != 0; 476 340 477 341 if (!needs_new_envelope) 478 - return false; 479 - 342 + return 0; 480 343 if (!old) 481 344 return needs_new_envelope; 482 345 ··· 488 353 /* 489 354 * Send constant force report to the device 490 355 */ 491 - static void pidff_set_constant_force_report(struct pidff_device *pidff, 492 - struct ff_effect *effect) 356 + static void pidff_set_constant_report(struct pidff_device *pidff, 357 + struct ff_effect *effect) 493 358 { 494 359 pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] = 495 360 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; ··· 497 362 effect->u.constant.level); 498 363 499 364 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT], 500 - HID_REQ_SET_REPORT); 365 + HID_REQ_SET_REPORT); 501 366 } 502 367 503 368 /* ··· 521 386 pidff->create_new_effect_type->value[0]; 522 387 523 388 pidff_set_duration(&pidff->set_effect[PID_DURATION], 524 - effect->replay.length); 389 + effect->replay.length); 525 390 526 391 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button; 527 392 pidff_set_time(&pidff->set_effect[PID_TRIGGER_REPEAT_INT], 528 - effect->trigger.interval); 393 + effect->trigger.interval); 529 394 pidff->set_effect[PID_GAIN].value[0] = 530 395 pidff->set_effect[PID_GAIN].field->logical_maximum; 531 - pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1; 532 396 533 - /* Use fixed direction if needed */ 534 - pidff->effect_direction->value[0] = pidff_rescale( 535 - pidff->quirks & HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION ? 536 - PIDFF_FIXED_WHEEL_DIRECTION : effect->direction, 537 - U16_MAX, pidff->effect_direction); 397 + pidff_set_effect_direction(pidff, effect); 538 398 539 399 /* Omit setting delay field if it's missing */ 540 400 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY)) 541 401 pidff_set_time(&pidff->set_effect[PID_START_DELAY], 542 - effect->replay.delay); 402 + effect->replay.delay); 543 403 544 404 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT], 545 - HID_REQ_SET_REPORT); 405 + HID_REQ_SET_REPORT); 546 406 } 547 407 548 408 /* ··· 567 437 effect->u.periodic.offset); 568 438 pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase); 569 439 pidff_set_time(&pidff->set_periodic[PID_PERIOD], 570 - effect->u.periodic.period); 440 + effect->u.periodic.period); 571 441 572 442 hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC], 573 - HID_REQ_SET_REPORT); 443 + HID_REQ_SET_REPORT); 574 444 } 575 445 576 446 /* ··· 617 487 pidff_set(&pidff->set_condition[PID_DEAD_BAND], 618 488 effect->u.condition[i].deadband); 619 489 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION], 620 - HID_REQ_SET_REPORT); 490 + HID_REQ_SET_REPORT); 621 491 } 622 492 } 623 493 ··· 648 518 /* 649 519 * Send ramp force report to the device 650 520 */ 651 - static void pidff_set_ramp_force_report(struct pidff_device *pidff, 652 - struct ff_effect *effect) 521 + static void pidff_set_ramp_report(struct pidff_device *pidff, 522 + struct ff_effect *effect) 653 523 { 654 524 pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] = 655 525 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; ··· 658 528 pidff_set_signed(&pidff->set_ramp[PID_RAMP_END], 659 529 effect->u.ramp.end_level); 660 530 hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP], 661 - HID_REQ_SET_REPORT); 531 + HID_REQ_SET_REPORT); 662 532 } 663 533 664 534 /* ··· 680 550 681 551 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain); 682 552 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN], 683 - HID_REQ_SET_REPORT); 553 + HID_REQ_SET_REPORT); 684 554 } 685 555 686 556 /* ··· 688 558 */ 689 559 static void pidff_set_device_control(struct pidff_device *pidff, int field) 690 560 { 691 - int i, index; 692 - int field_index = pidff->control_id[field]; 561 + const int field_index = pidff->control_id[field]; 693 562 694 563 if (field_index < 1) 695 564 return; ··· 698 569 hid_dbg(pidff->hid, "DEVICE_CONTROL is a bitmask\n"); 699 570 700 571 /* Clear current bitmask */ 701 - for (i = 0; i < sizeof(pidff_device_control); i++) { 702 - index = pidff->control_id[i]; 572 + for (int i = 0; i < ARRAY_SIZE(pidff_device_control); i++) { 573 + int index = pidff->control_id[i]; 574 + 703 575 if (index < 1) 704 576 continue; 705 577 ··· 715 585 716 586 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT); 717 587 hid_hw_wait(pidff->hid); 718 - } 719 - 720 - /* 721 - * Modify actuators state 722 - */ 723 - static void pidff_set_actuators(struct pidff_device *pidff, bool enable) 724 - { 725 - hid_dbg(pidff->hid, "%s actuators\n", enable ? "Enable" : "Disable"); 726 - pidff_set_device_control(pidff, 727 - enable ? PID_ENABLE_ACTUATORS : PID_DISABLE_ACTUATORS); 588 + hid_dbg(pidff->hid, "Device control command 0x%02x sent", 589 + pidff_device_control[field]); 728 590 } 729 591 730 592 /* ··· 730 608 pidff->effect_count = 0; 731 609 732 610 pidff_set_device_control(pidff, PID_STOP_ALL_EFFECTS); 733 - pidff_set_actuators(pidff, 1); 611 + pidff_set_device_control(pidff, PID_ENABLE_ACTUATORS); 734 612 } 735 613 736 614 /* ··· 766 644 */ 767 645 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum) 768 646 { 769 - int j; 770 - 771 - if (!pidff->effect_count) 772 - pidff_reset(pidff); 773 - 774 647 pidff->create_new_effect_type->value[0] = efnum; 775 648 hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT], 776 - HID_REQ_SET_REPORT); 649 + HID_REQ_SET_REPORT); 777 650 hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum); 778 651 779 652 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0; 780 653 pidff->block_load_status->value[0] = 0; 781 654 hid_hw_wait(pidff->hid); 782 655 783 - for (j = 0; j < 60; j++) { 656 + for (int i = 0; i < 60; i++) { 784 657 hid_dbg(pidff->hid, "pid_block_load requested\n"); 785 658 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD], 786 - HID_REQ_GET_REPORT); 659 + HID_REQ_GET_REPORT); 787 660 hid_hw_wait(pidff->hid); 788 661 if (pidff->block_load_status->value[0] == 789 662 pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) { 790 663 hid_dbg(pidff->hid, "device reported free memory: %d bytes\n", 791 664 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ? 792 665 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1); 793 - 794 - pidff->effect_count++; 795 666 return 0; 796 667 } 797 668 if (pidff->block_load_status->value[0] == ··· 804 689 return -EIO; 805 690 } 806 691 692 + static int pidff_needs_playback(struct pidff_device *pidff, int effect_id, int n) 693 + { 694 + return pidff->effect[effect_id].is_infinite || 695 + pidff->effect[effect_id].loop_count != n; 696 + } 697 + 807 698 /* 808 699 * Play the effect with PID id n times 809 700 */ 810 701 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n) 811 702 { 812 703 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id; 704 + 705 + hid_dbg(pidff->hid, "%s PID effect %d", n == 0 ? "stopping" : "playing", 706 + pid_id); 813 707 814 708 if (n == 0) { 815 709 pidff->effect_operation_status->value[0] = ··· 831 707 } 832 708 833 709 hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION], 834 - HID_REQ_SET_REPORT); 710 + HID_REQ_SET_REPORT); 835 711 } 836 712 837 713 /* ··· 841 717 { 842 718 struct pidff_device *pidff = dev->ff->private; 843 719 844 - pidff_playback_pid(pidff, pidff->pid_id[effect_id], value); 720 + if (!pidff_needs_playback(pidff, effect_id, value)) 721 + return 0; 722 + 723 + hid_dbg(pidff->hid, "requesting %s on FF effect %d", 724 + value == 0 ? "stop" : "playback", effect_id); 725 + 726 + pidff->effect[effect_id].loop_count = value; 727 + pidff_playback_pid(pidff, pidff->effect[effect_id].pid_id, value); 845 728 return 0; 846 729 } 847 730 ··· 860 729 { 861 730 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id; 862 731 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE], 863 - HID_REQ_SET_REPORT); 864 - 865 - if (pidff->effect_count > 0) 866 - pidff->effect_count--; 732 + HID_REQ_SET_REPORT); 867 733 } 868 734 869 735 /* ··· 869 741 static int pidff_erase_effect(struct input_dev *dev, int effect_id) 870 742 { 871 743 struct pidff_device *pidff = dev->ff->private; 872 - int pid_id = pidff->pid_id[effect_id]; 744 + int pid_id = pidff->effect[effect_id].pid_id; 873 745 874 - hid_dbg(pidff->hid, "starting to erase %d/%d\n", 875 - effect_id, pidff->pid_id[effect_id]); 746 + hid_dbg(pidff->hid, "starting to erase %d/%d\n", effect_id, pid_id); 876 747 877 748 /* 878 749 * Wait for the queue to clear. We do not want ··· 881 754 pidff_playback_pid(pidff, pid_id, 0); 882 755 pidff_erase_pid(pidff, pid_id); 883 756 757 + if (pidff->effect_count > 0) 758 + pidff->effect_count--; 759 + 760 + hid_dbg(pidff->hid, "current effect count: %d", pidff->effect_count); 884 761 return 0; 885 762 } 763 + 764 + #define PIDFF_SET_REPORT_IF_NEEDED(type, effect, old) \ 765 + ({ if (!old || pidff_needs_set_## type(effect, old)) \ 766 + pidff_set_ ##type## _report(pidff, effect); }) 767 + 768 + #define PIDFF_SET_ENVELOPE_IF_NEEDED(type, effect, old) \ 769 + ({ if (pidff_needs_set_envelope(&effect->u.type.envelope, \ 770 + old ? &old->u.type.envelope : NULL)) \ 771 + pidff_set_envelope_report(pidff, &effect->u.type.envelope); }) 886 772 887 773 /* 888 774 * Effect upload handler 889 775 */ 890 - static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect, 776 + static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *new, 891 777 struct ff_effect *old) 892 778 { 893 779 struct pidff_device *pidff = dev->ff->private; 894 - int type_id; 895 - int error; 780 + const int type_id = pidff_get_effect_type_id(pidff, new); 896 781 897 - pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0; 898 - if (old) { 899 - pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 900 - pidff->pid_id[effect->id]; 782 + if (!type_id) { 783 + hid_err(pidff->hid, "effect type not supported\n"); 784 + return -EINVAL; 901 785 } 902 786 903 - switch (effect->type) { 787 + if (!pidff->effect_count) 788 + pidff_reset(pidff); 789 + 790 + if (!old) { 791 + int error = pidff_request_effect_upload(pidff, type_id); 792 + 793 + if (error) 794 + return error; 795 + 796 + pidff->effect_count++; 797 + hid_dbg(pidff->hid, "current effect count: %d", pidff->effect_count); 798 + pidff->effect[new->id].loop_count = 0; 799 + pidff->effect[new->id].pid_id = 800 + pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 801 + } 802 + 803 + pidff->effect[new->id].is_infinite = 804 + pidff_is_duration_infinite(new->replay.length); 805 + 806 + pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 807 + pidff->effect[new->id].pid_id; 808 + 809 + PIDFF_SET_REPORT_IF_NEEDED(effect, new, old); 810 + switch (new->type) { 904 811 case FF_CONSTANT: 905 - if (!old) { 906 - error = pidff_request_effect_upload(pidff, 907 - pidff->type_id[PID_CONSTANT]); 908 - if (error) 909 - return error; 910 - } 911 - if (!old || pidff_needs_set_effect(effect, old)) 912 - pidff_set_effect_report(pidff, effect); 913 - if (!old || pidff_needs_set_constant(effect, old)) 914 - pidff_set_constant_force_report(pidff, effect); 915 - if (pidff_needs_set_envelope(&effect->u.constant.envelope, 916 - old ? &old->u.constant.envelope : NULL)) 917 - pidff_set_envelope_report(pidff, &effect->u.constant.envelope); 812 + PIDFF_SET_REPORT_IF_NEEDED(constant, new, old); 813 + PIDFF_SET_ENVELOPE_IF_NEEDED(constant, new, old); 918 814 break; 919 815 920 816 case FF_PERIODIC: 921 - if (!old) { 922 - switch (effect->u.periodic.waveform) { 923 - case FF_SQUARE: 924 - type_id = PID_SQUARE; 925 - break; 926 - case FF_TRIANGLE: 927 - type_id = PID_TRIANGLE; 928 - break; 929 - case FF_SINE: 930 - type_id = PID_SINE; 931 - break; 932 - case FF_SAW_UP: 933 - type_id = PID_SAW_UP; 934 - break; 935 - case FF_SAW_DOWN: 936 - type_id = PID_SAW_DOWN; 937 - break; 938 - default: 939 - hid_err(pidff->hid, "invalid waveform\n"); 940 - return -EINVAL; 941 - } 942 - 943 - if (pidff->quirks & HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY) 944 - type_id = PID_SINE; 945 - 946 - error = pidff_request_effect_upload(pidff, 947 - pidff->type_id[type_id]); 948 - if (error) 949 - return error; 950 - } 951 - if (!old || pidff_needs_set_effect(effect, old)) 952 - pidff_set_effect_report(pidff, effect); 953 - if (!old || pidff_needs_set_periodic(effect, old)) 954 - pidff_set_periodic_report(pidff, effect); 955 - if (pidff_needs_set_envelope(&effect->u.periodic.envelope, 956 - old ? &old->u.periodic.envelope : NULL)) 957 - pidff_set_envelope_report(pidff, &effect->u.periodic.envelope); 817 + PIDFF_SET_REPORT_IF_NEEDED(periodic, new, old); 818 + PIDFF_SET_ENVELOPE_IF_NEEDED(periodic, new, old); 958 819 break; 959 820 960 821 case FF_RAMP: 961 - if (!old) { 962 - error = pidff_request_effect_upload(pidff, 963 - pidff->type_id[PID_RAMP]); 964 - if (error) 965 - return error; 966 - } 967 - if (!old || pidff_needs_set_effect(effect, old)) 968 - pidff_set_effect_report(pidff, effect); 969 - if (!old || pidff_needs_set_ramp(effect, old)) 970 - pidff_set_ramp_force_report(pidff, effect); 971 - if (pidff_needs_set_envelope(&effect->u.ramp.envelope, 972 - old ? &old->u.ramp.envelope : NULL)) 973 - pidff_set_envelope_report(pidff, &effect->u.ramp.envelope); 822 + PIDFF_SET_REPORT_IF_NEEDED(ramp, new, old); 823 + PIDFF_SET_ENVELOPE_IF_NEEDED(ramp, new, old); 974 824 break; 975 825 976 826 case FF_SPRING: 977 827 case FF_DAMPER: 978 828 case FF_INERTIA: 979 829 case FF_FRICTION: 980 - if (!old) { 981 - switch (effect->type) { 982 - case FF_SPRING: 983 - type_id = PID_SPRING; 984 - break; 985 - case FF_DAMPER: 986 - type_id = PID_DAMPER; 987 - break; 988 - case FF_INERTIA: 989 - type_id = PID_INERTIA; 990 - break; 991 - case FF_FRICTION: 992 - type_id = PID_FRICTION; 993 - break; 994 - } 995 - error = pidff_request_effect_upload(pidff, 996 - pidff->type_id[type_id]); 997 - if (error) 998 - return error; 999 - } 1000 - if (!old || pidff_needs_set_effect(effect, old)) 1001 - pidff_set_effect_report(pidff, effect); 1002 - if (!old || pidff_needs_set_condition(effect, old)) 1003 - pidff_set_condition_report(pidff, effect); 830 + PIDFF_SET_REPORT_IF_NEEDED(condition, new, old); 1004 831 break; 1005 - 1006 - default: 1007 - hid_err(pidff->hid, "invalid type\n"); 1008 - return -EINVAL; 1009 832 } 1010 - 1011 - if (!old) 1012 - pidff->pid_id[effect->id] = 1013 - pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 1014 - 1015 833 hid_dbg(pidff->hid, "uploaded\n"); 1016 - 1017 834 return 0; 1018 835 } 1019 836 ··· 995 924 pidff->set_effect[PID_START_DELAY].value[0] = 0; 996 925 997 926 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT], 998 - HID_REQ_SET_REPORT); 927 + HID_REQ_SET_REPORT); 999 928 } 1000 929 1001 930 /* ··· 1007 936 } 1008 937 1009 938 /* 939 + * Find specific usage in a given hid_field 940 + */ 941 + static int pidff_find_usage(struct hid_field *fld, unsigned int usage_code) 942 + { 943 + for (int i = 0; i < fld->maxusage; i++) { 944 + if (fld->usage[i].hid == usage_code) 945 + return i; 946 + } 947 + return -1; 948 + } 949 + 950 + /* 951 + * Find hid_field with a specific usage. Return the usage index as well 952 + */ 953 + static int pidff_find_field_with_usage(int *usage_index, 954 + struct hid_report *report, 955 + unsigned int usage_code) 956 + { 957 + for (int i = 0; i < report->maxfield; i++) { 958 + struct hid_field *fld = report->field[i]; 959 + 960 + if (fld->maxusage != fld->report_count) { 961 + pr_debug("maxusage and report_count do not match, skipping\n"); 962 + continue; 963 + } 964 + 965 + int index = pidff_find_usage(fld, usage_code); 966 + 967 + if (index >= 0) { 968 + *usage_index = index; 969 + return i; 970 + } 971 + } 972 + return -1; 973 + } 974 + 975 + /* 1010 976 * Find fields from a report and fill a pidff_usage 1011 977 */ 1012 978 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table, 1013 - struct hid_report *report, int count, int strict) 979 + struct hid_report *report, int count, int strict, 980 + u32 *quirks) 1014 981 { 982 + const u8 block_offset = pidff_set_condition[PID_PARAM_BLOCK_OFFSET]; 983 + const u8 delay = pidff_set_effect[PID_START_DELAY]; 984 + 1015 985 if (!report) { 1016 986 pr_debug("%s, null report\n", __func__); 1017 987 return -1; 1018 988 } 1019 989 1020 - int i, j, k, found; 1021 - int return_value = 0; 990 + for (int i = 0; i < count; i++) { 991 + int index; 992 + int found = pidff_find_field_with_usage(&index, report, 993 + HID_UP_PID | table[i]); 1022 994 1023 - for (k = 0; k < count; k++) { 1024 - found = 0; 1025 - for (i = 0; i < report->maxfield; i++) { 1026 - if (report->field[i]->maxusage != 1027 - report->field[i]->report_count) { 1028 - pr_debug("maxusage and report_count do not match, skipping\n"); 1029 - continue; 1030 - } 1031 - for (j = 0; j < report->field[i]->maxusage; j++) { 1032 - if (report->field[i]->usage[j].hid == 1033 - (HID_UP_PID | table[k])) { 1034 - pr_debug("found %d at %d->%d\n", 1035 - k, i, j); 1036 - usage[k].field = report->field[i]; 1037 - usage[k].value = 1038 - &report->field[i]->value[j]; 1039 - found = 1; 1040 - break; 1041 - } 1042 - } 1043 - if (found) 1044 - break; 995 + if (found >= 0) { 996 + pr_debug("found %d at %d->%d\n", i, found, index); 997 + usage[i].field = report->field[found]; 998 + usage[i].value = &report->field[found]->value[index]; 999 + continue; 1045 1000 } 1046 - if (!found && table[k] == pidff_set_effect[PID_START_DELAY]) { 1001 + 1002 + if (table[i] == delay) { 1047 1003 pr_debug("Delay field not found, but that's OK\n"); 1048 1004 pr_debug("Setting MISSING_DELAY quirk\n"); 1049 - return_value |= HID_PIDFF_QUIRK_MISSING_DELAY; 1050 - } else if (!found && table[k] == pidff_set_condition[PID_PARAM_BLOCK_OFFSET]) { 1005 + *quirks |= HID_PIDFF_QUIRK_MISSING_DELAY; 1006 + 1007 + } else if (table[i] == block_offset) { 1051 1008 pr_debug("PBO field not found, but that's OK\n"); 1052 1009 pr_debug("Setting MISSING_PBO quirk\n"); 1053 - return_value |= HID_PIDFF_QUIRK_MISSING_PBO; 1054 - } else if (!found && strict) { 1055 - pr_debug("failed to locate %d\n", k); 1010 + *quirks |= HID_PIDFF_QUIRK_MISSING_PBO; 1011 + 1012 + } else if (strict) { 1013 + pr_debug("failed to locate %d\n", i); 1056 1014 return -1; 1057 1015 } 1058 1016 } 1059 - return return_value; 1017 + return 0; 1060 1018 } 1061 1019 1062 1020 /* ··· 1095 995 { 1096 996 int i; 1097 997 1098 - for (i = 0; i < sizeof(pidff_reports); i++) 998 + for (i = 0; i < ARRAY_SIZE(pidff_reports); i++) 1099 999 if (usage == (HID_UP_PID | pidff_reports[i])) 1100 1000 return i; 1101 1001 ··· 1150 1050 */ 1151 1051 static int pidff_reports_ok(struct pidff_device *pidff) 1152 1052 { 1153 - int i; 1154 - 1155 - for (i = 0; i <= PID_REQUIRED_REPORTS; i++) { 1053 + for (int i = 0; i < PID_REQUIRED_REPORTS; i++) { 1156 1054 if (!pidff->reports[i]) { 1157 1055 hid_dbg(pidff->hid, "%d missing\n", i); 1158 1056 return 0; ··· 1171 1073 return NULL; 1172 1074 } 1173 1075 1174 - int i; 1175 - 1176 - for (i = 0; i < report->maxfield; i++) { 1076 + for (int i = 0; i < report->maxfield; i++) { 1177 1077 if (report->field[i]->logical == (HID_UP_PID | usage) && 1178 1078 report->field[i]->report_count > 0) { 1179 1079 if (!enforce_min || ··· 1189 1093 * Fill a pidff->*_id struct table 1190 1094 */ 1191 1095 static int pidff_find_special_keys(int *keys, struct hid_field *fld, 1192 - const u8 *usagetable, int count) 1096 + const u8 *usagetable, int count, 1097 + unsigned int usage_page) 1193 1098 { 1194 - 1195 - int i, j; 1196 1099 int found = 0; 1197 1100 1198 - for (i = 0; i < count; i++) { 1199 - for (j = 0; j < fld->maxusage; j++) { 1200 - if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) { 1201 - keys[i] = j + 1; 1202 - found++; 1203 - break; 1204 - } 1205 - } 1101 + if (!fld) 1102 + return 0; 1103 + 1104 + for (int i = 0; i < count; i++) { 1105 + keys[i] = pidff_find_usage(fld, usage_page | usagetable[i]) + 1; 1106 + if (keys[i]) 1107 + found++; 1206 1108 } 1207 1109 return found; 1208 1110 } 1209 1111 1210 1112 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \ 1211 1113 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \ 1212 - sizeof(pidff_ ## name)) 1114 + ARRAY_SIZE(pidff_ ## name), HID_UP_PID) 1115 + 1116 + #define PIDFF_FIND_GENERAL_DESKTOP(keys, field, name) \ 1117 + pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \ 1118 + ARRAY_SIZE(pidff_ ## name), HID_UP_GENDESK) 1213 1119 1214 1120 /* 1215 1121 * Find and check the special fields ··· 1226 1128 pidff->set_effect_type = 1227 1129 pidff_find_special_field(pidff->reports[PID_SET_EFFECT], 1228 1130 PID_EFFECT_TYPE, 1); 1131 + pidff->axes_enable = 1132 + pidff_find_special_field(pidff->reports[PID_SET_EFFECT], 1133 + PID_AXES_ENABLE, 0); 1229 1134 pidff->effect_direction = 1230 1135 pidff_find_special_field(pidff->reports[PID_SET_EFFECT], 1231 1136 PID_DIRECTION, 0); 1232 1137 pidff->device_control = 1233 1138 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL], 1234 - PID_DEVICE_CONTROL_ARRAY, 1235 - !(pidff->quirks & HID_PIDFF_QUIRK_PERMISSIVE_CONTROL)); 1139 + PID_DEVICE_CONTROL_ARRAY, 1); 1140 + 1141 + /* Detect and set permissive control quirk */ 1142 + if (!pidff->device_control) { 1143 + pr_debug("Setting PERMISSIVE_CONTROL quirk\n"); 1144 + pidff->quirks |= HID_PIDFF_QUIRK_PERMISSIVE_CONTROL; 1145 + pidff->device_control = pidff_find_special_field( 1146 + pidff->reports[PID_DEVICE_CONTROL], 1147 + PID_DEVICE_CONTROL_ARRAY, 0); 1148 + } 1236 1149 1237 1150 pidff->block_load_status = 1238 1151 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD], ··· 1289 1180 1290 1181 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status, 1291 1182 block_load_status) != 1292 - sizeof(pidff_block_load_status)) { 1183 + ARRAY_SIZE(pidff_block_load_status)) { 1293 1184 hid_err(pidff->hid, 1294 1185 "block load status identifiers not found\n"); 1295 1186 return -1; ··· 1297 1188 1298 1189 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status, 1299 1190 effect_operation_status) != 1300 - sizeof(pidff_effect_operation_status)) { 1191 + ARRAY_SIZE(pidff_effect_operation_status)) { 1301 1192 hid_err(pidff->hid, "effect operation identifiers not found\n"); 1302 1193 return -1; 1303 1194 } 1195 + 1196 + if (!pidff->axes_enable) { 1197 + hid_info(pidff->hid, "axes enable field not found!\n"); 1198 + return 0; 1199 + } 1200 + 1201 + hid_dbg(pidff->hid, "axes enable report count: %u\n", 1202 + pidff->axes_enable->report_count); 1203 + 1204 + uint found = PIDFF_FIND_GENERAL_DESKTOP(direction_axis_id, axes_enable, 1205 + direction_axis); 1206 + 1207 + pidff->axis_count = found; 1208 + hid_dbg(pidff->hid, "found direction axes: %u", found); 1209 + 1210 + for (int i = 0; i < ARRAY_SIZE(pidff_direction_axis); i++) { 1211 + if (!pidff->direction_axis_id[i]) 1212 + continue; 1213 + 1214 + hid_dbg(pidff->hid, "axis %d, usage: 0x%04x, index: %d", i + 1, 1215 + pidff_direction_axis[i], pidff->direction_axis_id[i]); 1216 + } 1217 + 1218 + if (pidff->axes_enable && found != pidff->axes_enable->report_count) 1219 + hid_warn(pidff->hid, "axes_enable: %u != direction axes: %u", 1220 + pidff->axes_enable->report_count, found); 1304 1221 1305 1222 return 0; 1306 1223 } ··· 1339 1204 { 1340 1205 int i; 1341 1206 1342 - for (i = 0; i < sizeof(pidff_effect_types); i++) { 1207 + for (i = 0; i < ARRAY_SIZE(pidff_effect_types); i++) { 1343 1208 int pidff_type = pidff->type_id[i]; 1344 1209 1345 1210 if (pidff->set_effect_type->usage[pidff_type].hid != ··· 1389 1254 #define PIDFF_FIND_FIELDS(name, report, strict) \ 1390 1255 pidff_find_fields(pidff->name, pidff_ ## name, \ 1391 1256 pidff->reports[report], \ 1392 - sizeof(pidff_ ## name), strict) 1257 + ARRAY_SIZE(pidff_ ## name), strict, &pidff->quirks) 1393 1258 1394 1259 /* 1395 1260 * Fill and check the pidff_usages 1396 1261 */ 1397 1262 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev) 1398 1263 { 1399 - int status = 0; 1400 - 1401 - /* Save info about the device not having the DELAY ffb field. */ 1402 - status = PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1); 1403 - if (status == -1) { 1264 + if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) { 1404 1265 hid_err(pidff->hid, "unknown set_effect report layout\n"); 1405 1266 return -ENODEV; 1406 1267 } 1407 - pidff->quirks |= status; 1408 - 1409 - if (status & HID_PIDFF_QUIRK_MISSING_DELAY) 1410 - hid_dbg(pidff->hid, "Adding MISSING_DELAY quirk\n"); 1411 - 1412 1268 1413 1269 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0); 1414 1270 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) { ··· 1433 1307 "has periodic effect but no envelope\n"); 1434 1308 } 1435 1309 1436 - if (test_bit(FF_CONSTANT, dev->ffbit) && 1437 - PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) { 1310 + if (PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1) && 1311 + test_and_clear_bit(FF_CONSTANT, dev->ffbit)) 1438 1312 hid_warn(pidff->hid, "unknown constant effect layout\n"); 1439 - clear_bit(FF_CONSTANT, dev->ffbit); 1440 - } 1441 1313 1442 - if (test_bit(FF_RAMP, dev->ffbit) && 1443 - PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) { 1314 + if (PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1) && 1315 + test_and_clear_bit(FF_RAMP, dev->ffbit)) 1444 1316 hid_warn(pidff->hid, "unknown ramp effect layout\n"); 1445 - clear_bit(FF_RAMP, dev->ffbit); 1446 - } 1447 1317 1448 - if (test_bit(FF_SPRING, dev->ffbit) || 1449 - test_bit(FF_DAMPER, dev->ffbit) || 1450 - test_bit(FF_FRICTION, dev->ffbit) || 1451 - test_bit(FF_INERTIA, dev->ffbit)) { 1452 - status = PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1); 1453 - 1454 - if (status < 0) { 1318 + if (PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) { 1319 + if (test_and_clear_bit(FF_SPRING, dev->ffbit) || 1320 + test_and_clear_bit(FF_DAMPER, dev->ffbit) || 1321 + test_and_clear_bit(FF_FRICTION, dev->ffbit) || 1322 + test_and_clear_bit(FF_INERTIA, dev->ffbit)) 1455 1323 hid_warn(pidff->hid, "unknown condition effect layout\n"); 1456 - clear_bit(FF_SPRING, dev->ffbit); 1457 - clear_bit(FF_DAMPER, dev->ffbit); 1458 - clear_bit(FF_FRICTION, dev->ffbit); 1459 - clear_bit(FF_INERTIA, dev->ffbit); 1460 - } 1461 - pidff->quirks |= status; 1462 1324 } 1463 1325 1464 - if (test_bit(FF_PERIODIC, dev->ffbit) && 1465 - PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) { 1326 + if (PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1) && 1327 + test_and_clear_bit(FF_PERIODIC, dev->ffbit)) 1466 1328 hid_warn(pidff->hid, "unknown periodic effect layout\n"); 1467 - clear_bit(FF_PERIODIC, dev->ffbit); 1468 - } 1469 1329 1470 1330 PIDFF_FIND_FIELDS(pool, PID_POOL, 0); 1471 1331 ··· 1504 1392 int hid_pidff_init_with_quirks(struct hid_device *hid, u32 initial_quirks) 1505 1393 { 1506 1394 struct pidff_device *pidff; 1507 - struct hid_input *hidinput = list_entry(hid->inputs.next, 1508 - struct hid_input, list); 1395 + struct hid_input *hidinput = 1396 + list_entry(hid->inputs.next, struct hid_input, list); 1509 1397 struct input_dev *dev = hidinput->input; 1510 1398 struct ff_device *ff; 1511 1399 int max_effects; ··· 1585 1473 ff->set_autocenter = pidff_set_autocenter; 1586 1474 ff->playback = pidff_playback; 1587 1475 1588 - hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula <anssi.hannula@gmail.com>\n"); 1589 - hid_dbg(dev, "Active quirks mask: 0x%x\n", pidff->quirks); 1476 + hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula\n"); 1477 + hid_dbg(dev, "Active quirks mask: 0x%08x\n", pidff->quirks); 1590 1478 1591 1479 hid_device_io_stop(hid); 1592 1480 1593 1481 return 0; 1594 1482 1595 - fail: 1483 + fail: 1596 1484 hid_device_io_stop(hid); 1597 1485 1598 1486 kfree(pidff);
+1 -1
drivers/hid/usbhid/hid-pidff.h
··· 16 16 #define HID_PIDFF_QUIRK_PERMISSIVE_CONTROL BIT(2) 17 17 18 18 /* Use fixed 0x4000 direction during SET_EFFECT report upload */ 19 - #define HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION BIT(3) 19 + #define HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION BIT(3) 20 20 21 21 /* Force all periodic effects to be uploaded as SINE */ 22 22 #define HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY BIT(4)
+10 -4
drivers/input/input-mt.c
··· 198 198 struct input_mt *mt = dev->mt; 199 199 struct input_mt_slot *oldest; 200 200 int oldid, count, i; 201 + int p, reported_p = 0; 201 202 202 203 if (!mt) 203 204 return; ··· 216 215 if ((id - oldid) & TRKID_SGN) { 217 216 oldest = ps; 218 217 oldid = id; 218 + } 219 + if (test_bit(ABS_MT_PRESSURE, dev->absbit)) { 220 + p = input_mt_get_value(ps, ABS_MT_PRESSURE); 221 + if (mt->flags & INPUT_MT_TOTAL_FORCE) 222 + reported_p += p; 223 + else if (oldid == id) 224 + reported_p = p; 219 225 } 220 226 count++; 221 227 } ··· 253 245 input_event(dev, EV_ABS, ABS_X, x); 254 246 input_event(dev, EV_ABS, ABS_Y, y); 255 247 256 - if (test_bit(ABS_MT_PRESSURE, dev->absbit)) { 257 - int p = input_mt_get_value(oldest, ABS_MT_PRESSURE); 258 - input_event(dev, EV_ABS, ABS_PRESSURE, p); 259 - } 248 + if (test_bit(ABS_MT_PRESSURE, dev->absbit)) 249 + input_event(dev, EV_ABS, ABS_PRESSURE, reported_p); 260 250 } else { 261 251 if (test_bit(ABS_MT_PRESSURE, dev->absbit)) 262 252 input_event(dev, EV_ABS, ABS_PRESSURE, 0);
+30 -1
include/linux/hid.h
··· 156 156 #define HID_UP_TELEPHONY 0x000b0000 157 157 #define HID_UP_CONSUMER 0x000c0000 158 158 #define HID_UP_DIGITIZER 0x000d0000 159 + #define HID_UP_HAPTIC 0x000e0000 159 160 #define HID_UP_PID 0x000f0000 160 161 #define HID_UP_BATTERY 0x00850000 161 162 #define HID_UP_CAMERA 0x00900000 ··· 317 316 #define HID_DG_TOOLSERIALNUMBER 0x000d005b 318 317 #define HID_DG_LATENCYMODE 0x000d0060 319 318 319 + #define HID_HP_SIMPLECONTROLLER 0x000e0001 320 + #define HID_HP_WAVEFORMLIST 0x000e0010 321 + #define HID_HP_DURATIONLIST 0x000e0011 322 + #define HID_HP_AUTOTRIGGER 0x000e0020 323 + #define HID_HP_MANUALTRIGGER 0x000e0021 324 + #define HID_HP_AUTOTRIGGERASSOCIATEDCONTROL 0x000e0022 325 + #define HID_HP_INTENSITY 0x000e0023 326 + #define HID_HP_REPEATCOUNT 0x000e0024 327 + #define HID_HP_RETRIGGERPERIOD 0x000e0025 328 + #define HID_HP_WAVEFORMVENDORPAGE 0x000e0026 329 + #define HID_HP_WAVEFORMVENDORID 0x000e0027 330 + #define HID_HP_WAVEFORMCUTOFFTIME 0x000e0028 331 + #define HID_HP_WAVEFORMNONE 0x000e1001 332 + #define HID_HP_WAVEFORMSTOP 0x000e1002 333 + #define HID_HP_WAVEFORMCLICK 0x000e1003 334 + #define HID_HP_WAVEFORMBUZZCONTINUOUS 0x000e1004 335 + #define HID_HP_WAVEFORMRUMBLECONTINUOUS 0x000e1005 336 + #define HID_HP_WAVEFORMPRESS 0x000e1006 337 + #define HID_HP_WAVEFORMRELEASE 0x000e1007 338 + #define HID_HP_VENDORWAVEFORMMIN 0x000e2001 339 + #define HID_HP_VENDORWAVEFORMMAX 0x000e2fff 340 + 320 341 #define HID_BAT_ABSOLUTESTATEOFCHARGE 0x00850065 321 342 #define HID_BAT_CHARGING 0x00850044 322 343 ··· 447 424 */ 448 425 #define HID_REPORT_PROTOCOL 1 449 426 #define HID_BOOT_PROTOCOL 0 427 + 428 + /* 429 + * HID units 430 + */ 431 + #define HID_UNIT_GRAM 0x0101 432 + #define HID_UNIT_NEWTON 0xe111 450 433 451 434 /* 452 435 * This is the global environment of the parser. This information is ··· 847 818 * zero from them. 848 819 */ 849 820 struct hid_driver { 850 - char *name; 821 + const char *name; 851 822 const struct hid_device_id *id_table; 852 823 853 824 struct list_head dyn_list;
+1
include/linux/input/mt.h
··· 17 17 #define INPUT_MT_DROP_UNUSED 0x0004 /* drop contacts not seen in frame */ 18 18 #define INPUT_MT_TRACK 0x0008 /* use in-kernel tracking */ 19 19 #define INPUT_MT_SEMI_MT 0x0010 /* semi-mt device, finger count handled manually */ 20 + #define INPUT_MT_TOTAL_FORCE 0x0020 /* calculate total force from slots pressure */ 20 21 21 22 /** 22 23 * struct input_mt_slot - represents the state of an input MT slot
+2
include/uapi/linux/hidraw.h
··· 48 48 #define HIDIOCGOUTPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0C, len) 49 49 #define HIDIOCREVOKE _IOW('H', 0x0D, int) /* Revoke device access */ 50 50 51 + #define HIDIOCTL_LAST _IOC_NR(HIDIOCREVOKE) 52 + 51 53 #define HIDRAW_FIRST_MINOR 0 52 54 #define HIDRAW_MAX_DEVICES 64 53 55 /* number of reports to buffer */
+1
include/uapi/linux/input-event-codes.h
··· 27 27 #define INPUT_PROP_TOPBUTTONPAD 0x04 /* softbuttons at top of pad */ 28 28 #define INPUT_PROP_POINTING_STICK 0x05 /* is a pointing stick */ 29 29 #define INPUT_PROP_ACCELEROMETER 0x06 /* has accelerometer */ 30 + #define INPUT_PROP_HAPTIC_TOUCHPAD 0x07 /* is a haptic touchpad */ 30 31 31 32 #define INPUT_PROP_MAX 0x1f 32 33 #define INPUT_PROP_CNT (INPUT_PROP_MAX + 1)
+21 -1
include/uapi/linux/input.h
··· 430 430 }; 431 431 432 432 /** 433 + * struct ff_haptic_effect 434 + * @hid_usage: hid_usage according to Haptics page (WAVEFORM_CLICK, etc.) 435 + * @vendor_id: the waveform vendor ID if hid_usage is in the vendor-defined range 436 + * @vendor_waveform_page: the vendor waveform page if hid_usage is in the vendor-defined range 437 + * @intensity: strength of the effect as percentage 438 + * @repeat_count: number of times to retrigger effect 439 + * @retrigger_period: time before effect is retriggered (in ms) 440 + */ 441 + struct ff_haptic_effect { 442 + __u16 hid_usage; 443 + __u16 vendor_id; 444 + __u8 vendor_waveform_page; 445 + __u16 intensity; 446 + __u16 repeat_count; 447 + __u16 retrigger_period; 448 + }; 449 + 450 + /** 433 451 * struct ff_effect - defines force feedback effect 434 452 * @type: type of the effect (FF_CONSTANT, FF_PERIODIC, FF_RAMP, FF_SPRING, 435 453 * FF_FRICTION, FF_DAMPER, FF_RUMBLE, FF_INERTIA, or FF_CUSTOM) ··· 483 465 struct ff_periodic_effect periodic; 484 466 struct ff_condition_effect condition[2]; /* One for each axis */ 485 467 struct ff_rumble_effect rumble; 468 + struct ff_haptic_effect haptic; 486 469 } u; 487 470 }; 488 471 ··· 491 472 * Force feedback effect types 492 473 */ 493 474 475 + #define FF_HAPTIC 0x4f 494 476 #define FF_RUMBLE 0x50 495 477 #define FF_PERIODIC 0x51 496 478 #define FF_CONSTANT 0x52 ··· 501 481 #define FF_INERTIA 0x56 502 482 #define FF_RAMP 0x57 503 483 504 - #define FF_EFFECT_MIN FF_RUMBLE 484 + #define FF_EFFECT_MIN FF_HAPTIC 505 485 #define FF_EFFECT_MAX FF_RAMP 506 486 507 487 /*
+6
tools/testing/selftests/hid/hid_common.h
··· 230 230 break; 231 231 case UHID_SET_REPORT: 232 232 UHID_LOG("UHID_SET_REPORT from uhid-dev"); 233 + 234 + answer.type = UHID_SET_REPORT_REPLY; 235 + answer.u.set_report_reply.id = ev.u.set_report.id; 236 + answer.u.set_report_reply.err = 0; /* success */ 237 + 238 + uhid_write(_metadata, fd, &answer); 233 239 break; 234 240 default: 235 241 TH_LOG("Invalid event from uhid-dev: %u", ev.type);
+473
tools/testing/selftests/hid/hidraw.c
··· 2 2 /* Copyright (c) 2022-2024 Red Hat */ 3 3 4 4 #include "hid_common.h" 5 + #include <linux/input.h> 6 + #include <string.h> 7 + #include <sys/ioctl.h> 5 8 6 9 /* for older kernels */ 7 10 #ifndef HIDIOCREVOKE ··· 216 213 errno); 217 214 218 215 pthread_mutex_unlock(&uhid_output_mtx); 216 + } 217 + 218 + /* 219 + * Test HIDIOCGRDESCSIZE ioctl to get report descriptor size 220 + */ 221 + TEST_F(hidraw, ioctl_rdescsize) 222 + { 223 + int desc_size = 0; 224 + int err; 225 + 226 + /* call HIDIOCGRDESCSIZE ioctl */ 227 + err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size); 228 + ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRDESCSIZE ioctl failed"); 229 + 230 + /* verify the size matches our test report descriptor */ 231 + ASSERT_EQ(desc_size, sizeof(rdesc)) 232 + TH_LOG("expected size %zu, got %d", sizeof(rdesc), desc_size); 233 + } 234 + 235 + /* 236 + * Test HIDIOCGRDESC ioctl to get report descriptor data 237 + */ 238 + TEST_F(hidraw, ioctl_rdesc) 239 + { 240 + struct hidraw_report_descriptor desc; 241 + int err; 242 + 243 + /* get the full report descriptor */ 244 + desc.size = sizeof(rdesc); 245 + err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &desc); 246 + ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRDESC ioctl failed"); 247 + 248 + /* verify the descriptor data matches our test descriptor */ 249 + ASSERT_EQ(memcmp(desc.value, rdesc, sizeof(rdesc)), 0) 250 + TH_LOG("report descriptor data mismatch"); 251 + } 252 + 253 + /* 254 + * Test HIDIOCGRDESC ioctl with smaller buffer size 255 + */ 256 + TEST_F(hidraw, ioctl_rdesc_small_buffer) 257 + { 258 + struct hidraw_report_descriptor desc; 259 + int err; 260 + size_t small_size = sizeof(rdesc) / 2; /* request half the descriptor size */ 261 + 262 + /* get partial report descriptor */ 263 + desc.size = small_size; 264 + err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &desc); 265 + ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRDESC ioctl failed with small buffer"); 266 + 267 + /* verify we got the first part of the descriptor */ 268 + ASSERT_EQ(memcmp(desc.value, rdesc, small_size), 0) 269 + TH_LOG("partial report descriptor data mismatch"); 270 + } 271 + 272 + /* 273 + * Test HIDIOCGRAWINFO ioctl to get device information 274 + */ 275 + TEST_F(hidraw, ioctl_rawinfo) 276 + { 277 + struct hidraw_devinfo devinfo; 278 + int err; 279 + 280 + /* get device info */ 281 + err = ioctl(self->hidraw_fd, HIDIOCGRAWINFO, &devinfo); 282 + ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRAWINFO ioctl failed"); 283 + 284 + /* verify device info matches our test setup */ 285 + ASSERT_EQ(devinfo.bustype, BUS_USB) 286 + TH_LOG("expected bustype 0x03, got 0x%x", devinfo.bustype); 287 + ASSERT_EQ(devinfo.vendor, 0x0001) 288 + TH_LOG("expected vendor 0x0001, got 0x%x", devinfo.vendor); 289 + ASSERT_EQ(devinfo.product, 0x0a37) 290 + TH_LOG("expected product 0x0a37, got 0x%x", devinfo.product); 291 + } 292 + 293 + /* 294 + * Test HIDIOCGFEATURE ioctl to get feature report 295 + */ 296 + TEST_F(hidraw, ioctl_gfeature) 297 + { 298 + __u8 buf[10] = {0}; 299 + int err; 300 + 301 + /* set report ID 1 in first byte */ 302 + buf[0] = 1; 303 + 304 + /* get feature report */ 305 + err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf); 306 + ASSERT_EQ(err, sizeof(feature_data)) TH_LOG("HIDIOCGFEATURE ioctl failed, got %d", err); 307 + 308 + /* verify we got the expected feature data */ 309 + ASSERT_EQ(buf[0], feature_data[0]) 310 + TH_LOG("expected feature_data[0] = %d, got %d", feature_data[0], buf[0]); 311 + ASSERT_EQ(buf[1], feature_data[1]) 312 + TH_LOG("expected feature_data[1] = %d, got %d", feature_data[1], buf[1]); 313 + } 314 + 315 + /* 316 + * Test HIDIOCGFEATURE ioctl with invalid report ID 317 + */ 318 + TEST_F(hidraw, ioctl_gfeature_invalid) 319 + { 320 + __u8 buf[10] = {0}; 321 + int err; 322 + 323 + /* set invalid report ID (not 1) */ 324 + buf[0] = 2; 325 + 326 + /* try to get feature report */ 327 + err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf); 328 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGFEATURE should have failed with invalid report ID"); 329 + ASSERT_EQ(errno, EIO) TH_LOG("expected EIO, got errno %d", errno); 330 + } 331 + 332 + /* 333 + * Test ioctl with incorrect nr bits 334 + */ 335 + TEST_F(hidraw, ioctl_invalid_nr) 336 + { 337 + char buf[256] = {0}; 338 + int err; 339 + unsigned int bad_cmd; 340 + 341 + /* 342 + * craft an ioctl command with wrong _IOC_NR bits 343 + */ 344 + bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x00, sizeof(buf)); /* 0 is not valid */ 345 + 346 + /* test the ioctl */ 347 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 348 + ASSERT_LT(err, 0) TH_LOG("ioctl read-write with wrong _IOC_NR (0) should have failed"); 349 + ASSERT_EQ(errno, ENOTTY) 350 + TH_LOG("expected ENOTTY for wrong read-write _IOC_NR (0), got errno %d", errno); 351 + 352 + /* 353 + * craft an ioctl command with wrong _IOC_NR bits 354 + */ 355 + bad_cmd = _IOC(_IOC_READ, 'H', 0x00, sizeof(buf)); /* 0 is not valid */ 356 + 357 + /* test the ioctl */ 358 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 359 + ASSERT_LT(err, 0) TH_LOG("ioctl read-only with wrong _IOC_NR (0) should have failed"); 360 + ASSERT_EQ(errno, ENOTTY) 361 + TH_LOG("expected ENOTTY for wrong read-only _IOC_NR (0), got errno %d", errno); 362 + 363 + /* also test with bigger number */ 364 + bad_cmd = _IOC(_IOC_READ, 'H', 0x42, sizeof(buf)); /* 0x42 is not valid as well */ 365 + 366 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 367 + ASSERT_LT(err, 0) TH_LOG("ioctl read-only with wrong _IOC_NR (0x42) should have failed"); 368 + ASSERT_EQ(errno, ENOTTY) 369 + TH_LOG("expected ENOTTY for wrong read-only _IOC_NR (0x42), got errno %d", errno); 370 + 371 + /* also test with bigger number: 0x42 is not valid as well */ 372 + bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x42, sizeof(buf)); 373 + 374 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 375 + ASSERT_LT(err, 0) TH_LOG("ioctl read-write with wrong _IOC_NR (0x42) should have failed"); 376 + ASSERT_EQ(errno, ENOTTY) 377 + TH_LOG("expected ENOTTY for wrong read-write _IOC_NR (0x42), got errno %d", errno); 378 + } 379 + 380 + /* 381 + * Test ioctl with incorrect type bits 382 + */ 383 + TEST_F(hidraw, ioctl_invalid_type) 384 + { 385 + char buf[256] = {0}; 386 + int err; 387 + unsigned int bad_cmd; 388 + 389 + /* 390 + * craft an ioctl command with wrong _IOC_TYPE bits 391 + */ 392 + bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'I', 0x01, sizeof(buf)); /* 'I' should be 'H' */ 393 + 394 + /* test the ioctl */ 395 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 396 + ASSERT_LT(err, 0) TH_LOG("ioctl with wrong _IOC_TYPE (I) should have failed"); 397 + ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_NR, got errno %d", errno); 398 + } 399 + 400 + /* 401 + * Test HIDIOCGFEATURE ioctl with incorrect _IOC_DIR bits 402 + */ 403 + TEST_F(hidraw, ioctl_gfeature_invalid_dir) 404 + { 405 + __u8 buf[10] = {0}; 406 + int err; 407 + unsigned int bad_cmd; 408 + 409 + /* set report ID 1 in first byte */ 410 + buf[0] = 1; 411 + 412 + /* 413 + * craft an ioctl command with wrong _IOC_DIR bits 414 + * HIDIOCGFEATURE should have _IOC_WRITE|_IOC_READ, let's use only _IOC_WRITE 415 + */ 416 + bad_cmd = _IOC(_IOC_WRITE, 'H', 0x07, sizeof(buf)); /* should be _IOC_WRITE|_IOC_READ */ 417 + 418 + /* try to get feature report with wrong direction bits */ 419 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 420 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGFEATURE with wrong _IOC_DIR should have failed"); 421 + ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno); 422 + 423 + /* also test with only _IOC_READ */ 424 + bad_cmd = _IOC(_IOC_READ, 'H', 0x07, sizeof(buf)); /* should be _IOC_WRITE|_IOC_READ */ 425 + 426 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 427 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGFEATURE with wrong _IOC_DIR should have failed"); 428 + ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno); 429 + } 430 + 431 + /* 432 + * Test read-only ioctl with incorrect _IOC_DIR bits 433 + */ 434 + TEST_F(hidraw, ioctl_readonly_invalid_dir) 435 + { 436 + char buf[256] = {0}; 437 + int err; 438 + unsigned int bad_cmd; 439 + 440 + /* 441 + * craft an ioctl command with wrong _IOC_DIR bits 442 + * HIDIOCGRAWNAME should have _IOC_READ, let's use _IOC_WRITE 443 + */ 444 + bad_cmd = _IOC(_IOC_WRITE, 'H', 0x04, sizeof(buf)); /* should be _IOC_READ */ 445 + 446 + /* try to get device name with wrong direction bits */ 447 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 448 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGRAWNAME with wrong _IOC_DIR should have failed"); 449 + ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno); 450 + 451 + /* also test with _IOC_WRITE|_IOC_READ */ 452 + bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x04, sizeof(buf)); /* should be only _IOC_READ */ 453 + 454 + err = ioctl(self->hidraw_fd, bad_cmd, buf); 455 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGRAWNAME with wrong _IOC_DIR should have failed"); 456 + ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno); 457 + } 458 + 459 + /* 460 + * Test HIDIOCSFEATURE ioctl to set feature report 461 + */ 462 + TEST_F(hidraw, ioctl_sfeature) 463 + { 464 + __u8 buf[10] = {0}; 465 + int err; 466 + 467 + /* prepare feature report data */ 468 + buf[0] = 1; /* report ID */ 469 + buf[1] = 0x42; 470 + buf[2] = 0x24; 471 + 472 + /* set feature report */ 473 + err = ioctl(self->hidraw_fd, HIDIOCSFEATURE(3), buf); 474 + ASSERT_EQ(err, 3) TH_LOG("HIDIOCSFEATURE ioctl failed, got %d", err); 475 + 476 + /* 477 + * Note: The uhid mock doesn't validate the set report data, 478 + * so we just verify the ioctl succeeds 479 + */ 480 + } 481 + 482 + /* 483 + * Test HIDIOCGINPUT ioctl to get input report 484 + */ 485 + TEST_F(hidraw, ioctl_ginput) 486 + { 487 + __u8 buf[10] = {0}; 488 + int err; 489 + 490 + /* set report ID 1 in first byte */ 491 + buf[0] = 1; 492 + 493 + /* get input report */ 494 + err = ioctl(self->hidraw_fd, HIDIOCGINPUT(sizeof(buf)), buf); 495 + ASSERT_EQ(err, sizeof(feature_data)) TH_LOG("HIDIOCGINPUT ioctl failed, got %d", err); 496 + 497 + /* verify we got the expected input data */ 498 + ASSERT_EQ(buf[0], feature_data[0]) 499 + TH_LOG("expected feature_data[0] = %d, got %d", feature_data[0], buf[0]); 500 + ASSERT_EQ(buf[1], feature_data[1]) 501 + TH_LOG("expected feature_data[1] = %d, got %d", feature_data[1], buf[1]); 502 + } 503 + 504 + /* 505 + * Test HIDIOCGINPUT ioctl with invalid report ID 506 + */ 507 + TEST_F(hidraw, ioctl_ginput_invalid) 508 + { 509 + __u8 buf[10] = {0}; 510 + int err; 511 + 512 + /* set invalid report ID (not 1) */ 513 + buf[0] = 2; 514 + 515 + /* try to get input report */ 516 + err = ioctl(self->hidraw_fd, HIDIOCGINPUT(sizeof(buf)), buf); 517 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGINPUT should have failed with invalid report ID"); 518 + ASSERT_EQ(errno, EIO) TH_LOG("expected EIO, got errno %d", errno); 519 + } 520 + 521 + /* 522 + * Test HIDIOCSINPUT ioctl to set input report 523 + */ 524 + TEST_F(hidraw, ioctl_sinput) 525 + { 526 + __u8 buf[10] = {0}; 527 + int err; 528 + 529 + /* prepare input report data */ 530 + buf[0] = 1; /* report ID */ 531 + buf[1] = 0x55; 532 + buf[2] = 0xAA; 533 + 534 + /* set input report */ 535 + err = ioctl(self->hidraw_fd, HIDIOCSINPUT(3), buf); 536 + ASSERT_EQ(err, 3) TH_LOG("HIDIOCSINPUT ioctl failed, got %d", err); 537 + 538 + /* 539 + * Note: The uhid mock doesn't validate the set report data, 540 + * so we just verify the ioctl succeeds 541 + */ 542 + } 543 + 544 + /* 545 + * Test HIDIOCGOUTPUT ioctl to get output report 546 + */ 547 + TEST_F(hidraw, ioctl_goutput) 548 + { 549 + __u8 buf[10] = {0}; 550 + int err; 551 + 552 + /* set report ID 1 in first byte */ 553 + buf[0] = 1; 554 + 555 + /* get output report */ 556 + err = ioctl(self->hidraw_fd, HIDIOCGOUTPUT(sizeof(buf)), buf); 557 + ASSERT_EQ(err, sizeof(feature_data)) TH_LOG("HIDIOCGOUTPUT ioctl failed, got %d", err); 558 + 559 + /* verify we got the expected output data */ 560 + ASSERT_EQ(buf[0], feature_data[0]) 561 + TH_LOG("expected feature_data[0] = %d, got %d", feature_data[0], buf[0]); 562 + ASSERT_EQ(buf[1], feature_data[1]) 563 + TH_LOG("expected feature_data[1] = %d, got %d", feature_data[1], buf[1]); 564 + } 565 + 566 + /* 567 + * Test HIDIOCGOUTPUT ioctl with invalid report ID 568 + */ 569 + TEST_F(hidraw, ioctl_goutput_invalid) 570 + { 571 + __u8 buf[10] = {0}; 572 + int err; 573 + 574 + /* set invalid report ID (not 1) */ 575 + buf[0] = 2; 576 + 577 + /* try to get output report */ 578 + err = ioctl(self->hidraw_fd, HIDIOCGOUTPUT(sizeof(buf)), buf); 579 + ASSERT_LT(err, 0) TH_LOG("HIDIOCGOUTPUT should have failed with invalid report ID"); 580 + ASSERT_EQ(errno, EIO) TH_LOG("expected EIO, got errno %d", errno); 581 + } 582 + 583 + /* 584 + * Test HIDIOCSOUTPUT ioctl to set output report 585 + */ 586 + TEST_F(hidraw, ioctl_soutput) 587 + { 588 + __u8 buf[10] = {0}; 589 + int err; 590 + 591 + /* prepare output report data */ 592 + buf[0] = 1; /* report ID */ 593 + buf[1] = 0x33; 594 + buf[2] = 0xCC; 595 + 596 + /* set output report */ 597 + err = ioctl(self->hidraw_fd, HIDIOCSOUTPUT(3), buf); 598 + ASSERT_EQ(err, 3) TH_LOG("HIDIOCSOUTPUT ioctl failed, got %d", err); 599 + 600 + /* 601 + * Note: The uhid mock doesn't validate the set report data, 602 + * so we just verify the ioctl succeeds 603 + */ 604 + } 605 + 606 + /* 607 + * Test HIDIOCGRAWNAME ioctl to get device name string 608 + */ 609 + TEST_F(hidraw, ioctl_rawname) 610 + { 611 + char name[256] = {0}; 612 + char expected_name[64]; 613 + int err; 614 + 615 + /* get device name */ 616 + err = ioctl(self->hidraw_fd, HIDIOCGRAWNAME(sizeof(name)), name); 617 + ASSERT_GT(err, 0) TH_LOG("HIDIOCGRAWNAME ioctl failed, got %d", err); 618 + 619 + /* construct expected name based on device id */ 620 + snprintf(expected_name, sizeof(expected_name), "test-uhid-device-%d", self->hid.dev_id); 621 + 622 + /* verify the name matches expected pattern */ 623 + ASSERT_EQ(strcmp(name, expected_name), 0) 624 + TH_LOG("expected name '%s', got '%s'", expected_name, name); 625 + } 626 + 627 + /* 628 + * Test HIDIOCGRAWPHYS ioctl to get device physical address string 629 + */ 630 + TEST_F(hidraw, ioctl_rawphys) 631 + { 632 + char phys[256] = {0}; 633 + char expected_phys[64]; 634 + int err; 635 + 636 + /* get device physical address */ 637 + err = ioctl(self->hidraw_fd, HIDIOCGRAWPHYS(sizeof(phys)), phys); 638 + ASSERT_GT(err, 0) TH_LOG("HIDIOCGRAWPHYS ioctl failed, got %d", err); 639 + 640 + /* construct expected phys based on device id */ 641 + snprintf(expected_phys, sizeof(expected_phys), "%d", self->hid.dev_id); 642 + 643 + /* verify the phys matches expected value */ 644 + ASSERT_EQ(strcmp(phys, expected_phys), 0) 645 + TH_LOG("expected phys '%s', got '%s'", expected_phys, phys); 646 + } 647 + 648 + /* 649 + * Test HIDIOCGRAWUNIQ ioctl to get device unique identifier string 650 + */ 651 + TEST_F(hidraw, ioctl_rawuniq) 652 + { 653 + char uniq[256] = {0}; 654 + int err; 655 + 656 + /* get device unique identifier */ 657 + err = ioctl(self->hidraw_fd, HIDIOCGRAWUNIQ(sizeof(uniq)), uniq); 658 + ASSERT_GE(err, 0) TH_LOG("HIDIOCGRAWUNIQ ioctl failed, got %d", err); 659 + 660 + /* uniq is typically empty in our test setup */ 661 + ASSERT_EQ(strlen(uniq), 0) TH_LOG("expected empty uniq, got '%s'", uniq); 662 + } 663 + 664 + /* 665 + * Test device string ioctls with small buffer sizes 666 + */ 667 + TEST_F(hidraw, ioctl_strings_small_buffer) 668 + { 669 + char small_buf[8] = {0}; 670 + char expected_name[64]; 671 + int err; 672 + 673 + /* test HIDIOCGRAWNAME with small buffer */ 674 + err = ioctl(self->hidraw_fd, HIDIOCGRAWNAME(sizeof(small_buf)), small_buf); 675 + ASSERT_EQ(err, sizeof(small_buf)) 676 + TH_LOG("HIDIOCGRAWNAME with small buffer failed, got %d", err); 677 + 678 + /* construct expected truncated name */ 679 + snprintf(expected_name, sizeof(expected_name), "test-uhid-device-%d", self->hid.dev_id); 680 + 681 + /* verify we got truncated name (first 8 chars, no null terminator guaranteed) */ 682 + ASSERT_EQ(strncmp(small_buf, expected_name, sizeof(small_buf)), 0) 683 + TH_LOG("expected truncated name to match first %zu chars", sizeof(small_buf)); 684 + 685 + /* Note: hidraw driver doesn't guarantee null termination when buffer is too small */ 219 686 } 220 687 221 688 int main(int argc, char **argv)
+446 -268
tools/testing/selftests/hid/vmtest.sh
··· 1 1 #!/bin/bash 2 2 # SPDX-License-Identifier: GPL-2.0 3 + # 4 + # Copyright (c) 2025 Red Hat 5 + # Copyright (c) 2025 Meta Platforms, Inc. and affiliates 6 + # 7 + # Dependencies: 8 + # * virtme-ng 9 + # * busybox-static (used by virtme-ng) 10 + # * qemu (used by virtme-ng) 3 11 4 - set -u 5 - set -e 12 + readonly SCRIPT_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" 13 + readonly KERNEL_CHECKOUT=$(realpath "${SCRIPT_DIR}"/../../../../) 6 14 7 - # This script currently only works for x86_64 8 - ARCH="$(uname -m)" 9 - case "${ARCH}" in 10 - x86_64) 11 - QEMU_BINARY=qemu-system-x86_64 12 - BZIMAGE="arch/x86/boot/bzImage" 13 - ;; 14 - *) 15 - echo "Unsupported architecture" 16 - exit 1 17 - ;; 18 - esac 19 - SCRIPT_DIR="$(dirname $(realpath $0))" 20 - OUTPUT_DIR="$SCRIPT_DIR/results" 21 - KCONFIG_REL_PATHS=("${SCRIPT_DIR}/config" "${SCRIPT_DIR}/config.common" "${SCRIPT_DIR}/config.${ARCH}") 22 - B2C_URL="https://gitlab.freedesktop.org/gfx-ci/boot2container/-/raw/main/vm2c.py" 23 - NUM_COMPILE_JOBS="$(nproc)" 24 - LOG_FILE_BASE="$(date +"hid_selftests.%Y-%m-%d_%H-%M-%S")" 25 - LOG_FILE="${LOG_FILE_BASE}.log" 26 - EXIT_STATUS_FILE="${LOG_FILE_BASE}.exit_status" 27 - CONTAINER_IMAGE="registry.freedesktop.org/bentiss/hid/fedora/39:2023-11-22.1" 15 + source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh 28 16 29 - TARGETS="${TARGETS:=$(basename ${SCRIPT_DIR})}" 30 - DEFAULT_COMMAND="pip3 install hid-tools; make -C tools/testing/selftests TARGETS=${TARGETS} run_tests" 17 + readonly HID_BPF_TEST="${SCRIPT_DIR}"/hid_bpf 18 + readonly HIDRAW_TEST="${SCRIPT_DIR}"/hidraw 19 + readonly HID_BPF_PROGS="${KERNEL_CHECKOUT}/drivers/hid/bpf/progs" 20 + readonly SSH_GUEST_PORT=22 21 + readonly WAIT_PERIOD=3 22 + readonly WAIT_PERIOD_MAX=60 23 + readonly WAIT_TOTAL=$(( WAIT_PERIOD * WAIT_PERIOD_MAX )) 24 + readonly QEMU_PIDFILE=$(mktemp /tmp/qemu_hid_vmtest_XXXX.pid) 31 25 32 - usage() 33 - { 34 - cat <<EOF 35 - Usage: $0 [-j N] [-s] [-b] [-d <output_dir>] -- [<command>] 26 + readonly QEMU_OPTS="\ 27 + --pidfile ${QEMU_PIDFILE} \ 28 + " 29 + readonly KERNEL_CMDLINE="" 30 + readonly LOG=$(mktemp /tmp/hid_vmtest_XXXX.log) 31 + readonly TEST_NAMES=(vm_hid_bpf vm_hidraw vm_pytest) 32 + readonly TEST_DESCS=( 33 + "Run hid_bpf tests in the VM." 34 + "Run hidraw tests in the VM." 35 + "Run the hid-tools test-suite in the VM." 36 + ) 36 37 37 - <command> is the command you would normally run when you are in 38 - the source kernel direcory. e.g: 38 + VERBOSE=0 39 + SHELL_MODE=0 40 + BUILD_HOST="" 41 + BUILD_HOST_PODMAN_CONTAINER_NAME="" 39 42 40 - $0 -- ./tools/testing/selftests/hid/hid_bpf 43 + usage() { 44 + local name 45 + local desc 46 + local i 41 47 42 - If no command is specified and a debug shell (-s) is not requested, 43 - "${DEFAULT_COMMAND}" will be run by default. 48 + echo 49 + echo "$0 [OPTIONS] [TEST]... [-- tests-args]" 50 + echo "If no TEST argument is given, all tests will be run." 51 + echo 52 + echo "Options" 53 + echo " -b: build the kernel from the current source tree and use it for guest VMs" 54 + echo " -H: hostname for remote build host (used with -b)" 55 + echo " -p: podman container name for remote build host (used with -b)" 56 + echo " Example: -H beefyserver -p vng" 57 + echo " -q: set the path to or name of qemu binary" 58 + echo " -s: start a shell in the VM instead of running tests" 59 + echo " -v: more verbose output (can be repeated multiple times)" 60 + echo 61 + echo "Available tests" 44 62 45 - If you build your kernel using KBUILD_OUTPUT= or O= options, these 46 - can be passed as environment variables to the script: 47 - 48 - O=<kernel_build_path> $0 -- ./tools/testing/selftests/hid/hid_bpf 49 - 50 - or 51 - 52 - KBUILD_OUTPUT=<kernel_build_path> $0 -- ./tools/testing/selftests/hid/hid_bpf 53 - 54 - Options: 55 - 56 - -u) Update the boot2container script to a newer version. 57 - -d) Update the output directory (default: ${OUTPUT_DIR}) 58 - -b) Run only the build steps for the kernel and the selftests 59 - -j) Number of jobs for compilation, similar to -j in make 60 - (default: ${NUM_COMPILE_JOBS}) 61 - -s) Instead of powering off the VM, start an interactive 62 - shell. If <command> is specified, the shell runs after 63 - the command finishes executing 64 - EOF 65 - } 66 - 67 - download() 68 - { 69 - local file="$1" 70 - 71 - echo "Downloading $file..." >&2 72 - curl -Lsf "$file" -o "${@:2}" 73 - } 74 - 75 - recompile_kernel() 76 - { 77 - local kernel_checkout="$1" 78 - local make_command="$2" 79 - 80 - cd "${kernel_checkout}" 81 - 82 - ${make_command} olddefconfig 83 - ${make_command} headers 84 - ${make_command} 85 - } 86 - 87 - update_selftests() 88 - { 89 - local kernel_checkout="$1" 90 - local selftests_dir="${kernel_checkout}/tools/testing/selftests/hid" 91 - 92 - cd "${selftests_dir}" 93 - ${make_command} 94 - } 95 - 96 - run_vm() 97 - { 98 - local run_dir="$1" 99 - local b2c="$2" 100 - local kernel_bzimage="$3" 101 - local command="$4" 102 - local post_command="" 103 - 104 - cd "${run_dir}" 105 - 106 - if ! which "${QEMU_BINARY}" &> /dev/null; then 107 - cat <<EOF 108 - Could not find ${QEMU_BINARY} 109 - Please install qemu or set the QEMU_BINARY environment variable. 110 - EOF 111 - exit 1 112 - fi 113 - 114 - # alpine (used in post-container requires the PATH to have /bin 115 - export PATH=$PATH:/bin 116 - 117 - if [[ "${debug_shell}" != "yes" ]] 118 - then 119 - touch ${OUTPUT_DIR}/${LOG_FILE} 120 - command="mount bpffs -t bpf /sys/fs/bpf/; set -o pipefail ; ${command} 2>&1 | tee ${OUTPUT_DIR}/${LOG_FILE}" 121 - post_command="cat ${OUTPUT_DIR}/${LOG_FILE}" 122 - else 123 - command="mount bpffs -t bpf /sys/fs/bpf/; ${command}" 124 - fi 125 - 126 - set +e 127 - $b2c --command "${command}" \ 128 - --kernel ${kernel_bzimage} \ 129 - --workdir ${OUTPUT_DIR} \ 130 - --image ${CONTAINER_IMAGE} 131 - 132 - echo $? > ${OUTPUT_DIR}/${EXIT_STATUS_FILE} 133 - 134 - set -e 135 - 136 - ${post_command} 137 - } 138 - 139 - is_rel_path() 140 - { 141 - local path="$1" 142 - 143 - [[ ${path:0:1} != "/" ]] 144 - } 145 - 146 - do_update_kconfig() 147 - { 148 - local kernel_checkout="$1" 149 - local kconfig_file="$2" 150 - 151 - rm -f "$kconfig_file" 2> /dev/null 152 - 153 - for config in "${KCONFIG_REL_PATHS[@]}"; do 154 - local kconfig_src="${config}" 155 - cat "$kconfig_src" >> "$kconfig_file" 63 + for ((i = 0; i < ${#TEST_NAMES[@]}; i++)); do 64 + name=${TEST_NAMES[${i}]} 65 + desc=${TEST_DESCS[${i}]} 66 + printf "\t%-35s%-35s\n" "${name}" "${desc}" 156 67 done 68 + echo 69 + 70 + exit 1 157 71 } 158 72 159 - update_kconfig() 160 - { 161 - local kernel_checkout="$1" 162 - local kconfig_file="$2" 73 + die() { 74 + echo "$*" >&2 75 + exit "${KSFT_FAIL}" 76 + } 163 77 164 - if [[ -f "${kconfig_file}" ]]; then 165 - local local_modified="$(stat -c %Y "${kconfig_file}")" 78 + vm_ssh() { 79 + # vng --ssh-client keeps shouting "Warning: Permanently added 'virtme-ng%22' 80 + # (ED25519) to the list of known hosts.", 81 + # So replace the command with what's actually called and add the "-q" option 82 + stdbuf -oL ssh -q \ 83 + -F ${HOME}/.cache/virtme-ng/.ssh/virtme-ng-ssh.conf \ 84 + -l root virtme-ng%${SSH_GUEST_PORT} \ 85 + "$@" 86 + return $? 87 + } 166 88 167 - for config in "${KCONFIG_REL_PATHS[@]}"; do 168 - local kconfig_src="${config}" 169 - local src_modified="$(stat -c %Y "${kconfig_src}")" 170 - # Only update the config if it has been updated after the 171 - # previously cached config was created. This avoids 172 - # unnecessarily compiling the kernel and selftests. 173 - if [[ "${src_modified}" -gt "${local_modified}" ]]; then 174 - do_update_kconfig "$kernel_checkout" "$kconfig_file" 175 - # Once we have found one outdated configuration 176 - # there is no need to check other ones. 89 + cleanup() { 90 + if [[ -s "${QEMU_PIDFILE}" ]]; then 91 + pkill -SIGTERM -F "${QEMU_PIDFILE}" > /dev/null 2>&1 92 + fi 93 + 94 + # If failure occurred during or before qemu start up, then we need 95 + # to clean this up ourselves. 96 + if [[ -e "${QEMU_PIDFILE}" ]]; then 97 + rm "${QEMU_PIDFILE}" 98 + fi 99 + } 100 + 101 + check_args() { 102 + local found 103 + 104 + for arg in "$@"; do 105 + found=0 106 + for name in "${TEST_NAMES[@]}"; do 107 + if [[ "${name}" = "${arg}" ]]; then 108 + found=1 177 109 break 178 110 fi 179 111 done 180 - else 181 - do_update_kconfig "$kernel_checkout" "$kconfig_file" 182 - fi 183 - } 184 112 185 - main() 186 - { 187 - local script_dir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" 188 - local kernel_checkout=$(realpath "${script_dir}"/../../../../) 189 - # By default the script searches for the kernel in the checkout directory but 190 - # it also obeys environment variables O= and KBUILD_OUTPUT= 191 - local kernel_bzimage="${kernel_checkout}/${BZIMAGE}" 192 - local command="${DEFAULT_COMMAND}" 193 - local update_b2c="no" 194 - local debug_shell="no" 195 - local build_only="no" 196 - 197 - while getopts ':hsud:j:b' opt; do 198 - case ${opt} in 199 - u) 200 - update_b2c="yes" 201 - ;; 202 - d) 203 - OUTPUT_DIR="$OPTARG" 204 - ;; 205 - j) 206 - NUM_COMPILE_JOBS="$OPTARG" 207 - ;; 208 - s) 209 - command="/bin/sh" 210 - debug_shell="yes" 211 - ;; 212 - b) 213 - build_only="yes" 214 - ;; 215 - h) 113 + if [[ "${found}" -eq 0 ]]; then 114 + echo "${arg} is not an available test" >&2 216 115 usage 217 - exit 0 218 - ;; 219 - \? ) 220 - echo "Invalid Option: -$OPTARG" 221 - usage 222 - exit 1 223 - ;; 224 - : ) 225 - echo "Invalid Option: -$OPTARG requires an argument" 226 - usage 227 - exit 1 228 - ;; 229 - esac 116 + fi 230 117 done 231 - shift $((OPTIND -1)) 232 118 233 - # trap 'catch "$?"' EXIT 234 - if [[ "${build_only}" == "no" && "${debug_shell}" == "no" ]]; then 235 - if [[ $# -eq 0 ]]; then 236 - echo "No command specified, will run ${DEFAULT_COMMAND} in the vm" 237 - else 238 - command="$@" 239 - 240 - if [[ "${command}" == "/bin/bash" || "${command}" == "bash" ]] 241 - then 242 - debug_shell="yes" 243 - fi 119 + for arg in "$@"; do 120 + if ! command -v > /dev/null "test_${arg}"; then 121 + echo "Test ${arg} not found" >&2 122 + usage 244 123 fi 124 + done 125 + } 126 + 127 + check_deps() { 128 + for dep in vng ${QEMU} busybox pkill ssh pytest; do 129 + if [[ ! -x $(command -v "${dep}") ]]; then 130 + echo -e "skip: dependency ${dep} not found!\n" 131 + exit "${KSFT_SKIP}" 132 + fi 133 + done 134 + 135 + if [[ ! -x $(command -v "${HID_BPF_TEST}") ]]; then 136 + printf "skip: %s not found!" "${HID_BPF_TEST}" 137 + printf " Please build the kselftest hid_bpf target.\n" 138 + exit "${KSFT_SKIP}" 245 139 fi 246 140 247 - local kconfig_file="${OUTPUT_DIR}/latest.config" 248 - local make_command="make -j ${NUM_COMPILE_JOBS} KCONFIG_CONFIG=${kconfig_file}" 249 - 250 - # Figure out where the kernel is being built. 251 - # O takes precedence over KBUILD_OUTPUT. 252 - if [[ "${O:=""}" != "" ]]; then 253 - if is_rel_path "${O}"; then 254 - O="$(realpath "${PWD}/${O}")" 255 - fi 256 - kernel_bzimage="${O}/${BZIMAGE}" 257 - make_command="${make_command} O=${O}" 258 - elif [[ "${KBUILD_OUTPUT:=""}" != "" ]]; then 259 - if is_rel_path "${KBUILD_OUTPUT}"; then 260 - KBUILD_OUTPUT="$(realpath "${PWD}/${KBUILD_OUTPUT}")" 261 - fi 262 - kernel_bzimage="${KBUILD_OUTPUT}/${BZIMAGE}" 263 - make_command="${make_command} KBUILD_OUTPUT=${KBUILD_OUTPUT}" 264 - fi 265 - 266 - local b2c="${OUTPUT_DIR}/vm2c.py" 267 - 268 - echo "Output directory: ${OUTPUT_DIR}" 269 - 270 - mkdir -p "${OUTPUT_DIR}" 271 - update_kconfig "${kernel_checkout}" "${kconfig_file}" 272 - 273 - recompile_kernel "${kernel_checkout}" "${make_command}" 274 - update_selftests "${kernel_checkout}" "${make_command}" 275 - 276 - if [[ "${build_only}" == "no" ]]; then 277 - if [[ "${update_b2c}" == "no" && ! -f "${b2c}" ]]; then 278 - echo "vm2c script not found in ${b2c}" 279 - update_b2c="yes" 280 - fi 281 - 282 - if [[ "${update_b2c}" == "yes" ]]; then 283 - download $B2C_URL $b2c 284 - chmod +x $b2c 285 - fi 286 - 287 - run_vm "${kernel_checkout}" $b2c "${kernel_bzimage}" "${command}" 288 - if [[ "${debug_shell}" != "yes" ]]; then 289 - echo "Logs saved in ${OUTPUT_DIR}/${LOG_FILE}" 290 - fi 291 - 292 - exit $(cat ${OUTPUT_DIR}/${EXIT_STATUS_FILE}) 141 + if [[ ! -x $(command -v "${HIDRAW_TEST}") ]]; then 142 + printf "skip: %s not found!" "${HIDRAW_TEST}" 143 + printf " Please build the kselftest hidraw target.\n" 144 + exit "${KSFT_SKIP}" 293 145 fi 294 146 } 295 147 296 - main "$@" 148 + check_vng() { 149 + local tested_versions 150 + local version 151 + local ok 152 + 153 + tested_versions=("1.36" "1.37") 154 + version="$(vng --version)" 155 + 156 + ok=0 157 + for tv in "${tested_versions[@]}"; do 158 + if [[ "${version}" == *"${tv}"* ]]; then 159 + ok=1 160 + break 161 + fi 162 + done 163 + 164 + if [[ ! "${ok}" -eq 1 ]]; then 165 + printf "warning: vng version '%s' has not been tested and may " "${version}" >&2 166 + printf "not function properly.\n\tThe following versions have been tested: " >&2 167 + echo "${tested_versions[@]}" >&2 168 + fi 169 + } 170 + 171 + handle_build() { 172 + if [[ ! "${BUILD}" -eq 1 ]]; then 173 + return 174 + fi 175 + 176 + if [[ ! -d "${KERNEL_CHECKOUT}" ]]; then 177 + echo "-b requires vmtest.sh called from the kernel source tree" >&2 178 + exit 1 179 + fi 180 + 181 + pushd "${KERNEL_CHECKOUT}" &>/dev/null 182 + 183 + if ! vng --kconfig --config "${SCRIPT_DIR}"/config; then 184 + die "failed to generate .config for kernel source tree (${KERNEL_CHECKOUT})" 185 + fi 186 + 187 + local vng_args=("-v" "--config" "${SCRIPT_DIR}/config" "--build") 188 + 189 + if [[ -n "${BUILD_HOST}" ]]; then 190 + vng_args+=("--build-host" "${BUILD_HOST}") 191 + fi 192 + 193 + if [[ -n "${BUILD_HOST_PODMAN_CONTAINER_NAME}" ]]; then 194 + vng_args+=("--build-host-exec-prefix" \ 195 + "podman exec -ti ${BUILD_HOST_PODMAN_CONTAINER_NAME}") 196 + fi 197 + 198 + if ! vng "${vng_args[@]}"; then 199 + die "failed to build kernel from source tree (${KERNEL_CHECKOUT})" 200 + fi 201 + 202 + if ! make -j$(nproc) -C "${HID_BPF_PROGS}"; then 203 + die "failed to build HID bpf objects from source tree (${HID_BPF_PROGS})" 204 + fi 205 + 206 + if ! make -j$(nproc) -C "${SCRIPT_DIR}"; then 207 + die "failed to build HID selftests from source tree (${SCRIPT_DIR})" 208 + fi 209 + 210 + popd &>/dev/null 211 + } 212 + 213 + vm_start() { 214 + local logfile=/dev/null 215 + local verbose_opt="" 216 + local kernel_opt="" 217 + local qemu 218 + 219 + qemu=$(command -v "${QEMU}") 220 + 221 + if [[ "${VERBOSE}" -eq 2 ]]; then 222 + verbose_opt="--verbose" 223 + logfile=/dev/stdout 224 + fi 225 + 226 + # If we are running from within the kernel source tree, use the kernel source tree 227 + # as the kernel to boot, otherwise use the currently running kernel. 228 + if [[ "$(realpath "$(pwd)")" == "${KERNEL_CHECKOUT}"* ]]; then 229 + kernel_opt="${KERNEL_CHECKOUT}" 230 + fi 231 + 232 + vng \ 233 + --run \ 234 + ${kernel_opt} \ 235 + ${verbose_opt} \ 236 + --qemu-opts="${QEMU_OPTS}" \ 237 + --qemu="${qemu}" \ 238 + --user root \ 239 + --append "${KERNEL_CMDLINE}" \ 240 + --ssh "${SSH_GUEST_PORT}" \ 241 + --rw &> ${logfile} & 242 + 243 + local vng_pid=$! 244 + local elapsed=0 245 + 246 + while [[ ! -s "${QEMU_PIDFILE}" ]]; do 247 + if ! kill -0 "${vng_pid}" 2>/dev/null; then 248 + echo "vng process (PID ${vng_pid}) exited early, check logs for details" >&2 249 + die "failed to boot VM" 250 + fi 251 + 252 + if [[ ${elapsed} -ge ${WAIT_TOTAL} ]]; then 253 + echo "Timed out after ${WAIT_TOTAL} seconds waiting for VM to boot" >&2 254 + die "failed to boot VM" 255 + fi 256 + 257 + sleep 1 258 + elapsed=$((elapsed + 1)) 259 + done 260 + } 261 + 262 + vm_wait_for_ssh() { 263 + local i 264 + 265 + i=0 266 + while true; do 267 + if [[ ${i} -gt ${WAIT_PERIOD_MAX} ]]; then 268 + die "Timed out waiting for guest ssh" 269 + fi 270 + if vm_ssh -- true; then 271 + break 272 + fi 273 + i=$(( i + 1 )) 274 + sleep ${WAIT_PERIOD} 275 + done 276 + } 277 + 278 + vm_mount_bpffs() { 279 + vm_ssh -- mount bpffs -t bpf /sys/fs/bpf 280 + } 281 + 282 + __log_stdin() { 283 + stdbuf -oL awk '{ printf "%s:\t%s\n","'"${prefix}"'", $0; fflush() }' 284 + } 285 + 286 + __log_args() { 287 + echo "$*" | awk '{ printf "%s:\t%s\n","'"${prefix}"'", $0 }' 288 + } 289 + 290 + log() { 291 + local verbose="$1" 292 + shift 293 + 294 + local prefix="$1" 295 + 296 + shift 297 + local redirect= 298 + if [[ ${verbose} -le 0 ]]; then 299 + redirect=/dev/null 300 + else 301 + redirect=/dev/stdout 302 + fi 303 + 304 + if [[ "$#" -eq 0 ]]; then 305 + __log_stdin | tee -a "${LOG}" > ${redirect} 306 + else 307 + __log_args "$@" | tee -a "${LOG}" > ${redirect} 308 + fi 309 + } 310 + 311 + log_setup() { 312 + log $((VERBOSE-1)) "setup" "$@" 313 + } 314 + 315 + log_host() { 316 + local testname=$1 317 + 318 + shift 319 + log $((VERBOSE-1)) "test:${testname}:host" "$@" 320 + } 321 + 322 + log_guest() { 323 + local testname=$1 324 + 325 + shift 326 + log ${VERBOSE} "# test:${testname}" "$@" 327 + } 328 + 329 + test_vm_hid_bpf() { 330 + local testname="${FUNCNAME[0]#test_}" 331 + 332 + vm_ssh -- "${HID_BPF_TEST}" \ 333 + 2>&1 | log_guest "${testname}" 334 + 335 + return ${PIPESTATUS[0]} 336 + } 337 + 338 + test_vm_hidraw() { 339 + local testname="${FUNCNAME[0]#test_}" 340 + 341 + vm_ssh -- "${HIDRAW_TEST}" \ 342 + 2>&1 | log_guest "${testname}" 343 + 344 + return ${PIPESTATUS[0]} 345 + } 346 + 347 + test_vm_pytest() { 348 + local testname="${FUNCNAME[0]#test_}" 349 + 350 + shift 351 + 352 + vm_ssh -- pytest ${SCRIPT_DIR}/tests --color=yes "$@" \ 353 + 2>&1 | log_guest "${testname}" 354 + 355 + return ${PIPESTATUS[0]} 356 + } 357 + 358 + run_test() { 359 + local vm_oops_cnt_before 360 + local vm_warn_cnt_before 361 + local vm_oops_cnt_after 362 + local vm_warn_cnt_after 363 + local name 364 + local rc 365 + 366 + vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops') 367 + vm_error_cnt_before=$(vm_ssh -- dmesg --level=err | wc -l) 368 + 369 + name=$(echo "${1}" | awk '{ print $1 }') 370 + eval test_"${name}" "$@" 371 + rc=$? 372 + 373 + vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l) 374 + if [[ ${vm_oops_cnt_after} -gt ${vm_oops_cnt_before} ]]; then 375 + echo "FAIL: kernel oops detected on vm" | log_host "${name}" 376 + rc=$KSFT_FAIL 377 + fi 378 + 379 + vm_error_cnt_after=$(vm_ssh -- dmesg --level=err | wc -l) 380 + if [[ ${vm_error_cnt_after} -gt ${vm_error_cnt_before} ]]; then 381 + echo "FAIL: kernel error detected on vm" | log_host "${name}" 382 + vm_ssh -- dmesg --level=err | log_host "${name}" 383 + rc=$KSFT_FAIL 384 + fi 385 + 386 + return "${rc}" 387 + } 388 + 389 + QEMU="qemu-system-$(uname -m)" 390 + 391 + while getopts :hvsbq:H:p: o 392 + do 393 + case $o in 394 + v) VERBOSE=$((VERBOSE+1));; 395 + s) SHELL_MODE=1;; 396 + b) BUILD=1;; 397 + q) QEMU=$OPTARG;; 398 + H) BUILD_HOST=$OPTARG;; 399 + p) BUILD_HOST_PODMAN_CONTAINER_NAME=$OPTARG;; 400 + h|*) usage;; 401 + esac 402 + done 403 + shift $((OPTIND-1)) 404 + 405 + trap cleanup EXIT 406 + 407 + PARAMS="" 408 + 409 + if [[ ${#} -eq 0 ]]; then 410 + ARGS=("${TEST_NAMES[@]}") 411 + else 412 + ARGS=() 413 + COUNT=0 414 + for arg in $@; do 415 + COUNT=$((COUNT+1)) 416 + if [[ x"$arg" == x"--" ]]; then 417 + break 418 + fi 419 + ARGS+=($arg) 420 + done 421 + shift $COUNT 422 + PARAMS="$@" 423 + fi 424 + 425 + if [[ "${SHELL_MODE}" -eq 0 ]]; then 426 + check_args "${ARGS[@]}" 427 + echo "1..${#ARGS[@]}" 428 + fi 429 + check_deps 430 + check_vng 431 + handle_build 432 + 433 + log_setup "Booting up VM" 434 + vm_start 435 + vm_wait_for_ssh 436 + vm_mount_bpffs 437 + log_setup "VM booted up" 438 + 439 + if [[ "${SHELL_MODE}" -eq 1 ]]; then 440 + log_setup "Starting interactive shell in VM" 441 + echo "Starting shell in VM. Use 'exit' to quit and shutdown the VM." 442 + CURRENT_DIR="$(pwd)" 443 + vm_ssh -t -- "cd '${CURRENT_DIR}' && exec bash -l" 444 + exit "$KSFT_PASS" 445 + fi 446 + 447 + cnt_pass=0 448 + cnt_fail=0 449 + cnt_skip=0 450 + cnt_total=0 451 + for arg in "${ARGS[@]}"; do 452 + run_test "${arg}" "${PARAMS}" 453 + rc=$? 454 + if [[ ${rc} -eq $KSFT_PASS ]]; then 455 + cnt_pass=$(( cnt_pass + 1 )) 456 + echo "ok ${cnt_total} ${arg}" 457 + elif [[ ${rc} -eq $KSFT_SKIP ]]; then 458 + cnt_skip=$(( cnt_skip + 1 )) 459 + echo "ok ${cnt_total} ${arg} # SKIP" 460 + elif [[ ${rc} -eq $KSFT_FAIL ]]; then 461 + cnt_fail=$(( cnt_fail + 1 )) 462 + echo "not ok ${cnt_total} ${arg} # exit=$rc" 463 + fi 464 + cnt_total=$(( cnt_total + 1 )) 465 + done 466 + 467 + echo "SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}" 468 + echo "Log: ${LOG}" 469 + 470 + if [ $((cnt_pass + cnt_skip)) -eq ${cnt_total} ]; then 471 + exit "$KSFT_PASS" 472 + else 473 + exit "$KSFT_FAIL" 474 + fi