-
Notifications
You must be signed in to change notification settings - Fork 256
/
ds34bt.c
1704 lines (1410 loc) · 60.1 KB
/
ds34bt.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* based on https://github.com/IonAgorria/Arduino-PSRemote */
/* and https://github.com/felis/USB_Host_Shield_2.0 */
#include "types.h"
#include "loadcore.h"
#include "stdio.h"
#include "sifrpc.h"
#include "sysclib.h"
#include "usbd.h"
#include "usbd_macro.h"
#include "thbase.h"
#include "thsemap.h"
#include "ds34bt.h"
IRX_ID("ds34bt", 1, 1);
//#define DPRINTF(x...) printf(x)
#define DPRINTF(x...)
static int bt_probe(int devId);
static int bt_connect(int devId);
static int bt_disconnect(int devId);
static void bt_config_set(int result, int count, void *arg);
static UsbDriver bt_driver = {NULL, NULL, "ds34bt", bt_probe, bt_connect, bt_disconnect};
static bt_device bt_dev = {-1, -1, -1, -1, -1, -1, DS34BT_STATE_USB_DISCONNECTED};
static int chrg_probe(int devId);
static int chrg_connect(int devId);
static int chrg_disconnect(int devId);
static int chrg_dev = -1;
static UsbDriver chrg_driver = {NULL, NULL, "ds34chrg", chrg_probe, chrg_connect, chrg_disconnect};
static void ds34pad_clear(int pad);
static void ds34pad_init();
static int bt_probe(int devId)
{
UsbDeviceDescriptor *device = NULL;
UsbConfigDescriptor *config = NULL;
UsbInterfaceDescriptor *intf = NULL;
DPRINTF("DS34BT: probe: devId=%i\n", devId);
if ((bt_dev.devId > 0) && (bt_dev.status & DS34BT_STATE_USB_AUTHORIZED)) {
DPRINTF("DS34BT: Error - only one device allowed !\n");
return 0;
}
device = (UsbDeviceDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_DEVICE);
if (device == NULL) {
DPRINTF("DS34BT: Error - Couldn't get device descriptor\n");
return 0;
}
if (device->bNumConfigurations < 1)
return 0;
config = (UsbConfigDescriptor *)UsbGetDeviceStaticDescriptor(devId, device, USB_DT_CONFIG);
if (config == NULL) {
DPRINTF("DS34BT: Error - Couldn't get configuration descriptor\n");
return 0;
}
if ((config->bNumInterfaces < 1) || (config->wTotalLength < (sizeof(UsbConfigDescriptor) + sizeof(UsbInterfaceDescriptor)))) {
DPRINTF("DS34BT: Error - No interfaces available\n");
return 0;
}
intf = (UsbInterfaceDescriptor *)((char *)config + config->bLength);
DPRINTF("DS34BT: bInterfaceClass %X bInterfaceSubClass %X bInterfaceProtocol %X\n", intf->bInterfaceClass, intf->bInterfaceSubClass, intf->bInterfaceProtocol);
if ((intf->bInterfaceClass != USB_CLASS_WIRELESS_CONTROLLER) ||
(intf->bInterfaceSubClass != USB_SUBCLASS_RF_CONTROLLER) ||
(intf->bInterfaceProtocol != USB_PROTOCOL_BLUETOOTH_PROG) ||
(intf->bNumEndpoints < 3)) {
return 0;
}
return 1;
}
static int bt_connect(int devId)
{
int epCount;
UsbDeviceDescriptor *device;
UsbConfigDescriptor *config;
UsbInterfaceDescriptor *interface;
UsbEndpointDescriptor *endpoint;
DPRINTF("DS34BT: connect: devId=%i\n", devId);
if (bt_dev.devId != -1) {
DPRINTF("DS34BT: Error - only one device allowed !\n");
return 1;
}
bt_dev.status = DS34BT_STATE_USB_DISCONNECTED;
bt_dev.interruptEndp = -1;
bt_dev.inEndp = -1;
bt_dev.outEndp = -1;
bt_dev.controlEndp = UsbOpenEndpoint(devId, NULL);
device = (UsbDeviceDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_DEVICE);
config = (UsbConfigDescriptor *)UsbGetDeviceStaticDescriptor(devId, device, USB_DT_CONFIG);
interface = (UsbInterfaceDescriptor *)((char *)config + config->bLength);
bt_dev.vid = device->idVendor;
bt_dev.pid = device->idProduct;
bt_dev.rev = device->bcdDevice;
epCount = interface->bNumEndpoints - 1;
DPRINTF("DS34BT: Endpoint Count %d \n", epCount + 1);
endpoint = (UsbEndpointDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_ENDPOINT);
do {
if (endpoint->bmAttributes == USB_ENDPOINT_XFER_BULK) {
if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT && bt_dev.outEndp < 0) {
bt_dev.outEndp = UsbOpenEndpointAligned(devId, endpoint);
DPRINTF("DS34BT: register Output endpoint id =%i addr=%02X packetSize=%i\n", bt_dev.outEndp, endpoint->bEndpointAddress, (unsigned short int)endpoint->wMaxPacketSizeHB << 8 | endpoint->wMaxPacketSizeLB);
} else if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN && bt_dev.inEndp < 0) {
bt_dev.inEndp = UsbOpenEndpointAligned(devId, endpoint);
DPRINTF("DS34BT: register Input endpoint id =%i addr=%02X packetSize=%i\n", bt_dev.inEndp, endpoint->bEndpointAddress, (unsigned short int)endpoint->wMaxPacketSizeHB << 8 | endpoint->wMaxPacketSizeLB);
}
} else if (endpoint->bmAttributes == USB_ENDPOINT_XFER_INT) {
if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN && bt_dev.interruptEndp < 0) {
bt_dev.interruptEndp = UsbOpenEndpoint(devId, endpoint);
DPRINTF("DS34BT: register Interrupt endpoint id =%i addr=%02X packetSize=%i\n", bt_dev.interruptEndp, endpoint->bEndpointAddress, (unsigned short int)endpoint->wMaxPacketSizeHB << 8 | endpoint->wMaxPacketSizeLB);
}
}
endpoint = (UsbEndpointDescriptor *)((char *)endpoint + endpoint->bLength);
} while (epCount--);
if (bt_dev.interruptEndp < 0 || bt_dev.inEndp < 0 || bt_dev.outEndp < 0) {
DPRINTF("DS34BT: Error - connect failed: not enough endpoints! \n");
return -1;
}
bt_dev.devId = devId;
bt_dev.status = DS34BT_STATE_USB_AUTHORIZED;
UsbSetDeviceConfiguration(bt_dev.controlEndp, config->bConfigurationValue, bt_config_set, NULL);
return 0;
}
static int bt_disconnect(int devId)
{
DPRINTF("DS34BT: disconnect: devId=%i\n", devId);
if (bt_dev.status & DS34BT_STATE_USB_AUTHORIZED) {
if (bt_dev.interruptEndp >= 0)
UsbCloseEndpoint(bt_dev.interruptEndp);
if (bt_dev.inEndp >= 0)
UsbCloseEndpoint(bt_dev.inEndp);
if (bt_dev.outEndp >= 0)
UsbCloseEndpoint(bt_dev.outEndp);
bt_dev.devId = -1;
bt_dev.interruptEndp = -1;
bt_dev.inEndp = -1;
bt_dev.outEndp = -1;
bt_dev.controlEndp = -1;
bt_dev.status = DS34BT_STATE_USB_DISCONNECTED;
ds34pad_init();
SignalSema(bt_dev.hid_sema);
}
return 0;
}
int chrg_probe(int devId)
{
UsbDeviceDescriptor *device = NULL;
DPRINTF("DS34CHRG: probe: devId=%i\n", devId);
device = (UsbDeviceDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_DEVICE);
if (device == NULL) {
DPRINTF("DS34CHRG: Error - Couldn't get device descriptor\n");
return 0;
}
if (device->idVendor == DS34_VID && (device->idProduct == DS3_PID || device->idProduct == DS4_PID || device->idProduct == DS4_PID_SLIM))
return 1;
return 0;
}
int chrg_connect(int devId)
{
int chrg_end;
UsbDeviceDescriptor *device;
UsbConfigDescriptor *config;
DPRINTF("DS34CHRG: connect: devId=%i\n", devId);
if (chrg_dev != -1) {
DPRINTF("DS34CHRG: Error - only one device allowed !\n");
return 1;
}
chrg_dev = devId;
chrg_end = UsbOpenEndpoint(devId, NULL);
device = (UsbDeviceDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_DEVICE);
config = (UsbConfigDescriptor *)UsbGetDeviceStaticDescriptor(devId, device, USB_DT_CONFIG);
UsbSetDeviceConfiguration(chrg_end, config->bConfigurationValue, NULL, NULL);
return 0;
}
int chrg_disconnect(int devId)
{
DPRINTF("DS34CHRG: disconnect: devId=%i\n", devId);
chrg_dev = -1;
return 0;
}
static u8 output_01_report[] =
{
0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x02,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
static u8 led_patterns[][2] =
{
{0x1C, 0x02},
{0x1A, 0x04},
{0x16, 0x08},
{0x0E, 0x10},
};
static u8 power_level[] =
{
0x00, 0x00, 0x02, 0x06, 0x0E, 0x1E};
static u8 rgbled_patterns[][2][3] =
{
{{0x00, 0x00, 0x10}, {0x00, 0x00, 0x7F}}, // light blue/blue
{{0x00, 0x10, 0x00}, {0x00, 0x7F, 0x00}}, // light green/green/
{{0x10, 0x10, 0x00}, {0x7F, 0x7F, 0x00}}, // light yellow/yellow
{{0x00, 0x10, 0x10}, {0x00, 0x7F, 0x7F}}, // light cyan/cyan
};
static u8 link_key[] = //for ds4 authorisation
{
0x56, 0xE8, 0x81, 0x38, 0x08, 0x06, 0x51, 0x41,
0xC0, 0x7F, 0x12, 0xAA, 0xD9, 0x66, 0x3C, 0xCE
};
// Taken from nefarius' SCPToolkit
// https://github.com/nefarius/ScpToolkit/blob/master/ScpControl/ScpControl.ini
// Valid MAC addresses used by Sony
static u8 GenuineMacAddress[][3] =
{
// Bluetooth chips by ALPS ELECTRIC CO., LTD
{0x00, 0x02, 0xC7},
{0x00, 0x06, 0xF5},
{0x00, 0x06, 0xF7},
{0x00, 0x07, 0x04},
{0x00, 0x16, 0xFE},
{0x00, 0x19, 0xC1},
{0x00, 0x1B, 0xFB},
{0x00, 0x1E, 0x3D},
{0x00, 0x21, 0x4F},
{0x00, 0x23, 0x06},
{0x00, 0x24, 0x33},
{0x00, 0x26, 0x43},
{0x00, 0xA0, 0x79},
{0x04, 0x76, 0x6E},
{0x04, 0x98, 0xF3},
{0x28, 0xA1, 0x83},
{0x34, 0xC7, 0x31},
{0x38, 0xC0, 0x96},
{0x60, 0x38, 0x0E},
{0x64, 0xD4, 0xBD},
{0xAC, 0x7A, 0x4D},
{0xE0, 0x75, 0x0A},
{0xE0, 0xAE, 0x5E},
{0xFC, 0x62, 0xB9},
// Bluetooth chips by AzureWave Technology Inc.
{0xE0, 0xB9, 0xA5},
{0xDC, 0x85, 0xDE},
{0xD0, 0xE7, 0x82},
{0xB0, 0xEE, 0x45},
{0xAC, 0x89, 0x95},
{0xA8, 0x1D, 0x16},
{0x94, 0xDB, 0xC9},
{0x80, 0xD2, 0x1D},
{0x80, 0xA5, 0x89},
{0x78, 0x18, 0x81},
{0x74, 0xF0, 0x6D},
{0x74, 0xC6, 0x3B},
{0x74, 0x2F, 0x68},
{0x6C, 0xAD, 0xF8},
{0x6C, 0x71, 0xD9},
{0x60, 0x5B, 0xB4},
{0x5C, 0x96, 0x56},
{0x54, 0x27, 0x1E},
{0x4C, 0xAA, 0x16},
{0x48, 0x5D, 0x60},
{0x44, 0xD8, 0x32},
{0x40, 0xE2, 0x30},
{0x38, 0x4F, 0xF0},
{0x28, 0xC2, 0xDD},
{0x24, 0x0A, 0x64},
{0x1C, 0x4B, 0xD6},
{0x08, 0xA9, 0x5A},
{0x00, 0x25, 0xD3},
{0x00, 0x24, 0x23},
{0x00, 0x22, 0x43},
{0x00, 0x15, 0xAF},
//fake with AirohaTechnologyCorp's Chip
{0x0C, 0xFC, 0x83}};
#define REQ_HCI_OUT (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE)
#define HCI_COMMAND_REQ 0
#define MAX_PADS 2
static u8 bt_bdaddr[6]; //usb bt adapter mac address
static hci_information_t bt_version; //bt adapter version information
static u8 bt_features[8]; //bt adapter supported features
static u8 hci_buf[MAX_BUFFER_SIZE] __attribute((aligned(4))) = {0};
static u8 l2cap_buf[MAX_BUFFER_SIZE + 32] __attribute((aligned(4))) = {0};
static u8 hci_cmd_buf[MAX_BUFFER_SIZE] __attribute((aligned(4))) = {0};
static u8 l2cap_cmd_buf[MAX_BUFFER_SIZE + 32] __attribute((aligned(4))) = {0};
static u8 identifier;
static u8 press_emu;
static u8 disable_fake = 0;
static ds34bt_pad_t ds34pad[MAX_PADS];
static void hci_event_cb(int resultCode, int bytes, void *arg);
static void l2cap_event_cb(int resultCode, int bytes, void *arg);
static void ds34pad_init();
static int hid_initDS34(int pad);
static int hid_LEDRumble(u8 *led, u8 lrum, u8 rrum, int pad);
static void hid_readReport(u8 *data, int bytes, int pad);
static int l2cap_connection_request(u16 handle, u8 rxid, u16 scid, u16 psm);
static int hci_reset();
static void bt_config_set(int result, int count, void *arg)
{
PollSema(bt_dev.hid_sema);
UsbInterruptTransfer(bt_dev.interruptEndp, hci_buf, MAX_BUFFER_SIZE, hci_event_cb, NULL);
UsbBulkTransfer(bt_dev.inEndp, l2cap_buf, MAX_BUFFER_SIZE, l2cap_event_cb, NULL);
ds34pad_init();
hci_reset();
SignalSema(bt_dev.hid_sema);
}
/************************************************************/
/* HCI Commands */
/************************************************************/
static int HCI_Command(int nbytes, u8 *dataptr)
{
return UsbControlTransfer(bt_dev.controlEndp, REQ_HCI_OUT, HCI_COMMAND_REQ, 0, 0, nbytes, dataptr, NULL, NULL);
}
static int hci_reset()
{
hci_cmd_buf[0] = HCI_OCF_RESET;
hci_cmd_buf[1] = HCI_OGF_CTRL_BBAND;
hci_cmd_buf[2] = 0x00; // Parameter Total Length = 0
return HCI_Command(3, hci_cmd_buf);
}
static int hci_write_scan_enable(u8 conf)
{
hci_cmd_buf[0] = HCI_OCF_WRITE_SCAN_ENABLE;
hci_cmd_buf[1] = HCI_OGF_CTRL_BBAND;
hci_cmd_buf[2] = 0x01;
hci_cmd_buf[3] = conf;
return HCI_Command(4, hci_cmd_buf);
}
static int hci_accept_connection(u8 *bdaddr)
{
hci_cmd_buf[0] = HCI_OCF_ACCEPT_CONNECTION; // HCI OCF = 9
hci_cmd_buf[1] = HCI_OGF_LINK_CNTRL; // HCI OGF = 1
hci_cmd_buf[2] = 0x07; // parameter length 7
hci_cmd_buf[3] = *bdaddr; // 6 octet bluetooth address
hci_cmd_buf[4] = *(bdaddr + 1);
hci_cmd_buf[5] = *(bdaddr + 2);
hci_cmd_buf[6] = *(bdaddr + 3);
hci_cmd_buf[7] = *(bdaddr + 4);
hci_cmd_buf[8] = *(bdaddr + 5);
hci_cmd_buf[9] = 0x01; //switch role to (slave = 1 / master = 0)
return HCI_Command(10, hci_cmd_buf);
}
static int hci_remote_name(u8 *bdaddr)
{
hci_cmd_buf[0] = HCI_OCF_REMOTE_NAME; // HCI OCF = 19
hci_cmd_buf[1] = HCI_OGF_LINK_CNTRL; // HCI OGF = 1
hci_cmd_buf[2] = 0x0A; // parameter length = 10
hci_cmd_buf[3] = *bdaddr; // 6 octet bluetooth address
hci_cmd_buf[4] = *(bdaddr + 1);
hci_cmd_buf[5] = *(bdaddr + 2);
hci_cmd_buf[6] = *(bdaddr + 3);
hci_cmd_buf[7] = *(bdaddr + 4);
hci_cmd_buf[8] = *(bdaddr + 5);
hci_cmd_buf[9] = 0x01; // Page Scan Repetition Mode
hci_cmd_buf[10] = 0x00; // Reserved
hci_cmd_buf[11] = 0x00; // Clock offset - low byte
hci_cmd_buf[12] = 0x00; // Clock offset - high byte
return HCI_Command(13, hci_cmd_buf);
}
static int hci_reject_connection(u8 *bdaddr)
{
hci_cmd_buf[0] = HCI_OCF_REJECT_CONNECTION; // HCI OCF = A
hci_cmd_buf[1] = HCI_OGF_LINK_CNTRL; // HCI OGF = 1
hci_cmd_buf[2] = 0x07; // parameter length 7
hci_cmd_buf[3] = *bdaddr; // 6 octet bluetooth address
hci_cmd_buf[4] = *(bdaddr + 1);
hci_cmd_buf[5] = *(bdaddr + 2);
hci_cmd_buf[6] = *(bdaddr + 3);
hci_cmd_buf[7] = *(bdaddr + 4);
hci_cmd_buf[8] = *(bdaddr + 5);
hci_cmd_buf[9] = 0x09; //reason max connection
return HCI_Command(10, hci_cmd_buf);
}
static int hci_disconnect(u16 handle)
{
hci_cmd_buf[0] = HCI_OCF_DISCONNECT; // HCI OCF = 6
hci_cmd_buf[1] = HCI_OGF_LINK_CNTRL; // HCI OGF = 1
hci_cmd_buf[2] = 0x03; // parameter length = 3
hci_cmd_buf[3] = (u8)(handle & 0xFF); // connection handle - low byte
hci_cmd_buf[4] = (u8)((handle >> 8) & 0x0F); // connection handle - high byte
hci_cmd_buf[5] = 0x13; // reason
return HCI_Command(6, hci_cmd_buf);
}
static int hci_change_connection_type(u16 handle)
{
hci_cmd_buf[0] = HCI_OCF_CHANGE_CONNECTION_TYPE;
hci_cmd_buf[1] = HCI_OGF_LINK_CNTRL;
hci_cmd_buf[2] = 0x04; // parameter length 4
hci_cmd_buf[3] = (u8)(handle & 0xFF); // connection handle - low byte
hci_cmd_buf[4] = (u8)((handle >> 8) & 0x0F); // connection handle - high byte
hci_cmd_buf[5] = 0x18; // Packet Type: 0xcc18
hci_cmd_buf[6] = 0xcc;
return HCI_Command(7, hci_cmd_buf);
}
static int hci_read_bdaddr()
{
hci_cmd_buf[0] = HCI_OCF_READ_BDADDR; // HCI OCF = 9
hci_cmd_buf[1] = HCI_OGF_INFO_PARAM; // HCI OGF = 4
hci_cmd_buf[2] = 0x00;
return HCI_Command(3, hci_cmd_buf);
}
static int hci_read_local_version_information()
{
hci_cmd_buf[0] = HCI_OCF_READ_VERSION; // HCI OCF = 9
hci_cmd_buf[1] = HCI_OGF_INFO_PARAM; // HCI OGF = 4
hci_cmd_buf[2] = 0x00;
return HCI_Command(3, hci_cmd_buf);
}
static int hci_read_local_supported_features()
{
hci_cmd_buf[0] = HCI_OCF_READ_FEATURES; // HCI OCF = 9
hci_cmd_buf[1] = HCI_OGF_INFO_PARAM; // HCI OGF = 4
hci_cmd_buf[2] = 0x00;
return HCI_Command(3, hci_cmd_buf);
}
static int hci_link_key_request_reply(u8 *bdaddr)
{
hci_cmd_buf[0] = HCI_OCF_LINK_KEY_REQUEST_REPLY; // HCI OCF = 0E
hci_cmd_buf[1] = HCI_OGF_LINK_CNTRL; // HCI OGF = 1
hci_cmd_buf[2] = 0x06 + sizeof(link_key); // parameter length 6
hci_cmd_buf[3] = *bdaddr; // 6 octet bluetooth address
hci_cmd_buf[4] = *(bdaddr + 1);
hci_cmd_buf[5] = *(bdaddr + 2);
hci_cmd_buf[6] = *(bdaddr + 3);
hci_cmd_buf[7] = *(bdaddr + 4);
hci_cmd_buf[8] = *(bdaddr + 5);
mips_memcpy(&hci_cmd_buf[9], link_key, sizeof(link_key));
return HCI_Command(9 + sizeof(link_key), hci_cmd_buf);
}
static void HCI_event_task(int result)
{
int i, pad;
if (!result) {
/* buf[0] = Event Code */
/* buf[1] = Parameter Total Length */
/* buf[n] = Event Parameters based on each event */
DPRINTF("HCI event = 0x%02X \n", hci_buf[0]);
switch (hci_buf[0]) { // switch on event type
case HCI_EVENT_COMMAND_COMPLETE:
DPRINTF("HCI Command Complete = 0x%02X \n", hci_buf[3]);
DPRINTF("\tReturned = 0x%02X \n", hci_buf[5]);
if ((hci_buf[3] == HCI_OCF_RESET) && (hci_buf[4] == HCI_OGF_CTRL_BBAND)) {
if (hci_buf[5] == 0) {
hci_read_bdaddr();
} else {
DelayThread(500);
hci_reset();
}
} else if ((hci_buf[3] == HCI_OCF_READ_BDADDR) && (hci_buf[4] == HCI_OGF_INFO_PARAM)) {
if (hci_buf[5] == 0) {
mips_memcpy(bt_bdaddr, &hci_buf[6], 6);
hci_read_local_version_information();
} else {
DelayThread(500);
hci_reset();
}
} else if ((hci_buf[3] == HCI_OCF_READ_VERSION) && (hci_buf[4] == HCI_OGF_INFO_PARAM)) {
if (hci_buf[5] == 0) {
mips_memcpy(&bt_version, &hci_buf[6], sizeof(hci_information_t));
hci_read_local_supported_features();
} else {
DelayThread(500);
hci_reset();
}
} else if ((hci_buf[3] == HCI_OCF_READ_FEATURES) && (hci_buf[4] == HCI_OGF_INFO_PARAM)) {
if (hci_buf[5] == 0) {
mips_memcpy(bt_features, &hci_buf[6], sizeof(bt_features));
hci_write_scan_enable(SCAN_ENABLE_NOINQ_ENPAG);
} else {
DelayThread(500);
hci_reset();
}
} else if((hci_buf[3] == HCI_OCF_WRITE_SCAN_ENABLE) && (hci_buf[4] == HCI_OGF_CTRL_BBAND)) {
if (hci_buf[5] == 0) {
bt_dev.status |= DS34BT_STATE_USB_CONFIGURED;
} else {
DelayThread(500);
hci_reset();
}
}
break;
case HCI_EVENT_COMMAND_STATUS:
if (hci_buf[2]) // show status on serial if not OK
{
DPRINTF("HCI Command Failed: \n");
DPRINTF("\t Status = 0x%02X \n", hci_buf[2]);
DPRINTF("\t Command_OpCode(OGF) = 0x%02X \n", ((hci_buf[5] & 0xFC) >> 2));
DPRINTF("\t Command_OpCode(OCF) = 0x%02X 0x%02X \n", (hci_buf[5] & 0x03), hci_buf[4]);
}
break;
case HCI_EVENT_CONNECT_COMPLETE:
DPRINTF("HCI event Connect Complete: \n");
if (!hci_buf[2]) // check if connected OK
{
DPRINTF("\t Connection_Handle 0x%02X \n", hci_buf[3] | ((hci_buf[4] & 0x0F) << 8));
DPRINTF("\t Requested by BD_ADDR: \n\t");
for (i = 0; i < 6; i++) {
DPRINTF("0x%02X", hci_buf[5 + i]);
if (i < 5)
DPRINTF(":");
}
DPRINTF("\n");
for (i = 0; i < MAX_PADS; i++) {
if (strncmp(ds34pad[i].bdaddr, hci_buf + 5, 6) == 0) {
// store the handle for the ACL connection
ds34pad[i].hci_handle = hci_buf[3] | ((hci_buf[4] & 0x0F) << 8);
break;
}
}
if (i >= MAX_PADS) {
break;
}
pad_status_set(DS34BT_STATE_CONNECTED, i);
hci_remote_name(ds34pad[i].bdaddr);
} else {
DPRINTF("\t Error 0x%02X \n", hci_buf[2]);
}
break;
case HCI_EVENT_NUM_COMPLETED_PKT:
DPRINTF("HCI Number Of Completed Packets Event: \n");
DPRINTF("\t Number_of_Handles = 0x%02X \n", hci_buf[2]);
for (i = 0; i < hci_buf[2]; i++) {
DPRINTF("\t Connection_Handle = 0x%04X \n", (hci_buf[3 + i] | ((hci_buf[4 + i] & 0x0F) << 8)));
}
break;
case HCI_EVENT_QOS_SETUP_COMPLETE:
break;
case HCI_EVENT_DISCONN_COMPLETE:
DPRINTF("HCI Disconnection Complete Event: \n");
DPRINTF("\t Status = 0x%02X \n", hci_buf[2]);
DPRINTF("\t Connection_Handle = 0x%04X \n", (hci_buf[3] | ((hci_buf[4] & 0x0F) << 8)));
DPRINTF("\t Reason = 0x%02X \n", hci_buf[5]);
for (i = 0; i < MAX_PADS; i++) { //detect pad
if (ds34pad[i].hci_handle == (hci_buf[3] | ((hci_buf[4] & 0x0F) << 8))) {
break;
}
}
ds34pad_clear(i);
break;
case HCI_EVENT_AUTHENTICATION_COMPLETE:
DPRINTF("HCI Authentication Complete Event: \n");
DPRINTF("\t Status = 0x%02X \n", hci_buf[2]);
DPRINTF("\t Connection_Handle = 0x%04X \n", (hci_buf[3] | ((hci_buf[4] & 0x0F) << 8)));
if (!hci_buf[2]) {
DPRINTF("\t Success \n");
} else {
DPRINTF("\t Failed \n");
}
break;
case HCI_EVENT_REMOTE_NAME_COMPLETE:
DPRINTF("HCI Remote Name Requested Complete Event: \n");
DPRINTF("\t Status = 0x%02X \n", hci_buf[2]);
if (!hci_buf[2]) {
for (i = 0; i < MAX_PADS; i++) {
if (strncmp(ds34pad[i].bdaddr, hci_buf + 3, 6) == 0) {
break;
}
}
if (i >= MAX_PADS) {
break;
}
if (strncmp(hci_buf + 9, "Wireless Controller", 19) == 0) {
ds34pad[i].type = DS4;
ds34pad[i].isfake = 0;
DPRINTF("\t Type: Dualshock 4 \n");
} else {
ds34pad[i].type = DS3;
DPRINTF("\t Type: Dualshock 3 \n");
}
hci_change_connection_type(ds34pad[i].hci_handle);
if (ds34pad[i].type == DS4) {
identifier++;
l2cap_connection_request(ds34pad[i].hci_handle, identifier, 0x0070, L2CAP_PSM_CTRL);
}
}
break;
case HCI_EVENT_ENCRYPTION_CHANGE:
DPRINTF("HCI Encryption Change Event: \n");
DPRINTF("\t Status = 0x%02X \n", hci_buf[2]);
DPRINTF("\t Connection_Handle = 0x%04X \n", (hci_buf[3] | ((hci_buf[4] & 0x0F) << 8)));
DPRINTF("\t Encryption_Enabled = 0x%02X \n", hci_buf[5]);
break;
case HCI_EVENT_CONNECT_REQUEST:
DPRINTF("HCI Connection Requested by BD_ADDR: \n\t");
for (i = 0; i < 6; i++) {
DPRINTF("0x%02X", hci_buf[2 + i]);
if (i < 5)
DPRINTF(":");
}
DPRINTF("\n\t Link = 0x%02X \n", hci_buf[11]);
DPRINTF("\t Class = 0x%02X 0x%02X 0x%02X \n", hci_buf[8], hci_buf[9], hci_buf[10]);
for (i = 0; i < MAX_PADS; i++) //find free slot
{
if (!pad_status_check(DS34BT_STATE_RUNNING, i) && ds34pad[i].enabled) {
if (pad_status_check(DS34BT_STATE_CONNECTED, i)) {
if (pad_status_check(DS34BT_STATE_DISCONNECTING, i)) //if we're waiting for hci disconnect event
continue;
else
hci_disconnect(ds34pad[i].hci_handle); //try to disconnect
}
break;
}
}
if (i >= MAX_PADS) //no free slot
{
hci_reject_connection(hci_buf + 2);
break;
}
pad = i;
mips_memcpy(ds34pad[pad].bdaddr, hci_buf + 2, 6);
ds34pad[pad].isfake = 0;
if (!disable_fake) {
ds34pad[pad].isfake = 1; //fake ds3
for (i = 0; i < sizeof(GenuineMacAddress) / 3; i++) //check if ds3 is genuine
{
if (ds34pad[pad].bdaddr[5] == GenuineMacAddress[i][0] &&
ds34pad[pad].bdaddr[4] == GenuineMacAddress[i][1] &&
ds34pad[pad].bdaddr[3] == GenuineMacAddress[i][2]) {
ds34pad[pad].isfake = 0;
break;
}
}
}
pad_status_clear(DS34BT_STATE_CONNECTED, pad);
pad_status_clear(DS34BT_STATE_RUNNING, pad);
pad_status_clear(DS34BT_STATE_DISCONNECTING, pad);
hci_accept_connection(ds34pad[pad].bdaddr);
break;
case HCI_EVENT_ROLE_CHANGED:
DPRINTF("HCI Role Change Event: \n");
DPRINTF("\t Status = 0x%02X \n", hci_buf[2]);
DPRINTF("\t BD_ADDR: ");
for (i = 0; i < 6; i++) {
DPRINTF("0x%02X", hci_buf[3 + i]);
if (i < 5)
DPRINTF(":");
}
DPRINTF("\n\t Role 0x%02X \n", hci_buf[9]);
break;
case HCI_EVENT_MAX_SLOT_CHANGE:
DPRINTF("HCI Max Slot Change Event: \n");
DPRINTF("\t Connection_Handle = 0x%x \n", (hci_buf[2] | ((hci_buf[3] & 0x0F) << 8)));
DPRINTF("\t LMP Max Slots = 0x%x \n", hci_buf[5]);
break;
case HCI_EVENT_PIN_CODE_REQUEST:
DPRINTF("HCI Pin Code Request Event \n");
break;
case HCI_EVENT_LINK_KEY_REQUEST:
DPRINTF("HCI Link Key Request Event by BD_ADDR: \n\t");
for (i = 0; i < 6; i++) {
DPRINTF("0x%02X", hci_buf[2 + i]);
if (i < 5)
DPRINTF(":");
}
DPRINTF("\n");
hci_link_key_request_reply(hci_buf + 2);
break;
case HCI_EVENT_CHANGED_CONNECTION_TYPE:
DPRINTF("Packet Type Changed STATUS: 0x%x \n", hci_buf[2]);
DPRINTF("\t Type = %x \n", (hci_buf[5] | (hci_buf[6] << 8)));
break;
case HCI_EVENT_PAGE_SR_CHANGED:
break;
default:
DPRINTF("Unmanaged Event: 0x%02X \n", hci_buf[0]);
break;
} // switch (buf[0])
}
}
static void ds34pad_clear(int pad)
{
if (pad >= MAX_PADS)
return;
ds34pad[pad].hci_handle = 0x0FFF;
ds34pad[pad].control_scid = 0;
ds34pad[pad].interrupt_scid = 0;
mips_memset(ds34pad[pad].bdaddr, 0, 6);
ds34pad[pad].status = bt_dev.status;
ds34pad[pad].isfake = 0;
ds34pad[pad].type = 0;
ds34pad[pad].data[0] = 0xFF;
ds34pad[pad].data[1] = 0xFF;
mips_memset(&ds34pad[pad].data[2], 0x7F, 4);
mips_memset(&ds34pad[pad].data[6], 0x00, 12);
}
static void ds34pad_init()
{
int i;
for (i = 0; i < MAX_PADS; i++) {
ds34pad_clear(i);
}
press_emu = 0;
identifier = 0;
}
static void hci_event_cb(int resultCode, int bytes, void *arg)
{
PollSema(bt_dev.hid_sema);
HCI_event_task(resultCode);
UsbInterruptTransfer(bt_dev.interruptEndp, hci_buf, MAX_BUFFER_SIZE, hci_event_cb, NULL);
SignalSema(bt_dev.hid_sema);
}
/************************************************************/
/* L2CAP Commands */
/************************************************************/
static int L2CAP_Command(u16 handle, u8 *data, u8 length)
{
l2cap_cmd_buf[0] = (u8)(handle & 0xff); // HCI handle with PB,BC flag
l2cap_cmd_buf[1] = (u8)(((handle >> 8) & 0x0f) | 0x20);
l2cap_cmd_buf[2] = (u8)((4 + length) & 0xff); // HCI ACL total data length
l2cap_cmd_buf[3] = (u8)((4 + length) >> 8);
l2cap_cmd_buf[4] = (u8)(length & 0xff); // L2CAP header: Length
l2cap_cmd_buf[5] = (u8)(length >> 8);
l2cap_cmd_buf[6] = 0x01; // L2CAP header: Channel ID
l2cap_cmd_buf[7] = 0x00; // L2CAP Signalling channel over ACL-U logical link
mips_memcpy(&l2cap_cmd_buf[8], data, length);
// output on endpoint 2
return UsbBulkTransfer(bt_dev.outEndp, l2cap_cmd_buf, (8 + length), NULL, NULL);
}
static int l2cap_connection_request(u16 handle, u8 rxid, u16 scid, u16 psm)
{
u8 cmd_buf[8];
cmd_buf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
cmd_buf[1] = rxid; // Identifier
cmd_buf[2] = 0x04; // Length
cmd_buf[3] = 0x00;
cmd_buf[4] = (u8)(psm & 0xff); // PSM
cmd_buf[5] = (u8)(psm >> 8);
cmd_buf[6] = (u8)(scid & 0xff); // Source CID (PS Remote)
cmd_buf[7] = (u8)(scid >> 8);
return L2CAP_Command(handle, cmd_buf, 8);
}
static int l2cap_connection_response(u16 handle, u8 rxid, u16 dcid, u16 scid, u8 result)
{
u8 cmd_buf[12];
cmd_buf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
cmd_buf[1] = rxid; // Identifier
cmd_buf[2] = 0x08; // Length
cmd_buf[3] = 0x00;
cmd_buf[4] = (u8)(dcid & 0xff); // Destination CID (Our)
cmd_buf[5] = (u8)(dcid >> 8);
cmd_buf[6] = (u8)(scid & 0xff); // Source CID (PS Remote)
cmd_buf[7] = (u8)(scid >> 8);
cmd_buf[8] = result; // Result
cmd_buf[9] = 0x00;
cmd_buf[10] = 0x00; // Status
cmd_buf[11] = 0x00;
if (result != 0)
cmd_buf[10] = 0x01; // Authentication pending
return L2CAP_Command(handle, cmd_buf, 12);
}
static int l2cap_config_request(u16 handle, u8 rxid, u16 dcid)
{
u8 cmd_buf[12];
cmd_buf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
cmd_buf[1] = rxid; // Identifier
cmd_buf[2] = 0x08; // Length
cmd_buf[3] = 0x00;
cmd_buf[4] = (u8)(dcid & 0xff); // Destination CID
cmd_buf[5] = (u8)(dcid >> 8);
cmd_buf[6] = 0x00; // Flags
cmd_buf[7] = 0x00;
cmd_buf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit)
cmd_buf[9] = 0x02; // Config Opt: length
//cmd_buf[10] = 0x96; // Config Opt: data
//cmd_buf[11] = 0x00;
//this setting disable hid cmd reports from ds3
cmd_buf[10] = 0xFF; // Config Opt: data
cmd_buf[11] = 0xFF;
return L2CAP_Command(handle, cmd_buf, 12);
}
static int l2cap_config_response(u16 handle, u8 rxid, u16 scid)
{
u8 cmd_buf[14];
cmd_buf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
cmd_buf[1] = rxid; // Identifier
cmd_buf[2] = 0x0A; // Length
cmd_buf[3] = 0x00;
cmd_buf[4] = (u8)(scid & 0xff); // Source CID
cmd_buf[5] = (u8)(scid >> 8);
cmd_buf[6] = 0x00; // Result
cmd_buf[7] = 0x00;
cmd_buf[8] = 0x00; // Config
cmd_buf[9] = 0x00;
cmd_buf[10] = 0x01; // Config
cmd_buf[11] = 0x02;
cmd_buf[12] = 0xA0;
cmd_buf[13] = 0x02;
return L2CAP_Command(handle, cmd_buf, 14);
}
static int l2cap_disconnection_request(u16 handle, u8 rxid, u16 dcid, u16 scid)
{
u8 cmd_buf[8];
cmd_buf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
cmd_buf[1] = rxid; // Identifier
cmd_buf[2] = 0x04; // Length
cmd_buf[3] = 0x00;
cmd_buf[4] = (u8)(dcid & 0xff); // Destination CID
cmd_buf[5] = (u8)(dcid >> 8);
cmd_buf[6] = (u8)(scid & 0xff); // Source CID
cmd_buf[7] = (u8)(scid >> 8);
return L2CAP_Command(handle, cmd_buf, 8);
}
static int l2cap_disconnection_response(u16 handle, u8 rxid, u16 scid, u16 dcid)
{
u8 cmd_buf[8];
cmd_buf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
cmd_buf[1] = rxid; // Identifier
cmd_buf[2] = 0x04; // Length
cmd_buf[3] = 0x00;
cmd_buf[4] = (u8)(dcid & 0xff); // Destination CID
cmd_buf[5] = (u8)(dcid >> 8);
cmd_buf[6] = (u8)(scid & 0xff); // Source CID
cmd_buf[7] = (u8)(scid >> 8);
return L2CAP_Command(handle, cmd_buf, 8);
}
#define CMD_DELAY 2
static int L2CAP_event_task(int result, int bytes)
{
int pad = -1;
u16 control_dcid = 0x0040; // Channel endpoint on command source
u16 interrupt_dcid = 0x0041; // Channel endpoint on interrupt source
if (!result) {
for (pad = 0; pad < MAX_PADS; pad++) {
if (l2cap_handle_ok(ds34pad[pad].hci_handle))
break;
}
if (pad >= MAX_PADS) {
DPRINTF("L2CAP Wrong Handle = 0x%04X\n", ((l2cap_buf[0] | (l2cap_buf[1] << 8))));
return pad;
}
if (l2cap_handle_ok(ds34pad[pad].hci_handle)) {
if (ds34pad[pad].type == DS4) {
control_dcid = 0x0070;
interrupt_dcid = 0x0071;
}
if (l2cap_control_channel) {
DPRINTF("L2CAP Signaling Command = 0x%02X, pad %d \n", l2cap_buf[8], pad);
switch (l2cap_buf[8]) {
case L2CAP_CMD_COMMAND_REJECT: