hid-nintendo.c (697e5c7a34b0f69e1e438385cf85a9220069e079) hid-nintendo.c (c4eae84feff3e68c2f385aa10faea4a96791e7ad)
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * HID driver for Nintendo Switch Joy-Cons and Pro Controllers
4 *
5 * Copyright (c) 2019 Daniel J. Ogorchock <djogorchock@gmail.com>
6 *
7 * The following resources/projects were referenced for this driver:
8 * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
9 * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
10 * https://github.com/FrotBot/SwitchProConLinuxUSB
11 * https://github.com/MTCKC/ProconXInput
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * HID driver for Nintendo Switch Joy-Cons and Pro Controllers
4 *
5 * Copyright (c) 2019 Daniel J. Ogorchock <djogorchock@gmail.com>
6 *
7 * The following resources/projects were referenced for this driver:
8 * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
9 * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
10 * https://github.com/FrotBot/SwitchProConLinuxUSB
11 * https://github.com/MTCKC/ProconXInput
12 * https://github.com/Davidobot/BetterJoyForCemu
12 * hid-wiimote kernel hid driver
13 * hid-logitech-hidpp driver
14 * hid-sony driver
15 *
16 * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
17 * Pro Controllers can either be used over USB or Bluetooth.
18 *
19 * The driver will retrieve the factory calibration info from the controllers,
20 * so little to no user calibration should be required.
21 *
22 */
23
24#include "hid-ids.h"
25#include <linux/delay.h>
26#include <linux/device.h>
27#include <linux/hid.h>
28#include <linux/input.h>
13 * hid-wiimote kernel hid driver
14 * hid-logitech-hidpp driver
15 * hid-sony driver
16 *
17 * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The
18 * Pro Controllers can either be used over USB or Bluetooth.
19 *
20 * The driver will retrieve the factory calibration info from the controllers,
21 * so little to no user calibration should be required.
22 *
23 */
24
25#include "hid-ids.h"
26#include <linux/delay.h>
27#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/input.h>
30#include <linux/jiffies.h>
29#include <linux/leds.h>
30#include <linux/module.h>
31#include <linux/power_supply.h>
32#include <linux/spinlock.h>
33
34/*
35 * Reference the url below for the following HID report defines:
36 * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering

--- 68 unchanged lines hidden (view full) ---

105static const u16 JC_STICK_FUZZ = 250;
106static const u16 JC_STICK_FLAT = 500;
107
108/* Hat values for pro controller's d-pad */
109static const u16 JC_MAX_DPAD_MAG = 1;
110static const u16 JC_DPAD_FUZZ /*= 0*/;
111static const u16 JC_DPAD_FLAT /*= 0*/;
112
31#include <linux/leds.h>
32#include <linux/module.h>
33#include <linux/power_supply.h>
34#include <linux/spinlock.h>
35
36/*
37 * Reference the url below for the following HID report defines:
38 * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering

--- 68 unchanged lines hidden (view full) ---

107static const u16 JC_STICK_FUZZ = 250;
108static const u16 JC_STICK_FLAT = 500;
109
110/* Hat values for pro controller's d-pad */
111static const u16 JC_MAX_DPAD_MAG = 1;
112static const u16 JC_DPAD_FUZZ /*= 0*/;
113static const u16 JC_DPAD_FLAT /*= 0*/;
114
115/* frequency/amplitude tables for rumble */
116struct joycon_rumble_freq_data {
117 u16 high;
118 u8 low;
119 u16 freq; /* Hz*/
120};
121
122struct joycon_rumble_amp_data {
123 u8 high;
124 u16 low;
125 u16 amp;
126};
127
128/*
129 * These tables are from
130 * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
131 */
132static const struct joycon_rumble_freq_data joycon_rumble_frequencies[] = {
133 /* high, low, freq */
134 { 0x0000, 0x01, 41 }, { 0x0000, 0x02, 42 }, { 0x0000, 0x03, 43 },
135 { 0x0000, 0x04, 44 }, { 0x0000, 0x05, 45 }, { 0x0000, 0x06, 46 },
136 { 0x0000, 0x07, 47 }, { 0x0000, 0x08, 48 }, { 0x0000, 0x09, 49 },
137 { 0x0000, 0x0A, 50 }, { 0x0000, 0x0B, 51 }, { 0x0000, 0x0C, 52 },
138 { 0x0000, 0x0D, 53 }, { 0x0000, 0x0E, 54 }, { 0x0000, 0x0F, 55 },
139 { 0x0000, 0x10, 57 }, { 0x0000, 0x11, 58 }, { 0x0000, 0x12, 59 },
140 { 0x0000, 0x13, 60 }, { 0x0000, 0x14, 62 }, { 0x0000, 0x15, 63 },
141 { 0x0000, 0x16, 64 }, { 0x0000, 0x17, 66 }, { 0x0000, 0x18, 67 },
142 { 0x0000, 0x19, 69 }, { 0x0000, 0x1A, 70 }, { 0x0000, 0x1B, 72 },
143 { 0x0000, 0x1C, 73 }, { 0x0000, 0x1D, 75 }, { 0x0000, 0x1e, 77 },
144 { 0x0000, 0x1f, 78 }, { 0x0000, 0x20, 80 }, { 0x0400, 0x21, 82 },
145 { 0x0800, 0x22, 84 }, { 0x0c00, 0x23, 85 }, { 0x1000, 0x24, 87 },
146 { 0x1400, 0x25, 89 }, { 0x1800, 0x26, 91 }, { 0x1c00, 0x27, 93 },
147 { 0x2000, 0x28, 95 }, { 0x2400, 0x29, 97 }, { 0x2800, 0x2a, 99 },
148 { 0x2c00, 0x2b, 102 }, { 0x3000, 0x2c, 104 }, { 0x3400, 0x2d, 106 },
149 { 0x3800, 0x2e, 108 }, { 0x3c00, 0x2f, 111 }, { 0x4000, 0x30, 113 },
150 { 0x4400, 0x31, 116 }, { 0x4800, 0x32, 118 }, { 0x4c00, 0x33, 121 },
151 { 0x5000, 0x34, 123 }, { 0x5400, 0x35, 126 }, { 0x5800, 0x36, 129 },
152 { 0x5c00, 0x37, 132 }, { 0x6000, 0x38, 135 }, { 0x6400, 0x39, 137 },
153 { 0x6800, 0x3a, 141 }, { 0x6c00, 0x3b, 144 }, { 0x7000, 0x3c, 147 },
154 { 0x7400, 0x3d, 150 }, { 0x7800, 0x3e, 153 }, { 0x7c00, 0x3f, 157 },
155 { 0x8000, 0x40, 160 }, { 0x8400, 0x41, 164 }, { 0x8800, 0x42, 167 },
156 { 0x8c00, 0x43, 171 }, { 0x9000, 0x44, 174 }, { 0x9400, 0x45, 178 },
157 { 0x9800, 0x46, 182 }, { 0x9c00, 0x47, 186 }, { 0xa000, 0x48, 190 },
158 { 0xa400, 0x49, 194 }, { 0xa800, 0x4a, 199 }, { 0xac00, 0x4b, 203 },
159 { 0xb000, 0x4c, 207 }, { 0xb400, 0x4d, 212 }, { 0xb800, 0x4e, 217 },
160 { 0xbc00, 0x4f, 221 }, { 0xc000, 0x50, 226 }, { 0xc400, 0x51, 231 },
161 { 0xc800, 0x52, 236 }, { 0xcc00, 0x53, 241 }, { 0xd000, 0x54, 247 },
162 { 0xd400, 0x55, 252 }, { 0xd800, 0x56, 258 }, { 0xdc00, 0x57, 263 },
163 { 0xe000, 0x58, 269 }, { 0xe400, 0x59, 275 }, { 0xe800, 0x5a, 281 },
164 { 0xec00, 0x5b, 287 }, { 0xf000, 0x5c, 293 }, { 0xf400, 0x5d, 300 },
165 { 0xf800, 0x5e, 306 }, { 0xfc00, 0x5f, 313 }, { 0x0001, 0x60, 320 },
166 { 0x0401, 0x61, 327 }, { 0x0801, 0x62, 334 }, { 0x0c01, 0x63, 341 },
167 { 0x1001, 0x64, 349 }, { 0x1401, 0x65, 357 }, { 0x1801, 0x66, 364 },
168 { 0x1c01, 0x67, 372 }, { 0x2001, 0x68, 381 }, { 0x2401, 0x69, 389 },
169 { 0x2801, 0x6a, 397 }, { 0x2c01, 0x6b, 406 }, { 0x3001, 0x6c, 415 },
170 { 0x3401, 0x6d, 424 }, { 0x3801, 0x6e, 433 }, { 0x3c01, 0x6f, 443 },
171 { 0x4001, 0x70, 453 }, { 0x4401, 0x71, 462 }, { 0x4801, 0x72, 473 },
172 { 0x4c01, 0x73, 483 }, { 0x5001, 0x74, 494 }, { 0x5401, 0x75, 504 },
173 { 0x5801, 0x76, 515 }, { 0x5c01, 0x77, 527 }, { 0x6001, 0x78, 538 },
174 { 0x6401, 0x79, 550 }, { 0x6801, 0x7a, 562 }, { 0x6c01, 0x7b, 574 },
175 { 0x7001, 0x7c, 587 }, { 0x7401, 0x7d, 600 }, { 0x7801, 0x7e, 613 },
176 { 0x7c01, 0x7f, 626 }, { 0x8001, 0x00, 640 }, { 0x8401, 0x00, 654 },
177 { 0x8801, 0x00, 668 }, { 0x8c01, 0x00, 683 }, { 0x9001, 0x00, 698 },
178 { 0x9401, 0x00, 713 }, { 0x9801, 0x00, 729 }, { 0x9c01, 0x00, 745 },
179 { 0xa001, 0x00, 761 }, { 0xa401, 0x00, 778 }, { 0xa801, 0x00, 795 },
180 { 0xac01, 0x00, 812 }, { 0xb001, 0x00, 830 }, { 0xb401, 0x00, 848 },
181 { 0xb801, 0x00, 867 }, { 0xbc01, 0x00, 886 }, { 0xc001, 0x00, 905 },
182 { 0xc401, 0x00, 925 }, { 0xc801, 0x00, 945 }, { 0xcc01, 0x00, 966 },
183 { 0xd001, 0x00, 987 }, { 0xd401, 0x00, 1009 }, { 0xd801, 0x00, 1031 },
184 { 0xdc01, 0x00, 1053 }, { 0xe001, 0x00, 1076 }, { 0xe401, 0x00, 1100 },
185 { 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 },
186 { 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 }
187};
188
189#define joycon_max_rumble_amp (1003)
190static const struct joycon_rumble_amp_data joycon_rumble_amplitudes[] = {
191 /* high, low, amp */
192 { 0x00, 0x0040, 0 },
193 { 0x02, 0x8040, 10 }, { 0x04, 0x0041, 12 }, { 0x06, 0x8041, 14 },
194 { 0x08, 0x0042, 17 }, { 0x0a, 0x8042, 20 }, { 0x0c, 0x0043, 24 },
195 { 0x0e, 0x8043, 28 }, { 0x10, 0x0044, 33 }, { 0x12, 0x8044, 40 },
196 { 0x14, 0x0045, 47 }, { 0x16, 0x8045, 56 }, { 0x18, 0x0046, 67 },
197 { 0x1a, 0x8046, 80 }, { 0x1c, 0x0047, 95 }, { 0x1e, 0x8047, 112 },
198 { 0x20, 0x0048, 117 }, { 0x22, 0x8048, 123 }, { 0x24, 0x0049, 128 },
199 { 0x26, 0x8049, 134 }, { 0x28, 0x004a, 140 }, { 0x2a, 0x804a, 146 },
200 { 0x2c, 0x004b, 152 }, { 0x2e, 0x804b, 159 }, { 0x30, 0x004c, 166 },
201 { 0x32, 0x804c, 173 }, { 0x34, 0x004d, 181 }, { 0x36, 0x804d, 189 },
202 { 0x38, 0x004e, 198 }, { 0x3a, 0x804e, 206 }, { 0x3c, 0x004f, 215 },
203 { 0x3e, 0x804f, 225 }, { 0x40, 0x0050, 230 }, { 0x42, 0x8050, 235 },
204 { 0x44, 0x0051, 240 }, { 0x46, 0x8051, 245 }, { 0x48, 0x0052, 251 },
205 { 0x4a, 0x8052, 256 }, { 0x4c, 0x0053, 262 }, { 0x4e, 0x8053, 268 },
206 { 0x50, 0x0054, 273 }, { 0x52, 0x8054, 279 }, { 0x54, 0x0055, 286 },
207 { 0x56, 0x8055, 292 }, { 0x58, 0x0056, 298 }, { 0x5a, 0x8056, 305 },
208 { 0x5c, 0x0057, 311 }, { 0x5e, 0x8057, 318 }, { 0x60, 0x0058, 325 },
209 { 0x62, 0x8058, 332 }, { 0x64, 0x0059, 340 }, { 0x66, 0x8059, 347 },
210 { 0x68, 0x005a, 355 }, { 0x6a, 0x805a, 362 }, { 0x6c, 0x005b, 370 },
211 { 0x6e, 0x805b, 378 }, { 0x70, 0x005c, 387 }, { 0x72, 0x805c, 395 },
212 { 0x74, 0x005d, 404 }, { 0x76, 0x805d, 413 }, { 0x78, 0x005e, 422 },
213 { 0x7a, 0x805e, 431 }, { 0x7c, 0x005f, 440 }, { 0x7e, 0x805f, 450 },
214 { 0x80, 0x0060, 460 }, { 0x82, 0x8060, 470 }, { 0x84, 0x0061, 480 },
215 { 0x86, 0x8061, 491 }, { 0x88, 0x0062, 501 }, { 0x8a, 0x8062, 512 },
216 { 0x8c, 0x0063, 524 }, { 0x8e, 0x8063, 535 }, { 0x90, 0x0064, 547 },
217 { 0x92, 0x8064, 559 }, { 0x94, 0x0065, 571 }, { 0x96, 0x8065, 584 },
218 { 0x98, 0x0066, 596 }, { 0x9a, 0x8066, 609 }, { 0x9c, 0x0067, 623 },
219 { 0x9e, 0x8067, 636 }, { 0xa0, 0x0068, 650 }, { 0xa2, 0x8068, 665 },
220 { 0xa4, 0x0069, 679 }, { 0xa6, 0x8069, 694 }, { 0xa8, 0x006a, 709 },
221 { 0xaa, 0x806a, 725 }, { 0xac, 0x006b, 741 }, { 0xae, 0x806b, 757 },
222 { 0xb0, 0x006c, 773 }, { 0xb2, 0x806c, 790 }, { 0xb4, 0x006d, 808 },
223 { 0xb6, 0x806d, 825 }, { 0xb8, 0x006e, 843 }, { 0xba, 0x806e, 862 },
224 { 0xbc, 0x006f, 881 }, { 0xbe, 0x806f, 900 }, { 0xc0, 0x0070, 920 },
225 { 0xc2, 0x8070, 940 }, { 0xc4, 0x0071, 960 }, { 0xc6, 0x8071, 981 },
226 { 0xc8, 0x0072, joycon_max_rumble_amp }
227};
228
113/* States for controller state machine */
114enum joycon_ctlr_state {
115 JOYCON_CTLR_STATE_INIT,
116 JOYCON_CTLR_STATE_READ,
117};
118
119struct joycon_stick_cal {
120 s32 max;

--- 60 unchanged lines hidden (view full) ---

181 /*
182 * If support for firmware updates, gyroscope data, and/or NFC/IR
183 * are added in the future, this can be swapped for a union.
184 */
185 struct joycon_subcmd_reply reply;
186} __packed;
187
188#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)
229/* States for controller state machine */
230enum joycon_ctlr_state {
231 JOYCON_CTLR_STATE_INIT,
232 JOYCON_CTLR_STATE_READ,
233};
234
235struct joycon_stick_cal {
236 s32 max;

--- 60 unchanged lines hidden (view full) ---

297 /*
298 * If support for firmware updates, gyroscope data, and/or NFC/IR
299 * are added in the future, this can be swapped for a union.
300 */
301 struct joycon_subcmd_reply reply;
302} __packed;
303
304#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)
305#define JC_RUMBLE_DATA_SIZE 8
306#define JC_RUMBLE_QUEUE_SIZE 8
189
307
308static const u16 JC_RUMBLE_DFLT_LOW_FREQ = 160;
309static const u16 JC_RUMBLE_DFLT_HIGH_FREQ = 320;
310static const u16 JC_RUMBLE_PERIOD_MS = 50;
311
190static const char * const joycon_player_led_names[] = {
191 LED_FUNCTION_PLAYER1,
192 LED_FUNCTION_PLAYER2,
193 LED_FUNCTION_PLAYER3,
194 LED_FUNCTION_PLAYER4,
195};
196#define JC_NUM_LEDS ARRAY_SIZE(joycon_player_led_names)
197

