usb_std.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2016-2017, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*- Includes ----------------------------------------------------------------*/
  29. #include <stdbool.h>
  30. #include <stdalign.h>
  31. #include <string.h>
  32. #include "utils.h"
  33. #include "usb.h"
  34. #include "usb_std.h"
  35. #include "usb_descriptors.h"
  36. /*- Types -------------------------------------------------------------------*/
  37. typedef void (*usb_ep_callback_t)(int size);
  38. /*- Variables ---------------------------------------------------------------*/
  39. static usb_ep_callback_t usb_ep_callbacks[USB_EP_NUM];
  40. /*- Implementations ---------------------------------------------------------*/
  41. //-----------------------------------------------------------------------------
  42. void usb_init(void)
  43. {
  44. for (int i = 0; i < USB_EP_NUM; i++)
  45. usb_ep_callbacks[i] = NULL;
  46. usb_hw_init();
  47. }
  48. //-----------------------------------------------------------------------------
  49. void usb_set_callback(int ep, void (*callback)(int size))
  50. {
  51. usb_ep_callbacks[ep] = callback;
  52. }
  53. //-----------------------------------------------------------------------------
  54. WEAK bool usb_class_handle_request(usb_request_t *request)
  55. {
  56. (void)request;
  57. return false;
  58. }
  59. //-----------------------------------------------------------------------------
  60. bool usb_handle_standard_request(usb_request_t *request)
  61. {
  62. static int usb_config = 0;
  63. switch ((request->bRequest << 8) | request->bmRequestType)
  64. {
  65. case USB_CMD(IN, DEVICE, STANDARD, GET_DESCRIPTOR):
  66. {
  67. int type = request->wValue >> 8;
  68. int index = request->wValue & 0xff;
  69. int length = request->wLength;
  70. if (USB_DEVICE_DESCRIPTOR == type)
  71. {
  72. length = LIMIT(length, usb_device_descriptor.bLength);
  73. usb_control_send((uint8_t *)&usb_device_descriptor, length);
  74. }
  75. else if (USB_CONFIGURATION_DESCRIPTOR == type)
  76. {
  77. length = LIMIT(length, usb_configuration_hierarchy.configuration.wTotalLength);
  78. usb_control_send((uint8_t *)&usb_configuration_hierarchy, length);
  79. }
  80. else if (USB_STRING_DESCRIPTOR == type)
  81. {
  82. if (0 == index)
  83. {
  84. length = LIMIT(length, usb_string_descriptor_zero.bLength);
  85. usb_control_send((uint8_t *)&usb_string_descriptor_zero, length);
  86. }
  87. else if (index < USB_STR_COUNT)
  88. {
  89. const char *str = usb_strings[index];
  90. int len = strlen(str);
  91. int size = len*2 + 2;
  92. alignas(4) uint8_t buf[size];
  93. buf[0] = size;
  94. buf[1] = USB_STRING_DESCRIPTOR;
  95. for (int i = 0; i < len; i++)
  96. {
  97. buf[2 + i*2] = str[i];
  98. buf[3 + i*2] = 0;
  99. }
  100. length = LIMIT(length, size);
  101. usb_control_send(buf, length);
  102. }
  103. else
  104. {
  105. return false;
  106. }
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. } break;
  113. case USB_CMD(OUT, DEVICE, STANDARD, SET_ADDRESS):
  114. {
  115. usb_control_send_zlp();
  116. usb_set_address(request->wValue);
  117. } break;
  118. case USB_CMD(OUT, DEVICE, STANDARD, SET_CONFIGURATION):
  119. {
  120. usb_config = request->wValue;
  121. usb_control_send_zlp();
  122. if (usb_config)
  123. {
  124. int size = usb_configuration_hierarchy.configuration.wTotalLength;
  125. usb_descriptor_header_t *desc = (usb_descriptor_header_t *)&usb_configuration_hierarchy;
  126. while (size)
  127. {
  128. if (USB_ENDPOINT_DESCRIPTOR == desc->bDescriptorType)
  129. usb_configure_endpoint((usb_endpoint_descriptor_t *)desc);
  130. size -= desc->bLength;
  131. desc = (usb_descriptor_header_t *)((uint8_t *)desc + desc->bLength);
  132. }
  133. usb_configuration_callback(usb_config);
  134. }
  135. } break;
  136. case USB_CMD(IN, DEVICE, STANDARD, GET_CONFIGURATION):
  137. {
  138. uint8_t config = usb_config;
  139. usb_control_send(&config, sizeof(config));
  140. } break;
  141. case USB_CMD(IN, DEVICE, STANDARD, GET_STATUS):
  142. case USB_CMD(IN, INTERFACE, STANDARD, GET_STATUS):
  143. {
  144. uint16_t status = 0;
  145. usb_control_send((uint8_t *)&status, sizeof(status));
  146. } break;
  147. case USB_CMD(IN, ENDPOINT, STANDARD, GET_STATUS):
  148. {
  149. int ep = request->wIndex & USB_INDEX_MASK;
  150. int dir = request->wIndex & USB_DIRECTION_MASK;
  151. uint16_t status = 0;
  152. if (usb_endpoint_configured(ep, dir))
  153. {
  154. status = usb_endpoint_get_status(ep, dir);
  155. usb_control_send((uint8_t *)&status, sizeof(status));
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. } break;
  162. case USB_CMD(OUT, DEVICE, STANDARD, SET_FEATURE):
  163. {
  164. return false;
  165. } break;
  166. case USB_CMD(OUT, INTERFACE, STANDARD, SET_FEATURE):
  167. {
  168. usb_control_send_zlp();
  169. } break;
  170. case USB_CMD(OUT, ENDPOINT, STANDARD, SET_FEATURE):
  171. {
  172. int ep = request->wIndex & USB_INDEX_MASK;
  173. int dir = request->wIndex & USB_DIRECTION_MASK;
  174. if (0 == request->wValue && ep && usb_endpoint_configured(ep, dir))
  175. {
  176. usb_endpoint_set_feature(ep, dir);
  177. usb_control_send_zlp();
  178. }
  179. else
  180. {
  181. return false;
  182. }
  183. } break;
  184. case USB_CMD(OUT, DEVICE, STANDARD, CLEAR_FEATURE):
  185. {
  186. return false;
  187. } break;
  188. case USB_CMD(OUT, INTERFACE, STANDARD, CLEAR_FEATURE):
  189. {
  190. usb_control_send_zlp();
  191. } break;
  192. case USB_CMD(OUT, ENDPOINT, STANDARD, CLEAR_FEATURE):
  193. {
  194. int ep = request->wIndex & USB_INDEX_MASK;
  195. int dir = request->wIndex & USB_DIRECTION_MASK;
  196. if (0 == request->wValue && ep && usb_endpoint_configured(ep, dir))
  197. {
  198. usb_endpoint_clear_feature(ep, dir);
  199. usb_control_send_zlp();
  200. }
  201. else
  202. {
  203. return false;
  204. }
  205. } break;
  206. default:
  207. {
  208. if (!usb_class_handle_request(request))
  209. return false;
  210. } break;
  211. }
  212. return true;
  213. }
  214. //-----------------------------------------------------------------------------
  215. void usb_send_callback(int ep)
  216. {
  217. if (usb_ep_callbacks[ep])
  218. usb_ep_callbacks[ep](0);
  219. }
  220. //-----------------------------------------------------------------------------
  221. void usb_recv_callback(int ep, int size)
  222. {
  223. if (usb_ep_callbacks[ep])
  224. usb_ep_callbacks[ep](size);
  225. }