blob: 3cb7302b59031355b829cbfc5ee27c216891ba11 [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
10#include <errno.h>
11#include <common.h>
12#include <malloc.h>
13
14#include <mmc.h>
15#include <part.h>
16
17#include <g_dnl.h>
18#include "f_dfu.h"
19
20#include "gadget_chips.h"
21#include "composite.c"
Lukasz Majewskib528f712013-03-05 12:10:17 +010022#include "f_mass_storage.c"
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020023
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
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +020036/* Number of supported configurations */
37#define CONFIGURATION_NUMBER 1
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020038
39#define DRIVER_VERSION "usb_dnl 2.0"
40
41static const char shortname[] = "usb_dnl_";
42static const char product[] = "USB download gadget";
43static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
44
45static struct usb_device_descriptor device_desc = {
46 .bLength = sizeof device_desc,
47 .bDescriptorType = USB_DT_DEVICE,
48
49 .bcdUSB = __constant_cpu_to_le16(0x0200),
50 .bDeviceClass = USB_CLASS_COMM,
51 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
52
53 .idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
54 .idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
55 .iProduct = STRING_PRODUCT,
56 .bNumConfigurations = 1,
57};
58
59/* static strings, in UTF-8 */
60static struct usb_string g_dnl_string_defs[] = {
61 { 0, manufacturer, },
62 { 1, product, },
Pantelis Antoniou598cf602012-11-30 08:01:06 +000063 { } /* end of list */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020064};
65
66static struct usb_gadget_strings g_dnl_string_tab = {
67 .language = 0x0409, /* en-us */
68 .strings = g_dnl_string_defs,
69};
70
71static struct usb_gadget_strings *g_dnl_composite_strings[] = {
72 &g_dnl_string_tab,
73 NULL,
74};
75
76static int g_dnl_unbind(struct usb_composite_dev *cdev)
77{
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000078 struct usb_gadget *gadget = cdev->gadget;
79
80 debug("%s: calling usb_gadget_disconnect for "
81 "controller '%s'\n", shortname, gadget->name);
82 usb_gadget_disconnect(gadget);
83
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020084 return 0;
85}
86
87static int g_dnl_do_config(struct usb_configuration *c)
88{
89 const char *s = c->cdev->driver->name;
90 int ret = -1;
91
92 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
93 __func__, c, c->cdev);
94
95 printf("GADGET DRIVER: %s\n", s);
96 if (!strcmp(s, "usb_dnl_dfu"))
97 ret = dfu_add(c);
Lukasz Majewskib528f712013-03-05 12:10:17 +010098 else if (!strcmp(s, "usb_dnl_ums"))
99 ret = fsg_add(c);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200100
101 return ret;
102}
103
104static int g_dnl_config_register(struct usb_composite_dev *cdev)
105{
106 static struct usb_configuration config = {
107 .label = "usb_dnload",
108 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Lukasz Majewskicfc2d0d2013-08-19 17:17:18 +0200109 .bConfigurationValue = CONFIGURATION_NUMBER,
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200110 .iConfiguration = STRING_USBDOWN,
111
112 .bind = g_dnl_do_config,
113 };
114
115 return usb_add_config(cdev, &config);
116}
117
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200118__weak
119int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
120{
121 return 0;
122}
123
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200124static int g_dnl_bind(struct usb_composite_dev *cdev)
125{
126 struct usb_gadget *gadget = cdev->gadget;
127 int id, ret;
128 int gcnum;
129
130 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
131
132 id = usb_string_id(cdev);
133
134 if (id < 0)
135 return id;
136 g_dnl_string_defs[0].id = id;
137 device_desc.iManufacturer = id;
138
139 id = usb_string_id(cdev);
140 if (id < 0)
141 return id;
142
143 g_dnl_string_defs[1].id = id;
144 device_desc.iProduct = id;
145
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200146 g_dnl_bind_fixup(&device_desc);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200147 ret = g_dnl_config_register(cdev);
148 if (ret)
149 goto error;
150
151 gcnum = usb_gadget_controller_number(gadget);
152
153 debug("gcnum: %d\n", gcnum);
154 if (gcnum >= 0)
155 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
156 else {
157 debug("%s: controller '%s' not recognized\n",
158 shortname, gadget->name);
159 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
160 }
161
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000162 debug("%s: calling usb_gadget_connect for "
163 "controller '%s'\n", shortname, gadget->name);
164 usb_gadget_connect(gadget);
165
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200166 return 0;
167
168 error:
169 g_dnl_unbind(cdev);
170 return -ENOMEM;
171}
172
173static struct usb_composite_driver g_dnl_driver = {
174 .name = NULL,
175 .dev = &device_desc,
176 .strings = g_dnl_composite_strings,
177
178 .bind = g_dnl_bind,
179 .unbind = g_dnl_unbind,
180};
181
182int g_dnl_register(const char *type)
183{
184 /* We only allow "dfu" atm, so 3 should be enough */
185 static char name[sizeof(shortname) + 3];
186 int ret;
187
188 if (!strcmp(type, "dfu")) {
189 strcpy(name, shortname);
190 strcat(name, type);
Lukasz Majewskib528f712013-03-05 12:10:17 +0100191 } else if (!strcmp(type, "ums")) {
192 strcpy(name, shortname);
193 strcat(name, type);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200194 } else {
195 printf("%s: unknown command: %s\n", __func__, type);
196 return -EINVAL;
197 }
198
199 g_dnl_driver.name = name;
200
201 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
202 ret = usb_composite_register(&g_dnl_driver);
203
204 if (ret) {
205 printf("%s: failed!, error: %d\n", __func__, ret);
206 return ret;
207 }
208
209 return 0;
210}
211
212void g_dnl_unregister(void)
213{
214 usb_composite_unregister(&g_dnl_driver);
215}