summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndis.c
blob: 056b0ff0fd8e10c5d4db6d17ca145cb499536dc8 (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
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
/** @file
  This file contains code for USB Remote Network Driver
  Interface Spec. Driver Binding

  Copyright (c) 2023, American Megatrends International LLC. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include "UsbRndis.h"

EFI_DRIVER_BINDING_PROTOCOL  gUsbRndisDriverBinding = {
  UsbRndisDriverSupported,
  UsbRndisDriverStart,
  UsbRndisDriverStop,
  USB_RNDIS_DRIVER_VERSION,
  NULL,
  NULL
};

/**
  Check if this interface is USB Rndis SubType

  @param[in]  UsbIo  A pointer to the EFI_USB_IO_PROTOCOL instance.

  @retval TRUE   USB Rndis SubType.
  @retval FALSE  Not USB Rndis SubType.

**/
BOOLEAN
IsSupportedDevice (
  IN EFI_USB_IO_PROTOCOL  *UsbIo
  )
{
  EFI_STATUS                    Status;
  EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;

  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);
  if (EFI_ERROR (Status)) {
    return FALSE;
  }

  // Check specific device/RNDIS and CDC-DATA
  if (((InterfaceDescriptor.InterfaceClass == 0x2) &&
       (InterfaceDescriptor.InterfaceSubClass == 0x2) &&
       (InterfaceDescriptor.InterfaceProtocol == 0xFF)) || \
      ((InterfaceDescriptor.InterfaceClass == 0xEF) &&
       (InterfaceDescriptor.InterfaceSubClass == 0x4) &&
       (InterfaceDescriptor.InterfaceProtocol == 0x1)) || \
      ((InterfaceDescriptor.InterfaceClass == 0xA) &&
       (InterfaceDescriptor.InterfaceSubClass == 0x0) &&
       (InterfaceDescriptor.InterfaceProtocol == 0x00))
      )
  {
    return TRUE;
  }

  return FALSE;
}

/**
  Check if this interface is USB Rndis SubType but not CDC Data interface

  @param[in]  UsbIo  A pointer to the EFI_USB_IO_PROTOCOL instance.

  @retval TRUE   USB Rndis SubType.
  @retval FALSE  Not USB Rndis SubType.
**/
BOOLEAN
IsRndisInterface (
  IN EFI_USB_IO_PROTOCOL  *UsbIo
  )
{
  EFI_STATUS                    Status;
  EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;

  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);
  if (EFI_ERROR (Status)) {
    return FALSE;
  }

  // Check for specific device/RNDIS and CDC-DATA
  if (((InterfaceDescriptor.InterfaceClass == 0x2) &&
       (InterfaceDescriptor.InterfaceSubClass == 0x2) &&
       (InterfaceDescriptor.InterfaceProtocol == 0xFF)) || \
      ((InterfaceDescriptor.InterfaceClass == 0xEF) &&
       (InterfaceDescriptor.InterfaceSubClass == 0x4) &&
       (InterfaceDescriptor.InterfaceProtocol == 0x1))
      )
  {
    return TRUE;
  }

  return FALSE;
}

/**
  Check if the USB RNDIS and USB CDC Data interfaces are from the same device.

  @param[in]  UsbRndisDataPath  A pointer to the EFI_DEVICE_PATH_PROTOCOL instance.
  @param[in]  UsbCdcDataPath    A pointer to the EFI_DEVICE_PATH_PROTOCOL instance.

  @retval EFI_SUCCESS           Is the same device.
  @retval EFI_UNSUPPORTED       Is not the same device.

**/
EFI_STATUS
IsSameDevice (
  IN  EFI_DEVICE_PATH_PROTOCOL  *UsbRndisDataPath,
  IN  EFI_DEVICE_PATH_PROTOCOL  *UsbCdcDataPath
  )
{
  DEBUG ((DEBUG_VERBOSE, "IsSameDevice Entry \n"));
  while (1) {
    if (IsDevicePathEnd (NextDevicePathNode (UsbRndisDataPath))) {
      if (((USB_DEVICE_PATH *)UsbRndisDataPath)->ParentPortNumber ==
          ((USB_DEVICE_PATH *)UsbCdcDataPath)->ParentPortNumber)
      {
        return EFI_SUCCESS;
      } else {
        return EFI_UNSUPPORTED;
      }
    } else {
      if (CompareMem (UsbCdcDataPath, UsbRndisDataPath, sizeof (EFI_DEVICE_PATH_PROTOCOL)) != 0) {
        return EFI_UNSUPPORTED;
      }

      UsbRndisDataPath = NextDevicePathNode (UsbRndisDataPath);
      UsbCdcDataPath   = NextDevicePathNode (UsbCdcDataPath);
    }
  }

  DEBUG ((DEBUG_VERBOSE, "IsSameDevice Exit \n"));
}

