-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhid-nswitch.c
615 lines (534 loc) · 15.8 KB
/
hid-nswitch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* HID driver for Nintendo Switch peripherals
* Copyright (c) 2018 Nabil Boutemeur <[email protected]>
*/
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include "hid-nswitch.h"
DEFINE_SPINLOCK(global_lock);
__u8 allocated_players[8];
LIST_HEAD(ljoycons);
LIST_HEAD(rjoycons);
LIST_HEAD(procontrollers);
/* Event Handler */
void simplejc_prepare(nswitch_dev *ndev);
/* Worker Thread */
void handshake_rumble(nswitch_dev *ndev) {
nswitch_dev_input_report res;
res = ns_exchange(ndev, &(output_command) {
RUMBLE_REPORT, 0, 0, {
0xc2, 0xc8, 0x03, 0x72,
0xc2, 0xc8, 0x03, 0x72
}, SET_VIBRATION, {}
});
res = ns_exchange(ndev, &(output_command) {
RUMBLE_REPORT, 0, 0, {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
}, SET_VIBRATION, {}
});
/*
res = ns_exchange(ndev, &(output_command) {
RUMBLE_REPORT, 0, 0, {
0xc2, 0xc8, 0x03, 0x72,
0xc2, 0xc8, 0x03, 0x72
}, SET_VIBRATION
});
res = ns_exchange(ndev, &(output_command) {
RUMBLE_REPORT, 0, 0, {
0xc2, 0xc8, 0x60, 0x64,
0xc2, 0xc8, 0x60, 0x64
}, SET_VIBRATION
});
*/
}
static struct list_head *select_list(enum nswitch_dev_type type) {
switch(type) {
case LEFT_JOYCON:
return &ljoycons;
case RIGHT_JOYCON:
return &rjoycons;
case PRO_CONTROLLER:
return &procontrollers;
default:
break;
}
return 0;
}
void dump_mem(struct hid_device *hdev, __u8 *s, int size);
static ssize_t nswitch_dev_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
return sprintf(buf, "something\n");
}
/*
Requires cmd_lock
*/
/* TODO: rewrite the mecanism to work from any thread */
int nd_send_cmd(nswitch_dev *ndev, output_command *oc) {
oc->gpn = ++ndev->cmdcounter;
if (oc->report == BASIC) {
ndev->reply = oc->subcommand;
hid_info(ndev->hdev, "Sending command %02x", oc->subcommand);
reinit_completion(&ndev->cmd_pending);
}
hid_hw_output_report(ndev->hdev, (void*)oc, sizeof(*oc));
return 1;
}
/* Worker Thread */
int nd_wait(nswitch_dev *jdev, struct completion *smth) {
int ret;
ret = wait_for_completion_interruptible(smth);
if (ret < 0)
return -ERESTARTSYS;
return ret;
}
/* Worker Thread */
int nd_wait_reply(nswitch_dev *jdev) {
if (!jdev->reply) {
hid_err(jdev->hdev, "No reply awaiting\n");
return 0;
}
return nd_wait(jdev, &jdev->cmd_pending);
}
/* Worker Thread */
static void init_calibration_data(nswitch_dev *ndev) {
nswitch_dev_input_report res;
left_stick_calibration_data *lscd;
right_stick_calibration_data *rscd;
sax_calibration_data *scd;
spi_read_reply *srr;
__u32 *coeff;
/* TODO: Refactor */
srr = (void*)&res.full.reply.data;
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SPI_FLASH_READ, {
.spi_read = USER_CALIBRATION_LEFT_STICK
}
});
if (srr->data[0] != 0xB2 || srr->data[1] != 0xA1) {
hid_info(ndev->hdev, "No LS user config, loading factory settings...");
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SPI_FLASH_READ, {
.spi_read = FACTORY_CALIBRATION_LEFT_STICK
}
});
lscd = (void*)srr->data;
} else
lscd = (void*)(srr->data + 2);
ndev->calibration.left_stick = *lscd;
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SPI_FLASH_READ, {
.spi_read = USER_CALIBRATION_RIGHT_STICK
}
});
if (srr->data[0] != 0xB2 || srr->data[1] != 0xA1) {
hid_info(ndev->hdev, "No RS user config, loading factory settings...");
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SPI_FLASH_READ, {
.spi_read = FACTORY_CALIBRATION_RIGHT_STICK
}
});
rscd = (void*)srr->data;
} else
rscd = (void*)(srr->data + 2);
ndev->calibration.right_stick = *rscd;
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SPI_FLASH_READ, {
.spi_read = USER_CALIBRATION_6AXIS
}
});
if (srr->data[0] != 0xB2 || srr->data[1] != 0xA1) {
hid_info(ndev->hdev, "No 6AXIS user config, loading factory settings...");
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SPI_FLASH_READ, {
.spi_read = FACTORY_CALIBRATION_6AXIS
}
});
scd = (void*)srr->data;
} else
scd = (void*)(srr->data + 2);
ndev->calibration.sax = *scd;
coeff = ndev->calibration.gyro_coeff;
coeff[0] = 816 / (13371 - (scd->gyroscope_origin[0]));
coeff[1] = 816 / (13371 - (scd->gyroscope_origin[1]));
coeff[2] = 816 / (13371 - (scd->gyroscope_origin[2]));
}
/* Event Handler */
static void prepare_projoypad(nswitch_dev *ndev) {
calibration_data *lcd, *rcd;
unsigned int i;
lcd = &ndev->calibration;
rcd = &ndev->calibration;
ndev->siminput = input_allocate_device();
input_set_drvdata(ndev->siminput, ndev);
ndev->siminput->dev.parent = &ndev->hdev->dev;
ndev->siminput->id.bustype = ndev->hdev->bus;
ndev->siminput->id.vendor = ndev->hdev->vendor;
ndev->siminput->id.product = ndev->hdev->product;
ndev->siminput->id.version = ndev->hdev->version;
ndev->siminput->name = kasprintf(GFP_KERNEL, "%s Dual Joycon Controller" , ndev->hdev->name);
set_bit(EV_KEY, ndev->siminput->evbit);
for (i = 0; i < ARRAY_SIZE(ns_buttons); ++i)
set_bit(ns_buttons[i], ndev->siminput->keybit);
set_bit(EV_ABS, ndev->siminput->evbit);
set_bit(ABS_X, ndev->siminput->absbit);
set_bit(ABS_Y, ndev->siminput->absbit);
set_bit(ABS_RX, ndev->siminput->absbit);
set_bit(ABS_RY, ndev->siminput->absbit);
input_set_abs_params(ndev->siminput, ABS_X,
lcd->left_stick.xcenter - lcd->left_stick.xmin_offset,
lcd->left_stick.xcenter + lcd->left_stick.xmax_offset, 10, 0);
input_set_abs_params(ndev->siminput, ABS_Y,
lcd->left_stick.ycenter - lcd->left_stick.ymin_offset,
lcd->left_stick.ycenter + lcd->left_stick.ymax_offset, 10, 0);
input_set_abs_params(ndev->siminput, ABS_RX,
rcd->right_stick.xcenter - rcd->right_stick.xmin_offset,
rcd->right_stick.xcenter + rcd->right_stick.xmax_offset, 10, 0);
input_set_abs_params(ndev->siminput, ABS_RY,
rcd->right_stick.ycenter - rcd->right_stick.ymin_offset,
rcd->right_stick.ycenter + rcd->right_stick.ymax_offset, 10, 0);
//ndev->handler = report_dual_keys;
hid_info(ndev->hdev, "Handler set to report keys...");
input_register_device(ndev->siminput);
ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SET_INPUT_REPORT_MODE, {
.mode = STANDARD
}});
}
/* Event Handler */
static void projc_prepare(nswitch_dev *ndev) {
if (ndev->ledcache >> 4 != 0xF) {
set_leds(ndev, 0xF0);
}
if (ndev->state.simple.right) {
/* TODO: Implement this */
hid_info(ndev->hdev, "ProJC validated");
prepare_projoypad(ndev);
} else if (ndev->state.simple.down) {
hid_info(ndev->hdev, "ProJC unvalidated");
ndev->handler = projc_prepare;
}
}
/*
Should only be called from a worker thread
*/
/* Worker Thread */
nswitch_dev_input_report ns_exchange(nswitch_dev *ndev,
output_command *oc) {
unsigned long flags;
nswitch_dev_input_report ret = {0};
int r;
spin_lock_irqsave(&ndev->cmd_lock, flags);
if (ndev->reply) {
hid_err(ndev->hdev, "Command %02x pending, waiting for it to finish", ndev->reply);
spin_unlock_irqrestore(&ndev->cmd_lock, flags);
r = wait_for_completion_interruptible_timeout(&ndev->cmd_pending, HZ);
spin_lock_irqsave(&ndev->cmd_lock, flags);
if (r <= 0 || ndev->reply) {
hid_err(ndev->hdev, "Something's gone horribly wrong %d %02x\n", r, ndev->reply);
goto end;
}
}
nd_send_cmd(ndev, oc);
spin_unlock_irqrestore(&ndev->cmd_lock, flags);
if (ndev->deinit) {
hid_err(ndev->hdev, "Device is deiniting\n");
goto end;
}
if(oc->report == BASIC) {
nd_wait_reply(ndev);
spin_lock_irqsave(&ndev->state_lock, flags);
memcpy(&ret, &ndev->state, sizeof(ret));
spin_unlock_irqrestore(&ndev->state_lock, flags);
}
end:
return ret;
}
/* Worker Thread */
static void init_rumble(nswitch_dev *ndev) {
ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, SET_VIBRATION, {
.vibrate = 1
}
});
}
static void init_ir_cam(nswitch_dev *ndev) {
/* TODO: */
}
static void init_home_led(nswitch_dev *ndev) {
/* TODO: */
}
/*
Put the device in a simple state.
Get info on the device and initialize the needed devices in sysfs
*/
/* Worker Thread */
void nswitch_dev_init_worker(struct work_struct *work)
{
nswitch_dev *ndev = container_of(work,
nswitch_dev,
init_worker);
nswitch_dev_input_report res;
nswitch_devinfo *info = (void*)&res.full.reply.data;
unsigned long flags;
struct list_head *target;
nswitch_list *nl;
res = ns_exchange(ndev, &(output_command) {
BASIC, 0, 0, {}, DEVICE_INFO, {}
});
hid_info(ndev->hdev, "Type: ID: %d\n", info->type);
hid_info(ndev->hdev, "New %s. v%d.%d. (%02x:%02x:%02x:%02x:%02x:%02x)",
ndev->hdev->name,
info->vmajor, info->vminor,
info->mac[0], info->mac[1], info->mac[2],
info->mac[3], info->mac[4], info->mac[5]);
ndev->info = *info;
hid_info(ndev->hdev, "Allocated at: %p\n", ndev);
dump_mem(ndev->hdev, (void*)&ndev->info, sizeof(*info));
init_player_leds(ndev);
init_battery(ndev);
ndev->inited_hw = 1;
//init_keys(ndev);
//init_axis(ndev);
init_rumble(ndev);
init_calibration_data(ndev);
// TODO: exposes rom/ram/spi into char devices
//init_memory_map(ndev);
handshake_rumble(ndev);
switch(info->type) {
case RIGHT_JOYCON:
init_ir_cam(ndev);
/* fallthrough */
case PRO_CONTROLLER:
init_home_led(ndev);
/* fallthrough */
case LEFT_JOYCON:
ndev->handler = ((update_fun_t[]){
&simplejc_prepare,
&simplejc_prepare,
&projc_prepare
})[info->type - LEFT_JOYCON];
break;
default:
hid_info(ndev->hdev, "Unknown device type %d\n", info->type);
return;
}
nl = kzalloc(sizeof(*nl), GFP_KERNEL);
INIT_LIST_HEAD(&nl->list);
nl->ndev = ndev;
target = select_list(info->type);
spin_lock_irqsave(&global_lock, flags);
list_add(target, &nl->list);
spin_unlock_irqrestore(&global_lock, flags);
while (nd_wait(ndev, &ndev->state_pending) >= 0 && !ndev->deinit) {
reinit_completion(&ndev->state_pending);
ndev->handler(ndev);
}
}
static DEVICE_ATTR(devtype, S_IRUGO, nswitch_dev_show, NULL);
/* Event Handler */
static nswitch_dev *nswitch_dev_create(struct hid_device *hdev,
const struct hid_device_id *id)
{
nswitch_dev *nsd;
nsd = kzalloc(sizeof(*nsd), GFP_KERNEL);
if (!nsd)
return NULL;
memset(nsd, 0, sizeof(*nsd));
nsd->hdev = hdev;
hid_set_drvdata(hdev, nsd);
spin_lock_init(&nsd->state_lock);
spin_lock_init(&nsd->cmd_lock);
init_completion(&nsd->cmd_pending);
init_completion(&nsd->state_pending);
INIT_WORK(&nsd->init_worker, nswitch_dev_init_worker);
//INIT_WORK(&nsd->cmd_worker, nswitch_dev_cmd_worker);
schedule_work(&nsd->init_worker);
return nsd;
}
/* Event Handler */
static int nswitch_hid_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
int ret;
nswitch_dev *nsdev;
nsdev = nswitch_dev_create(hdev, id);
if (!nsdev) {
hid_err(hdev, "Can't alloc device\n");
return -ENOMEM;
}
hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
ret = hid_parse(hdev);
if (ret) {
hid_err(hdev, "HID parse failed\n");
goto err;
}
ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
if (ret) {
hid_err(hdev, "HW start failed\n");
goto err;
}
ret = hid_hw_open(hdev);
if (ret) {
hid_err(hdev, "cannot start hardware I/O\n");
goto err_stop;
}
ret = device_create_file(&hdev->dev, &dev_attr_devtype);
if (ret) {
hid_err(hdev, "cannot create sysfs attribute\n");
goto err_close;
}
hid_info(hdev, "New device registered\n");
return 0;
err_close:
hid_hw_close(hdev);
err_stop:
hid_hw_stop(hdev);
err:
kfree(nsdev);
return ret;
}
void dump_mem(struct hid_device *hdev, __u8 *s, int size) {
int i = 0;
__u8 buf[8];
while (size > 0) {
memset(buf, 0xFE, sizeof(buf));
memcpy(buf, s + i, size >= 8 ? 8 : size);
hid_warn(hdev, "%d: %02x%02x %02x%02x %02x%02x %02x%02x",
i,
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5], buf[6], buf[7]
);
size -= 8;
i += 8;
}
}
/* Event Handler */
static int nswitch_hid_event(struct hid_device *hdev,
struct hid_report *report,
u8 *raw_data,
int size) {
nswitch_dev *nsdev = hid_get_drvdata(hdev);
nswitch_dev_input_report *rep;
unsigned long flags;
if ((unsigned)size > sizeof(*rep)) {
hid_warn(hdev, "Input report too big");
return 1;
}
rep = (void*) raw_data;
spin_lock_irqsave(&nsdev->state_lock, flags);
nsdev->state.input_report = rep->input_report;
spin_unlock_irqrestore(&nsdev->state_lock, flags);
switch (rep->input_report) {
case REPLY:
dump_mem(hdev, raw_data, size);
if (nsdev->reply != rep->full.reply.reply_to) {
hid_warn(nsdev->hdev, "Got a reply for an unsollicited command");
hid_warn(nsdev->hdev, "Expected %02x, got %02x", nsdev->reply, rep->full.reply.reply_to);
return 1;
} else {
hid_info(nsdev->hdev, "Reply %02x received on event handler\n", nsdev->reply);
}
spin_lock_irqsave(&nsdev->cmd_lock, flags);
nsdev->reply = 0;
spin_unlock_irqrestore(&nsdev->cmd_lock, flags);
spin_lock_irqsave(&nsdev->state_lock, flags);
nsdev->reply_data = rep->full.reply;
spin_unlock_irqrestore(&nsdev->state_lock, flags);
complete_all(&nsdev->cmd_pending);
/* fall through */
case STANDARD:
case STD_NFCIR:
case STD_UNKNOWN0:
case STD_UNKNOWN1:
spin_lock_irqsave(&nsdev->state_lock, flags);
nsdev->state.full = rep->full;
spin_unlock_irqrestore(&nsdev->state_lock, flags);
break;
case SIMPLE:
spin_lock_irqsave(&nsdev->state_lock, flags);
nsdev->state.simple = rep->simple;
spin_unlock_irqrestore(&nsdev->state_lock, flags);
break;
default:
hid_warn(hdev, "Unhandled input report type %02x", rep->input_report);
}
complete_all(&nsdev->state_pending);
return 0;
}
/* Event Handler */
static void nswitch_hid_remove(struct hid_device *hdev) {
int i;
nswitch_dev *ndev = hid_get_drvdata(hdev);
ndev->deinit = 1;
complete_all(&ndev->state_pending);
hid_info(hdev, "remove requested");
cancel_work_sync(&ndev->init_worker);
//cancel_work_sync(&ndev->cmd_worker);
if (ndev->inited_hw) {
for (i = 0; i < 4; ++i)
led_classdev_unregister(ndev->player_leds + i);
power_supply_unregister(ndev->battery);
kfree(ndev->battery_desc.name);
//kfree(ndev->siminput->name);
//ndev->siminput->name = 0;
if (ndev->siminput) {
input_unregister_device(ndev->siminput);
}
if (ndev->right) {
if (ndev->info.type == LEFT_JOYCON) {
ndev->right->handler = &simplejc_prepare;
ns_exchange(ndev->right, &(output_command) {
BASIC, 0, 0, {}, SET_INPUT_REPORT_MODE, {
.mode = SIMPLE
}});
ndev->right->right = 0;
ndev->right = 0;
}
}
}
hid_info(hdev, "finished disabling hardware");
device_remove_file(&hdev->dev, &dev_attr_devtype);
hid_hw_close(hdev);
hid_hw_stop(hdev);
kfree(ndev);
}
static const struct hid_device_id nswitch_hid_devices[] = {
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
USB_DEVICE_ID_NINTENDO_JOYCON_L) },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
USB_DEVICE_ID_NINTENDO_JOYCON_R) },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
USB_DEVICE_ID_NINTENDO_NS_PRO_CONTROLLER) },
/* { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO,
USB_DEVICE_ID_NINTENDO_NS_PRO_CONTROLLER) },*/
{ }
};
MODULE_DEVICE_TABLE(hid, nswitch_hid_devices);
static struct hid_driver nswitch_hid_driver = {
.name = "nswitch",
.id_table = nswitch_hid_devices,
.probe = nswitch_hid_probe,
.remove = nswitch_hid_remove,
.raw_event = nswitch_hid_event
};
static int __init nswitch_hid_driver_init(void)
{
/* TODO: init RPC file */
return hid_register_driver(&nswitch_hid_driver);
}
static void __exit nswitch_hid_driver_exit(void)
{
hid_unregister_driver(&nswitch_hid_driver);
}
module_init(nswitch_hid_driver_init);
module_exit(nswitch_hid_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Nabil Boutemeur <[email protected]>");
MODULE_DESCRIPTION("Driver for Nintendo Switch peripherals");