usb: dfu: introduce dfuMANIFEST state
[oweals/u-boot.git] / drivers / usb / gadget / g_dnl.c
index 8a3777b6e0b2d8e09525b600084465f612c6bb28..dd95afe86a5d5d44bbc07c91c883d09b1efa6958 100644 (file)
@@ -4,22 +4,9 @@
  * Copyright (C) 2012 Samsung Electronics
  * Lukasz Majewski  <l.majewski@samsung.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
-#include <errno.h>
 #include <common.h>
 #include <malloc.h>
 
 #include <part.h>
 
 #include <g_dnl.h>
-#include "f_dfu.h"
+#include <usb_mass_storage.h>
+#include <dfu.h>
+#include <thor.h>
 
 #include "gadget_chips.h"
 #include "composite.c"
-#include "f_mass_storage.c"
 
 /*
  * One needs to define the following:
 
 #define STRING_MANUFACTURER 25
 #define STRING_PRODUCT 2
+/* Index of String Descriptor describing this configuration */
 #define STRING_USBDOWN 2
-#define CONFIG_USBDOWNLOADER 2
+/* Index of String serial */
+#define STRING_SERIAL  3
+#define MAX_STRING_SERIAL      32
+/* Number of supported configurations */
+#define CONFIGURATION_NUMBER 1
 
 #define DRIVER_VERSION         "usb_dnl 2.0"
 
 static const char shortname[] = "usb_dnl_";
 static const char product[] = "USB download gadget";
+static char g_dnl_serial[MAX_STRING_SERIAL];
 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
 
+void g_dnl_set_serialnumber(char *s)
+{
+       memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
+       if (strlen(s) < MAX_STRING_SERIAL)
+               strncpy(g_dnl_serial, s, strlen(s));
+}
+
 static struct usb_device_descriptor device_desc = {
        .bLength = sizeof device_desc,
        .bDescriptorType = USB_DT_DEVICE,
@@ -63,14 +64,19 @@ static struct usb_device_descriptor device_desc = {
        .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
        .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
        .iProduct = STRING_PRODUCT,
+       .iSerialNumber = STRING_SERIAL,
        .bNumConfigurations = 1,
 };
 
-/* static strings, in UTF-8 */
+/*
+ * static strings, in UTF-8
+ * IDs for those strings are assigned dynamically at g_dnl_bind()
+ */
 static struct usb_string g_dnl_string_defs[] = {
-       { 0, manufacturer, },
-       { 1, product, },
-       {  }            /* end of list */
+       {.s = manufacturer},
+       {.s = product},
+       {.s = g_dnl_serial},
+       { }             /* end of list */
 };
 
 static struct usb_gadget_strings g_dnl_string_tab = {
@@ -87,6 +93,8 @@ static int g_dnl_unbind(struct usb_composite_dev *cdev)
 {
        struct usb_gadget *gadget = cdev->gadget;
 
+       free(cdev->config);
+       cdev->config = NULL;
        debug("%s: calling usb_gadget_disconnect for "
                        "controller '%s'\n", shortname, gadget->name);
        usb_gadget_disconnect(gadget);
@@ -107,30 +115,55 @@ static int g_dnl_do_config(struct usb_configuration *c)
                ret = dfu_add(c);
        else if (!strcmp(s, "usb_dnl_ums"))
                ret = fsg_add(c);
+       else if (!strcmp(s, "usb_dnl_thor"))
+               ret = thor_add(c);
 
        return ret;
 }
 
 static int g_dnl_config_register(struct usb_composite_dev *cdev)
 {
-       static struct usb_configuration config = {
-               .label = "usb_dnload",
-               .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
-               .bConfigurationValue =  CONFIG_USBDOWNLOADER,
-               .iConfiguration =       STRING_USBDOWN,
+       struct usb_configuration *config;
+       const char *name = "usb_dnload";
+
+       config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
+       if (!config)
+               return -ENOMEM;
 
-               .bind = g_dnl_do_config,
-       };
+       memset(config, 0, sizeof(*config));
 
-       return usb_add_config(cdev, &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);
 }
 
 __weak
-int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
+int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
 {
        return 0;
 }
 
+__weak int g_dnl_get_board_bcd_device_number(int gcnum)
+{
+       return gcnum;
+}
+
+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);
+}
+
 static int g_dnl_bind(struct usb_composite_dev *cdev)
 {
        struct usb_gadget *gadget = cdev->gadget;
@@ -153,16 +186,21 @@ static int g_dnl_bind(struct usb_composite_dev *cdev)
        g_dnl_string_defs[1].id = id;
        device_desc.iProduct = id;
 
-       g_dnl_bind_fixup(&device_desc);
+       id = usb_string_id(cdev);
+       if (id < 0)
+               return id;
+
+       g_dnl_string_defs[2].id = id;
+       device_desc.iSerialNumber = id;
+
+       g_dnl_bind_fixup(&device_desc, cdev->driver->name);
        ret = g_dnl_config_register(cdev);
        if (ret)
                goto error;
 
-       gcnum = usb_gadget_controller_number(gadget);
-
-       debug("gcnum: %d\n", gcnum);
+       gcnum = g_dnl_get_bcd_device_number(cdev);
        if (gcnum >= 0)
-               device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
+               device_desc.bcdDevice = cpu_to_le16(gcnum);
        else {
                debug("%s: controller '%s' not recognized\n",
                        shortname, gadget->name);
@@ -191,8 +229,8 @@ static struct usb_composite_driver g_dnl_driver = {
 
 int g_dnl_register(const char *type)
 {
-       /* We only allow "dfu" atm, so 3 should be enough */
-       static char name[sizeof(shortname) + 3];
+       /* The largest function name is 4 */
+       static char name[sizeof(shortname) + 4];
        int ret;
 
        if (!strcmp(type, "dfu")) {
@@ -201,6 +239,9 @@ int g_dnl_register(const char *type)
        } else if (!strcmp(type, "ums")) {
                strcpy(name, shortname);
                strcat(name, type);
+       } else if (!strcmp(type, "thor")) {
+               strcpy(name, shortname);
+               strcat(name, type);
        } else {
                printf("%s: unknown command: %s\n", __func__, type);
                return -EINVAL;