blob: 99d500a6af4e71bb9a9c52463b8f06854fb02572 [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>
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
93static int g_dnl_unbind(struct usb_composite_dev *cdev)
94{
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000095 struct usb_gadget *gadget = cdev->gadget;
96
97 debug("%s: calling usb_gadget_disconnect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +020098 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000099 usb_gadget_disconnect(gadget);
100
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200101 return 0;
102}
103
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200104static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
105{
106 return ll_entry_start(struct g_dnl_bind_callback,
107 g_dnl_bind_callbacks);
108}
109
110static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
111{
112 return ll_entry_end(struct g_dnl_bind_callback,
113 g_dnl_bind_callbacks);
114}
115
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200116static int g_dnl_do_config(struct usb_configuration *c)
117{
118 const char *s = c->cdev->driver->name;
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200119 struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200120
121 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
122 __func__, c, c->cdev);
123
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200124 for (; callback != g_dnl_bind_callback_end(); callback++)
125 if (!strcmp(s, callback->usb_function_name))
126 return callback->fptr(c);
127 return -ENODEV;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200128}
129
130static int g_dnl_config_register(struct usb_composite_dev *cdev)
131{
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200132 struct usb_configuration *config;
133 const char *name = "usb_dnload";
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200134
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200135 config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
136 if (!config)
137 return -ENOMEM;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200138
Lukasz Majewski7b412ab2013-10-08 14:30:40 +0200139 memset(config, 0, sizeof(*config));
140
141 config->label = name;
142 config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
143 config->bConfigurationValue = CONFIGURATION_NUMBER;
144 config->iConfiguration = STRING_USBDOWN;
145 config->bind = g_dnl_do_config;
146
147 return usb_add_config(cdev, config);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200148}
149
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200150__weak
Paul Kocialkowski99fc2222015-06-12 19:57:01 +0200151int board_usb_init(int index, enum usb_init_type init)
152{
153 return 0;
154}
155
156__weak
157int board_usb_cleanup(int index, enum usb_init_type init)
158{
159 return 0;
160}
161
162__weak
Lukasz Majewskid6eae7b2013-10-08 14:30:41 +0200163int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200164{
165 return 0;
166}
167
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100168__weak int g_dnl_get_board_bcd_device_number(int gcnum)
169{
170 return gcnum;
171}
172
Mateusz Zalega75504e92014-04-30 13:07:48 +0200173__weak int g_dnl_board_usb_cable_connected(void)
174{
175 return -EOPNOTSUPP;
176}
177
Rob Herringfe1b28c2014-12-10 14:43:03 -0600178static bool g_dnl_detach_request;
179
180bool g_dnl_detach(void)
181{
182 return g_dnl_detach_request;
183}
184
185void g_dnl_trigger_detach(void)
186{
187 g_dnl_detach_request = true;
188}
189
190void g_dnl_clear_detach(void)
191{
192 g_dnl_detach_request = false;
193}
194
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100195static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
196{
197 struct usb_gadget *gadget = cdev->gadget;
198 int gcnum;
199
200 gcnum = usb_gadget_controller_number(gadget);
201 if (gcnum > 0)
202 gcnum += 0x200;
203
204 return g_dnl_get_board_bcd_device_number(gcnum);
205}
206
Sam Protsenkode4e4ed2017-09-01 15:42:03 +0300207/**
208 * Update internal serial number variable when the "serial#" env var changes.
209 *
210 * Handle all cases, even when flags == H_PROGRAMMATIC or op == env_op_delete.
211 */
212static int on_serialno(const char *name, const char *value, enum env_op op,
213 int flags)
214{
215 g_dnl_set_serialnumber((char *)value);
216 return 0;
217}
218U_BOOT_ENV_CALLBACK(serialno, on_serialno);
219
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200220static int g_dnl_bind(struct usb_composite_dev *cdev)
221{
222 struct usb_gadget *gadget = cdev->gadget;
223 int id, ret;
224 int gcnum;
225
226 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
227
228 id = usb_string_id(cdev);
229
230 if (id < 0)
231 return id;
232 g_dnl_string_defs[0].id = id;
233 device_desc.iManufacturer = id;
234
235 id = usb_string_id(cdev);
236 if (id < 0)
237 return id;
238
239 g_dnl_string_defs[1].id = id;
240 device_desc.iProduct = id;
241
Lukasz Majewski2a0583e2017-06-26 13:04:58 +0200242 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
243
Felipe Balbi842778a2017-02-22 17:12:40 +0200244 if (strlen(g_dnl_serial)) {
245 id = usb_string_id(cdev);
246 if (id < 0)
247 return id;
Heiko Schocherec9002e2013-11-04 14:05:01 +0100248
Felipe Balbi842778a2017-02-22 17:12:40 +0200249 g_dnl_string_defs[2].id = id;
250 device_desc.iSerialNumber = id;
251 }
Heiko Schocherec9002e2013-11-04 14:05:01 +0100252
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200253 ret = g_dnl_config_register(cdev);
254 if (ret)
255 goto error;
256
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100257 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200258 if (gcnum >= 0)
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100259 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200260 else {
261 debug("%s: controller '%s' not recognized\n",
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200262 __func__, gadget->name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200263 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
264 }
265
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000266 debug("%s: calling usb_gadget_connect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200267 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000268 usb_gadget_connect(gadget);
269
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200270 return 0;
271
272 error:
273 g_dnl_unbind(cdev);
274 return -ENOMEM;
275}
276
277static struct usb_composite_driver g_dnl_driver = {
278 .name = NULL,
279 .dev = &device_desc,
280 .strings = g_dnl_composite_strings,
281
282 .bind = g_dnl_bind,
283 .unbind = g_dnl_unbind,
284};
285
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200286/*
287 * NOTICE:
288 * Registering via USB function name won't be necessary after rewriting
289 * g_dnl to support multiple USB functions.
290 */
291int g_dnl_register(const char *name)
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200292{
Stephen Warren25fbf962014-05-01 15:45:16 -0600293 int ret;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200294
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200295 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200296 g_dnl_driver.name = name;
297
Stephen Warren25fbf962014-05-01 15:45:16 -0600298 ret = usb_composite_register(&g_dnl_driver);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200299 if (ret) {
300 printf("%s: failed!, error: %d\n", __func__, ret);
301 return ret;
302 }
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200303 return 0;
304}
305
306void g_dnl_unregister(void)
307{
308 usb_composite_unregister(&g_dnl_driver);
309}