diff options
Diffstat (limited to 'drivers/hid/hid-nintendo.c')
| -rw-r--r-- | drivers/hid/hid-nintendo.c | 1320 |
1 files changed, 919 insertions, 401 deletions
diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c index 5bfc0c450460..7ac9217d9096 100644 --- a/drivers/hid/hid-nintendo.c +++ b/drivers/hid/hid-nintendo.c @@ -3,6 +3,9 @@ * HID driver for Nintendo Switch Joy-Cons and Pro Controllers * * Copyright (c) 2019-2021 Daniel J. Ogorchock <djogorchock@gmail.com> + * Portions Copyright (c) 2020 Nadia Holmquist Pedersen <nadia@nhp.sh> + * Copyright (c) 2022 Emily Strickland <linux@emily.st> + * Copyright (c) 2023 Ryan McClelland <rymcclel@gmail.com> * * The following resources/projects were referenced for this driver: * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering @@ -17,17 +20,21 @@ * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The * Pro Controllers can either be used over USB or Bluetooth. * + * This driver also incorporates support for Nintendo Switch Online controllers + * for the NES, SNES, Sega Genesis, and N64. + * * The driver will retrieve the factory calibration info from the controllers, * so little to no user calibration should be required. * */ #include "hid-ids.h" -#include <asm/unaligned.h> +#include <linux/unaligned.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/kernel.h> #include <linux/hid.h> +#include <linux/idr.h> #include <linux/input.h> #include <linux/jiffies.h> #include <linux/leds.h> @@ -301,13 +308,19 @@ enum joycon_ctlr_state { JOYCON_CTLR_STATE_INIT, JOYCON_CTLR_STATE_READ, JOYCON_CTLR_STATE_REMOVED, + JOYCON_CTLR_STATE_SUSPENDED, }; /* Controller type received as part of device info */ enum joycon_ctlr_type { - JOYCON_CTLR_TYPE_JCL = 0x01, - JOYCON_CTLR_TYPE_JCR = 0x02, - JOYCON_CTLR_TYPE_PRO = 0x03, + JOYCON_CTLR_TYPE_JCL = 0x01, + JOYCON_CTLR_TYPE_JCR = 0x02, + JOYCON_CTLR_TYPE_PRO = 0x03, + JOYCON_CTLR_TYPE_NESL = 0x09, + JOYCON_CTLR_TYPE_NESR = 0x0A, + JOYCON_CTLR_TYPE_SNES = 0x0B, + JOYCON_CTLR_TYPE_GEN = 0x0D, + JOYCON_CTLR_TYPE_N64 = 0x0C, }; struct joycon_stick_cal { @@ -325,28 +338,155 @@ struct joycon_imu_cal { * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. */ -static const u32 JC_BTN_Y = BIT(0); -static const u32 JC_BTN_X = BIT(1); -static const u32 JC_BTN_B = BIT(2); -static const u32 JC_BTN_A = BIT(3); -static const u32 JC_BTN_SR_R = BIT(4); -static const u32 JC_BTN_SL_R = BIT(5); -static const u32 JC_BTN_R = BIT(6); -static const u32 JC_BTN_ZR = BIT(7); -static const u32 JC_BTN_MINUS = BIT(8); -static const u32 JC_BTN_PLUS = BIT(9); -static const u32 JC_BTN_RSTICK = BIT(10); -static const u32 JC_BTN_LSTICK = BIT(11); -static const u32 JC_BTN_HOME = BIT(12); -static const u32 JC_BTN_CAP = BIT(13); /* capture button */ -static const u32 JC_BTN_DOWN = BIT(16); -static const u32 JC_BTN_UP = BIT(17); -static const u32 JC_BTN_RIGHT = BIT(18); -static const u32 JC_BTN_LEFT = BIT(19); -static const u32 JC_BTN_SR_L = BIT(20); -static const u32 JC_BTN_SL_L = BIT(21); -static const u32 JC_BTN_L = BIT(22); -static const u32 JC_BTN_ZL = BIT(23); +#define JC_BTN_Y BIT(0) +#define JC_BTN_X BIT(1) +#define JC_BTN_B BIT(2) +#define JC_BTN_A BIT(3) +#define JC_BTN_SR_R BIT(4) +#define JC_BTN_SL_R BIT(5) +#define JC_BTN_R BIT(6) +#define JC_BTN_ZR BIT(7) +#define JC_BTN_MINUS BIT(8) +#define JC_BTN_PLUS BIT(9) +#define JC_BTN_RSTICK BIT(10) +#define JC_BTN_LSTICK BIT(11) +#define JC_BTN_HOME BIT(12) +#define JC_BTN_CAP BIT(13) /* capture button */ +#define JC_BTN_DOWN BIT(16) +#define JC_BTN_UP BIT(17) +#define JC_BTN_RIGHT BIT(18) +#define JC_BTN_LEFT BIT(19) +#define JC_BTN_SR_L BIT(20) +#define JC_BTN_SL_L BIT(21) +#define JC_BTN_L BIT(22) +#define JC_BTN_ZL BIT(23) + +struct joycon_ctlr_button_mapping { + u32 code; + u32 bit; +}; + +/* + * D-pad is configured as buttons for the left Joy-Con only! + */ +static const struct joycon_ctlr_button_mapping left_joycon_button_mappings[] = { + { BTN_TL, JC_BTN_L, }, + { BTN_TL2, JC_BTN_ZL, }, + { BTN_SELECT, JC_BTN_MINUS, }, + { BTN_THUMBL, JC_BTN_LSTICK, }, + { BTN_DPAD_UP, JC_BTN_UP, }, + { BTN_DPAD_DOWN, JC_BTN_DOWN, }, + { BTN_DPAD_LEFT, JC_BTN_LEFT, }, + { BTN_DPAD_RIGHT, JC_BTN_RIGHT, }, + { BTN_Z, JC_BTN_CAP, }, + { /* sentinel */ }, +}; + +/* + * The unused *right*-side triggers become the SL/SR triggers for the *left* + * Joy-Con, if and only if we're not using a charging grip. + */ +static const struct joycon_ctlr_button_mapping left_joycon_s_button_mappings[] = { + { BTN_TR, JC_BTN_SL_L, }, + { BTN_TR2, JC_BTN_SR_L, }, + { /* sentinel */ }, +}; + +static const struct joycon_ctlr_button_mapping right_joycon_button_mappings[] = { + { BTN_EAST, JC_BTN_A, }, + { BTN_SOUTH, JC_BTN_B, }, + { BTN_NORTH, JC_BTN_X, }, + { BTN_WEST, JC_BTN_Y, }, + { BTN_TR, JC_BTN_R, }, + { BTN_TR2, JC_BTN_ZR, }, + { BTN_START, JC_BTN_PLUS, }, + { BTN_THUMBR, JC_BTN_RSTICK, }, + { BTN_MODE, JC_BTN_HOME, }, + { /* sentinel */ }, +}; + +/* + * The unused *left*-side triggers become the SL/SR triggers for the *right* + * Joy-Con, if and only if we're not using a charging grip. + */ +static const struct joycon_ctlr_button_mapping right_joycon_s_button_mappings[] = { + { BTN_TL, JC_BTN_SL_R, }, + { BTN_TL2, JC_BTN_SR_R, }, + { /* sentinel */ }, +}; + +static const struct joycon_ctlr_button_mapping procon_button_mappings[] = { + { BTN_EAST, JC_BTN_A, }, + { BTN_SOUTH, JC_BTN_B, }, + { BTN_NORTH, JC_BTN_X, }, + { BTN_WEST, JC_BTN_Y, }, + { BTN_TL, JC_BTN_L, }, + { BTN_TR, JC_BTN_R, }, + { BTN_TL2, JC_BTN_ZL, }, + { BTN_TR2, JC_BTN_ZR, }, + { BTN_SELECT, JC_BTN_MINUS, }, + { BTN_START, JC_BTN_PLUS, }, + { BTN_THUMBL, JC_BTN_LSTICK, }, + { BTN_THUMBR, JC_BTN_RSTICK, }, + { BTN_MODE, JC_BTN_HOME, }, + { BTN_Z, JC_BTN_CAP, }, + { /* sentinel */ }, +}; + +static const struct joycon_ctlr_button_mapping nescon_button_mappings[] = { + { BTN_SOUTH, JC_BTN_A, }, + { BTN_EAST, JC_BTN_B, }, + { BTN_TL, JC_BTN_L, }, + { BTN_TR, JC_BTN_R, }, + { BTN_SELECT, JC_BTN_MINUS, }, + { BTN_START, JC_BTN_PLUS, }, + { /* sentinel */ }, +}; + +static const struct joycon_ctlr_button_mapping snescon_button_mappings[] = { + { BTN_EAST, JC_BTN_A, }, + { BTN_SOUTH, JC_BTN_B, }, + { BTN_NORTH, JC_BTN_X, }, + { BTN_WEST, JC_BTN_Y, }, + { BTN_TL, JC_BTN_L, }, + { BTN_TR, JC_BTN_R, }, + { BTN_TL2, JC_BTN_ZL, }, + { BTN_TR2, JC_BTN_ZR, }, + { BTN_SELECT, JC_BTN_MINUS, }, + { BTN_START, JC_BTN_PLUS, }, + { /* sentinel */ }, +}; + +static const struct joycon_ctlr_button_mapping gencon_button_mappings[] = { + { BTN_WEST, JC_BTN_A, }, /* A */ + { BTN_SOUTH, JC_BTN_B, }, /* B */ + { BTN_EAST, JC_BTN_R, }, /* C */ + { BTN_TL, JC_BTN_X, }, /* X MD/GEN 6B Only */ + { BTN_NORTH, JC_BTN_Y, }, /* Y MD/GEN 6B Only */ + { BTN_TR, JC_BTN_L, }, /* Z MD/GEN 6B Only */ + { BTN_SELECT, JC_BTN_ZR, }, /* Mode */ + { BTN_START, JC_BTN_PLUS, }, + { BTN_MODE, JC_BTN_HOME, }, + { BTN_Z, JC_BTN_CAP, }, + { /* sentinel */ }, +}; + +static const struct joycon_ctlr_button_mapping n64con_button_mappings[] = { + { BTN_A, JC_BTN_A, }, + { BTN_B, JC_BTN_B, }, + { BTN_TL2, JC_BTN_ZL, }, /* Z */ + { BTN_TL, JC_BTN_L, }, + { BTN_TR, JC_BTN_R, }, + { BTN_TR2, JC_BTN_LSTICK, }, /* ZR */ + { BTN_START, JC_BTN_PLUS, }, + { BTN_SELECT, JC_BTN_Y, }, /* C UP */ + { BTN_X, JC_BTN_ZR, }, /* C DOWN */ + { BTN_Y, JC_BTN_X, }, /* C LEFT */ + { BTN_C, JC_BTN_MINUS, }, /* C RIGHT */ + { BTN_MODE, JC_BTN_HOME, }, + { BTN_Z, JC_BTN_CAP, }, + { /* sentinel */ }, +}; enum joycon_msg_type { JOYCON_MSG_TYPE_NONE, @@ -410,11 +550,24 @@ static const char * const joycon_player_led_names[] = { LED_FUNCTION_PLAYER4, }; #define JC_NUM_LEDS ARRAY_SIZE(joycon_player_led_names) +#define JC_NUM_LED_PATTERNS 8 +/* Taken from https://www.nintendo.com/my/support/qa/detail/33822 */ +static const enum led_brightness joycon_player_led_patterns[JC_NUM_LED_PATTERNS][JC_NUM_LEDS] = { + { 1, 0, 0, 0 }, + { 1, 1, 0, 0 }, + { 1, 1, 1, 0 }, + { 1, 1, 1, 1 }, + { 1, 0, 0, 1 }, + { 1, 0, 1, 0 }, + { 1, 0, 1, 1 }, + { 0, 1, 1, 0 }, +}; /* Each physical controller is associated with a joycon_ctlr struct */ struct joycon_ctlr { struct hid_device *hdev; struct input_dev *input; + u32 player_id; struct led_classdev leds[JC_NUM_LEDS]; /* player leds */ struct led_classdev home_led; enum joycon_ctlr_state ctlr_state; @@ -433,7 +586,9 @@ struct joycon_ctlr { u8 usb_ack_match; u8 subcmd_ack_match; bool received_input_report; + unsigned int last_input_report_msecs; unsigned int last_subcmd_sent_msecs; + unsigned int consecutive_valid_report_deltas; /* factory calibration data */ struct joycon_stick_cal left_stick_cal_x; @@ -492,13 +647,130 @@ struct joycon_ctlr { /* Does this controller have inputs associated with left joycon? */ #define jc_type_has_left(ctlr) \ (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \ - ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO) + ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO || \ + ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64) /* Does this controller have inputs associated with right joycon? */ #define jc_type_has_right(ctlr) \ (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \ ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO) +/* + * Controller device helpers + * + * These look at the device ID known to the HID subsystem to identify a device, + * but take caution: some NSO devices lie about themselves (NES Joy-Cons and + * Sega Genesis controller). See type helpers below. + * + * These helpers are most useful early during the HID probe or in conjunction + * with the capability helpers below. + */ +static inline bool joycon_device_is_chrggrip(struct joycon_ctlr *ctlr) +{ + return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP; +} + +/* + * Controller type helpers + * + * These are slightly different than the device-ID-based helpers above. They are + * generally more reliable, since they can distinguish between, e.g., Genesis + * versus SNES, or NES Joy-Cons versus regular Switch Joy-Cons. They're most + * useful for reporting available inputs. For other kinds of distinctions, see + * the capability helpers below. + * + * They have two major drawbacks: (1) they're not available until after we set + * the reporting method and then request the device info; (2) they can't + * distinguish all controllers (like the Charging Grip from the Pro controller.) + */ +static inline bool joycon_type_is_left_joycon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL; +} + +static inline bool joycon_type_is_right_joycon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR; +} + +static inline bool joycon_type_is_procon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO; +} + +static inline bool joycon_type_is_snescon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_SNES; +} + +static inline bool joycon_type_is_gencon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_GEN; +} + +static inline bool joycon_type_is_n64con(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64; +} + +static inline bool joycon_type_is_left_nescon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESL; +} + +static inline bool joycon_type_is_right_nescon(struct joycon_ctlr *ctlr) +{ + return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESR; +} + +static inline bool joycon_type_is_any_joycon(struct joycon_ctlr *ctlr) +{ + return joycon_type_is_left_joycon(ctlr) || + joycon_type_is_right_joycon(ctlr) || + joycon_device_is_chrggrip(ctlr); +} + +static inline bool joycon_type_is_any_nescon(struct joycon_ctlr *ctlr) +{ + return joycon_type_is_left_nescon(ctlr) || + joycon_type_is_right_nescon(ctlr); +} + +/* + * Controller capability helpers + * + * These helpers combine the use of the helpers above to detect certain + * capabilities during initialization. They are always accurate but (since they + * use type helpers) cannot be used early in the HID probe. + */ +static inline bool joycon_has_imu(struct joycon_ctlr *ctlr) +{ + return joycon_device_is_chrggrip(ctlr) || + joycon_type_is_any_joycon(ctlr) || + joycon_type_is_procon(ctlr); +} + +static inline bool joycon_has_joysticks(struct joycon_ctlr *ctlr) +{ + return joycon_device_is_chrggrip(ctlr) || + joycon_type_is_any_joycon(ctlr) || + joycon_type_is_procon(ctlr) || + joycon_type_is_n64con(ctlr); +} + +static inline bool joycon_has_rumble(struct joycon_ctlr *ctlr) +{ + return joycon_device_is_chrggrip(ctlr) || + joycon_type_is_any_joycon(ctlr) || + joycon_type_is_procon(ctlr) || + joycon_type_is_n64con(ctlr); +} + +static inline bool joycon_using_usb(struct joycon_ctlr *ctlr) +{ + return ctlr->hdev->bus == BUS_USB; +} + static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len) { u8 *buf; @@ -543,19 +815,54 @@ static void joycon_wait_for_input_report(struct joycon_ctlr *ctlr) * Sending subcommands and/or rumble data at too high a rate can cause bluetooth * controller disconnections. */ +#define JC_INPUT_REPORT_MIN_DELTA 8 +#define JC_INPUT_REPORT_MAX_DELTA 17 +#define JC_SUBCMD_TX_OFFSET_MS 4 +#define JC_SUBCMD_VALID_DELTA_REQ 3 +#define JC_SUBCMD_RATE_MAX_ATTEMPTS 25 +#define JC_SUBCMD_RATE_LIMITER_USB_MS 20 +#define JC_SUBCMD_RATE_LIMITER_BT_MS 60 +#define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_MS : JC_SUBCMD_RATE_LIMITER_BT_MS) static void joycon_enforce_subcmd_rate(struct joycon_ctlr *ctlr) { - static const unsigned int max_subcmd_rate_ms = 25; - unsigned int current_ms = jiffies_to_msecs(jiffies); - unsigned int delta_ms = current_ms - ctlr->last_subcmd_sent_msecs; + unsigned int current_ms; + unsigned long subcmd_delta; + int consecutive_valid_deltas = 0; + int attempts = 0; + unsigned long flags; - while (delta_ms < max_subcmd_rate_ms && - ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { + if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ)) + return; + + do { joycon_wait_for_input_report(ctlr); current_ms = jiffies_to_msecs(jiffies); - delta_ms = current_ms - ctlr->last_subcmd_sent_msecs; + subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs; + + spin_lock_irqsave(&ctlr->lock, flags); + consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas; + spin_unlock_irqrestore(&ctlr->lock, flags); + + attempts++; + } while ((consecutive_valid_deltas < JC_SUBCMD_VALID_DELTA_REQ || + subcmd_delta < JC_SUBCMD_RATE_LIMITER_MS(ctlr)) && + ctlr->ctlr_state == JOYCON_CTLR_STATE_READ && + attempts < JC_SUBCMD_RATE_MAX_ATTEMPTS); + + if (attempts >= JC_SUBCMD_RATE_MAX_ATTEMPTS) { + hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__); + return; } + ctlr->last_subcmd_sent_msecs = current_ms; + + /* + * Wait a short time after receiving an input report before + * transmitting. This should reduce odds of a TX coinciding with an RX. + * Minimizing concurrent BT traffic with the controller seems to lower + * the rate of disconnections. + */ + msleep(JC_SUBCMD_TX_OFFSET_MS); } static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len, @@ -662,6 +969,25 @@ static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on) return joycon_send_subcmd(ctlr, req, 1, HZ/4); } +static int joycon_set_home_led(struct joycon_ctlr *ctlr, enum led_brightness brightness) +{ + struct joycon_subcmd_request *req; + u8 buffer[sizeof(*req) + 5] = { 0 }; + u8 *data; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; + data = req->data; + data[0] = 0x01; + data[1] = brightness << 4; + data[2] = brightness | (brightness << 4); + data[3] = 0x11; + data[4] = 0x11; + + hid_dbg(ctlr->hdev, "setting home led brightness\n"); + return joycon_send_subcmd(ctlr, req, 5, HZ/4); +} + static int joycon_request_spi_flash_read(struct joycon_ctlr *ctlr, u32 start_addr, u8 size, u8 **reply) { @@ -859,14 +1185,27 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) */ static void joycon_calc_imu_cal_divisors(struct joycon_ctlr *ctlr) { - int i; + int i, divz = 0; for (i = 0; i < 3; i++) { ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - ctlr->accel_cal.offset[i]; ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - ctlr->gyro_cal.offset[i]; + + if (ctlr->imu_cal_accel_divisor[i] == 0) { + ctlr->imu_cal_accel_divisor[i] = 1; + divz++; + } + + if (ctlr->imu_cal_gyro_divisor[i] == 0) { + ctlr->imu_cal_gyro_divisor[i] = 1; + divz++; + } } + + if (divz) + hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz); } static const s16 DFLT_ACCEL_OFFSET /*= 0*/; @@ -1095,16 +1434,16 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr, JC_IMU_SAMPLES_PER_DELTA_AVG) { ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / ctlr->imu_delta_samples_count; - /* don't ever want divide by zero shenanigans */ - if (ctlr->imu_avg_delta_ms == 0) { - ctlr->imu_avg_delta_ms = 1; - hid_warn(ctlr->hdev, - "calculated avg imu delta of 0\n"); - } ctlr->imu_delta_samples_count = 0; ctlr->imu_delta_samples_sum = 0; } + /* don't ever want divide by zero shenanigans */ + if (ctlr->imu_avg_delta_ms == 0) { + ctlr->imu_avg_delta_ms = 1; + hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n"); + } + /* useful for debugging IMU sample rate */ hid_dbg(ctlr->hdev, "imu_report: ms=%u last_ms=%u delta=%u avg_delta=%u\n", @@ -1116,10 +1455,10 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr, ctlr->imu_avg_delta_ms; ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; if (dropped_pkts > JC_IMU_DROPPED_PKT_WARNING) { - hid_warn(ctlr->hdev, + hid_warn_ratelimited(ctlr->hdev, "compensating for %u dropped IMU reports\n", dropped_pkts); - hid_warn(ctlr->hdev, + hid_warn_ratelimited(ctlr->hdev, "delta=%u avg_delta=%u\n", delta, ctlr->imu_avg_delta_ms); } @@ -1215,13 +1554,9 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr, } } -static void joycon_parse_report(struct joycon_ctlr *ctlr, - struct joycon_input_report *rep) +static void joycon_handle_rumble_report(struct joycon_ctlr *ctlr, struct joycon_input_report *rep) { - struct input_dev *dev = ctlr->input; unsigned long flags; - u8 tmp; - u32 btns; unsigned long msecs = jiffies_to_msecs(jiffies); spin_lock_irqsave(&ctlr->lock, flags); @@ -1241,11 +1576,21 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); } - /* Parse the battery status */ + spin_unlock_irqrestore(&ctlr->lock, flags); +} + +static void joycon_parse_battery_status(struct joycon_ctlr *ctlr, struct joycon_input_report *rep) +{ + u8 tmp; + unsigned long flags; + + spin_lock_irqsave(&ctlr->lock, flags); + tmp = rep->bat_con; ctlr->host_powered = tmp & BIT(0); ctlr->battery_charging = tmp & BIT(4); tmp = tmp >> 5; + switch (tmp) { case 0: /* empty */ ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; @@ -1267,102 +1612,146 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, hid_warn(ctlr->hdev, "Invalid battery status\n"); break; } + spin_unlock_irqrestore(&ctlr->lock, flags); +} - /* Parse the buttons and sticks */ - btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); - - if (jc_type_has_left(ctlr)) { - u16 raw_x; - u16 raw_y; - s32 x; - s32 y; - - /* get raw stick values */ - raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); - raw_y = hid_field_extract(ctlr->hdev, - rep->left_stick + 1, 4, 12); - /* map the stick values */ - x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); - y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); - /* report sticks */ - input_report_abs(dev, ABS_X, x); - input_report_abs(dev, ABS_Y, y); - - /* report buttons */ - input_report_key(dev, BTN_TL, btns & JC_BTN_L); - input_report_key(dev, BTN_TL2, btns & JC_BTN_ZL); - input_report_key(dev, BTN_SELECT, btns & JC_BTN_MINUS); - input_report_key(dev, BTN_THUMBL, btns & JC_BTN_LSTICK); - input_report_key(dev, BTN_Z, btns & JC_BTN_CAP); - - if (jc_type_is_joycon(ctlr)) { - /* Report the S buttons as the non-existent triggers */ - input_report_key(dev, BTN_TR, btns & JC_BTN_SL_L); - input_report_key(dev, BTN_TR2, btns & JC_BTN_SR_L); - - /* Report d-pad as digital buttons for the joy-cons */ - input_report_key(dev, BTN_DPAD_DOWN, - btns & JC_BTN_DOWN); - input_report_key(dev, BTN_DPAD_UP, btns & JC_BTN_UP); - input_report_key(dev, BTN_DPAD_RIGHT, - btns & JC_BTN_RIGHT); - input_report_key(dev, BTN_DPAD_LEFT, - btns & JC_BTN_LEFT); - } else { - int hatx = 0; - int haty = 0; - - /* d-pad x */ - if (btns & JC_BTN_LEFT) - hatx = -1; - else if (btns & JC_BTN_RIGHT) - hatx = 1; - input_report_abs(dev, ABS_HAT0X, hatx); - - /* d-pad y */ - if (btns & JC_BTN_UP) - haty = -1; - else if (btns & JC_BTN_DOWN) - haty = 1; - input_report_abs(dev, ABS_HAT0Y, haty); - } +static void joycon_report_left_stick(struct joycon_ctlr *ctlr, + struct joycon_input_report *rep) +{ + u16 raw_x; + u16 raw_y; + s32 x; + s32 y; + + raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); + raw_y = hid_field_extract(ctlr->hdev, rep->left_stick + 1, 4, 12); + + x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); + y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); + + input_report_abs(ctlr->input, ABS_X, x); + input_report_abs(ctlr->input, ABS_Y, y); +} + +static void joycon_report_right_stick(struct joycon_ctlr *ctlr, + struct joycon_input_report *rep) +{ + u16 raw_x; + u16 raw_y; + s32 x; + s32 y; + + raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); + raw_y = hid_field_extract(ctlr->hdev, rep->right_stick + 1, 4, 12); + + x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); + y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); + + input_report_abs(ctlr->input, ABS_RX, x); + input_report_abs(ctlr->input, ABS_RY, y); +} + +static void joycon_report_dpad(struct joycon_ctlr *ctlr, + struct joycon_input_report *rep) +{ + int hatx = 0; + int haty = 0; + u32 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); + + if (btns & JC_BTN_LEFT) + hatx = -1; + else if (btns & JC_BTN_RIGHT) + hatx = 1; + + if (btns & JC_BTN_UP) + haty = -1; + else if (btns & JC_BTN_DOWN) + haty = 1; + + input_report_abs(ctlr->input, ABS_HAT0X, hatx); + input_report_abs(ctlr->input, ABS_HAT0Y, haty); +} + +static void joycon_report_buttons(struct joycon_ctlr *ctlr, + struct joycon_input_report *rep, + const struct joycon_ctlr_button_mapping button_mappings[]) +{ + const struct joycon_ctlr_button_mapping *button; + u32 status = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); + + for (button = button_mappings; button->code; button++) + input_report_key(ctlr->input, button->code, status & button->bit); +} + +static void joycon_parse_report(struct joycon_ctlr *ctlr, + struct joycon_input_report *rep) +{ + unsigned long flags; + unsigned long msecs = jiffies_to_msecs(jiffies); + unsigned long report_delta_ms = msecs - ctlr->last_input_report_msecs; + + if (joycon_has_rumble(ctlr)) + joycon_handle_rumble_report(ctlr, rep); + + joycon_parse_battery_status(ctlr, rep); + + if (joycon_type_is_left_joycon(ctlr)) { + joycon_report_left_stick(ctlr, rep); + joycon_report_buttons(ctlr, rep, left_joycon_button_mappings); + if (!joycon_device_is_chrggrip(ctlr)) + joycon_report_buttons(ctlr, rep, left_joycon_s_button_mappings); + } else if (joycon_type_is_right_joycon(ctlr)) { + joycon_report_right_stick(ctlr, rep); + joycon_report_buttons(ctlr, rep, right_joycon_button_mappings); + if (!joycon_device_is_chrggrip(ctlr)) + joycon_report_buttons(ctlr, rep, right_joycon_s_button_mappings); + } else if (joycon_type_is_procon(ctlr)) { + joycon_report_left_stick(ctlr, rep); + joycon_report_right_stick(ctlr, rep); + joycon_report_dpad(ctlr, rep); + joycon_report_buttons(ctlr, rep, procon_button_mappings); + } else if (joycon_type_is_any_nescon(ctlr)) { + joycon_report_dpad(ctlr, rep); + joycon_report_buttons(ctlr, rep, nescon_button_mappings); + } else if (joycon_type_is_snescon(ctlr)) { + joycon_report_dpad(ctlr, rep); + joycon_report_buttons(ctlr, rep, snescon_button_mappings); + } else if (joycon_type_is_gencon(ctlr)) { + joycon_report_dpad(ctlr, rep); + joycon_report_buttons(ctlr, rep, gencon_button_mappings); + } else if (joycon_type_is_n64con(ctlr)) { + joycon_report_left_stick(ctlr, rep); + joycon_report_dpad(ctlr, rep); + joycon_report_buttons(ctlr, rep, n64con_button_mappings); } - if (jc_type_has_right(ctlr)) { - u16 raw_x; - u16 raw_y; - s32 x; - s32 y; - - /* get raw stick values */ - raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); - raw_y = hid_field_extract(ctlr->hdev, - rep->right_stick + 1, 4, 12); - /* map stick values */ - x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); - y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); - /* report sticks */ - input_report_abs(dev, ABS_RX, x); - input_report_abs(dev, ABS_RY, y); - - /* report buttons */ - input_report_key(dev, BTN_TR, btns & JC_BTN_R); - input_report_key(dev, BTN_TR2, btns & JC_BTN_ZR); - if (jc_type_is_joycon(ctlr)) { - /* Report the S buttons as the non-existent triggers */ - input_report_key(dev, BTN_TL, btns & JC_BTN_SL_R); - input_report_key(dev, BTN_TL2, btns & JC_BTN_SR_R); - } - input_report_key(dev, BTN_START, btns & JC_BTN_PLUS); - input_report_key(dev, BTN_THUMBR, btns & JC_BTN_RSTICK); - input_report_key(dev, BTN_MODE, btns & JC_BTN_HOME); - input_report_key(dev, BTN_WEST, btns & JC_BTN_Y); - input_report_key(dev, BTN_NORTH, btns & JC_BTN_X); - input_report_key(dev, BTN_EAST, btns & JC_BTN_A); - input_report_key(dev, BTN_SOUTH, btns & JC_BTN_B); + + input_sync(ctlr->input); + + spin_lock_irqsave(&ctlr->lock, flags); + ctlr->last_input_report_msecs = msecs; + /* + * Was this input report a reasonable time delta compared to the prior + * report? We use this information to decide when a safe time is to send + * rumble packets or subcommand packets. + */ + if (report_delta_ms >= JC_INPUT_REPORT_MIN_DELTA && + report_delta_ms <= JC_INPUT_REPORT_MAX_DELTA) { + if (ctlr->consecutive_valid_report_deltas < JC_SUBCMD_VALID_DELTA_REQ) + ctlr->consecutive_valid_report_deltas++; + } else { + ctlr->consecutive_valid_report_deltas = 0; } + /* + * Our consecutive valid report tracking is only relevant for + * bluetooth-connected controllers. For USB devices, we're beholden to + * USB's underlying polling rate anyway. Always set to the consecutive + * delta requirement. + */ + if (ctlr->hdev->bus == BUS_USB) + ctlr->consecutive_valid_report_deltas = JC_SUBCMD_VALID_DELTA_REQ; - input_sync(dev); + spin_unlock_irqrestore(&ctlr->lock, flags); /* * Immediately after receiving a report is the most reliable time to @@ -1377,7 +1766,7 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, } /* parse IMU data if present */ - if (rep->id == JC_INPUT_IMU_DATA) + if ((rep->id == JC_INPUT_IMU_DATA) && joycon_has_imu(ctlr)) joycon_parse_imu_report(ctlr, rep); } @@ -1527,6 +1916,7 @@ static int joycon_set_rumble(struct joycon_ctlr *ctlr, u16 amp_r, u16 amp_l, u16 freq_l_low; u16 freq_l_high; unsigned long flags; + int next_rq_head; spin_lock_irqsave(&ctlr->lock, flags); freq_r_low = ctlr->rumble_rl_freq; @@ -1547,8 +1937,21 @@ static int joycon_set_rumble(struct joycon_ctlr *ctlr, u16 amp_r, u16 amp_l, joycon_encode_rumble(data, freq_l_low, freq_l_high, amp); spin_lock_irqsave(&ctlr->lock, flags); - if (++ctlr->rumble_queue_head >= JC_RUMBLE_QUEUE_SIZE) - ctlr->rumble_queue_head = 0; + + next_rq_head = ctlr->rumble_queue_head + 1; + if (next_rq_head >= JC_RUMBLE_QUEUE_SIZE) + next_rq_head = 0; + + /* Did we overrun the circular buffer? + * If so, be sure we keep the latest intended rumble state. + */ + if (next_rq_head == ctlr->rumble_queue_tail) { + hid_dbg(ctlr->hdev, "rumble queue is full"); + /* overwrite the prior value at the end of the circular buf */ + next_rq_head = ctlr->rumble_queue_head; + } + + ctlr->rumble_queue_head = next_rq_head; memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, JC_RUMBLE_DATA_SIZE); @@ -1576,123 +1979,65 @@ static int joycon_play_effect(struct input_dev *dev, void *data, } #endif /* IS_ENABLED(CONFIG_NINTENDO_FF) */ -static const unsigned int joycon_button_inputs_l[] = { - BTN_SELECT, BTN_Z, BTN_THUMBL, - BTN_TL, BTN_TL2, - 0 /* 0 signals end of array */ -}; - -static const unsigned int joycon_button_inputs_r[] = { - BTN_START, BTN_MODE, BTN_THUMBR, - BTN_SOUTH, BTN_EAST, BTN_NORTH, BTN_WEST, - BTN_TR, BTN_TR2, - 0 /* 0 signals end of array */ -}; - -/* We report joy-con d-pad inputs as buttons and pro controller as a hat. */ -static const unsigned int joycon_dpad_inputs_jc[] = { - BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT, - 0 /* 0 signals end of array */ -}; - -static int joycon_input_create(struct joycon_ctlr *ctlr) +static void joycon_config_left_stick(struct input_dev *idev) { - struct hid_device *hdev; - const char *name; - const char *imu_name; - int ret; - int i; - - hdev = ctlr->hdev; + input_set_abs_params(idev, + ABS_X, + -JC_MAX_STICK_MAG, + JC_MAX_STICK_MAG, + JC_STICK_FUZZ, + JC_STICK_FLAT); + input_set_abs_params(idev, + ABS_Y, + -JC_MAX_STICK_MAG, + JC_MAX_STICK_MAG, + JC_STICK_FUZZ, + JC_STICK_FLAT); +} - switch (hdev->product) { - case USB_DEVICE_ID_NINTENDO_PROCON: - name = "Nintendo Switch Pro Controller"; - imu_name = "Nintendo Switch Pro Controller IMU"; - break; - case USB_DEVICE_ID_NINTENDO_CHRGGRIP: - if (jc_type_has_left(ctlr)) { - name = "Nintendo Switch Left Joy-Con (Grip)"; - imu_name = "Nintendo Switch Left Joy-Con IMU (Grip)"; - } else { - name = "Nintendo Switch Right Joy-Con (Grip)"; - imu_name = "Nintendo Switch Right Joy-Con IMU (Grip)"; - } - break; - case USB_DEVICE_ID_NINTENDO_JOYCONL: - name = "Nintendo Switch Left Joy-Con"; - imu_name = "Nintendo Switch Left Joy-Con IMU"; - break; - case USB_DEVICE_ID_NINTENDO_JOYCONR: - name = "Nintendo Switch Right Joy-Con"; - imu_name = "Nintendo Switch Right Joy-Con IMU"; - break; - default: /* Should be impossible */ - hid_err(hdev, "Invalid hid product\n"); - return -EINVAL; - } +static void joycon_config_right_stick(struct input_dev *idev) +{ + input_set_abs_params(idev, + ABS_RX, + -JC_MAX_STICK_MAG, + JC_MAX_STICK_MAG, + JC_STICK_FUZZ, + JC_STICK_FLAT); + input_set_abs_params(idev, + ABS_RY, + -JC_MAX_STICK_MAG, + JC_MAX_STICK_MAG, + JC_STICK_FUZZ, + JC_STICK_FLAT); +} - ctlr->input = devm_input_allocate_device(&hdev->dev); - if (!ctlr->input) - return -ENOMEM; - ctlr->input->id.bustype = hdev->bus; - ctlr->input->id.vendor = hdev->vendor; - ctlr->input->id.product = hdev->product; - ctlr->input->id.version = hdev->version; - ctlr->input->uniq = ctlr->mac_addr_str; - ctlr->input->name = name; - ctlr->input->phys = hdev->phys; - input_set_drvdata(ctlr->input, ctlr); +static void joycon_config_dpad(struct input_dev *idev) +{ + input_set_abs_params(idev, + ABS_HAT0X, + -JC_MAX_DPAD_MAG, + JC_MAX_DPAD_MAG, + JC_DPAD_FUZZ, + JC_DPAD_FLAT); + input_set_abs_params(idev, + ABS_HAT0Y, + -JC_MAX_DPAD_MAG, + JC_MAX_DPAD_MAG, + JC_DPAD_FUZZ, + JC_DPAD_FLAT); +} - /* set up sticks and buttons */ - if (jc_type_has_left(ctlr)) { - input_set_abs_params(ctlr->input, ABS_X, - -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, - JC_STICK_FUZZ, JC_STICK_FLAT); - input_set_abs_params(ctlr->input, ABS_Y, - -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, - JC_STICK_FUZZ, JC_STICK_FLAT); - - for (i = 0; joycon_button_inputs_l[i] > 0; i++) - input_set_capability(ctlr->input, EV_KEY, - joycon_button_inputs_l[i]); - - /* configure d-pad differently for joy-con vs pro controller */ - if (hdev->product != USB_DEVICE_ID_NINTENDO_PROCON) { - for (i = 0; joycon_dpad_inputs_jc[i] > 0; i++) - input_set_capability(ctlr->input, EV_KEY, - joycon_dpad_inputs_jc[i]); - } else { - input_set_abs_params(ctlr->input, ABS_HAT0X, - -JC_MAX_DPAD_MAG, JC_MAX_DPAD_MAG, - JC_DPAD_FUZZ, JC_DPAD_FLAT); - input_set_abs_params(ctlr->input, ABS_HAT0Y, - -JC_MAX_DPAD_MAG, JC_MAX_DPAD_MAG, - JC_DPAD_FUZZ, JC_DPAD_FLAT); - } - } - if (jc_type_has_right(ctlr)) { - input_set_abs_params(ctlr->input, ABS_RX, - -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, - JC_STICK_FUZZ, JC_STICK_FLAT); - input_set_abs_params(ctlr->input, ABS_RY, - -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, - JC_STICK_FUZZ, JC_STICK_FLAT); - - for (i = 0; joycon_button_inputs_r[i] > 0; i++) - input_set_capability(ctlr->input, EV_KEY, - joycon_button_inputs_r[i]); - } +static void joycon_config_buttons(struct input_dev *idev, + const struct joycon_ctlr_button_mapping button_mappings[]) +{ + const struct joycon_ctlr_button_mapping *button; - /* Let's report joy-con S triggers separately */ - if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL) { - input_set_capability(ctlr->input, EV_KEY, BTN_TR); - input_set_capability(ctlr->input, EV_KEY, BTN_TR2); - } else if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR) { - input_set_capability(ctlr->input, EV_KEY, BTN_TL); - input_set_capability(ctlr->input, EV_KEY, BTN_TL2); - } + for (button = button_mappings; button->code; button++) + input_set_capability(idev, EV_KEY, button->code); +} +static void joycon_config_rumble(struct joycon_ctlr *ctlr) +{ #if IS_ENABLED(CONFIG_NINTENDO_FF) /* set up rumble */ input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); @@ -1705,10 +2050,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) joycon_set_rumble(ctlr, 0, 0, false); ctlr->rumble_msecs = jiffies_to_msecs(jiffies); #endif +} - ret = input_register_device(ctlr->input); - if (ret) - return ret; +static int joycon_imu_input_create(struct joycon_ctlr *ctlr) +{ + struct hid_device *hdev; + const char *imu_name; + int ret; + + hdev = ctlr->hdev; /* configure the imu input device */ ctlr->imu_input = devm_input_allocate_device(&hdev->dev); @@ -1720,8 +2070,14 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) ctlr->imu_input->id.product = hdev->product; ctlr->imu_input->id.version = hdev->version; ctlr->imu_input->uniq = ctlr->mac_addr_str; - ctlr->imu_input->name = imu_name; ctlr->imu_input->phys = hdev->phys; + + imu_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s (IMU)", ctlr->input->name); + if (!imu_name) + return -ENOMEM; + + ctlr->imu_input->name = imu_name; + input_set_drvdata(ctlr->imu_input, ctlr); /* configure imu axes */ @@ -1763,6 +2119,72 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) return 0; } +static int joycon_input_create(struct joycon_ctlr *ctlr) +{ + struct hid_device *hdev; + int ret; + + hdev = ctlr->hdev; + + ctlr->input = devm_input_allocate_device(&hdev->dev); + if (!ctlr->input) + return -ENOMEM; + ctlr->input->id.bustype = hdev->bus; + ctlr->input->id.vendor = hdev->vendor; + ctlr->input->id.product = hdev->product; + ctlr->input->id.version = hdev->version; + ctlr->input->uniq = ctlr->mac_addr_str; + ctlr->input->name = hdev->name; + ctlr->input->phys = hdev->phys; + input_set_drvdata(ctlr->input, ctlr); + + ret = input_register_device(ctlr->input); + if (ret) + return ret; + + if (joycon_type_is_right_joycon(ctlr)) { + joycon_config_right_stick(ctlr->input); + joycon_config_buttons(ctlr->input, right_joycon_button_mappings); + if (!joycon_device_is_chrggrip(ctlr)) + joycon_config_buttons(ctlr->input, right_joycon_s_button_mappings); + } else if (joycon_type_is_left_joycon(ctlr)) { + joycon_config_left_stick(ctlr->input); + joycon_config_buttons(ctlr->input, left_joycon_button_mappings); + if (!joycon_device_is_chrggrip(ctlr)) + joycon_config_buttons(ctlr->input, left_joycon_s_button_mappings); + } else if (joycon_type_is_procon(ctlr)) { + joycon_config_left_stick(ctlr->input); + joycon_config_right_stick(ctlr->input); + joycon_config_dpad(ctlr->input); + joycon_config_buttons(ctlr->input, procon_button_mappings); + } else if (joycon_type_is_any_nescon(ctlr)) { + joycon_config_dpad(ctlr->input); + joycon_config_buttons(ctlr->input, nescon_button_mappings); + } else if (joycon_type_is_snescon(ctlr)) { + joycon_config_dpad(ctlr->input); + joycon_config_buttons(ctlr->input, snescon_button_mappings); + } else if (joycon_type_is_gencon(ctlr)) { + joycon_config_dpad(ctlr->input); + joycon_config_buttons(ctlr->input, gencon_button_mappings); + } else if (joycon_type_is_n64con(ctlr)) { + joycon_config_dpad(ctlr->input); + joycon_config_left_stick(ctlr->input); + joycon_config_buttons(ctlr->input, n64con_button_mappings); + } + + if (joycon_has_imu(ctlr)) { + ret = joycon_imu_input_create(ctlr); + if (ret) + return ret; + } + + if (joycon_has_rumble(ctlr)) + joycon_config_rumble(ctlr); + + return 0; +} + +/* Because the subcommand sets all the leds at once, the brightness argument is ignored */ static int joycon_player_led_brightness_set(struct led_classdev *led, enum led_brightness brightness) { @@ -1772,7 +2194,6 @@ static int joycon_player_led_brightness_set(struct led_classdev *led, int val = 0; int i; int ret; - int num; ctlr = hid_get_drvdata(hdev); if (!ctlr) { @@ -1780,21 +2201,10 @@ static int joycon_player_led_brightness_set(struct led_classdev *led, return -ENODEV; } - /* determine which player led this is */ - for (num = 0; num < JC_NUM_LEDS; num++) { - if (&ctlr->leds[num] == led) - break; - } - if (num >= JC_NUM_LEDS) - return -EINVAL; + for (i = 0; i < JC_NUM_LEDS; i++) + val |= ctlr->leds[i].brightness << i; mutex_lock(&ctlr->output_mutex); - for (i = 0; i < JC_NUM_LEDS; i++) { - if (i == num) - val |= brightness << i; - else - val |= ctlr->leds[i].brightness << i; - } ret = joycon_set_player_leds(ctlr, 0, val); mutex_unlock(&ctlr->output_mutex); @@ -1807,9 +2217,6 @@ static int joycon_home_led_brightness_set(struct led_classdev *led, struct device *dev = led->dev->parent; struct hid_device *hdev = to_hid_device(dev); struct joycon_ctlr *ctlr; - struct joycon_subcmd_request *req; - u8 buffer[sizeof(*req) + 5] = { 0 }; - u8 *data; int ret; ctlr = hid_get_drvdata(hdev); @@ -1817,75 +2224,73 @@ static int joycon_home_led_brightness_set(struct led_classdev *led, hid_err(hdev, "No controller data\n"); return -ENODEV; } - - req = (struct joycon_subcmd_request *)buffer; - req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; - data = req->data; - data[0] = 0x01; - data[1] = brightness << 4; - data[2] = brightness | (brightness << 4); - data[3] = 0x11; - data[4] = 0x11; - - hid_dbg(hdev, "setting home led brightness\n"); mutex_lock(&ctlr->output_mutex); - ret = joycon_send_subcmd(ctlr, req, 5, HZ/4); + ret = joycon_set_home_led(ctlr, brightness); mutex_unlock(&ctlr->output_mutex); - return ret; } -static DEFINE_MUTEX(joycon_input_num_mutex); +static DEFINE_IDA(nintendo_player_id_allocator); + static int joycon_leds_create(struct joycon_ctlr *ctlr) { struct hid_device *hdev = ctlr->hdev; struct device *dev = &hdev->dev; const char *d_name = dev_name(dev); struct led_classdev *led; + int led_val = 0; char *name; - int ret = 0; + int ret; int i; - static int input_num = 1; - - /* Set the default controller player leds based on controller number */ - mutex_lock(&joycon_input_num_mutex); - mutex_lock(&ctlr->output_mutex); - ret = joycon_set_player_leds(ctlr, 0, 0xF >> (4 - input_num)); - if (ret) - hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret); - mutex_unlock(&ctlr->output_mutex); + int player_led_pattern; /* configure the player LEDs */ + ctlr->player_id = U32_MAX; + ret = ida_alloc(&nintendo_player_id_allocator, GFP_KERNEL); + if (ret < 0) { + hid_warn(hdev, "Failed to allocate player ID, skipping; ret=%d\n", ret); + goto home_led; + } + ctlr->player_id = ret; + player_led_pattern = ret % JC_NUM_LED_PATTERNS; + hid_info(ctlr->hdev, "assigned player %d led pattern", player_led_pattern + 1); + for (i = 0; i < JC_NUM_LEDS; i++) { name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s:%s", d_name, "green", joycon_player_led_names[i]); - if (!name) { - mutex_unlock(&joycon_input_num_mutex); + if (!name) return -ENOMEM; - } led = &ctlr->leds[i]; led->name = name; - led->brightness = ((i + 1) <= input_num) ? 1 : 0; + led->brightness = joycon_player_led_patterns[player_led_pattern][i]; led->max_brightness = 1; led->brightness_set_blocking = joycon_player_led_brightness_set; led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; + led_val |= joycon_player_led_patterns[player_led_pattern][i] << i; + } + mutex_lock(&ctlr->output_mutex); + ret = joycon_set_player_leds(ctlr, 0, led_val); + mutex_unlock(&ctlr->output_mutex); + if (ret) { + hid_warn(hdev, "Failed to set players LEDs, skipping registration; ret=%d\n", ret); + goto home_led; + } + + for (i = 0; i < JC_NUM_LEDS; i++) { + led = &ctlr->leds[i]; ret = devm_led_classdev_register(&hdev->dev, led); if (ret) { - hid_err(hdev, "Failed registering %s LED\n", led->name); - mutex_unlock(&joycon_input_num_mutex); + hid_err(hdev, "Failed to register player %d LED; ret=%d\n", i + 1, ret); return ret; } } - if (++input_num > 4) - input_num = 1; - mutex_unlock(&joycon_input_num_mutex); - +home_led: /* configure the home LED */ if (jc_type_has_right(ctlr)) { name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s:%s", @@ -1901,16 +2306,20 @@ static int joycon_leds_create(struct joycon_ctlr *ctlr) led->max_brightness = 0xF; led->brightness_set_blocking = joycon_home_led_brightness_set; led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; - ret = devm_led_classdev_register(&hdev->dev, led); + + /* Set the home LED to 0 as default state */ + mutex_lock(&ctlr->output_mutex); + ret = joycon_set_home_led(ctlr, 0); + mutex_unlock(&ctlr->output_mutex); if (ret) { - hid_err(hdev, "Failed registering home led\n"); - return ret; + hid_warn(hdev, "Failed to set home LED, skipping registration; ret=%d\n", ret); + return 0; } - /* Set the home LED to 0 as default state */ - ret = joycon_home_led_brightness_set(led, 0); + + ret = devm_led_classdev_register(&hdev->dev, led); if (ret) { - hid_warn(hdev, "Failed to set home LED default, unregistering home LED"); - devm_led_classdev_unregister(&hdev->dev, led); + hid_err(hdev, "Failed to register home LED; ret=%d\n", ret); + return ret; } } @@ -2011,7 +2420,7 @@ static int joycon_read_info(struct joycon_ctlr *ctlr) struct joycon_input_report *report; req.subcmd_id = JC_SUBCMD_REQ_DEV_INFO; - ret = joycon_send_subcmd(ctlr, &req, 0, HZ); + ret = joycon_send_subcmd(ctlr, &req, 0, 2 * HZ); if (ret) { hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); return ret; @@ -2034,12 +2443,116 @@ static int joycon_read_info(struct joycon_ctlr *ctlr) return -ENOMEM; hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); - /* Retrieve the type so we can distinguish for charging grip */ + /* + * Retrieve the type so we can distinguish the controller type + * Unfortantly the hdev->product can't always be used due to a ?bug? + * with the NSO Genesis controller. Over USB, it will report the + * PID as 0x201E, but over bluetooth it will report the PID as 0x2017 + * which is the same as the NSO SNES controller. This is different from + * the rest of the controllers which will report the same PID over USB + * and bluetooth. + */ ctlr->ctlr_type = report->subcmd_reply.data[2]; + hid_dbg(ctlr->hdev, "controller type = 0x%02X\n", ctlr->ctlr_type); return 0; } +static int joycon_init(struct hid_device *hdev) +{ + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + int ret = 0; + + mutex_lock(&ctlr->output_mutex); + /* if handshake command fails, assume ble pro controller */ + if (joycon_using_usb(ctlr) && !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ)) { + hid_dbg(hdev, "detected USB controller\n"); + /* set baudrate for improved latency */ + ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M, HZ); + if (ret) { + /* + * We can function with the default baudrate. + * Provide a warning, and continue on. + */ + hid_warn(hdev, "Failed to set baudrate (ret=%d), continuing anyway\n", ret); + } + /* handshake */ + ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ); + if (ret) { + hid_err(hdev, "Failed handshake; ret=%d\n", ret); + goto out_unlock; + } + /* + * Set no timeout (to keep controller in USB mode). + * This doesn't send a response, so ignore the timeout. + */ + joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT, HZ/10); + } else if (jc_type_is_chrggrip(ctlr)) { + hid_err(hdev, "Failed charging grip handshake\n"); + ret = -ETIMEDOUT; + goto out_unlock; + } + + /* needed to retrieve the controller type */ + ret = joycon_read_info(ctlr); + if (ret) { + hid_err(hdev, "Failed to retrieve controller info; ret=%d\n", + ret); + goto out_unlock; + } + + if (joycon_has_joysticks(ctlr)) { + /* get controller calibration data, and parse it */ + ret = joycon_request_calibration(ctlr); + if (ret) { + /* + * We can function with default calibration, but it may be + * inaccurate. Provide a warning, and continue on. + */ + hid_warn(hdev, "Analog stick positions may be inaccurate\n"); + } + } + + if (joycon_has_imu(ctlr)) { + /* get IMU calibration data, and parse it */ + ret = joycon_request_imu_calibration(ctlr); + if (ret) { + /* + * We can function with default calibration, but it may be + * inaccurate. Provide a warning, and continue on. + */ + hid_warn(hdev, "Unable to read IMU calibration data\n"); + } + + /* Enable the IMU */ + ret = joycon_enable_imu(ctlr); + if (ret) { + hid_err(hdev, "Failed to enable the IMU; ret=%d\n", ret); + goto out_unlock; + } + } + + /* Set the reporting mode to 0x30, which is the full report mode */ + ret = joycon_set_report_mode(ctlr); + if (ret) { + hid_err(hdev, "Failed to set report mode; ret=%d\n", ret); + goto out_unlock; + } + + if (joycon_has_rumble(ctlr)) { + /* Enable rumble */ + ret = joycon_enable_rumble(ctlr); + if (ret) { + hid_err(hdev, "Failed to enable rumble; ret=%d\n", ret); + goto out_unlock; + } + } + +out_unlock: + mutex_unlock(&ctlr->output_mutex); + return ret; +} + /* Common handler for parsing inputs */ static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data, int size) @@ -2128,14 +2641,15 @@ static int nintendo_hid_probe(struct hid_device *hdev, ctlr->hdev = hdev; ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; - ctlr->rumble_queue_head = JC_RUMBLE_QUEUE_SIZE - 1; + ctlr->rumble_queue_head = 0; ctlr->rumble_queue_tail = 0; hid_set_drvdata(hdev, ctlr); mutex_init(&ctlr->output_mutex); init_waitqueue_head(&ctlr->wait); spin_lock_init(&ctlr->lock); ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", - WQ_FREEZABLE | WQ_MEM_RECLAIM, 0); + WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU, + 0); if (!ctlr->rumble_queue) { ret = -ENOMEM; goto err; @@ -2171,85 +2685,12 @@ static int nintendo_hid_probe(struct hid_device *hdev, hid_device_io_start(hdev); - /* Initialize the controller */ - mutex_lock(&ctlr->output_mutex); - /* if handshake command fails, assume ble pro controller */ - if ((jc_type_is_procon(ctlr) || jc_type_is_chrggrip(ctlr)) && - !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ)) { - hid_dbg(hdev, "detected USB controller\n"); - /* set baudrate for improved latency */ - ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M, HZ); - if (ret) { - hid_err(hdev, "Failed to set baudrate; ret=%d\n", ret); - goto err_mutex; - } - /* handshake */ - ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ); - if (ret) { - hid_err(hdev, "Failed handshake; ret=%d\n", ret); - goto err_mutex; - } - /* - * Set no timeout (to keep controller in USB mode). - * This doesn't send a response, so ignore the timeout. - */ - joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT, HZ/10); - } else if (jc_type_is_chrggrip(ctlr)) { - hid_err(hdev, "Failed charging grip handshake\n"); - ret = -ETIMEDOUT; - goto err_mutex; - } - - /* get controller calibration data, and parse it */ - ret = joycon_request_calibration(ctlr); - if (ret) { - /* - * We can function with default calibration, but it may be - * inaccurate. Provide a warning, and continue on. - */ - hid_warn(hdev, "Analog stick positions may be inaccurate\n"); - } - - /* get IMU calibration data, and parse it */ - ret = joycon_request_imu_calibration(ctlr); - if (ret) { - /* - * We can function with default calibration, but it may be - * inaccurate. Provide a warning, and continue on. - */ - hid_warn(hdev, "Unable to read IMU calibration data\n"); - } - - /* Set the reporting mode to 0x30, which is the full report mode */ - ret = joycon_set_report_mode(ctlr); - if (ret) { - hid_err(hdev, "Failed to set report mode; ret=%d\n", ret); - goto err_mutex; - } - - /* Enable rumble */ - ret = joycon_enable_rumble(ctlr); - if (ret) { - hid_err(hdev, "Failed to enable rumble; ret=%d\n", ret); - goto err_mutex; - } - - /* Enable the IMU */ - ret = joycon_enable_imu(ctlr); + ret = joycon_init(hdev); if (ret) { - hid_err(hdev, "Failed to enable the IMU; ret=%d\n", ret); - goto err_mutex; - } - - ret = joycon_read_info(ctlr); - if (ret) { - hid_err(hdev, "Failed to retrieve controller info; ret=%d\n", - ret); - goto err_mutex; + hid_err(hdev, "Failed to initialize controller; ret=%d\n", ret); + goto err_close; } - mutex_unlock(&ctlr->output_mutex); - /* Initialize the leds */ ret = joycon_leds_create(ctlr); if (ret) { @@ -2261,13 +2702,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, ret = joycon_power_supply_create(ctlr); if (ret) { hid_err(hdev, "Failed to create power_supply; ret=%d\n", ret); - goto err_close; + goto err_ida; } ret = joycon_input_create(ctlr); if (ret) { hid_err(hdev, "Failed to create input device; ret=%d\n", ret); - goto err_close; + goto err_ida; } ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; @@ -2275,8 +2716,8 @@ static int nintendo_hid_probe(struct hid_device *hdev, hid_dbg(hdev, "probe - success\n"); return 0; -err_mutex: - mutex_unlock(&ctlr->output_mutex); +err_ida: + ida_free(&nintendo_player_id_allocator, ctlr->player_id); err_close: hid_hw_close(hdev); err_stop: @@ -2301,14 +2742,67 @@ static void nintendo_hid_remove(struct hid_device *hdev) spin_unlock_irqrestore(&ctlr->lock, flags); destroy_workqueue(ctlr->rumble_queue); + ida_free(&nintendo_player_id_allocator, ctlr->player_id); hid_hw_close(hdev); hid_hw_stop(hdev); } +#ifdef CONFIG_PM + +static int nintendo_hid_resume(struct hid_device *hdev) +{ + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + int ret; + + hid_dbg(hdev, "resume\n"); + if (!joycon_using_usb(ctlr)) { + hid_dbg(hdev, "no-op resume for bt ctlr\n"); + ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; + return 0; + } + + ret = joycon_init(hdev); + if (ret) + hid_err(hdev, + "Failed to restore controller after resume: %d\n", + ret); + else + ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; + + return ret; +} + +static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) +{ + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + + hid_dbg(hdev, "suspend: %d\n", message.event); + /* + * Avoid any blocking loops in suspend/resume transitions. + * + * joycon_enforce_subcmd_rate() can result in repeated retries if for + * whatever reason the controller stops providing input reports. + * + * This has been observed with bluetooth controllers which lose + * connectivity prior to suspend (but not long enough to result in + * complete disconnection). + */ + ctlr->ctlr_state = JOYCON_CTLR_STATE_SUSPENDED; + return 0; +} + +#endif + static const struct hid_device_id nintendo_hid_devices[] = { { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_PROCON) }, + { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_SNESCON) }, + { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_GENCON) }, + { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_N64CON) }, { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_PROCON) }, { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO, @@ -2317,6 +2811,12 @@ static const struct hid_device_id nintendo_hid_devices[] = { USB_DEVICE_ID_NINTENDO_JOYCONL) }, { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_JOYCONR) }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_SNESCON) }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_GENCON) }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_N64CON) }, { } }; MODULE_DEVICE_TABLE(hid, nintendo_hid_devices); @@ -2327,10 +2827,28 @@ static struct hid_driver nintendo_hid_driver = { .probe = nintendo_hid_probe, .remove = nintendo_hid_remove, .raw_event = nintendo_hid_event, + +#ifdef CONFIG_PM + .resume = nintendo_hid_resume, + .suspend = nintendo_hid_suspend, +#endif }; -module_hid_driver(nintendo_hid_driver); +static int __init nintendo_init(void) +{ + return hid_register_driver(&nintendo_hid_driver); +} + +static void __exit nintendo_exit(void) +{ + hid_unregister_driver(&nintendo_hid_driver); + ida_destroy(&nintendo_player_id_allocator); +} + +module_init(nintendo_init); +module_exit(nintendo_exit); MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Ryan McClelland <rymcclel@gmail.com>"); +MODULE_AUTHOR("Emily Strickland <linux@emily.st>"); MODULE_AUTHOR("Daniel J. Ogorchock <djogorchock@gmail.com>"); MODULE_DESCRIPTION("Driver for Nintendo Switch Controllers"); - |
