-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathDsHidMiniDrv.c
1897 lines (1513 loc) · 42.7 KB
/
DsHidMiniDrv.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
#include "Driver.h"
#include "DsHidMiniDrv.tmh"
PWSTR G_DsHidMini_Strings[] =
{
L"String 0" // TODO: bug in DMF module? Seems required for serial string to work
};
#define DSHIDMINI_DEVICE_STRING L"DS3 Compatible HID Device"
#define DSHIDMINI_MANUFACTURER_STRING L"Nefarius Software Solutions e.U."
#define DSHIDMINI_PRODUCT_STRING L"DS3 Compatible HID Device"
#define DSHIDMINI_SERIAL_NUMBER_STRING L"TODO: use device address"
// This macro declares the following function:
// DMF_CONTEXT_GET()
//
DMF_MODULE_DECLARE_CONTEXT(DsHidMini)
// This macro declares the following function:
// DMF_CONFIG_GET()
//
DMF_MODULE_DECLARE_CONFIG(DsHidMini)
//
// Bootstrap DMF initialization
//
#pragma code_seg("PAGE")
_IRQL_requires_max_(PASSIVE_LEVEL)
_Must_inspect_result_
NTSTATUS
DMF_DsHidMini_Create(
_In_ WDFDEVICE Device,
_In_ DMF_MODULE_ATTRIBUTES* DmfModuleAttributes,
_In_ WDF_OBJECT_ATTRIBUTES* ObjectAttributes,
_Out_ DMFMODULE* DmfModule
)
{
NTSTATUS status;
DMF_MODULE_DESCRIPTOR dsHidMiniDesc;
DMF_CALLBACKS_DMF dsHidMiniCallbacks;
PDEVICE_CONTEXT pDevCtx;
PAGED_CODE();
FuncEntry(TRACE_DSHIDMINIDRV);
do
{
pDevCtx = DeviceGetContext(Device);
//
// Set defaults
//
DS_DRIVER_CONFIGURATION_INIT_DEFAULTS(&pDevCtx->Configuration);
//
// Set Virtual HID Mini properties
//
DMF_CALLBACKS_DMF_INIT(&dsHidMiniCallbacks);
dsHidMiniCallbacks.ChildModulesAdd = DMF_DsHidMini_ChildModulesAdd;
dsHidMiniCallbacks.DeviceOpen = DMF_DsHidMini_Open;
dsHidMiniCallbacks.DeviceClose = DMF_DsHidMini_Close;
DMF_MODULE_DESCRIPTOR_INIT_CONTEXT_TYPE(
dsHidMiniDesc,
DsHidMini,
DMF_CONTEXT_DsHidMini,
DMF_MODULE_OPTIONS_PASSIVE,
DMF_MODULE_OPEN_OPTION_OPEN_D0EntrySystemPowerUp
);
dsHidMiniDesc.CallbacksDmf = &dsHidMiniCallbacks;
status = DMF_ModuleCreate(
Device,
DmfModuleAttributes,
ObjectAttributes,
&dsHidMiniDesc,
DmfModule);
if (!NT_SUCCESS(status))
{
TraceError(
TRACE_DSHIDMINIDRV,
"DMF_ModuleCreate failed with status %!STATUS!",
status
);
break;
}
}
while (FALSE);
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
#pragma code_seg()
#pragma region DMF Initialization and clean-up
//
// Bootstraps VirtualHidMini DMF module
//
#pragma code_seg("PAGE")
_Function_class_(DMF_ChildModulesAdd)
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
DMF_DsHidMini_ChildModulesAdd(
_In_ DMFMODULE DmfModule,
_In_ DMF_MODULE_ATTRIBUTES* DmfParentModuleAttributes,
_In_ PDMFMODULE_INIT DmfModuleInit
)
{
DMF_MODULE_ATTRIBUTES moduleAttributes;
DMF_CONTEXT_DsHidMini* moduleContext;
DMF_CONFIG_VirtualHidMini vHidCfg;
PDEVICE_CONTEXT pDevCtx;
PAGED_CODE();
UNREFERENCED_PARAMETER(DmfParentModuleAttributes);
FuncEntry(TRACE_DSHIDMINIDRV);
moduleContext = DMF_CONTEXT_GET(DmfModule);
pDevCtx = DeviceGetContext(DMF_ParentDeviceGet(DmfModule));
DMF_CONFIG_VirtualHidMini_AND_ATTRIBUTES_INIT(&vHidCfg,
&moduleAttributes);
vHidCfg.GetInputReport = DsHidMini_GetInputReport;
vHidCfg.GetFeature = DsHidMini_GetFeature;
vHidCfg.SetFeature = DsHidMini_SetFeature;
vHidCfg.SetOutputReport = DsHidMini_SetOutputReport;
vHidCfg.WriteReport = DsHidMini_WriteReport;
vHidCfg.StringSizeCbManufacturer = sizeof(DSHIDMINI_MANUFACTURER_STRING);
vHidCfg.StringManufacturer = DSHIDMINI_MANUFACTURER_STRING;
vHidCfg.StringSizeCbProduct = sizeof(DSHIDMINI_PRODUCT_STRING);
vHidCfg.StringProduct = DSHIDMINI_PRODUCT_STRING;
vHidCfg.StringSizeCbSerialNumber = sizeof(DSHIDMINI_SERIAL_NUMBER_STRING);
vHidCfg.StringSerialNumber = DSHIDMINI_SERIAL_NUMBER_STRING;
vHidCfg.Strings = G_DsHidMini_Strings;
vHidCfg.NumberOfStrings = 1;
DMF_DmfModuleAdd(
DmfModuleInit,
&moduleAttributes,
WDF_NO_OBJECT_ATTRIBUTES,
&moduleContext->DmfModuleVirtualHidMini
);
FuncExitNoReturn(TRACE_DSHIDMINIDRV);
}
#pragma code_seg()
//
// Read/refresh configuration here after power-up
//
#pragma code_seg("PAGE")
_Function_class_(DMF_Open)
_IRQL_requires_max_(PASSIVE_LEVEL)
_Must_inspect_result_
static
NTSTATUS
DMF_DsHidMini_Open(
_In_ DMFMODULE DmfModule
)
{
NTSTATUS status = STATUS_SUCCESS;
DMF_CONTEXT_DsHidMini* moduleContext;
DMF_CONFIG_VirtualHidMini* pHidCfg;
PDEVICE_CONTEXT pDevCtx;
UNREFERENCED_PARAMETER(DmfModule);
PAGED_CODE();
FuncEntry(TRACE_DSHIDMINIDRV);
moduleContext = DMF_CONTEXT_GET(DmfModule);
pDevCtx = DeviceGetContext(DMF_ParentDeviceGet(DmfModule));
pHidCfg = DMF_ModuleConfigGet(moduleContext->DmfModuleVirtualHidMini);
//
// Update settings queried in PrepareHardware
//
pHidCfg->VendorId = pDevCtx->VendorId;
pHidCfg->ProductId = pDevCtx->ProductId;
pHidCfg->VersionNumber = pDevCtx->VersionNumber;
pHidCfg->HidDeviceAttributes.VendorID = pDevCtx->VendorId;
pHidCfg->HidDeviceAttributes.ProductID = pDevCtx->ProductId;
pHidCfg->HidDeviceAttributes.VersionNumber = pDevCtx->VersionNumber;
switch (pDevCtx->Configuration.HidDeviceMode)
{
case DsHidMiniDeviceModeSingle:
pHidCfg->HidDescriptor = &G_Ds3HidDescriptor_Single_Mode;
pHidCfg->HidDescriptorLength = sizeof(G_Ds3HidDescriptor_Single_Mode);
pHidCfg->HidReportDescriptor = G_Ds3HidReportDescriptor_Single_Mode;
pHidCfg->HidReportDescriptorLength = G_Ds3HidDescriptor_Single_Mode.DescriptorList[0].wReportLength;
break;
case DsHidMiniDeviceModeMulti:
pHidCfg->HidDescriptor = &G_Ds3HidDescriptor_Split_Mode;
pHidCfg->HidDescriptorLength = sizeof(G_Ds3HidDescriptor_Split_Mode);
pHidCfg->HidReportDescriptor = G_Ds3HidReportDescriptor_Split_Mode;
pHidCfg->HidReportDescriptorLength = G_Ds3HidDescriptor_Split_Mode.DescriptorList[0].wReportLength;
break;
case DsHidMiniDeviceModeSixaxisCompatible:
pHidCfg->HidDescriptor = &G_SixaxisHidDescriptor;
pHidCfg->HidDescriptorLength = sizeof(G_SixaxisHidDescriptor);
pHidCfg->HidReportDescriptor = G_SixaxisHidReportDescriptor;
pHidCfg->HidReportDescriptorLength = G_SixaxisHidDescriptor.DescriptorList[0].wReportLength;
break;
case DsHidMiniDeviceModeDualShock4Rev1Compatible:
pHidCfg->HidDescriptor = &G_DualShock4Rev1HidDescriptor;
pHidCfg->HidDescriptorLength = sizeof(G_DualShock4Rev1HidDescriptor);
pHidCfg->HidReportDescriptor = G_DualShock4Rev1HidReportDescriptor;
pHidCfg->HidReportDescriptorLength = G_DualShock4Rev1HidDescriptor.DescriptorList[0].wReportLength;
pHidCfg->VendorId = pDevCtx->VendorId = DS3_DS4REV1_HID_VID;
pHidCfg->ProductId = pDevCtx->ProductId = DS3_DS4REV1_HID_PID;
pHidCfg->VersionNumber = pDevCtx->VersionNumber;
pHidCfg->HidDeviceAttributes.VendorID = pDevCtx->VendorId;
pHidCfg->HidDeviceAttributes.ProductID = pDevCtx->ProductId;
pHidCfg->HidDeviceAttributes.VersionNumber = pDevCtx->VersionNumber;
break;
default:
TraceError(
TRACE_DSHIDMINIDRV,
"Unknown HID Device Mode: 0x%02X", pDevCtx->Configuration.HidDeviceMode);
}
//
// Increase pad instance count (TODO: unused)
//
numInstances++;
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
#pragma code_seg()
#pragma code_seg("PAGE")
_Function_class_(DMF_Close)
_IRQL_requires_max_(PASSIVE_LEVEL)
static
VOID
DMF_DsHidMini_Close(
_In_ DMFMODULE DmfModule
)
{
PDEVICE_CONTEXT pDevCtx;
PAGED_CODE();
FuncEntry(TRACE_DSHIDMINIDRV);
pDevCtx = DeviceGetContext(DMF_ParentDeviceGet(DmfModule));
//
// TODO: free resources if necessary, save configuration
//
UNREFERENCED_PARAMETER(pDevCtx);
//
// Decrease pad instance count
//
numInstances--;
FuncExitNoReturn(TRACE_DSHIDMINIDRV);
}
#pragma code_seg()
#pragma endregion
#pragma region DMF Virtual HID Mini-specific
NTSTATUS
DsHidMini_GetInputReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_In_ HID_XFER_PACKET* Packet,
_Out_ ULONG* ReportSize
)
{
NTSTATUS status = STATUS_NOT_IMPLEMENTED;
UNREFERENCED_PARAMETER(DmfModule);
UNREFERENCED_PARAMETER(Request);
UNREFERENCED_PARAMETER(Packet);
UNREFERENCED_PARAMETER(ReportSize);
FuncEntry(TRACE_DSHIDMINIDRV);
//
// NOTE: not really used by any modern game
//
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
//
// Submit new HID Input Report.
//
NTSTATUS
DsHidMini_RetrieveNextInputReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_Out_ UCHAR** Buffer,
_Out_ ULONG* BufferSize
)
{
NTSTATUS status = STATUS_SUCCESS;
DMFMODULE dmfModuleParent;
DMF_CONTEXT_DsHidMini* moduleContext;
PDEVICE_CONTEXT pDevCtx;
UNREFERENCED_PARAMETER(Request);
FuncEntry(TRACE_DSHIDMINIDRV);
dmfModuleParent = DMF_ParentModuleGet(DmfModule);
moduleContext = DMF_CONTEXT_GET(dmfModuleParent);
pDevCtx = DeviceGetContext(DMF_ParentDeviceGet(DmfModule));
*Buffer = moduleContext->InputReport;
switch (pDevCtx->Configuration.HidDeviceMode)
{
case DsHidMiniDeviceModeSingle:
case DsHidMiniDeviceModeMulti:
*BufferSize = DS3_SPLIT_SINGLE_HID_INPUT_REPORT_SIZE;
break;
case DsHidMiniDeviceModeSixaxisCompatible:
*BufferSize = SIXAXIS_HID_INPUT_REPORT_SIZE;
break;
case DsHidMiniDeviceModeDualShock4Rev1Compatible:
*BufferSize = DS3_DS4REV1_HID_INPUT_REPORT_SIZE;
break;
default:
TraceError(
TRACE_DSHIDMINIDRV,
"Unsupported HID device mode: 0x%04X",
pDevCtx->Configuration.HidDeviceMode
);
status = STATUS_INVALID_PARAMETER;
break;
}
/*
#ifdef DBG
DumpAsHex(">> Report", *Buffer, (ULONG)*BufferSize);
#endif
*/
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
//
// Handles GET_FEATURE requests
//
NTSTATUS
DsHidMini_GetFeature(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_In_ HID_XFER_PACKET* Packet,
_Out_ ULONG* ReportSize
)
{
NTSTATUS status = STATUS_NOT_IMPLEMENTED;
UNREFERENCED_PARAMETER(Request);
PDEVICE_CONTEXT pDevCtx = DeviceGetContext(DMF_ParentDeviceGet(DmfModule));
DMF_CONTEXT_DsHidMini* pModCtx = DMF_CONTEXT_GET(DMF_ParentModuleGet(DmfModule));
FuncEntry(TRACE_DSHIDMINIDRV);
#ifdef DSHM_FEATURE_FFB
PFFB_ATTRIBUTES pEntry = NULL;
PPID_POOL_REPORT pPool;
PPID_BLOCK_LOAD_REPORT pBlockLoad;
switch (Packet->reportId)
{
case PID_POOL_REPORT_ID:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_POOL_REPORT_ID");
pPool = (PPID_POOL_REPORT)Packet->reportBuffer;
/*
* Static information about the fictitious device memory pool.
* Since we manage everything in software, size constraints
* are not an issue and we can report the maximum values.
*/
pPool->ReportId = PID_POOL_REPORT_ID;
pPool->RamPoolSize = 65535;
pPool->SimultaneousEffectsMax = MAX_EFFECT_BLOCKS;
pPool->DeviceManagedPool = 1;
pPool->SharedParameterBlocks = 0;
*ReportSize = sizeof(PID_POOL_REPORT) - 1;
status = STATUS_SUCCESS;
break;
case PID_BLOCK_LOAD_REPORT_ID:
pBlockLoad = (PPID_BLOCK_LOAD_REPORT)Packet->reportBuffer;
pBlockLoad->ReportId = PID_BLOCK_LOAD_REPORT_ID;
pBlockLoad->RamPoolAvailable = 65535;
pBlockLoad->BlockLoadStatus = PidBlsFull;
//
// Here we should have at least one new effect block index ready
//
for (pEntry = pModCtx->FfbAttributes; pEntry != NULL; pEntry = pEntry->hh.next)
{
/** Skip if already in use. */
if (pEntry->Reported)
continue;
pEntry->Reported = TRUE; // mark as claimed/in use
pBlockLoad->EffectBlockIndex = pEntry->EffectBlockIndex;
pBlockLoad->BlockLoadStatus = PidBlsSuccess;
break;
}
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_BLOCK_LOAD_REPORT_ID (EffectBlockIndex: %d)",
pBlockLoad->EffectBlockIndex);
*ReportSize = sizeof(PID_BLOCK_LOAD_REPORT) - 1;
status = STATUS_SUCCESS;
break;
}
#endif
if (Packet->reportId == 0x00 && pDevCtx->Configuration.HidDeviceMode == DsHidMiniDeviceModeSixaxisCompatible)
{
//
// Copy last received raw report to buffer
//
RtlCopyMemory(
Packet->reportBuffer,
pModCtx->GetFeatureReport,
Packet->reportBufferLen < SIXAXIS_HID_GET_FEATURE_REPORT_SIZE ? Packet->reportBufferLen :
SIXAXIS_HID_GET_FEATURE_REPORT_SIZE
);
//
// Alter report ID header to expected values
//
Packet->reportBuffer[0] = 0x00;
Packet->reportBuffer[1] = 0x3F;
*ReportSize = Packet->reportBufferLen - 1;
status = STATUS_SUCCESS;
}
else if (
Packet->reportId == 0x12
&& Packet->reportBufferLen == 64
&& pDevCtx->Configuration.HidDeviceMode == DsHidMiniDeviceModeDualShock4Rev1Compatible
)
{
RtlCopyMemory(
&Packet->reportBuffer[1],
&pDevCtx->DeviceAddress,
sizeof(BD_ADDR)
);
*ReportSize = Packet->reportBufferLen - 1;
status = STATUS_SUCCESS;
}
if (!NT_SUCCESS(status))
{
TraceEvents(
TRACE_LEVEL_WARNING,
TRACE_DSHIDMINIDRV,
"%!FUNC! Not implemented"
);
TraceVerbose(
TRACE_DSHIDMINIDRV,
"-- Packet->reportId: %d, Packet->reportBufferLen: %d",
Packet->reportId,
Packet->reportBufferLen
);
}
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
//
// Handles SET_FEATURE requests
//
NTSTATUS
DsHidMini_SetFeature(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_In_ HID_XFER_PACKET* Packet,
_Out_ ULONG* ReportSize
)
{
NTSTATUS status = STATUS_SUCCESS;
UNREFERENCED_PARAMETER(Request);
FuncEntry(TRACE_DSHIDMINIDRV);
DMF_CONTEXT_DsHidMini* pModCtx = DMF_CONTEXT_GET(DMF_ParentModuleGet(DmfModule));
UNREFERENCED_PARAMETER(pModCtx);
#ifdef DSHM_FEATURE_FFB
PFFB_ATTRIBUTES pEntry = NULL;
PPID_NEW_EFFECT_REPORT pNewEffect;
switch (Packet->reportId)
{
case PID_NEW_EFFECT_REPORT_ID:
pNewEffect = (PPID_NEW_EFFECT_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_CREATE_NEW_EFFECT_REPORT");
//
// Look for free effect block index and allocate new entry
//
for (UCHAR index = 1; index < MAX_EFFECT_BLOCKS; index++)
{
HASH_FIND(hh, pModCtx->FfbAttributes, &index, sizeof(UCHAR), pEntry);
if (pEntry == NULL)
{
pEntry = (PFFB_ATTRIBUTES)malloc(sizeof(FFB_ATTRIBUTES));
pEntry->EffectBlockIndex = index; // next free index
pEntry->EffectType = pNewEffect->EffectType; // effect type requested
pEntry->Reported = FALSE; // index allocated but not claimed yet
HASH_ADD(hh, pModCtx->FfbAttributes, EffectBlockIndex, sizeof(UCHAR), pEntry);
break;
}
}
//
// Whoops, guess we're full!
//
if (pEntry == NULL)
{
status = STATUS_INVALID_PARAMETER;
break;
}
switch (pNewEffect->EffectType)
{
case PidEtConstantForce:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Constant Force");
break;
case PidEtRamp:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Ramp");
break;
case PidEtSquare:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Square");
break;
case PidEtSine:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Sine");
break;
case PidEtTriangle:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Triangle");
break;
case PidEtSawtoothUp:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Sawtooth Up");
break;
case PidEtSawtoothDown:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Sawtooth Down");
break;
case PidEtSpring:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Spring");
break;
case PidEtDamper:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Damper");
break;
case PidEtInertia:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Inertia");
break;
case PidEtFriction:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! ET Friction");
break;
}
*ReportSize = Packet->reportBufferLen;
break;
default:
TraceEvents(TRACE_LEVEL_WARNING,
TRACE_DSHIDMINIDRV, "%!FUNC! Not implemented");
TraceVerbose(TRACE_DSHIDMINIDRV, "-- Packet->reportId: %d, Packet->reportBufferLen: %d",
Packet->reportId,
Packet->reportBufferLen);
DumpAsHex("-- SET_FEATURE.reportBuffer", Packet->reportBuffer, Packet->reportBufferLen);
break;
}
#endif
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
NTSTATUS
DsHidMini_SetOutputReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_In_ HID_XFER_PACKET* Packet,
_Out_ ULONG* ReportSize
)
{
NTSTATUS status = STATUS_NOT_IMPLEMENTED;
UNREFERENCED_PARAMETER(DmfModule);
UNREFERENCED_PARAMETER(Request);
UNREFERENCED_PARAMETER(Packet);
UNREFERENCED_PARAMETER(ReportSize);
FuncEntry(TRACE_DSHIDMINIDRV);
//
// NOTE: not really used by any modern game
//
FuncExit(TRACE_DSHIDMINIDRV, "status=%!STATUS!", status);
return status;
}
NTSTATUS
DsHidMini_WriteReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_In_ HID_XFER_PACKET* Packet,
_Out_ ULONG* ReportSize
)
{
NTSTATUS status = STATUS_NOT_IMPLEMENTED;
UNREFERENCED_PARAMETER(Request);
UNREFERENCED_PARAMETER(ReportSize);
FuncEntry(TRACE_DSHIDMINIDRV);
PDEVICE_CONTEXT pDevCtx = DeviceGetContext(DMF_ParentDeviceGet(DmfModule));
DMF_CONTEXT_DsHidMini* pModCtx = DMF_CONTEXT_GET(DMF_ParentModuleGet(DmfModule));
PUCHAR buffer = NULL;
size_t bufferSize = 0;
#ifdef DSHM_FEATURE_FFB
PFFB_ATTRIBUTES pEntry = NULL;
PPID_DEVICE_CONTROL_REPORT pDeviceControl;
PPID_DEVICE_GAIN_REPORT pGain;
PPID_SET_CONDITION_REPORT pSetCondition;
PPID_SET_EFFECT_REPORT pSetEffect;
PPID_SET_PERIODIC_REPORT pSetPeriodic;
PPID_SET_CONSTANT_FORCE_REPORT pSetConstant;
PPID_EFFECT_OPERATION_REPORT pEffectOperation;
PPID_BLOCK_FREE_REPORT pBlockFree;
UCHAR rumbleValue = 0;
switch (Packet->reportId)
{
case PID_DEVICE_CONTROL_REPORT_ID:
pDeviceControl = (PPID_DEVICE_CONTROL_REPORT)Packet->reportBuffer;
switch (pDeviceControl->DeviceControlCommand)
{
case PidDcEnableActuators:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DC Enable Actuators");
break;
case PidDcDisableActuators:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DC Disable Actuators");
break;
case PidDcReset:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DC Reset");
HASH_CLEAR(hh, pModCtx->FfbAttributes);
// Fall through
case PidDcStopAllEffects:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DC Stop All Effects");
DS3_SET_SMALL_RUMBLE_STRENGTH(pDevCtx, 0);
DS3_SET_LARGE_RUMBLE_STRENGTH(pDevCtx, 0);
(void)Ds_SendOutputReport(pDevCtx);
break;
case PidDcPause:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DC Pause");
break;
case PidDcContinue:
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DC Continue");
break;
default:
break;
}
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_DEVICE_GAIN_REPORT_ID:
pGain = (PPID_DEVICE_GAIN_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_DEVICE_GAIN_REPORT, DeviceGain: %d",
pGain->DeviceGain);
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_SET_CONDITION_REPORT_ID:
pSetCondition = (PPID_SET_CONDITION_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_SET_CONDITION_REPORT, EffectBlockIndex: %d",
pSetCondition->EffectBlockIndex);
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_SET_EFFECT_REPORT_ID:
pSetEffect = (PPID_SET_EFFECT_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! SET_EFFECT_REPORT, EffectBlockIndex: %d, "
"EffectType: %d, Duration: %d, TriggerRepeatInterval: %d, "
"SamplePeriod: %d, Gain: %d, TriggerButton: %d, AxesEnableX: %d, AxesEnableY: %d, "
"DirectionEnable: %d, DirectionInstance1: %d, DirectionInstance2: %d, StartDelay: %d",
pSetEffect->EffectBlockIndex,
pSetEffect->EffectType,
pSetEffect->Duration,
pSetEffect->TriggerRepeatInterval,
pSetEffect->SamplePeriod,
pSetEffect->Gain,
pSetEffect->TriggerButton,
pSetEffect->AxesEnableX,
pSetEffect->AxesEnableY,
pSetEffect->DirectionEnable,
pSetEffect->DirectionInstance1,
pSetEffect->DirectionInstance2,
pSetEffect->StartDelay);
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_SET_PERIODIC_REPORT_ID:
pSetPeriodic = (PPID_SET_PERIODIC_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_SET_PERIODIC_REPORT, "
"EffectBlockIndex: %d, Magnitude: %d, Offset: %d, Phase: %d, Period: %d",
pSetPeriodic->EffectBlockIndex,
pSetPeriodic->Magnitude,
pSetPeriodic->Offset,
pSetPeriodic->Phase,
pSetPeriodic->Period
);
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_SET_CONSTANT_FORCE_REPORT_ID:
pSetConstant = (PPID_SET_CONSTANT_FORCE_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_SET_CONSTANT_FORCE_REPORT, EffectBlockIndex: %d, Magnitude: %d",
pSetConstant->EffectBlockIndex,
pSetConstant->Magnitude);
rumbleValue = (UCHAR)(pSetConstant->Magnitude / 10000.0f * 255.0f);
TraceVerbose(TRACE_DSHIDMINIDRV, "!! DS3 Rumble Value: %d",
rumbleValue);
if (pSetConstant->EffectBlockIndex % 2 == 0)
{
DS3_SET_SMALL_RUMBLE_STRENGTH(pDevCtx, rumbleValue);
}
else
{
DS3_SET_LARGE_RUMBLE_STRENGTH(pDevCtx, rumbleValue);
}
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_EFFECT_OPERATION_REPORT_ID:
pEffectOperation = (PPID_EFFECT_OPERATION_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_EFFECT_OPERATION_REPORT, EffectBlockIndex: %d, "
"EffectOperation: %d, LoopCount: %d",
pEffectOperation->EffectBlockIndex,
pEffectOperation->EffectOperation,
pEffectOperation->LoopCount);
switch (pEffectOperation->EffectOperation)
{
case PidEoStart:
(void)Ds_SendOutputReport(pDevCtx);
break;
case PidEoStop:
DS3_SET_SMALL_RUMBLE_STRENGTH(pDevCtx, 0);
DS3_SET_LARGE_RUMBLE_STRENGTH(pDevCtx, 0);
(void)Ds_SendOutputReport(pDevCtx);
break;
default:
break;
}
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
case PID_BLOCK_FREE_REPORT_ID:
pBlockFree = (PPID_BLOCK_FREE_REPORT)Packet->reportBuffer;
TraceVerbose(TRACE_DSHIDMINIDRV, "!! PID_BLOCK_FREE_REPORT, EffectBlockIndex: %d",
pBlockFree->EffectBlockIndex);
//
// Lookup our entry in the list
//
HASH_FIND(hh, pModCtx->FfbAttributes, &pBlockFree->EffectBlockIndex, sizeof(UCHAR), pEntry);
//
// Remove from list
//
HASH_DEL(pModCtx->FfbAttributes, pEntry);
//
// Free memory
//
free(pEntry);
*ReportSize = Packet->reportBufferLen;
status = STATUS_SUCCESS;
break;
}
#endif
//
// Handle other non-FFB cases
//
//
// SIXAXIS.SYS emulation
//
if (Packet->reportId == 0x00 && pDevCtx->Configuration.HidDeviceMode == DsHidMiniDeviceModeSixaxisCompatible)
{
//
// External output report overrides internal behaviour, keep note
//
pDevCtx->OutputReport.Mode = Ds3OutputReportModeWriteReportPassThrough;
//
// Get raw buffer pointer (connection agnostic)
//
DS3_GET_UNIFIED_OUTPUT_REPORT_BUFFER(
pDevCtx,
&buffer,
&bufferSize
);
//
// Overwrite with what we received
//
RtlCopyMemory(
buffer,
&Packet->reportBuffer[3],
bufferSize
);
(void)Ds_SendOutputReport(pDevCtx);
status = STATUS_SUCCESS;
}
//
// DS4 Rev1 emulation
//
if (Packet->reportId == 0x05 && pDevCtx->Configuration.HidDeviceMode == DsHidMiniDeviceModeDualShock4Rev1Compatible)
{
//
// External output report overrides internal behaviour, keep note
//
pDevCtx->OutputReport.Mode = Ds3OutputReportModeWriteReportPassThrough;
DS3_SET_SMALL_RUMBLE_STRENGTH(pDevCtx, Packet->reportBuffer[4]);
DS3_SET_LARGE_RUMBLE_STRENGTH(pDevCtx, Packet->reportBuffer[5]);
// Color values (RGB)
//
UCHAR r = Packet->reportBuffer[6];
UCHAR g = Packet->reportBuffer[7];
UCHAR b = Packet->reportBuffer[8];
//
// Single color RED intensity indicates battery level
//
if (g == 0x00 && b == 0x00)
{
if (r >= 192)
DS3_SET_LED(pDevCtx, DS3_LED_4);
else if (r >= 128)
DS3_SET_LED(pDevCtx, DS3_LED_3);
else if (r >= 64)
DS3_SET_LED(pDevCtx, DS3_LED_2);
else
DS3_SET_LED(pDevCtx, DS3_LED_1);
}
//
// Decode custom LED status from color RED intensity
//
else if (g == 0xFF && b == 0xFF)
{
if (r == 0x00)
DS3_SET_LED(pDevCtx, DS3_LED_OFF);