--- 23 unchanged lines hidden (view full) ---

221 struct joycon_stick_cal right_stick_cal_y;
222
223 /* power supply data */
224 struct power_supply *battery;
225 struct power_supply_desc battery_desc;
226 u8 battery_capacity;
227 bool battery_charging;
228 bool host_powered;
312static const char * const joycon_player_led_names[] = {
313 LED_FUNCTION_PLAYER1,
314 LED_FUNCTION_PLAYER2,
315 LED_FUNCTION_PLAYER3,
316 LED_FUNCTION_PLAYER4,
317};
318#define JC_NUM_LEDS ARRAY_SIZE(joycon_player_led_names)
319

--- 23 unchanged lines hidden (view full) ---

343 struct joycon_stick_cal right_stick_cal_y;
344
345 /* power supply data */
346 struct power_supply *battery;
347 struct power_supply_desc battery_desc;
348 u8 battery_capacity;
349 bool battery_charging;
350 bool host_powered;
351
352 /* rumble */
353 u8 rumble_data[JC_RUMBLE_QUEUE_SIZE][JC_RUMBLE_DATA_SIZE];
354 int rumble_queue_head;
355 int rumble_queue_tail;
356 struct workqueue_struct *rumble_queue;
357 struct work_struct rumble_worker;
358 unsigned int rumble_msecs;
359 u16 rumble_ll_freq;
360 u16 rumble_lh_freq;
361 u16 rumble_rl_freq;
362 u16 rumble_rh_freq;
229};
230
231static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len)
232{
233 u8 *buf;
234 int ret;
235
236 buf = kmemdup(data, len, GFP_KERNEL);

--- 40 unchanged lines hidden (view full) ---

277 return ret;
278}
279
280static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
281 struct joycon_subcmd_request *subcmd,
282 size_t data_len)
283{
284 int ret;
363};
364
365static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len)
366{
367 u8 *buf;
368 int ret;
369
370 buf = kmemdup(data, len, GFP_KERNEL);

--- 40 unchanged lines hidden (view full) ---

411 return ret;
412}
413
414static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
415 struct joycon_subcmd_request *subcmd,
416 size_t data_len)
417{
418 int ret;
419 unsigned long flags;
285
420
421 spin_lock_irqsave(&ctlr->lock, flags);
422 memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail],
423 JC_RUMBLE_DATA_SIZE);
424 spin_unlock_irqrestore(&ctlr->lock, flags);
425
286 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD;
287 subcmd->packet_num = ctlr->subcmd_num;
288 if (++ctlr->subcmd_num > 0xF)
289 ctlr->subcmd_num = 0;
290 ctlr->subcmd_ack_match = subcmd->subcmd_id;
291 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD;
292
293 ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd,

