blob: 0491a0eea941902bb6cf494dccb9e8f64d6eae20 [file] [log] [blame]
Lukasz Majewskic574de52012-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 Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskic574de52012-08-06 14:41:05 +02008 */
9
Lukasz Majewskic574de52012-08-06 14:41:05 +020010#include <common.h>
11#include <malloc.h>
12
13#include <mmc.h>
14#include <part.h>
Paul Kocialkowski46f97292015-06-12 19:57:01 +020015#include <usb.h>
Lukasz Majewskic574de52012-08-06 14:41:05 +020016
17#include <g_dnl.h>
Lukasz Majewski0c635232013-09-17 15:58:21 +020018#include <usb_mass_storage.h>
Lukasz Majewskic4219462013-09-17 15:58:23 +020019#include <dfu.h>
Lukasz Majewski1c1af4f2013-10-08 14:30:43 +020020#include <thor.h>
Lukasz Majewskic574de52012-08-06 14:41:05 +020021
22#include "gadget_chips.h"
23#include "composite.c"
24
25/*
26 * One needs to define the following:
27 * CONFIG_G_DNL_VENDOR_NUM
28 * CONFIG_G_DNL_PRODUCT_NUM
29 * CONFIG_G_DNL_MANUFACTURER
Sam Protsenkob706ffd2016-04-13 14:20:30 +030030 * at e.g. ./configs/<board>_defconfig
Lukasz Majewskic574de52012-08-06 14:41:05 +020031 */
32
33#define STRING_MANUFACTURER 25
34#define STRING_PRODUCT 2
Lukasz Majewski0d9752e2013-08-19 17:17:18 +020035/* Index of String Descriptor describing this configuration */
Lukasz Majewskic574de52012-08-06 14:41:05 +020036#define STRING_USBDOWN 2
Heiko Schocher1799fa82013-11-04 14:05:01 +010037/* Index of String serial */
38#define STRING_SERIAL 3
Felipe Balbi02ae1292017-02-22 17:12:39 +020039#define MAX_STRING_SERIAL 256
Lukasz Majewski0d9752e2013-08-19 17:17:18 +020040/* Number of supported configurations */
41#define CONFIGURATION_NUMBER 1
Lukasz Majewskic574de52012-08-06 14:41:05 +020042
43#define DRIVER_VERSION "usb_dnl 2.0"
44
Lukasz Majewskic574de52012-08-06 14:41:05 +020045static const char product[] = "USB download gadget";
Heiko Schocher1799fa82013-11-04 14:05:01 +010046static char g_dnl_serial[MAX_STRING_SERIAL];
Lukasz Majewskic574de52012-08-06 14:41:05 +020047static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
48
Heiko Schocher1799fa82013-11-04 14:05:01 +010049void g_dnl_set_serialnumber(char *s)
50{
51 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
Felipe Balbia4b305a2017-02-22 11:22:37 +020052 strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
Heiko Schocher1799fa82013-11-04 14:05:01 +010053}
54
Lukasz Majewskic574de52012-08-06 14:41:05 +020055static struct usb_device_descriptor device_desc = {
56 .bLength = sizeof device_desc,
57 .bDescriptorType = USB_DT_DEVICE,
58
59 .bcdUSB = __constant_cpu_to_le16(0x0200),
John Tobias83ba2e72016-04-07 16:49:03 -070060 .bDeviceClass = USB_CLASS_PER_INTERFACE,
61 .bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/
Lukasz Majewskic574de52012-08-06 14:41:05 +020062
63 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
64 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
Felipe Balbic891ca92017-02-22 17:12:41 +020065 /* .iProduct = DYNAMIC */
66 /* .iSerialNumber = DYNAMIC */
Lukasz Majewskic574de52012-08-06 14:41:05 +020067 .bNumConfigurations = 1,
68};
69
Lukasz Majewskidbbaef72013-08-19 17:17:19 +020070/*
71 * static strings, in UTF-8
72 * IDs for those strings are assigned dynamically at g_dnl_bind()
73 */
Lukasz Majewskic574de52012-08-06 14:41:05 +020074static struct usb_string g_dnl_string_defs[] = {
Lukasz Majewskidbbaef72013-08-19 17:17:19 +020075 {.s = manufacturer},
76 {.s = product},
Heiko Schocher1799fa82013-11-04 14:05:01 +010077 {.s = g_dnl_serial},
Lukasz Majewskidbbaef72013-08-19 17:17:19 +020078 { } /* end of list */
Lukasz Majewskic574de52012-08-06 14:41:05 +020079};
80
81static struct usb_gadget_strings g_dnl_string_tab = {
82 .language = 0x0409, /* en-us */
83 .strings = g_dnl_string_defs,
84};
85
86static struct usb_gadget_strings *g_dnl_composite_strings[] = {
87 &g_dnl_string_tab,
88 NULL,
89};
90
91static int g_dnl_unbind(struct usb_composite_dev *cdev)
92{
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +000093 struct usb_gadget *gadget = cdev->gadget;
94
95 debug("%s: calling usb_gadget_disconnect for "
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +020096 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +000097 usb_gadget_disconnect(gadget);
98
Lukasz Majewskic574de52012-08-06 14:41:05 +020099 return 0;
100}
101
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200102static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
103{
104 return ll_entry_start(struct g_dnl_bind_callback,
105 g_dnl_bind_callbacks);
106}
107
108static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
109{
110 return ll_entry_end(struct g_dnl_bind_callback,
111 g_dnl_bind_callbacks);
112}
113
Lukasz Majewskic574de52012-08-06 14:41:05 +0200114static int g_dnl_do_config(struct usb_configuration *c)
115{
116 const char *s = c->cdev->driver->name;
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200117 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
Lukasz Majewskic574de52012-08-06 14:41:05 +0200118
119 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
120 __func__, c, c->cdev);
121
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200122 for (; callback != g_dnl_bind_callback_end(); callback++)
123 if (!strcmp(s, callback->usb_function_name))
124 return callback->fptr(c);
125 return -ENODEV;
Lukasz Majewskic574de52012-08-06 14:41:05 +0200126}
127
128static int g_dnl_config_register(struct usb_composite_dev *cdev)
129{
Lukasz Majewski8143f222013-10-08 14:30:40 +0200130 struct usb_configuration *config;
131 const char *name = "usb_dnload";
Lukasz Majewskic574de52012-08-06 14:41:05 +0200132
Lukasz Majewski8143f222013-10-08 14:30:40 +0200133 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
134 if (!config)
135 return -ENOMEM;
136
137 memset(config, 0, sizeof(*config));
138
139 config->label = name;
140 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
141 config->bConfigurationValue = CONFIGURATION_NUMBER;
142 config->iConfiguration = STRING_USBDOWN;
143 config->bind = g_dnl_do_config;
Lukasz Majewskic574de52012-08-06 14:41:05 +0200144
Lukasz Majewski8143f222013-10-08 14:30:40 +0200145 return usb_add_config(cdev, config);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200146}
147
Heiko Schocher07a12ea2013-06-04 11:19:50 +0200148__weak
Paul Kocialkowski46f97292015-06-12 19:57:01 +0200149int board_usb_init(int index, enum usb_init_type init)
150{
151 return 0;
152}
153
154__weak
155int board_usb_cleanup(int index, enum usb_init_type init)
156{
157 return 0;
158}
159
160__weak
Lukasz Majewski44b5b382013-10-08 14:30:41 +0200161int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocher07a12ea2013-06-04 11:19:50 +0200162{
163 return 0;
164}
165
Heiko Schocher6f464a52013-11-04 14:05:02 +0100166__weak int g_dnl_get_board_bcd_device_number(int gcnum)
167{
168 return gcnum;
169}
170
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200171__weak int g_dnl_board_usb_cable_connected(void)
172{
173 return -EOPNOTSUPP;
174}
175
Rob Herringd38f24e2014-12-10 14:43:03 -0600176static bool g_dnl_detach_request;
177
178bool g_dnl_detach(void)
179{
180 return g_dnl_detach_request;
181}
182
183void g_dnl_trigger_detach(void)
184{
185 g_dnl_detach_request = true;
186}
187
188void g_dnl_clear_detach(void)
189{
190 g_dnl_detach_request = false;
191}
192
Heiko Schocher6f464a52013-11-04 14:05:02 +0100193static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
194{
195 struct usb_gadget *gadget = cdev->gadget;
196 int gcnum;
197
198 gcnum = usb_gadget_controller_number(gadget);
199 if (gcnum > 0)
200 gcnum += 0x200;
201
202 return g_dnl_get_board_bcd_device_number(gcnum);
203}
204
Lukasz Majewskic574de52012-08-06 14:41:05 +0200205static int g_dnl_bind(struct usb_composite_dev *cdev)
206{
207 struct usb_gadget *gadget = cdev->gadget;
208 int id, ret;
209 int gcnum;
210
211 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
212
213 id = usb_string_id(cdev);
214
215 if (id < 0)
216 return id;
217 g_dnl_string_defs[0].id = id;
218 device_desc.iManufacturer = id;
219
220 id = usb_string_id(cdev);
221 if (id < 0)
222 return id;
223
224 g_dnl_string_defs[1].id = id;
225 device_desc.iProduct = id;
226
Lukasz Majewskif32333b2017-06-26 13:04:58 +0200227 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
228
Felipe Balbi169dd122017-02-22 17:12:40 +0200229 if (strlen(g_dnl_serial)) {
230 id = usb_string_id(cdev);
231 if (id < 0)
232 return id;
Heiko Schocher1799fa82013-11-04 14:05:01 +0100233
Felipe Balbi169dd122017-02-22 17:12:40 +0200234 g_dnl_string_defs[2].id = id;
235 device_desc.iSerialNumber = id;
236 }
Heiko Schocher1799fa82013-11-04 14:05:01 +0100237
Lukasz Majewskic574de52012-08-06 14:41:05 +0200238 ret = g_dnl_config_register(cdev);
239 if (ret)
240 goto error;
241
Heiko Schocher6f464a52013-11-04 14:05:02 +0100242 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200243 if (gcnum >= 0)
Heiko Schocher6f464a52013-11-04 14:05:02 +0100244 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200245 else {
246 debug("%s: controller '%s' not recognized\n",
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200247 __func__, gadget->name);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200248 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
249 }
250
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +0000251 debug("%s: calling usb_gadget_connect for "
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200252 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +0000253 usb_gadget_connect(gadget);
254
Lukasz Majewskic574de52012-08-06 14:41:05 +0200255 return 0;
256
257 error:
258 g_dnl_unbind(cdev);
259 return -ENOMEM;
260}
261
262static struct usb_composite_driver g_dnl_driver = {
263 .name = NULL,
264 .dev = &device_desc,
265 .strings = g_dnl_composite_strings,
266
267 .bind = g_dnl_bind,
268 .unbind = g_dnl_unbind,
269};
270
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200271/*
272 * NOTICE:
273 * Registering via USB function name won't be necessary after rewriting
274 * g_dnl to support multiple USB functions.
275 */
276int g_dnl_register(const char *name)
Lukasz Majewskic574de52012-08-06 14:41:05 +0200277{
Stephen Warrene239fa52014-05-01 15:45:16 -0600278 int ret;
Lukasz Majewskic574de52012-08-06 14:41:05 +0200279
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200280 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200281 g_dnl_driver.name = name;
282
Stephen Warrene239fa52014-05-01 15:45:16 -0600283 ret = usb_composite_register(&g_dnl_driver);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200284 if (ret) {
285 printf("%s: failed!, error: %d\n", __func__, ret);
286 return ret;
287 }
Lukasz Majewskic574de52012-08-06 14:41:05 +0200288 return 0;
289}
290
291void g_dnl_unregister(void)
292{
293 usb_composite_unregister(&g_dnl_driver);
294}