Lines Matching +full:usb +full:- +full:cal

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
21 * Pro Controllers can either be used over USB or Bluetooth.
31 #include "hid-ids.h"
97 /* USB Commands */
119 (JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
129 (JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
139 /* Hat values for pro controller's d-pad */
153 * configured with a range of +-8000 milliGs. Therefore, the resolution can be
154 * calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
165 * configured with a range of +-2000 degrees/second.
166 * Digits per dps: (2^16 -1)/(2000*2) = 16.38375
167 * dps per digit: 16.38375E-1 = .0610
173 * digits per dps (corrected): .0702E-1 = 14.247
180 #define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
368 * D-pad is configured as buttons for the left Joy-Con only!
384 * The unused *right*-side triggers become the SL/SR triggers for the *left*
385 * Joy-Con, if and only if we're not using a charging grip.
407 * The unused *left*-side triggers become the SL/SR triggers for the *right*
408 * Joy-Con, if and only if we're not using a charging grip.
474 * N64's C buttons get assigned to d-pad directions and registered as buttons.
640 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
641 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
642 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
644 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
646 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
650 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
651 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO || \
652 ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64)
656 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
657 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
664 * but take caution: some NSO devices lie about themselves (NES Joy-Cons and
672 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL; in joycon_device_is_left_joycon()
677 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR; in joycon_device_is_right_joycon()
682 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON; in joycon_device_is_procon()
687 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP; in joycon_device_is_chrggrip()
692 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_SNESCON; in joycon_device_is_snescon()
697 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_GENCON; in joycon_device_is_gencon()
702 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_N64CON; in joycon_device_is_n64con()
717 * These are slightly different than the device-ID-based helpers above. They are
719 * versus SNES, or NES Joy-Cons versus regular Switch Joy-Cons. They're most
729 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL; in joycon_type_is_left_joycon()
734 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR; in joycon_type_is_right_joycon()
739 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO; in joycon_type_is_procon()
744 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_SNES; in joycon_type_is_snescon()
749 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_GEN; in joycon_type_is_gencon()
754 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64; in joycon_type_is_n64con()
759 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESL; in joycon_type_is_left_nescon()
764 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESR; in joycon_type_is_right_nescon()
824 return ctlr->hdev->bus == BUS_USB; in joycon_using_usb()
834 return -ENOMEM; in __joycon_hid_send()
851 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_wait_for_input_report()
854 spin_lock_irqsave(&ctlr->lock, flags); in joycon_wait_for_input_report()
855 ctlr->received_input_report = false; in joycon_wait_for_input_report()
856 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_wait_for_input_report()
857 ret = wait_event_timeout(ctlr->wait, in joycon_wait_for_input_report()
858 ctlr->received_input_report, in joycon_wait_for_input_report()
862 hid_warn(ctlr->hdev, in joycon_wait_for_input_report()
878 #define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_…
887 if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ)) in joycon_enforce_subcmd_rate()
893 subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
895 spin_lock_irqsave(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
896 consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas; in joycon_enforce_subcmd_rate()
897 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
902 ctlr->ctlr_state == JOYCON_CTLR_STATE_READ && in joycon_enforce_subcmd_rate()
906 hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__); in joycon_enforce_subcmd_rate()
910 ctlr->last_subcmd_sent_msecs = current_ms; in joycon_enforce_subcmd_rate()
931 while (tries--) { in joycon_hid_send_sync()
934 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
936 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
940 ret = wait_event_timeout(ctlr->wait, ctlr->received_resp, in joycon_hid_send_sync()
943 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
946 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
949 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
950 ret = -ETIMEDOUT; in joycon_hid_send_sync()
957 ctlr->received_resp = false; in joycon_hid_send_sync()
967 ctlr->usb_ack_match = cmd; in joycon_send_usb()
968 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
971 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
982 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_subcmd()
987 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_subcmd()
988 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
989 return -ENODEV; in joycon_send_subcmd()
991 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_subcmd()
993 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
995 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; in joycon_send_subcmd()
996 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
997 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
998 ctlr->subcmd_num = 0; in joycon_send_subcmd()
999 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
1000 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
1005 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
1018 req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; in joycon_set_player_leds()
1019 req->data[0] = (flash << 4) | on; in joycon_set_player_leds()
1021 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
1032 req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; in joycon_set_home_led()
1033 data = req->data; in joycon_set_home_led()
1040 hid_dbg(ctlr->hdev, "setting home led brightness\n"); in joycon_set_home_led()
1054 return -EINVAL; in joycon_request_spi_flash_read()
1057 req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; in joycon_request_spi_flash_read()
1058 data = req->data; in joycon_request_spi_flash_read()
1062 hid_dbg(ctlr->hdev, "requesting SPI flash data\n"); in joycon_request_spi_flash_read()
1065 hid_err(ctlr->hdev, "failed reading SPI flash; ret=%d\n", ret); in joycon_request_spi_flash_read()
1067 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_spi_flash_read()
1069 *reply = &report->subcmd_reply.data[5]; in joycon_request_spi_flash_read()
1110 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1112 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1114 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1116 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1118 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1120 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1123 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1125 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1127 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1129 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1131 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1133 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1137 cal_x->max = cal_x->center + x_max_above; in joycon_read_stick_calibration()
1138 cal_x->min = cal_x->center - x_min_below; in joycon_read_stick_calibration()
1139 cal_y->max = cal_y->center + y_max_above; in joycon_read_stick_calibration()
1140 cal_y->min = cal_y->center - y_min_below; in joycon_read_stick_calibration()
1143 if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || in joycon_read_stick_calibration()
1144 cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) in joycon_read_stick_calibration()
1145 ret = -EINVAL; in joycon_read_stick_calibration()
1159 "Failed to read %s stick cal, using defaults; e=%d\n", in joycon_use_default_calibration()
1162 cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; in joycon_use_default_calibration()
1163 cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; in joycon_use_default_calibration()
1164 cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; in joycon_use_default_calibration()
1173 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
1178 hid_info(ctlr->hdev, "using user cal for left stick\n"); in joycon_request_calibration()
1180 hid_info(ctlr->hdev, "using factory cal for left stick\n"); in joycon_request_calibration()
1184 hid_info(ctlr->hdev, "using user cal for right stick\n"); in joycon_request_calibration()
1186 hid_info(ctlr->hdev, "using factory cal for right stick\n"); in joycon_request_calibration()
1191 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1192 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1196 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1197 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1198 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1203 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1204 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1208 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1209 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1210 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1213 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
1218 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
1219 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
1220 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
1221 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
1222 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
1223 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
1224 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
1225 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
1226 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
1227 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
1228 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
1229 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
1244 ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1245 ctlr->accel_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1246 ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1247 ctlr->gyro_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1249 if (ctlr->imu_cal_accel_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1250 ctlr->imu_cal_accel_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1254 if (ctlr->imu_cal_gyro_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1255 ctlr->imu_cal_gyro_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1261 hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz); in joycon_calc_imu_cal_divisors()
1278 hid_info(ctlr->hdev, "using user cal for IMU\n"); in joycon_request_imu_calibration()
1280 hid_info(ctlr->hdev, "using factory cal for IMU\n"); in joycon_request_imu_calibration()
1284 hid_dbg(ctlr->hdev, "requesting IMU cal data\n"); in joycon_request_imu_calibration()
1288 hid_warn(ctlr->hdev, in joycon_request_imu_calibration()
1289 "Failed to read IMU cal, using defaults; ret=%d\n", in joycon_request_imu_calibration()
1293 ctlr->accel_cal.offset[i] = DFLT_ACCEL_OFFSET; in joycon_request_imu_calibration()
1294 ctlr->accel_cal.scale[i] = DFLT_ACCEL_SCALE; in joycon_request_imu_calibration()
1295 ctlr->gyro_cal.offset[i] = DFLT_GYRO_OFFSET; in joycon_request_imu_calibration()
1296 ctlr->gyro_cal.scale[i] = DFLT_GYRO_SCALE; in joycon_request_imu_calibration()
1306 ctlr->accel_cal.offset[i] = get_unaligned_le16(raw_cal + j); in joycon_request_imu_calibration()
1307 ctlr->accel_cal.scale[i] = get_unaligned_le16(raw_cal + j + 6); in joycon_request_imu_calibration()
1308 ctlr->gyro_cal.offset[i] = get_unaligned_le16(raw_cal + j + 12); in joycon_request_imu_calibration()
1309 ctlr->gyro_cal.scale[i] = get_unaligned_le16(raw_cal + j + 18); in joycon_request_imu_calibration()
1314 hid_dbg(ctlr->hdev, "IMU calibration:\n" in joycon_request_imu_calibration()
1319 ctlr->accel_cal.offset[0], in joycon_request_imu_calibration()
1320 ctlr->accel_cal.offset[1], in joycon_request_imu_calibration()
1321 ctlr->accel_cal.offset[2], in joycon_request_imu_calibration()
1322 ctlr->accel_cal.scale[0], in joycon_request_imu_calibration()
1323 ctlr->accel_cal.scale[1], in joycon_request_imu_calibration()
1324 ctlr->accel_cal.scale[2], in joycon_request_imu_calibration()
1325 ctlr->gyro_cal.offset[0], in joycon_request_imu_calibration()
1326 ctlr->gyro_cal.offset[1], in joycon_request_imu_calibration()
1327 ctlr->gyro_cal.offset[2], in joycon_request_imu_calibration()
1328 ctlr->gyro_cal.scale[0], in joycon_request_imu_calibration()
1329 ctlr->gyro_cal.scale[1], in joycon_request_imu_calibration()
1330 ctlr->gyro_cal.scale[2]); in joycon_request_imu_calibration()
1341 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; in joycon_set_report_mode()
1342 req->data[0] = 0x30; /* standard, full report mode */ in joycon_set_report_mode()
1344 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
1354 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; in joycon_enable_rumble()
1355 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_rumble()
1357 hid_dbg(ctlr->hdev, "enabling rumble\n"); in joycon_enable_rumble()
1367 req->subcmd_id = JC_SUBCMD_ENABLE_IMU; in joycon_enable_imu()
1368 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_imu()
1370 hid_dbg(ctlr->hdev, "enabling IMU\n"); in joycon_enable_imu()
1374 static s32 joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val) in joycon_map_stick_val() argument
1376 s32 center = cal->center; in joycon_map_stick_val()
1377 s32 min = cal->min; in joycon_map_stick_val()
1378 s32 max = cal->max; in joycon_map_stick_val()
1382 new_val = (val - center) * JC_MAX_STICK_MAG; in joycon_map_stick_val()
1383 new_val /= (max - center); in joycon_map_stick_val()
1385 new_val = (center - val) * -JC_MAX_STICK_MAG; in joycon_map_stick_val()
1386 new_val /= (center - min); in joycon_map_stick_val()
1388 new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG); in joycon_map_stick_val()
1396 u8 *raw = rep->imu_raw_bytes; in joycon_input_report_parse_imu_data()
1402 data->accel_x = get_unaligned_le16(raw + 0); in joycon_input_report_parse_imu_data()
1403 data->accel_y = get_unaligned_le16(raw + 2); in joycon_input_report_parse_imu_data()
1404 data->accel_z = get_unaligned_le16(raw + 4); in joycon_input_report_parse_imu_data()
1405 data->gyro_x = get_unaligned_le16(raw + 6); in joycon_input_report_parse_imu_data()
1406 data->gyro_y = get_unaligned_le16(raw + 8); in joycon_input_report_parse_imu_data()
1407 data->gyro_z = get_unaligned_le16(raw + 10); in joycon_input_report_parse_imu_data()
1417 struct input_dev *idev = ctlr->imu_input; in joycon_parse_imu_report()
1419 unsigned int last_msecs = ctlr->imu_last_pkt_ms; in joycon_parse_imu_report()
1429 * incrementing 8-bit counter per input report, but it is not very in joycon_parse_imu_report()
1431 * increments at or if it varies based on packet push rate - more on in joycon_parse_imu_report()
1434 * The reverse engineering work done on the joy-cons and pro controllers in joycon_parse_imu_report()
1436 * - The controller samples the IMU every 1.35ms. It then does some of in joycon_parse_imu_report()
1438 * - Each imu input report contains 3 IMU samples, (usually 5ms apart). in joycon_parse_imu_report()
1439 * - In the standard reporting mode (which this driver uses exclusively) in joycon_parse_imu_report()
1441 * * joy-con (bluetooth): every 15 ms in joycon_parse_imu_report()
1442 * * joy-cons (in charging grip via USB): every 15 ms in joycon_parse_imu_report()
1443 * * pro controller (USB): every 15 ms in joycon_parse_imu_report()
1449 * SSR to 11ms for both the joy-cons and pro controllers). in joycon_parse_imu_report()
1472 if (!ctlr->imu_first_packet_received) { in joycon_parse_imu_report()
1473 ctlr->imu_timestamp_us = 0; in joycon_parse_imu_report()
1474 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1475 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1476 ctlr->imu_avg_delta_ms = JC_IMU_DFLT_AVG_DELTA_MS; in joycon_parse_imu_report()
1477 ctlr->imu_first_packet_received = true; in joycon_parse_imu_report()
1479 unsigned int delta = msecs - last_msecs; in joycon_parse_imu_report()
1484 ctlr->imu_delta_samples_sum += delta; in joycon_parse_imu_report()
1485 ctlr->imu_delta_samples_count++; in joycon_parse_imu_report()
1486 if (ctlr->imu_delta_samples_count >= in joycon_parse_imu_report()
1488 ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / in joycon_parse_imu_report()
1489 ctlr->imu_delta_samples_count; in joycon_parse_imu_report()
1490 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1491 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1495 if (ctlr->imu_avg_delta_ms == 0) { in joycon_parse_imu_report()
1496 ctlr->imu_avg_delta_ms = 1; in joycon_parse_imu_report()
1497 hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n"); in joycon_parse_imu_report()
1501 hid_dbg(ctlr->hdev, in joycon_parse_imu_report()
1503 msecs, last_msecs, delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1506 dropped_threshold = ctlr->imu_avg_delta_ms * 3 / 2; in joycon_parse_imu_report()
1507 dropped_pkts = (delta - min(delta, dropped_threshold)) / in joycon_parse_imu_report()
1508 ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1509 ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1511 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1514 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1516 delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1519 ctlr->imu_last_pkt_ms = msecs; in joycon_parse_imu_report()
1524 ctlr->imu_timestamp_us); in joycon_parse_imu_report()
1529 * found in the community's reverse-engineering repo (linked at in joycon_parse_imu_report()
1530 * top of driver). For hid-nintendo, we make sure that the final in joycon_parse_imu_report()
1539 * precision-saving scaling factor to prevent large inaccuracies in joycon_parse_imu_report()
1545 (imu_data[i].gyro_x - in joycon_parse_imu_report()
1546 ctlr->gyro_cal.offset[0])), in joycon_parse_imu_report()
1547 ctlr->gyro_cal.scale[0], in joycon_parse_imu_report()
1548 ctlr->imu_cal_gyro_divisor[0]); in joycon_parse_imu_report()
1550 (imu_data[i].gyro_y - in joycon_parse_imu_report()
1551 ctlr->gyro_cal.offset[1])), in joycon_parse_imu_report()
1552 ctlr->gyro_cal.scale[1], in joycon_parse_imu_report()
1553 ctlr->imu_cal_gyro_divisor[1]); in joycon_parse_imu_report()
1555 (imu_data[i].gyro_z - in joycon_parse_imu_report()
1556 ctlr->gyro_cal.offset[2])), in joycon_parse_imu_report()
1557 ctlr->gyro_cal.scale[2], in joycon_parse_imu_report()
1558 ctlr->imu_cal_gyro_divisor[2]); in joycon_parse_imu_report()
1561 ctlr->accel_cal.scale[0]) / in joycon_parse_imu_report()
1562 ctlr->imu_cal_accel_divisor[0]; in joycon_parse_imu_report()
1564 ctlr->accel_cal.scale[1]) / in joycon_parse_imu_report()
1565 ctlr->imu_cal_accel_divisor[1]; in joycon_parse_imu_report()
1567 ctlr->accel_cal.scale[2]) / in joycon_parse_imu_report()
1568 ctlr->imu_cal_accel_divisor[2]; in joycon_parse_imu_report()
1570 hid_dbg(ctlr->hdev, "raw_gyro: g_x=%d g_y=%d g_z=%d\n", in joycon_parse_imu_report()
1573 hid_dbg(ctlr->hdev, "raw_accel: a_x=%d a_y=%d a_z=%d\n", in joycon_parse_imu_report()
1578 * The right joy-con has 2 axes negated, Y and Z. This is due to in joycon_parse_imu_report()
1580 * axes' values in order to be consistent with the left joy-con in joycon_parse_imu_report()
1585 * The axes follow the right-hand rule. in joycon_parse_imu_report()
1594 value[j] *= -1; in joycon_parse_imu_report()
1606 ctlr->imu_timestamp_us += ctlr->imu_avg_delta_ms * 1000 / 3; in joycon_parse_imu_report()
1615 spin_lock_irqsave(&ctlr->lock, flags); in joycon_handle_rumble_report()
1616 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report && in joycon_handle_rumble_report()
1617 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED && in joycon_handle_rumble_report()
1618 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS && in joycon_handle_rumble_report()
1619 (ctlr->rumble_queue_head != ctlr->rumble_queue_tail || in joycon_handle_rumble_report()
1620 ctlr->rumble_zero_countdown > 0)) { in joycon_handle_rumble_report()
1627 if (ctlr->rumble_zero_countdown > 0) in joycon_handle_rumble_report()
1628 ctlr->rumble_zero_countdown--; in joycon_handle_rumble_report()
1629 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_handle_rumble_report()
1632 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_handle_rumble_report()
1640 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_battery_status()
1642 tmp = rep->bat_con; in joycon_parse_battery_status()
1643 ctlr->host_powered = tmp & BIT(0); in joycon_parse_battery_status()
1644 ctlr->battery_charging = tmp & BIT(4); in joycon_parse_battery_status()
1649 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; in joycon_parse_battery_status()
1652 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; in joycon_parse_battery_status()
1655 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; in joycon_parse_battery_status()
1658 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; in joycon_parse_battery_status()
1661 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; in joycon_parse_battery_status()
1664 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_parse_battery_status()
1665 hid_warn(ctlr->hdev, "Invalid battery status\n"); in joycon_parse_battery_status()
1669 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_battery_status()
1680 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_report_left_stick()
1681 raw_y = hid_field_extract(ctlr->hdev, rep->left_stick + 1, 4, 12); in joycon_report_left_stick()
1683 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_report_left_stick()
1684 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_report_left_stick()
1686 input_report_abs(ctlr->input, ABS_X, x); in joycon_report_left_stick()
1687 input_report_abs(ctlr->input, ABS_Y, y); in joycon_report_left_stick()
1698 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_report_right_stick()
1699 raw_y = hid_field_extract(ctlr->hdev, rep->right_stick + 1, 4, 12); in joycon_report_right_stick()
1701 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_report_right_stick()
1702 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_report_right_stick()
1704 input_report_abs(ctlr->input, ABS_RX, x); in joycon_report_right_stick()
1705 input_report_abs(ctlr->input, ABS_RY, y); in joycon_report_right_stick()
1713 u32 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_dpad()
1716 hatx = -1; in joycon_report_dpad()
1721 haty = -1; in joycon_report_dpad()
1725 input_report_abs(ctlr->input, ABS_HAT0X, hatx); in joycon_report_dpad()
1726 input_report_abs(ctlr->input, ABS_HAT0Y, haty); in joycon_report_dpad()
1734 u32 status = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_buttons()
1736 for (button = button_mappings; button->code; button++) in joycon_report_buttons()
1737 input_report_key(ctlr->input, button->code, status & button->bit); in joycon_report_buttons()
1745 unsigned long report_delta_ms = msecs - ctlr->last_input_report_msecs; in joycon_parse_report()
1782 input_sync(ctlr->input); in joycon_parse_report()
1784 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1785 ctlr->last_input_report_msecs = msecs; in joycon_parse_report()
1793 if (ctlr->consecutive_valid_report_deltas < JC_SUBCMD_VALID_DELTA_REQ) in joycon_parse_report()
1794 ctlr->consecutive_valid_report_deltas++; in joycon_parse_report()
1796 ctlr->consecutive_valid_report_deltas = 0; in joycon_parse_report()
1800 * bluetooth-connected controllers. For USB devices, we're beholden to in joycon_parse_report()
1801 * USB's underlying polling rate anyway. Always set to the consecutive in joycon_parse_report()
1804 if (ctlr->hdev->bus == BUS_USB) in joycon_parse_report()
1805 ctlr->consecutive_valid_report_deltas = JC_SUBCMD_VALID_DELTA_REQ; in joycon_parse_report()
1807 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1814 if (unlikely(mutex_is_locked(&ctlr->output_mutex))) { in joycon_parse_report()
1815 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1816 ctlr->received_input_report = true; in joycon_parse_report()
1817 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1818 wake_up(&ctlr->wait); in joycon_parse_report()
1822 if ((rep->id == JC_INPUT_IMU_DATA) && joycon_has_imu(ctlr)) in joycon_parse_report()
1832 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_rumble_data()
1837 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_rumble_data()
1838 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1839 return -ENODEV; in joycon_send_rumble_data()
1842 ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_rumble_data()
1844 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1847 rumble_output.packet_num = ctlr->subcmd_num; in joycon_send_rumble_data()
1848 if (++ctlr->subcmd_num > 0xF) in joycon_send_rumble_data()
1849 ctlr->subcmd_num = 0; in joycon_send_rumble_data()
1853 ret = __joycon_hid_send(ctlr->hdev, (u8 *)&rumble_output, in joycon_send_rumble_data()
1867 mutex_lock(&ctlr->output_mutex); in joycon_rumble_worker()
1869 mutex_unlock(&ctlr->output_mutex); in joycon_rumble_worker()
1871 /* -ENODEV means the controller was just unplugged */ in joycon_rumble_worker()
1872 spin_lock_irqsave(&ctlr->lock, flags); in joycon_rumble_worker()
1873 if (ret < 0 && ret != -ENODEV && in joycon_rumble_worker()
1874 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_rumble_worker()
1875 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret); in joycon_rumble_worker()
1877 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_rumble_worker()
1878 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) { in joycon_rumble_worker()
1879 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE) in joycon_rumble_worker()
1880 ctlr->rumble_queue_tail = 0; in joycon_rumble_worker()
1884 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_rumble_worker()
1896 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_freq()
1897 if (freq > data[i - 1].freq && freq <= data[i].freq) in joycon_find_rumble_freq()
1912 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_amp()
1913 if (amp > data[i - 1].amp && amp <= data[i].amp) in joycon_find_rumble_amp()
1946 spin_lock_irqsave(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1947 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, in joycon_clamp_rumble_freqs()
1950 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, in joycon_clamp_rumble_freqs()
1953 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, in joycon_clamp_rumble_freqs()
1956 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, in joycon_clamp_rumble_freqs()
1959 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1974 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1975 freq_r_low = ctlr->rumble_rl_freq; in joycon_set_rumble()
1976 freq_r_high = ctlr->rumble_rh_freq; in joycon_set_rumble()
1977 freq_l_low = ctlr->rumble_ll_freq; in joycon_set_rumble()
1978 freq_l_high = ctlr->rumble_lh_freq; in joycon_set_rumble()
1981 ctlr->rumble_zero_countdown = JC_RUMBLE_ZERO_AMP_PKT_CNT; in joycon_set_rumble()
1982 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1984 /* right joy-con */ in joycon_set_rumble()
1988 /* left joy-con */ in joycon_set_rumble()
1992 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1994 next_rq_head = ctlr->rumble_queue_head + 1; in joycon_set_rumble()
2001 if (next_rq_head == ctlr->rumble_queue_tail) { in joycon_set_rumble()
2002 hid_dbg(ctlr->hdev, "rumble queue is full"); in joycon_set_rumble()
2004 next_rq_head = ctlr->rumble_queue_head; in joycon_set_rumble()
2007 ctlr->rumble_queue_head = next_rq_head; in joycon_set_rumble()
2008 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, in joycon_set_rumble()
2012 if (schedule_now && ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_set_rumble()
2013 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_set_rumble()
2015 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
2025 if (effect->type != FF_RUMBLE) in joycon_play_effect()
2029 effect->u.rumble.weak_magnitude, in joycon_play_effect()
2030 effect->u.rumble.strong_magnitude, in joycon_play_effect()
2039 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
2045 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
2055 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2061 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2071 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2077 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2088 for (button = button_mappings; button->code; button++) in joycon_config_buttons()
2089 input_set_capability(idev, EV_KEY, button->code); in joycon_config_buttons()
2096 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); in joycon_config_rumble()
2097 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); in joycon_config_rumble()
2098 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2099 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2100 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2101 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2104 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_config_rumble()
2114 hdev = ctlr->hdev; in joycon_imu_input_create()
2117 ctlr->imu_input = devm_input_allocate_device(&hdev->dev); in joycon_imu_input_create()
2118 if (!ctlr->imu_input) in joycon_imu_input_create()
2119 return -ENOMEM; in joycon_imu_input_create()
2121 ctlr->imu_input->id.bustype = hdev->bus; in joycon_imu_input_create()
2122 ctlr->imu_input->id.vendor = hdev->vendor; in joycon_imu_input_create()
2123 ctlr->imu_input->id.product = hdev->product; in joycon_imu_input_create()
2124 ctlr->imu_input->id.version = hdev->version; in joycon_imu_input_create()
2125 ctlr->imu_input->uniq = ctlr->mac_addr_str; in joycon_imu_input_create()
2126 ctlr->imu_input->phys = hdev->phys; in joycon_imu_input_create()
2128 imu_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s (IMU)", ctlr->input->name); in joycon_imu_input_create()
2130 return -ENOMEM; in joycon_imu_input_create()
2132 ctlr->imu_input->name = imu_name; in joycon_imu_input_create()
2134 input_set_drvdata(ctlr->imu_input, ctlr); in joycon_imu_input_create()
2137 input_set_abs_params(ctlr->imu_input, ABS_X, in joycon_imu_input_create()
2138 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2140 input_set_abs_params(ctlr->imu_input, ABS_Y, in joycon_imu_input_create()
2141 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2143 input_set_abs_params(ctlr->imu_input, ABS_Z, in joycon_imu_input_create()
2144 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2146 input_abs_set_res(ctlr->imu_input, ABS_X, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2147 input_abs_set_res(ctlr->imu_input, ABS_Y, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2148 input_abs_set_res(ctlr->imu_input, ABS_Z, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2150 input_set_abs_params(ctlr->imu_input, ABS_RX, in joycon_imu_input_create()
2151 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2153 input_set_abs_params(ctlr->imu_input, ABS_RY, in joycon_imu_input_create()
2154 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2156 input_set_abs_params(ctlr->imu_input, ABS_RZ, in joycon_imu_input_create()
2157 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2160 input_abs_set_res(ctlr->imu_input, ABS_RX, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2161 input_abs_set_res(ctlr->imu_input, ABS_RY, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2162 input_abs_set_res(ctlr->imu_input, ABS_RZ, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2164 __set_bit(EV_MSC, ctlr->imu_input->evbit); in joycon_imu_input_create()
2165 __set_bit(MSC_TIMESTAMP, ctlr->imu_input->mscbit); in joycon_imu_input_create()
2166 __set_bit(INPUT_PROP_ACCELEROMETER, ctlr->imu_input->propbit); in joycon_imu_input_create()
2168 ret = input_register_device(ctlr->imu_input); in joycon_imu_input_create()
2180 hdev = ctlr->hdev; in joycon_input_create()
2182 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
2183 if (!ctlr->input) in joycon_input_create()
2184 return -ENOMEM; in joycon_input_create()
2185 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
2186 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
2187 ctlr->input->id.product = hdev->product; in joycon_input_create()
2188 ctlr->input->id.version = hdev->version; in joycon_input_create()
2189 ctlr->input->uniq = ctlr->mac_addr_str; in joycon_input_create()
2190 ctlr->input->name = hdev->name; in joycon_input_create()
2191 ctlr->input->phys = hdev->phys; in joycon_input_create()
2192 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
2194 ret = input_register_device(ctlr->input); in joycon_input_create()
2199 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2200 joycon_config_buttons(ctlr->input, right_joycon_button_mappings); in joycon_input_create()
2202 joycon_config_buttons(ctlr->input, right_joycon_s_button_mappings); in joycon_input_create()
2204 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2205 joycon_config_buttons(ctlr->input, left_joycon_button_mappings); in joycon_input_create()
2207 joycon_config_buttons(ctlr->input, left_joycon_s_button_mappings); in joycon_input_create()
2209 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2210 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2211 joycon_config_dpad(ctlr->input); in joycon_input_create()
2212 joycon_config_buttons(ctlr->input, procon_button_mappings); in joycon_input_create()
2214 joycon_config_dpad(ctlr->input); in joycon_input_create()
2215 joycon_config_buttons(ctlr->input, nescon_button_mappings); in joycon_input_create()
2217 joycon_config_dpad(ctlr->input); in joycon_input_create()
2218 joycon_config_buttons(ctlr->input, snescon_button_mappings); in joycon_input_create()
2220 joycon_config_dpad(ctlr->input); in joycon_input_create()
2221 joycon_config_buttons(ctlr->input, gencon_button_mappings); in joycon_input_create()
2223 joycon_config_dpad(ctlr->input); in joycon_input_create()
2224 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2225 joycon_config_buttons(ctlr->input, n64con_button_mappings); in joycon_input_create()
2244 struct device *dev = led->dev->parent; in joycon_player_led_brightness_set()
2254 return -ENODEV; in joycon_player_led_brightness_set()
2258 val |= ctlr->leds[i].brightness << i; in joycon_player_led_brightness_set()
2260 mutex_lock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2262 mutex_unlock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2270 struct device *dev = led->dev->parent; in joycon_home_led_brightness_set()
2278 return -ENODEV; in joycon_home_led_brightness_set()
2280 mutex_lock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2282 mutex_unlock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2289 struct hid_device *hdev = ctlr->hdev; in joycon_leds_create()
2290 struct device *dev = &hdev->dev; in joycon_leds_create()
2317 return -ENOMEM; in joycon_leds_create()
2319 led = &ctlr->leds[i]; in joycon_leds_create()
2320 led->name = name; in joycon_leds_create()
2321 led->brightness = joycon_player_led_patterns[player_led_pattern][i]; in joycon_leds_create()
2322 led->max_brightness = 1; in joycon_leds_create()
2323 led->brightness_set_blocking = in joycon_leds_create()
2325 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2329 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2331 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2338 led = &ctlr->leds[i]; in joycon_leds_create()
2339 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2354 return -ENOMEM; in joycon_leds_create()
2356 led = &ctlr->home_led; in joycon_leds_create()
2357 led->name = name; in joycon_leds_create()
2358 led->brightness = 0; in joycon_leds_create()
2359 led->max_brightness = 0xF; in joycon_leds_create()
2360 led->brightness_set_blocking = joycon_home_led_brightness_set; in joycon_leds_create()
2361 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2364 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2366 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2372 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2393 spin_lock_irqsave(&ctlr->lock, flags); in joycon_battery_get_property()
2394 capacity = ctlr->battery_capacity; in joycon_battery_get_property()
2395 charging = ctlr->battery_charging; in joycon_battery_get_property()
2396 powered = ctlr->host_powered; in joycon_battery_get_property()
2397 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_battery_get_property()
2401 val->intval = 1; in joycon_battery_get_property()
2404 val->intval = POWER_SUPPLY_SCOPE_DEVICE; in joycon_battery_get_property()
2407 val->intval = capacity; in joycon_battery_get_property()
2411 val->intval = POWER_SUPPLY_STATUS_CHARGING; in joycon_battery_get_property()
2414 val->intval = POWER_SUPPLY_STATUS_FULL; in joycon_battery_get_property()
2416 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in joycon_battery_get_property()
2419 ret = -EINVAL; in joycon_battery_get_property()
2434 struct hid_device *hdev = ctlr->hdev; in joycon_power_supply_create()
2440 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_power_supply_create()
2443 ctlr->battery_desc.properties = joycon_battery_props; in joycon_power_supply_create()
2444 ctlr->battery_desc.num_properties = in joycon_power_supply_create()
2446 ctlr->battery_desc.get_property = joycon_battery_get_property; in joycon_power_supply_create()
2447 ctlr->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; in joycon_power_supply_create()
2448 ctlr->battery_desc.use_for_apm = 0; in joycon_power_supply_create()
2449 ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, in joycon_power_supply_create()
2451 dev_name(&hdev->dev)); in joycon_power_supply_create()
2452 if (!ctlr->battery_desc.name) in joycon_power_supply_create()
2453 return -ENOMEM; in joycon_power_supply_create()
2455 ctlr->battery = devm_power_supply_register(&hdev->dev, in joycon_power_supply_create()
2456 &ctlr->battery_desc, in joycon_power_supply_create()
2458 if (IS_ERR(ctlr->battery)) { in joycon_power_supply_create()
2459 ret = PTR_ERR(ctlr->battery); in joycon_power_supply_create()
2464 return power_supply_powers(ctlr->battery, &hdev->dev); in joycon_power_supply_create()
2478 hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); in joycon_read_info()
2482 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_read_info()
2485 ctlr->mac_addr[j] = report->subcmd_reply.data[i]; in joycon_read_info()
2487 ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, in joycon_read_info()
2489 ctlr->mac_addr[0], in joycon_read_info()
2490 ctlr->mac_addr[1], in joycon_read_info()
2491 ctlr->mac_addr[2], in joycon_read_info()
2492 ctlr->mac_addr[3], in joycon_read_info()
2493 ctlr->mac_addr[4], in joycon_read_info()
2494 ctlr->mac_addr[5]); in joycon_read_info()
2495 if (!ctlr->mac_addr_str) in joycon_read_info()
2496 return -ENOMEM; in joycon_read_info()
2497 hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); in joycon_read_info()
2501 * Unfortantly the hdev->product can't always be used due to a ?bug? in joycon_read_info()
2502 * with the NSO Genesis controller. Over USB, it will report the in joycon_read_info()
2505 * the rest of the controllers which will report the same PID over USB in joycon_read_info()
2508 ctlr->ctlr_type = report->subcmd_reply.data[2]; in joycon_read_info()
2509 hid_dbg(ctlr->hdev, "controller type = 0x%02X\n", ctlr->ctlr_type); in joycon_read_info()
2519 mutex_lock(&ctlr->output_mutex); in joycon_init()
2522 hid_dbg(hdev, "detected USB controller\n"); in joycon_init()
2536 * Set no timeout (to keep controller in USB mode). in joycon_init()
2542 ret = -ETIMEDOUT; in joycon_init()
2602 mutex_unlock(&ctlr->output_mutex); in joycon_init()
2627 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
2628 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
2629 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
2634 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
2642 if (report->subcmd_reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
2650 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
2652 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
2653 ctlr->received_resp = true; in joycon_ctlr_handle_event()
2654 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
2661 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
2673 return -EINVAL; in nintendo_hid_event()
2684 hid_dbg(hdev, "probe - start\n"); in nintendo_hid_probe()
2686 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
2688 ret = -ENOMEM; in nintendo_hid_probe()
2692 ctlr->hdev = hdev; in nintendo_hid_probe()
2693 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
2694 ctlr->rumble_queue_head = 0; in nintendo_hid_probe()
2695 ctlr->rumble_queue_tail = 0; in nintendo_hid_probe()
2697 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
2698 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
2699 spin_lock_init(&ctlr->lock); in nintendo_hid_probe()
2700 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", in nintendo_hid_probe()
2702 if (!ctlr->rumble_queue) { in nintendo_hid_probe()
2703 ret = -ENOMEM; in nintendo_hid_probe()
2706 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); in nintendo_hid_probe()
2721 hdev->version |= 0x8000; in nintendo_hid_probe()
2763 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
2765 hid_dbg(hdev, "probe - success\n"); in nintendo_hid_probe()
2773 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_probe()
2775 hid_err(hdev, "probe - fail = %d\n", ret); in nintendo_hid_probe()
2787 spin_lock_irqsave(&ctlr->lock, flags); in nintendo_hid_remove()
2788 ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED; in nintendo_hid_remove()
2789 spin_unlock_irqrestore(&ctlr->lock, flags); in nintendo_hid_remove()
2791 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_remove()