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