blob: 86fdd16b012576dbe409a621b31989285a6885a8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskic574de52012-08-06 14:41:05 +02002/*
3 * g_dnl.c -- USB Downloader Gadget
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewskic574de52012-08-06 14:41:05 +02007 */
8
Lukasz Majewskic574de52012-08-06 14:41:05 +02009#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Lukasz Majewskic574de52012-08-06 14:41:05 +020011#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
Sam Protsenko708bc682017-09-01 15:42:03 +030022#include <env_callback.h>
23
Lukasz Majewskic574de52012-08-06 14:41:05 +020024#include "gadget_chips.h"
25#include "composite.c"
26
27/*
28 * One needs to define the following:
Maxime Ripard7f78b9d2017-09-07 08:58:08 +020029 * CONFIG_USB_GADGET_VENDOR_NUM
30 * CONFIG_USB_GADGET_PRODUCT_NUM
31 * CONFIG_USB_GADGET_MANUFACTURER
Sam Protsenkob706ffd2016-04-13 14:20:30 +030032 * at e.g. ./configs/<board>_defconfig
Lukasz Majewskic574de52012-08-06 14:41:05 +020033 */
34
35#define STRING_MANUFACTURER 25
36#define STRING_PRODUCT 2
Lukasz Majewski0d9752e2013-08-19 17:17:18 +020037/* Index of String Descriptor describing this configuration */
Lukasz Majewskic574de52012-08-06 14:41:05 +020038#define STRING_USBDOWN 2
Heiko Schocher1799fa82013-11-04 14:05:01 +010039/* Index of String serial */
40#define STRING_SERIAL 3
Felipe Balbi02ae1292017-02-22 17:12:39 +020041#define MAX_STRING_SERIAL 256
Lukasz Majewski0d9752e2013-08-19 17:17:18 +020042/* Number of supported configurations */
43#define CONFIGURATION_NUMBER 1
Lukasz Majewskic574de52012-08-06 14:41:05 +020044
45#define DRIVER_VERSION "usb_dnl 2.0"
46
Lukasz Majewskic574de52012-08-06 14:41:05 +020047static const char product[] = "USB download gadget";
Heiko Schocher1799fa82013-11-04 14:05:01 +010048static char g_dnl_serial[MAX_STRING_SERIAL];
Maxime Ripard7f78b9d2017-09-07 08:58:08 +020049static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER;
Lukasz Majewskic574de52012-08-06 14:41:05 +020050
Heiko Schocher1799fa82013-11-04 14:05:01 +010051void g_dnl_set_serialnumber(char *s)
52{
53 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
Felipe Balbia4b305a2017-02-22 11:22:37 +020054 strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
Heiko Schocher1799fa82013-11-04 14:05:01 +010055}
56
Lukasz Majewskic574de52012-08-06 14:41:05 +020057static struct usb_device_descriptor device_desc = {
58 .bLength = sizeof device_desc,
59 .bDescriptorType = USB_DT_DEVICE,
60
61 .bcdUSB = __constant_cpu_to_le16(0x0200),
John Tobias83ba2e72016-04-07 16:49:03 -070062 .bDeviceClass = USB_CLASS_PER_INTERFACE,
63 .bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/
Lukasz Majewskic574de52012-08-06 14:41:05 +020064
Maxime Ripard7f78b9d2017-09-07 08:58:08 +020065 .idVendor = __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM),
66 .idProduct = __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM),
Felipe Balbic891ca92017-02-22 17:12:41 +020067 /* .iProduct = DYNAMIC */
68 /* .iSerialNumber = DYNAMIC */
Lukasz Majewskic574de52012-08-06 14:41:05 +020069 .bNumConfigurations = 1,
70};
71
Lukasz Majewskidbbaef72013-08-19 17:17:19 +020072/*
73 * static strings, in UTF-8
74 * IDs for those strings are assigned dynamically at g_dnl_bind()
75 */
Lukasz Majewskic574de52012-08-06 14:41:05 +020076static struct usb_string g_dnl_string_defs[] = {
Lukasz Majewskidbbaef72013-08-19 17:17:19 +020077 {.s = manufacturer},
78 {.s = product},
Heiko Schocher1799fa82013-11-04 14:05:01 +010079 {.s = g_dnl_serial},
Lukasz Majewskidbbaef72013-08-19 17:17:19 +020080 { } /* end of list */
Lukasz Majewskic574de52012-08-06 14:41:05 +020081};
82
83static struct usb_gadget_strings g_dnl_string_tab = {
84 .language = 0x0409, /* en-us */
85 .strings = g_dnl_string_defs,
86};
87
88static struct usb_gadget_strings *g_dnl_composite_strings[] = {
89 &g_dnl_string_tab,
90 NULL,
91};
92
Patrick Delaunay6dbadd32020-03-18 09:24:46 +010093void g_dnl_set_product(const char *s)
94{
95 if (s)
96 g_dnl_string_defs[1].s = s;
97 else
98 g_dnl_string_defs[1].s = product;
99}
100
Lukasz Majewskic574de52012-08-06 14:41:05 +0200101static int g_dnl_unbind(struct usb_composite_dev *cdev)
102{
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +0000103 struct usb_gadget *gadget = cdev->gadget;
104
105 debug("%s: calling usb_gadget_disconnect for "
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200106 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +0000107 usb_gadget_disconnect(gadget);
108
Lukasz Majewskic574de52012-08-06 14:41:05 +0200109 return 0;
110}
111
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200112static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
113{
114 return ll_entry_start(struct g_dnl_bind_callback,
115 g_dnl_bind_callbacks);
116}
117
118static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
119{
120 return ll_entry_end(struct g_dnl_bind_callback,
121 g_dnl_bind_callbacks);
122}
123
Lukasz Majewskic574de52012-08-06 14:41:05 +0200124static int g_dnl_do_config(struct usb_configuration *c)
125{
126 const char *s = c->cdev->driver->name;
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200127 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
Lukasz Majewskic574de52012-08-06 14:41:05 +0200128
129 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
130 __func__, c, c->cdev);
131
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200132 for (; callback != g_dnl_bind_callback_end(); callback++)
133 if (!strcmp(s, callback->usb_function_name))
134 return callback->fptr(c);
135 return -ENODEV;
Lukasz Majewskic574de52012-08-06 14:41:05 +0200136}
137
138static int g_dnl_config_register(struct usb_composite_dev *cdev)
139{
Lukasz Majewski8143f222013-10-08 14:30:40 +0200140 struct usb_configuration *config;
141 const char *name = "usb_dnload";
Lukasz Majewskic574de52012-08-06 14:41:05 +0200142
Lukasz Majewski8143f222013-10-08 14:30:40 +0200143 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
144 if (!config)
145 return -ENOMEM;
146
147 memset(config, 0, sizeof(*config));
148
149 config->label = name;
150 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
151 config->bConfigurationValue = CONFIGURATION_NUMBER;
152 config->iConfiguration = STRING_USBDOWN;
153 config->bind = g_dnl_do_config;
Lukasz Majewskic574de52012-08-06 14:41:05 +0200154
Lukasz Majewski8143f222013-10-08 14:30:40 +0200155 return usb_add_config(cdev, config);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200156}
157
Heiko Schocher07a12ea2013-06-04 11:19:50 +0200158__weak
Paul Kocialkowski46f97292015-06-12 19:57:01 +0200159int board_usb_init(int index, enum usb_init_type init)
160{
161 return 0;
162}
163
164__weak
165int board_usb_cleanup(int index, enum usb_init_type init)
166{
167 return 0;
168}
169
170__weak
Lukasz Majewski44b5b382013-10-08 14:30:41 +0200171int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocher07a12ea2013-06-04 11:19:50 +0200172{
173 return 0;
174}
175
Heiko Schocher6f464a52013-11-04 14:05:02 +0100176__weak int g_dnl_get_board_bcd_device_number(int gcnum)
177{
178 return gcnum;
179}
180
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200181__weak int g_dnl_board_usb_cable_connected(void)
182{
183 return -EOPNOTSUPP;
184}
185
Rob Herringd38f24e2014-12-10 14:43:03 -0600186static bool g_dnl_detach_request;
187
188bool g_dnl_detach(void)
189{
190 return g_dnl_detach_request;
191}
192
193void g_dnl_trigger_detach(void)
194{
195 g_dnl_detach_request = true;
196}
197
198void g_dnl_clear_detach(void)
199{
200 g_dnl_detach_request = false;
201}
202
Heiko Schocher6f464a52013-11-04 14:05:02 +0100203static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
204{
205 struct usb_gadget *gadget = cdev->gadget;
206 int gcnum;
207
208 gcnum = usb_gadget_controller_number(gadget);
209 if (gcnum > 0)
210 gcnum += 0x200;
211
212 return g_dnl_get_board_bcd_device_number(gcnum);
213}
Sam Protsenko708bc682017-09-01 15:42:03 +0300214
215/**
216 * Update internal serial number variable when the "serial#" env var changes.
217 *
218 * Handle all cases, even when flags == H_PROGRAMMATIC or op == env_op_delete.
219 */
220static int on_serialno(const char *name, const char *value, enum env_op op,
221 int flags)
222{
223 g_dnl_set_serialnumber((char *)value);
224 return 0;
225}
226U_BOOT_ENV_CALLBACK(serialno, on_serialno);
Heiko Schocher6f464a52013-11-04 14:05:02 +0100227
Lukasz Majewskic574de52012-08-06 14:41:05 +0200228static int g_dnl_bind(struct usb_composite_dev *cdev)
229{
230 struct usb_gadget *gadget = cdev->gadget;
231 int id, ret;
232 int gcnum;
233
234 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
235
236 id = usb_string_id(cdev);
237
238 if (id < 0)
239 return id;
240 g_dnl_string_defs[0].id = id;
241 device_desc.iManufacturer = id;
242
243 id = usb_string_id(cdev);
244 if (id < 0)
245 return id;
246
247 g_dnl_string_defs[1].id = id;
248 device_desc.iProduct = id;
249
Lukasz Majewskif32333b2017-06-26 13:04:58 +0200250 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
251
Felipe Balbi169dd122017-02-22 17:12:40 +0200252 if (strlen(g_dnl_serial)) {
253 id = usb_string_id(cdev);
254 if (id < 0)
255 return id;
Heiko Schocher1799fa82013-11-04 14:05:01 +0100256
Felipe Balbi169dd122017-02-22 17:12:40 +0200257 g_dnl_string_defs[2].id = id;
258 device_desc.iSerialNumber = id;
259 }
Heiko Schocher1799fa82013-11-04 14:05:01 +0100260
Lukasz Majewskic574de52012-08-06 14:41:05 +0200261 ret = g_dnl_config_register(cdev);
262 if (ret)
263 goto error;
264
Heiko Schocher6f464a52013-11-04 14:05:02 +0100265 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200266 if (gcnum >= 0)
Heiko Schocher6f464a52013-11-04 14:05:02 +0100267 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200268 else {
269 debug("%s: controller '%s' not recognized\n",
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200270 __func__, gadget->name);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200271 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
272 }
273
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +0000274 debug("%s: calling usb_gadget_connect for "
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200275 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniouc29d2be2012-11-30 08:01:05 +0000276 usb_gadget_connect(gadget);
277
Lukasz Majewskic574de52012-08-06 14:41:05 +0200278 return 0;
279
280 error:
281 g_dnl_unbind(cdev);
282 return -ENOMEM;
283}
284
285static struct usb_composite_driver g_dnl_driver = {
286 .name = NULL,
287 .dev = &device_desc,
288 .strings = g_dnl_composite_strings,
289
290 .bind = g_dnl_bind,
291 .unbind = g_dnl_unbind,
292};
293
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200294/*
295 * NOTICE:
296 * Registering via USB function name won't be necessary after rewriting
297 * g_dnl to support multiple USB functions.
298 */
299int g_dnl_register(const char *name)
Lukasz Majewskic574de52012-08-06 14:41:05 +0200300{
Stephen Warrene239fa52014-05-01 15:45:16 -0600301 int ret;
Lukasz Majewskic574de52012-08-06 14:41:05 +0200302
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200303 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200304 g_dnl_driver.name = name;
305
Stephen Warrene239fa52014-05-01 15:45:16 -0600306 ret = usb_composite_register(&g_dnl_driver);
Lukasz Majewskic574de52012-08-06 14:41:05 +0200307 if (ret) {
308 printf("%s: failed!, error: %d\n", __func__, ret);
309 return ret;
310 }
Lukasz Majewskic574de52012-08-06 14:41:05 +0200311 return 0;
312}
313
314void g_dnl_unregister(void)
315{
316 usb_composite_unregister(&g_dnl_driver);
317}