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 <errno.h> 11 #include <common.h> 12 #include <malloc.h> 13 14 #include <mmc.h> 15 #include <part.h> 16 17 #include <g_dnl.h> 18 #include "f_dfu.h" 19 20 #include "gadget_chips.h" 21 #include "composite.c" 22 #include "f_mass_storage.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 /* Number of supported configurations */ 37 #define CONFIGURATION_NUMBER 1 38 39 #define DRIVER_VERSION "usb_dnl 2.0" 40 41 static const char shortname[] = "usb_dnl_"; 42 static const char product[] = "USB download gadget"; 43 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER; 44 45 static struct usb_device_descriptor device_desc = { 46 .bLength = sizeof device_desc, 47 .bDescriptorType = USB_DT_DEVICE, 48 49 .bcdUSB = __constant_cpu_to_le16(0x0200), 50 .bDeviceClass = USB_CLASS_COMM, 51 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/ 52 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, 57 }; 58 59 /* 60 * static strings, in UTF-8 61 * IDs for those strings are assigned dynamically at g_dnl_bind() 62 */ 63 static struct usb_string g_dnl_string_defs[] = { 64 {.s = manufacturer}, 65 {.s = product}, 66 { } /* end of list */ 67 }; 68 69 static struct usb_gadget_strings g_dnl_string_tab = { 70 .language = 0x0409, /* en-us */ 71 .strings = g_dnl_string_defs, 72 }; 73 74 static struct usb_gadget_strings *g_dnl_composite_strings[] = { 75 &g_dnl_string_tab, 76 NULL, 77 }; 78 79 static int g_dnl_unbind(struct usb_composite_dev *cdev) 80 { 81 struct usb_gadget *gadget = cdev->gadget; 82 83 debug("%s: calling usb_gadget_disconnect for " 84 "controller '%s'\n", shortname, gadget->name); 85 usb_gadget_disconnect(gadget); 86 87 return 0; 88 } 89 90 static int g_dnl_do_config(struct usb_configuration *c) 91 { 92 const char *s = c->cdev->driver->name; 93 int ret = -1; 94 95 debug("%s: configuration: 0x%p composite dev: 0x%p\n", 96 __func__, c, c->cdev); 97 98 printf("GADGET DRIVER: %s\n", s); 99 if (!strcmp(s, "usb_dnl_dfu")) 100 ret = dfu_add(c); 101 else if (!strcmp(s, "usb_dnl_ums")) 102 ret = fsg_add(c); 103 104 return ret; 105 } 106 107 static int g_dnl_config_register(struct usb_composite_dev *cdev) 108 { 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, 114 115 .bind = g_dnl_do_config, 116 }; 117 118 return usb_add_config(cdev, &config); 119 } 120 121 __weak 122 int g_dnl_bind_fixup(struct usb_device_descriptor *dev) 123 { 124 return 0; 125 } 126 127 static int g_dnl_bind(struct usb_composite_dev *cdev) 128 { 129 struct usb_gadget *gadget = cdev->gadget; 130 int id, ret; 131 int gcnum; 132 133 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); 134 135 id = usb_string_id(cdev); 136 137 if (id < 0) 138 return id; 139 g_dnl_string_defs[0].id = id; 140 device_desc.iManufacturer = id; 141 142 id = usb_string_id(cdev); 143 if (id < 0) 144 return id; 145 146 g_dnl_string_defs[1].id = id; 147 device_desc.iProduct = id; 148 149 g_dnl_bind_fixup(&device_desc); 150 ret = g_dnl_config_register(cdev); 151 if (ret) 152 goto error; 153 154 gcnum = usb_gadget_controller_number(gadget); 155 156 debug("gcnum: %d\n", gcnum); 157 if (gcnum >= 0) 158 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); 159 else { 160 debug("%s: controller '%s' not recognized\n", 161 shortname, gadget->name); 162 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); 163 } 164 165 debug("%s: calling usb_gadget_connect for " 166 "controller '%s'\n", shortname, gadget->name); 167 usb_gadget_connect(gadget); 168 169 return 0; 170 171 error: 172 g_dnl_unbind(cdev); 173 return -ENOMEM; 174 } 175 176 static struct usb_composite_driver g_dnl_driver = { 177 .name = NULL, 178 .dev = &device_desc, 179 .strings = g_dnl_composite_strings, 180 181 .bind = g_dnl_bind, 182 .unbind = g_dnl_unbind, 183 }; 184 185 int g_dnl_register(const char *type) 186 { 187 /* We only allow "dfu" atm, so 3 should be enough */ 188 static char name[sizeof(shortname) + 3]; 189 int ret; 190 191 if (!strcmp(type, "dfu")) { 192 strcpy(name, shortname); 193 strcat(name, type); 194 } else if (!strcmp(type, "ums")) { 195 strcpy(name, shortname); 196 strcat(name, type); 197 } else { 198 printf("%s: unknown command: %s\n", __func__, type); 199 return -EINVAL; 200 } 201 202 g_dnl_driver.name = name; 203 204 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name); 205 ret = usb_composite_register(&g_dnl_driver); 206 207 if (ret) { 208 printf("%s: failed!, error: %d\n", __func__, ret); 209 return ret; 210 } 211 212 return 0; 213 } 214 215 void g_dnl_unregister(void) 216 { 217 usb_composite_unregister(&g_dnl_driver); 218 } 219