Lines Matching +full:t +full:- +full:calibration +full:- +full:data
1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Nintendo Switch Joy-Cons and Pro Controllers
5 * Copyright (c) 2019-2021 Daniel J. Ogorchock <djogorchock@gmail.com>
9 * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
13 * hid-wiimote kernel hid driver
14 * hid-logitech-hidpp driver
15 * hid-sony driver
17 * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
20 * The driver will retrieve the factory calibration info from the controllers,
21 * so little to no user calibration should be required.
25 #include "hid-ids.h"
101 /* Magic value denoting presence of user calibration */
106 /* SPI storage addresses of user calibration data */
113 (JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
115 /* SPI storage addresses of factory calibration data */
119 /* SPI storage addresses of IMU factory calibration data */
123 (JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
124 /* SPI storage addresses of IMU user calibration data */
133 /* Hat values for pro controller's d-pad */
147 * configured with a range of +-8000 milliGs. Therefore, the resolution can be
148 * calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
159 * configured with a range of +-2000 degrees/second.
160 * Digits per dps: (2^16 -1)/(2000*2) = 16.38375
161 * dps per digit: 16.38375E-1 = .0610
167 * digits per dps (corrected): .0702E-1 = 14.247
174 #define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
368 u8 data[]; /* length depends on the subcommand */ member
374 u8 data[]; /* will be at most 35 bytes */ member
440 /* factory calibration data */
453 /* power supply data */
486 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
487 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
488 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
490 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
492 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
496 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
497 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
501 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
502 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
504 static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len) in __joycon_hid_send() argument
509 buf = kmemdup(data, len, GFP_KERNEL); in __joycon_hid_send()
511 return -ENOMEM; in __joycon_hid_send()
528 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_wait_for_input_report()
531 spin_lock_irqsave(&ctlr->lock, flags); in joycon_wait_for_input_report()
532 ctlr->received_input_report = false; in joycon_wait_for_input_report()
533 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_wait_for_input_report()
534 ret = wait_event_timeout(ctlr->wait, in joycon_wait_for_input_report()
535 ctlr->received_input_report, in joycon_wait_for_input_report()
539 hid_warn(ctlr->hdev, in joycon_wait_for_input_report()
545 * Sending subcommands and/or rumble data at too high a rate can cause bluetooth
555 #define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_…
564 if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ)) in joycon_enforce_subcmd_rate()
570 subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
572 spin_lock_irqsave(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
573 consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas; in joycon_enforce_subcmd_rate()
574 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
579 ctlr->ctlr_state == JOYCON_CTLR_STATE_READ && in joycon_enforce_subcmd_rate()
583 hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__); in joycon_enforce_subcmd_rate()
587 ctlr->last_subcmd_sent_msecs = current_ms; in joycon_enforce_subcmd_rate()
598 static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len, in joycon_hid_send_sync() argument
608 while (tries--) { in joycon_hid_send_sync()
611 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
613 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
617 ret = wait_event_timeout(ctlr->wait, ctlr->received_resp, in joycon_hid_send_sync()
620 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
623 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
626 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
627 ret = -ETIMEDOUT; in joycon_hid_send_sync()
634 ctlr->received_resp = false; in joycon_hid_send_sync()
644 ctlr->usb_ack_match = cmd; in joycon_send_usb()
645 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
648 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
659 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_subcmd()
662 * subsystem doesn't print invalid errors on removal. in joycon_send_subcmd()
664 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_subcmd()
665 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
666 return -ENODEV; in joycon_send_subcmd()
668 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_subcmd()
670 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
672 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; in joycon_send_subcmd()
673 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
674 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
675 ctlr->subcmd_num = 0; in joycon_send_subcmd()
676 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
677 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
682 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
695 req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; in joycon_set_player_leds()
696 req->data[0] = (flash << 4) | on; in joycon_set_player_leds()
698 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
708 u8 *data; in joycon_request_spi_flash_read() local
712 return -EINVAL; in joycon_request_spi_flash_read()
715 req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; in joycon_request_spi_flash_read()
716 data = req->data; in joycon_request_spi_flash_read()
717 put_unaligned_le32(start_addr, data); in joycon_request_spi_flash_read()
718 data[4] = size; in joycon_request_spi_flash_read()
720 hid_dbg(ctlr->hdev, "requesting SPI flash data\n"); in joycon_request_spi_flash_read()
723 hid_err(ctlr->hdev, "failed reading SPI flash; ret=%d\n", ret); in joycon_request_spi_flash_read()
725 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_spi_flash_read()
726 /* The read data starts at the 6th byte */ in joycon_request_spi_flash_read()
727 *reply = &report->subcmd_reply.data[5]; in joycon_request_spi_flash_read()
733 * User calibration's presence is denoted with a magic byte preceding it.
766 /* stick calibration parsing: note the order differs based on stick */ in joycon_read_stick_calibration()
768 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
770 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
772 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
774 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
776 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
778 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
781 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
783 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
785 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
787 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
789 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
791 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
795 cal_x->max = cal_x->center + x_max_above; in joycon_read_stick_calibration()
796 cal_x->min = cal_x->center - x_min_below; in joycon_read_stick_calibration()
797 cal_y->max = cal_y->center + y_max_above; in joycon_read_stick_calibration()
798 cal_y->min = cal_y->center - y_min_below; in joycon_read_stick_calibration()
800 /* check if calibration values are plausible */ in joycon_read_stick_calibration()
801 if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || in joycon_read_stick_calibration()
802 cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) in joycon_read_stick_calibration()
803 ret = -EINVAL; in joycon_read_stick_calibration()
820 cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; in joycon_use_default_calibration()
821 cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; in joycon_use_default_calibration()
822 cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; in joycon_use_default_calibration()
831 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
836 hid_info(ctlr->hdev, "using user cal for left stick\n"); in joycon_request_calibration()
838 hid_info(ctlr->hdev, "using factory cal for left stick\n"); in joycon_request_calibration()
842 hid_info(ctlr->hdev, "using user cal for right stick\n"); in joycon_request_calibration()
844 hid_info(ctlr->hdev, "using factory cal for right stick\n"); in joycon_request_calibration()
847 /* read the left stick calibration data */ in joycon_request_calibration()
849 &ctlr->left_stick_cal_x, in joycon_request_calibration()
850 &ctlr->left_stick_cal_y, in joycon_request_calibration()
854 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
855 &ctlr->left_stick_cal_x, in joycon_request_calibration()
856 &ctlr->left_stick_cal_y, in joycon_request_calibration()
859 /* read the right stick calibration data */ in joycon_request_calibration()
861 &ctlr->right_stick_cal_x, in joycon_request_calibration()
862 &ctlr->right_stick_cal_y, in joycon_request_calibration()
866 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
867 &ctlr->right_stick_cal_x, in joycon_request_calibration()
868 &ctlr->right_stick_cal_y, in joycon_request_calibration()
871 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
876 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
877 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
878 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
879 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
880 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
881 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
882 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
883 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
884 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
885 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
886 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
887 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
894 * dependent on the IMU calibration values. They are used when processing the
902 ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - in joycon_calc_imu_cal_divisors()
903 ctlr->accel_cal.offset[i]; in joycon_calc_imu_cal_divisors()
904 ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - in joycon_calc_imu_cal_divisors()
905 ctlr->gyro_cal.offset[i]; in joycon_calc_imu_cal_divisors()
907 if (ctlr->imu_cal_accel_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
908 ctlr->imu_cal_accel_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
912 if (ctlr->imu_cal_gyro_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
913 ctlr->imu_cal_gyro_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
919 hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz); in joycon_calc_imu_cal_divisors()
933 /* check if user calibration exists */ in joycon_request_imu_calibration()
936 hid_info(ctlr->hdev, "using user cal for IMU\n"); in joycon_request_imu_calibration()
938 hid_info(ctlr->hdev, "using factory cal for IMU\n"); in joycon_request_imu_calibration()
941 /* request IMU calibration data */ in joycon_request_imu_calibration()
942 hid_dbg(ctlr->hdev, "requesting IMU cal data\n"); in joycon_request_imu_calibration()
946 hid_warn(ctlr->hdev, in joycon_request_imu_calibration()
951 ctlr->accel_cal.offset[i] = DFLT_ACCEL_OFFSET; in joycon_request_imu_calibration()
952 ctlr->accel_cal.scale[i] = DFLT_ACCEL_SCALE; in joycon_request_imu_calibration()
953 ctlr->gyro_cal.offset[i] = DFLT_GYRO_OFFSET; in joycon_request_imu_calibration()
954 ctlr->gyro_cal.scale[i] = DFLT_GYRO_SCALE; in joycon_request_imu_calibration()
960 /* IMU calibration parsing */ in joycon_request_imu_calibration()
964 ctlr->accel_cal.offset[i] = get_unaligned_le16(raw_cal + j); in joycon_request_imu_calibration()
965 ctlr->accel_cal.scale[i] = get_unaligned_le16(raw_cal + j + 6); in joycon_request_imu_calibration()
966 ctlr->gyro_cal.offset[i] = get_unaligned_le16(raw_cal + j + 12); in joycon_request_imu_calibration()
967 ctlr->gyro_cal.scale[i] = get_unaligned_le16(raw_cal + j + 18); in joycon_request_imu_calibration()
972 hid_dbg(ctlr->hdev, "IMU calibration:\n" in joycon_request_imu_calibration()
977 ctlr->accel_cal.offset[0], in joycon_request_imu_calibration()
978 ctlr->accel_cal.offset[1], in joycon_request_imu_calibration()
979 ctlr->accel_cal.offset[2], in joycon_request_imu_calibration()
980 ctlr->accel_cal.scale[0], in joycon_request_imu_calibration()
981 ctlr->accel_cal.scale[1], in joycon_request_imu_calibration()
982 ctlr->accel_cal.scale[2], in joycon_request_imu_calibration()
983 ctlr->gyro_cal.offset[0], in joycon_request_imu_calibration()
984 ctlr->gyro_cal.offset[1], in joycon_request_imu_calibration()
985 ctlr->gyro_cal.offset[2], in joycon_request_imu_calibration()
986 ctlr->gyro_cal.scale[0], in joycon_request_imu_calibration()
987 ctlr->gyro_cal.scale[1], in joycon_request_imu_calibration()
988 ctlr->gyro_cal.scale[2]); in joycon_request_imu_calibration()
999 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; in joycon_set_report_mode()
1000 req->data[0] = 0x30; /* standard, full report mode */ in joycon_set_report_mode()
1002 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
1012 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; in joycon_enable_rumble()
1013 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_rumble()
1015 hid_dbg(ctlr->hdev, "enabling rumble\n"); in joycon_enable_rumble()
1025 req->subcmd_id = JC_SUBCMD_ENABLE_IMU; in joycon_enable_imu()
1026 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_imu()
1028 hid_dbg(ctlr->hdev, "enabling IMU\n"); in joycon_enable_imu()
1034 s32 center = cal->center; in joycon_map_stick_val()
1035 s32 min = cal->min; in joycon_map_stick_val()
1036 s32 max = cal->max; in joycon_map_stick_val()
1040 new_val = (val - center) * JC_MAX_STICK_MAG; in joycon_map_stick_val()
1041 new_val /= (max - center); in joycon_map_stick_val()
1043 new_val = (center - val) * -JC_MAX_STICK_MAG; in joycon_map_stick_val()
1044 new_val /= (center - min); in joycon_map_stick_val()
1046 new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG); in joycon_map_stick_val()
1054 u8 *raw = rep->imu_raw_bytes; in joycon_input_report_parse_imu_data()
1058 struct joycon_imu_data *data = &imu_data[i]; in joycon_input_report_parse_imu_data() local
1060 data->accel_x = get_unaligned_le16(raw + 0); in joycon_input_report_parse_imu_data()
1061 data->accel_y = get_unaligned_le16(raw + 2); in joycon_input_report_parse_imu_data()
1062 data->accel_z = get_unaligned_le16(raw + 4); in joycon_input_report_parse_imu_data()
1063 data->gyro_x = get_unaligned_le16(raw + 6); in joycon_input_report_parse_imu_data()
1064 data->gyro_y = get_unaligned_le16(raw + 8); in joycon_input_report_parse_imu_data()
1065 data->gyro_z = get_unaligned_le16(raw + 10); in joycon_input_report_parse_imu_data()
1075 struct input_dev *idev = ctlr->imu_input; in joycon_parse_imu_report()
1077 unsigned int last_msecs = ctlr->imu_last_pkt_ms; in joycon_parse_imu_report()
1087 * incrementing 8-bit counter per input report, but it is not very in joycon_parse_imu_report()
1089 * increments at or if it varies based on packet push rate - more on in joycon_parse_imu_report()
1092 * The reverse engineering work done on the joy-cons and pro controllers in joycon_parse_imu_report()
1094 * - The controller samples the IMU every 1.35ms. It then does some of in joycon_parse_imu_report()
1096 * - Each imu input report contains 3 IMU samples, (usually 5ms apart). in joycon_parse_imu_report()
1097 * - In the standard reporting mode (which this driver uses exclusively) in joycon_parse_imu_report()
1099 * * joy-con (bluetooth): every 15 ms in joycon_parse_imu_report()
1100 * * joy-cons (in charging grip via USB): every 15 ms in joycon_parse_imu_report()
1107 * SSR to 11ms for both the joy-cons and pro controllers). in joycon_parse_imu_report()
1111 * stable after connecting. It isn't 100% clear what determines this in joycon_parse_imu_report()
1130 if (!ctlr->imu_first_packet_received) { in joycon_parse_imu_report()
1131 ctlr->imu_timestamp_us = 0; in joycon_parse_imu_report()
1132 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1133 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1134 ctlr->imu_avg_delta_ms = JC_IMU_DFLT_AVG_DELTA_MS; in joycon_parse_imu_report()
1135 ctlr->imu_first_packet_received = true; in joycon_parse_imu_report()
1137 unsigned int delta = msecs - last_msecs; in joycon_parse_imu_report()
1142 ctlr->imu_delta_samples_sum += delta; in joycon_parse_imu_report()
1143 ctlr->imu_delta_samples_count++; in joycon_parse_imu_report()
1144 if (ctlr->imu_delta_samples_count >= in joycon_parse_imu_report()
1146 ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / in joycon_parse_imu_report()
1147 ctlr->imu_delta_samples_count; in joycon_parse_imu_report()
1148 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1149 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1152 /* don't ever want divide by zero shenanigans */ in joycon_parse_imu_report()
1153 if (ctlr->imu_avg_delta_ms == 0) { in joycon_parse_imu_report()
1154 ctlr->imu_avg_delta_ms = 1; in joycon_parse_imu_report()
1155 hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n"); in joycon_parse_imu_report()
1159 hid_dbg(ctlr->hdev, in joycon_parse_imu_report()
1161 msecs, last_msecs, delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1164 dropped_threshold = ctlr->imu_avg_delta_ms * 3 / 2; in joycon_parse_imu_report()
1165 dropped_pkts = (delta - min(delta, dropped_threshold)) / in joycon_parse_imu_report()
1166 ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1167 ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1169 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1172 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1174 delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1177 ctlr->imu_last_pkt_ms = msecs; in joycon_parse_imu_report()
1182 ctlr->imu_timestamp_us); in joycon_parse_imu_report()
1185 * These calculations (which use the controller's calibration in joycon_parse_imu_report()
1187 * found in the community's reverse-engineering repo (linked at in joycon_parse_imu_report()
1188 * top of driver). For hid-nintendo, we make sure that the final in joycon_parse_imu_report()
1192 * Currently only the gyro calculations subtract the calibration in joycon_parse_imu_report()
1197 * precision-saving scaling factor to prevent large inaccuracies in joycon_parse_imu_report()
1203 (imu_data[i].gyro_x - in joycon_parse_imu_report()
1204 ctlr->gyro_cal.offset[0])), in joycon_parse_imu_report()
1205 ctlr->gyro_cal.scale[0], in joycon_parse_imu_report()
1206 ctlr->imu_cal_gyro_divisor[0]); in joycon_parse_imu_report()
1208 (imu_data[i].gyro_y - in joycon_parse_imu_report()
1209 ctlr->gyro_cal.offset[1])), in joycon_parse_imu_report()
1210 ctlr->gyro_cal.scale[1], in joycon_parse_imu_report()
1211 ctlr->imu_cal_gyro_divisor[1]); in joycon_parse_imu_report()
1213 (imu_data[i].gyro_z - in joycon_parse_imu_report()
1214 ctlr->gyro_cal.offset[2])), in joycon_parse_imu_report()
1215 ctlr->gyro_cal.scale[2], in joycon_parse_imu_report()
1216 ctlr->imu_cal_gyro_divisor[2]); in joycon_parse_imu_report()
1219 ctlr->accel_cal.scale[0]) / in joycon_parse_imu_report()
1220 ctlr->imu_cal_accel_divisor[0]; in joycon_parse_imu_report()
1222 ctlr->accel_cal.scale[1]) / in joycon_parse_imu_report()
1223 ctlr->imu_cal_accel_divisor[1]; in joycon_parse_imu_report()
1225 ctlr->accel_cal.scale[2]) / in joycon_parse_imu_report()
1226 ctlr->imu_cal_accel_divisor[2]; in joycon_parse_imu_report()
1228 hid_dbg(ctlr->hdev, "raw_gyro: g_x=%d g_y=%d g_z=%d\n", in joycon_parse_imu_report()
1231 hid_dbg(ctlr->hdev, "raw_accel: a_x=%d a_y=%d a_z=%d\n", in joycon_parse_imu_report()
1236 * The right joy-con has 2 axes negated, Y and Z. This is due to in joycon_parse_imu_report()
1238 * axes' values in order to be consistent with the left joy-con in joycon_parse_imu_report()
1243 * The axes follow the right-hand rule. in joycon_parse_imu_report()
1252 value[j] *= -1; in joycon_parse_imu_report()
1264 ctlr->imu_timestamp_us += ctlr->imu_avg_delta_ms * 1000 / 3; in joycon_parse_imu_report()
1271 struct input_dev *dev = ctlr->input; in joycon_parse_report()
1276 unsigned long report_delta_ms = msecs - ctlr->last_input_report_msecs; in joycon_parse_report()
1278 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1279 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report && in joycon_parse_report()
1280 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED && in joycon_parse_report()
1281 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS && in joycon_parse_report()
1282 (ctlr->rumble_queue_head != ctlr->rumble_queue_tail || in joycon_parse_report()
1283 ctlr->rumble_zero_countdown > 0)) { in joycon_parse_report()
1290 if (ctlr->rumble_zero_countdown > 0) in joycon_parse_report()
1291 ctlr->rumble_zero_countdown--; in joycon_parse_report()
1292 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_parse_report()
1296 tmp = rep->bat_con; in joycon_parse_report()
1297 ctlr->host_powered = tmp & BIT(0); in joycon_parse_report()
1298 ctlr->battery_charging = tmp & BIT(4); in joycon_parse_report()
1302 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; in joycon_parse_report()
1305 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; in joycon_parse_report()
1308 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; in joycon_parse_report()
1311 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; in joycon_parse_report()
1314 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; in joycon_parse_report()
1317 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_parse_report()
1318 hid_warn(ctlr->hdev, "Invalid battery status\n"); in joycon_parse_report()
1321 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1324 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_parse_report()
1333 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_parse_report()
1334 raw_y = hid_field_extract(ctlr->hdev, in joycon_parse_report()
1335 rep->left_stick + 1, 4, 12); in joycon_parse_report()
1337 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_parse_report()
1338 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_parse_report()
1351 /* Report the S buttons as the non-existent triggers */ in joycon_parse_report()
1355 /* Report d-pad as digital buttons for the joy-cons */ in joycon_parse_report()
1367 /* d-pad x */ in joycon_parse_report()
1369 hatx = -1; in joycon_parse_report()
1374 /* d-pad y */ in joycon_parse_report()
1376 haty = -1; in joycon_parse_report()
1389 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_parse_report()
1390 raw_y = hid_field_extract(ctlr->hdev, in joycon_parse_report()
1391 rep->right_stick + 1, 4, 12); in joycon_parse_report()
1393 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_parse_report()
1394 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_parse_report()
1403 /* Report the S buttons as the non-existent triggers */ in joycon_parse_report()
1418 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1419 ctlr->last_input_report_msecs = msecs; in joycon_parse_report()
1427 if (ctlr->consecutive_valid_report_deltas < JC_SUBCMD_VALID_DELTA_REQ) in joycon_parse_report()
1428 ctlr->consecutive_valid_report_deltas++; in joycon_parse_report()
1430 ctlr->consecutive_valid_report_deltas = 0; in joycon_parse_report()
1434 * bluetooth-connected controllers. For USB devices, we're beholden to in joycon_parse_report()
1438 if (ctlr->hdev->bus == BUS_USB) in joycon_parse_report()
1439 ctlr->consecutive_valid_report_deltas = JC_SUBCMD_VALID_DELTA_REQ; in joycon_parse_report()
1441 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1448 if (unlikely(mutex_is_locked(&ctlr->output_mutex))) { in joycon_parse_report()
1449 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1450 ctlr->received_input_report = true; in joycon_parse_report()
1451 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1452 wake_up(&ctlr->wait); in joycon_parse_report()
1455 /* parse IMU data if present */ in joycon_parse_report()
1456 if (rep->id == JC_INPUT_IMU_DATA) in joycon_parse_report()
1466 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_rumble_data()
1469 * subsystem doesn't print invalid errors on removal. in joycon_send_rumble_data()
1471 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_rumble_data()
1472 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1473 return -ENODEV; in joycon_send_rumble_data()
1476 ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_rumble_data()
1478 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1481 rumble_output.packet_num = ctlr->subcmd_num; in joycon_send_rumble_data()
1482 if (++ctlr->subcmd_num > 0xF) in joycon_send_rumble_data()
1483 ctlr->subcmd_num = 0; in joycon_send_rumble_data()
1487 ret = __joycon_hid_send(ctlr->hdev, (u8 *)&rumble_output, in joycon_send_rumble_data()
1501 mutex_lock(&ctlr->output_mutex); in joycon_rumble_worker()
1503 mutex_unlock(&ctlr->output_mutex); in joycon_rumble_worker()
1505 /* -ENODEV means the controller was just unplugged */ in joycon_rumble_worker()
1506 spin_lock_irqsave(&ctlr->lock, flags); in joycon_rumble_worker()
1507 if (ret < 0 && ret != -ENODEV && in joycon_rumble_worker()
1508 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_rumble_worker()
1509 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret); in joycon_rumble_worker()
1511 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_rumble_worker()
1512 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) { in joycon_rumble_worker()
1513 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE) in joycon_rumble_worker()
1514 ctlr->rumble_queue_tail = 0; in joycon_rumble_worker()
1518 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_rumble_worker()
1526 const struct joycon_rumble_freq_data *data = joycon_rumble_frequencies; in joycon_find_rumble_freq() local
1529 if (freq > data[0].freq) { in joycon_find_rumble_freq()
1530 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_freq()
1531 if (freq > data[i - 1].freq && freq <= data[i].freq) in joycon_find_rumble_freq()
1536 return data[i]; in joycon_find_rumble_freq()
1542 const struct joycon_rumble_amp_data *data = joycon_rumble_amplitudes; in joycon_find_rumble_amp() local
1545 if (amp > data[0].amp) { in joycon_find_rumble_amp()
1546 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_amp()
1547 if (amp > data[i - 1].amp && amp <= data[i].amp) in joycon_find_rumble_amp()
1552 return data[i]; in joycon_find_rumble_amp()
1555 static void joycon_encode_rumble(u8 *data, u16 freq_low, u16 freq_high, u16 amp) in joycon_encode_rumble() argument
1565 data[0] = (freq_data_high.high >> 8) & 0xFF; in joycon_encode_rumble()
1566 data[1] = (freq_data_high.high & 0xFF) + amp_data.high; in joycon_encode_rumble()
1567 data[2] = freq_data_low.low + ((amp_data.low >> 8) & 0xFF); in joycon_encode_rumble()
1568 data[3] = amp_data.low & 0xFF; in joycon_encode_rumble()
1580 spin_lock_irqsave(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1581 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, in joycon_clamp_rumble_freqs()
1584 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, in joycon_clamp_rumble_freqs()
1587 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, in joycon_clamp_rumble_freqs()
1590 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, in joycon_clamp_rumble_freqs()
1593 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1599 u8 data[JC_RUMBLE_DATA_SIZE]; in joycon_set_rumble() local
1608 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1609 freq_r_low = ctlr->rumble_rl_freq; in joycon_set_rumble()
1610 freq_r_high = ctlr->rumble_rh_freq; in joycon_set_rumble()
1611 freq_l_low = ctlr->rumble_ll_freq; in joycon_set_rumble()
1612 freq_l_high = ctlr->rumble_lh_freq; in joycon_set_rumble()
1615 ctlr->rumble_zero_countdown = JC_RUMBLE_ZERO_AMP_PKT_CNT; in joycon_set_rumble()
1616 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1618 /* right joy-con */ in joycon_set_rumble()
1620 joycon_encode_rumble(data + 4, freq_r_low, freq_r_high, amp); in joycon_set_rumble()
1622 /* left joy-con */ in joycon_set_rumble()
1624 joycon_encode_rumble(data, freq_l_low, freq_l_high, amp); in joycon_set_rumble()
1626 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1628 next_rq_head = ctlr->rumble_queue_head + 1; in joycon_set_rumble()
1635 if (next_rq_head == ctlr->rumble_queue_tail) { in joycon_set_rumble()
1636 hid_dbg(ctlr->hdev, "rumble queue is full"); in joycon_set_rumble()
1638 next_rq_head = ctlr->rumble_queue_head; in joycon_set_rumble()
1641 ctlr->rumble_queue_head = next_rq_head; in joycon_set_rumble()
1642 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, in joycon_set_rumble()
1645 /* don't wait for the periodic send (reduces latency) */ in joycon_set_rumble()
1646 if (schedule_now && ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_set_rumble()
1647 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_set_rumble()
1649 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1654 static int joycon_play_effect(struct input_dev *dev, void *data, in joycon_play_effect() argument
1659 if (effect->type != FF_RUMBLE) in joycon_play_effect()
1663 effect->u.rumble.weak_magnitude, in joycon_play_effect()
1664 effect->u.rumble.strong_magnitude, in joycon_play_effect()
1682 /* We report joy-con d-pad inputs as buttons and pro controller as a hat. */
1696 hdev = ctlr->hdev; in joycon_input_create()
1698 switch (hdev->product) { in joycon_input_create()
1705 name = "Nintendo Switch Left Joy-Con (Grip)"; in joycon_input_create()
1706 imu_name = "Nintendo Switch Left Joy-Con IMU (Grip)"; in joycon_input_create()
1708 name = "Nintendo Switch Right Joy-Con (Grip)"; in joycon_input_create()
1709 imu_name = "Nintendo Switch Right Joy-Con IMU (Grip)"; in joycon_input_create()
1713 name = "Nintendo Switch Left Joy-Con"; in joycon_input_create()
1714 imu_name = "Nintendo Switch Left Joy-Con IMU"; in joycon_input_create()
1717 name = "Nintendo Switch Right Joy-Con"; in joycon_input_create()
1718 imu_name = "Nintendo Switch Right Joy-Con IMU"; in joycon_input_create()
1722 return -EINVAL; in joycon_input_create()
1725 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
1726 if (!ctlr->input) in joycon_input_create()
1727 return -ENOMEM; in joycon_input_create()
1728 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
1729 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
1730 ctlr->input->id.product = hdev->product; in joycon_input_create()
1731 ctlr->input->id.version = hdev->version; in joycon_input_create()
1732 ctlr->input->uniq = ctlr->mac_addr_str; in joycon_input_create()
1733 ctlr->input->name = name; in joycon_input_create()
1734 ctlr->input->phys = hdev->phys; in joycon_input_create()
1735 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
1739 input_set_abs_params(ctlr->input, ABS_X, in joycon_input_create()
1740 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1742 input_set_abs_params(ctlr->input, ABS_Y, in joycon_input_create()
1743 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1747 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
1750 /* configure d-pad differently for joy-con vs pro controller */ in joycon_input_create()
1751 if (hdev->product != USB_DEVICE_ID_NINTENDO_PROCON) { in joycon_input_create()
1753 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
1756 input_set_abs_params(ctlr->input, ABS_HAT0X, in joycon_input_create()
1757 -JC_MAX_DPAD_MAG, JC_MAX_DPAD_MAG, in joycon_input_create()
1759 input_set_abs_params(ctlr->input, ABS_HAT0Y, in joycon_input_create()
1760 -JC_MAX_DPAD_MAG, JC_MAX_DPAD_MAG, in joycon_input_create()
1765 input_set_abs_params(ctlr->input, ABS_RX, in joycon_input_create()
1766 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1768 input_set_abs_params(ctlr->input, ABS_RY, in joycon_input_create()
1769 -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, in joycon_input_create()
1773 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
1777 /* Let's report joy-con S triggers separately */ in joycon_input_create()
1778 if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL) { in joycon_input_create()
1779 input_set_capability(ctlr->input, EV_KEY, BTN_TR); in joycon_input_create()
1780 input_set_capability(ctlr->input, EV_KEY, BTN_TR2); in joycon_input_create()
1781 } else if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR) { in joycon_input_create()
1782 input_set_capability(ctlr->input, EV_KEY, BTN_TL); in joycon_input_create()
1783 input_set_capability(ctlr->input, EV_KEY, BTN_TL2); in joycon_input_create()
1788 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); in joycon_input_create()
1789 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); in joycon_input_create()
1790 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_input_create()
1791 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_input_create()
1792 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_input_create()
1793 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_input_create()
1796 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_input_create()
1799 ret = input_register_device(ctlr->input); in joycon_input_create()
1804 ctlr->imu_input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
1805 if (!ctlr->imu_input) in joycon_input_create()
1806 return -ENOMEM; in joycon_input_create()
1808 ctlr->imu_input->id.bustype = hdev->bus; in joycon_input_create()
1809 ctlr->imu_input->id.vendor = hdev->vendor; in joycon_input_create()
1810 ctlr->imu_input->id.product = hdev->product; in joycon_input_create()
1811 ctlr->imu_input->id.version = hdev->version; in joycon_input_create()
1812 ctlr->imu_input->uniq = ctlr->mac_addr_str; in joycon_input_create()
1813 ctlr->imu_input->name = imu_name; in joycon_input_create()
1814 ctlr->imu_input->phys = hdev->phys; in joycon_input_create()
1815 input_set_drvdata(ctlr->imu_input, ctlr); in joycon_input_create()
1818 input_set_abs_params(ctlr->imu_input, ABS_X, in joycon_input_create()
1819 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_input_create()
1821 input_set_abs_params(ctlr->imu_input, ABS_Y, in joycon_input_create()
1822 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_input_create()
1824 input_set_abs_params(ctlr->imu_input, ABS_Z, in joycon_input_create()
1825 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_input_create()
1827 input_abs_set_res(ctlr->imu_input, ABS_X, JC_IMU_ACCEL_RES_PER_G); in joycon_input_create()
1828 input_abs_set_res(ctlr->imu_input, ABS_Y, JC_IMU_ACCEL_RES_PER_G); in joycon_input_create()
1829 input_abs_set_res(ctlr->imu_input, ABS_Z, JC_IMU_ACCEL_RES_PER_G); in joycon_input_create()
1831 input_set_abs_params(ctlr->imu_input, ABS_RX, in joycon_input_create()
1832 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_input_create()
1834 input_set_abs_params(ctlr->imu_input, ABS_RY, in joycon_input_create()
1835 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_input_create()
1837 input_set_abs_params(ctlr->imu_input, ABS_RZ, in joycon_input_create()
1838 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_input_create()
1841 input_abs_set_res(ctlr->imu_input, ABS_RX, JC_IMU_GYRO_RES_PER_DPS); in joycon_input_create()
1842 input_abs_set_res(ctlr->imu_input, ABS_RY, JC_IMU_GYRO_RES_PER_DPS); in joycon_input_create()
1843 input_abs_set_res(ctlr->imu_input, ABS_RZ, JC_IMU_GYRO_RES_PER_DPS); in joycon_input_create()
1845 __set_bit(EV_MSC, ctlr->imu_input->evbit); in joycon_input_create()
1846 __set_bit(MSC_TIMESTAMP, ctlr->imu_input->mscbit); in joycon_input_create()
1847 __set_bit(INPUT_PROP_ACCELEROMETER, ctlr->imu_input->propbit); in joycon_input_create()
1849 ret = input_register_device(ctlr->imu_input); in joycon_input_create()
1859 struct device *dev = led->dev->parent; in joycon_player_led_brightness_set()
1869 hid_err(hdev, "No controller data\n"); in joycon_player_led_brightness_set()
1870 return -ENODEV; in joycon_player_led_brightness_set()
1875 if (&ctlr->leds[num] == led) in joycon_player_led_brightness_set()
1879 return -EINVAL; in joycon_player_led_brightness_set()
1881 mutex_lock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
1886 val |= ctlr->leds[i].brightness << i; in joycon_player_led_brightness_set()
1889 mutex_unlock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
1897 struct device *dev = led->dev->parent; in joycon_home_led_brightness_set()
1902 u8 *data; in joycon_home_led_brightness_set() local
1907 hid_err(hdev, "No controller data\n"); in joycon_home_led_brightness_set()
1908 return -ENODEV; in joycon_home_led_brightness_set()
1912 req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; in joycon_home_led_brightness_set()
1913 data = req->data; in joycon_home_led_brightness_set()
1914 data[0] = 0x01; in joycon_home_led_brightness_set()
1915 data[1] = brightness << 4; in joycon_home_led_brightness_set()
1916 data[2] = brightness | (brightness << 4); in joycon_home_led_brightness_set()
1917 data[3] = 0x11; in joycon_home_led_brightness_set()
1918 data[4] = 0x11; in joycon_home_led_brightness_set()
1921 mutex_lock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
1923 mutex_unlock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
1931 struct hid_device *hdev = ctlr->hdev; in joycon_leds_create()
1932 struct device *dev = &hdev->dev; in joycon_leds_create()
1942 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
1943 ret = joycon_set_player_leds(ctlr, 0, 0xF >> (4 - input_num)); in joycon_leds_create()
1945 hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret); in joycon_leds_create()
1946 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
1956 return -ENOMEM; in joycon_leds_create()
1959 led = &ctlr->leds[i]; in joycon_leds_create()
1960 led->name = name; in joycon_leds_create()
1961 led->brightness = ((i + 1) <= input_num) ? 1 : 0; in joycon_leds_create()
1962 led->max_brightness = 1; in joycon_leds_create()
1963 led->brightness_set_blocking = in joycon_leds_create()
1965 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
1967 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
1969 hid_err(hdev, "Failed registering %s LED\n", led->name); in joycon_leds_create()
1986 return -ENOMEM; in joycon_leds_create()
1988 led = &ctlr->home_led; in joycon_leds_create()
1989 led->name = name; in joycon_leds_create()
1990 led->brightness = 0; in joycon_leds_create()
1991 led->max_brightness = 0xF; in joycon_leds_create()
1992 led->brightness_set_blocking = joycon_home_led_brightness_set; in joycon_leds_create()
1993 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
1994 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2003 devm_led_classdev_unregister(&hdev->dev, led); in joycon_leds_create()
2021 spin_lock_irqsave(&ctlr->lock, flags); in joycon_battery_get_property()
2022 capacity = ctlr->battery_capacity; in joycon_battery_get_property()
2023 charging = ctlr->battery_charging; in joycon_battery_get_property()
2024 powered = ctlr->host_powered; in joycon_battery_get_property()
2025 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_battery_get_property()
2029 val->intval = 1; in joycon_battery_get_property()
2032 val->intval = POWER_SUPPLY_SCOPE_DEVICE; in joycon_battery_get_property()
2035 val->intval = capacity; in joycon_battery_get_property()
2039 val->intval = POWER_SUPPLY_STATUS_CHARGING; in joycon_battery_get_property()
2042 val->intval = POWER_SUPPLY_STATUS_FULL; in joycon_battery_get_property()
2044 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in joycon_battery_get_property()
2047 ret = -EINVAL; in joycon_battery_get_property()
2062 struct hid_device *hdev = ctlr->hdev; in joycon_power_supply_create()
2068 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_power_supply_create()
2071 ctlr->battery_desc.properties = joycon_battery_props; in joycon_power_supply_create()
2072 ctlr->battery_desc.num_properties = in joycon_power_supply_create()
2074 ctlr->battery_desc.get_property = joycon_battery_get_property; in joycon_power_supply_create()
2075 ctlr->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; in joycon_power_supply_create()
2076 ctlr->battery_desc.use_for_apm = 0; in joycon_power_supply_create()
2077 ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, in joycon_power_supply_create()
2079 dev_name(&hdev->dev)); in joycon_power_supply_create()
2080 if (!ctlr->battery_desc.name) in joycon_power_supply_create()
2081 return -ENOMEM; in joycon_power_supply_create()
2083 ctlr->battery = devm_power_supply_register(&hdev->dev, in joycon_power_supply_create()
2084 &ctlr->battery_desc, in joycon_power_supply_create()
2086 if (IS_ERR(ctlr->battery)) { in joycon_power_supply_create()
2087 ret = PTR_ERR(ctlr->battery); in joycon_power_supply_create()
2092 return power_supply_powers(ctlr->battery, &hdev->dev); in joycon_power_supply_create()
2104 mutex_lock(&ctlr->output_mutex); in joycon_read_info()
2106 mutex_unlock(&ctlr->output_mutex); in joycon_read_info()
2108 hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); in joycon_read_info()
2112 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_read_info()
2115 ctlr->mac_addr[j] = report->subcmd_reply.data[i]; in joycon_read_info()
2117 ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, in joycon_read_info()
2119 ctlr->mac_addr[0], in joycon_read_info()
2120 ctlr->mac_addr[1], in joycon_read_info()
2121 ctlr->mac_addr[2], in joycon_read_info()
2122 ctlr->mac_addr[3], in joycon_read_info()
2123 ctlr->mac_addr[4], in joycon_read_info()
2124 ctlr->mac_addr[5]); in joycon_read_info()
2125 if (!ctlr->mac_addr_str) in joycon_read_info()
2126 return -ENOMEM; in joycon_read_info()
2127 hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); in joycon_read_info()
2130 ctlr->ctlr_type = report->subcmd_reply.data[2]; in joycon_read_info()
2140 mutex_lock(&ctlr->output_mutex); in joycon_init()
2159 * This doesn't send a response, so ignore the timeout. in joycon_init()
2164 ret = -ETIMEDOUT; in joycon_init()
2168 /* get controller calibration data, and parse it */ in joycon_init()
2172 * We can function with default calibration, but it may be in joycon_init()
2178 /* get IMU calibration data, and parse it */ in joycon_init()
2182 * We can function with default calibration, but it may be in joycon_init()
2185 hid_warn(hdev, "Unable to read IMU calibration data\n"); in joycon_init()
2210 mutex_unlock(&ctlr->output_mutex); in joycon_init()
2215 static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data, in joycon_ctlr_read_handler() argument
2218 if (data[0] == JC_INPUT_SUBCMD_REPLY || data[0] == JC_INPUT_IMU_DATA || in joycon_ctlr_read_handler()
2219 data[0] == JC_INPUT_MCU_DATA) { in joycon_ctlr_read_handler()
2222 (struct joycon_input_report *)data); in joycon_ctlr_read_handler()
2228 static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, in joycon_ctlr_handle_event() argument
2235 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
2236 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
2237 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
2241 if (data[0] == JC_INPUT_USB_RESPONSE && in joycon_ctlr_handle_event()
2242 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
2247 data[0] != JC_INPUT_SUBCMD_REPLY) in joycon_ctlr_handle_event()
2249 report = (struct joycon_input_report *)data; in joycon_ctlr_handle_event()
2250 if (report->subcmd_reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
2258 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
2260 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
2261 ctlr->received_resp = true; in joycon_ctlr_handle_event()
2262 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
2269 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
2270 ret = joycon_ctlr_read_handler(ctlr, data, size); in joycon_ctlr_handle_event()
2281 return -EINVAL; in nintendo_hid_event()
2292 hid_dbg(hdev, "probe - start\n"); in nintendo_hid_probe()
2294 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
2296 ret = -ENOMEM; in nintendo_hid_probe()
2300 ctlr->hdev = hdev; in nintendo_hid_probe()
2301 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
2302 ctlr->rumble_queue_head = 0; in nintendo_hid_probe()
2303 ctlr->rumble_queue_tail = 0; in nintendo_hid_probe()
2305 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
2306 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
2307 spin_lock_init(&ctlr->lock); in nintendo_hid_probe()
2308 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", in nintendo_hid_probe()
2310 if (!ctlr->rumble_queue) { in nintendo_hid_probe()
2311 ret = -ENOMEM; in nintendo_hid_probe()
2314 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); in nintendo_hid_probe()
2329 hdev->version |= 0x8000; in nintendo_hid_probe()
2378 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
2380 hid_dbg(hdev, "probe - success\n"); in nintendo_hid_probe()
2388 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_probe()
2390 hid_err(hdev, "probe - fail = %d\n", ret); in nintendo_hid_probe()
2402 spin_lock_irqsave(&ctlr->lock, flags); in nintendo_hid_remove()
2403 ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED; in nintendo_hid_remove()
2404 spin_unlock_irqrestore(&ctlr->lock, flags); in nintendo_hid_remove()
2406 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_remove()