-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathIOCTL.c
1527 lines (1226 loc) · 53.1 KB
/
IOCTL.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
/***************************************************************************
* PPJoy Virtual Joystick for Microsoft Windows *
* Copyright (C) 2011 Deon van der Westhuysen *
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/***************************************************************************/
/** **/
/** Parallel port joystick driver, (C) Deon van der Westhuysen 2002 **/
/** **/
/** IOCTL.c Routines to handle Internal IOCTL request from the **/
/** class driver **/
/** **/
/** **/
/** **/
/** Portions of this file derived from the Windows 2000 DDK **/
/** **/
/***************************************************************************/
#include "PPortJoy.h"
#include "stdio.h"
/* These two include files ship with the HIDGAME Win2K DDK samples */
#include <hidtoken.h>
#include <hidusage.h>
/**************************************************************************/
/* Declare the code segments into which each routine will be placed */
/* Used to declare pagable and initialisation code. */
/**************************************************************************/
#ifdef ALLOC_PRAGMA
#pragma alloc_text (PAGE, PPJoy_Ctl_Ioctl)
#pragma alloc_text (PAGE, PPJoy_GetDeviceDescriptor)
#pragma alloc_text (PAGE, PPJoy_GetReportDescriptor)
#pragma alloc_text (PAGE, PPJoy_GetAttributes)
#pragma alloc_text (PAGE, PPJoy_GenerateReport)
#pragma alloc_text (PAGE, PPJoy_SetJoystickState)
#pragma alloc_text (PAGE, PPJoy_GetJoystickState)
#pragma alloc_text (PAGE, PPJoy_SetJoystickMap)
#pragma alloc_text (PAGE, PPJoy_GetJoystickMap)
#pragma alloc_text (PAGE, PPJoy_DelJoystickMap)
#pragma alloc_text (PAGE, PPJoy_SetJoyTiming)
#pragma alloc_text (PAGE, PPJoy_GetJoyTiming)
#pragma alloc_text (PAGE, PPJoy_DelJoyTiming)
#pragma alloc_text (PAGE, PPJoy_ActJoyTiming)
#pragma alloc_text (PAGE, PPJoy_GetPPJoyOptions)
#pragma alloc_text (PAGE, PPJoy_SetPPJoyOptions)
#endif
/**************************************************************************/
/* Routine to handle Internal IOCTL IRPs. Most of the work is delegated */
/* helper functions. */
/**************************************************************************/
NTSTATUS PPJoy_InternalIoctl (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
PIO_STACK_LOCATION IrpStack;
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_InternalIoctl(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
/* InternalIoctl is not pagable. This is because IOCTL_HID_READ_REPORT */
/* can be called at dispatch-level. */
IrpStack= IoGetCurrentIrpStackLocation(Irp);
DeviceExtension= GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
ntStatus= PPJoy_EnterRequest (DeviceExtension);
if (!NT_SUCCESS(ntStatus))
goto Exit;
switch(IrpStack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_HID_GET_DEVICE_DESCRIPTOR:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_HID_GET_DEVICE_DESCRIPTOR") );
ntStatus= PPJoy_GetDeviceDescriptor (DeviceObject,Irp);
break;
case IOCTL_HID_GET_REPORT_DESCRIPTOR:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_HID_GET_REPORT_DESCRIPTOR") );
ntStatus= PPJoy_GetReportDescriptor (DeviceObject,Irp);
break;
case IOCTL_HID_READ_REPORT:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_HID_READ_REPORT") );
ntStatus= PPJoy_ReadReport (DeviceObject,Irp);
break;
case IOCTL_HID_GET_DEVICE_ATTRIBUTES:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_HID_GET_DEVICE_ATTRIBUTES") );
ntStatus= PPJoy_GetAttributes (DeviceObject,Irp);
break;
default:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_WARN,("Unknown or unsupported IOCTL (%x)",
IrpStack->Parameters.DeviceIoControl.IoControlCode) );
ntStatus= STATUS_NOT_SUPPORTED;
break;
}
Exit:
/* Set the return status for operation in the IRP */
Irp->IoStatus.Status= ntStatus;
PPJoy_LeaveRequest (DeviceExtension);
/* Complete the IRP */
IoCompleteRequest (Irp,IO_NO_INCREMENT);
ntStatus= STATUS_SUCCESS;
PPJOY_EXITPROC(FILE_IOCTL | PPJOY_FEXIT_STATUSOK, "PPJoy_InternalIoctl", ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine to handle Internal IOCTL IRPs. If not control device then we */
/* pass the IRP on to HID.sys. */
/**************************************************************************/
NTSTATUS PPJoy_Ctl_InternalIoctl (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
PIO_STACK_LOCATION IrpStack;
/* InternalIoctl is not pagable. This is because IOCTL_HID_READ_REPORT */
/* can be called at dispatch-level. */
/* If it is not for one of our control devices - pass on to HID.sys */
if (GET_NEXT_DEVICE_OBJECT(DeviceObject))
{
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_HIDHACK, ("Not a control device - passing request on to HID.sys (PPJoy_Ctl_InternalIoctl)") );
return HIDMajorFunctions[IoGetCurrentIrpStackLocation(Irp)->MajorFunction](DeviceObject,Irp);
}
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_Ctl_InternalIoctl(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
IrpStack= IoGetCurrentIrpStackLocation(Irp);
DeviceExtension= GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
ntStatus= PPJoy_EnterRequest (DeviceExtension);
if (!NT_SUCCESS(ntStatus))
goto Exit;
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_WARN,("Unknown or unsupported IOCTL (%x)",IrpStack->Parameters.DeviceIoControl.IoControlCode) );
ntStatus= STATUS_NOT_SUPPORTED;
Exit:
/* Set the return status for operation in the IRP */
Irp->IoStatus.Status= ntStatus;
PPJoy_LeaveRequest (DeviceExtension);
/* Complete the IRP */
IoCompleteRequest (Irp,IO_NO_INCREMENT);
/* According to DriverVerifier return code should be same as IRP status. */
/* ntStatus= STATUS_SUCCESS; */
PPJOY_EXITPROC(FILE_IOCTL | PPJOY_FEXIT_STATUSOK, "PPJoy_Ctl_InternalIoctl", ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine to handle IOCTL IRPs from the outside world. */
/**************************************************************************/
NTSTATUS PPJoy_Ctl_Ioctl (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
PIO_STACK_LOCATION IrpStack;
PAGED_CODE ();
/* If it is not for one of our control devices - pass on to HID.sys */
if (GET_NEXT_DEVICE_OBJECT(DeviceObject))
{
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_HIDHACK, ("Not a control device - passing request on to HID.sys (PPJoy_Ctl_Ioctl)") );
return HIDMajorFunctions[IoGetCurrentIrpStackLocation(Irp)->MajorFunction](DeviceObject,Irp);
}
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_Ctl_Ioctl(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
IrpStack= IoGetCurrentIrpStackLocation(Irp);
DeviceExtension= GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
ntStatus= PPJoy_EnterRequest (DeviceExtension);
if (!NT_SUCCESS(ntStatus))
goto Exit;
switch(IrpStack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_PPORTJOY_SET_STATE:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_SET_STATE") );
ntStatus= PPJoy_SetJoystickState (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_GET_STATE:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_GET_STATE") );
ntStatus= PPJoy_GetJoystickState (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_SET_MAPPING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_SET_MAPPING") );
ntStatus= PPJoy_SetJoystickMap (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_GET_MAPPING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_GET_MAPPING") );
ntStatus= PPJoy_GetJoystickMap (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_DEL_MAPPING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_DEL_MAPPING") );
ntStatus= PPJoy_DelJoystickMap (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_SET_FORCE:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_SET_FORCE") );
ntStatus= STATUS_NOT_SUPPORTED;
break;
case IOCTL_PPORTJOY_SET_TIMING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_SET_TIMING") );
ntStatus= PPJoy_SetJoyTiming (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_GET_TIMING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_GET_TIMING") );
ntStatus= PPJoy_GetJoyTiming (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_DEL_TIMING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_DEL_TIMING") );
ntStatus= PPJoy_DelJoyTiming (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_ACT_TIMING:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_ACT_TIMING") );
ntStatus= PPJoy_ActJoyTiming (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_GET_OPTIONS:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_GET_OPTIONS") );
ntStatus= PPJoy_GetPPJoyOptions (DeviceObject,Irp);
break;
case IOCTL_PPORTJOY_SET_OPTIONS:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("IOCTL_PPORTJOY_SET_OPTIONS") );
ntStatus= PPJoy_SetPPJoyOptions (DeviceObject,Irp);
break;
default:
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_WARN,("Unknown or unsupported IOCTL (%x)",
IrpStack->Parameters.DeviceIoControl.IoControlCode) );
ntStatus= STATUS_NOT_SUPPORTED;
break;
}
Exit:
/* Set the return status for operation in the IRP */
Irp->IoStatus.Status= ntStatus;
PPJoy_LeaveRequest (DeviceExtension);
/* Complete the IRP */
IoCompleteRequest (Irp,IO_NO_INCREMENT);
/* According to DriverVerifier return code should be same as IRP status. */
/* ntStatus= STATUS_SUCCESS; */
PPJOY_EXITPROC(FILE_IOCTL | PPJOY_FEXIT_STATUSOK, "PPJoy_Ctl_Ioctl", ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine to construct a USB HID Device Descriptor for the joystick. */
/**************************************************************************/
NTSTATUS PPJoy_GetDeviceDescriptor (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
PHID_DESCRIPTOR pHidDescriptor;
USHORT cbReport;
UCHAR rgGameReport[PPJOY_REPORT_MAXSIZE];
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
PIO_STACK_LOCATION IrpStack;
PAGED_CODE ();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_GetDeviceDescriptor(DeviceObject=0x%p,Irp=0x%p)",
DeviceObject,Irp) );
ntStatus= STATUS_SUCCESS;
IrpStack = IoGetCurrentIrpStackLocation(Irp);
DeviceExtension = GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
/* Return HID descriptor in the buffer pointed to by Irp->UserBuffer */
pHidDescriptor = (PHID_DESCRIPTOR) Irp->UserBuffer;
/* Check that we will not overflow the supplied buffer. Bad karma */
if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(*pHidDescriptor))
{
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR,("PPJoy_GetDeviceDescriptor: OutBufferLength(0x%x) < sizeof(HID_DESCRIPTOR)(0x%x)", \
IrpStack->Parameters.DeviceIoControl.OutputBufferLength, sizeof(*pHidDescriptor)) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
}
else
{
/* OK we know the buffer is large enough. Good */
/* Generate a report (report description really) so we know its size. */
/* We use a locally allocated buffer to store it in; and the Generate */
/* Report routine will not return more than PPJOY_REPORT_MAXSIZE bytes */
ntStatus= PPJoy_GenerateReport (DeviceObject,rgGameReport,&cbReport);
/* If we successfully generated the report, construct the descriptor */
if (NT_SUCCESS (ntStatus))
{
RtlZeroMemory( pHidDescriptor, sizeof(*pHidDescriptor) );
pHidDescriptor->bLength= sizeof(*pHidDescriptor);
pHidDescriptor->bDescriptorType= HID_HID_DESCRIPTOR_TYPE;
pHidDescriptor->bcdHID= HID_REVISION;
pHidDescriptor->bCountry= 0; /*not localized*/
pHidDescriptor->bNumDescriptors= 1;
pHidDescriptor->DescriptorList[0].bReportType= HID_REPORT_DESCRIPTOR_TYPE;
pHidDescriptor->DescriptorList[0].wReportLength= cbReport;
/* Return the size of the HID report descriptor */
Irp->IoStatus.Information = sizeof(*pHidDescriptor);
}
else
{
/* Report size overflow, don't return a descriptor */
Irp->IoStatus.Information = 0x0;
}
}
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT_STATUSOK, "PPJoy_GetDeviceDescriptor",ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine that returns the USB HID Report Descriptor (describing the */
/* the joystick input) created by PPJoy_GenerateReport(). */
/**************************************************************************/
NTSTATUS PPJoy_GetReportDescriptor (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
PDEVICE_EXTENSION DeviceExtension;
PIO_STACK_LOCATION IrpStack;
NTSTATUS ntStatus;
UCHAR rgGameReport[PPJOY_REPORT_MAXSIZE] ;
USHORT cbReport;
PAGED_CODE ();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_GetReportDescriptor(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
IrpStack = IoGetCurrentIrpStackLocation(Irp);
DeviceExtension = GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
/* Generate our report, based on the current number of axes and buttons */
ntStatus = PPJoy_GenerateReport(DeviceObject, rgGameReport, &cbReport);
/* If we successfully generated the report... */
if (NT_SUCCESS (ntStatus))
{
/* Check to see if supplied buffer will hold our descriptor */
if (cbReport > (USHORT)IrpStack->Parameters.DeviceIoControl.OutputBufferLength)
{
/* Surprise, buffer too small. */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("HGM_GetReportDescriptor: cbReport(0x%x) OutputBufferLength(0x%x)",\
cbReport, IrpStack->Parameters.DeviceIoControl.OutputBufferLength));
ntStatus = STATUS_BUFFER_TOO_SMALL;
}
else
{
/* Copy report in supplied buffer */
RtlCopyMemory (Irp->UserBuffer,rgGameReport,cbReport);
/* Report how many bytes were copied */
Irp->IoStatus.Information = cbReport;
ntStatus = STATUS_SUCCESS;
}
}
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT_STATUSOK, "PPJOY_GetReportDescriptor",ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine that returns the Vendor and Product IDs for the joystick. */
/* We will return the same IDs for all the joysticks */
/**************************************************************************/
NTSTATUS PPJoy_GetAttributes (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PIO_STACK_LOCATION IrpStack;
PDEVICE_EXTENSION DeviceExtension;
PHID_DEVICE_ATTRIBUTES DeviceAttributes;
PAGED_CODE();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_GetAttributes(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
ntStatus= STATUS_SUCCESS;
DeviceExtension= GET_MINIDRIVER_DEVICE_EXTENSION(DeviceObject);
IrpStack= IoGetCurrentIrpStackLocation(Irp);
/* Check to see if the supplied buffer is large enough */
if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(HID_DEVICE_ATTRIBUTES))
{
/* No it is not */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_GetAttributes: cbReport(0x%x) OutputBufferLength(0x%x)",\
sizeof (HID_DEVICE_ATTRIBUTES), IrpStack->Parameters.DeviceIoControl.OutputBufferLength) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
}
else
{
/* Get pointer to result buffer and initialise it with zeros */
DeviceAttributes= (PHID_DEVICE_ATTRIBUTES) Irp->UserBuffer;
RtlZeroMemory (DeviceAttributes,sizeof(*DeviceAttributes));
/* Set the size of the returned result */
Irp->IoStatus.Information= sizeof(*DeviceAttributes);
DeviceAttributes->Size= sizeof (*DeviceAttributes);
/* And return the Attributes as requested */
DeviceAttributes->VendorID= DeviceExtension->Config.VendorID;
DeviceAttributes->ProductID= DeviceExtension->Config.ProductID;
DeviceAttributes->VersionNumber= PPJOY_VERSION_NUMBER;
}
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT_STATUSOK, "PPJoy_GetAttributes",ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine that returns the Report (i.e. input data) back to HID Class */
/* driver. */
/**************************************************************************/
NTSTATUS PPJoy_ReadReport (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
PIO_STACK_LOCATION IrpStack;
/* ReadReport is not pagable. This is because IOCTL_HID_READ_REPORT can */
/* be called at dispatch-level. */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_ReadReport(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
ntStatus= STATUS_SUCCESS;
IrpStack= IoGetCurrentIrpStackLocation(Irp);
/* Check that the output buffer supplied is large enough */
if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(HIDREPORT))
{
/* No it is not */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_WARN,("PPJoy_ReadReport: Buffer too small, output=0x%x need=0x%x", \
IrpStack->Parameters.DeviceIoControl.OutputBufferLength,sizeof(HIDREPORT)) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
}
DeviceExtension = GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
/* Return device busy status if our DeviceObject has not been started */
if (!(DeviceExtension->Flags&PPJOY_FLAGS_STARTED))
ntStatus= STATUS_DEVICE_NOT_READY;
/* Set zero length read - assume the worst */
Irp->IoStatus.Information= 0x0;
if(NT_SUCCESS (ntStatus))
{
ntStatus= PPJoy_UpdateJoystickData (DeviceExtension);
PPJoy_MapRawToReport ((PHIDREPORT)(Irp->UserBuffer),&(DeviceExtension->RawInput),DeviceExtension->Mapping,DeviceExtension->ActiveMap);
// ntStatus= STATUS_SUCCESS;
// RtlZeroMemory(Irp->UserBuffer,sizeof(HIDREPORT));
/* If we successfully read the report */
if (NT_SUCCESS (ntStatus))
{
#ifdef DBG
#if DBG
{
char Buffer[1024];
int Count;
for (Count=0;Count<sizeof(HIDREPORT);Count++)
sprintf (Buffer+(Count*2),"%02X",*(((UCHAR*)Irp->UserBuffer)+Count));
Buffer[Count*2]= 0;
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE2,("PPJoy_ReadReport: sizeof(HIDREPORT)=%d Report contents= %s\n",sizeof(HIDREPORT),Buffer) );
}
#endif
#endif
Irp->IoStatus.Information = sizeof(HIDREPORT);
}
}
Irp->IoStatus.Status= ntStatus;
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT, "PPJoy_ReadReport", ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine to construct a HID Report Descriptor for the joystick input. */
/* Based in part in code from the hidjoy.c Win2K DDK sample code. */
/**************************************************************************/
NTSTATUS PPJoy_GenerateReport (IN PDEVICE_OBJECT DeviceObject, OUT UCHAR rgGameReport[PPJOY_REPORT_MAXSIZE], OUT PUSHORT pCbReport)
{
/* First, some private declarations for the GenerateReport routine. */
#define ITEM_VARIABLE 0x02 /* as ITEM_DEFAULT but value is a variable, not an array */
#define ITEM_HASNULL 0x40 /* as ITEM_DEFAULT but values out of range are considered NULL */
#define ITEM_ANALOG_AXIS (ITEM_VARIABLE)
#define ITEM_POVHAT (ITEM_VARIABLE|ITEM_HASNULL)
#define ITEM_BUTTON (ITEM_VARIABLE)
#define ITEM_PADDING 0x01 /* Constant (nothing else applies) */
/* These macros are used to build the report by adding a byte/word */
/* at atime while checking for buffer overflows. */
#define NEXT_BYTE(pReport,Data) if ((ReportSize+=1)<=PPJOY_REPORT_MAXSIZE) *pReport++ = (Data);
#define NEXT_LONG(pReport,Data) if ((ReportSize+=4)<=PPJOY_REPORT_MAXSIZE) *(((LONG UNALIGNED*)(pReport))++) = (Data);
NTSTATUS ntStatus;
PDEVICE_EXTENSION DeviceExtension;
UCHAR *pucReport;
int ReportSize;
int Count;
UCHAR NumAxes;
UCHAR NumHats;
UCHAR NumButtons;
PAGED_CODE();
PPJOY_DBGPRINT (FILE_JOYSTICK|PPJOY_FENTRY, ("PPJoy_GenerateReport(ucIn=0x%p,DeviceObject=0x%p)",rgGameReport,DeviceObject) );
DeviceExtension = GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
/* Initialise variables used to build report. The NEXT_BYTE/NEXT_LONG */
/* macros modifies them. */
ReportSize= 0;
pucReport= rgGameReport;
/* USAGE_PAGE (Generic Desktop) */
NEXT_BYTE (pucReport,HIDP_GLOBAL_USAGE_PAGE_1);
NEXT_BYTE (pucReport,HID_USAGE_PAGE_GENERIC);
/* USAGE (Joystick) */
NEXT_BYTE (pucReport,HIDP_LOCAL_USAGE_1);
NEXT_BYTE (pucReport,HID_USAGE_GENERIC_JOYSTICK);
/* Logical Min is the smallest value that an axis will return */
NEXT_BYTE (pucReport,HIDP_GLOBAL_LOG_MIN_4);
NEXT_LONG (pucReport,PPJOY_AXIS_MIN);
/* Logical Max is the largest value that an axis will return */
NEXT_BYTE (pucReport,HIDP_GLOBAL_LOG_MAX_4);
NEXT_LONG (pucReport,PPJOY_AXIS_MAX);
/* Start a Linked collection */
NEXT_BYTE (pucReport,HIDP_MAIN_COLLECTION);
NEXT_BYTE (pucReport,0x0);
/* Define one axis at a time */
NEXT_BYTE (pucReport,HIDP_GLOBAL_REPORT_COUNT_1);
NEXT_BYTE (pucReport,0x1);
/* Each axis is a ULONG (times 8 to get number of bits) */
NEXT_BYTE (pucReport,HIDP_GLOBAL_REPORT_SIZE);
NEXT_BYTE (pucReport,8*sizeof(ULONG));
NumAxes= DeviceExtension->Mapping->NumAxes;
NumHats= DeviceExtension->Mapping->NumHats;
NumButtons= DeviceExtension->Mapping->NumButtons;
PPJOY_DBGPRINT (FILE_JOYSTICK|PPJOY_BABBLE2, ("Num axes: %d (JoyMapping)",NumAxes) );
/* First, report the axes configured for this joystick */
for (Count=0;Count<NumAxes;Count++)
{
NEXT_BYTE (pucReport,HIDP_LOCAL_USAGE_4);
NEXT_BYTE (pucReport,DeviceExtension->Mapping->Data[Count*2+1]);
NEXT_BYTE (pucReport,0x0);
NEXT_BYTE (pucReport,DeviceExtension->Mapping->Data[Count*2]);
NEXT_BYTE (pucReport,0x0);
/* Data Field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_ANALOG_AXIS);
}
/* Then report padding (dummy) fields up to MAX_REPORT_AXES. This is used */
/* because the HIDREPORT structure always has MAX_REPORT_AXES, whether */
/* the joystick used them or not. This report descriptor must describe */
/* exactly sizeof(HIDREPORT) bytes. This also lines up the hats fields. */
while (Count<MAX_REPORT_AXES)
{
/* Constant Field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_PADDING);
Count++;
}
PPJOY_DBGPRINT (FILE_JOYSTICK|PPJOY_BABBLE2, ("Num hats: %d (JoyMapping)",NumHats) );
/* Logical Min is the smallest value that an hat will return */
NEXT_BYTE (pucReport,HIDP_GLOBAL_LOG_MIN_4);
NEXT_LONG (pucReport,PPJOY_HAT_MIN);
/* Logical Max is the largest value that an hat will return */
NEXT_BYTE (pucReport,HIDP_GLOBAL_LOG_MAX_4);
NEXT_LONG (pucReport,PPJOY_HAT_MAX);
/* First, report the hats configured for this joystick */
for (Count=0;Count<NumHats;Count++)
{
NEXT_BYTE (pucReport,HIDP_LOCAL_USAGE_4);
NEXT_BYTE (pucReport,DeviceExtension->Mapping->Data[(NumAxes+NumButtons)*2+Count*2+1]);
NEXT_BYTE (pucReport,0x0);
NEXT_BYTE (pucReport,DeviceExtension->Mapping->Data[(NumAxes+NumButtons)*2+Count*2]);
NEXT_BYTE (pucReport,0x0);
/* Data Field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_ANALOG_AXIS);
}
/* Then report padding (dummy) fields up to MAX_REPORT_HATS. This is used */
/* because the HIDREPORT structure always has MAX_REPORT_HSTS, whether */
/* the joystick used them or not. This report descriptor must describe */
/* exactly sizeof(HIDREPORT) bytes. This also lines up the button fields. */
while (Count<MAX_REPORT_HATS)
{
/* Constant Field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_PADDING);
Count++;
}
PPJOY_DBGPRINT (FILE_JOYSTICK|PPJOY_BABBLE2, ("Num buttons: %d (JoyMapping)",NumButtons) );
/* Now the buttons */
for (Count=0;Count<NumButtons;Count++)
{
/* Report size is 1 bit for button */
NEXT_BYTE (pucReport,HIDP_GLOBAL_REPORT_SIZE);
NEXT_BYTE (pucReport,0x1);
NEXT_BYTE (pucReport,HIDP_LOCAL_USAGE_4);
NEXT_BYTE (pucReport,DeviceExtension->Mapping->Data[NumAxes*2+Count*2+1]);
NEXT_BYTE (pucReport,0x0);
NEXT_BYTE (pucReport,DeviceExtension->Mapping->Data[NumAxes*2+Count*2]);
NEXT_BYTE (pucReport,0x0);
/* Data field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_BUTTON);
/* 7 bits of constant data (padding) */
NEXT_BYTE (pucReport,HIDP_GLOBAL_REPORT_SIZE);
NEXT_BYTE (pucReport,0x7);
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_PADDING);
}
/* Now we pad up the descriptor until exactly sizeof(HIDREPORT) bytes has */
/* been described. */
if (Count<MAX_REPORT_BUTTONS)
{
while ((Count+16)<MAX_REPORT_BUTTONS)
{
/* Set 16 bytes/buttons at a time to unused */
NEXT_BYTE (pucReport,HIDP_GLOBAL_REPORT_SIZE);
NEXT_BYTE (pucReport,(UCHAR)(16*8));
/* Define the constant field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_PADDING);
Count+= 16;
}
/* Constant report for 8 * unused buttons bits */
NEXT_BYTE (pucReport,HIDP_GLOBAL_REPORT_SIZE);
NEXT_BYTE (pucReport,(UCHAR)((MAX_REPORT_BUTTONS-Count)*8));
/* Define the constant field */
NEXT_BYTE (pucReport,HIDP_MAIN_INPUT_1);
NEXT_BYTE (pucReport,ITEM_PADDING);
}
/* End of collection, We're done ! */
NEXT_BYTE (pucReport,HIDP_MAIN_ENDCOLLECTION);
/* Check too see that we did not overflow the buffer */
if (ReportSize>PPJOY_REPORT_MAXSIZE)
{
/* Sorry, we need a bigger buffer. */
PPJOY_DBGPRINT (FILE_JOYSTICK|PPJOY_ERROR, ("PPJoy_GenerateReport: Report (%d bytes) is larger than PPJOY_REPORT_MAXSIZE",ReportSize) );
ntStatus= STATUS_BUFFER_TOO_SMALL;
*pCbReport= 0x0;
RtlZeroMemory(rgGameReport, sizeof(rgGameReport));
}
else
{
*pCbReport= (USHORT) ReportSize;
ntStatus= STATUS_SUCCESS;
}
PPJOY_DBGPRINT (FILE_JOYSTICK|PPJOY_GEN_REPORT, ("PPJoy_GenerateReport: ReportSize=0x%x", *pCbReport) );
PPJOY_EXITPROC (FILE_JOYSTICK|PPJOY_FEXIT_STATUSOK, "PPJoy_GenerateReport",ntStatus);
return ntStatus;
#undef NEXT_BYTE
#undef NEXT_LONG
}
/**************************************************************************/
/* Routine that sets the button and position state for a virtual joystick */
/**************************************************************************/
NTSTATUS PPJoy_SetJoystickState (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PIO_STACK_LOCATION IrpStack;
PDEVICE_EXTENSION JoyDevExtension;
ULONG InputBufferLen;
PJOYSTICK_SET_STATE InputBuffer;
ULONG NumButtons;
ULONG NumAxes;
PAGED_CODE();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJOY_SetJoystickState(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
ntStatus= STATUS_SUCCESS;
IrpStack= IoGetCurrentIrpStackLocation(Irp);
JoyDevExtension = GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
InputBufferLen= IrpStack->Parameters.DeviceIoControl.InputBufferLength;
InputBuffer= Irp->AssociatedIrp.SystemBuffer;
if (!InputBuffer)
{
/* Hey, who passed a NULL pointer as buffer? */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_SetJoystickState: Stop passing us NULL pointers") );
ntStatus = STATUS_BUFFER_TOO_SMALL; /* Can later look for better error code */
goto Exit;
}
/* Check to see if the supplied buffer is large enough */
if (InputBufferLen<3*sizeof(ULONG)) /* 3*ULONG seems too much - why not ULONG+2*UCHAR??? */
{
/* No it is not */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_SetJoystickState: Packet too small - packet size 0x%x",InputBufferLen) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
goto Exit;
}
/* Process packet if we know how*/
if (InputBuffer->Version==JOYSTICK_STATE_V1)
{
NumAxes= InputBuffer->Data[0];
if (InputBufferLen>=(NumAxes*sizeof(ULONG)+5))
{
NumButtons= InputBuffer->Data[NumAxes*sizeof(ULONG)+1];
if (InputBufferLen>=(NumAxes*sizeof(ULONG)+6+NumButtons))
{
/* First do buttons as we need the unmodified "NumAxes" value for number of axes in the input buffer */
if (NumButtons>MAX_DIGITAL_RAW)
NumButtons= MAX_DIGITAL_RAW;
RtlCopyMemory (JoyDevExtension->RawInput.Digital,&(InputBuffer->Data[NumAxes*sizeof(ULONG)+2]),NumButtons);
if (NumAxes>MAX_ANALOG_RAW)
NumAxes= MAX_ANALOG_RAW;
RtlCopyMemory (JoyDevExtension->RawInput.Analog,&(InputBuffer->Data[1]),NumAxes*sizeof(ULONG));
ntStatus= STATUS_SUCCESS;
goto Exit;
}
}
/* Hmm, somewhere the packet got damaged */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_SetJoystickState: Packet too small - packet size 0x%x",InputBufferLen) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
goto Exit;
}
ntStatus= STATUS_NOT_SUPPORTED;
Exit:
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT_STATUSOK, "PPJoy_SetJoystickState",ntStatus);
return ntStatus;
}
/**************************************************************************/
/* Routine that return the current button and position state for joystick */
/**************************************************************************/
NTSTATUS PPJoy_GetJoystickState (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PIO_STACK_LOCATION IrpStack;
PDEVICE_EXTENSION JoyDevExtension;
ULONG BufLen;
PJOYSTICK_SET_STATE Buffer;
PAGED_CODE();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJoy_GetJoystickState(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
ntStatus= STATUS_SUCCESS;
IrpStack= IoGetCurrentIrpStackLocation(Irp);
JoyDevExtension = GET_MINIDRIVER_DEVICE_EXTENSION (DeviceObject);
ntStatus= PPJoy_UpdateJoystickData(JoyDevExtension);
if(!NT_SUCCESS(ntStatus))
{
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_GetJoystickState: Error updating current joystick state") );
goto Exit;
}
Buffer= Irp->AssociatedIrp.SystemBuffer;
BufLen= IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
if (!Buffer)
{
/* Hey, who passed a NULL pointer as buffer? */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_GetJoystickState: Stop passing us NULL pointers") );
ntStatus = STATUS_BUFFER_TOO_SMALL; /* Can later look for better error code */
goto Exit;
}
/* Check to see if the supplied buffer is large enough */
if (BufLen<6)
{
/* No it is not */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_GetJoystickState: Buffer too small - buffer size 0x%x",BufLen) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
goto Exit;
}
/* Now fill as much of the sucker as we can */
Buffer->Version= JOYSTICK_STATE_V1;
Buffer->Data[0]= MAX_ANALOG_RAW;
Buffer->Data[1+MAX_ANALOG_RAW*sizeof(ULONG)]= MAX_DIGITAL_RAW;
if (BufLen<(6+MAX_ANALOG_RAW*sizeof(ULONG)+MAX_DIGITAL_RAW*sizeof(UCHAR)))
{
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_WARN, ("PPJoy_GetJoystickState: Buffer too small - buffer size 0x%x - only returning header",BufLen) );
Irp->IoStatus.Information= 6; /* Size of header - version + num analog + num digital */
ntStatus= STATUS_SUCCESS;
goto Exit;
}
RtlCopyMemory(Buffer->Data+1,JoyDevExtension->RawInput.Analog,MAX_ANALOG_RAW*sizeof(ULONG));
RtlCopyMemory(Buffer->Data+2+MAX_ANALOG_RAW*sizeof(ULONG),JoyDevExtension->RawInput.Digital,MAX_DIGITAL_RAW*sizeof(UCHAR));
Irp->IoStatus.Information= 6+MAX_ANALOG_RAW*sizeof(ULONG)+MAX_DIGITAL_RAW*sizeof(UCHAR);
ntStatus= STATUS_SUCCESS;
Exit:
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT_STATUSOK, "PPJoy_GetJoystickState",ntStatus);
return ntStatus;
}
/**************************************************************************/
/* */
/**************************************************************************/
NTSTATUS PPJoy_SetJoystickMap (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PIO_STACK_LOCATION IrpStack;
PDEVICE_OBJECT JoyDeviceObject;
ULONG BufferLen;
PJOYSTICKMAP_HEADER Buffer;
PJOYSTICK_MAP JoyMap;
PAGED_CODE();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJOY_SetJoystickMap(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
IrpStack= IoGetCurrentIrpStackLocation(Irp);
JoyDeviceObject= ((PCTLDEV_EXTENSION)DeviceObject->DeviceExtension)->JoyDeviceObject;
BufferLen= IrpStack->Parameters.DeviceIoControl.InputBufferLength;
Buffer= Irp->AssociatedIrp.SystemBuffer;
if (!Buffer)
{
/* Hey, who passed a NULL pointer as buffer? */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_SetJoystickMap: Stop passing us NULL pointers") );
ntStatus = STATUS_BUFFER_TOO_SMALL; /* Can later look for better error code */
goto Exit;
}
/* Check to see if the supplied buffer is large enough */
if ((BufferLen<(sizeof(JOYSTICKMAP_HEADER)+4))||(BufferLen<(sizeof(JOYSTICKMAP_HEADER)+Buffer->MapSize)))
{
/* No it is not */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_BABBLE, ("PPJoy_SetJoystickMap: BufferLen= %d sizeof(JOYSTICKMAP_HEADER)= %d Buffer->MapSize= %d",BufferLen,sizeof(JOYSTICKMAP_HEADER),Buffer->MapSize) );
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_SetJoystickMap: Packet too small - packet size 0x%x",BufferLen) );
ntStatus = STATUS_BUFFER_TOO_SMALL;
goto Exit;
}
ntStatus= STATUS_UNSUCCESSFUL;
/* Process packet if we know how*/
JoyMap=(PJOYSTICK_MAP) ( ((UCHAR*)Buffer) + sizeof(JOYSTICKMAP_HEADER) );
if ((Buffer->Version==JOYSTICK_MAP_V1)&&(Buffer->MapSize==PPJOY_ComputeMappingSize(JoyMap)))
{
switch (Buffer->MapScope)
{
case MAP_SCOPE_DRIVER:
ntStatus= PPJoy_WriteDriverMapping (JoyDeviceObject,JoyMap,Buffer->JoyType);
break;
case MAP_SCOPE_DEVICE:
ntStatus= PPJoy_WriteDeviceMapping (JoyDeviceObject,JoyMap,Buffer->JoyType);
break;
case MAP_SCOPE_DEFAULT:
default:
ntStatus= STATUS_NOT_SUPPORTED;
break;
}
}
else
{
}
Exit:
PPJOY_EXITPROC (FILE_IOCTL|PPJOY_FEXIT_STATUSOK, "PPJoy_SetJoystickMap",ntStatus);
return ntStatus;
}
/**************************************************************************/
/* */
/**************************************************************************/
NTSTATUS PPJoy_GetJoystickMap (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS ntStatus;
PIO_STACK_LOCATION IrpStack;
PDEVICE_OBJECT JoyDeviceObject;
ULONG BufLen;
PJOYSTICKMAP_HEADER Buffer;
PJOYSTICK_MAP JoyMap;
int MapSize;
PAGED_CODE();
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_FENTRY, ("PPJOY_GetJoystickMap(DeviceObject=0x%p,Irp=0x%p)",DeviceObject,Irp) );
IrpStack= IoGetCurrentIrpStackLocation(Irp);
JoyDeviceObject= ((PCTLDEV_EXTENSION)DeviceObject->DeviceExtension)->JoyDeviceObject;
Buffer= Irp->AssociatedIrp.SystemBuffer;
BufLen= IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
if (!Buffer)
{
/* Hey, who passed a NULL pointer as buffer? */
PPJOY_DBGPRINT (FILE_IOCTL|PPJOY_ERROR, ("PPJoy_GetJoystickMap: Stop passing us NULL pointers") );
ntStatus = STATUS_BUFFER_TOO_SMALL; /* Can later look for better error code */
goto Exit;
}
/* Check to see if the supplied buffer is large enough - part 1 */
if (BufLen<sizeof(JOYSTICKMAP_HEADER))