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 /* Index of String serial */
37 #define STRING_SERIAL 3
38 #define MAX_STRING_SERIAL 32
39 /* Number of supported configurations */
40 #define CONFIGURATION_NUMBER 1
42 #define DRIVER_VERSION "usb_dnl 2.0"
44 static const char product[] = "USB download gadget";
45 static char g_dnl_serial[MAX_STRING_SERIAL];
46 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
48 void g_dnl_set_serialnumber(char *s)
50 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
51 if (strlen(s) < MAX_STRING_SERIAL)
52 strncpy(g_dnl_serial, s, strlen(s));
55 static struct usb_device_descriptor device_desc = {
56 .bLength = sizeof device_desc,
57 .bDescriptorType = USB_DT_DEVICE,
59 .bcdUSB = __constant_cpu_to_le16(0x0200),
60 .bDeviceClass = USB_CLASS_COMM,
61 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
63 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
64 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
65 .iProduct = STRING_PRODUCT,
66 .iSerialNumber = STRING_SERIAL,
67 .bNumConfigurations = 1,
71 * static strings, in UTF-8
72 * IDs for those strings are assigned dynamically at g_dnl_bind()
74 static struct usb_string g_dnl_string_defs[] = {
81 static struct usb_gadget_strings g_dnl_string_tab = {
82 .language = 0x0409, /* en-us */
83 .strings = g_dnl_string_defs,
86 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
91 static int g_dnl_unbind(struct usb_composite_dev *cdev)
93 struct usb_gadget *gadget = cdev->gadget;
97 debug("%s: calling usb_gadget_disconnect for "
98 "controller '%s'\n", __func__, gadget->name);
99 usb_gadget_disconnect(gadget);
104 static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
106 return ll_entry_start(struct g_dnl_bind_callback,
107 g_dnl_bind_callbacks);
110 static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
112 return ll_entry_end(struct g_dnl_bind_callback,
113 g_dnl_bind_callbacks);
116 static int g_dnl_do_config(struct usb_configuration *c)
118 const char *s = c->cdev->driver->name;
119 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
121 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
122 __func__, c, c->cdev);
124 for (; callback != g_dnl_bind_callback_end(); callback++)
125 if (!strcmp(s, callback->usb_function_name))
126 return callback->fptr(c);
130 static int g_dnl_config_register(struct usb_composite_dev *cdev)
132 struct usb_configuration *config;
133 const char *name = "usb_dnload";
135 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
139 memset(config, 0, sizeof(*config));
141 config->label = name;
142 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
143 config->bConfigurationValue = CONFIGURATION_NUMBER;
144 config->iConfiguration = STRING_USBDOWN;
145 config->bind = g_dnl_do_config;
147 return usb_add_config(cdev, config);
151 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
156 __weak int g_dnl_get_board_bcd_device_number(int gcnum)
161 __weak int g_dnl_board_usb_cable_connected(void)
166 static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
168 struct usb_gadget *gadget = cdev->gadget;
171 gcnum = usb_gadget_controller_number(gadget);
175 return g_dnl_get_board_bcd_device_number(gcnum);
178 static int g_dnl_bind(struct usb_composite_dev *cdev)
180 struct usb_gadget *gadget = cdev->gadget;
184 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
186 id = usb_string_id(cdev);
190 g_dnl_string_defs[0].id = id;
191 device_desc.iManufacturer = id;
193 id = usb_string_id(cdev);
197 g_dnl_string_defs[1].id = id;
198 device_desc.iProduct = id;
200 id = usb_string_id(cdev);
204 g_dnl_string_defs[2].id = id;
205 device_desc.iSerialNumber = id;
207 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
208 ret = g_dnl_config_register(cdev);
212 gcnum = g_dnl_get_bcd_device_number(cdev);
214 device_desc.bcdDevice = cpu_to_le16(gcnum);
216 debug("%s: controller '%s' not recognized\n",
217 __func__, gadget->name);
218 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
221 debug("%s: calling usb_gadget_connect for "
222 "controller '%s'\n", __func__, gadget->name);
223 usb_gadget_connect(gadget);
232 static struct usb_composite_driver g_dnl_driver = {
235 .strings = g_dnl_composite_strings,
238 .unbind = g_dnl_unbind,
243 * Registering via USB function name won't be necessary after rewriting
244 * g_dnl to support multiple USB functions.
246 int g_dnl_register(const char *name)
250 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
251 g_dnl_driver.name = name;
253 ret = usb_composite_register(&g_dnl_driver);
255 printf("%s: failed!, error: %d\n", __func__, ret);
261 void g_dnl_unregister(void)
263 usb_composite_unregister(&g_dnl_driver);