1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 * HID driver for NVIDIA SHIELD peripherals.
9 #include <linux/input-event-codes.h>
10 #include <linux/input.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <linux/workqueue.h>
18 #define NOT_INIT_STR "NOT INITIALIZED"
19 #define android_map_key(c) hid_map_usage(hi, usage, bit, max, EV_KEY, (c))
22 HID_USAGE_ANDROID_PLAYPAUSE_BTN = 0xcd, /* Double-tap volume slider */
23 HID_USAGE_ANDROID_VOLUMEUP_BTN = 0xe9,
24 HID_USAGE_ANDROID_VOLUMEDOWN_BTN = 0xea,
25 HID_USAGE_ANDROID_SEARCH_BTN = 0x221, /* NVIDIA btn on Thunderstrike */
26 HID_USAGE_ANDROID_HOME_BTN = 0x223,
27 HID_USAGE_ANDROID_BACK_BTN = 0x224,
31 SHIELD_FW_VERSION_INITIALIZED = 0,
32 SHIELD_BOARD_INFO_INITIALIZED,
36 THUNDERSTRIKE_FW_VERSION_UPDATE = 0,
37 THUNDERSTRIKE_BOARD_INFO_UPDATE,
38 THUNDERSTRIKE_HAPTICS_UPDATE,
39 THUNDERSTRIKE_LED_UPDATE,
43 THUNDERSTRIKE_HOSTCMD_REPORT_SIZE = 33,
44 THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID = 0x4,
45 THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID = 0x3,
49 THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION = 1,
50 THUNDERSTRIKE_HOSTCMD_ID_LED = 6,
51 THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO = 16,
52 THUNDERSTRIKE_HOSTCMD_ID_USB_INIT = 53,
53 THUNDERSTRIKE_HOSTCMD_ID_HAPTICS = 57,
54 THUNDERSTRIKE_HOSTCMD_ID_BLUETOOTH_INIT = 58,
57 enum thunderstrike_led_state {
58 THUNDERSTRIKE_LED_OFF = 1,
59 THUNDERSTRIKE_LED_ON = 8,
61 static_assert(sizeof(enum thunderstrike_led_state) == 1);
63 struct thunderstrike_hostcmd_board_info {
68 struct thunderstrike_hostcmd_haptics {
73 struct thunderstrike_hostcmd_resp_report {
74 u8 report_id; /* THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID */
79 struct thunderstrike_hostcmd_board_info board_info;
80 struct thunderstrike_hostcmd_haptics motors;
82 enum thunderstrike_led_state led_state;
86 static_assert(sizeof(struct thunderstrike_hostcmd_resp_report) ==
87 THUNDERSTRIKE_HOSTCMD_REPORT_SIZE);
89 struct thunderstrike_hostcmd_req_report {
90 u8 report_id; /* THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID */
97 enum thunderstrike_led_state state;
101 struct thunderstrike_hostcmd_haptics motors;
104 u8 reserved_at_30[27];
106 static_assert(sizeof(struct thunderstrike_hostcmd_req_report) ==
107 THUNDERSTRIKE_HOSTCMD_REPORT_SIZE);
109 /* Common struct for shield accessories. */
110 struct shield_device {
111 struct hid_device *hdev;
113 unsigned long initialized_flags;
114 const char *codename;
118 char serial_number[15];
122 struct thunderstrike {
123 struct shield_device base;
126 struct input_dev *haptics_dev;
127 struct led_classdev led_dev;
130 void *req_report_dmabuf;
131 unsigned long update_flags;
132 struct thunderstrike_hostcmd_haptics haptics_val;
133 spinlock_t haptics_update_lock;
135 enum thunderstrike_led_state led_value;
136 struct work_struct hostcmd_req_work;
139 static inline void thunderstrike_hostcmd_req_report_init(
140 struct thunderstrike_hostcmd_req_report *report, u8 cmd_id)
142 memset(report, 0, sizeof(*report));
143 report->report_id = THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID;
144 report->cmd_id = cmd_id;
147 static inline void shield_strrev(char *dest, size_t len, u16 rev)
149 dest[0] = ('A' - 1) + (rev >> 8);
150 snprintf(&dest[1], len - 1, "%02X", 0xff & rev);
153 static struct input_dev *shield_allocate_input_dev(struct hid_device *hdev,
154 const char *name_suffix)
156 struct input_dev *idev;
158 idev = input_allocate_device();
162 idev->id.bustype = hdev->bus;
163 idev->id.vendor = hdev->vendor;
164 idev->id.product = hdev->product;
165 idev->id.version = hdev->version;
166 idev->uniq = hdev->uniq;
167 idev->name = devm_kasprintf(&idev->dev, GFP_KERNEL, "%s %s", hdev->name,
172 input_set_drvdata(idev, hdev);
177 input_free_device(idev);
179 return ERR_PTR(-ENOMEM);
182 static struct input_dev *shield_haptics_create(
183 struct shield_device *dev,
184 int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
186 struct input_dev *haptics;
189 if (!IS_ENABLED(CONFIG_NVIDIA_SHIELD_FF))
192 haptics = shield_allocate_input_dev(dev->hdev, "Haptics");
196 input_set_capability(haptics, EV_FF, FF_RUMBLE);
197 input_ff_create_memless(haptics, NULL, play_effect);
199 ret = input_register_device(haptics);
206 input_free_device(haptics);
210 static inline void thunderstrike_send_hostcmd_request(struct thunderstrike *ts)
212 struct thunderstrike_hostcmd_req_report *report = ts->req_report_dmabuf;
213 struct shield_device *shield_dev = &ts->base;
216 ret = hid_hw_raw_request(shield_dev->hdev, report->report_id,
217 ts->req_report_dmabuf,
218 THUNDERSTRIKE_HOSTCMD_REPORT_SIZE,
219 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
222 hid_err(shield_dev->hdev,
223 "Failed to output Thunderstrike HOSTCMD request HID report due to %pe\n",
228 static void thunderstrike_hostcmd_req_work_handler(struct work_struct *work)
230 struct thunderstrike *ts =
231 container_of(work, struct thunderstrike, hostcmd_req_work);
232 struct thunderstrike_hostcmd_req_report *report;
235 report = ts->req_report_dmabuf;
237 if (test_and_clear_bit(THUNDERSTRIKE_FW_VERSION_UPDATE, &ts->update_flags)) {
238 thunderstrike_hostcmd_req_report_init(
239 report, THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION);
240 thunderstrike_send_hostcmd_request(ts);
243 if (test_and_clear_bit(THUNDERSTRIKE_LED_UPDATE, &ts->update_flags)) {
244 thunderstrike_hostcmd_req_report_init(report, THUNDERSTRIKE_HOSTCMD_ID_LED);
245 report->led.update = 1;
246 report->led.state = ts->led_value;
247 thunderstrike_send_hostcmd_request(ts);
250 if (test_and_clear_bit(THUNDERSTRIKE_BOARD_INFO_UPDATE, &ts->update_flags)) {
251 thunderstrike_hostcmd_req_report_init(
252 report, THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO);
253 thunderstrike_send_hostcmd_request(ts);
256 if (test_and_clear_bit(THUNDERSTRIKE_HAPTICS_UPDATE, &ts->update_flags)) {
257 thunderstrike_hostcmd_req_report_init(
258 report, THUNDERSTRIKE_HOSTCMD_ID_HAPTICS);
260 report->haptics.update = 1;
261 spin_lock_irqsave(&ts->haptics_update_lock, flags);
262 report->haptics.motors = ts->haptics_val;
263 spin_unlock_irqrestore(&ts->haptics_update_lock, flags);
265 thunderstrike_send_hostcmd_request(ts);
269 static inline void thunderstrike_request_firmware_version(struct thunderstrike *ts)
271 set_bit(THUNDERSTRIKE_FW_VERSION_UPDATE, &ts->update_flags);
272 schedule_work(&ts->hostcmd_req_work);
275 static inline void thunderstrike_request_board_info(struct thunderstrike *ts)
277 set_bit(THUNDERSTRIKE_BOARD_INFO_UPDATE, &ts->update_flags);
278 schedule_work(&ts->hostcmd_req_work);
282 thunderstrike_update_haptics(struct thunderstrike *ts,
283 struct thunderstrike_hostcmd_haptics *motors)
287 spin_lock_irqsave(&ts->haptics_update_lock, flags);
288 ts->haptics_val = *motors;
289 spin_unlock_irqrestore(&ts->haptics_update_lock, flags);
291 set_bit(THUNDERSTRIKE_HAPTICS_UPDATE, &ts->update_flags);
292 schedule_work(&ts->hostcmd_req_work);
297 static int thunderstrike_play_effect(struct input_dev *idev, void *data,
298 struct ff_effect *effect)
300 struct hid_device *hdev = input_get_drvdata(idev);
301 struct thunderstrike_hostcmd_haptics motors;
302 struct shield_device *shield_dev;
303 struct thunderstrike *ts;
305 if (effect->type != FF_RUMBLE)
308 shield_dev = hid_get_drvdata(hdev);
309 ts = container_of(shield_dev, struct thunderstrike, base);
311 /* Thunderstrike motor values range from 0 to 32 inclusively */
312 motors.motor_left = effect->u.rumble.strong_magnitude / 2047;
313 motors.motor_right = effect->u.rumble.weak_magnitude / 2047;
315 hid_dbg(hdev, "Thunderstrike FF_RUMBLE request, left: %u right: %u\n",
316 motors.motor_left, motors.motor_right);
318 return thunderstrike_update_haptics(ts, &motors);
321 static enum led_brightness
322 thunderstrike_led_get_brightness(struct led_classdev *led)
324 struct hid_device *hdev = to_hid_device(led->dev->parent);
325 struct shield_device *shield_dev = hid_get_drvdata(hdev);
326 struct thunderstrike *ts;
328 ts = container_of(shield_dev, struct thunderstrike, base);
330 return ts->led_state;
333 static void thunderstrike_led_set_brightness(struct led_classdev *led,
334 enum led_brightness value)
336 struct hid_device *hdev = to_hid_device(led->dev->parent);
337 struct shield_device *shield_dev = hid_get_drvdata(hdev);
338 struct thunderstrike *ts;
340 ts = container_of(shield_dev, struct thunderstrike, base);
344 ts->led_value = THUNDERSTRIKE_LED_OFF;
347 ts->led_value = THUNDERSTRIKE_LED_ON;
351 set_bit(THUNDERSTRIKE_LED_UPDATE, &ts->update_flags);
352 schedule_work(&ts->hostcmd_req_work);
356 thunderstrike_parse_fw_version_payload(struct shield_device *shield_dev,
359 shield_dev->fw_version = le16_to_cpu(fw_version);
361 set_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags);
363 hid_dbg(shield_dev->hdev, "Thunderstrike firmware version 0x%04X\n",
364 shield_dev->fw_version);
368 thunderstrike_parse_board_info_payload(struct shield_device *shield_dev,
369 struct thunderstrike_hostcmd_board_info *board_info)
371 char board_revision_str[4];
374 shield_dev->board_info.revision = le16_to_cpu(board_info->revision);
375 for (i = 0; i < 7; ++i) {
376 u16 val = le16_to_cpu(board_info->serial[i]);
378 shield_dev->board_info.serial_number[2 * i] = val & 0xFF;
379 shield_dev->board_info.serial_number[2 * i + 1] = val >> 8;
381 shield_dev->board_info.serial_number[14] = '\0';
383 set_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags);
385 shield_strrev(board_revision_str, 4, shield_dev->board_info.revision);
386 hid_dbg(shield_dev->hdev,
387 "Thunderstrike BOARD_REVISION_%s (0x%04X) S/N: %s\n",
388 board_revision_str, shield_dev->board_info.revision,
389 shield_dev->board_info.serial_number);
393 thunderstrike_parse_haptics_payload(struct shield_device *shield_dev,
394 struct thunderstrike_hostcmd_haptics *haptics)
396 hid_dbg(shield_dev->hdev,
397 "Thunderstrike haptics HOSTCMD response, left: %u right: %u\n",
398 haptics->motor_left, haptics->motor_right);
402 thunderstrike_parse_led_payload(struct shield_device *shield_dev,
403 enum thunderstrike_led_state led_state)
405 struct thunderstrike *ts = container_of(shield_dev, struct thunderstrike, base);
408 case THUNDERSTRIKE_LED_OFF:
411 case THUNDERSTRIKE_LED_ON:
416 hid_dbg(shield_dev->hdev, "Thunderstrike led HOSTCMD response, 0x%02X\n", led_state);
419 static int thunderstrike_parse_report(struct shield_device *shield_dev,
420 struct hid_report *report, u8 *data,
423 struct thunderstrike_hostcmd_resp_report *hostcmd_resp_report;
424 struct thunderstrike *ts =
425 container_of(shield_dev, struct thunderstrike, base);
426 struct hid_device *hdev = shield_dev->hdev;
428 switch (report->id) {
429 case THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID:
430 if (size != THUNDERSTRIKE_HOSTCMD_REPORT_SIZE) {
432 "Encountered Thunderstrike HOSTCMD HID report with unexpected size %d\n",
437 hostcmd_resp_report =
438 (struct thunderstrike_hostcmd_resp_report *)data;
440 switch (hostcmd_resp_report->cmd_id) {
441 case THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION:
442 thunderstrike_parse_fw_version_payload(
443 shield_dev, hostcmd_resp_report->fw_version);
445 case THUNDERSTRIKE_HOSTCMD_ID_LED:
446 thunderstrike_parse_led_payload(shield_dev, hostcmd_resp_report->led_state);
448 case THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO:
449 thunderstrike_parse_board_info_payload(
450 shield_dev, &hostcmd_resp_report->board_info);
452 case THUNDERSTRIKE_HOSTCMD_ID_HAPTICS:
453 thunderstrike_parse_haptics_payload(
454 shield_dev, &hostcmd_resp_report->motors);
457 case THUNDERSTRIKE_HOSTCMD_ID_USB_INIT:
458 case THUNDERSTRIKE_HOSTCMD_ID_BLUETOOTH_INIT:
459 /* May block HOSTCMD requests till received initially */
460 thunderstrike_request_firmware_version(ts);
461 thunderstrike_request_board_info(ts);
462 /* Only HOSTCMD that can be triggered without a request */
466 "Unhandled Thunderstrike HOSTCMD id %d\n",
467 hostcmd_resp_report->cmd_id);
479 static inline int thunderstrike_led_create(struct thunderstrike *ts)
481 struct led_classdev *led = &ts->led_dev;
483 led->name = "thunderstrike:blue:led";
484 led->max_brightness = 1;
485 led->flags = LED_CORE_SUSPENDRESUME;
486 led->brightness_get = &thunderstrike_led_get_brightness;
487 led->brightness_set = &thunderstrike_led_set_brightness;
489 return led_classdev_register(&ts->base.hdev->dev, led);
492 static struct shield_device *thunderstrike_create(struct hid_device *hdev)
494 struct shield_device *shield_dev;
495 struct thunderstrike *ts;
498 ts = devm_kzalloc(&hdev->dev, sizeof(*ts), GFP_KERNEL);
500 return ERR_PTR(-ENOMEM);
502 ts->req_report_dmabuf = devm_kzalloc(
503 &hdev->dev, THUNDERSTRIKE_HOSTCMD_REPORT_SIZE, GFP_KERNEL);
504 if (!ts->req_report_dmabuf)
505 return ERR_PTR(-ENOMEM);
507 shield_dev = &ts->base;
508 shield_dev->hdev = hdev;
509 shield_dev->codename = "Thunderstrike";
511 spin_lock_init(&ts->haptics_update_lock);
512 INIT_WORK(&ts->hostcmd_req_work, thunderstrike_hostcmd_req_work_handler);
514 hid_set_drvdata(hdev, shield_dev);
516 ret = thunderstrike_led_create(ts);
518 hid_err(hdev, "Failed to create Thunderstrike LED instance\n");
522 ts->haptics_dev = shield_haptics_create(shield_dev, thunderstrike_play_effect);
523 if (IS_ERR(ts->haptics_dev))
526 hid_info(hdev, "Registered Thunderstrike controller\n");
530 led_classdev_unregister(&ts->led_dev);
531 return ERR_CAST(ts->haptics_dev);
534 static int android_input_mapping(struct hid_device *hdev, struct hid_input *hi,
535 struct hid_field *field,
536 struct hid_usage *usage, unsigned long **bit,
539 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
542 switch (usage->hid & HID_USAGE) {
543 case HID_USAGE_ANDROID_PLAYPAUSE_BTN:
544 android_map_key(KEY_PLAYPAUSE);
546 case HID_USAGE_ANDROID_VOLUMEUP_BTN:
547 android_map_key(KEY_VOLUMEUP);
549 case HID_USAGE_ANDROID_VOLUMEDOWN_BTN:
550 android_map_key(KEY_VOLUMEDOWN);
552 case HID_USAGE_ANDROID_SEARCH_BTN:
553 android_map_key(BTN_Z);
555 case HID_USAGE_ANDROID_HOME_BTN:
556 android_map_key(BTN_MODE);
558 case HID_USAGE_ANDROID_BACK_BTN:
559 android_map_key(BTN_SELECT);
568 static ssize_t firmware_version_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
571 struct hid_device *hdev = to_hid_device(dev);
572 struct shield_device *shield_dev;
575 shield_dev = hid_get_drvdata(hdev);
577 if (test_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags))
578 ret = sysfs_emit(buf, "0x%04X\n", shield_dev->fw_version);
580 ret = sysfs_emit(buf, NOT_INIT_STR "\n");
585 static DEVICE_ATTR_RO(firmware_version);
587 static ssize_t hardware_version_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct hid_device *hdev = to_hid_device(dev);
591 struct shield_device *shield_dev;
592 char board_revision_str[4];
595 shield_dev = hid_get_drvdata(hdev);
597 if (test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags)) {
598 shield_strrev(board_revision_str, 4, shield_dev->board_info.revision);
599 ret = sysfs_emit(buf, "%s BOARD_REVISION_%s (0x%04X)\n",
600 shield_dev->codename, board_revision_str,
601 shield_dev->board_info.revision);
603 ret = sysfs_emit(buf, NOT_INIT_STR "\n");
608 static DEVICE_ATTR_RO(hardware_version);
610 static ssize_t serial_number_show(struct device *dev,
611 struct device_attribute *attr, char *buf)
613 struct hid_device *hdev = to_hid_device(dev);
614 struct shield_device *shield_dev;
617 shield_dev = hid_get_drvdata(hdev);
619 if (test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags))
620 ret = sysfs_emit(buf, "%s\n", shield_dev->board_info.serial_number);
622 ret = sysfs_emit(buf, NOT_INIT_STR "\n");
627 static DEVICE_ATTR_RO(serial_number);
629 static struct attribute *shield_device_attrs[] = {
630 &dev_attr_firmware_version.attr,
631 &dev_attr_hardware_version.attr,
632 &dev_attr_serial_number.attr,
635 ATTRIBUTE_GROUPS(shield_device);
637 static int shield_raw_event(struct hid_device *hdev, struct hid_report *report,
640 struct shield_device *dev = hid_get_drvdata(hdev);
642 return thunderstrike_parse_report(dev, report, data, size);
645 static int shield_probe(struct hid_device *hdev, const struct hid_device_id *id)
647 struct shield_device *shield_dev = NULL;
648 struct thunderstrike *ts;
651 ret = hid_parse(hdev);
653 hid_err(hdev, "Parse failed\n");
657 switch (id->product) {
658 case USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER:
659 shield_dev = thunderstrike_create(hdev);
663 if (unlikely(!shield_dev)) {
664 hid_err(hdev, "Failed to identify SHIELD device\n");
667 if (IS_ERR(shield_dev)) {
668 hid_err(hdev, "Failed to create SHIELD device\n");
669 return PTR_ERR(shield_dev);
672 ts = container_of(shield_dev, struct thunderstrike, base);
674 ret = hid_hw_start(hdev, HID_CONNECT_HIDINPUT);
676 hid_err(hdev, "Failed to start HID device\n");
680 ret = hid_hw_open(hdev);
682 hid_err(hdev, "Failed to open HID device\n");
686 thunderstrike_request_firmware_version(ts);
687 thunderstrike_request_board_info(ts);
695 input_unregister_device(ts->haptics_dev);
699 static void shield_remove(struct hid_device *hdev)
701 struct shield_device *dev = hid_get_drvdata(hdev);
702 struct thunderstrike *ts;
704 ts = container_of(dev, struct thunderstrike, base);
707 led_classdev_unregister(&ts->led_dev);
709 input_unregister_device(ts->haptics_dev);
710 cancel_work_sync(&ts->hostcmd_req_work);
714 static const struct hid_device_id shield_devices[] = {
715 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NVIDIA,
716 USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER) },
717 { HID_USB_DEVICE(USB_VENDOR_ID_NVIDIA,
718 USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER) },
721 MODULE_DEVICE_TABLE(hid, shield_devices);
723 static struct hid_driver shield_driver = {
725 .id_table = shield_devices,
726 .input_mapping = android_input_mapping,
727 .probe = shield_probe,
728 .remove = shield_remove,
729 .raw_event = shield_raw_event,
731 .dev_groups = shield_device_groups,
734 module_hid_driver(shield_driver);
736 MODULE_AUTHOR("Rahul Rameshbabu <rrameshbabu@nvidia.com>");
737 MODULE_DESCRIPTION("HID Driver for NVIDIA SHIELD peripherals.");
738 MODULE_LICENSE("GPL");