usbdcomp.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. //****************************************************************************
  2. //
  3. // usbdcomp.c - USB composite device class driver.
  4. //
  5. // Copyright (c) 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_memmap.h"
  26. #include "hw_types.h"
  27. #include "debug.h"
  28. #include "interrupt.h"
  29. //#include "sysctl.h"
  30. #include "usb.h"
  31. //#include "udma.h"
  32. #include "usblib.h"
  33. #include "usb-ids.h"
  34. #include "usbcdc.h"
  35. #include "usbdevice.h"
  36. #include "usbdcdc.h"
  37. #include "usbdcomp.h"
  38. #include "usblibpriv.h"
  39. //****************************************************************************
  40. //
  41. //! \addtogroup composite_device_class_api
  42. //! @{
  43. //
  44. //****************************************************************************
  45. //****************************************************************************
  46. //
  47. // Device Descriptor. This is stored in RAM to allow several fields to be
  48. // changed at runtime based on the client's requirements.
  49. //
  50. //****************************************************************************
  51. static unsigned char g_pCompDeviceDescriptor[] =
  52. {
  53. 18, // Size of this structure.
  54. USB_DTYPE_DEVICE, // Type of this structure.
  55. USBShort(0x110), // USB version 1.1 (if we say 2.0, hosts assume
  56. // high-speed - see USB 2.0 spec 9.2.6.6)
  57. USB_CLASS_MISC, // USB Device Class (spec 5.1.1)
  58. USB_MISC_SUBCLASS_COMMON, // USB Device Sub-class (spec 5.1.1)
  59. USB_MISC_PROTOCOL_IAD, // USB Device protocol (spec 5.1.1)
  60. 64, // Maximum packet size for default pipe.
  61. USBShort(0), // Vendor ID (filled in during USBDCompositeInit).
  62. USBShort(0), // Product ID (filled in during USBDCompositeInit).
  63. USBShort(0x100), // Device Version BCD.
  64. 1, // Manufacturer string identifier.
  65. 2, // Product string identifier.
  66. 3, // Product serial number.
  67. 1 // Number of configurations.
  68. };
  69. //****************************************************************************
  70. //
  71. // Composite class device configuration descriptor.
  72. //
  73. // It is vital that the configuration descriptor bConfigurationValue field
  74. // (byte 6) is 1 for the first configuration and increments by 1 for each
  75. // additional configuration defined here. This relationship is assumed in the
  76. // device stack for simplicity even though the USB 2.0 specification imposes
  77. // no such restriction on the bConfigurationValue values.
  78. //
  79. // Note that this structure is deliberately located in RAM since we need to
  80. // be able to patch some values in it based on client requirements.
  81. //
  82. //****************************************************************************
  83. static const unsigned char g_pCompConfigDescriptor[] =
  84. {
  85. //
  86. // Configuration descriptor header.
  87. //
  88. 9, // Size of the configuration descriptor.
  89. USB_DTYPE_CONFIGURATION, // Type of this descriptor.
  90. USBShort(0), // The total size of this full structure.
  91. 0, // The number of interfaces in this
  92. // configuration, this will be filled by
  93. // the class as it discovers all classes
  94. // supported.
  95. 1, // The unique value for this configuration.
  96. 0, // The string identifier that describes this
  97. // configuration.
  98. USB_CONF_ATTR_BUS_PWR, // .
  99. 250, // The maximum power in 2mA increments.
  100. };
  101. //****************************************************************************
  102. //
  103. // Various internal handlers needed by this class.
  104. //
  105. //****************************************************************************
  106. static void HandleDisconnect(void *pvInstance);
  107. static void InterfaceChange(void *pvInstance, unsigned char ucInterfaceNum,
  108. unsigned char ucAlternateSetting);
  109. static void ConfigChangeHandler(void *pvInstance, unsigned int ulValue,
  110. unsigned int ulIndex);
  111. static void DataSent(void *pvInstance, unsigned int ulInfo,
  112. unsigned int ulIndex);
  113. static void DataReceived(void *pvInstance, unsigned int ulInfo,
  114. unsigned int ulIndex);
  115. static void HandleEndpoints(void *pvInstance, unsigned int ulStatus,
  116. unsigned int ulIndex);
  117. static void HandleRequests(void *pvInstance, tUSBRequest *pUSBRequest,
  118. unsigned int ulIndex);
  119. static void SuspendHandler(void *pvInstance);
  120. static void ResumeHandler(void *pvInstance);
  121. static void ResetHandler(void *pvInstance);
  122. static void GetDescriptor(void *pvInstance, tUSBRequest *pUSBRequest,
  123. unsigned int ulIndex);
  124. //****************************************************************************
  125. //
  126. // Configuration Descriptor.
  127. //
  128. //****************************************************************************
  129. tConfigHeader *g_pCompConfigDescriptors[1];
  130. //****************************************************************************
  131. //
  132. // The FIFO configuration for USB mass storage class device.
  133. //
  134. //****************************************************************************
  135. tFIFOConfig g_sUSBCompositeFIFOConfig =
  136. {
  137. //
  138. // IN endpoints.
  139. //
  140. {
  141. { 1, false, USB_EP_DEV_IN },
  142. { 1, false, USB_EP_DEV_IN },
  143. { 1, false, USB_EP_DEV_IN },
  144. { 1, false, USB_EP_DEV_IN },
  145. { 1, false, USB_EP_DEV_IN },
  146. { 1, false, USB_EP_DEV_IN },
  147. { 1, false, USB_EP_DEV_IN },
  148. { 1, false, USB_EP_DEV_IN },
  149. { 1, false, USB_EP_DEV_IN },
  150. { 1, false, USB_EP_DEV_IN },
  151. { 1, false, USB_EP_DEV_IN },
  152. { 1, false, USB_EP_DEV_IN },
  153. { 1, false, USB_EP_DEV_IN },
  154. { 1, false, USB_EP_DEV_IN },
  155. { 1, false, USB_EP_DEV_IN }
  156. },
  157. //
  158. // OUT endpoints.
  159. //
  160. {
  161. { 1, false, USB_EP_DEV_OUT },
  162. { 1, false, USB_EP_DEV_OUT },
  163. { 1, false, USB_EP_DEV_OUT },
  164. { 1, false, USB_EP_DEV_OUT },
  165. { 1, false, USB_EP_DEV_OUT },
  166. { 1, false, USB_EP_DEV_OUT },
  167. { 1, false, USB_EP_DEV_OUT },
  168. { 1, false, USB_EP_DEV_OUT },
  169. { 1, false, USB_EP_DEV_OUT },
  170. { 1, false, USB_EP_DEV_OUT },
  171. { 1, false, USB_EP_DEV_OUT },
  172. { 1, false, USB_EP_DEV_OUT },
  173. { 1, false, USB_EP_DEV_OUT },
  174. { 1, false, USB_EP_DEV_OUT },
  175. { 1, false, USB_EP_DEV_OUT }
  176. },
  177. };
  178. //****************************************************************************
  179. //
  180. // The device information structure for the USB Composite device.
  181. //
  182. //****************************************************************************
  183. tDeviceInfo g_sCompositeDeviceInfo =
  184. {
  185. //
  186. // Device event handler callbacks.
  187. //
  188. {
  189. //
  190. // GetDescriptor
  191. //
  192. GetDescriptor,
  193. //
  194. // RequestHandler
  195. //
  196. HandleRequests,
  197. //
  198. // InterfaceChange
  199. //
  200. InterfaceChange,
  201. //
  202. // ConfigChange
  203. //
  204. ConfigChangeHandler,
  205. //
  206. // DataReceived
  207. //
  208. DataReceived,
  209. //
  210. // DataSentCallback
  211. //
  212. DataSent,
  213. //
  214. // ResetHandler
  215. //
  216. ResetHandler,
  217. //
  218. // SuspendHandler
  219. //
  220. SuspendHandler,
  221. //
  222. // ResumeHandler
  223. //
  224. ResumeHandler,
  225. //
  226. // DisconnectHandler
  227. //
  228. HandleDisconnect,
  229. //
  230. // EndpointHandler
  231. //
  232. HandleEndpoints
  233. },
  234. g_pCompDeviceDescriptor,
  235. (const tConfigHeader **)g_pCompConfigDescriptors,
  236. 0,
  237. 0,
  238. &g_sUSBCompositeFIFOConfig
  239. };
  240. //****************************************************************************
  241. //
  242. // This function will check if any device classes need a get descriptor
  243. // handler called.
  244. //
  245. //****************************************************************************
  246. static void
  247. GetDescriptor(void *pvInstance, tUSBRequest *pUSBRequest, unsigned int ulIndex)
  248. {
  249. unsigned int ulIdx;
  250. const tDeviceInfo *pDeviceInfo;
  251. tUSBDCompositeDevice *psDevice;
  252. //
  253. // Create the device instance pointer.
  254. //
  255. psDevice = (tUSBDCompositeDevice *)pvInstance;
  256. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  257. {
  258. //
  259. // Create the individual device pointer.
  260. //
  261. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  262. if(pDeviceInfo->sCallbacks.pfnGetDescriptor)
  263. {
  264. pDeviceInfo->sCallbacks.pfnGetDescriptor(
  265. psDevice->psDevices[ulIdx].pvInstance, pUSBRequest, ulIndex);
  266. }
  267. }
  268. }
  269. //****************************************************************************
  270. //
  271. // This function will check if any device classes need an suspend handler
  272. // called.
  273. //
  274. //****************************************************************************
  275. static void
  276. SuspendHandler(void *pvInstance)
  277. {
  278. unsigned int ulIdx;
  279. tUSBDCompositeDevice *psDevice;
  280. const tDeviceInfo *pDeviceInfo;
  281. void *pvDeviceInst;
  282. ASSERT(pvInstance != 0);
  283. //
  284. // Create the device instance pointer.
  285. //
  286. psDevice = (tUSBDCompositeDevice *)pvInstance;
  287. //
  288. // Inform the application that the device has resumed.
  289. //
  290. if(psDevice->pfnCallback)
  291. {
  292. psDevice->pfnCallback(pvInstance, USB_EVENT_SUSPEND, 0, 0);
  293. }
  294. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  295. {
  296. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  297. pvDeviceInst = psDevice->psDevices[ulIdx].pvInstance;
  298. if(pDeviceInfo->sCallbacks.pfnSuspendHandler)
  299. {
  300. pDeviceInfo->sCallbacks.pfnSuspendHandler(pvDeviceInst);
  301. }
  302. }
  303. }
  304. //****************************************************************************
  305. //
  306. // This function will check if any device classes need an resume handler
  307. // called.
  308. //
  309. //****************************************************************************
  310. static void
  311. ResumeHandler(void *pvInstance)
  312. {
  313. unsigned int ulIdx;
  314. tUSBDCompositeDevice *psDevice;
  315. const tDeviceInfo *pDeviceInfo;
  316. void *pvDeviceInst;
  317. ASSERT(pvInstance != 0);
  318. //
  319. // Create the device instance pointer.
  320. //
  321. psDevice = (tUSBDCompositeDevice *)pvInstance;
  322. //
  323. // Inform the application that the device has resumed.
  324. //
  325. if(psDevice->pfnCallback)
  326. {
  327. psDevice->pfnCallback(pvInstance, USB_EVENT_RESUME, 0, 0);
  328. }
  329. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  330. {
  331. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  332. pvDeviceInst = psDevice->psDevices[ulIdx].pvInstance;
  333. if(pDeviceInfo->sCallbacks.pfnResumeHandler)
  334. {
  335. pDeviceInfo->sCallbacks.pfnResumeHandler(pvDeviceInst);
  336. }
  337. }
  338. }
  339. //****************************************************************************
  340. //
  341. // This function will check if any device classes need an reset handler
  342. // called.
  343. //
  344. //****************************************************************************
  345. static void
  346. ResetHandler(void *pvInstance)
  347. {
  348. unsigned int ulIdx;
  349. tUSBDCompositeDevice *psDevice;
  350. const tDeviceInfo *pDeviceInfo;
  351. void *pvDeviceInst;
  352. ASSERT(pvInstance != 0);
  353. //
  354. // Create the device instance pointer.
  355. //
  356. psDevice = (tUSBDCompositeDevice *)pvInstance;
  357. //
  358. // Inform the application that the device has been connected.
  359. //
  360. if(psDevice->pfnCallback)
  361. {
  362. psDevice->pfnCallback(pvInstance, USB_EVENT_CONNECTED, 0, 0);
  363. }
  364. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  365. {
  366. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  367. pvDeviceInst = psDevice->psDevices[ulIdx].pvInstance;
  368. if(pDeviceInfo->sCallbacks.pfnResetHandler)
  369. {
  370. pDeviceInfo->sCallbacks.pfnResetHandler(pvDeviceInst);
  371. }
  372. }
  373. }
  374. //****************************************************************************
  375. //
  376. // This function is called to handle data being set to the host so that the
  377. // application callback can be called when the data has been transferred.
  378. //
  379. //****************************************************************************
  380. static void
  381. DataSent(void *pvInstance, unsigned int ulInfo, unsigned int ulIndex)
  382. {
  383. unsigned int ulIdx;
  384. const tDeviceInfo *pDeviceInfo;
  385. tUSBDCompositeDevice *psDevice;
  386. //
  387. // Create the device instance pointer.
  388. //
  389. psDevice = (tUSBDCompositeDevice *)pvInstance;
  390. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  391. {
  392. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  393. if(pDeviceInfo->sCallbacks.pfnDataSent)
  394. {
  395. pDeviceInfo->sCallbacks.pfnDataSent(
  396. psDevice->psDevices[ulIdx].pvInstance, ulInfo, ulIndex);
  397. }
  398. }
  399. }
  400. //****************************************************************************
  401. //
  402. // This function is called to handle data being received back from the host so
  403. // that the application callback can be called when the new data is ready.
  404. //
  405. //****************************************************************************
  406. static void
  407. DataReceived(void *pvInstance, unsigned int ulInfo, unsigned int ulIndex)
  408. {
  409. unsigned int ulIdx;
  410. const tDeviceInfo *pDeviceInfo;
  411. tUSBDCompositeDevice *psDevice;
  412. //
  413. // Create the device instance pointer.
  414. //
  415. psDevice = (tUSBDCompositeDevice *)pvInstance;
  416. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  417. {
  418. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  419. if(pDeviceInfo->sCallbacks.pfnDataReceived)
  420. {
  421. pDeviceInfo->sCallbacks.pfnDataReceived(
  422. psDevice->psDevices[ulIdx].pvInstance, ulInfo, ulIndex);
  423. }
  424. }
  425. }
  426. //****************************************************************************
  427. //
  428. // This function will check if any device classes need an endpoint handler
  429. // called.
  430. //
  431. //****************************************************************************
  432. static void
  433. HandleEndpoints(void *pvInstance, unsigned int ulStatus,
  434. unsigned int ulIndex)
  435. {
  436. unsigned int ulIdx;
  437. const tDeviceInfo *pDeviceInfo;
  438. tUSBDCompositeDevice *psDevice;
  439. //
  440. // Create the device instance pointer.
  441. //
  442. psDevice = (tUSBDCompositeDevice *)pvInstance;
  443. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  444. {
  445. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  446. if(pDeviceInfo->sCallbacks.pfnEndpointHandler)
  447. {
  448. pDeviceInfo->sCallbacks.pfnEndpointHandler(
  449. psDevice->psDevices[ulIdx].pvInstance, ulStatus, ulIndex);
  450. }
  451. }
  452. }
  453. //****************************************************************************
  454. //
  455. // This function is called by the USB device stack whenever the device is
  456. // disconnected from the host.
  457. //
  458. //****************************************************************************
  459. static void
  460. HandleDisconnect(void *pvInstance)
  461. {
  462. unsigned int ulIdx;
  463. const tDeviceInfo *pDeviceInfo;
  464. tUSBDCompositeDevice *psDevice;
  465. ASSERT(pvInstance != 0);
  466. //
  467. // Create the device instance pointer.
  468. //
  469. psDevice = (tUSBDCompositeDevice *)pvInstance;
  470. //
  471. // Inform the application that the device has been disconnected.
  472. //
  473. if(psDevice->pfnCallback)
  474. {
  475. psDevice->pfnCallback(pvInstance, USB_EVENT_DISCONNECTED, 0, 0);
  476. }
  477. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  478. {
  479. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  480. if(pDeviceInfo->sCallbacks.pfnEndpointHandler)
  481. {
  482. pDeviceInfo->sCallbacks.pfnDisconnectHandler(
  483. psDevice->psDevices[ulIdx].pvInstance);
  484. }
  485. }
  486. }
  487. //****************************************************************************
  488. //
  489. // This function is called by the USB device stack whenever the device
  490. // interface changes. It will be passed on to the device classes if they have
  491. // a handler for this function.
  492. //
  493. //****************************************************************************
  494. static void
  495. InterfaceChange(void *pvInstance, unsigned char ucInterfaceNum,
  496. unsigned char ucAlternateSetting)
  497. {
  498. unsigned int ulIdx;
  499. const tDeviceInfo *pDeviceInfo;
  500. tUSBDCompositeDevice *psDevice;
  501. ASSERT(pvInstance != 0);
  502. //
  503. // Create the device instance pointer.
  504. //
  505. psDevice = (tUSBDCompositeDevice *)pvInstance;
  506. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  507. {
  508. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  509. if(pDeviceInfo->sCallbacks.pfnInterfaceChange)
  510. {
  511. pDeviceInfo->sCallbacks.pfnInterfaceChange(
  512. psDevice->psDevices[ulIdx].pvInstance, ucInterfaceNum,
  513. ucAlternateSetting);
  514. }
  515. }
  516. }
  517. //****************************************************************************
  518. //
  519. // This function is called by the USB device stack whenever the device
  520. // configuration changes. It will be passed on to the device classes if they
  521. // have a handler for this function.
  522. //
  523. //****************************************************************************
  524. static void
  525. ConfigChangeHandler(void *pvInstance, unsigned int ulValue,
  526. unsigned int ulIndex)
  527. {
  528. unsigned int ulIdx;
  529. const tDeviceInfo *pDeviceInfo;
  530. tUSBDCompositeDevice *psDevice;
  531. ASSERT(pvInstance != 0);
  532. //
  533. // Create the device instance pointer.
  534. //
  535. psDevice = (tUSBDCompositeDevice *)pvInstance;
  536. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  537. {
  538. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  539. if(pDeviceInfo->sCallbacks.pfnConfigChange)
  540. {
  541. pDeviceInfo->sCallbacks.pfnConfigChange(
  542. psDevice->psDevices[ulIdx].pvInstance, ulValue, ulIndex);
  543. }
  544. }
  545. }
  546. //****************************************************************************
  547. //
  548. // This function is called by the USB device stack whenever a non-standard
  549. // request is received.
  550. //
  551. // \param pvInstance
  552. // \param pUSBRequest points to the request received.
  553. //
  554. // This call will be passed on to the device classes if they have a handler
  555. // for this function.
  556. //
  557. // \return None.
  558. //
  559. //****************************************************************************
  560. static void
  561. HandleRequests(void *pvInstance, tUSBRequest *pUSBRequest,
  562. unsigned int ulIndex)
  563. {
  564. unsigned int ulIdx;
  565. const tDeviceInfo *pDeviceInfo;
  566. tUSBDCompositeDevice *psDevice;
  567. ASSERT(pvInstance != 0);
  568. //
  569. // Create the device instance pointer.
  570. //
  571. psDevice = (tUSBDCompositeDevice *)pvInstance;
  572. for(ulIdx = 0; ulIdx < psDevice->ulNumDevices; ulIdx++)
  573. {
  574. pDeviceInfo = psDevice->psDevices[ulIdx].psDevice;
  575. if(pDeviceInfo->sCallbacks.pfnRequestHandler)
  576. {
  577. pDeviceInfo->sCallbacks.pfnRequestHandler(
  578. psDevice->psDevices[ulIdx].pvInstance, pUSBRequest, ulIndex);
  579. }
  580. }
  581. }
  582. //****************************************************************************
  583. //
  584. // This function handles sending interface number changes to device instances.
  585. //
  586. //****************************************************************************
  587. static void
  588. CompositeIfaceChange(tCompositeEntry *pCompDevice, unsigned char ucOld,
  589. unsigned char ucNew)
  590. {
  591. unsigned char pucInterfaces[2];
  592. if(pCompDevice->psDevice->sCallbacks.pfnDeviceHandler)
  593. {
  594. //
  595. // Create the data to pass to the device handler.
  596. //
  597. pucInterfaces[0] = ucOld;
  598. pucInterfaces[1] = ucNew;
  599. //
  600. // Call the device handler to inform the class of the interface number
  601. // change.
  602. //
  603. pCompDevice->psDevice->sCallbacks.pfnDeviceHandler(
  604. pCompDevice->pvInstance, USB_EVENT_COMP_IFACE_CHANGE,
  605. (void *)pucInterfaces);
  606. }
  607. }
  608. //****************************************************************************
  609. //
  610. // This function handles sending endpoint number changes to device instances.
  611. //
  612. //****************************************************************************
  613. static void
  614. CompositeEPChange(tCompositeEntry *pCompDevice, unsigned char ucOld,
  615. unsigned char ucNew)
  616. {
  617. unsigned char pucInterfaces[2];
  618. unsigned char ucIndex;
  619. if(pCompDevice->psDevice->sCallbacks.pfnDeviceHandler)
  620. {
  621. //
  622. // Create the data to pass to the device handler.
  623. //
  624. pucInterfaces[0] = ucOld;
  625. pucInterfaces[1] = ucNew;
  626. ucNew--;
  627. if(ucOld & USB_RTYPE_DIR_IN)
  628. {
  629. ucIndex = (ucOld & ~USB_RTYPE_DIR_IN) - 1;
  630. g_sUSBCompositeFIFOConfig.sIn[ucNew].bDoubleBuffer =
  631. pCompDevice->psDevice->psFIFOConfig->sIn[ucIndex].bDoubleBuffer;
  632. g_sUSBCompositeFIFOConfig.sIn[ucNew].cMultiplier =
  633. pCompDevice->psDevice->psFIFOConfig->sIn[ucIndex].cMultiplier;
  634. g_sUSBCompositeFIFOConfig.sIn[ucNew].usEPFlags =
  635. pCompDevice->psDevice->psFIFOConfig->sIn[ucIndex].usEPFlags;
  636. }
  637. else
  638. {
  639. ucIndex = ucOld - 1;
  640. g_sUSBCompositeFIFOConfig.sOut[ucNew].bDoubleBuffer =
  641. pCompDevice->psDevice->psFIFOConfig->sOut[ucIndex].bDoubleBuffer;
  642. g_sUSBCompositeFIFOConfig.sOut[ucNew].cMultiplier =
  643. pCompDevice->psDevice->psFIFOConfig->sOut[ucIndex].cMultiplier;
  644. g_sUSBCompositeFIFOConfig.sOut[ucNew].usEPFlags =
  645. pCompDevice->psDevice->psFIFOConfig->sOut[ucIndex].usEPFlags;
  646. }
  647. //
  648. // Call the device handler to inform the class of the interface number
  649. // change.
  650. //
  651. pCompDevice->psDevice->sCallbacks.pfnDeviceHandler(
  652. pCompDevice->pvInstance, USB_EVENT_COMP_EP_CHANGE,
  653. (void *)pucInterfaces);
  654. }
  655. }
  656. //****************************************************************************
  657. //
  658. // This function merges the configuration descriptors into a single multiple
  659. // instance device.
  660. //
  661. //****************************************************************************
  662. unsigned int
  663. BuildCompositeDescriptor(tUSBDCompositeDevice *psCompDevice)
  664. {
  665. unsigned int ulIdx, ulOffset, ulCPIdx, ulFixINT, ulDev;
  666. unsigned short usTotalLength, usBytes;
  667. unsigned char ucInterface, ucINEndpoint, ucOUTEndpoint;
  668. unsigned char *pucData, *pucConfig;
  669. const tConfigHeader *pConfigHeader;
  670. tDescriptorHeader *psHeader;
  671. const unsigned char *pucDescriptor;
  672. tInterfaceDescriptor *psInterface;
  673. tEndpointDescriptor *psEndpoint;
  674. const tDeviceInfo *psDevice;
  675. //
  676. // Save the number of devices to look through.
  677. //
  678. ulDev = 0;
  679. ulIdx = 0;
  680. ucInterface = 0;
  681. ucINEndpoint = 1;
  682. ucOUTEndpoint = 1;
  683. ulOffset = 0;
  684. ulFixINT = 0;
  685. //
  686. // This puts the first section pointer in the first entry in the list
  687. // of sections.
  688. //
  689. psCompDevice->psPrivateData->ppsCompSections[0] =
  690. &psCompDevice->psPrivateData->psCompSections[0];
  691. //
  692. // Put the pointer to this instances configuration descriptor into the
  693. // front of the list.
  694. //
  695. psCompDevice->psPrivateData->ppsCompSections[0]->pucData =
  696. (unsigned char *)&psCompDevice->psPrivateData->sConfigDescriptor;
  697. psCompDevice->psPrivateData->ppsCompSections[0]->ucSize =
  698. psCompDevice->psPrivateData->sConfigDescriptor.bLength;
  699. //
  700. // The configuration descriptor is 9 bytes so initialize the total length
  701. // to 9 bytes.
  702. //
  703. usTotalLength = 9;
  704. //
  705. // Copy the section pointer into the section array for the composite
  706. // device. This is awkward but is required given the definition
  707. // of the structures.
  708. //
  709. psCompDevice->psPrivateData->ppsCompSections[1] =
  710. &psCompDevice->psPrivateData->psCompSections[1];
  711. //
  712. // Copy the point to the application supplied space into the section list.
  713. //
  714. psCompDevice->psPrivateData->ppsCompSections[1]->ucSize = 0;
  715. psCompDevice->psPrivateData->ppsCompSections[1]->pucData =
  716. psCompDevice->psPrivateData->pucData;
  717. //
  718. // Create a local pointer to the data that is used to copy data from
  719. // the other devices into the composite descriptor.
  720. //
  721. pucData = psCompDevice->psPrivateData->pucData;
  722. while(ulDev < psCompDevice->ulNumDevices)
  723. {
  724. //
  725. // Save the current starting address of this descriptor.
  726. //
  727. pucConfig = pucData + ulOffset;
  728. //
  729. // Create a local pointer to the configuration header.
  730. //
  731. psDevice = psCompDevice->psDevices[ulDev].psDevice;
  732. pConfigHeader = psDevice->ppConfigDescriptors[0];
  733. //
  734. // Loop through the sections, skipping the first which is always the
  735. // configuration descriptor for the device.
  736. //
  737. for(ulIdx = 1; ulIdx < pConfigHeader->ucNumSections; ulIdx++)
  738. {
  739. //
  740. // Initialize the local offset in this descriptor.
  741. //
  742. usBytes = 0;
  743. //
  744. // Get a pointer to the configuration descriptor.
  745. //
  746. pucDescriptor = pConfigHeader->psSections[ulIdx]->pucData;
  747. //
  748. // Bounds check the allocated space and return if there is not
  749. // enough space.
  750. //
  751. if(ulOffset > psCompDevice->psPrivateData->ulDataSize)
  752. {
  753. return(1);
  754. }
  755. //
  756. // Copy the descriptor from the device into the descriptor list.
  757. //
  758. for(ulCPIdx = 0;
  759. ulCPIdx < pConfigHeader->psSections[ulIdx]->ucSize;
  760. ulCPIdx++)
  761. {
  762. pucData[ulCPIdx + ulOffset] = pucDescriptor[ulCPIdx];
  763. }
  764. //
  765. // Read out the descriptors in this section.
  766. //
  767. while(usBytes < pConfigHeader->psSections[ulIdx]->ucSize)
  768. {
  769. //
  770. // Create a descriptor header pointer.
  771. //
  772. psHeader = (tDescriptorHeader *)&pucData[ulOffset + usBytes];
  773. //
  774. // Check for interface descriptors and modify the numbering to
  775. // match the composite device.
  776. //
  777. if(psHeader->bDescriptorType == USB_DTYPE_INTERFACE)
  778. {
  779. psInterface = (tInterfaceDescriptor *)psHeader;
  780. //
  781. // See if this is an alternate setting or the initial
  782. // setting.
  783. //
  784. if(psInterface->bAlternateSetting != 0)
  785. {
  786. //
  787. // If this is an alternate setting then use the
  788. // previous interface number because the current one
  789. // has already been incremented.
  790. //
  791. psInterface->bInterfaceNumber = ucInterface - 1;
  792. }
  793. else
  794. {
  795. //
  796. // Notify the class that it's interface number has
  797. // changed.
  798. //
  799. CompositeIfaceChange(&psCompDevice->psDevices[ulDev],
  800. psInterface->bInterfaceNumber,
  801. ucInterface);
  802. //
  803. // This was the non-alternate setting so save the
  804. // value and move to the next interface number.
  805. //
  806. psInterface->bInterfaceNumber = ucInterface;
  807. //
  808. // No strings allowed on interface descriptors for
  809. // composite devices.
  810. //
  811. psInterface->iInterface = 0;
  812. ucInterface++;
  813. }
  814. }
  815. //
  816. // Check for endpoint descriptors and modify the numbering to
  817. // match the composite device.
  818. //
  819. else if(psHeader->bDescriptorType == USB_DTYPE_ENDPOINT)
  820. {
  821. psEndpoint = (tEndpointDescriptor *)psHeader;
  822. //
  823. // Check if this is an IN or OUT endpoint.
  824. //
  825. if(psEndpoint->bEndpointAddress & USB_RTYPE_DIR_IN)
  826. {
  827. //
  828. // Check if this is the special Fixed Interrupt class
  829. // and this is the interrupt endpoint.
  830. //
  831. if(((psEndpoint->bmAttributes & USB_EP_ATTR_TYPE_M) ==
  832. USB_EP_ATTR_INT) &&
  833. (psCompDevice->usPID == USB_PID_COMP_SERIAL))
  834. {
  835. //
  836. // Check if the Fixed Interrupt endpoint has been
  837. // set yet.
  838. //
  839. if(ulFixINT == 0)
  840. {
  841. //
  842. // Allocate he fixed interrupt endpoint and
  843. // save its number.
  844. //
  845. ulFixINT = ucINEndpoint++;
  846. }
  847. CompositeEPChange(&psCompDevice->psDevices[ulDev],
  848. psEndpoint->bEndpointAddress,
  849. ulFixINT);
  850. psEndpoint->bEndpointAddress = ulFixINT |
  851. USB_RTYPE_DIR_IN;
  852. }
  853. else
  854. {
  855. //
  856. // Notify the class that it's interface number has
  857. // changed.
  858. //
  859. CompositeEPChange(&psCompDevice->psDevices[ulDev],
  860. psEndpoint->bEndpointAddress,
  861. ucINEndpoint);
  862. psEndpoint->bEndpointAddress = ucINEndpoint++ |
  863. USB_RTYPE_DIR_IN;
  864. }
  865. }
  866. else
  867. {
  868. //
  869. // Notify the class that it's interface number has
  870. // changed.
  871. //
  872. CompositeEPChange(&psCompDevice->psDevices[ulDev],
  873. psEndpoint->bEndpointAddress,
  874. ucOUTEndpoint);
  875. psEndpoint->bEndpointAddress = ucOUTEndpoint++;
  876. }
  877. }
  878. //
  879. // Move on to the next descriptor.
  880. //
  881. usBytes += psHeader->bLength;
  882. }
  883. ulOffset += pConfigHeader->psSections[ulIdx]->ucSize;
  884. usTotalLength += usBytes;
  885. }
  886. //
  887. // Allow the device class to make adjustments to the configuration
  888. // descriptor.
  889. //
  890. psCompDevice->psDevices[ulDev].psDevice->sCallbacks.pfnDeviceHandler(
  891. psCompDevice->psDevices[ulDev].pvInstance,
  892. USB_EVENT_COMP_CONFIG, (void *)pucConfig);
  893. ulDev++;
  894. }
  895. //
  896. // Modify the configuration descriptor to match the number of interfaces
  897. // and the new total size.
  898. //
  899. psCompDevice->psPrivateData->sCompConfigHeader.ucNumSections = 2;
  900. psCompDevice->psPrivateData->ppsCompSections[1]->ucSize = ulOffset;
  901. psCompDevice->psPrivateData->sConfigDescriptor.bNumInterfaces =
  902. ucInterface;
  903. psCompDevice->psPrivateData->sConfigDescriptor.wTotalLength =
  904. usTotalLength;
  905. return(0);
  906. }
  907. //****************************************************************************
  908. //
  909. //! This function should be called once for the composite class device to
  910. //! initialized basic operation and prepare for enumeration.
  911. //!
  912. //! \param ulIndex is the index of the USB controller to initialize for
  913. //! composite device operation.
  914. //! \param psDevice points to a structure containing parameters customizing
  915. //! the operation of the composite device.
  916. //! \param ulSize is the size in bytes of the data pointed to by the
  917. //! \e pucData parameter.
  918. //! \param pucData is the data area that the composite class can use to build
  919. //! up descriptors.
  920. //!
  921. //! In order for an application to initialize the USB composite device class,
  922. //! it must first call this function with the a valid composite device class
  923. //! structure in the \e psDevice parameter. This allows this function to
  924. //! initialize the USB controller and device code to be prepared to enumerate
  925. //! and function as a USB composite device. The \e ulSize and \e pucData
  926. //! parameters should be large enough to hold all of the class instances
  927. //! passed in via the psDevice structure. This is typically the full size of
  928. //! the configuration descriptor for a device minus its configuration
  929. //! header(9 bytes).
  930. //!
  931. //! This function returns a void pointer that must be passed in to all other
  932. //! APIs used by the composite class.
  933. //!
  934. //! See the documentation on the tUSBDCompositeDevice structure for more
  935. //! information on how to properly fill the structure members.
  936. //!
  937. //! \return This function returns 0 on failure or a non-zero void pointer on
  938. //! success.
  939. //
  940. //****************************************************************************
  941. void *
  942. USBDCompositeInit(unsigned int ulIndex, tUSBDCompositeDevice *psDevice,
  943. unsigned int ulSize, unsigned char *pucData)
  944. {
  945. tCompositeInstance *psInst;
  946. int iIdx;
  947. unsigned char *pucTemp;
  948. //
  949. // Check parameter validity.
  950. //
  951. ASSERT(ulIndex == 0);
  952. ASSERT(psDevice);
  953. ASSERT(psDevice->ppStringDescriptors);
  954. ASSERT(psDevice->psPrivateData);
  955. //
  956. // Initialize the work space in the passed instance structure.
  957. //
  958. psInst = psDevice->psPrivateData;
  959. psInst->ulDataSize = ulSize;
  960. psInst->pucData = pucData;
  961. //
  962. // Set the device information for the composite device.
  963. //
  964. psInst->psDevInfo = &g_sCompositeDeviceInfo;
  965. g_pCompConfigDescriptors[0] = &psInst->sCompConfigHeader;
  966. g_pCompConfigDescriptors[0]->ucNumSections = 0;
  967. g_pCompConfigDescriptors[0]->psSections =
  968. (const tConfigSection * const *)psDevice->psPrivateData->ppsCompSections;
  969. //
  970. // Create a byte pointer to use with the copy.
  971. //
  972. pucTemp = (unsigned char *)&psInst->sConfigDescriptor;
  973. //
  974. // Copy the default configuration descriptor into the instance data.
  975. //
  976. for(iIdx = 0; iIdx < g_pCompConfigDescriptor[0]; iIdx++)
  977. {
  978. pucTemp[iIdx] = g_pCompConfigDescriptor[iIdx];
  979. }
  980. //
  981. // Create a byte pointer to use with the copy.
  982. //
  983. pucTemp = (unsigned char *)&psInst->sDeviceDescriptor;
  984. //
  985. // Copy the default configuration descriptor into the instance data.
  986. //
  987. for(iIdx = 0; iIdx < g_pCompDeviceDescriptor[0]; iIdx++)
  988. {
  989. pucTemp[iIdx] = g_pCompDeviceDescriptor[iIdx];
  990. }
  991. //
  992. // Fix up the device descriptor with the client-supplied values.
  993. //
  994. psInst->sDeviceDescriptor.idVendor = psDevice->usVID;
  995. psInst->sDeviceDescriptor.idProduct = psDevice->usPID;
  996. //
  997. // Fix up the configuration descriptor with client-supplied values.
  998. //
  999. psInst->sConfigDescriptor.bmAttributes = psDevice->ucPwrAttributes;
  1000. psInst->sConfigDescriptor.bMaxPower =
  1001. (unsigned char)(psDevice->usMaxPowermA>>1);
  1002. g_sCompositeDeviceInfo.pDeviceDescriptor =
  1003. (const unsigned char *)&psInst->sDeviceDescriptor;
  1004. //
  1005. // Plug in the client's string stable to the device information
  1006. // structure.
  1007. //
  1008. psInst->psDevInfo->ppStringDescriptors = psDevice->ppStringDescriptors;
  1009. psInst->psDevInfo->ulNumStringDescriptors =
  1010. psDevice->ulNumStringDescriptors;
  1011. //
  1012. // Create the combined descriptors.
  1013. //
  1014. if(BuildCompositeDescriptor(psDevice))
  1015. {
  1016. return(0);
  1017. }
  1018. //
  1019. // Set the instance data for this device.
  1020. //
  1021. psInst->psDevInfo->pvInstance = (void *)psDevice;
  1022. //
  1023. // All is well so now pass the descriptors to the lower layer and put
  1024. // the bulk device on the bus.
  1025. //
  1026. USBDCDInit(ulIndex, psInst->psDevInfo);
  1027. //
  1028. // Return the pointer to the instance indicating that everything went
  1029. // well.
  1030. //
  1031. return ((void *)psDevice);
  1032. }
  1033. //****************************************************************************
  1034. //
  1035. //! Shuts down the composite device.
  1036. //!
  1037. //! \param pvInstance is the pointer to the device instance structure as
  1038. //! returned by USBDCompositeInit().
  1039. //!
  1040. //! This function terminates composite device interface for the instance
  1041. //! supplied. Following this call, the \e pvInstance instance should not me
  1042. //! used in any other calls.
  1043. //!
  1044. //! \return None.
  1045. //
  1046. //****************************************************************************
  1047. void
  1048. USBDCompositeTerm(void *pvInstance)
  1049. {
  1050. ASSERT(pvInstance != 0);
  1051. }
  1052. //****************************************************************************
  1053. //
  1054. // Close the Doxygen group.
  1055. //! @}
  1056. //
  1057. //****************************************************************************