blob: ee52a294679ad5b98de1e81f01b790306ae00f99 [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>
15
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
21#include "gadget_chips.h"
22#include "composite.c"
23
24/*
25 * One needs to define the following:
26 * CONFIG_G_DNL_VENDOR_NUM
27 * CONFIG_G_DNL_PRODUCT_NUM
28 * CONFIG_G_DNL_MANUFACTURER
29 * at e.g. ./include/configs/<board>.h
30 */
31
32#define STRING_MANUFACTURER 25
33#define STRING_PRODUCT 2
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020034/* Index of String Descriptor describing this configuration */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020035#define STRING_USBDOWN 2
Heiko Schocherec9002e2013-11-04 14:05:01 +010036/* Index of String serial */
37#define STRING_SERIAL 3
38#define MAX_STRING_SERIAL 32
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020039/* Number of supported configurations */
40#define CONFIGURATION_NUMBER 1
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020041
42#define DRIVER_VERSION "usb_dnl 2.0"
43
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020044static const char product[] = "USB download gadget";
Heiko Schocherec9002e2013-11-04 14:05:01 +010045static char g_dnl_serial[MAX_STRING_SERIAL];
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020046static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
47
Heiko Schocherec9002e2013-11-04 14:05:01 +010048void g_dnl_set_serialnumber(char *s)
49{
50 memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
51 if (strlen(s) < MAX_STRING_SERIAL)
52 strncpy(g_dnl_serial, s, strlen(s));
53}
54
Lukasz Majewski1d4a0b62012-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),
60 .bDeviceClass = USB_CLASS_COMM,
61 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
62
63 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
64 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
65 .iProduct = STRING_PRODUCT,
Heiko Schocherec9002e2013-11-04 14:05:01 +010066 .iSerialNumber = STRING_SERIAL,
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020067 .bNumConfigurations = 1,
68};
69
Lukasz Majewskic4219a82013-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 Majewski1d4a0b62012-08-06 14:41:05 +020074static struct usb_string g_dnl_string_defs[] = {
Lukasz Majewskic4219a82013-08-19 17:17:19 +020075 {.s = manufacturer},
76 {.s = product},
Heiko Schocherec9002e2013-11-04 14:05:01 +010077 {.s = g_dnl_serial},
Lukasz Majewskic4219a82013-08-19 17:17:19 +020078 { } /* end of list */
Lukasz Majewski1d4a0b62012-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 Antoniou5a413ca2012-11-30 08:01:05 +000093 struct usb_gadget *gadget = cdev->gadget;
94
Lukasz Majewski7b412ab2013-10-08 14:30:40 +020095 free(cdev->config);
96 cdev->config = NULL;
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000097 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
Lukasz Majewskid6eae7b2013-10-08 14:30:41 +0200151int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200152{
153 return 0;
154}
155
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100156__weak int g_dnl_get_board_bcd_device_number(int gcnum)
157{
158 return gcnum;
159}
160
Mateusz Zalega75504e92014-04-30 13:07:48 +0200161__weak int g_dnl_board_usb_cable_connected(void)
162{
163 return -EOPNOTSUPP;
164}
165
Rob Herringfe1b28c2014-12-10 14:43:03 -0600166static bool g_dnl_detach_request;
167
168bool g_dnl_detach(void)
169{
170 return g_dnl_detach_request;
171}
172
173void g_dnl_trigger_detach(void)
174{
175 g_dnl_detach_request = true;
176}
177
178void g_dnl_clear_detach(void)
179{
180 g_dnl_detach_request = false;
181}
182
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100183static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
184{
185 struct usb_gadget *gadget = cdev->gadget;
186 int gcnum;
187
188 gcnum = usb_gadget_controller_number(gadget);
189 if (gcnum > 0)
190 gcnum += 0x200;
191
192 return g_dnl_get_board_bcd_device_number(gcnum);
193}
194
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200195static int g_dnl_bind(struct usb_composite_dev *cdev)
196{
197 struct usb_gadget *gadget = cdev->gadget;
198 int id, ret;
199 int gcnum;
200
201 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
202
203 id = usb_string_id(cdev);
204
205 if (id < 0)
206 return id;
207 g_dnl_string_defs[0].id = id;
208 device_desc.iManufacturer = id;
209
210 id = usb_string_id(cdev);
211 if (id < 0)
212 return id;
213
214 g_dnl_string_defs[1].id = id;
215 device_desc.iProduct = id;
216
Heiko Schocherec9002e2013-11-04 14:05:01 +0100217 id = usb_string_id(cdev);
218 if (id < 0)
219 return id;
220
221 g_dnl_string_defs[2].id = id;
222 device_desc.iSerialNumber = id;
223
Lukasz Majewskid6eae7b2013-10-08 14:30:41 +0200224 g_dnl_bind_fixup(&device_desc, cdev->driver->name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200225 ret = g_dnl_config_register(cdev);
226 if (ret)
227 goto error;
228
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100229 gcnum = g_dnl_get_bcd_device_number(cdev);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200230 if (gcnum >= 0)
Heiko Schocher7a0d4632013-11-04 14:05:02 +0100231 device_desc.bcdDevice = cpu_to_le16(gcnum);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200232 else {
233 debug("%s: controller '%s' not recognized\n",
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200234 __func__, gadget->name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200235 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
236 }
237
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000238 debug("%s: calling usb_gadget_connect for "
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200239 "controller '%s'\n", __func__, gadget->name);
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000240 usb_gadget_connect(gadget);
241
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200242 return 0;
243
244 error:
245 g_dnl_unbind(cdev);
246 return -ENOMEM;
247}
248
249static struct usb_composite_driver g_dnl_driver = {
250 .name = NULL,
251 .dev = &device_desc,
252 .strings = g_dnl_composite_strings,
253
254 .bind = g_dnl_bind,
255 .unbind = g_dnl_unbind,
256};
257
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200258/*
259 * NOTICE:
260 * Registering via USB function name won't be necessary after rewriting
261 * g_dnl to support multiple USB functions.
262 */
263int g_dnl_register(const char *name)
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200264{
Stephen Warren25fbf962014-05-01 15:45:16 -0600265 int ret;
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200266
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200267 debug("%s: g_dnl_driver.name = %s\n", __func__, name);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200268 g_dnl_driver.name = name;
269
Stephen Warren25fbf962014-05-01 15:45:16 -0600270 ret = usb_composite_register(&g_dnl_driver);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200271 if (ret) {
272 printf("%s: failed!, error: %d\n", __func__, ret);
273 return ret;
274 }
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200275 return 0;
276}
277
278void g_dnl_unregister(void)
279{
280 usb_composite_unregister(&g_dnl_driver);
281}