blob: afb7b74f3057047f21e580fca4808c8302247551 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski1d4a0b62012-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 Majewski1d4a0b62012-08-06 14:41:05 +02007 */
8
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +02009#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020011#include <malloc.h>
12
13#include <mmc.h>
14#include <part.h>
Paul Kocialkowski99fc2222015-06-12 19:57:01 +020015#include <usb.h>
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020016
17#include <g_dnl.h>
Lukasz Majewskiba4e95c2013-09-17 15:58:21 +020018#include <usb_mass_storage.h>
Lukasz Majewskia6921ad2013-09-17 15:58:23 +020019#include <dfu.h>
Lukasz Majewskib958fb92013-10-08 14:30:43 +020020#include <thor.h>
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020021
Sam Protsenkode4e4ed2017-09-01 15:42:03 +030022#include <env_callback.h>
23
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020024#include "gadget_chips.h"
25#include "composite.c"
26
27/*
28 * One needs to define the following:
Maxime Riparda95aee62017-09-07 08:58:08 +020029 * CONFIG_USB_GADGET_VENDOR_NUM
30 * CONFIG_USB_GADGET_PRODUCT_NUM
31 * CONFIG_USB_GADGET_MANUFACTURER
Sam Protsenkoe6c0bc02016-04-13 14:20:30 +030032 * at e.g. ./configs/<board>_defconfig
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020033 */
34
35#define STRING_MANUFACTURER 25
36#define STRING_PRODUCT 2
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020037/* Index of String Descriptor describing this configuration */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020038#define STRING_USBDOWN 2
Heiko Schocherec9002e2013-11-04 14:05:01 +010039/* Index of String serial */
40#define STRING_SERIAL 3
Felipe Balbi12d0b8f2017-02-22 17:12:39 +020041#define MAX_STRING_SERIAL 256
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020042/* Number of supported configurations */
43#define CONFIGURATION_NUMBER 1
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020044
45#define DRIVER_VERSION "usb_dnl 2.0"
46
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020047static const char product[] = "USB download gadget";
Heiko Schocherec9002e2013-11-04 14:05:01 +010048static char g_dnl_serial[MAX_STRING_SERIAL];
Maxime Riparda95aee62017-09-07 08:58:08 +020049static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020050
Heiko Schocherec9002e2013-11-04 14:05:01 +010051void g_dnl_set_serialnumber(char *s)
52{
53 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
Felipe Balbi949bf792017-02-22 11:22:37 +020054 strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
Heiko Schocherec9002e2013-11-04 14:05:01 +010055}
56
Lukasz Majewski1d4a0b62012-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 Tobias5b718402016-04-07 16:49:03 -070062 .bDeviceClass = USB_CLASS_PER_INTERFACE,
63 .bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020064
Maxime Riparda95aee62017-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 Balbi207835b2017-02-22 17:12:41 +020067 /* .iProduct = DYNAMIC */
68 /* .iSerialNumber = DYNAMIC */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020069 .bNumConfigurations = 1,
70};
71
Lukasz Majewskic4219a82013-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 Majewski1d4a0b62012-08-06 14:41:05 +020076static struct usb_string g_dnl_string_defs[] = {
Lukasz Majewskic4219a82013-08-19 17:17:19 +020077 {.s = manufacturer},
78 {.s = product},
Heiko Schocherec9002e2013-11-04 14:05:01 +010079 {.s = g_dnl_serial},
Lukasz Majewskic4219a82013-08-19 17:17:19 +020080 { } /* end of list */
Lukasz Majewski1d4a0b62012-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 Delaunaybcd677f2020-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 Majewski1d4a0b62012-08-06 14:41:05 +0200101static int g_dnl_unbind(struct usb_composite_dev *cdev)
102{
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000103 struct usb_gadget *gadget = cdev->gadget;
104
105 debug("%s: calling usb_gadget_disconnect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200106 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000107 usb_gadget_disconnect(gadget);
108
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200109 return 0;
110}
111
Mateusz Zalegac4d0e852014-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 Majewski1d4a0b62012-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 Zalegac4d0e852014-04-28 21:13:28 +0200127 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200128
129 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
130 __func__, c, c->cdev);
131
Mateusz Zalegac4d0e852014-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 Majewski1d4a0b62012-08-06 14:41:05 +0200136}
137
138static int g_dnl_config_register(struct usb_composite_dev *cdev)
139{
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200140 struct usb_configuration *config;
141 const char *name = "usb_dnload";
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200142
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200143 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
144 if (!config)
145 return -ENOMEM;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200146
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200147 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;
154
155 return usb_add_config(cdev, config);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200156}
157
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200158__weak
Paul Kocialkowski99fc2222015-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 Majewskid6eae7b2013-10-08 14:30:41 +0200171int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200172{
173 return 0;
174}
175
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100176__weak int g_dnl_get_board_bcd_device_number(int gcnum)
177{
178 return gcnum;
179}
180
Mateusz Zalega75504e92014-04-30 13:07:48 +0200181__weak int g_dnl_board_usb_cable_connected(void)
182{
183 return -EOPNOTSUPP;
184}
185
Rob Herringfe1b28c2014-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 Schocher7a0d4632013-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}
214
Sam Protsenkode4e4ed2017-09-01 15:42:03 +0300215/**
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);
227
Lukasz Majewski1d4a0b62012-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 Majewski2a0583e2017-06-26 13:04:58 +0200250 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
251
Felipe Balbi842778a2017-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 Schocherec9002e2013-11-04 14:05:01 +0100256
Felipe Balbi842778a2017-02-22 17:12:40 +0200257 g_dnl_string_defs[2].id = id;
258 device_desc.iSerialNumber = id;
259 }
Heiko Schocherec9002e2013-11-04 14:05:01 +0100260
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200261 ret = g_dnl_config_register(cdev);
262 if (ret)
263 goto error;
264
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100265 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200266 if (gcnum >= 0)
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100267 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200268 else {
269 debug("%s: controller '%s' not recognized\n",
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200270 __func__, gadget->name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200271 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
272 }
273
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000274 debug("%s: calling usb_gadget_connect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200275 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000276 usb_gadget_connect(gadget);
277
Lukasz Majewski1d4a0b62012-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,
Li Jun429e2f22021-01-25 21:43:56 +0800289 .max_speed = USB_SPEED_SUPER,
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200290
291 .bind = g_dnl_bind,
292 .unbind = g_dnl_unbind,
293};
294
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200295/*
296 * NOTICE:
297 * Registering via USB function name won't be necessary after rewriting
298 * g_dnl to support multiple USB functions.
299 */
300int g_dnl_register(const char *name)
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200301{
Stephen Warren25fbf962014-05-01 15:45:16 -0600302 int ret;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200303
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200304 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200305 g_dnl_driver.name = name;
306
Stephen Warren25fbf962014-05-01 15:45:16 -0600307 ret = usb_composite_register(&g_dnl_driver);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200308 if (ret) {
309 printf("%s: failed!, error: %d\n", __func__, ret);
310 return ret;
311 }
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200312 return 0;
313}
314
315void g_dnl_unregister(void)
316{
317 usb_composite_unregister(&g_dnl_driver);
318}