blob: e9e1600a1a506bf6c252cbcc969863199b92d43d [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>
10#include <malloc.h>
11
12#include <mmc.h>
13#include <part.h>
Paul Kocialkowski99fc2222015-06-12 19:57:01 +020014#include <usb.h>
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020015
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
Sam Protsenkode4e4ed2017-09-01 15:42:03 +030021#include <env_callback.h>
22
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020023#include "gadget_chips.h"
24#include "composite.c"
25
26/*
27 * One needs to define the following:
Maxime Riparda95aee62017-09-07 08:58:08 +020028 * CONFIG_USB_GADGET_VENDOR_NUM
29 * CONFIG_USB_GADGET_PRODUCT_NUM
30 * CONFIG_USB_GADGET_MANUFACTURER
Sam Protsenkoe6c0bc02016-04-13 14:20:30 +030031 * at e.g. ./configs/<board>_defconfig
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020032 */
33
34#define STRING_MANUFACTURER 25
35#define STRING_PRODUCT 2
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020036/* Index of String Descriptor describing this configuration */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020037#define STRING_USBDOWN 2
Heiko Schocherec9002e2013-11-04 14:05:01 +010038/* Index of String serial */
39#define STRING_SERIAL 3
Felipe Balbi12d0b8f2017-02-22 17:12:39 +020040#define MAX_STRING_SERIAL 256
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020041/* Number of supported configurations */
42#define CONFIGURATION_NUMBER 1
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020043
44#define DRIVER_VERSION "usb_dnl 2.0"
45
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020046static const char product[] = "USB download gadget";
Heiko Schocherec9002e2013-11-04 14:05:01 +010047static char g_dnl_serial[MAX_STRING_SERIAL];
Maxime Riparda95aee62017-09-07 08:58:08 +020048static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020049
Heiko Schocherec9002e2013-11-04 14:05:01 +010050void g_dnl_set_serialnumber(char *s)
51{
52 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
Felipe Balbi949bf792017-02-22 11:22:37 +020053 strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
Heiko Schocherec9002e2013-11-04 14:05:01 +010054}
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),
John Tobias5b718402016-04-07 16:49:03 -070061 .bDeviceClass = USB_CLASS_PER_INTERFACE,
62 .bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020063
Maxime Riparda95aee62017-09-07 08:58:08 +020064 .idVendor = __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM),
65 .idProduct = __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM),
Felipe Balbi207835b2017-02-22 17:12:41 +020066 /* .iProduct = DYNAMIC */
67 /* .iSerialNumber = DYNAMIC */
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
96 debug("%s: calling usb_gadget_disconnect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +020097 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000098 usb_gadget_disconnect(gadget);
99
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200100 return 0;
101}
102
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200103static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
104{
105 return ll_entry_start(struct g_dnl_bind_callback,
106 g_dnl_bind_callbacks);
107}
108
109static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
110{
111 return ll_entry_end(struct g_dnl_bind_callback,
112 g_dnl_bind_callbacks);
113}
114
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200115static int g_dnl_do_config(struct usb_configuration *c)
116{
117 const char *s = c->cdev->driver->name;
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200118 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200119
120 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
121 __func__, c, c->cdev);
122
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200123 for (; callback != g_dnl_bind_callback_end(); callback++)
124 if (!strcmp(s, callback->usb_function_name))
125 return callback->fptr(c);
126 return -ENODEV;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200127}
128
129static int g_dnl_config_register(struct usb_composite_dev *cdev)
130{
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200131 struct usb_configuration *config;
132 const char *name = "usb_dnload";
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200133
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200134 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
135 if (!config)
136 return -ENOMEM;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200137
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200138 memset(config, 0, sizeof(*config));
139
140 config->label = name;
141 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
142 config->bConfigurationValue = CONFIGURATION_NUMBER;
143 config->iConfiguration = STRING_USBDOWN;
144 config->bind = g_dnl_do_config;
145
146 return usb_add_config(cdev, config);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200147}
148
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200149__weak
Paul Kocialkowski99fc2222015-06-12 19:57:01 +0200150int board_usb_init(int index, enum usb_init_type init)
151{
152 return 0;
153}
154
155__weak
156int board_usb_cleanup(int index, enum usb_init_type init)
157{
158 return 0;
159}
160
161__weak
Lukasz Majewskid6eae7b2013-10-08 14:30:41 +0200162int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200163{
164 return 0;
165}
166
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100167__weak int g_dnl_get_board_bcd_device_number(int gcnum)
168{
169 return gcnum;
170}
171
Mateusz Zalega75504e92014-04-30 13:07:48 +0200172__weak int g_dnl_board_usb_cable_connected(void)
173{
174 return -EOPNOTSUPP;
175}
176
Rob Herringfe1b28c2014-12-10 14:43:03 -0600177static bool g_dnl_detach_request;
178
179bool g_dnl_detach(void)
180{
181 return g_dnl_detach_request;
182}
183
184void g_dnl_trigger_detach(void)
185{
186 g_dnl_detach_request = true;
187}
188
189void g_dnl_clear_detach(void)
190{
191 g_dnl_detach_request = false;
192}
193
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100194static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
195{
196 struct usb_gadget *gadget = cdev->gadget;
197 int gcnum;
198
199 gcnum = usb_gadget_controller_number(gadget);
200 if (gcnum > 0)
201 gcnum += 0x200;
202
203 return g_dnl_get_board_bcd_device_number(gcnum);
204}
205
Sam Protsenkode4e4ed2017-09-01 15:42:03 +0300206/**
207 * Update internal serial number variable when the "serial#" env var changes.
208 *
209 * Handle all cases, even when flags == H_PROGRAMMATIC or op == env_op_delete.
210 */
211static int on_serialno(const char *name, const char *value, enum env_op op,
212 int flags)
213{
214 g_dnl_set_serialnumber((char *)value);
215 return 0;
216}
217U_BOOT_ENV_CALLBACK(serialno, on_serialno);
218
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200219static int g_dnl_bind(struct usb_composite_dev *cdev)
220{
221 struct usb_gadget *gadget = cdev->gadget;
222 int id, ret;
223 int gcnum;
224
225 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
226
227 id = usb_string_id(cdev);
228
229 if (id < 0)
230 return id;
231 g_dnl_string_defs[0].id = id;
232 device_desc.iManufacturer = id;
233
234 id = usb_string_id(cdev);
235 if (id < 0)
236 return id;
237
238 g_dnl_string_defs[1].id = id;
239 device_desc.iProduct = id;
240
Lukasz Majewski2a0583e2017-06-26 13:04:58 +0200241 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
242
Felipe Balbi842778a2017-02-22 17:12:40 +0200243 if (strlen(g_dnl_serial)) {
244 id = usb_string_id(cdev);
245 if (id < 0)
246 return id;
Heiko Schocherec9002e2013-11-04 14:05:01 +0100247
Felipe Balbi842778a2017-02-22 17:12:40 +0200248 g_dnl_string_defs[2].id = id;
249 device_desc.iSerialNumber = id;
250 }
Heiko Schocherec9002e2013-11-04 14:05:01 +0100251
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200252 ret = g_dnl_config_register(cdev);
253 if (ret)
254 goto error;
255
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100256 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200257 if (gcnum >= 0)
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100258 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200259 else {
260 debug("%s: controller '%s' not recognized\n",
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200261 __func__, gadget->name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200262 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
263 }
264
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000265 debug("%s: calling usb_gadget_connect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200266 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000267 usb_gadget_connect(gadget);
268
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200269 return 0;
270
271 error:
272 g_dnl_unbind(cdev);
273 return -ENOMEM;
274}
275
276static struct usb_composite_driver g_dnl_driver = {
277 .name = NULL,
278 .dev = &device_desc,
279 .strings = g_dnl_composite_strings,
280
281 .bind = g_dnl_bind,
282 .unbind = g_dnl_unbind,
283};
284
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200285/*
286 * NOTICE:
287 * Registering via USB function name won't be necessary after rewriting
288 * g_dnl to support multiple USB functions.
289 */
290int g_dnl_register(const char *name)
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200291{
Stephen Warren25fbf962014-05-01 15:45:16 -0600292 int ret;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200293
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200294 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200295 g_dnl_driver.name = name;
296
Stephen Warren25fbf962014-05-01 15:45:16 -0600297 ret = usb_composite_register(&g_dnl_driver);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200298 if (ret) {
299 printf("%s: failed!, error: %d\n", __func__, ret);
300 return ret;
301 }
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200302 return 0;
303}
304
305void g_dnl_unregister(void)
306{
307 usb_composite_unregister(&g_dnl_driver);
308}