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 | |
| 10 | #include <errno.h> |
| 11 | #include <common.h> |
| 12 | #include <malloc.h> |
| 13 | |
| 14 | #include <mmc.h> |
| 15 | #include <part.h> |
| 16 | |
| 17 | #include <g_dnl.h> |
| 18 | #include "f_dfu.h" |
| 19 | |
| 20 | #include "gadget_chips.h" |
| 21 | #include "composite.c" |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 22 | #include "f_mass_storage.c" |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 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 |
| 34 | #define STRING_USBDOWN 2 |
| 35 | #define CONFIG_USBDOWNLOADER 2 |
| 36 | |
| 37 | #define DRIVER_VERSION "usb_dnl 2.0" |
| 38 | |
| 39 | static const char shortname[] = "usb_dnl_"; |
| 40 | static const char product[] = "USB download gadget"; |
| 41 | static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER; |
| 42 | |
| 43 | static struct usb_device_descriptor device_desc = { |
| 44 | .bLength = sizeof device_desc, |
| 45 | .bDescriptorType = USB_DT_DEVICE, |
| 46 | |
| 47 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 48 | .bDeviceClass = USB_CLASS_COMM, |
| 49 | .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/ |
| 50 | |
| 51 | .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM), |
| 52 | .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM), |
| 53 | .iProduct = STRING_PRODUCT, |
| 54 | .bNumConfigurations = 1, |
| 55 | }; |
| 56 | |
| 57 | /* static strings, in UTF-8 */ |
| 58 | static struct usb_string g_dnl_string_defs[] = { |
| 59 | { 0, manufacturer, }, |
| 60 | { 1, product, }, |
Pantelis Antoniou | 598cf60 | 2012-11-30 08:01:06 +0000 | [diff] [blame] | 61 | { } /* end of list */ |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static struct usb_gadget_strings g_dnl_string_tab = { |
| 65 | .language = 0x0409, /* en-us */ |
| 66 | .strings = g_dnl_string_defs, |
| 67 | }; |
| 68 | |
| 69 | static struct usb_gadget_strings *g_dnl_composite_strings[] = { |
| 70 | &g_dnl_string_tab, |
| 71 | NULL, |
| 72 | }; |
| 73 | |
| 74 | static int g_dnl_unbind(struct usb_composite_dev *cdev) |
| 75 | { |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 76 | struct usb_gadget *gadget = cdev->gadget; |
| 77 | |
| 78 | debug("%s: calling usb_gadget_disconnect for " |
| 79 | "controller '%s'\n", shortname, gadget->name); |
| 80 | usb_gadget_disconnect(gadget); |
| 81 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int g_dnl_do_config(struct usb_configuration *c) |
| 86 | { |
| 87 | const char *s = c->cdev->driver->name; |
| 88 | int ret = -1; |
| 89 | |
| 90 | debug("%s: configuration: 0x%p composite dev: 0x%p\n", |
| 91 | __func__, c, c->cdev); |
| 92 | |
| 93 | printf("GADGET DRIVER: %s\n", s); |
| 94 | if (!strcmp(s, "usb_dnl_dfu")) |
| 95 | ret = dfu_add(c); |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 96 | else if (!strcmp(s, "usb_dnl_ums")) |
| 97 | ret = fsg_add(c); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int g_dnl_config_register(struct usb_composite_dev *cdev) |
| 103 | { |
| 104 | static struct usb_configuration config = { |
| 105 | .label = "usb_dnload", |
| 106 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 107 | .bConfigurationValue = CONFIG_USBDOWNLOADER, |
| 108 | .iConfiguration = STRING_USBDOWN, |
| 109 | |
| 110 | .bind = g_dnl_do_config, |
| 111 | }; |
| 112 | |
| 113 | return usb_add_config(cdev, &config); |
| 114 | } |
| 115 | |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 116 | __weak |
| 117 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev) |
| 118 | { |
| 119 | return 0; |
| 120 | } |
| 121 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 122 | static int g_dnl_bind(struct usb_composite_dev *cdev) |
| 123 | { |
| 124 | struct usb_gadget *gadget = cdev->gadget; |
| 125 | int id, ret; |
| 126 | int gcnum; |
| 127 | |
| 128 | debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev); |
| 129 | |
| 130 | id = usb_string_id(cdev); |
| 131 | |
| 132 | if (id < 0) |
| 133 | return id; |
| 134 | g_dnl_string_defs[0].id = id; |
| 135 | device_desc.iManufacturer = id; |
| 136 | |
| 137 | id = usb_string_id(cdev); |
| 138 | if (id < 0) |
| 139 | return id; |
| 140 | |
| 141 | g_dnl_string_defs[1].id = id; |
| 142 | device_desc.iProduct = id; |
| 143 | |
Heiko Schocher | c5398cc | 2013-06-04 11:19:50 +0200 | [diff] [blame] | 144 | g_dnl_bind_fixup(&device_desc); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 145 | ret = g_dnl_config_register(cdev); |
| 146 | if (ret) |
| 147 | goto error; |
| 148 | |
| 149 | gcnum = usb_gadget_controller_number(gadget); |
| 150 | |
| 151 | debug("gcnum: %d\n", gcnum); |
| 152 | if (gcnum >= 0) |
| 153 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); |
| 154 | else { |
| 155 | debug("%s: controller '%s' not recognized\n", |
| 156 | shortname, gadget->name); |
| 157 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 158 | } |
| 159 | |
Pantelis Antoniou | 5a413ca | 2012-11-30 08:01:05 +0000 | [diff] [blame] | 160 | debug("%s: calling usb_gadget_connect for " |
| 161 | "controller '%s'\n", shortname, gadget->name); |
| 162 | usb_gadget_connect(gadget); |
| 163 | |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 164 | return 0; |
| 165 | |
| 166 | error: |
| 167 | g_dnl_unbind(cdev); |
| 168 | return -ENOMEM; |
| 169 | } |
| 170 | |
| 171 | static struct usb_composite_driver g_dnl_driver = { |
| 172 | .name = NULL, |
| 173 | .dev = &device_desc, |
| 174 | .strings = g_dnl_composite_strings, |
| 175 | |
| 176 | .bind = g_dnl_bind, |
| 177 | .unbind = g_dnl_unbind, |
| 178 | }; |
| 179 | |
| 180 | int g_dnl_register(const char *type) |
| 181 | { |
| 182 | /* We only allow "dfu" atm, so 3 should be enough */ |
| 183 | static char name[sizeof(shortname) + 3]; |
| 184 | int ret; |
| 185 | |
| 186 | if (!strcmp(type, "dfu")) { |
| 187 | strcpy(name, shortname); |
| 188 | strcat(name, type); |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 189 | } else if (!strcmp(type, "ums")) { |
| 190 | strcpy(name, shortname); |
| 191 | strcat(name, type); |
Lukasz Majewski | 1d4a0b6 | 2012-08-06 14:41:05 +0200 | [diff] [blame] | 192 | } else { |
| 193 | printf("%s: unknown command: %s\n", __func__, type); |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | g_dnl_driver.name = name; |
| 198 | |
| 199 | debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name); |
| 200 | ret = usb_composite_register(&g_dnl_driver); |
| 201 | |
| 202 | if (ret) { |
| 203 | printf("%s: failed!, error: %d\n", __func__, ret); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | void g_dnl_unregister(void) |
| 211 | { |
| 212 | usb_composite_unregister(&g_dnl_driver); |
| 213 | } |