3cb7302b59031355b829cbfc5ee27c216891ba11
[oweals/u-boot.git] / drivers / usb / gadget / g_dnl.c
1 /*
2  * g_dnl.c -- USB Downloader Gadget
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * Lukasz Majewski  <l.majewski@samsung.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
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"
22 #include "f_mass_storage.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
34 /* Index of String Descriptor describing this configuration */
35 #define STRING_USBDOWN 2
36 /* Number of supported configurations */
37 #define CONFIGURATION_NUMBER 1
38
39 #define DRIVER_VERSION          "usb_dnl 2.0"
40
41 static const char shortname[] = "usb_dnl_";
42 static const char product[] = "USB download gadget";
43 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
44
45 static 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 */
60 static struct usb_string g_dnl_string_defs[] = {
61         { 0, manufacturer, },
62         { 1, product, },
63         {  }            /* end of list */
64 };
65
66 static struct usb_gadget_strings g_dnl_string_tab = {
67         .language = 0x0409, /* en-us */
68         .strings = g_dnl_string_defs,
69 };
70
71 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
72         &g_dnl_string_tab,
73         NULL,
74 };
75
76 static int g_dnl_unbind(struct usb_composite_dev *cdev)
77 {
78         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
84         return 0;
85 }
86
87 static 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);
98         else if (!strcmp(s, "usb_dnl_ums"))
99                 ret = fsg_add(c);
100
101         return ret;
102 }
103
104 static 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,
109                 .bConfigurationValue =  CONFIGURATION_NUMBER,
110                 .iConfiguration =       STRING_USBDOWN,
111
112                 .bind = g_dnl_do_config,
113         };
114
115         return usb_add_config(cdev, &config);
116 }
117
118 __weak
119 int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
120 {
121         return 0;
122 }
123
124 static 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
146         g_dnl_bind_fixup(&device_desc);
147         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
162         debug("%s: calling usb_gadget_connect for "
163                         "controller '%s'\n", shortname, gadget->name);
164         usb_gadget_connect(gadget);
165
166         return 0;
167
168  error:
169         g_dnl_unbind(cdev);
170         return -ENOMEM;
171 }
172
173 static 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
182 int 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);
191         } else if (!strcmp(type, "ums")) {
192                 strcpy(name, shortname);
193                 strcat(name, type);
194         } 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
212 void g_dnl_unregister(void)
213 {
214         usb_composite_unregister(&g_dnl_driver);
215 }