summaryrefslogtreecommitdiff
path: root/Core/EM/usb/rt/usbms.c
blob: 7c24dffa6114d437b864d5662fc8901c1095447b (plain)
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
#pragma warning(disable: 4001)
//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2016, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************

//****************************************************************************
// $Header: /Alaska/SOURCE/Modules/USB/ALASKA/RT/usbms.c 50    10/16/16 10:12p Wilsonlee $
//
// $Revision: 50 $
//
// $Date: 10/16/16 10:12p $
//****************************************************************************
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/USB/ALASKA/RT/usbms.c $
// 
// 50    10/16/16 10:12p Wilsonlee
// [TAG]  		EIP288158
// [Category]  	Improvement
// [Description]  	Check if gUsbData is integrity.
// [Files]  		amiusb.cif, usbsb.c, AmiUsbLib.cif, AmiUsbLib.sdl,
// AmiUsbSmmGlobalDataValidationLib.c,
// AmiUsbSmmGlobalDataValidationLib.cif,
// AmiUsbSmmGlobalDataValidationLib.mak, Crc32.c, amiusb.c, amiusb.h,
// ehci.c, elib.c, ohci.c, syskbc.c, uhci.c, usb.c, usbCCID.c, usbdef.h,
// usbhid.c, usbhub.c, usbkbd.c, usbmass.c, usbms.c, usbrt.mak, xhci.c,
// amiusbhc.c, efiusbccid.c, efiusbmass.c, uhcd.c, usbmisc.c,
// AmiUsbController.h, AmiUsbLibInclude.cif,
// AmiUsbSmmGlobalDataValidationLib.h
// 
// 49    4/29/15 5:29a Wilsonlee
// [TAG]  		EIP215830
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	CYBORG R.A.T3 Gaming Mouse left/right button has no
// function, instead DPI UP/DOWN config button has left/right click
// function.
// [RootCause]  	We get mouuse button data from constant data offset.
// [Solution]  	Mouse Data should be defining report fields that contain
// modifiable device data.
// [Files]  		usbms.c, usbhid.c, usbpoint.c
// 
// 48    2/24/15 5:50a Wilsonlee
// [TAG]  		EIP149716
// [Category]  	Improvement
// [Description]  	Error Handling in USB mouse data.
// [Files]  		usbms.c, usbkbd.h, syskbc.c, xhci.c
// 
// 47    12/24/14 9:33p Wilsonlee
// [TAG]  		EIP194683
// [Category]  	Improvement
// [Description]  	Add the flag "USB_INCMPT_HID_BOOT_PROTOCOL_ONLY" of usb
// bad device table to keep devices use boot protocol.
// [Files]  		usbkbd.c, usbms.c, usbhid.c, usbdef.h
// 
// 46    8/12/14 3:04a Wilsonlee
// [TAG]  		EIP180970
// [Category]  	Improvement
// [Description]  	Update X and Y data to usbmousedata and install
// SimplePointerProtocol interface if the mouses are using boot protocol
// interface.
// [Files]  		efiusbhid.c, usbms.c
// 
// 45    5/06/14 5:16a Ryanchou
// [TAG]  		EIP166835
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Arrow keys cannot work with specific USB keyboard
// [RootCause]  	HID driver cannot parse a input report that includes both
// usage minimum/maximum and single usage.
// [Solution]  	Store the usage in the same array to determine the input
// data format.
// [Files]  		syskbc.c, sysnokbc.c, usbdef.h, usbhid.c, usbkbd.c,
// usbkbd.h, usbms.c, usbpoint, efiusbhid.c, efiusbpoint.c
// 
// 44    2/26/14 1:55a Wilsonlee
// [TAG]  		EIP149854
// [Category]  	Improvement
// [Description]  	Add data length parameter to polling callback function.
// [Files]  		usbkbd.c, uhci.c, usb.c, usbhub.c, usbCCID.c, usbms.c,
// usbhid.c, usbpoint.c, usbkbd.h, ehci.c, ohci.c, xhci.c, usbdef.h
// 
// 43    12/30/13 3:47a Wilsonlee
// [TAG]  		EIP148707
// [Category]  	Improvement
// [Description]  	We need to store wheel data before clearing the buffer.
// [Files]  		usbms.c, efiusbms.c
// 
// 42    8/22/13 6:33a Wilsonlee
// [TAG]  		EIP122944
// [Category]  	Improvement
// [Description]  	Remove mouse_flag3 and check the mouse interface status
// in the CCB byte before we send the data to KBC.
// [Files]  		syskbc.c, usbms.c
// 
// 41    7/04/13 5:46a Roberthsu
// [TAG]           EIP127014 
// [Category]      Bug Fix
// [Severity]      Normal
// [Symptom]       Mouse drifting not smooth
// [RootCause]     Bbecause Efi simple point protocol RelativeMovementX
// type is INT32.
// [Solution]      Transfer data type to INT32.
// [Files]         usbdef.h,usbhid.c,usbms.c,usbkbd.h
// 
// 40    1/11/13 4:15a Ryanchou
// [TAG]  		EIP102491
// [Category]  	Improvement
// [Description]  	Synchronized with Aptio V USB module
// [Files]  		usbport.c, usbsb.c, ehci.c, ehci.h, ohci.c, ohci.h, uhci.h,
// usb.c, usbdef.h, usbhid.c, usbhub.c, usbkbd.c, usbkbd.h, usbmass.c.
// usbms.c, usbpoint.c, xhci.h, usb.sd, amiusbhc.c, componentname.c,
// efiusbkc.c, efiusbmass.c, uhcd.c, uhcd.h, usbbus.c, usbbus.h, usbmisc.c
// 
// 39    11/10/12 6:39a Ryanchou
// [TAG]  		EIP99431
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Cannot use the UsbIo's UsbAsyncInterruptTransfer for
// keyboard input
// [RootCause]  	Stopping EFI USB keyboard driver does not stop the
// endpoint polling, then application calls UsbAsyncInterruptTransfer,
// error will be returned.
// [Solution]  	Stops endpoint polling and release resource when
// disconnecting the device driver. And improve the
// UsbSyncInterruptTransfer.
// [Files]  		amiusb.c, amiusb.h, ehci.c, ohci.c, uhci.c, usb.c,
// usbCCID.c, usbdef.h, usbhub.c, usbkbd.c, usbmass.c, usbms.c,
// usbpoint.c, amiusbhc.c, efiusbhid.c, usbbus.c, usbbus.h
// 
// 38    8/21/12 2:33a Roberthsu
// [TAG]           EIP91835
// [Category]      Bug Fix
// [Severity]      Normal
// [Symptom]       Wireless mouse auto click" problem
// [RootCause]     Because button status and X Y in different packet.
// [Solution]      Save button status.
// [Files]         usbms.c
// 
// 37    8/07/12 12:07a Roberthsu
// [TAG]           EIP95351
// [Category]      Bug Fix
// [Severity]      Normal
// [Symptom]       Usb mouse work unnormal
// [RootCause]     This mouse will send garbage data.
// [Solution]      Change check section
// [Files]         usbms.c
// 
// 36    5/03/12 6:30a Roberthsu
// [TAG]           EIP84455
// [Category]      Improvement
// [Description]   Implement usb hid device gencric.
// [Files]  		amiusb.c,amiusbhc.c,efiusbhid.c,efiusbkb.c,ehci.c,ohci.c,uhc
// d.c,uhci.c,usbdef.h,usbhid.c,usbhub.c,usbkbd.c,usbkbd.h,usbms.c,usbsb.c
// ,usbsrc.sdl
// 
// 35    9/27/11 1:40a Roberthsu
// [TAG]     EIP67400
// [Category]    Bug Fix
// [Severity]    Normal
// [Symptom]     Microsoft wireless Media Desktop 1000 can¡¦t work normal.
// [RootCause]   Wireless ms report data contains usage page keyboard.And
// output data not contains vaild report id.
// [Solution]    Check usage page led to decide kb or ms.Check correct
// report id with report data.
// [Files]       usbkbd.c,usbhid.c,usbms.c
// 
// 34    9/19/11 9:34a Lavanyap
// [TAG]  		EIP66198
// [Category]  	Improvement
// [Description]  	Added Mouse Wheel support in PS2 and USB drivers.
// [Files]  		usbdef.h, usbms.c, efiusbms.c, ps2mouse.h, mouse.c
// 
// 33    8/05/11 2:03a Ryanchou
// 
// 32    7/15/11 6:11a Ryanchou
// [TAG]  		EIP38434
// [Category]  	New Feature
// [Description]  	Added USB HID report protocol support.
// [Files]  		amiusb.c, AmiUsbController.h, amiusbhc.c, efiusbkb.c,
// efiusbkb.h, ehci.c, ohci.c, uhcd.c uhcd.cif, uhci.c, usb.c, usbdef.h,
// usbkbd.c, usbkbd.h, usbms.c, usbrt.cif, usbsb.c, usbsetup.c,
// usbsrc.sdl, xhci.c
// 
// 31    7/12/11 11:40p Ryanchou
// [TAG] EIP63752
// [Bug fix] Left click status lost on USB Mousee.
// [Symptom] First time Getstate will return that Left Click is Pressed.
// On the Second GetState Call it will return that Left click released.
// [Root Cause] We are clearing the Button status once we send the data.
// So next time getstate will return that left click is released.
// [Solution] We should not clear the ButtonStatus.Insteed of OR the data,
// we should have taken directly from the fpBuffer.
// 
// 30    5/03/11 6:56a Ryanchou
// [TAG]  		EIP57745
// [Category]  	Improvement
// [Description]  	The token CHECK_MOUSE_FLAG is depend on CSM version,
// remove the token and check CSM verion to support this feature or not.
// [Files]  		syskbc.c, usbms.c, usbsrc.sdl
// 
// 29    3/30/11 8:16a Ryanchou
// [TAG]  		EIP54126
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Sometimes system hangs at checkpoint 0xB4.
// [RootCause]  	The bLength field of configuration descriptor is zero.
// [Solution]  	Check wether bLength field is zero before paring next
// descriptor.
// [Files]  		usb.c, usbbus.c, usbhub.c, usbkbd.c, usbmass.c, usbms.c
// 
// 28    3/17/11 12:23a Ryanchou
// [TAG]  		EIP49214
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	USB mouse can't work on legacy free system
// [RootCause]  	The key repeat SMI didn't enabled.
// [Solution]  	Enable the key repeat SMI if receive mouse data.
// [Files]  		usbms.c
// 
// 27    2/10/11 7:41a Ryanchou
// [TAG]  		EIP52206
// [Category]  	Improvement
// [Description]  	Remote wakeup command should be sent before sleep,
// comment out  the command.
// [Files]  		usbkbd.c, usbms.c
// 
// 26    1/17/11 4:35a Ryanchou
// [TAG]  		EIP51108
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Some HID devices that has two interfaces can't work.
// [RootCause]  	BIOS using the wrong endpoint to poll KBD/MS data. 
// [Solution]  	Get the first interrupt in endpoint when parsing
// descriptors.
// [Files]  		usbkbd.c, usbms.c
// 
// 25    9/24/10 5:38p Olegi
// EIP38221: Added the code that properly initializes
// DEV_INFO.bIntEndpoint field; interrupt endpoint polling is using this
// endpoint number.
// 
// 24    9/16/10 1:08p Olegi
// - removed junk function code
// - EIP40959:: activate mouse polling depending on the project settings
// 
// 23    9/08/10 8:05a Ryanchou
// EIP43822: Add a toekn "CHECK_MOUSE_FLAG", the token controls whether
// apply EIP40121 solution.
// 
// 22    8/30/10 12:16p Olegi
// Send mouse data only when driver is active; EIP40121
// 
// 21    6/22/10 9:11p Olegi
// EIP39708: Added new incompatibility type for HIDs that ignore boot
// protocol.
// 
// 20    5/11/10 1:52p Olegi
// Corrected the mouse data report. EIP37798
// 
// 19    11/24/09 11:39a Olegi
// EIP#29733 - BIOS adds an USB API (Block KBC Access)
// 
// 18    9/10/09 3:58p Davidd
// Corrected build error caused by previous change.
// 
// 17    9/10/09 9:43a Olegi
// EIP25224: When set boot protocol for mouse, the interface number may
// not be 0. Some keyboard/mouse composite devices have one more
// interface.
// 
// 16    7/07/08 4:01p Olegi
// 
// 15    5/16/08 12:06p Olegi
// 
// 14    5/16/08 12:01p Olegi
// Compliance with AMI coding standard.
// 
// 13    12/17/07 4:04p Olegi
// KBC emulation support added.
// 
// 12    3/29/07 6:40p Olegi
// 
// 11    3/20/07 12:20p Olegi
//
// 9     4/14/06 6:39p Olegi
// Conversion to be able to use x64 compiler.
//
// 8     3/20/06 3:37p Olegi
// Version 8.5 - x64 compatible.
//
// 7     2/06/06 9:35a Andriyn
//
// 6     1/24/06 12:28p Andriyn
//
// 5     8/27/05 3:44p Andriyn
// Fix: lost mouse click when mouse is not moving
//
// 4     8/26/05 12:25p Andriyn
// Simulate Mouse Sampling rate by disabling Mouse Polling (reduce USB
// SMI# generation)
//
// 3     8/25/05 7:19p Andriyn
// USB Keyboard and mouse to use EMUL 60/64 for passing data to KBC.
// Fall-back when EMUL 60/64 is not present
//
// 2     8/04/05 5:03p Andriyn
// cosmetic changes
//
// 1     3/28/05 6:20p Olegi
//
// 2     3/18/05 9:41a Olegi
// Correction in Y coordinate calculations.
//
// 1     3/15/05 9:23a Olegi
// Initial VSS check-in.
//
//****************************************************************************

