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"
51 #define JC_OUTPUT_RUMBLE_AND_SUBCMD 0x01
52 #define JC_OUTPUT_FW_UPDATE_PKT 0x03
53 #define JC_OUTPUT_RUMBLE_ONLY 0x10
54 #define JC_OUTPUT_MCU_DATA 0x11
55 #define JC_OUTPUT_USB_CMD 0x80
58 #define JC_SUBCMD_STATE 0x00
59 #define JC_SUBCMD_MANUAL_BT_PAIRING 0x01
60 #define JC_SUBCMD_REQ_DEV_INFO 0x02
61 #define JC_SUBCMD_SET_REPORT_MODE 0x03
62 #define JC_SUBCMD_TRIGGERS_ELAPSED 0x04
63 #define JC_SUBCMD_GET_PAGE_LIST_STATE 0x05
64 #define JC_SUBCMD_SET_HCI_STATE 0x06
65 #define JC_SUBCMD_RESET_PAIRING_INFO 0x07
66 #define JC_SUBCMD_LOW_POWER_MODE 0x08
67 #define JC_SUBCMD_SPI_FLASH_READ 0x10
68 #define JC_SUBCMD_SPI_FLASH_WRITE 0x11
69 #define JC_SUBCMD_RESET_MCU 0x20
70 #define JC_SUBCMD_SET_MCU_CONFIG 0x21
71 #define JC_SUBCMD_SET_MCU_STATE 0x22
72 #define JC_SUBCMD_SET_PLAYER_LIGHTS 0x30
73 #define JC_SUBCMD_GET_PLAYER_LIGHTS 0x31
74 #define JC_SUBCMD_SET_HOME_LIGHT 0x38
75 #define JC_SUBCMD_ENABLE_IMU 0x40
76 #define JC_SUBCMD_SET_IMU_SENSITIVITY 0x41
77 #define JC_SUBCMD_WRITE_IMU_REG 0x42
78 #define JC_SUBCMD_READ_IMU_REG 0x43
79 #define JC_SUBCMD_ENABLE_VIBRATION 0x48
80 #define JC_SUBCMD_GET_REGULATED_VOLTAGE 0x50
83 #define JC_INPUT_BUTTON_EVENT 0x3F
84 #define JC_INPUT_SUBCMD_REPLY 0x21
85 #define JC_INPUT_IMU_DATA 0x30
86 #define JC_INPUT_MCU_DATA 0x31
87 #define JC_INPUT_USB_RESPONSE 0x81
90 #define JC_FEATURE_LAST_SUBCMD 0x02
91 #define JC_FEATURE_OTA_FW_UPGRADE 0x70
92 #define JC_FEATURE_SETUP_MEM_READ 0x71
93 #define JC_FEATURE_MEM_READ 0x72
94 #define JC_FEATURE_ERASE_MEM_SECTOR 0x73
95 #define JC_FEATURE_MEM_WRITE 0x74
96 #define JC_FEATURE_LAUNCH 0x75
99 #define JC_USB_CMD_CONN_STATUS 0x01
100 #define JC_USB_CMD_HANDSHAKE 0x02
101 #define JC_USB_CMD_BAUDRATE_3M 0x03
102 #define JC_USB_CMD_NO_TIMEOUT 0x04
103 #define JC_USB_CMD_EN_TIMEOUT 0x05
104 #define JC_USB_RESET 0x06
105 #define JC_USB_PRE_HANDSHAKE 0x91
106 #define JC_USB_SEND_UART 0x92
109 #define JC_CAL_USR_MAGIC_0 0xB2
110 #define JC_CAL_USR_MAGIC_1 0xA1
114 #define JC_CAL_USR_LEFT_MAGIC_ADDR 0x8010
115 #define JC_CAL_USR_LEFT_DATA_ADDR 0x8012
116 #define JC_CAL_USR_LEFT_DATA_END 0x801A
117 #define JC_CAL_USR_RIGHT_MAGIC_ADDR 0x801B
118 #define JC_CAL_USR_RIGHT_DATA_ADDR 0x801D
120 (JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
123 #define JC_CAL_FCT_DATA_LEFT_ADDR 0x603d
124 #define JC_CAL_FCT_DATA_RIGHT_ADDR 0x6046
127 #define JC_IMU_CAL_FCT_DATA_ADDR 0x6020
128 #define JC_IMU_CAL_FCT_DATA_END 0x6037
130 (JC_IMU_CAL_FCT_DATA_END - JC_IMU_CAL_FCT_DATA_ADDR + 1)
132 #define JC_IMU_CAL_USR_MAGIC_ADDR 0x8026
133 #define JC_IMU_CAL_USR_DATA_ADDR 0x8028
140 /* Hat values for pro controller's d-pad */
142 #define JC_DPAD_FUZZ 0
143 #define JC_DPAD_FLAT 0
154 * configured with a range of +-8000 milliGs. Therefore, the resolution can be
155 * calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
162 #define JC_IMU_ACCEL_FLAT 0
166 * configured with a range of +-2000 degrees/second.
167 * Digits per dps: (2^16 -1)/(2000*2) = 16.38375
168 * dps per digit: 16.38375E-1 = .0610
174 * digits per dps (corrected): .0702E-1 = 14.247
181 #define JC_IMU_MAX_GYRO_MAG 32767000 /* (2^16-1)*1000 */
184 #define JC_IMU_GYRO_FLAT 0
206 { 0x0000, 0x01, 41 }, { 0x0000, 0x02, 42 }, { 0x0000, 0x03, 43 },
207 { 0x0000, 0x04, 44 }, { 0x0000, 0x05, 45 }, { 0x0000, 0x06, 46 },
208 { 0x0000, 0x07, 47 }, { 0x0000, 0x08, 48 }, { 0x0000, 0x09, 49 },
209 { 0x0000, 0x0A, 50 }, { 0x0000, 0x0B, 51 }, { 0x0000, 0x0C, 52 },
210 { 0x0000, 0x0D, 53 }, { 0x0000, 0x0E, 54 }, { 0x0000, 0x0F, 55 },
211 { 0x0000, 0x10, 57 }, { 0x0000, 0x11, 58 }, { 0x0000, 0x12, 59 },
212 { 0x0000, 0x13, 60 }, { 0x0000, 0x14, 62 }, { 0x0000, 0x15, 63 },
213 { 0x0000, 0x16, 64 }, { 0x0000, 0x17, 66 }, { 0x0000, 0x18, 67 },
214 { 0x0000, 0x19, 69 }, { 0x0000, 0x1A, 70 }, { 0x0000, 0x1B, 72 },
215 { 0x0000, 0x1C, 73 }, { 0x0000, 0x1D, 75 }, { 0x0000, 0x1e, 77 },
216 { 0x0000, 0x1f, 78 }, { 0x0000, 0x20, 80 }, { 0x0400, 0x21, 82 },
217 { 0x0800, 0x22, 84 }, { 0x0c00, 0x23, 85 }, { 0x1000, 0x24, 87 },
218 { 0x1400, 0x25, 89 }, { 0x1800, 0x26, 91 }, { 0x1c00, 0x27, 93 },
219 { 0x2000, 0x28, 95 }, { 0x2400, 0x29, 97 }, { 0x2800, 0x2a, 99 },
220 { 0x2c00, 0x2b, 102 }, { 0x3000, 0x2c, 104 }, { 0x3400, 0x2d, 106 },
221 { 0x3800, 0x2e, 108 }, { 0x3c00, 0x2f, 111 }, { 0x4000, 0x30, 113 },
222 { 0x4400, 0x31, 116 }, { 0x4800, 0x32, 118 }, { 0x4c00, 0x33, 121 },
223 { 0x5000, 0x34, 123 }, { 0x5400, 0x35, 126 }, { 0x5800, 0x36, 129 },
224 { 0x5c00, 0x37, 132 }, { 0x6000, 0x38, 135 }, { 0x6400, 0x39, 137 },
225 { 0x6800, 0x3a, 141 }, { 0x6c00, 0x3b, 144 }, { 0x7000, 0x3c, 147 },
226 { 0x7400, 0x3d, 150 }, { 0x7800, 0x3e, 153 }, { 0x7c00, 0x3f, 157 },
227 { 0x8000, 0x40, 160 }, { 0x8400, 0x41, 164 }, { 0x8800, 0x42, 167 },
228 { 0x8c00, 0x43, 171 }, { 0x9000, 0x44, 174 }, { 0x9400, 0x45, 178 },
229 { 0x9800, 0x46, 182 }, { 0x9c00, 0x47, 186 }, { 0xa000, 0x48, 190 },
230 { 0xa400, 0x49, 194 }, { 0xa800, 0x4a, 199 }, { 0xac00, 0x4b, 203 },
231 { 0xb000, 0x4c, 207 }, { 0xb400, 0x4d, 212 }, { 0xb800, 0x4e, 217 },
232 { 0xbc00, 0x4f, 221 }, { 0xc000, 0x50, 226 }, { 0xc400, 0x51, 231 },
233 { 0xc800, 0x52, 236 }, { 0xcc00, 0x53, 241 }, { 0xd000, 0x54, 247 },
234 { 0xd400, 0x55, 252 }, { 0xd800, 0x56, 258 }, { 0xdc00, 0x57, 263 },
235 { 0xe000, 0x58, 269 }, { 0xe400, 0x59, 275 }, { 0xe800, 0x5a, 281 },
236 { 0xec00, 0x5b, 287 }, { 0xf000, 0x5c, 293 }, { 0xf400, 0x5d, 300 },
237 { 0xf800, 0x5e, 306 }, { 0xfc00, 0x5f, 313 }, { 0x0001, 0x60, 320 },
238 { 0x0401, 0x61, 327 }, { 0x0801, 0x62, 334 }, { 0x0c01, 0x63, 341 },
239 { 0x1001, 0x64, 349 }, { 0x1401, 0x65, 357 }, { 0x1801, 0x66, 364 },
240 { 0x1c01, 0x67, 372 }, { 0x2001, 0x68, 381 }, { 0x2401, 0x69, 389 },
241 { 0x2801, 0x6a, 397 }, { 0x2c01, 0x6b, 406 }, { 0x3001, 0x6c, 415 },
242 { 0x3401, 0x6d, 424 }, { 0x3801, 0x6e, 433 }, { 0x3c01, 0x6f, 443 },
243 { 0x4001, 0x70, 453 }, { 0x4401, 0x71, 462 }, { 0x4801, 0x72, 473 },
244 { 0x4c01, 0x73, 483 }, { 0x5001, 0x74, 494 }, { 0x5401, 0x75, 504 },
245 { 0x5801, 0x76, 515 }, { 0x5c01, 0x77, 527 }, { 0x6001, 0x78, 538 },
246 { 0x6401, 0x79, 550 }, { 0x6801, 0x7a, 562 }, { 0x6c01, 0x7b, 574 },
247 { 0x7001, 0x7c, 587 }, { 0x7401, 0x7d, 600 }, { 0x7801, 0x7e, 613 },
248 { 0x7c01, 0x7f, 626 }, { 0x8001, 0x00, 640 }, { 0x8401, 0x00, 654 },
249 { 0x8801, 0x00, 668 }, { 0x8c01, 0x00, 683 }, { 0x9001, 0x00, 698 },
250 { 0x9401, 0x00, 713 }, { 0x9801, 0x00, 729 }, { 0x9c01, 0x00, 745 },
251 { 0xa001, 0x00, 761 }, { 0xa401, 0x00, 778 }, { 0xa801, 0x00, 795 },
252 { 0xac01, 0x00, 812 }, { 0xb001, 0x00, 830 }, { 0xb401, 0x00, 848 },
253 { 0xb801, 0x00, 867 }, { 0xbc01, 0x00, 886 }, { 0xc001, 0x00, 905 },
254 { 0xc401, 0x00, 925 }, { 0xc801, 0x00, 945 }, { 0xcc01, 0x00, 966 },
255 { 0xd001, 0x00, 987 }, { 0xd401, 0x00, 1009 }, { 0xd801, 0x00, 1031 },
256 { 0xdc01, 0x00, 1053 }, { 0xe001, 0x00, 1076 }, { 0xe401, 0x00, 1100 },
257 { 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 },
258 { 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 }
264 { 0x00, 0x0040, 0 },
265 { 0x02, 0x8040, 10 }, { 0x04, 0x0041, 12 }, { 0x06, 0x8041, 14 },
266 { 0x08, 0x0042, 17 }, { 0x0a, 0x8042, 20 }, { 0x0c, 0x0043, 24 },
267 { 0x0e, 0x8043, 28 }, { 0x10, 0x0044, 33 }, { 0x12, 0x8044, 40 },
268 { 0x14, 0x0045, 47 }, { 0x16, 0x8045, 56 }, { 0x18, 0x0046, 67 },
269 { 0x1a, 0x8046, 80 }, { 0x1c, 0x0047, 95 }, { 0x1e, 0x8047, 112 },
270 { 0x20, 0x0048, 117 }, { 0x22, 0x8048, 123 }, { 0x24, 0x0049, 128 },
271 { 0x26, 0x8049, 134 }, { 0x28, 0x004a, 140 }, { 0x2a, 0x804a, 146 },
272 { 0x2c, 0x004b, 152 }, { 0x2e, 0x804b, 159 }, { 0x30, 0x004c, 166 },
273 { 0x32, 0x804c, 173 }, { 0x34, 0x004d, 181 }, { 0x36, 0x804d, 189 },
274 { 0x38, 0x004e, 198 }, { 0x3a, 0x804e, 206 }, { 0x3c, 0x004f, 215 },
275 { 0x3e, 0x804f, 225 }, { 0x40, 0x0050, 230 }, { 0x42, 0x8050, 235 },
276 { 0x44, 0x0051, 240 }, { 0x46, 0x8051, 245 }, { 0x48, 0x0052, 251 },
277 { 0x4a, 0x8052, 256 }, { 0x4c, 0x0053, 262 }, { 0x4e, 0x8053, 268 },
278 { 0x50, 0x0054, 273 }, { 0x52, 0x8054, 279 }, { 0x54, 0x0055, 286 },
279 { 0x56, 0x8055, 292 }, { 0x58, 0x0056, 298 }, { 0x5a, 0x8056, 305 },
280 { 0x5c, 0x0057, 311 }, { 0x5e, 0x8057, 318 }, { 0x60, 0x0058, 325 },
281 { 0x62, 0x8058, 332 }, { 0x64, 0x0059, 340 }, { 0x66, 0x8059, 347 },
282 { 0x68, 0x005a, 355 }, { 0x6a, 0x805a, 362 }, { 0x6c, 0x005b, 370 },
283 { 0x6e, 0x805b, 378 }, { 0x70, 0x005c, 387 }, { 0x72, 0x805c, 395 },
284 { 0x74, 0x005d, 404 }, { 0x76, 0x805d, 413 }, { 0x78, 0x005e, 422 },
285 { 0x7a, 0x805e, 431 }, { 0x7c, 0x005f, 440 }, { 0x7e, 0x805f, 450 },
286 { 0x80, 0x0060, 460 }, { 0x82, 0x8060, 470 }, { 0x84, 0x0061, 480 },
287 { 0x86, 0x8061, 491 }, { 0x88, 0x0062, 501 }, { 0x8a, 0x8062, 512 },
288 { 0x8c, 0x0063, 524 }, { 0x8e, 0x8063, 535 }, { 0x90, 0x0064, 547 },
289 { 0x92, 0x8064, 559 }, { 0x94, 0x0065, 571 }, { 0x96, 0x8065, 584 },
290 { 0x98, 0x0066, 596 }, { 0x9a, 0x8066, 609 }, { 0x9c, 0x0067, 623 },
291 { 0x9e, 0x8067, 636 }, { 0xa0, 0x0068, 650 }, { 0xa2, 0x8068, 665 },
292 { 0xa4, 0x0069, 679 }, { 0xa6, 0x8069, 694 }, { 0xa8, 0x006a, 709 },
293 { 0xaa, 0x806a, 725 }, { 0xac, 0x006b, 741 }, { 0xae, 0x806b, 757 },
294 { 0xb0, 0x006c, 773 }, { 0xb2, 0x806c, 790 }, { 0xb4, 0x006d, 808 },
295 { 0xb6, 0x806d, 825 }, { 0xb8, 0x006e, 843 }, { 0xba, 0x806e, 862 },
296 { 0xbc, 0x006f, 881 }, { 0xbe, 0x806f, 900 }, { 0xc0, 0x0070, 920 },
297 { 0xc2, 0x8070, 940 }, { 0xc4, 0x0071, 960 }, { 0xc6, 0x8071, 981 },
298 { 0xc8, 0x0072, joycon_max_rumble_amp }
315 JOYCON_CTLR_TYPE_JCL = 0x01,
316 JOYCON_CTLR_TYPE_JCR = 0x02,
317 JOYCON_CTLR_TYPE_PRO = 0x03,
318 JOYCON_CTLR_TYPE_NESL = 0x09,
319 JOYCON_CTLR_TYPE_NESR = 0x0A,
320 JOYCON_CTLR_TYPE_SNES = 0x0B,
321 JOYCON_CTLR_TYPE_GEN = 0x0D,
322 JOYCON_CTLR_TYPE_N64 = 0x0C,
340 #define JC_BTN_Y BIT(0)
369 * D-pad is configured as buttons for the left Joy-Con only!
385 * The unused *right*-side triggers become the SL/SR triggers for the *left*
386 * Joy-Con, if and only if we're not using a charging grip.
408 * The unused *left*-side triggers become the SL/SR triggers for the *right*
409 * Joy-Con, if and only if we're not using a charging grip.
475 * N64's C buttons get assigned to d-pad directions and registered as buttons.
507 u8 output_id; /* must be 0x01 for subcommand, 0x10 for rumble only */
515 u8 ack; /* MSB 1 for ACK, 0 for NACK */
559 { 1, 0, 0, 0 },
560 { 1, 1, 0, 0 },
561 { 1, 1, 1, 0 },
563 { 1, 0, 0, 1 },
564 { 1, 0, 1, 0 },
565 { 1, 0, 1, 1 },
566 { 0, 1, 1, 0 },
642 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL || \
643 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR || \
644 ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
646 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_PROCON)
648 (ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP)
652 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL || \
653 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO || \
654 ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64)
658 (ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR || \
659 ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO)
665 * but take caution: some NSO devices lie about themselves (NES Joy-Cons and
673 return ctlr->hdev->product == USB_DEVICE_ID_NINTENDO_CHRGGRIP; in joycon_device_is_chrggrip()
679 * These are slightly different than the device-ID-based helpers above. They are
681 * versus SNES, or NES Joy-Cons versus regular Switch Joy-Cons. They're most
691 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCL; in joycon_type_is_left_joycon()
696 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_JCR; in joycon_type_is_right_joycon()
701 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_PRO; in joycon_type_is_procon()
706 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_SNES; in joycon_type_is_snescon()
711 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_GEN; in joycon_type_is_gencon()
716 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_N64; in joycon_type_is_n64con()
721 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESL; in joycon_type_is_left_nescon()
726 return ctlr->ctlr_type == JOYCON_CTLR_TYPE_NESR; in joycon_type_is_right_nescon()
774 return ctlr->hdev->bus == BUS_USB; in joycon_using_usb()
784 return -ENOMEM; in __joycon_hid_send()
787 if (ret < 0) in __joycon_hid_send()
801 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) { in joycon_wait_for_input_report()
804 spin_lock_irqsave(&ctlr->lock, flags); in joycon_wait_for_input_report()
805 ctlr->received_input_report = false; in joycon_wait_for_input_report()
806 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_wait_for_input_report()
807 ret = wait_event_timeout(ctlr->wait, in joycon_wait_for_input_report()
808 ctlr->received_input_report, in joycon_wait_for_input_report()
812 hid_warn(ctlr->hdev, in joycon_wait_for_input_report()
828 #define JC_SUBCMD_RATE_LIMITER_MS(ctlr) ((ctlr)->hdev->bus == BUS_USB ? JC_SUBCMD_RATE_LIMITER_USB_…
833 int consecutive_valid_deltas = 0; in joycon_enforce_subcmd_rate()
834 int attempts = 0; in joycon_enforce_subcmd_rate()
837 if (unlikely(ctlr->ctlr_state != JOYCON_CTLR_STATE_READ)) in joycon_enforce_subcmd_rate()
843 subcmd_delta = current_ms - ctlr->last_subcmd_sent_msecs; in joycon_enforce_subcmd_rate()
845 spin_lock_irqsave(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
846 consecutive_valid_deltas = ctlr->consecutive_valid_report_deltas; in joycon_enforce_subcmd_rate()
847 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_enforce_subcmd_rate()
852 ctlr->ctlr_state == JOYCON_CTLR_STATE_READ && in joycon_enforce_subcmd_rate()
856 hid_warn(ctlr->hdev, "%s: exceeded max attempts", __func__); in joycon_enforce_subcmd_rate()
860 ctlr->last_subcmd_sent_msecs = current_ms; in joycon_enforce_subcmd_rate()
881 while (tries--) { in joycon_hid_send_sync()
884 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
885 if (ret < 0) { in joycon_hid_send_sync()
886 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
890 ret = wait_event_timeout(ctlr->wait, ctlr->received_resp, in joycon_hid_send_sync()
893 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
896 hid_dbg(ctlr->hdev, in joycon_hid_send_sync()
899 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
900 ret = -ETIMEDOUT; in joycon_hid_send_sync()
902 ret = 0; in joycon_hid_send_sync()
907 ctlr->received_resp = false; in joycon_hid_send_sync()
917 ctlr->usb_ack_match = cmd; in joycon_send_usb()
918 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
921 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
932 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_subcmd()
937 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_subcmd()
938 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
939 return -ENODEV; in joycon_send_subcmd()
941 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_subcmd()
943 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_subcmd()
945 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; in joycon_send_subcmd()
946 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
947 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
948 ctlr->subcmd_num = 0; in joycon_send_subcmd()
949 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
950 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
954 if (ret < 0) in joycon_send_subcmd()
955 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
957 ret = 0; in joycon_send_subcmd()
965 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_set_player_leds()
968 req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; in joycon_set_player_leds()
969 req->data[0] = (flash << 4) | on; in joycon_set_player_leds()
971 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
978 u8 buffer[sizeof(*req) + 5] = { 0 }; in joycon_set_home_led()
982 req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; in joycon_set_home_led()
983 data = req->data; in joycon_set_home_led()
984 data[0] = 0x01; in joycon_set_home_led()
987 data[3] = 0x11; in joycon_set_home_led()
988 data[4] = 0x11; in joycon_set_home_led()
990 hid_dbg(ctlr->hdev, "setting home led brightness\n"); in joycon_set_home_led()
999 u8 buffer[sizeof(*req) + 5] = { 0 }; in joycon_request_spi_flash_read()
1004 return -EINVAL; in joycon_request_spi_flash_read()
1007 req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; in joycon_request_spi_flash_read()
1008 data = req->data; in joycon_request_spi_flash_read()
1012 hid_dbg(ctlr->hdev, "requesting SPI flash data\n"); in joycon_request_spi_flash_read()
1015 hid_err(ctlr->hdev, "failed reading SPI flash; ret=%d\n", ret); in joycon_request_spi_flash_read()
1017 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_spi_flash_read()
1019 *reply = &report->subcmd_reply.data[5]; in joycon_request_spi_flash_read()
1026 * returns 0 if magic val is present, 1 if not present, < 0 on error
1038 return reply[0] != JC_CAL_USR_MAGIC_0 || reply[1] != JC_CAL_USR_MAGIC_1; in joycon_check_for_cal_magic()
1060 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1062 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1064 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1066 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1068 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1070 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1073 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, in joycon_read_stick_calibration()
1075 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, in joycon_read_stick_calibration()
1077 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, in joycon_read_stick_calibration()
1079 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, in joycon_read_stick_calibration()
1081 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, in joycon_read_stick_calibration()
1083 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, in joycon_read_stick_calibration()
1087 cal_x->max = cal_x->center + x_max_above; in joycon_read_stick_calibration()
1088 cal_x->min = cal_x->center - x_min_below; in joycon_read_stick_calibration()
1089 cal_y->max = cal_y->center + y_max_above; in joycon_read_stick_calibration()
1090 cal_y->min = cal_y->center - y_min_below; in joycon_read_stick_calibration()
1093 if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || in joycon_read_stick_calibration()
1094 cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) in joycon_read_stick_calibration()
1095 ret = -EINVAL; in joycon_read_stick_calibration()
1112 cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; in joycon_use_default_calibration()
1113 cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; in joycon_use_default_calibration()
1114 cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; in joycon_use_default_calibration()
1123 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
1128 hid_info(ctlr->hdev, "using user cal for left stick\n"); in joycon_request_calibration()
1130 hid_info(ctlr->hdev, "using factory cal for left stick\n"); in joycon_request_calibration()
1134 hid_info(ctlr->hdev, "using user cal for right stick\n"); in joycon_request_calibration()
1136 hid_info(ctlr->hdev, "using factory cal for right stick\n"); in joycon_request_calibration()
1141 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1142 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1146 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1147 &ctlr->left_stick_cal_x, in joycon_request_calibration()
1148 &ctlr->left_stick_cal_y, in joycon_request_calibration()
1153 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1154 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1158 joycon_use_default_calibration(ctlr->hdev, in joycon_request_calibration()
1159 &ctlr->right_stick_cal_x, in joycon_request_calibration()
1160 &ctlr->right_stick_cal_y, in joycon_request_calibration()
1163 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
1168 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
1169 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
1170 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
1171 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
1172 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
1173 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
1174 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
1175 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
1176 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
1177 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
1178 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
1179 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
1181 return 0; in joycon_request_calibration()
1191 int i, divz = 0; in joycon_calc_imu_cal_divisors()
1193 for (i = 0; i < 3; i++) { in joycon_calc_imu_cal_divisors()
1194 ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1195 ctlr->accel_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1196 ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] - in joycon_calc_imu_cal_divisors()
1197 ctlr->gyro_cal.offset[i]; in joycon_calc_imu_cal_divisors()
1199 if (ctlr->imu_cal_accel_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1200 ctlr->imu_cal_accel_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1204 if (ctlr->imu_cal_gyro_divisor[i] == 0) { in joycon_calc_imu_cal_divisors()
1205 ctlr->imu_cal_gyro_divisor[i] = 1; in joycon_calc_imu_cal_divisors()
1211 hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz); in joycon_calc_imu_cal_divisors()
1214 static const s16 DFLT_ACCEL_OFFSET /*= 0*/;
1216 static const s16 DFLT_GYRO_OFFSET /*= 0*/;
1228 hid_info(ctlr->hdev, "using user cal for IMU\n"); in joycon_request_imu_calibration()
1230 hid_info(ctlr->hdev, "using factory cal for IMU\n"); in joycon_request_imu_calibration()
1234 hid_dbg(ctlr->hdev, "requesting IMU cal data\n"); in joycon_request_imu_calibration()
1238 hid_warn(ctlr->hdev, in joycon_request_imu_calibration()
1242 for (i = 0; i < 3; i++) { in joycon_request_imu_calibration()
1243 ctlr->accel_cal.offset[i] = DFLT_ACCEL_OFFSET; in joycon_request_imu_calibration()
1244 ctlr->accel_cal.scale[i] = DFLT_ACCEL_SCALE; in joycon_request_imu_calibration()
1245 ctlr->gyro_cal.offset[i] = DFLT_GYRO_OFFSET; in joycon_request_imu_calibration()
1246 ctlr->gyro_cal.scale[i] = DFLT_GYRO_SCALE; in joycon_request_imu_calibration()
1253 for (i = 0; i < 3; i++) { in joycon_request_imu_calibration()
1256 ctlr->accel_cal.offset[i] = get_unaligned_le16(raw_cal + j); in joycon_request_imu_calibration()
1257 ctlr->accel_cal.scale[i] = get_unaligned_le16(raw_cal + j + 6); in joycon_request_imu_calibration()
1258 ctlr->gyro_cal.offset[i] = get_unaligned_le16(raw_cal + j + 12); in joycon_request_imu_calibration()
1259 ctlr->gyro_cal.scale[i] = get_unaligned_le16(raw_cal + j + 18); in joycon_request_imu_calibration()
1264 hid_dbg(ctlr->hdev, "IMU calibration:\n" in joycon_request_imu_calibration()
1265 "a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" in joycon_request_imu_calibration()
1266 "a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" in joycon_request_imu_calibration()
1267 "g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" in joycon_request_imu_calibration()
1268 "g_s[0]=%d g_s[1]=%d g_s[2]=%d\n", in joycon_request_imu_calibration()
1269 ctlr->accel_cal.offset[0], in joycon_request_imu_calibration()
1270 ctlr->accel_cal.offset[1], in joycon_request_imu_calibration()
1271 ctlr->accel_cal.offset[2], in joycon_request_imu_calibration()
1272 ctlr->accel_cal.scale[0], in joycon_request_imu_calibration()
1273 ctlr->accel_cal.scale[1], in joycon_request_imu_calibration()
1274 ctlr->accel_cal.scale[2], in joycon_request_imu_calibration()
1275 ctlr->gyro_cal.offset[0], in joycon_request_imu_calibration()
1276 ctlr->gyro_cal.offset[1], in joycon_request_imu_calibration()
1277 ctlr->gyro_cal.offset[2], in joycon_request_imu_calibration()
1278 ctlr->gyro_cal.scale[0], in joycon_request_imu_calibration()
1279 ctlr->gyro_cal.scale[1], in joycon_request_imu_calibration()
1280 ctlr->gyro_cal.scale[2]); in joycon_request_imu_calibration()
1282 return 0; in joycon_request_imu_calibration()
1288 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_set_report_mode()
1291 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; in joycon_set_report_mode()
1292 req->data[0] = 0x30; /* standard, full report mode */ in joycon_set_report_mode()
1294 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
1301 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_enable_rumble()
1304 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; in joycon_enable_rumble()
1305 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_rumble()
1307 hid_dbg(ctlr->hdev, "enabling rumble\n"); in joycon_enable_rumble()
1314 u8 buffer[sizeof(*req) + 1] = { 0 }; in joycon_enable_imu()
1317 req->subcmd_id = JC_SUBCMD_ENABLE_IMU; in joycon_enable_imu()
1318 req->data[0] = 0x01; /* note: 0x00 would disable */ in joycon_enable_imu()
1320 hid_dbg(ctlr->hdev, "enabling IMU\n"); in joycon_enable_imu()
1326 s32 center = cal->center; in joycon_map_stick_val()
1327 s32 min = cal->min; in joycon_map_stick_val()
1328 s32 max = cal->max; in joycon_map_stick_val()
1332 new_val = (val - center) * JC_MAX_STICK_MAG; in joycon_map_stick_val()
1333 new_val /= (max - center); in joycon_map_stick_val()
1335 new_val = (center - val) * -JC_MAX_STICK_MAG; in joycon_map_stick_val()
1336 new_val /= (center - min); in joycon_map_stick_val()
1338 new_val = clamp(new_val, (s32)-JC_MAX_STICK_MAG, (s32)JC_MAX_STICK_MAG); in joycon_map_stick_val()
1346 u8 *raw = rep->imu_raw_bytes; in joycon_input_report_parse_imu_data()
1349 for (i = 0; i < 3; i++) { in joycon_input_report_parse_imu_data()
1352 data->accel_x = get_unaligned_le16(raw + 0); in joycon_input_report_parse_imu_data()
1353 data->accel_y = get_unaligned_le16(raw + 2); in joycon_input_report_parse_imu_data()
1354 data->accel_z = get_unaligned_le16(raw + 4); in joycon_input_report_parse_imu_data()
1355 data->gyro_x = get_unaligned_le16(raw + 6); in joycon_input_report_parse_imu_data()
1356 data->gyro_y = get_unaligned_le16(raw + 8); in joycon_input_report_parse_imu_data()
1357 data->gyro_z = get_unaligned_le16(raw + 10); in joycon_input_report_parse_imu_data()
1366 struct joycon_imu_data imu_data[3] = {0}; /* 3 reports per packet */ in joycon_parse_imu_report()
1367 struct input_dev *idev = ctlr->imu_input; in joycon_parse_imu_report()
1369 unsigned int last_msecs = ctlr->imu_last_pkt_ms; in joycon_parse_imu_report()
1379 * incrementing 8-bit counter per input report, but it is not very in joycon_parse_imu_report()
1381 * increments at or if it varies based on packet push rate - more on in joycon_parse_imu_report()
1384 * The reverse engineering work done on the joy-cons and pro controllers in joycon_parse_imu_report()
1386 * - The controller samples the IMU every 1.35ms. It then does some of in joycon_parse_imu_report()
1388 * - Each imu input report contains 3 IMU samples, (usually 5ms apart). in joycon_parse_imu_report()
1389 * - In the standard reporting mode (which this driver uses exclusively) in joycon_parse_imu_report()
1391 * * joy-con (bluetooth): every 15 ms in joycon_parse_imu_report()
1392 * * joy-cons (in charging grip via USB): every 15 ms in joycon_parse_imu_report()
1399 * SSR to 11ms for both the joy-cons and pro controllers). in joycon_parse_imu_report()
1422 if (!ctlr->imu_first_packet_received) { in joycon_parse_imu_report()
1423 ctlr->imu_timestamp_us = 0; in joycon_parse_imu_report()
1424 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1425 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1426 ctlr->imu_avg_delta_ms = JC_IMU_DFLT_AVG_DELTA_MS; in joycon_parse_imu_report()
1427 ctlr->imu_first_packet_received = true; in joycon_parse_imu_report()
1429 unsigned int delta = msecs - last_msecs; in joycon_parse_imu_report()
1434 ctlr->imu_delta_samples_sum += delta; in joycon_parse_imu_report()
1435 ctlr->imu_delta_samples_count++; in joycon_parse_imu_report()
1436 if (ctlr->imu_delta_samples_count >= in joycon_parse_imu_report()
1438 ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum / in joycon_parse_imu_report()
1439 ctlr->imu_delta_samples_count; in joycon_parse_imu_report()
1440 ctlr->imu_delta_samples_count = 0; in joycon_parse_imu_report()
1441 ctlr->imu_delta_samples_sum = 0; in joycon_parse_imu_report()
1445 if (ctlr->imu_avg_delta_ms == 0) { in joycon_parse_imu_report()
1446 ctlr->imu_avg_delta_ms = 1; in joycon_parse_imu_report()
1447 hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n"); in joycon_parse_imu_report()
1451 hid_dbg(ctlr->hdev, in joycon_parse_imu_report()
1453 msecs, last_msecs, delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1456 dropped_threshold = ctlr->imu_avg_delta_ms * 3 / 2; in joycon_parse_imu_report()
1457 dropped_pkts = (delta - min(delta, dropped_threshold)) / in joycon_parse_imu_report()
1458 ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1459 ctlr->imu_timestamp_us += 1000 * ctlr->imu_avg_delta_ms; in joycon_parse_imu_report()
1461 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1464 hid_warn(ctlr->hdev, in joycon_parse_imu_report()
1466 delta, ctlr->imu_avg_delta_ms); in joycon_parse_imu_report()
1469 ctlr->imu_last_pkt_ms = msecs; in joycon_parse_imu_report()
1472 for (i = 0; i < 3; i++) { in joycon_parse_imu_report()
1474 ctlr->imu_timestamp_us); in joycon_parse_imu_report()
1479 * found in the community's reverse-engineering repo (linked at in joycon_parse_imu_report()
1480 * top of driver). For hid-nintendo, we make sure that the final in joycon_parse_imu_report()
1489 * precision-saving scaling factor to prevent large inaccuracies in joycon_parse_imu_report()
1494 value[0] = mult_frac((JC_IMU_PREC_RANGE_SCALE * in joycon_parse_imu_report()
1495 (imu_data[i].gyro_x - in joycon_parse_imu_report()
1496 ctlr->gyro_cal.offset[0])), in joycon_parse_imu_report()
1497 ctlr->gyro_cal.scale[0], in joycon_parse_imu_report()
1498 ctlr->imu_cal_gyro_divisor[0]); in joycon_parse_imu_report()
1500 (imu_data[i].gyro_y - in joycon_parse_imu_report()
1501 ctlr->gyro_cal.offset[1])), in joycon_parse_imu_report()
1502 ctlr->gyro_cal.scale[1], in joycon_parse_imu_report()
1503 ctlr->imu_cal_gyro_divisor[1]); in joycon_parse_imu_report()
1505 (imu_data[i].gyro_z - in joycon_parse_imu_report()
1506 ctlr->gyro_cal.offset[2])), in joycon_parse_imu_report()
1507 ctlr->gyro_cal.scale[2], in joycon_parse_imu_report()
1508 ctlr->imu_cal_gyro_divisor[2]); in joycon_parse_imu_report()
1511 ctlr->accel_cal.scale[0]) / in joycon_parse_imu_report()
1512 ctlr->imu_cal_accel_divisor[0]; in joycon_parse_imu_report()
1514 ctlr->accel_cal.scale[1]) / in joycon_parse_imu_report()
1515 ctlr->imu_cal_accel_divisor[1]; in joycon_parse_imu_report()
1517 ctlr->accel_cal.scale[2]) / in joycon_parse_imu_report()
1518 ctlr->imu_cal_accel_divisor[2]; in joycon_parse_imu_report()
1520 hid_dbg(ctlr->hdev, "raw_gyro: g_x=%d g_y=%d g_z=%d\n", in joycon_parse_imu_report()
1523 hid_dbg(ctlr->hdev, "raw_accel: a_x=%d a_y=%d a_z=%d\n", in joycon_parse_imu_report()
1528 * The right joy-con has 2 axes negated, Y and Z. This is due to in joycon_parse_imu_report()
1530 * axes' values in order to be consistent with the left joy-con in joycon_parse_imu_report()
1535 * The axes follow the right-hand rule. in joycon_parse_imu_report()
1544 value[j] *= -1; in joycon_parse_imu_report()
1548 input_report_abs(idev, ABS_RX, value[0]); in joycon_parse_imu_report()
1556 ctlr->imu_timestamp_us += ctlr->imu_avg_delta_ms * 1000 / 3; in joycon_parse_imu_report()
1565 spin_lock_irqsave(&ctlr->lock, flags); in joycon_handle_rumble_report()
1566 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report && in joycon_handle_rumble_report()
1567 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED && in joycon_handle_rumble_report()
1568 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS && in joycon_handle_rumble_report()
1569 (ctlr->rumble_queue_head != ctlr->rumble_queue_tail || in joycon_handle_rumble_report()
1570 ctlr->rumble_zero_countdown > 0)) { in joycon_handle_rumble_report()
1572 * When this value reaches 0, we know we've sent multiple in joycon_handle_rumble_report()
1577 if (ctlr->rumble_zero_countdown > 0) in joycon_handle_rumble_report()
1578 ctlr->rumble_zero_countdown--; in joycon_handle_rumble_report()
1579 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_handle_rumble_report()
1582 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_handle_rumble_report()
1590 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_battery_status()
1592 tmp = rep->bat_con; in joycon_parse_battery_status()
1593 ctlr->host_powered = tmp & BIT(0); in joycon_parse_battery_status()
1594 ctlr->battery_charging = tmp & BIT(4); in joycon_parse_battery_status()
1598 case 0: /* empty */ in joycon_parse_battery_status()
1599 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; in joycon_parse_battery_status()
1602 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; in joycon_parse_battery_status()
1605 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; in joycon_parse_battery_status()
1608 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; in joycon_parse_battery_status()
1611 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; in joycon_parse_battery_status()
1614 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_parse_battery_status()
1615 hid_warn(ctlr->hdev, "Invalid battery status\n"); in joycon_parse_battery_status()
1619 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_battery_status()
1630 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_report_left_stick()
1631 raw_y = hid_field_extract(ctlr->hdev, rep->left_stick + 1, 4, 12); in joycon_report_left_stick()
1633 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_report_left_stick()
1634 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_report_left_stick()
1636 input_report_abs(ctlr->input, ABS_X, x); in joycon_report_left_stick()
1637 input_report_abs(ctlr->input, ABS_Y, y); in joycon_report_left_stick()
1648 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_report_right_stick()
1649 raw_y = hid_field_extract(ctlr->hdev, rep->right_stick + 1, 4, 12); in joycon_report_right_stick()
1651 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_report_right_stick()
1652 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_report_right_stick()
1654 input_report_abs(ctlr->input, ABS_RX, x); in joycon_report_right_stick()
1655 input_report_abs(ctlr->input, ABS_RY, y); in joycon_report_right_stick()
1661 int hatx = 0; in joycon_report_dpad()
1662 int haty = 0; in joycon_report_dpad()
1663 u32 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_dpad()
1666 hatx = -1; in joycon_report_dpad()
1671 haty = -1; in joycon_report_dpad()
1675 input_report_abs(ctlr->input, ABS_HAT0X, hatx); in joycon_report_dpad()
1676 input_report_abs(ctlr->input, ABS_HAT0Y, haty); in joycon_report_dpad()
1684 u32 status = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_report_buttons()
1686 for (button = button_mappings; button->code; button++) in joycon_report_buttons()
1687 input_report_key(ctlr->input, button->code, status & button->bit); in joycon_report_buttons()
1695 unsigned long report_delta_ms = msecs - ctlr->last_input_report_msecs; in joycon_parse_report()
1732 input_sync(ctlr->input); in joycon_parse_report()
1734 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1735 ctlr->last_input_report_msecs = msecs; in joycon_parse_report()
1743 if (ctlr->consecutive_valid_report_deltas < JC_SUBCMD_VALID_DELTA_REQ) in joycon_parse_report()
1744 ctlr->consecutive_valid_report_deltas++; in joycon_parse_report()
1746 ctlr->consecutive_valid_report_deltas = 0; in joycon_parse_report()
1750 * bluetooth-connected controllers. For USB devices, we're beholden to in joycon_parse_report()
1754 if (ctlr->hdev->bus == BUS_USB) in joycon_parse_report()
1755 ctlr->consecutive_valid_report_deltas = JC_SUBCMD_VALID_DELTA_REQ; in joycon_parse_report()
1757 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1764 if (unlikely(mutex_is_locked(&ctlr->output_mutex))) { in joycon_parse_report()
1765 spin_lock_irqsave(&ctlr->lock, flags); in joycon_parse_report()
1766 ctlr->received_input_report = true; in joycon_parse_report()
1767 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_parse_report()
1768 wake_up(&ctlr->wait); in joycon_parse_report()
1772 if ((rep->id == JC_INPUT_IMU_DATA) && joycon_has_imu(ctlr)) in joycon_parse_report()
1780 struct joycon_rumble_output rumble_output = { 0 }; in joycon_send_rumble_data()
1782 spin_lock_irqsave(&ctlr->lock, flags); in joycon_send_rumble_data()
1787 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) { in joycon_send_rumble_data()
1788 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1789 return -ENODEV; in joycon_send_rumble_data()
1792 ctlr->rumble_data[ctlr->rumble_queue_tail], in joycon_send_rumble_data()
1794 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_send_rumble_data()
1797 rumble_output.packet_num = ctlr->subcmd_num; in joycon_send_rumble_data()
1798 if (++ctlr->subcmd_num > 0xF) in joycon_send_rumble_data()
1799 ctlr->subcmd_num = 0; in joycon_send_rumble_data()
1803 ret = __joycon_hid_send(ctlr->hdev, (u8 *)&rumble_output, in joycon_send_rumble_data()
1817 mutex_lock(&ctlr->output_mutex); in joycon_rumble_worker()
1819 mutex_unlock(&ctlr->output_mutex); in joycon_rumble_worker()
1821 /* -ENODEV means the controller was just unplugged */ in joycon_rumble_worker()
1822 spin_lock_irqsave(&ctlr->lock, flags); in joycon_rumble_worker()
1823 if (ret < 0 && ret != -ENODEV && in joycon_rumble_worker()
1824 ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_rumble_worker()
1825 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret); in joycon_rumble_worker()
1827 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_rumble_worker()
1828 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) { in joycon_rumble_worker()
1829 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE) in joycon_rumble_worker()
1830 ctlr->rumble_queue_tail = 0; in joycon_rumble_worker()
1834 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_rumble_worker()
1843 int i = 0; in joycon_find_rumble_freq()
1845 if (freq > data[0].freq) { in joycon_find_rumble_freq()
1846 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_freq()
1847 if (freq > data[i - 1].freq && freq <= data[i].freq) in joycon_find_rumble_freq()
1859 int i = 0; in joycon_find_rumble_amp()
1861 if (amp > data[0].amp) { in joycon_find_rumble_amp()
1862 for (i = 1; i < length - 1; i++) { in joycon_find_rumble_amp()
1863 if (amp > data[i - 1].amp && amp <= data[i].amp) in joycon_find_rumble_amp()
1881 data[0] = (freq_data_high.high >> 8) & 0xFF; in joycon_encode_rumble()
1882 data[1] = (freq_data_high.high & 0xFF) + amp_data.high; in joycon_encode_rumble()
1883 data[2] = freq_data_low.low + ((amp_data.low >> 8) & 0xFF); in joycon_encode_rumble()
1884 data[3] = amp_data.low & 0xFF; in joycon_encode_rumble()
1896 spin_lock_irqsave(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1897 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, in joycon_clamp_rumble_freqs()
1900 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, in joycon_clamp_rumble_freqs()
1903 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, in joycon_clamp_rumble_freqs()
1906 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, in joycon_clamp_rumble_freqs()
1909 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_clamp_rumble_freqs()
1924 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1925 freq_r_low = ctlr->rumble_rl_freq; in joycon_set_rumble()
1926 freq_r_high = ctlr->rumble_rh_freq; in joycon_set_rumble()
1927 freq_l_low = ctlr->rumble_ll_freq; in joycon_set_rumble()
1928 freq_l_high = ctlr->rumble_lh_freq; in joycon_set_rumble()
1930 if (amp_l != 0 || amp_r != 0) in joycon_set_rumble()
1931 ctlr->rumble_zero_countdown = JC_RUMBLE_ZERO_AMP_PKT_CNT; in joycon_set_rumble()
1932 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1934 /* right joy-con */ in joycon_set_rumble()
1938 /* left joy-con */ in joycon_set_rumble()
1942 spin_lock_irqsave(&ctlr->lock, flags); in joycon_set_rumble()
1944 next_rq_head = ctlr->rumble_queue_head + 1; in joycon_set_rumble()
1946 next_rq_head = 0; in joycon_set_rumble()
1951 if (next_rq_head == ctlr->rumble_queue_tail) { in joycon_set_rumble()
1952 hid_dbg(ctlr->hdev, "rumble queue is full"); in joycon_set_rumble()
1954 next_rq_head = ctlr->rumble_queue_head; in joycon_set_rumble()
1957 ctlr->rumble_queue_head = next_rq_head; in joycon_set_rumble()
1958 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data, in joycon_set_rumble()
1962 if (schedule_now && ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED) in joycon_set_rumble()
1963 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker); in joycon_set_rumble()
1965 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_set_rumble()
1967 return 0; in joycon_set_rumble()
1975 if (effect->type != FF_RUMBLE) in joycon_play_effect()
1976 return 0; in joycon_play_effect()
1979 effect->u.rumble.weak_magnitude, in joycon_play_effect()
1980 effect->u.rumble.strong_magnitude, in joycon_play_effect()
1989 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
1995 -JC_MAX_STICK_MAG, in joycon_config_left_stick()
2005 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2011 -JC_MAX_STICK_MAG, in joycon_config_right_stick()
2021 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2027 -JC_MAX_DPAD_MAG, in joycon_config_dpad()
2038 for (button = button_mappings; button->code; button++) in joycon_config_buttons()
2039 input_set_capability(idev, EV_KEY, button->code); in joycon_config_buttons()
2046 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); in joycon_config_rumble()
2047 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); in joycon_config_rumble()
2048 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2049 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2050 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; in joycon_config_rumble()
2051 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; in joycon_config_rumble()
2053 joycon_set_rumble(ctlr, 0, 0, false); in joycon_config_rumble()
2054 ctlr->rumble_msecs = jiffies_to_msecs(jiffies); in joycon_config_rumble()
2064 hdev = ctlr->hdev; in joycon_imu_input_create()
2067 ctlr->imu_input = devm_input_allocate_device(&hdev->dev); in joycon_imu_input_create()
2068 if (!ctlr->imu_input) in joycon_imu_input_create()
2069 return -ENOMEM; in joycon_imu_input_create()
2071 ctlr->imu_input->id.bustype = hdev->bus; in joycon_imu_input_create()
2072 ctlr->imu_input->id.vendor = hdev->vendor; in joycon_imu_input_create()
2073 ctlr->imu_input->id.product = hdev->product; in joycon_imu_input_create()
2074 ctlr->imu_input->id.version = hdev->version; in joycon_imu_input_create()
2075 ctlr->imu_input->uniq = ctlr->mac_addr_str; in joycon_imu_input_create()
2076 ctlr->imu_input->phys = hdev->phys; in joycon_imu_input_create()
2078 imu_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s (IMU)", ctlr->input->name); in joycon_imu_input_create()
2080 return -ENOMEM; in joycon_imu_input_create()
2082 ctlr->imu_input->name = imu_name; in joycon_imu_input_create()
2084 input_set_drvdata(ctlr->imu_input, ctlr); in joycon_imu_input_create()
2087 input_set_abs_params(ctlr->imu_input, ABS_X, in joycon_imu_input_create()
2088 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2090 input_set_abs_params(ctlr->imu_input, ABS_Y, in joycon_imu_input_create()
2091 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2093 input_set_abs_params(ctlr->imu_input, ABS_Z, in joycon_imu_input_create()
2094 -JC_IMU_MAX_ACCEL_MAG, JC_IMU_MAX_ACCEL_MAG, in joycon_imu_input_create()
2096 input_abs_set_res(ctlr->imu_input, ABS_X, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2097 input_abs_set_res(ctlr->imu_input, ABS_Y, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2098 input_abs_set_res(ctlr->imu_input, ABS_Z, JC_IMU_ACCEL_RES_PER_G); in joycon_imu_input_create()
2100 input_set_abs_params(ctlr->imu_input, ABS_RX, in joycon_imu_input_create()
2101 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2103 input_set_abs_params(ctlr->imu_input, ABS_RY, in joycon_imu_input_create()
2104 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2106 input_set_abs_params(ctlr->imu_input, ABS_RZ, in joycon_imu_input_create()
2107 -JC_IMU_MAX_GYRO_MAG, JC_IMU_MAX_GYRO_MAG, in joycon_imu_input_create()
2110 input_abs_set_res(ctlr->imu_input, ABS_RX, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2111 input_abs_set_res(ctlr->imu_input, ABS_RY, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2112 input_abs_set_res(ctlr->imu_input, ABS_RZ, JC_IMU_GYRO_RES_PER_DPS); in joycon_imu_input_create()
2114 __set_bit(EV_MSC, ctlr->imu_input->evbit); in joycon_imu_input_create()
2115 __set_bit(MSC_TIMESTAMP, ctlr->imu_input->mscbit); in joycon_imu_input_create()
2116 __set_bit(INPUT_PROP_ACCELEROMETER, ctlr->imu_input->propbit); in joycon_imu_input_create()
2118 ret = input_register_device(ctlr->imu_input); in joycon_imu_input_create()
2122 return 0; in joycon_imu_input_create()
2130 hdev = ctlr->hdev; in joycon_input_create()
2132 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
2133 if (!ctlr->input) in joycon_input_create()
2134 return -ENOMEM; in joycon_input_create()
2135 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
2136 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
2137 ctlr->input->id.product = hdev->product; in joycon_input_create()
2138 ctlr->input->id.version = hdev->version; in joycon_input_create()
2139 ctlr->input->uniq = ctlr->mac_addr_str; in joycon_input_create()
2140 ctlr->input->name = hdev->name; in joycon_input_create()
2141 ctlr->input->phys = hdev->phys; in joycon_input_create()
2142 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
2144 ret = input_register_device(ctlr->input); in joycon_input_create()
2149 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2150 joycon_config_buttons(ctlr->input, right_joycon_button_mappings); in joycon_input_create()
2152 joycon_config_buttons(ctlr->input, right_joycon_s_button_mappings); in joycon_input_create()
2154 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2155 joycon_config_buttons(ctlr->input, left_joycon_button_mappings); in joycon_input_create()
2157 joycon_config_buttons(ctlr->input, left_joycon_s_button_mappings); in joycon_input_create()
2159 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2160 joycon_config_right_stick(ctlr->input); in joycon_input_create()
2161 joycon_config_dpad(ctlr->input); in joycon_input_create()
2162 joycon_config_buttons(ctlr->input, procon_button_mappings); in joycon_input_create()
2164 joycon_config_dpad(ctlr->input); in joycon_input_create()
2165 joycon_config_buttons(ctlr->input, nescon_button_mappings); in joycon_input_create()
2167 joycon_config_dpad(ctlr->input); in joycon_input_create()
2168 joycon_config_buttons(ctlr->input, snescon_button_mappings); in joycon_input_create()
2170 joycon_config_dpad(ctlr->input); in joycon_input_create()
2171 joycon_config_buttons(ctlr->input, gencon_button_mappings); in joycon_input_create()
2173 joycon_config_dpad(ctlr->input); in joycon_input_create()
2174 joycon_config_left_stick(ctlr->input); in joycon_input_create()
2175 joycon_config_buttons(ctlr->input, n64con_button_mappings); in joycon_input_create()
2187 return 0; in joycon_input_create()
2194 struct device *dev = led->dev->parent; in joycon_player_led_brightness_set()
2197 int val = 0; in joycon_player_led_brightness_set()
2204 return -ENODEV; in joycon_player_led_brightness_set()
2207 for (i = 0; i < JC_NUM_LEDS; i++) in joycon_player_led_brightness_set()
2208 val |= ctlr->leds[i].brightness << i; in joycon_player_led_brightness_set()
2210 mutex_lock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2211 ret = joycon_set_player_leds(ctlr, 0, val); in joycon_player_led_brightness_set()
2212 mutex_unlock(&ctlr->output_mutex); in joycon_player_led_brightness_set()
2220 struct device *dev = led->dev->parent; in joycon_home_led_brightness_set()
2228 return -ENODEV; in joycon_home_led_brightness_set()
2230 mutex_lock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2232 mutex_unlock(&ctlr->output_mutex); in joycon_home_led_brightness_set()
2240 struct hid_device *hdev = ctlr->hdev; in joycon_leds_create()
2241 struct device *dev = &hdev->dev; in joycon_leds_create()
2244 int led_val = 0; in joycon_leds_create()
2251 ctlr->player_id = U32_MAX; in joycon_leds_create()
2253 if (ret < 0) { in joycon_leds_create()
2257 ctlr->player_id = ret; in joycon_leds_create()
2259 hid_info(ctlr->hdev, "assigned player %d led pattern", player_led_pattern + 1); in joycon_leds_create()
2261 for (i = 0; i < JC_NUM_LEDS; i++) { in joycon_leds_create()
2267 return -ENOMEM; in joycon_leds_create()
2269 led = &ctlr->leds[i]; in joycon_leds_create()
2270 led->name = name; in joycon_leds_create()
2271 led->brightness = joycon_player_led_patterns[player_led_pattern][i]; in joycon_leds_create()
2272 led->max_brightness = 1; in joycon_leds_create()
2273 led->brightness_set_blocking = in joycon_leds_create()
2275 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2279 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2280 ret = joycon_set_player_leds(ctlr, 0, led_val); in joycon_leds_create()
2281 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2287 for (i = 0; i < JC_NUM_LEDS; i++) { in joycon_leds_create()
2288 led = &ctlr->leds[i]; in joycon_leds_create()
2289 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2304 return -ENOMEM; in joycon_leds_create()
2306 led = &ctlr->home_led; in joycon_leds_create()
2307 led->name = name; in joycon_leds_create()
2308 led->brightness = 0; in joycon_leds_create()
2309 led->max_brightness = 0xF; in joycon_leds_create()
2310 led->brightness_set_blocking = joycon_home_led_brightness_set; in joycon_leds_create()
2311 led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; in joycon_leds_create()
2313 /* Set the home LED to 0 as default state */ in joycon_leds_create()
2314 mutex_lock(&ctlr->output_mutex); in joycon_leds_create()
2315 ret = joycon_set_home_led(ctlr, 0); in joycon_leds_create()
2316 mutex_unlock(&ctlr->output_mutex); in joycon_leds_create()
2319 return 0; in joycon_leds_create()
2322 ret = devm_led_classdev_register(&hdev->dev, led); in joycon_leds_create()
2329 return 0; in joycon_leds_create()
2338 int ret = 0; in joycon_battery_get_property()
2343 spin_lock_irqsave(&ctlr->lock, flags); in joycon_battery_get_property()
2344 capacity = ctlr->battery_capacity; in joycon_battery_get_property()
2345 charging = ctlr->battery_charging; in joycon_battery_get_property()
2346 powered = ctlr->host_powered; in joycon_battery_get_property()
2347 spin_unlock_irqrestore(&ctlr->lock, flags); in joycon_battery_get_property()
2351 val->intval = 1; in joycon_battery_get_property()
2354 val->intval = POWER_SUPPLY_SCOPE_DEVICE; in joycon_battery_get_property()
2357 val->intval = capacity; in joycon_battery_get_property()
2361 val->intval = POWER_SUPPLY_STATUS_CHARGING; in joycon_battery_get_property()
2364 val->intval = POWER_SUPPLY_STATUS_FULL; in joycon_battery_get_property()
2366 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in joycon_battery_get_property()
2369 ret = -EINVAL; in joycon_battery_get_property()
2384 struct hid_device *hdev = ctlr->hdev; in joycon_power_supply_create()
2387 int ret = 0; in joycon_power_supply_create()
2390 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in joycon_power_supply_create()
2393 ctlr->battery_desc.properties = joycon_battery_props; in joycon_power_supply_create()
2394 ctlr->battery_desc.num_properties = in joycon_power_supply_create()
2396 ctlr->battery_desc.get_property = joycon_battery_get_property; in joycon_power_supply_create()
2397 ctlr->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; in joycon_power_supply_create()
2398 ctlr->battery_desc.use_for_apm = 0; in joycon_power_supply_create()
2399 ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, in joycon_power_supply_create()
2401 dev_name(&hdev->dev)); in joycon_power_supply_create()
2402 if (!ctlr->battery_desc.name) in joycon_power_supply_create()
2403 return -ENOMEM; in joycon_power_supply_create()
2405 ctlr->battery = devm_power_supply_register(&hdev->dev, in joycon_power_supply_create()
2406 &ctlr->battery_desc, in joycon_power_supply_create()
2408 if (IS_ERR(ctlr->battery)) { in joycon_power_supply_create()
2409 ret = PTR_ERR(ctlr->battery); in joycon_power_supply_create()
2414 return power_supply_powers(ctlr->battery, &hdev->dev); in joycon_power_supply_create()
2422 struct joycon_subcmd_request req = { 0 }; in joycon_read_info()
2426 ret = joycon_send_subcmd(ctlr, &req, 0, HZ); in joycon_read_info()
2428 hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret); in joycon_read_info()
2432 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_read_info()
2434 for (i = 4, j = 0; j < 6; i++, j++) in joycon_read_info()
2435 ctlr->mac_addr[j] = report->subcmd_reply.data[i]; in joycon_read_info()
2437 ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, in joycon_read_info()
2439 ctlr->mac_addr[0], in joycon_read_info()
2440 ctlr->mac_addr[1], in joycon_read_info()
2441 ctlr->mac_addr[2], in joycon_read_info()
2442 ctlr->mac_addr[3], in joycon_read_info()
2443 ctlr->mac_addr[4], in joycon_read_info()
2444 ctlr->mac_addr[5]); in joycon_read_info()
2445 if (!ctlr->mac_addr_str) in joycon_read_info()
2446 return -ENOMEM; in joycon_read_info()
2447 hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str); in joycon_read_info()
2451 * Unfortantly the hdev->product can't always be used due to a ?bug? in joycon_read_info()
2453 * PID as 0x201E, but over bluetooth it will report the PID as 0x2017 in joycon_read_info()
2458 ctlr->ctlr_type = report->subcmd_reply.data[2]; in joycon_read_info()
2459 hid_dbg(ctlr->hdev, "controller type = 0x%02X\n", ctlr->ctlr_type); in joycon_read_info()
2461 return 0; in joycon_read_info()
2467 int ret = 0; in joycon_init()
2469 mutex_lock(&ctlr->output_mutex); in joycon_init()
2495 ret = -ETIMEDOUT; in joycon_init()
2538 /* Set the reporting mode to 0x30, which is the full report mode */ in joycon_init()
2555 mutex_unlock(&ctlr->output_mutex); in joycon_init()
2563 if (data[0] == JC_INPUT_SUBCMD_REPLY || data[0] == JC_INPUT_IMU_DATA || in joycon_ctlr_read_handler()
2564 data[0] == JC_INPUT_MCU_DATA) { in joycon_ctlr_read_handler()
2570 return 0; in joycon_ctlr_read_handler()
2576 int ret = 0; in joycon_ctlr_handle_event()
2580 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
2581 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
2582 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
2586 if (data[0] == JC_INPUT_USB_RESPONSE && in joycon_ctlr_handle_event()
2587 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
2592 data[0] != JC_INPUT_SUBCMD_REPLY) in joycon_ctlr_handle_event()
2595 if (report->subcmd_reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
2603 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
2605 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
2606 ctlr->received_resp = true; in joycon_ctlr_handle_event()
2607 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
2614 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
2626 return -EINVAL; in nintendo_hid_event()
2637 hid_dbg(hdev, "probe - start\n"); in nintendo_hid_probe()
2639 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
2641 ret = -ENOMEM; in nintendo_hid_probe()
2645 ctlr->hdev = hdev; in nintendo_hid_probe()
2646 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
2647 ctlr->rumble_queue_head = 0; in nintendo_hid_probe()
2648 ctlr->rumble_queue_tail = 0; in nintendo_hid_probe()
2650 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
2651 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
2652 spin_lock_init(&ctlr->lock); in nintendo_hid_probe()
2653 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq", in nintendo_hid_probe()
2654 WQ_FREEZABLE | WQ_MEM_RECLAIM, 0); in nintendo_hid_probe()
2655 if (!ctlr->rumble_queue) { in nintendo_hid_probe()
2656 ret = -ENOMEM; in nintendo_hid_probe()
2659 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); in nintendo_hid_probe()
2674 hdev->version |= 0x8000; in nintendo_hid_probe()
2716 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
2718 hid_dbg(hdev, "probe - success\n"); in nintendo_hid_probe()
2719 return 0; in nintendo_hid_probe()
2722 ida_free(&nintendo_player_id_allocator, ctlr->player_id); in nintendo_hid_probe()
2728 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_probe()
2730 hid_err(hdev, "probe - fail = %d\n", ret); in nintendo_hid_probe()
2742 spin_lock_irqsave(&ctlr->lock, flags); in nintendo_hid_remove()
2743 ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED; in nintendo_hid_remove()
2744 spin_unlock_irqrestore(&ctlr->lock, flags); in nintendo_hid_remove()
2746 destroy_workqueue(ctlr->rumble_queue); in nintendo_hid_remove()
2747 ida_free(&nintendo_player_id_allocator, ctlr->player_id); in nintendo_hid_remove()