g_dnl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * g_dnl.c -- USB Downloader Gadget
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Lukasz Majewski <l.majewski@samsung.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <mmc.h>
  12. #include <part.h>
  13. #include <usb.h>
  14. #include <g_dnl.h>
  15. #include <usb_mass_storage.h>
  16. #include <dfu.h>
  17. #include <thor.h>
  18. #include "gadget_chips.h"
  19. #include "composite.c"
  20. /*
  21. * One needs to define the following:
  22. * CONFIG_G_DNL_VENDOR_NUM
  23. * CONFIG_G_DNL_PRODUCT_NUM
  24. * CONFIG_G_DNL_MANUFACTURER
  25. * at e.g. ./include/configs/<board>.h
  26. */
  27. #define STRING_MANUFACTURER 25
  28. #define STRING_PRODUCT 2
  29. /* Index of String Descriptor describing this configuration */
  30. #define STRING_USBDOWN 2
  31. /* Index of String serial */
  32. #define STRING_SERIAL 3
  33. #define MAX_STRING_SERIAL 32
  34. /* Number of supported configurations */
  35. #define CONFIGURATION_NUMBER 1
  36. #define DRIVER_VERSION "usb_dnl 2.0"
  37. static const char product[] = "USB download gadget";
  38. static char g_dnl_serial[MAX_STRING_SERIAL];
  39. static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
  40. void g_dnl_set_serialnumber(char *s)
  41. {
  42. memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
  43. if (strlen(s) < MAX_STRING_SERIAL)
  44. strncpy(g_dnl_serial, s, strlen(s));
  45. }
  46. static struct usb_device_descriptor device_desc = {
  47. .bLength = sizeof device_desc,
  48. .bDescriptorType = USB_DT_DEVICE,
  49. .bcdUSB = __constant_cpu_to_le16(0x0200),
  50. .bDeviceClass = USB_CLASS_COMM,
  51. .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
  52. .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
  53. .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
  54. .iProduct = STRING_PRODUCT,
  55. .iSerialNumber = STRING_SERIAL,
  56. .bNumConfigurations = 1,
  57. };
  58. /*
  59. * static strings, in UTF-8
  60. * IDs for those strings are assigned dynamically at g_dnl_bind()
  61. */
  62. static struct usb_string g_dnl_string_defs[] = {
  63. {.s = manufacturer},
  64. {.s = product},
  65. {.s = g_dnl_serial},
  66. { } /* end of list */
  67. };
  68. static struct usb_gadget_strings g_dnl_string_tab = {
  69. .language = 0x0409, /* en-us */
  70. .strings = g_dnl_string_defs,
  71. };
  72. static struct usb_gadget_strings *g_dnl_composite_strings[] = {
  73. &g_dnl_string_tab,
  74. NULL,
  75. };
  76. static int g_dnl_unbind(struct usb_composite_dev *cdev)
  77. {
  78. struct usb_gadget *gadget = cdev->gadget;
  79. debug("%s: calling usb_gadget_disconnect for "
  80. "controller '%s'\n", __func__, gadget->name);
  81. usb_gadget_disconnect(gadget);
  82. return 0;
  83. }
  84. static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
  85. {
  86. return ll_entry_start(struct g_dnl_bind_callback,
  87. g_dnl_bind_callbacks);
  88. }
  89. static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
  90. {
  91. return ll_entry_end(struct g_dnl_bind_callback,
  92. g_dnl_bind_callbacks);
  93. }
  94. static int g_dnl_do_config(struct usb_configuration *c)
  95. {
  96. const char *s = c->cdev->driver->name;
  97. struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
  98. debug("%s: configuration: 0x%p composite dev: 0x%p\n",
  99. __func__, c, c->cdev);
  100. for (; callback != g_dnl_bind_callback_end(); callback++)
  101. if (!strcmp(s, callback->usb_function_name))
  102. return callback->fptr(c);
  103. return -ENODEV;
  104. }
  105. static int g_dnl_config_register(struct usb_composite_dev *cdev)
  106. {
  107. struct usb_configuration *config;
  108. const char *name = "usb_dnload";
  109. config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
  110. if (!config)
  111. return -ENOMEM;
  112. memset(config, 0, sizeof(*config));
  113. config->label = name;
  114. config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
  115. config->bConfigurationValue = CONFIGURATION_NUMBER;
  116. config->iConfiguration = STRING_USBDOWN;
  117. config->bind = g_dnl_do_config;
  118. return usb_add_config(cdev, config);
  119. }
  120. __weak
  121. int board_usb_init(int index, enum usb_init_type init)
  122. {
  123. return 0;
  124. }
  125. __weak
  126. int board_usb_cleanup(int index, enum usb_init_type init)
  127. {
  128. return 0;
  129. }
  130. __weak
  131. int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
  132. {
  133. return 0;
  134. }
  135. __weak int g_dnl_get_board_bcd_device_number(int gcnum)
  136. {
  137. return gcnum;
  138. }
  139. __weak int g_dnl_board_usb_cable_connected(void)
  140. {
  141. return -EOPNOTSUPP;
  142. }
  143. static bool g_dnl_detach_request;
  144. bool g_dnl_detach(void)
  145. {
  146. return g_dnl_detach_request;
  147. }
  148. void g_dnl_trigger_detach(void)
  149. {
  150. g_dnl_detach_request = true;
  151. }
  152. void g_dnl_clear_detach(void)
  153. {
  154. g_dnl_detach_request = false;
  155. }
  156. static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
  157. {
  158. struct usb_gadget *gadget = cdev->gadget;
  159. int gcnum;
  160. gcnum = usb_gadget_controller_number(gadget);
  161. if (gcnum > 0)
  162. gcnum += 0x200;
  163. return g_dnl_get_board_bcd_device_number(gcnum);
  164. }
  165. static int g_dnl_bind(struct usb_composite_dev *cdev)
  166. {
  167. struct usb_gadget *gadget = cdev->gadget;
  168. int id, ret;
  169. int gcnum;
  170. debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
  171. id = usb_string_id(cdev);
  172. if (id < 0)
  173. return id;
  174. g_dnl_string_defs[0].id = id;
  175. device_desc.iManufacturer = id;
  176. id = usb_string_id(cdev);
  177. if (id < 0)
  178. return id;
  179. g_dnl_string_defs[1].id = id;
  180. device_desc.iProduct = id;
  181. id = usb_string_id(cdev);
  182. if (id < 0)
  183. return id;
  184. g_dnl_string_defs[2].id = id;
  185. device_desc.iSerialNumber = id;
  186. g_dnl_bind_fixup(&device_desc, cdev->driver->name);
  187. ret = g_dnl_config_register(cdev);
  188. if (ret)
  189. goto error;
  190. gcnum = g_dnl_get_bcd_device_number(cdev);
  191. if (gcnum >= 0)
  192. device_desc.bcdDevice = cpu_to_le16(gcnum);
  193. else {
  194. debug("%s: controller '%s' not recognized\n",
  195. __func__, gadget->name);
  196. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  197. }
  198. debug("%s: calling usb_gadget_connect for "
  199. "controller '%s'\n", __func__, gadget->name);
  200. usb_gadget_connect(gadget);
  201. return 0;
  202. error:
  203. g_dnl_unbind(cdev);
  204. return -ENOMEM;
  205. }
  206. static struct usb_composite_driver g_dnl_driver = {
  207. .name = NULL,
  208. .dev = &device_desc,
  209. .strings = g_dnl_composite_strings,
  210. .bind = g_dnl_bind,
  211. .unbind = g_dnl_unbind,
  212. };
  213. /*
  214. * NOTICE:
  215. * Registering via USB function name won't be necessary after rewriting
  216. * g_dnl to support multiple USB functions.
  217. */
  218. int g_dnl_register(const char *name)
  219. {
  220. int ret;
  221. debug("%s: g_dnl_driver.name = %s\n", __func__, name);
  222. g_dnl_driver.name = name;
  223. ret = usb_composite_register(&g_dnl_driver);
  224. if (ret) {
  225. printf("%s: failed!, error: %d\n", __func__, ret);
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. void g_dnl_unregister(void)
  231. {
  232. usb_composite_unregister(&g_dnl_driver);
  233. }