//<AMI_FHDR_START>
//-----------------------------------------------------------------------------
//
//  Name:           UsbMs.c
//
//  Description:    AMI USB mouse support implementation
//
//-----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "amidef.h"
#include "usbdef.h"
#include "amiusb.h"
#include "usbkbd.h"

extern  USB_GLOBAL_DATA     *gUsbData;

extern  UINT8 IsKbcAccessBlocked;	//(EIP29733+)

extern  EFI_EMUL6064MSINPUT_PROTOCOL* gMsInput;

VOID        USBMSInitialize (VOID);
DEV_INFO*   USBMSConfigureDevice (HC_STRUC*, DEV_INFO*, UINT8*, UINT16, UINT16);
UINT8       USBMSProcessMouseData (HC_STRUC*, DEV_INFO*, UINT8*, UINT8*, UINT16);
VOID        USBKeyRepeat(HC_STRUC*, UINT8);
VOID		SetMouseData (UINT8*, USBMS_DATA*, UINT8, UINT8, HID_REPORT_FIELD*);		//(EIP127014+)		
EFI_STATUS  SendMouseData(PS2MouseData*);
UINT8       OrgButtonStatus = 0;		//(EIP91835)

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        USBMSInitialize
//
// Description: This routine is called once to initialize the USB mouse data
//              area
//
// Input:       None
//
// Output:      Nothing
//
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID
USBMSInitialize()
{
    //
    // Initialize the mouse input buffer head and tail values
    //
    gUsbData->fpMouseInputBufferHeadPtr = &gUsbData->aMouseInputBuffer[0];
    gUsbData->fpMouseInputBufferTailPtr = &gUsbData->aMouseInputBuffer[0];
    USB_DEBUG(DEBUG_LEVEL_3, "USBMSInitialize: Head and Tail are at %x\n", gUsbData->fpMouseInputBufferHeadPtr);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   USBMSConfigureDevice
//
// Description: This routine checks an interface descriptor of the USB device
//              detected to see if it describes a HID/Boot/Mouse device.
//              If the device matches the above criteria, then the device is
//              configured and initialized
//
// Input:       HcStruc   HCStruc pointer
//              DevInfo   Device information structure pointer
//              Desc      Pointer to the descriptor structure
//              Start      Offset within interface descriptor
//                      supported by the device
//              End        End offset of the device descriptor
//
// Output:      FPDEV_INFO  New device info structure, 0 on error
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

DEV_INFO*
USBMSConfigureDevice (
    HC_STRUC*   HcStruc,
    DEV_INFO*   DevInfo,
    UINT8*      Desc,
    UINT16      Start,
    UINT16      End)
{
    return NULL;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        USBMSProcessMouseData
//
// Description: This function is called at regular intervals with USB mouse
//              report data. This function handles the translation of USB
//              mouse data into PS/2 mouse data, and makes the PS/2 data
//              available to software using ports 60/64 to communicate with
//              a PS/2 mouse.
//
// Input:       HcStruc   Pointer to HCStruc
//              DevInfo   Pointer to device information structure
//              Td        Pointer to the polling TD
//              Buffer    Pointer to the data buffer
//
// Output:      Nothing
//
// Notes:       The format of 3 byte data packet is as follow:
//               Byte              Description
//          -----------------------------------------------------------
//              0   Bit     Description
//              -------------------------------------------
//                   0      If set, button 1 is pressed
//                   1      If set, button 2 is pressed
//                   2      If set, button 3 is pressed
//                   3-7        Reserved
//              -------------------------------------------
//              1   X displacement value
//              2   Y displacement value
//          -----------------------------------------------------------
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8
USBMSProcessMouseData (
    HC_STRUC    *HcStruc,
    DEV_INFO    *DevInfo,
    UINT8       *Td,
    UINT8       *Buffer,
    UINT16      DataLength
)
{
    UINT8*              MachineConfigPtr = (UINT8*)(UINTN)0x410;
    PS2MouseData        MouseData;
    USBMS_DATA          TempData;
    INT32               Coordinates;
	HID_REPORT_FIELD 	*Field = NULL;
	UINT8 	            OffsetTmp = 0;
    UINT8               XStart;
    UINT8               XEnd;
    UINT8               YStart;
    UINT8               YEnd;
    UINT8               ButtonStart;
    UINT8               WheelStart;
    UINT8               i;
    UINT16              j;
    UINT8               ButtonSet = 0;
    UINT8               XSet = 0;
    UINT8               YSet = 0;
    UINT8               WheelSet = 0;

    //Is KBC access allowed?
    if (IsKbcAccessBlocked) {
        return USB_SUCCESS; //(EIP29733+)
    }
    
    MemSet(&TempData, sizeof(USBMS_DATA), 0);	//(EIP127014)

    if (DevInfo->HidReport.Flag & HID_REPORT_FLAG_REPORT_PROTOCOL) {
        //serach button and X Y
        for (i = 0; i < DevInfo->HidReport.FieldCount; i++) {
			Field = DevInfo->HidReport.Fields[i];

            //Check if it is input?
            if (!(Field->Flag & HID_REPORT_FIELD_FLAG_INPUT)) {
				continue;
            }
            //if report id is exist, check first byte
            if (Field->ReportId != 0 && Field->ReportId != Buffer[0]) {
				continue;
            }

            // Check if the field is contant.
            if (Field->Flag & HID_REPORT_FIELD_FLAG_CONSTANT) {
                OffsetTmp += Field->ReportCount * Field->ReportSize;
                continue;
            }
            
            //Check Button
            if ((Field->UsagePage == 9) && (Field->UsageCount != 0) && (Field->Usages[0] == 1)) {
                ButtonSet = 1;
                ButtonStart = OffsetTmp;
                if (Field->ReportId != 0) {
                    ButtonStart += 8;
                }
                ButtonStart /= 8;
                TempData.ButtonByte = *(Buffer + ButtonStart);
            }
            //Check X,Y
			if ((Field->UsagePage == 1) && (Field->UsageCount != 0)) {
                for (j = 0; j < Field->UsageCount; j++) {
					//find X
                    if (Field->Usages[j] == 0x30) {
                        XSet = 1;         
                        XStart = (OffsetTmp + j * Field->ReportSize); 
                        if (Field->ReportId != 0) {
                            XStart += 8;
                        }
                        XEnd = XStart + Field->ReportSize;
                        TempData.FillUsage = 0x30;	//(EIP127014)
                        SetMouseData(Buffer, &TempData, XStart, XEnd, Field);
					}
                    //find Y
					if (Field->Usages[j] == 0x31) {
                        YSet = 1;         
						YStart = (OffsetTmp + j * Field->ReportSize);
                        if (Field->ReportId != 0) {
                            YStart += 8;
                        }
						YEnd = YStart + Field->ReportSize;
                        TempData.FillUsage = 0x31;
						SetMouseData(Buffer, &TempData, YStart, YEnd, Field);
					}
					//find Wheel
                    if (Field->Usages[j] == 0x38) {
                        WheelSet = 1;
                        WheelStart = (OffsetTmp + j * Field->ReportSize) / 8;
                        if (Field->ReportId != 0) {
                            WheelStart += 1;
                        }
                        TempData.Z = *(Buffer + WheelStart);
					}
				}
			}
            OffsetTmp += Field->ReportCount * Field->ReportSize;
		}
                                        
        for (i = 0; i < 8; i++) {
        	Buffer[i] = 0;
        }

		//fill MS DATA
        if (ButtonSet != 0) {
    		*Buffer = TempData.ButtonByte;
            OrgButtonStatus = TempData.ButtonByte;
        } else {
            *Buffer = OrgButtonStatus;
        }
        
        if (XSet == 1) {
    	    *(Buffer + 1) = TempData.X;
        }
        if (YSet == 1) {
		    *(Buffer + 2) = TempData.Y;
        }
        if (WheelSet == 1) { 
		    *(Buffer + 3) = TempData.Z;
        }
	} else {
        TempData.EfiX = *((INT8*)Buffer + 1);
        TempData.EfiY = *((INT8*)Buffer + 2);
    }

    if (DevInfo->wIncompatFlags & USB_INCMPT_BOOT_PROTOCOL_IGNORED) {
        Buffer++;
    }
    
	if ((BOOT_PROTOCOL_SUPPORT == 0) && 
        !(DevInfo->wIncompatFlags & USB_INCMPT_HID_BOOT_PROTOCOL_ONLY)) {
        if (!(ButtonSet || XSet || YSet || WheelSet)) {
            return USB_SUCCESS;
        }
    }
    
 
    if (gUsbData->dUSBStateFlag & USB_FLAG_RUNNING_UNDER_EFI) {

        gUsbData->MouseData.ButtonStatus = *(UINT8*)Buffer;

        Coordinates = (INT16)TempData.EfiX;		//(EIP127014)
        gUsbData->MouseData.MouseX += Coordinates;

        Coordinates = (INT16)TempData.EfiY;		//(EIP127014)
        gUsbData->MouseData.MouseY += Coordinates;

        Coordinates= *((INT8*)Buffer + 3);
        gUsbData->MouseData.MouseZ += Coordinates;

        return USB_SUCCESS; // Here should be code that prepares buffer for AMIUHCD
    }

    if (!(*MachineConfigPtr & BIT2)) {
        return USB_SUCCESS;    // No mouse indication in BIOS Data area equipment byte
    }
/*
										//(EIP57745+)>
    //
    // Check the version of CSM16, support is available for ver 7.64 or later
    //
    {
        UINT8	MjCsmVer = *(UINT8*)0xF0018;
        UINT8	MnCsmVer = *(UINT8*)0xF0019;
		UINT8	mouse_flag3 = *((UINT8*)((UINTN)((*(UINT16*)0x40E) << 4) + 0x30));

        if (MjCsmVer > 7 || MnCsmVer > 0x63) {
			if(!(mouse_flag3 & BIT0)) {
				return USB_SUCCESS;
			}
        }
    } 
										//<(EIP57745+)
*/
    //
    // Check mouse data availability
    //
    if (gMsInput != 0) {
        //
        // Get mouse status byte and prepare it.
        // Bit 2, 1, 0 = Middle, right and left button status
        // Bit 3 is always 1
        //
        MouseData.flags = (*(UINT8*)Buffer) & 7 | 8;

        //
        // Get mouse X, Y position
        //
        MouseData.x = (*((UINT8*)Buffer + 1));
        MouseData.y = (UINT8)(-*((INT8*)Buffer + 2)); // Y data is opposite in USB than PS2

        //
        // Verify the direction of X-axis movement
        //
        if (MouseData.x >= 0x80) {
            MouseData.flags |= 0x10;    // Negative X-axis movement
        }
        if (MouseData.y >= 0x80) {
            MouseData.flags |= 0x20;    // Negative Y-axis movement
        }

        if (gUsbData->kbc_support || (gUsbData->dUSBStateFlag & USB_FLAG_6064EMULATION_ON)) {
            SendMouseData(&MouseData);
            USBKeyRepeat(NULL, 2);          // Enable Key repeat	//(EIP49214+)
        }				    
    }

    return USB_SUCCESS;
}
										//(EIP127014+)>
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SetMouseData
//
// Description: This routine checks for mouse type device from the
//              interface data provided
//
// Input:   bBaseClass  USB base class code
//          bSubClass   USB sub-class code
//          bProtocol   USB protocol code
//
// Output:  BIOS_DEV_TYPE_MOUSE type on success or 0FFH on error
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID
SetMouseData (
    UINT8           	*Buffer,
    USBMS_DATA 	    	*MsData,
    UINT8 		    	Start,
    UINT8 		    	End,
    HID_REPORT_FIELD	*Field
)
{
	UINT8 	ReportSize;
    UINT8   Size;
    UINT8   PreSkip;
    UINT8   PostSkip;
	UINT16  TempData = 0;
    UINT16  MinMask = 0;
    UINT16  Multi = 1;
    UINT16  Resolution;
    UINT16  Count = 0;
    UINT16  i;

    if ((Field->PhysicalMax == 0) && (Field->PhysicalMin == 0)) {
        Field->PhysicalMax = Field->LogicalMax;
        Field->PhysicalMin = Field->LogicalMin;
    }
    if (Field->UnitExponent != 0) {
        Count = (~Field->UnitExponent) + 1;
    }
    
    for (i = 0; i < Count; i++){
        Multi = Multi * 10;
    }        
    
    Resolution = ((INT16)Field->LogicalMax - (INT16)Field->LogicalMin) * Multi /
                 ((INT16)Field->PhysicalMax - (INT16)Field->PhysicalMin);

	ReportSize = End - Start;
    MinMask = ((~MinMask) >> ReportSize) << ReportSize;

	Size = ReportSize / 8;
    
    if ((ReportSize % 8) != 0) {
        Size++;
    }

	ASSERT(Size > 0 && Size <= sizeof(TempData));
    if ((Size == 0) || (Size > sizeof(TempData))) {
        return;
    }

    MemCpy(&TempData, Buffer + Start / 8, Size);

	PreSkip = Start % 8;
	PostSkip = End % 8;
		
    if (PreSkip != 0) {
        TempData = TempData >> PreSkip;
    }
    if (PostSkip != 0) {
        TempData = TempData << PostSkip; 	
        TempData = TempData >> PostSkip; 			
	}	

    if (TempData > Field->LogicalMax) {
        TempData |= MinMask;
    }

    if (MsData->FillUsage == 0x30) {
        MsData->EfiX = TempData;
        MsData->X = (UINT8)TempData; 
    }
    if (MsData->FillUsage == 0x31) {
        MsData->EfiY = TempData;
        MsData->Y = (UINT8)TempData; 
    } 

	return;
} 
										//<(EIP127014+)
//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2016, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************