usbdenum.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113
  1. //*****************************************************************************
  2. //
  3. // usbenum.c - Enumeration code to handle all endpoint zero traffic.
  4. //
  5. // Copyright (c) 2007-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 AM1808 StarterWare USB Library and reused from revision 6288
  22. // of the Stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_usb.h"
  26. #include "hw_types.h"
  27. #include "debug.h"
  28. #include "usb.h"
  29. #include "usblib.h"
  30. #include "usbdevice.h"
  31. #include "usbdevicepriv.h"
  32. #include "usblibpriv.h"
  33. #include "delay.h"
  34. #include "interrupt.h"
  35. //*****************************************************************************
  36. //
  37. // External prototypes.
  38. //
  39. //*****************************************************************************
  40. //extern tUSBMode g_eUSBMode;
  41. static tUSBMode g_eUSBMode = USB_MODE_DEVICE;
  42. //*****************************************************************************
  43. //
  44. // USB instance Object
  45. //
  46. //*****************************************************************************
  47. extern tUSBInstanceObject g_USBInstance[];
  48. //*****************************************************************************
  49. //
  50. // Local functions prototypes.
  51. //
  52. //*****************************************************************************
  53. static void USBDGetStatus(void *pvInstance, tUSBRequest *pUSBRequest,
  54. unsigned int ulIndex);
  55. static void USBDClearFeature(void *pvInstance, tUSBRequest *pUSBRequest,
  56. unsigned int ulIndex);
  57. static void USBDSetFeature(void *pvInstance, tUSBRequest *pUSBRequest,
  58. unsigned int ulIndex);
  59. static void USBDSetAddress(void *pvInstance, tUSBRequest *pUSBRequest,
  60. unsigned int ulIndex);
  61. static void USBDGetDescriptor(void *pvInstance, tUSBRequest *pUSBRequest,
  62. unsigned int ulIndex);
  63. static void USBDSetDescriptor(void *pvInstance, tUSBRequest *pUSBRequest,
  64. unsigned int ulIndex);
  65. static void USBDGetConfiguration(void *pvInstance,
  66. tUSBRequest *pUSBRequest, unsigned int ulIndex);
  67. static void USBDSetConfiguration(void *pvInstance,
  68. tUSBRequest *pUSBRequest, unsigned int ulIndex);
  69. static void USBDGetInterface(void *pvInstance, tUSBRequest *pUSBRequest,
  70. unsigned int ulIndex);
  71. static void USBDSetInterface(void *pvInstance, tUSBRequest *pUSBRequest,
  72. unsigned int ulIndex);
  73. static void USBDSyncFrame(void *pvInstance, tUSBRequest *pUSBRequest,
  74. unsigned int ulIndex);
  75. static void USBDEP0StateTx(unsigned int ulIndex);
  76. static void USBDEP0StateTxConfig(unsigned int ulIndex);
  77. static int USBDStringIndexFromRequest(unsigned short usLang,
  78. unsigned short usIndex, unsigned int ulIndex);
  79. //*****************************************************************************
  80. //
  81. //! \addtogroup device_api
  82. //! @{
  83. //
  84. //*****************************************************************************
  85. //*****************************************************************************
  86. //
  87. //! The default USB endpoint FIFO configuration structure. This structure
  88. //! contains definitions to set all USB FIFOs into single buffered mode with
  89. //! no DMA use. Each endpoint's FIFO is sized to hold the largest maximum
  90. //! packet size for any interface alternate setting in the current
  91. //! configuration descriptor. A pointer to this structure may be passed in the
  92. //! psFIFOConfig field of the tDeviceInfo structure passed to USBCDCInit if the
  93. //! application does not require any special handling of the USB controller
  94. //! FIFO.
  95. //
  96. //*****************************************************************************
  97. const tFIFOConfig g_sUSBDefaultFIFOConfig =
  98. {
  99. {
  100. { 1, false, 0 },
  101. { 1, false, 0 },
  102. { 1, false, 0 },
  103. { 1, false, 0 },
  104. { 1, false, 0 },
  105. { 1, false, 0 },
  106. { 1, false, 0 },
  107. { 1, false, 0 },
  108. { 1, false, 0 },
  109. { 1, false, 0 },
  110. { 1, false, 0 },
  111. { 1, false, 0 },
  112. { 1, false, 0 },
  113. { 1, false, 0 },
  114. { 1, false, 0 }
  115. },
  116. {
  117. { 1, false, 0 },
  118. { 1, false, 0 },
  119. { 1, false, 0 },
  120. { 1, false, 0 },
  121. { 1, false, 0 },
  122. { 1, false, 0 },
  123. { 1, false, 0 },
  124. { 1, false, 0 },
  125. { 1, false, 0 },
  126. { 1, false, 0 },
  127. { 1, false, 0 },
  128. { 1, false, 0 },
  129. { 1, false, 0 },
  130. { 1, false, 0 },
  131. { 1, false, 0 }
  132. },
  133. };
  134. //*****************************************************************************
  135. //
  136. // Indices into the ucHalt array to select the IN or OUT endpoint group.
  137. //
  138. //*****************************************************************************
  139. #define HALT_EP_IN 0
  140. #define HALT_EP_OUT 1
  141. //*****************************************************************************
  142. //
  143. // The states for endpoint zero during enumeration.
  144. //
  145. //*****************************************************************************
  146. typedef enum
  147. {
  148. //
  149. // The USB device is waiting on a request from the host controller on
  150. // endpoint zero.
  151. //
  152. USB_STATE_IDLE,
  153. //
  154. // The USB device is sending data back to the host due to an IN request.
  155. //
  156. USB_STATE_TX,
  157. //
  158. // The USB device is sending the configuration descriptor back to the host
  159. // due to an IN request.
  160. //
  161. USB_STATE_TX_CONFIG,
  162. //
  163. // The USB device is receiving data from the host due to an OUT
  164. // request from the host.
  165. //
  166. USB_STATE_RX,
  167. //
  168. // The USB device has completed the IN or OUT request and is now waiting
  169. // for the host to acknowledge the end of the IN/OUT transaction. This
  170. // is the status phase for a USB control transaction.
  171. //
  172. USB_STATE_STATUS,
  173. //
  174. // This endpoint has signaled a stall condition and is waiting for the
  175. // stall to be acknowledged by the host controller.
  176. //
  177. USB_STATE_STALL
  178. }
  179. tEP0State;
  180. //*****************************************************************************
  181. //
  182. // Define the max packet size for endpoint zero.
  183. //
  184. //*****************************************************************************
  185. #define EP0_MAX_PACKET_SIZE 64
  186. //*****************************************************************************
  187. //
  188. // This is a flag used with g_sUSBDeviceState.ulDevAddress to indicate that a
  189. // device address change is pending.
  190. //
  191. //*****************************************************************************
  192. #define DEV_ADDR_PENDING 0x80000000
  193. //*****************************************************************************
  194. //
  195. // This label defines the default configuration number to use after a bus
  196. // reset. This may be overridden by calling USBDCDSetDefaultConfiguration()
  197. // during processing of the device reset handler if required.
  198. //
  199. //*****************************************************************************
  200. #define DEFAULT_CONFIG_ID 1
  201. //*****************************************************************************
  202. //
  203. // This label defines the number of milliseconds that the remote wake up signal
  204. // must remain asserted before removing it. Section 7.1.7.7 of the USB 2.0 spec
  205. // states that "the remote wake up device must hold the resume signaling for at
  206. // least 1ms but for no more than 15ms" so 10mS seems a reasonable choice.
  207. //
  208. //*****************************************************************************
  209. #define REMOTE_WAKEUP_PULSE_MS 10
  210. //*****************************************************************************
  211. //
  212. // This label defines the number of milliseconds between the point where we
  213. // assert the remote wake up signal and calling the client back to tell it that
  214. // bus operation has been resumed. This value is based on the timings provided
  215. // in section 7.1.7.7 of the USB 2.0 specification which indicates that the host
  216. // (which takes over resume signaling when the device's initial signal is
  217. // detected) must hold the resume signaling for at least 20mS.
  218. //
  219. //*****************************************************************************
  220. #define REMOTE_WAKEUP_READY_MS 20
  221. //*****************************************************************************
  222. //
  223. // The buffer for reading data coming into EP0
  224. //
  225. //*****************************************************************************
  226. static unsigned char g_pucDataBufferIn[EP0_MAX_PACKET_SIZE];
  227. //*****************************************************************************
  228. //
  229. // The USB controller device information.
  230. //
  231. //*****************************************************************************
  232. typedef struct
  233. {
  234. //
  235. // The device information for the USB device.
  236. //
  237. tDeviceInfo *psInfo;
  238. //
  239. // The instance data for the USB device.
  240. //
  241. void *pvInstance;
  242. //
  243. // The current state of endpoint zero.
  244. //
  245. volatile tEP0State eEP0State;
  246. //
  247. // The devices current address, this also has a change pending bit in the
  248. // MSB of this value specified by DEV_ADDR_PENDING.
  249. //
  250. volatile unsigned int ulDevAddress;
  251. //
  252. // This holds the current active configuration for this device.
  253. //
  254. unsigned int ulConfiguration;
  255. //
  256. // This holds the configuration id that will take effect after a reset.
  257. //
  258. unsigned int ulDefaultConfiguration;
  259. //
  260. // This holds the current alternate interface for this device.
  261. //
  262. unsigned char pucAltSetting[USB_MAX_INTERFACES_PER_DEVICE];
  263. //
  264. // This is the pointer to the current data being sent out or received
  265. // on endpoint zero.
  266. //
  267. unsigned char *pEP0Data;
  268. //
  269. // This is the number of bytes that remain to be sent from or received
  270. // into the g_sUSBDeviceState.pEP0Data data buffer.
  271. //
  272. volatile unsigned int ulEP0DataRemain;
  273. //
  274. // The amount of data being sent/received due to a custom request.
  275. //
  276. unsigned int ulOUTDataSize;
  277. //
  278. // Holds the current device status.
  279. //
  280. unsigned char ucStatus;
  281. //
  282. // Holds the endpoint status for the HALT condition. This array is sized
  283. // to hold halt status for all IN and OUT endpoints.
  284. //
  285. unsigned char ucHalt[2][NUM_USB_EP - 1];
  286. //
  287. // Holds the configuration descriptor section number currently being sent
  288. // to the host.
  289. //
  290. unsigned char ucConfigSection;
  291. //
  292. // Holds the offset within the configuration descriptor section currently
  293. // being sent to the host.
  294. //
  295. unsigned char ucSectionOffset;
  296. //
  297. // Holds the index of the configuration that we are currently sending back
  298. // to the host.
  299. //
  300. unsigned char ucConfigIndex;
  301. //
  302. // This flag is set to true if the client has called USBDPowerStatusSet
  303. // and tells the USB library not to try to determine the current power
  304. // status from the configuration descriptor.
  305. //
  306. tBoolean bPwrSrcSet;
  307. //
  308. // This flag indicates whether or not remote wake up signaling is in
  309. // progress.
  310. //
  311. tBoolean bRemoteWakeup;
  312. //
  313. // During remote wake up signaling, this counter is used to track the
  314. // number of milliseconds since the signaling was initiated.
  315. //
  316. unsigned char ucRemoteWakeupCount;
  317. }
  318. tDeviceInstance;
  319. tDeviceInstance g_psUSBDevice[USB_NUM_INSTANCE];
  320. //*****************************************************************************
  321. //
  322. // Function table to handle standard requests.
  323. //
  324. //*****************************************************************************
  325. static const tStdRequest g_psUSBDStdRequests[] =
  326. {
  327. USBDGetStatus,
  328. USBDClearFeature,
  329. 0,
  330. USBDSetFeature,
  331. 0,
  332. USBDSetAddress,
  333. USBDGetDescriptor,
  334. USBDSetDescriptor,
  335. USBDGetConfiguration,
  336. USBDSetConfiguration,
  337. USBDGetInterface,
  338. USBDSetInterface,
  339. USBDSyncFrame
  340. };
  341. //*****************************************************************************
  342. //
  343. // Functions accessible by USBLIB clients.
  344. //
  345. //*****************************************************************************
  346. //*****************************************************************************
  347. //
  348. //! Initialize the USB library device control driver for a given hardware
  349. //! controller.
  350. //!
  351. //! \param ulIndex is the index of the USB controller which is to be
  352. //! initialized.
  353. //! \param psDevice is a pointer to a structure containing information that
  354. //! the USB library requires to support operation of this application's
  355. //! device. The structure contains event handler callbacks and pointers to the
  356. //! various standard descriptors that the device wishes to publish to the
  357. //! host.
  358. //!
  359. //! This function must be called by any application which wishes to operate
  360. //! as a USB device. It initializes the USB device control driver for the
  361. //! given controller and saves the device information for future use. Prior to
  362. //! returning from this function, the device is connected to the USB bus.
  363. //! Following return, the caller can expect to receive a callback to the
  364. //! supplied <tt>pfnResetHandler</tt> function when a host connects to the
  365. //! device.
  366. //!
  367. //! The device information structure passed in \e psDevice must remain
  368. //! unchanged between this call and any matching call to USBDCDTerm() since
  369. //! it is not copied by the USB library.
  370. //!
  371. //! \return None.
  372. //
  373. //*****************************************************************************
  374. void
  375. USBDCDInit(unsigned int ulIndex, tDeviceInfo *psDevice)
  376. {
  377. const tConfigHeader *psHdr;
  378. const tConfigDescriptor *psDesc;
  379. //
  380. // Check the arguments.
  381. //
  382. ASSERT(ulIndex == 0);
  383. ASSERT(psDevice != 0);
  384. //
  385. // Should not call this if the stack is in host mode.
  386. //
  387. ASSERT(g_eUSBMode != USB_MODE_HOST)
  388. //
  389. // Initialize a couple of fields in the device state structure.
  390. //
  391. g_psUSBDevice[ulIndex].ulConfiguration = DEFAULT_CONFIG_ID;
  392. g_psUSBDevice[ulIndex].ulDefaultConfiguration = DEFAULT_CONFIG_ID;
  393. //
  394. // Remember the device information pointer.
  395. //
  396. g_psUSBDevice[ulIndex].psInfo = psDevice;
  397. g_psUSBDevice[ulIndex].pvInstance = psDevice->pvInstance;
  398. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_IDLE;
  399. //
  400. // If no mode is set then make the mode become device mode.
  401. //
  402. if(g_eUSBMode == USB_MODE_NONE)
  403. {
  404. g_eUSBMode = USB_MODE_DEVICE;
  405. }
  406. //
  407. // Only do hardware update if the stack is in Device mode, do not touch the
  408. // hardware for OTG mode operation.
  409. //
  410. if(g_eUSBMode == USB_MODE_DEVICE)
  411. {
  412. //
  413. // Enable Clocking to the USB controller.
  414. //
  415. USBModuleClkEnable(ulIndex, g_USBInstance[ulIndex].uiBaseAddr);
  416. USBReset(g_USBInstance[ulIndex].uiSubBaseAddr);
  417. //
  418. // Turn on USB Phy clock.
  419. //
  420. UsbPhyOn(ulIndex);
  421. }
  422. //
  423. // Only do hardware update if the stack is in Device mode, do not touch the
  424. // hardware for OTG mode operation.
  425. //
  426. if(g_eUSBMode == USB_MODE_DEVICE)
  427. {
  428. //
  429. // Ask for the interrupt status. As a side effect, this clears all
  430. // pending USB interrupts.
  431. //
  432. USBIntStatusControl(g_USBInstance[ulIndex].uiBaseAddr);
  433. USBIntStatusEndpoint(g_USBInstance[ulIndex].uiBaseAddr);
  434. if(USB_REV_AM1808 == USBVersionGet())
  435. {
  436. USBClearOtgIntr(g_USBInstance[ulIndex].uiSubBaseAddr);
  437. }
  438. USBEnableOtgIntr(g_USBInstance[ulIndex].uiSubBaseAddr);
  439. //
  440. // Enable USB Interrupts.
  441. //
  442. USBIntEnableControl(g_USBInstance[ulIndex].uiBaseAddr, USB_INTCTRL_RESET |
  443. USB_INTCTRL_DISCONNECT |
  444. USB_INTCTRL_RESUME |
  445. USB_INTCTRL_SUSPEND |
  446. USB_INTCTRL_SOF);
  447. USBIntEnableEndpoint(g_USBInstance[ulIndex].uiBaseAddr, USB_INTEP_ALL);
  448. }
  449. //
  450. // Get a pointer to the default configuration descriptor.
  451. //
  452. psHdr = psDevice->ppConfigDescriptors[
  453. g_psUSBDevice[ulIndex].ulDefaultConfiguration - 1];
  454. psDesc = (const tConfigDescriptor *)(psHdr->psSections[0]->pucData);
  455. //
  456. // Default to the state where remote wake up is disabled.
  457. //
  458. g_psUSBDevice[ulIndex].ucStatus = 0;
  459. g_psUSBDevice[ulIndex].bRemoteWakeup = false;
  460. //
  461. // Determine the self- or bus-powered state based on the flags the
  462. // user provided.
  463. //
  464. g_psUSBDevice[ulIndex].bPwrSrcSet = false;
  465. if((psDesc->bmAttributes & USB_CONF_ATTR_PWR_M) == USB_CONF_ATTR_SELF_PWR)
  466. {
  467. g_psUSBDevice[ulIndex].ucStatus |= USB_STATUS_SELF_PWR;
  468. }
  469. else
  470. {
  471. g_psUSBDevice[ulIndex].ucStatus &= ~USB_STATUS_SELF_PWR;
  472. }
  473. //
  474. // Only do hardware update if the stack is in Device mode, do not touch the
  475. // hardware for OTG mode operation.
  476. //
  477. if(g_eUSBMode == USB_MODE_DEVICE)
  478. {
  479. //
  480. // Make sure we disconnect from the host for a while. This ensures
  481. // that the host will enumerate us even if we were previously
  482. // connected to the bus.
  483. //
  484. USBDevDisconnect(g_USBInstance[ulIndex].uiBaseAddr);
  485. //
  486. // Wait about 100mS.
  487. //
  488. delay(100);
  489. //
  490. // Attach the device using the soft connect.
  491. //
  492. USBDevConnect(g_USBInstance[ulIndex].uiBaseAddr);
  493. //
  494. // Enable the USB interrupt.
  495. //
  496. #ifdef _TMS320C6X
  497. /* No DSP API to enable USB0 event */
  498. #else
  499. IntSystemEnable(g_USBInstance[ulIndex].uiInterruptNum);
  500. #endif
  501. }
  502. }
  503. //*****************************************************************************
  504. //
  505. //! Free the USB library device control driver for a given hardware controller.
  506. //!
  507. //! \param ulIndex is the index of the USB controller which is to be
  508. //! freed.
  509. //!
  510. //! This function should be called by an application if it no longer requires
  511. //! the use of a given USB controller to support its operation as a USB device.
  512. //! It frees the controller for use by another client.
  513. //!
  514. //! It is the caller's responsibility to remove its device from the USB bus
  515. //! prior to calling this function.
  516. //!
  517. //! \return None.
  518. //
  519. //*****************************************************************************
  520. void
  521. USBDCDTerm(unsigned int ulIndex)
  522. {
  523. //
  524. // Check the arguments.
  525. //
  526. ASSERT(ulIndex == 0);
  527. g_psUSBDevice[ulIndex].psInfo = (tDeviceInfo *)0;
  528. g_psUSBDevice[ulIndex].pvInstance = 0;
  529. //
  530. // Disable the USB interrupts.
  531. //
  532. #ifdef _TMS320C6X
  533. /* No DSP API to disable USB0 event */
  534. #else
  535. IntSystemDisable(g_USBInstance[ulIndex].uiInterruptNum);
  536. #endif
  537. USBIntDisableControl(g_USBInstance[ulIndex].uiBaseAddr, USB_INTCTRL_ALL);
  538. USBIntDisableEndpoint(g_USBInstance[ulIndex].uiBaseAddr, USB_INTEP_ALL);
  539. //
  540. // Detach the device using the soft connect.
  541. //
  542. USBDevDisconnect(g_USBInstance[ulIndex].uiBaseAddr);
  543. //
  544. // Clear any pending interrupts.
  545. //
  546. USBIntStatusControl(g_USBInstance[ulIndex].uiBaseAddr);
  547. USBIntStatusEndpoint(g_USBInstance[ulIndex].uiBaseAddr);
  548. if(USB_REV_AM1808 == USBVersionGet())
  549. {
  550. USBClearOtgIntr(g_USBInstance[ulIndex].uiSubBaseAddr);
  551. }
  552. //
  553. // Turn off USB Phy clock.
  554. //
  555. UsbPhyOff(ulIndex);
  556. //
  557. // Disable the USB peripheral
  558. //
  559. USBModuleClkDisable(ulIndex, g_USBInstance[ulIndex].uiBaseAddr);
  560. }
  561. //*****************************************************************************
  562. //
  563. //! This function starts the request for data from the host on endpoint zero.
  564. //!
  565. //! \param ulIndex is the index of the USB controller from which the data
  566. //! is being requested.
  567. //! \param pucData is a pointer to the buffer to fill with data from the USB
  568. //! host.
  569. //! \param ulSize is the size of the buffer or data to return from the USB
  570. //! host.
  571. //!
  572. //! This function handles retrieving data from the host when a custom command
  573. //! has been issued on endpoint zero. If the application needs notification
  574. //! when the data has been received,
  575. //! <tt>tDeviceInfo.sCallbacks.pfnDataReceived</tt> should contain valid
  576. //! function pointer. In nearly all cases this is necessary because the caller
  577. //! of this function would likely need to know that the data requested was
  578. //! received.
  579. //!
  580. //! \return None.
  581. //
  582. //*****************************************************************************
  583. void
  584. USBDCDRequestDataEP0(unsigned int ulIndex, unsigned char *pucData,
  585. unsigned int ulSize)
  586. {
  587. ASSERT(ulIndex == 0);
  588. //
  589. // Enter the RX state on end point 0.
  590. //
  591. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_RX;
  592. //
  593. // Save the pointer to the data.
  594. //
  595. g_psUSBDevice[ulIndex].pEP0Data = pucData;
  596. //
  597. // Location to save the current number of bytes received.
  598. //
  599. g_psUSBDevice[ulIndex].ulOUTDataSize = ulSize;
  600. //
  601. // Bytes remaining to be received.
  602. //
  603. g_psUSBDevice[ulIndex].ulEP0DataRemain = ulSize;
  604. }
  605. //*****************************************************************************
  606. //
  607. //! This function requests transfer of data to the host on endpoint zero.
  608. //!
  609. //! \param ulIndex is the index of the USB controller which is to be used to
  610. //! send the data.
  611. //! \param pucData is a pointer to the buffer to send via endpoint zero.
  612. //! \param ulSize is the amount of data to send in bytes.
  613. //!
  614. //! This function handles sending data to the host when a custom command is
  615. //! issued or non-standard descriptor has been requested on endpoint zero. If
  616. //! the application needs notification when this is complete,
  617. //! <tt>tDeviceInfo.sCallbacks.pfnDataSent</tt> should contain a valid function
  618. //! pointer. This callback could be used to free up the buffer passed into
  619. //! this function in the \e pucData parameter. The contents of the \e pucData
  620. //! buffer must remain unchanged until the <tt>pfnDataSent</tt> callback is
  621. //! received.
  622. //!
  623. //! \return None.
  624. //
  625. //*****************************************************************************
  626. void
  627. USBDCDSendDataEP0(unsigned int ulIndex, unsigned char *pucData,
  628. unsigned int ulSize)
  629. {
  630. ASSERT(ulIndex == 0);
  631. //
  632. // Return the externally provided device descriptor.
  633. //
  634. g_psUSBDevice[ulIndex].pEP0Data = pucData;
  635. //
  636. // The size of the device descriptor is in the first byte.
  637. //
  638. g_psUSBDevice[ulIndex].ulEP0DataRemain = ulSize;
  639. //
  640. // Save the total size of the data sent.
  641. //
  642. g_psUSBDevice[ulIndex].ulOUTDataSize = ulSize;
  643. //
  644. // Now in the transmit data state.
  645. //
  646. USBDEP0StateTx(ulIndex);
  647. }
  648. //*****************************************************************************
  649. //
  650. //! This function sets the default configuration for the device.
  651. //!
  652. //! \param ulIndex is the index of the USB controller whose default
  653. //! configuration is to be set.
  654. //! \param ulDefaultConfig is the configuration identifier (byte 6 of the
  655. //! standard configuration descriptor) which is to be presented to the host
  656. //! as the default configuration in cases where the configuration descriptor is
  657. //! queried prior to any specific configuration being set.
  658. //!
  659. //! This function allows a device to override the default configuration
  660. //! descriptor that will be returned to a host whenever it is queried prior
  661. //! to a specific configuration having been set. The parameter passed must
  662. //! equal one of the configuration identifiers found in the
  663. //! <tt>ppConfigDescriptors</tt> array for the device.
  664. //!
  665. //! If this function is not called, the USB library will return the first
  666. //! configuration in the <tt>ppConfigDescriptors</tt> array as the default
  667. //! configuration.
  668. //!
  669. //! \note The USB device stack assumes that the configuration IDs (byte 6 of
  670. //! the configuration descriptor, <tt>bConfigurationValue</tt>) stored within
  671. //! the configuration descriptor array, <tt>ppConfigDescriptors</tt>,
  672. //! are equal to the array index + 1. In other words, the first entry in the
  673. //! array must contain a descriptor with <tt>bConfigurationValue</tt> 1, the
  674. //! second must have <tt>bConfigurationValue</tt> 2 and so on.
  675. //!
  676. //! \return None.
  677. //
  678. //*****************************************************************************
  679. void
  680. USBDCDSetDefaultConfiguration(unsigned int ulIndex,
  681. unsigned int ulDefaultConfig)
  682. {
  683. ASSERT(ulIndex == 0);
  684. g_psUSBDevice[ulIndex].ulDefaultConfiguration = ulDefaultConfig;
  685. }
  686. //*****************************************************************************
  687. //
  688. //! This function generates a stall condition on endpoint zero.
  689. //!
  690. //! \param ulIndex is the index of the USB controller whose endpoint zero is to
  691. //! be stalled.
  692. //!
  693. //! This function is typically called to signal an error condition to the host
  694. //! when an unsupported request is received by the device. It should be
  695. //! called from within the callback itself (in interrupt context) and not
  696. //! deferred until later since it affects the operation of the endpoint zero
  697. //! state machine in the USB library.
  698. //!
  699. //! \return None.
  700. //
  701. //*****************************************************************************
  702. void
  703. USBDCDStallEP0(unsigned int ulIndex)
  704. {
  705. ASSERT(ulIndex == 0);
  706. //
  707. // Stall the endpoint in question.
  708. //
  709. USBDevEndpointStall(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, USB_EP_DEV_OUT);
  710. //
  711. // Enter the stalled state.
  712. //
  713. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_STALL;
  714. }
  715. //*****************************************************************************
  716. //
  717. //! Reports the device power status (bus- or self-powered) to the library.
  718. //!
  719. //! \param ulIndex is the index of the USB controller whose device power
  720. //! status is being reported.
  721. //! \param ucPower indicates the current power status, either \b
  722. //! USB_STATUS_SELF_PWR or \b USB_STATUS_BUS_PWR.
  723. //!
  724. //! Applications which support switching between bus- or self-powered
  725. //! operation should call this function whenever the power source changes
  726. //! to indicate the current power status to the USB library. This information
  727. //! is required by the library to allow correct responses to be provided when
  728. //! the host requests status from the device.
  729. //!
  730. //! \return None.
  731. //
  732. //*****************************************************************************
  733. void
  734. USBDCDPowerStatusSet(unsigned int ulIndex, unsigned char ucPower)
  735. {
  736. //
  737. // Check for valid parameters.
  738. //
  739. ASSERT((ucPower == USB_STATUS_BUS_PWR) ||
  740. (ucPower == USB_STATUS_SELF_PWR));
  741. ASSERT(ulIndex == 0);
  742. //
  743. // Update the device status with the new power status flag.
  744. //
  745. g_psUSBDevice[ulIndex].bPwrSrcSet = true;
  746. g_psUSBDevice[ulIndex].ucStatus &= ~USB_STATUS_PWR_M;
  747. g_psUSBDevice[ulIndex].ucStatus |= ucPower;
  748. }
  749. //*****************************************************************************
  750. //
  751. //! Requests a remote wake up to resume communication when in suspended state.
  752. //!
  753. //! \param ulIndex is the index of the USB controller that will request
  754. //! a bus wake up.
  755. //!
  756. //! When the bus is suspended, an application which supports remote wake up
  757. //! (advertised to the host via the configuration descriptor) may call this
  758. //! function to initiate remote wake up signaling to the host. If the remote
  759. //! wake up feature has not been disabled by the host, this will cause the bus
  760. //! to resume operation within 20mS. If the host has disabled remote wake up,
  761. //! \b false will be returned to indicate that the wake up request was not
  762. //! successful.
  763. //!
  764. //! \return Returns \b true if the remote wake up is not disabled and the
  765. //! signaling was started or \b false if remote wake up is disabled or if
  766. //! signaling is currently ongoing following a previous call to this function.
  767. //
  768. //*****************************************************************************
  769. tBoolean
  770. USBDCDRemoteWakeupRequest(unsigned int ulIndex)
  771. {
  772. //
  773. // Check for parameter validity.
  774. //
  775. ASSERT(ulIndex == 0);
  776. //
  777. // Is remote wake up signaling currently enabled?
  778. //
  779. if(g_psUSBDevice[ulIndex].ucStatus & USB_STATUS_REMOTE_WAKE)
  780. {
  781. //
  782. // The host has not disabled remote wake up. Are we still in the
  783. // middle of a previous wake up sequence?
  784. //
  785. if(!g_psUSBDevice[ulIndex].bRemoteWakeup)
  786. {
  787. //
  788. // No - we are not in the middle of a wake up sequence so start
  789. // one here.
  790. //
  791. g_psUSBDevice[ulIndex].ucRemoteWakeupCount = 0;
  792. g_psUSBDevice[ulIndex].bRemoteWakeup = true;
  793. USBHostResume(g_USBInstance[ulIndex].uiBaseAddr, true);
  794. return(true);
  795. }
  796. }
  797. //
  798. // If we drop through to here, signaling was not initiated so return
  799. // false.
  800. return(false);
  801. }
  802. //*****************************************************************************
  803. //
  804. // Internal Functions, not to be called by applications
  805. //
  806. //*****************************************************************************
  807. //*****************************************************************************
  808. //
  809. // This internal function is called on the SOF interrupt to process any
  810. // outstanding remote wake up requests.
  811. //
  812. // \return None.
  813. //
  814. //*****************************************************************************
  815. void
  816. USBDeviceResumeTickHandler(unsigned int ulIndex)
  817. {
  818. if(g_psUSBDevice[ulIndex].bRemoteWakeup)
  819. {
  820. //
  821. // Increment the millisecond counter we use to time the resume
  822. // signaling.
  823. //
  824. g_psUSBDevice[ulIndex].ucRemoteWakeupCount++;
  825. //
  826. // Have we reached the 10mS mark? If so, we need to turn the signaling
  827. // off again.
  828. //
  829. if(g_psUSBDevice[ulIndex].ucRemoteWakeupCount == REMOTE_WAKEUP_PULSE_MS)
  830. {
  831. USBHostResume(g_USBInstance[ulIndex].uiBaseAddr, false);
  832. }
  833. //
  834. // Have we reached the point at which we can tell the client that the
  835. // bus has resumed? The controller doesn't give us an interrupt if we
  836. // initiated the wake up signaling so we just wait until 20mS have
  837. // passed then tell the client all is well.
  838. //
  839. if(g_psUSBDevice[ulIndex].ucRemoteWakeupCount == REMOTE_WAKEUP_READY_MS)
  840. {
  841. //
  842. // We are now finished with the remote wake up signaling.
  843. //
  844. g_psUSBDevice[ulIndex].bRemoteWakeup = false;
  845. //
  846. // If the client has registered a resume callback, call it. In the
  847. // case of a remote wake up request, we do not get a resume
  848. // interrupt from the controller so we need to fake it here.
  849. //
  850. if(g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnResumeHandler)
  851. {
  852. g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnResumeHandler(
  853. g_psUSBDevice[ulIndex].pvInstance);
  854. }
  855. }
  856. }
  857. }
  858. //*****************************************************************************
  859. //
  860. // This internal function reads a request data packet and dispatches it to
  861. // either a standard request handler or the registered device request
  862. // callback depending upon the request type.
  863. //
  864. // \return None.
  865. //
  866. //*****************************************************************************
  867. static void
  868. USBDReadAndDispatchRequest(unsigned int ulIndex)
  869. {
  870. unsigned int ulSize;
  871. tUSBRequest *pRequest;
  872. //
  873. // Cast the buffer to a request structure.
  874. //
  875. pRequest = (tUSBRequest *)g_pucDataBufferIn;
  876. //
  877. // Set the buffer size.
  878. //
  879. ulSize = EP0_MAX_PACKET_SIZE;
  880. //
  881. // Get the data from the USB controller end point 0.
  882. //
  883. USBEndpointDataGet(g_USBInstance[ulIndex].uiBaseAddr,
  884. USB_EP_0,
  885. g_pucDataBufferIn,
  886. &ulSize);
  887. //
  888. // If there was a null setup packet then just return.
  889. //
  890. if(!ulSize)
  891. {
  892. return;
  893. }
  894. //
  895. // See if this is a standard request or not.
  896. //
  897. if((pRequest->bmRequestType & USB_RTYPE_TYPE_M) != USB_RTYPE_STANDARD)
  898. {
  899. //
  900. // Since this is not a standard request, see if there is
  901. // an external handler present.
  902. //
  903. if(g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnRequestHandler)
  904. {
  905. g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnRequestHandler(
  906. g_psUSBDevice[ulIndex].pvInstance, pRequest, ulIndex);
  907. }
  908. else
  909. {
  910. //
  911. // If there is no handler then stall this request.
  912. //
  913. USBDCDStallEP0(ulIndex);
  914. }
  915. }
  916. else
  917. {
  918. //
  919. // Assure that the jump table is not out of bounds.
  920. //
  921. if((pRequest->bRequest <
  922. (sizeof(g_psUSBDStdRequests) / sizeof(tStdRequest))) &&
  923. (g_psUSBDStdRequests[pRequest->bRequest] != 0))
  924. {
  925. //
  926. // Jump table to the appropriate handler.
  927. //
  928. g_psUSBDStdRequests[pRequest->bRequest](&g_psUSBDevice[ulIndex],
  929. pRequest, ulIndex);
  930. }
  931. else
  932. {
  933. //
  934. // If there is no handler then stall this request.
  935. //
  936. USBDCDStallEP0(ulIndex);
  937. }
  938. }
  939. }
  940. //*****************************************************************************
  941. //
  942. // This is interrupt handler for endpoint zero.
  943. //
  944. // This function handles all interrupts on endpoint zero in order to maintain
  945. // the state needed for the control endpoint on endpoint zero. In order to
  946. // successfully enumerate and handle all USB standard requests, all requests
  947. // on endpoint zero must pass through this function. The endpoint has the
  948. // following states: \b USB_STATE_IDLE, \b USB_STATE_TX, \b USB_STATE_RX,
  949. // \b USB_STATE_STALL, and \b USB_STATE_STATUS. In the \b USB_STATE_IDLE
  950. // state the USB controller has not received the start of a request, and once
  951. // it does receive the data for the request it will either enter the
  952. // \b USB_STATE_TX, \b USB_STATE_RX, or \b USB_STATE_STALL depending on the
  953. // command. If the controller enters the \b USB_STATE_TX or \b USB_STATE_RX
  954. // then once all data has been sent or received, it must pass through the
  955. // \b USB_STATE_STATUS state to allow the host to acknowledge completion of
  956. // the request. The \b USB_STATE_STALL is entered from \b USB_STATE_IDLE in
  957. // the event that the USB request was not valid. Both the \b USB_STATE_STALL
  958. // and \b USB_STATE_STATUS are transitional states that return to the
  959. // \b USB_STATE_IDLE state.
  960. //
  961. // \return None.
  962. //
  963. // USB_STATE_IDLE -*--> USB_STATE_TX -*-> USB_STATE_STATUS -*->USB_STATE_IDLE
  964. // | | |
  965. // |--> USB_STATE_RX - |
  966. // | |
  967. // |--> USB_STATE_STALL ---------->---------
  968. //
  969. // ----------------------------------------------------------------
  970. // | Current State | State 0 | State 1 |
  971. // | --------------------|-------------------|----------------------
  972. // | USB_STATE_IDLE | USB_STATE_TX/RX | USB_STATE_STALL |
  973. // | USB_STATE_TX | USB_STATE_STATUS | |
  974. // | USB_STATE_RX | USB_STATE_STATUS | |
  975. // | USB_STATE_STATUS | USB_STATE_IDLE | |
  976. // | USB_STATE_STALL | USB_STATE_IDLE | |
  977. // ----------------------------------------------------------------
  978. //
  979. //*****************************************************************************
  980. void
  981. USBDeviceEnumHandler(tDeviceInstance *pDevInstance, unsigned int ulIndex)
  982. {
  983. unsigned int ulEPStatus;
  984. //
  985. // Get the end point 0 status.
  986. //
  987. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0);
  988. switch(pDevInstance->eEP0State)
  989. {
  990. //
  991. // Handle the status state, this is a transitory state from
  992. // USB_STATE_TX or USB_STATE_RX back to USB_STATE_IDLE.
  993. //
  994. case USB_STATE_STATUS:
  995. {
  996. //
  997. // Just go back to the idle state.
  998. //
  999. pDevInstance->eEP0State = USB_STATE_IDLE;
  1000. //
  1001. // If there is a pending address change then set the address.
  1002. //
  1003. if(pDevInstance->ulDevAddress & DEV_ADDR_PENDING)
  1004. {
  1005. //
  1006. // Clear the pending address change and set the address.
  1007. //
  1008. pDevInstance->ulDevAddress &= ~DEV_ADDR_PENDING;
  1009. USBDevAddrSet(g_USBInstance[ulIndex].uiBaseAddr,
  1010. pDevInstance->ulDevAddress);
  1011. }
  1012. //
  1013. // If a new packet is already pending, we need to read it
  1014. // and handle whatever request it contains.
  1015. //
  1016. if(ulEPStatus & USB_DEV_EP0_OUT_PKTRDY)
  1017. {
  1018. //
  1019. // Process the newly arrived packet.
  1020. //
  1021. USBDReadAndDispatchRequest(0);
  1022. }
  1023. break;
  1024. }
  1025. //
  1026. // In the IDLE state the code is waiting to receive data from the host.
  1027. //
  1028. case USB_STATE_IDLE:
  1029. {
  1030. //
  1031. // Is there a packet waiting for us?
  1032. //
  1033. if(ulEPStatus & USB_DEV_EP0_OUT_PKTRDY)
  1034. {
  1035. //
  1036. // Yes - process it.
  1037. //
  1038. USBDReadAndDispatchRequest(0);
  1039. }
  1040. break;
  1041. }
  1042. //
  1043. // Data is still being sent to the host so handle this in the
  1044. // EP0StateTx() function.
  1045. //
  1046. case USB_STATE_TX:
  1047. {
  1048. USBDEP0StateTx(ulIndex);
  1049. break;
  1050. }
  1051. //
  1052. // We are still in the middle of sending the configuration descriptor
  1053. // so handle this in the EP0StateTxConfig() function.
  1054. //
  1055. case USB_STATE_TX_CONFIG:
  1056. {
  1057. USBDEP0StateTxConfig(ulIndex);
  1058. break;
  1059. }
  1060. //
  1061. // Handle the receive state for commands that are receiving data on
  1062. // endpoint zero.
  1063. //
  1064. case USB_STATE_RX:
  1065. {
  1066. unsigned int ulDataSize;
  1067. //
  1068. // Set the number of bytes to get out of this next packet.
  1069. //
  1070. if(pDevInstance->ulEP0DataRemain > EP0_MAX_PACKET_SIZE)
  1071. {
  1072. //
  1073. // Don't send more than EP0_MAX_PACKET_SIZE bytes.
  1074. //
  1075. ulDataSize = EP0_MAX_PACKET_SIZE;
  1076. }
  1077. else
  1078. {
  1079. //
  1080. // There was space so send the remaining bytes.
  1081. //
  1082. ulDataSize = pDevInstance->ulEP0DataRemain;
  1083. }
  1084. //
  1085. // Get the data from the USB controller end point 0.
  1086. //
  1087. USBEndpointDataGet(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0,
  1088. pDevInstance->pEP0Data, &ulDataSize);
  1089. //
  1090. // If there we not more that EP0_MAX_PACKET_SIZE or more bytes
  1091. // remaining then this transfer is complete. If there were less than
  1092. // EP0_MAX_PACKET_SIZE remaining then there still needs to be
  1093. // null packet sent before this is complete.
  1094. //
  1095. if(pDevInstance->ulEP0DataRemain <= EP0_MAX_PACKET_SIZE)
  1096. {
  1097. //
  1098. // Need to ACK the data on end point 0 in this case and set the
  1099. // data end as this is the last of the data.
  1100. //
  1101. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  1102. //
  1103. // Return to the idle state.
  1104. //
  1105. pDevInstance->eEP0State = USB_STATE_IDLE;
  1106. //
  1107. // If there is a receive callback then call it.
  1108. //
  1109. if((pDevInstance->psInfo->sCallbacks.pfnDataReceived) &&
  1110. (pDevInstance->ulOUTDataSize != 0))
  1111. {
  1112. //
  1113. // Call the custom receive handler to handle the data
  1114. // that was received.
  1115. //
  1116. pDevInstance->psInfo->sCallbacks.pfnDataReceived(
  1117. pDevInstance->pvInstance,
  1118. pDevInstance->ulOUTDataSize, ulIndex);
  1119. //
  1120. // Indicate that there is no longer any data being waited
  1121. // on.
  1122. //
  1123. pDevInstance->ulOUTDataSize = 0;
  1124. }
  1125. }
  1126. else
  1127. {
  1128. //
  1129. // Need to ACK the data on end point 0 in this case
  1130. // without setting data end because more data is coming.
  1131. //
  1132. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, false);
  1133. }
  1134. //
  1135. // Advance the pointer.
  1136. //
  1137. pDevInstance->pEP0Data += ulDataSize;
  1138. //
  1139. // Decrement the number of bytes that are being waited on.
  1140. //
  1141. pDevInstance->ulEP0DataRemain -= ulDataSize;
  1142. break;
  1143. }
  1144. //
  1145. // The device stalled endpoint zero so check if the stall needs to be
  1146. // cleared once it has been successfully sent.
  1147. //
  1148. case USB_STATE_STALL:
  1149. {
  1150. //
  1151. // If we sent a stall then acknowledge this interrupt.
  1152. //
  1153. if(ulEPStatus & USB_DEV_EP0_SENT_STALL)
  1154. {
  1155. //
  1156. // Clear the Setup End condition.
  1157. //
  1158. USBDevEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0,
  1159. USB_DEV_EP0_SENT_STALL);
  1160. //
  1161. // Reset the global end point 0 state to IDLE.
  1162. //
  1163. pDevInstance->eEP0State = USB_STATE_IDLE;
  1164. }
  1165. break;
  1166. }
  1167. //
  1168. // Halt on an unknown state, but only in DEBUG mode builds.
  1169. //
  1170. default:
  1171. {
  1172. ASSERT(0);
  1173. }
  1174. }
  1175. }
  1176. //*****************************************************************************
  1177. //
  1178. // This function handles bus reset notifications.
  1179. //
  1180. // This function is called from the low level USB interrupt handler whenever
  1181. // a bus reset is detected. It performs tidy-up as required and resets the
  1182. // configuration back to defaults in preparation for descriptor queries from
  1183. // the host.
  1184. //
  1185. // \return None.
  1186. //
  1187. //*****************************************************************************
  1188. void
  1189. USBDeviceEnumResetHandler(tDeviceInstance *pDevInstance)
  1190. {
  1191. unsigned int ulLoop;
  1192. //
  1193. // Disable remote wake up signaling (as per USB 2.0 spec 9.1.1.6).
  1194. //
  1195. pDevInstance->ucStatus &= ~USB_STATUS_REMOTE_WAKE;
  1196. pDevInstance->bRemoteWakeup = false;
  1197. //
  1198. // Call the device dependent code to indicate a bus reset has occurred.
  1199. //
  1200. if(pDevInstance->psInfo->sCallbacks.pfnResetHandler)
  1201. {
  1202. pDevInstance->psInfo->sCallbacks.pfnResetHandler(
  1203. pDevInstance->pvInstance);
  1204. }
  1205. //
  1206. // Reset the default configuration identifier and alternate function
  1207. // selections.
  1208. //
  1209. pDevInstance->ulConfiguration = pDevInstance->ulDefaultConfiguration;
  1210. for(ulLoop = 0; ulLoop < USB_MAX_INTERFACES_PER_DEVICE; ulLoop++)
  1211. {
  1212. pDevInstance->pucAltSetting[ulLoop] = (unsigned char)0;
  1213. }
  1214. }
  1215. //*****************************************************************************
  1216. //
  1217. // This function handles the GET_STATUS standard USB request.
  1218. //
  1219. // \param pvInstance is the USB device controller instance data.
  1220. // \param pUSBRequest holds the request type and endpoint number if endpoint
  1221. // status is requested.
  1222. //
  1223. // This function handles responses to a Get Status request from the host
  1224. // controller. A status request can be for the device, an interface or an
  1225. // endpoint. If any other type of request is made this function will cause
  1226. // a stall condition to indicate that the command is not supported. The
  1227. // \e pUSBRequest structure holds the type of the request in the
  1228. // bmRequestType field. If the type indicates that this is a request for an
  1229. // endpoint's status, then the wIndex field holds the endpoint number.
  1230. //
  1231. // \return None.
  1232. //
  1233. //*****************************************************************************
  1234. static void
  1235. USBDGetStatus(void *pvInstance, tUSBRequest *pUSBRequest,
  1236. unsigned int ulIndex)
  1237. {
  1238. unsigned short usData;
  1239. tDeviceInstance *psUSBControl;
  1240. ASSERT(pUSBRequest != 0);
  1241. ASSERT(pvInstance != 0);
  1242. //
  1243. // Create the device information pointer.
  1244. //
  1245. psUSBControl = (tDeviceInstance *)pvInstance;
  1246. //
  1247. // Need to ACK the data on end point 0 without setting last data as there
  1248. // will be a data phase.
  1249. //
  1250. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, false);
  1251. //
  1252. // Determine what type of status was requested.
  1253. //
  1254. switch(pUSBRequest->bmRequestType & USB_RTYPE_RECIPIENT_M)
  1255. {
  1256. //
  1257. // This was a Device Status request.
  1258. //
  1259. case USB_RTYPE_DEVICE:
  1260. {
  1261. //
  1262. // Return the current status for the device.
  1263. //
  1264. usData = (unsigned short)psUSBControl->ucStatus;
  1265. break;
  1266. }
  1267. //
  1268. // This was a Interface status request.
  1269. //
  1270. case USB_RTYPE_INTERFACE:
  1271. {
  1272. //
  1273. // Interface status always returns 0.
  1274. //
  1275. usData = (unsigned short)0;
  1276. break;
  1277. }
  1278. //
  1279. // This was an endpoint status request.
  1280. //
  1281. case USB_RTYPE_ENDPOINT:
  1282. {
  1283. unsigned short usIndex;
  1284. unsigned int ulDir;
  1285. //
  1286. // Which endpoint are we dealing with?
  1287. //
  1288. usIndex = pUSBRequest->wIndex & USB_REQ_EP_NUM_M;
  1289. //
  1290. // Check if this was a valid endpoint request.
  1291. //
  1292. if((usIndex == 0) || (usIndex >= NUM_USB_EP))
  1293. {
  1294. USBDCDStallEP0(ulIndex);
  1295. return;
  1296. }
  1297. else
  1298. {
  1299. //
  1300. // Are we dealing with an IN or OUT endpoint?
  1301. //
  1302. ulDir = ((pUSBRequest->wIndex & USB_REQ_EP_DIR_M) ==
  1303. USB_REQ_EP_DIR_IN) ? HALT_EP_IN : HALT_EP_OUT;
  1304. //
  1305. // Get the current halt status for this endpoint.
  1306. //
  1307. usData =
  1308. (unsigned short)psUSBControl->ucHalt[ulDir][usIndex - 1];
  1309. }
  1310. break;
  1311. }
  1312. //
  1313. // This was an unknown request.
  1314. //
  1315. default:
  1316. {
  1317. //
  1318. // Anything else causes a stall condition to indicate that the
  1319. // command was not supported.
  1320. //
  1321. USBDCDStallEP0(ulIndex);
  1322. return;
  1323. }
  1324. }
  1325. //
  1326. // Send the two byte status response.
  1327. //
  1328. psUSBControl->ulEP0DataRemain = 2;
  1329. psUSBControl->pEP0Data = (unsigned char *)&usData;
  1330. //
  1331. // Send the response.
  1332. //
  1333. USBDEP0StateTx(ulIndex);
  1334. }
  1335. //*****************************************************************************
  1336. //
  1337. // This function handles the CLEAR_FEATURE standard USB request.
  1338. //
  1339. // \param pvInstance is the USB device controller instance data.
  1340. // \param pUSBRequest holds the options for the Clear Feature USB request.
  1341. //
  1342. // This function handles device or endpoint clear feature requests. The
  1343. // \e pUSBRequest structure holds the type of the request in the bmRequestType
  1344. // field and the feature is held in the wValue field. The device can only
  1345. // clear the Remote Wake feature. This device request should only be made if
  1346. // the descriptor indicates that Remote Wake is implemented by the device.
  1347. // Endpoints can only clear a halt on a given endpoint. If any other
  1348. // requests are made, then the device will stall the request to indicate to
  1349. // the host that the command was not supported.
  1350. //
  1351. // \return None.
  1352. //
  1353. //*****************************************************************************
  1354. static void
  1355. USBDClearFeature(void *pvInstance, tUSBRequest *pUSBRequest,
  1356. unsigned int ulIndex)
  1357. {
  1358. tDeviceInstance *psUSBControl;
  1359. ASSERT(pUSBRequest != 0);
  1360. ASSERT(pvInstance != 0);
  1361. //
  1362. // Create the device information pointer.
  1363. //
  1364. psUSBControl = (tDeviceInstance *)pvInstance;
  1365. //
  1366. // Need to ACK the data on end point 0 with last data set as this has no
  1367. // data phase.
  1368. //
  1369. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  1370. //
  1371. // Determine what type of status was requested.
  1372. //
  1373. switch(pUSBRequest->bmRequestType & USB_RTYPE_RECIPIENT_M)
  1374. {
  1375. //
  1376. // This is a clear feature request at the device level.
  1377. //
  1378. case USB_RTYPE_DEVICE:
  1379. {
  1380. //
  1381. // Only remote wake is can be cleared by this function.
  1382. //
  1383. if(USB_FEATURE_REMOTE_WAKE & pUSBRequest->wValue)
  1384. {
  1385. //
  1386. // Clear the remote wake up state.
  1387. //
  1388. psUSBControl->ucStatus &= ~USB_STATUS_REMOTE_WAKE;
  1389. }
  1390. else
  1391. {
  1392. USBDCDStallEP0(ulIndex);
  1393. }
  1394. break;
  1395. }
  1396. //
  1397. // This is a clear feature request at the endpoint level.
  1398. //
  1399. case USB_RTYPE_ENDPOINT:
  1400. {
  1401. unsigned int ulDir;
  1402. unsigned short usIndex;
  1403. //
  1404. // Which endpoint are we dealing with?
  1405. //
  1406. usIndex = pUSBRequest->wIndex & USB_REQ_EP_NUM_M;
  1407. //
  1408. // Not a valid endpoint.
  1409. //
  1410. if((usIndex == 0) || (usIndex > NUM_USB_EP))
  1411. {
  1412. USBDCDStallEP0(ulIndex);
  1413. }
  1414. else
  1415. {
  1416. //
  1417. // Only the halt feature is supported.
  1418. //
  1419. if(USB_FEATURE_EP_HALT == pUSBRequest->wValue)
  1420. {
  1421. //
  1422. // Are we dealing with an IN or OUT endpoint?
  1423. //
  1424. ulDir = ((pUSBRequest->wIndex & USB_REQ_EP_DIR_M) ==
  1425. USB_REQ_EP_DIR_IN) ? HALT_EP_IN : HALT_EP_OUT;
  1426. //
  1427. // Clear the halt condition on this endpoint.
  1428. //
  1429. psUSBControl->ucHalt[ulDir][usIndex - 1] = 0;
  1430. if(ulDir == HALT_EP_IN)
  1431. {
  1432. USBDevEndpointStallClear(g_USBInstance[ulIndex].uiBaseAddr,
  1433. INDEX_TO_USB_EP(usIndex),
  1434. USB_EP_DEV_IN);
  1435. }
  1436. else
  1437. {
  1438. USBDevEndpointStallClear(g_USBInstance[ulIndex].uiBaseAddr,
  1439. INDEX_TO_USB_EP(usIndex),
  1440. USB_EP_DEV_OUT);
  1441. }
  1442. }
  1443. else
  1444. {
  1445. //
  1446. // If any other feature is requested, this is an error.
  1447. //
  1448. USBDCDStallEP0(ulIndex);
  1449. return;
  1450. }
  1451. }
  1452. break;
  1453. }
  1454. //
  1455. // This is an unknown request.
  1456. //
  1457. default:
  1458. {
  1459. USBDCDStallEP0(ulIndex);
  1460. return;
  1461. }
  1462. }
  1463. }
  1464. //*****************************************************************************
  1465. //
  1466. // This function handles the SET_FEATURE standard USB request.
  1467. //
  1468. // \param pvInstance is the USB device controller instance data.
  1469. // \param pUSBRequest holds the feature in the wValue field of the USB
  1470. // request.
  1471. //
  1472. // This function handles device or endpoint set feature requests. The
  1473. // \e pUSBRequest structure holds the type of the request in the bmRequestType
  1474. // field and the feature is held in the wValue field. The device can only
  1475. // set the Remote Wake feature. This device request should only be made if the
  1476. // descriptor indicates that Remote Wake is implemented by the device.
  1477. // Endpoint requests can only issue a halt on a given endpoint. If any other
  1478. // requests are made, then the device will stall the request to indicate to the
  1479. // host that the command was not supported.
  1480. //
  1481. // \return None.
  1482. //
  1483. //*****************************************************************************
  1484. static void
  1485. USBDSetFeature(void *pvInstance, tUSBRequest *pUSBRequest,
  1486. unsigned int ulIndex)
  1487. {
  1488. tDeviceInstance *psUSBControl;
  1489. ASSERT(pUSBRequest != 0);
  1490. ASSERT(pvInstance != 0);
  1491. //
  1492. // Create the device information pointer.
  1493. //
  1494. psUSBControl = (tDeviceInstance *)pvInstance;
  1495. //
  1496. // Need to ACK the data on end point 0 with last data set as this has no
  1497. // data phase.
  1498. //
  1499. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  1500. //
  1501. // Determine what type of status was requested.
  1502. //
  1503. switch(pUSBRequest->bmRequestType & USB_RTYPE_RECIPIENT_M)
  1504. {
  1505. //
  1506. // This is a set feature request at the device level.
  1507. //
  1508. case USB_RTYPE_DEVICE:
  1509. {
  1510. //
  1511. // Only remote wake is the only feature that can be set by this
  1512. // function.
  1513. //
  1514. if(USB_FEATURE_REMOTE_WAKE & pUSBRequest->wValue)
  1515. {
  1516. //
  1517. // Set the remote wake up state.
  1518. //
  1519. psUSBControl->ucStatus |= USB_STATUS_REMOTE_WAKE;
  1520. }
  1521. else
  1522. {
  1523. USBDCDStallEP0(ulIndex);
  1524. }
  1525. break;
  1526. }
  1527. //
  1528. // This is a set feature request at the endpoint level.
  1529. //
  1530. case USB_RTYPE_ENDPOINT:
  1531. {
  1532. unsigned short usIndex;
  1533. unsigned int ulDir;
  1534. //
  1535. // Which endpoint are we dealing with?
  1536. //
  1537. usIndex = pUSBRequest->wIndex & USB_REQ_EP_NUM_M;
  1538. //
  1539. // Not a valid endpoint?
  1540. //
  1541. if((usIndex == 0) || (usIndex >= NUM_USB_EP))
  1542. {
  1543. USBDCDStallEP0(ulIndex);
  1544. }
  1545. else
  1546. {
  1547. //
  1548. // Only the Halt feature can be set.
  1549. //
  1550. if(USB_FEATURE_EP_HALT == pUSBRequest->wValue)
  1551. {
  1552. //
  1553. // Are we dealing with an IN or OUT endpoint?
  1554. //
  1555. ulDir = ((pUSBRequest->wIndex & USB_REQ_EP_DIR_M) ==
  1556. USB_REQ_EP_DIR_IN) ? HALT_EP_IN : HALT_EP_OUT;
  1557. //
  1558. // Clear the halt condition on this endpoint.
  1559. //
  1560. psUSBControl->ucHalt[ulDir][usIndex - 1] = 1;
  1561. }
  1562. else
  1563. {
  1564. //
  1565. // No other requests are supported.
  1566. //
  1567. USBDCDStallEP0(ulIndex);
  1568. return;
  1569. }
  1570. }
  1571. break;
  1572. }
  1573. //
  1574. // This is an unknown request.
  1575. //
  1576. default:
  1577. {
  1578. USBDCDStallEP0(ulIndex);
  1579. return;
  1580. }
  1581. }
  1582. }
  1583. //*****************************************************************************
  1584. //
  1585. // This function handles the SET_ADDRESS standard USB request.
  1586. //
  1587. // \param pvInstance is the USB device controller instance data.
  1588. // \param pUSBRequest holds the new address to use in the wValue field of the
  1589. // USB request.
  1590. //
  1591. // This function is called to handle the change of address request from the
  1592. // host controller. This can only start the sequence as the host must
  1593. // acknowledge that the device has changed address. Thus this function sets
  1594. // the address change as pending until the status phase of the request has
  1595. // been completed successfully. This prevents the devices address from
  1596. // changing and not properly responding to the status phase.
  1597. //
  1598. // \return None.
  1599. //
  1600. //*****************************************************************************
  1601. static void
  1602. USBDSetAddress(void *pvInstance, tUSBRequest *pUSBRequest,
  1603. unsigned int ulIndex)
  1604. {
  1605. tDeviceInstance *psUSBControl;
  1606. ASSERT(pUSBRequest != 0);
  1607. ASSERT(pvInstance != 0);
  1608. //
  1609. // Create the device information pointer.
  1610. //
  1611. psUSBControl = (tDeviceInstance *)pvInstance;
  1612. //
  1613. // Need to ACK the data on end point 0 with last data set as this has no
  1614. // data phase.
  1615. //
  1616. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  1617. //
  1618. // Save the device address as we cannot change address until the status
  1619. // phase is complete.
  1620. //
  1621. psUSBControl->ulDevAddress = pUSBRequest->wValue | DEV_ADDR_PENDING;
  1622. //
  1623. // Transition directly to the status state since there is no data phase
  1624. // for this request.
  1625. //
  1626. psUSBControl->eEP0State = USB_STATE_STATUS;
  1627. }
  1628. //*****************************************************************************
  1629. //
  1630. // This function handles the GET_DESCRIPTOR standard USB request.
  1631. //
  1632. // \param pvInstance is the USB device controller instance data.
  1633. // \param pUSBRequest holds the data for this request.
  1634. //
  1635. // This function will return most of the descriptors requested by the host
  1636. // controller. The descriptor specified by \e
  1637. // pvInstance->psInfo->pDeviceDescriptor will be returned when the device
  1638. // descriptor is requested. If a request for a specific configuration
  1639. // descriptor is made, then the appropriate descriptor from the \e
  1640. // g_pConfigDescriptors will be returned. When a request for a string
  1641. // descriptor is made, the appropriate string from the
  1642. // \e pvInstance->psInfo->pStringDescriptors will be returned. If the \e
  1643. // pvInstance->psInfo->sCallbacks.GetDescriptor is specified it will be
  1644. // called to handle the request. In this case it must call the
  1645. // USBDCDSendDataEP0() function to send the data to the host controller. If
  1646. // the callback is not specified, and the descriptor request is not for a
  1647. // device, configuration, or string descriptor then this function will stall
  1648. // the request to indicate that the request was not supported by the device.
  1649. //
  1650. // \return None.
  1651. //
  1652. //*****************************************************************************
  1653. static void
  1654. USBDGetDescriptor(void *pvInstance, tUSBRequest *pUSBRequest,
  1655. unsigned int ulIndex)
  1656. {
  1657. tBoolean bConfig;
  1658. tDeviceInstance *psUSBControl;
  1659. tDeviceInfo *psDevice;
  1660. ASSERT(pUSBRequest != 0);
  1661. ASSERT(pvInstance != 0);
  1662. //
  1663. // Create the device information pointer.
  1664. //
  1665. psUSBControl = (tDeviceInstance *)pvInstance;
  1666. psDevice = psUSBControl->psInfo;
  1667. //
  1668. // Need to ACK the data on end point 0 without setting last data as there
  1669. // will be a data phase.
  1670. //
  1671. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, false);
  1672. //
  1673. // Assume we are not sending the configuration descriptor until we
  1674. // determine otherwise.
  1675. //
  1676. bConfig = false;
  1677. //
  1678. // Which descriptor are we being asked for?
  1679. //
  1680. switch(pUSBRequest->wValue >> 8)
  1681. {
  1682. //
  1683. // This request was for a device descriptor.
  1684. //
  1685. case USB_DTYPE_DEVICE:
  1686. {
  1687. //
  1688. // Return the externally provided device descriptor.
  1689. //
  1690. psUSBControl->pEP0Data =
  1691. (unsigned char *)psDevice->pDeviceDescriptor;
  1692. //
  1693. // The size of the device descriptor is in the first byte.
  1694. //
  1695. psUSBControl->ulEP0DataRemain = psDevice->pDeviceDescriptor[0];
  1696. break;
  1697. }
  1698. //
  1699. // This request was for a configuration descriptor.
  1700. //
  1701. case USB_DTYPE_CONFIGURATION:
  1702. {
  1703. const tConfigHeader *psConfig;
  1704. const tDeviceDescriptor *psDeviceDesc;
  1705. unsigned char ucIndex;
  1706. //
  1707. // Which configuration are we being asked for?
  1708. //
  1709. ucIndex = (unsigned char)(pUSBRequest->wValue & 0xFF);
  1710. //
  1711. // Is this valid?
  1712. //
  1713. psDeviceDesc =
  1714. (const tDeviceDescriptor *)psDevice->pDeviceDescriptor;
  1715. if(ucIndex >= psDeviceDesc->bNumConfigurations)
  1716. {
  1717. //
  1718. // This is an invalid configuration index. Stall EP0 to
  1719. // indicate a request error.
  1720. //
  1721. USBDCDStallEP0(ulIndex);
  1722. psUSBControl->pEP0Data = 0;
  1723. psUSBControl->ulEP0DataRemain = 0;
  1724. }
  1725. else
  1726. {
  1727. //
  1728. // Return the externally specified configuration descriptor.
  1729. //
  1730. psConfig = psDevice->ppConfigDescriptors[ucIndex];
  1731. //
  1732. // Start by sending data from the beginning of the first
  1733. // descriptor.
  1734. //
  1735. psUSBControl->ucConfigSection = 0;
  1736. psUSBControl->ucSectionOffset = 0;
  1737. psUSBControl->pEP0Data = (unsigned char *)
  1738. psConfig->psSections[0]->pucData;
  1739. //
  1740. // Determine the total size of the configuration descriptor
  1741. // by counting the sizes of the sections comprising it.
  1742. //
  1743. psUSBControl->ulEP0DataRemain =
  1744. USBDCDConfigDescGetSize(psConfig);
  1745. //
  1746. // Remember that we need to send the configuration descriptor
  1747. // and which descriptor we need to send.
  1748. //
  1749. psUSBControl->ucConfigIndex = ucIndex;
  1750. bConfig = true;
  1751. }
  1752. break;
  1753. }
  1754. //
  1755. // This request was for a string descriptor.
  1756. //
  1757. case USB_DTYPE_STRING:
  1758. {
  1759. int lIndex;
  1760. //
  1761. // Determine the correct descriptor index based on the requested
  1762. // language ID and index.
  1763. //
  1764. lIndex = USBDStringIndexFromRequest(pUSBRequest->wIndex,
  1765. pUSBRequest->wValue & 0xFF, ulIndex);
  1766. //
  1767. // If the mapping function returned -1 then stall the request to
  1768. // indicate that the request was not valid.
  1769. //
  1770. if(lIndex == -1)
  1771. {
  1772. USBDCDStallEP0(ulIndex);
  1773. break;
  1774. }
  1775. //
  1776. // Return the externally specified configuration descriptor.
  1777. //
  1778. psUSBControl->pEP0Data =
  1779. (unsigned char *)psDevice->ppStringDescriptors[lIndex];
  1780. //
  1781. // The total size of a string descriptor is in byte 0.
  1782. //
  1783. psUSBControl->ulEP0DataRemain =
  1784. psDevice->ppStringDescriptors[lIndex][0];
  1785. break;
  1786. }
  1787. //
  1788. // Any other request is not handled by the default enumeration handler
  1789. // so see if it needs to be passed on to another handler.
  1790. //
  1791. default:
  1792. {
  1793. //
  1794. // If there is a handler for requests that are not handled then
  1795. // call it.
  1796. //
  1797. if(psDevice->sCallbacks.pfnGetDescriptor)
  1798. {
  1799. psDevice->sCallbacks.pfnGetDescriptor(psUSBControl->pvInstance,
  1800. pUSBRequest, ulIndex);
  1801. return;
  1802. }
  1803. else
  1804. {
  1805. //
  1806. // Whatever this was this handler does not understand it so
  1807. // just stall the request.
  1808. //
  1809. USBDCDStallEP0(ulIndex);
  1810. }
  1811. break;
  1812. }
  1813. }
  1814. //
  1815. // If this request has data to send, then send it.
  1816. //
  1817. if(psUSBControl->pEP0Data)
  1818. {
  1819. //
  1820. // If there is more data to send than is requested then just
  1821. // send the requested amount of data.
  1822. //
  1823. if(psUSBControl->ulEP0DataRemain > pUSBRequest->wLength)
  1824. {
  1825. psUSBControl->ulEP0DataRemain = pUSBRequest->wLength;
  1826. }
  1827. //
  1828. // Now in the transmit data state. Be careful to call the correct
  1829. // function since we need to handle the configuration descriptor
  1830. // differently from the others.
  1831. //
  1832. if(!bConfig)
  1833. {
  1834. USBDEP0StateTx(ulIndex);
  1835. }
  1836. else
  1837. {
  1838. USBDEP0StateTxConfig(ulIndex);
  1839. }
  1840. }
  1841. }
  1842. //*****************************************************************************
  1843. //
  1844. // This function determines which string descriptor to send to satisfy a
  1845. // request for a given index and language.
  1846. //
  1847. // \param usLang is the requested string language ID.
  1848. // \param usIndex is the requested string descriptor index.
  1849. //
  1850. // When a string descriptor is requested, the host provides a language ID and
  1851. // index to identify the string ("give me string number 5 in French"). This
  1852. // function maps these two parameters to an index within our device's string
  1853. // descriptor array which is arranged as multiple groups of strings with
  1854. // one group for each language advertised via string descriptor 0.
  1855. //
  1856. // We assume that there are an equal number of strings per language and
  1857. // that the first descriptor is the language descriptor and use this fact to
  1858. // perform the mapping.
  1859. //
  1860. // \return The index of the string descriptor to return or -1 if the string
  1861. // could not be found.
  1862. //
  1863. //*****************************************************************************
  1864. static int
  1865. USBDStringIndexFromRequest(unsigned short usLang, unsigned short usIndex,
  1866. unsigned int ulIndex)
  1867. {
  1868. tString0Descriptor *pLang;
  1869. unsigned int ulNumLangs;
  1870. unsigned int ulNumStringsPerLang;
  1871. unsigned int ulLoop;
  1872. //
  1873. // Make sure we have a string table at all.
  1874. //
  1875. if((g_psUSBDevice[ulIndex].psInfo == 0) ||
  1876. (g_psUSBDevice[ulIndex].psInfo->ppStringDescriptors == 0))
  1877. {
  1878. return(-1);
  1879. }
  1880. //
  1881. // First look for the trivial case where descriptor 0 is being
  1882. // requested. This is the special case since descriptor 0 contains the
  1883. // language codes supported by the device.
  1884. //
  1885. if(usIndex == 0)
  1886. {
  1887. return(0);
  1888. }
  1889. //
  1890. // How many languages does this device support? This is determined by
  1891. // looking at the length of the first descriptor in the string table,
  1892. // subtracting 2 for the header and dividing by two (the size of each
  1893. // language code).
  1894. //
  1895. ulNumLangs = (g_psUSBDevice[ulIndex].psInfo->ppStringDescriptors[0][0] - 2) / 2;
  1896. //
  1897. // We assume that the table includes the same number of strings for each
  1898. // supported language. We know the number of entries in the string table,
  1899. // so how many are there for each language? This may seem an odd way to
  1900. // do this (why not just have the application tell us in the device info
  1901. // structure?) but it's needed since we didn't want to change the API
  1902. // after the first release which did not support multiple languages.
  1903. //
  1904. ulNumStringsPerLang = ((g_psUSBDevice[ulIndex].psInfo->ulNumStringDescriptors - 1) /
  1905. ulNumLangs);
  1906. //
  1907. // Just to be sure, make sure that the calculation indicates an equal
  1908. // number of strings per language. We expect the string table to contain
  1909. // (1 + (strings_per_language * languages)) entries.
  1910. //
  1911. if((1 + (ulNumStringsPerLang * ulNumLangs)) !=
  1912. g_psUSBDevice[ulIndex].psInfo->ulNumStringDescriptors)
  1913. {
  1914. return(-1);
  1915. }
  1916. //
  1917. // Now determine which language we are looking for. It is assumed that
  1918. // the order of the groups of strings per language in the table is the
  1919. // same as the order of the language IDs listed in the first descriptor.
  1920. //
  1921. pLang = (tString0Descriptor *)(g_psUSBDevice[ulIndex].psInfo->ppStringDescriptors[0]);
  1922. //
  1923. // Look through the supported languages looking for the one we were asked
  1924. // for.
  1925. //
  1926. for(ulLoop = 0; ulLoop < ulNumLangs; ulLoop++)
  1927. {
  1928. //
  1929. // Have we found the requested language?
  1930. //
  1931. if(pLang->wLANGID[ulLoop] == usLang)
  1932. {
  1933. //
  1934. // Yes - calculate the index of the descriptor to send.
  1935. //
  1936. return((ulNumStringsPerLang * ulLoop) + usIndex);
  1937. }
  1938. }
  1939. //
  1940. // If we drop out of the loop, the requested language was not found so
  1941. // return -1 to indicate the error.
  1942. //
  1943. return(-1);
  1944. }
  1945. //*****************************************************************************
  1946. //
  1947. // This function handles the SET_DESCRIPTOR standard USB request.
  1948. //
  1949. // \param pvInstance is the USB device controller instance data.
  1950. // \param pUSBRequest holds the data for this request.
  1951. //
  1952. // This function currently is not supported and will respond with a Stall
  1953. // to indicate that this command is not supported by the device.
  1954. //
  1955. // \return None.
  1956. //
  1957. //*****************************************************************************
  1958. static void
  1959. USBDSetDescriptor(void *pvInstance, tUSBRequest *pUSBRequest,
  1960. unsigned int ulIndex)
  1961. {
  1962. //
  1963. // Need to ACK the data on end point 0 without setting last data as there
  1964. // will be a data phase.
  1965. //
  1966. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, false);
  1967. //
  1968. // This function is not handled by default.
  1969. //
  1970. USBDCDStallEP0(ulIndex);
  1971. }
  1972. //*****************************************************************************
  1973. //
  1974. // This function handles the GET_CONFIGURATION standard USB request.
  1975. //
  1976. // \param pvInstance is the USB device controller instance data.
  1977. // \param pUSBRequest holds the data for this request.
  1978. //
  1979. // This function responds to a host request to return the current
  1980. // configuration of the USB device. The function will send the configuration
  1981. // response to the host and return. This value will either be 0 or the last
  1982. // value received from a call to SetConfiguration().
  1983. //
  1984. // \return None.
  1985. //
  1986. //*****************************************************************************
  1987. static void
  1988. USBDGetConfiguration(void *pvInstance, tUSBRequest *pUSBRequest,
  1989. unsigned int ulIndex)
  1990. {
  1991. unsigned char ucValue;
  1992. tDeviceInstance *psUSBControl;
  1993. ASSERT(pUSBRequest != 0);
  1994. ASSERT(pvInstance != 0);
  1995. //
  1996. // Create the device information pointer.
  1997. //
  1998. psUSBControl = (tDeviceInstance *)pvInstance;
  1999. //
  2000. // Need to ACK the data on end point 0 without setting last data as there
  2001. // will be a data phase.
  2002. //
  2003. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, false);
  2004. //
  2005. // If we still have an address pending then the device is still not
  2006. // configured.
  2007. //
  2008. if(psUSBControl->ulDevAddress & DEV_ADDR_PENDING)
  2009. {
  2010. ucValue = 0;
  2011. }
  2012. else
  2013. {
  2014. ucValue = (unsigned char)psUSBControl->ulConfiguration;
  2015. }
  2016. psUSBControl->ulEP0DataRemain = 1;
  2017. psUSBControl->pEP0Data = &ucValue;
  2018. //
  2019. // Send the single byte response.
  2020. //
  2021. USBDEP0StateTx(ulIndex);
  2022. }
  2023. //*****************************************************************************
  2024. //
  2025. // This function handles the SET_CONFIGURATION standard USB request.
  2026. //
  2027. // \param pvInstance is the USB device controller instance data.
  2028. // \param pUSBRequest holds the data for this request.
  2029. //
  2030. // This function responds to a host request to change the current
  2031. // configuration of the USB device. The actual configuration number is taken
  2032. // from the structure passed in via \e pUSBRequest. This number should be one
  2033. // of the configurations that was specified in the descriptors. If the
  2034. // \e ConfigChange callback is specified in \e pvInstance->psInfo->sCallbacks,
  2035. // it will be called so that the application can respond to a change in
  2036. // configuration.
  2037. //
  2038. // \return None.
  2039. //
  2040. //*****************************************************************************
  2041. static void
  2042. USBDSetConfiguration(void *pvInstance, tUSBRequest *pUSBRequest,
  2043. unsigned int ulIndex)
  2044. {
  2045. tDeviceInstance *psUSBControl;
  2046. tDeviceInfo *psDevice;
  2047. //
  2048. // Create the device information pointer.
  2049. //
  2050. psUSBControl = (tDeviceInstance *)pvInstance;
  2051. psDevice = psUSBControl->psInfo;
  2052. //
  2053. // Need to ACK the data on end point 0 with last data set as this has no
  2054. // data phase.
  2055. //
  2056. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  2057. //
  2058. // Cannot set the configuration to one that does not exist so check the
  2059. // enumeration structure to see how many valid configurations are present.
  2060. //
  2061. if(pUSBRequest->wValue > psUSBControl->psInfo->pDeviceDescriptor[17])
  2062. {
  2063. //
  2064. // The passed configuration number is not valid. Stall the endpoint to
  2065. // signal the error to the host.
  2066. //
  2067. USBDCDStallEP0(ulIndex);
  2068. }
  2069. else
  2070. {
  2071. //
  2072. // Save the configuration.
  2073. //
  2074. psUSBControl->ulConfiguration = pUSBRequest->wValue;
  2075. //
  2076. // If passed a configuration other than 0 (which tells us that we are
  2077. // not currently configured), configure the endpoints (other than EP0)
  2078. // appropriately.
  2079. //
  2080. if(psUSBControl->ulConfiguration)
  2081. {
  2082. const tConfigHeader *psHdr;
  2083. const tConfigDescriptor *psDesc;
  2084. //
  2085. // Get a pointer to the configuration descriptor. This will always
  2086. // be the first section in the current configuration.
  2087. //
  2088. psHdr = psDevice->ppConfigDescriptors[pUSBRequest->wValue - 1];
  2089. psDesc = (const tConfigDescriptor *)(psHdr->psSections[0]->pucData);
  2090. //
  2091. // Remember the new self- or bus-powered state if the user has not
  2092. // already called us to tell us the state to report.
  2093. //
  2094. if(!psUSBControl->bPwrSrcSet)
  2095. {
  2096. if((psDesc->bmAttributes & USB_CONF_ATTR_PWR_M) ==
  2097. USB_CONF_ATTR_SELF_PWR)
  2098. {
  2099. psUSBControl->ucStatus |= USB_STATUS_SELF_PWR;
  2100. }
  2101. else
  2102. {
  2103. psUSBControl->ucStatus &= ~USB_STATUS_SELF_PWR;
  2104. }
  2105. }
  2106. //
  2107. // Configure endpoints for the new configuration.
  2108. //
  2109. USBDeviceConfig(0,
  2110. psDevice->ppConfigDescriptors[pUSBRequest->wValue - 1],
  2111. psDevice->psFIFOConfig);
  2112. }
  2113. //
  2114. // If there is a configuration change callback then call it.
  2115. //
  2116. if(psDevice->sCallbacks.pfnConfigChange)
  2117. {
  2118. psDevice->sCallbacks.pfnConfigChange(
  2119. psUSBControl->pvInstance, psUSBControl->ulConfiguration, ulIndex);
  2120. }
  2121. }
  2122. }
  2123. //*****************************************************************************
  2124. //
  2125. // This function handles the GET_INTERFACE standard USB request.
  2126. //
  2127. // \param pvInstance is the USB device controller instance data.
  2128. // \param pUSBRequest holds the data for this request.
  2129. //
  2130. // This function is called when the host controller request the current
  2131. // interface that is in use by the device. This simply returns the value set
  2132. // by the last call to SetInterface().
  2133. //
  2134. // \return None.
  2135. //
  2136. //*****************************************************************************
  2137. static void
  2138. USBDGetInterface(void *pvInstance, tUSBRequest *pUSBRequest,
  2139. unsigned int ulIndex)
  2140. {
  2141. unsigned char ucValue;
  2142. tDeviceInstance *psUSBControl;
  2143. ASSERT(pUSBRequest != 0);
  2144. ASSERT(pvInstance != 0);
  2145. //
  2146. // Create the device information pointer.
  2147. //
  2148. psUSBControl = (tDeviceInstance *)pvInstance;
  2149. //
  2150. // Need to ACK the data on end point 0 without setting last data as there
  2151. // will be a data phase.
  2152. //
  2153. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, false);
  2154. //
  2155. // If we still have an address pending then the device is still not
  2156. // configured.
  2157. //
  2158. if(psUSBControl->ulDevAddress & DEV_ADDR_PENDING)
  2159. {
  2160. ucValue = (unsigned char)0;
  2161. }
  2162. else
  2163. {
  2164. //
  2165. // Is the interface number valid?
  2166. //
  2167. if(pUSBRequest->wIndex < USB_MAX_INTERFACES_PER_DEVICE)
  2168. {
  2169. //
  2170. // Read the current alternate setting for the required interface.
  2171. //
  2172. ucValue = psUSBControl->pucAltSetting[pUSBRequest->wIndex];
  2173. }
  2174. else
  2175. {
  2176. //
  2177. // An invalid interface number was specified.
  2178. //
  2179. USBDCDStallEP0(ulIndex);
  2180. return;
  2181. }
  2182. }
  2183. //
  2184. // Send the single byte response.
  2185. //
  2186. psUSBControl->ulEP0DataRemain = 1;
  2187. psUSBControl->pEP0Data = &ucValue;
  2188. //
  2189. // Send the single byte response.
  2190. //
  2191. USBDEP0StateTx(ulIndex);
  2192. }
  2193. //*****************************************************************************
  2194. //
  2195. // This function handles the SET_INTERFACE standard USB request.
  2196. //
  2197. // \param pvInstance is the USB device controller instance data.
  2198. // \param pUSBRequest holds the data for this request.
  2199. //
  2200. // This function is called when a standard request for changing the interface
  2201. // is received from the host controller. If this is a valid request the
  2202. // function will call the function specified by the InterfaceChange in the
  2203. // \e pvInstance->psInfo->sCallbacks variable to notify the application that the
  2204. // interface has changed and will pass it the new alternate interface number.
  2205. //
  2206. // \return None.
  2207. //
  2208. //*****************************************************************************
  2209. static void
  2210. USBDSetInterface(void *pvInstance, tUSBRequest *pUSBRequest,
  2211. unsigned int ulIndex)
  2212. {
  2213. const tConfigHeader *psConfig;
  2214. tInterfaceDescriptor *psInterface;
  2215. unsigned int ulLoop;
  2216. unsigned int ulSection;
  2217. unsigned int ulNumInterfaces;
  2218. unsigned char ucInterface;
  2219. tBoolean bRetcode;
  2220. tDeviceInstance *psUSBControl;
  2221. tDeviceInfo *psDevice;
  2222. ASSERT(pUSBRequest != 0);
  2223. ASSERT(pvInstance != 0);
  2224. //
  2225. // Create the device information pointer.
  2226. //
  2227. psUSBControl = (tDeviceInstance *)pvInstance;
  2228. psDevice = psUSBControl->psInfo;
  2229. //
  2230. // Need to ACK the data on end point 0 with last data set as this has no
  2231. // data phase.
  2232. //
  2233. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  2234. //
  2235. // Use the current configuration.
  2236. //
  2237. psConfig = psDevice->ppConfigDescriptors[psUSBControl->ulConfiguration - 1];
  2238. //
  2239. // How many interfaces are included in the descriptor?
  2240. //
  2241. ulNumInterfaces = USBDCDConfigDescGetNum(psConfig,
  2242. USB_DTYPE_INTERFACE);
  2243. //
  2244. // Find the interface descriptor for the supplied interface and alternate
  2245. // setting numbers.
  2246. //
  2247. for(ulLoop = 0; ulLoop < ulNumInterfaces; ulLoop++)
  2248. {
  2249. //
  2250. // Get the next interface descriptor in the configuration descriptor.
  2251. //
  2252. psInterface = USBDCDConfigGetInterface(psConfig, ulLoop, USB_DESC_ANY,
  2253. &ulSection);
  2254. //
  2255. // Is this the required interface with the correct alternate setting?
  2256. //
  2257. if(psInterface &&
  2258. (psInterface->bInterfaceNumber == pUSBRequest->wIndex) &&
  2259. (psInterface->bAlternateSetting == pUSBRequest->wValue))
  2260. {
  2261. ucInterface = psInterface->bInterfaceNumber;
  2262. //
  2263. // Make sure we don't write outside the bounds of the pucAltSetting
  2264. // array (in a debug build, anyway, since this indicates an error
  2265. // in the device descriptor).
  2266. //
  2267. ASSERT(ucInterface < USB_MAX_INTERFACES_PER_DEVICE);
  2268. //
  2269. // If anything changed, reconfigure the endpoints for the new
  2270. // alternate setting.
  2271. //
  2272. if(psUSBControl->pucAltSetting[ucInterface] !=
  2273. psInterface->bAlternateSetting)
  2274. {
  2275. //
  2276. // This is the correct interface descriptor so save the
  2277. // setting.
  2278. //
  2279. psUSBControl->pucAltSetting[ucInterface] =
  2280. psInterface->bAlternateSetting;
  2281. //
  2282. // Reconfigure the endpoints to match the requirements of the
  2283. // new alternate setting for the interface.
  2284. //
  2285. bRetcode = USBDeviceConfigAlternate(0, psConfig, ucInterface,
  2286. psInterface->bAlternateSetting);
  2287. //
  2288. // If there is a callback then notify the application of the
  2289. // change to the alternate interface.
  2290. //
  2291. if(bRetcode && psDevice->sCallbacks.pfnInterfaceChange)
  2292. {
  2293. psDevice->sCallbacks.pfnInterfaceChange(
  2294. psUSBControl->pvInstance,
  2295. pUSBRequest->wIndex,
  2296. pUSBRequest->wValue);
  2297. }
  2298. }
  2299. //
  2300. // All done.
  2301. //
  2302. return;
  2303. }
  2304. }
  2305. //
  2306. // If we drop out of the loop, we didn't find an interface descriptor
  2307. // matching the requested number and alternate setting or there was an
  2308. // error while trying to set up for the new alternate setting.
  2309. //
  2310. USBDCDStallEP0(ulIndex);
  2311. }
  2312. //*****************************************************************************
  2313. //
  2314. // This function handles the SYNC_FRAME standard USB request.
  2315. //
  2316. // \param pvInstance is the USB device controller instance data.
  2317. // \param pUSBRequest holds the data for this request.
  2318. //
  2319. // This is currently a stub function that will stall indicating that the
  2320. // command is not supported.
  2321. //
  2322. // \return None.
  2323. //
  2324. //*****************************************************************************
  2325. static void
  2326. USBDSyncFrame(void *pvInstance, tUSBRequest *pUSBRequest,
  2327. unsigned int ulIndex)
  2328. {
  2329. //
  2330. // Need to ACK the data on end point 0 with last data set as this has no
  2331. // data phase.
  2332. //
  2333. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, true);
  2334. //
  2335. // Not handled yet so stall this request.
  2336. //
  2337. USBDCDStallEP0(ulIndex);
  2338. }
  2339. //*****************************************************************************
  2340. //
  2341. // This internal function handles sending data on endpoint zero.
  2342. //
  2343. // \param ulIndex is the index of the USB controller which is to be
  2344. // initialized.
  2345. //
  2346. // \return None.
  2347. //
  2348. //*****************************************************************************
  2349. static void
  2350. USBDEP0StateTx(unsigned int ulIndex)
  2351. {
  2352. unsigned int ulNumBytes;
  2353. unsigned char *pData;
  2354. ASSERT(ulIndex == 0);
  2355. //
  2356. // In the TX state on endpoint zero.
  2357. //
  2358. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_TX;
  2359. //
  2360. // Set the number of bytes to send this iteration.
  2361. //
  2362. ulNumBytes = g_psUSBDevice[ulIndex].ulEP0DataRemain;
  2363. //
  2364. // Limit individual transfers to 64 bytes.
  2365. //
  2366. if(ulNumBytes > EP0_MAX_PACKET_SIZE)
  2367. {
  2368. ulNumBytes = EP0_MAX_PACKET_SIZE;
  2369. }
  2370. //
  2371. // Save the pointer so that it can be passed to the USBEndpointDataPut()
  2372. // function.
  2373. //
  2374. pData = (unsigned char *)g_psUSBDevice[ulIndex].pEP0Data;
  2375. //
  2376. // Advance the data pointer and counter to the next data to be sent.
  2377. //
  2378. g_psUSBDevice[ulIndex].ulEP0DataRemain -= ulNumBytes;
  2379. g_psUSBDevice[ulIndex].pEP0Data += ulNumBytes;
  2380. //
  2381. // Put the data in the correct FIFO.
  2382. //
  2383. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, pData, ulNumBytes);
  2384. //
  2385. // If this is exactly 64 then don't set the last packet yet.
  2386. //
  2387. if(ulNumBytes == EP0_MAX_PACKET_SIZE)
  2388. {
  2389. //
  2390. // There is more data to send or exactly 64 bytes were sent, this
  2391. // means that there is either more data coming or a null packet needs
  2392. // to be sent to complete the transaction.
  2393. //
  2394. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, USB_TRANS_IN);
  2395. }
  2396. else
  2397. {
  2398. //
  2399. // Now go to the status state and wait for the transmit to complete.
  2400. //
  2401. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_STATUS;
  2402. //
  2403. // Send the last bit of data.
  2404. //
  2405. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0,
  2406. USB_TRANS_IN_LAST);
  2407. //
  2408. // If there is a sent callback then call it.
  2409. //
  2410. if((g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnDataSent) &&
  2411. (g_psUSBDevice[ulIndex].ulOUTDataSize != 0))
  2412. {
  2413. //
  2414. // Call the custom handler.
  2415. //
  2416. g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnDataSent(
  2417. g_psUSBDevice[ulIndex].pvInstance, g_psUSBDevice[ulIndex].ulOUTDataSize,
  2418. ulIndex);
  2419. //
  2420. // There is no longer any data pending to be sent.
  2421. //
  2422. g_psUSBDevice[ulIndex].ulOUTDataSize = 0;
  2423. }
  2424. }
  2425. }
  2426. //*****************************************************************************
  2427. //
  2428. // This internal function handles sending the configuration descriptor on
  2429. // endpoint zero.
  2430. //
  2431. // \param ulIndex is the index of the USB controller which is to be used.
  2432. //
  2433. //
  2434. // \return None.
  2435. //
  2436. //*****************************************************************************
  2437. static void
  2438. USBDEP0StateTxConfig(unsigned int ulIndex)
  2439. {
  2440. unsigned int ulNumBytes;
  2441. unsigned int ulSecBytes;
  2442. unsigned int ulToSend;
  2443. unsigned char *pData;
  2444. tConfigDescriptor sConfDesc;
  2445. const tConfigHeader *psConfig;
  2446. const tConfigSection *psSection;
  2447. ASSERT(ulIndex == 0);
  2448. //
  2449. // In the TX state on endpoint zero.
  2450. //
  2451. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_TX_CONFIG;
  2452. //
  2453. // Find the current configuration descriptor definition.
  2454. //
  2455. psConfig = g_psUSBDevice[ulIndex].psInfo->ppConfigDescriptors[
  2456. g_psUSBDevice[ulIndex].ucConfigIndex];
  2457. //
  2458. // Set the number of bytes to send this iteration.
  2459. //
  2460. ulNumBytes = g_psUSBDevice[ulIndex].ulEP0DataRemain;
  2461. //
  2462. // Limit individual transfers to 64 bytes.
  2463. //
  2464. if(ulNumBytes > EP0_MAX_PACKET_SIZE)
  2465. {
  2466. ulNumBytes = EP0_MAX_PACKET_SIZE;
  2467. }
  2468. //
  2469. // If this is the first call, we need to fix up the total length of the
  2470. // configuration descriptor. This has already been determined and set in
  2471. // g_sUSBDeviceState.ulEP0DataRemain.
  2472. //
  2473. if((g_psUSBDevice[ulIndex].ucSectionOffset == 0) &&
  2474. (g_psUSBDevice[ulIndex].ucConfigSection == 0))
  2475. {
  2476. //
  2477. // Copy the USB configuration descriptor from the beginning of the
  2478. // first section of the current configuration.
  2479. //
  2480. sConfDesc = *(tConfigDescriptor *)g_psUSBDevice[ulIndex].pEP0Data;
  2481. //
  2482. // Update the total size.
  2483. //
  2484. sConfDesc.wTotalLength = (unsigned short)USBDCDConfigDescGetSize(
  2485. psConfig);
  2486. //
  2487. // Write the descriptor to the USB FIFO.
  2488. //
  2489. ulToSend = (ulNumBytes < sizeof(tConfigDescriptor)) ? ulNumBytes :
  2490. sizeof(tConfigDescriptor);
  2491. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0,
  2492. (unsigned char *)&sConfDesc, ulToSend);
  2493. //
  2494. // Did we reach the end of the first section?
  2495. //
  2496. if(psConfig->psSections[0]->ucSize == ulToSend)
  2497. {
  2498. //
  2499. // Update our tracking indices to point to the start of the next
  2500. // section.
  2501. //
  2502. g_psUSBDevice[ulIndex].ucSectionOffset = 0;
  2503. g_psUSBDevice[ulIndex].ucConfigSection = 1;
  2504. }
  2505. else
  2506. {
  2507. //
  2508. // Note that we have sent the first few bytes of the descriptor.
  2509. //
  2510. g_psUSBDevice[ulIndex].ucSectionOffset = (unsigned char)ulToSend;
  2511. }
  2512. //
  2513. // How many bytes do we have remaining to send on this iteration?
  2514. //
  2515. ulToSend = ulNumBytes - ulToSend;
  2516. }
  2517. else
  2518. {
  2519. //
  2520. // Set the number of bytes we still have to send on this call.
  2521. //
  2522. ulToSend = ulNumBytes;
  2523. }
  2524. //
  2525. // Add the relevant number of bytes to the USB FIFO
  2526. //
  2527. while(ulToSend)
  2528. {
  2529. //
  2530. // Get a pointer to the current configuration section.
  2531. //
  2532. psSection = psConfig->psSections[g_psUSBDevice[ulIndex].ucConfigSection];
  2533. //
  2534. // Calculate bytes are available in the current configuration section.
  2535. //
  2536. ulSecBytes = (unsigned int)(psSection->ucSize -
  2537. g_psUSBDevice[ulIndex].ucSectionOffset);
  2538. //
  2539. // Save the pointer so that it can be passed to the
  2540. // USBEndpointDataPut() function.
  2541. //
  2542. pData = (unsigned char *)psSection->pucData +
  2543. g_psUSBDevice[ulIndex].ucSectionOffset;
  2544. //
  2545. // Are there more bytes in this section that we still have to send?
  2546. //
  2547. if(ulSecBytes > ulToSend)
  2548. {
  2549. //
  2550. // Yes - send only the remaining bytes in the transfer.
  2551. //
  2552. ulSecBytes = ulToSend;
  2553. }
  2554. //
  2555. // Put the data in the correct FIFO.
  2556. //
  2557. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, pData, ulSecBytes);
  2558. //
  2559. // Fix up our pointers for the next iteration.
  2560. //
  2561. ulToSend -= ulSecBytes;
  2562. g_psUSBDevice[ulIndex].ucSectionOffset += (unsigned char)ulSecBytes;
  2563. //
  2564. // Have we reached the end of a section?
  2565. //
  2566. if(g_psUSBDevice[ulIndex].ucSectionOffset == psSection->ucSize)
  2567. {
  2568. //
  2569. // Yes - move to the next one.
  2570. //
  2571. g_psUSBDevice[ulIndex].ucConfigSection++;
  2572. g_psUSBDevice[ulIndex].ucSectionOffset = 0;
  2573. }
  2574. }
  2575. //
  2576. // Fix up the number of bytes remaining to be sent and the start pointer.
  2577. //
  2578. g_psUSBDevice[ulIndex].ulEP0DataRemain -= ulNumBytes;
  2579. //
  2580. // If we ran out of bytes in the configuration section, bail and just
  2581. // send out what we have.
  2582. //
  2583. if(psConfig->ucNumSections <= g_psUSBDevice[ulIndex].ucConfigSection)
  2584. {
  2585. g_psUSBDevice[ulIndex].ulEP0DataRemain = 0;
  2586. }
  2587. //
  2588. // If there is no more data don't keep looking or ucConfigSection might
  2589. // overrun the available space.
  2590. //
  2591. if(g_psUSBDevice[ulIndex].ulEP0DataRemain != 0)
  2592. {
  2593. pData =(unsigned char *)
  2594. psConfig->psSections[g_psUSBDevice[ulIndex].ucConfigSection]->pucData;
  2595. ulToSend = g_psUSBDevice[ulIndex].ucSectionOffset;
  2596. g_psUSBDevice[ulIndex].pEP0Data = (pData + ulToSend);
  2597. }
  2598. //
  2599. // If this is exactly 64 then don't set the last packet yet.
  2600. //
  2601. if(ulNumBytes == EP0_MAX_PACKET_SIZE)
  2602. {
  2603. //
  2604. // There is more data to send or exactly 64 bytes were sent, this
  2605. // means that there is either more data coming or a null packet needs
  2606. // to be sent to complete the transaction.
  2607. //
  2608. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0, USB_TRANS_IN);
  2609. }
  2610. else
  2611. {
  2612. //
  2613. // Send the last bit of data.
  2614. //
  2615. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0,
  2616. USB_TRANS_IN_LAST);
  2617. //
  2618. // If there is a sent callback then call it.
  2619. //
  2620. if((g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnDataSent) &&
  2621. (g_psUSBDevice[ulIndex].ulOUTDataSize != 0))
  2622. {
  2623. //
  2624. // Call the custom handler.
  2625. //
  2626. g_psUSBDevice[ulIndex].psInfo->sCallbacks.pfnDataSent(
  2627. g_psUSBDevice[ulIndex].pvInstance, g_psUSBDevice[ulIndex].ulOUTDataSize,
  2628. ulIndex);
  2629. //
  2630. // There is no longer any data pending to be sent.
  2631. //
  2632. g_psUSBDevice[ulIndex].ulOUTDataSize = 0;
  2633. }
  2634. //
  2635. // Now go to the status state and wait for the transmit to complete.
  2636. //
  2637. g_psUSBDevice[ulIndex].eEP0State = USB_STATE_STATUS;
  2638. }
  2639. }
  2640. //*****************************************************************************
  2641. //
  2642. // The internal USB device interrupt handler.
  2643. //
  2644. // \param ulIndex is the USB controller associated with this interrupt.
  2645. // \param ulStatus is the current interrupt status as read via a call to
  2646. // USBIntStatusControl().
  2647. //
  2648. // This function is called from either \e USB0DualModeIntHandler() or
  2649. // \e USB0DeviceIntHandler() to process USB interrupts when in device mode.
  2650. // This handler will branch the interrupt off to the appropriate application or
  2651. // stack handlers depending on the current status of the USB controller.
  2652. //
  2653. // The two-tiered structure for the interrupt handler ensures that it is
  2654. // possible to use the same handler code in both device and OTG modes and
  2655. // means that host code can be excluded from applications that only require
  2656. // support for USB device mode operation.
  2657. //
  2658. // \return None.
  2659. //
  2660. //*****************************************************************************
  2661. void
  2662. USBDeviceIntHandlerInternal(unsigned int ulIndex, unsigned int ulStatus,
  2663. unsigned int *endPStatus)
  2664. {
  2665. static unsigned int ulSOFDivide = 0;
  2666. tDeviceInfo *psInfo;
  2667. void *pvInstance;
  2668. unsigned int epStatus;
  2669. unsigned int epnStatus = 0;
  2670. //
  2671. // Get the controller interrupt status from the wrapper registers
  2672. // Only the lower 16bits contain EP intr data
  2673. //
  2674. if(endPStatus == NULL)
  2675. {
  2676. epStatus= 0xFFFF & ulStatus;
  2677. ulStatus >>=16;
  2678. }
  2679. else
  2680. {
  2681. epStatus = *endPStatus;
  2682. }
  2683. ulStatus |= USBIntStatusControl(g_USBInstance[ulIndex].uiBaseAddr);
  2684. //
  2685. // If device initialization has not been performed then just disconnect
  2686. // from the USB bus and return from the handler.
  2687. //
  2688. if(g_psUSBDevice[ulIndex].psInfo == 0)
  2689. {
  2690. USBDevDisconnect(g_USBInstance[ulIndex].uiBaseAddr);
  2691. return;
  2692. }
  2693. psInfo = g_psUSBDevice[ulIndex].psInfo;
  2694. pvInstance = g_psUSBDevice[ulIndex].pvInstance;
  2695. //
  2696. // Received a reset from the host.
  2697. //
  2698. if(ulStatus & USB_INTCTRL_RESET)
  2699. {
  2700. USBDeviceEnumResetHandler(&g_psUSBDevice[ulIndex]);
  2701. }
  2702. //
  2703. // Suspend was signaled on the bus.
  2704. //
  2705. if(ulStatus & USB_INTCTRL_SUSPEND)
  2706. {
  2707. //
  2708. // Call the SuspendHandler() if it was specified.
  2709. //
  2710. if(psInfo->sCallbacks.pfnSuspendHandler)
  2711. {
  2712. psInfo->sCallbacks.pfnSuspendHandler(pvInstance);
  2713. }
  2714. }
  2715. //
  2716. // Resume was signaled on the bus.
  2717. //
  2718. if(ulStatus & USB_INTCTRL_RESUME)
  2719. {
  2720. //
  2721. // Call the ResumeHandler() if it was specified.
  2722. //
  2723. if(psInfo->sCallbacks.pfnResumeHandler)
  2724. {
  2725. psInfo->sCallbacks.pfnResumeHandler(pvInstance);
  2726. }
  2727. }
  2728. //
  2729. // USB device was disconnected.
  2730. //
  2731. if(ulStatus & USB_INTCTRL_DISCONNECT)
  2732. {
  2733. //
  2734. // Call the DisconnectHandler() if it was specified.
  2735. //
  2736. if(psInfo->sCallbacks.pfnDisconnectHandler)
  2737. {
  2738. psInfo->sCallbacks.pfnDisconnectHandler(pvInstance);
  2739. }
  2740. }
  2741. //
  2742. // Start of Frame was received.
  2743. //
  2744. if(ulStatus & USB_INTCTRL_SOF)
  2745. {
  2746. //
  2747. // Increment the global Start of Frame counter.
  2748. //
  2749. g_ulUSBSOFCount++;
  2750. //
  2751. // Increment our SOF divider.
  2752. //
  2753. ulSOFDivide++;
  2754. //
  2755. // Handle resume signaling if required.
  2756. //
  2757. USBDeviceResumeTickHandler(ulIndex);
  2758. //
  2759. // Have we counted enough SOFs to allow us to call the tick function?
  2760. //
  2761. if(ulSOFDivide == USB_SOF_TICK_DIVIDE)
  2762. {
  2763. //
  2764. // Yes - reset the divider and call the SOF tick handler.
  2765. //
  2766. ulSOFDivide = 0;
  2767. InternalUSBStartOfFrameTick(USB_SOF_TICK_DIVIDE, ulIndex);
  2768. }
  2769. }
  2770. //
  2771. // Handle end point 0 interrupts.
  2772. //
  2773. if(epStatus & USB_INTEP_0)
  2774. {
  2775. USBDeviceEnumHandler(&g_psUSBDevice[ulIndex], ulIndex);
  2776. }
  2777. /*
  2778. converting the epstatus(Wrapper register data) to ulStatus( MUSB register data)
  2779. */
  2780. if(endPStatus == NULL)
  2781. {
  2782. epnStatus = 0xFF & epStatus;
  2783. epnStatus = epnStatus | ((0xFF00 & epStatus)<<8);
  2784. }
  2785. else
  2786. {
  2787. epnStatus = epStatus;
  2788. }
  2789. //
  2790. // Because there is no way to detect if a uDMA interrupt has occurred, the
  2791. // check for and endpoint callback and call it if it is available.
  2792. //
  2793. if(psInfo->sCallbacks.pfnEndpointHandler)
  2794. {
  2795. psInfo->sCallbacks.pfnEndpointHandler(pvInstance, epnStatus, ulIndex);
  2796. }
  2797. }
  2798. //*****************************************************************************
  2799. //
  2800. // Close the Doxygen group.
  2801. //! @}
  2802. //
  2803. //*****************************************************************************