USBHostMode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * USB Host Mode management functions and variables. This file contains the LUFA code required to
  29. * manage the USB RNDIS host mode.
  30. */
  31. #include "USBHostMode.h"
  32. /** LUFA RNDIS Class driver interface configuration and state information. This structure is
  33. * passed to all RNDIS Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface_Host =
  37. {
  38. .Config =
  39. {
  40. .DataINPipe =
  41. {
  42. .Address = (PIPE_DIR_IN | 1),
  43. .Banks = 1,
  44. },
  45. .DataOUTPipe =
  46. {
  47. .Address = (PIPE_DIR_OUT | 2),
  48. .Banks = 1,
  49. },
  50. .NotificationPipe =
  51. {
  52. .Address = (PIPE_DIR_IN | 3),
  53. .Banks = 1,
  54. },
  55. .HostMaxPacketSize = UIP_CONF_BUFFER_SIZE,
  56. },
  57. };
  58. /** USB host mode management task. This function manages the RNDIS Host class driver and uIP stack when the device is
  59. * initialized in USB host mode.
  60. */
  61. void USBHostMode_USBTask(void)
  62. {
  63. if (USB_CurrentMode != USB_MODE_Host)
  64. return;
  65. uIPManagement_ManageNetwork();
  66. RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface_Host);
  67. }
  68. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  69. * starts the library USB task to begin the enumeration and USB management process.
  70. */
  71. void EVENT_USB_Host_DeviceAttached(void)
  72. {
  73. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  74. }
  75. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  76. * stops the library USB task management process.
  77. */
  78. void EVENT_USB_Host_DeviceUnattached(void)
  79. {
  80. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  81. }
  82. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  83. * enumerated by the host and is now ready to be used by the application.
  84. */
  85. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  86. {
  87. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  88. uint16_t ConfigDescriptorSize;
  89. uint8_t ConfigDescriptorData[512];
  90. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  91. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  92. {
  93. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  94. return;
  95. }
  96. if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface_Host,
  97. ConfigDescriptorSize, ConfigDescriptorData) != RNDIS_ENUMERROR_NoError)
  98. {
  99. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  100. return;
  101. }
  102. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  103. {
  104. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  105. return;
  106. }
  107. if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface_Host) != HOST_SENDCONTROL_Successful)
  108. {
  109. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  110. USB_Host_SetDeviceConfiguration(0);
  111. return;
  112. }
  113. uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST);
  114. if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface_Host, OID_GEN_CURRENT_PACKET_FILTER,
  115. &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)
  116. {
  117. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  118. USB_Host_SetDeviceConfiguration(0);
  119. return;
  120. }
  121. if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface_Host, OID_802_3_CURRENT_ADDRESS,
  122. &MACAddress, sizeof(MACAddress)) != HOST_SENDCONTROL_Successful)
  123. {
  124. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  125. USB_Host_SetDeviceConfiguration(0);
  126. return;
  127. }
  128. /* Initialize uIP stack */
  129. uIPManagement_Init();
  130. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  131. }
  132. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  133. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  134. {
  135. USB_Disable();
  136. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  137. for(;;);
  138. }
  139. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  140. * enumerating an attached USB device.
  141. */
  142. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
  143. {
  144. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  145. }