g_dnl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. };
  61. static struct usb_gadget_strings g_dnl_string_tab = {
  62. .language = 0x0409, /* en-us */
  63. .strings = g_dnl_string_defs,
  64. };
  65. static struct usb_gadget_strings *g_dnl_composite_strings[] = {
  66. &g_dnl_string_tab,
  67. NULL,
  68. };
  69. static int g_dnl_unbind(struct usb_composite_dev *cdev)
  70. {
  71. debug("%s\n", __func__);
  72. return 0;
  73. }
  74. static int g_dnl_do_config(struct usb_configuration *c)
  75. {
  76. const char *s = c->cdev->driver->name;
  77. int ret = -1;
  78. debug("%s: configuration: 0x%p composite dev: 0x%p\n",
  79. __func__, c, c->cdev);
  80. printf("GADGET DRIVER: %s\n", s);
  81. if (!strcmp(s, "usb_dnl_dfu"))
  82. ret = dfu_add(c);
  83. return ret;
  84. }
  85. static int g_dnl_config_register(struct usb_composite_dev *cdev)
  86. {
  87. static struct usb_configuration config = {
  88. .label = "usb_dnload",
  89. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  90. .bConfigurationValue = CONFIG_USBDOWNLOADER,
  91. .iConfiguration = STRING_USBDOWN,
  92. .bind = g_dnl_do_config,
  93. };
  94. return usb_add_config(cdev, &config);
  95. }
  96. static int g_dnl_bind(struct usb_composite_dev *cdev)
  97. {
  98. struct usb_gadget *gadget = cdev->gadget;
  99. int id, ret;
  100. int gcnum;
  101. debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
  102. id = usb_string_id(cdev);
  103. if (id < 0)
  104. return id;
  105. g_dnl_string_defs[0].id = id;
  106. device_desc.iManufacturer = id;
  107. id = usb_string_id(cdev);
  108. if (id < 0)
  109. return id;
  110. g_dnl_string_defs[1].id = id;
  111. device_desc.iProduct = id;
  112. ret = g_dnl_config_register(cdev);
  113. if (ret)
  114. goto error;
  115. gcnum = usb_gadget_controller_number(gadget);
  116. debug("gcnum: %d\n", gcnum);
  117. if (gcnum >= 0)
  118. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  119. else {
  120. debug("%s: controller '%s' not recognized\n",
  121. shortname, gadget->name);
  122. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  123. }
  124. return 0;
  125. error:
  126. g_dnl_unbind(cdev);
  127. return -ENOMEM;
  128. }
  129. static struct usb_composite_driver g_dnl_driver = {
  130. .name = NULL,
  131. .dev = &device_desc,
  132. .strings = g_dnl_composite_strings,
  133. .bind = g_dnl_bind,
  134. .unbind = g_dnl_unbind,
  135. };
  136. int g_dnl_register(const char *type)
  137. {
  138. /* We only allow "dfu" atm, so 3 should be enough */
  139. static char name[sizeof(shortname) + 3];
  140. int ret;
  141. if (!strcmp(type, "dfu")) {
  142. strcpy(name, shortname);
  143. strcat(name, type);
  144. } else {
  145. printf("%s: unknown command: %s\n", __func__, type);
  146. return -EINVAL;
  147. }
  148. g_dnl_driver.name = name;
  149. debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
  150. ret = usb_composite_register(&g_dnl_driver);
  151. if (ret) {
  152. printf("%s: failed!, error: %d\n", __func__, ret);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. void g_dnl_unregister(void)
  158. {
  159. usb_composite_unregister(&g_dnl_driver);
  160. }