usb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <err.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <dev/usbc.h>
  28. #include <dev/usb.h>
  29. #define LOCAL_TRACE 0
  30. #define MAX_STRINGS 8
  31. static usb_string strings[MAX_STRINGS];
  32. static usb_config *config;
  33. static uint8_t active_config;
  34. static bool usb_active = false;
  35. static void append_desc_data(usb_descriptor *desc, const void *dat, size_t len)
  36. {
  37. uint8_t *ptr = malloc(desc->len + len);
  38. memcpy(ptr, desc->desc, desc->len);
  39. memcpy(ptr + desc->len, dat, len);
  40. free(desc->desc);
  41. desc->desc = ptr;
  42. desc->len += len;
  43. }
  44. /* returns the interface number assigned */
  45. static int usb_append_interface(usb_descriptor *desc, const uint8_t *int_descr, size_t len)
  46. {
  47. uint8_t *ptr = malloc(len);
  48. int interface_num;
  49. // create a temporary copy of the interface
  50. memcpy(ptr, int_descr, len);
  51. // find the last interface used
  52. interface_num = ((uint8_t *)desc->desc)[4]; // current interface
  53. // patch our interface descriptor with the new id
  54. ptr[2] = interface_num;
  55. // append it to our config desriptor
  56. append_desc_data(desc, ptr, len);
  57. free(ptr);
  58. // patch the total length of the config descriptor and set the number of interfaces
  59. ((uint16_t *)desc->desc)[1] += len;
  60. interface_num++;
  61. ((uint8_t *)desc->desc)[4] = interface_num;
  62. return interface_num - 1;
  63. }
  64. int usb_append_interface_highspeed(const uint8_t *int_descr, size_t len)
  65. {
  66. return usb_append_interface(&config->highspeed.config, int_descr, len);
  67. }
  68. int usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len)
  69. {
  70. return usb_append_interface(&config->lowspeed.config, int_descr, len);
  71. }
  72. void usb_set_string_descriptor(usb_descriptor *desc, const char *string)
  73. {
  74. int len = strlen(string);
  75. ushort *data;
  76. int datalen = len * 2 + 2;
  77. data = malloc(datalen);
  78. /* write length field */
  79. data[0] = 0x0300 + datalen;
  80. /* copy the string into the uint16_t based usb string */
  81. int i;
  82. for (i = 0; i < len; i++) {
  83. data[i + 1] = string[i];
  84. }
  85. desc->desc = (void *)data;
  86. desc->len = datalen;
  87. }
  88. static void set_usb_id(uint16_t vendor, uint16_t product)
  89. {
  90. // patch the current configuration to with the vendor/product id
  91. ((uint16_t *)config->lowspeed.device.desc)[4] = vendor;
  92. ((uint16_t *)config->lowspeed.device.desc)[5] = product;
  93. ((uint16_t *)config->highspeed.device.desc)[4] = vendor;
  94. ((uint16_t *)config->highspeed.device.desc)[5] = product;
  95. }
  96. void usb_add_string(const char *string, uint8_t id)
  97. {
  98. uint i;
  99. size_t len = strlen(string);
  100. uint16_t *strbuf = malloc(len * 2 + 2);
  101. /* build the usb string descriptor */
  102. strbuf[0] = 0x300 | (len * 2 + 2);
  103. for (i = 0; i < len; i++) {
  104. strbuf[i + 1] = (uint16_t)string[i];
  105. }
  106. /* find a slot to put it */
  107. for (i = 0; i < MAX_STRINGS; i++) {
  108. if (strings[i].id == 0) {
  109. strings[i].string.desc = strbuf;
  110. strings[i].string.len = len * 2 + 2;
  111. strings[i].id = id;
  112. break;
  113. }
  114. }
  115. }
  116. static int default_usb_callback(usbc_callback_op_t op, const union usb_callback_args *args)
  117. {
  118. LTRACEF("op %d, args %p\n", op, args);
  119. /* start looking for specific things to handle */
  120. if (op == CB_SETUP_MSG) {
  121. const struct usb_setup *setup = args->setup;
  122. DEBUG_ASSERT(setup);
  123. LTRACEF("SETUP: req_type=%#x req=%#x value=%#x index=%#x len=%#x\n", setup->request_type, setup->request, setup->value, setup->index, setup->length);
  124. if ((setup->request_type & TYPE_MASK) == TYPE_STANDARD) {
  125. switch (setup->request) {
  126. case SET_ADDRESS:
  127. LTRACEF("SET_ADDRESS 0x%x\n", setup->value);
  128. usbc_ep0_ack();
  129. break;
  130. case SET_FEATURE:
  131. case CLEAR_FEATURE:
  132. // OTAY
  133. LTRACEF("SET/CLEAR_FEATURE, feature 0x%x\n", setup->value);
  134. usbc_ep0_ack();
  135. break;
  136. case SET_DESCRIPTOR:
  137. LTRACEF("SET_DESCRIPTOR\n");
  138. usbc_ep0_stall();
  139. break;
  140. case GET_DESCRIPTOR: {
  141. /* Get the right descriptors based on current speed */
  142. const struct usb_descriptor_speed *speed;
  143. if (usbc_is_highspeed()) {
  144. speed = &config->highspeed;
  145. } else {
  146. speed = &config->lowspeed;
  147. }
  148. if ((setup->request_type & RECIP_MASK) == RECIP_DEVICE) {
  149. switch (setup->value) {
  150. case 0x100: /* device */
  151. LTRACEF("got GET_DESCRIPTOR, device descriptor\n");
  152. usbc_ep0_send(speed->device.desc, speed->device.len,
  153. setup->length);
  154. break;
  155. case 0x200: /* CONFIGURATION */
  156. LTRACEF("got GET_DESCRIPTOR, config descriptor\n");
  157. usbc_ep0_send(speed->config.desc, speed->config.len,
  158. setup->length);
  159. break;
  160. case 0x300: /* Language ID */
  161. LTRACEF("got GET_DESCRIPTOR, language id\n");
  162. usbc_ep0_send(config->langid.desc,
  163. config->langid.len, setup->length);
  164. break;
  165. case (0x301)...(0x3ff): {
  166. /* string descriptor, search our list for a match */
  167. uint i;
  168. bool found = false;
  169. uint8_t id = setup->value & 0xff;
  170. for (i = 0; i < MAX_STRINGS; i++) {
  171. if (strings[i].id == id) {
  172. usbc_ep0_send(strings[i].string.desc,
  173. strings[i].string.len,
  174. setup->length);
  175. found = true;
  176. break;
  177. }
  178. }
  179. if (!found) {
  180. /* couldn't find one, stall */
  181. usbc_ep0_stall();
  182. }
  183. break;
  184. }
  185. case 0x600: /* DEVICE QUALIFIER */
  186. LTRACEF("got GET_DESCRIPTOR, device qualifier\n");
  187. usbc_ep0_send(speed->device_qual.desc,
  188. speed->device_qual.len, setup->length);
  189. break;
  190. case 0xa00:
  191. /* we aint got one of these */
  192. LTRACEF("got GET_DESCRIPTOR, debug descriptor\n");
  193. usbc_ep0_stall();
  194. break;
  195. default:
  196. LTRACEF("unhandled descriptor %#x\n", setup->value);
  197. // stall
  198. break;
  199. }
  200. } else {
  201. // interface/endpoint descriptors? let someone else handle it
  202. // STALL
  203. }
  204. break;
  205. }
  206. case SET_CONFIGURATION:
  207. LTRACEF("SET_CONFIGURATION %d\n", setup->value);
  208. active_config = setup->value;
  209. usbc_ep0_ack();
  210. break;
  211. case GET_CONFIGURATION:
  212. LTRACEF("GET_CONFIGURATION\n");
  213. usbc_ep0_send(&active_config, 1, setup->length);
  214. break;
  215. case SET_INTERFACE:
  216. LTRACEF("SET_INTERFACE %d\n", setup->value);
  217. usbc_ep0_ack();
  218. break;
  219. case GET_INTERFACE: {
  220. static uint8_t i = 1;
  221. LTRACEF("GET_INTERFACE\n");
  222. usbc_ep0_send(&i, 1, setup->length);
  223. break;
  224. }
  225. case GET_STATUS: {
  226. static uint16_t i = 1; // self powered
  227. LTRACEF("GET_STATUS\n");
  228. usbc_ep0_send(&i, 2, setup->length);
  229. break;
  230. }
  231. default:
  232. LTRACEF("unhandled standard request 0x%x\n", setup->request);
  233. }
  234. }
  235. }
  236. return 0;
  237. }
  238. void usb_setup(usb_config *_config)
  239. {
  240. ASSERT(_config);
  241. config = _config;
  242. ASSERT(usb_active == false);
  243. // set the default usb control callback handler
  244. usbc_set_callback(&default_usb_callback);
  245. }
  246. void usb_start(void)
  247. {
  248. ASSERT(config);
  249. ASSERT(usb_active == false);
  250. // go online
  251. usbc_set_active(true);
  252. usb_active = true;
  253. }
  254. void usb_stop(void)
  255. {
  256. ASSERT(usb_active == true);
  257. usb_active = false;
  258. usbc_set_active(false);
  259. }
  260. void usb_init(void)
  261. {
  262. }