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,
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;
83 debug("%s: calling usb_gadget_disconnect for "
84 "controller '%s'\n", shortname, gadget->name);
85 usb_gadget_disconnect(gadget);
90 static int g_dnl_do_config(struct usb_configuration *c)
92 const char *s = c->cdev->driver->name;
95 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
96 __func__, c, c->cdev);
98 printf("GADGET DRIVER: %s\n", s);
99 if (!strcmp(s, "usb_dnl_dfu"))
101 else if (!strcmp(s, "usb_dnl_ums"))
107 static int g_dnl_config_register(struct usb_composite_dev *cdev)
109 static struct usb_configuration config = {
110 .label = "usb_dnload",
111 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
112 .bConfigurationValue = CONFIGURATION_NUMBER,
113 .iConfiguration = STRING_USBDOWN,
115 .bind = g_dnl_do_config,
118 return usb_add_config(cdev, &config);
122 int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
127 static int g_dnl_bind(struct usb_composite_dev *cdev)
129 struct usb_gadget *gadget = cdev->gadget;
133 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
135 id = usb_string_id(cdev);
139 g_dnl_string_defs[0].id = id;
140 device_desc.iManufacturer = id;
142 id = usb_string_id(cdev);
146 g_dnl_string_defs[1].id = id;
147 device_desc.iProduct = id;
149 g_dnl_bind_fixup(&device_desc);
150 ret = g_dnl_config_register(cdev);
154 gcnum = usb_gadget_controller_number(gadget);
156 debug("gcnum: %d\n", gcnum);
158 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
160 debug("%s: controller '%s' not recognized\n",
161 shortname, gadget->name);
162 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
165 debug("%s: calling usb_gadget_connect for "
166 "controller '%s'\n", shortname, gadget->name);
167 usb_gadget_connect(gadget);
176 static struct usb_composite_driver g_dnl_driver = {
179 .strings = g_dnl_composite_strings,
182 .unbind = g_dnl_unbind,
185 int g_dnl_register(const char *type)
187 /* We only allow "dfu" atm, so 3 should be enough */
188 static char name[sizeof(shortname) + 3];
191 if (!strcmp(type, "dfu")) {
192 strcpy(name, shortname);
194 } else if (!strcmp(type, "ums")) {
195 strcpy(name, shortname);
198 printf("%s: unknown command: %s\n", __func__, type);
202 g_dnl_driver.name = name;
204 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
205 ret = usb_composite_register(&g_dnl_driver);
208 printf("%s: failed!, error: %d\n", __func__, ret);
215 void g_dnl_unregister(void)
217 usb_composite_unregister(&g_dnl_driver);