/**
  Check if the USB CDC Data(UsbIo) installed and return USB CDC Data Handle.

  @param[in]  UsbIo  A pointer to the EFI_USB_IO_PROTOCOL instance.

  @retval TRUE              USB CDC Data(UsbIo) installed.
  @retval FALSE             USB CDC Data(UsbIo) did not installed.

**/
BOOLEAN
IsUsbCdcData (
  IN EFI_USB_IO_PROTOCOL  *UsbIo
  )
{
  EFI_STATUS                    Status;
  EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;

  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);
  if (EFI_ERROR (Status)) {
    return FALSE;
  }

  // Check for CDC-DATA
  if ((InterfaceDescriptor.InterfaceClass == 0xA) &&
      (InterfaceDescriptor.InterfaceSubClass == 0x0) &&
      (InterfaceDescriptor.InterfaceProtocol == 0x0))
  {
    return TRUE;
  }

  return FALSE;
}

/**
  Check if the USB Rndis(UsbIo) installed

  @param[in]  UsbIo        A pointer to the EFI_USB_IO_PROTOCOL instance.

  @retval TRUE              USB Rndis(UsbIo) installed.
  @retval FALSE             USB Rndis(UsbIo) did not installed.

**/
BOOLEAN
IsUsbRndis (
  IN EFI_USB_IO_PROTOCOL  *UsbIo
  )
{
  EFI_STATUS                    Status;
  EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;

  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);
  if (EFI_ERROR (Status)) {
    return FALSE;
  }

  // Check for Rndis
  if ((InterfaceDescriptor.InterfaceClass == 0x2) &&
      (InterfaceDescriptor.InterfaceSubClass == 0x2) &&
      (InterfaceDescriptor.InterfaceProtocol == 0xFF))
  {
    return TRUE;
  }

  return FALSE;
}

/**
  Control comes here when a CDC device is found.Check if a RNDIS interface is already found for this device or not.
  For one device two USBIO will be installed each for CDC and RNDIS interface.

  @param[in]  UsbCdcDataPath    A pointer to the EFI_DEVICE_PATH_PROTOCOL instance.
  @param[out] UsbRndisDevice    A pointer to the USB_RNDIS_DEVICE Data.

  @retval EFI_SUCCESS             The USB_RNDIS_DEVICE matching this CDC Data is found.
  @retval EFI_NOT_FOUND           The USB_RNDIS_DEVICE matching this CDC Data is not found.

**/
EFI_STATUS
UpdateRndisDevice (
  IN  EFI_DEVICE_PATH_PROTOCOL  *UsbCdcDataPath,
  OUT USB_RNDIS_DEVICE          **UsbRndisDevice
  )
{
  EFI_STATUS                   Status;
  UINTN                        Index;
  UINTN                        HandleCount;
  EFI_HANDLE                   *HandleBuffer;
  EDKII_USB_ETHERNET_PROTOCOL  *UsbEthDevice;
  EFI_DEVICE_PATH_PROTOCOL     *UsbRndisDataPath;
  EFI_USB_IO_PROTOCOL          *UsbIo;
  BOOLEAN                      IsRndisInterfaceFlag;

  IsRndisInterfaceFlag = FALSE;

  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEdkIIUsbEthProtocolGuid,
                  NULL,
                  &HandleCount,
                  &HandleBuffer
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  for (Index = 0; Index < HandleCount; Index++) {
    Status = gBS->HandleProtocol (
                    HandleBuffer[Index],
                    &gEdkIIUsbEthProtocolGuid,
                    (VOID **)&UsbEthDevice
                    );
    if (EFI_ERROR (Status)) {
      continue;
    }

    Status = gBS->HandleProtocol (
                    HandleBuffer[Index],
                    &gEfiUsbIoProtocolGuid,
                    (VOID **)&UsbIo
                    );
    if (EFI_ERROR (Status)) {
      continue;
    }

    IsRndisInterfaceFlag = IsRndisInterface (UsbIo);
    if (IsRndisInterfaceFlag == FALSE) {
      continue;
    }

    Status = gBS->HandleProtocol (
                    HandleBuffer[Index],
                    &gEfiDevicePathProtocolGuid,
                    (VOID **)&UsbRndisDataPath
                    );
    if (EFI_ERROR (Status)) {
      continue;
    }

    Status = IsSameDevice (UsbRndisDataPath, UsbCdcDataPath);

    DEBUG ((DEBUG_VERBOSE, "Rndis IsSameDevice %r\n", Status));

    if (!EFI_ERROR (Status)) {
      *UsbRndisDevice = USB_RNDIS_DEVICE_FROM_THIS (UsbEthDevice);
      FreePool (HandleBuffer);
      return EFI_SUCCESS;
    }
  }   // End of For loop

  FreePool (HandleBuffer);
  return EFI_NOT_FOUND;
}

