^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * HID driver for Nintendo Switch Joy-Cons and Pro Controllers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2019 Daniel J. Ogorchock <djogorchock@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * The following resources/projects were referenced for this driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * https://github.com/FrotBot/SwitchProConLinuxUSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * https://github.com/MTCKC/ProconXInput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * hid-wiimote kernel hid driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * hid-logitech-hidpp driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Pro Controllers can either be used over USB or Bluetooth.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * The driver will retrieve the factory calibration info from the controllers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * so little to no user calibration should be required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Reference the url below for the following HID report defines:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Output Reports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const u8 JC_OUTPUT_RUMBLE_AND_SUBCMD = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const u8 JC_OUTPUT_FW_UPDATE_PKT = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static const u8 JC_OUTPUT_RUMBLE_ONLY = 0x10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static const u8 JC_OUTPUT_MCU_DATA = 0x11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static const u8 JC_OUTPUT_USB_CMD = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Subcommand IDs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static const u8 JC_SUBCMD_STATE /*= 0x00*/;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static const u8 JC_SUBCMD_MANUAL_BT_PAIRING = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static const u8 JC_SUBCMD_REQ_DEV_INFO = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static const u8 JC_SUBCMD_SET_REPORT_MODE = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static const u8 JC_SUBCMD_TRIGGERS_ELAPSED = 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static const u8 JC_SUBCMD_GET_PAGE_LIST_STATE = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static const u8 JC_SUBCMD_SET_HCI_STATE = 0x06;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const u8 JC_SUBCMD_RESET_PAIRING_INFO = 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static const u8 JC_SUBCMD_LOW_POWER_MODE = 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static const u8 JC_SUBCMD_SPI_FLASH_READ = 0x10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static const u8 JC_SUBCMD_SPI_FLASH_WRITE = 0x11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static const u8 JC_SUBCMD_RESET_MCU = 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static const u8 JC_SUBCMD_SET_MCU_CONFIG = 0x21;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static const u8 JC_SUBCMD_SET_MCU_STATE = 0x22;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const u8 JC_SUBCMD_SET_PLAYER_LIGHTS = 0x30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static const u8 JC_SUBCMD_GET_PLAYER_LIGHTS = 0x31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static const u8 JC_SUBCMD_SET_HOME_LIGHT = 0x38;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static const u8 JC_SUBCMD_ENABLE_IMU = 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static const u8 JC_SUBCMD_SET_IMU_SENSITIVITY = 0x41;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static const u8 JC_SUBCMD_WRITE_IMU_REG = 0x42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static const u8 JC_SUBCMD_READ_IMU_REG = 0x43;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static const u8 JC_SUBCMD_ENABLE_VIBRATION = 0x48;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static const u8 JC_SUBCMD_GET_REGULATED_VOLTAGE = 0x50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* Input Reports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static const u8 JC_INPUT_BUTTON_EVENT = 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static const u8 JC_INPUT_SUBCMD_REPLY = 0x21;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static const u8 JC_INPUT_IMU_DATA = 0x30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static const u8 JC_INPUT_MCU_DATA = 0x31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static const u8 JC_INPUT_USB_RESPONSE = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Feature Reports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static const u8 JC_FEATURE_LAST_SUBCMD = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static const u8 JC_FEATURE_OTA_FW_UPGRADE = 0x70;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static const u8 JC_FEATURE_SETUP_MEM_READ = 0x71;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static const u8 JC_FEATURE_MEM_READ = 0x72;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static const u8 JC_FEATURE_ERASE_MEM_SECTOR = 0x73;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static const u8 JC_FEATURE_MEM_WRITE = 0x74;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static const u8 JC_FEATURE_LAUNCH = 0x75;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* USB Commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static const u8 JC_USB_CMD_CONN_STATUS = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static const u8 JC_USB_CMD_HANDSHAKE = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static const u8 JC_USB_CMD_BAUDRATE_3M = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static const u8 JC_USB_CMD_NO_TIMEOUT = 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static const u8 JC_USB_CMD_EN_TIMEOUT = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static const u8 JC_USB_RESET = 0x06;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static const u8 JC_USB_PRE_HANDSHAKE = 0x91;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static const u8 JC_USB_SEND_UART = 0x92;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* SPI storage addresses of factory calibration data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static const u16 JC_CAL_DATA_START = 0x603d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static const u16 JC_CAL_DATA_END = 0x604e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* The raw analog joystick values will be mapped in terms of this magnitude */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static const u16 JC_MAX_STICK_MAG = 32767;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static const u16 JC_STICK_FUZZ = 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static const u16 JC_STICK_FLAT = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* States for controller state machine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) enum joycon_ctlr_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) JOYCON_CTLR_STATE_INIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) JOYCON_CTLR_STATE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct joycon_stick_cal {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) s32 max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) s32 min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) s32 center;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * All the controller's button values are stored in a u32.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * They can be accessed with bitwise ANDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static const u32 JC_BTN_Y = BIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static const u32 JC_BTN_X = BIT(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static const u32 JC_BTN_B = BIT(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static const u32 JC_BTN_A = BIT(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static const u32 JC_BTN_SR_R = BIT(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static const u32 JC_BTN_SL_R = BIT(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static const u32 JC_BTN_R = BIT(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static const u32 JC_BTN_ZR = BIT(7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static const u32 JC_BTN_MINUS = BIT(8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static const u32 JC_BTN_PLUS = BIT(9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static const u32 JC_BTN_RSTICK = BIT(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static const u32 JC_BTN_LSTICK = BIT(11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static const u32 JC_BTN_HOME = BIT(12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static const u32 JC_BTN_CAP = BIT(13); /* capture button */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static const u32 JC_BTN_DOWN = BIT(16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static const u32 JC_BTN_UP = BIT(17);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static const u32 JC_BTN_RIGHT = BIT(18);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static const u32 JC_BTN_LEFT = BIT(19);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static const u32 JC_BTN_SR_L = BIT(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static const u32 JC_BTN_SL_L = BIT(21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const u32 JC_BTN_L = BIT(22);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static const u32 JC_BTN_ZL = BIT(23);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) enum joycon_msg_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) JOYCON_MSG_TYPE_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) JOYCON_MSG_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) JOYCON_MSG_TYPE_SUBCMD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct joycon_subcmd_request {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) u8 output_id; /* must be 0x01 for subcommand, 0x10 for rumble only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) u8 packet_num; /* incremented every send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) u8 rumble_data[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) u8 subcmd_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) u8 data[0]; /* length depends on the subcommand */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct joycon_subcmd_reply {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) u8 ack; /* MSB 1 for ACK, 0 for NACK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) u8 id; /* id of requested subcmd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u8 data[0]; /* will be at most 35 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct joycon_input_report {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) u8 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) u8 timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u8 bat_con; /* battery and connection info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u8 button_status[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) u8 left_stick[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) u8 right_stick[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) u8 vibrator_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * If support for firmware updates, gyroscope data, and/or NFC/IR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * are added in the future, this can be swapped for a union.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct joycon_subcmd_reply reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* Each physical controller is associated with a joycon_ctlr struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct joycon_ctlr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) enum joycon_ctlr_state ctlr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* The following members are used for synchronous sends/receives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) enum joycon_msg_type msg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) u8 subcmd_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct mutex output_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) u8 input_buf[JC_MAX_RESP_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) bool received_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u8 usb_ack_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u8 subcmd_ack_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /* factory calibration data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct joycon_stick_cal left_stick_cal_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct joycon_stick_cal left_stick_cal_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct joycon_stick_cal right_stick_cal_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct joycon_stick_cal right_stick_cal_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) buf = kmemdup(data, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ret = hid_hw_output_report(hdev, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) hid_dbg(hdev, "Failed to send output report ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ret = __joycon_hid_send(ctlr->hdev, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!wait_event_timeout(ctlr->wait, ctlr->received_resp, HZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) hid_dbg(ctlr->hdev, "synchronous send/receive timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ctlr->received_resp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) u8 buf[2] = {JC_OUTPUT_USB_CMD};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) buf[1] = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ctlr->usb_ack_match = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ctlr->msg_type = JOYCON_MSG_TYPE_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ret = joycon_hid_send_sync(ctlr, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct joycon_subcmd_request *subcmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) size_t data_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) subcmd->packet_num = ctlr->subcmd_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (++ctlr->subcmd_num > 0xF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ctlr->subcmd_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ctlr->subcmd_ack_match = subcmd->subcmd_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) sizeof(*subcmd) + data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* Supply nibbles for flash and on. Ones correspond to active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct joycon_subcmd_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) u8 buffer[sizeof(*req) + 1] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) req = (struct joycon_subcmd_request *)buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) req->data[0] = (flash << 4) | on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) hid_dbg(ctlr->hdev, "setting player leds\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return joycon_send_subcmd(ctlr, req, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static const u16 DFLT_STICK_CAL_CEN = 2000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static const u16 DFLT_STICK_CAL_MAX = 3500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static const u16 DFLT_STICK_CAL_MIN = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int joycon_request_calibration(struct joycon_ctlr *ctlr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct joycon_subcmd_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) u8 buffer[sizeof(*req) + 5] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct joycon_input_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct joycon_stick_cal *cal_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct joycon_stick_cal *cal_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) s32 x_max_above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) s32 x_min_below;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) s32 y_max_above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) s32 y_min_below;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) u8 *raw_cal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) req = (struct joycon_subcmd_request *)buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) data = req->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) data[0] = 0xFF & JC_CAL_DATA_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) data[1] = 0xFF & (JC_CAL_DATA_START >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) data[2] = 0xFF & (JC_CAL_DATA_START >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) data[3] = 0xFF & (JC_CAL_DATA_START >> 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) data[4] = JC_CAL_DATA_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) hid_dbg(ctlr->hdev, "requesting cal data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ret = joycon_send_subcmd(ctlr, req, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) hid_warn(ctlr->hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) "Failed to read stick cal, using defaults; ret=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ctlr->left_stick_cal_x.center = DFLT_STICK_CAL_CEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ctlr->left_stick_cal_x.max = DFLT_STICK_CAL_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ctlr->left_stick_cal_x.min = DFLT_STICK_CAL_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ctlr->left_stick_cal_y.center = DFLT_STICK_CAL_CEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ctlr->left_stick_cal_y.max = DFLT_STICK_CAL_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ctlr->left_stick_cal_y.min = DFLT_STICK_CAL_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ctlr->right_stick_cal_x.center = DFLT_STICK_CAL_CEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ctlr->right_stick_cal_x.max = DFLT_STICK_CAL_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ctlr->right_stick_cal_x.min = DFLT_STICK_CAL_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ctlr->right_stick_cal_y.center = DFLT_STICK_CAL_CEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ctlr->right_stick_cal_y.max = DFLT_STICK_CAL_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ctlr->right_stick_cal_y.min = DFLT_STICK_CAL_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) report = (struct joycon_input_report *)ctlr->input_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) raw_cal = &report->reply.data[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* left stick calibration parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) cal_x = &ctlr->left_stick_cal_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) cal_y = &ctlr->left_stick_cal_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) cal_x->max = cal_x->center + x_max_above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) cal_x->min = cal_x->center - x_min_below;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) cal_y->max = cal_y->center + y_max_above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) cal_y->min = cal_y->center - y_min_below;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* right stick calibration parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) raw_cal += 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) cal_x = &ctlr->right_stick_cal_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) cal_y = &ctlr->right_stick_cal_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) cal_x->max = cal_x->center + x_max_above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) cal_x->min = cal_x->center - x_min_below;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) cal_y->max = cal_y->center + y_max_above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) cal_y->min = cal_y->center - y_min_below;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) hid_dbg(ctlr->hdev, "calibration:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) "l_x_c=%d l_x_max=%d l_x_min=%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) "l_y_c=%d l_y_max=%d l_y_min=%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) "r_x_c=%d r_x_max=%d r_x_min=%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) "r_y_c=%d r_y_max=%d r_y_min=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ctlr->left_stick_cal_x.center,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ctlr->left_stick_cal_x.max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ctlr->left_stick_cal_x.min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) ctlr->left_stick_cal_y.center,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ctlr->left_stick_cal_y.max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ctlr->left_stick_cal_y.min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ctlr->right_stick_cal_x.center,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ctlr->right_stick_cal_x.max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ctlr->right_stick_cal_x.min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ctlr->right_stick_cal_y.center,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ctlr->right_stick_cal_y.max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ctlr->right_stick_cal_y.min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static int joycon_set_report_mode(struct joycon_ctlr *ctlr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct joycon_subcmd_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) u8 buffer[sizeof(*req) + 1] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) req = (struct joycon_subcmd_request *)buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) req->data[0] = 0x30; /* standard, full report mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) hid_dbg(ctlr->hdev, "setting controller report mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return joycon_send_subcmd(ctlr, req, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static s32 joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) s32 center = cal->center;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) s32 min = cal->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) s32 max = cal->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) s32 new_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (val > center) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) new_val = (val - center) * JC_MAX_STICK_MAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) new_val /= (max - center);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) new_val = (center - val) * -JC_MAX_STICK_MAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) new_val /= (center - min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return new_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static void joycon_parse_report(struct joycon_ctlr *ctlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) struct joycon_input_report *rep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct input_dev *dev = ctlr->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) u32 btns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) u32 id = ctlr->hdev->product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (id != USB_DEVICE_ID_NINTENDO_JOYCONR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) u16 raw_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) u16 raw_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) s32 x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) s32 y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* get raw stick values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) raw_y = hid_field_extract(ctlr->hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) rep->left_stick + 1, 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* map the stick values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* report sticks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) input_report_abs(dev, ABS_X, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) input_report_abs(dev, ABS_Y, y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /* report buttons */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) input_report_key(dev, BTN_TL, btns & JC_BTN_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) input_report_key(dev, BTN_TL2, btns & JC_BTN_ZL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (id != USB_DEVICE_ID_NINTENDO_PROCON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* Report the S buttons as the non-existent triggers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) input_report_key(dev, BTN_TR, btns & JC_BTN_SL_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) input_report_key(dev, BTN_TR2, btns & JC_BTN_SR_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) input_report_key(dev, BTN_SELECT, btns & JC_BTN_MINUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) input_report_key(dev, BTN_THUMBL, btns & JC_BTN_LSTICK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) input_report_key(dev, BTN_Z, btns & JC_BTN_CAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) input_report_key(dev, BTN_DPAD_DOWN, btns & JC_BTN_DOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) input_report_key(dev, BTN_DPAD_UP, btns & JC_BTN_UP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) input_report_key(dev, BTN_DPAD_RIGHT, btns & JC_BTN_RIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) input_report_key(dev, BTN_DPAD_LEFT, btns & JC_BTN_LEFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (id != USB_DEVICE_ID_NINTENDO_JOYCONL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) u16 raw_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) u16 raw_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) s32 x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) s32 y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* get raw stick values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) raw_y = hid_field_extract(ctlr->hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) rep->right_stick + 1, 4, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) /* map stick values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* report sticks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) input_report_abs(dev, ABS_RX, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) input_report_abs(dev, ABS_RY, y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /* report buttons */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) input_report_key(dev, BTN_TR, btns & JC_BTN_R);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) input_report_key(dev, BTN_TR2, btns & JC_BTN_ZR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (id != USB_DEVICE_ID_NINTENDO_PROCON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* Report the S buttons as the non-existent triggers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) input_report_key(dev, BTN_TL, btns & JC_BTN_SL_R);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) input_report_key(dev, BTN_TL2, btns & JC_BTN_SR_R);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) input_report_key(dev, BTN_START, btns & JC_BTN_PLUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) input_report_key(dev, BTN_THUMBR, btns & JC_BTN_RSTICK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) input_report_key(dev, BTN_MODE, btns & JC_BTN_HOME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) input_report_key(dev, BTN_WEST, btns & JC_BTN_Y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) input_report_key(dev, BTN_NORTH, btns & JC_BTN_X);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) input_report_key(dev, BTN_EAST, btns & JC_BTN_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) input_report_key(dev, BTN_SOUTH, btns & JC_BTN_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static const unsigned int joycon_button_inputs_l[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) BTN_SELECT, BTN_Z, BTN_THUMBL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) BTN_TL, BTN_TL2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 0 /* 0 signals end of array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static const unsigned int joycon_button_inputs_r[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) BTN_START, BTN_MODE, BTN_THUMBR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) BTN_SOUTH, BTN_EAST, BTN_NORTH, BTN_WEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) BTN_TR, BTN_TR2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 0 /* 0 signals end of array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static DEFINE_MUTEX(joycon_input_num_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static int joycon_input_create(struct joycon_ctlr *ctlr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static int input_num = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) hdev = ctlr->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) case USB_DEVICE_ID_NINTENDO_PROCON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) name = "Nintendo Switch Pro Controller";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) case USB_DEVICE_ID_NINTENDO_JOYCONL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) name = "Nintendo Switch Left Joy-Con";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) case USB_DEVICE_ID_NINTENDO_JOYCONR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) name = "Nintendo Switch Right Joy-Con";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) default: /* Should be impossible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) hid_err(hdev, "Invalid hid product\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) ctlr->input = devm_input_allocate_device(&hdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (!ctlr->input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ctlr->input->id.bustype = hdev->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ctlr->input->id.vendor = hdev->vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) ctlr->input->id.product = hdev->product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) ctlr->input->id.version = hdev->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) ctlr->input->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) input_set_drvdata(ctlr->input, ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* set up sticks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) input_set_abs_params(ctlr->input, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) JC_STICK_FUZZ, JC_STICK_FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) input_set_abs_params(ctlr->input, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) JC_STICK_FUZZ, JC_STICK_FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) input_set_abs_params(ctlr->input, ABS_RX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) JC_STICK_FUZZ, JC_STICK_FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) input_set_abs_params(ctlr->input, ABS_RY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) JC_STICK_FUZZ, JC_STICK_FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /* set up buttons */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) for (i = 0; joycon_button_inputs_l[i] > 0; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) input_set_capability(ctlr->input, EV_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) joycon_button_inputs_l[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) for (i = 0; joycon_button_inputs_r[i] > 0; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) input_set_capability(ctlr->input, EV_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) joycon_button_inputs_r[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) ret = input_register_device(ctlr->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* Set the default controller player leds based on controller number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) mutex_lock(&joycon_input_num_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) mutex_lock(&ctlr->output_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) ret = joycon_set_player_leds(ctlr, 0, 0xF >> (4 - input_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) mutex_unlock(&ctlr->output_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if (++input_num > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) input_num = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) mutex_unlock(&joycon_input_num_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /* Common handler for parsing inputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (data[0] == JC_INPUT_SUBCMD_REPLY || data[0] == JC_INPUT_IMU_DATA ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) data[0] == JC_INPUT_MCU_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (size >= 12) /* make sure it contains the input report */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) joycon_parse_report(ctlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) (struct joycon_input_report *)data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) bool match = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct joycon_input_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (unlikely(mutex_is_locked(&ctlr->output_mutex)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) ctlr->msg_type != JOYCON_MSG_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) switch (ctlr->msg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) case JOYCON_MSG_TYPE_USB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (size < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (data[0] == JC_INPUT_USB_RESPONSE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) data[1] == ctlr->usb_ack_match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) case JOYCON_MSG_TYPE_SUBCMD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (size < sizeof(struct joycon_input_report) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) data[0] != JC_INPUT_SUBCMD_REPLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) report = (struct joycon_input_report *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) if (report->reply.id == ctlr->subcmd_ack_match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) memcpy(ctlr->input_buf, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) min(size, (int)JC_MAX_RESP_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) ctlr->msg_type = JOYCON_MSG_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ctlr->received_resp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) wake_up(&ctlr->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /* This message has been handled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) ret = joycon_ctlr_read_handler(ctlr, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static int nintendo_hid_event(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct hid_report *report, u8 *raw_data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) struct joycon_ctlr *ctlr = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (size < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return joycon_ctlr_handle_event(ctlr, raw_data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static int nintendo_hid_probe(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct joycon_ctlr *ctlr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) hid_dbg(hdev, "probe - start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (!ctlr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) ctlr->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) hid_set_drvdata(hdev, ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) mutex_init(&ctlr->output_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) init_waitqueue_head(&ctlr->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) hid_err(hdev, "HID parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) hid_err(hdev, "HW start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) ret = hid_hw_open(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) hid_err(hdev, "cannot start hardware I/O\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) goto err_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) hid_device_io_start(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /* Initialize the controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) mutex_lock(&ctlr->output_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /* if handshake command fails, assume ble pro controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (hdev->product == USB_DEVICE_ID_NINTENDO_PROCON &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) hid_dbg(hdev, "detected USB controller\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) /* set baudrate for improved latency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) hid_err(hdev, "Failed to set baudrate; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) goto err_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) /* handshake */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) hid_err(hdev, "Failed handshake; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) goto err_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * Set no timeout (to keep controller in USB mode).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * This doesn't send a response, so ignore the timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* get controller calibration data, and parse it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) ret = joycon_request_calibration(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * We can function with default calibration, but it may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * inaccurate. Provide a warning, and continue on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) hid_warn(hdev, "Analog stick positions may be inaccurate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /* Set the reporting mode to 0x30, which is the full report mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) ret = joycon_set_report_mode(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) hid_err(hdev, "Failed to set report mode; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) goto err_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) mutex_unlock(&ctlr->output_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) ret = joycon_input_create(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) hid_err(hdev, "Failed to create input device; ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) ctlr->ctlr_state = JOYCON_CTLR_STATE_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) hid_dbg(hdev, "probe - success\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) err_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) mutex_unlock(&ctlr->output_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) err_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) hid_hw_close(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) err_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) hid_err(hdev, "probe - fail = %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) static void nintendo_hid_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) hid_dbg(hdev, "remove\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) hid_hw_close(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static const struct hid_device_id nintendo_hid_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) USB_DEVICE_ID_NINTENDO_PROCON) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) USB_DEVICE_ID_NINTENDO_PROCON) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) USB_DEVICE_ID_NINTENDO_JOYCONL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) USB_DEVICE_ID_NINTENDO_JOYCONR) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) MODULE_DEVICE_TABLE(hid, nintendo_hid_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static struct hid_driver nintendo_hid_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) .name = "nintendo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) .id_table = nintendo_hid_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) .probe = nintendo_hid_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) .remove = nintendo_hid_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) .raw_event = nintendo_hid_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) module_hid_driver(nintendo_hid_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) MODULE_AUTHOR("Daniel J. Ogorchock <djogorchock@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) MODULE_DESCRIPTION("Driver for Nintendo Switch Controllers");