usb.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2016, 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 "udc.h"
  33. #include "usb.h"
  34. #include "utils.h"
  35. #include "usb_descriptors.h"
  36. /*- Definitions -------------------------------------------------------------*/
  37. #define USB_CMD(dir, rcpt, type, cmd) \
  38. ((USB_##cmd << 8) | (USB_##dir##_TRANSFER << 7) | (USB_##type##_REQUEST << 5) | (USB_##rcpt##_RECIPIENT << 0))
  39. /*- Types -------------------------------------------------------------------*/
  40. typedef struct
  41. {
  42. void (*callback)(void);
  43. } usb_endpoint_bank_t;
  44. typedef struct
  45. {
  46. usb_endpoint_bank_t out;
  47. usb_endpoint_bank_t in;
  48. } usb_endpoint_t;
  49. /*- Variables ---------------------------------------------------------------*/
  50. static usb_endpoint_t usb_endpoints[UDC_EPT_NUM];
  51. static int usb_config;
  52. /*- Implementations ---------------------------------------------------------*/
  53. //-----------------------------------------------------------------------------
  54. void usb_init(void)
  55. {
  56. usb_config = 0;
  57. udc_init();
  58. }
  59. //-----------------------------------------------------------------------------
  60. void usb_send(int ep, uint8_t *data, int size, void (*callback)(void))
  61. {
  62. usb_endpoints[ep].in.callback = callback;
  63. udc_send(ep, data, size);
  64. }
  65. //-----------------------------------------------------------------------------
  66. void usb_recv(int ep, uint8_t *data, int size, void (*callback)(void))
  67. {
  68. usb_endpoints[ep].out.callback = callback;
  69. udc_recv(ep, data, size);
  70. }
  71. //-----------------------------------------------------------------------------
  72. void usb_handle_standard_request(usb_request_t *request)
  73. {
  74. switch ((request->bRequest << 8) | request->bmRequestType)
  75. {
  76. case USB_CMD(IN, DEVICE, STANDARD, GET_DESCRIPTOR):
  77. {
  78. uint8_t type = request->wValue >> 8;
  79. uint8_t index = request->wValue & 0xff;
  80. uint16_t length = request->wLength;
  81. if (USB_DEVICE_DESCRIPTOR == type)
  82. {
  83. length = LIMIT(length, usb_device_descriptor.bLength);
  84. udc_control_send((uint8_t *)&usb_device_descriptor, length);
  85. }
  86. else if (USB_CONFIGURATION_DESCRIPTOR == type)
  87. {
  88. length = LIMIT(length, usb_configuration_hierarchy.configuration.wTotalLength);
  89. udc_control_send((uint8_t *)&usb_configuration_hierarchy, length);
  90. }
  91. else if (USB_STRING_DESCRIPTOR == type)
  92. {
  93. if (0 == index)
  94. {
  95. length = LIMIT(length, usb_string_descriptor_zero.bLength);
  96. udc_control_send((uint8_t *)&usb_string_descriptor_zero, length);
  97. }
  98. else if (index < USB_STR_COUNT)
  99. {
  100. const char *str = usb_strings[index];
  101. int len = strlen(str);
  102. int size = len*2 + 2;
  103. alignas(4) uint8_t buf[size];
  104. buf[0] = size;
  105. buf[1] = USB_STRING_DESCRIPTOR;
  106. for (int i = 0; i < len; i++)
  107. {
  108. buf[2 + i*2] = str[i];
  109. buf[3 + i*2] = 0;
  110. }
  111. length = LIMIT(length, size);
  112. udc_control_send(buf, length);
  113. }
  114. else
  115. {
  116. udc_control_stall();
  117. }
  118. }
  119. else
  120. udc_control_stall();
  121. } break;
  122. case USB_CMD(OUT, DEVICE, STANDARD, SET_ADDRESS):
  123. {
  124. udc_control_send_zlp();
  125. udc_set_address(request->wValue);
  126. } break;
  127. case USB_CMD(OUT, DEVICE, STANDARD, SET_CONFIGURATION):
  128. {
  129. usb_config = request->wValue;
  130. udc_control_send_zlp();
  131. if (usb_config)
  132. {
  133. int size = usb_configuration_hierarchy.configuration.wTotalLength;
  134. usb_descriptor_header_t *desc = (usb_descriptor_header_t *)&usb_configuration_hierarchy;
  135. while (size)
  136. {
  137. if (USB_ENDPOINT_DESCRIPTOR == desc->bDescriptorType)
  138. udc_configure_endpoint((usb_endpoint_descriptor_t *)desc);
  139. size -= desc->bLength;
  140. desc = (usb_descriptor_header_t *)((uint8_t *)desc + desc->bLength);
  141. }
  142. usb_configuration_callback(usb_config);
  143. }
  144. } break;
  145. case USB_CMD(IN, DEVICE, STANDARD, GET_CONFIGURATION):
  146. {
  147. uint8_t config = usb_config;
  148. udc_control_send(&config, sizeof(config));
  149. } break;
  150. case USB_CMD(IN, DEVICE, STANDARD, GET_STATUS):
  151. case USB_CMD(IN, INTERFACE, STANDARD, GET_STATUS):
  152. {
  153. uint16_t status = 0;
  154. udc_control_send((uint8_t *)&status, sizeof(status));
  155. } break;
  156. case USB_CMD(IN, ENDPOINT, STANDARD, GET_STATUS):
  157. {
  158. int ep = request->wIndex & USB_INDEX_MASK;
  159. int dir = request->wIndex & USB_DIRECTION_MASK;
  160. uint16_t status = 0;
  161. if (udc_endpoint_configured(ep, dir))
  162. {
  163. status = udc_endpoint_get_status(ep, dir);
  164. udc_control_send((uint8_t *)&status, sizeof(status));
  165. }
  166. else
  167. {
  168. udc_control_stall();
  169. }
  170. } break;
  171. case USB_CMD(OUT, DEVICE, STANDARD, SET_FEATURE):
  172. {
  173. udc_control_stall();
  174. } break;
  175. case USB_CMD(OUT, INTERFACE, STANDARD, SET_FEATURE):
  176. {
  177. udc_control_send_zlp();
  178. } break;
  179. case USB_CMD(OUT, ENDPOINT, STANDARD, SET_FEATURE):
  180. {
  181. int ep = request->wIndex & USB_INDEX_MASK;
  182. int dir = request->wIndex & USB_DIRECTION_MASK;
  183. if (0 == request->wValue && ep && udc_endpoint_configured(ep, dir))
  184. {
  185. udc_endpoint_set_feature(ep, dir);
  186. udc_control_send_zlp();
  187. }
  188. else
  189. {
  190. udc_control_stall();
  191. }
  192. } break;
  193. case USB_CMD(OUT, DEVICE, STANDARD, CLEAR_FEATURE):
  194. {
  195. udc_control_stall();
  196. } break;
  197. case USB_CMD(OUT, INTERFACE, STANDARD, CLEAR_FEATURE):
  198. {
  199. udc_control_send_zlp();
  200. } break;
  201. case USB_CMD(OUT, ENDPOINT, STANDARD, CLEAR_FEATURE):
  202. {
  203. int ep = request->wIndex & USB_INDEX_MASK;
  204. int dir = request->wIndex & USB_DIRECTION_MASK;
  205. if (0 == request->wValue && ep && udc_endpoint_configured(ep, dir))
  206. {
  207. udc_endpoint_clear_feature(ep, dir);
  208. udc_control_send_zlp();
  209. }
  210. else
  211. {
  212. udc_control_stall();
  213. }
  214. } break;
  215. // TODO: This actually belongs to HID implementation
  216. case USB_CMD(IN, INTERFACE, STANDARD, GET_DESCRIPTOR):
  217. {
  218. uint16_t length = request->wLength;
  219. length = LIMIT(length, sizeof(usb_hid_report_descriptor));
  220. udc_control_send((uint8_t *)usb_hid_report_descriptor, length);
  221. } break;
  222. default:
  223. {
  224. udc_control_stall();
  225. } break;
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. void udc_recv_callback(int ep)
  230. {
  231. if (usb_endpoints[ep].out.callback)
  232. usb_endpoints[ep].out.callback();
  233. }
  234. //-----------------------------------------------------------------------------
  235. void udc_send_callback(int ep)
  236. {
  237. if (usb_endpoints[ep].in.callback)
  238. usb_endpoints[ep].in.callback();
  239. }