/**

  For the given Rndis Device, find a matching CDC device already exists or not. If found update the handle
  and UsbIO protocol.

  @param[in]  UsbRndisDevice        A pointer to the USB_RNDIS_DEVICE data.

**/
VOID
FindMatchingCdcData (
  IN USB_RNDIS_DEVICE  *UsbRndisDevice
  )
{
  EFI_STATUS                Status;
  UINTN                     Index;
  UINTN                     HandleCount;
  EFI_HANDLE                *HandleBuffer;
  EFI_USB_IO_PROTOCOL       *UsbIo;
  EFI_DEVICE_PATH_PROTOCOL  *UsbRndisDataPath;
  EFI_DEVICE_PATH_PROTOCOL  *UsbCdcDataPath;

  // Find the parent RNDIS and update the UsbIo for the CDC device
  Status = gBS->HandleProtocol (
                  UsbRndisDevice->UsbRndisHandle,
                  &gEfiDevicePathProtocolGuid,
                  (VOID **)&UsbRndisDataPath
                  );

  if (EFI_ERROR (Status)) {
    return;
  }

  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEfiUsbIoProtocolGuid,
                  NULL,
                  &HandleCount,
                  &HandleBuffer
                  );
  if (EFI_ERROR (Status)) {
    return;
  }

  for (Index = 0; Index < HandleCount; Index++) {
    Status = gBS->HandleProtocol (
                    HandleBuffer[Index],
                    &gEfiUsbIoProtocolGuid,
                    (VOID **)&UsbIo
                    );
    ASSERT_EFI_ERROR (Status);

    if (IsUsbCdcData (UsbIo)) {
      DEBUG ((DEBUG_VERBOSE, "Rndis FindMatchingCdcData CDCData interface found\n"));

      Status = gBS->HandleProtocol (
                      HandleBuffer[Index],
                      &gEfiDevicePathProtocolGuid,
                      (VOID **)&UsbCdcDataPath
                      );
      if (EFI_ERROR (Status)) {
        DEBUG ((DEBUG_VERBOSE, "Rndis CDCData DevicePath not found\n"));
        FreePool (HandleBuffer);
        return;
      }

      Status = IsSameDevice (UsbRndisDataPath, UsbCdcDataPath);
      DEBUG ((DEBUG_VERBOSE, "Rndis IsSameDevice %r\n", Status));
      if (!EFI_ERROR (Status)) {
        UsbRndisDevice->UsbCdcDataHandle = HandleBuffer[Index];
        UsbRndisDevice->UsbIoCdcData     = UsbIo;
        GetEndpoint (UsbRndisDevice->UsbIoCdcData, UsbRndisDevice);
        FreePool (HandleBuffer);
        return;
      }
    }
  }   // End of For loop

  FreePool (HandleBuffer);
}

