app_usbd_core.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_USBD)
  42. #include "app_usbd_core.h"
  43. #include "app_usbd.h"
  44. #include "app_usbd_request.h"
  45. #include "app_usbd_string_desc.h"
  46. #include "nrf.h"
  47. #include "nrf_atomic.h"
  48. #include "app_util_platform.h"
  49. #include "app_usbd.h"
  50. #include "app_usbd_class_base.h"
  51. #define NRF_LOG_MODULE_NAME app_usbd_core
  52. #if APP_USBD_CONFIG_LOG_ENABLED
  53. #define NRF_LOG_LEVEL APP_USBD_CONFIG_LOG_LEVEL
  54. #define NRF_LOG_INFO_COLOR APP_USBD_CONFIG_INFO_COLOR
  55. #define NRF_LOG_DEBUG_COLOR APP_USBD_CONFIG_DEBUG_COLOR
  56. #else //APP_USBD_CONFIG_LOG_ENABLED
  57. #define NRF_LOG_LEVEL 0
  58. #endif //APP_USBD_CONFIG_LOG_ENABLED
  59. #include "nrf_log.h"
  60. NRF_LOG_MODULE_REGISTER();
  61. /* Test if VID was configured */
  62. #ifndef APP_USBD_VID
  63. #error APP_USBD_VID not properly defined.
  64. #endif
  65. /* Device version checking */
  66. #if defined(APP_USBD_DEVICE_VER_MAJOR) && defined(APP_USBD_DEVICE_VER_MINOR)
  67. #if ((APP_USBD_DEVICE_VER_MAJOR)) > 99 || ((APP_USBD_DEVICE_VER_MINOR) > 99)
  68. #error Major and minor device version value have to be limited to 99.
  69. #endif
  70. #else
  71. #error The definition of a pair APP_USBD_DEVICE_VER_MAJOR and APP_USBD_DEVICE_VER_MINOR required.
  72. #endif
  73. /**
  74. * @internal
  75. * @defgroup app_usbd_core_internals USB Device high level library core module internals
  76. * @ingroup app_usbd_core
  77. *
  78. * Internal variables, auxiliary macros and functions of USBD high level core module.
  79. * @{
  80. */
  81. /** @brief Make USB power value */
  82. #define APP_USBD_POWER_MAKE(ma) (((ma) + 1) / 2)
  83. /**
  84. @brief Default device descriptor initializer @ref app_usbd_descriptor_device_t
  85. * */
  86. #define APP_USBD_CORE_DEVICE_DESCRIPTOR { \
  87. .bLength = sizeof(app_usbd_descriptor_device_t), /* descriptor size */ \
  88. .bDescriptorType = APP_USBD_DESCRIPTOR_DEVICE, /* descriptor type */ \
  89. .bcdUSB = APP_USBD_BCD_VER_MAKE(2,0,0), /* USB BCD version: 2.0 */ \
  90. .bDeviceClass = 0, /* device class: 0 - specified by interface */ \
  91. .bDeviceSubClass = 0, /* device subclass: 0 - specified by interface */ \
  92. .bDeviceProtocol = 0, /* device protocol: 0 - specified by interface */ \
  93. .bMaxPacketSize0 = NRF_DRV_USBD_EPSIZE, /* endpoint size: fixed to: NRF_DRV_USBD_EPSIZE*/ \
  94. .idVendor = APP_USBD_VID, /* Vendor ID*/ \
  95. .idProduct = APP_USBD_PID, /* Product ID*/ \
  96. .bcdDevice = APP_USBD_BCD_VER_MAKE( /* Device version BCD */ \
  97. APP_USBD_DEVICE_VER_MAJOR, \
  98. APP_USBD_DEVICE_VER_MINOR, \
  99. APP_USBD_DEVICE_VER_SUB), \
  100. .iManufacturer = APP_USBD_STRING_ID_MANUFACTURER, /* String ID: manufacturer */ \
  101. .iProduct = APP_USBD_STRING_ID_PRODUCT, /* String ID: product */ \
  102. .iSerialNumber = APP_USBD_STRING_ID_SERIAL, /* String ID: serial */ \
  103. .bNumConfigurations = 1 /* Fixed value: only one configuration supported*/\
  104. }
  105. #define APP_USBD_CORE_CONFIGURATION_DESCRIPTOR { \
  106. .bLength = sizeof(app_usbd_descriptor_configuration_t), \
  107. .bDescriptorType = APP_USBD_DESCRIPTOR_CONFIGURATION, \
  108. .wTotalLength = 0, /*Calculated dynamically*/ \
  109. .bNumInterfaces = 0, /*Calculated dynamically*/ \
  110. .bConfigurationValue = 1, /*Value passed to set configuration*/ \
  111. .iConfiguration = APP_USBD_STRING_ID_CONFIGURATION, /*Configuration ID*/ \
  112. .bmAttributes = APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_ALWAYS_SET_MASK | \
  113. ((APP_USBD_CONFIG_SELF_POWERED) ? \
  114. APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_SELF_POWERED_MASK \
  115. : \
  116. 0), \
  117. .bMaxPower = APP_USBD_POWER_MAKE(APP_USBD_CONFIG_MAX_POWER), \
  118. }
  119. /**
  120. * @brief Device descriptor instance.
  121. *
  122. * @note
  123. * Constant part of the device descriptor.
  124. * Values that must be calculated are updated directly in the buffer
  125. * just before the transmission.
  126. */
  127. static const app_usbd_descriptor_device_t m_device_dsc =
  128. APP_USBD_CORE_DEVICE_DESCRIPTOR;
  129. /**
  130. * @brief Configuration descriptor instance.
  131. *
  132. * @note
  133. * Constant part of the device descriptor.
  134. * Values that must be calculated are updated directly in the buffer
  135. * just before the transmission.
  136. */
  137. static const app_usbd_descriptor_configuration_t m_configuration_dsc =
  138. APP_USBD_CORE_CONFIGURATION_DESCRIPTOR;
  139. /* Required early declaration of event handler function */
  140. static ret_code_t app_usbd_core_event_handler(app_usbd_class_inst_t const * const p_inst,
  141. app_usbd_complex_evt_t const * const p_event);
  142. /**
  143. * @brief Current USB device state.
  144. *
  145. * This variable is updated automatically by core library.
  146. */
  147. static app_usbd_state_t m_app_usbd_state = APP_USBD_STATE_Disabled;
  148. /**
  149. * @brief Active device features.
  150. *
  151. * @note Only @ref APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP is supported for device.
  152. */
  153. static uint8_t m_device_features_state;
  154. /**
  155. * @brief Remote wake-up pending flag.
  156. */
  157. static nrf_atomic_flag_t m_rwu_pending;
  158. /**
  159. * @brief Core class methods.
  160. *
  161. * Base methods interface for core class.
  162. * This is quite specific class - it would be only connected into endpoint 0.
  163. * Not connected into instances list.
  164. */
  165. static const app_usbd_class_methods_t m_core_methods = {
  166. .event_handler = app_usbd_core_event_handler,
  167. .feed_descriptors = NULL,
  168. };
  169. /**
  170. * @brief Setup transfer buffer.
  171. */
  172. static uint8_t m_setup_transfer_buff[NRF_DRV_USBD_EPSIZE];
  173. /**
  174. * @brief Handler for outgoing setup data.
  175. *
  176. */
  177. static app_usbd_core_setup_data_handler_desc_t m_ep0_handler_desc;
  178. #define APP_USBD_CORE_CLASS_INSTANCE_CONFIG ()
  179. /*lint -u -save -e26 -e40 -e64 -e123 -e505 -e651*/
  180. /**
  181. * @brief Core instance.
  182. *
  183. * Create instance that would be connected into endpoints in USBD library.
  184. */
  185. APP_USBD_CLASS_INST_GLOBAL_DEF(
  186. app_usbd_core_inst,
  187. app_usbd_core,
  188. &m_core_methods,
  189. APP_USBD_CORE_CLASS_CONFIGURATION,
  190. () );
  191. /*lint -restore*/
  192. /**
  193. * @brief Set the new USB state.
  194. *
  195. * Function changes the internal status of the bus.
  196. * If the bus status is different than the one configured, an event is passed to all
  197. * the instances.
  198. *
  199. * @param state New state to be set.
  200. *
  201. * @sa usbd_core_state_get
  202. */
  203. static void usbd_core_state_set(app_usbd_state_t state)
  204. {
  205. if (m_app_usbd_state != state)
  206. {
  207. m_app_usbd_state = state;
  208. if(state != APP_USBD_STATE_Configured)
  209. {
  210. CLR_BIT(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP);
  211. }
  212. static const app_usbd_evt_t evt_data = {
  213. .type = APP_USBD_EVT_STATE_CHANGED
  214. };
  215. app_usbd_event_execute((app_usbd_internal_evt_t const *)&evt_data);
  216. }
  217. }
  218. /**
  219. * @brief Get the current USB state - internal function.
  220. *
  221. * This is just a wrapper for @ref app_usbd_core_state_get
  222. * to make symmetrical function to the internal @ref usbd_core_state_set.
  223. *
  224. * @return Current USB state.
  225. *
  226. * @sa usbd_core_state_set
  227. * @sa app_usbd_core_state_get
  228. */
  229. static inline app_usbd_state_t usbd_core_state_get(void)
  230. {
  231. return m_app_usbd_state;
  232. }
  233. /**
  234. * @brief Check current USBD power connection status.
  235. *
  236. */
  237. static inline bool usbd_core_power_is_detected(void)
  238. {
  239. return 0 != ( (NRF_POWER->USBREGSTATUS) & POWER_USBREGSTATUS_VBUSDETECT_Msk);
  240. }
  241. /**
  242. * @brief Clear current EP0 handler.
  243. *
  244. * Function just clears the EP0 handler without calling it.
  245. */
  246. static inline void usbd_core_ep0_handler_clear(void)
  247. {
  248. m_ep0_handler_desc.handler = NULL;
  249. }
  250. /**
  251. * @brief Safely call EP0 handler.
  252. *
  253. * Function calls EP0 handler only if its pointer is non-zero.
  254. *
  255. * @param status Status to send as a handler parameter.
  256. */
  257. static inline ret_code_t usbd_core_ep0_handler_call_and_clear(nrf_drv_usbd_ep_status_t status)
  258. {
  259. app_usbd_core_setup_data_handler_t handler = m_ep0_handler_desc.handler;
  260. if (NULL != handler)
  261. {
  262. usbd_core_ep0_handler_clear();
  263. return handler(status, m_ep0_handler_desc.p_context);
  264. }
  265. return NRF_ERROR_NULL;
  266. }
  267. /**
  268. * @brief Check if EP0 handler is configured.
  269. *
  270. * EP0 handler is configured is any instance that has processed SETUP command
  271. * expects some incoming / outgoing data.
  272. *
  273. * EP0 handler should be cleared automatically just before it is called
  274. * (see @ref usbd_core_ep0_handler_call_and_clear).
  275. * If instance requires more data - it has to setup EP0 handler once more time
  276. * (see @ref app_usbd_core_setup_data_handler_set).
  277. *
  278. * This function adds small layer of abstraction for checking if EP0 handler
  279. * is already configured.
  280. *
  281. * @retval true EP0 handler is set.
  282. * @retval false EP0 handler is cleared.
  283. */
  284. static inline bool usb_core_ep0_handler_check(void)
  285. {
  286. return (NULL != m_ep0_handler_desc.handler);
  287. }
  288. /**
  289. * @brief Empty data handler.
  290. *
  291. * Data handler used only to mark that there is requested data during SETUP.
  292. *
  293. * @return Always NRF_SUCCESS
  294. * @sa setup_empty_data_handler_desc
  295. */
  296. static ret_code_t setup_data_handler_empty(nrf_drv_usbd_ep_status_t status, void * p_contex)
  297. {
  298. UNUSED_PARAMETER(status);
  299. UNUSED_PARAMETER(p_contex);
  300. return NRF_SUCCESS;
  301. }
  302. /**
  303. * @brief
  304. *
  305. * Empty EP0 transfer transfer handler.
  306. */
  307. static app_usbd_core_setup_data_handler_desc_t const m_setup_data_handler_empty_desc =
  308. {
  309. .handler = setup_data_handler_empty,
  310. .p_context = NULL
  311. };
  312. /**
  313. * @brief Structure used as a context for descriptor feeder.
  314. *
  315. * Structure with all the data required to process instances to generate descriptor
  316. * data chunk.
  317. */
  318. typedef struct
  319. {
  320. app_usbd_class_inst_t const * p_cinst; //!< The class instance that is to be processed next.
  321. const uint8_t * p_desc; //!< Pointer at current descriptor or NULL if finished.
  322. /**<
  323. * If the value passed by @ref p_desc is NULL on transfer function enter it means that ZLP is required.
  324. * Or it is time to finish the transfer (depending on @c total_left).
  325. */
  326. size_t desc_left; //!< Number of bytes left in the current class descriptor to send
  327. size_t total_left; //!< Number of bytes left that was requested by the host
  328. app_usbd_class_descriptor_ctx_t feed_thread; //!< Class descriptor context
  329. } app_usbd_core_descriptor_conf_feed_data_t;
  330. /**
  331. * @brief Default data used by the feeder.
  332. *
  333. *
  334. */
  335. static app_usbd_core_descriptor_conf_feed_data_t m_descriptor_conf_feed_data;
  336. /**
  337. * @brief Descriptor feeder.
  338. *
  339. * Descriptor feeder is used as an callback function when descriptors are
  340. * transfered and buffer is ready for next data.
  341. * It prepares next chunk of data to be sent.
  342. *
  343. * @param p_next See @ref nrf_drv_usbd_next_transfer_handler_t documentation.
  344. * @param p_context Pointer to @ref app_usbd_core_descriptor_feed_data_t data type.
  345. * @param ep_size The size of the endpoint.
  346. *
  347. * @return See @ref nrf_drv_usbd_next_transfer_handler_t documentation.
  348. */
  349. static bool usbd_descriptor_conf_feeder(
  350. nrf_drv_usbd_ep_transfer_t * p_next,
  351. void * p_context,
  352. size_t ep_size)
  353. {
  354. bool continue_req = true;
  355. app_usbd_core_descriptor_conf_feed_data_t * p_data = p_context;
  356. if ((p_data->p_desc == NULL) && (app_usbd_class_next_get(p_data->p_cinst) == NULL)
  357. && (p_data->desc_left == 0))
  358. {
  359. /* ZLP */
  360. continue_req = false;
  361. p_next->p_data.tx = NULL;
  362. p_next->size = 0;
  363. }
  364. else
  365. {
  366. ASSERT(ep_size <= NRF_DRV_USBD_FEEDER_BUFFER_SIZE);
  367. uint8_t * p_tx_buff;
  368. size_t size = 0; /* Currently added number of bytes */
  369. size_t tx_size; /* Number of bytes to send right now */
  370. bool feeding = false;
  371. /* Feeder function can use the USBD driver internal buffer */
  372. p_tx_buff = nrf_drv_usbd_feeder_buffer_get();
  373. tx_size = MIN(ep_size, p_data->total_left);
  374. while (0 != tx_size)
  375. {
  376. size_t to_copy = MIN(tx_size, p_data->desc_left);
  377. /* First transfer */
  378. if (p_data->p_desc != NULL)
  379. {
  380. memcpy(p_tx_buff + size, p_data->p_desc, to_copy);
  381. p_data->p_desc = NULL;
  382. }
  383. /* Starting with second transfer */
  384. else if (0 < p_data->desc_left)
  385. {
  386. UNUSED_RETURN_VALUE(p_data->p_cinst->p_class_methods->feed_descriptors(
  387. &p_data->feed_thread, p_data->p_cinst,
  388. (uint8_t *)p_tx_buff + size, to_copy));
  389. feeding = true;
  390. }
  391. else
  392. {
  393. ;
  394. }
  395. p_data->desc_left -= to_copy;
  396. p_data->total_left -= to_copy;
  397. tx_size -= to_copy;
  398. size += to_copy;
  399. /* Switch to next class if no descriptor left and first feeding was done */
  400. if(p_data->desc_left == 0 && feeding)
  401. {
  402. p_data->p_cinst = app_usbd_class_next_get(p_data->p_cinst);
  403. }
  404. if (0 == p_data->total_left)
  405. {
  406. continue_req = false;
  407. }
  408. else if (0 == p_data->desc_left)
  409. {
  410. if (NULL == p_data->p_cinst)
  411. {
  412. p_data->p_desc = NULL;
  413. /* No more data - check if ZLP is required */
  414. if (size > 0)
  415. {
  416. if (size < ep_size)
  417. {
  418. continue_req = false;
  419. }
  420. }
  421. break;
  422. }
  423. else
  424. {
  425. /* New class - count descriptor size and initialize feeding thread */
  426. app_usbd_class_descriptor_ctx_t desiz;
  427. APP_USBD_CLASS_DESCRIPTOR_INIT(&desiz);
  428. while(p_data->p_cinst->p_class_methods->feed_descriptors(
  429. &desiz, p_data->p_cinst, NULL, sizeof(uint8_t)))
  430. {
  431. p_data->desc_left++;
  432. }
  433. APP_USBD_CLASS_DESCRIPTOR_INIT(&p_data->feed_thread);
  434. }
  435. }
  436. else
  437. {
  438. /* Nothing to do */
  439. }
  440. }
  441. p_next->p_data.tx = p_tx_buff;
  442. p_next->size = size;
  443. }
  444. return continue_req;
  445. }
  446. /**
  447. * @brief Standard endpoint request handle.
  448. *
  449. * @param[in] p_setup_ev Setup event.
  450. *
  451. * @return Standard error code.
  452. */
  453. static ret_code_t setup_endpoint_req_std(app_usbd_setup_evt_t const * p_setup_ev)
  454. {
  455. if (APP_USBD_SETUP_REQTYPE_STD != app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType))
  456. {
  457. return NRF_ERROR_NOT_SUPPORTED;
  458. }
  459. nrf_drv_usbd_ep_t ep_addr = (nrf_drv_usbd_ep_t)(p_setup_ev->setup.wIndex.lb);
  460. app_usbd_state_t usb_state = usbd_core_state_get();
  461. switch (p_setup_ev->setup.bRequest)
  462. {
  463. case APP_USBD_SETUP_STDREQ_GET_STATUS:
  464. {
  465. if ((usb_state == APP_USBD_STATE_Configured) || (NRF_USBD_EP_NR_GET(ep_addr) == 0))
  466. {
  467. size_t tx_size;
  468. uint16_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size);
  469. p_tx_buff[0] = nrf_drv_usbd_ep_stall_check(ep_addr) ? 1 : 0;
  470. return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint16_t));
  471. }
  472. else
  473. {
  474. return NRF_ERROR_INVALID_STATE;
  475. }
  476. }
  477. case APP_USBD_SETUP_STDREQ_SET_FEATURE:
  478. {
  479. if ((!NRF_USBD_EPISO_CHECK(ep_addr)) &&
  480. (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT))
  481. {
  482. if ((usb_state == APP_USBD_STATE_Configured) || (NRF_USBD_EP_NR_GET(ep_addr) == 0))
  483. {
  484. nrf_drv_usbd_ep_stall(ep_addr);
  485. return NRF_SUCCESS;
  486. }
  487. else
  488. {
  489. return NRF_ERROR_INVALID_STATE;
  490. }
  491. }
  492. break;
  493. }
  494. case APP_USBD_SETUP_STDREQ_CLEAR_FEATURE:
  495. {
  496. if ((!NRF_USBD_EPISO_CHECK(ep_addr)) &&
  497. (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT))
  498. {
  499. if ((usb_state == APP_USBD_STATE_Configured) || (NRF_USBD_EP_NR_GET(ep_addr) == 0))
  500. {
  501. nrf_drv_usbd_ep_dtoggle_clear(ep_addr);
  502. nrf_drv_usbd_ep_stall_clear(ep_addr);
  503. return NRF_SUCCESS;
  504. }
  505. else
  506. {
  507. return NRF_ERROR_INVALID_STATE;
  508. }
  509. }
  510. break;
  511. }
  512. default:
  513. break;
  514. }
  515. return NRF_ERROR_NOT_SUPPORTED;
  516. }
  517. /**
  518. * @brief Standard interface request handle.
  519. *
  520. * @param[in,out] p_class_inst Class instance that holds selected interface.
  521. * @param[in] iface_idx Index of the interface in class structure.
  522. * @param[in] p_event Event structure to be processed.
  523. *
  524. * @return Operation status.
  525. */
  526. static ret_code_t setup_interface_req_std_handle(
  527. app_usbd_class_inst_t const * const p_class_inst,
  528. uint8_t iface_idx,
  529. app_usbd_setup_evt_t const * p_setup_ev)
  530. {
  531. if (APP_USBD_SETUP_REQTYPE_STD != app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType))
  532. {
  533. return NRF_ERROR_NOT_SUPPORTED;
  534. }
  535. app_usbd_state_t usb_state = usbd_core_state_get();
  536. if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN)
  537. {
  538. switch (p_setup_ev->setup.bRequest)
  539. {
  540. case APP_USBD_SETUP_STDREQ_GET_STATUS:
  541. {
  542. if (!(usb_state == APP_USBD_STATE_Configured))
  543. {
  544. return NRF_ERROR_INVALID_STATE;
  545. }
  546. size_t tx_size;
  547. uint16_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size);
  548. p_tx_buff[0] = 0;
  549. return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint16_t));
  550. }
  551. case APP_USBD_SETUP_STDREQ_GET_INTERFACE:
  552. {
  553. if (!(usb_state == APP_USBD_STATE_Configured))
  554. {
  555. return NRF_ERROR_INVALID_STATE;
  556. }
  557. size_t tx_size;
  558. uint8_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size);
  559. p_tx_buff[0] = app_usbd_iface_selection_get(p_class_inst, iface_idx);
  560. return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint8_t));
  561. }
  562. }
  563. }
  564. else /* APP_USBD_SETUP_REQDIR_OUT */
  565. {
  566. switch (p_setup_ev->setup.bRequest)
  567. {
  568. case APP_USBD_SETUP_STDREQ_SET_INTERFACE:
  569. {
  570. if (!(usb_state == APP_USBD_STATE_Configured))
  571. {
  572. return NRF_ERROR_INVALID_STATE;
  573. }
  574. if(p_setup_ev->setup.wValue.w > UINT8_MAX)
  575. {
  576. return NRF_ERROR_INVALID_PARAM;
  577. }
  578. uint8_t alt = p_setup_ev->setup.wValue.lb;
  579. return app_usbd_iface_select(p_class_inst, iface_idx, alt);
  580. }
  581. default:
  582. break;
  583. }
  584. }
  585. return NRF_ERROR_NOT_SUPPORTED;
  586. }
  587. /**
  588. * @brief
  589. *
  590. * Descriptors feeder handle structure.
  591. */
  592. static const nrf_drv_usbd_handler_desc_t usbd_descriptor_feeder_desc =
  593. {
  594. .handler = { .feeder = usbd_descriptor_conf_feeder },
  595. .p_context = &m_descriptor_conf_feed_data
  596. };
  597. static ret_code_t setup_device_req_get_status(
  598. app_usbd_class_inst_t const * const p_inst,
  599. app_usbd_setup_evt_t const * const p_setup_ev)
  600. {
  601. size_t max_size;
  602. uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size);
  603. ASSERT(sizeof(uint16_t) <= max_size);
  604. memset(p_trans_buff, 0, sizeof(uint16_t));
  605. if (m_configuration_dsc.bmAttributes &
  606. APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_SELF_POWERED_MASK)
  607. {
  608. SET_BIT(p_trans_buff[0], 0);
  609. }
  610. if (IS_SET(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP))
  611. {
  612. SET_BIT(p_trans_buff[0], 1);
  613. }
  614. return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, sizeof(uint16_t));
  615. }
  616. static ret_code_t setup_device_req_get_descriptor(app_usbd_class_inst_t const * const p_inst,
  617. app_usbd_setup_evt_t const * const p_setup_ev)
  618. {
  619. switch (p_setup_ev->setup.wValue.hb)
  620. {
  621. case APP_USBD_DESCRIPTOR_DEVICE:
  622. {
  623. if(p_setup_ev->setup.wLength.w == 0)
  624. {
  625. return NRF_SUCCESS;
  626. }
  627. return app_usbd_core_setup_rsp(&(p_setup_ev->setup),
  628. &m_device_dsc,
  629. sizeof(m_device_dsc));
  630. }
  631. case APP_USBD_DESCRIPTOR_CONFIGURATION:
  632. {
  633. if(p_setup_ev->setup.wLength.w == 0)
  634. {
  635. return NRF_SUCCESS;
  636. }
  637. /* The size equals the size of configuration descriptor and all classes descriptors */
  638. const size_t size = MIN(
  639. sizeof(app_usbd_descriptor_configuration_t),
  640. p_setup_ev->setup.wLength.w);
  641. size_t total_length = sizeof(app_usbd_descriptor_configuration_t);
  642. uint8_t iface_count = 0;
  643. /* Iterate over all registered classes count descriptors and total size */
  644. app_usbd_class_inst_t const * p_class;
  645. for (p_class = app_usbd_class_first_get(); p_class != NULL;
  646. p_class = app_usbd_class_next_get(p_class))
  647. {
  648. ASSERT(NULL != (p_class->p_class_methods));
  649. ASSERT(NULL != (p_class->p_class_methods->feed_descriptors));
  650. size_t dsc_size = 0;
  651. app_usbd_class_descriptor_ctx_t siz_desc;
  652. APP_USBD_CLASS_DESCRIPTOR_INIT(&siz_desc);
  653. while(p_class->p_class_methods->feed_descriptors(&siz_desc,
  654. p_class,
  655. NULL,
  656. sizeof(uint8_t))
  657. )
  658. {
  659. dsc_size++;
  660. }
  661. total_length += dsc_size;
  662. iface_count += app_usbd_class_iface_count_get(p_class);
  663. }
  664. /* Access transmission buffer */
  665. size_t max_size;
  666. app_usbd_descriptor_configuration_t * p_trans_buff =
  667. app_usbd_core_setup_transfer_buff_get(&max_size);
  668. /* Copy the configuration descriptor and update the fields that require it */
  669. ASSERT(size <= max_size);
  670. memcpy(p_trans_buff, &m_configuration_dsc, size);
  671. p_trans_buff->bNumInterfaces = iface_count;
  672. p_trans_buff->wTotalLength = total_length;
  673. if (app_usbd_class_rwu_enabled_check())
  674. {
  675. p_trans_buff->bmAttributes |=
  676. APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_REMOTE_WAKEUP_MASK;
  677. }
  678. m_descriptor_conf_feed_data.p_cinst = app_usbd_class_first_get();
  679. m_descriptor_conf_feed_data.p_desc = (void *)p_trans_buff;
  680. m_descriptor_conf_feed_data.desc_left = size;
  681. m_descriptor_conf_feed_data.total_left = p_setup_ev->setup.wLength.w;
  682. /* Start first transfer */
  683. ret_code_t ret;
  684. CRITICAL_REGION_ENTER();
  685. ret = app_usbd_ep_handled_transfer(
  686. NRF_DRV_USBD_EPIN0,
  687. &usbd_descriptor_feeder_desc);
  688. if (NRF_SUCCESS == ret)
  689. {
  690. ret = app_usbd_core_setup_data_handler_set(
  691. NRF_DRV_USBD_EPIN0,
  692. &m_setup_data_handler_empty_desc);
  693. }
  694. CRITICAL_REGION_EXIT();
  695. return ret;
  696. }
  697. case APP_USBD_DESCRIPTOR_STRING:
  698. {
  699. if(p_setup_ev->setup.wLength.w == 0)
  700. {
  701. return NRF_SUCCESS;
  702. }
  703. app_usbd_string_desc_idx_t id =
  704. (app_usbd_string_desc_idx_t)(p_setup_ev->setup.wValue.lb);
  705. uint16_t langid = p_setup_ev->setup.wIndex.w;
  706. uint16_t const * p_string_dsc = app_usbd_string_desc_get(id, langid);
  707. if (p_string_dsc == NULL)
  708. {
  709. return NRF_ERROR_NOT_SUPPORTED;
  710. }
  711. return app_usbd_core_setup_rsp(
  712. &p_setup_ev->setup,
  713. p_string_dsc,
  714. app_usbd_string_desc_length(p_string_dsc));
  715. }
  716. default:
  717. break;
  718. }
  719. return NRF_ERROR_NOT_SUPPORTED;
  720. }
  721. static ret_code_t setup_device_req_get_configuration(
  722. app_usbd_class_inst_t const * const p_inst,
  723. app_usbd_setup_evt_t const * const p_setup_ev)
  724. {
  725. size_t max_size;
  726. uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size);
  727. app_usbd_state_t usb_state = usbd_core_state_get();
  728. if (usb_state == APP_USBD_STATE_Configured)
  729. {
  730. p_trans_buff[0] = 1;
  731. }
  732. else if (usb_state == APP_USBD_STATE_Addressed)
  733. {
  734. p_trans_buff[0] = 0;
  735. }
  736. else
  737. {
  738. return NRF_ERROR_NOT_SUPPORTED;
  739. }
  740. return app_usbd_core_setup_rsp(&p_setup_ev->setup, p_trans_buff, sizeof(p_trans_buff[0]));
  741. }
  742. static ret_code_t setup_device_req_set_configuration(
  743. app_usbd_class_inst_t const * const p_inst,
  744. app_usbd_setup_evt_t const * const p_setup_ev)
  745. {
  746. app_usbd_state_t usb_state = usbd_core_state_get();
  747. if (!((usb_state == APP_USBD_STATE_Configured) ||
  748. (usb_state == APP_USBD_STATE_Addressed)))
  749. {
  750. return NRF_ERROR_INVALID_STATE;
  751. }
  752. if (p_setup_ev->setup.wValue.lb == 0)
  753. {
  754. app_usbd_all_iface_deselect();
  755. usbd_core_state_set(APP_USBD_STATE_Addressed);
  756. }
  757. else if (p_setup_ev->setup.wValue.lb == 1)
  758. {
  759. /*Clear all bulk/interrupt endpoint status and set toggle to DATA0*/
  760. app_usbd_all_iface_select_0();
  761. usbd_core_state_set(APP_USBD_STATE_Configured);
  762. }
  763. else
  764. {
  765. /*In this driver only one configuration is supported.*/
  766. return NRF_ERROR_INVALID_PARAM;
  767. }
  768. return NRF_SUCCESS;
  769. }
  770. /**
  771. * @brief Internal SETUP event handler.
  772. * @param[in] p_inst Instance of the class.
  773. * @param[in] p_setup_ev Setup request.
  774. * @return Standard error code.
  775. * @retval NRF_SUCCESS Request handled correctly.
  776. * @retval NRF_ERROR_NOT_SUPPORTED Request is not supported.
  777. */
  778. static ret_code_t setup_device_req_std_handler(app_usbd_class_inst_t const * const p_inst,
  779. app_usbd_setup_evt_t const * const p_setup_ev)
  780. {
  781. ASSERT(p_inst != NULL);
  782. ASSERT(p_setup_ev != NULL);
  783. if (APP_USBD_SETUP_REQTYPE_STD != app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType))
  784. {
  785. return NRF_ERROR_NOT_SUPPORTED;
  786. }
  787. if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN)
  788. {
  789. switch (p_setup_ev->setup.bRequest)
  790. {
  791. case APP_USBD_SETUP_STDREQ_GET_STATUS:
  792. {
  793. return setup_device_req_get_status(p_inst, p_setup_ev);
  794. }
  795. case APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR:
  796. {
  797. return setup_device_req_get_descriptor(p_inst, p_setup_ev);
  798. }
  799. case APP_USBD_SETUP_STDREQ_GET_CONFIGURATION:
  800. {
  801. return setup_device_req_get_configuration(p_inst, p_setup_ev);
  802. }
  803. default:
  804. /*Not supported*/
  805. break;
  806. }
  807. return NRF_ERROR_NOT_SUPPORTED;
  808. }
  809. else /* APP_USBD_SETUP_REQDIR_OUT */
  810. {
  811. switch (p_setup_ev->setup.bRequest)
  812. {
  813. case APP_USBD_SETUP_STDREQ_SET_ADDRESS:
  814. {
  815. ASSERT(0); /* should never reach this point */
  816. break;
  817. }
  818. case APP_USBD_SETUP_STDREQ_SET_FEATURE:
  819. {
  820. if (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP)
  821. {
  822. if (!app_usbd_class_rwu_enabled_check())
  823. {
  824. return NRF_ERROR_FORBIDDEN;
  825. }
  826. SET_BIT(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP);
  827. return NRF_SUCCESS;
  828. }
  829. break;
  830. }
  831. case APP_USBD_SETUP_STDREQ_CLEAR_FEATURE:
  832. {
  833. if (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP)
  834. {
  835. if (!app_usbd_class_rwu_enabled_check())
  836. {
  837. return NRF_ERROR_FORBIDDEN;
  838. }
  839. CLR_BIT(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP);
  840. return NRF_SUCCESS;
  841. }
  842. break;
  843. }
  844. case APP_USBD_SETUP_STDREQ_SET_CONFIGURATION:
  845. {
  846. return setup_device_req_set_configuration(p_inst, p_setup_ev);
  847. }
  848. case APP_USBD_SETUP_STDREQ_SET_DESCRIPTOR:
  849. {
  850. /*Not supported yet.*/
  851. break;
  852. }
  853. default:
  854. /*Not supported*/
  855. break;
  856. }
  857. return NRF_ERROR_NOT_SUPPORTED;
  858. }
  859. }
  860. /**
  861. * @brief Process SETUP command.
  862. *
  863. * Auxiliary function for SETUP command processing.
  864. */
  865. static inline ret_code_t app_usbd_core_setup_req_handler(app_usbd_class_inst_t const * const p_inst)
  866. {
  867. app_usbd_setup_evt_t setup_ev;
  868. ret_code_t ret = NRF_ERROR_NOT_SUPPORTED; /* Final result of request processing function */
  869. setup_ev.type = APP_USBD_EVT_DRV_SETUP;
  870. nrf_drv_usbd_setup_get((nrf_drv_usbd_setup_t *)&(setup_ev.setup));
  871. NRF_LOG_DEBUG("SETUP: t: 0x%.2x r: 0x%.2x",
  872. setup_ev.setup.bmRequestType,
  873. setup_ev.setup.bRequest);
  874. if (usb_core_ep0_handler_check())
  875. {
  876. NRF_LOG_WARNING("Previous setup not finished!");
  877. }
  878. /* Clear EP0 handler if there is anything in progress */
  879. usbd_core_ep0_handler_clear();
  880. switch (app_usbd_setup_req_rec(setup_ev.setup.bmRequestType))
  881. {
  882. case APP_USBD_SETUP_REQREC_DEVICE:
  883. {
  884. /* Endpoint 0 has core instance (that process device requests) connected */
  885. ret = setup_device_req_std_handler(p_inst, &setup_ev);
  886. if (ret == NRF_ERROR_NOT_SUPPORTED)
  887. {
  888. ret = app_usbd_all_until_served_call((app_usbd_complex_evt_t const *)&setup_ev);
  889. }
  890. break;
  891. }
  892. case APP_USBD_SETUP_REQREC_INTERFACE:
  893. {
  894. uint8_t const iface_number = setup_ev.setup.wIndex.lb;
  895. uint8_t iface_idx;
  896. app_usbd_class_inst_t const * p_inst_found = app_usbd_iface_find(
  897. iface_number,
  898. &iface_idx);
  899. if (p_inst_found == NULL)
  900. {
  901. ret = NRF_ERROR_INVALID_ADDR;
  902. }
  903. else
  904. {
  905. ret = app_usbd_iface_call(
  906. p_inst_found, iface_idx,
  907. (app_usbd_complex_evt_t const *)&setup_ev);
  908. if (ret == NRF_ERROR_NOT_SUPPORTED)
  909. {
  910. ret = setup_interface_req_std_handle(p_inst_found, iface_idx, &setup_ev);
  911. }
  912. }
  913. break;
  914. }
  915. case APP_USBD_SETUP_REQREC_ENDPOINT:
  916. {
  917. ret = NRF_ERROR_NOT_SUPPORTED;
  918. nrf_drv_usbd_ep_t ep = (nrf_drv_usbd_ep_t)setup_ev.setup.wIndex.lb;
  919. if ((NRF_USBD_EP_NR_GET(ep) != 0)) /* For EP0 we would call this function again! */
  920. {
  921. ret = app_usbd_ep_call(ep, (app_usbd_complex_evt_t const *)&setup_ev);
  922. }
  923. if (ret == NRF_ERROR_NOT_SUPPORTED)
  924. {
  925. ret = setup_endpoint_req_std(&setup_ev);
  926. }
  927. break;
  928. }
  929. case APP_USBD_SETUP_REQREC_OTHER:
  930. {
  931. /* Try to process via every instance */
  932. ret = app_usbd_all_until_served_call((app_usbd_complex_evt_t const *)&setup_ev);
  933. break;
  934. }
  935. default:
  936. break;
  937. }
  938. /* Processing result */
  939. if (ret == NRF_SUCCESS)
  940. {
  941. if (usb_core_ep0_handler_check())
  942. {
  943. if (NRF_DRV_USBD_EPOUT0 == nrf_drv_usbd_last_setup_dir_get())
  944. {
  945. /* Request processed successfully and requires SETUP data */
  946. nrf_drv_usbd_setup_data_clear();
  947. }
  948. }
  949. else
  950. {
  951. /* Request processed successfully */
  952. nrf_drv_usbd_setup_clear();
  953. }
  954. }
  955. else
  956. {
  957. /* Request finished with error */
  958. nrf_drv_usbd_setup_stall();
  959. }
  960. return ret;
  961. }
  962. /**
  963. * @brief Event handler for core module.
  964. *
  965. * The event handler that would process all events directed to device.
  966. *
  967. */
  968. static ret_code_t app_usbd_core_event_handler(app_usbd_class_inst_t const * const p_inst,
  969. app_usbd_complex_evt_t const * const p_event)
  970. {
  971. ret_code_t ret = NRF_ERROR_NOT_SUPPORTED;
  972. switch (p_event->type)
  973. {
  974. case APP_USBD_EVT_DRV_RESET:
  975. {
  976. usbd_core_state_set(APP_USBD_STATE_Default);
  977. break;
  978. }
  979. case APP_USBD_EVT_DRV_SUSPEND:
  980. {
  981. ret = NRF_SUCCESS;
  982. break;
  983. }
  984. case APP_USBD_EVT_DRV_RESUME:
  985. {
  986. if (nrf_atomic_flag_clear_fetch(&m_rwu_pending) != 0)
  987. {
  988. nrf_usbd_task_trigger(NRF_USBD_TASK_NODRIVEDPDM);
  989. }
  990. ASSERT(usbd_core_state_get() >= APP_USBD_STATE_Unattached);
  991. ret = NRF_SUCCESS;
  992. break;
  993. }
  994. case APP_USBD_EVT_SETUP_SETADDRESS:
  995. {
  996. app_usbd_state_t usb_state = usbd_core_state_get();
  997. if (usb_state == APP_USBD_STATE_Default)
  998. {
  999. usbd_core_state_set(APP_USBD_STATE_Addressed);
  1000. }
  1001. break;
  1002. }
  1003. case APP_USBD_EVT_DRV_SETUP:
  1004. {
  1005. ret = app_usbd_core_setup_req_handler(p_inst);
  1006. break;
  1007. }
  1008. case APP_USBD_EVT_INST_APPEND:
  1009. {
  1010. ASSERT(usbd_core_state_get() == APP_USBD_STATE_Disabled);
  1011. ret = NRF_SUCCESS;
  1012. break;
  1013. }
  1014. case APP_USBD_EVT_INST_REMOVE:
  1015. {
  1016. ASSERT(usbd_core_state_get() == APP_USBD_STATE_Unattached);
  1017. usbd_core_state_set(APP_USBD_STATE_Disabled);
  1018. ret = NRF_SUCCESS;
  1019. break;
  1020. }
  1021. case APP_USBD_EVT_STARTED:
  1022. {
  1023. if (usbd_core_power_is_detected())
  1024. {
  1025. usbd_core_state_set(APP_USBD_STATE_Powered);
  1026. }
  1027. else
  1028. {
  1029. usbd_core_state_set(APP_USBD_STATE_Unattached);
  1030. }
  1031. ret = NRF_SUCCESS;
  1032. break;
  1033. }
  1034. case APP_USBD_EVT_STOPPED:
  1035. {
  1036. ASSERT(usbd_core_state_get() >= APP_USBD_STATE_Powered);
  1037. usbd_core_state_set(APP_USBD_STATE_Unattached);
  1038. ret = NRF_SUCCESS;
  1039. break;
  1040. }
  1041. /* Data transfer on endpoint 0 */
  1042. case APP_USBD_EVT_DRV_EPTRANSFER:
  1043. {
  1044. if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_ABORTED)
  1045. {
  1046. /* Just ignore aborting */
  1047. break;
  1048. }
  1049. /* This EPTRANSFER event has to be called only for EP0 */
  1050. ASSERT((p_event->drv_evt.data.eptransfer.ep == NRF_DRV_USBD_EPOUT0) ||
  1051. (p_event->drv_evt.data.eptransfer.ep == NRF_DRV_USBD_EPIN0));
  1052. ret = usbd_core_ep0_handler_call_and_clear(p_event->drv_evt.data.eptransfer.status);
  1053. /* Processing result */
  1054. if (ret == NRF_SUCCESS)
  1055. {
  1056. if (usb_core_ep0_handler_check())
  1057. {
  1058. if (p_event->drv_evt.data.eptransfer.ep == NRF_DRV_USBD_EPOUT0)
  1059. {
  1060. /* Request processed successfully and requires SETUP data */
  1061. nrf_drv_usbd_setup_data_clear();
  1062. }
  1063. }
  1064. else
  1065. {
  1066. nrf_drv_usbd_setup_clear();
  1067. }
  1068. }
  1069. else
  1070. {
  1071. /* Request finished with error */
  1072. nrf_drv_usbd_setup_stall();
  1073. }
  1074. break;
  1075. }
  1076. default:
  1077. break;
  1078. }
  1079. return ret;
  1080. }
  1081. /** @} */
  1082. void app_usbd_core_ep0_enable(void)
  1083. {
  1084. app_usbd_ep_enable(NRF_DRV_USBD_EPOUT0);
  1085. app_usbd_ep_enable(NRF_DRV_USBD_EPIN0);
  1086. }
  1087. void app_usbd_core_ep0_disable(void)
  1088. {
  1089. app_usbd_ep_disable(NRF_DRV_USBD_EPOUT0);
  1090. app_usbd_ep_disable(NRF_DRV_USBD_EPIN0);
  1091. }
  1092. ret_code_t app_usbd_core_setup_rsp(app_usbd_setup_t const * p_setup,
  1093. void const * p_data,
  1094. size_t size)
  1095. {
  1096. size_t req_size = p_setup->wLength.w;
  1097. size_t tx_size = MIN(req_size, size);
  1098. bool zlp_required = (size < req_size) &&
  1099. (0 == (size % nrf_drv_usbd_ep_max_packet_size_get(NRF_DRV_USBD_EPIN0)));
  1100. NRF_DRV_USBD_TRANSFER_IN_FLAGS(
  1101. transfer,
  1102. p_data,
  1103. tx_size,
  1104. zlp_required ? NRF_DRV_USBD_TRANSFER_ZLP_FLAG : 0);
  1105. ret_code_t ret;
  1106. CRITICAL_REGION_ENTER();
  1107. ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &transfer);
  1108. if (NRF_SUCCESS == ret)
  1109. {
  1110. ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPIN0,
  1111. &m_setup_data_handler_empty_desc);
  1112. }
  1113. CRITICAL_REGION_EXIT();
  1114. return ret;
  1115. }
  1116. ret_code_t app_usbd_core_setup_data_handler_set(
  1117. nrf_drv_usbd_ep_t ep,
  1118. app_usbd_core_setup_data_handler_desc_t const * const p_handler_desc)
  1119. {
  1120. if (nrf_drv_usbd_last_setup_dir_get() != ep)
  1121. {
  1122. return NRF_ERROR_INVALID_ADDR;
  1123. }
  1124. m_ep0_handler_desc = *p_handler_desc;
  1125. return NRF_SUCCESS;
  1126. }
  1127. void * app_usbd_core_setup_transfer_buff_get(size_t * p_size)
  1128. {
  1129. if (p_size != NULL)
  1130. *p_size = sizeof(m_setup_transfer_buff);
  1131. return m_setup_transfer_buff;
  1132. }
  1133. app_usbd_state_t app_usbd_core_state_get(void)
  1134. {
  1135. return usbd_core_state_get();
  1136. }
  1137. bool app_usbd_core_feature_state_get(app_usbd_setup_stdfeature_t feature)
  1138. {
  1139. return IS_SET(m_device_features_state, feature) ? true : false;
  1140. }
  1141. #endif //NRF_MODULE_ENABLED(APP_USBD)