summaryrefslogtreecommitdiff
path: root/Core/EM/usb/efiusbpoint.c
blob: d8ba13b4f6922d9a584020b0f63ef54c47796c2c (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
//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2015, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************

//****************************************************************************
// $Header: /Alaska/SOURCE/Modules/USB/ALASKA/efiusbpoint.c 7     5/28/15 5:01a Wilsonlee $
//
// $Revision: 7 $
//
// $Date: 5/28/15 5:01a $
//
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/USB/ALASKA/efiusbpoint.c $
// 
// 7     5/28/15 5:01a Wilsonlee
// [TAG]  		EIP218997
// [Category]  	Improvement
// [Description]  	Break the GetQueue loop if UninstallDevice is failed.
// [Files]  		usbbus.c, efiusbkb.c, efiusbmass.c, efiusbms.c,
// efiusbpoint.c, efiusbhid.c
// 
// 6     5/06/14 5:17a 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
// 
// 5     10/24/13 10:25p Wilsonlee
// [TAG]  		EIP131339
// [Category]  	Improvement
// [Description]  	Synchronize USB\Touch device mouse pointer position.
// [Files]  		efiusbms.c, efiusbpoint.c
// 
// 4     11/05/12 12:56a Rameshr
// [TAG]  		EIP103791
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	WaitForInput function destroys the absolute device data
// [RootCause]  	WaitforInput checks for the Input data and throws the
// data out from local buffer
// [Solution]  	Data availability checking done in current buffer, instead
// using the UpdateUsbAbsMouseData function
// [Files]  		efiusbpoint.c
// 
// 3     11/23/11 4:46a Roberthsu
// [TAG]           EIP74509
// [Category]      Improvement
// [Description]   Correct comment header
// [Files]         efiusbpoint.c
// 
// 2     8/05/11 7:29a Ryanchou
// [TAG]  		EIP66231
// [Category]  	Improvement
// [Description]  	Remove token POINT_SUPPORT.Add token USB_DEV_POINT.Add
// check core version in point driver.Add check device descriptor to send
// get lang id command.Modify check button usage page.
// [Files]  		efiusbhid.c, efiusbpoint.c, usbbus.c, usbhid.c, usbpoint.c,
// usbsrc.sdl
// 
// 1     7/15/11 6:26a Ryanchou
// [TAG]  		EIP38434
// [Category]  	New Feature
// [Description]  	Added USB HID report protocol support.
// [Files]  		amiusb.c, AmiUsbController.h, amiusbhc.c, efiusbkb.c,
// efiusbkb.h, efiusbpoint.c, ehci.c, ohci.c, uhcd.c uhcd.cif, uhci.c,
// usb.c, usbdef.h, usbhid.c, usbkbd.c, usbkbd.h, usbms.c, usbpoint.c,
// usbrt.cif, usbsb.c, usbsetup.c, usbsrc.sdl, xhci.c
// 
//****************************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
//  Name:           EfiusbAbs.C
//
//  Description:    EFI USB Absolute pointer Driver
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "amidef.h"
#include "usbdef.h"
#include "uhcd.h"
#include "componentname.h"
#include "usbbus.h"

#if USB_DEV_POINT                       //(EIP66231) 

#include "Protocol\AbsPointerProtocol.h"   
#define USB_ABSOLUTE_MOUSE_DRIVER_VERSION 1

#define USB_ABSOLUTE_MOUSE_DEV_SIGNATURE   EFI_SIGNATURE_32('u','a','b','s')
#define USB_ABSOLUTE_MOUSE_DEV_FROM_ABSOLUTE_PROTOCOL(a) \
    CR(a, USB_ABSOLUTE_MOUSE_DEV, AbsolutePointerProtocol, USB_ABSOLUTE_MOUSE_DEV_SIGNATURE)

EFI_GUID    gEfiAbsolutePointerProtocolGuid=EFI_ABSOLUTE_POINTER_PROTOCOL_GUID;

typedef struct
{
    UINTN                           Signature;
    EFI_ABSOLUTE_POINTER_PROTOCOL   AbsolutePointerProtocol;
    EFI_ABSOLUTE_POINTER_STATE      State;
    EFI_ABSOLUTE_POINTER_MODE       Mode;
    BOOLEAN                         StateChanged;
} USB_ABSOLUTE_MOUSE_DEV;

static VOID
UsbAbsMouseWaitForInput (
  IN  EFI_EVENT               Event,
  IN  VOID                    *Context
  );

static EFI_STATUS
UpdateUsbAbsMouseData (
  IN  ABS_MOUSE              *Data
  );

//
// ABS Protocol
//
static EFI_STATUS
GetAbsMouseState(
  IN   EFI_ABSOLUTE_POINTER_PROTOCOL  *This,
  OUT  EFI_ABSOLUTE_POINTER_STATE     *AbsState
);

static EFI_STATUS
UsbAbsMouseReset(
  IN EFI_ABSOLUTE_POINTER_PROTOCOL      *This,
  IN BOOLEAN                            ExtendedVerification
  );

extern USB_GLOBAL_DATA *gUsbData;

USB_ABSOLUTE_MOUSE_DEV         *UsbAbsMouseDevice=0;
static int                      gAbsMouseRefCount=0;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name:        InstallUSBAbsMouse
//
// Description: Installs ABSOLUTEPointerProtocol interface on a given controller.
//
// Input:       Controller - controller handle to install interface on.
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
InstallUSBAbsMouse(
    EFI_HANDLE Controller,
    DEV_INFO   *pDevInfo
)
{
    EFI_STATUS Status;
    int RefCount;

    ATOMIC( RefCount = gAbsMouseRefCount++ );

    if (RefCount == 0){
        VERIFY_EFI_ERROR(
            Status = gBS->AllocatePool(
            EfiBootServicesData,
            sizeof(USB_ABSOLUTE_MOUSE_DEV),
            &UsbAbsMouseDevice));

        EfiZeroMem(UsbAbsMouseDevice, sizeof(USB_ABSOLUTE_MOUSE_DEV));

        //
        // Initialize UsbABSDevice
        //
        UsbAbsMouseDevice->Signature = USB_ABSOLUTE_MOUSE_DEV_SIGNATURE;

        UsbAbsMouseDevice->AbsolutePointerProtocol.GetState = GetAbsMouseState;
        UsbAbsMouseDevice->AbsolutePointerProtocol.Reset = UsbAbsMouseReset;
        UsbAbsMouseDevice->AbsolutePointerProtocol.Mode = &UsbAbsMouseDevice->Mode;

        UsbAbsMouseDevice->Mode.Attributes = EFI_ABSP_SupportsPressureAsZ;

        UsbAbsMouseDevice->Mode.AbsoluteMinX = 0;
        UsbAbsMouseDevice->Mode.AbsoluteMinY = 0;
        UsbAbsMouseDevice->Mode.AbsoluteMinZ = 0;
        UsbAbsMouseDevice->Mode.AbsoluteMaxX = pDevInfo->HidReport.AbsMaxX;
        UsbAbsMouseDevice->Mode.AbsoluteMaxY = pDevInfo->HidReport.AbsMaxY;
        UsbAbsMouseDevice->Mode.AbsoluteMaxZ = 0;

        EfiZeroMem (&UsbAbsMouseDevice->State, sizeof(EFI_ABSOLUTE_POINTER_STATE));
        UsbAbsMouseDevice->StateChanged = FALSE;

        UsbAbsMouseReset(NULL, FALSE);
        
        VERIFY_EFI_ERROR(
            Status = gBS->CreateEvent (
            EFI_EVENT_NOTIFY_WAIT,
            EFI_TPL_NOTIFY,
            UsbAbsMouseWaitForInput,
            UsbAbsMouseDevice,
            &((UsbAbsMouseDevice->AbsolutePointerProtocol).WaitForInput)
            ));

        USB_DEBUG(DEBUG_LEVEL_4, "ABS event is created, status = %r\n", Status);
    }
    //
    // Install protocol interfaces for the USB ABS device
    //
    VERIFY_EFI_ERROR(
        Status = gBS->InstallProtocolInterface(
        &Controller,
        &gEfiAbsolutePointerProtocolGuid,
        EFI_NATIVE_INTERFACE,
        &UsbAbsMouseDevice->AbsolutePointerProtocol));

    USB_DEBUG(DEBUG_LEVEL_4, "ABS protocol is installed, status = %r\n", Status);
	
	return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name:        UninstallUSBAbsMouse
//
// Description: Uninstalls ABSOLUTEPointerProtocol interface.
//
// Input:       Controller - controller handle.
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
UninstallUSBAbsMouse(
    IN EFI_HANDLE Controller
)
{
    EFI_STATUS Status;
    int RefCount;

    Status = gBS->UninstallProtocolInterface(
            Controller,
            &gEfiAbsolutePointerProtocolGuid,
            &UsbAbsMouseDevice->AbsolutePointerProtocol);
    
    if (EFI_ERROR(Status)) {
        return Status;
    }

    ATOMIC( RefCount = --gAbsMouseRefCount );
    if (RefCount == 0) {
        VERIFY_EFI_ERROR(
            gBS->CloseEvent (
            (UsbAbsMouseDevice->AbsolutePointerProtocol).WaitForInput));

        gBS->FreePool(UsbAbsMouseDevice);
        UsbAbsMouseDevice = 0;
    }
    return Status;
}




//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name:        UsbAbsMouseReset
//
// Description: This routine is a part of ABSOLUTEPointerProtocol implementation;
//              it resets USB ABS.
//
// Input:       This - A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance.
//              ExtendedVerification - Indicates that the driver may perform
//              a more exhaustive verification operation of the device during
//              reset.
//
// Output:      EFI_SUCCESS or EFI_DEVICE_ERROR
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

static EFI_STATUS
UsbAbsMouseReset(
    IN EFI_ABSOLUTE_POINTER_PROTOCOL    *This,
    IN BOOLEAN                          ExtendedVerification
  )
{

    EfiZeroMem (
            &UsbAbsMouseDevice->State,
            sizeof(EFI_ABSOLUTE_POINTER_STATE)
            );
    UsbAbsMouseDevice->StateChanged = FALSE;
    
    EfiZeroMem (&gUsbData->AbsMouseData, 10 * sizeof(ABS_MOUSE));

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name:        GetAbsMouseState
//
// Description: This routine is a part of ABSOLUTEPointerProtocol implementation;
//              it retrieves the current state of a pointer device.
//
// Input:       This - A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance.
//              ABSState - A pointer to the state information on the pointer
//              device. Type EFI_ABSOLUTE_POINTER_STATE is defined as follows:
//                typedef struct {
//                    INT32 RelativeMovementX;
//                    INT32 RelativeMovementY;
//                    INT32 RelativeMovementZ;
//                    BOOLEAN LeftButton;
//                    BOOLEAN RightButton;
//                } EFI_ABSOLUTE_POINTER_STATE;
//
// Output:      EFI_SUCCESS      - The state of the pointer device was returned
//                                 in ABSState.
//              EFI_NOT_READY    - The state of the pointer device has not changed
//                                 since the last call to GetABSState().
//              EFI_DEVICE_ERROR - A device error occurred while attempting to
//                                 retrieve the pointer device’s current state.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

static EFI_STATUS
GetAbsMouseState(
    EFI_ABSOLUTE_POINTER_PROTOCOL  *This,
    EFI_ABSOLUTE_POINTER_STATE     *AbsMouseState
)
{
    if (AbsMouseState == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    UpdateUsbAbsMouseData (&gUsbData->AbsMouseData[0]);

    if (UsbAbsMouseDevice->StateChanged == FALSE) {
        return EFI_NOT_READY;
    }

    EfiCopyMem(
        AbsMouseState,
        &UsbAbsMouseDevice->State,
        sizeof(EFI_ABSOLUTE_POINTER_STATE)
    );
    UsbAbsMouseDevice->StateChanged = FALSE;

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name:        UpdateUsbAbsMouseData
//
// Description: This routine updates current AbsMouse data.
//
// Input:       Data* - pointer to the data area to be updated.
//
// Output:      EFI_SUCCESS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

static EFI_STATUS
UpdateUsbAbsMouseData (
    ABS_MOUSE *AbsData
)
{
    if(UsbAbsMouseDevice->State.CurrentX != (UINT64)AbsData->Xcoordinate ||
        UsbAbsMouseDevice->State.CurrentY != (UINT64)AbsData->Ycoordinate ||
        UsbAbsMouseDevice->State.CurrentZ != (UINT64)AbsData->Pressure ||
        UsbAbsMouseDevice->State.ActiveButtons != (UINT32)AbsData->ButtonStauts) {
            UsbAbsMouseDevice->StateChanged=TRUE;
    } else {
            UsbAbsMouseDevice->StateChanged=FALSE;
    }

    if(UsbAbsMouseDevice->StateChanged) {
        UsbAbsMouseDevice->State.ActiveButtons = (UINT32)AbsData->ButtonStauts;
        UsbAbsMouseDevice->State.CurrentX = (UINT64)AbsData->Xcoordinate;
        UsbAbsMouseDevice->State.CurrentY = (UINT64)AbsData->Ycoordinate;
        UsbAbsMouseDevice->State.CurrentZ = (UINT64)AbsData->Pressure; 
        UsbAbsMouseDevice->Mode.AbsoluteMaxX= (UINT64)AbsData->Max_X; 
        UsbAbsMouseDevice->Mode.AbsoluteMaxY= (UINT64)AbsData->Max_Y;  
    }

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name:        UsbAbsMouseWaitForInput
//
// Description: Event notification function for AbsMouseOLUTE_POINTER.WaitForInput
//              event. Signal the event if there is input from AbsMouse.
//
// Input:       Event - event to signal in case of AbsMouse activity
//              Context - data to pass along with the event.
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

static VOID
EFIAPI
UsbAbsMouseWaitForInput (
    EFI_EVENT   Event,
    VOID        *Context
    )
{

    //
    // Someone is waiting on the AbsMouse event, if there's
    // input from AbsMouse, signal the event
    //
    if(UsbAbsMouseDevice->State.CurrentX != (UINT64)gUsbData->AbsMouseData[0].Xcoordinate ||
       UsbAbsMouseDevice->State.CurrentY != (UINT64)gUsbData->AbsMouseData[0].Ycoordinate ||
       UsbAbsMouseDevice->State.CurrentZ != (UINT64)gUsbData->AbsMouseData[0].Pressure ||
       UsbAbsMouseDevice->State.ActiveButtons != (UINT32)gUsbData->AbsMouseData[0].ButtonStauts) {
        gBS->SignalEvent(Event);
    }

    return;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbAbsMsStop
//
// Description: Stops USB ABS device
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
UsbAbsMsStop (
    EFI_DRIVER_BINDING_PROTOCOL *This,
    EFI_HANDLE Controller,
    UINTN NumberOfChildren,
    EFI_HANDLE *Children
)
{
    EFI_STATUS Status;

    Status = UninstallUSBAbsMouse(Controller);
    VERIFY_EFI_ERROR(
        gBS->CloseProtocol (
        Controller, &gEfiUsbIoProtocolGuid,
        This->DriverBindingHandle, Controller));
    return Status;
}

#endif

//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2015, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************