msc_class.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. **************************************************************************
  3. * @file msc_class.c
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb msc class type
  7. **************************************************************************
  8. * Copyright notice & Disclaimer
  9. *
  10. * The software Board Support Package (BSP) that is made available to
  11. * download from Artery official website is the copyrighted work of Artery.
  12. * Artery authorizes customers to use, copy, and distribute the BSP
  13. * software and its related documentation for the purpose of design and
  14. * development in conjunction with Artery microcontrollers. Use of the
  15. * software is governed by this copyright notice and the following disclaimer.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23. *
  24. **************************************************************************
  25. */
  26. #include "usbd_core.h"
  27. #include "msc_class.h"
  28. #include "msc_desc.h"
  29. #include "msc_bot_scsi.h"
  30. /** @addtogroup AT32F403A_407_middlewares_usbd_class
  31. * @{
  32. */
  33. /** @defgroup USB_msc_class
  34. * @brief usb device class msc demo
  35. * @{
  36. */
  37. /** @defgroup USB_msc_class_private_functions
  38. * @{
  39. */
  40. usb_sts_type class_init_handler(void *udev);
  41. usb_sts_type class_clear_handler(void *udev);
  42. usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
  43. usb_sts_type class_ept0_tx_handler(void *udev);
  44. usb_sts_type class_ept0_rx_handler(void *udev);
  45. usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
  46. usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
  47. usb_sts_type class_sof_handler(void *udev);
  48. usb_sts_type class_event_handler(void *udev, usbd_event_type event);
  49. /* usb rx and tx buffer */
  50. static uint32_t alt_setting = 0;
  51. extern msc_type msc_struct;
  52. extern cbw_type cbw_struct;
  53. extern csw_type csw_struct;
  54. /* usb device class handler */
  55. usbd_class_handler msc_class_handler =
  56. {
  57. class_init_handler,
  58. class_clear_handler,
  59. class_setup_handler,
  60. class_ept0_tx_handler,
  61. class_ept0_rx_handler,
  62. class_in_handler,
  63. class_out_handler,
  64. class_sof_handler,
  65. class_event_handler,
  66. };
  67. /**
  68. * @brief initialize usb endpoint
  69. * @param udev: to the structure of usbd_core_type
  70. * @retval status of usb_sts_type
  71. */
  72. usb_sts_type class_init_handler(void *udev)
  73. {
  74. usb_sts_type status = USB_OK;
  75. usbd_core_type *pudev = (usbd_core_type *)udev;
  76. /* open in endpoint */
  77. usbd_ept_open(pudev, USBD_MSC_BULK_IN_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
  78. /* open out endpoint */
  79. usbd_ept_open(pudev, USBD_MSC_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
  80. bot_scsi_init(udev);
  81. return status;
  82. }
  83. /**
  84. * @brief clear endpoint or other state
  85. * @param udev: to the structure of usbd_core_type
  86. * @retval status of usb_sts_type
  87. */
  88. usb_sts_type class_clear_handler(void *udev)
  89. {
  90. usb_sts_type status = USB_OK;
  91. usbd_core_type *pudev = (usbd_core_type *)udev;
  92. /* close in endpoint */
  93. usbd_ept_close(pudev, USBD_MSC_BULK_IN_EPT);
  94. /* close out endpoint */
  95. usbd_ept_close(pudev, USBD_MSC_BULK_OUT_EPT);
  96. return status;
  97. }
  98. /**
  99. * @brief usb device class setup request handler
  100. * @param udev: to the structure of usbd_core_type
  101. * @param setup: setup packet
  102. * @retval status of usb_sts_type
  103. */
  104. usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
  105. {
  106. usb_sts_type status = USB_OK;
  107. usbd_core_type *pudev = (usbd_core_type *)udev;
  108. switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
  109. {
  110. /* class request */
  111. case USB_REQ_TYPE_CLASS:
  112. switch(setup->bRequest)
  113. {
  114. case MSC_REQ_GET_MAX_LUN:
  115. usbd_ctrl_send(pudev, (uint8_t *)&msc_struct.max_lun, 1);
  116. break;
  117. case MSC_REQ_BO_RESET:
  118. bot_scsi_reset(udev);
  119. usbd_ctrl_send_status(pudev);
  120. break;
  121. default:
  122. usbd_ctrl_unsupport(pudev);
  123. break;
  124. }
  125. break;
  126. /* standard request */
  127. case USB_REQ_TYPE_STANDARD:
  128. switch(setup->bRequest)
  129. {
  130. case USB_STD_REQ_GET_DESCRIPTOR:
  131. usbd_ctrl_unsupport(pudev);
  132. break;
  133. case USB_STD_REQ_GET_INTERFACE:
  134. usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
  135. break;
  136. case USB_STD_REQ_SET_INTERFACE:
  137. alt_setting = setup->wValue;
  138. break;
  139. case USB_STD_REQ_CLEAR_FEATURE:
  140. usbd_ept_close(pudev, (uint8_t)setup->wIndex);
  141. if((setup->wIndex & 0x80) == 0x80)
  142. {
  143. usbd_ept_open(pudev, (uint8_t)setup->wIndex, EPT_BULK_TYPE, USBD_IN_MAXPACKET_SIZE);
  144. }
  145. else
  146. {
  147. usbd_ept_open(pudev, (uint8_t)setup->wIndex, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
  148. }
  149. bot_scsi_clear_feature(udev, setup->wIndex);
  150. break;
  151. default:
  152. break;
  153. }
  154. break;
  155. default:
  156. usbd_ctrl_unsupport(pudev);
  157. break;
  158. }
  159. return status;
  160. }
  161. /**
  162. * @brief usb device endpoint 0 in status stage complete
  163. * @param udev: to the structure of usbd_core_type
  164. * @retval status of usb_sts_type
  165. */
  166. usb_sts_type class_ept0_tx_handler(void *udev)
  167. {
  168. usb_sts_type status = USB_OK;
  169. /* ...user code... */
  170. return status;
  171. }
  172. /**
  173. * @brief usb device endpoint 0 out status stage complete
  174. * @param udev: usb device core handler type
  175. * @retval status of usb_sts_type
  176. */
  177. usb_sts_type class_ept0_rx_handler(void *udev)
  178. {
  179. usb_sts_type status = USB_OK;
  180. usbd_core_type *pudev = (usbd_core_type *)udev;
  181. uint32_t recv_len = usbd_get_recv_len(pudev, 0);
  182. /* ...user code... */
  183. return status;
  184. }
  185. /**
  186. * @brief usb device transmision complete handler
  187. * @param udev: to the structure of usbd_core_type
  188. * @param ept_num: endpoint number
  189. * @retval status of usb_sts_type
  190. */
  191. usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
  192. {
  193. usb_sts_type status = USB_OK;
  194. bot_scsi_datain_handler(udev, ept_num);
  195. return status;
  196. }
  197. /**
  198. * @brief usb device endpoint receive data
  199. * @param udev: to the structure of usbd_core_type
  200. * @param ept_num: endpoint number
  201. * @retval status of usb_sts_type
  202. */
  203. usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
  204. {
  205. usb_sts_type status = USB_OK;
  206. bot_scsi_dataout_handler(udev, ept_num);
  207. return status;
  208. }
  209. /**
  210. * @brief usb device sof handler
  211. * @param udev: to the structure of usbd_core_type
  212. * @retval status of usb_sts_type
  213. */
  214. usb_sts_type class_sof_handler(void *udev)
  215. {
  216. usb_sts_type status = USB_OK;
  217. /* ...user code... */
  218. return status;
  219. }
  220. /**
  221. * @brief usb device event handler
  222. * @param udev: to the structure of usbd_core_type
  223. * @param event: usb device event
  224. * @retval status of usb_sts_type
  225. */
  226. usb_sts_type class_event_handler(void *udev, usbd_event_type event)
  227. {
  228. usb_sts_type status = USB_OK;
  229. switch(event)
  230. {
  231. case USBD_RESET_EVENT:
  232. /* ...user code... */
  233. break;
  234. case USBD_SUSPEND_EVENT:
  235. /* ...user code... */
  236. break;
  237. case USBD_WAKEUP_EVENT:
  238. /* ...user code... */
  239. break;
  240. default:
  241. break;
  242. }
  243. return status;
  244. }
  245. /**
  246. * @}
  247. */
  248. /**
  249. * @}
  250. */
  251. /**
  252. * @}
  253. */