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