g_dnl.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /*
  31. * One needs to define the following:
  32. * CONFIG_G_DNL_VENDOR_NUM
  33. * CONFIG_G_DNL_PRODUCT_NUM
  34. * CONFIG_G_DNL_MANUFACTURER
  35. * at e.g. ./include/configs/<board>.h
  36. */
  37. #define STRING_MANUFACTURER 25
  38. #define STRING_PRODUCT 2
  39. #define STRING_USBDOWN 2
  40. #define CONFIG_USBDOWNLOADER 2
  41. #define DRIVER_VERSION "usb_dnl 2.0"
  42. static const char shortname[] = "usb_dnl_";
  43. static const char product[] = "USB download gadget";
  44. static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
  45. static struct usb_device_descriptor device_desc = {
  46. .bLength = sizeof device_desc,
  47. .bDescriptorType = USB_DT_DEVICE,
  48. .bcdUSB = __constant_cpu_to_le16(0x0200),
  49. .bDeviceClass = USB_CLASS_COMM,
  50. .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
  51. .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
  52. .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
  53. .iProduct = STRING_PRODUCT,
  54. .bNumConfigurations = 1,
  55. };
  56. /* static strings, in UTF-8 */
  57. static struct usb_string g_dnl_string_defs[] = {
  58. { 0, manufacturer, },
  59. { 1, product, },
  60. { } /* end of list */
  61. };
  62. static struct usb_gadget_strings g_dnl_string_tab = {
  63. .language = 0x0409, /* en-us */
  64. .strings = g_dnl_string_defs,
  65. };
  66. static struct usb_gadget_strings *g_dnl_composite_strings[] = {
  67. &g_dnl_string_tab,
  68. NULL,
  69. };
  70. static int g_dnl_unbind(struct usb_composite_dev *cdev)
  71. {
  72. struct usb_gadget *gadget = cdev->gadget;
  73. debug("%s: calling usb_gadget_disconnect for "
  74. "controller '%s'\n", shortname, gadget->name);
  75. usb_gadget_disconnect(gadget);
  76. return 0;
  77. }
  78. static int g_dnl_do_config(struct usb_configuration *c)
  79. {
  80. const char *s = c->cdev->driver->name;
  81. int ret = -1;
  82. debug("%s: configuration: 0x%p composite dev: 0x%p\n",
  83. __func__, c, c->cdev);
  84. printf("GADGET DRIVER: %s\n", s);
  85. if (!strcmp(s, "usb_dnl_dfu"))
  86. ret = dfu_add(c);
  87. return ret;
  88. }
  89. static int g_dnl_config_register(struct usb_composite_dev *cdev)
  90. {
  91. static struct usb_configuration config = {
  92. .label = "usb_dnload",
  93. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  94. .bConfigurationValue = CONFIG_USBDOWNLOADER,
  95. .iConfiguration = STRING_USBDOWN,
  96. .bind = g_dnl_do_config,
  97. };
  98. return usb_add_config(cdev, &config);
  99. }
  100. static int g_dnl_bind(struct usb_composite_dev *cdev)
  101. {
  102. struct usb_gadget *gadget = cdev->gadget;
  103. int id, ret;
  104. int gcnum;
  105. debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
  106. id = usb_string_id(cdev);
  107. if (id < 0)
  108. return id;
  109. g_dnl_string_defs[0].id = id;
  110. device_desc.iManufacturer = id;
  111. id = usb_string_id(cdev);
  112. if (id < 0)
  113. return id;
  114. g_dnl_string_defs[1].id = id;
  115. device_desc.iProduct = id;
  116. ret = g_dnl_config_register(cdev);
  117. if (ret)
  118. goto error;
  119. gcnum = usb_gadget_controller_number(gadget);
  120. debug("gcnum: %d\n", gcnum);
  121. if (gcnum >= 0)
  122. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  123. else {
  124. debug("%s: controller '%s' not recognized\n",
  125. shortname, gadget->name);
  126. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  127. }
  128. debug("%s: calling usb_gadget_connect for "
  129. "controller '%s'\n", shortname, gadget->name);
  130. usb_gadget_connect(gadget);
  131. return 0;
  132. error:
  133. g_dnl_unbind(cdev);
  134. return -ENOMEM;
  135. }
  136. static struct usb_composite_driver g_dnl_driver = {
  137. .name = NULL,
  138. .dev = &device_desc,
  139. .strings = g_dnl_composite_strings,
  140. .bind = g_dnl_bind,
  141. .unbind = g_dnl_unbind,
  142. };
  143. int g_dnl_register(const char *type)
  144. {
  145. /* We only allow "dfu" atm, so 3 should be enough */
  146. static char name[sizeof(shortname) + 3];
  147. int ret;
  148. if (!strcmp(type, "dfu")) {
  149. strcpy(name, shortname);
  150. strcat(name, type);
  151. } else {
  152. printf("%s: unknown command: %s\n", __func__, type);
  153. return -EINVAL;
  154. }
  155. g_dnl_driver.name = name;
  156. debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
  157. ret = usb_composite_register(&g_dnl_driver);
  158. if (ret) {
  159. printf("%s: failed!, error: %d\n", __func__, ret);
  160. return ret;
  161. }
  162. return 0;
  163. }
  164. void g_dnl_unregister(void)
  165. {
  166. usb_composite_unregister(&g_dnl_driver);
  167. }