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 | |
| 44 | static const char shortname[] = "usb_dnl_"; |
| 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 " |
| 99 | "controller '%s'\n", shortname, gadget->name); |
| 100 | usb_gadget_disconnect(gadget); |
| 101 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int g_dnl_do_config(struct usb_configuration *c) |
| 106 | { |
| 107 | const char *s = c->cdev->driver->name; |
| 108 | int ret = -1; |
| 109 | |
| 110 | debug("%s: configuration: 0x%p composite dev: 0x%p\n", |
| 111 | __func__, c, c->cdev); |
| 112 | |
| 113 | printf("GADGET DRIVER: %s\n", s); |
| 114 | if (!strcmp(s, "usb_dnl_dfu")) |
| 115 | ret = dfu_add(c); |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 116 | else if (!strcmp(s, "usb_dnl_ums")) |
| 117 | ret = fsg_add(c); |
Lukasz Majewski | b958fb9 | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 118 | else if (!strcmp(s, "usb_dnl_thor")) |
| 119 | ret = thor_add(c); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 125 | { |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 126 | struct usb_configuration *config; |
| 127 | const char *name = "usb_dnload"; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 128 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 129 | config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config)); |
| 130 | if (!config) |
| 131 | return -ENOMEM; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 132 | |
Lukasz Majewski | 7b412ab | 2013-10-08 14:30:40 +0200 | [diff] [blame] | 133 | memset(config, 0, sizeof(*config)); |
| 134 | |
| 135 | config->label = name; |
| 136 | config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER; |
| 137 | config->bConfigurationValue = CONFIGURATION_NUMBER; |
| 138 | config->iConfiguration = STRING_USBDOWN; |
| 139 | config->bind = g_dnl_do_config; |
| 140 | |
| 141 | return usb_add_config(cdev, config); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 144 | __weak |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 150 | __weak int g_dnl_get_board_bcd_device_number(int gcnum) |
| 151 | { |
| 152 | return gcnum; |
| 153 | } |
| 154 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame^] | 155 | __weak int g_dnl_board_usb_cable_connected(void) |
| 156 | { |
| 157 | return -EOPNOTSUPP; |
| 158 | } |
| 159 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 160 | static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev) |
| 161 | { |
| 162 | struct usb_gadget *gadget = cdev->gadget; |
| 163 | int gcnum; |
| 164 | |
| 165 | gcnum = usb_gadget_controller_number(gadget); |
| 166 | if (gcnum > 0) |
| 167 | gcnum += 0x200; |
| 168 | |
| 169 | return g_dnl_get_board_bcd_device_number(gcnum); |
| 170 | } |
| 171 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 172 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 173 | { |
| 174 | struct usb_gadget *gadget = cdev->gadget; |
| 175 | int id, ret; |
| 176 | int gcnum; |
| 177 | |
| 178 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 179 | |
| 180 | id = usb_string_id(cdev); |
| 181 | |
| 182 | if (id < 0) |
| 183 | return id; |
| 184 | g_dnl_string_defs[0].id = id; |
| 185 | device_desc.iManufacturer = id; |
| 186 | |
| 187 | id = usb_string_id(cdev); |
| 188 | if (id < 0) |
| 189 | return id; |
| 190 | |
| 191 | g_dnl_string_defs[1].id = id; |
| 192 | device_desc.iProduct = id; |
| 193 | |
Heiko Schocher | ec9002e | 2013-11-04 14:05:01 +0100 | [diff] [blame] | 194 | id = usb_string_id(cdev); |
| 195 | if (id < 0) |
| 196 | return id; |
| 197 | |
| 198 | g_dnl_string_defs[2].id = id; |
| 199 | device_desc.iSerialNumber = id; |
| 200 | |
Lukasz Majewski | d6eae7b | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 201 | g_dnl_bind_fixup(&device_desc, cdev->driver->name); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 202 | ret = g_dnl_config_register(cdev); |
| 203 | if (ret) |
| 204 | goto error; |
| 205 | |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 206 | gcnum = g_dnl_get_bcd_device_number(cdev); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 207 | if (gcnum >= 0) |
Heiko Schocher | 7a0d463 | 2013-11-04 14:05:02 +0100 | [diff] [blame] | 208 | device_desc.bcdDevice = cpu_to_le16(gcnum); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 209 | else { |
| 210 | debug("%s: controller '%s' not recognized\n", |
| 211 | shortname, gadget->name); |
| 212 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 213 | } |
| 214 | |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 215 | debug("%s: calling usb_gadget_connect for " |
| 216 | "controller '%s'\n", shortname, gadget->name); |
| 217 | usb_gadget_connect(gadget); |
| 218 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | |
| 221 | error: |
| 222 | g_dnl_unbind(cdev); |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | |
| 226 | static struct usb_composite_driver g_dnl_driver = { |
| 227 | .name = NULL, |
| 228 | .dev = &device_desc, |
| 229 | .strings = g_dnl_composite_strings, |
| 230 | |
| 231 | .bind = g_dnl_bind, |
| 232 | .unbind = g_dnl_unbind, |
| 233 | }; |
| 234 | |
| 235 | int g_dnl_register(const char *type) |
| 236 | { |
Lukasz Majewski | b958fb9 | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 237 | /* The largest function name is 4 */ |
| 238 | static char name[sizeof(shortname) + 4]; |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 239 | int ret; |
| 240 | |
| 241 | if (!strcmp(type, "dfu")) { |
| 242 | strcpy(name, shortname); |
| 243 | strcat(name, type); |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 244 | } else if (!strcmp(type, "ums")) { |
| 245 | strcpy(name, shortname); |
| 246 | strcat(name, type); |
Lukasz Majewski | b958fb9 | 2013-10-08 14:30:43 +0200 | [diff] [blame] | 247 | } else if (!strcmp(type, "thor")) { |
| 248 | strcpy(name, shortname); |
| 249 | strcat(name, type); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 250 | } else { |
| 251 | printf("%s: unknown command: %s\n", __func__, type); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | |
| 255 | g_dnl_driver.name = name; |
| 256 | |
| 257 | debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name); |
| 258 | ret = usb_composite_register(&g_dnl_driver); |
| 259 | |
| 260 | if (ret) { |
| 261 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | void g_dnl_unregister(void) |
| 269 | { |
| 270 | usb_composite_unregister(&g_dnl_driver); |
| 271 | } |