/**

  For the given UsbIo CdcData, find a matching RNDIS device already exists or not.

  @param[in]  CdcHandle       A pointer to the EFI_HANDLE for USB CDC Data.
  @param[out] CdcUsbIo        A pointer for retrieve the EFI_USB_IO_PROTOCOL instance.
  @param[out] RndisHandle     A pointer for retrieve the handle of RNDIS device.

  @retval EFI_SUCCESS             The USB_RNDIS_DEVICE matching this CDC Data is found.
  @retval EFI_NOT_FOUND           The USB_RNDIS_DEVICE matching this CDC Data is not found.

**/
EFI_STATUS
EFIAPI
FindMatchingRndisDev (
  IN  EFI_HANDLE           CdcHandle,
  OUT EFI_USB_IO_PROTOCOL  **CdcUsbIo,
  OUT EFI_HANDLE           *RndisHandle
  )
{
  EFI_STATUS                Status;
  UINTN                     Index;
  UINTN                     HandleCount;
  EFI_HANDLE                *HandleBuffer;
  EFI_USB_IO_PROTOCOL       *UsbIo;
  EFI_DEVICE_PATH_PROTOCOL  *UsbRndisDataPath;
  EFI_DEVICE_PATH_PROTOCOL  *UsbCdcDataPath;

  // Find the parent RNDIS and update the UsbIo for the CDC device
  Status = gBS->HandleProtocol (
                  CdcHandle,
                  &gEfiDevicePathProtocolGuid,
                  (VOID **)&UsbCdcDataPath
                  );

  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEfiUsbIoProtocolGuid,
                  NULL,
                  &HandleCount,
                  &HandleBuffer
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  for (Index = 0; Index < HandleCount; Index++) {
    Status = gBS->HandleProtocol (
                    HandleBuffer[Index],
                    &gEfiUsbIoProtocolGuid,
                    (VOID **)&UsbIo
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    if (IsUsbRndis (UsbIo)) {
      Status = gBS->HandleProtocol (
                      HandleBuffer[Index],
                      &gEfiDevicePathProtocolGuid,
                      (VOID **)&UsbRndisDataPath
                      );
      if (EFI_ERROR (Status)) {
        DEBUG ((DEBUG_ERROR, "Usb Rndis DevicePath not found\n"));
        break;
      }

      Status = IsSameDevice (UsbRndisDataPath, UsbCdcDataPath);

      if (!EFI_ERROR (Status)) {
        *RndisHandle = HandleBuffer[Index];
        *CdcUsbIo    = UsbIo;
        FreePool (HandleBuffer);
        return Status;
      }
    }
  }   // End of For loop

  FreePool (HandleBuffer);

  return EFI_NOT_FOUND;
}

/**
  USB Rndis Driver Binding Support.

  @param[in]  This                    Protocol instance pointer.
  @param[in]  ControllerHandle        Handle of device to test.
  @param[in]  RemainingDevicePath     Optional parameter use to pick a specific child
                                      device to start.

  @retval EFI_SUCCESS                 This driver supports this device.
  @retval EFI_ALREADY_STARTED         This driver is already running on this device.
  @retval other                       This driver does not support this device.

**/
EFI_STATUS
EFIAPI
UsbRndisDriverSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN EFI_HANDLE                   ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
  )
{
  EFI_STATUS           Status;
  EFI_USB_IO_PROTOCOL  *UsbIo;

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiUsbIoProtocolGuid,
                  (VOID **)&UsbIo,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = IsSupportedDevice (UsbIo) ? EFI_SUCCESS : EFI_UNSUPPORTED;

  gBS->CloseProtocol (
         ControllerHandle,
         &gEfiUsbIoProtocolGuid,
         This->DriverBindingHandle,
         ControllerHandle
         );
  return Status;
}

