TianoCore EDK2 master
Loading...
Searching...
No Matches
UsbCdcEcm.c
Go to the documentation of this file.
1
8#include "UsbCdcEcm.h"
9
10EFI_DRIVER_BINDING_PROTOCOL gUsbEcmDriverBinding = {
14 USB_ECM_DRIVER_VERSION,
15 NULL,
16 NULL
17};
18
28BOOLEAN
31 )
32{
33 EFI_STATUS Status;
34 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
35
36 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);
37 if (EFI_ERROR (Status)) {
38 return FALSE;
39 }
40
41 if ((InterfaceDescriptor.InterfaceClass == USB_CDC_CLASS) &&
42 (InterfaceDescriptor.InterfaceSubClass == USB_CDC_ECM_SUBCLASS) &&
43 (InterfaceDescriptor.InterfaceProtocol == USB_NO_CLASS_PROTOCOL))
44 {
45 return TRUE;
46 }
47
48 return FALSE;
49}
50
65EFIAPI
68 IN EFI_HANDLE ControllerHandle,
69 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
70 )
71{
72 EFI_STATUS Status;
74
75 Status = gBS->OpenProtocol (
76 ControllerHandle,
77 &gEfiUsbIoProtocolGuid,
78 (VOID **)&UsbIo,
79 This->DriverBindingHandle,
80 ControllerHandle,
81 EFI_OPEN_PROTOCOL_BY_DRIVER
82 );
83 if (EFI_ERROR (Status)) {
84 return Status;
85 }
86
87 Status = IsSupportedDevice (UsbIo) ? EFI_SUCCESS : EFI_UNSUPPORTED;
88
89 gBS->CloseProtocol (
90 ControllerHandle,
91 &gEfiUsbIoProtocolGuid,
92 This->DriverBindingHandle,
93 ControllerHandle
94 );
95 return Status;
96}
97
110 IN EFI_DEVICE_PATH_PROTOCOL *UsbEthPath,
111 IN EFI_DEVICE_PATH_PROTOCOL *UsbCdcDataPath
112 )
113{
114 while (1) {
115 if ((UsbEthPath->Type == ACPI_DEVICE_PATH) && (UsbEthPath->SubType == ACPI_DP)) {
116 if (CompareMem ((ACPI_HID_DEVICE_PATH *)UsbCdcDataPath, (ACPI_HID_DEVICE_PATH *)UsbEthPath, sizeof (ACPI_HID_DEVICE_PATH))) {
117 return EFI_NOT_FOUND;
118 }
119 }
120
121 if ((UsbEthPath->Type == HARDWARE_DEVICE_PATH) && (UsbEthPath->SubType == HW_PCI_DP)) {
122 if (CompareMem ((PCI_DEVICE_PATH *)UsbCdcDataPath, (PCI_DEVICE_PATH *)UsbEthPath, sizeof (PCI_DEVICE_PATH))) {
123 return EFI_NOT_FOUND;
124 }
125 }
126
127 if ((UsbEthPath->Type == MESSAGING_DEVICE_PATH) && (UsbEthPath->SubType == MSG_USB_DP)) {
128 if (IsDevicePathEnd (NextDevicePathNode (UsbEthPath))) {
129 if (((USB_DEVICE_PATH *)UsbEthPath)->ParentPortNumber ==
130 ((USB_DEVICE_PATH *)UsbCdcDataPath)->ParentPortNumber)
131 {
132 return EFI_SUCCESS;
133 } else {
134 return EFI_NOT_FOUND;
135 }
136 } else {
137 if (CompareMem ((USB_DEVICE_PATH *)UsbCdcDataPath, (USB_DEVICE_PATH *)UsbEthPath, sizeof (USB_DEVICE_PATH))) {
138 return EFI_NOT_FOUND;
139 }
140 }
141 }
142
143 UsbEthPath = NextDevicePathNode (UsbEthPath);
144 UsbCdcDataPath = NextDevicePathNode (UsbCdcDataPath);
145 }
146}
147
158BOOLEAN
160 IN EFI_DEVICE_PATH_PROTOCOL *UsbEthPath,
161 IN OUT EFI_HANDLE *UsbCdcDataHandle
162 )
163{
164 EFI_STATUS Status;
165 UINTN Index;
166 UINTN HandleCount;
167 EFI_HANDLE *HandleBuffer;
168 EFI_USB_IO_PROTOCOL *UsbIo;
170 EFI_DEVICE_PATH_PROTOCOL *UsbCdcDataPath;
171
172 Status = gBS->LocateHandleBuffer (
174 &gEfiUsbIoProtocolGuid,
175 NULL,
176 &HandleCount,
177 &HandleBuffer
178 );
179 if (EFI_ERROR (Status)) {
180 return FALSE;
181 }
182
183 for (Index = 0; Index < HandleCount; Index++) {
184 Status = gBS->HandleProtocol (
185 HandleBuffer[Index],
186 &gEfiUsbIoProtocolGuid,
187 (VOID **)&UsbIo
188 );
189 ASSERT_EFI_ERROR (Status);
190
191 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface);
192 ASSERT_EFI_ERROR (Status);
193
194 if ((Interface.InterfaceClass == USB_CDC_DATA_CLASS) &&
195 (Interface.InterfaceSubClass == USB_CDC_DATA_SUBCLASS) &&
196 (Interface.InterfaceProtocol == USB_NO_CLASS_PROTOCOL))
197 {
198 Status = gBS->HandleProtocol (
199 HandleBuffer[Index],
200 &gEfiDevicePathProtocolGuid,
201 (VOID **)&UsbCdcDataPath
202 );
203 if (EFI_ERROR (Status)) {
204 continue;
205 }
206
207 Status = IsSameDevice (UsbEthPath, UsbCdcDataPath);
208 if (!EFI_ERROR (Status)) {
209 CopyMem (UsbCdcDataHandle, &HandleBuffer[Index], sizeof (EFI_HANDLE));
210 FreePool (HandleBuffer);
211 return TRUE;
212 }
213 }
214 }
215
216 FreePool (HandleBuffer);
217 return FALSE;
218}
219
228VOID
229EFIAPI
231 IN EFI_EVENT Event,
232 IN VOID *Context
233 )
234{
235 EFI_STATUS Status;
236 UINTN Index;
237 UINTN HandleCount;
238 EFI_HANDLE *HandleBuffer;
239 EFI_USB_IO_PROTOCOL *UsbIo;
241
242 Status = gBS->LocateHandleBuffer (
244 &gEfiUsbIoProtocolGuid,
245 NULL,
246 &HandleCount,
247 &HandleBuffer
248 );
249 if (EFI_ERROR (Status)) {
250 return;
251 }
252
253 for (Index = 0; Index < HandleCount; Index++) {
254 Status = gBS->HandleProtocol (
255 HandleBuffer[Index],
256 &gEfiUsbIoProtocolGuid,
257 (VOID **)&UsbIo
258 );
259 ASSERT_EFI_ERROR (Status);
260
261 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface);
262 ASSERT_EFI_ERROR (Status);
263
264 if ((Interface.InterfaceClass == USB_CDC_CLASS) &&
265 (Interface.InterfaceSubClass == USB_CDC_ECM_SUBCLASS) &&
266 (Interface.InterfaceProtocol == USB_NO_CLASS_PROTOCOL))
267 {
268 gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
269 }
270 }
271
272 FreePool (HandleBuffer);
273 gBS->CloseEvent (Event);
274}
275
291EFIAPI
294 IN EFI_HANDLE ControllerHandle,
295 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
296 )
297{
298 EFI_STATUS Status;
299 VOID *Reg;
300 EFI_EVENT Event;
301 USB_ETHERNET_DRIVER *UsbEthDriver;
302 EFI_DEVICE_PATH_PROTOCOL *UsbEthPath;
303 EFI_HANDLE UsbCdcDataHandle;
304 EFI_USB_IO_PROTOCOL *UsbIo;
306
307 Status = gBS->OpenProtocol (
308 ControllerHandle,
309 &gEfiUsbIoProtocolGuid,
310 (VOID **)&UsbIo,
311 This->DriverBindingHandle,
312 ControllerHandle,
313 EFI_OPEN_PROTOCOL_BY_DRIVER
314 );
315 if (EFI_ERROR (Status)) {
316 return Status;
317 }
318
319 Status = gBS->OpenProtocol (
320 ControllerHandle,
321 &gEfiDevicePathProtocolGuid,
322 (VOID **)&UsbEthPath,
323 This->DriverBindingHandle,
324 ControllerHandle,
325 EFI_OPEN_PROTOCOL_GET_PROTOCOL
326 );
327 if (EFI_ERROR (Status)) {
328 gBS->CloseProtocol (
329 ControllerHandle,
330 &gEfiUsbIoProtocolGuid,
331 This->DriverBindingHandle,
332 ControllerHandle
333 );
334 return Status;
335 }
336
337 Status = IsUsbCdcData (UsbEthPath, &UsbCdcDataHandle) ? EFI_SUCCESS : EFI_UNSUPPORTED;
338 if (EFI_ERROR (Status)) {
339 gBS->CloseProtocol (
340 ControllerHandle,
341 &gEfiUsbIoProtocolGuid,
342 This->DriverBindingHandle,
343 ControllerHandle
344 );
345
346 Status = gBS->CreateEvent (EVT_NOTIFY_SIGNAL, TPL_CALLBACK, CallbackFunction, NULL, &Event);
347 if (EFI_ERROR (Status)) {
348 return Status;
349 }
350
351 Status = gBS->RegisterProtocolNotify (&gEfiUsbIoProtocolGuid, Event, &Reg);
352 return Status;
353 }
354
355 UsbEthDriver = AllocateZeroPool (sizeof (USB_ETHERNET_DRIVER));
356 if (!UsbEthDriver) {
357 gBS->CloseProtocol (
358 ControllerHandle,
359 &gEfiUsbIoProtocolGuid,
360 This->DriverBindingHandle,
361 ControllerHandle
362 );
363 return EFI_OUT_OF_RESOURCES;
364 }
365
366 Status = LoadAllDescriptor (UsbIo, &UsbEthDriver->Config);
367 ASSERT_EFI_ERROR (Status);
368
369 GetEndpoint (UsbIo, UsbEthDriver);
370
371 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface);
372 ASSERT_EFI_ERROR (Status);
373
374 UsbEthDriver->Signature = USB_ETHERNET_SIGNATURE;
375 UsbEthDriver->NumOfInterface = Interface.InterfaceNumber;
376 UsbEthDriver->UsbCdcDataHandle = UsbCdcDataHandle;
377 UsbEthDriver->UsbIo = UsbIo;
378 UsbEthDriver->UsbEth.UsbEthReceive = UsbEthEcmReceive;
379 UsbEthDriver->UsbEth.UsbEthTransmit = UsbEthEcmTransmit;
380 UsbEthDriver->UsbEth.UsbEthInterrupt = UsbEthEcmInterrupt;
381 UsbEthDriver->UsbEth.UsbEthMacAddress = GetUsbEthMacAddress;
382 UsbEthDriver->UsbEth.UsbEthMaxBulkSize = UsbEthEcmBulkSize;
383 UsbEthDriver->UsbEth.UsbHeaderFunDescriptor = GetUsbHeaderFunDescriptor;
384 UsbEthDriver->UsbEth.UsbUnionFunDescriptor = GetUsbUnionFunDescriptor;
385 UsbEthDriver->UsbEth.UsbEthFunDescriptor = GetUsbEthFunDescriptor;
386 UsbEthDriver->UsbEth.SetUsbEthMcastFilter = SetUsbEthMcastFilter;
387 UsbEthDriver->UsbEth.SetUsbEthPowerPatternFilter = SetUsbEthPowerFilter;
388 UsbEthDriver->UsbEth.GetUsbEthPowerPatternFilter = GetUsbEthPowerFilter;
389 UsbEthDriver->UsbEth.SetUsbEthPacketFilter = SetUsbEthPacketFilter;
390 UsbEthDriver->UsbEth.GetUsbEthStatistic = GetUsbEthStatistic;
391
392 Status = gBS->InstallProtocolInterface (
393 &ControllerHandle,
394 &gEdkIIUsbEthProtocolGuid,
396 &(UsbEthDriver->UsbEth)
397 );
398 if (EFI_ERROR (Status)) {
399 gBS->CloseProtocol (
400 ControllerHandle,
401 &gEfiUsbIoProtocolGuid,
402 This->DriverBindingHandle,
403 ControllerHandle
404 );
405 FreePool (UsbEthDriver);
406 return Status;
407 }
408
409 return Status;
410}
411
426EFIAPI
429 IN EFI_HANDLE ControllerHandle,
430 IN UINTN NumberOfChildren,
431 IN EFI_HANDLE *ChildHandleBuffer
432 )
433{
434 EFI_STATUS Status;
435 EDKII_USB_ETHERNET_PROTOCOL *UsbEthProtocol;
436 USB_ETHERNET_DRIVER *UsbEthDriver;
437
438 Status = gBS->OpenProtocol (
439 ControllerHandle,
440 &gEdkIIUsbEthProtocolGuid,
441 (VOID **)&UsbEthProtocol,
442 This->DriverBindingHandle,
443 ControllerHandle,
444 EFI_OPEN_PROTOCOL_GET_PROTOCOL
445 );
446 if (EFI_ERROR (Status)) {
447 return Status;
448 }
449
450 UsbEthDriver = USB_ETHERNET_DEV_FROM_THIS (UsbEthProtocol);
451
452 Status = gBS->UninstallProtocolInterface (
453 ControllerHandle,
454 &gEdkIIUsbEthProtocolGuid,
455 UsbEthProtocol
456 );
457 if (EFI_ERROR (Status)) {
458 return Status;
459 }
460
461 Status = gBS->CloseProtocol (
462 ControllerHandle,
463 &gEfiUsbIoProtocolGuid,
464 This->DriverBindingHandle,
465 ControllerHandle
466 );
467 FreePool (UsbEthDriver->Config);
468 FreePool (UsbEthDriver);
469 return Status;
470}
471
485EFIAPI
487 IN EFI_HANDLE ImageHandle,
488 IN EFI_SYSTEM_TABLE *SystemTable
489 )
490{
491 gUsbEcmDriverBinding.DriverBindingHandle = ImageHandle;
492 gUsbEcmDriverBinding.ImageHandle = ImageHandle;
493
494 return gBS->InstallMultipleProtocolInterfaces (
495 &gUsbEcmDriverBinding.DriverBindingHandle,
496 &gEfiDriverBindingProtocolGuid,
497 &gUsbEcmDriverBinding,
498 &gEfiComponentName2ProtocolGuid,
499 &gUsbEcmComponentName2,
500 NULL
501 );
502}
UINT64 UINTN
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define HARDWARE_DEVICE_PATH
Definition: DevicePath.h:68
#define ACPI_DEVICE_PATH
Definition: DevicePath.h:190
#define MSG_USB_DP
Definition: DevicePath.h:418
#define ACPI_DP
Definition: DevicePath.h:195
#define HW_PCI_DP
Definition: DevicePath.h:73
#define MESSAGING_DEVICE_PATH
Definition: DevicePath.h:321
BOOLEAN EFIAPI IsDevicePathEnd(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_EVENT
Definition: UefiBaseType.h:37
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
@ EFI_NATIVE_INTERFACE
Definition: UefiSpec.h:1193
@ ByProtocol
Definition: UefiSpec.h:1518
EFI_STATUS EFIAPI UsbEcmDriverStop(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE ControllerHandle, IN UINTN NumberOfChildren, IN EFI_HANDLE *ChildHandleBuffer)
Definition: UsbCdcEcm.c:427
EFI_STATUS EFIAPI UsbEcmEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: UsbCdcEcm.c:486
EFI_STATUS EFIAPI UsbEcmDriverSupported(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE ControllerHandle, IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath)
Definition: UsbCdcEcm.c:66
BOOLEAN IsSupportedDevice(IN EFI_USB_IO_PROTOCOL *UsbIo)
Definition: UsbCdcEcm.c:29
EFI_STATUS IsSameDevice(IN EFI_DEVICE_PATH_PROTOCOL *UsbEthPath, IN EFI_DEVICE_PATH_PROTOCOL *UsbCdcDataPath)
Definition: UsbCdcEcm.c:109
EFI_STATUS EFIAPI UsbEcmDriverStart(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE ControllerHandle, IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath)
Definition: UsbCdcEcm.c:292
VOID EFIAPI CallbackFunction(IN EFI_EVENT Event, IN VOID *Context)
Definition: UsbCdcEcm.c:230
BOOLEAN IsUsbCdcData(IN EFI_DEVICE_PATH_PROTOCOL *UsbEthPath, IN OUT EFI_HANDLE *UsbCdcDataHandle)
Definition: UsbCdcEcm.c:159
EFI_STATUS EFIAPI GetUsbEthMacAddress(IN EDKII_USB_ETHERNET_PROTOCOL *This, OUT EFI_MAC_ADDRESS *MacAddress)
EFI_STATUS EFIAPI SetUsbEthPowerFilter(IN EDKII_USB_ETHERNET_PROTOCOL *This, IN UINT16 Value, IN UINT16 Length, IN VOID *PatternFilter)
EFI_STATUS EFIAPI SetUsbEthPacketFilter(IN EDKII_USB_ETHERNET_PROTOCOL *This, IN UINT16 Value)
EFI_STATUS EFIAPI UsbEthEcmInterrupt(IN EDKII_USB_ETHERNET_PROTOCOL *This, IN BOOLEAN IsNewTransfer, IN UINTN PollingInterval, IN EFI_USB_DEVICE_REQUEST *Request)
VOID GetEndpoint(IN EFI_USB_IO_PROTOCOL *UsbIo, IN OUT USB_ETHERNET_DRIVER *UsbEthDriver)
EFI_STATUS EFIAPI UsbEthEcmBulkSize(IN EDKII_USB_ETHERNET_PROTOCOL *This, OUT UINTN *BulkSize)
EFI_STATUS EFIAPI GetUsbEthFunDescriptor(IN EDKII_USB_ETHERNET_PROTOCOL *This, OUT USB_ETHERNET_FUN_DESCRIPTOR *UsbEthFunDescriptor)
EFI_STATUS EFIAPI GetUsbEthPowerFilter(IN EDKII_USB_ETHERNET_PROTOCOL *This, IN UINT16 Value, OUT BOOLEAN *PatternActive)
EFI_STATUS LoadAllDescriptor(IN EFI_USB_IO_PROTOCOL *UsbIo, OUT EFI_USB_CONFIG_DESCRIPTOR **ConfigDesc)
EFI_STATUS EFIAPI UsbEthEcmTransmit(IN PXE_CDB *Cdb, IN EDKII_USB_ETHERNET_PROTOCOL *This, IN VOID *Packet, IN OUT UINTN *PacketLength)
EFI_STATUS EFIAPI GetUsbEthStatistic(IN EDKII_USB_ETHERNET_PROTOCOL *This, IN UINT16 FeatureSelector, OUT VOID *Statistic)
EFI_STATUS EFIAPI SetUsbEthMcastFilter(IN EDKII_USB_ETHERNET_PROTOCOL *This, IN UINT16 Value, IN VOID *McastAddr)
EFI_STATUS EFIAPI GetUsbHeaderFunDescriptor(IN EDKII_USB_ETHERNET_PROTOCOL *This, OUT USB_HEADER_FUN_DESCRIPTOR *UsbHeaderFunDescriptor)
EFI_STATUS EFIAPI UsbEthEcmReceive(IN PXE_CDB *Cdb, IN EDKII_USB_ETHERNET_PROTOCOL *This, IN OUT VOID *Packet, IN OUT UINTN *PacketLength)
EFI_STATUS EFIAPI GetUsbUnionFunDescriptor(IN EDKII_USB_ETHERNET_PROTOCOL *This, OUT USB_UNION_FUN_DESCRIPTOR *UsbUnionFunDescriptor)