blob: a3e05a872a7bdd157ddcad653a32cc9f97f391d5 [file] [log] [blame]
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +02001/*
2 * g_dnl.c -- USB Downloader Gadget
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * Lukasz Majewski <l.majewski@samsung.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +02008 */
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 Majewskib528f712013-03-05 12:10:17 +010022#include "f_mass_storage.c"
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020023
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 Majewskicfc2d0d2013-08-19 17:17:18 +020034/* Index of String Descriptor describing this configuration */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020035#define STRING_USBDOWN 2
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020036/* Number of supported configurations */
37#define CONFIGURATION_NUMBER 1
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020038
39#define DRIVER_VERSION "usb_dnl 2.0"
40
41static const char shortname[] = "usb_dnl_";
42static const char product[] = "USB download gadget";
43static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
44
45static struct usb_device_descriptor device_desc = {
46 .bLength = sizeof device_desc,
47 .bDescriptorType = USB_DT_DEVICE,
48
49 .bcdUSB = __constant_cpu_to_le16(0x0200),
50 .bDeviceClass = USB_CLASS_COMM,
51 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
52
53 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
54 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
55 .iProduct = STRING_PRODUCT,
56 .bNumConfigurations = 1,
57};
58
Lukasz Majewskic4219a82013-08-19 17:17:19 +020059/*
60 * static strings, in UTF-8
61 * IDs for those strings are assigned dynamically at g_dnl_bind()
62 */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020063static struct usb_string g_dnl_string_defs[] = {
Lukasz Majewskic4219a82013-08-19 17:17:19 +020064 {.s = manufacturer},
65 {.s = product},
66 { } /* end of list */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020067};
68
69static struct usb_gadget_strings g_dnl_string_tab = {
70 .language = 0x0409, /* en-us */
71 .strings = g_dnl_string_defs,
72};
73
74static struct usb_gadget_strings *g_dnl_composite_strings[] = {
75 &g_dnl_string_tab,
76 NULL,
77};
78
79static int g_dnl_unbind(struct usb_composite_dev *cdev)
80{
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000081 struct usb_gadget *gadget = cdev->gadget;
82
83 debug("%s: calling usb_gadget_disconnect for "
84 "controller '%s'\n", shortname, gadget->name);
85 usb_gadget_disconnect(gadget);
86
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020087 return 0;
88}
89
90static int g_dnl_do_config(struct usb_configuration *c)
91{
92 const char *s = c->cdev->driver->name;
93 int ret = -1;
94
95 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
96 __func__, c, c->cdev);
97
98 printf("GADGET DRIVER: %s\n", s);
99 if (!strcmp(s, "usb_dnl_dfu"))
100 ret = dfu_add(c);
Lukasz Majewskib528f712013-03-05 12:10:17 +0100101 else if (!strcmp(s, "usb_dnl_ums"))
102 ret = fsg_add(c);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200103
104 return ret;
105}
106
107static int g_dnl_config_register(struct usb_composite_dev *cdev)
108{
109 static struct usb_configuration config = {
110 .label = "usb_dnload",
111 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +0200112 .bConfigurationValue = CONFIGURATION_NUMBER,
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200113 .iConfiguration = STRING_USBDOWN,
114
115 .bind = g_dnl_do_config,
116 };
117
118 return usb_add_config(cdev, &config);
119}
120
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200121__weak
122int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
123{
124 return 0;
125}
126
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200127static int g_dnl_bind(struct usb_composite_dev *cdev)
128{
129 struct usb_gadget *gadget = cdev->gadget;
130 int id, ret;
131 int gcnum;
132
133 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
134
135 id = usb_string_id(cdev);
136
137 if (id < 0)
138 return id;
139 g_dnl_string_defs[0].id = id;
140 device_desc.iManufacturer = id;
141
142 id = usb_string_id(cdev);
143 if (id < 0)
144 return id;
145
146 g_dnl_string_defs[1].id = id;
147 device_desc.iProduct = id;
148
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200149 g_dnl_bind_fixup(&device_desc);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200150 ret = g_dnl_config_register(cdev);
151 if (ret)
152 goto error;
153
154 gcnum = usb_gadget_controller_number(gadget);
155
156 debug("gcnum: %d\n", gcnum);
157 if (gcnum >= 0)
158 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
159 else {
160 debug("%s: controller '%s' not recognized\n",
161 shortname, gadget->name);
162 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
163 }
164
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000165 debug("%s: calling usb_gadget_connect for "
166 "controller '%s'\n", shortname, gadget->name);
167 usb_gadget_connect(gadget);
168
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200169 return 0;
170
171 error:
172 g_dnl_unbind(cdev);
173 return -ENOMEM;
174}
175
176static struct usb_composite_driver g_dnl_driver = {
177 .name = NULL,
178 .dev = &device_desc,
179 .strings = g_dnl_composite_strings,
180
181 .bind = g_dnl_bind,
182 .unbind = g_dnl_unbind,
183};
184
185int g_dnl_register(const char *type)
186{
187 /* We only allow "dfu" atm, so 3 should be enough */
188 static char name[sizeof(shortname) + 3];
189 int ret;
190
191 if (!strcmp(type, "dfu")) {
192 strcpy(name, shortname);
193 strcat(name, type);
Lukasz Majewskib528f712013-03-05 12:10:17 +0100194 } else if (!strcmp(type, "ums")) {
195 strcpy(name, shortname);
196 strcat(name, type);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200197 } else {
198 printf("%s: unknown command: %s\n", __func__, type);
199 return -EINVAL;
200 }
201
202 g_dnl_driver.name = name;
203
204 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
205 ret = usb_composite_register(&g_dnl_driver);
206
207 if (ret) {
208 printf("%s: failed!, error: %d\n", __func__, ret);
209 return ret;
210 }
211
212 return 0;
213}
214
215void g_dnl_unregister(void)
216{
217 usb_composite_unregister(&g_dnl_driver);
218}