Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * g_dnl.c -- USB Downloader Gadget |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * Lukasz Majewski <l.majewski@samsung.com> |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <malloc.h> |
| 12 | |
| 13 | #include <mmc.h> |
| 14 | #include <part.h> |
| 15 | |
| 16 | #include <g_dnl.h> |
Lukasz Majewski | 0c63523 | 2013-09-17 15:58:21 +0200 | [diff] [blame] | 17 | #include <usb_mass_storage.h> |
Lukasz Majewski | c421946 | 2013-09-17 15:58:23 +0200 | [diff] [blame] | 18 | #include <dfu.h> |
Lukasz Majewski | 1c1af4f | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 19 | #include <thor.h> |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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 |
Lukasz Majewski | 0d9752e | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 34 | /* Index of String Descriptor describing this configuration */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 35 | #define STRING_USBDOWN 2 |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 36 | /* Index of String serial */ |
| 37 | #define STRING_SERIAL 3 |
| 38 | #define MAX_STRING_SERIAL 32 |
Lukasz Majewski | 0d9752e | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 39 | /* Number of supported configurations */ |
| 40 | #define CONFIGURATION_NUMBER 1 |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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"; |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 46 | static char g_dnl_serial[MAX_STRING_SERIAL]; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 47 | static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER; |
| 48 | |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 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 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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, |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 67 | .iSerialNumber = STRING_SERIAL, |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 68 | .bNumConfigurations = 1, |
| 69 | }; |
| 70 | |
Lukasz Majewski | dbbaef7 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 71 | /* |
| 72 | * static strings, in UTF-8 |
| 73 | * IDs for those strings are assigned dynamically at g_dnl_bind() |
| 74 | */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 75 | static struct usb_string g_dnl_string_defs[] = { |
Lukasz Majewski | dbbaef7 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 76 | {.s = manufacturer}, |
| 77 | {.s = product}, |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 78 | {.s = g_dnl_serial}, |
Lukasz Majewski | dbbaef7 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 79 | { } /* end of list */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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 | { |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 94 | struct usb_gadget *gadget = cdev->gadget; |
| 95 | |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 96 | free(cdev->config); |
| 97 | cdev->config = NULL; |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 98 | debug("%s: calling usb_gadget_disconnect for " |
| 99 | "controller '%s'\n", shortname, gadget->name); |
| 100 | usb_gadget_disconnect(gadget); |
| 101 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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); |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 116 | else if (!strcmp(s, "usb_dnl_ums")) |
| 117 | ret = fsg_add(c); |
Lukasz Majewski | 1c1af4f | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 118 | else if (!strcmp(s, "usb_dnl_thor")) |
| 119 | ret = thor_add(c); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 125 | { |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 126 | struct usb_configuration *config; |
| 127 | const char *name = "usb_dnload"; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 128 | |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 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; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 140 | |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 141 | return usb_add_config(cdev, config); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Heiko Schocher | 07a12ea | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 144 | __weak |
Lukasz Majewski | 44b5b38 | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 145 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) |
Heiko Schocher | 07a12ea | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 150 | __weak int g_dnl_get_board_bcd_device_number(int gcnum) |
| 151 | { |
| 152 | return gcnum; |
| 153 | } |
| 154 | |
| 155 | static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev) |
| 156 | { |
| 157 | struct usb_gadget *gadget = cdev->gadget; |
| 158 | int gcnum; |
| 159 | |
| 160 | gcnum = usb_gadget_controller_number(gadget); |
| 161 | if (gcnum > 0) |
| 162 | gcnum += 0x200; |
| 163 | |
| 164 | return g_dnl_get_board_bcd_device_number(gcnum); |
| 165 | } |
| 166 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 167 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 168 | { |
| 169 | struct usb_gadget *gadget = cdev->gadget; |
| 170 | int id, ret; |
| 171 | int gcnum; |
| 172 | |
| 173 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 174 | |
| 175 | id = usb_string_id(cdev); |
| 176 | |
| 177 | if (id < 0) |
| 178 | return id; |
| 179 | g_dnl_string_defs[0].id = id; |
| 180 | device_desc.iManufacturer = id; |
| 181 | |
| 182 | id = usb_string_id(cdev); |
| 183 | if (id < 0) |
| 184 | return id; |
| 185 | |
| 186 | g_dnl_string_defs[1].id = id; |
| 187 | device_desc.iProduct = id; |
| 188 | |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 189 | id = usb_string_id(cdev); |
| 190 | if (id < 0) |
| 191 | return id; |
| 192 | |
| 193 | g_dnl_string_defs[2].id = id; |
| 194 | device_desc.iSerialNumber = id; |
| 195 | |
Lukasz Majewski | 44b5b38 | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 196 | g_dnl_bind_fixup(&device_desc, cdev->driver->name); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 197 | ret = g_dnl_config_register(cdev); |
| 198 | if (ret) |
| 199 | goto error; |
| 200 | |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 201 | gcnum = g_dnl_get_bcd_device_number(cdev); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 202 | if (gcnum >= 0) |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 203 | device_desc.bcdDevice = cpu_to_le16(gcnum); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 204 | else { |
| 205 | debug("%s: controller '%s' not recognized\n", |
| 206 | shortname, gadget->name); |
| 207 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 208 | } |
| 209 | |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 210 | debug("%s: calling usb_gadget_connect for " |
| 211 | "controller '%s'\n", shortname, gadget->name); |
| 212 | usb_gadget_connect(gadget); |
| 213 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 214 | return 0; |
| 215 | |
| 216 | error: |
| 217 | g_dnl_unbind(cdev); |
| 218 | return -ENOMEM; |
| 219 | } |
| 220 | |
| 221 | static struct usb_composite_driver g_dnl_driver = { |
| 222 | .name = NULL, |
| 223 | .dev = &device_desc, |
| 224 | .strings = g_dnl_composite_strings, |
| 225 | |
| 226 | .bind = g_dnl_bind, |
| 227 | .unbind = g_dnl_unbind, |
| 228 | }; |
| 229 | |
| 230 | int g_dnl_register(const char *type) |
| 231 | { |
Lukasz Majewski | 1c1af4f | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 232 | /* The largest function name is 4 */ |
| 233 | static char name[sizeof(shortname) + 4]; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 234 | int ret; |
| 235 | |
| 236 | if (!strcmp(type, "dfu")) { |
| 237 | strcpy(name, shortname); |
| 238 | strcat(name, type); |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 239 | } else if (!strcmp(type, "ums")) { |
| 240 | strcpy(name, shortname); |
| 241 | strcat(name, type); |
Lukasz Majewski | 1c1af4f | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 242 | } else if (!strcmp(type, "thor")) { |
| 243 | strcpy(name, shortname); |
| 244 | strcat(name, type); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 245 | } else { |
| 246 | printf("%s: unknown command: %s\n", __func__, type); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | g_dnl_driver.name = name; |
| 251 | |
| 252 | debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name); |
| 253 | ret = usb_composite_register(&g_dnl_driver); |
| 254 | |
| 255 | if (ret) { |
| 256 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void g_dnl_unregister(void) |
| 264 | { |
| 265 | usb_composite_unregister(&g_dnl_driver); |
| 266 | } |