/**
  USB RNDIS Driver Binding Start.

  @param[in]  This                    Protocol instance pointer.
  @param[in]  ControllerHandle        Handle of device to bind driver to.
  @param[in]  RemainingDevicePath     Optional parameter use to pick a specific child
                                      device to start.

  @retval EFI_SUCCESS                 This driver is added to ControllerHandle
  @retval EFI_DEVICE_ERROR            This driver could not be started due to a device error
  @retval EFI_OUT_OF_RESOURCES        The driver could not install successfully due to a lack of resources.
  @retval other                       This driver does not support this device

**/
EFI_STATUS
EFIAPI
UsbRndisDriverStart (
  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN EFI_HANDLE                   ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
  )
{
  EFI_STATUS                    Status;
  USB_RNDIS_DEVICE              *UsbRndisDevice;
  EFI_DEVICE_PATH_PROTOCOL      *UsbEthPath;
  EFI_USB_IO_PROTOCOL           *UsbIo;
  EFI_USB_INTERFACE_DESCRIPTOR  Interface;
  EFI_HANDLE                    RndisHandle;

  RndisHandle = ControllerHandle;

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiUsbIoProtocolGuid,
                  (VOID **)&UsbIo,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiDevicePathProtocolGuid,
                  (VOID **)&UsbEthPath,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    gBS->CloseProtocol (
           ControllerHandle,
           &gEfiUsbIoProtocolGuid,
           This->DriverBindingHandle,
           ControllerHandle
           );
    return Status;
  }

  gBS->CloseProtocol (
         ControllerHandle,
         &gEfiDevicePathProtocolGuid,
         This->DriverBindingHandle,
         ControllerHandle
         );

  // Controls come here for RNDIS and CDC. If it is CDC, check whether RNDIS is present on the same controller or not.
  if (IsUsbCdcData (UsbIo)) {
    DEBUG ((DEBUG_INFO, "Rndis CDCData interface found\n"));

    // Find the parent RNDIS and update the UsbIo for the CDC device
    Status = UpdateRndisDevice (
               UsbEthPath,
               &UsbRndisDevice
               );

    if (!EFI_ERROR (Status)) {
      DEBUG ((DEBUG_INFO, "Rndis Matching interface found\n"));
      UsbRndisDevice->UsbCdcDataHandle = ControllerHandle;
      UsbRndisDevice->UsbIoCdcData     = UsbIo;
      GetEndpoint (UsbRndisDevice->UsbIoCdcData, UsbRndisDevice);
      return Status;
    } else {
      // Check if RnDis exist
      Status = FindMatchingRndisDev (
                 ControllerHandle,
                 &UsbIo,
                 &RndisHandle
                 );

      if (EFI_ERROR (Status)) {
        gBS->CloseProtocol (
               ControllerHandle,
               &gEfiUsbIoProtocolGuid,
               This->DriverBindingHandle,
               ControllerHandle
               );
        return Status;
      }
    }
  }

  UsbRndisDevice = AllocateZeroPool (sizeof (USB_RNDIS_DEVICE));

  if (!UsbRndisDevice) {
    DEBUG ((DEBUG_ERROR, "AllocateZeroPool Fail\n"));

    gBS->CloseProtocol (
           ControllerHandle,
           &gEfiUsbIoProtocolGuid,
           This->DriverBindingHandle,
           ControllerHandle
           );
    return EFI_OUT_OF_RESOURCES;
  }

  Status = LoadAllDescriptor (
             UsbIo,
             &UsbRndisDevice->Config
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:LoadAllDescriptor status = %r\n", __func__, Status));
    gBS->CloseProtocol (
           ControllerHandle,
           &gEfiUsbIoProtocolGuid,
           This->DriverBindingHandle,
           ControllerHandle
           );
    FreePool (UsbRndisDevice);
    return Status;
  }

  Status = UsbIo->UsbGetInterfaceDescriptor (
                    UsbIo,
                    &Interface
                    );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:UsbGetInterfaceDescriptor status = %r\n", __func__, Status));
    gBS->CloseProtocol (
           ControllerHandle,
           &gEfiUsbIoProtocolGuid,
           This->DriverBindingHandle,
           ControllerHandle
           );
    FreePool (UsbRndisDevice->Config);
    FreePool (UsbRndisDevice);
    return Status;
  }

  UsbRndisDevice->Signature                          = USB_RNDIS_SIGNATURE;
  UsbRndisDevice->NumOfInterface                     = Interface.InterfaceNumber;
  UsbRndisDevice->UsbRndisHandle                     = RndisHandle;
  UsbRndisDevice->UsbCdcDataHandle                   = 0;
  UsbRndisDevice->UsbIo                              = UsbIo;
  UsbRndisDevice->UsbEth.UsbEthReceive               = RndisUndiReceive;
  UsbRndisDevice->UsbEth.UsbEthTransmit              = RndisUndiTransmit;
  UsbRndisDevice->UsbEth.UsbEthInterrupt             = UsbRndisInterrupt;
  UsbRndisDevice->UsbEth.UsbEthMacAddress            = GetUsbEthMacAddress;
  UsbRndisDevice->UsbEth.UsbEthMaxBulkSize           = UsbEthBulkSize;
  UsbRndisDevice->UsbEth.UsbHeaderFunDescriptor      = GetUsbHeaderFunDescriptor;
  UsbRndisDevice->UsbEth.UsbUnionFunDescriptor       = GetUsbUnionFunDescriptor;
  UsbRndisDevice->UsbEth.UsbEthFunDescriptor         = GetUsbRndisFunDescriptor;
  UsbRndisDevice->UsbEth.SetUsbEthMcastFilter        = SetUsbRndisMcastFilter;
  UsbRndisDevice->UsbEth.SetUsbEthPowerPatternFilter = SetUsbRndisPowerFilter;
  UsbRndisDevice->UsbEth.GetUsbEthPowerPatternFilter = GetUsbRndisPowerFilter;
  UsbRndisDevice->UsbEth.SetUsbEthPacketFilter       = SetUsbRndisPacketFilter;
  UsbRndisDevice->UsbEth.GetUsbEthStatistic          = GetRndisStatistic;

  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiGetState        = RndisDummyReturn;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiStart           = RndisUndiStart;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiStop            = RndisUndiStop;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiGetInitInfo     = RndisUndiGetInitInfo;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiGetConfigInfo   = RndisUndiGetConfigInfo;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiInitialize      = RndisUndiInitialize;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiReset           = RndisUndiReset;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiShutdown        = RndisUndiShutdown;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiInterruptEnable = RndisDummyReturn;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiReceiveFilter   = RndisUndiReceiveFilter;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiStationAddress  = RndisDummyReturn;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiStatistics      = NULL;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiMcastIp2Mac     = RndisDummyReturn;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiNvData          = RndisDummyReturn;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiGetStatus       = RndisUndiGetStatus;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiFillHeader      = RndisDummyReturn;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiTransmit        = NULL;
  UsbRndisDevice->UsbEth.UsbEthUndi.UsbEthUndiReceive         = NULL;

  UsbRndisDevice->MaxTransferSize       = RNDIS_MAX_TRANSFER_SIZE;
  UsbRndisDevice->MaxPacketsPerTransfer = 1;
  UsbRndisDevice->PacketAlignmentFactor = 0;

  InitializeListHead (&UsbRndisDevice->ReceivePacketList);

  // This is a RNDIS interface. See whether CDC-DATA interface has already been connected or not
  FindMatchingCdcData (UsbRndisDevice);

  if (UsbRndisDevice->UsbIoCdcData) {
    Status = gBS->InstallProtocolInterface (
                    &ControllerHandle,
                    &gEdkIIUsbEthProtocolGuid,
                    EFI_NATIVE_INTERFACE,
                    &(UsbRndisDevice->UsbEth)
                    );
    if (EFI_ERROR (Status)) {
      gBS->CloseProtocol (
             ControllerHandle,
             &gEfiUsbIoProtocolGuid,
             This->DriverBindingHandle,
             ControllerHandle
             );

      FreePool (UsbRndisDevice->Config);
      FreePool (UsbRndisDevice);
      return Status;
    }

    GetEndpoint (UsbRndisDevice->UsbIo, UsbRndisDevice);

    DEBUG ((DEBUG_INFO, "Rndis DeviceHandle %r\n", UsbRndisDevice->UsbRndisHandle));
    DEBUG ((DEBUG_INFO, "CDC DeviceHandle %r\n", UsbRndisDevice->UsbCdcDataHandle));
    return EFI_SUCCESS;
  }

  FreePool (UsbRndisDevice->Config);
  FreePool (UsbRndisDevice);

  return EFI_SUCCESS;
}