--- 135 unchanged lines hidden (view full) ---

429 req = (struct joycon_subcmd_request *)buffer;
430 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE;
431 req->data[0] = 0x30; /* standard, full report mode */
432
433 hid_dbg(ctlr->hdev, "setting controller report mode\n");
434 return joycon_send_subcmd(ctlr, req, 1);
435}
436
426 subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD;
427 subcmd->packet_num = ctlr->subcmd_num;
428 if (++ctlr->subcmd_num > 0xF)
429 ctlr->subcmd_num = 0;
430 ctlr->subcmd_ack_match = subcmd->subcmd_id;
431 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD;
432
433 ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd,

--- 135 unchanged lines hidden (view full) ---

569 req = (struct joycon_subcmd_request *)buffer;
570 req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE;
571 req->data[0] = 0x30; /* standard, full report mode */
572
573 hid_dbg(ctlr->hdev, "setting controller report mode\n");
574 return joycon_send_subcmd(ctlr, req, 1);
575}
576
577static int joycon_enable_rumble(struct joycon_ctlr *ctlr)
578{
579 struct joycon_subcmd_request *req;
580 u8 buffer[sizeof(*req) + 1] = { 0 };
581
582 req = (struct joycon_subcmd_request *)buffer;
583 req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION;
584 req->data[0] = 0x01; /* note: 0x00 would disable */
585
586 hid_dbg(ctlr->hdev, "enabling rumble\n");
587 return joycon_send_subcmd(ctlr, req, 1);
588}
589
437static s32 joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val)
438{
439 s32 center = cal->center;
440 s32 min = cal->min;
441 s32 max = cal->max;
442 s32 new_val;
443
444 if (val > center) {

--- 10 unchanged lines hidden (view full) ---

455static void joycon_parse_report(struct joycon_ctlr *ctlr,
456 struct joycon_input_report *rep)
457{
458 struct input_dev *dev = ctlr->input;
459 unsigned long flags;
460 u8 tmp;
461 u32 btns;
462 u32 id = ctlr->hdev->product;
590static s32 joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val)
591{
592 s32 center = cal->center;
593 s32 min = cal->min;
594 s32 max = cal->max;
595 s32 new_val;
596
597 if (val > center) {

--- 10 unchanged lines hidden (view full) ---

608static void joycon_parse_report(struct joycon_ctlr *ctlr,
609 struct joycon_input_report *rep)
610{
611 struct input_dev *dev = ctlr->input;
612 unsigned long flags;
613 u8 tmp;
614 u32 btns;
615 u32 id = ctlr->hdev->product;
616 unsigned long msecs = jiffies_to_msecs(jiffies);
463
617
618 spin_lock_irqsave(&ctlr->lock, flags);
619 if (IS_ENABLED(CONFIG_NINTENDO_FF) && rep->vibrator_report &&
620 (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS)
621 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker);
622
464 /* Parse the battery status */
465 tmp = rep->bat_con;
623 /* Parse the battery status */
624 tmp = rep->bat_con;
466 spin_lock_irqsave(&ctlr->lock, flags);
467 ctlr->host_powered = tmp & BIT(0);
468 ctlr->battery_charging = tmp & BIT(4);
469 tmp = tmp >> 5;
470 switch (tmp) {
471 case 0: /* empty */
472 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
473 break;
474 case 1: /* low */

--- 106 unchanged lines hidden (view full) ---

581 input_report_key(dev, BTN_NORTH, btns & JC_BTN_X);
582 input_report_key(dev, BTN_EAST, btns & JC_BTN_A);
583 input_report_key(dev, BTN_SOUTH, btns & JC_BTN_B);
584 }
585
586 input_sync(dev);
587}
588
625 ctlr->host_powered = tmp & BIT(0);
626 ctlr->battery_charging = tmp & BIT(4);
627 tmp = tmp >> 5;
628 switch (tmp) {
629 case 0: /* empty */
630 ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
631 break;
632 case 1: /* low */

--- 106 unchanged lines hidden (view full) ---

739 input_report_key(dev, BTN_NORTH, btns & JC_BTN_X);
740 input_report_key(dev, BTN_EAST, btns & JC_BTN_A);
741 input_report_key(dev, BTN_SOUTH, btns & JC_BTN_B);
742 }
743
744 input_sync(dev);
745}
746
747static void joycon_rumble_worker(struct work_struct *work)
748{
749 struct joycon_ctlr *ctlr = container_of(work, struct joycon_ctlr,
750 rumble_worker);
751 unsigned long flags;
752 bool again = true;
753 int ret;
589
754
755 while (again) {
756 mutex_lock(&ctlr->output_mutex);
757 ret = joycon_enable_rumble(ctlr);
758 mutex_unlock(&ctlr->output_mutex);
759 if (ret < 0)
760 hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret);
761
762 spin_lock_irqsave(&ctlr->lock, flags);
763 ctlr->rumble_msecs = jiffies_to_msecs(jiffies);
764 if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) {
765 if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE)
766 ctlr->rumble_queue_tail = 0;
767 } else {
768 again = false;
769 }
770 spin_unlock_irqrestore(&ctlr->lock, flags);
771 }
772}
773
774#if IS_ENABLED(CONFIG_NINTENDO_FF)
775static struct joycon_rumble_freq_data joycon_find_rumble_freq(u16 freq)
776{
777 const size_t length = ARRAY_SIZE(joycon_rumble_frequencies);
778 const struct joycon_rumble_freq_data *data = joycon_rumble_frequencies;
779 int i = 0;
780
781 if (freq > data[0].freq) {
782 for (i = 1; i < length - 1; i++) {
783 if (freq > data[i - 1].freq && freq <= data[i].freq)
784 break;
785 }
786 }
787
788 return data[i];
789}
790
791static struct joycon_rumble_amp_data joycon_find_rumble_amp(u16 amp)
792{
793 const size_t length = ARRAY_SIZE(joycon_rumble_amplitudes);
794 const struct joycon_rumble_amp_data *data = joycon_rumble_amplitudes;
795 int i = 0;
796
797 if (amp > data[0].amp) {
798 for (i = 1; i < length - 1; i++) {
799 if (amp > data[i - 1].amp && amp <= data[i].amp)
800 break;
801 }
802 }
803
804 return data[i];
805}
806
807static void joycon_encode_rumble(u8 *data, u16 freq_low, u16 freq_high, u16 amp)
808{
809 struct joycon_rumble_freq_data freq_data_low;
810 struct joycon_rumble_freq_data freq_data_high;
811 struct joycon_rumble_amp_data amp_data;
812
813 freq_data_low = joycon_find_rumble_freq(freq_low);
814 freq_data_high = joycon_find_rumble_freq(freq_high);
815 amp_data = joycon_find_rumble_amp(amp);
816
817 data[0] = (freq_data_high.high >> 8) & 0xFF;
818 data[1] = (freq_data_high.high & 0xFF) + amp_data.high;
819 data[2] = freq_data_low.low + ((amp_data.low >> 8) & 0xFF);
820 data[3] = amp_data.low & 0xFF;
821}
822
823static const u16 JOYCON_MAX_RUMBLE_HIGH_FREQ = 1253;
824static const u16 JOYCON_MIN_RUMBLE_HIGH_FREQ = 82;
825static const u16 JOYCON_MAX_RUMBLE_LOW_FREQ = 626;
826static const u16 JOYCON_MIN_RUMBLE_LOW_FREQ = 41;
827
828static void joycon_clamp_rumble_freqs(struct joycon_ctlr *ctlr)
829{
830 unsigned long flags;
831
832 spin_lock_irqsave(&ctlr->lock, flags);
833 ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq,
834 JOYCON_MIN_RUMBLE_LOW_FREQ,
835 JOYCON_MAX_RUMBLE_LOW_FREQ);
836 ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq,
837 JOYCON_MIN_RUMBLE_HIGH_FREQ,
838 JOYCON_MAX_RUMBLE_HIGH_FREQ);
839 ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq,
840 JOYCON_MIN_RUMBLE_LOW_FREQ,
841 JOYCON_MAX_RUMBLE_LOW_FREQ);
842 ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq,
843 JOYCON_MIN_RUMBLE_HIGH_FREQ,
844 JOYCON_MAX_RUMBLE_HIGH_FREQ);
845 spin_unlock_irqrestore(&ctlr->lock, flags);
846}
847
848static int joycon_set_rumble(struct joycon_ctlr *ctlr, u16 amp_r, u16 amp_l,
849 bool schedule_now)
850{
851 u8 data[JC_RUMBLE_DATA_SIZE];
852 u16 amp;
853 u16 freq_r_low;
854 u16 freq_r_high;
855 u16 freq_l_low;
856 u16 freq_l_high;
857 unsigned long flags;
858
859 spin_lock_irqsave(&ctlr->lock, flags);
860 freq_r_low = ctlr->rumble_rl_freq;
861 freq_r_high = ctlr->rumble_rh_freq;
862 freq_l_low = ctlr->rumble_ll_freq;
863 freq_l_high = ctlr->rumble_lh_freq;
864 spin_unlock_irqrestore(&ctlr->lock, flags);
865
866 /* right joy-con */
867 amp = amp_r * (u32)joycon_max_rumble_amp / 65535;
868 joycon_encode_rumble(data + 4, freq_r_low, freq_r_high, amp);
869
870 /* left joy-con */
871 amp = amp_l * (u32)joycon_max_rumble_amp / 65535;
872 joycon_encode_rumble(data, freq_l_low, freq_l_high, amp);
873
874 spin_lock_irqsave(&ctlr->lock, flags);
875 if (++ctlr->rumble_queue_head >= JC_RUMBLE_QUEUE_SIZE)
876 ctlr->rumble_queue_head = 0;
877 memcpy(ctlr->rumble_data[ctlr->rumble_queue_head], data,
878 JC_RUMBLE_DATA_SIZE);
879 spin_unlock_irqrestore(&ctlr->lock, flags);
880
881 /* don't wait for the periodic send (reduces latency) */
882 if (schedule_now)
883 queue_work(ctlr->rumble_queue, &ctlr->rumble_worker);
884
885 return 0;
886}
887
888static int joycon_play_effect(struct input_dev *dev, void *data,
889 struct ff_effect *effect)
890{
891 struct joycon_ctlr *ctlr = input_get_drvdata(dev);
892
893 if (effect->type != FF_RUMBLE)
894 return 0;
895
896 return joycon_set_rumble(ctlr,
897 effect->u.rumble.weak_magnitude,
898 effect->u.rumble.strong_magnitude,
899 true);
900}
901#endif /* IS_ENABLED(CONFIG_NINTENDO_FF) */
902
590static const unsigned int joycon_button_inputs_l[] = {
591 BTN_SELECT, BTN_Z, BTN_THUMBL,
592 BTN_TL, BTN_TL2,
593 0 /* 0 signals end of array */
594};
595
596static const unsigned int joycon_button_inputs_r[] = {
597 BTN_START, BTN_MODE, BTN_THUMBR,

--- 86 unchanged lines hidden (view full) ---

684 if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL) {
685 input_set_capability(ctlr->input, EV_KEY, BTN_TR);
686 input_set_capability(ctlr->input, EV_KEY, BTN_TR2);
687 } else if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR) {
688 input_set_capability(ctlr->input, EV_KEY, BTN_TL);
689 input_set_capability(ctlr->input, EV_KEY, BTN_TL2);
690 }
691
903static const unsigned int joycon_button_inputs_l[] = {
904 BTN_SELECT, BTN_Z, BTN_THUMBL,
905 BTN_TL, BTN_TL2,
906 0 /* 0 signals end of array */
907};
908
909static const unsigned int joycon_button_inputs_r[] = {
910 BTN_START, BTN_MODE, BTN_THUMBR,

--- 86 unchanged lines hidden (view full) ---

997 if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONL) {
998 input_set_capability(ctlr->input, EV_KEY, BTN_TR);
999 input_set_capability(ctlr->input, EV_KEY, BTN_TR2);
1000 } else if (hdev->product == USB_DEVICE_ID_NINTENDO_JOYCONR) {
1001 input_set_capability(ctlr->input, EV_KEY, BTN_TL);
1002 input_set_capability(ctlr->input, EV_KEY, BTN_TL2);
1003 }
1004
1005#if IS_ENABLED(CONFIG_NINTENDO_FF)
1006 /* set up rumble */
1007 input_set_capability(ctlr->input, EV_FF, FF_RUMBLE);
1008 input_ff_create_memless(ctlr->input, NULL, joycon_play_effect);
1009 ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ;
1010 ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ;
1011 ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ;
1012 ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ;
1013 joycon_clamp_rumble_freqs(ctlr);
1014 joycon_set_rumble(ctlr, 0, 0, false);
1015 ctlr->rumble_msecs = jiffies_to_msecs(jiffies);
1016#endif
1017
692 ret = input_register_device(ctlr->input);
693 if (ret)
694 return ret;
695
696 return 0;
697}
698
699static int joycon_player_led_brightness_set(struct led_classdev *led,

--- 316 unchanged lines hidden (view full) ---

1016 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL);
1017 if (!ctlr) {
1018 ret = -ENOMEM;
1019 goto err;
1020 }
1021
1022 ctlr->hdev = hdev;
1023 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT;
1018 ret = input_register_device(ctlr->input);
1019 if (ret)
1020 return ret;
1021
1022 return 0;
1023}
1024
1025static int joycon_player_led_brightness_set(struct led_classdev *led,

--- 316 unchanged lines hidden (view full) ---

1342 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL);
1343 if (!ctlr) {
1344 ret = -ENOMEM;
1345 goto err;
1346 }
1347
1348 ctlr->hdev = hdev;
1349 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT;
1350 ctlr->rumble_queue_head = JC_RUMBLE_QUEUE_SIZE - 1;
1351 ctlr->rumble_queue_tail = 0;
1024 hid_set_drvdata(hdev, ctlr);
1025 mutex_init(&ctlr->output_mutex);
1026 init_waitqueue_head(&ctlr->wait);
1027 spin_lock_init(&ctlr->lock);
1352 hid_set_drvdata(hdev, ctlr);
1353 mutex_init(&ctlr->output_mutex);
1354 init_waitqueue_head(&ctlr->wait);
1355 spin_lock_init(&ctlr->lock);
1356 ctlr->rumble_queue = alloc_workqueue("hid-nintendo-rumble_wq",
1357 WQ_FREEZABLE | WQ_MEM_RECLAIM, 0);
1358 INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker);
1028
1029 ret = hid_parse(hdev);
1030 if (ret) {
1031 hid_err(hdev, "HID parse failed\n");
1359
1360 ret = hid_parse(hdev);
1361 if (ret) {
1362 hid_err(hdev, "HID parse failed\n");
1032 goto err;
1363 goto err_wq;
1033 }
1034
1035 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1036 if (ret) {
1037 hid_err(hdev, "HW start failed\n");
1364 }
1365
1366 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1367 if (ret) {
1368 hid_err(hdev, "HW start failed\n");
1038 goto err;
1369 goto err_wq;
1039 }
1040
1041 ret = hid_hw_open(hdev);
1042 if (ret) {
1043 hid_err(hdev, "cannot start hardware I/O\n");
1044 goto err_stop;
1045 }
1046

--- 36 unchanged lines hidden (view full) ---

1083
1084 /* Set the reporting mode to 0x30, which is the full report mode */
1085 ret = joycon_set_report_mode(ctlr);
1086 if (ret) {
1087 hid_err(hdev, "Failed to set report mode; ret=%d\n", ret);
1088 goto err_mutex;
1089 }
1090
1370 }
1371
1372 ret = hid_hw_open(hdev);
1373 if (ret) {
1374 hid_err(hdev, "cannot start hardware I/O\n");
1375 goto err_stop;
1376 }
1377

