musb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. musb.h
  5. Abstract:
  6. This header contains definitions for the Mentor Graphics USB controller
  7. driver support.
  8. Author:
  9. Evan Green 11-Sep-2015
  10. --*/
  11. //
  12. // ------------------------------------------------------------------- Includes
  13. //
  14. #include "musbhw.h"
  15. #include "cppi.h"
  16. //
  17. // --------------------------------------------------------------------- Macros
  18. //
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #define MUSB_ALLOCATION_TAG 0x6273554D
  23. //
  24. // Define (software) USB transfer flags.
  25. //
  26. #define MUSB_TRANSFER_OUT 0x0001
  27. #define MUSB_TRANSFER_SETUP 0x0002
  28. #define MUSB_TRANSFER_STATUS 0x0004
  29. #define MUSB_TRANSFER_DMA 0x0008
  30. //
  31. // ------------------------------------------------------ Data Type Definitions
  32. //
  33. typedef enum _MUSB_ENDPOINT_DIRECTION {
  34. MusbEndpointInvalid,
  35. MusbEndpointTx,
  36. MusbEndpointRx,
  37. MusbEndpointTxRx
  38. } MUSB_ENDPOINT_DIRECTION, *PMUSB_ENDPOINT_DIRECTION;
  39. /*++
  40. Structure Description:
  41. This structure stores context for a MUSB software endpoint (not to be
  42. confused with the hardware endpoints which are more like channels in host
  43. mode).
  44. Members:
  45. HardwareIndex - Stores the index of the allocated hardware endpoint, or
  46. -1 if the endpoint is currently not assigned.
  47. Device - Stores the device ID, which always starts out as zero.
  48. EndpointNumber - Stores the USB endpoint number for this endpoint.
  49. Type - Stores the value to plunk in the type register.
  50. Interval - Stores the value to put in the TX/RX interval register.
  51. HubAddress - Stores the hub address this device is connected to if it is
  52. a full or low-speed device.
  53. HubPort - Stores the port on the hub this device is connected to if it is
  54. a full or low-speed device.
  55. Control - Stores the value to put in the control register.
  56. MaxPayload - Stores the value to put in the max payload register.
  57. Direction - Stores the endpoint direction.
  58. InFlight - Stores the count of transfer sets currently in flight.
  59. --*/
  60. typedef struct _MUSB_SOFT_ENDPOINT {
  61. UCHAR HardwareIndex;
  62. UCHAR Device;
  63. UCHAR EndpointNumber;
  64. UCHAR Type;
  65. UCHAR Interval;
  66. UCHAR HubAddress;
  67. UCHAR HubPort;
  68. USHORT Control;
  69. USHORT MaxPayload;
  70. USB_TRANSFER_DIRECTION Direction;
  71. ULONG InFlight;
  72. } MUSB_SOFT_ENDPOINT, *PMUSB_SOFT_ENDPOINT;
  73. /*++
  74. Structure Description:
  75. This structure stores context for a MUSB hardware endpoint.
  76. Members:
  77. CurrentEndpoint - Stores a pointer to the soft endpoint this channel is
  78. currently configured to.
  79. TransferList - Stores the head of the list of transfers to execute on this
  80. endpoint.
  81. TxFifoSize - Stores the transmit FIFO size for this endpoint.
  82. RxFifoSize - Stores the receive FIFO size for this endpoint.
  83. --*/
  84. typedef struct _MUSB_HARD_ENDPOINT {
  85. PMUSB_SOFT_ENDPOINT CurrentEndpoint;
  86. LIST_ENTRY TransferList;
  87. USHORT TxFifoSize;
  88. USHORT RxFifoSize;
  89. } MUSB_HARD_ENDPOINT, *PMUSB_HARD_ENDPOINT;
  90. /*++
  91. Structure Description:
  92. This structure stores controller information for a Mentor Graphics USB OTG
  93. host/device controller.
  94. Members:
  95. InterruptLine - Stores the interrupt line that this controller's interrupt
  96. comes in on.
  97. InterruptVector - Stores the interrupt vector that this controller's
  98. interrupt comes in on.
  99. InterruptHandle - Stores a pointer to the handle received when the
  100. interrupt was connected.
  101. ControllerBase - Stores the virtual address of the hardware registers.
  102. Driver - Stores a pointer to the controller driver.
  103. PhysicalBase - Stores the physical address of the controller.
  104. Endpoints - Stores the array of hardware endpoint state.
  105. EndpointCount - Stores the number of endpoints present in this controller
  106. instance.
  107. CurrentIndex - Stores the current value programmed into the index register.
  108. NextEndpointAssignment - Stores the index to start the next search for an
  109. appropriate hardware endpoint for a software endpoint.
  110. UsbInterruptEnable - Stores the mask of enabled USB interrupts.
  111. Instance - Stores the instance number of this controller, which is passed
  112. in to the potentially common DMA controller.
  113. Lock - Stores the spin lock used to serialize access to the device.
  114. OldRunLevel - Stores the runlevel to return to when the lock is released.
  115. This value must be read before the lock is actually released, and set
  116. only after the lock is acquired.
  117. TxInterruptEnable - Stores the TX interrupt enable register value.
  118. RxInterruptEnable - Stores the RX interrupt enable register value.
  119. PendingUsbInterrupts - Stores the mask of USB interrupts pending.
  120. PendingEndpointInterrupts - Stores the mask of endpoint interrupts pending,
  121. with the RX interrupts in the upper 16 bits and the TX interrupts in
  122. the lower 16 bits.
  123. Connect - Stores a boolean indicating whether a device is currently
  124. connected.
  125. UsbCoreHandle - Stores the handle to the USB core representing this
  126. controller.
  127. CppiDma - Stores an optional pointer to the CPPI DMA controller to use for
  128. DMA.
  129. --*/
  130. typedef struct _MUSB_CONTROLLER {
  131. ULONGLONG InterruptLine;
  132. ULONGLONG InterruptVector;
  133. HANDLE InterruptHandle;
  134. PVOID ControllerBase;
  135. PDRIVER Driver;
  136. PHYSICAL_ADDRESS PhysicalBase;
  137. MUSB_HARD_ENDPOINT Endpoints[MUSB_MAX_ENDPOINTS];
  138. UCHAR EndpointCount;
  139. UCHAR CurrentIndex;
  140. UCHAR NextEndpointAssignment;
  141. UCHAR UsbInterruptEnable;
  142. UCHAR Instance;
  143. KSPIN_LOCK Lock;
  144. RUNLEVEL OldRunLevel;
  145. USHORT TxInterruptEnable;
  146. USHORT RxInterruptEnable;
  147. ULONG PendingUsbInterrupts;
  148. ULONG PendingEndpointInterrupts;
  149. BOOL Connected;
  150. HANDLE UsbCoreHandle;
  151. PCPPI_DMA_CONTROLLER CppiDma;
  152. } MUSB_CONTROLLER, *PMUSB_CONTROLLER;
  153. /*++
  154. Structure Description:
  155. This structure defines the FIFO configuration for a hardware endpoint.
  156. Members:
  157. Endpoint - Stores the hardware endpoint index.
  158. Direction - Stores the endpoint direction, of type MUSB_ENDPOINT_DIRECTION.
  159. MaxPacketSize - Stores the max packet size for the endpoint.
  160. --*/
  161. typedef struct _MUSB_FIFO_CONFIGURATION {
  162. UCHAR Endpoint;
  163. UCHAR Direction;
  164. USHORT MaxPacketSize;
  165. } MUSB_FIFO_CONFIGURATION, *PMUSB_FIFO_CONFIGURATION;
  166. /*++
  167. Structure Description:
  168. This structure stores the context for an individual packet going out on the
  169. USB bus via MUSB.
  170. Members:
  171. Size - Stores the size of the packet in bytes.
  172. Flags - Stores a bitfield of flags. See MUSB_TRANSFER_* definitions.
  173. BufferVirtual - Stores the virtual address of the buffer.
  174. BufferPhysical - Stores the physical address of the buffer.
  175. DmaData - Stores the DMA information for this transfer.
  176. --*/
  177. typedef struct _MUSB_TRANSFER {
  178. USHORT Size;
  179. USHORT Flags;
  180. PUCHAR BufferVirtual;
  181. ULONG BufferPhysical;
  182. CPPI_DESCRIPTOR_DATA DmaData;
  183. } MUSB_TRANSFER, *PMUSB_TRANSFER;
  184. /*++
  185. Structure Description:
  186. This structure stores the context for a complete USB transfer in the MUSB
  187. controller.
  188. Members:
  189. ListEntry - Stores pointers to the next and previous transfer sets in the
  190. queue for the hardware endpoint.
  191. Count - Stores the number of transfers configured in the set currently.
  192. MaxCount - Stores the maximum number of transfers that can be configured
  193. for this set.
  194. CurrentIndex - Stores the index of the transfer that is currently executing
  195. or should be executed next.
  196. SoftEndpoint - Stores a pointer to the endpoint this transfer is queued on.
  197. UsbTransfer - Stores a pointer to the USB transfer associated with this
  198. transfer set.
  199. Transfers - Stores a pointer to the array of transfers.
  200. --*/
  201. typedef struct _MUSB_TRANSFER_SET {
  202. LIST_ENTRY ListEntry;
  203. USHORT Count;
  204. USHORT MaxCount;
  205. USHORT CurrentIndex;
  206. PMUSB_SOFT_ENDPOINT SoftEndpoint;
  207. PUSB_TRANSFER_INTERNAL UsbTransfer;
  208. PMUSB_TRANSFER Transfers;
  209. } MUSB_TRANSFER_SET, *PMUSB_TRANSFER_SET;
  210. //
  211. // -------------------------------------------------------------------- Globals
  212. //
  213. //
  214. // -------------------------------------------------------- Function Prototypes
  215. //
  216. KSTATUS
  217. MusbInitializeControllerState (
  218. PMUSB_CONTROLLER Controller,
  219. PVOID RegisterBase,
  220. PDRIVER Driver,
  221. PHYSICAL_ADDRESS PhysicalBase,
  222. PCPPI_DMA_CONTROLLER DmaController,
  223. UCHAR Instance
  224. );
  225. /*++
  226. Routine Description:
  227. This routine initializes data structures for the Mentor USB controller.
  228. It's assumed the controller structure has already been properly zeroed.
  229. Arguments:
  230. Controller - Supplies a pointer to the controller structure, which has
  231. already been zeroed.
  232. RegisterBase - Supplies the virtual address of the registers for the
  233. device.
  234. Driver - Supplies a pointer to the driver that owns this device.
  235. PhysicalBase - Supplies the physical address of the controller.
  236. DmaController - Supplies an optional pointer to the DMA controller to use.
  237. Instance - Supplies the instance ID to pass into the potentially shared
  238. DMA controller.
  239. Return Value:
  240. Status code.
  241. --*/
  242. KSTATUS
  243. MusbDestroyControllerState (
  244. PMUSB_CONTROLLER Controller
  245. );
  246. /*++
  247. Routine Description:
  248. This routine destroys the given Mentor USB controller structure, freeing
  249. all resources associated with the controller except the controller
  250. structure itself and the register base, which were passed in on initialize.
  251. Arguments:
  252. Controller - Supplies a pointer to the controller to destroy.
  253. Return Value:
  254. Status code.
  255. --*/
  256. KSTATUS
  257. MusbResetController (
  258. PMUSB_CONTROLLER Controller
  259. );
  260. /*++
  261. Routine Description:
  262. This routine resets and reinitializes the given controller.
  263. Arguments:
  264. Controller - Supplies a pointer to the controller.
  265. Return Value:
  266. Status code.
  267. --*/
  268. KSTATUS
  269. MusbRegisterController (
  270. PMUSB_CONTROLLER Controller,
  271. PDEVICE Device
  272. );
  273. /*++
  274. Routine Description:
  275. This routine registers the started Mentor USB controller with the core USB
  276. library.
  277. Arguments:
  278. Controller - Supplies a pointer to the state of the controller to register.
  279. Device - Supplies a pointer to the device object.
  280. Return Value:
  281. Status code.
  282. --*/
  283. INTERRUPT_STATUS
  284. MusbInterruptService (
  285. PVOID Context
  286. );
  287. /*++
  288. Routine Description:
  289. This routine implements the MUSB interrupt service routine.
  290. Arguments:
  291. Context - Supplies the context pointer given to the system when the
  292. interrupt was connected. In this case, this points to the controller.
  293. Return Value:
  294. Interrupt status.
  295. --*/
  296. INTERRUPT_STATUS
  297. MusbInterruptServiceDpc (
  298. PVOID Parameter
  299. );
  300. /*++
  301. Routine Description:
  302. This routine implements the MUSB dispatch level interrupt service.
  303. Arguments:
  304. Parameter - Supplies the context, in this case the controller structure.
  305. Return Value:
  306. None.
  307. --*/