Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 2 | /* |
| 3 | * g_dnl.c -- USB Downloader Gadget |
| 4 | * |
| 5 | * Copyright (C) 2012 Samsung Electronics |
| 6 | * Lukasz Majewski <l.majewski@samsung.com> |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | |
| 13 | #include <mmc.h> |
| 14 | #include <part.h> |
Paul Kocialkowski | 99fc222 | 2015-06-12 19:57:01 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 16 | |
| 17 | #include <g_dnl.h> |
Lukasz Majewski | ba4e95c | 2013-09-17 15:58:21 +0200 | [diff] [blame] | 18 | #include <usb_mass_storage.h> |
Lukasz Majewski | a6921ad | 2013-09-17 15:58:23 +0200 | [diff] [blame] | 19 | #include <dfu.h> |
Lukasz Majewski | b958fb9 | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 20 | #include <thor.h> |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 21 | |
Sam Protsenko | de4e4ed | 2017-09-01 15:42:03 +0300 | [diff] [blame] | 22 | #include <env_callback.h> |
| 23 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 24 | #include "gadget_chips.h" |
| 25 | #include "composite.c" |
| 26 | |
| 27 | /* |
| 28 | * One needs to define the following: |
Maxime Ripard | a95aee6 | 2017-09-07 08:58:08 +0200 | [diff] [blame] | 29 | * CONFIG_USB_GADGET_VENDOR_NUM |
| 30 | * CONFIG_USB_GADGET_PRODUCT_NUM |
| 31 | * CONFIG_USB_GADGET_MANUFACTURER |
Sam Protsenko | e6c0bc0 | 2016-04-13 14:20:30 +0300 | [diff] [blame] | 32 | * at e.g. ./configs/<board>_defconfig |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #define STRING_MANUFACTURER 25 |
| 36 | #define STRING_PRODUCT 2 |
Lukasz Majewski | cfc2d0d | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 37 | /* Index of String Descriptor describing this configuration */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 38 | #define STRING_USBDOWN 2 |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 39 | /* Index of String serial */ |
| 40 | #define STRING_SERIAL 3 |
Felipe Balbi | 12d0b8f | 2017-02-22 17:12:39 +0200 | [diff] [blame] | 41 | #define MAX_STRING_SERIAL 256 |
Lukasz Majewski | cfc2d0d | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 42 | /* Number of supported configurations */ |
| 43 | #define CONFIGURATION_NUMBER 1 |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 44 | |
| 45 | #define DRIVER_VERSION "usb_dnl 2.0" |
| 46 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 47 | static const char product[] = "USB download gadget"; |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 48 | static char g_dnl_serial[MAX_STRING_SERIAL]; |
Maxime Ripard | a95aee6 | 2017-09-07 08:58:08 +0200 | [diff] [blame] | 49 | static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 50 | |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 51 | void g_dnl_set_serialnumber(char *s) |
| 52 | { |
| 53 | memset(g_dnl_serial, 0, MAX_STRING_SERIAL); |
Felipe Balbi | 949bf79 | 2017-02-22 11:22:37 +0200 | [diff] [blame] | 54 | strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1); |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 57 | static struct usb_device_descriptor device_desc = { |
| 58 | .bLength = sizeof device_desc, |
| 59 | .bDescriptorType = USB_DT_DEVICE, |
| 60 | |
| 61 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
John Tobias | 5b71840 | 2016-04-07 16:49:03 -0700 | [diff] [blame] | 62 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 63 | .bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 64 | |
Maxime Ripard | a95aee6 | 2017-09-07 08:58:08 +0200 | [diff] [blame] | 65 | .idVendor = __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM), |
| 66 | .idProduct = __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM), |
Felipe Balbi | 207835b | 2017-02-22 17:12:41 +0200 | [diff] [blame] | 67 | /* .iProduct = DYNAMIC */ |
| 68 | /* .iSerialNumber = DYNAMIC */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 69 | .bNumConfigurations = 1, |
| 70 | }; |
| 71 | |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 72 | /* |
| 73 | * static strings, in UTF-8 |
| 74 | * IDs for those strings are assigned dynamically at g_dnl_bind() |
| 75 | */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 76 | static struct usb_string g_dnl_string_defs[] = { |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 77 | {.s = manufacturer}, |
| 78 | {.s = product}, |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 79 | {.s = g_dnl_serial}, |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 80 | { } /* end of list */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static struct usb_gadget_strings g_dnl_string_tab = { |
| 84 | .language = 0x0409, /* en-us */ |
| 85 | .strings = g_dnl_string_defs, |
| 86 | }; |
| 87 | |
| 88 | static struct usb_gadget_strings *g_dnl_composite_strings[] = { |
| 89 | &g_dnl_string_tab, |
| 90 | NULL, |
| 91 | }; |
| 92 | |
Patrick Delaunay | bcd677f | 2020-03-18 09:24:46 +0100 | [diff] [blame] | 93 | void g_dnl_set_product(const char *s) |
| 94 | { |
| 95 | if (s) |
| 96 | g_dnl_string_defs[1].s = s; |
| 97 | else |
| 98 | g_dnl_string_defs[1].s = product; |
| 99 | } |
| 100 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 101 | static int g_dnl_unbind(struct usb_composite_dev *cdev) |
| 102 | { |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 103 | struct usb_gadget *gadget = cdev->gadget; |
| 104 | |
| 105 | debug("%s: calling usb_gadget_disconnect for " |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 106 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 107 | usb_gadget_disconnect(gadget); |
| 108 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 112 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void) |
| 113 | { |
| 114 | return ll_entry_start(struct g_dnl_bind_callback, |
| 115 | g_dnl_bind_callbacks); |
| 116 | } |
| 117 | |
| 118 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void) |
| 119 | { |
| 120 | return ll_entry_end(struct g_dnl_bind_callback, |
| 121 | g_dnl_bind_callbacks); |
| 122 | } |
| 123 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 124 | static int g_dnl_do_config(struct usb_configuration *c) |
| 125 | { |
| 126 | const char *s = c->cdev->driver->name; |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 127 | struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first(); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 128 | |
| 129 | debug("%s: configuration: 0x%p composite dev: 0x%p\n", |
| 130 | __func__, c, c->cdev); |
| 131 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 132 | for (; callback != g_dnl_bind_callback_end(); callback++) |
| 133 | if (!strcmp(s, callback->usb_function_name)) |
| 134 | return callback->fptr(c); |
| 135 | return -ENODEV; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 139 | { |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 140 | struct usb_configuration *config; |
| 141 | const char *name = "usb_dnload"; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 142 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 143 | config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config)); |
| 144 | if (!config) |
| 145 | return -ENOMEM; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 146 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 147 | memset(config, 0, sizeof(*config)); |
| 148 | |
| 149 | config->label = name; |
| 150 | config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER; |
| 151 | config->bConfigurationValue = CONFIGURATION_NUMBER; |
| 152 | config->iConfiguration = STRING_USBDOWN; |
| 153 | config->bind = g_dnl_do_config; |
| 154 | |
| 155 | return usb_add_config(cdev, config); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 158 | __weak |
Paul Kocialkowski | 99fc222 | 2015-06-12 19:57:01 +0200 | [diff] [blame] | 159 | int board_usb_init(int index, enum usb_init_type init) |
| 160 | { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | __weak |
| 165 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 166 | { |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | __weak |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 171 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 172 | { |
| 173 | return 0; |
| 174 | } |
| 175 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 176 | __weak int g_dnl_get_board_bcd_device_number(int gcnum) |
| 177 | { |
| 178 | return gcnum; |
| 179 | } |
| 180 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 181 | __weak int g_dnl_board_usb_cable_connected(void) |
| 182 | { |
| 183 | return -EOPNOTSUPP; |
| 184 | } |
| 185 | |
Rob Herring | fe1b28c | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 186 | static bool g_dnl_detach_request; |
| 187 | |
| 188 | bool g_dnl_detach(void) |
| 189 | { |
| 190 | return g_dnl_detach_request; |
| 191 | } |
| 192 | |
| 193 | void g_dnl_trigger_detach(void) |
| 194 | { |
| 195 | g_dnl_detach_request = true; |
| 196 | } |
| 197 | |
| 198 | void g_dnl_clear_detach(void) |
| 199 | { |
| 200 | g_dnl_detach_request = false; |
| 201 | } |
| 202 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 203 | static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev) |
| 204 | { |
| 205 | struct usb_gadget *gadget = cdev->gadget; |
| 206 | int gcnum; |
| 207 | |
| 208 | gcnum = usb_gadget_controller_number(gadget); |
| 209 | if (gcnum > 0) |
| 210 | gcnum += 0x200; |
| 211 | |
| 212 | return g_dnl_get_board_bcd_device_number(gcnum); |
| 213 | } |
| 214 | |
Sam Protsenko | de4e4ed | 2017-09-01 15:42:03 +0300 | [diff] [blame] | 215 | /** |
| 216 | * Update internal serial number variable when the "serial#" env var changes. |
| 217 | * |
| 218 | * Handle all cases, even when flags == H_PROGRAMMATIC or op == env_op_delete. |
| 219 | */ |
| 220 | static int on_serialno(const char *name, const char *value, enum env_op op, |
| 221 | int flags) |
| 222 | { |
| 223 | g_dnl_set_serialnumber((char *)value); |
| 224 | return 0; |
| 225 | } |
| 226 | U_BOOT_ENV_CALLBACK(serialno, on_serialno); |
| 227 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 228 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 229 | { |
| 230 | struct usb_gadget *gadget = cdev->gadget; |
| 231 | int id, ret; |
| 232 | int gcnum; |
| 233 | |
| 234 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 235 | |
| 236 | id = usb_string_id(cdev); |
| 237 | |
| 238 | if (id < 0) |
| 239 | return id; |
| 240 | g_dnl_string_defs[0].id = id; |
| 241 | device_desc.iManufacturer = id; |
| 242 | |
| 243 | id = usb_string_id(cdev); |
| 244 | if (id < 0) |
| 245 | return id; |
| 246 | |
| 247 | g_dnl_string_defs[1].id = id; |
| 248 | device_desc.iProduct = id; |
| 249 | |
Lukasz Majewski | 2a0583e | 2017-06-26 13:04:58 +0200 | [diff] [blame] | 250 | g_dnl_bind_fixup(&device_desc, cdev->driver->name); |
| 251 | |
Felipe Balbi | 842778a | 2017-02-22 17:12:40 +0200 | [diff] [blame] | 252 | if (strlen(g_dnl_serial)) { |
| 253 | id = usb_string_id(cdev); |
| 254 | if (id < 0) |
| 255 | return id; |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 256 | |
Felipe Balbi | 842778a | 2017-02-22 17:12:40 +0200 | [diff] [blame] | 257 | g_dnl_string_defs[2].id = id; |
| 258 | device_desc.iSerialNumber = id; |
| 259 | } |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 260 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 261 | ret = g_dnl_config_register(cdev); |
| 262 | if (ret) |
| 263 | goto error; |
| 264 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 265 | gcnum = g_dnl_get_bcd_device_number(cdev); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 266 | if (gcnum >= 0) |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 267 | device_desc.bcdDevice = cpu_to_le16(gcnum); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 268 | else { |
| 269 | debug("%s: controller '%s' not recognized\n", |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 270 | __func__, gadget->name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 271 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 272 | } |
| 273 | |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 274 | debug("%s: calling usb_gadget_connect for " |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 275 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 276 | usb_gadget_connect(gadget); |
| 277 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | |
| 280 | error: |
| 281 | g_dnl_unbind(cdev); |
| 282 | return -ENOMEM; |
| 283 | } |
| 284 | |
| 285 | static struct usb_composite_driver g_dnl_driver = { |
| 286 | .name = NULL, |
| 287 | .dev = &device_desc, |
| 288 | .strings = g_dnl_composite_strings, |
| 289 | |
| 290 | .bind = g_dnl_bind, |
| 291 | .unbind = g_dnl_unbind, |
| 292 | }; |
| 293 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 294 | /* |
| 295 | * NOTICE: |
| 296 | * Registering via USB function name won't be necessary after rewriting |
| 297 | * g_dnl to support multiple USB functions. |
| 298 | */ |
| 299 | int g_dnl_register(const char *name) |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 300 | { |
Stephen Warren | 25fbf96 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 301 | int ret; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 302 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 303 | debug("%s: g_dnl_driver.name = %s\n", __func__, name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 304 | g_dnl_driver.name = name; |
| 305 | |
Stephen Warren | 25fbf96 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 306 | ret = usb_composite_register(&g_dnl_driver); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 307 | if (ret) { |
| 308 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 309 | return ret; |
| 310 | } |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | void g_dnl_unregister(void) |
| 315 | { |
| 316 | usb_composite_unregister(&g_dnl_driver); |
| 317 | } |