app_usbd_hid_generic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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_HID_GENERIC)
  42. #include <string.h>
  43. #include "app_usbd_hid_generic.h"
  44. #include "app_util_platform.h"
  45. /**
  46. * @ingroup app_usbd_hid_generic
  47. *
  48. * Module with types, definitions and API used by HID generic.
  49. * @{
  50. */
  51. /**
  52. * @brief Auxiliary function to access HID generic context data.
  53. *
  54. * @param[in] p_generic HID generic instance.
  55. *
  56. * @return HID generic class instance data context.
  57. */
  58. static inline app_usbd_hid_generic_ctx_t *
  59. hid_generic_ctx_get(app_usbd_hid_generic_t const * p_generic)
  60. {
  61. ASSERT(p_generic != NULL);
  62. ASSERT(p_generic->specific.p_data != NULL);
  63. return &p_generic->specific.p_data->ctx;
  64. }
  65. /**
  66. * @brief Auxiliary function to access HID generic instance data.
  67. *
  68. * @param[in] p_inst Class instance data.
  69. *
  70. * @return HID generic class instance data.
  71. */
  72. static inline app_usbd_hid_generic_t const *
  73. hid_generic_get(app_usbd_class_inst_t const * p_inst)
  74. {
  75. ASSERT(p_inst != NULL);
  76. return (app_usbd_hid_generic_t const *)p_inst;
  77. }
  78. /**
  79. * @brief Returns report ID buffer descriptor.
  80. *
  81. * @param[in] p_generic Internal HID generic context.
  82. *
  83. * @return HID report buffer.
  84. */
  85. static inline app_usbd_hid_report_buffer_t *
  86. hid_generic_rep_buffer_get(app_usbd_hid_generic_t const * p_generic)
  87. {
  88. ASSERT(p_generic != NULL);
  89. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  90. return app_usbd_hid_rep_buff_in_get(p_hinst);
  91. }
  92. /**
  93. * @brief Auxiliary function to prepare report transfer buffer to next transfer.
  94. *
  95. * @param[in] p_generic HID generic instance.
  96. *
  97. * @retval true Next transfer required.
  98. * @retval false Next transfer not required.
  99. */
  100. static inline bool hid_generic_transfer_next(app_usbd_hid_generic_t const * p_generic)
  101. {
  102. nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue;
  103. return !nrf_queue_is_empty(p_rep_in_queue);
  104. }
  105. /**
  106. * @brief Triggers IN endpoint transfer.
  107. *
  108. * @param[in] p_generic HID generic instance.
  109. *
  110. * @return Standard error code.
  111. */
  112. static inline ret_code_t hid_generic_transfer_set(app_usbd_hid_generic_t const * p_generic)
  113. {
  114. app_usbd_class_inst_t const * p_inst = (app_usbd_class_inst_t const *)p_generic;
  115. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  116. nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epin_addr_get(p_inst);
  117. app_usbd_hid_state_flag_clr(&p_generic_ctx->hid_ctx,
  118. APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS);
  119. if (!hid_generic_transfer_next(p_generic))
  120. {
  121. return NRF_SUCCESS;
  122. }
  123. app_usbd_hid_report_buffer_t * p_rep_buff = hid_generic_rep_buffer_get(p_generic);
  124. nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue;
  125. ret_code_t ret = nrf_queue_pop(p_rep_in_queue, p_rep_buff);
  126. ASSERT(ret == NRF_SUCCESS);
  127. NRF_DRV_USBD_TRANSFER_IN(transfer, p_rep_buff->p_buff, p_rep_buff->size);
  128. CRITICAL_REGION_ENTER();
  129. ret = app_usbd_ep_transfer(ep_addr, &transfer);
  130. if (ret == NRF_SUCCESS)
  131. {
  132. app_usbd_hid_state_flag_set(&p_generic_ctx->hid_ctx,
  133. APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS);
  134. }
  135. CRITICAL_REGION_EXIT();
  136. return ret;
  137. }
  138. ret_code_t hid_generic_clear_buffer(app_usbd_class_inst_t const * p_inst)
  139. {
  140. ASSERT(p_inst != NULL);
  141. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  142. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  143. app_usbd_hid_report_buffer_t * p_rep_buffer = hid_generic_rep_buffer_get(p_generic);
  144. nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue;
  145. ret_code_t ret = NRF_SUCCESS;
  146. /* Clear all queued reports */
  147. while(ret != NRF_ERROR_NOT_FOUND)
  148. {
  149. ret = nrf_queue_pop(p_rep_in_queue, p_rep_buffer);
  150. }
  151. CRITICAL_REGION_ENTER();
  152. uint8_t iface_count = app_usbd_class_iface_count_get(p_inst);
  153. app_usbd_class_iface_conf_t const * p_iface = NULL;
  154. for (uint8_t i = 0; i < iface_count; ++i)
  155. {
  156. p_iface = app_usbd_class_iface_get(p_inst, i);
  157. uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface);
  158. for (uint8_t j = 0; j < ep_count; ++j)
  159. {
  160. /*Abort transfer on every IN endpoint*/
  161. app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, j);
  162. ASSERT(!NRF_USBD_EPISO_CHECK(p_ep->address));
  163. if (NRF_USBD_EPIN_CHECK(p_ep->address))
  164. {
  165. nrf_drv_usbd_ep_abort(p_ep->address);
  166. }
  167. }
  168. }
  169. app_usbd_hid_state_flag_clr(&p_generic_ctx->hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS);
  170. CRITICAL_REGION_EXIT();
  171. return ret;
  172. }
  173. ret_code_t app_usbd_hid_generic_in_report_set(app_usbd_hid_generic_t const * p_generic,
  174. const void * p_buff,
  175. size_t size)
  176. {
  177. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  178. nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue;
  179. const app_usbd_hid_report_buffer_t rep_buff = {
  180. .p_buff = (void *)p_buff,
  181. .size = size,
  182. };
  183. if (nrf_queue_push(p_rep_in_queue, &rep_buff) != NRF_SUCCESS)
  184. {
  185. return NRF_ERROR_BUSY;
  186. }
  187. ret_code_t ret = NRF_SUCCESS;
  188. if (app_usbd_hid_trans_required(&p_generic_ctx->hid_ctx) &&
  189. !p_generic_ctx->hid_ctx.idle_on)
  190. {
  191. ret = hid_generic_transfer_set(p_generic);
  192. }
  193. return ret;
  194. }
  195. ret_code_t app_usbd_hid_generic_idle_report_set(app_usbd_hid_generic_t const * p_generic,
  196. const void * p_buff,
  197. size_t size)
  198. {
  199. app_usbd_class_inst_t const * p_inst = (app_usbd_class_inst_t const *)p_generic;
  200. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  201. ret_code_t ret;
  202. nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epin_addr_get(p_inst);
  203. app_usbd_hid_state_flag_clr(&p_generic_ctx->hid_ctx,
  204. APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS);
  205. NRF_DRV_USBD_TRANSFER_IN(transfer, p_buff, size);
  206. CRITICAL_REGION_ENTER();
  207. ret = app_usbd_ep_transfer(ep_addr, &transfer);
  208. if (ret == NRF_SUCCESS)
  209. {
  210. app_usbd_hid_state_flag_set(&p_generic_ctx->hid_ctx,
  211. APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS);
  212. }
  213. CRITICAL_REGION_EXIT();
  214. return ret;
  215. }
  216. const void * app_usbd_hid_generic_in_report_get(app_usbd_hid_generic_t const * p_generic,
  217. size_t * p_size)
  218. {
  219. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  220. *p_size = p_hinst->p_rep_buffer_in->size;
  221. return p_hinst->p_rep_buffer_in->p_buff;
  222. }
  223. const void * app_usbd_hid_generic_out_report_get(app_usbd_hid_generic_t const * p_generic,
  224. size_t * p_size)
  225. {
  226. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  227. *p_size = p_hinst->p_rep_buffer_out->size;
  228. return p_hinst->p_rep_buffer_out->p_buff;
  229. }
  230. const void * app_usbd_hid_generic_feature_report_get(app_usbd_hid_generic_t const * p_generic,
  231. size_t * p_size)
  232. {
  233. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  234. *p_size = p_hinst->p_rep_buffer_feature->size;
  235. return p_hinst->p_rep_buffer_feature->p_buff;
  236. }
  237. /**
  238. * @brief @ref app_usbd_hid_interface_t::on_get_report
  239. */
  240. static ret_code_t hid_generic_on_get_report(app_usbd_class_inst_t const * p_inst,
  241. app_usbd_setup_evt_t const * p_setup_ev)
  242. {
  243. app_usbd_hid_generic_t const * p_hinst = hid_generic_get(p_inst);
  244. uint8_t const * p_rep_buffer = NULL;
  245. size_t buffer_size = 0;
  246. if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_INPUT)
  247. {
  248. p_rep_buffer = app_usbd_hid_generic_in_report_get(p_hinst, &buffer_size);
  249. }
  250. else if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_OUTPUT)
  251. {
  252. p_rep_buffer = app_usbd_hid_generic_out_report_get(p_hinst, &buffer_size);
  253. }
  254. else if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_FEATURE)
  255. {
  256. p_rep_buffer = app_usbd_hid_generic_feature_report_get(p_hinst, &buffer_size);
  257. }
  258. else
  259. {
  260. return NRF_ERROR_NOT_SUPPORTED;
  261. }
  262. return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_rep_buffer, buffer_size);
  263. }
  264. static ret_code_t hid_generic_on_set_report_data_cb(nrf_drv_usbd_ep_status_t status,
  265. void * p_context)
  266. {
  267. app_usbd_hid_user_ev_handler_t handler;
  268. app_usbd_hid_generic_t const * p_generic = (app_usbd_hid_generic_t const *)p_context;
  269. if (status != NRF_USBD_EP_OK)
  270. {
  271. return NRF_ERROR_INTERNAL;
  272. }
  273. handler = p_generic->specific.inst.hid_inst.user_event_handler;
  274. handler((app_usbd_class_inst_t const *)p_generic,
  275. APP_USBD_HID_USER_EVT_OUT_REPORT_READY);
  276. return NRF_SUCCESS;
  277. }
  278. static ret_code_t hid_generic_on_set_report_feature_data_cb(nrf_drv_usbd_ep_status_t status,
  279. void * p_context)
  280. {
  281. app_usbd_hid_user_ev_handler_t handler;
  282. app_usbd_hid_generic_t const * p_generic = (app_usbd_hid_generic_t const *)p_context;
  283. if (status != NRF_USBD_EP_OK)
  284. {
  285. return NRF_ERROR_INTERNAL;
  286. }
  287. handler = p_generic->specific.inst.hid_inst.user_event_handler;
  288. handler((app_usbd_class_inst_t const *)p_generic,
  289. APP_USBD_HID_USER_EVT_FEATURE_REPORT_READY);
  290. return NRF_SUCCESS;
  291. }
  292. /**
  293. * @brief @ref app_usbd_hid_interface_t::on_set_report
  294. */
  295. static ret_code_t hid_generic_on_set_report(app_usbd_class_inst_t const * p_inst,
  296. app_usbd_setup_evt_t const * p_setup_ev)
  297. {
  298. if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_OUTPUT)
  299. {
  300. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  301. /*Request setup data*/
  302. app_usbd_hid_report_buffer_t const * p_rep_buff;
  303. p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_generic->specific.inst.hid_inst);
  304. p_rep_buff->p_buff[0] = p_setup_ev->setup.wValue.lb;
  305. NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff + 1, p_rep_buff->size - 1);
  306. ret_code_t ret;
  307. CRITICAL_REGION_ENTER();
  308. ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer);
  309. if (ret == NRF_SUCCESS)
  310. {
  311. app_usbd_core_setup_data_handler_desc_t desc = {
  312. .handler = hid_generic_on_set_report_data_cb,
  313. .p_context = (void *)p_generic
  314. };
  315. ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPOUT0, &desc);
  316. }
  317. CRITICAL_REGION_EXIT();
  318. return ret;
  319. }
  320. else if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_FEATURE)
  321. {
  322. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  323. /*Request setup data*/
  324. app_usbd_hid_report_buffer_t const * p_rep_buff;
  325. p_rep_buff = app_usbd_hid_rep_buff_feature_get(&p_generic->specific.inst.hid_inst);
  326. NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff, p_rep_buff->size);
  327. ret_code_t ret;
  328. CRITICAL_REGION_ENTER();
  329. ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer);
  330. if (ret == NRF_SUCCESS)
  331. {
  332. app_usbd_core_setup_data_handler_desc_t desc = {
  333. .handler = hid_generic_on_set_report_feature_data_cb,
  334. .p_context = (void *)p_generic
  335. };
  336. ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPOUT0, &desc);
  337. }
  338. CRITICAL_REGION_EXIT();
  339. return ret;
  340. }
  341. return NRF_ERROR_NOT_SUPPORTED;
  342. }
  343. /**
  344. * @brief @ref app_usbd_hid_interface_t::ep_transfer_in
  345. */
  346. static ret_code_t hid_generic_ep_transfer_in(app_usbd_class_inst_t const * p_inst)
  347. {
  348. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  349. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  350. nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue;
  351. if (nrf_queue_is_empty(p_rep_in_queue))
  352. {
  353. app_usbd_hid_state_flag_clr(&p_generic_ctx->hid_ctx,
  354. APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS);
  355. return NRF_SUCCESS;
  356. }
  357. /* Get next report to send */
  358. return hid_generic_transfer_set((app_usbd_hid_generic_t const *)p_inst);
  359. }
  360. /**
  361. * @brief @ref app_usbd_hid_interface_t::ep_transfer_out
  362. */
  363. static ret_code_t hid_generic_ep_transfer_out(app_usbd_class_inst_t const * p_inst)
  364. {
  365. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  366. nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epout_addr_get(p_inst);
  367. /*Request setup data*/
  368. app_usbd_hid_report_buffer_t const * p_rep_buff;
  369. p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_generic->specific.inst.hid_inst);
  370. NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff, p_rep_buff->size);
  371. return app_usbd_ep_transfer(ep_addr, &transfer);
  372. }
  373. /**
  374. * @brief @ref app_usbd_class_interface_t::event_handler
  375. */
  376. static ret_code_t hid_generic_event_handler(app_usbd_class_inst_t const * p_inst,
  377. app_usbd_complex_evt_t const * p_event)
  378. {
  379. ASSERT(p_inst != NULL);
  380. ASSERT(p_event != NULL);
  381. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  382. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  383. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  384. app_usbd_hid_ctx_t * p_hid_ctx = &p_generic_ctx->hid_ctx;
  385. /*Try handle event by generic HID event handler*/
  386. return app_usbd_hid_event_handler(p_inst, p_hinst, p_hid_ctx, p_event);
  387. }
  388. static uint8_t hid_generic_get_class_descriptors_count(app_usbd_class_inst_t const * p_inst)
  389. {
  390. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  391. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  392. return p_hinst->subclass_desc_count;
  393. }
  394. static app_usbd_descriptor_t hid_generic_get_class_descriptors_type(
  395. app_usbd_class_inst_t const * p_inst,
  396. uint8_t desc_num)
  397. {
  398. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  399. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  400. return p_hinst->p_subclass_desc[desc_num]->type;
  401. }
  402. static size_t hid_generic_get_class_descriptors_length(app_usbd_class_inst_t const * p_inst,
  403. uint8_t desc_num)
  404. {
  405. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  406. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  407. return p_hinst->p_subclass_desc[desc_num]->size;
  408. }
  409. static const uint8_t * hid_generic_get_class_descriptors_data(app_usbd_class_inst_t const * p_inst,
  410. uint8_t desc_num,
  411. uint32_t cur_byte)
  412. {
  413. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  414. app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
  415. const uint8_t * p_byte = &p_hinst->p_subclass_desc[desc_num]->p_data[cur_byte];
  416. return p_byte;
  417. }
  418. /**
  419. * @brief @ref app_usbd_class_interface_t::feed_descriptors
  420. */
  421. static bool hid_generic_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx,
  422. app_usbd_class_inst_t const * p_inst,
  423. uint8_t * p_buff,
  424. size_t max_size)
  425. {
  426. static uint8_t ifaces = 0;
  427. ifaces = app_usbd_class_iface_count_get(p_inst);
  428. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  429. APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size);
  430. static uint8_t i = 0;
  431. for (i = 0; i < ifaces; i++)
  432. {
  433. /* INTERFACE DESCRIPTOR */
  434. APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength
  435. APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface
  436. static app_usbd_class_iface_conf_t const * p_cur_iface = NULL;
  437. p_cur_iface = app_usbd_class_iface_get(p_inst, i);
  438. APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber
  439. APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting
  440. APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints
  441. APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_CLASS); // bInterfaceClass = HID
  442. APP_USBD_CLASS_DESCRIPTOR_WRITE(p_generic->specific.inst.hid_inst.subclass_boot); // bInterfaceSubclass (Boot Interface)
  443. APP_USBD_CLASS_DESCRIPTOR_WRITE(p_generic->specific.inst.hid_inst.protocol); // bInterfaceProtocol
  444. APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface
  445. /* HID DESCRIPTOR */
  446. APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength
  447. APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_DESCRIPTOR_HID); // bDescriptorType = HID
  448. APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(APP_USBD_HID_BCD_VER)); // bcdHID LSB
  449. APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(APP_USBD_HID_BCD_VER)); // bcdHID MSB
  450. APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_COUNTRY_NOT_SUPPORTED); // bCountryCode
  451. APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_generic_get_class_descriptors_count(p_inst)); // bNumDescriptors
  452. static uint8_t class_desc_cnt = 0;
  453. class_desc_cnt = hid_generic_get_class_descriptors_count(p_inst);
  454. static uint8_t j = 0;
  455. static uint16_t class_desc_len = 0;
  456. for (j = 0; j < class_desc_cnt; j++)
  457. {
  458. APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_generic_get_class_descriptors_type(p_inst, j)); // bDescriptorType
  459. class_desc_len = hid_generic_get_class_descriptors_length(p_inst, j);
  460. APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(class_desc_len)); // wDescriptorLength LSB
  461. APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(class_desc_len)); // wDescriptorLength MSB
  462. }
  463. static uint8_t endpoints = 0;
  464. endpoints = app_usbd_class_iface_ep_count_get(p_cur_iface);
  465. for (j = 0; j < endpoints; j++)
  466. {
  467. /* ENDPOINT DESCRIPTOR */
  468. APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLength
  469. APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint
  470. static app_usbd_class_ep_conf_t const * p_cur_ep = NULL;
  471. p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, j);
  472. APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress
  473. APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT); // bmAttributes
  474. APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize LSB
  475. APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize MSB
  476. APP_USBD_CLASS_DESCRIPTOR_WRITE(p_generic->specific.inst.hid_inst.p_ep_interval[j]); // bInterval
  477. }
  478. }
  479. APP_USBD_CLASS_DESCRIPTOR_END();
  480. }
  481. ret_code_t hid_generic_on_set_protocol(app_usbd_hid_generic_t const * p_generic, app_usbd_hid_user_event_t ev)
  482. {
  483. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  484. if (ev == APP_USBD_HID_USER_EVT_SET_BOOT_PROTO)
  485. {
  486. p_generic_ctx->hid_ctx.selected_protocol = APP_USBD_HID_PROTO_BOOT;
  487. }
  488. else if (ev == APP_USBD_HID_USER_EVT_SET_REPORT_PROTO)
  489. {
  490. p_generic_ctx->hid_ctx.selected_protocol = APP_USBD_HID_PROTO_REPORT;
  491. }
  492. else
  493. {
  494. return NRF_ERROR_NOT_SUPPORTED;
  495. }
  496. return NRF_SUCCESS;
  497. }
  498. ret_code_t hid_generic_idle_handler_set(app_usbd_class_inst_t const * p_inst,
  499. app_usbd_hid_idle_handler_t handler)
  500. {
  501. ASSERT(handler != NULL);
  502. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  503. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  504. p_generic_ctx->hid_ctx.idle_handler = handler;
  505. return NRF_SUCCESS;
  506. }
  507. static ret_code_t hid_generic_on_idle(app_usbd_class_inst_t const * p_inst, uint8_t report_id)
  508. {
  509. app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
  510. app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
  511. if(p_generic_ctx->hid_ctx.idle_handler != NULL)
  512. {
  513. return p_generic_ctx->hid_ctx.idle_handler(p_inst, report_id);
  514. }
  515. else
  516. {
  517. return NRF_ERROR_NOT_SUPPORTED;
  518. }
  519. }
  520. /** @} */
  521. const app_usbd_hid_methods_t app_usbd_hid_generic_methods = {
  522. .on_get_report = hid_generic_on_get_report,
  523. .on_set_report = hid_generic_on_set_report,
  524. .ep_transfer_in = hid_generic_ep_transfer_in,
  525. .ep_transfer_out = hid_generic_ep_transfer_out,
  526. .subclass_count = hid_generic_get_class_descriptors_count,
  527. .subclass_length = hid_generic_get_class_descriptors_length,
  528. .subclass_data = hid_generic_get_class_descriptors_data,
  529. .on_idle = hid_generic_on_idle,
  530. .set_idle_handler = hid_generic_idle_handler_set,
  531. };
  532. const app_usbd_class_methods_t app_usbd_generic_class_methods = {
  533. .event_handler = hid_generic_event_handler,
  534. .feed_descriptors = hid_generic_feed_descriptors,
  535. };
  536. #endif //NRF_MODULE_ENABLED(APP_USBD_HID_GENERIC)