usbhost.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //*****************************************************************************
  2. //
  3. // usbhost.h - Host specific definitions for the USB host library.
  4. //
  5. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 6288 of the Stellaris USB Library.
  22. //
  23. //*****************************************************************************
  24. #ifndef __USBHOST_H__
  25. #define __USBHOST_H__
  26. //*****************************************************************************
  27. //
  28. // If building with a C++ compiler, make all of the definitions in this header
  29. // have a C binding.
  30. //
  31. //*****************************************************************************
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. //*****************************************************************************
  37. //
  38. //! \addtogroup usblib_hcd
  39. //! @{
  40. //
  41. //*****************************************************************************
  42. //*****************************************************************************
  43. //
  44. // This is the type used to identify what the pipe is currently in use for.
  45. //
  46. //*****************************************************************************
  47. #define USBHCD_PIPE_UNUSED 0x00100000
  48. #define USBHCD_PIPE_CONTROL 0x00130000
  49. #define USBHCD_PIPE_BULK_OUT 0x00210000
  50. #define USBHCD_PIPE_BULK_IN 0x00220000
  51. #define USBHCD_PIPE_INTR_OUT 0x00410000
  52. #define USBHCD_PIPE_INTR_IN 0x00420000
  53. #define USBHCD_PIPE_ISOC_OUT 0x00810000
  54. #define USBHCD_PIPE_ISOC_IN 0x00820000
  55. #define USBHCD_PIPE_ISOC_OUT_DMA 0x01810000
  56. #define USBHCD_PIPE_ISOC_IN_DMA 0x01820000
  57. #define USBHCD_PIPE_BULK_OUT_DMA 0x01210000
  58. #define USBHCD_PIPE_BULK_IN_DMA 0x01220000
  59. //*****************************************************************************
  60. //
  61. // These are the defines that are used with USBHCDPowerConfigInit().
  62. //
  63. //*****************************************************************************
  64. #define USBHCD_FAULT_LOW 0x00000010
  65. #define USBHCD_FAULT_HIGH 0x00000030
  66. #define USBHCD_FAULT_VBUS_NONE 0x00000000
  67. #define USBHCD_FAULT_VBUS_TRI 0x00000140
  68. #define USBHCD_FAULT_VBUS_DIS 0x00000400
  69. #define USBHCD_VBUS_MANUAL 0x00000004
  70. #define USBHCD_VBUS_AUTO_LOW 0x00000002
  71. #define USBHCD_VBUS_AUTO_HIGH 0x00000003
  72. #define USBHCD_VBUS_FILTER 0x00010000
  73. //*****************************************************************************
  74. //
  75. // This is a fixed number as it relates to the maximum number of USB pipes
  76. // available on any USB controller. The actual number on a given device may
  77. // be less than this number.
  78. //
  79. //*****************************************************************************
  80. #define USBHCD_DEV_RECOVER_RETRY 3
  81. //*****************************************************************************
  82. //
  83. // This is a fixed number as it relates to the maximum number of USB pipes
  84. // available on any USB controller. The actual number on a given device may
  85. // be less than this number.
  86. //
  87. //*****************************************************************************
  88. #define MAX_NUM_PIPES 15
  89. //*****************************************************************************
  90. //
  91. // This is a fixed number as it relates to the number of USB pipes available
  92. // in the USB controller.
  93. //
  94. //*****************************************************************************
  95. #define MAX_NUM_DMA_CHANNELS 6
  96. //*****************************************************************************
  97. //
  98. // Marker for an unused DMA channel slot.
  99. //
  100. //*****************************************************************************
  101. #define USBHCD_DMA_UNUSED 0xff
  102. //*****************************************************************************
  103. //
  104. // This structure holds the timeOut values for different communications.
  105. //
  106. //*****************************************************************************
  107. #define USB_EP0_TIMEOUT_MILLISECS 7000
  108. #define USB_NONEP0_TIMEOUT_MILLISECS 7000
  109. #define USB_TIMEOUT_DISABLE -1
  110. typedef struct
  111. {
  112. signed int slEP0;
  113. signed int slNonEP0;
  114. }
  115. tUSBHTimeOutType;
  116. typedef struct
  117. {
  118. tUSBHTimeOutType Value;
  119. tUSBHTimeOutType Status;
  120. }
  121. tUSBHTimeOut;
  122. //*****************************************************************************
  123. //
  124. //! This macro is used to declare an instance of an Event driver for the USB
  125. //! library.
  126. //!
  127. //! \param VarName is the name of the variable.
  128. //! \param pfnOpen is the callback for the Open call to this driver. This
  129. //! value is currently reserved and should be set to 0.
  130. //! \param pfnClose is the callback for the Close call to this driver. This
  131. //! value is currently reserved and should be set to 0.
  132. //! \param pfnEvent is the callback that will be called for various USB events.
  133. //!
  134. //! The first parameter is the actual name of the variable that will
  135. //! be declared by this macro. The second and third parameter are reserved
  136. //! for future functionality and are unused and should be set to zero. The
  137. //! last parameter is the actual callback function and is specified as
  138. //! a function pointer of the type:
  139. //!
  140. //! void (*pfnEvent)(void *pvData);
  141. //!
  142. //! When the \e pfnEvent function is called the void pointer that is passed in
  143. //! as a parameter should be cast to a pointer to a structure of type
  144. //! tEventInfo. This will contain the event that caused the pfnEvent function
  145. //! to be called.
  146. //
  147. //*****************************************************************************
  148. #define DECLARE_EVENT_DRIVER(VarName, pfnOpen, pfnClose, pfnEvent) \
  149. void IntFn(void *pvData); \
  150. const tUSBHostClassDriver VarName = \
  151. { \
  152. USB_CLASS_EVENTS, \
  153. 0, \
  154. 0, \
  155. pfnEvent \
  156. }
  157. //*****************************************************************************
  158. //
  159. //! This is the structure that holds all of the information for devices
  160. //! that are enumerated in the system. It is passed in to Open function of
  161. //! USB host class drivers so that they can allocate any endpoints and parse
  162. //! out other information that the device class needs to complete enumeration.
  163. //
  164. //*****************************************************************************
  165. typedef struct
  166. {
  167. //
  168. //! The current device address for this device.
  169. //
  170. unsigned int ulAddress;
  171. //
  172. //! The current interface for this device.
  173. //
  174. unsigned int ulInterface;
  175. //
  176. //! A pointer to the device descriptor for this device.
  177. //
  178. tDeviceDescriptor DeviceDescriptor;
  179. //
  180. //! A pointer to the configuration descriptor for this device.
  181. //
  182. tConfigDescriptor *pConfigDescriptor;
  183. //
  184. //! The size of the buffer allocated to pConfigDescriptor.
  185. //
  186. unsigned int ulConfigDescriptorSize;
  187. //
  188. //! Operating speed of the device
  189. //
  190. unsigned int ulDeviceSpeed;
  191. }
  192. tUSBHostDevice;
  193. //*****************************************************************************
  194. //
  195. //! This structure defines a USB host class driver interface, it is parsed to
  196. //! find a USB class driver once a USB device is enumerated.
  197. //
  198. //*****************************************************************************
  199. typedef struct
  200. {
  201. //
  202. //! The interface class that this device class driver supports.
  203. //
  204. unsigned int ulInterfaceClass;
  205. //
  206. //! The function is called when this class of device has been detected.
  207. //
  208. void * (*pfnOpen)(tUSBHostDevice *pDevice, unsigned int instNum);
  209. //
  210. //! The function is called when the device, originally opened with a call
  211. //! to the pfnOpen function, is disconnected.
  212. //
  213. void (*pfnClose)(void *pvInstance);
  214. //
  215. //! This is the optional interrupt handler that will be called when an
  216. //! endpoint associated with this device instance generates an interrupt.
  217. //
  218. void (*pfnIntHandler)(void *pvInstance);
  219. }
  220. tUSBHostClassDriver;
  221. //*****************************************************************************
  222. //
  223. //! This structure is used to return generic event based information to an
  224. //! application. The following events are currently supported:
  225. //! USB_EVENT_CONNECTED, USB_EVENT_DISCONNECTED, and USB_EVENT_POWER_FAULT.
  226. //
  227. //*****************************************************************************
  228. typedef struct
  229. {
  230. unsigned int ulEvent;
  231. unsigned int ulInstance;
  232. }
  233. tEventInfo;
  234. //*****************************************************************************
  235. //
  236. // This is the type definition a call back for events on USB Pipes allocated
  237. // by USBHCDPipeAlloc().
  238. //
  239. // \param ulPipe is well the pipe
  240. // \param ulEvent is well the event
  241. //
  242. // inter def thand may need more text in order to be recogized what should
  243. // this really say about ourselves.
  244. //
  245. // \return None.
  246. //
  247. //*****************************************************************************
  248. typedef void (* tHCDPipeCallback)(unsigned int ulIndex, unsigned int ulPipe,
  249. unsigned int ulEvent);
  250. //*****************************************************************************
  251. //
  252. // Close the Doxygen group.
  253. //! @}
  254. //
  255. //*****************************************************************************
  256. //*****************************************************************************
  257. //
  258. // If the g_USBEventDriver is included in the host controller driver list then
  259. // this function must be provided by the application.
  260. //
  261. //*****************************************************************************
  262. void USBHCDEvents(void *pvData);
  263. void USBMOUSEHCDEvents(void *pvData);
  264. //*****************************************************************************
  265. //
  266. // Prototypes for the USB Host controller APIs.
  267. //
  268. //*****************************************************************************
  269. extern void USBHCDMain(unsigned int ulIndex, unsigned int ulInstance);
  270. extern void USBHCDInit(unsigned int ulIndex, void *pData,
  271. unsigned int ulSize);
  272. extern void USBHCDPowerConfigInit(unsigned int ulIndex,
  273. unsigned int ulFlags);
  274. extern unsigned int USBHCDPowerConfigGet(unsigned int ulIndex);
  275. extern unsigned int USBHCDPowerConfigSet(unsigned int ulIndex,
  276. unsigned int ulConfig);
  277. extern unsigned int USBHCDPowerAutomatic(unsigned int ulIndex);
  278. extern void
  279. USBHCDRegisterDrivers(unsigned int ulIndex,
  280. const tUSBHostClassDriver * const *ppHClassDrvrs,
  281. unsigned int ulNumDrivers);
  282. extern void USBHCDTerm(unsigned int ulIndex);
  283. extern void USBHCDSetConfig(unsigned int ulIndex, unsigned int ulDevice,
  284. unsigned int ulConfiguration);
  285. extern void USBHCDSetInterface(unsigned int ulIndex, unsigned int ulDevice,
  286. unsigned int ulInterface,
  287. unsigned ulAltSetting);
  288. extern unsigned int USBHCDGetSpeed(unsigned int ulIndex);
  289. extern void USBHCDSuspend(unsigned int ulIndex);
  290. extern void USBHCDResume(unsigned int ulIndex);
  291. extern void USBHCDReset(unsigned int ulIndex);
  292. extern void USBHCDPipeFree(unsigned int ulIndex, unsigned int ulPipe);
  293. extern unsigned int USBHCDPipeAlloc(unsigned int ulIndex,
  294. unsigned int ulEndpointType,
  295. unsigned int ulDevAddr,
  296. tHCDPipeCallback pCallback);
  297. extern unsigned int USBHCDPipeAllocSize(unsigned int ulIndex,
  298. unsigned int ulEndpointType,
  299. unsigned int ulDevAddr,
  300. unsigned int ulFIFOSize,
  301. tHCDPipeCallback pCallback);
  302. extern unsigned int USBHCDPipeConfig(unsigned int ulIndex,
  303. unsigned int ulPipe,
  304. unsigned int ulMaxPayload,
  305. unsigned int ulInterval,
  306. unsigned int ulTargetEndpoint);
  307. extern unsigned int USBHCDPipeStatus(unsigned int ulPipe);
  308. extern unsigned int USBHCDPipeWrite(unsigned int ulIndex,
  309. unsigned int ulPipe,
  310. unsigned char *pData,
  311. unsigned int ulSize);
  312. extern unsigned int USBHCDPipeRead(unsigned int ulIndex,
  313. unsigned int ulPipe,
  314. unsigned char *pData,
  315. unsigned int ulSize);
  316. extern unsigned int USBHCDPipeSchedule(unsigned int ulIndex,
  317. unsigned int ulPipe,
  318. unsigned char *pucData,
  319. unsigned int ulSize);
  320. extern unsigned int USBHCDPipeReadNonBlocking(unsigned int ulIndex,
  321. unsigned int ulPipe,
  322. unsigned char *pucData,
  323. unsigned int ulSize);
  324. extern unsigned int USBHCDControlTransfer(unsigned int ulIndex,
  325. tUSBRequest *pSetupPacket,
  326. unsigned int ulAddress,
  327. unsigned char *pData,
  328. unsigned int ulSize,
  329. unsigned int ulMaxPacketSize);
  330. extern void USB0HostIntHandler(void);
  331. extern void USB1HostIntHandler(void);
  332. extern void USBHCDTimeOutHook(unsigned int ulIndex,
  333. tUSBHTimeOut **USBHTimeOut);
  334. //*****************************************************************************
  335. //
  336. // The host class drivers supported by the USB library.
  337. //
  338. //*****************************************************************************
  339. extern const tUSBHostClassDriver g_USBHostMSCClassDriver;
  340. extern const tUSBHostClassDriver g_USBHIDClassDriver;
  341. extern const tUSBHostClassDriver g_USBHostAudioClassDriver;
  342. //*****************************************************************************
  343. //
  344. // This is the structure used to hold the information for a given USB pipe
  345. // that is attached to a device.
  346. //
  347. //*****************************************************************************
  348. typedef struct
  349. {
  350. //
  351. // The current address for this pipe.
  352. //
  353. unsigned int ulDevAddr;
  354. //
  355. // The current address for this pipe.
  356. //
  357. unsigned char ucEPNumber;
  358. //
  359. // The DMA channel assigned to this endpoint.
  360. //
  361. unsigned char ucDMAChannel;
  362. //
  363. // The current type for this pipe.
  364. //
  365. unsigned int ulType;
  366. //
  367. // The millisecond interval for this pipe.
  368. //
  369. unsigned int ulInterval;
  370. //
  371. // The next tick value to trigger and event on this pipe.
  372. //
  373. unsigned int ulNextEventTick;
  374. //
  375. // The current call back for this pipe.
  376. //
  377. tHCDPipeCallback pfnCallback;
  378. //
  379. // The state of a given USB pipe.
  380. //
  381. volatile enum
  382. {
  383. PIPE_READING,
  384. PIPE_DATA_READY,
  385. PIPE_DATA_SENT,
  386. PIPE_WRITING,
  387. PIPE_STALLED,
  388. PIPE_ERROR,
  389. PIPE_IDLE,
  390. PIPE_DISABLED
  391. }
  392. eState;
  393. //
  394. // The actual FIFO offset allocated to this endpoint.
  395. //
  396. unsigned short usFIFOAddr;
  397. //
  398. // The size of the FIFO entry based on the size parameter. These are
  399. // equivalent to the USB_FIFO_SZ_* values in usb.h.
  400. //
  401. unsigned char ucFIFOSize;
  402. //
  403. // The bit offset in the allocation structure.
  404. //
  405. unsigned char ucFIFOBitOffset;
  406. //
  407. // Speed of the pipe.
  408. //
  409. unsigned int ulPipeSpeed;
  410. //
  411. // Speed of the pipe.
  412. //
  413. unsigned int ulEpMaxPacketSize;
  414. }
  415. tUSBHCDPipe;
  416. //*****************************************************************************
  417. //
  418. // The internal state of the device.
  419. //
  420. //*****************************************************************************
  421. typedef enum
  422. {
  423. HCD_DEV_DISCONNECTED,
  424. HCD_DEV_CONNECTED,
  425. HCD_DEV_REQUEST,
  426. HCD_DEV_RESET,
  427. HCD_DEV_ADDRESSED,
  428. HCD_DEV_CONFIGURED,
  429. HCD_DEV_GETSTRINGS,
  430. HCD_DEV_ERROR,
  431. HCD_VBUS_ERROR,
  432. HCD_POWER_FAULT,
  433. HCD_IDLE,
  434. HCD_BABBLE_ERROR
  435. }
  436. tUSBHDeviceState;
  437. //*****************************************************************************
  438. //
  439. // This structure holds the state information for a given host controller.
  440. //
  441. //*****************************************************************************
  442. typedef struct
  443. {
  444. unsigned int ulUSBBase;
  445. unsigned int ulIndex;
  446. unsigned char *rxBuffer;
  447. volatile unsigned int ulUSBHIntEvents;
  448. volatile unsigned int ulDMAPending;
  449. unsigned int ulPowerConfig;
  450. volatile unsigned int ulCurrentTick;
  451. int iUSBHActiveDriver;
  452. void *pvDriverInstance;
  453. tUSBHCDPipe USBControlPipe;
  454. tUSBHCDPipe USBOUTPipes[MAX_NUM_PIPES];
  455. tUSBHCDPipe USBINPipes[MAX_NUM_PIPES];
  456. unsigned char ucDMAChannels[MAX_NUM_DMA_CHANNELS];
  457. //
  458. // Each devices state.
  459. //
  460. tUSBHostDevice USBDevice[1];
  461. //
  462. // Holds the current state of the device.
  463. //
  464. volatile tUSBHDeviceState eDeviceState[1];
  465. //
  466. // Pointer to the memory pool for this controller.
  467. //
  468. void *pvPool;
  469. //
  470. // The pool size for this controller.
  471. //
  472. unsigned int ulPoolSize;
  473. //
  474. // The class drivers for this controller.
  475. //
  476. const tUSBHostClassDriver * const *pClassDrivers;
  477. //
  478. // The number of class drivers.
  479. //
  480. unsigned int ulNumClassDrivers;
  481. //
  482. // This is the index in the driver list of the event driver.
  483. //
  484. int iEventDriver;
  485. //
  486. // This is the generic event information used by the event driver.
  487. //
  488. tEventInfo EventInfo;
  489. unsigned int ulClass;
  490. //
  491. // Error handling specific information.
  492. //
  493. // This indicates the number of times host trys to re-initiate communication with device before
  494. // escalating to the application. By default, it is initialized to USBHCD_DEV_RECOVER_RETRY
  495. // in HCDINIT. User can update this using 'SET_CONNECT_RETRY' MACRO after HCDINIT
  496. unsigned int ulConnectRetry;
  497. // This field is maintained by the USB stack and stores the Timeout related information.
  498. tUSBHTimeOut USBHTimeOut;
  499. // This is a pointer to Timeout related information and can be used by
  500. // the application.
  501. tUSBHTimeOut *pUSBHTimeOut;
  502. }
  503. tUSBHCD;
  504. extern unsigned int g_ulConnectRetry[USB_NUM_INSTANCE];
  505. #define SET_CONNECT_RETRY(USBINSTANCE, RETRY_COUNT) g_ulConnectRetry[USBINSTANCE] = RETRY_COUNT
  506. //*****************************************************************************
  507. //
  508. // Mark the end of the C bindings section for C++ compilers.
  509. //
  510. //*****************************************************************************
  511. #ifdef __cplusplus
  512. }
  513. #endif
  514. #endif // __USBHOST_H__