/**
  CheckandStopRndisDevice

  @param[in]  This                 Protocol instance pointer.
  @param[in]  ControllerHandle     Handle of device to bind driver to.

  @retval EFI_SUCCESS          This driver is added to ControllerHandle
  @retval EFI_DEVICE_ERROR     This driver could not be started due to a device error
  @retval other                This driver does not support this device

**/
EFI_STATUS
EFIAPI
CheckandStopRndisDevice (
  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN  EFI_HANDLE                   ControllerHandle
  )
{
  EFI_STATUS           Status;
  EFI_USB_IO_PROTOCOL  *UsbIo;

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiUsbIoProtocolGuid,
                  (VOID **)&UsbIo,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (IsUsbRndis (UsbIo)) {
    Status = gBS->CloseProtocol (
                    ControllerHandle,
                    &gEfiUsbIoProtocolGuid,
                    This->DriverBindingHandle,
                    ControllerHandle
                    );
    DEBUG ((DEBUG_ERROR, "Rndis ControllerHandle Stop %r\n", Status));
    return Status;
  }

  return EFI_UNSUPPORTED;
}

/**
  USB Rndis Driver Binding Stop.

  @param[in]  This                  Protocol instance pointer.
  @param[in]  ControllerHandle      Handle of device to stop driver on
  @param[in]  NumberOfChildren      Number of Handles in ChildHandleBuffer. If number of
                                    children is zero stop the entire bus driver.
  @param[in]  ChildHandleBuffer     List of Child Handles to Stop.

  @retval EFI_SUCCESS               This driver is removed ControllerHandle
  @retval other                     This driver was not removed from this device

**/
EFI_STATUS
EFIAPI
UsbRndisDriverStop (
  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN  EFI_HANDLE                   ControllerHandle,
  IN  UINTN                        NumberOfChildren,
  IN  EFI_HANDLE                   *ChildHandleBuffer
  )
{
  EFI_STATUS                   Status;
  EDKII_USB_ETHERNET_PROTOCOL  *UsbEthProtocol;
  USB_RNDIS_DEVICE             *UsbRndisDevice;

  DEBUG ((DEBUG_INFO, "UsbRndisDriverStop ControllerHandle %lx\n", ControllerHandle));

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEdkIIUsbEthProtocolGuid,
                  (VOID **)&UsbEthProtocol,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    Status = CheckandStopRndisDevice (This, ControllerHandle);
    return Status;
  }

  UsbRndisDevice = USB_RNDIS_DEVICE_FROM_THIS (UsbEthProtocol);

  Status = gBS->CloseProtocol (
                  UsbRndisDevice->UsbCdcDataHandle,
                  &gEfiUsbIoProtocolGuid,
                  This->DriverBindingHandle,
                  UsbRndisDevice->UsbCdcDataHandle
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:CloseProtocol status = %r\n", __func__, Status));
  }

  Status = gBS->UninstallProtocolInterface (
                  ControllerHandle,
                  &gEdkIIUsbEthProtocolGuid,
                  UsbEthProtocol
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->CloseProtocol (
                  ControllerHandle,
                  &gEfiUsbIoProtocolGuid,
                  This->DriverBindingHandle,
                  ControllerHandle
                  );

  FreePool (UsbRndisDevice->Config);
  FreePool (UsbRndisDevice);

  DEBUG ((DEBUG_INFO, "UsbRndisDriverStop %r\n", Status));
  return Status;
}

/**
  Entrypoint of RNDIS Driver.

  This function is the entrypoint of RNDIS Driver. It installs Driver Binding
  Protocols together with Component Name Protocols.

  @param[in]  ImageHandle       The firmware allocated handle for the EFI image.
  @param[in]  SystemTable       A pointer to the EFI System Table.

  @retval EFI_SUCCESS           The entry point is executed successfully.

**/
EFI_STATUS
EFIAPI
UsbRndisEntry (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  gUsbRndisDriverBinding.DriverBindingHandle = ImageHandle;
  gUsbRndisDriverBinding.ImageHandle         = ImageHandle;

  return gBS->InstallMultipleProtocolInterfaces (
                &gUsbRndisDriverBinding.DriverBindingHandle,
                &gEfiDriverBindingProtocolGuid,
                &gUsbRndisDriverBinding,
                &gEfiComponentName2ProtocolGuid,
                &gUsbRndisComponentName2,
                NULL
                );
}