blob: 973d7378b52a05bf79d47f45286e90624e82c697 [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
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020010#include <common.h>
11#include <malloc.h>
12
13#include <mmc.h>
14#include <part.h>
15
16#include <g_dnl.h>
Lukasz Majewskiba4e95c2013-09-17 15:58:21 +020017#include <usb_mass_storage.h>
Lukasz Majewskia6921ad2013-09-17 15:58:23 +020018#include <dfu.h>
Lukasz Majewskib958fb92013-10-08 14:30:43 +020019#include <thor.h>
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020020
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 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
Heiko Schocherec9002e2013-11-04 14:05:01 +010036/* Index of String serial */
37#define STRING_SERIAL 3
38#define MAX_STRING_SERIAL 32
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020039/* Number of supported configurations */
40#define CONFIGURATION_NUMBER 1
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020041
42#define DRIVER_VERSION "usb_dnl 2.0"
43
44static const char shortname[] = "usb_dnl_";
45static const char product[] = "USB download gadget";
Heiko Schocherec9002e2013-11-04 14:05:01 +010046static char g_dnl_serial[MAX_STRING_SERIAL];
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020047static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
48
Heiko Schocherec9002e2013-11-04 14:05:01 +010049void 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 Majewski1d4a0b62012-08-06 14:41:05 +020056static 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 Schocherec9002e2013-11-04 14:05:01 +010067 .iSerialNumber = STRING_SERIAL,
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020068 .bNumConfigurations = 1,
69};
70
Lukasz Majewskic4219a82013-08-19 17:17:19 +020071/*
72 * static strings, in UTF-8
73 * IDs for those strings are assigned dynamically at g_dnl_bind()
74 */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020075static struct usb_string g_dnl_string_defs[] = {
Lukasz Majewskic4219a82013-08-19 17:17:19 +020076 {.s = manufacturer},
77 {.s = product},
Heiko Schocherec9002e2013-11-04 14:05:01 +010078 {.s = g_dnl_serial},
Lukasz Majewskic4219a82013-08-19 17:17:19 +020079 { } /* end of list */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020080};
81
82static struct usb_gadget_strings g_dnl_string_tab = {
83 .language = 0x0409, /* en-us */
84 .strings = g_dnl_string_defs,
85};
86
87static struct usb_gadget_strings *g_dnl_composite_strings[] = {
88 &g_dnl_string_tab,
89 NULL,
90};
91
92static int g_dnl_unbind(struct usb_composite_dev *cdev)
93{
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000094 struct usb_gadget *gadget = cdev->gadget;
95
Lukasz Majewski7b412ab2013-10-08 14:30:40 +020096 free(cdev->config);
97 cdev->config = NULL;
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000098 debug("%s: calling usb_gadget_disconnect for "
99 "controller '%s'\n", shortname, gadget->name);
100 usb_gadget_disconnect(gadget);
101
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200102 return 0;
103}
104
105static 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 Majewskib528f712013-03-05 12:10:17 +0100116 else if (!strcmp(s, "usb_dnl_ums"))
117 ret = fsg_add(c);
Lukasz Majewskib958fb92013-10-08 14:30:43 +0200118 else if (!strcmp(s, "usb_dnl_thor"))
119 ret = thor_add(c);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200120
121 return ret;
122}
123
124static int g_dnl_config_register(struct usb_composite_dev *cdev)
125{
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200126 struct usb_configuration *config;
127 const char *name = "usb_dnload";
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200128
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200129 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
130 if (!config)
131 return -ENOMEM;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200132
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200133 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 Majewski1d4a0b62012-08-06 14:41:05 +0200142}
143
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200144__weak
Lukasz Majewskid6eae7b2013-10-08 14:30:41 +0200145int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200146{
147 return 0;
148}
149
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100150__weak int g_dnl_get_board_bcd_device_number(int gcnum)
151{
152 return gcnum;
153}
154
Mateusz Zalega75504e92014-04-30 13:07:48 +0200155__weak int g_dnl_board_usb_cable_connected(void)
156{
157 return -EOPNOTSUPP;
158}
159
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100160static 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 Majewski1d4a0b62012-08-06 14:41:05 +0200172static 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 Schocherec9002e2013-11-04 14:05:01 +0100194 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 Majewskid6eae7b2013-10-08 14:30:41 +0200201 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200202 ret = g_dnl_config_register(cdev);
203 if (ret)
204 goto error;
205
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100206 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200207 if (gcnum >= 0)
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100208 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200209 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 Antoniou5a413ca2012-11-30 08:01:05 +0000215 debug("%s: calling usb_gadget_connect for "
216 "controller '%s'\n", shortname, gadget->name);
217 usb_gadget_connect(gadget);
218
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200219 return 0;
220
221 error:
222 g_dnl_unbind(cdev);
223 return -ENOMEM;
224}
225
226static 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
235int g_dnl_register(const char *type)
236{
Lukasz Majewskib958fb92013-10-08 14:30:43 +0200237 /* The largest function name is 4 */
238 static char name[sizeof(shortname) + 4];
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200239 int ret;
240
241 if (!strcmp(type, "dfu")) {
242 strcpy(name, shortname);
243 strcat(name, type);
Lukasz Majewskib528f712013-03-05 12:10:17 +0100244 } else if (!strcmp(type, "ums")) {
245 strcpy(name, shortname);
246 strcat(name, type);
Lukasz Majewskib958fb92013-10-08 14:30:43 +0200247 } else if (!strcmp(type, "thor")) {
248 strcpy(name, shortname);
249 strcat(name, type);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200250 } 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
268void g_dnl_unregister(void)
269{
270 usb_composite_unregister(&g_dnl_driver);
271}