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> |
Paul Kocialkowski | 46f9729 | 2015-06-12 19:57:01 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 16 | |
| 17 | #include <g_dnl.h> |
Lukasz Majewski | 0c63523 | 2013-09-17 15:58:21 +0200 | [diff] [blame] | 18 | #include <usb_mass_storage.h> |
Lukasz Majewski | c421946 | 2013-09-17 15:58:23 +0200 | [diff] [blame] | 19 | #include <dfu.h> |
Lukasz Majewski | 1c1af4f | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 20 | #include <thor.h> |
Lukasz Majewski | c574de5 | 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 |
Sam Protsenko | b706ffd | 2016-04-13 14:20:30 +0300 | [diff] [blame] | 30 | * at e.g. ./configs/<board>_defconfig |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #define STRING_MANUFACTURER 25 |
| 34 | #define STRING_PRODUCT 2 |
Lukasz Majewski | 0d9752e | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 35 | /* Index of String Descriptor describing this configuration */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 36 | #define STRING_USBDOWN 2 |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 37 | /* Index of String serial */ |
| 38 | #define STRING_SERIAL 3 |
Felipe Balbi | 02ae129 | 2017-02-22 17:12:39 +0200 | [diff] [blame] | 39 | #define MAX_STRING_SERIAL 256 |
Lukasz Majewski | 0d9752e | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 40 | /* Number of supported configurations */ |
| 41 | #define CONFIGURATION_NUMBER 1 |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 42 | |
| 43 | #define DRIVER_VERSION "usb_dnl 2.0" |
| 44 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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); |
Felipe Balbi | a4b305a | 2017-02-22 11:22:37 +0200 | [diff] [blame] | 52 | strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1); |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 55 | static struct usb_device_descriptor device_desc = { |
| 56 | .bLength = sizeof device_desc, |
| 57 | .bDescriptorType = USB_DT_DEVICE, |
| 58 | |
| 59 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
John Tobias | 83ba2e7 | 2016-04-07 16:49:03 -0700 | [diff] [blame] | 60 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 61 | .bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 62 | |
| 63 | .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM), |
| 64 | .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM), |
Felipe Balbi | c891ca9 | 2017-02-22 17:12:41 +0200 | [diff] [blame] | 65 | /* .iProduct = DYNAMIC */ |
| 66 | /* .iSerialNumber = DYNAMIC */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 67 | .bNumConfigurations = 1, |
| 68 | }; |
| 69 | |
Lukasz Majewski | dbbaef7 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 70 | /* |
| 71 | * static strings, in UTF-8 |
| 72 | * IDs for those strings are assigned dynamically at g_dnl_bind() |
| 73 | */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 74 | static struct usb_string g_dnl_string_defs[] = { |
Lukasz Majewski | dbbaef7 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 75 | {.s = manufacturer}, |
| 76 | {.s = product}, |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 77 | {.s = g_dnl_serial}, |
Lukasz Majewski | dbbaef7 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 78 | { } /* end of list */ |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static struct usb_gadget_strings g_dnl_string_tab = { |
| 82 | .language = 0x0409, /* en-us */ |
| 83 | .strings = g_dnl_string_defs, |
| 84 | }; |
| 85 | |
| 86 | static struct usb_gadget_strings *g_dnl_composite_strings[] = { |
| 87 | &g_dnl_string_tab, |
| 88 | NULL, |
| 89 | }; |
| 90 | |
| 91 | static int g_dnl_unbind(struct usb_composite_dev *cdev) |
| 92 | { |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 93 | struct usb_gadget *gadget = cdev->gadget; |
| 94 | |
| 95 | debug("%s: calling usb_gadget_disconnect for " |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 96 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 97 | usb_gadget_disconnect(gadget); |
| 98 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 102 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void) |
| 103 | { |
| 104 | return ll_entry_start(struct g_dnl_bind_callback, |
| 105 | g_dnl_bind_callbacks); |
| 106 | } |
| 107 | |
| 108 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void) |
| 109 | { |
| 110 | return ll_entry_end(struct g_dnl_bind_callback, |
| 111 | g_dnl_bind_callbacks); |
| 112 | } |
| 113 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 114 | static int g_dnl_do_config(struct usb_configuration *c) |
| 115 | { |
| 116 | const char *s = c->cdev->driver->name; |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 117 | struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first(); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 118 | |
| 119 | debug("%s: configuration: 0x%p composite dev: 0x%p\n", |
| 120 | __func__, c, c->cdev); |
| 121 | |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 122 | for (; callback != g_dnl_bind_callback_end(); callback++) |
| 123 | if (!strcmp(s, callback->usb_function_name)) |
| 124 | return callback->fptr(c); |
| 125 | return -ENODEV; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 129 | { |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 130 | struct usb_configuration *config; |
| 131 | const char *name = "usb_dnload"; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 132 | |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 133 | config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config)); |
| 134 | if (!config) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | memset(config, 0, sizeof(*config)); |
| 138 | |
| 139 | config->label = name; |
| 140 | config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER; |
| 141 | config->bConfigurationValue = CONFIGURATION_NUMBER; |
| 142 | config->iConfiguration = STRING_USBDOWN; |
| 143 | config->bind = g_dnl_do_config; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 144 | |
Lukasz Majewski | 8143f22 | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 145 | return usb_add_config(cdev, config); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Heiko Schocher | 07a12ea | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 148 | __weak |
Paul Kocialkowski | 46f9729 | 2015-06-12 19:57:01 +0200 | [diff] [blame] | 149 | int board_usb_init(int index, enum usb_init_type init) |
| 150 | { |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | __weak |
| 155 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | __weak |
Lukasz Majewski | 44b5b38 | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 161 | 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] | 162 | { |
| 163 | return 0; |
| 164 | } |
| 165 | |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 166 | __weak int g_dnl_get_board_bcd_device_number(int gcnum) |
| 167 | { |
| 168 | return gcnum; |
| 169 | } |
| 170 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 171 | __weak int g_dnl_board_usb_cable_connected(void) |
| 172 | { |
| 173 | return -EOPNOTSUPP; |
| 174 | } |
| 175 | |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 176 | static bool g_dnl_detach_request; |
| 177 | |
| 178 | bool g_dnl_detach(void) |
| 179 | { |
| 180 | return g_dnl_detach_request; |
| 181 | } |
| 182 | |
| 183 | void g_dnl_trigger_detach(void) |
| 184 | { |
| 185 | g_dnl_detach_request = true; |
| 186 | } |
| 187 | |
| 188 | void g_dnl_clear_detach(void) |
| 189 | { |
| 190 | g_dnl_detach_request = false; |
| 191 | } |
| 192 | |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 193 | static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev) |
| 194 | { |
| 195 | struct usb_gadget *gadget = cdev->gadget; |
| 196 | int gcnum; |
| 197 | |
| 198 | gcnum = usb_gadget_controller_number(gadget); |
| 199 | if (gcnum > 0) |
| 200 | gcnum += 0x200; |
| 201 | |
| 202 | return g_dnl_get_board_bcd_device_number(gcnum); |
| 203 | } |
| 204 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 205 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 206 | { |
| 207 | struct usb_gadget *gadget = cdev->gadget; |
| 208 | int id, ret; |
| 209 | int gcnum; |
| 210 | |
| 211 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 212 | |
| 213 | id = usb_string_id(cdev); |
| 214 | |
| 215 | if (id < 0) |
| 216 | return id; |
| 217 | g_dnl_string_defs[0].id = id; |
| 218 | device_desc.iManufacturer = id; |
| 219 | |
| 220 | id = usb_string_id(cdev); |
| 221 | if (id < 0) |
| 222 | return id; |
| 223 | |
| 224 | g_dnl_string_defs[1].id = id; |
| 225 | device_desc.iProduct = id; |
| 226 | |
Felipe Balbi | 169dd12 | 2017-02-22 17:12:40 +0200 | [diff] [blame] | 227 | if (strlen(g_dnl_serial)) { |
| 228 | id = usb_string_id(cdev); |
| 229 | if (id < 0) |
| 230 | return id; |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 231 | |
Felipe Balbi | 169dd12 | 2017-02-22 17:12:40 +0200 | [diff] [blame] | 232 | g_dnl_string_defs[2].id = id; |
| 233 | device_desc.iSerialNumber = id; |
| 234 | } |
Heiko Schocher | 1799fa8 | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 235 | |
Lukasz Majewski | 44b5b38 | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 236 | g_dnl_bind_fixup(&device_desc, cdev->driver->name); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 237 | ret = g_dnl_config_register(cdev); |
| 238 | if (ret) |
| 239 | goto error; |
| 240 | |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 241 | gcnum = g_dnl_get_bcd_device_number(cdev); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 242 | if (gcnum >= 0) |
Heiko Schocher | 6f464a5 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 243 | device_desc.bcdDevice = cpu_to_le16(gcnum); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 244 | else { |
| 245 | debug("%s: controller '%s' not recognized\n", |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 246 | __func__, gadget->name); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 247 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 248 | } |
| 249 | |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 250 | debug("%s: calling usb_gadget_connect for " |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 251 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | c29d2be | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 252 | usb_gadget_connect(gadget); |
| 253 | |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 254 | return 0; |
| 255 | |
| 256 | error: |
| 257 | g_dnl_unbind(cdev); |
| 258 | return -ENOMEM; |
| 259 | } |
| 260 | |
| 261 | static struct usb_composite_driver g_dnl_driver = { |
| 262 | .name = NULL, |
| 263 | .dev = &device_desc, |
| 264 | .strings = g_dnl_composite_strings, |
| 265 | |
| 266 | .bind = g_dnl_bind, |
| 267 | .unbind = g_dnl_unbind, |
| 268 | }; |
| 269 | |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 270 | /* |
| 271 | * NOTICE: |
| 272 | * Registering via USB function name won't be necessary after rewriting |
| 273 | * g_dnl to support multiple USB functions. |
| 274 | */ |
| 275 | int g_dnl_register(const char *name) |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 276 | { |
Stephen Warren | e239fa5 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 277 | int ret; |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 278 | |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 279 | debug("%s: g_dnl_driver.name = %s\n", __func__, name); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 280 | g_dnl_driver.name = name; |
| 281 | |
Stephen Warren | e239fa5 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 282 | ret = usb_composite_register(&g_dnl_driver); |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 283 | if (ret) { |
| 284 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 285 | return ret; |
| 286 | } |
Lukasz Majewski | c574de5 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | void g_dnl_unregister(void) |
| 291 | { |
| 292 | usb_composite_unregister(&g_dnl_driver); |
| 293 | } |