usb_bridge.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. #define __CORE_USB_HEADER__
  2. #include "usb/usb_bridge.h"
  3. #include "usb/usb_config.h"
  4. #include "usbd_vendor.h" // PLANAR Protocol
  5. #include "usbd_usbtmc.h" // USBTMC Protocol
  6. #include "core/config.h" // CONFIG_AUTOMAT_MODE_USB_POWERBANK
  7. #include "core/gpio.h"
  8. #include "usbapp/usb_application_switch.h"
  9. #include "usbapp/usb_application_flash.h"
  10. #include "usbapp/usb_application_temp.h"
  11. #if CONFIG_AUTOMAT_MODE_USB_POWERBANK && CONFIG_AUTOMAT_MODE
  12. #include "usbapp/usb_application_enumspy.h"
  13. #endif
  14. #include "usbapp/usb_application_usbtmc.h"
  15. #include "usbapp/usb_application_benchmark.h"
  16. #include "usbapp/usb_application_service.h"
  17. #include "usbapp/usb_application_default_dataapp.h"
  18. #include "usbapp/usb_application_interfaceswitch.h"
  19. #include "usbapp/usb_application_power.h"
  20. #include "usbapp/usb_application_led.h"
  21. // Module usb_bridge
  22. // File: usb_bridge.c
  23. // Description: this module connects the middleware USBD-drivers and user USB
  24. // applications using so calling "bridges".
  25. // The bridge - is a user declared special structure that announces the application
  26. // in the module application pool - this process is also knowed as "registering".
  27. // The registering process is fully static process. Once registerd the application
  28. // can not be unregisted. In the same time there no way to register application in runtime.
  29. // Each USBD-class (VENDOR, USBTMC, etc.) have dedicated application pool.
  30. // For example: VENDOR interface is responsible for PLANAR protocol that used to
  31. // interact to the host applications using VENDOR-requests only via CONTROL transfers.
  32. // This VENDOR interface has application pool: @usbCtlAppList_Vendor.
  33. // Each set of logically combined vendor-specific requests represent "application".
  34. // You must register such sets in the @usbCtlAppList_Vendor.
  35. // All the applications are services in order of registering in the pool.
  36. // The first application that successfully processed the CONTROL-request is considered
  37. // as an application that the request is intended for.
  38. // So you must delegate only unique requests for each application.
  39. // As for BULK transfers, the bridges for bulk transfers work some kind differ.
  40. // Unlike CONTROL transfer, the bulk ones does not relies on SETUP packets that
  41. // could help to identify the application the data is intended for.
  42. // Instead of the SETUP-requests, the data flow is being controlled by the USB endpoint
  43. // address (EP number). Unlike the CONTROL application, each data-application must register
  44. // the data handler dynamically during the CONTROL application initializing.
  45. // So, the data applications are depends on the control applications and can not be used
  46. // separately. The pool for the data applications have equal size as a number of hardware
  47. // possible number of data-endpoints plus one (including the only control endpoint).
  48. // This extra entry (for control endpoint) is reserved in the beginning of the pool and is
  49. // used for the endpoints are not occupated by any data-application, aka default handler.
  50. // Once registed the application can be unregisted in the any time. The endpoint
  51. // the application registered for remains occupied until the application is un-registered.
  52. // So only one data-application can be registered to serve each logical endpoint.
  53. // -----------------------------------------------------------------------------
  54. #define cellsof(A) (sizeof(A)/sizeof(A[0]))
  55. #define lastcell(A) A[cellsof(A)]
  56. #define foreachidx_ctl_app_vendor(app,i) for( size_t i = ( app = usbCtlAppList_Vendor[0], 0 ); i < cellsof(usbCtlAppList_Vendor); app = usbCtlAppList_Vendor[++i] )
  57. #define foreach_ctl_app_vendor(app) foreachidx_ctl_app_vendor(app,__i)
  58. #define foreachidx_ctl_app_usbtmc(app,i) for( size_t i = ( app = usbCtlAppList_Usbtmc[0], 0 ); i < cellsof(usbCtlAppList_Usbtmc); app = usbCtlAppList_Usbtmc[++i] )
  59. #define foreach_ctl_app_usbtmc(app) foreachidx_ctl_app_usbtmc(app,__i)
  60. #define foreachidx_data_app_usbtmc(app,i) for( size_t i = ( app = usbDataAppList_Usbtmc[0], 0 ); i < cellsof(usbDataAppList_Usbtmc); app = usbDataAppList_Usbtmc[++i] )
  61. #define foreach_data_app_usbtmc(app) foreachidx_ctl_app_usbtmc(app,__i)
  62. #define with_data_app_for(ep, app) if(((ep) > 0)\
  63. && ((0x7F & (ep)) < (MAX_DATAAPP_ENDPOINTS))\
  64. && (NULL != (app = usbDataAppList_Usbtmc[ (0x7F & (ep)) ], app) ) )
  65. #define with_default_data_app(ep, app) if(((ep) > 0)\
  66. && (NULL != (app = usbDataAppList_Usbtmc[ 0 ], app) ) )
  67. // -----------------------------------------------------------------------------
  68. // PLANAR protocol: application index
  69. // @usbCtlApp_appIdx_Vendor
  70. // stores the active application index the next control transaction
  71. // will be passed for.
  72. static size_t usbCtlApp_appIdx_Vendor = 0;
  73. // USBTMC protocol: application index
  74. // @usbCtlApp_appIdx_Usbtmc
  75. // stores the active application index the next control transaction
  76. // will be passed for.
  77. static size_t usbCtlApp_appIdx_Usbtmc = 0;
  78. /* --- Vendor Specific Applications ---
  79. PLANAR Protocol:
  80. 1. Application Key Switch [usb_application_switch.h]
  81. 2. Application Flash Control [usb_application_switch.h]
  82. 3. Application Temperature Monitor [usb_application_temp.h]
  83. 4. Application USB-Enumeration Spy [usb_application_enumspy.h]
  84. ------------------------------------- */
  85. // PLANAR protocol: applications list
  86. // @usbCtlAppList_Vendor
  87. // USB Control Interface applications list
  88. static const sUSBAppEntry_Control_t * const usbCtlAppList_Vendor[] = {
  89. #if CONFIG_AUTOMAT_MODE_USB_POWERBANK && CONFIG_AUTOMAT_MODE
  90. &usbapplication_ACM_planarproto_enumspy,
  91. #endif
  92. &usbapplication_ACM_planarproto_flash,
  93. //&usbapplication_ACM_planarproto_temp,
  94. //&usbapplication_ACM_planarproto_switch,
  95. &usbapplication_ACM_planarproto_service,
  96. &usbapplication_ACM_planarproto_interfaceswitch,
  97. //&usbapplication_ACM_planarproto_power,
  98. &usbapplication_ACM_planarproto_led
  99. };
  100. // USBTMC protocol: CONTROL-applications list
  101. // @usbCtlAppList_Usbtmc
  102. // USB Control Interface applications list
  103. static const sUSBAppEntry_Control_t * const usbCtlAppList_Usbtmc[] = {
  104. #if CONFIG_AUTOMAT_MODE_USB_POWERBANK && CONFIG_AUTOMAT_MODE
  105. &usbapplication_ACM_planarproto_enumspy,
  106. #endif
  107. &usbapplication_USBTMC_control,
  108. //&usbapplication_BENCHMARK_control,
  109. };
  110. // USBTMC protocol: BULK-applications list
  111. // @usbDataAppList_Usbtmc
  112. // USB Bulk Interface applications list
  113. static const sUSBAppEntry_Data_t * usbDataAppList_Usbtmc[ MAX_DATAAPP_ENDPOINTS ] = {
  114. NULL // no applications registered by default: you must register applications in runtime
  115. };
  116. // -----------------------------------------------------------------------------
  117. // @usb_bridge_register_dataapp
  118. // Register (attach) the data-stream handler @dataApp for the specified endpoint (@epnum)
  119. // After the registering all the data requests relates to the endpoint will be routed to
  120. // the aproptiate handler.
  121. bool usb_bridge_register_dataapp( uint8_t epnum, const sUSBAppEntry_Data_t * dataApp )
  122. {
  123. epnum = epnum & 0x7F;
  124. if( ((epnum > 0) && (epnum < MAX_DATAAPP_ENDPOINTS)) && ( NULL != dataApp ) )
  125. {
  126. if( NULL == usbDataAppList_Usbtmc[ epnum ] )
  127. {
  128. if( NULL == dataApp->fDataInitHandler )
  129. {
  130. usbDataAppList_Usbtmc[ epnum ] = dataApp;
  131. return true;
  132. }
  133. else
  134. if( dataApp->fDataInitHandler( epnum ) )
  135. {
  136. usbDataAppList_Usbtmc[ epnum ] = dataApp;
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. // @usb_bridge_unregister_dataapp
  144. // De-Register (detach) the data-stream handler @dataApp from the specified endpoint (@epnum)
  145. // After the deregistering all the data requests relates to the endpoint will be routed to
  146. // the default data handler handler or will be discared
  147. bool usb_bridge_unregister_dataapp( uint8_t epnum )
  148. {
  149. epnum = epnum & 0x7F;
  150. if( (epnum > 0) && (epnum < MAX_DATAAPP_ENDPOINTS) )
  151. {
  152. if( (NULL != usbDataAppList_Usbtmc[ epnum ]) && (NULL != usbDataAppList_Usbtmc[ epnum ]->fDataDeInitHandler) )
  153. {
  154. usbDataAppList_Usbtmc[ epnum ]->fDataDeInitHandler( epnum );
  155. }
  156. usbDataAppList_Usbtmc[ epnum ] = NULL;
  157. return true;
  158. }
  159. return false;
  160. }
  161. // -----------------------------------------------------------------------------
  162. // PLANAR protocol procedures:
  163. // "usblib"-layer procedures
  164. static int8_t fUsbInit_Vendor();
  165. static void fUsbReset_Vendor();
  166. static size_t fUsbEp0Rx_Vendor( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  167. static size_t fUsbEp0Tx_Vendor( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  168. static bool fUsbSetInterface_Vendor( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc );
  169. static void fUsbClrInterface_Vendor( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc );
  170. static bool fUsbSetupRx_Vendor( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  171. static int8_t fUsbDeInit_Vendor();
  172. // -----------------------------------------------------------------------------
  173. // PLANAR Protocol: VENDOR bridge descriptor
  174. const USBD_COMMON_ItfTypeDef USBD_Interface_Vendor_UsbInterface =
  175. {
  176. .fUsbInit = fUsbInit_Vendor, //
  177. .fUsbDeInit = fUsbDeInit_Vendor, //
  178. .fUsbSetup = fUsbSetupRx_Vendor, // SETUP-packet filtering
  179. .fUsbCtlEpRx = fUsbEp0Rx_Vendor, // Control-transfer-OUT handler
  180. .fUsbCtlEpTx = fUsbEp0Tx_Vendor, // Control-transfer-IN handler
  181. .fResetEvent = fUsbReset_Vendor, // USB Reset handler
  182. .fSuspendEvent = NULL, //
  183. .fSetIface = fUsbSetInterface_Vendor, // SetInterface/SetConfiguration request handler
  184. .fClrIface = fUsbClrInterface_Vendor, // SetInterface/SetConfiguration request handler
  185. .fGetIface = NULL, //
  186. .fDataRxHandler = NULL, // Bulk-transfer-OUT handler
  187. .fDataTxHandler = NULL, // Bulk-transfer-IN handler
  188. .fDataErrHandler = NULL, // Bulk-transfer error handler
  189. };
  190. // -----------------------------------------------------------------------------
  191. // -----------------------------------------------------------------------------
  192. // USBTMC protocol procedures:
  193. // "usblib"-layer procedures
  194. static int8_t fUsbInit_Usbtmc();
  195. static void fUsbReset_Usbtmc();
  196. static size_t fUsbEp0Rx_Usbtmc( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  197. static size_t fUsbEp0Tx_Usbtmc( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  198. static bool fUsbSetInterface_Usbtmc( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc );
  199. static void fUsbClrInterface_Usbtmc( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc );
  200. static bool fUsbDataRx_Usbtmc( uint8_t bEpLogAddress, sUSBTransfer_t * rx );
  201. static bool fUsbDataTx_Usbtmc( uint8_t bEpLogAddress, sUSBTransfer_t * tx );
  202. static bool fUsbDataErr_Usbtmc( uint8_t bEpLogAddress, uint32_t err );
  203. static bool fUsbSetupRx_Usbtmc( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  204. static int8_t fUsbDeInit_Usbtmc();
  205. // USBTMC Protocol: USBTMC bridge descriptor
  206. #if CONFIG_USB_USBTMC_ENABLE
  207. const USBD_COMMON_ItfTypeDef USBD_Interface_Usbtmc_UsbInterface =
  208. {
  209. .fUsbInit = fUsbInit_Usbtmc, //
  210. .fUsbDeInit = fUsbDeInit_Usbtmc, //
  211. .fUsbSetup = fUsbSetupRx_Usbtmc, // SETUP-packet filtering
  212. .fUsbCtlEpRx = fUsbEp0Rx_Usbtmc, // Control-transfer-OUT handler
  213. .fUsbCtlEpTx = fUsbEp0Tx_Usbtmc, // Control-transfer-IN handler
  214. .fResetEvent = fUsbReset_Usbtmc, // USB Reset handler
  215. .fSuspendEvent = NULL, //
  216. .fSetIface = fUsbSetInterface_Usbtmc, // SetInterface/SetConfiguration request handler
  217. .fClrIface = fUsbClrInterface_Usbtmc, // SetInterface/SetConfiguration request handler
  218. .fGetIface = NULL, //
  219. .fDataRxHandler = fUsbDataRx_Usbtmc, // Bulk-transfer-OUT handler
  220. .fDataTxHandler = fUsbDataTx_Usbtmc, // Bulk-transfer-IN handler
  221. .fDataErrHandler = fUsbDataErr_Usbtmc, // Bulk-transfer error handler
  222. };
  223. #endif
  224. // -----------------------------------------------------------------------------
  225. // -----------------------------------------------------------------------------
  226. // fUsbInit_Vendor()
  227. // VENDOR interface: initialize VENDOR bridge
  228. static int8_t fUsbInit_Vendor()
  229. {
  230. usbCtlApp_appIdx_Vendor = 0;
  231. const sUSBAppEntry_Control_t * app;
  232. foreach_ctl_app_vendor( app )
  233. {
  234. int8_t ret = app->fUsbInit(); // "fUsbDeInit_Vendor -> fUsbInit_Vendor", Fixed: 08/07/19
  235. if( ret != 0 ) return ret;
  236. }
  237. return 0;
  238. }
  239. // fUsbInit_Usbtmc()
  240. // USBTMC interface: initialize USBTMC bridge
  241. static int8_t fUsbInit_Usbtmc()
  242. {
  243. usbCtlApp_appIdx_Usbtmc = 0;
  244. {
  245. const sUSBAppEntry_Data_t * dataapp;
  246. foreachidx_data_app_usbtmc( dataapp, dataapp_idx )
  247. {
  248. // deinitialize applications
  249. usbDataAppList_Usbtmc[ dataapp_idx ] = NULL; // data application must be registed in runtime
  250. }
  251. (void)dataapp;
  252. // register default data-application
  253. usbDataAppList_Usbtmc[ 0 ] = &usbapplication_default_dataapp;
  254. }
  255. {
  256. const sUSBAppEntry_Control_t * ctlapp;
  257. foreach_ctl_app_usbtmc( ctlapp )
  258. {
  259. int8_t ret = ctlapp->fUsbInit();
  260. if( ret != 0 ) return ret;
  261. }
  262. }
  263. return 0;
  264. }
  265. // -----------------------------------------------------------------------------
  266. // fUsbDeInit_Vendor()
  267. // VENDOR interface: deinitialize VENDOR bridge
  268. static int8_t fUsbDeInit_Vendor()
  269. {
  270. usbCtlApp_appIdx_Vendor = 0;
  271. const sUSBAppEntry_Control_t * app;
  272. foreach_ctl_app_vendor( app )
  273. {
  274. app->fUsbDeInit();
  275. }
  276. return 0;
  277. }
  278. // fUsbDeInit_Usbtmc()
  279. // USBTMC interface: deinitialize USBTMC bridge
  280. static int8_t fUsbDeInit_Usbtmc()
  281. {
  282. usbCtlApp_appIdx_Usbtmc = 0;
  283. {
  284. const sUSBAppEntry_Control_t * app;
  285. foreach_ctl_app_usbtmc( app )
  286. {
  287. app->fUsbDeInit();
  288. }
  289. }
  290. {
  291. const sUSBAppEntry_Data_t * dataapp;
  292. foreachidx_data_app_usbtmc( dataapp, dataapp_idx )
  293. {
  294. // destroy application pointer
  295. usbDataAppList_Usbtmc[ dataapp_idx ] = NULL;
  296. }
  297. (void)dataapp;
  298. }
  299. return 0;
  300. }
  301. // -----------------------------------------------------------------------------
  302. // fUsbReset_Vendor()
  303. // VENDOR interface: USB-bus Reset handler
  304. static void fUsbReset_Vendor()
  305. {
  306. const sUSBAppEntry_Control_t * app;
  307. foreach_ctl_app_vendor( app )
  308. {
  309. app->fResetEvent();
  310. }
  311. }
  312. // fUsbReset_Usbtmc()
  313. // USBTMC interface: USB-bus Reset handler
  314. static void fUsbReset_Usbtmc()
  315. {
  316. const sUSBAppEntry_Control_t * app;
  317. foreach_ctl_app_usbtmc( app )
  318. {
  319. app->fResetEvent();
  320. }
  321. }
  322. // -----------------------------------------------------------------------------
  323. // fUsbSetupRx_Vendor()
  324. // VENDOR interface: SETUP-packet interceptor
  325. static bool fUsbSetupRx_Vendor( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success )
  326. {
  327. if( bFirstStage )
  328. {
  329. const sUSBAppEntry_Control_t * app;
  330. foreachidx_ctl_app_vendor( app, idx )
  331. {
  332. if( app->fUsbSetup( pSetup, bFirstStage, success ) )
  333. {
  334. usbCtlApp_appIdx_Vendor = idx;
  335. return true;
  336. }
  337. }
  338. }
  339. usbCtlApp_appIdx_Vendor = 0;
  340. return false;
  341. }
  342. // -----------------------------------------------------------------------------
  343. // fUsbSetupRx_Usbtmc()
  344. // USBTMC interface: SETUP-packet interceptor
  345. static bool fUsbSetupRx_Usbtmc( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success )
  346. {
  347. if( bFirstStage )
  348. {
  349. const sUSBAppEntry_Control_t * app;
  350. foreachidx_ctl_app_usbtmc( app, idx )
  351. {
  352. if( app->fUsbSetup( pSetup, bFirstStage, success ) )
  353. {
  354. usbCtlApp_appIdx_Usbtmc = idx;
  355. return true;
  356. }
  357. }
  358. }
  359. usbCtlApp_appIdx_Usbtmc = 0;
  360. return false;
  361. }
  362. // -----------------------------------------------------------------------------
  363. // fUsbEp0Rx_Vendor()
  364. // VENDOR interface: Control-transfer-OUT handler
  365. static size_t fUsbEp0Rx_Vendor( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining )
  366. {
  367. return usbCtlAppList_Vendor[ usbCtlApp_appIdx_Vendor ]->fUsbCtlEpRx( pSetup, rx, idx, bytesRemaining );
  368. }
  369. // -----------------------------------------------------------------------------
  370. // fUsbEp0Rx_Usbtmc()
  371. // USBTMC interface: Control-transfer-OUT handler
  372. static size_t fUsbEp0Rx_Usbtmc( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining )
  373. {
  374. return usbCtlAppList_Usbtmc[ usbCtlApp_appIdx_Usbtmc ]->fUsbCtlEpRx( pSetup, rx, idx, bytesRemaining );
  375. }
  376. // -----------------------------------------------------------------------------
  377. // fUsbEp0Tx_Vendor()
  378. // VENDOR interface: Control-transfer-IN handler
  379. static size_t fUsbEp0Tx_Vendor( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining )
  380. {
  381. return usbCtlAppList_Vendor[ usbCtlApp_appIdx_Vendor ]->fUsbCtlEpTx( pSetup, tx, idx, bytesRemaining );
  382. }
  383. // fUsbEp0Tx_Usbtmc()
  384. // USBTMC interface: Control-transfer-IN handler
  385. static size_t fUsbEp0Tx_Usbtmc( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining )
  386. {
  387. return usbCtlAppList_Usbtmc[ usbCtlApp_appIdx_Usbtmc ]->fUsbCtlEpTx( pSetup, tx, idx, bytesRemaining );
  388. }
  389. // -----------------------------------------------------------------------------
  390. // fUsbSetInterface_Vendor()
  391. // VENDOR interface: USB-interface SetInterface request handler
  392. static bool fUsbSetInterface_Vendor( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc )
  393. {
  394. if( (NULL != ifdesc)
  395. && USB_ACM_CONFIGURATION_VALUE == cfgidx
  396. && USB_ACM_INTERFACE_VALUE == ifdesc->bInterfaceNumber )
  397. {
  398. return true;
  399. }
  400. return false;
  401. }
  402. // fUsbSetInterface_Usbtmc()
  403. // USBTMC interface: USB-interface SetInterface request handler
  404. static bool fUsbSetInterface_Usbtmc( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc )
  405. {
  406. if( (NULL != ifdesc)
  407. && USB_TMC_CONFIGURATION_VALUE == cfgidx
  408. && USB_TMC_INTERFACE_VALUE == ifdesc->bInterfaceNumber )
  409. {
  410. return true;
  411. }
  412. return false;
  413. }
  414. // -----------------------------------------------------------------------------
  415. // fUsbClrInterface_Vendor()
  416. // VENDOR interface: USB-interface ClearInterface request handler
  417. static void fUsbClrInterface_Vendor( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc )
  418. {
  419. if( (NULL != ifdesc)
  420. && USB_ACM_CONFIGURATION_VALUE == cfgidx
  421. && USB_ACM_INTERFACE_VALUE == ifdesc->bInterfaceNumber )
  422. {
  423. return;
  424. }
  425. }
  426. // fUsbClrInterface_Usbtmc()
  427. // USBTMC interface: USB-interface ClearInterface request handler
  428. static void fUsbClrInterface_Usbtmc( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc )
  429. {
  430. if( (NULL != ifdesc)
  431. && USB_TMC_CONFIGURATION_VALUE == cfgidx
  432. && USB_TMC_INTERFACE_VALUE == ifdesc->bInterfaceNumber )
  433. {
  434. return;
  435. }
  436. }
  437. // -----------------------------------------------------------------------------
  438. // fUsbDataRx_Usbtmc()
  439. // USBTMC interface: Data-Rx handler [Bulk-OUT-transfer]
  440. static bool fUsbDataRx_Usbtmc( uint8_t bEpLogAddress, sUSBTransfer_t * rx )
  441. {
  442. const sUSBAppEntry_Data_t * app = NULL;
  443. with_data_app_for( bEpLogAddress, app )
  444. {
  445. if( NULL != app->fDataRxHandler )
  446. {
  447. return app->fDataRxHandler( bEpLogAddress, rx );
  448. }
  449. }
  450. else
  451. with_default_data_app( bEpLogAddress, app )
  452. {
  453. if( NULL != app->fDataRxHandler )
  454. {
  455. return app->fDataRxHandler( bEpLogAddress, rx );
  456. }
  457. }
  458. return false;
  459. }
  460. // fUsbDataTx_Usbtmc()
  461. // USBTMC interface: Data-Tx handler [Bulk-IN-transfer]
  462. // Is called after the first packet sent (need to call @usb_bridge_datain_beginsend)
  463. static bool fUsbDataTx_Usbtmc( uint8_t bEpLogAddress, sUSBTransfer_t * tx )
  464. {
  465. const sUSBAppEntry_Data_t * app = NULL;
  466. with_data_app_for( bEpLogAddress, app )
  467. {
  468. if( NULL != app->fDataTxHandler )
  469. {
  470. return app->fDataTxHandler( bEpLogAddress, tx );
  471. }
  472. }
  473. else
  474. with_default_data_app( bEpLogAddress, app )
  475. {
  476. if( NULL != app->fDataTxHandler )
  477. {
  478. return app->fDataTxHandler( bEpLogAddress, tx );
  479. }
  480. }
  481. return false;
  482. }
  483. static bool fUsbDataErr_Usbtmc( uint8_t bEpLogAddress, uint32_t err )
  484. {
  485. const sUSBAppEntry_Data_t * app = NULL;
  486. with_data_app_for( bEpLogAddress, app )
  487. {
  488. if( NULL != app->fDataErrHandler )
  489. {
  490. return app->fDataErrHandler( bEpLogAddress, err );
  491. }
  492. }
  493. else
  494. with_default_data_app( bEpLogAddress, app )
  495. {
  496. if( NULL != app->fDataErrHandler )
  497. {
  498. return app->fDataErrHandler( bEpLogAddress, err );
  499. }
  500. }
  501. return true; // true by default
  502. }