Lukasz Majewski | 1d4a0b6 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Lukasz Majewski | 1d4a0b6 | 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> |
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 | |
| 22 | #include "gadget_chips.h" |
| 23 | #include "composite.c" |
| 24 | |
| 25 | /* |
| 26 | * One needs to define the following: |
| 27 | * CONFIG_G_DNL_VENDOR_NUM |
| 28 | * CONFIG_G_DNL_PRODUCT_NUM |
| 29 | * CONFIG_G_DNL_MANUFACTURER |
| 30 | * at e.g. ./include/configs/<board>.h |
| 31 | */ |
| 32 | |
| 33 | #define STRING_MANUFACTURER 25 |
| 34 | #define STRING_PRODUCT 2 |
Lukasz Majewski | cfc2d0d | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 35 | /* Index of String Descriptor describing this configuration */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 36 | #define STRING_USBDOWN 2 |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 37 | /* Index of String serial */ |
| 38 | #define STRING_SERIAL 3 |
| 39 | #define MAX_STRING_SERIAL 32 |
Lukasz Majewski | cfc2d0d | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 40 | /* Number of supported configurations */ |
| 41 | #define CONFIGURATION_NUMBER 1 |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 42 | |
| 43 | #define DRIVER_VERSION "usb_dnl 2.0" |
| 44 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 45 | static const char product[] = "USB download gadget"; |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 46 | static char g_dnl_serial[MAX_STRING_SERIAL]; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 47 | static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER; |
| 48 | |
Heiko Schocher | ec9002e | 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 | 1d4a0b6 | 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 | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 67 | .iSerialNumber = STRING_SERIAL, |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 68 | .bNumConfigurations = 1, |
| 69 | }; |
| 70 | |
Lukasz Majewski | c4219a8 | 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 | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 75 | static struct usb_string g_dnl_string_defs[] = { |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 76 | {.s = manufacturer}, |
| 77 | {.s = product}, |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 78 | {.s = g_dnl_serial}, |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 79 | { } /* end of list */ |
Lukasz Majewski | 1d4a0b6 | 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 | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 94 | struct usb_gadget *gadget = cdev->gadget; |
| 95 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 96 | free(cdev->config); |
| 97 | cdev->config = NULL; |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 98 | debug("%s: calling usb_gadget_disconnect for " |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 99 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 100 | usb_gadget_disconnect(gadget); |
| 101 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 105 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void) |
| 106 | { |
| 107 | return ll_entry_start(struct g_dnl_bind_callback, |
| 108 | g_dnl_bind_callbacks); |
| 109 | } |
| 110 | |
| 111 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void) |
| 112 | { |
| 113 | return ll_entry_end(struct g_dnl_bind_callback, |
| 114 | g_dnl_bind_callbacks); |
| 115 | } |
| 116 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 117 | static int g_dnl_do_config(struct usb_configuration *c) |
| 118 | { |
| 119 | const char *s = c->cdev->driver->name; |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 120 | struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first(); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 121 | |
| 122 | debug("%s: configuration: 0x%p composite dev: 0x%p\n", |
| 123 | __func__, c, c->cdev); |
| 124 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 125 | for (; callback != g_dnl_bind_callback_end(); callback++) |
| 126 | if (!strcmp(s, callback->usb_function_name)) |
| 127 | return callback->fptr(c); |
| 128 | return -ENODEV; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 132 | { |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 133 | struct usb_configuration *config; |
| 134 | const char *name = "usb_dnload"; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 135 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 136 | config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config)); |
| 137 | if (!config) |
| 138 | return -ENOMEM; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 139 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 140 | memset(config, 0, sizeof(*config)); |
| 141 | |
| 142 | config->label = name; |
| 143 | config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER; |
| 144 | config->bConfigurationValue = CONFIGURATION_NUMBER; |
| 145 | config->iConfiguration = STRING_USBDOWN; |
| 146 | config->bind = g_dnl_do_config; |
| 147 | |
| 148 | return usb_add_config(cdev, config); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 151 | __weak |
Paul Kocialkowski | 99fc222 | 2015-06-12 19:57:01 +0200 | [diff] [blame^] | 152 | int board_usb_init(int index, enum usb_init_type init) |
| 153 | { |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | __weak |
| 158 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 159 | { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | __weak |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 164 | 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] | 165 | { |
| 166 | return 0; |
| 167 | } |
| 168 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 169 | __weak int g_dnl_get_board_bcd_device_number(int gcnum) |
| 170 | { |
| 171 | return gcnum; |
| 172 | } |
| 173 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 174 | __weak int g_dnl_board_usb_cable_connected(void) |
| 175 | { |
| 176 | return -EOPNOTSUPP; |
| 177 | } |
| 178 | |
Rob Herring | fe1b28c | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 179 | static bool g_dnl_detach_request; |
| 180 | |
| 181 | bool g_dnl_detach(void) |
| 182 | { |
| 183 | return g_dnl_detach_request; |
| 184 | } |
| 185 | |
| 186 | void g_dnl_trigger_detach(void) |
| 187 | { |
| 188 | g_dnl_detach_request = true; |
| 189 | } |
| 190 | |
| 191 | void g_dnl_clear_detach(void) |
| 192 | { |
| 193 | g_dnl_detach_request = false; |
| 194 | } |
| 195 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 196 | static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev) |
| 197 | { |
| 198 | struct usb_gadget *gadget = cdev->gadget; |
| 199 | int gcnum; |
| 200 | |
| 201 | gcnum = usb_gadget_controller_number(gadget); |
| 202 | if (gcnum > 0) |
| 203 | gcnum += 0x200; |
| 204 | |
| 205 | return g_dnl_get_board_bcd_device_number(gcnum); |
| 206 | } |
| 207 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 208 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 209 | { |
| 210 | struct usb_gadget *gadget = cdev->gadget; |
| 211 | int id, ret; |
| 212 | int gcnum; |
| 213 | |
| 214 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 215 | |
| 216 | id = usb_string_id(cdev); |
| 217 | |
| 218 | if (id < 0) |
| 219 | return id; |
| 220 | g_dnl_string_defs[0].id = id; |
| 221 | device_desc.iManufacturer = id; |
| 222 | |
| 223 | id = usb_string_id(cdev); |
| 224 | if (id < 0) |
| 225 | return id; |
| 226 | |
| 227 | g_dnl_string_defs[1].id = id; |
| 228 | device_desc.iProduct = id; |
| 229 | |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 230 | id = usb_string_id(cdev); |
| 231 | if (id < 0) |
| 232 | return id; |
| 233 | |
| 234 | g_dnl_string_defs[2].id = id; |
| 235 | device_desc.iSerialNumber = id; |
| 236 | |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 237 | g_dnl_bind_fixup(&device_desc, cdev->driver->name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 238 | ret = g_dnl_config_register(cdev); |
| 239 | if (ret) |
| 240 | goto error; |
| 241 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 242 | gcnum = g_dnl_get_bcd_device_number(cdev); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 243 | if (gcnum >= 0) |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 244 | device_desc.bcdDevice = cpu_to_le16(gcnum); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 245 | else { |
| 246 | debug("%s: controller '%s' not recognized\n", |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 247 | __func__, gadget->name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 248 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 249 | } |
| 250 | |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 251 | debug("%s: calling usb_gadget_connect for " |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 252 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 253 | usb_gadget_connect(gadget); |
| 254 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 255 | return 0; |
| 256 | |
| 257 | error: |
| 258 | g_dnl_unbind(cdev); |
| 259 | return -ENOMEM; |
| 260 | } |
| 261 | |
| 262 | static struct usb_composite_driver g_dnl_driver = { |
| 263 | .name = NULL, |
| 264 | .dev = &device_desc, |
| 265 | .strings = g_dnl_composite_strings, |
| 266 | |
| 267 | .bind = g_dnl_bind, |
| 268 | .unbind = g_dnl_unbind, |
| 269 | }; |
| 270 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 271 | /* |
| 272 | * NOTICE: |
| 273 | * Registering via USB function name won't be necessary after rewriting |
| 274 | * g_dnl to support multiple USB functions. |
| 275 | */ |
| 276 | int g_dnl_register(const char *name) |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 277 | { |
Stephen Warren | 25fbf96 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 278 | int ret; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 279 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 280 | debug("%s: g_dnl_driver.name = %s\n", __func__, name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 281 | g_dnl_driver.name = name; |
| 282 | |
Stephen Warren | 25fbf96 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 283 | ret = usb_composite_register(&g_dnl_driver); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 284 | if (ret) { |
| 285 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 286 | return ret; |
| 287 | } |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | void g_dnl_unregister(void) |
| 292 | { |
| 293 | usb_composite_unregister(&g_dnl_driver); |
| 294 | } |