blob: 8a3777b6e0b2d8e09525b600084465f612c6bb28 [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <errno.h>
23#include <common.h>
24#include <malloc.h>
25
26#include <mmc.h>
27#include <part.h>
28
29#include <g_dnl.h>
30#include "f_dfu.h"
31
32#include "gadget_chips.h"
33#include "composite.c"
Lukasz Majewskib528f712013-03-05 12:10:17 +010034#include "f_mass_storage.c"
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020035
36/*
37 * One needs to define the following:
38 * CONFIG_G_DNL_VENDOR_NUM
39 * CONFIG_G_DNL_PRODUCT_NUM
40 * CONFIG_G_DNL_MANUFACTURER
41 * at e.g. ./include/configs/<board>.h
42 */
43
44#define STRING_MANUFACTURER 25
45#define STRING_PRODUCT 2
46#define STRING_USBDOWN 2
47#define CONFIG_USBDOWNLOADER 2
48
49#define DRIVER_VERSION "usb_dnl 2.0"
50
51static const char shortname[] = "usb_dnl_";
52static const char product[] = "USB download gadget";
53static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
54
55static 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,
66 .bNumConfigurations = 1,
67};
68
69/* static strings, in UTF-8 */
70static struct usb_string g_dnl_string_defs[] = {
71 { 0, manufacturer, },
72 { 1, product, },
Pantelis Antoniou598cf602012-11-30 08:01:06 +000073 { } /* end of list */
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020074};
75
76static struct usb_gadget_strings g_dnl_string_tab = {
77 .language = 0x0409, /* en-us */
78 .strings = g_dnl_string_defs,
79};
80
81static struct usb_gadget_strings *g_dnl_composite_strings[] = {
82 &g_dnl_string_tab,
83 NULL,
84};
85
86static int g_dnl_unbind(struct usb_composite_dev *cdev)
87{
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +000088 struct usb_gadget *gadget = cdev->gadget;
89
90 debug("%s: calling usb_gadget_disconnect for "
91 "controller '%s'\n", shortname, gadget->name);
92 usb_gadget_disconnect(gadget);
93
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +020094 return 0;
95}
96
97static int g_dnl_do_config(struct usb_configuration *c)
98{
99 const char *s = c->cdev->driver->name;
100 int ret = -1;
101
102 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
103 __func__, c, c->cdev);
104
105 printf("GADGET DRIVER: %s\n", s);
106 if (!strcmp(s, "usb_dnl_dfu"))
107 ret = dfu_add(c);
Lukasz Majewskib528f712013-03-05 12:10:17 +0100108 else if (!strcmp(s, "usb_dnl_ums"))
109 ret = fsg_add(c);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200110
111 return ret;
112}
113
114static int g_dnl_config_register(struct usb_composite_dev *cdev)
115{
116 static struct usb_configuration config = {
117 .label = "usb_dnload",
118 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
119 .bConfigurationValue = CONFIG_USBDOWNLOADER,
120 .iConfiguration = STRING_USBDOWN,
121
122 .bind = g_dnl_do_config,
123 };
124
125 return usb_add_config(cdev, &config);
126}
127
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200128__weak
129int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
130{
131 return 0;
132}
133
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200134static int g_dnl_bind(struct usb_composite_dev *cdev)
135{
136 struct usb_gadget *gadget = cdev->gadget;
137 int id, ret;
138 int gcnum;
139
140 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
141
142 id = usb_string_id(cdev);
143
144 if (id < 0)
145 return id;
146 g_dnl_string_defs[0].id = id;
147 device_desc.iManufacturer = id;
148
149 id = usb_string_id(cdev);
150 if (id < 0)
151 return id;
152
153 g_dnl_string_defs[1].id = id;
154 device_desc.iProduct = id;
155
Heiko Schocherc5398cc2013-06-04 11:19:50 +0200156 g_dnl_bind_fixup(&device_desc);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200157 ret = g_dnl_config_register(cdev);
158 if (ret)
159 goto error;
160
161 gcnum = usb_gadget_controller_number(gadget);
162
163 debug("gcnum: %d\n", gcnum);
164 if (gcnum >= 0)
165 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
166 else {
167 debug("%s: controller '%s' not recognized\n",
168 shortname, gadget->name);
169 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
170 }
171
Pantelis Antoniou5a413ca2012-11-30 08:01:05 +0000172 debug("%s: calling usb_gadget_connect for "
173 "controller '%s'\n", shortname, gadget->name);
174 usb_gadget_connect(gadget);
175
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200176 return 0;
177
178 error:
179 g_dnl_unbind(cdev);
180 return -ENOMEM;
181}
182
183static struct usb_composite_driver g_dnl_driver = {
184 .name = NULL,
185 .dev = &device_desc,
186 .strings = g_dnl_composite_strings,
187
188 .bind = g_dnl_bind,
189 .unbind = g_dnl_unbind,
190};
191
192int g_dnl_register(const char *type)
193{
194 /* We only allow "dfu" atm, so 3 should be enough */
195 static char name[sizeof(shortname) + 3];
196 int ret;
197
198 if (!strcmp(type, "dfu")) {
199 strcpy(name, shortname);
200 strcat(name, type);
Lukasz Majewskib528f712013-03-05 12:10:17 +0100201 } else if (!strcmp(type, "ums")) {
202 strcpy(name, shortname);
203 strcat(name, type);
Lukasz Majewski1d4a0b62012-08-06 14:41:05 +0200204 } else {
205 printf("%s: unknown command: %s\n", __func__, type);
206 return -EINVAL;
207 }
208
209 g_dnl_driver.name = name;
210
211 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
212 ret = usb_composite_register(&g_dnl_driver);
213
214 if (ret) {
215 printf("%s: failed!, error: %d\n", __func__, ret);
216 return ret;
217 }
218
219 return 0;
220}
221
222void g_dnl_unregister(void)
223{
224 usb_composite_unregister(&g_dnl_driver);
225}