Lines Matching +full:0 +full:- +full:987
1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Nintendo Switch Joy-Cons and Pro Controllers
5 * Copyright (c) 2019-2021 Daniel J. Ogorchock <djogorchock@gmail.com>
12 * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
16 * hid-wiimote kernel hid driver
17 * hid-logitech-hidpp driver
18 * hid-sony driver
20 * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
31 #include "hid-ids.h"
50 #define JC_OUTPUT_RUMBLE_AND_SUBCMD 0x01
51 #define JC_OUTPUT_FW_UPDATE_PKT 0x03
52 #define JC_OUTPUT_RUMBLE_ONLY 0x10
53 #define JC_OUTPUT_MCU_DATA 0x11
54 #define JC_OUTPUT_USB_CMD 0x80
57 #define JC_SUBCMD_STATE 0x00
58 #define JC_SUBCMD_MANUAL_BT_PAIRING 0x01
59 #define JC_SUBCMD_REQ_DEV_INFO 0x02
60 #define JC_SUBCMD_SET_REPORT_MODE 0x03
61 #define JC_SUBCMD_TRIGGERS_ELAPSED 0x04
62 #define JC_SUBCMD_GET_PAGE_LIST_STATE 0x05
63 #define JC_SUBCMD_SET_HCI_STATE 0x06
64 #define JC_SUBCMD_RESET_PAIRING_INFO 0x07
65 #define JC_SUBCMD_LOW_POWER_MODE 0x08
66 #define JC_SUBCMD_SPI_FLASH_READ 0x10
67 #define JC_SUBCMD_SPI_FLASH_WRITE 0x11
68 #define JC_SUBCMD_RESET_MCU 0x20
69 #define JC_SUBCMD_SET_MCU_CONFIG 0x21
70 #define JC_SUBCMD_SET_MCU_STATE 0x22
71 #define JC_SUBCMD_SET_PLAYER_LIGHTS 0x30
72 #define JC_SUBCMD_GET_PLAYER_LIGHTS 0x31
73 #define JC_SUBCMD_SET_HOME_LIGHT 0x38
74 #define JC_SUBCMD_ENABLE_IMU 0x40
75 #define JC_SUBCMD_SET_IMU_SENSITIVITY 0x41
76 #define JC_SUBCMD_WRITE_IMU_REG 0x42
77 #define JC_SUBCMD_READ_IMU_REG 0x43
78 #define JC_SUBCMD_ENABLE_VIBRATION 0x48
79 #define JC_SUBCMD_GET_REGULATED_VOLTAGE 0x50
82 #define JC_INPUT_BUTTON_EVENT 0x3F
83 #define JC_INPUT_SUBCMD_REPLY 0x21
84 #define JC_INPUT_IMU_DATA 0x30
85 #define JC_INPUT_MCU_DATA 0x31
86 #define JC_INPUT_USB_RESPONSE 0x81
89 #define JC_FEATURE_LAST_SUBCMD 0x02
90 #define JC_FEATURE_OTA_FW_UPGRADE 0x70
91 #define JC_FEATURE_SETUP_MEM_READ 0x71
92 #define JC_FEATURE_MEM_READ 0x72
93 #define JC_FEATURE_ERASE_MEM_SECTOR 0x73
94 #define JC_FEATURE_MEM_WRITE 0x74
95 #define JC_FEATURE_LAUNCH 0x75
98 #define JC_USB_CMD_CONN_STATUS 0x01
99 #define JC_USB_CMD_HANDSHAKE 0x02
100 #define JC_USB_CMD_BAUDRATE_3M 0x03
101 #define JC_USB_CMD_NO_TIMEOUT 0x04
102 #define JC_USB_CMD_EN_TIMEOUT 0x05
103 #define JC_USB_RESET 0x06
104 #define JC_USB_PRE_HANDSHAKE 0x91
105 #define JC_USB_SEND_UART 0x92
108 #define JC_CAL_USR_MAGIC_0 0xB2
109 #define JC_CAL_USR_MAGIC_1 0xA1
113 #define JC_CAL_USR_LEFT_MAGIC_ADDR 0x8010
114 #define JC_CAL_USR_LEFT_DATA_ADDR 0x8012
115 #define JC_CAL_USR_LEFT_DATA_END 0x801A
116 #define JC_CAL_USR_RIGHT_MAGIC_ADDR 0x801B
117 #define JC_CAL_USR_RIGHT_DATA_ADDR 0x801D
119 (JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
122 #define JC_CAL_FCT_DATA_LEFT_ADDR 0x603d
123 #define JC_CAL_FCT_DATA_RIGHT_ADDR 0x6046
126 #define JC_IMU_CAL_FCT_DATA_ADDR 0x6020
127 #define JC_IMU_CAL_FCT_DATA_END 0x6037
129 (JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
131 #define JC_IMU_CAL_USR_MAGIC_ADDR 0x8026
132 #define JC_IMU_CAL_USR_DATA_ADDR 0x8028
139 /* Hat values for pro controller's d-pad */
141 #define JC_DPAD_FUZZ 0
142 #define JC_DPAD_FLAT 0
153 * configured with a range of +-8000 milliGs. Therefore, the resolution can be
154 * calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
161 #define JC_IMU_ACCEL_FLAT 0
165 * configured with a range of +-2000 degrees/second.
166 * Digits per dps: (2^16 -1)/(2000*2) = 16.38375
167 * dps per digit: 16.38375E-1 = .0610
173 * digits per dps (corrected): .0702E-1 = 14.247
180 #define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
183 #define JC_IMU_GYRO_FLAT 0
205 { 0x0000, 0x01, 41 }, { 0x0000, 0x02, 42 }, { 0x0000, 0x03, 43 },
206 { 0x0000, 0x04, 44 }, { 0x0000, 0x05, 45 }, { 0x0000, 0x06, 46 },
207 { 0x0000, 0x07, 47 }, { 0x0000, 0x08, 48 }, { 0x0000, 0x09, 49 },
208 { 0x0000, 0x0A, 50 }, { 0x0000, 0x0B, 51 }, { 0x0000, 0x0C, 52 },
209 { 0x0000, 0x0D, 53 }, { 0x0000, 0x0E, 54 }, { 0x0000, 0x0F, 55 },
210 { 0x0000, 0x10, 57 }, { 0x0000, 0x11, 58 }, { 0x0000, 0x12, 59 },
211 { 0x0000, 0x13, 60 }, { 0x0000, 0x14, 62 }, { 0x0000, 0x15, 63 },
212 { 0x0000, 0x16, 64 }, { 0x0000, 0x17, 66 }, { 0x0000, 0x18, 67 },
213 { 0x0000, 0x19, 69 }, { 0x0000, 0x1A, 70 }, { 0x0000, 0x1B, 72 },
214 { 0x0000, 0x1C, 73 }, { 0x0000, 0x1D, 75 }, { 0x0000, 0x1e, 77 },
215 { 0x0000, 0x1f, 78 }, { 0x0000, 0x20, 80 }, { 0x0400, 0x21, 82 },
216 { 0x0800, 0x22, 84 }, { 0x0c00, 0x23, 85 }, { 0x1000, 0x24, 87 },
217 { 0x1400, 0x25, 89 }, { 0x1800, 0x26, 91 }, { 0x1c00, 0x27, 93 },
218 { 0x2000, 0x28, 95 }, { 0x2400, 0x29, 97 }, { 0x2800, 0x2a, 99 },
219 { 0x2c00, 0x2b, 102 }, { 0x3000, 0x2c, 104 }, { 0x3400, 0x2d, 106 },
220 { 0x3800, 0x2e, 108 }, { 0x3c00, 0x2f, 111 }, { 0x4000, 0x30, 113 },
221 { 0x4400, 0x31, 116 }, { 0x4800, 0x32, 118 }, { 0x4c00, 0x33, 121 },
222 { 0x5000, 0x34, 123 }, { 0x5400, 0x35, 126 }, { 0x5800, 0x36, 129 },
223 { 0x5c00, 0x37, 132 }, { 0x6000, 0x38, 135 }, { 0x6400, 0x39, 137 },
224 { 0x6800, 0x3a, 141 }, { 0x6c00, 0x3b, 144 }, { 0x7000, 0x3c, 147 },
225 { 0x7400, 0x3d, 150 }, { 0x7800, 0x3e, 153 }, { 0x7c00, 0x3f, 157 },
226 { 0x8000, 0x40, 160 }, { 0x8400, 0x41, 164 }, { 0x8800, 0x42, 167 },
227 { 0x8c00, 0x43, 171 }, { 0x9000, 0x44, 174 }, { 0x9400, 0x45, 178 },
228 { 0x9800, 0x46, 182 }, { 0x9c00, 0x47, 186 }, { 0xa000, 0x48, 190 },
229 { 0xa400, 0x49, 194 }, { 0xa800, 0x4a, 199 }, { 0xac00, 0x4b, 203 },
230 { 0xb000, 0x4c, 207 }, { 0xb400, 0x4d, 212 }, { 0xb800, 0x4e, 217 },
231 { 0xbc00, 0x4f, 221 }, { 0xc000, 0x50, 226 }, { 0xc400, 0x51, 231 },
232 { 0xc800, 0x52, 236 }, { 0xcc00, 0x53, 241 }, { 0xd000, 0x54, 247 },
233 { 0xd400, 0x55, 252 }, { 0xd800, 0x56, 258 }, { 0xdc00, 0x57, 263 },
234 { 0xe000, 0x58, 269 }, { 0xe400, 0x59, 275 }, { 0xe800, 0x5a, 281 },
235 { 0xec00, 0x5b, 287 }, { 0xf000, 0x5c, 293 }, { 0xf400, 0x5d, 300 },
236 { 0xf800, 0x5e, 306 }, { 0xfc00, 0x5f, 313 }, { 0x0001, 0x60, 320 },
237 { 0x0401, 0x61, 327 }, { 0x0801, 0x62, 334 }, { 0x0c01, 0x63, 341 },
238 { 0x1001, 0x64, 349 }, { 0x1401, 0x65, 357 }, { 0x1801, 0x66, 364 },
239 { 0x1c01, 0x67, 372 }, { 0x2001, 0x68, 381 }, { 0x2401, 0x69, 389 },
240 { 0x2801, 0x6a, 397 }, { 0x2c01, 0x6b, 406 }, { 0x3001, 0x6c, 415 },
241 { 0x3401, 0x6d, 424 }, { 0x3801, 0x6e, 433 }, { 0x3c01, 0x6f, 443 },
242 { 0x4001, 0x70, 453 }, { 0x4401, 0x71, 462 }, { 0x4801, 0x72, 473 },
243 { 0x4c01, 0x73, 483 }, { 0x5001, 0x74, 494 }, { 0x5401, 0x75, 504 },
244 { 0x5801, 0x76, 515 }, { 0x5c01, 0x77, 527 }, { 0x6001, 0x78, 538 },
245 { 0x6401, 0x79, 550 }, { 0x6801, 0x7a, 562 }, { 0x6c01, 0x7b, 574 },
246 { 0x7001, 0x7c, 587 }, { 0x7401, 0x7d, 600 }, { 0x7801, 0x7e, 613 },
247 { 0x7c01, 0x7f, 626 }, { 0x8001, 0x00, 640 }, { 0x8401, 0x00, 654 },
248 { 0x8801, 0x00, 668 }, { 0x8c01, 0x00, 683 }, { 0x9001, 0x00, 698 },
249 { 0x9401, 0x00, 713 }, { 0x9801, 0x00, 729 }, { 0x9c01, 0x00, 745 },
250 { 0xa001, 0x00, 761 }, { 0xa401, 0x00, 778 }, { 0xa801, 0x00, 795 },
251 { 0xac01, 0x00, 812 }, { 0xb001, 0x00, 830 }, { 0xb401, 0x00, 848 },
252 { 0xb801, 0x00, 867 }, { 0xbc01, 0x00, 886 }, { 0xc001, 0x00, 905 },
253 { 0xc401, 0x00, 925 }, { 0xc801, 0x00, 945 }, { 0xcc01, 0x00, 966 },
254 { 0xd001, 0x00, 987 }, { 0xd401, 0x00, 1009 }, { 0xd801, 0x00, 1031 },
255 { 0xdc01, 0x00, 1053 }, { 0xe001, 0x00, 1076 }, { 0xe401, 0x00, 1100 },
256 { 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 },
257 { 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 }
263 { 0x00, 0x0040, 0 },
264 { 0x02, 0x8040, 10 }, { 0x04, 0x0041, 12 }, { 0x06, 0x8041, 14 },
265 { 0x08, 0x0042, 17 }, { 0x0a, 0x8042, 20 }, { 0x0c, 0x0043, 24 },
266 { 0x0e, 0x8043, 28 }, { 0x10, 0x0044, 33 }, { 0x12, 0x8044, 40 },
267 { 0x14, 0x0045, 47 }, { 0x16, 0x8045, 56 }, { 0x18, 0x0046, 67 },
268 { 0x1a, 0x8046, 80 }, { 0x1c, 0x0047, 95 }, { 0x1e, 0x8047, 112 },
269 { 0x20, 0x0048, 117 }, { 0x22, 0x8048, 123 }, { 0x24, 0x0049, 128 },
270 { 0x26, 0x8049, 134 }, { 0x28, 0x004a, 140 }, { 0x2a, 0x804a, 146 },
271 { 0x2c, 0x004b, 152 }, { 0x2e, 0x804b, 159 }, { 0x30, 0x004c, 166 },
272 { 0x32, 0x804c, 173 }, { 0x34, 0x004d, 181 }, { 0x36, 0x804d, 189 },
273 { 0x38, 0x004e, 198 }, { 0x3a, 0x804e, 206 }, { 0x3c, 0x004f, 215 },
274 { 0x3e, 0x804f, 225 }, { 0x40, 0x0050, 230 }, { 0x42, 0x8050, 235 },
275 { 0x44, 0x0051, 240 }, { 0x46, 0x8051, 245 }, { 0x48, 0x0052, 251 },
276 { 0x4a, 0x8052, 256 }, { 0x4c, 0x0053, 262 }, { 0x4e, 0x8053, 268 },
277 { 0x50, 0x0054, 273 }, { 0x52, 0x8054, 279 }, { 0x54, 0x0055, 286 },
278 { 0x56, 0x8055, 292 }, { 0x58, 0x0056, 298 }, { 0x5a, 0x8056, 305 },
279 { 0x5c, 0x0057, 311 }, { 0x5e, 0x8057, 318 }, { 0x60, 0x0058, 325 },
280 { 0x62, 0x8058, 332 }, { 0x64, 0x0059, 340 }, { 0x66, 0x8059, 347 },
281 { 0x68, 0x005a, 355 }, { 0x6a, 0x805a, 362 }, { 0x6c, 0x005b, 370 },
282 { 0x6e, 0x805b, 378 }, { 0x70, 0x005c, 387 }, { 0x72, 0x805c, 395 },
283 { 0x74, 0x005d, 404 }, { 0x76, 0x805d, 413 }, { 0x78, 0x005e, 422 },
284 { 0x7a, 0x805e, 431 }, { 0x7c, 0x005f, 440 }, { 0x7e, 0x805f, 450 },
285 { 0x80, 0x0060, 460 }, { 0x82, 0x8060, 470 }, { 0x84, 0x0061, 480 },
286 { 0x86, 0x8061, 491 }, { 0x88, 0x0062, 501 }, { 0x8a, 0x8062, 512 },
287 { 0x8c, 0x0063, 524 }, { 0x8e, 0x8063, 535 }, { 0x90, 0x0064, 547 },
288 { 0x92, 0x8064, 559 }, { 0x94, 0x0065, 571 }, { 0x96, 0x8065, 584 },
289 { 0x98, 0x0066, 596 }, { 0x9a, 0x8066, 609 }, { 0x9c, 0x0067, 623 },
290 { 0x9e, 0x8067, 636 }, { 0xa0, 0x0068, 650 }, { 0xa2, 0x8068, 665 },
291 { 0xa4, 0x0069, 679 }, { 0xa6, 0x8069, 694 }, { 0xa8, 0x006a, 709 },
292 { 0xaa, 0x806a, 725 }, { 0xac, 0x006b, 741 }, { 0xae, 0x806b, 757 },
293 { 0xb0, 0x006c, 773 }, { 0xb2, 0x806c, 790 }, { 0xb4, 0x006d, 808 },
294 { 0xb6, 0x806d, 825 }, { 0xb8, 0x006e, 843 }, { 0xba, 0x806e, 862 },
295 { 0xbc, 0x006f, 881 }, { 0xbe, 0x806f, 900 }, { 0xc0, 0x0070, 920 },
296 { 0xc2, 0x8070, 940 }, { 0xc4, 0x0071, 960 }, { 0xc6, 0x8071, 981 },
297 { 0xc8, 0x0072, joycon_max_rumble_amp }
314 JOYCON_CTLR_TYPE_JCL = 0x01,
315 JOYCON_CTLR_TYPE_JCR = 0x02,
316 JOYCON_CTLR_TYPE_PRO = 0x03,
317 JOYCON_CTLR_TYPE_NESL = 0x09,
318 JOYCON_CTLR_TYPE_NESR = 0x0A,
319 JOYCON_CTLR_TYPE_SNES = 0x0B,
320 JOYCON_CTLR_TYPE_GEN = 0x0D,
321 JOYCON_CTLR_TYPE_N64 = 0x0C,
339 #define JC_BTN_Y BIT(0)
368 * D-pad is configured as buttons for the left Joy-Con only!
384 * The unused *right*-side triggers become the SL/SR triggers for the *left*
385 * Joy-Con, if and only if we're not using a charging grip.
407 * The unused *left*-side triggers become the SL/SR triggers for the *right*
408 * Joy-Con, if and only if we're not using a charging grip.
474 * N64's C buttons get assigned to d-pad directions and registered as buttons.
506 u8 output_id; /* must be 0x01 for subcommand, 0x10 for rumble only */
514 u8 ack; /* MSB 1 for ACK, 0 for NACK */
558 { 1, 0, 0, 0 },
559 { 1, 1, 0, 0 },
560 { 1, 1, 1, 0 },
562 { 1, 0, 0, 1 },
563 { 1, 0, 1, 0 },
564 { 1, 0, 1, 1 },
565 { 0, 1, 1, 0 },
640 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
641 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
642 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
644 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
646 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
650 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
651 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO || \
652 ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64)
656 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
657 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
664 * but take caution: some NSO devices lie about themselves (NES Joy-Cons and
672 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL; in joycon_device_is_left_joycon()
677 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR; in joycon_device_is_right_joycon()
682 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON; in joycon_device_is_procon()
687 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP; in joycon_device_is_chrggrip()
692 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_SNESCON; in joycon_device_is_snescon()
697 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_GENCON; in joycon_device_is_gencon()
702 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_N64CON; in joycon_device_is_n64con()
717 * These are slightly different than the device-ID-based helpers above. They are
719 * versus SNES, or NES Joy-Cons versus regular Switch Joy-Cons. They're most
729 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL; in joycon_type_is_left_joycon()
734 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR; in joycon_type_is_right_joycon()
739 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO; in joycon_type_is_procon()
744 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_SNES; in joycon_type_is_snescon()
749 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_GEN; in joycon_type_is_gencon()
754 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64; in joycon_type_is_n64con()
759 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESL; in joycon_type_is_left_nescon()
764 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESR; in joycon_type_is_right_nescon()
824 return ctlr->hdev->bus == BUS_USB; in joycon_using_usb()
834 return -ENOMEM; in __joycon_hid_send()
837 if (ret < 0) in __joycon_hid_send()
851 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_wait_for_input_report()
854 spin_lock_irqsave(&ctlr->lock, flags); in joycon_wait_for_input_report()
855 ctlr->received_input_report = false; in joycon_wait_for_input_report()
856 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_wait_for_input_report()
857 ret = wait_event_timeout(ctlr->wait, in joycon_wait_for_input_report()
858 ctlr->received_input_report, in joycon_wait_for_input_report()
862 hid_warn(ctlr->hdev, in joycon_wait_for_input_report()
878 #define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_…
883 int consecutive_valid_deltas = 0; in joycon_enforce_subcmd_rate()
884 int attempts = 0; in joycon_enforce_subcmd_rate()
887 if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ)) in joycon_enforce_subcmd_rate()
893 subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
895 spin_lock_irqsave(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
896 consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas; in joycon_enforce_subcmd_rate()
897 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
902 ctlr->ctlr_state == JOYCON_CTLR_STATE_READ && in joycon_enforce_subcmd_rate()
906 hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__); in joycon_enforce_subcmd_rate()
910 ctlr->last_subcmd_sent_msecs = current_ms; in joycon_enforce_subcmd_rate()
931 while (tries--) { in joycon_hid_send_sync()
934 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
935 if (ret < 0) { in joycon_hid_send_sync()
936 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
940 ret = wait_event_timeout(ctlr->wait, ctlr->received_resp, in joycon_hid_send_sync()
943 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
946 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
949 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
950 ret = -ETIMEDOUT; in joycon_hid_send_sync()
952 ret = 0; in joycon_hid_send_sync()
957 ctlr->received_resp = false; in joycon_hid_send_sync()
967 ctlr->usb_ack_match = cmd; in joycon_send_usb()
968 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
971 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
982 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_subcmd()
987 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_subcmd()
988 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
989 return -ENODEV; in joycon_send_subcmd()
991 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_subcmd()
993 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
995 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; in joycon_send_subcmd()
996 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
997 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
998 ctlr->subcmd_num = 0; in joycon_send_subcmd()
999 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
1000 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
1004 if (ret < 0) in joycon_send_subcmd()
1005 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
1007 ret = 0; in joycon_send_subcmd()
1015 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_set_player_leds()
1018 req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; in joycon_set_player_leds()
1019 req->data[0] = (flash << 4) | on; in joycon_set_player_leds()
1021 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
1028 u8 buffer[sizeof(*req) + 5] = { 0 }; in joycon_set_home_led()
1032 req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; in joycon_set_home_led()
1033 data = req->data; in joycon_set_home_led()
1034 data[0] = 0x01; in joycon_set_home_led()
1037 data[3] = 0x11; in joycon_set_home_led()
1038 data[4] = 0x11; in joycon_set_home_led()
1040 hid_dbg(ctlr->hdev, "setting home led brightness\n"); in joycon_set_home_led()
1049 u8 buffer[sizeof(*req) + 5] = { 0 }; in joycon_request_spi_flash_read()
1054 return -EINVAL; in joycon_request_spi_flash_read()
1057 req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; in joycon_request_spi_flash_read()
1058 data = req->data; in joycon_request_spi_flash_read()
1062 hid_dbg(ctlr->hdev, "requesting SPI flash data\n"); in joycon_request_spi_flash_read()
1065 hid_err(ctlr->hdev, "failed reading SPI flash; ret=%d\n", ret); in joycon_request_spi_flash_read()
1067 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_spi_flash_read()
1069 *reply = &report->subcmd_reply.data[5]; in joycon_request_spi_flash_read()
1076 * returns 0 if magic val is present, 1 if not present, < 0 on error
1088 return reply[0] != JC_CAL_USR_MAGIC_0 || reply[1] != JC_CAL_USR_MAGIC_1; in joycon_check_for_cal_magic()
1110 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1112 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1114 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1116 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1118 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1120 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1123 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1125 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1127 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1129 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1131 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1133 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1137 cal_x->max = cal_x->center + x_max_above; in joycon_read_stick_calibration()
1138 cal_x->min = cal_x->center - x_min_below; in joycon_read_stick_calibration()
1139 cal_y->max = cal_y->center + y_max_above; in joycon_read_stick_calibration()
1140 cal_y->min = cal_y->center - y_min_below; in joycon_read_stick_calibration()
1143 if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || in joycon_read_stick_calibration()
1144 cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) in joycon_read_stick_calibration()
1145 ret = -EINVAL; in joycon_read_stick_calibration()
1162 cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; in joycon_use_default_calibration()
1163 cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; in joycon_use_default_calibration()
1164 cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; in joycon_use_default_calibration()
1173 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
1178 hid_info(ctlr->hdev, "using user cal for left stick\n"); in joycon_request_calibration()
1180 hid_info(ctlr->hdev, "using factory cal for left stick\n"); in joycon_request_calibration()
1184 hid_info(ctlr->hdev, "using user cal for right stick\n"); in joycon_request_calibration()
1186 hid_info(ctlr->hdev, "using factory cal for right stick\n"); in joycon_request_calibration()
1191 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1192 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1196 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1197 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1198 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1203 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1204 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1208 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1209 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1210 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1213 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
1218 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
1219 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
1220 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
1221 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
1222 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
1223 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
1224 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
1225 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
1226 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
1227 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
1228 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
1229 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
1231 return 0; in joycon_request_calibration()
1241 int i, divz = 0; in joycon_calc_imu_cal_divisors()
1243 for (i = 0; i < 3; i++) { in joycon_calc_imu_cal_divisors()
1244 ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1245 ctlr->accel_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1246 ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1247 ctlr->gyro_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1249 if (ctlr->imu_cal_accel_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1250 ctlr->imu_cal_accel_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1254 if (ctlr->imu_cal_gyro_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1255 ctlr->imu_cal_gyro_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1261 hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz); in joycon_calc_imu_cal_divisors()
1264 static const s16 DFLT_ACCEL_OFFSET /*= 0*/;
1266 static const s16 DFLT_GYRO_OFFSET /*= 0*/;
1278 hid_info(ctlr->hdev, "using user cal for IMU\n"); in joycon_request_imu_calibration()
1280 hid_info(ctlr->hdev, "using factory cal for IMU\n"); in joycon_request_imu_calibration()
1284 hid_dbg(ctlr->hdev, "requesting IMU cal data\n"); in joycon_request_imu_calibration()
1288 hid_warn(ctlr->hdev, in joycon_request_imu_calibration()
1292 for (i = 0; i < 3; i++) { in joycon_request_imu_calibration()
1293 ctlr->accel_cal.offset[i] = DFLT_ACCEL_OFFSET; in joycon_request_imu_calibration()
1294 ctlr->accel_cal.scale[i] = DFLT_ACCEL_SCALE; in joycon_request_imu_calibration()
1295 ctlr->gyro_cal.offset[i] = DFLT_GYRO_OFFSET; in joycon_request_imu_calibration()
1296 ctlr->gyro_cal.scale[i] = DFLT_GYRO_SCALE; in joycon_request_imu_calibration()
1303 for (i = 0; i < 3; i++) { in joycon_request_imu_calibration()
1306 ctlr->accel_cal.offset[i] = get_unaligned_le16(raw_cal + j); in joycon_request_imu_calibration()
1307 ctlr->accel_cal.scale[i] = get_unaligned_le16(raw_cal + j + 6); in joycon_request_imu_calibration()
1308 ctlr->gyro_cal.offset[i] = get_unaligned_le16(raw_cal + j + 12); in joycon_request_imu_calibration()
1309 ctlr->gyro_cal.scale[i] = get_unaligned_le16(raw_cal + j + 18); in joycon_request_imu_calibration()
1314 hid_dbg(ctlr->hdev, "IMU calibration:\n" in joycon_request_imu_calibration()
1315 "a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" in joycon_request_imu_calibration()
1316 "a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" in joycon_request_imu_calibration()
1317 "g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" in joycon_request_imu_calibration()
1318 "g_s[0]=%d g_s[1]=%d g_s[2]=%d\n", in joycon_request_imu_calibration()
1319 ctlr->accel_cal.offset[0], in joycon_request_imu_calibration()
1320 ctlr->accel_cal.offset[1], in joycon_request_imu_calibration()
1321 ctlr->accel_cal.offset[2], in joycon_request_imu_calibration()
1322 ctlr->accel_cal.scale[0], in joycon_request_imu_calibration()
1323 ctlr->accel_cal.scale[1], in joycon_request_imu_calibration()
1324 ctlr->accel_cal.scale[2], in joycon_request_imu_calibration()
1325 ctlr->gyro_cal.offset[0], in joycon_request_imu_calibration()
1326 ctlr->gyro_cal.offset[1], in joycon_request_imu_calibration()
1327 ctlr->gyro_cal.offset[2], in joycon_request_imu_calibration()
1328 ctlr->gyro_cal.scale[0], in joycon_request_imu_calibration()
1329 ctlr->gyro_cal.scale[1], in joycon_request_imu_calibration()
1330 ctlr->gyro_cal.scale[2]); in joycon_request_imu_calibration()
1332 return 0; in joycon_request_imu_calibration()
1338 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_set_report_mode()
1341 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; in joycon_set_report_mode()
1342 req->data[0] = 0x30; /* standard, full report mode */ in joycon_set_report_mode()
1344 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
1351 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_enable_rumble()
1354 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; in joycon_enable_rumble()
1355 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_rumble()
1357 hid_dbg(ctlr->hdev, "enabling rumble\n"); in joycon_enable_rumble()
1364 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_enable_imu()
1367 req->subcmd_id = JC_SUBCMD_ENABLE_IMU; in joycon_enable_imu()
1368 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_imu()
1370 hid_dbg(ctlr->hdev, "enabling IMU\n"); in joycon_enable_imu()
1376 s32 center = cal->center; in joycon_map_stick_val()
1377 s32 min = cal->min; in joycon_map_stick_val()
1378 s32 max = cal->max; in joycon_map_stick_val()
1382 new_val = (val - center) * JC_MAX_STICK_MAG; in joycon_map_stick_val()
1383 new_val /= (max - center); in joycon_map_stick_val()
1385 new_val = (center - val) * -JC_MAX_STICK_MAG; in joycon_map_stick_val()
1386 new_val /= (center - min); in joycon_map_stick_val()
1388 new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG); in joycon_map_stick_val()
1396 u8 *raw = rep->imu_raw_bytes; in joycon_input_report_parse_imu_data()
1399 for (i = 0; i < 3; i++) { in joycon_input_report_parse_imu_data()
1402 data->accel_x = get_unaligned_le16(raw + 0); in joycon_input_report_parse_imu_data()
1403 data->accel_y = get_unaligned_le16(raw + 2); in joycon_input_report_parse_imu_data()
1404 data->accel_z = get_unaligned_le16(raw + 4); in joycon_input_report_parse_imu_data()
1405 data->gyro_x = get_unaligned_le16(raw + 6); in joycon_input_report_parse_imu_data()
1406 data->gyro_y = get_unaligned_le16(raw + 8); in joycon_input_report_parse_imu_data()
1407 data->gyro_z = get_unaligned_le16(raw + 10); in joycon_input_report_parse_imu_data()
1416 struct joycon_imu_data imu_data[3] = {0}; /* 3 reports per packet */ in joycon_parse_imu_report()
1417 struct input_dev *idev = ctlr->imu_input; in joycon_parse_imu_report()
1419 unsigned int last_msecs = ctlr->imu_last_pkt_ms; in joycon_parse_imu_report()
1429 * incrementing 8-bit counter per input report, but it is not very in joycon_parse_imu_report()
1431 * increments at or if it varies based on packet push rate - more on in joycon_parse_imu_report()
1434 * The reverse engineering work done on the joy-cons and pro controllers in joycon_parse_imu_report()
1436 * - The controller samples the IMU every 1.35ms. It then does some of in joycon_parse_imu_report()
1438 * - Each imu input report contains 3 IMU samples, (usually 5ms apart). in joycon_parse_imu_report()
1439 * - In the standard reporting mode (which this driver uses exclusively) in joycon_parse_imu_report()
1441 * * joy-con (bluetooth): every 15 ms in joycon_parse_imu_report()
1442 * * joy-cons (in charging grip via USB): every 15 ms in joycon_parse_imu_report()
1449 * SSR to 11ms for both the joy-cons and pro controllers). in joycon_parse_imu_report()
1472 if (!ctlr->imu_first_packet_received) { in joycon_parse_imu_report()
1473 ctlr->imu_timestamp_us = 0; in joycon_parse_imu_report()
1474 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1475 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1476 ctlr->imu_avg_delta_ms = JC_IMU_DFLT_AVG_DELTA_MS; in joycon_parse_imu_report()
1477 ctlr->imu_first_packet_received = true; in joycon_parse_imu_report()
1479 unsigned int delta = msecs - last_msecs; in joycon_parse_imu_report()
1484 ctlr->imu_delta_samples_sum += delta; in joycon_parse_imu_report()
1485 ctlr->imu_delta_samples_count++; in joycon_parse_imu_report()
1486 if (ctlr->imu_delta_samples_count >= in joycon_parse_imu_report()
1488 ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / in joycon_parse_imu_report()
1489 ctlr->imu_delta_samples_count; in joycon_parse_imu_report()
1490 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1491 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1495 if (ctlr->imu_avg_delta_ms == 0) { in joycon_parse_imu_report()
1496 ctlr->imu_avg_delta_ms = 1; in joycon_parse_imu_report()
1497 hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n"); in joycon_parse_imu_report()
1501 hid_dbg(ctlr->hdev, in joycon_parse_imu_report()
1503 msecs, last_msecs, delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1506 dropped_threshold = ctlr->imu_avg_delta_ms * 3 / 2; in joycon_parse_imu_report()
1507 dropped_pkts = (delta - min(delta, dropped_threshold)) / in joycon_parse_imu_report()
1508 ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1509 ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1511 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1514 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1516 delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1519 ctlr->imu_last_pkt_ms = msecs; in joycon_parse_imu_report()
1522 for (i = 0; i < 3; i++) { in joycon_parse_imu_report()
1524 ctlr->imu_timestamp_us); in joycon_parse_imu_report()
1529 * found in the community's reverse-engineering repo (linked at in joycon_parse_imu_report()
1530 * top of driver). For hid-nintendo, we make sure that the final in joycon_parse_imu_report()
1539 * precision-saving scaling factor to prevent large inaccuracies in joycon_parse_imu_report()
1544 value[0] = mult_frac((JC_IMU_PREC_RANGE_SCALE * in joycon_parse_imu_report()
1545 (imu_data[i].gyro_x - in joycon_parse_imu_report()
1546 ctlr->gyro_cal.offset[0])), in joycon_parse_imu_report()
1547 ctlr->gyro_cal.scale[0], in joycon_parse_imu_report()
1548 ctlr->imu_cal_gyro_divisor[0]); in joycon_parse_imu_report()
1550 (imu_data[i].gyro_y - in joycon_parse_imu_report()
1551 ctlr->gyro_cal.offset[1])), in joycon_parse_imu_report()
1552 ctlr->gyro_cal.scale[1], in joycon_parse_imu_report()
1553 ctlr->imu_cal_gyro_divisor[1]); in joycon_parse_imu_report()
1555 (imu_data[i].gyro_z - in joycon_parse_imu_report()
1556 ctlr->gyro_cal.offset[2])), in joycon_parse_imu_report()
1557 ctlr->gyro_cal.scale[2], in joycon_parse_imu_report()
1558 ctlr->imu_cal_gyro_divisor[2]); in joycon_parse_imu_report()
1561 ctlr->accel_cal.scale[0]) / in joycon_parse_imu_report()
1562 ctlr->imu_cal_accel_divisor[0]; in joycon_parse_imu_report()
1564 ctlr->accel_cal.scale[1]) / in joycon_parse_imu_report()
1565 ctlr->imu_cal_accel_divisor[1]; in joycon_parse_imu_report()
1567 ctlr->accel_cal.scale[2]) / in joycon_parse_imu_report()
1568 ctlr->imu_cal_accel_divisor[2]; in joycon_parse_imu_report()
1570 hid_dbg(ctlr->hdev, "raw_gyro: g_x=%d g_y=%d g_z=%d\n", in joycon_parse_imu_report()
1573 hid_dbg(ctlr->hdev, "raw_accel: a_x=%d a_y=%d a_z=%d\n", in joycon_parse_imu_report()
1578 * The right joy-con has 2 axes negated, Y and Z. This is due to in joycon_parse_imu_report()
1580 * axes' values in order to be consistent with the left joy-con in joycon_parse_imu_report()
1585 * The axes follow the right-hand rule. in joycon_parse_imu_report()
1594 value[j] *= -1; in joycon_parse_imu_report()
1598 input_report_abs(idev, ABS_RX, value[0]); in joycon_parse_imu_report()
1606 ctlr->imu_timestamp_us += ctlr->imu_avg_delta_ms * 1000 / 3; in joycon_parse_imu_report()
1615 spin_lock_irqsave(&ctlr->lock, flags); in joycon_handle_rumble_report()
1616 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report && in joycon_handle_rumble_report()
1617 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED && in joycon_handle_rumble_report()
1618 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS && in joycon_handle_rumble_report()
1619 (ctlr->rumble_queue_head != ctlr->rumble_queue_tail || in joycon_handle_rumble_report()
1620 ctlr->rumble_zero_countdown > 0)) { in joycon_handle_rumble_report()
1622 * When this value reaches 0, we know we've sent multiple in joycon_handle_rumble_report()
1627 if (ctlr->rumble_zero_countdown > 0) in joycon_handle_rumble_report()
1628 ctlr->rumble_zero_countdown--; in joycon_handle_rumble_report()
1629 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_handle_rumble_report()
1632 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_handle_rumble_report()
1640 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_battery_status()
1642 tmp = rep->bat_con; in joycon_parse_battery_status()
1643 ctlr->host_powered = tmp & BIT(0); in joycon_parse_battery_status()
1644 ctlr->battery_charging = tmp & BIT(4); in joycon_parse_battery_status()
1648 case 0: /* empty */ in joycon_parse_battery_status()
1649 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; in joycon_parse_battery_status()
1652 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; in joycon_parse_battery_status()
1655 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; in joycon_parse_battery_status()
1658 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; in joycon_parse_battery_status()
1661 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; in joycon_parse_battery_status()
1664 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_parse_battery_status()
1665 hid_warn(ctlr->hdev, "Invalid battery status\n"); in joycon_parse_battery_status()
1669 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_battery_status()
1680 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_report_left_stick()
1681 raw_y = hid_field_extract(ctlr->hdev, rep->left_stick + 1, 4, 12); in joycon_report_left_stick()
1683 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_report_left_stick()
1684 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_report_left_stick()
1686 input_report_abs(ctlr->input, ABS_X, x); in joycon_report_left_stick()
1687 input_report_abs(ctlr->input, ABS_Y, y); in joycon_report_left_stick()
1698 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_report_right_stick()
1699 raw_y = hid_field_extract(ctlr->hdev, rep->right_stick + 1, 4, 12); in joycon_report_right_stick()
1701 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_report_right_stick()
1702 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_report_right_stick()
1704 input_report_abs(ctlr->input, ABS_RX, x); in joycon_report_right_stick()
1705 input_report_abs(ctlr->input, ABS_RY, y); in joycon_report_right_stick()
1711 int hatx = 0; in joycon_report_dpad()
1712 int haty = 0; in joycon_report_dpad()
1713 u32 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_dpad()
1716 hatx = -1; in joycon_report_dpad()
1721 haty = -1; in joycon_report_dpad()
1725 input_report_abs(ctlr->input, ABS_HAT0X, hatx); in joycon_report_dpad()
1726 input_report_abs(ctlr->input, ABS_HAT0Y, haty); in joycon_report_dpad()
1734 u32 status = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_buttons()
1736 for (button = button_mappings; button->code; button++) in joycon_report_buttons()
1737 input_report_key(ctlr->input, button->code, status & button->bit); in joycon_report_buttons()
1745 unsigned long report_delta_ms = msecs - ctlr->last_input_report_msecs; in joycon_parse_report()
1782 input_sync(ctlr->input); in joycon_parse_report()
1784 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1785 ctlr->last_input_report_msecs = msecs; in joycon_parse_report()
1793 if (ctlr->consecutive_valid_report_deltas < JC_SUBCMD_VALID_DELTA_REQ) in joycon_parse_report()
1794 ctlr->consecutive_valid_report_deltas++; in joycon_parse_report()
1796 ctlr->consecutive_valid_report_deltas = 0; in joycon_parse_report()
1800 * bluetooth-connected controllers. For USB devices, we're beholden to in joycon_parse_report()
1804 if (ctlr->hdev->bus == BUS_USB) in joycon_parse_report()
1805 ctlr->consecutive_valid_report_deltas = JC_SUBCMD_VALID_DELTA_REQ; in joycon_parse_report()
1807 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1814 if (unlikely(mutex_is_locked(&ctlr->output_mutex))) { in joycon_parse_report()
1815 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1816 ctlr->received_input_report = true; in joycon_parse_report()
1817 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1818 wake_up(&ctlr->wait); in joycon_parse_report()
1822 if ((rep->id == JC_INPUT_IMU_DATA) && joycon_has_imu(ctlr)) in joycon_parse_report()
1830 struct joycon_rumble_output rumble_output = { 0 }; in joycon_send_rumble_data()
1832 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_rumble_data()
1837 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_rumble_data()
1838 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1839 return -ENODEV; in joycon_send_rumble_data()
1842 ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_rumble_data()
1844 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1847 rumble_output.packet_num = ctlr->subcmd_num; in joycon_send_rumble_data()
1848 if (++ctlr->subcmd_num > 0xF) in joycon_send_rumble_data()
1849 ctlr->subcmd_num = 0; in joycon_send_rumble_data()
1853 ret = __joycon_hid_send(ctlr->hdev, (u8 *)&rumble_output, in joycon_send_rumble_data()
1867 mutex_lock(&ctlr->output_mutex); in joycon_rumble_worker()
1869 mutex_unlock(&ctlr->output_mutex); in joycon_rumble_worker()
1871 /* -ENODEV means the controller was just unplugged */ in joycon_rumble_worker()
1872 spin_lock_irqsave(&ctlr->lock, flags); in joycon_rumble_worker()
1873 if (ret < 0 && ret != -ENODEV && in joycon_rumble_worker()
1874 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_rumble_worker()
1875 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret); in joycon_rumble_worker()
1877 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_rumble_worker()
1878 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) { in joycon_rumble_worker()
1879 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE) in joycon_rumble_worker()
1880 ctlr->rumble_queue_tail = 0; in joycon_rumble_worker()
1884 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_rumble_worker()
1893 int i = 0; in joycon_find_rumble_freq()
1895 if (freq > data[0].freq) { in joycon_find_rumble_freq()
1896 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_freq()
1897 if (freq > data[i - 1].freq && freq <= data[i].freq) in joycon_find_rumble_freq()
1909 int i = 0; in joycon_find_rumble_amp()
1911 if (amp > data[0].amp) { in joycon_find_rumble_amp()
1912 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_amp()
1913 if (amp > data[i - 1].amp && amp <= data[i].amp) in joycon_find_rumble_amp()
1931 data[0] = (freq_data_high.high >> 8) & 0xFF; in joycon_encode_rumble()
1932 data[1] = (freq_data_high.high & 0xFF) + amp_data.high; in joycon_encode_rumble()
1933 data[2] = freq_data_low.low + ((amp_data.low >> 8) & 0xFF); in joycon_encode_rumble()
1934 data[3] = amp_data.low & 0xFF; in joycon_encode_rumble()
1946 spin_lock_irqsave(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1947 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, in joycon_clamp_rumble_freqs()
1950 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, in joycon_clamp_rumble_freqs()
1953 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, in joycon_clamp_rumble_freqs()
1956 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, in joycon_clamp_rumble_freqs()
1959 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1974 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1975 freq_r_low = ctlr->rumble_rl_freq; in joycon_set_rumble()
1976 freq_r_high = ctlr->rumble_rh_freq; in joycon_set_rumble()
1977 freq_l_low = ctlr->rumble_ll_freq; in joycon_set_rumble()
1978 freq_l_high = ctlr->rumble_lh_freq; in joycon_set_rumble()
1980 if (amp_l != 0 || amp_r != 0) in joycon_set_rumble()
1981 ctlr->rumble_zero_countdown = JC_RUMBLE_ZERO_AMP_PKT_CNT; in joycon_set_rumble()
1982 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1984 /* right joy-con */ in joycon_set_rumble()
1988 /* left joy-con */ in joycon_set_rumble()
1992 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1994 next_rq_head = ctlr->rumble_queue_head + 1; in joycon_set_rumble()
1996 next_rq_head = 0; in joycon_set_rumble()
2001 if (next_rq_head == ctlr->rumble_queue_tail) { in joycon_set_rumble()
2002 hid_dbg(ctlr->hdev, "rumble queue is full"); in joycon_set_rumble()
2004 next_rq_head = ctlr->rumble_queue_head; in joycon_set_rumble()
2007 ctlr->rumble_queue_head = next_rq_head; in joycon_set_rumble()
2008 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, in joycon_set_rumble()
2012 if (schedule_now && ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_set_rumble()
2013 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_set_rumble()
2015 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
2017 return 0; in joycon_set_rumble()
2025 if (effect->type != FF_RUMBLE) in joycon_play_effect()
2026 return 0; in joycon_play_effect()
2029 effect->u.rumble.weak_magnitude, in joycon_play_effect()
2030 effect->u.rumble.strong_magnitude, in joycon_play_effect()
2039 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
2045 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
2055 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2061 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2071 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2077 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2088 for (button = button_mappings; button->code; button++) in joycon_config_buttons()
2089 input_set_capability(idev, EV_KEY, button->code); in joycon_config_buttons()
2096 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); in joycon_config_rumble()
2097 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); in joycon_config_rumble()
2098 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2099 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2100 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2101 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2103 joycon_set_rumble(ctlr, 0, 0, false); in joycon_config_rumble()
2104 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_config_rumble()
2114 hdev = ctlr->hdev; in joycon_imu_input_create()
2117 ctlr->imu_input = devm_input_allocate_device(&hdev->dev); in joycon_imu_input_create()
2118 if (!ctlr->imu_input) in joycon_imu_input_create()
2119 return -ENOMEM; in joycon_imu_input_create()
2121 ctlr->imu_input->id.bustype = hdev->bus; in joycon_imu_input_create()
2122 ctlr->imu_input->id.vendor = hdev->vendor; in joycon_imu_input_create()
2123 ctlr->imu_input->id.product = hdev->product; in joycon_imu_input_create()
2124 ctlr->imu_input->id.version = hdev->version; in joycon_imu_input_create()
2125 ctlr->imu_input->uniq = ctlr->mac_addr_str; in joycon_imu_input_create()
2126 ctlr->imu_input->phys = hdev->phys; in joycon_imu_input_create()
2128 imu_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s (IMU)", ctlr->input->name); in joycon_imu_input_create()
2130 return -ENOMEM; in joycon_imu_input_create()
2132 ctlr->imu_input->name = imu_name; in joycon_imu_input_create()
2134 input_set_drvdata(ctlr->imu_input, ctlr); in joycon_imu_input_create()
2137 input_set_abs_params(ctlr->imu_input, ABS_X, in joycon_imu_input_create()
2138 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2140 input_set_abs_params(ctlr->imu_input, ABS_Y, in joycon_imu_input_create()
2141 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2143 input_set_abs_params(ctlr->imu_input, ABS_Z, in joycon_imu_input_create()
2144 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2146 input_abs_set_res(ctlr->imu_input, ABS_X, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2147 input_abs_set_res(ctlr->imu_input, ABS_Y, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2148 input_abs_set_res(ctlr->imu_input, ABS_Z, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2150 input_set_abs_params(ctlr->imu_input, ABS_RX, in joycon_imu_input_create()
2151 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2153 input_set_abs_params(ctlr->imu_input, ABS_RY, in joycon_imu_input_create()
2154 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2156 input_set_abs_params(ctlr->imu_input, ABS_RZ, in joycon_imu_input_create()
2157 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2160 input_abs_set_res(ctlr->imu_input, ABS_RX, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2161 input_abs_set_res(ctlr->imu_input, ABS_RY, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2162 input_abs_set_res(ctlr->imu_input, ABS_RZ, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2164 __set_bit(EV_MSC, ctlr->imu_input->evbit); in joycon_imu_input_create()
2165 __set_bit(MSC_TIMESTAMP, ctlr->imu_input->mscbit); in joycon_imu_input_create()
2166 __set_bit(INPUT_PROP_ACCELEROMETER, ctlr->imu_input->propbit); in joycon_imu_input_create()
2168 ret = input_register_device(ctlr->imu_input); in joycon_imu_input_create()
2172 return 0; in joycon_imu_input_create()
2180 hdev = ctlr->hdev; in joycon_input_create()
2182 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
2183 if (!ctlr->input) in joycon_input_create()
2184 return -ENOMEM; in joycon_input_create()
2185 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
2186 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
2187 ctlr->input->id.product = hdev->product; in joycon_input_create()
2188 ctlr->input->id.version = hdev->version; in joycon_input_create()
2189 ctlr->input->uniq = ctlr->mac_addr_str; in joycon_input_create()
2190 ctlr->input->name = hdev->name; in joycon_input_create()
2191 ctlr->input->phys = hdev->phys; in joycon_input_create()
2192 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
2194 ret = input_register_device(ctlr->input); in joycon_input_create()
2199 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2200 joycon_config_buttons(ctlr->input, right_joycon_button_mappings); in joycon_input_create()
2202 joycon_config_buttons(ctlr->input, right_joycon_s_button_mappings); in joycon_input_create()
2204 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2205 joycon_config_buttons(ctlr->input, left_joycon_button_mappings); in joycon_input_create()
2207 joycon_config_buttons(ctlr->input, left_joycon_s_button_mappings); in joycon_input_create()
2209 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2210 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2211 joycon_config_dpad(ctlr->input); in joycon_input_create()
2212 joycon_config_buttons(ctlr->input, procon_button_mappings); in joycon_input_create()
2214 joycon_config_dpad(ctlr->input); in joycon_input_create()
2215 joycon_config_buttons(ctlr->input, nescon_button_mappings); in joycon_input_create()
2217 joycon_config_dpad(ctlr->input); in joycon_input_create()
2218 joycon_config_buttons(ctlr->input, snescon_button_mappings); in joycon_input_create()
2220 joycon_config_dpad(ctlr->input); in joycon_input_create()
2221 joycon_config_buttons(ctlr->input, gencon_button_mappings); in joycon_input_create()
2223 joycon_config_dpad(ctlr->input); in joycon_input_create()
2224 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2225 joycon_config_buttons(ctlr->input, n64con_button_mappings); in joycon_input_create()
2237 return 0; in joycon_input_create()
2244 struct device *dev = led->dev->parent; in joycon_player_led_brightness_set()
2247 int val = 0; in joycon_player_led_brightness_set()
2254 return -ENODEV; in joycon_player_led_brightness_set()
2257 for (i = 0; i < JC_NUM_LEDS; i++) in joycon_player_led_brightness_set()
2258 val |= ctlr->leds[i].brightness << i; in joycon_player_led_brightness_set()
2260 mutex_lock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2261 ret = joycon_set_player_leds(ctlr, 0, val); in joycon_player_led_brightness_set()
2262 mutex_unlock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2270 struct device *dev = led->dev->parent; in joycon_home_led_brightness_set()
2278 return -ENODEV; in joycon_home_led_brightness_set()
2280 mutex_lock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2282 mutex_unlock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2289 struct hid_device *hdev = ctlr->hdev; in joycon_leds_create()
2290 struct device *dev = &hdev->dev; in joycon_leds_create()
2293 int led_val = 0; in joycon_leds_create()
2311 for (i = 0; i < JC_NUM_LEDS; i++) { in joycon_leds_create()
2317 return -ENOMEM; in joycon_leds_create()
2319 led = &ctlr->leds[i]; in joycon_leds_create()
2320 led->name = name; in joycon_leds_create()
2321 led->brightness = joycon_player_led_patterns[player_led_pattern][i]; in joycon_leds_create()
2322 led->max_brightness = 1; in joycon_leds_create()
2323 led->brightness_set_blocking = in joycon_leds_create()
2325 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2329 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2330 ret = joycon_set_player_leds(ctlr, 0, led_val); in joycon_leds_create()
2331 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2337 for (i = 0; i < JC_NUM_LEDS; i++) { in joycon_leds_create()
2338 led = &ctlr->leds[i]; in joycon_leds_create()
2339 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2354 return -ENOMEM; in joycon_leds_create()
2356 led = &ctlr->home_led; in joycon_leds_create()
2357 led->name = name; in joycon_leds_create()
2358 led->brightness = 0; in joycon_leds_create()
2359 led->max_brightness = 0xF; in joycon_leds_create()
2360 led->brightness_set_blocking = joycon_home_led_brightness_set; in joycon_leds_create()
2361 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2363 /* Set the home LED to 0 as default state */ in joycon_leds_create()
2364 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2365 ret = joycon_set_home_led(ctlr, 0); in joycon_leds_create()
2366 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2369 return 0; in joycon_leds_create()
2372 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2379 return 0; in joycon_leds_create()
2388 int ret = 0; in joycon_battery_get_property()
2393 spin_lock_irqsave(&ctlr->lock, flags); in joycon_battery_get_property()
2394 capacity = ctlr->battery_capacity; in joycon_battery_get_property()
2395 charging = ctlr->battery_charging; in joycon_battery_get_property()
2396 powered = ctlr->host_powered; in joycon_battery_get_property()
2397 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_battery_get_property()
2401 val->intval = 1; in joycon_battery_get_property()
2404 val->intval = POWER_SUPPLY_SCOPE_DEVICE; in joycon_battery_get_property()
2407 val->intval = capacity; in joycon_battery_get_property()
2411 val->intval = POWER_SUPPLY_STATUS_CHARGING; in joycon_battery_get_property()
2414 val->intval = POWER_SUPPLY_STATUS_FULL; in joycon_battery_get_property()
2416 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in joycon_battery_get_property()
2419 ret = -EINVAL; in joycon_battery_get_property()
2434 struct hid_device *hdev = ctlr->hdev; in joycon_power_supply_create()
2437 int ret = 0; in joycon_power_supply_create()
2440 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_power_supply_create()
2443 ctlr->battery_desc.properties = joycon_battery_props; in joycon_power_supply_create()
2444 ctlr->battery_desc.num_properties = in joycon_power_supply_create()
2446 ctlr->battery_desc.get_property = joycon_battery_get_property; in joycon_power_supply_create()
2447 ctlr->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; in joycon_power_supply_create()
2448 ctlr->battery_desc.use_for_apm = 0; in joycon_power_supply_create()
2449 ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, in joycon_power_supply_create()
2451 dev_name(&hdev->dev)); in joycon_power_supply_create()
2452 if (!ctlr->battery_desc.name) in joycon_power_supply_create()
2453 return -ENOMEM; in joycon_power_supply_create()
2455 ctlr->battery = devm_power_supply_register(&hdev->dev, in joycon_power_supply_create()
2456 &ctlr->battery_desc, in joycon_power_supply_create()
2458 if (IS_ERR(ctlr->battery)) { in joycon_power_supply_create()
2459 ret = PTR_ERR(ctlr->battery); in joycon_power_supply_create()
2464 return power_supply_powers(ctlr->battery, &hdev->dev); in joycon_power_supply_create()
2472 struct joycon_subcmd_request req = { 0 }; in joycon_read_info()
2476 ret = joycon_send_subcmd(ctlr, &req, 0, HZ); in joycon_read_info()
2478 hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); in joycon_read_info()
2482 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_read_info()
2484 for (i = 4, j = 0; j < 6; i++, j++) in joycon_read_info()
2485 ctlr->mac_addr[j] = report->subcmd_reply.data[i]; in joycon_read_info()
2487 ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, in joycon_read_info()
2489 ctlr->mac_addr[0], in joycon_read_info()
2490 ctlr->mac_addr[1], in joycon_read_info()
2491 ctlr->mac_addr[2], in joycon_read_info()
2492 ctlr->mac_addr[3], in joycon_read_info()
2493 ctlr->mac_addr[4], in joycon_read_info()
2494 ctlr->mac_addr[5]); in joycon_read_info()
2495 if (!ctlr->mac_addr_str) in joycon_read_info()
2496 return -ENOMEM; in joycon_read_info()
2497 hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); in joycon_read_info()
2501 * Unfortantly the hdev->product can't always be used due to a ?bug? in joycon_read_info()
2503 * PID as 0x201E, but over bluetooth it will report the PID as 0x2017 in joycon_read_info()
2508 ctlr->ctlr_type = report->subcmd_reply.data[2]; in joycon_read_info()
2509 hid_dbg(ctlr->hdev, "controller type = 0x%02X\n", ctlr->ctlr_type); in joycon_read_info()
2511 return 0; in joycon_read_info()
2517 int ret = 0; in joycon_init()
2519 mutex_lock(&ctlr->output_mutex); in joycon_init()
2542 ret = -ETIMEDOUT; in joycon_init()
2585 /* Set the reporting mode to 0x30, which is the full report mode */ in joycon_init()
2602 mutex_unlock(&ctlr->output_mutex); in joycon_init()
2610 if (data[0] == JC_INPUT_SUBCMD_REPLY || data[0] == JC_INPUT_IMU_DATA || in joycon_ctlr_read_handler()
2611 data[0] == JC_INPUT_MCU_DATA) { in joycon_ctlr_read_handler()
2617 return 0; in joycon_ctlr_read_handler()
2623 int ret = 0; in joycon_ctlr_handle_event()
2627 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
2628 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
2629 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
2633 if (data[0] == JC_INPUT_USB_RESPONSE && in joycon_ctlr_handle_event()
2634 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
2639 data[0] != JC_INPUT_SUBCMD_REPLY) in joycon_ctlr_handle_event()
2642 if (report->subcmd_reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
2650 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
2652 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
2653 ctlr->received_resp = true; in joycon_ctlr_handle_event()
2654 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
2661 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
2673 return -EINVAL; in nintendo_hid_event()
2684 hid_dbg(hdev, "probe - start\n"); in nintendo_hid_probe()
2686 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
2688 ret = -ENOMEM; in nintendo_hid_probe()
2692 ctlr->hdev = hdev; in nintendo_hid_probe()
2693 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
2694 ctlr->rumble_queue_head = 0; in nintendo_hid_probe()
2695 ctlr->rumble_queue_tail = 0; in nintendo_hid_probe()
2697 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
2698 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
2699 spin_lock_init(&ctlr->lock); in nintendo_hid_probe()
2700 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", in nintendo_hid_probe()
2701 WQ_FREEZABLE | WQ_MEM_RECLAIM, 0); in nintendo_hid_probe()
2702 if (!ctlr->rumble_queue) { in nintendo_hid_probe()
2703 ret = -ENOMEM; in nintendo_hid_probe()
2706 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); in nintendo_hid_probe()
2721 hdev->version |= 0x8000; in nintendo_hid_probe()
2763 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
2765 hid_dbg(hdev, "probe - success\n"); in nintendo_hid_probe()
2766 return 0; in nintendo_hid_probe()
2773 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_probe()
2775 hid_err(hdev, "probe - fail = %d\n", ret); in nintendo_hid_probe()
2787 spin_lock_irqsave(&ctlr->lock, flags); in nintendo_hid_remove()
2788 ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED; in nintendo_hid_remove()
2789 spin_unlock_irqrestore(&ctlr->lock, flags); in nintendo_hid_remove()
2791 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_remove()