2 * g_dnl.c -- USB Downloader Gadget
4 * Copyright (C) 2012 Samsung Electronics
5 * Lukasz Majewski <l.majewski@samsung.com>
7 * SPDX-License-Identifier: GPL-2.0+
20 #include "gadget_chips.h"
21 #include "composite.c"
22 #include "f_mass_storage.c"
25 * One needs to define the following:
26 * CONFIG_G_DNL_VENDOR_NUM
27 * CONFIG_G_DNL_PRODUCT_NUM
28 * CONFIG_G_DNL_MANUFACTURER
29 * at e.g. ./include/configs/<board>.h
32 #define STRING_MANUFACTURER 25
33 #define STRING_PRODUCT 2
34 /* Index of String Descriptor describing this configuration */
35 #define STRING_USBDOWN 2
36 /* Number of supported configurations */
37 #define CONFIGURATION_NUMBER 1
39 #define DRIVER_VERSION "usb_dnl 2.0"
41 static const char shortname[] = "usb_dnl_";
42 static const char product[] = "USB download gadget";
43 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,
49 .bcdUSB = __constant_cpu_to_le16(0x0200),
50 .bDeviceClass = USB_CLASS_COMM,
51 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
53 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
54 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
55 .iProduct = STRING_PRODUCT,
56 .bNumConfigurations = 1,
59 /* static strings, in UTF-8 */
60 static struct usb_string g_dnl_string_defs[] = {
66 static struct usb_gadget_strings g_dnl_string_tab = {
67 .language = 0x0409, /* en-us */
68 .strings = g_dnl_string_defs,
71 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
76 static int g_dnl_unbind(struct usb_composite_dev *cdev)
78 struct usb_gadget *gadget = cdev->gadget;
80 debug("%s: calling usb_gadget_disconnect for "
81 "controller '%s'\n", shortname, gadget->name);
82 usb_gadget_disconnect(gadget);
87 static int g_dnl_do_config(struct usb_configuration *c)
89 const char *s = c->cdev->driver->name;
92 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
93 __func__, c, c->cdev);
95 printf("GADGET DRIVER: %s\n", s);
96 if (!strcmp(s, "usb_dnl_dfu"))
98 else if (!strcmp(s, "usb_dnl_ums"))
104 static int g_dnl_config_register(struct usb_composite_dev *cdev)
106 static struct usb_configuration config = {
107 .label = "usb_dnload",
108 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
109 .bConfigurationValue = CONFIGURATION_NUMBER,
110 .iConfiguration = STRING_USBDOWN,
112 .bind = g_dnl_do_config,
115 return usb_add_config(cdev, &config);
119 int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
124 static int g_dnl_bind(struct usb_composite_dev *cdev)
126 struct usb_gadget *gadget = cdev->gadget;
130 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
132 id = usb_string_id(cdev);
136 g_dnl_string_defs[0].id = id;
137 device_desc.iManufacturer = id;
139 id = usb_string_id(cdev);
143 g_dnl_string_defs[1].id = id;
144 device_desc.iProduct = id;
146 g_dnl_bind_fixup(&device_desc);
147 ret = g_dnl_config_register(cdev);
151 gcnum = usb_gadget_controller_number(gadget);
153 debug("gcnum: %d\n", gcnum);
155 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
157 debug("%s: controller '%s' not recognized\n",
158 shortname, gadget->name);
159 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
162 debug("%s: calling usb_gadget_connect for "
163 "controller '%s'\n", shortname, gadget->name);
164 usb_gadget_connect(gadget);
173 static struct usb_composite_driver g_dnl_driver = {
176 .strings = g_dnl_composite_strings,
179 .unbind = g_dnl_unbind,
182 int g_dnl_register(const char *type)
184 /* We only allow "dfu" atm, so 3 should be enough */
185 static char name[sizeof(shortname) + 3];
188 if (!strcmp(type, "dfu")) {
189 strcpy(name, shortname);
191 } else if (!strcmp(type, "ums")) {
192 strcpy(name, shortname);
195 printf("%s: unknown command: %s\n", __func__, type);
199 g_dnl_driver.name = name;
201 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
202 ret = usb_composite_register(&g_dnl_driver);
205 printf("%s: failed!, error: %d\n", __func__, ret);
212 void g_dnl_unregister(void)
214 usb_composite_unregister(&g_dnl_driver);