2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-08-06 12:41:05 +00:00
|
|
|
/*
|
|
|
|
* g_dnl.c -- USB Downloader Gadget
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Samsung Electronics
|
|
|
|
* Lukasz Majewski <l.majewski@samsung.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2012-08-06 12:41:05 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#include <mmc.h>
|
|
|
|
#include <part.h>
|
2015-06-12 17:57:01 +00:00
|
|
|
#include <usb.h>
|
2012-08-06 12:41:05 +00:00
|
|
|
|
|
|
|
#include <g_dnl.h>
|
2013-09-17 13:58:21 +00:00
|
|
|
#include <usb_mass_storage.h>
|
2013-09-17 13:58:23 +00:00
|
|
|
#include <dfu.h>
|
2013-10-08 12:30:43 +00:00
|
|
|
#include <thor.h>
|
2012-08-06 12:41:05 +00:00
|
|
|
|
2017-09-01 12:42:03 +00:00
|
|
|
#include <env_callback.h>
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
#include "gadget_chips.h"
|
|
|
|
#include "composite.c"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* One needs to define the following:
|
2017-09-07 06:58:08 +00:00
|
|
|
* CONFIG_USB_GADGET_VENDOR_NUM
|
|
|
|
* CONFIG_USB_GADGET_PRODUCT_NUM
|
|
|
|
* CONFIG_USB_GADGET_MANUFACTURER
|
2016-04-13 11:20:30 +00:00
|
|
|
* at e.g. ./configs/<board>_defconfig
|
2012-08-06 12:41:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define STRING_MANUFACTURER 25
|
|
|
|
#define STRING_PRODUCT 2
|
2013-08-19 15:17:18 +00:00
|
|
|
/* Index of String Descriptor describing this configuration */
|
2012-08-06 12:41:05 +00:00
|
|
|
#define STRING_USBDOWN 2
|
2013-11-04 13:05:01 +00:00
|
|
|
/* Index of String serial */
|
|
|
|
#define STRING_SERIAL 3
|
2017-02-22 15:12:39 +00:00
|
|
|
#define MAX_STRING_SERIAL 256
|
2013-08-19 15:17:18 +00:00
|
|
|
/* Number of supported configurations */
|
|
|
|
#define CONFIGURATION_NUMBER 1
|
2012-08-06 12:41:05 +00:00
|
|
|
|
|
|
|
#define DRIVER_VERSION "usb_dnl 2.0"
|
|
|
|
|
|
|
|
static const char product[] = "USB download gadget";
|
2013-11-04 13:05:01 +00:00
|
|
|
static char g_dnl_serial[MAX_STRING_SERIAL];
|
2017-09-07 06:58:08 +00:00
|
|
|
static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER;
|
2012-08-06 12:41:05 +00:00
|
|
|
|
2013-11-04 13:05:01 +00:00
|
|
|
void g_dnl_set_serialnumber(char *s)
|
|
|
|
{
|
|
|
|
memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
|
2017-02-22 09:22:37 +00:00
|
|
|
strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
|
2013-11-04 13:05:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
static struct usb_device_descriptor device_desc = {
|
|
|
|
.bLength = sizeof device_desc,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
|
|
|
|
|
|
|
.bcdUSB = __constant_cpu_to_le16(0x0200),
|
2016-04-07 23:49:03 +00:00
|
|
|
.bDeviceClass = USB_CLASS_PER_INTERFACE,
|
|
|
|
.bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/
|
2012-08-06 12:41:05 +00:00
|
|
|
|
2017-09-07 06:58:08 +00:00
|
|
|
.idVendor = __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM),
|
|
|
|
.idProduct = __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM),
|
2017-02-22 15:12:41 +00:00
|
|
|
/* .iProduct = DYNAMIC */
|
|
|
|
/* .iSerialNumber = DYNAMIC */
|
2012-08-06 12:41:05 +00:00
|
|
|
.bNumConfigurations = 1,
|
|
|
|
};
|
|
|
|
|
2013-08-19 15:17:19 +00:00
|
|
|
/*
|
|
|
|
* static strings, in UTF-8
|
|
|
|
* IDs for those strings are assigned dynamically at g_dnl_bind()
|
|
|
|
*/
|
2012-08-06 12:41:05 +00:00
|
|
|
static struct usb_string g_dnl_string_defs[] = {
|
2013-08-19 15:17:19 +00:00
|
|
|
{.s = manufacturer},
|
|
|
|
{.s = product},
|
2013-11-04 13:05:01 +00:00
|
|
|
{.s = g_dnl_serial},
|
2013-08-19 15:17:19 +00:00
|
|
|
{ } /* end of list */
|
2012-08-06 12:41:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings g_dnl_string_tab = {
|
|
|
|
.language = 0x0409, /* en-us */
|
|
|
|
.strings = g_dnl_string_defs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings *g_dnl_composite_strings[] = {
|
|
|
|
&g_dnl_string_tab,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2020-03-18 08:24:46 +00:00
|
|
|
void g_dnl_set_product(const char *s)
|
|
|
|
{
|
|
|
|
if (s)
|
|
|
|
g_dnl_string_defs[1].s = s;
|
|
|
|
else
|
|
|
|
g_dnl_string_defs[1].s = product;
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
static int g_dnl_unbind(struct usb_composite_dev *cdev)
|
|
|
|
{
|
2012-11-30 08:01:05 +00:00
|
|
|
struct usb_gadget *gadget = cdev->gadget;
|
|
|
|
|
|
|
|
debug("%s: calling usb_gadget_disconnect for "
|
2014-04-28 19:13:28 +00:00
|
|
|
"controller '%s'\n", __func__, gadget->name);
|
2012-11-30 08:01:05 +00:00
|
|
|
usb_gadget_disconnect(gadget);
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-28 19:13:28 +00:00
|
|
|
static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
|
|
|
|
{
|
|
|
|
return ll_entry_start(struct g_dnl_bind_callback,
|
|
|
|
g_dnl_bind_callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
|
|
|
|
{
|
|
|
|
return ll_entry_end(struct g_dnl_bind_callback,
|
|
|
|
g_dnl_bind_callbacks);
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
static int g_dnl_do_config(struct usb_configuration *c)
|
|
|
|
{
|
|
|
|
const char *s = c->cdev->driver->name;
|
2014-04-28 19:13:28 +00:00
|
|
|
struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
|
2012-08-06 12:41:05 +00:00
|
|
|
|
|
|
|
debug("%s: configuration: 0x%p composite dev: 0x%p\n",
|
|
|
|
__func__, c, c->cdev);
|
|
|
|
|
2014-04-28 19:13:28 +00:00
|
|
|
for (; callback != g_dnl_bind_callback_end(); callback++)
|
|
|
|
if (!strcmp(s, callback->usb_function_name))
|
|
|
|
return callback->fptr(c);
|
|
|
|
return -ENODEV;
|
2012-08-06 12:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int g_dnl_config_register(struct usb_composite_dev *cdev)
|
|
|
|
{
|
2013-10-08 12:30:40 +00:00
|
|
|
struct usb_configuration *config;
|
|
|
|
const char *name = "usb_dnload";
|
2012-08-06 12:41:05 +00:00
|
|
|
|
2013-10-08 12:30:40 +00:00
|
|
|
config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
|
|
|
|
if (!config)
|
|
|
|
return -ENOMEM;
|
2012-08-06 12:41:05 +00:00
|
|
|
|
2013-10-08 12:30:40 +00:00
|
|
|
memset(config, 0, sizeof(*config));
|
|
|
|
|
|
|
|
config->label = name;
|
|
|
|
config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
|
|
|
|
config->bConfigurationValue = CONFIGURATION_NUMBER;
|
|
|
|
config->iConfiguration = STRING_USBDOWN;
|
|
|
|
config->bind = g_dnl_do_config;
|
|
|
|
|
|
|
|
return usb_add_config(cdev, config);
|
2012-08-06 12:41:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 17:57:01 +00:00
|
|
|
__weak
|
|
|
|
int board_usb_init(int index, enum usb_init_type init)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__weak
|
|
|
|
int board_usb_cleanup(int index, enum usb_init_type init)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-04 09:19:50 +00:00
|
|
|
__weak
|
2013-10-08 12:30:41 +00:00
|
|
|
int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
|
2013-06-04 09:19:50 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 13:05:02 +00:00
|
|
|
__weak int g_dnl_get_board_bcd_device_number(int gcnum)
|
|
|
|
{
|
|
|
|
return gcnum;
|
|
|
|
}
|
|
|
|
|
2014-04-30 11:07:48 +00:00
|
|
|
__weak int g_dnl_board_usb_cable_connected(void)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2014-12-10 20:43:03 +00:00
|
|
|
static bool g_dnl_detach_request;
|
|
|
|
|
|
|
|
bool g_dnl_detach(void)
|
|
|
|
{
|
|
|
|
return g_dnl_detach_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_dnl_trigger_detach(void)
|
|
|
|
{
|
|
|
|
g_dnl_detach_request = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_dnl_clear_detach(void)
|
|
|
|
{
|
|
|
|
g_dnl_detach_request = false;
|
|
|
|
}
|
|
|
|
|
2013-11-04 13:05:02 +00:00
|
|
|
static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
|
|
|
|
{
|
|
|
|
struct usb_gadget *gadget = cdev->gadget;
|
|
|
|
int gcnum;
|
|
|
|
|
|
|
|
gcnum = usb_gadget_controller_number(gadget);
|
|
|
|
if (gcnum > 0)
|
|
|
|
gcnum += 0x200;
|
|
|
|
|
|
|
|
return g_dnl_get_board_bcd_device_number(gcnum);
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:42:03 +00:00
|
|
|
/**
|
|
|
|
* Update internal serial number variable when the "serial#" env var changes.
|
|
|
|
*
|
|
|
|
* Handle all cases, even when flags == H_PROGRAMMATIC or op == env_op_delete.
|
|
|
|
*/
|
|
|
|
static int on_serialno(const char *name, const char *value, enum env_op op,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
g_dnl_set_serialnumber((char *)value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
U_BOOT_ENV_CALLBACK(serialno, on_serialno);
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
static int g_dnl_bind(struct usb_composite_dev *cdev)
|
|
|
|
{
|
|
|
|
struct usb_gadget *gadget = cdev->gadget;
|
|
|
|
int id, ret;
|
|
|
|
int gcnum;
|
|
|
|
|
|
|
|
debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
|
|
|
|
|
|
|
|
id = usb_string_id(cdev);
|
|
|
|
|
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
g_dnl_string_defs[0].id = id;
|
|
|
|
device_desc.iManufacturer = id;
|
|
|
|
|
|
|
|
id = usb_string_id(cdev);
|
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
|
|
|
|
g_dnl_string_defs[1].id = id;
|
|
|
|
device_desc.iProduct = id;
|
|
|
|
|
2017-06-26 11:04:58 +00:00
|
|
|
g_dnl_bind_fixup(&device_desc, cdev->driver->name);
|
|
|
|
|
2017-02-22 15:12:40 +00:00
|
|
|
if (strlen(g_dnl_serial)) {
|
|
|
|
id = usb_string_id(cdev);
|
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
|
|
|
|
g_dnl_string_defs[2].id = id;
|
|
|
|
device_desc.iSerialNumber = id;
|
|
|
|
}
|
2013-11-04 13:05:01 +00:00
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
ret = g_dnl_config_register(cdev);
|
|
|
|
if (ret)
|
|
|
|
goto error;
|
|
|
|
|
2013-11-04 13:05:02 +00:00
|
|
|
gcnum = g_dnl_get_bcd_device_number(cdev);
|
2012-08-06 12:41:05 +00:00
|
|
|
if (gcnum >= 0)
|
2013-11-04 13:05:02 +00:00
|
|
|
device_desc.bcdDevice = cpu_to_le16(gcnum);
|
2012-08-06 12:41:05 +00:00
|
|
|
else {
|
|
|
|
debug("%s: controller '%s' not recognized\n",
|
2014-04-28 19:13:28 +00:00
|
|
|
__func__, gadget->name);
|
2012-08-06 12:41:05 +00:00
|
|
|
device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
|
|
|
|
}
|
|
|
|
|
2012-11-30 08:01:05 +00:00
|
|
|
debug("%s: calling usb_gadget_connect for "
|
2014-04-28 19:13:28 +00:00
|
|
|
"controller '%s'\n", __func__, gadget->name);
|
2012-11-30 08:01:05 +00:00
|
|
|
usb_gadget_connect(gadget);
|
|
|
|
|
2012-08-06 12:41:05 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
g_dnl_unbind(cdev);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct usb_composite_driver g_dnl_driver = {
|
|
|
|
.name = NULL,
|
|
|
|
.dev = &device_desc,
|
|
|
|
.strings = g_dnl_composite_strings,
|
|
|
|
|
|
|
|
.bind = g_dnl_bind,
|
|
|
|
.unbind = g_dnl_unbind,
|
|
|
|
};
|
|
|
|
|
2014-04-28 19:13:28 +00:00
|
|
|
/*
|
|
|
|
* NOTICE:
|
|
|
|
* Registering via USB function name won't be necessary after rewriting
|
|
|
|
* g_dnl to support multiple USB functions.
|
|
|
|
*/
|
|
|
|
int g_dnl_register(const char *name)
|
2012-08-06 12:41:05 +00:00
|
|
|
{
|
2014-05-01 21:45:16 +00:00
|
|
|
int ret;
|
2012-08-06 12:41:05 +00:00
|
|
|
|
2014-04-28 19:13:28 +00:00
|
|
|
debug("%s: g_dnl_driver.name = %s\n", __func__, name);
|
2012-08-06 12:41:05 +00:00
|
|
|
g_dnl_driver.name = name;
|
|
|
|
|
2014-05-01 21:45:16 +00:00
|
|
|
ret = usb_composite_register(&g_dnl_driver);
|
2012-08-06 12:41:05 +00:00
|
|
|
if (ret) {
|
|
|
|
printf("%s: failed!, error: %d\n", __func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_dnl_unregister(void)
|
|
|
|
{
|
|
|
|
usb_composite_unregister(&g_dnl_driver);
|
|
|
|
}
|