1 /* 2 * g_dnl.c -- USB Downloader Gadget 3 * 4 * Copyright (C) 2012 Samsung Electronics 5 * Lukasz Majewski <l.majewski@samsung.com> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #include <common.h> 11 #include <malloc.h> 12 13 #include <mmc.h> 14 #include <part.h> 15 16 #include <g_dnl.h> 17 #include <usb_mass_storage.h> 18 #include <dfu.h> 19 #include <thor.h> 20 21 #include "gadget_chips.h" 22 #include "composite.c" 23 24 /* 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 30 */ 31 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 41 42 #define DRIVER_VERSION "usb_dnl 2.0" 43 44 static const char shortname[] = "usb_dnl_"; 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; 48 49 void g_dnl_set_serialnumber(char *s) 50 { 51 memset(g_dnl_serial, 0, MAX_STRING_SERIAL); 52 if (strlen(s) < MAX_STRING_SERIAL) 53 strncpy(g_dnl_serial, s, strlen(s)); 54 } 55 56 static struct usb_device_descriptor device_desc = { 57 .bLength = sizeof device_desc, 58 .bDescriptorType = USB_DT_DEVICE, 59 60 .bcdUSB = __constant_cpu_to_le16(0x0200), 61 .bDeviceClass = USB_CLASS_COMM, 62 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/ 63 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, 69 }; 70 71 /* 72 * static strings, in UTF-8 73 * IDs for those strings are assigned dynamically at g_dnl_bind() 74 */ 75 static struct usb_string g_dnl_string_defs[] = { 76 {.s = manufacturer}, 77 {.s = product}, 78 {.s = g_dnl_serial}, 79 { } /* end of list */ 80 }; 81 82 static struct usb_gadget_strings g_dnl_string_tab = { 83 .language = 0x0409, /* en-us */ 84 .strings = g_dnl_string_defs, 85 }; 86 87 static struct usb_gadget_strings *g_dnl_composite_strings[] = { 88 &g_dnl_string_tab, 89 NULL, 90 }; 91 92 static int g_dnl_unbind(struct usb_composite_dev *cdev) 93 { 94 struct usb_gadget *gadget = cdev->gadget; 95 96 free(cdev->config); 97 cdev->config = NULL; 98 debug("%s: calling usb_gadget_disconnect for " 99 "controller '%s'\n", shortname, gadget->name); 100 usb_gadget_disconnect(gadget); 101 102 return 0; 103 } 104 105 static int g_dnl_do_config(struct usb_configuration *c) 106 { 107 const char *s = c->cdev->driver->name; 108 int ret = -1; 109 110 debug("%s: configuration: 0x%p composite dev: 0x%p\n", 111 __func__, c, c->cdev); 112 113 printf("GADGET DRIVER: %s\n", s); 114 if (!strcmp(s, "usb_dnl_dfu")) 115 ret = dfu_add(c); 116 else if (!strcmp(s, "usb_dnl_ums")) 117 ret = fsg_add(c); 118 else if (!strcmp(s, "usb_dnl_thor")) 119 ret = thor_add(c); 120 121 return ret; 122 } 123 124 static int g_dnl_config_register(struct usb_composite_dev *cdev) 125 { 126 struct usb_configuration *config; 127 const char *name = "usb_dnload"; 128 129 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config)); 130 if (!config) 131 return -ENOMEM; 132 133 memset(config, 0, sizeof(*config)); 134 135 config->label = name; 136 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER; 137 config->bConfigurationValue = CONFIGURATION_NUMBER; 138 config->iConfiguration = STRING_USBDOWN; 139 config->bind = g_dnl_do_config; 140 141 return usb_add_config(cdev, config); 142 } 143 144 __weak 145 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 146 { 147 return 0; 148 } 149 150 static int g_dnl_bind(struct usb_composite_dev *cdev) 151 { 152 struct usb_gadget *gadget = cdev->gadget; 153 int id, ret; 154 int gcnum; 155 156 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); 157 158 id = usb_string_id(cdev); 159 160 if (id < 0) 161 return id; 162 g_dnl_string_defs[0].id = id; 163 device_desc.iManufacturer = id; 164 165 id = usb_string_id(cdev); 166 if (id < 0) 167 return id; 168 169 g_dnl_string_defs[1].id = id; 170 device_desc.iProduct = id; 171 172 id = usb_string_id(cdev); 173 if (id < 0) 174 return id; 175 176 g_dnl_string_defs[2].id = id; 177 device_desc.iSerialNumber = id; 178 179 g_dnl_bind_fixup(&device_desc, cdev->driver->name); 180 ret = g_dnl_config_register(cdev); 181 if (ret) 182 goto error; 183 184 gcnum = usb_gadget_controller_number(gadget); 185 186 debug("gcnum: %d\n", gcnum); 187 if (gcnum >= 0) 188 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); 189 else { 190 debug("%s: controller '%s' not recognized\n", 191 shortname, gadget->name); 192 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); 193 } 194 195 debug("%s: calling usb_gadget_connect for " 196 "controller '%s'\n", shortname, gadget->name); 197 usb_gadget_connect(gadget); 198 199 return 0; 200 201 error: 202 g_dnl_unbind(cdev); 203 return -ENOMEM; 204 } 205 206 static struct usb_composite_driver g_dnl_driver = { 207 .name = NULL, 208 .dev = &device_desc, 209 .strings = g_dnl_composite_strings, 210 211 .bind = g_dnl_bind, 212 .unbind = g_dnl_unbind, 213 }; 214 215 int g_dnl_register(const char *type) 216 { 217 /* The largest function name is 4 */ 218 static char name[sizeof(shortname) + 4]; 219 int ret; 220 221 if (!strcmp(type, "dfu")) { 222 strcpy(name, shortname); 223 strcat(name, type); 224 } else if (!strcmp(type, "ums")) { 225 strcpy(name, shortname); 226 strcat(name, type); 227 } else if (!strcmp(type, "thor")) { 228 strcpy(name, shortname); 229 strcat(name, type); 230 } else { 231 printf("%s: unknown command: %s\n", __func__, type); 232 return -EINVAL; 233 } 234 235 g_dnl_driver.name = name; 236 237 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name); 238 ret = usb_composite_register(&g_dnl_driver); 239 240 if (ret) { 241 printf("%s: failed!, error: %d\n", __func__, ret); 242 return ret; 243 } 244 245 return 0; 246 } 247 248 void g_dnl_unregister(void) 249 { 250 usb_composite_unregister(&g_dnl_driver); 251 } 252