Merge branch 'master' of git://git.denx.de/u-boot-socfpga
[oweals/u-boot.git] / drivers / usb / gadget / f_fastboot.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008 - 2009
4  * Windriver, <www.windriver.com>
5  * Tom Rix <Tom.Rix@windriver.com>
6  *
7  * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8  *
9  * Copyright 2014 Linaro, Ltd.
10  * Rob Herring <robh@kernel.org>
11  */
12 #include <config.h>
13 #include <common.h>
14 #include <env.h>
15 #include <errno.h>
16 #include <fastboot.h>
17 #include <malloc.h>
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/gadget.h>
20 #include <linux/usb/composite.h>
21 #include <linux/compiler.h>
22 #include <g_dnl.h>
23
24 #define FASTBOOT_INTERFACE_CLASS        0xff
25 #define FASTBOOT_INTERFACE_SUB_CLASS    0x42
26 #define FASTBOOT_INTERFACE_PROTOCOL     0x03
27
28 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
29 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
30 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
31
32 #define EP_BUFFER_SIZE                  4096
33 /*
34  * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
35  * (64 or 512 or 1024), else we break on certain controllers like DWC3
36  * that expect bulk OUT requests to be divisible by maxpacket size.
37  */
38
39 struct f_fastboot {
40         struct usb_function usb_function;
41
42         /* IN/OUT EP's and corresponding requests */
43         struct usb_ep *in_ep, *out_ep;
44         struct usb_request *in_req, *out_req;
45 };
46
47 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
48 {
49         return container_of(f, struct f_fastboot, usb_function);
50 }
51
52 static struct f_fastboot *fastboot_func;
53
54 static struct usb_endpoint_descriptor fs_ep_in = {
55         .bLength            = USB_DT_ENDPOINT_SIZE,
56         .bDescriptorType    = USB_DT_ENDPOINT,
57         .bEndpointAddress   = USB_DIR_IN,
58         .bmAttributes       = USB_ENDPOINT_XFER_BULK,
59         .wMaxPacketSize     = cpu_to_le16(64),
60 };
61
62 static struct usb_endpoint_descriptor fs_ep_out = {
63         .bLength                = USB_DT_ENDPOINT_SIZE,
64         .bDescriptorType        = USB_DT_ENDPOINT,
65         .bEndpointAddress       = USB_DIR_OUT,
66         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
67         .wMaxPacketSize         = cpu_to_le16(64),
68 };
69
70 static struct usb_endpoint_descriptor hs_ep_in = {
71         .bLength                = USB_DT_ENDPOINT_SIZE,
72         .bDescriptorType        = USB_DT_ENDPOINT,
73         .bEndpointAddress       = USB_DIR_IN,
74         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
75         .wMaxPacketSize         = cpu_to_le16(512),
76 };
77
78 static struct usb_endpoint_descriptor hs_ep_out = {
79         .bLength                = USB_DT_ENDPOINT_SIZE,
80         .bDescriptorType        = USB_DT_ENDPOINT,
81         .bEndpointAddress       = USB_DIR_OUT,
82         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
83         .wMaxPacketSize         = cpu_to_le16(512),
84 };
85
86 static struct usb_interface_descriptor interface_desc = {
87         .bLength                = USB_DT_INTERFACE_SIZE,
88         .bDescriptorType        = USB_DT_INTERFACE,
89         .bInterfaceNumber       = 0x00,
90         .bAlternateSetting      = 0x00,
91         .bNumEndpoints          = 0x02,
92         .bInterfaceClass        = FASTBOOT_INTERFACE_CLASS,
93         .bInterfaceSubClass     = FASTBOOT_INTERFACE_SUB_CLASS,
94         .bInterfaceProtocol     = FASTBOOT_INTERFACE_PROTOCOL,
95 };
96
97 static struct usb_descriptor_header *fb_fs_function[] = {
98         (struct usb_descriptor_header *)&interface_desc,
99         (struct usb_descriptor_header *)&fs_ep_in,
100         (struct usb_descriptor_header *)&fs_ep_out,
101 };
102
103 static struct usb_descriptor_header *fb_hs_function[] = {
104         (struct usb_descriptor_header *)&interface_desc,
105         (struct usb_descriptor_header *)&hs_ep_in,
106         (struct usb_descriptor_header *)&hs_ep_out,
107         NULL,
108 };
109
110 static struct usb_endpoint_descriptor *
111 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
112             struct usb_endpoint_descriptor *hs)
113 {
114         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
115                 return hs;
116         return fs;
117 }
118
119 /*
120  * static strings, in UTF-8
121  */
122 static const char fastboot_name[] = "Android Fastboot";
123
124 static struct usb_string fastboot_string_defs[] = {
125         [0].s = fastboot_name,
126         {  }                    /* end of list */
127 };
128
129 static struct usb_gadget_strings stringtab_fastboot = {
130         .language       = 0x0409,       /* en-us */
131         .strings        = fastboot_string_defs,
132 };
133
134 static struct usb_gadget_strings *fastboot_strings[] = {
135         &stringtab_fastboot,
136         NULL,
137 };
138
139 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
140
141 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
142 {
143         int status = req->status;
144         if (!status)
145                 return;
146         printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
147 }
148
149 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
150 {
151         int id;
152         struct usb_gadget *gadget = c->cdev->gadget;
153         struct f_fastboot *f_fb = func_to_fastboot(f);
154         const char *s;
155
156         /* DYNAMIC interface numbers assignments */
157         id = usb_interface_id(c, f);
158         if (id < 0)
159                 return id;
160         interface_desc.bInterfaceNumber = id;
161
162         id = usb_string_id(c->cdev);
163         if (id < 0)
164                 return id;
165         fastboot_string_defs[0].id = id;
166         interface_desc.iInterface = id;
167
168         f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
169         if (!f_fb->in_ep)
170                 return -ENODEV;
171         f_fb->in_ep->driver_data = c->cdev;
172
173         f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
174         if (!f_fb->out_ep)
175                 return -ENODEV;
176         f_fb->out_ep->driver_data = c->cdev;
177
178         f->descriptors = fb_fs_function;
179
180         if (gadget_is_dualspeed(gadget)) {
181                 /* Assume endpoint addresses are the same for both speeds */
182                 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
183                 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
184                 /* copy HS descriptors */
185                 f->hs_descriptors = fb_hs_function;
186         }
187
188         s = env_get("serial#");
189         if (s)
190                 g_dnl_set_serialnumber((char *)s);
191
192         return 0;
193 }
194
195 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
196 {
197         memset(fastboot_func, 0, sizeof(*fastboot_func));
198 }
199
200 static void fastboot_disable(struct usb_function *f)
201 {
202         struct f_fastboot *f_fb = func_to_fastboot(f);
203
204         usb_ep_disable(f_fb->out_ep);
205         usb_ep_disable(f_fb->in_ep);
206
207         if (f_fb->out_req) {
208                 free(f_fb->out_req->buf);
209                 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
210                 f_fb->out_req = NULL;
211         }
212         if (f_fb->in_req) {
213                 free(f_fb->in_req->buf);
214                 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
215                 f_fb->in_req = NULL;
216         }
217 }
218
219 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
220 {
221         struct usb_request *req;
222
223         req = usb_ep_alloc_request(ep, 0);
224         if (!req)
225                 return NULL;
226
227         req->length = EP_BUFFER_SIZE;
228         req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
229         if (!req->buf) {
230                 usb_ep_free_request(ep, req);
231                 return NULL;
232         }
233
234         memset(req->buf, 0, req->length);
235         return req;
236 }
237
238 static int fastboot_set_alt(struct usb_function *f,
239                             unsigned interface, unsigned alt)
240 {
241         int ret;
242         struct usb_composite_dev *cdev = f->config->cdev;
243         struct usb_gadget *gadget = cdev->gadget;
244         struct f_fastboot *f_fb = func_to_fastboot(f);
245         const struct usb_endpoint_descriptor *d;
246
247         debug("%s: func: %s intf: %d alt: %d\n",
248               __func__, f->name, interface, alt);
249
250         d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
251         ret = usb_ep_enable(f_fb->out_ep, d);
252         if (ret) {
253                 puts("failed to enable out ep\n");
254                 return ret;
255         }
256
257         f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
258         if (!f_fb->out_req) {
259                 puts("failed to alloc out req\n");
260                 ret = -EINVAL;
261                 goto err;
262         }
263         f_fb->out_req->complete = rx_handler_command;
264
265         d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
266         ret = usb_ep_enable(f_fb->in_ep, d);
267         if (ret) {
268                 puts("failed to enable in ep\n");
269                 goto err;
270         }
271
272         f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
273         if (!f_fb->in_req) {
274                 puts("failed alloc req in\n");
275                 ret = -EINVAL;
276                 goto err;
277         }
278         f_fb->in_req->complete = fastboot_complete;
279
280         ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
281         if (ret)
282                 goto err;
283
284         return 0;
285 err:
286         fastboot_disable(f);
287         return ret;
288 }
289
290 static int fastboot_add(struct usb_configuration *c)
291 {
292         struct f_fastboot *f_fb = fastboot_func;
293         int status;
294
295         debug("%s: cdev: 0x%p\n", __func__, c->cdev);
296
297         if (!f_fb) {
298                 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
299                 if (!f_fb)
300                         return -ENOMEM;
301
302                 fastboot_func = f_fb;
303                 memset(f_fb, 0, sizeof(*f_fb));
304         }
305
306         f_fb->usb_function.name = "f_fastboot";
307         f_fb->usb_function.bind = fastboot_bind;
308         f_fb->usb_function.unbind = fastboot_unbind;
309         f_fb->usb_function.set_alt = fastboot_set_alt;
310         f_fb->usb_function.disable = fastboot_disable;
311         f_fb->usb_function.strings = fastboot_strings;
312
313         status = usb_add_function(c, &f_fb->usb_function);
314         if (status) {
315                 free(f_fb);
316                 fastboot_func = f_fb;
317         }
318
319         return status;
320 }
321 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
322
323 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
324 {
325         struct usb_request *in_req = fastboot_func->in_req;
326         int ret;
327
328         memcpy(in_req->buf, buffer, buffer_size);
329         in_req->length = buffer_size;
330
331         usb_ep_dequeue(fastboot_func->in_ep, in_req);
332
333         ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
334         if (ret)
335                 printf("Error %d on queue\n", ret);
336         return 0;
337 }
338
339 static int fastboot_tx_write_str(const char *buffer)
340 {
341         return fastboot_tx_write(buffer, strlen(buffer));
342 }
343
344 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
345 {
346         do_reset(NULL, 0, 0, NULL);
347 }
348
349 static unsigned int rx_bytes_expected(struct usb_ep *ep)
350 {
351         int rx_remain = fastboot_data_remaining();
352         unsigned int rem;
353         unsigned int maxpacket = ep->maxpacket;
354
355         if (rx_remain <= 0)
356                 return 0;
357         else if (rx_remain > EP_BUFFER_SIZE)
358                 return EP_BUFFER_SIZE;
359
360         /*
361          * Some controllers e.g. DWC3 don't like OUT transfers to be
362          * not ending in maxpacket boundary. So just make them happy by
363          * always requesting for integral multiple of maxpackets.
364          * This shouldn't bother controllers that don't care about it.
365          */
366         rem = rx_remain % maxpacket;
367         if (rem > 0)
368                 rx_remain = rx_remain + (maxpacket - rem);
369
370         return rx_remain;
371 }
372
373 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
374 {
375         char response[FASTBOOT_RESPONSE_LEN] = {0};
376         unsigned int transfer_size = fastboot_data_remaining();
377         const unsigned char *buffer = req->buf;
378         unsigned int buffer_size = req->actual;
379
380         if (req->status != 0) {
381                 printf("Bad status: %d\n", req->status);
382                 return;
383         }
384
385         if (buffer_size < transfer_size)
386                 transfer_size = buffer_size;
387
388         fastboot_data_download(buffer, transfer_size, response);
389         if (response[0]) {
390                 fastboot_tx_write_str(response);
391         } else if (!fastboot_data_remaining()) {
392                 fastboot_data_complete(response);
393
394                 /*
395                  * Reset global transfer variable
396                  */
397                 req->complete = rx_handler_command;
398                 req->length = EP_BUFFER_SIZE;
399
400                 fastboot_tx_write_str(response);
401         } else {
402                 req->length = rx_bytes_expected(ep);
403         }
404
405         req->actual = 0;
406         usb_ep_queue(ep, req, 0);
407 }
408
409 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
410 {
411         g_dnl_trigger_detach();
412 }
413
414 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
415 {
416         fastboot_boot();
417         do_exit_on_complete(ep, req);
418 }
419
420 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
421 {
422         char *cmdbuf = req->buf;
423         char response[FASTBOOT_RESPONSE_LEN] = {0};
424         int cmd = -1;
425
426         if (req->status != 0 || req->length == 0)
427                 return;
428
429         if (req->actual < req->length) {
430                 cmdbuf[req->actual] = '\0';
431                 cmd = fastboot_handle_command(cmdbuf, response);
432         } else {
433                 pr_err("buffer overflow");
434                 fastboot_fail("buffer overflow", response);
435         }
436
437         if (!strncmp("DATA", response, 4)) {
438                 req->complete = rx_handler_dl_image;
439                 req->length = rx_bytes_expected(ep);
440         }
441
442         fastboot_tx_write_str(response);
443
444         if (!strncmp("OKAY", response, 4)) {
445                 switch (cmd) {
446                 case FASTBOOT_COMMAND_BOOT:
447                         fastboot_func->in_req->complete = do_bootm_on_complete;
448                         break;
449
450                 case FASTBOOT_COMMAND_CONTINUE:
451                         fastboot_func->in_req->complete = do_exit_on_complete;
452                         break;
453
454                 case FASTBOOT_COMMAND_REBOOT:
455                 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
456                         fastboot_func->in_req->complete = compl_do_reset;
457                         break;
458                 }
459         }
460
461         *cmdbuf = '\0';
462         req->actual = 0;
463         usb_ep_queue(ep, req, 0);
464 }