--- 36 unchanged lines hidden (view full) ---

1414
1415 /* Set the reporting mode to 0x30, which is the full report mode */
1416 ret = joycon_set_report_mode(ctlr);
1417 if (ret) {
1418 hid_err(hdev, "Failed to set report mode; ret=%d\n", ret);
1419 goto err_mutex;
1420 }
1421
1422 /* Enable rumble */
1423 ret = joycon_enable_rumble(ctlr);
1424 if (ret) {
1425 hid_err(hdev, "Failed to enable rumble; ret=%d\n", ret);
1426 goto err_mutex;
1427 }
1428
1091 mutex_unlock(&ctlr->output_mutex);
1092
1093 /* Initialize the leds */
1094 ret = joycon_leds_create(ctlr);
1095 if (ret) {
1096 hid_err(hdev, "Failed to create leds; ret=%d\n", ret);
1097 goto err_close;
1098 }

--- 17 unchanged lines hidden (view full) ---

1116 return 0;
1117
1118err_mutex:
1119 mutex_unlock(&ctlr->output_mutex);
1120err_close:
1121 hid_hw_close(hdev);
1122err_stop:
1123 hid_hw_stop(hdev);
1429 mutex_unlock(&ctlr->output_mutex);
1430
1431 /* Initialize the leds */
1432 ret = joycon_leds_create(ctlr);
1433 if (ret) {
1434 hid_err(hdev, "Failed to create leds; ret=%d\n", ret);
1435 goto err_close;
1436 }

