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+
17 #include <usb_mass_storage.h>
21 #include "gadget_chips.h"
22 #include "composite.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,
60 * static strings, in UTF-8
61 * IDs for those strings are assigned dynamically at g_dnl_bind()
63 static struct usb_string g_dnl_string_defs[] = {
69 static struct usb_gadget_strings g_dnl_string_tab = {
70 .language = 0x0409, /* en-us */
71 .strings = g_dnl_string_defs,
74 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
79 static int g_dnl_unbind(struct usb_composite_dev *cdev)
81 struct usb_gadget *gadget = cdev->gadget;
85 debug("%s: calling usb_gadget_disconnect for "
86 "controller '%s'\n", shortname, gadget->name);
87 usb_gadget_disconnect(gadget);
92 static int g_dnl_do_config(struct usb_configuration *c)
94 const char *s = c->cdev->driver->name;
97 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
98 __func__, c, c->cdev);
100 printf("GADGET DRIVER: %s\n", s);
101 if (!strcmp(s, "usb_dnl_dfu"))
103 else if (!strcmp(s, "usb_dnl_ums"))
105 else if (!strcmp(s, "usb_dnl_thor"))
111 static int g_dnl_config_register(struct usb_composite_dev *cdev)
113 struct usb_configuration *config;
114 const char *name = "usb_dnload";
116 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
120 memset(config, 0, sizeof(*config));
122 config->label = name;
123 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
124 config->bConfigurationValue = CONFIGURATION_NUMBER;
125 config->iConfiguration = STRING_USBDOWN;
126 config->bind = g_dnl_do_config;
128 return usb_add_config(cdev, config);
132 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
137 static int g_dnl_bind(struct usb_composite_dev *cdev)
139 struct usb_gadget *gadget = cdev->gadget;
143 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
145 id = usb_string_id(cdev);
149 g_dnl_string_defs[0].id = id;
150 device_desc.iManufacturer = id;
152 id = usb_string_id(cdev);
156 g_dnl_string_defs[1].id = id;
157 device_desc.iProduct = id;
159 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
160 ret = g_dnl_config_register(cdev);
164 gcnum = usb_gadget_controller_number(gadget);
166 debug("gcnum: %d\n", gcnum);
168 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
170 debug("%s: controller '%s' not recognized\n",
171 shortname, gadget->name);
172 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
175 debug("%s: calling usb_gadget_connect for "
176 "controller '%s'\n", shortname, gadget->name);
177 usb_gadget_connect(gadget);
186 static struct usb_composite_driver g_dnl_driver = {
189 .strings = g_dnl_composite_strings,
192 .unbind = g_dnl_unbind,
195 int g_dnl_register(const char *type)
197 /* The largest function name is 4 */
198 static char name[sizeof(shortname) + 4];
201 if (!strcmp(type, "dfu")) {
202 strcpy(name, shortname);
204 } else if (!strcmp(type, "ums")) {
205 strcpy(name, shortname);
207 } else if (!strcmp(type, "thor")) {
208 strcpy(name, shortname);
211 printf("%s: unknown command: %s\n", __func__, type);
215 g_dnl_driver.name = name;
217 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
218 ret = usb_composite_register(&g_dnl_driver);
221 printf("%s: failed!, error: %d\n", __func__, ret);
228 void g_dnl_unregister(void)
230 usb_composite_unregister(&g_dnl_driver);