usblib.h 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. //
  2. // usblib.h - Main header file for the USB Library.
  3. //
  4. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  5. // Software License Agreement
  6. //
  7. // Texas Instruments (TI) is supplying this software for use solely and
  8. // exclusively on TI's microcontroller products. The software is owned by
  9. // TI and/or its suppliers, and is protected under applicable copyright
  10. // laws. You may not combine this software with "viral" open-source
  11. // software in order to form a larger program.
  12. //
  13. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  14. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  15. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  17. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  18. // DAMAGES, FOR ANY REASON WHATSOEVER.
  19. //
  20. //
  21. //*****************************************************************************
  22. #ifndef __USBLIB_H__
  23. #define __USBLIB_H__
  24. //*****************************************************************************
  25. //
  26. // If building with a C++ compiler, make all of the definitions in this header
  27. // have a C binding.
  28. //
  29. //*****************************************************************************
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif
  34. /* standard device requests -- USB_SetupDataPacket::bRequest */
  35. #define USB_REQUEST_GETSTATUS (0u)
  36. #define USB_REQUEST_CLEARFEATURE (1u)
  37. #define USB_REQUEST_SETFEATURE (3u)
  38. #define USB_REQUEST_SETADDRESS (5u)
  39. #define USB_REQUEST_GETDESCRIPTOR (6u)
  40. #define USB_REQUEST_SETDESCRIPTOR (7u)
  41. #define USB_REQUEST_GETCONFIGURATION (8u)
  42. #define USB_REQUEST_SETCONFIGURATION (9u)
  43. #define USB_REQUEST_GETINTERFACE (10u)
  44. #define USB_REQUEST_SETINTERFACE (11u)
  45. #define USB_REQUEST_SYNCHFRAME (12u)
  46. //*****************************************************************************
  47. //
  48. // This is the maximum number of endpoints supported by the usblib.
  49. //
  50. //*****************************************************************************
  51. #define USBLIB_NUM_EP 16 // Number of supported endpoints.
  52. //*****************************************************************************
  53. //
  54. // The following macro allows compiler-independent syntax to be used to
  55. // define packed structures. A typical structure definition using these
  56. // macros will look similar to the following example:
  57. //
  58. // #ifdef ewarm
  59. // #pragma pack(1)
  60. // #endif
  61. //
  62. // typedef struct _PackedStructName
  63. // {
  64. // uint32 ulFirstField;
  65. // char cCharMember;
  66. // uint16 usShort;
  67. // }
  68. // PACKED tPackedStructName;
  69. //
  70. // #ifdef ewarm
  71. // #pragma pack()
  72. // #endif
  73. //
  74. // The conditional blocks related to ewarm include the #pragma pack() lines
  75. // only if the IAR Embedded Workbench compiler is being used. Unfortunately,
  76. // it is not possible to emit a #pragma from within a macro definition so this
  77. // must be done explicitly.
  78. //
  79. //*****************************************************************************
  80. #if defined(ccs) || \
  81. defined(codered) || \
  82. defined(gcc) || \
  83. defined(rvmdk) || \
  84. defined(__ARMCC_VERSION) || \
  85. defined(sourcerygxx)
  86. #define PACKED __attribute__ ((packed))
  87. #elif defined(ewarm) || defined(__IAR_SYSTEMS_ICC__)
  88. #define PACKED
  89. #elif (__TMS470__)
  90. #define PACKED __attribute__ ((packed))
  91. #else
  92. #error Unrecognized COMPILER!
  93. #endif
  94. //*****************************************************************************
  95. //
  96. // Assorted language IDs from the document "USB_LANGIDs.pdf" provided by the
  97. // USB Implementers' Forum (Version 1.0).
  98. //
  99. //*****************************************************************************
  100. #define USB_LANG_CHINESE_PRC 0x0804 // Chinese (PRC)
  101. #define USB_LANG_CHINESE_TAIWAN 0x0404 // Chinese (Taiwan)
  102. #define USB_LANG_EN_US 0x0409 // English (United States)
  103. #define USB_LANG_EN_UK 0x0809 // English (United Kingdom)
  104. #define USB_LANG_EN_AUS 0x0C09 // English (Australia)
  105. #define USB_LANG_EN_CA 0x1009 // English (Canada)
  106. #define USB_LANG_EN_NZ 0x1409 // English (New Zealand)
  107. #define USB_LANG_FRENCH 0x040C // French (Standard)
  108. #define USB_LANG_GERMAN 0x0407 // German (Standard)
  109. #define USB_LANG_HINDI 0x0439 // Hindi
  110. #define USB_LANG_ITALIAN 0x0410 // Italian (Standard)
  111. #define USB_LANG_JAPANESE 0x0411 // Japanese
  112. #define USB_LANG_KOREAN 0x0412 // Korean
  113. #define USB_LANG_ES_TRAD 0x040A // Spanish (Traditional)
  114. #define USB_LANG_ES_MODERN 0x0C0A // Spanish (Modern)
  115. #define USB_LANG_SWAHILI 0x0441 // Swahili (Kenya)
  116. #define USB_LANG_URDU_IN 0x0820 // Urdu (India)
  117. #define USB_LANG_URDU_PK 0x0420 // Urdu (Pakistan)
  118. //*****************************************************************************
  119. //
  120. //! \addtogroup usbchap9_src
  121. //! @{
  122. //
  123. //*****************************************************************************
  124. //*****************************************************************************
  125. //
  126. // Note:
  127. //
  128. // Structure definitions which are derived directly from the USB specification
  129. // use field names from the specification. Since a somewhat different version
  130. // of Hungarian prefix notation is used from the Stellaris standard, beware of
  131. // making assumptions about field sizes based on the field prefix when using
  132. // these structures. Of particular note is the difference in the meaning of
  133. // the 'i' prefix. In USB structures, this indicates a single byte index
  134. // whereas in Stellaris code, this is a 32 bit integer.
  135. //
  136. //*****************************************************************************
  137. //*****************************************************************************
  138. //
  139. // All structures defined in this section of the header require byte packing of
  140. // fields. This is usually accomplished using the PACKED macro but, for IAR
  141. // Embedded Workbench, this requires a pragma.
  142. //
  143. //*****************************************************************************
  144. #if defined(ewarm) || defined(__IAR_SYSTEMS_ICC__)
  145. #pragma pack(1)
  146. #endif
  147. //*****************************************************************************
  148. //
  149. // Definitions related to standard USB device requests (sections 9.3 & 9.4)
  150. //
  151. //*****************************************************************************
  152. //*****************************************************************************
  153. //
  154. //! The standard USB request header as defined in section 9.3 of the USB 2.0
  155. //! specification.
  156. //
  157. //*****************************************************************************
  158. typedef struct
  159. {
  160. //
  161. //! Determines the type and direction of the request.
  162. //
  163. uint8 bmRequestType;
  164. //
  165. //! Identifies the specific request being made.
  166. //
  167. uint8 bRequest;
  168. //
  169. //! Word-sized field that varies according to the request.
  170. //
  171. uint16 wValue;
  172. //
  173. //! Word-sized field that varies according to the request; typically used
  174. //! to pass an index or offset.
  175. //
  176. uint16 wIndex;
  177. //
  178. //! The number of bytes to transfer if there is a data stage to the
  179. //! request.
  180. //
  181. uint16 wLength;
  182. }
  183. PACKED tUSBRequest;
  184. //*****************************************************************************
  185. //
  186. // The following defines are used with the bmRequestType member of tUSBRequest.
  187. //
  188. // Request types have 3 bit fields:
  189. // 4:0 - Is the recipient type.
  190. // 6:5 - Is the request type.
  191. // 7 - Is the direction of the request.
  192. //
  193. //*****************************************************************************
  194. #define USB_RTYPE_DIR_IN 0x80
  195. #define USB_RTYPE_DIR_OUT 0x00
  196. #define USB_RTYPE_TYPE_M 0x60
  197. #define USB_RTYPE_VENDOR 0x40
  198. #define USB_RTYPE_CLASS 0x20
  199. #define USB_RTYPE_STANDARD 0x00
  200. #define USB_RTYPE_RECIPIENT_M 0x1f
  201. #define USB_RTYPE_OTHER 0x03
  202. #define USB_RTYPE_ENDPOINT 0x02
  203. #define USB_RTYPE_INTERFACE 0x01
  204. #define USB_RTYPE_DEVICE 0x00
  205. //*****************************************************************************
  206. //
  207. // Standard USB requests IDs used in the bRequest field of tUSBRequest.
  208. //
  209. //*****************************************************************************
  210. #define USBREQ_GET_STATUS 0x00
  211. #define USBREQ_CLEAR_FEATURE 0x01
  212. #define USBREQ_SET_FEATURE 0x03
  213. #define USBREQ_SET_ADDRESS 0x05
  214. #define USBREQ_GET_DESCRIPTOR 0x06
  215. #define USBREQ_SET_DESCRIPTOR 0x07
  216. #define USBREQ_GET_CONFIG 0x08
  217. #define USBREQ_SET_CONFIG 0x09
  218. #define USBREQ_GET_INTERFACE 0x0a
  219. #define USBREQ_SET_INTERFACE 0x0b
  220. #define USBREQ_SYNC_FRAME 0x0c
  221. //*****************************************************************************
  222. //
  223. // Data returned from a USBREQ_GET_STATUS request to a device.
  224. //
  225. //*****************************************************************************
  226. #define USB_STATUS_SELF_PWR 0x0001 // Currently self powered.
  227. #define USB_STATUS_BUS_PWR 0x0000 // Currently bus-powered.
  228. #define USB_STATUS_PWR_M 0x0001 // Mask for power mode.
  229. #define USB_STATUS_REMOTE_WAKE 0x0002 // Remote wake-up is currently enabled.
  230. //*****************************************************************************
  231. //
  232. // Feature Selectors (tUSBRequest.wValue) passed on USBREQ_CLEAR_FEATURE and
  233. // USBREQ_SET_FEATURE.
  234. //
  235. //*****************************************************************************
  236. #define USB_FEATURE_EP_HALT 0x0000 // Endpoint halt feature.
  237. #define USB_FEATURE_REMOTE_WAKE 0x0001 // Remote wake feature, device only.
  238. #define USB_FEATURE_TEST_MODE 0x0002 // Test mode
  239. //*****************************************************************************
  240. //
  241. // Endpoint Selectors (tUSBRequest.wIndex) passed on USBREQ_CLEAR_FEATURE,
  242. // USBREQ_SET_FEATURE and USBREQ_GET_STATUS.
  243. //
  244. //*****************************************************************************
  245. #define USB_REQ_EP_NUM_M 0x007F
  246. #define USB_REQ_EP_DIR_M 0x0080
  247. #define USB_REQ_EP_DIR_IN 0x0080
  248. #define USB_REQ_EP_DIR_OUT 0x0000
  249. //*****************************************************************************
  250. //
  251. // Standard USB descriptor types. These values are passed in the upper bytes
  252. // of tUSBRequest.wValue on USBREQ_GET_DESCRIPTOR and also appear in the
  253. // bDescriptorType field of standard USB descriptors.
  254. //
  255. //*****************************************************************************
  256. #define USB_DTYPE_DEVICE 1
  257. #define USB_DTYPE_CONFIGURATION 2
  258. #define USB_DTYPE_STRING 3
  259. #define USB_DTYPE_INTERFACE 4
  260. #define USB_DTYPE_ENDPOINT 5
  261. #define USB_DTYPE_DEVICE_QUAL 6
  262. #define USB_DTYPE_OSPEED_CONF 7
  263. #define USB_DTYPE_INTERFACE_PWR 8
  264. #define USB_DTYPE_OTG 9
  265. #define USB_DTYPE_INTERFACE_ASC 11
  266. #define USB_DTYPE_CS_INTERFACE 36
  267. //*****************************************************************************
  268. //
  269. // Definitions related to USB descriptors (sections 9.5 & 9.6)
  270. //
  271. //*****************************************************************************
  272. //*****************************************************************************
  273. //
  274. //! This structure describes a generic descriptor header. These fields are to
  275. //! be found at the beginning of all valid USB descriptors.
  276. //
  277. //*****************************************************************************
  278. typedef struct
  279. {
  280. //
  281. //! The length of this descriptor (including this length byte) expressed
  282. //! in bytes.
  283. //
  284. uint8 bLength;
  285. //
  286. //! The type identifier of the descriptor whose information follows. For
  287. //! standard descriptors, this field could contain, for example,
  288. //! USB_DTYPE_DEVICE to identify a device descriptor or USB_DTYPE_ENDPOINT
  289. //! to identify an endpoint descriptor.
  290. //
  291. uint8 bDescriptorType;
  292. }
  293. PACKED tDescriptorHeader;
  294. //*****************************************************************************
  295. //
  296. //! This structure describes the USB device descriptor as defined in USB
  297. //! 2.0 specification section 9.6.1.
  298. //
  299. //*****************************************************************************
  300. typedef struct
  301. {
  302. //
  303. //! The length of this descriptor in bytes. All device descriptors are
  304. //! 18 bytes long.
  305. //
  306. uint8 bLength;
  307. //
  308. //! The type of the descriptor. For a device descriptor, this will be
  309. //! USB_DTYPE_DEVICE (1).
  310. //
  311. uint8 bDescriptorType;
  312. //
  313. //! The USB Specification Release Number in BCD format. For USB 2.0, this
  314. //! will be 0x0200.
  315. //
  316. uint16 bcdUSB;
  317. //
  318. //! The device class code.
  319. //
  320. uint8 bDeviceClass;
  321. //
  322. //! The device subclass code. This value qualifies the value found in the
  323. //! bDeviceClass field.
  324. //
  325. uint8 bDeviceSubClass;
  326. //
  327. //! The device protocol code. This value is qualified by the values of
  328. //! bDeviceClass and bDeviceSubClass.
  329. //
  330. uint8 bDeviceProtocol;
  331. //
  332. //! The maximum packet size for endpoint zero. Valid values are 8, 16, 32
  333. //! and 64.
  334. //
  335. uint8 bMaxPacketSize0;
  336. //
  337. //! The device Vendor ID (VID) as assigned by the USB-IF.
  338. //
  339. uint16 idVendor;
  340. //
  341. //! The device Product ID (PID) as assigned by the manufacturer.
  342. //
  343. uint16 idProduct;
  344. //
  345. //! The device release number in BCD format.
  346. //
  347. uint16 bcdDevice;
  348. //
  349. //! The index of a string descriptor describing the manufacturer.
  350. //
  351. uint8 iManufacturer;
  352. //
  353. //! The index of a string descriptor describing the product.
  354. //
  355. uint8 iProduct;
  356. //
  357. //! The index of a string descriptor describing the device's serial
  358. //! number.
  359. //
  360. uint8 iSerialNumber;
  361. //
  362. //! The number of possible configurations offered by the device. This
  363. //! field indicates the number of distinct configuration descriptors that
  364. //! the device offers.
  365. //
  366. uint8 bNumConfigurations;
  367. }
  368. PACKED tDeviceDescriptor;
  369. //*****************************************************************************
  370. //
  371. // USB Device Class codes used in the tDeviceDescriptor.bDeviceClass field.
  372. // Definitions for the bDeviceSubClass and bDeviceProtocol fields are device
  373. // specific and can be found in the appropriate device class header files.
  374. //
  375. //*****************************************************************************
  376. #define USB_CLASS_DEVICE 0x00
  377. #define USB_CLASS_AUDIO 0x01
  378. #define USB_CLASS_CDC 0x02
  379. #define USB_CLASS_HID 0x03
  380. #define USB_CLASS_PHYSICAL 0x05
  381. #define USB_CLASS_IMAGE 0x06
  382. #define USB_CLASS_PRINTER 0x07
  383. #define USB_CLASS_MASS_STORAGE 0x08
  384. #define USB_CLASS_HUB 0x09
  385. #define USB_CLASS_CDC_DATA 0x0a
  386. #define USB_CLASS_SMART_CARD 0x0b
  387. #define USB_CLASS_SECURITY 0x0d
  388. #define USB_CLASS_VIDEO 0x0e
  389. #define USB_CLASS_HEALTHCARE 0x0f
  390. #define USB_CLASS_DIAG_DEVICE 0xdc
  391. #define USB_CLASS_WIRELESS 0xe0
  392. #define USB_CLASS_MISC 0xef
  393. #define USB_CLASS_APP_SPECIFIC 0xfe
  394. #define USB_CLASS_VEND_SPECIFIC 0xff
  395. #define USB_CLASS_EVENTS 0xffffffff
  396. //*****************************************************************************
  397. //
  398. // Generic values for undefined subclass and protocol.
  399. //
  400. //*****************************************************************************
  401. #define USB_SUBCLASS_UNDEFINED 0x00
  402. #define USB_PROTOCOL_UNDEFINED 0x00
  403. //*****************************************************************************
  404. //
  405. // The following are the miscellaneous subclass values.
  406. //
  407. //*****************************************************************************
  408. #define USB_MISC_SUBCLASS_SYNC 0x01
  409. #define USB_MISC_SUBCLASS_COMMON 0x02
  410. //*****************************************************************************
  411. //
  412. // These following are miscellaneous protocol values.
  413. //
  414. //*****************************************************************************
  415. #define USB_MISC_PROTOCOL_IAD 0x01
  416. //*****************************************************************************
  417. //
  418. //! This structure describes the USB device qualifier descriptor as defined in
  419. //! the USB 2.0 specification, section 9.6.2.
  420. //
  421. //*****************************************************************************
  422. typedef struct
  423. {
  424. //
  425. //! The length of this descriptor in bytes. All device qualifier
  426. //! descriptors are 10 bytes long.
  427. //
  428. uint8 bLength;
  429. //
  430. //! The type of the descriptor. For a device descriptor, this will be
  431. //! USB_DTYPE_DEVICE_QUAL (6).
  432. //
  433. uint8 bDescriptorType;
  434. //
  435. //! The USB Specification Release Number in BCD format. For USB 2.0, this
  436. //! will be 0x0200.
  437. //
  438. uint16 bcdUSB;
  439. //
  440. //! The device class code.
  441. //
  442. uint8 bDeviceClass;
  443. //
  444. //! The device subclass code. This value qualifies the value found in the
  445. //! bDeviceClass field.
  446. //
  447. uint8 bDeviceSubClass;
  448. //
  449. //! The device protocol code. This value is qualified by the values of
  450. //! bDeviceClass and bDeviceSubClass.
  451. //
  452. uint8 bDeviceProtocol;
  453. //
  454. //! The maximum packet size for endpoint zero when operating at a speed
  455. //! other than high speed.
  456. //
  457. uint8 bMaxPacketSize0;
  458. //
  459. //! The number of other-speed configurations supported.
  460. //
  461. uint8 bNumConfigurations;
  462. //
  463. //! Reserved for future use. Must be set to zero.
  464. //
  465. uint8 bReserved;
  466. }
  467. PACKED tDeviceQualifierDescriptor;
  468. //*****************************************************************************
  469. //
  470. //! This structure describes the USB configuration descriptor as defined in
  471. //! USB 2.0 specification section 9.6.3. This structure also applies to the
  472. //! USB other speed configuration descriptor defined in section 9.6.4.
  473. //
  474. //*****************************************************************************
  475. typedef struct
  476. {
  477. //
  478. //! The length of this descriptor in bytes. All configuration descriptors
  479. //! are 9 bytes long.
  480. //
  481. uint8 bLength;
  482. //
  483. //! The type of the descriptor. For a configuration descriptor, this will
  484. //! be USB_DTYPE_CONFIGURATION (2).
  485. //
  486. uint8 bDescriptorType;
  487. //
  488. //! The total length of data returned for this configuration. This
  489. //! includes the combined length of all descriptors (configuration,
  490. //! interface, endpoint and class- or vendor-specific) returned for this
  491. //! configuration.
  492. //
  493. uint16 wTotalLength;
  494. //
  495. //! The number of interface supported by this configuration.
  496. //
  497. uint8 bNumInterfaces;
  498. //
  499. //! The value used as an argument to the SetConfiguration standard request
  500. //! to select this configuration.
  501. //
  502. uint8 bConfigurationValue;
  503. //
  504. //! The index of a string descriptor describing this configuration.
  505. //
  506. uint8 iConfiguration;
  507. //
  508. //! Attributes of this configuration.
  509. //
  510. uint8 bmAttributes;
  511. //
  512. //! The maximum power consumption of the USB device from the bus in this
  513. //! configuration when the device is fully operational. This is expressed
  514. //! in units of 2mA so, for example, 100 represents 200mA.
  515. //
  516. uint8 bMaxPower;
  517. }
  518. PACKED tConfigDescriptor;
  519. //*****************************************************************************
  520. //
  521. // Flags used in constructing the value assigned to the field
  522. // tConfigDescriptor.bmAttributes. Note that bit 7 is reserved and must be set
  523. // to 1.
  524. //
  525. //*****************************************************************************
  526. #define USB_CONF_ATTR_PWR_M 0xC0
  527. #define USB_CONF_ATTR_SELF_PWR 0xC0
  528. #define USB_CONF_ATTR_BUS_PWR 0x80
  529. #define USB_CONF_ATTR_RWAKE 0xA0
  530. //*****************************************************************************
  531. //
  532. //! This structure describes the USB interface descriptor as defined in USB
  533. //! 2.0 specification section 9.6.5.
  534. //
  535. //*****************************************************************************
  536. typedef struct
  537. {
  538. //
  539. //! The length of this descriptor in bytes. All interface descriptors
  540. //! are 9 bytes long.
  541. //
  542. uint8 bLength;
  543. //
  544. //! The type of the descriptor. For an interface descriptor, this will
  545. //! be USB_DTYPE_INTERFACE (4).
  546. //
  547. uint8 bDescriptorType;
  548. //
  549. //! The number of this interface. This is a zero based index into the
  550. //! array of concurrent interfaces supported by this configuration.
  551. //
  552. uint8 bInterfaceNumber;
  553. //
  554. //! The value used to select this alternate setting for the interface
  555. //! defined in bInterfaceNumber.
  556. //
  557. uint8 bAlternateSetting;
  558. //
  559. //! The number of endpoints used by this interface (excluding endpoint
  560. //! zero).
  561. //
  562. uint8 bNumEndpoints;
  563. //
  564. //! The interface class code as assigned by the USB-IF.
  565. //
  566. uint8 bInterfaceClass;
  567. //
  568. //! The interface subclass code as assigned by the USB-IF.
  569. //
  570. uint8 bInterfaceSubClass;
  571. //
  572. //! The interface protocol code as assigned by the USB-IF.
  573. //
  574. uint8 bInterfaceProtocol;
  575. //
  576. //! The index of a string descriptor describing this interface.
  577. //
  578. uint8 iInterface;
  579. }
  580. PACKED tInterfaceDescriptor;
  581. //*****************************************************************************
  582. //
  583. //! This structure describes the USB endpoint descriptor as defined in USB
  584. //! 2.0 specification section 9.6.6.
  585. //
  586. //*****************************************************************************
  587. typedef struct
  588. {
  589. //
  590. //! The length of this descriptor in bytes. All endpoint descriptors
  591. //! are 7 bytes long.
  592. //
  593. uint8 bLength;
  594. //
  595. //! The type of the descriptor. For an endpoint descriptor, this will
  596. //! be USB_DTYPE_ENDPOINT (5).
  597. //
  598. uint8 bDescriptorType;
  599. //
  600. //! The address of the endpoint. This field contains the endpoint number
  601. //! ORed with flag USB_EP_DESC_OUT or USB_EP_DESC_IN to indicate the
  602. //! endpoint direction.
  603. //
  604. uint8 bEndpointAddress;
  605. //
  606. //! The endpoint transfer type, USB_EP_ATTR_CONTROL, USB_EP_ATTR_ISOC,
  607. //! USB_EP_ATTR_BULK or USB_EP_ATTR_INT and, if isochronous, additional
  608. //! flags indicating usage type and synchronization method.
  609. //
  610. uint8 bmAttributes;
  611. //
  612. //! The maximum packet size this endpoint is capable of sending or
  613. //! receiving when this configuration is selected. For high speed
  614. //! isochronous or interrupt endpoints, bits 11 and 12 are used to
  615. //! pass additional information.
  616. //
  617. uint16 wMaxPacketSize;
  618. //
  619. //! The polling interval for data transfers expressed in frames or
  620. //! micro frames depending upon the operating speed.
  621. //
  622. uint8 bInterval;
  623. }
  624. PACKED tEndpointDescriptor;
  625. //*****************************************************************************
  626. //
  627. // Flags used in constructing the value assigned to the field
  628. // tEndpointDescriptor.bEndpointAddress.
  629. //
  630. //*****************************************************************************
  631. #define USB_EP_DESC_OUT 0x00
  632. #define USB_EP_DESC_IN 0x80
  633. #define USB_EP_DESC_NUM_M 0x0f
  634. //*****************************************************************************
  635. //
  636. // Mask used to extract the maximum packet size (in bytes) from the
  637. // wMaxPacketSize field of the endpoint descriptor.
  638. //
  639. //*****************************************************************************
  640. #define USB_EP_MAX_PACKET_COUNT_M 0x07FF
  641. //*****************************************************************************
  642. //
  643. // Endpoint attributes used in tEndpointDescriptor.bmAttributes.
  644. //
  645. //*****************************************************************************
  646. #define USB_EP_ATTR_CONTROL 0x00
  647. #define USB_EP_ATTR_ISOC 0x01
  648. #define USB_EP_ATTR_BULK 0x02
  649. #define USB_EP_ATTR_INT 0x03
  650. #define USB_EP_ATTR_TYPE_M 0x03
  651. #define USB_EP_ATTR_ISOC_M 0x0c
  652. #define USB_EP_ATTR_ISOC_NOSYNC 0x00
  653. #define USB_EP_ATTR_ISOC_ASYNC 0x04
  654. #define USB_EP_ATTR_ISOC_ADAPT 0x08
  655. #define USB_EP_ATTR_ISOC_SYNC 0x0c
  656. #define USB_EP_ATTR_USAGE_M 0x30
  657. #define USB_EP_ATTR_USAGE_DATA 0x00
  658. #define USB_EP_ATTR_USAGE_FEEDBACK 0x10
  659. #define USB_EP_ATTR_USAGE_IMPFEEDBACK 0x20
  660. //*****************************************************************************
  661. //
  662. //! This structure describes the USB string descriptor for index 0 as defined
  663. //! in USB 2.0 specification section 9.6.7. Note that the number of language
  664. //! IDs is variable and can be determined by examining bLength. The number of
  665. //! language IDs present in the descriptor is given by ((bLength - 2) / 2).
  666. //
  667. //*****************************************************************************
  668. typedef struct
  669. {
  670. //
  671. //! The length of this descriptor in bytes. This value will vary
  672. //! depending upon the number of language codes provided in the descriptor.
  673. //
  674. uint8 bLength;
  675. //
  676. //! The type of the descriptor. For a string descriptor, this will be
  677. //! USB_DTYPE_STRING (3).
  678. //
  679. uint8 bDescriptorType;
  680. //
  681. //! The language code (LANGID) for the first supported language. Note that
  682. //! this descriptor may support multiple languages, in which case, the
  683. //! number of elements in the wLANGID array will increase and bLength will
  684. //! be updated accordingly.
  685. //
  686. uint16 wLANGID[1];
  687. }
  688. PACKED tString0Descriptor;
  689. //*****************************************************************************
  690. //
  691. //! This structure describes the USB string descriptor for all string indexes
  692. //! other than 0 as defined in USB 2.0 specification section 9.6.7.
  693. //
  694. //*****************************************************************************
  695. typedef struct
  696. {
  697. //
  698. //! The length of this descriptor in bytes. This value will be 2 greater
  699. //! than the number of bytes comprising the UNICODE string that the
  700. //! descriptor contains.
  701. //
  702. uint8 bLength;
  703. //
  704. //! The type of the descriptor. For a string descriptor, this will be
  705. //! USB_DTYPE_STRING (3).
  706. //
  707. uint8 bDescriptorType;
  708. //
  709. //! The first byte of the UNICODE string. This string is not NULL
  710. //! terminated. Its length (in bytes) can be computed by subtracting 2
  711. //! from the value in the bLength field.
  712. //
  713. uint8 bString;
  714. }
  715. PACKED tStringDescriptor;
  716. //*****************************************************************************
  717. //
  718. //! Write a 2 byte uint16 value to a USB descriptor block.
  719. //!
  720. //! \param usValue is the two byte uint16 that is to be written to
  721. //! the descriptor.
  722. //!
  723. //! This helper macro is used in descriptor definitions to write two-byte
  724. //! values. Since the configuration descriptor contains all interface and
  725. //! endpoint descriptors in a contiguous block of memory, these descriptors are
  726. //! typically defined using an array of bytes rather than as packed structures.
  727. //!
  728. //! \return Not a function.
  729. //
  730. //*****************************************************************************
  731. #define USBShort(usValue) (usValue & 0xff), (usValue >> 8)
  732. //*****************************************************************************
  733. //
  734. //! Write a 3 byte uint32 value to a USB descriptor block.
  735. //!
  736. //! \param ulValue is the three byte unsigned value that is to be written to the
  737. //! descriptor.
  738. //!
  739. //! This helper macro is used in descriptor definitions to write three-byte
  740. //! values. Since the configuration descriptor contains all interface and
  741. //! endpoint descriptors in a contiguous block of memory, these descriptors are
  742. //! typically defined using an array of bytes rather than as packed structures.
  743. //!
  744. //! \return Not a function.
  745. //
  746. //*****************************************************************************
  747. #define USB3Byte(ulValue) (ulValue & 0xff), \
  748. ((ulValue >> 8) & 0xff), \
  749. ((ulValue >> 16) & 0xff)
  750. //*****************************************************************************
  751. //
  752. //! Write a 4 byte uint32 value to a USB descriptor block.
  753. //!
  754. //! \param ulValue is the four byte uint32 that is to be written to the
  755. //! descriptor.
  756. //!
  757. //! This helper macro is used in descriptor definitions to write four-byte
  758. //! values. Since the configuration descriptor contains all interface and
  759. //! endpoint descriptors in a contiguous block of memory, these descriptors are
  760. //! typically defined using an array of bytes rather than as packed structures.
  761. //!
  762. //! \return Not a function.
  763. //
  764. //*****************************************************************************
  765. #define USBLong(ulValue) (ulValue & 0xff), \
  766. ((ulValue >> 8) & 0xff), \
  767. ((ulValue >> 16) & 0xff), \
  768. ((ulValue >> 24) & 0xff)
  769. //*****************************************************************************
  770. //
  771. //! Traverse to the next USB descriptor in a block.
  772. //!
  773. //! \param ptr points to the first byte of a descriptor in a block of
  774. //! USB descriptors.
  775. //!
  776. //! This macro aids in traversing lists of descriptors by returning a pointer
  777. //! to the next descriptor in the list given a pointer to the current one.
  778. //!
  779. //! \return Returns a pointer to the next descriptor in the block following
  780. //! \e ptr.
  781. //!
  782. //*****************************************************************************
  783. #define NEXT_USB_DESCRIPTOR(ptr) \
  784. (tDescriptorHeader *)(((uint8 *)(ptr)) + \
  785. *((uint8 *)(ptr)))
  786. //*****************************************************************************
  787. //
  788. // Return to default packing when using the IAR Embedded Workbench compiler.
  789. //
  790. //*****************************************************************************
  791. #if defined(ewarm) || defined(__IAR_SYSTEMS_ICC__)
  792. #pragma pack()
  793. #endif
  794. //*****************************************************************************
  795. //
  796. // Close the usbchap9_src Doxygen group.
  797. //! @}
  798. //
  799. //*****************************************************************************
  800. //*****************************************************************************
  801. //
  802. //! \addtogroup device_api
  803. //! @{
  804. //
  805. //*****************************************************************************
  806. //*****************************************************************************
  807. //
  808. // Function prototype for any standard USB request.
  809. //
  810. //*****************************************************************************
  811. typedef void (* tStdRequest)(void *pvInstance, tUSBRequest *pUSBRequest);
  812. //*****************************************************************************
  813. //
  814. // Data callback for receiving data from an endpoint.
  815. //
  816. //*****************************************************************************
  817. typedef void (* tInfoCallback)(void *pvInstance, uint32 ulInfo);
  818. //*****************************************************************************
  819. //
  820. // Callback made to indicate that an interface alternate setting change has
  821. // occurred.
  822. //
  823. //*****************************************************************************
  824. typedef void (* tInterfaceCallback)(void *pvInstance,
  825. uint8 ucInterfaceNum,
  826. uint8 ucAlternateSetting);
  827. //*****************************************************************************
  828. //
  829. // Generic interrupt handler callbacks.
  830. //
  831. //*****************************************************************************
  832. typedef void (* tUSBIntHandler)(void *pvInstance);
  833. //*****************************************************************************
  834. //
  835. // Interrupt handler callbacks that have status information.
  836. //
  837. //*****************************************************************************
  838. typedef void (* tUSBEPIntHandler)(void *pvInstance,
  839. uint32 ulStatus);
  840. //*****************************************************************************
  841. //
  842. // Generic handler callbacks that are used when the callers needs to call into
  843. // an instance of class.
  844. //
  845. //*****************************************************************************
  846. typedef void (* tUSBDeviceHandler)(void *pvInstance,
  847. uint32 ulRequest,
  848. void *pvRequestData);
  849. //*****************************************************************************
  850. //
  851. //! USB event handler functions used during enumeration and operation of the
  852. //! device stack.
  853. //
  854. //*****************************************************************************
  855. typedef struct
  856. {
  857. //
  858. //! This callback is made whenever the USB host requests a non-standard
  859. //! descriptor from the device.
  860. //
  861. tStdRequest pfnGetDescriptor;
  862. //
  863. //! This callback is made whenever the USB host makes a non-standard
  864. //! request.
  865. //
  866. tStdRequest pfnRequestHandler;
  867. //
  868. //! This callback is made in response to a SetInterface request from the
  869. //! host.
  870. //
  871. tInterfaceCallback pfnInterfaceChange;
  872. //
  873. //! This callback is made in response to a SetConfiguration request from
  874. //! the host.
  875. //
  876. tInfoCallback pfnConfigChange;
  877. //
  878. //! This callback is made when data has been received following to a call
  879. //! to USBDCDRequestDataEP0.
  880. //
  881. tInfoCallback pfnDataReceived;
  882. //
  883. //! This callback is made when data has been transmitted following a call
  884. //! to USBDCDSendDataEP0.
  885. //
  886. tInfoCallback pfnDataSent;
  887. //
  888. //! This callback is made when a USB reset is detected.
  889. //
  890. tUSBIntHandler pfnResetHandler;
  891. //
  892. //! This callback is made when the bus has been inactive long enough to
  893. //! trigger a suspend condition.
  894. //
  895. tUSBIntHandler pfnSuspendHandler;
  896. //
  897. //! This is called when resume signaling is detected.
  898. //
  899. tUSBIntHandler pfnResumeHandler;
  900. //
  901. //! This callback is made when the device is disconnected from the USB bus.
  902. //
  903. tUSBIntHandler pfnDisconnectHandler;
  904. //
  905. //! This callback is made to inform the device of activity on all endpoints
  906. //! other than endpoint zero.
  907. //
  908. tUSBEPIntHandler pfnEndpointHandler;
  909. //
  910. //! This generic handler is provided to allow requests based on
  911. //! a given instance to be passed into a device. This is commonly used
  912. //! by a top level composite device that is using multiple instances of
  913. //! a class.
  914. //
  915. tUSBDeviceHandler pfnDeviceHandler;
  916. }
  917. tCustomHandlers;
  918. //*****************************************************************************
  919. //
  920. //! This structure defines how a given endpoint's FIFO is configured in
  921. //! relation to the maximum packet size for the endpoint as specified in the
  922. //! endpoint descriptor.
  923. //
  924. //*****************************************************************************
  925. typedef struct
  926. {
  927. //
  928. //! The multiplier to apply to an endpoint's maximum packet size when
  929. //! configuring the FIFO for that endpoint. For example, setting this
  930. //! value to 2 will result in a 128 byte FIFO being configured if
  931. //! bDoubleBuffer is FALSE and the associated endpoint is set to use a 64
  932. //! byte maximum packet size.
  933. //
  934. uint8 cMultiplier;
  935. //
  936. //! This field indicates whether to configure an endpoint's FIFO to be
  937. //! double- or single-buffered. If TRUE, a double-buffered FIFO is
  938. //! created and the amount of required FIFO storage is multiplied by two.
  939. //
  940. tBoolean bDoubleBuffer;
  941. //
  942. //! This field defines endpoint mode flags which cannot be deduced from
  943. //! the configuration descriptor, namely any in the set USB_EP_AUTO_xxx or
  944. //! USB_EP_DMA_MODE_x. USBDCDConfig adds these flags to the endpoint
  945. //! mode and direction determined from the config descriptor before it
  946. //! configures the endpoint using a call to USBDevEndpointConfigSet().
  947. //
  948. uint16 usEPFlags;
  949. }
  950. tFIFOEntry;
  951. //*****************************************************************************
  952. //
  953. //! This structure defines endpoint and FIFO configuration information for
  954. //! all endpoints that the device wishes to use. This information cannot be
  955. //! determined by examining the USB configuration descriptor and is
  956. //! provided to USBDCDConfig by the application to allow the USB controller
  957. //! endpoints to be correctly configured.
  958. //
  959. //*****************************************************************************
  960. typedef struct
  961. {
  962. //
  963. //! An array containing one FIFO entry for each of the IN endpoints.
  964. //! Note that endpoint 0 is configured and managed by the USB device stack
  965. //! so is excluded from this array. The index 0 entry of the array
  966. //! corresponds to endpoint 1, index 1 to endpoint 2, etc.
  967. //
  968. tFIFOEntry sIn[USBLIB_NUM_EP - 1];
  969. //
  970. //! An array containing one FIFO entry for each of the OUT endpoints.
  971. //! Note that endpoint 0 is configured and managed by the USB device stack
  972. //! so is excluded from this array. The index 0 entry of the array
  973. //! corresponds to endpoint 1, index 1 to endpoint 2, etc.
  974. //
  975. tFIFOEntry sOut[USBLIB_NUM_EP - 1];
  976. }
  977. tFIFOConfig;
  978. //*****************************************************************************
  979. //
  980. //! This structure defines a contiguous block of data which contains a group
  981. //! of descriptors that form part of a configuration descriptor for a device.
  982. //! It is assumed that a config section contains only whole descriptors. It is
  983. //! not valid to split a single descriptor across multiple sections.
  984. //!
  985. //*****************************************************************************
  986. typedef struct
  987. {
  988. //
  989. //! The number of bytes of descriptor data pointed to by pucData.
  990. //
  991. uint8 ucSize;
  992. //
  993. //! A pointer to a block of data containing an integral number of
  994. //! USB descriptors which form part of a larger configuration descriptor.
  995. //
  996. const uint8 *pucData;
  997. }
  998. tConfigSection;
  999. //*****************************************************************************
  1000. //
  1001. //! This is the top level structure defining a USB device configuration
  1002. //! descriptor. A configuration descriptor contains a collection of device-
  1003. //! specific descriptors in addition to the basic config, interface and
  1004. //! endpoint descriptors. To allow flexibility in constructing the
  1005. //! configuration, the descriptor is described in terms of a list of data
  1006. //! blocks. The first block must contain the configuration descriptor itself
  1007. //! and the following blocks are appended to this in order to produce the
  1008. //! full descriptor sent to the host in response to a GetDescriptor request
  1009. //! for the configuration descriptor.
  1010. //!
  1011. //*****************************************************************************
  1012. typedef struct
  1013. {
  1014. //
  1015. //! The number of sections comprising the full descriptor for this
  1016. //! configuration.
  1017. //
  1018. uint8 ucNumSections;
  1019. //
  1020. //! A pointer to an array of ucNumSections section pointers which must
  1021. //! be concatenated to form the configuration descriptor.
  1022. //
  1023. const tConfigSection * const *psSections;
  1024. }
  1025. tConfigHeader;
  1026. //*****************************************************************************
  1027. //
  1028. //! This structure is passed to the USB library on a call to USBDCDInit and
  1029. //! provides the library with information about the device that the
  1030. //! application is implementing. It contains functions pointers for the
  1031. //! various USB event handlers and pointers to each of the standard device
  1032. //! descriptors.
  1033. //
  1034. //*****************************************************************************
  1035. typedef struct
  1036. {
  1037. //
  1038. //! A pointer to a structure containing pointers to event handler functions
  1039. //! provided by the client to support the operation of this device.
  1040. //
  1041. tCustomHandlers sCallbacks;
  1042. //
  1043. //! A pointer to the device descriptor for this device.
  1044. //
  1045. const uint8 *pDeviceDescriptor;
  1046. //
  1047. //! A pointer to an array of configuration descriptor pointers. Each entry
  1048. //! in the array corresponds to one configuration that the device may be set
  1049. //! to use by the USB host. The number of entries in the array must
  1050. //! match the bNumConfigurations value in the device descriptor
  1051. //! array, pDeviceDescriptor.
  1052. //
  1053. const tConfigHeader * const *ppConfigDescriptors;
  1054. //
  1055. //! A pointer to the string descriptor array for this device. This array
  1056. //! must be arranged as follows:
  1057. //!
  1058. //! - [0] - Standard descriptor containing supported language codes.
  1059. //! - [1] - String 1 for the first language listed in descriptor 0.
  1060. //! - [2] - String 2 for the first language listed in descriptor 0.
  1061. //! - ...
  1062. //! - [n] - String n for the first language listed in descriptor 0.
  1063. //! - [n+1] - String 1 for the second language listed in descriptor 0.
  1064. //! - ...
  1065. //! - [2n] - String n for the second language listed in descriptor 0.
  1066. //! - [2n+1]- String 1 for the third language listed in descriptor 0.
  1067. //! - ...
  1068. //! - [3n] - String n for the third language listed in descriptor 0.
  1069. //!
  1070. //! and so on.
  1071. //
  1072. const uint8 * const *ppStringDescriptors;
  1073. //
  1074. //! The total number of descriptors provided in the ppStringDescriptors
  1075. //! array.
  1076. //
  1077. uint32 ulNumStringDescriptors;
  1078. //
  1079. //! A structure defining how the USB controller FIFO is to be partitioned
  1080. //! between the various endpoints. This member can be set to point to
  1081. //! g_sUSBDefaultFIFOConfig if the default FIFO configuration is acceptable
  1082. //! This configuration sets each endpoint FIFO to be single buffered and
  1083. //! sized to hold the maximum packet size for the endpoint.
  1084. //
  1085. const tFIFOConfig *psFIFOConfig;
  1086. //
  1087. //! This value will be passed back to all call back functions so that
  1088. //! they have access to individual instance data based on the this pointer.
  1089. //
  1090. void *pvInstance;
  1091. }
  1092. tDeviceInfo;
  1093. //*****************************************************************************
  1094. //
  1095. // Close the Doxygen group.
  1096. //! @}
  1097. //
  1098. //*****************************************************************************
  1099. //*****************************************************************************
  1100. //
  1101. //! \addtogroup general_usblib_api
  1102. //! @{
  1103. //
  1104. //*****************************************************************************
  1105. //*****************************************************************************
  1106. //
  1107. // USB descriptor parsing functions found in usbdesc.c
  1108. //
  1109. //*****************************************************************************
  1110. //*****************************************************************************
  1111. //
  1112. //! The USB_DESC_ANY label is used as a wild card in several of the descriptor
  1113. //! parsing APIs to determine whether or not particular search criteria should
  1114. //! be ignored.
  1115. //
  1116. //*****************************************************************************
  1117. #define USB_DESC_ANY 0xFFFFFFFF
  1118. extern uint32 USBDescGetNum(tDescriptorHeader *psDesc,
  1119. uint32 ulSize, uint32 ulType);
  1120. extern tDescriptorHeader *USBDescGet(tDescriptorHeader *psDesc,
  1121. uint32 ulSize,
  1122. uint32 ulType,
  1123. uint32 ulIndex);
  1124. extern uint32
  1125. USBDescGetNumAlternateInterfaces(tConfigDescriptor *psConfig,
  1126. uint8 ucInterfaceNumber);
  1127. extern tInterfaceDescriptor *USBDescGetInterface(tConfigDescriptor *psConfig,
  1128. uint32 ulIndex,
  1129. uint32 ulAltCfg);
  1130. extern tEndpointDescriptor *
  1131. USBDescGetInterfaceEndpoint(tInterfaceDescriptor *psInterface,
  1132. uint32 ulIndex,
  1133. uint32 ulSize);
  1134. //*****************************************************************************
  1135. //
  1136. //! The operating mode required by the USB library client. This type is used
  1137. //! by applications which wish to be able to switch between host and device
  1138. //! modes by calling the USBStackModeSet() API.
  1139. //
  1140. //*****************************************************************************
  1141. typedef enum
  1142. {
  1143. //
  1144. //! The application wishes to operate as a USB device.
  1145. //
  1146. USB_MODE_DEVICE = 0,
  1147. //
  1148. //! The application wishes to operate as a USB host.
  1149. //
  1150. USB_MODE_HOST,
  1151. //
  1152. //! The application wishes to operate as both a host and device using
  1153. //! On-The-Go protocols to negotiate.
  1154. //
  1155. USB_MODE_OTG,
  1156. //
  1157. //! A marker indicating that no USB mode has yet been set by the
  1158. //! application.
  1159. //
  1160. USB_MODE_NONE
  1161. } tUSBMode;
  1162. //*****************************************************************************
  1163. //
  1164. // A pointer to a USB mode callback function. This function is called by the
  1165. // USB library to indicate to the application which operating mode it should
  1166. // use, host or device.
  1167. //
  1168. //*****************************************************************************
  1169. typedef void (*tUSBModeCallback)(uint32 ulIndex, tUSBMode eMode);
  1170. //*****************************************************************************
  1171. //
  1172. // Mode selection and dual mode interrupt steering functions.
  1173. //
  1174. //*****************************************************************************
  1175. extern void USBStackModeSet(uint32 ulIndex, tUSBMode eUSBMode,
  1176. tUSBModeCallback pfnCallback);
  1177. extern void USBDualModeInit(uint32 ulIndex);
  1178. extern void USBDualModeTerm(uint32 ulIndex);
  1179. extern void USBOTGMain(uint32 ulMsTicks);
  1180. extern void USBOTGPollRate(uint32 ulIndex, uint32 ulPollRate);
  1181. extern void USBOTGModeInit(uint32 ulIndex, uint32 ulPollRate,
  1182. void *pHostData, uint32 ulHostDataSize);
  1183. extern void USBOTGModeTerm(uint32 ulIndex);
  1184. extern void USB0OTGModeIntHandler(void);
  1185. extern void USB0DualModeIntHandler(void);
  1186. //*****************************************************************************
  1187. //
  1188. //! USB callback function.
  1189. //!
  1190. //! \param pvCBData is the callback pointer associated with the instance
  1191. //! generating the callback. This is a value provided by the client during
  1192. //! initialization of the instance making the callback.
  1193. //! \param ulEvent is the identifier of the asynchronous event which is being
  1194. //! notified to the client.
  1195. //! \param ulMsgParam is an event-specific parameter.
  1196. //! \param pvMsgData is an event-specific data pointer.
  1197. //!
  1198. //! A function pointer provided to the USB layer by the application
  1199. //! which will be called to notify it of all asynchronous events relating to
  1200. //! data transmission or reception. This callback is used by device class
  1201. //! drivers and host pipe functions.
  1202. //!
  1203. //! \return Returns an event-dependent value.
  1204. //
  1205. //*****************************************************************************
  1206. typedef uint32 (* tUSBCallback)(void *pvCBData, uint32 ulEvent,
  1207. uint32 ulMsgParam,
  1208. void *pvMsgData);
  1209. //*****************************************************************************
  1210. //
  1211. // Base identifiers for groups of USB events. These are used by both the
  1212. // device class drivers and host layer.
  1213. //
  1214. // USB_CLASS_EVENT_BASE is the lowest identifier that should be used for
  1215. // a class-specific event. Individual event bases are defined for each
  1216. // of the supported device class drivers. Events with IDs between
  1217. // USB_EVENT_BASE and USB_CLASS_EVENT_BASE are reserved for stack use.
  1218. //
  1219. //*****************************************************************************
  1220. #define USB_EVENT_BASE 0x0000
  1221. #define USB_CLASS_EVENT_BASE 0x8000
  1222. //*****************************************************************************
  1223. //
  1224. // Event base identifiers for the various device classes supported in host
  1225. // and device modes.
  1226. // The first 0x800 values of a range are reserved for the device specific
  1227. // messages and the second 0x800 values of a range are used for the host
  1228. // specific messages for a given class.
  1229. //
  1230. //*****************************************************************************
  1231. #define USBD_CDC_EVENT_BASE (USB_CLASS_EVENT_BASE + 0)
  1232. #define USBD_HID_EVENT_BASE (USB_CLASS_EVENT_BASE + 0x1000)
  1233. #define USBD_HID_KEYB_EVENT_BASE (USBD_HID_EVENT_BASE + 0x100)
  1234. #define USBD_BULK_EVENT_BASE (USB_CLASS_EVENT_BASE + 0x2000)
  1235. #define USBD_MSC_EVENT_BASE (USB_CLASS_EVENT_BASE + 0x3000)
  1236. #define USBD_AUDIO_EVENT_BASE (USB_CLASS_EVENT_BASE + 0x4000)
  1237. #define USBH_CDC_EVENT_BASE (USBD_CDC_EVENT_BASE + 0x800)
  1238. #define USBH_HID_EVENT_BASE (USBD_HID_EVENT_BASE + 0x800)
  1239. #define USBH_BULK_EVENT_BASE (USBD_BULK_EVENT_BASE + 0x800)
  1240. #define USBH_MSC_EVENT_BASE (USBD_MSC_EVENT_BASE + 0x800)
  1241. #define USBH_AUDIO_EVENT_BASE (USBD_AUDIO_EVENT_BASE + 0x800)
  1242. //*****************************************************************************
  1243. //
  1244. // General events supported by device classes and host pipes.
  1245. //
  1246. //*****************************************************************************
  1247. //
  1248. //! The device is now attached to a USB host and ready to begin sending
  1249. //! and receiving data (used by device classes only).
  1250. //
  1251. #define USB_EVENT_CONNECTED (USB_EVENT_BASE + 0)
  1252. //
  1253. //! The device has been disconnected from the USB host (used by device classes
  1254. //! only).
  1255. //!
  1256. //! Note: Due to a hardware erratum in revision A of LM3S3748, this
  1257. //! event is not posted to self-powered USB devices when they are disconnected
  1258. //! from the USB host.
  1259. //
  1260. #define USB_EVENT_DISCONNECTED (USB_EVENT_BASE + 1)
  1261. //
  1262. //! Data has been received and is in the buffer provided.
  1263. //
  1264. #define USB_EVENT_RX_AVAILABLE (USB_EVENT_BASE + 2)
  1265. //
  1266. //! This event is sent by a lower layer to inquire about the amount of
  1267. //! unprocessed data buffered in the layers above. It is used in cases
  1268. //! where a low level driver needs to ensure that all preceding data has
  1269. //! been processed prior to performing some action or making some notification.
  1270. //! Clients receiving this event should return the number of bytes of data
  1271. //! that are unprocessed or 0 if no outstanding data remains.
  1272. //
  1273. #define USB_EVENT_DATA_REMAINING (USB_EVENT_BASE + 3)
  1274. //
  1275. //! This event is sent by a lower layer supporting DMA to request a buffer in
  1276. //! which the next received packet may be stored. The \e ulMsgValue parameter
  1277. //! indicates the maximum size of packet that can be received in this channel
  1278. //! and \e pvMsgData points to storage which should be written with the
  1279. //! returned buffer pointer. The return value from the callback should be the
  1280. //! size of the buffer allocated (which may be less than the maximum size
  1281. //! passed in \e ulMsgValue if the client knows that fewer bytes are expected
  1282. //! to be received) or 0 if no buffer is being returned.
  1283. //
  1284. #define USB_EVENT_REQUEST_BUFFER (USB_EVENT_BASE + 4)
  1285. //
  1286. //! Data has been sent and acknowledged. If this event is received via the
  1287. //! USB buffer callback, the \e ulMsgValue parameter indicates the number of
  1288. //! bytes from the transmit buffer that have been successfully transmitted
  1289. //! and acknowledged.
  1290. //
  1291. #define USB_EVENT_TX_COMPLETE (USB_EVENT_BASE + 5)
  1292. //
  1293. //! An error has been reported on the channel or pipe. The \e ulMsgValue
  1294. //! parameter indicates the source(s) of the error and is the logical OR
  1295. //! combination of "USBERR_" flags defined below.
  1296. //
  1297. #define USB_EVENT_ERROR (USB_EVENT_BASE + 6)
  1298. //
  1299. //! The bus has entered suspend state.
  1300. //
  1301. #define USB_EVENT_SUSPEND (USB_EVENT_BASE + 7)
  1302. //
  1303. //! The bus has left suspend state.
  1304. //
  1305. #define USB_EVENT_RESUME (USB_EVENT_BASE + 8)
  1306. //
  1307. //! A scheduler event has occurred.
  1308. //
  1309. #define USB_EVENT_SCHEDULER (USB_EVENT_BASE + 9)
  1310. //
  1311. //! A device or host has detected a stall condition.
  1312. //
  1313. #define USB_EVENT_STALL (USB_EVENT_BASE + 10)
  1314. //
  1315. //! The host detected a power fault condition.
  1316. //
  1317. #define USB_EVENT_POWER_FAULT (USB_EVENT_BASE + 11)
  1318. //
  1319. //! The controller has detected a A-Side cable and needs power applied This is
  1320. //! only generated on OTG parts if automatic power control is disabled.
  1321. //
  1322. #define USB_EVENT_POWER_ENABLE (USB_EVENT_BASE + 12)
  1323. //
  1324. //! The controller needs power removed, This is only generated on OTG parts
  1325. //! if automatic power control is disabled.
  1326. //
  1327. #define USB_EVENT_POWER_DISABLE (USB_EVENT_BASE + 13)
  1328. //
  1329. //! Used with pfnDeviceHandler handler function is classes to indicate changes
  1330. //! in the interface number by a class outside the class being accessed.
  1331. //! Typically this is when composite device class is in use.
  1332. //!
  1333. //! The \e pvInstance value should point to an instance of the device being
  1334. //! accessed.
  1335. //!
  1336. //! The \e ulRequest should be USB_EVENT_COMP_IFACE_CHANGE.
  1337. //!
  1338. //! The \e pvRequestData should point to a two byte array where the first value
  1339. //! is the old interface number and the second is the new interface number.
  1340. //
  1341. #define USB_EVENT_COMP_IFACE_CHANGE (USB_EVENT_BASE + 14)
  1342. //
  1343. //! Used with pfnDeviceHandler handler function is classes to indicate changes
  1344. //! in endpoint number by a class outside the class being accessed.
  1345. //! Typically this is when composite device class is in use.
  1346. //!
  1347. //! The \e pvInstance value should point to an instance of the device being
  1348. //! accessed.
  1349. //!
  1350. //! The \e ulRequest should be USB_EVENT_COMP_EP_CHANGE.
  1351. //!
  1352. //! The \e pvRequestData should point to a two byte array where the first value
  1353. //! is the old endpoint number and the second is the new endpoint number. The
  1354. //! endpoint numbers should be exactly as USB specification defines them and
  1355. //! bit 7 set indicates an IN endpoint and bit 7 clear indicates an OUT
  1356. //! endpoint.
  1357. //
  1358. #define USB_EVENT_COMP_EP_CHANGE (USB_EVENT_BASE + 15)
  1359. //
  1360. //! Used with pfnDeviceHandler handler function is classes to indicate changes
  1361. //! in string index number by a class outside the class being accessed.
  1362. //! Typically this is when composite device class is in use.
  1363. //!
  1364. //! The \e pvInstance value should point to an instance of the device being
  1365. //! accessed.
  1366. //!
  1367. //! The \e ulRequest should be USB_EVENT_COMP_STR_CHANGE.
  1368. //!
  1369. //! The \e pvRequestData should point to a two byte array where the first value
  1370. //! is the old string index and the second is the new string index.
  1371. //
  1372. #define USB_EVENT_COMP_STR_CHANGE (USB_EVENT_BASE + 16)
  1373. //
  1374. //! Used with pfnDeviceHandler handler function is classes to allow the device
  1375. //! class to make final adjustments to the configuration descriptor.
  1376. //! This is only used when a device class is used in a composite device class
  1377. //! is in use.
  1378. //!
  1379. //! The \e pvInstance value should point to an instance of the device being
  1380. //! accessed.
  1381. //!
  1382. //! The \e ulRequest should be USB_EVENT_COMP_CONFIG.
  1383. //!
  1384. //! The \e pvRequestData should point to the beginning of the configuration
  1385. //! descriptor for the device instance.
  1386. //
  1387. #define USB_EVENT_COMP_CONFIG (USB_EVENT_BASE + 17)
  1388. //*****************************************************************************
  1389. //
  1390. // Error sources reported via USB_EVENT_ERROR.
  1391. //
  1392. //*****************************************************************************
  1393. //
  1394. //! The host received an invalid PID in a transaction.
  1395. //
  1396. #define USBERR_HOST_IN_PID_ERROR 0x01000000
  1397. //
  1398. //! The host did not receive a response from a device.
  1399. //
  1400. #define USBERR_HOST_IN_NOT_COMP 0x00100000
  1401. //
  1402. //! The host received a stall on an IN endpoint.
  1403. //
  1404. #define USBERR_HOST_IN_STALL 0x00400000
  1405. //
  1406. //! The host detected a CRC or bit-stuffing error (isochronous mode).
  1407. //
  1408. #define USBERR_HOST_IN_DATA_ERROR 0x00080000
  1409. //
  1410. //! The host received NAK on an IN endpoint for longer than the specified
  1411. //! timeout period (interrupt, bulk and control modes).
  1412. //
  1413. #define USBERR_HOST_IN_NAK_TO 0x00080000
  1414. //
  1415. //! The host failed to communicate with a device via an IN endpoint.
  1416. //
  1417. #define USBERR_HOST_IN_ERROR 0x00040000
  1418. //
  1419. //! The host receive FIFO is full.
  1420. //
  1421. #define USBERR_HOST_IN_FIFO_FULL 0x00020000 // RX FIFO full
  1422. //
  1423. //! The host received NAK on an OUT endpoint for longer than the specified
  1424. //! timeout period (bulk, interrupt and control modes).
  1425. //
  1426. #define USBERR_HOST_OUT_NAK_TO 0x00000080
  1427. //
  1428. //! The host did not receive a response from a device (isochronous mode).
  1429. //
  1430. #define USBERR_HOST_OUT_NOT_COMP 0x00000080
  1431. //
  1432. //! The host received a stall on an OUT endpoint.
  1433. //
  1434. #define USBERR_HOST_OUT_STALL 0x00000020
  1435. //
  1436. //! The host failed to communicate with a device via an OUT endpoint.
  1437. //
  1438. #define USBERR_HOST_OUT_ERROR 0x00000004
  1439. //
  1440. //! The host received NAK on endpoint 0 for longer than the configured
  1441. //! timeout.
  1442. //
  1443. #define USBERR_HOST_EP0_NAK_TO 0x00000080
  1444. //
  1445. //! The host failed to communicate with a device via an endpoint zero.
  1446. //
  1447. #define USBERR_HOST_EP0_ERROR 0x00000010
  1448. //
  1449. //! The device detected a CRC error in received data.
  1450. //
  1451. #define USBERR_DEV_RX_DATA_ERROR 0x00080000
  1452. //
  1453. //! The device was unable to receive a packet from the host since the receive
  1454. //! FIFO is full.
  1455. //
  1456. #define USBERR_DEV_RX_OVERRUN 0x00040000
  1457. //
  1458. //! The device receive FIFO is full.
  1459. //
  1460. #define USBERR_DEV_RX_FIFO_FULL 0x00020000 // RX FIFO full
  1461. //*****************************************************************************
  1462. //
  1463. // Close the general_usblib_api Doxygen group.
  1464. //! @}
  1465. //
  1466. //*****************************************************************************
  1467. //*****************************************************************************
  1468. //
  1469. //! \addtogroup usblib_buffer_api
  1470. //! @{
  1471. //
  1472. //*****************************************************************************
  1473. //*****************************************************************************
  1474. //
  1475. //! A function pointer type which describes either a class driver packet read
  1476. //! or packet write function (both have the same prototype) to the USB
  1477. //! buffer object.
  1478. //
  1479. //*****************************************************************************
  1480. typedef uint32 (* tUSBPacketTransfer)(void *pvHandle,
  1481. uint8 *pcData,
  1482. uint32 ulLength,
  1483. tBoolean bLast);
  1484. //*****************************************************************************
  1485. //
  1486. //! A function pointer type which describes either a class driver transmit
  1487. //! or receive packet available function (both have the same prototype) to the
  1488. //! USB buffer object.
  1489. //
  1490. //*****************************************************************************
  1491. typedef uint32 (* tUSBPacketAvailable)(void *pvHandle);
  1492. //*****************************************************************************
  1493. //
  1494. //! The number of bytes of workspace that each USB buffer object requires.
  1495. //! This workspace memory is provided to the buffer on USBBufferInit() in
  1496. //! the \e pvWorkspace field of the \e tUSBBuffer structure.
  1497. //
  1498. //*****************************************************************************
  1499. #define USB_BUFFER_WORKSPACE_SIZE 16
  1500. //*****************************************************************************
  1501. //
  1502. //! The structure used by the application to initialize a buffer object that
  1503. //! will provide buffered access to either a transmit or receive channel.
  1504. //
  1505. //*****************************************************************************
  1506. typedef struct
  1507. {
  1508. //
  1509. //! This field sets the mode of the buffer. If TRUE, the buffer
  1510. //! operates as a transmit buffer and supports calls to USBBufferWrite
  1511. //! by the client. If FALSE, the buffer operates as a receive buffer
  1512. //! and supports calls to USBBufferRead.
  1513. //
  1514. tBoolean bTransmitBuffer;
  1515. //
  1516. //! A pointer to the callback function which will be called to notify
  1517. //! the application of all asynchronous events related to the operation
  1518. //! of the buffer.
  1519. //
  1520. tUSBCallback pfnCallback;
  1521. //
  1522. //! A pointer that the buffer will pass back to the client in the
  1523. //! first parameter of all callbacks related to this instance.
  1524. //
  1525. void *pvCBData;
  1526. //
  1527. //! The function which should be called to transmit a packet of data
  1528. //! in transmit mode or receive a packet in receive mode.
  1529. //
  1530. tUSBPacketTransfer pfnTransfer;
  1531. //
  1532. //! The function which should be called to determine if the endpoint is
  1533. //! ready to accept a new packet for transmission in transmit mode or
  1534. //! to determine the size of the buffer required to read a packet in
  1535. //! receive mode.
  1536. //
  1537. tUSBPacketAvailable pfnAvailable;
  1538. //
  1539. //! The handle to pass to the low level function pointers
  1540. //! provided in the pfnTransfer and pfnAvailable members. For USB device
  1541. //! use, this is the psDevice parameter required by the relevant device
  1542. //! class driver APIs. For USB host use, this is the pipe identifier
  1543. //! returned by USBHCDPipeAlloc.
  1544. //
  1545. void *pvHandle;
  1546. //
  1547. //! A pointer to memory to be used as the ring buffer for this
  1548. //! instance.
  1549. //
  1550. uint8 *pcBuffer;
  1551. //
  1552. //! The size, in bytes, of the buffer pointed to by pcBuffer.
  1553. //
  1554. uint32 ulBufferSize;
  1555. //
  1556. //! A pointer to USB_BUFFER_WORKSPACE_SIZE bytes of RAM that the buffer
  1557. //! object can use for workspace.
  1558. //
  1559. void *pvWorkspace;
  1560. }
  1561. tUSBBuffer;
  1562. //*****************************************************************************
  1563. //
  1564. //! The structure used for encapsulating all the items associated with a
  1565. //! ring buffer.
  1566. //
  1567. //*****************************************************************************
  1568. typedef struct
  1569. {
  1570. //
  1571. //! The ring buffer size.
  1572. //
  1573. uint32 ulSize;
  1574. //
  1575. //! The ring buffer write index.
  1576. //
  1577. volatile uint32 ulWriteIndex;
  1578. //
  1579. //! The ring buffer read index.
  1580. //
  1581. volatile uint32 ulReadIndex;
  1582. //
  1583. //! The ring buffer.
  1584. //
  1585. uint8 *pucBuf;
  1586. }
  1587. tUSBRingBufObject;
  1588. //*****************************************************************************
  1589. //
  1590. // USB buffer API function prototypes.
  1591. //
  1592. //*****************************************************************************
  1593. extern const tUSBBuffer *USBBufferInit(const tUSBBuffer *psBuffer);
  1594. extern void USBBufferInfoGet(const tUSBBuffer *psBuffer,
  1595. tUSBRingBufObject *psRingBuf);
  1596. extern void *USBBufferCallbackDataSet(tUSBBuffer *psBuffer, void *pvCBData);
  1597. extern uint32 USBBufferWrite(const tUSBBuffer *psBuffer,
  1598. const uint8 *pucData,
  1599. uint32 ulLength);
  1600. extern void USBBufferDataWritten(const tUSBBuffer *psBuffer,
  1601. uint32 ulLength);
  1602. extern void USBBufferDataRemoved(const tUSBBuffer *psBuffer,
  1603. uint32 ulLength);
  1604. extern void USBBufferFlush(const tUSBBuffer *psBuffer);
  1605. extern uint32 USBBufferRead(const tUSBBuffer *psBuffer,
  1606. uint8 *pucData,
  1607. uint32 ulLength);
  1608. extern uint32 USBBufferDataAvailable(const tUSBBuffer *psBuffer);
  1609. extern uint32 USBBufferSpaceAvailable(const tUSBBuffer *psBuffer);
  1610. extern uint32 USBBufferEventCallback(void *pvCBData,
  1611. uint32 ulEvent,
  1612. uint32 ulMsgValue,
  1613. void *pvMsgData);
  1614. extern tBoolean USBRingBufFull(tUSBRingBufObject *ptUSBRingBuf);
  1615. extern tBoolean USBRingBufEmpty(tUSBRingBufObject *ptUSBRingBuf);
  1616. extern void USBRingBufFlush(tUSBRingBufObject *ptUSBRingBuf);
  1617. extern uint32 USBRingBufUsed(tUSBRingBufObject *ptUSBRingBuf);
  1618. extern uint32 USBRingBufFree(tUSBRingBufObject *ptUSBRingBuf);
  1619. extern uint32 USBRingBufContigUsed(tUSBRingBufObject *ptUSBRingBuf);
  1620. extern uint32 USBRingBufContigFree(tUSBRingBufObject *ptUSBRingBuf);
  1621. extern uint32 USBRingBufSize(tUSBRingBufObject *ptUSBRingBuf);
  1622. extern uint8 USBRingBufReadOne(tUSBRingBufObject *ptUSBRingBuf);
  1623. extern void USBRingBufRead(tUSBRingBufObject *ptUSBRingBuf,
  1624. uint8 *pucData, uint32 ulLength);
  1625. extern void USBRingBufWriteOne(tUSBRingBufObject *ptUSBRingBuf,
  1626. uint8 ucData);
  1627. extern void USBRingBufWrite(tUSBRingBufObject *ptUSBRingBuf,
  1628. const uint8 *pucData,
  1629. uint32 ulLength);
  1630. extern void USBRingBufAdvanceWrite(tUSBRingBufObject *ptUSBRingBuf,
  1631. uint32 ulNumBytes);
  1632. extern void USBRingBufAdvanceRead(tUSBRingBufObject *ptUSBRingBuf,
  1633. uint32 ulNumBytes);
  1634. extern void USBRingBufInit(tUSBRingBufObject *ptUSBRingBuf,
  1635. uint8 *pucBuf, uint32 ulSize);
  1636. //*****************************************************************************
  1637. //
  1638. // Close the Doxygen group.
  1639. //! @}
  1640. //
  1641. //*****************************************************************************
  1642. //*****************************************************************************
  1643. //
  1644. // Mark the end of the C bindings section for C++ compilers.
  1645. //
  1646. //*****************************************************************************
  1647. #ifdef __cplusplus
  1648. }
  1649. #endif
  1650. #endif // __USBLIB_H__