2 * g_dnl.c -- USB Downloader Gadget
4 * Copyright (C) 2012 Samsung Electronics
5 * Lukasz Majewski <l.majewski@samsung.com>
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.
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.
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
32 #include "gadget_chips.h"
33 #include "composite.c"
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
43 #define STRING_MANUFACTURER 25
44 #define STRING_PRODUCT 2
45 #define STRING_USBDOWN 2
46 #define CONFIG_USBDOWNLOADER 2
48 #define DRIVER_VERSION "usb_dnl 2.0"
50 static const char shortname[] = "usb_dnl_";
51 static const char product[] = "USB download gadget";
52 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
54 static struct usb_device_descriptor device_desc = {
55 .bLength = sizeof device_desc,
56 .bDescriptorType = USB_DT_DEVICE,
58 .bcdUSB = __constant_cpu_to_le16(0x0200),
59 .bDeviceClass = USB_CLASS_COMM,
60 .bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
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,
68 /* static strings, in UTF-8 */
69 static struct usb_string g_dnl_string_defs[] = {
74 static struct usb_gadget_strings g_dnl_string_tab = {
75 .language = 0x0409, /* en-us */
76 .strings = g_dnl_string_defs,
79 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
84 static int g_dnl_unbind(struct usb_composite_dev *cdev)
86 struct usb_gadget *gadget = cdev->gadget;
88 debug("%s: calling usb_gadget_disconnect for "
89 "controller '%s'\n", shortname, gadget->name);
90 usb_gadget_disconnect(gadget);
95 static int g_dnl_do_config(struct usb_configuration *c)
97 const char *s = c->cdev->driver->name;
100 debug("%s: configuration: 0x%p composite dev: 0x%p\n",
101 __func__, c, c->cdev);
103 printf("GADGET DRIVER: %s\n", s);
104 if (!strcmp(s, "usb_dnl_dfu"))
110 static int g_dnl_config_register(struct usb_composite_dev *cdev)
112 static struct usb_configuration config = {
113 .label = "usb_dnload",
114 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
115 .bConfigurationValue = CONFIG_USBDOWNLOADER,
116 .iConfiguration = STRING_USBDOWN,
118 .bind = g_dnl_do_config,
121 return usb_add_config(cdev, &config);
124 static int g_dnl_bind(struct usb_composite_dev *cdev)
126 struct usb_gadget *gadget = cdev->gadget;
130 debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
132 id = usb_string_id(cdev);
136 g_dnl_string_defs[0].id = id;
137 device_desc.iManufacturer = id;
139 id = usb_string_id(cdev);
143 g_dnl_string_defs[1].id = id;
144 device_desc.iProduct = id;
146 ret = g_dnl_config_register(cdev);
150 gcnum = usb_gadget_controller_number(gadget);
152 debug("gcnum: %d\n", gcnum);
154 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
156 debug("%s: controller '%s' not recognized\n",
157 shortname, gadget->name);
158 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
161 debug("%s: calling usb_gadget_connect for "
162 "controller '%s'\n", shortname, gadget->name);
163 usb_gadget_connect(gadget);
172 static struct usb_composite_driver g_dnl_driver = {
175 .strings = g_dnl_composite_strings,
178 .unbind = g_dnl_unbind,
181 int g_dnl_register(const char *type)
183 /* We only allow "dfu" atm, so 3 should be enough */
184 static char name[sizeof(shortname) + 3];
187 if (!strcmp(type, "dfu")) {
188 strcpy(name, shortname);
191 printf("%s: unknown command: %s\n", __func__, type);
195 g_dnl_driver.name = name;
197 debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
198 ret = usb_composite_register(&g_dnl_driver);
201 printf("%s: failed!, error: %d\n", __func__, ret);
208 void g_dnl_unregister(void)
210 usb_composite_unregister(&g_dnl_driver);