xref: /openbmc/u-boot/drivers/usb/gadget/g_dnl.c (revision a2ac1b3a)
1 /*
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"
34 
35 /*
36  * One needs to define the following:
37  * CONFIG_G_DNL_VENDOR_NUM
38  * CONFIG_G_DNL_PRODUCT_NUM
39  * CONFIG_G_DNL_MANUFACTURER
40  * at e.g. ./include/configs/<board>.h
41  */
42 
43 #define STRING_MANUFACTURER 25
44 #define STRING_PRODUCT 2
45 #define STRING_USBDOWN 2
46 #define CONFIG_USBDOWNLOADER 2
47 
48 #define DRIVER_VERSION		"usb_dnl 2.0"
49 
50 static const char shortname[] = "usb_dnl_";
51 static const char product[] = "USB download gadget";
52 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
53 
54 static struct usb_device_descriptor device_desc = {
55 	.bLength = sizeof device_desc,
56 	.bDescriptorType = USB_DT_DEVICE,
57 
58 	.bcdUSB = __constant_cpu_to_le16(0x0200),
59 	.bDeviceClass = USB_CLASS_COMM,
60 	.bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
61 
62 	.idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
63 	.idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
64 	.iProduct = STRING_PRODUCT,
65 	.bNumConfigurations = 1,
66 };
67 
68 /* static strings, in UTF-8 */
69 static struct usb_string g_dnl_string_defs[] = {
70 	{ 0, manufacturer, },
71 	{ 1, product, },
72 };
73 
74 static struct usb_gadget_strings g_dnl_string_tab = {
75 	.language = 0x0409, /* en-us */
76 	.strings = g_dnl_string_defs,
77 };
78 
79 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
80 	&g_dnl_string_tab,
81 	NULL,
82 };
83 
84 static int g_dnl_unbind(struct usb_composite_dev *cdev)
85 {
86 	debug("%s\n", __func__);
87 	return 0;
88 }
89 
90 static int g_dnl_do_config(struct usb_configuration *c)
91 {
92 	const char *s = c->cdev->driver->name;
93 	int ret = -1;
94 
95 	debug("%s: configuration: 0x%p composite dev: 0x%p\n",
96 	      __func__, c, c->cdev);
97 
98 	printf("GADGET DRIVER: %s\n", s);
99 	if (!strcmp(s, "usb_dnl_dfu"))
100 		ret = dfu_add(c);
101 
102 	return ret;
103 }
104 
105 static int g_dnl_config_register(struct usb_composite_dev *cdev)
106 {
107 	static struct usb_configuration config = {
108 		.label = "usb_dnload",
109 		.bmAttributes =	USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
110 		.bConfigurationValue =	CONFIG_USBDOWNLOADER,
111 		.iConfiguration =	STRING_USBDOWN,
112 
113 		.bind = g_dnl_do_config,
114 	};
115 
116 	return usb_add_config(cdev, &config);
117 }
118 
119 static int g_dnl_bind(struct usb_composite_dev *cdev)
120 {
121 	struct usb_gadget *gadget = cdev->gadget;
122 	int id, ret;
123 	int gcnum;
124 
125 	debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
126 
127 	id = usb_string_id(cdev);
128 
129 	if (id < 0)
130 		return id;
131 	g_dnl_string_defs[0].id = id;
132 	device_desc.iManufacturer = id;
133 
134 	id = usb_string_id(cdev);
135 	if (id < 0)
136 		return id;
137 
138 	g_dnl_string_defs[1].id = id;
139 	device_desc.iProduct = id;
140 
141 	ret = g_dnl_config_register(cdev);
142 	if (ret)
143 		goto error;
144 
145 	gcnum = usb_gadget_controller_number(gadget);
146 
147 	debug("gcnum: %d\n", gcnum);
148 	if (gcnum >= 0)
149 		device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
150 	else {
151 		debug("%s: controller '%s' not recognized\n",
152 			shortname, gadget->name);
153 		device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
154 	}
155 
156 	return 0;
157 
158  error:
159 	g_dnl_unbind(cdev);
160 	return -ENOMEM;
161 }
162 
163 static struct usb_composite_driver g_dnl_driver = {
164 	.name = NULL,
165 	.dev = &device_desc,
166 	.strings = g_dnl_composite_strings,
167 
168 	.bind = g_dnl_bind,
169 	.unbind = g_dnl_unbind,
170 };
171 
172 int g_dnl_register(const char *type)
173 {
174 	/* We only allow "dfu" atm, so 3 should be enough */
175 	static char name[sizeof(shortname) + 3];
176 	int ret;
177 
178 	if (!strcmp(type, "dfu")) {
179 		strcpy(name, shortname);
180 		strcat(name, type);
181 	} else {
182 		printf("%s: unknown command: %s\n", __func__, type);
183 		return -EINVAL;
184 	}
185 
186 	g_dnl_driver.name = name;
187 
188 	debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
189 	ret = usb_composite_register(&g_dnl_driver);
190 
191 	if (ret) {
192 		printf("%s: failed!, error: %d\n", __func__, ret);
193 		return ret;
194 	}
195 
196 	return 0;
197 }
198 
199 void g_dnl_unregister(void)
200 {
201 	usb_composite_unregister(&g_dnl_driver);
202 }
203