--- 17 unchanged lines hidden (view full) ---

1454 return 0;
1455
1456err_mutex:
1457 mutex_unlock(&ctlr->output_mutex);
1458err_close:
1459 hid_hw_close(hdev);
1460err_stop:
1461 hid_hw_stop(hdev);
1462err_wq:
1463 destroy_workqueue(ctlr->rumble_queue);
1124err:
1125 hid_err(hdev, "probe - fail = %d\n", ret);
1126 return ret;
1127}
1128
1129static void nintendo_hid_remove(struct hid_device *hdev)
1130{
1464err:
1465 hid_err(hdev, "probe - fail = %d\n", ret);
1466 return ret;
1467}
1468
1469static void nintendo_hid_remove(struct hid_device *hdev)
1470{
1471 struct joycon_ctlr *ctlr = hid_get_drvdata(hdev);
1472
1131 hid_dbg(hdev, "remove\n");
1473 hid_dbg(hdev, "remove\n");
1474 destroy_workqueue(ctlr->rumble_queue);
1132 hid_hw_close(hdev);
1133 hid_hw_stop(hdev);
1134}
1135
1136static const struct hid_device_id nintendo_hid_devices[] = {
1137 { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO,
1138 USB_DEVICE_ID_NINTENDO_PROCON) },
1139 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,

--- 21 unchanged lines hidden ---
1475 hid_hw_close(hdev);
1476 hid_hw_stop(hdev);
1477}
1478
1479static const struct hid_device_id nintendo_hid_devices[] = {
1480 { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO,
1481 USB_DEVICE_ID_NINTENDO_PROCON) },
1482 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,

--- 21 unchanged lines hidden ---