Lines Matching +full:report +full:- +full:rate +full:- +full:hz

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>
12 * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
16 * hid-wiimote kernel hid driver
17 * hid-logitech-hidpp driver
18 * hid-sony driver
20 * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
31 #include "hid-ids.h"
46 * Reference the url below for the following HID report defines:
120 (JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
130 (JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
140 /* Hat values for pro controller's d-pad */
147 /* How many samples to sum before calculating average IMU report delta */
154 * configured with a range of +-8000 milliGs. Therefore, the resolution can be
155 * calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
166 * configured with a range of +-2000 degrees/second.
167 * Digits per dps: (2^16 -1)/(2000*2) = 16.38375
168 * dps per digit: 16.38375E-1 = .0610
174 * digits per dps (corrected): .0702E-1 = 14.247
181 #define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
190 u16 freq; /* Hz*/
369 * D-pad is configured as buttons for the left Joy-Con only!
385 * The unused *right*-side triggers become the SL/SR triggers for the *left*
386 * Joy-Con, if and only if we're not using a charging grip.
408 * The unused *left*-side triggers become the SL/SR triggers for the *right*
409 * Joy-Con, if and only if we're not using a charging grip.
475 * N64's C buttons get assigned to d-pad directions and registered as buttons.
632 unsigned int imu_timestamp_us; /* timestamp we report to userspace */
633 unsigned int imu_last_pkt_ms; /* used to calc imu report delta */
634 /* the following are used to track the average imu report time delta */
642 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
643 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
644 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
646 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
648 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
652 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
653 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO || \
654 ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64)
658 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
659 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
665 * but take caution: some NSO devices lie about themselves (NES Joy-Cons and
673 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP; in joycon_device_is_chrggrip()
679 * These are slightly different than the device-ID-based helpers above. They are
681 * versus SNES, or NES Joy-Cons versus regular Switch Joy-Cons. They're most
691 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL; in joycon_type_is_left_joycon()
696 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR; in joycon_type_is_right_joycon()
701 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO; in joycon_type_is_procon()
706 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_SNES; in joycon_type_is_snescon()
711 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_GEN; in joycon_type_is_gencon()
716 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64; in joycon_type_is_n64con()
721 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESL; in joycon_type_is_left_nescon()
726 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESR; in joycon_type_is_right_nescon()
774 return ctlr->hdev->bus == BUS_USB; in joycon_using_usb()
784 return -ENOMEM; in __joycon_hid_send()
788 hid_dbg(hdev, "Failed to send output report ret=%d\n", ret); in __joycon_hid_send()
798 * report prior to sending the subcommand. This improves in joycon_wait_for_input_report()
801 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_wait_for_input_report()
804 spin_lock_irqsave(&ctlr->lock, flags); in joycon_wait_for_input_report()
805 ctlr->received_input_report = false; in joycon_wait_for_input_report()
806 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_wait_for_input_report()
807 ret = wait_event_timeout(ctlr->wait, in joycon_wait_for_input_report()
808 ctlr->received_input_report, in joycon_wait_for_input_report()
809 HZ / 4); in joycon_wait_for_input_report()
812 hid_warn(ctlr->hdev, in joycon_wait_for_input_report()
813 "timeout waiting for input report\n"); in joycon_wait_for_input_report()
818 * Sending subcommands and/or rumble data at too high a rate can cause bluetooth
828 #define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_…
837 if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ)) in joycon_enforce_subcmd_rate()
843 subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
845 spin_lock_irqsave(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
846 consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas; in joycon_enforce_subcmd_rate()
847 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
852 ctlr->ctlr_state == JOYCON_CTLR_STATE_READ && in joycon_enforce_subcmd_rate()
856 hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__); in joycon_enforce_subcmd_rate()
860 ctlr->last_subcmd_sent_msecs = current_ms; in joycon_enforce_subcmd_rate()
863 * Wait a short time after receiving an input report before in joycon_enforce_subcmd_rate()
866 * the rate of disconnections. in joycon_enforce_subcmd_rate()
881 while (tries--) { in joycon_hid_send_sync()
884 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
886 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
890 ret = wait_event_timeout(ctlr->wait, ctlr->received_resp, in joycon_hid_send_sync()
893 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
896 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
899 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
900 ret = -ETIMEDOUT; in joycon_hid_send_sync()
907 ctlr->received_resp = false; in joycon_hid_send_sync()
917 ctlr->usb_ack_match = cmd; in joycon_send_usb()
918 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
921 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
932 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_subcmd()
937 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_subcmd()
938 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
939 return -ENODEV; in joycon_send_subcmd()
941 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_subcmd()
943 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
945 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; in joycon_send_subcmd()
946 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
947 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
948 ctlr->subcmd_num = 0; in joycon_send_subcmd()
949 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
950 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
955 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
968 req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; in joycon_set_player_leds()
969 req->data[0] = (flash << 4) | on; in joycon_set_player_leds()
971 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
972 return joycon_send_subcmd(ctlr, req, 1, HZ/4); in joycon_set_player_leds()
982 req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; in joycon_set_home_led()
983 data = req->data; in joycon_set_home_led()
990 hid_dbg(ctlr->hdev, "setting home led brightness\n"); in joycon_set_home_led()
991 return joycon_send_subcmd(ctlr, req, 5, HZ/4); in joycon_set_home_led()
998 struct joycon_input_report *report; in joycon_request_spi_flash_read() local
1004 return -EINVAL; in joycon_request_spi_flash_read()
1007 req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; in joycon_request_spi_flash_read()
1008 data = req->data; in joycon_request_spi_flash_read()
1012 hid_dbg(ctlr->hdev, "requesting SPI flash data\n"); in joycon_request_spi_flash_read()
1013 ret = joycon_send_subcmd(ctlr, req, 5, HZ); in joycon_request_spi_flash_read()
1015 hid_err(ctlr->hdev, "failed reading SPI flash; ret=%d\n", ret); in joycon_request_spi_flash_read()
1017 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_spi_flash_read()
1019 *reply = &report->subcmd_reply.data[5]; in joycon_request_spi_flash_read()
1060 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1062 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1064 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1066 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1068 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1070 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1073 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1075 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1077 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1079 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1081 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1083 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1087 cal_x->max = cal_x->center + x_max_above; in joycon_read_stick_calibration()
1088 cal_x->min = cal_x->center - x_min_below; in joycon_read_stick_calibration()
1089 cal_y->max = cal_y->center + y_max_above; in joycon_read_stick_calibration()
1090 cal_y->min = cal_y->center - y_min_below; in joycon_read_stick_calibration()
1093 if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || in joycon_read_stick_calibration()
1094 cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) in joycon_read_stick_calibration()
1095 ret = -EINVAL; in joycon_read_stick_calibration()
1112 cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; in joycon_use_default_calibration()
1113 cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; in joycon_use_default_calibration()
1114 cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; in joycon_use_default_calibration()
1123 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
1128 hid_info(ctlr->hdev, "using user cal for left stick\n"); in joycon_request_calibration()
1130 hid_info(ctlr->hdev, "using factory cal for left stick\n"); in joycon_request_calibration()
1134 hid_info(ctlr->hdev, "using user cal for right stick\n"); in joycon_request_calibration()
1136 hid_info(ctlr->hdev, "using factory cal for right stick\n"); in joycon_request_calibration()
1141 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1142 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1146 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1147 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1148 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1153 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1154 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1158 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1159 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1160 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1163 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
1168 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
1169 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
1170 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
1171 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
1172 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
1173 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
1174 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
1175 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
1176 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
1177 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
1178 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
1179 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
1194 ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1195 ctlr->accel_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1196 ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1197 ctlr->gyro_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1199 if (ctlr->imu_cal_accel_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1200 ctlr->imu_cal_accel_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1204 if (ctlr->imu_cal_gyro_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1205 ctlr->imu_cal_gyro_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1211 hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz); in joycon_calc_imu_cal_divisors()
1228 hid_info(ctlr->hdev, "using user cal for IMU\n"); in joycon_request_imu_calibration()
1230 hid_info(ctlr->hdev, "using factory cal for IMU\n"); in joycon_request_imu_calibration()
1234 hid_dbg(ctlr->hdev, "requesting IMU cal data\n"); in joycon_request_imu_calibration()
1238 hid_warn(ctlr->hdev, in joycon_request_imu_calibration()
1243 ctlr->accel_cal.offset[i] = DFLT_ACCEL_OFFSET; in joycon_request_imu_calibration()
1244 ctlr->accel_cal.scale[i] = DFLT_ACCEL_SCALE; in joycon_request_imu_calibration()
1245 ctlr->gyro_cal.offset[i] = DFLT_GYRO_OFFSET; in joycon_request_imu_calibration()
1246 ctlr->gyro_cal.scale[i] = DFLT_GYRO_SCALE; in joycon_request_imu_calibration()
1256 ctlr->accel_cal.offset[i] = get_unaligned_le16(raw_cal + j); in joycon_request_imu_calibration()
1257 ctlr->accel_cal.scale[i] = get_unaligned_le16(raw_cal + j + 6); in joycon_request_imu_calibration()
1258 ctlr->gyro_cal.offset[i] = get_unaligned_le16(raw_cal + j + 12); in joycon_request_imu_calibration()
1259 ctlr->gyro_cal.scale[i] = get_unaligned_le16(raw_cal + j + 18); in joycon_request_imu_calibration()
1264 hid_dbg(ctlr->hdev, "IMU calibration:\n" in joycon_request_imu_calibration()
1269 ctlr->accel_cal.offset[0], in joycon_request_imu_calibration()
1270 ctlr->accel_cal.offset[1], in joycon_request_imu_calibration()
1271 ctlr->accel_cal.offset[2], in joycon_request_imu_calibration()
1272 ctlr->accel_cal.scale[0], in joycon_request_imu_calibration()
1273 ctlr->accel_cal.scale[1], in joycon_request_imu_calibration()
1274 ctlr->accel_cal.scale[2], in joycon_request_imu_calibration()
1275 ctlr->gyro_cal.offset[0], in joycon_request_imu_calibration()
1276 ctlr->gyro_cal.offset[1], in joycon_request_imu_calibration()
1277 ctlr->gyro_cal.offset[2], in joycon_request_imu_calibration()
1278 ctlr->gyro_cal.scale[0], in joycon_request_imu_calibration()
1279 ctlr->gyro_cal.scale[1], in joycon_request_imu_calibration()
1280 ctlr->gyro_cal.scale[2]); in joycon_request_imu_calibration()
1291 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; in joycon_set_report_mode()
1292 req->data[0] = 0x30; /* standard, full report mode */ in joycon_set_report_mode()
1294 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
1295 return joycon_send_subcmd(ctlr, req, 1, HZ); in joycon_set_report_mode()
1304 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; in joycon_enable_rumble()
1305 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_rumble()
1307 hid_dbg(ctlr->hdev, "enabling rumble\n"); in joycon_enable_rumble()
1308 return joycon_send_subcmd(ctlr, req, 1, HZ/4); in joycon_enable_rumble()
1317 req->subcmd_id = JC_SUBCMD_ENABLE_IMU; in joycon_enable_imu()
1318 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_imu()
1320 hid_dbg(ctlr->hdev, "enabling IMU\n"); in joycon_enable_imu()
1321 return joycon_send_subcmd(ctlr, req, 1, HZ); in joycon_enable_imu()
1326 s32 center = cal->center; in joycon_map_stick_val()
1327 s32 min = cal->min; in joycon_map_stick_val()
1328 s32 max = cal->max; in joycon_map_stick_val()
1332 new_val = (val - center) * JC_MAX_STICK_MAG; in joycon_map_stick_val()
1333 new_val /= (max - center); in joycon_map_stick_val()
1335 new_val = (center - val) * -JC_MAX_STICK_MAG; in joycon_map_stick_val()
1336 new_val /= (center - min); in joycon_map_stick_val()
1338 new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG); in joycon_map_stick_val()
1346 u8 *raw = rep->imu_raw_bytes; in joycon_input_report_parse_imu_data()
1352 data->accel_x = get_unaligned_le16(raw + 0); in joycon_input_report_parse_imu_data()
1353 data->accel_y = get_unaligned_le16(raw + 2); in joycon_input_report_parse_imu_data()
1354 data->accel_z = get_unaligned_le16(raw + 4); in joycon_input_report_parse_imu_data()
1355 data->gyro_x = get_unaligned_le16(raw + 6); in joycon_input_report_parse_imu_data()
1356 data->gyro_y = get_unaligned_le16(raw + 8); in joycon_input_report_parse_imu_data()
1357 data->gyro_z = get_unaligned_le16(raw + 10); in joycon_input_report_parse_imu_data()
1367 struct input_dev *idev = ctlr->imu_input; in joycon_parse_imu_report()
1369 unsigned int last_msecs = ctlr->imu_last_pkt_ms; in joycon_parse_imu_report()
1379 * incrementing 8-bit counter per input report, but it is not very in joycon_parse_imu_report()
1380 * useful for this purpose (it is not entirely clear what rate it in joycon_parse_imu_report()
1381 * increments at or if it varies based on packet push rate - more on in joycon_parse_imu_report()
1382 * the push rate below...). in joycon_parse_imu_report()
1384 * The reverse engineering work done on the joy-cons and pro controllers in joycon_parse_imu_report()
1386 * - The controller samples the IMU every 1.35ms. It then does some of in joycon_parse_imu_report()
1388 * - Each imu input report contains 3 IMU samples, (usually 5ms apart). in joycon_parse_imu_report()
1389 * - In the standard reporting mode (which this driver uses exclusively) in joycon_parse_imu_report()
1391 * * joy-con (bluetooth): every 15 ms in joycon_parse_imu_report()
1392 * * joy-cons (in charging grip via USB): every 15 ms in joycon_parse_imu_report()
1397 * to alter the controller's packet rate by hardcoding the bluetooth in joycon_parse_imu_report()
1399 * SSR to 11ms for both the joy-cons and pro controllers). in joycon_parse_imu_report()
1402 * reports IMU sample batches every 11ms or every 15ms. This rate is in joycon_parse_imu_report()
1404 * rate. Importantly, even when sending every 11ms, none of the samples in joycon_parse_imu_report()
1406 * reported samples can vary based on the input report rate. in joycon_parse_imu_report()
1415 * Keeping track of the average report delta allows us to submit our in joycon_parse_imu_report()
1416 * timestamps to userspace based on that. Each report contains 3 in joycon_parse_imu_report()
1417 * samples, so the IMU sampling rate should be avg_time_delta/3. We can in joycon_parse_imu_report()
1422 if (!ctlr->imu_first_packet_received) { in joycon_parse_imu_report()
1423 ctlr->imu_timestamp_us = 0; in joycon_parse_imu_report()
1424 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1425 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1426 ctlr->imu_avg_delta_ms = JC_IMU_DFLT_AVG_DELTA_MS; in joycon_parse_imu_report()
1427 ctlr->imu_first_packet_received = true; in joycon_parse_imu_report()
1429 unsigned int delta = msecs - last_msecs; in joycon_parse_imu_report()
1433 /* avg imu report delta housekeeping */ in joycon_parse_imu_report()
1434 ctlr->imu_delta_samples_sum += delta; in joycon_parse_imu_report()
1435 ctlr->imu_delta_samples_count++; in joycon_parse_imu_report()
1436 if (ctlr->imu_delta_samples_count >= in joycon_parse_imu_report()
1438 ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / in joycon_parse_imu_report()
1439 ctlr->imu_delta_samples_count; in joycon_parse_imu_report()
1440 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1441 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1445 if (ctlr->imu_avg_delta_ms == 0) { in joycon_parse_imu_report()
1446 ctlr->imu_avg_delta_ms = 1; in joycon_parse_imu_report()
1447 hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n"); in joycon_parse_imu_report()
1450 /* useful for debugging IMU sample rate */ in joycon_parse_imu_report()
1451 hid_dbg(ctlr->hdev, in joycon_parse_imu_report()
1453 msecs, last_msecs, delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1456 dropped_threshold = ctlr->imu_avg_delta_ms * 3 / 2; in joycon_parse_imu_report()
1457 dropped_pkts = (delta - min(delta, dropped_threshold)) / in joycon_parse_imu_report()
1458 ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1459 ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1461 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1464 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1466 delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1469 ctlr->imu_last_pkt_ms = msecs; in joycon_parse_imu_report()
1471 /* Each IMU input report contains three samples */ in joycon_parse_imu_report()
1474 ctlr->imu_timestamp_us); in joycon_parse_imu_report()
1479 * found in the community's reverse-engineering repo (linked at in joycon_parse_imu_report()
1480 * top of driver). For hid-nintendo, we make sure that the final in joycon_parse_imu_report()
1489 * precision-saving scaling factor to prevent large inaccuracies in joycon_parse_imu_report()
1495 (imu_data[i].gyro_x - in joycon_parse_imu_report()
1496 ctlr->gyro_cal.offset[0])), in joycon_parse_imu_report()
1497 ctlr->gyro_cal.scale[0], in joycon_parse_imu_report()
1498 ctlr->imu_cal_gyro_divisor[0]); in joycon_parse_imu_report()
1500 (imu_data[i].gyro_y - in joycon_parse_imu_report()
1501 ctlr->gyro_cal.offset[1])), in joycon_parse_imu_report()
1502 ctlr->gyro_cal.scale[1], in joycon_parse_imu_report()
1503 ctlr->imu_cal_gyro_divisor[1]); in joycon_parse_imu_report()
1505 (imu_data[i].gyro_z - in joycon_parse_imu_report()
1506 ctlr->gyro_cal.offset[2])), in joycon_parse_imu_report()
1507 ctlr->gyro_cal.scale[2], in joycon_parse_imu_report()
1508 ctlr->imu_cal_gyro_divisor[2]); in joycon_parse_imu_report()
1511 ctlr->accel_cal.scale[0]) / in joycon_parse_imu_report()
1512 ctlr->imu_cal_accel_divisor[0]; in joycon_parse_imu_report()
1514 ctlr->accel_cal.scale[1]) / in joycon_parse_imu_report()
1515 ctlr->imu_cal_accel_divisor[1]; in joycon_parse_imu_report()
1517 ctlr->accel_cal.scale[2]) / in joycon_parse_imu_report()
1518 ctlr->imu_cal_accel_divisor[2]; in joycon_parse_imu_report()
1520 hid_dbg(ctlr->hdev, "raw_gyro: g_x=%d g_y=%d g_z=%d\n", in joycon_parse_imu_report()
1523 hid_dbg(ctlr->hdev, "raw_accel: a_x=%d a_y=%d a_z=%d\n", in joycon_parse_imu_report()
1528 * The right joy-con has 2 axes negated, Y and Z. This is due to in joycon_parse_imu_report()
1530 * axes' values in order to be consistent with the left joy-con in joycon_parse_imu_report()
1535 * The axes follow the right-hand rule. in joycon_parse_imu_report()
1544 value[j] *= -1; in joycon_parse_imu_report()
1555 /* convert to micros and divide by 3 (3 samples per report). */ in joycon_parse_imu_report()
1556 ctlr->imu_timestamp_us += ctlr->imu_avg_delta_ms * 1000 / 3; in joycon_parse_imu_report()
1565 spin_lock_irqsave(&ctlr->lock, flags); in joycon_handle_rumble_report()
1566 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report && in joycon_handle_rumble_report()
1567 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED && in joycon_handle_rumble_report()
1568 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS && in joycon_handle_rumble_report()
1569 (ctlr->rumble_queue_head != ctlr->rumble_queue_tail || in joycon_handle_rumble_report()
1570 ctlr->rumble_zero_countdown > 0)) { in joycon_handle_rumble_report()
1577 if (ctlr->rumble_zero_countdown > 0) in joycon_handle_rumble_report()
1578 ctlr->rumble_zero_countdown--; in joycon_handle_rumble_report()
1579 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_handle_rumble_report()
1582 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_handle_rumble_report()
1590 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_battery_status()
1592 tmp = rep->bat_con; in joycon_parse_battery_status()
1593 ctlr->host_powered = tmp & BIT(0); in joycon_parse_battery_status()
1594 ctlr->battery_charging = tmp & BIT(4); in joycon_parse_battery_status()
1599 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; in joycon_parse_battery_status()
1602 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; in joycon_parse_battery_status()
1605 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; in joycon_parse_battery_status()
1608 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; in joycon_parse_battery_status()
1611 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; in joycon_parse_battery_status()
1614 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_parse_battery_status()
1615 hid_warn(ctlr->hdev, "Invalid battery status\n"); in joycon_parse_battery_status()
1619 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_battery_status()
1630 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_report_left_stick()
1631 raw_y = hid_field_extract(ctlr->hdev, rep->left_stick + 1, 4, 12); in joycon_report_left_stick()
1633 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_report_left_stick()
1634 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_report_left_stick()
1636 input_report_abs(ctlr->input, ABS_X, x); in joycon_report_left_stick()
1637 input_report_abs(ctlr->input, ABS_Y, y); in joycon_report_left_stick()
1648 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_report_right_stick()
1649 raw_y = hid_field_extract(ctlr->hdev, rep->right_stick + 1, 4, 12); in joycon_report_right_stick()
1651 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_report_right_stick()
1652 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_report_right_stick()
1654 input_report_abs(ctlr->input, ABS_RX, x); in joycon_report_right_stick()
1655 input_report_abs(ctlr->input, ABS_RY, y); in joycon_report_right_stick()
1663 u32 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_dpad()
1666 hatx = -1; in joycon_report_dpad()
1671 haty = -1; in joycon_report_dpad()
1675 input_report_abs(ctlr->input, ABS_HAT0X, hatx); in joycon_report_dpad()
1676 input_report_abs(ctlr->input, ABS_HAT0Y, haty); in joycon_report_dpad()
1684 u32 status = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_buttons()
1686 for (button = button_mappings; button->code; button++) in joycon_report_buttons()
1687 input_report_key(ctlr->input, button->code, status & button->bit); in joycon_report_buttons()
1695 unsigned long report_delta_ms = msecs - ctlr->last_input_report_msecs; in joycon_parse_report()
1732 input_sync(ctlr->input); in joycon_parse_report()
1734 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1735 ctlr->last_input_report_msecs = msecs; in joycon_parse_report()
1737 * Was this input report a reasonable time delta compared to the prior in joycon_parse_report()
1738 * report? We use this information to decide when a safe time is to send in joycon_parse_report()
1743 if (ctlr->consecutive_valid_report_deltas < JC_SUBCMD_VALID_DELTA_REQ) in joycon_parse_report()
1744 ctlr->consecutive_valid_report_deltas++; in joycon_parse_report()
1746 ctlr->consecutive_valid_report_deltas = 0; in joycon_parse_report()
1749 * Our consecutive valid report tracking is only relevant for in joycon_parse_report()
1750 * bluetooth-connected controllers. For USB devices, we're beholden to in joycon_parse_report()
1751 * USB's underlying polling rate anyway. Always set to the consecutive in joycon_parse_report()
1754 if (ctlr->hdev->bus == BUS_USB) in joycon_parse_report()
1755 ctlr->consecutive_valid_report_deltas = JC_SUBCMD_VALID_DELTA_REQ; in joycon_parse_report()
1757 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1760 * Immediately after receiving a report is the most reliable time to in joycon_parse_report()
1762 * waiting for a report. in joycon_parse_report()
1764 if (unlikely(mutex_is_locked(&ctlr->output_mutex))) { in joycon_parse_report()
1765 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1766 ctlr->received_input_report = true; in joycon_parse_report()
1767 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1768 wake_up(&ctlr->wait); in joycon_parse_report()
1772 if ((rep->id == JC_INPUT_IMU_DATA) && joycon_has_imu(ctlr)) in joycon_parse_report()
1782 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_rumble_data()
1787 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_rumble_data()
1788 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1789 return -ENODEV; in joycon_send_rumble_data()
1792 ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_rumble_data()
1794 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1797 rumble_output.packet_num = ctlr->subcmd_num; in joycon_send_rumble_data()
1798 if (++ctlr->subcmd_num > 0xF) in joycon_send_rumble_data()
1799 ctlr->subcmd_num = 0; in joycon_send_rumble_data()
1803 ret = __joycon_hid_send(ctlr->hdev, (u8 *)&rumble_output, in joycon_send_rumble_data()
1817 mutex_lock(&ctlr->output_mutex); in joycon_rumble_worker()
1819 mutex_unlock(&ctlr->output_mutex); in joycon_rumble_worker()
1821 /* -ENODEV means the controller was just unplugged */ in joycon_rumble_worker()
1822 spin_lock_irqsave(&ctlr->lock, flags); in joycon_rumble_worker()
1823 if (ret < 0 && ret != -ENODEV && in joycon_rumble_worker()
1824 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_rumble_worker()
1825 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret); in joycon_rumble_worker()
1827 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_rumble_worker()
1828 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) { in joycon_rumble_worker()
1829 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE) in joycon_rumble_worker()
1830 ctlr->rumble_queue_tail = 0; in joycon_rumble_worker()
1834 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_rumble_worker()
1846 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_freq()
1847 if (freq > data[i - 1].freq && freq <= data[i].freq) in joycon_find_rumble_freq()
1862 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_amp()
1863 if (amp > data[i - 1].amp && amp <= data[i].amp) in joycon_find_rumble_amp()
1896 spin_lock_irqsave(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1897 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, in joycon_clamp_rumble_freqs()
1900 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, in joycon_clamp_rumble_freqs()
1903 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, in joycon_clamp_rumble_freqs()
1906 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, in joycon_clamp_rumble_freqs()
1909 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1924 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1925 freq_r_low = ctlr->rumble_rl_freq; in joycon_set_rumble()
1926 freq_r_high = ctlr->rumble_rh_freq; in joycon_set_rumble()
1927 freq_l_low = ctlr->rumble_ll_freq; in joycon_set_rumble()
1928 freq_l_high = ctlr->rumble_lh_freq; in joycon_set_rumble()
1931 ctlr->rumble_zero_countdown = JC_RUMBLE_ZERO_AMP_PKT_CNT; in joycon_set_rumble()
1932 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1934 /* right joy-con */ in joycon_set_rumble()
1938 /* left joy-con */ in joycon_set_rumble()
1942 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1944 next_rq_head = ctlr->rumble_queue_head + 1; in joycon_set_rumble()
1951 if (next_rq_head == ctlr->rumble_queue_tail) { in joycon_set_rumble()
1952 hid_dbg(ctlr->hdev, "rumble queue is full"); in joycon_set_rumble()
1954 next_rq_head = ctlr->rumble_queue_head; in joycon_set_rumble()
1957 ctlr->rumble_queue_head = next_rq_head; in joycon_set_rumble()
1958 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, in joycon_set_rumble()
1962 if (schedule_now && ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_set_rumble()
1963 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_set_rumble()
1965 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1975 if (effect->type != FF_RUMBLE) in joycon_play_effect()
1979 effect->u.rumble.weak_magnitude, in joycon_play_effect()
1980 effect->u.rumble.strong_magnitude, in joycon_play_effect()
1989 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
1995 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
2005 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2011 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2021 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2027 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2038 for (button = button_mappings; button->code; button++) in joycon_config_buttons()
2039 input_set_capability(idev, EV_KEY, button->code); in joycon_config_buttons()
2046 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); in joycon_config_rumble()
2047 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); in joycon_config_rumble()
2048 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2049 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2050 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2051 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2054 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_config_rumble()
2064 hdev = ctlr->hdev; in joycon_imu_input_create()
2067 ctlr->imu_input = devm_input_allocate_device(&hdev->dev); in joycon_imu_input_create()
2068 if (!ctlr->imu_input) in joycon_imu_input_create()
2069 return -ENOMEM; in joycon_imu_input_create()
2071 ctlr->imu_input->id.bustype = hdev->bus; in joycon_imu_input_create()
2072 ctlr->imu_input->id.vendor = hdev->vendor; in joycon_imu_input_create()
2073 ctlr->imu_input->id.product = hdev->product; in joycon_imu_input_create()
2074 ctlr->imu_input->id.version = hdev->version; in joycon_imu_input_create()
2075 ctlr->imu_input->uniq = ctlr->mac_addr_str; in joycon_imu_input_create()
2076 ctlr->imu_input->phys = hdev->phys; in joycon_imu_input_create()
2078 imu_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s (IMU)", ctlr->input->name); in joycon_imu_input_create()
2080 return -ENOMEM; in joycon_imu_input_create()
2082 ctlr->imu_input->name = imu_name; in joycon_imu_input_create()
2084 input_set_drvdata(ctlr->imu_input, ctlr); in joycon_imu_input_create()
2087 input_set_abs_params(ctlr->imu_input, ABS_X, in joycon_imu_input_create()
2088 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2090 input_set_abs_params(ctlr->imu_input, ABS_Y, in joycon_imu_input_create()
2091 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2093 input_set_abs_params(ctlr->imu_input, ABS_Z, in joycon_imu_input_create()
2094 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2096 input_abs_set_res(ctlr->imu_input, ABS_X, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2097 input_abs_set_res(ctlr->imu_input, ABS_Y, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2098 input_abs_set_res(ctlr->imu_input, ABS_Z, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2100 input_set_abs_params(ctlr->imu_input, ABS_RX, in joycon_imu_input_create()
2101 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2103 input_set_abs_params(ctlr->imu_input, ABS_RY, in joycon_imu_input_create()
2104 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2106 input_set_abs_params(ctlr->imu_input, ABS_RZ, in joycon_imu_input_create()
2107 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2110 input_abs_set_res(ctlr->imu_input, ABS_RX, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2111 input_abs_set_res(ctlr->imu_input, ABS_RY, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2112 input_abs_set_res(ctlr->imu_input, ABS_RZ, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2114 __set_bit(EV_MSC, ctlr->imu_input->evbit); in joycon_imu_input_create()
2115 __set_bit(MSC_TIMESTAMP, ctlr->imu_input->mscbit); in joycon_imu_input_create()
2116 __set_bit(INPUT_PROP_ACCELEROMETER, ctlr->imu_input->propbit); in joycon_imu_input_create()
2118 ret = input_register_device(ctlr->imu_input); in joycon_imu_input_create()
2130 hdev = ctlr->hdev; in joycon_input_create()
2132 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
2133 if (!ctlr->input) in joycon_input_create()
2134 return -ENOMEM; in joycon_input_create()
2135 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
2136 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
2137 ctlr->input->id.product = hdev->product; in joycon_input_create()
2138 ctlr->input->id.version = hdev->version; in joycon_input_create()
2139 ctlr->input->uniq = ctlr->mac_addr_str; in joycon_input_create()
2140 ctlr->input->name = hdev->name; in joycon_input_create()
2141 ctlr->input->phys = hdev->phys; in joycon_input_create()
2142 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
2144 ret = input_register_device(ctlr->input); in joycon_input_create()
2149 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2150 joycon_config_buttons(ctlr->input, right_joycon_button_mappings); in joycon_input_create()
2152 joycon_config_buttons(ctlr->input, right_joycon_s_button_mappings); in joycon_input_create()
2154 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2155 joycon_config_buttons(ctlr->input, left_joycon_button_mappings); in joycon_input_create()
2157 joycon_config_buttons(ctlr->input, left_joycon_s_button_mappings); in joycon_input_create()
2159 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2160 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2161 joycon_config_dpad(ctlr->input); in joycon_input_create()
2162 joycon_config_buttons(ctlr->input, procon_button_mappings); in joycon_input_create()
2164 joycon_config_dpad(ctlr->input); in joycon_input_create()
2165 joycon_config_buttons(ctlr->input, nescon_button_mappings); in joycon_input_create()
2167 joycon_config_dpad(ctlr->input); in joycon_input_create()
2168 joycon_config_buttons(ctlr->input, snescon_button_mappings); in joycon_input_create()
2170 joycon_config_dpad(ctlr->input); in joycon_input_create()
2171 joycon_config_buttons(ctlr->input, gencon_button_mappings); in joycon_input_create()
2173 joycon_config_dpad(ctlr->input); in joycon_input_create()
2174 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2175 joycon_config_buttons(ctlr->input, n64con_button_mappings); in joycon_input_create()
2194 struct device *dev = led->dev->parent; in joycon_player_led_brightness_set()
2204 return -ENODEV; in joycon_player_led_brightness_set()
2208 val |= ctlr->leds[i].brightness << i; in joycon_player_led_brightness_set()
2210 mutex_lock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2212 mutex_unlock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2220 struct device *dev = led->dev->parent; in joycon_home_led_brightness_set()
2228 return -ENODEV; in joycon_home_led_brightness_set()
2230 mutex_lock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2232 mutex_unlock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2240 struct hid_device *hdev = ctlr->hdev; in joycon_leds_create()
2241 struct device *dev = &hdev->dev; in joycon_leds_create()
2251 ctlr->player_id = U32_MAX; in joycon_leds_create()
2257 ctlr->player_id = ret; in joycon_leds_create()
2259 hid_info(ctlr->hdev, "assigned player %d led pattern", player_led_pattern + 1); in joycon_leds_create()
2267 return -ENOMEM; in joycon_leds_create()
2269 led = &ctlr->leds[i]; in joycon_leds_create()
2270 led->name = name; in joycon_leds_create()
2271 led->brightness = joycon_player_led_patterns[player_led_pattern][i]; in joycon_leds_create()
2272 led->max_brightness = 1; in joycon_leds_create()
2273 led->brightness_set_blocking = in joycon_leds_create()
2275 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2279 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2281 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2288 led = &ctlr->leds[i]; in joycon_leds_create()
2289 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2304 return -ENOMEM; in joycon_leds_create()
2306 led = &ctlr->home_led; in joycon_leds_create()
2307 led->name = name; in joycon_leds_create()
2308 led->brightness = 0; in joycon_leds_create()
2309 led->max_brightness = 0xF; in joycon_leds_create()
2310 led->brightness_set_blocking = joycon_home_led_brightness_set; in joycon_leds_create()
2311 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2314 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2316 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2322 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2343 spin_lock_irqsave(&ctlr->lock, flags); in joycon_battery_get_property()
2344 capacity = ctlr->battery_capacity; in joycon_battery_get_property()
2345 charging = ctlr->battery_charging; in joycon_battery_get_property()
2346 powered = ctlr->host_powered; in joycon_battery_get_property()
2347 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_battery_get_property()
2351 val->intval = 1; in joycon_battery_get_property()
2354 val->intval = POWER_SUPPLY_SCOPE_DEVICE; in joycon_battery_get_property()
2357 val->intval = capacity; in joycon_battery_get_property()
2361 val->intval = POWER_SUPPLY_STATUS_CHARGING; in joycon_battery_get_property()
2364 val->intval = POWER_SUPPLY_STATUS_FULL; in joycon_battery_get_property()
2366 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in joycon_battery_get_property()
2369 ret = -EINVAL; in joycon_battery_get_property()
2384 struct hid_device *hdev = ctlr->hdev; in joycon_power_supply_create()
2389 /* Set initially to unknown before receiving first input report */ in joycon_power_supply_create()
2390 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_power_supply_create()
2393 ctlr->battery_desc.properties = joycon_battery_props; in joycon_power_supply_create()
2394 ctlr->battery_desc.num_properties = in joycon_power_supply_create()
2396 ctlr->battery_desc.get_property = joycon_battery_get_property; in joycon_power_supply_create()
2397 ctlr->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; in joycon_power_supply_create()
2398 ctlr->battery_desc.use_for_apm = 0; in joycon_power_supply_create()
2399 ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, in joycon_power_supply_create()
2401 dev_name(&hdev->dev)); in joycon_power_supply_create()
2402 if (!ctlr->battery_desc.name) in joycon_power_supply_create()
2403 return -ENOMEM; in joycon_power_supply_create()
2405 ctlr->battery = devm_power_supply_register(&hdev->dev, in joycon_power_supply_create()
2406 &ctlr->battery_desc, in joycon_power_supply_create()
2408 if (IS_ERR(ctlr->battery)) { in joycon_power_supply_create()
2409 ret = PTR_ERR(ctlr->battery); in joycon_power_supply_create()
2414 return power_supply_powers(ctlr->battery, &hdev->dev); in joycon_power_supply_create()
2423 struct joycon_input_report *report; in joycon_read_info() local
2426 ret = joycon_send_subcmd(ctlr, &req, 0, HZ); in joycon_read_info()
2428 hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); in joycon_read_info()
2432 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_read_info()
2435 ctlr->mac_addr[j] = report->subcmd_reply.data[i]; in joycon_read_info()
2437 ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, in joycon_read_info()
2439 ctlr->mac_addr[0], in joycon_read_info()
2440 ctlr->mac_addr[1], in joycon_read_info()
2441 ctlr->mac_addr[2], in joycon_read_info()
2442 ctlr->mac_addr[3], in joycon_read_info()
2443 ctlr->mac_addr[4], in joycon_read_info()
2444 ctlr->mac_addr[5]); in joycon_read_info()
2445 if (!ctlr->mac_addr_str) in joycon_read_info()
2446 return -ENOMEM; in joycon_read_info()
2447 hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); in joycon_read_info()
2451 * Unfortantly the hdev->product can't always be used due to a ?bug? in joycon_read_info()
2452 * with the NSO Genesis controller. Over USB, it will report the in joycon_read_info()
2453 * PID as 0x201E, but over bluetooth it will report the PID as 0x2017 in joycon_read_info()
2455 * the rest of the controllers which will report the same PID over USB in joycon_read_info()
2458 ctlr->ctlr_type = report->subcmd_reply.data[2]; in joycon_read_info()
2459 hid_dbg(ctlr->hdev, "controller type = 0x%02X\n", ctlr->ctlr_type); in joycon_read_info()
2469 mutex_lock(&ctlr->output_mutex); in joycon_init()
2471 if (joycon_using_usb(ctlr) && !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ)) { in joycon_init()
2474 ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M, HZ); in joycon_init()
2483 ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ); in joycon_init()
2492 joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT, HZ/10); in joycon_init()
2495 ret = -ETIMEDOUT; in joycon_init()
2538 /* Set the reporting mode to 0x30, which is the full report mode */ in joycon_init()
2541 hid_err(hdev, "Failed to set report mode; ret=%d\n", ret); in joycon_init()
2555 mutex_unlock(&ctlr->output_mutex); in joycon_init()
2565 if (size >= 12) /* make sure it contains the input report */ in joycon_ctlr_read_handler()
2578 struct joycon_input_report *report; in joycon_ctlr_handle_event() local
2580 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
2581 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
2582 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
2587 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
2594 report = (struct joycon_input_report *)data; in joycon_ctlr_handle_event()
2595 if (report->subcmd_reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
2603 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
2605 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
2606 ctlr->received_resp = true; in joycon_ctlr_handle_event()
2607 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
2614 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
2621 struct hid_report *report, u8 *raw_data, int size) in nintendo_hid_event() argument
2626 return -EINVAL; in nintendo_hid_event()
2637 hid_dbg(hdev, "probe - start\n"); in nintendo_hid_probe()
2639 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
2641 ret = -ENOMEM; in nintendo_hid_probe()
2645 ctlr->hdev = hdev; in nintendo_hid_probe()
2646 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
2647 ctlr->rumble_queue_head = 0; in nintendo_hid_probe()
2648 ctlr->rumble_queue_tail = 0; in nintendo_hid_probe()
2650 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
2651 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
2652 spin_lock_init(&ctlr->lock); in nintendo_hid_probe()
2653 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", in nintendo_hid_probe()
2655 if (!ctlr->rumble_queue) { in nintendo_hid_probe()
2656 ret = -ENOMEM; in nintendo_hid_probe()
2659 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); in nintendo_hid_probe()
2674 hdev->version |= 0x8000; in nintendo_hid_probe()
2716 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
2718 hid_dbg(hdev, "probe - success\n"); in nintendo_hid_probe()
2722 ida_free(&nintendo_player_id_allocator, ctlr->player_id); in nintendo_hid_probe()
2728 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_probe()
2730 hid_err(hdev, "probe - fail = %d\n", ret); in nintendo_hid_probe()
2742 spin_lock_irqsave(&ctlr->lock, flags); in nintendo_hid_remove()
2743 ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED; in nintendo_hid_remove()
2744 spin_unlock_irqrestore(&ctlr->lock, flags); in nintendo_hid_remove()
2746 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_remove()
2747 ida_free(&nintendo_player_id_allocator, ctlr->player_id); in nintendo_hid_remove()