g_dnl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * g_dnl.c -- USB Downloader Gadget
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Lukasz Majewski <l.majewski@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <errno.h>
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <mmc.h>
  25. #include <part.h>
  26. #include <g_dnl.h>
  27. #include "f_dfu.h"
  28. #include "gadget_chips.h"
  29. #include "composite.c"
  30. #include "f_mass_storage.c"
  31. /*
  32. * One needs to define the following:
  33. * CONFIG_G_DNL_VENDOR_NUM
  34. * CONFIG_G_DNL_PRODUCT_NUM
  35. * CONFIG_G_DNL_MANUFACTURER
  36. * at e.g. ./include/configs/<board>.h
  37. */
  38. #define STRING_MANUFACTURER 25
  39. #define STRING_PRODUCT 2
  40. #define STRING_USBDOWN 2
  41. #define CONFIG_USBDOWNLOADER 2
  42. #define DRIVER_VERSION "usb_dnl 2.0"
  43. static const char shortname[] = "usb_dnl_";
  44. static const char product[] = "USB download gadget";
  45. static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
  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. .bNumConfigurations = 1,
  56. };
  57. /* static strings, in UTF-8 */
  58. static struct usb_string g_dnl_string_defs[] = {
  59. { 0, manufacturer, },
  60. { 1, product, },
  61. { } /* end of list */
  62. };
  63. static struct usb_gadget_strings g_dnl_string_tab = {
  64. .language = 0x0409, /* en-us */
  65. .strings = g_dnl_string_defs,
  66. };
  67. static struct usb_gadget_strings *g_dnl_composite_strings[] = {
  68. &g_dnl_string_tab,
  69. NULL,
  70. };
  71. static int g_dnl_unbind(struct usb_composite_dev *cdev)
  72. {
  73. struct usb_gadget *gadget = cdev->gadget;
  74. debug("%s: calling usb_gadget_disconnect for "
  75. "controller '%s'\n", shortname, gadget->name);
  76. usb_gadget_disconnect(gadget);
  77. return 0;
  78. }
  79. static int g_dnl_do_config(struct usb_configuration *c)
  80. {
  81. const char *s = c->cdev->driver->name;
  82. int ret = -1;
  83. debug("%s: configuration: 0x%p composite dev: 0x%p\n",
  84. __func__, c, c->cdev);
  85. printf("GADGET DRIVER: %s\n", s);
  86. if (!strcmp(s, "usb_dnl_dfu"))
  87. ret = dfu_add(c);
  88. else if (!strcmp(s, "usb_dnl_ums"))
  89. ret = fsg_add(c);
  90. return ret;
  91. }
  92. static int g_dnl_config_register(struct usb_composite_dev *cdev)
  93. {
  94. static struct usb_configuration config = {
  95. .label = "usb_dnload",
  96. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  97. .bConfigurationValue = CONFIG_USBDOWNLOADER,
  98. .iConfiguration = STRING_USBDOWN,
  99. .bind = g_dnl_do_config,
  100. };
  101. return usb_add_config(cdev, &config);
  102. }
  103. static int g_dnl_bind(struct usb_composite_dev *cdev)
  104. {
  105. struct usb_gadget *gadget = cdev->gadget;
  106. int id, ret;
  107. int gcnum;
  108. debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
  109. id = usb_string_id(cdev);
  110. if (id < 0)
  111. return id;
  112. g_dnl_string_defs[0].id = id;
  113. device_desc.iManufacturer = id;
  114. id = usb_string_id(cdev);
  115. if (id < 0)
  116. return id;
  117. g_dnl_string_defs[1].id = id;
  118. device_desc.iProduct = id;
  119. ret = g_dnl_config_register(cdev);
  120. if (ret)
  121. goto error;
  122. gcnum = usb_gadget_controller_number(gadget);
  123. debug("gcnum: %d\n", gcnum);
  124. if (gcnum >= 0)
  125. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  126. else {
  127. debug("%s: controller '%s' not recognized\n",
  128. shortname, gadget->name);
  129. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  130. }
  131. debug("%s: calling usb_gadget_connect for "
  132. "controller '%s'\n", shortname, gadget->name);
  133. usb_gadget_connect(gadget);
  134. return 0;
  135. error:
  136. g_dnl_unbind(cdev);
  137. return -ENOMEM;
  138. }
  139. static struct usb_composite_driver g_dnl_driver = {
  140. .name = NULL,
  141. .dev = &device_desc,
  142. .strings = g_dnl_composite_strings,
  143. .bind = g_dnl_bind,
  144. .unbind = g_dnl_unbind,
  145. };
  146. int g_dnl_register(const char *type)
  147. {
  148. /* We only allow "dfu" atm, so 3 should be enough */
  149. static char name[sizeof(shortname) + 3];
  150. int ret;
  151. if (!strcmp(type, "dfu")) {
  152. strcpy(name, shortname);
  153. strcat(name, type);
  154. } else if (!strcmp(type, "ums")) {
  155. strcpy(name, shortname);
  156. strcat(name, type);
  157. } else {
  158. printf("%s: unknown command: %s\n", __func__, type);
  159. return -EINVAL;
  160. }
  161. g_dnl_driver.name = name;
  162. debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
  163. ret = usb_composite_register(&g_dnl_driver);
  164. if (ret) {
  165. printf("%s: failed!, error: %d\n", __func__, ret);
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. void g_dnl_unregister(void)
  171. {
  172. usb_composite_unregister(&g_dnl_driver);
  173. }