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+
18 #include <usb_mass_storage.h>
22 #include "gadget_chips.h"
23 #include "composite.c"
26 * One needs to define the following:
27 * CONFIG_G_DNL_VENDOR_NUM
28 * CONFIG_G_DNL_PRODUCT_NUM
29 * CONFIG_G_DNL_MANUFACTURER
30 * at e.g. ./include/configs/<board>.h
33 #define STRING_MANUFACTURER 25
34 #define STRING_PRODUCT 2
35 /* Index of String Descriptor describing this configuration */
36 #define STRING_USBDOWN 2
37 /* Index of String serial */
38 #define STRING_SERIAL 3
39 #define MAX_STRING_SERIAL 32
40 /* Number of supported configurations */
41 #define CONFIGURATION_NUMBER 1
43 #define DRIVER_VERSION "usb_dnl 2.0"
45 static const char product[] = "USB download gadget";
46 static char g_dnl_serial[MAX_STRING_SERIAL];
47 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
49 void g_dnl_set_serialnumber(char *s)
51 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
52 if (strlen(s) < MAX_STRING_SERIAL)
53 strncpy(g_dnl_serial, s, strlen(s));
56 static struct usb_device_descriptor device_desc = {
57 .bLength = sizeof device_desc,
58 .bDescriptorType = USB_DT_DEVICE,
60 .bcdUSB = __constant_cpu_to_le16(0x0200),
61 .bDeviceClass = USB_CLASS_COMM,
62 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
64 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
65 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
66 .iProduct = STRING_PRODUCT,
67 .iSerialNumber = STRING_SERIAL,
68 .bNumConfigurations = 1,
72 * static strings, in UTF-8
73 * IDs for those strings are assigned dynamically at g_dnl_bind()
75 static struct usb_string g_dnl_string_defs[] = {
82 static struct usb_gadget_strings g_dnl_string_tab = {
83 .language = 0x0409, /* en-us */
84 .strings = g_dnl_string_defs,
87 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
92 static int g_dnl_unbind(struct usb_composite_dev *cdev)
94 struct usb_gadget *gadget = cdev->gadget;
96 debug("%s: calling usb_gadget_disconnect for "
97 "controller '%s'\n", __func__, gadget->name);
98 usb_gadget_disconnect(gadget);
103 static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
105 return ll_entry_start(struct g_dnl_bind_callback,
106 g_dnl_bind_callbacks);
109 static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
111 return ll_entry_end(struct g_dnl_bind_callback,
112 g_dnl_bind_callbacks);
115 static int g_dnl_do_config(struct usb_configuration *c)
117 const char *s = c->cdev->driver->name;
118 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
120 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
121 __func__, c, c->cdev);
123 for (; callback != g_dnl_bind_callback_end(); callback++)
124 if (!strcmp(s, callback->usb_function_name))
125 return callback->fptr(c);
129 static int g_dnl_config_register(struct usb_composite_dev *cdev)
131 struct usb_configuration *config;
132 const char *name = "usb_dnload";
134 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
138 memset(config, 0, sizeof(*config));
140 config->label = name;
141 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
142 config->bConfigurationValue = CONFIGURATION_NUMBER;
143 config->iConfiguration = STRING_USBDOWN;
144 config->bind = g_dnl_do_config;
146 return usb_add_config(cdev, config);
150 int board_usb_init(int index, enum usb_init_type init)
156 int board_usb_cleanup(int index, enum usb_init_type init)
162 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
167 __weak int g_dnl_get_board_bcd_device_number(int gcnum)
172 __weak int g_dnl_board_usb_cable_connected(void)
177 static bool g_dnl_detach_request;
179 bool g_dnl_detach(void)
181 return g_dnl_detach_request;
184 void g_dnl_trigger_detach(void)
186 g_dnl_detach_request = true;
189 void g_dnl_clear_detach(void)
191 g_dnl_detach_request = false;
194 static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
196 struct usb_gadget *gadget = cdev->gadget;
199 gcnum = usb_gadget_controller_number(gadget);
203 return g_dnl_get_board_bcd_device_number(gcnum);
206 static int g_dnl_bind(struct usb_composite_dev *cdev)
208 struct usb_gadget *gadget = cdev->gadget;
212 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
214 id = usb_string_id(cdev);
218 g_dnl_string_defs[0].id = id;
219 device_desc.iManufacturer = id;
221 id = usb_string_id(cdev);
225 g_dnl_string_defs[1].id = id;
226 device_desc.iProduct = id;
228 id = usb_string_id(cdev);
232 g_dnl_string_defs[2].id = id;
233 device_desc.iSerialNumber = id;
235 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
236 ret = g_dnl_config_register(cdev);
240 gcnum = g_dnl_get_bcd_device_number(cdev);
242 device_desc.bcdDevice = cpu_to_le16(gcnum);
244 debug("%s: controller '%s' not recognized\n",
245 __func__, gadget->name);
246 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
249 debug("%s: calling usb_gadget_connect for "
250 "controller '%s'\n", __func__, gadget->name);
251 usb_gadget_connect(gadget);
260 static struct usb_composite_driver g_dnl_driver = {
263 .strings = g_dnl_composite_strings,
266 .unbind = g_dnl_unbind,
271 * Registering via USB function name won't be necessary after rewriting
272 * g_dnl to support multiple USB functions.
274 int g_dnl_register(const char *name)
278 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
279 g_dnl_driver.name = name;
281 ret = usb_composite_register(&g_dnl_driver);
283 printf("%s: failed!, error: %d\n", __func__, ret);
289 void g_dnl_unregister(void)
291 usb_composite_unregister(&g_dnl_driver);