Lines Matching +full:3 +full:- +full:axis +full:- +full:digital +full:- +full:gyroscope

1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Nintendo Switch Joy-Cons and Pro Controllers
5 * Copyright (c) 2019-2021 Daniel J. Ogorchock <djogorchock@gmail.com>
9 * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
13 * hid-wiimote kernel hid driver
14 * hid-logitech-hidpp driver
15 * hid-sony driver
17 * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
25 #include "hid-ids.h"
113 (JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
123 (JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
133 /* Hat values for pro controller's d-pad */
143 #define JC_IMU_DROPPED_PKT_WARNING 3
147 * configured with a range of +-8000 milliGs. Therefore, the resolution can be
148 * calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
158 * The controller's gyroscope has a sensor resolution of 16bits and is
159 * configured with a range of +-2000 degrees/second.
160 * Digits per dps: (2^16 -1)/(2000*2) = 16.38375
161 * dps per digit: 16.38375E-1 = .0610
167 * digits per dps (corrected): .0702E-1 = 14.247
174 #define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
320 s16 offset[3];
321 s16 scale[3];
331 static const u32 JC_BTN_A = BIT(3);
390 u8 button_status[3];
391 u8 left_stick[3];
392 u8 right_stick[3];
397 /* IMU input reports contain 3 samples */
398 u8 imu_raw_bytes[sizeof(struct joycon_imu_data) * 3];
448 s32 imu_cal_accel_divisor[3];
449 s32 imu_cal_gyro_divisor[3];
484 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
485 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
486 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
488 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
490 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
494 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
495 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
499 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
500 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
509 return -ENOMEM; in __joycon_hid_send()
526 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_wait_for_input_report()
529 spin_lock_irqsave(&ctlr->lock, flags); in joycon_wait_for_input_report()
530 ctlr->received_input_report = false; in joycon_wait_for_input_report()
531 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_wait_for_input_report()
532 ret = wait_event_timeout(ctlr->wait, in joycon_wait_for_input_report()
533 ctlr->received_input_report, in joycon_wait_for_input_report()
537 hid_warn(ctlr->hdev, in joycon_wait_for_input_report()
550 unsigned int delta_ms = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
553 ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_enforce_subcmd_rate()
556 delta_ms = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
558 ctlr->last_subcmd_sent_msecs = current_ms; in joycon_enforce_subcmd_rate()
571 while (tries--) { in joycon_hid_send_sync()
574 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
576 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
580 ret = wait_event_timeout(ctlr->wait, ctlr->received_resp, in joycon_hid_send_sync()
583 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
586 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
589 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
590 ret = -ETIMEDOUT; in joycon_hid_send_sync()
597 ctlr->received_resp = false; in joycon_hid_send_sync()
607 ctlr->usb_ack_match = cmd; in joycon_send_usb()
608 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
611 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
622 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_subcmd()
627 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_subcmd()
628 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
629 return -ENODEV; in joycon_send_subcmd()
631 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_subcmd()
633 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
635 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; in joycon_send_subcmd()
636 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
637 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
638 ctlr->subcmd_num = 0; in joycon_send_subcmd()
639 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
640 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
645 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
658 req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; in joycon_set_player_leds()
659 req->data[0] = (flash << 4) | on; in joycon_set_player_leds()
661 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
675 return -EINVAL; in joycon_request_spi_flash_read()
678 req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; in joycon_request_spi_flash_read()
679 data = req->data; in joycon_request_spi_flash_read()
683 hid_dbg(ctlr->hdev, "requesting SPI flash data\n"); in joycon_request_spi_flash_read()
686 hid_err(ctlr->hdev, "failed reading SPI flash; ret=%d\n", ret); in joycon_request_spi_flash_read()
688 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_spi_flash_read()
690 *reply = &report->subcmd_reply.data[5]; in joycon_request_spi_flash_read()
731 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
733 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
735 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
737 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
739 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
741 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
744 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
746 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
748 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
750 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
752 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
754 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
758 cal_x->max = cal_x->center + x_max_above; in joycon_read_stick_calibration()
759 cal_x->min = cal_x->center - x_min_below; in joycon_read_stick_calibration()
760 cal_y->max = cal_y->center + y_max_above; in joycon_read_stick_calibration()
761 cal_y->min = cal_y->center - y_min_below; in joycon_read_stick_calibration()
764 if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || in joycon_read_stick_calibration()
765 cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) in joycon_read_stick_calibration()
766 ret = -EINVAL; in joycon_read_stick_calibration()
783 cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; in joycon_use_default_calibration()
784 cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; in joycon_use_default_calibration()
785 cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; in joycon_use_default_calibration()
794 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
799 hid_info(ctlr->hdev, "using user cal for left stick\n"); in joycon_request_calibration()
801 hid_info(ctlr->hdev, "using factory cal for left stick\n"); in joycon_request_calibration()
805 hid_info(ctlr->hdev, "using user cal for right stick\n"); in joycon_request_calibration()
807 hid_info(ctlr->hdev, "using factory cal for right stick\n"); in joycon_request_calibration()
812 &ctlr->left_stick_cal_x, in joycon_request_calibration()
813 &ctlr->left_stick_cal_y, in joycon_request_calibration()
817 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
818 &ctlr->left_stick_cal_x, in joycon_request_calibration()
819 &ctlr->left_stick_cal_y, in joycon_request_calibration()
824 &ctlr->right_stick_cal_x, in joycon_request_calibration()
825 &ctlr->right_stick_cal_y, in joycon_request_calibration()
829 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
830 &ctlr->right_stick_cal_x, in joycon_request_calibration()
831 &ctlr->right_stick_cal_y, in joycon_request_calibration()
834 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
839 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
840 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
841 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
842 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
843 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
844 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
845 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
846 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
847 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
848 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
849 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
850 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
864 for (i = 0; i < 3; i++) { in joycon_calc_imu_cal_divisors()
865 ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - in joycon_calc_imu_cal_divisors()
866 ctlr->accel_cal.offset[i]; in joycon_calc_imu_cal_divisors()
867 ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - in joycon_calc_imu_cal_divisors()
868 ctlr->gyro_cal.offset[i]; in joycon_calc_imu_cal_divisors()
886 hid_info(ctlr->hdev, "using user cal for IMU\n"); in joycon_request_imu_calibration()
888 hid_info(ctlr->hdev, "using factory cal for IMU\n"); in joycon_request_imu_calibration()
892 hid_dbg(ctlr->hdev, "requesting IMU cal data\n"); in joycon_request_imu_calibration()
896 hid_warn(ctlr->hdev, in joycon_request_imu_calibration()
900 for (i = 0; i < 3; i++) { in joycon_request_imu_calibration()
901 ctlr->accel_cal.offset[i] = DFLT_ACCEL_OFFSET; in joycon_request_imu_calibration()
902 ctlr->accel_cal.scale[i] = DFLT_ACCEL_SCALE; in joycon_request_imu_calibration()
903 ctlr->gyro_cal.offset[i] = DFLT_GYRO_OFFSET; in joycon_request_imu_calibration()
904 ctlr->gyro_cal.scale[i] = DFLT_GYRO_SCALE; in joycon_request_imu_calibration()
911 for (i = 0; i < 3; i++) { in joycon_request_imu_calibration()
914 ctlr->accel_cal.offset[i] = get_unaligned_le16(raw_cal + j); in joycon_request_imu_calibration()
915 ctlr->accel_cal.scale[i] = get_unaligned_le16(raw_cal + j + 6); in joycon_request_imu_calibration()
916 ctlr->gyro_cal.offset[i] = get_unaligned_le16(raw_cal + j + 12); in joycon_request_imu_calibration()
917 ctlr->gyro_cal.scale[i] = get_unaligned_le16(raw_cal + j + 18); in joycon_request_imu_calibration()
922 hid_dbg(ctlr->hdev, "IMU calibration:\n" in joycon_request_imu_calibration()
927 ctlr->accel_cal.offset[0], in joycon_request_imu_calibration()
928 ctlr->accel_cal.offset[1], in joycon_request_imu_calibration()
929 ctlr->accel_cal.offset[2], in joycon_request_imu_calibration()
930 ctlr->accel_cal.scale[0], in joycon_request_imu_calibration()
931 ctlr->accel_cal.scale[1], in joycon_request_imu_calibration()
932 ctlr->accel_cal.scale[2], in joycon_request_imu_calibration()
933 ctlr->gyro_cal.offset[0], in joycon_request_imu_calibration()
934 ctlr->gyro_cal.offset[1], in joycon_request_imu_calibration()
935 ctlr->gyro_cal.offset[2], in joycon_request_imu_calibration()
936 ctlr->gyro_cal.scale[0], in joycon_request_imu_calibration()
937 ctlr->gyro_cal.scale[1], in joycon_request_imu_calibration()
938 ctlr->gyro_cal.scale[2]); in joycon_request_imu_calibration()
949 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; in joycon_set_report_mode()
950 req->data[0] = 0x30; /* standard, full report mode */ in joycon_set_report_mode()
952 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
962 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; in joycon_enable_rumble()
963 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_rumble()
965 hid_dbg(ctlr->hdev, "enabling rumble\n"); in joycon_enable_rumble()
975 req->subcmd_id = JC_SUBCMD_ENABLE_IMU; in joycon_enable_imu()
976 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_imu()
978 hid_dbg(ctlr->hdev, "enabling IMU\n"); in joycon_enable_imu()
984 s32 center = cal->center; in joycon_map_stick_val()
985 s32 min = cal->min; in joycon_map_stick_val()
986 s32 max = cal->max; in joycon_map_stick_val()
990 new_val = (val - center) * JC_MAX_STICK_MAG; in joycon_map_stick_val()
991 new_val /= (max - center); in joycon_map_stick_val()
993 new_val = (center - val) * -JC_MAX_STICK_MAG; in joycon_map_stick_val()
994 new_val /= (center - min); in joycon_map_stick_val()
996 new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG); in joycon_map_stick_val()
1004 u8 *raw = rep->imu_raw_bytes; in joycon_input_report_parse_imu_data()
1007 for (i = 0; i < 3; i++) { in joycon_input_report_parse_imu_data()
1010 data->accel_x = get_unaligned_le16(raw + 0); in joycon_input_report_parse_imu_data()
1011 data->accel_y = get_unaligned_le16(raw + 2); in joycon_input_report_parse_imu_data()
1012 data->accel_z = get_unaligned_le16(raw + 4); in joycon_input_report_parse_imu_data()
1013 data->gyro_x = get_unaligned_le16(raw + 6); in joycon_input_report_parse_imu_data()
1014 data->gyro_y = get_unaligned_le16(raw + 8); in joycon_input_report_parse_imu_data()
1015 data->gyro_z = get_unaligned_le16(raw + 10); in joycon_input_report_parse_imu_data()
1024 struct joycon_imu_data imu_data[3] = {0}; /* 3 reports per packet */ in joycon_parse_imu_report()
1025 struct input_dev *idev = ctlr->imu_input; in joycon_parse_imu_report()
1027 unsigned int last_msecs = ctlr->imu_last_pkt_ms; in joycon_parse_imu_report()
1037 * incrementing 8-bit counter per input report, but it is not very in joycon_parse_imu_report()
1039 * increments at or if it varies based on packet push rate - more on in joycon_parse_imu_report()
1042 * The reverse engineering work done on the joy-cons and pro controllers in joycon_parse_imu_report()
1044 * - The controller samples the IMU every 1.35ms. It then does some of in joycon_parse_imu_report()
1046 * - Each imu input report contains 3 IMU samples, (usually 5ms apart). in joycon_parse_imu_report()
1047 * - In the standard reporting mode (which this driver uses exclusively) in joycon_parse_imu_report()
1049 * * joy-con (bluetooth): every 15 ms in joycon_parse_imu_report()
1050 * * joy-cons (in charging grip via USB): every 15 ms in joycon_parse_imu_report()
1057 * SSR to 11ms for both the joy-cons and pro controllers). in joycon_parse_imu_report()
1074 * timestamps to userspace based on that. Each report contains 3 in joycon_parse_imu_report()
1075 * samples, so the IMU sampling rate should be avg_time_delta/3. We can in joycon_parse_imu_report()
1080 if (!ctlr->imu_first_packet_received) { in joycon_parse_imu_report()
1081 ctlr->imu_timestamp_us = 0; in joycon_parse_imu_report()
1082 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1083 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1084 ctlr->imu_avg_delta_ms = JC_IMU_DFLT_AVG_DELTA_MS; in joycon_parse_imu_report()
1085 ctlr->imu_first_packet_received = true; in joycon_parse_imu_report()
1087 unsigned int delta = msecs - last_msecs; in joycon_parse_imu_report()
1092 ctlr->imu_delta_samples_sum += delta; in joycon_parse_imu_report()
1093 ctlr->imu_delta_samples_count++; in joycon_parse_imu_report()
1094 if (ctlr->imu_delta_samples_count >= in joycon_parse_imu_report()
1096 ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / in joycon_parse_imu_report()
1097 ctlr->imu_delta_samples_count; in joycon_parse_imu_report()
1099 if (ctlr->imu_avg_delta_ms == 0) { in joycon_parse_imu_report()
1100 ctlr->imu_avg_delta_ms = 1; in joycon_parse_imu_report()
1101 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1104 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1105 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1109 hid_dbg(ctlr->hdev, in joycon_parse_imu_report()
1111 msecs, last_msecs, delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1114 dropped_threshold = ctlr->imu_avg_delta_ms * 3 / 2; in joycon_parse_imu_report()
1115 dropped_pkts = (delta - min(delta, dropped_threshold)) / in joycon_parse_imu_report()
1116 ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1117 ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1119 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1122 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1124 delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1127 ctlr->imu_last_pkt_ms = msecs; in joycon_parse_imu_report()
1130 for (i = 0; i < 3; i++) { in joycon_parse_imu_report()
1132 ctlr->imu_timestamp_us); in joycon_parse_imu_report()
1137 * found in the community's reverse-engineering repo (linked at in joycon_parse_imu_report()
1138 * top of driver). For hid-nintendo, we make sure that the final in joycon_parse_imu_report()
1139 * value given to userspace is always in terms of the axis in joycon_parse_imu_report()
1147 * precision-saving scaling factor to prevent large inaccuracies in joycon_parse_imu_report()
1153 (imu_data[i].gyro_x - in joycon_parse_imu_report()
1154 ctlr->gyro_cal.offset[0])), in joycon_parse_imu_report()
1155 ctlr->gyro_cal.scale[0], in joycon_parse_imu_report()
1156 ctlr->imu_cal_gyro_divisor[0]); in joycon_parse_imu_report()
1158 (imu_data[i].gyro_y - in joycon_parse_imu_report()
1159 ctlr->gyro_cal.offset[1])), in joycon_parse_imu_report()
1160 ctlr->gyro_cal.scale[1], in joycon_parse_imu_report()
1161 ctlr->imu_cal_gyro_divisor[1]); in joycon_parse_imu_report()
1163 (imu_data[i].gyro_z - in joycon_parse_imu_report()
1164 ctlr->gyro_cal.offset[2])), in joycon_parse_imu_report()
1165 ctlr->gyro_cal.scale[2], in joycon_parse_imu_report()
1166 ctlr->imu_cal_gyro_divisor[2]); in joycon_parse_imu_report()
1168 value[3] = ((s32)imu_data[i].accel_x * in joycon_parse_imu_report()
1169 ctlr->accel_cal.scale[0]) / in joycon_parse_imu_report()
1170 ctlr->imu_cal_accel_divisor[0]; in joycon_parse_imu_report()
1172 ctlr->accel_cal.scale[1]) / in joycon_parse_imu_report()
1173 ctlr->imu_cal_accel_divisor[1]; in joycon_parse_imu_report()
1175 ctlr->accel_cal.scale[2]) / in joycon_parse_imu_report()
1176 ctlr->imu_cal_accel_divisor[2]; in joycon_parse_imu_report()
1178 hid_dbg(ctlr->hdev, "raw_gyro: g_x=%d g_y=%d g_z=%d\n", in joycon_parse_imu_report()
1181 hid_dbg(ctlr->hdev, "raw_accel: a_x=%d a_y=%d a_z=%d\n", in joycon_parse_imu_report()
1186 * The right joy-con has 2 axes negated, Y and Z. This is due to in joycon_parse_imu_report()
1188 * axes' values in order to be consistent with the left joy-con in joycon_parse_imu_report()
1193 * The axes follow the right-hand rule. in joycon_parse_imu_report()
1198 /* negate all but x axis */ in joycon_parse_imu_report()
1200 if (j == 3) in joycon_parse_imu_report()
1202 value[j] *= -1; in joycon_parse_imu_report()
1209 input_report_abs(idev, ABS_X, value[3]); in joycon_parse_imu_report()
1213 /* convert to micros and divide by 3 (3 samples per report). */ in joycon_parse_imu_report()
1214 ctlr->imu_timestamp_us += ctlr->imu_avg_delta_ms * 1000 / 3; in joycon_parse_imu_report()
1221 struct input_dev *dev = ctlr->input; in joycon_parse_report()
1227 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1228 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report && in joycon_parse_report()
1229 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED && in joycon_parse_report()
1230 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS && in joycon_parse_report()
1231 (ctlr->rumble_queue_head != ctlr->rumble_queue_tail || in joycon_parse_report()
1232 ctlr->rumble_zero_countdown > 0)) { in joycon_parse_report()
1239 if (ctlr->rumble_zero_countdown > 0) in joycon_parse_report()
1240 ctlr->rumble_zero_countdown--; in joycon_parse_report()
1241 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_parse_report()
1245 tmp = rep->bat_con; in joycon_parse_report()
1246 ctlr->host_powered = tmp & BIT(0); in joycon_parse_report()
1247 ctlr->battery_charging = tmp & BIT(4); in joycon_parse_report()
1251 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; in joycon_parse_report()
1254 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; in joycon_parse_report()
1257 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; in joycon_parse_report()
1259 case 3: /* high */ in joycon_parse_report()
1260 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; in joycon_parse_report()
1263 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; in joycon_parse_report()
1266 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_parse_report()
1267 hid_warn(ctlr->hdev, "Invalid battery status\n"); in joycon_parse_report()
1270 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1273 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_parse_report()
1282 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_parse_report()
1283 raw_y = hid_field_extract(ctlr->hdev, in joycon_parse_report()
1284 rep->left_stick + 1, 4, 12); in joycon_parse_report()
1286 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_parse_report()
1287 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_parse_report()
1300 /* Report the S buttons as the non-existent triggers */ in joycon_parse_report()
1304 /* Report d-pad as digital buttons for the joy-cons */ in joycon_parse_report()
1316 /* d-pad x */ in joycon_parse_report()
1318 hatx = -1; in joycon_parse_report()
1323 /* d-pad y */ in joycon_parse_report()
1325 haty = -1; in joycon_parse_report()
1338 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_parse_report()
1339 raw_y = hid_field_extract(ctlr->hdev, in joycon_parse_report()
1340 rep->right_stick + 1, 4, 12); in joycon_parse_report()
1342 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_parse_report()
1343 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_parse_report()
1352 /* Report the S buttons as the non-existent triggers */ in joycon_parse_report()
1372 if (unlikely(mutex_is_locked(&ctlr->output_mutex))) { in joycon_parse_report()
1373 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1374 ctlr->received_input_report = true; in joycon_parse_report()
1375 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1376 wake_up(&ctlr->wait); in joycon_parse_report()
1380 if (rep->id == JC_INPUT_IMU_DATA) in joycon_parse_report()
1390 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_rumble_data()
1395 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_rumble_data()
1396 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1397 return -ENODEV; in joycon_send_rumble_data()
1400 ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_rumble_data()
1402 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1405 rumble_output.packet_num = ctlr->subcmd_num; in joycon_send_rumble_data()
1406 if (++ctlr->subcmd_num > 0xF) in joycon_send_rumble_data()
1407 ctlr->subcmd_num = 0; in joycon_send_rumble_data()
1411 ret = __joycon_hid_send(ctlr->hdev, (u8 *)&rumble_output, in joycon_send_rumble_data()
1425 mutex_lock(&ctlr->output_mutex); in joycon_rumble_worker()
1427 mutex_unlock(&ctlr->output_mutex); in joycon_rumble_worker()
1429 /* -ENODEV means the controller was just unplugged */ in joycon_rumble_worker()
1430 spin_lock_irqsave(&ctlr->lock, flags); in joycon_rumble_worker()
1431 if (ret < 0 && ret != -ENODEV && in joycon_rumble_worker()
1432 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_rumble_worker()
1433 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret); in joycon_rumble_worker()
1435 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_rumble_worker()
1436 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) { in joycon_rumble_worker()
1437 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE) in joycon_rumble_worker()
1438 ctlr->rumble_queue_tail = 0; in joycon_rumble_worker()
1442 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_rumble_worker()
1454 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_freq()
1455 if (freq > data[i - 1].freq && freq <= data[i].freq) in joycon_find_rumble_freq()
1470 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_amp()
1471 if (amp > data[i - 1].amp && amp <= data[i].amp) in joycon_find_rumble_amp()
1492 data[3] = amp_data.low & 0xFF; in joycon_encode_rumble()
1504 spin_lock_irqsave(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1505 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, in joycon_clamp_rumble_freqs()
1508 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, in joycon_clamp_rumble_freqs()
1511 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, in joycon_clamp_rumble_freqs()
1514 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, in joycon_clamp_rumble_freqs()
1517 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1531 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1532 freq_r_low = ctlr->rumble_rl_freq; in joycon_set_rumble()
1533 freq_r_high = ctlr->rumble_rh_freq; in joycon_set_rumble()
1534 freq_l_low = ctlr->rumble_ll_freq; in joycon_set_rumble()
1535 freq_l_high = ctlr->rumble_lh_freq; in joycon_set_rumble()
1538 ctlr->rumble_zero_countdown = JC_RUMBLE_ZERO_AMP_PKT_CNT; in joycon_set_rumble()
1539 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1541 /* right joy-con */ in joycon_set_rumble()
1545 /* left joy-con */ in joycon_set_rumble()
1549 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1550 if (++ctlr->rumble_queue_head >= JC_RUMBLE_QUEUE_SIZE) in joycon_set_rumble()
1551 ctlr->rumble_queue_head = 0; in joycon_set_rumble()
1552 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, in joycon_set_rumble()
1556 if (schedule_now && ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_set_rumble()
1557 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_set_rumble()
1559 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1569 if (effect->type != FF_RUMBLE) in joycon_play_effect()
1573 effect->u.rumble.weak_magnitude, in joycon_play_effect()
1574 effect->u.rumble.strong_magnitude, in joycon_play_effect()
1592 /* We report joy-con d-pad inputs as buttons and pro controller as a hat. */
1606 hdev = ctlr->hdev; in joycon_input_create()
1608 switch (hdev->product) { in joycon_input_create()
1615 name = "Nintendo Switch Left Joy-Con (Grip)"; in joycon_input_create()
1616 imu_name = "Nintendo Switch Left Joy-Con IMU (Grip)"; in joycon_input_create()
1618 name = "Nintendo Switch Right Joy-Con (Grip)"; in joycon_input_create()
1619 imu_name = "Nintendo Switch Right Joy-Con IMU (Grip)"; in joycon_input_create()
1623 name = "Nintendo Switch Left Joy-Con"; in joycon_input_create()
1624 imu_name = "Nintendo Switch Left Joy-Con IMU"; in joycon_input_create()
1627 name = "Nintendo Switch Right Joy-Con"; in joycon_input_create()
1628 imu_name = "Nintendo Switch Right Joy-Con IMU"; in joycon_input_create()
1632 return -EINVAL; in joycon_input_create()
1635 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
1636 if (!ctlr->input) in joycon_input_create()
1637 return -ENOMEM; in joycon_input_create()
1638 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
1639 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
1640 ctlr->input->id.product = hdev->product; in joycon_input_create()
1641 ctlr->input->id.version = hdev->version; in joycon_input_create()
1642 ctlr->input->uniq = ctlr->mac_addr_str; in joycon_input_create()
1643 ctlr->input->name = name; in joycon_input_create()
1644 ctlr->input->phys = hdev->phys; in joycon_input_create()
1645 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
1649 input_set_abs_params(ctlr->input, ABS_X, in joycon_input_create()
1650 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1652 input_set_abs_params(ctlr->input, ABS_Y, in joycon_input_create()
1653 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1657 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
1660 /* configure d-pad differently for joy-con vs pro controller */ in joycon_input_create()
1661 if (hdev->product != USB_DEVICE_ID_NINTENDO_PROCON) { in joycon_input_create()
1663 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
1666 input_set_abs_params(ctlr->input, ABS_HAT0X, in joycon_input_create()
1667 -JC_MAX_DPAD_MAG, JC_MAX_DPAD_MAG, in joycon_input_create()
1669 input_set_abs_params(ctlr->input, ABS_HAT0Y, in joycon_input_create()
1670 -JC_MAX_DPAD_MAG, JC_MAX_DPAD_MAG, in joycon_input_create()
1675 input_set_abs_params(ctlr->input, ABS_RX, in joycon_input_create()
1676 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1678 input_set_abs_params(ctlr->input, ABS_RY, in joycon_input_create()
1679 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1683 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
1687 /* Let's report joy-con S triggers separately */ in joycon_input_create()
1688 if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL) { in joycon_input_create()
1689 input_set_capability(ctlr->input, EV_KEY, BTN_TR); in joycon_input_create()
1690 input_set_capability(ctlr->input, EV_KEY, BTN_TR2); in joycon_input_create()
1691 } else if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR) { in joycon_input_create()
1692 input_set_capability(ctlr->input, EV_KEY, BTN_TL); in joycon_input_create()
1693 input_set_capability(ctlr->input, EV_KEY, BTN_TL2); in joycon_input_create()
1698 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); in joycon_input_create()
1699 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); in joycon_input_create()
1700 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_input_create()
1701 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_input_create()
1702 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_input_create()
1703 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_input_create()
1706 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_input_create()
1709 ret = input_register_device(ctlr->input); in joycon_input_create()
1714 ctlr->imu_input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
1715 if (!ctlr->imu_input) in joycon_input_create()
1716 return -ENOMEM; in joycon_input_create()
1718 ctlr->imu_input->id.bustype = hdev->bus; in joycon_input_create()
1719 ctlr->imu_input->id.vendor = hdev->vendor; in joycon_input_create()
1720 ctlr->imu_input->id.product = hdev->product; in joycon_input_create()
1721 ctlr->imu_input->id.version = hdev->version; in joycon_input_create()
1722 ctlr->imu_input->uniq = ctlr->mac_addr_str; in joycon_input_create()
1723 ctlr->imu_input->name = imu_name; in joycon_input_create()
1724 ctlr->imu_input->phys = hdev->phys; in joycon_input_create()
1725 input_set_drvdata(ctlr->imu_input, ctlr); in joycon_input_create()
1728 input_set_abs_params(ctlr->imu_input, ABS_X, in joycon_input_create()
1729 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_input_create()
1731 input_set_abs_params(ctlr->imu_input, ABS_Y, in joycon_input_create()
1732 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_input_create()
1734 input_set_abs_params(ctlr->imu_input, ABS_Z, in joycon_input_create()
1735 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_input_create()
1737 input_abs_set_res(ctlr->imu_input, ABS_X, JC_IMU_ACCEL_RES_PER_G); in joycon_input_create()
1738 input_abs_set_res(ctlr->imu_input, ABS_Y, JC_IMU_ACCEL_RES_PER_G); in joycon_input_create()
1739 input_abs_set_res(ctlr->imu_input, ABS_Z, JC_IMU_ACCEL_RES_PER_G); in joycon_input_create()
1741 input_set_abs_params(ctlr->imu_input, ABS_RX, in joycon_input_create()
1742 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_input_create()
1744 input_set_abs_params(ctlr->imu_input, ABS_RY, in joycon_input_create()
1745 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_input_create()
1747 input_set_abs_params(ctlr->imu_input, ABS_RZ, in joycon_input_create()
1748 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_input_create()
1751 input_abs_set_res(ctlr->imu_input, ABS_RX, JC_IMU_GYRO_RES_PER_DPS); in joycon_input_create()
1752 input_abs_set_res(ctlr->imu_input, ABS_RY, JC_IMU_GYRO_RES_PER_DPS); in joycon_input_create()
1753 input_abs_set_res(ctlr->imu_input, ABS_RZ, JC_IMU_GYRO_RES_PER_DPS); in joycon_input_create()
1755 __set_bit(EV_MSC, ctlr->imu_input->evbit); in joycon_input_create()
1756 __set_bit(MSC_TIMESTAMP, ctlr->imu_input->mscbit); in joycon_input_create()
1757 __set_bit(INPUT_PROP_ACCELEROMETER, ctlr->imu_input->propbit); in joycon_input_create()
1759 ret = input_register_device(ctlr->imu_input); in joycon_input_create()
1769 struct device *dev = led->dev->parent; in joycon_player_led_brightness_set()
1780 return -ENODEV; in joycon_player_led_brightness_set()
1785 if (&ctlr->leds[num] == led) in joycon_player_led_brightness_set()
1789 return -EINVAL; in joycon_player_led_brightness_set()
1791 mutex_lock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
1796 val |= ctlr->leds[i].brightness << i; in joycon_player_led_brightness_set()
1799 mutex_unlock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
1807 struct device *dev = led->dev->parent; in joycon_home_led_brightness_set()
1818 return -ENODEV; in joycon_home_led_brightness_set()
1822 req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; in joycon_home_led_brightness_set()
1823 data = req->data; in joycon_home_led_brightness_set()
1827 data[3] = 0x11; in joycon_home_led_brightness_set()
1831 mutex_lock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
1833 mutex_unlock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
1841 struct hid_device *hdev = ctlr->hdev; in joycon_leds_create()
1842 struct device *dev = &hdev->dev; in joycon_leds_create()
1852 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
1853 ret = joycon_set_player_leds(ctlr, 0, 0xF >> (4 - input_num)); in joycon_leds_create()
1855 hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret); in joycon_leds_create()
1856 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
1866 return -ENOMEM; in joycon_leds_create()
1869 led = &ctlr->leds[i]; in joycon_leds_create()
1870 led->name = name; in joycon_leds_create()
1871 led->brightness = ((i + 1) <= input_num) ? 1 : 0; in joycon_leds_create()
1872 led->max_brightness = 1; in joycon_leds_create()
1873 led->brightness_set_blocking = in joycon_leds_create()
1875 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
1877 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
1879 hid_err(hdev, "Failed registering %s LED\n", led->name); in joycon_leds_create()
1896 return -ENOMEM; in joycon_leds_create()
1898 led = &ctlr->home_led; in joycon_leds_create()
1899 led->name = name; in joycon_leds_create()
1900 led->brightness = 0; in joycon_leds_create()
1901 led->max_brightness = 0xF; in joycon_leds_create()
1902 led->brightness_set_blocking = joycon_home_led_brightness_set; in joycon_leds_create()
1903 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
1904 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
1913 devm_led_classdev_unregister(&hdev->dev, led); in joycon_leds_create()
1931 spin_lock_irqsave(&ctlr->lock, flags); in joycon_battery_get_property()
1932 capacity = ctlr->battery_capacity; in joycon_battery_get_property()
1933 charging = ctlr->battery_charging; in joycon_battery_get_property()
1934 powered = ctlr->host_powered; in joycon_battery_get_property()
1935 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_battery_get_property()
1939 val->intval = 1; in joycon_battery_get_property()
1942 val->intval = POWER_SUPPLY_SCOPE_DEVICE; in joycon_battery_get_property()
1945 val->intval = capacity; in joycon_battery_get_property()
1949 val->intval = POWER_SUPPLY_STATUS_CHARGING; in joycon_battery_get_property()
1952 val->intval = POWER_SUPPLY_STATUS_FULL; in joycon_battery_get_property()
1954 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in joycon_battery_get_property()
1957 ret = -EINVAL; in joycon_battery_get_property()
1972 struct hid_device *hdev = ctlr->hdev; in joycon_power_supply_create()
1978 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_power_supply_create()
1981 ctlr->battery_desc.properties = joycon_battery_props; in joycon_power_supply_create()
1982 ctlr->battery_desc.num_properties = in joycon_power_supply_create()
1984 ctlr->battery_desc.get_property = joycon_battery_get_property; in joycon_power_supply_create()
1985 ctlr->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; in joycon_power_supply_create()
1986 ctlr->battery_desc.use_for_apm = 0; in joycon_power_supply_create()
1987 ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, in joycon_power_supply_create()
1989 dev_name(&hdev->dev)); in joycon_power_supply_create()
1990 if (!ctlr->battery_desc.name) in joycon_power_supply_create()
1991 return -ENOMEM; in joycon_power_supply_create()
1993 ctlr->battery = devm_power_supply_register(&hdev->dev, in joycon_power_supply_create()
1994 &ctlr->battery_desc, in joycon_power_supply_create()
1996 if (IS_ERR(ctlr->battery)) { in joycon_power_supply_create()
1997 ret = PTR_ERR(ctlr->battery); in joycon_power_supply_create()
2002 return power_supply_powers(ctlr->battery, &hdev->dev); in joycon_power_supply_create()
2016 hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); in joycon_read_info()
2020 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_read_info()
2023 ctlr->mac_addr[j] = report->subcmd_reply.data[i]; in joycon_read_info()
2025 ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, in joycon_read_info()
2027 ctlr->mac_addr[0], in joycon_read_info()
2028 ctlr->mac_addr[1], in joycon_read_info()
2029 ctlr->mac_addr[2], in joycon_read_info()
2030 ctlr->mac_addr[3], in joycon_read_info()
2031 ctlr->mac_addr[4], in joycon_read_info()
2032 ctlr->mac_addr[5]); in joycon_read_info()
2033 if (!ctlr->mac_addr_str) in joycon_read_info()
2034 return -ENOMEM; in joycon_read_info()
2035 hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); in joycon_read_info()
2038 ctlr->ctlr_type = report->subcmd_reply.data[2]; in joycon_read_info()
2064 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
2065 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
2066 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
2071 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
2079 if (report->subcmd_reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
2087 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
2089 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
2090 ctlr->received_resp = true; in joycon_ctlr_handle_event()
2091 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
2098 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
2110 return -EINVAL; in nintendo_hid_event()
2121 hid_dbg(hdev, "probe - start\n"); in nintendo_hid_probe()
2123 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
2125 ret = -ENOMEM; in nintendo_hid_probe()
2129 ctlr->hdev = hdev; in nintendo_hid_probe()
2130 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
2131 ctlr->rumble_queue_head = JC_RUMBLE_QUEUE_SIZE - 1; in nintendo_hid_probe()
2132 ctlr->rumble_queue_tail = 0; in nintendo_hid_probe()
2134 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
2135 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
2136 spin_lock_init(&ctlr->lock); in nintendo_hid_probe()
2137 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", in nintendo_hid_probe()
2139 if (!ctlr->rumble_queue) { in nintendo_hid_probe()
2140 ret = -ENOMEM; in nintendo_hid_probe()
2143 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); in nintendo_hid_probe()
2158 hdev->version |= 0x8000; in nintendo_hid_probe()
2175 mutex_lock(&ctlr->output_mutex); in nintendo_hid_probe()
2199 ret = -ETIMEDOUT; in nintendo_hid_probe()
2251 mutex_unlock(&ctlr->output_mutex); in nintendo_hid_probe()
2273 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
2275 hid_dbg(hdev, "probe - success\n"); in nintendo_hid_probe()
2279 mutex_unlock(&ctlr->output_mutex); in nintendo_hid_probe()
2285 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_probe()
2287 hid_err(hdev, "probe - fail = %d\n", ret); in nintendo_hid_probe()
2299 spin_lock_irqsave(&ctlr->lock, flags); in nintendo_hid_remove()
2300 ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED; in nintendo_hid_remove()
2301 spin_unlock_irqrestore(&ctlr->lock, flags); in nintendo_hid_remove()
2303 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_remove()