blob: d0e92c7a071fc66db78822246bb674bbb6e6c631 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9d4471e2014-05-05 15:08:10 -05002/*
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>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050011 */
Simon Glassed38aef2020-05-10 11:40:03 -060012#include <command.h>
Steve Rae56ad7922014-08-26 11:47:29 -070013#include <config.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050014#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060015#include <env.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050016#include <errno.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020017#include <fastboot.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050019#include <malloc.h>
20#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include <linux/usb/composite.h>
23#include <linux/compiler.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050024#include <g_dnl.h>
25
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050026#define FASTBOOT_INTERFACE_CLASS 0xff
27#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
28#define FASTBOOT_INTERFACE_PROTOCOL 0x03
29
30#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
31#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
32#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
33
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050034#define EP_BUFFER_SIZE 4096
Roger Quadrosaefb0212016-04-19 10:16:59 +030035/*
36 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
37 * (64 or 512 or 1024), else we break on certain controllers like DWC3
38 * that expect bulk OUT requests to be divisible by maxpacket size.
39 */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050040
41struct f_fastboot {
42 struct usb_function usb_function;
43
Steve Rae56ad7922014-08-26 11:47:29 -070044 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050045 struct usb_ep *in_ep, *out_ep;
46 struct usb_request *in_req, *out_req;
47};
48
Li Jun2afadd62021-01-25 21:43:51 +080049static char fb_ext_prop_name[] = "DeviceInterfaceGUID";
50static char fb_ext_prop_data[] = "{4866319A-F4D6-4374-93B9-DC2DEB361BA9}";
51
52static struct usb_os_desc_ext_prop fb_ext_prop = {
53 .type = 1, /* NUL-terminated Unicode String (REG_SZ) */
54 .name = fb_ext_prop_name,
55 .data = fb_ext_prop_data,
56};
57
58/* 16 bytes of "Compatible ID" and "Subcompatible ID" */
59static char fb_cid[16] = {'W', 'I', 'N', 'U', 'S', 'B'};
60static struct usb_os_desc fb_os_desc = {
61 .ext_compat_id = fb_cid,
62};
63
64static struct usb_os_desc_table fb_os_desc_table = {
65 .os_desc = &fb_os_desc,
66};
67
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050068static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
69{
70 return container_of(f, struct f_fastboot, usb_function);
71}
72
73static struct f_fastboot *fastboot_func;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050074
75static struct usb_endpoint_descriptor fs_ep_in = {
76 .bLength = USB_DT_ENDPOINT_SIZE,
77 .bDescriptorType = USB_DT_ENDPOINT,
78 .bEndpointAddress = USB_DIR_IN,
79 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030080 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050081};
82
83static struct usb_endpoint_descriptor fs_ep_out = {
84 .bLength = USB_DT_ENDPOINT_SIZE,
85 .bDescriptorType = USB_DT_ENDPOINT,
86 .bEndpointAddress = USB_DIR_OUT,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030088 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050089};
90
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030091static struct usb_endpoint_descriptor hs_ep_in = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_BULK,
96 .wMaxPacketSize = cpu_to_le16(512),
97};
98
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050099static struct usb_endpoint_descriptor hs_ep_out = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
102 .bEndpointAddress = USB_DIR_OUT,
103 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300104 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500105};
106
107static struct usb_interface_descriptor interface_desc = {
108 .bLength = USB_DT_INTERFACE_SIZE,
109 .bDescriptorType = USB_DT_INTERFACE,
110 .bInterfaceNumber = 0x00,
111 .bAlternateSetting = 0x00,
112 .bNumEndpoints = 0x02,
113 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
114 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
115 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
116};
117
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300118static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500119 (struct usb_descriptor_header *)&interface_desc,
120 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300121 (struct usb_descriptor_header *)&fs_ep_out,
qianfan Zhao64b31692022-08-22 09:18:31 +0800122 NULL,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300123};
124
125static struct usb_descriptor_header *fb_hs_function[] = {
126 (struct usb_descriptor_header *)&interface_desc,
127 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500128 (struct usb_descriptor_header *)&hs_ep_out,
129 NULL,
130};
131
Li Jun394d0092021-01-25 21:43:55 +0800132/* Super speed */
133static struct usb_endpoint_descriptor ss_ep_in = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = cpu_to_le16(1024),
139};
140
141static struct usb_endpoint_descriptor ss_ep_out = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = USB_DIR_OUT,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = cpu_to_le16(1024),
147};
148
149static struct usb_ss_ep_comp_descriptor fb_ss_bulk_comp_desc = {
150 .bLength = sizeof(fb_ss_bulk_comp_desc),
151 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
152};
153
154static struct usb_descriptor_header *fb_ss_function[] = {
155 (struct usb_descriptor_header *)&interface_desc,
156 (struct usb_descriptor_header *)&ss_ep_in,
157 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
158 (struct usb_descriptor_header *)&ss_ep_out,
159 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
160 NULL,
161};
162
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300163static struct usb_endpoint_descriptor *
164fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Li Jun394d0092021-01-25 21:43:55 +0800165 struct usb_endpoint_descriptor *hs,
166 struct usb_endpoint_descriptor *ss)
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300167{
Li Jun394d0092021-01-25 21:43:55 +0800168 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER)
169 return ss;
170
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300171 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
172 return hs;
173 return fs;
174}
175
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500176/*
177 * static strings, in UTF-8
178 */
179static const char fastboot_name[] = "Android Fastboot";
180
181static struct usb_string fastboot_string_defs[] = {
182 [0].s = fastboot_name,
183 { } /* end of list */
184};
185
186static struct usb_gadget_strings stringtab_fastboot = {
187 .language = 0x0409, /* en-us */
188 .strings = fastboot_string_defs,
189};
190
191static struct usb_gadget_strings *fastboot_strings[] = {
192 &stringtab_fastboot,
193 NULL,
194};
195
196static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
197
198static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
199{
200 int status = req->status;
201 if (!status)
202 return;
203 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
204}
205
206static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
207{
208 int id;
209 struct usb_gadget *gadget = c->cdev->gadget;
210 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta447c9752015-02-13 14:33:43 +0800211 const char *s;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500212
213 /* DYNAMIC interface numbers assignments */
214 id = usb_interface_id(c, f);
215 if (id < 0)
216 return id;
217 interface_desc.bInterfaceNumber = id;
218
Li Jun2afadd62021-01-25 21:43:51 +0800219 /* Enable OS and Extended Properties Feature Descriptor */
220 c->cdev->use_os_string = 1;
221 f->os_desc_table = &fb_os_desc_table;
222 f->os_desc_n = 1;
223 f->os_desc_table->if_id = id;
224 INIT_LIST_HEAD(&fb_os_desc.ext_prop);
225 fb_ext_prop.name_len = strlen(fb_ext_prop.name) * 2 + 2;
226 fb_os_desc.ext_prop_len = 10 + fb_ext_prop.name_len;
227 fb_os_desc.ext_prop_count = 1;
228 fb_ext_prop.data_len = strlen(fb_ext_prop.data) * 2 + 2;
229 fb_os_desc.ext_prop_len += fb_ext_prop.data_len + 4;
230 list_add_tail(&fb_ext_prop.entry, &fb_os_desc.ext_prop);
231
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500232 id = usb_string_id(c->cdev);
233 if (id < 0)
234 return id;
235 fastboot_string_defs[0].id = id;
236 interface_desc.iInterface = id;
237
238 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
239 if (!f_fb->in_ep)
240 return -ENODEV;
241 f_fb->in_ep->driver_data = c->cdev;
242
243 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
244 if (!f_fb->out_ep)
245 return -ENODEV;
246 f_fb->out_ep->driver_data = c->cdev;
247
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300248 f->descriptors = fb_fs_function;
249
250 if (gadget_is_dualspeed(gadget)) {
251 /* Assume endpoint addresses are the same for both speeds */
252 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
253 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
254 /* copy HS descriptors */
255 f->hs_descriptors = fb_hs_function;
256 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500257
Li Jun394d0092021-01-25 21:43:55 +0800258 if (gadget_is_superspeed(gadget)) {
259 ss_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
260 ss_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
261 f->ss_descriptors = fb_ss_function;
262 }
263
Simon Glass64b723f2017-08-03 12:22:12 -0600264 s = env_get("serial#");
Dileep Katta447c9752015-02-13 14:33:43 +0800265 if (s)
266 g_dnl_set_serialnumber((char *)s);
267
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500268 return 0;
269}
270
271static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
272{
Li Jun2afadd62021-01-25 21:43:51 +0800273 f->os_desc_table = NULL;
274 list_del(&fb_os_desc.ext_prop);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500275 memset(fastboot_func, 0, sizeof(*fastboot_func));
276}
277
278static void fastboot_disable(struct usb_function *f)
279{
280 struct f_fastboot *f_fb = func_to_fastboot(f);
281
282 usb_ep_disable(f_fb->out_ep);
283 usb_ep_disable(f_fb->in_ep);
284
285 if (f_fb->out_req) {
286 free(f_fb->out_req->buf);
287 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
288 f_fb->out_req = NULL;
289 }
290 if (f_fb->in_req) {
291 free(f_fb->in_req->buf);
292 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
293 f_fb->in_req = NULL;
294 }
295}
296
297static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
298{
299 struct usb_request *req;
300
301 req = usb_ep_alloc_request(ep, 0);
302 if (!req)
303 return NULL;
304
305 req->length = EP_BUFFER_SIZE;
306 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
307 if (!req->buf) {
308 usb_ep_free_request(ep, req);
309 return NULL;
310 }
311
312 memset(req->buf, 0, req->length);
313 return req;
314}
315
316static int fastboot_set_alt(struct usb_function *f,
317 unsigned interface, unsigned alt)
318{
319 int ret;
320 struct usb_composite_dev *cdev = f->config->cdev;
321 struct usb_gadget *gadget = cdev->gadget;
322 struct f_fastboot *f_fb = func_to_fastboot(f);
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300323 const struct usb_endpoint_descriptor *d;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500324
325 debug("%s: func: %s intf: %d alt: %d\n",
326 __func__, f->name, interface, alt);
327
Li Jun394d0092021-01-25 21:43:55 +0800328 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out, &ss_ep_out);
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300329 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500330 if (ret) {
331 puts("failed to enable out ep\n");
332 return ret;
333 }
334
335 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
336 if (!f_fb->out_req) {
337 puts("failed to alloc out req\n");
338 ret = -EINVAL;
339 goto err;
340 }
341 f_fb->out_req->complete = rx_handler_command;
342
Li Jun394d0092021-01-25 21:43:55 +0800343 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in, &ss_ep_in);
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300344 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500345 if (ret) {
346 puts("failed to enable in ep\n");
347 goto err;
348 }
349
350 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
351 if (!f_fb->in_req) {
352 puts("failed alloc req in\n");
353 ret = -EINVAL;
354 goto err;
355 }
356 f_fb->in_req->complete = fastboot_complete;
357
358 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
359 if (ret)
360 goto err;
361
362 return 0;
363err:
364 fastboot_disable(f);
365 return ret;
366}
367
368static int fastboot_add(struct usb_configuration *c)
369{
370 struct f_fastboot *f_fb = fastboot_func;
371 int status;
372
373 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
374
375 if (!f_fb) {
376 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
377 if (!f_fb)
378 return -ENOMEM;
379
380 fastboot_func = f_fb;
381 memset(f_fb, 0, sizeof(*f_fb));
382 }
383
384 f_fb->usb_function.name = "f_fastboot";
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500385 f_fb->usb_function.bind = fastboot_bind;
386 f_fb->usb_function.unbind = fastboot_unbind;
387 f_fb->usb_function.set_alt = fastboot_set_alt;
388 f_fb->usb_function.disable = fastboot_disable;
389 f_fb->usb_function.strings = fastboot_strings;
390
391 status = usb_add_function(c, &f_fb->usb_function);
392 if (status) {
393 free(f_fb);
Andy Shevchenkoab81b2a2020-12-03 17:32:05 +0200394 fastboot_func = NULL;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500395 }
396
397 return status;
398}
399DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
400
Steve Rae56ad7922014-08-26 11:47:29 -0700401static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500402{
403 struct usb_request *in_req = fastboot_func->in_req;
404 int ret;
405
406 memcpy(in_req->buf, buffer, buffer_size);
407 in_req->length = buffer_size;
Paul Kocialkowski2032bfd2015-07-04 16:46:16 +0200408
409 usb_ep_dequeue(fastboot_func->in_ep, in_req);
410
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500411 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
412 if (ret)
413 printf("Error %d on queue\n", ret);
414 return 0;
415}
416
417static int fastboot_tx_write_str(const char *buffer)
418{
419 return fastboot_tx_write(buffer, strlen(buffer));
420}
421
422static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
423{
424 do_reset(NULL, 0, 0, NULL);
425}
426
Roger Quadrosaefb0212016-04-19 10:16:59 +0300427static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500428{
Alex Kiernane80d2942018-05-29 15:30:55 +0000429 int rx_remain = fastboot_data_remaining();
Roger Quadrosaefb0212016-04-19 10:16:59 +0300430 unsigned int rem;
Li Juna4b9f4d2021-01-25 21:43:59 +0800431 unsigned int maxpacket = usb_endpoint_maxp(ep->desc);
Roger Quadrosaefb0212016-04-19 10:16:59 +0300432
433 if (rx_remain <= 0)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500434 return 0;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300435 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500436 return EP_BUFFER_SIZE;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300437
438 /*
439 * Some controllers e.g. DWC3 don't like OUT transfers to be
440 * not ending in maxpacket boundary. So just make them happy by
441 * always requesting for integral multiple of maxpackets.
442 * This shouldn't bother controllers that don't care about it.
443 */
444 rem = rx_remain % maxpacket;
445 if (rem > 0)
Dileep Katta50b4c172015-02-17 02:02:36 +0530446 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosaefb0212016-04-19 10:16:59 +0300447
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500448 return rx_remain;
449}
450
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500451static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
452{
Alex Kiernane80d2942018-05-29 15:30:55 +0000453 char response[FASTBOOT_RESPONSE_LEN] = {0};
454 unsigned int transfer_size = fastboot_data_remaining();
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500455 const unsigned char *buffer = req->buf;
456 unsigned int buffer_size = req->actual;
457
458 if (req->status != 0) {
459 printf("Bad status: %d\n", req->status);
460 return;
461 }
462
463 if (buffer_size < transfer_size)
464 transfer_size = buffer_size;
465
Alex Kiernane80d2942018-05-29 15:30:55 +0000466 fastboot_data_download(buffer, transfer_size, response);
467 if (response[0]) {
468 fastboot_tx_write_str(response);
469 } else if (!fastboot_data_remaining()) {
470 fastboot_data_complete(response);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500471
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500472 /*
Alex Kiernane80d2942018-05-29 15:30:55 +0000473 * Reset global transfer variable
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500474 */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500475 req->complete = rx_handler_command;
476 req->length = EP_BUFFER_SIZE;
477
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500478 fastboot_tx_write_str(response);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500479 } else {
Roger Quadrosaefb0212016-04-19 10:16:59 +0300480 req->length = rx_bytes_expected(ep);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500481 }
482
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500483 req->actual = 0;
484 usb_ep_queue(ep, req, 0);
485}
486
Rob Herring5aba3982014-12-10 14:43:04 -0600487static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
488{
489 g_dnl_trigger_detach();
490}
491
Alex Kiernane80d2942018-05-29 15:30:55 +0000492static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
Rob Herring5aba3982014-12-10 14:43:04 -0600493{
Alex Kiernane80d2942018-05-29 15:30:55 +0000494 fastboot_boot();
495 do_exit_on_complete(ep, req);
Rob Herring5aba3982014-12-10 14:43:04 -0600496}
497
Heiko Schocher3a994482021-02-10 09:29:03 +0100498#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
499static void do_acmd_complete(struct usb_ep *ep, struct usb_request *req)
500{
501 /* When usb dequeue complete will be called
502 * Need status value before call run_command.
503 * otherwise, host can't get last message.
504 */
505 if (req->status == 0)
506 fastboot_acmd_complete();
507}
508#endif
509
Alex Kiernane80d2942018-05-29 15:30:55 +0000510static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
Steve Raebfb9ba42014-08-26 11:47:28 -0700511{
Alex Kiernane80d2942018-05-29 15:30:55 +0000512 char *cmdbuf = req->buf;
513 char response[FASTBOOT_RESPONSE_LEN] = {0};
514 int cmd = -1;
Steve Raebfb9ba42014-08-26 11:47:28 -0700515
Alex Kiernane80d2942018-05-29 15:30:55 +0000516 if (req->status != 0 || req->length == 0)
Steve Raebfb9ba42014-08-26 11:47:28 -0700517 return;
Steve Raebfb9ba42014-08-26 11:47:28 -0700518
Alex Kiernane80d2942018-05-29 15:30:55 +0000519 if (req->actual < req->length) {
520 cmdbuf[req->actual] = '\0';
521 cmd = fastboot_handle_command(cmdbuf, response);
522 } else {
523 pr_err("buffer overflow");
524 fastboot_fail("buffer overflow", response);
Michael Scott7e23abf2015-01-26 15:49:00 -0600525 }
Michael Scott7e23abf2015-01-26 15:49:00 -0600526
Alex Kiernane80d2942018-05-29 15:30:55 +0000527 if (!strncmp("DATA", response, 4)) {
528 req->complete = rx_handler_dl_image;
529 req->length = rx_bytes_expected(ep);
Dileep Kattab1228202015-02-17 18:48:23 +0530530 }
531
Alex Kiernane80d2942018-05-29 15:30:55 +0000532 if (!strncmp("OKAY", response, 4)) {
533 switch (cmd) {
534 case FASTBOOT_COMMAND_BOOT:
535 fastboot_func->in_req->complete = do_bootm_on_complete;
536 break;
Paul Kocialkowski4a126b62015-07-04 16:46:15 +0200537
Alex Kiernane80d2942018-05-29 15:30:55 +0000538 case FASTBOOT_COMMAND_CONTINUE:
539 fastboot_func->in_req->complete = do_exit_on_complete;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500540 break;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500541
Alex Kiernane80d2942018-05-29 15:30:55 +0000542 case FASTBOOT_COMMAND_REBOOT:
543 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
Roman Kovalivskyib30b97b2020-07-28 23:35:33 +0300544 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
545 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
Alex Kiernane80d2942018-05-29 15:30:55 +0000546 fastboot_func->in_req->complete = compl_do_reset;
547 break;
Heiko Schocher3a994482021-02-10 09:29:03 +0100548#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
549 case FASTBOOT_COMMAND_ACMD:
550 fastboot_func->in_req->complete = do_acmd_complete;
551 break;
552#endif
Eric Nelson3d626cd2014-10-01 14:30:56 -0700553 }
Steve Rae56ad7922014-08-26 11:47:29 -0700554 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500555
yurii.pidhornyi5953ffa2020-08-20 18:41:18 +0300556 fastboot_tx_write_str(response);
557
Paul Kocialkowski4a126b62015-07-04 16:46:15 +0200558 *cmdbuf = '\0';
559 req->actual = 0;
560 usb_ep_queue(ep, req, 0);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500561}