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> |
| 15 | |
| 16 | #include <g_dnl.h> |
Lukasz Majewski | ba4e95c | 2013-09-17 15:58:21 +0200 | [diff] [blame] | 17 | #include <usb_mass_storage.h> |
Lukasz Majewski | a6921ad | 2013-09-17 15:58:23 +0200 | [diff] [blame] | 18 | #include <dfu.h> |
Lukasz Majewski | b958fb9 | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 19 | #include <thor.h> |
Lukasz Majewski | 1d4a0b6 | 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 | cfc2d0d | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 34 | /* Index of String Descriptor describing this configuration */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 35 | #define STRING_USBDOWN 2 |
Heiko Schocher | ec9002e | 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 | cfc2d0d | 2013-08-19 17:17:18 +0200 | [diff] [blame] | 39 | /* Number of supported configurations */ |
| 40 | #define CONFIGURATION_NUMBER 1 |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 41 | |
| 42 | #define DRIVER_VERSION "usb_dnl 2.0" |
| 43 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 44 | static const char product[] = "USB download gadget"; |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 45 | static char g_dnl_serial[MAX_STRING_SERIAL]; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 46 | static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER; |
| 47 | |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 48 | void g_dnl_set_serialnumber(char *s) |
| 49 | { |
| 50 | memset(g_dnl_serial, 0, MAX_STRING_SERIAL); |
| 51 | if (strlen(s) < MAX_STRING_SERIAL) |
| 52 | strncpy(g_dnl_serial, s, strlen(s)); |
| 53 | } |
| 54 | |
Lukasz Majewski | 1d4a0b6 | 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), |
| 60 | .bDeviceClass = USB_CLASS_COMM, |
| 61 | .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/ |
| 62 | |
| 63 | .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM), |
| 64 | .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM), |
| 65 | .iProduct = STRING_PRODUCT, |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 66 | .iSerialNumber = STRING_SERIAL, |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 67 | .bNumConfigurations = 1, |
| 68 | }; |
| 69 | |
Lukasz Majewski | c4219a8 | 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 | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 74 | static struct usb_string g_dnl_string_defs[] = { |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 75 | {.s = manufacturer}, |
| 76 | {.s = product}, |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 77 | {.s = g_dnl_serial}, |
Lukasz Majewski | c4219a8 | 2013-08-19 17:17:19 +0200 | [diff] [blame] | 78 | { } /* end of list */ |
Lukasz Majewski | 1d4a0b6 | 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 | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 93 | struct usb_gadget *gadget = cdev->gadget; |
| 94 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 95 | free(cdev->config); |
| 96 | cdev->config = NULL; |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 97 | debug("%s: calling usb_gadget_disconnect for " |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 98 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 99 | usb_gadget_disconnect(gadget); |
| 100 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 104 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void) |
| 105 | { |
| 106 | return ll_entry_start(struct g_dnl_bind_callback, |
| 107 | g_dnl_bind_callbacks); |
| 108 | } |
| 109 | |
| 110 | static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void) |
| 111 | { |
| 112 | return ll_entry_end(struct g_dnl_bind_callback, |
| 113 | g_dnl_bind_callbacks); |
| 114 | } |
| 115 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 116 | static int g_dnl_do_config(struct usb_configuration *c) |
| 117 | { |
| 118 | const char *s = c->cdev->driver->name; |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 119 | struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first(); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 120 | |
| 121 | debug("%s: configuration: 0x%p composite dev: 0x%p\n", |
| 122 | __func__, c, c->cdev); |
| 123 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 124 | for (; callback != g_dnl_bind_callback_end(); callback++) |
| 125 | if (!strcmp(s, callback->usb_function_name)) |
| 126 | return callback->fptr(c); |
| 127 | return -ENODEV; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 131 | { |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 132 | struct usb_configuration *config; |
| 133 | const char *name = "usb_dnload"; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 134 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 135 | config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config)); |
| 136 | if (!config) |
| 137 | return -ENOMEM; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 138 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 139 | memset(config, 0, sizeof(*config)); |
| 140 | |
| 141 | config->label = name; |
| 142 | config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER; |
| 143 | config->bConfigurationValue = CONFIGURATION_NUMBER; |
| 144 | config->iConfiguration = STRING_USBDOWN; |
| 145 | config->bind = g_dnl_do_config; |
| 146 | |
| 147 | return usb_add_config(cdev, config); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 150 | __weak |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 151 | 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] | 152 | { |
| 153 | return 0; |
| 154 | } |
| 155 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 156 | __weak int g_dnl_get_board_bcd_device_number(int gcnum) |
| 157 | { |
| 158 | return gcnum; |
| 159 | } |
| 160 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 161 | __weak int g_dnl_board_usb_cable_connected(void) |
| 162 | { |
| 163 | return -EOPNOTSUPP; |
| 164 | } |
| 165 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 166 | static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev) |
| 167 | { |
| 168 | struct usb_gadget *gadget = cdev->gadget; |
| 169 | int gcnum; |
| 170 | |
| 171 | gcnum = usb_gadget_controller_number(gadget); |
| 172 | if (gcnum > 0) |
| 173 | gcnum += 0x200; |
| 174 | |
| 175 | return g_dnl_get_board_bcd_device_number(gcnum); |
| 176 | } |
| 177 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 178 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 179 | { |
| 180 | struct usb_gadget *gadget = cdev->gadget; |
| 181 | int id, ret; |
| 182 | int gcnum; |
| 183 | |
| 184 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 185 | |
| 186 | id = usb_string_id(cdev); |
| 187 | |
| 188 | if (id < 0) |
| 189 | return id; |
| 190 | g_dnl_string_defs[0].id = id; |
| 191 | device_desc.iManufacturer = id; |
| 192 | |
| 193 | id = usb_string_id(cdev); |
| 194 | if (id < 0) |
| 195 | return id; |
| 196 | |
| 197 | g_dnl_string_defs[1].id = id; |
| 198 | device_desc.iProduct = id; |
| 199 | |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 200 | id = usb_string_id(cdev); |
| 201 | if (id < 0) |
| 202 | return id; |
| 203 | |
| 204 | g_dnl_string_defs[2].id = id; |
| 205 | device_desc.iSerialNumber = id; |
| 206 | |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 207 | g_dnl_bind_fixup(&device_desc, cdev->driver->name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 208 | ret = g_dnl_config_register(cdev); |
| 209 | if (ret) |
| 210 | goto error; |
| 211 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 212 | gcnum = g_dnl_get_bcd_device_number(cdev); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 213 | if (gcnum >= 0) |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 214 | device_desc.bcdDevice = cpu_to_le16(gcnum); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 215 | else { |
| 216 | debug("%s: controller '%s' not recognized\n", |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 217 | __func__, gadget->name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 218 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 219 | } |
| 220 | |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 221 | debug("%s: calling usb_gadget_connect for " |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 222 | "controller '%s'\n", __func__, gadget->name); |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 223 | usb_gadget_connect(gadget); |
| 224 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 225 | return 0; |
| 226 | |
| 227 | error: |
| 228 | g_dnl_unbind(cdev); |
| 229 | return -ENOMEM; |
| 230 | } |
| 231 | |
| 232 | static struct usb_composite_driver g_dnl_driver = { |
| 233 | .name = NULL, |
| 234 | .dev = &device_desc, |
| 235 | .strings = g_dnl_composite_strings, |
| 236 | |
| 237 | .bind = g_dnl_bind, |
| 238 | .unbind = g_dnl_unbind, |
| 239 | }; |
| 240 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 241 | /* |
| 242 | * NOTICE: |
| 243 | * Registering via USB function name won't be necessary after rewriting |
| 244 | * g_dnl to support multiple USB functions. |
| 245 | */ |
| 246 | int g_dnl_register(const char *name) |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 247 | { |
Stephen Warren | 25fbf96 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 248 | int ret; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 249 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 250 | debug("%s: g_dnl_driver.name = %s\n", __func__, name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 251 | g_dnl_driver.name = name; |
| 252 | |
Stephen Warren | 25fbf96 | 2014-05-01 15:45:16 -0600 | [diff] [blame] | 253 | ret = usb_composite_register(&g_dnl_driver); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 254 | if (ret) { |
| 255 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 256 | return ret; |
| 257 | } |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | void g_dnl_unregister(void) |
| 262 | { |
| 263 | usb_composite_unregister(&g_dnl_driver); |
| 264 | } |