]> git.sur5r.net Git - u-boot/blob - drivers/usb/gadget/f_fastboot.c
fastboot: Rename fb_set_reboot_flag to fastboot_set_reboot_flag
[u-boot] / 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 <errno.h>
15 #include <fastboot.h>
16 #include <malloc.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/composite.h>
20 #include <linux/compiler.h>
21 #include <version.h>
22 #include <g_dnl.h>
23 #ifdef CONFIG_FASTBOOT_FLASH_MMC
24 #include <fb_mmc.h>
25 #endif
26 #ifdef CONFIG_FASTBOOT_FLASH_NAND
27 #include <fb_nand.h>
28 #endif
29
30 #define FASTBOOT_VERSION                "0.4"
31
32 #define FASTBOOT_INTERFACE_CLASS        0xff
33 #define FASTBOOT_INTERFACE_SUB_CLASS    0x42
34 #define FASTBOOT_INTERFACE_PROTOCOL     0x03
35
36 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
37 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
38 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
39
40 #define EP_BUFFER_SIZE                  4096
41 /*
42  * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
43  * (64 or 512 or 1024), else we break on certain controllers like DWC3
44  * that expect bulk OUT requests to be divisible by maxpacket size.
45  */
46
47 struct f_fastboot {
48         struct usb_function usb_function;
49
50         /* IN/OUT EP's and corresponding requests */
51         struct usb_ep *in_ep, *out_ep;
52         struct usb_request *in_req, *out_req;
53 };
54
55 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
56 {
57         return container_of(f, struct f_fastboot, usb_function);
58 }
59
60 static struct f_fastboot *fastboot_func;
61 static unsigned int download_size;
62 static unsigned int download_bytes;
63
64 static struct usb_endpoint_descriptor fs_ep_in = {
65         .bLength            = USB_DT_ENDPOINT_SIZE,
66         .bDescriptorType    = USB_DT_ENDPOINT,
67         .bEndpointAddress   = USB_DIR_IN,
68         .bmAttributes       = USB_ENDPOINT_XFER_BULK,
69         .wMaxPacketSize     = cpu_to_le16(64),
70 };
71
72 static struct usb_endpoint_descriptor fs_ep_out = {
73         .bLength                = USB_DT_ENDPOINT_SIZE,
74         .bDescriptorType        = USB_DT_ENDPOINT,
75         .bEndpointAddress       = USB_DIR_OUT,
76         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
77         .wMaxPacketSize         = cpu_to_le16(64),
78 };
79
80 static struct usb_endpoint_descriptor hs_ep_in = {
81         .bLength                = USB_DT_ENDPOINT_SIZE,
82         .bDescriptorType        = USB_DT_ENDPOINT,
83         .bEndpointAddress       = USB_DIR_IN,
84         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
85         .wMaxPacketSize         = cpu_to_le16(512),
86 };
87
88 static struct usb_endpoint_descriptor hs_ep_out = {
89         .bLength                = USB_DT_ENDPOINT_SIZE,
90         .bDescriptorType        = USB_DT_ENDPOINT,
91         .bEndpointAddress       = USB_DIR_OUT,
92         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
93         .wMaxPacketSize         = cpu_to_le16(512),
94 };
95
96 static struct usb_interface_descriptor interface_desc = {
97         .bLength                = USB_DT_INTERFACE_SIZE,
98         .bDescriptorType        = USB_DT_INTERFACE,
99         .bInterfaceNumber       = 0x00,
100         .bAlternateSetting      = 0x00,
101         .bNumEndpoints          = 0x02,
102         .bInterfaceClass        = FASTBOOT_INTERFACE_CLASS,
103         .bInterfaceSubClass     = FASTBOOT_INTERFACE_SUB_CLASS,
104         .bInterfaceProtocol     = FASTBOOT_INTERFACE_PROTOCOL,
105 };
106
107 static struct usb_descriptor_header *fb_fs_function[] = {
108         (struct usb_descriptor_header *)&interface_desc,
109         (struct usb_descriptor_header *)&fs_ep_in,
110         (struct usb_descriptor_header *)&fs_ep_out,
111 };
112
113 static struct usb_descriptor_header *fb_hs_function[] = {
114         (struct usb_descriptor_header *)&interface_desc,
115         (struct usb_descriptor_header *)&hs_ep_in,
116         (struct usb_descriptor_header *)&hs_ep_out,
117         NULL,
118 };
119
120 static struct usb_endpoint_descriptor *
121 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
122             struct usb_endpoint_descriptor *hs)
123 {
124         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
125                 return hs;
126         return fs;
127 }
128
129 /*
130  * static strings, in UTF-8
131  */
132 static const char fastboot_name[] = "Android Fastboot";
133
134 static struct usb_string fastboot_string_defs[] = {
135         [0].s = fastboot_name,
136         {  }                    /* end of list */
137 };
138
139 static struct usb_gadget_strings stringtab_fastboot = {
140         .language       = 0x0409,       /* en-us */
141         .strings        = fastboot_string_defs,
142 };
143
144 static struct usb_gadget_strings *fastboot_strings[] = {
145         &stringtab_fastboot,
146         NULL,
147 };
148
149 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
150 static int strcmp_l1(const char *s1, const char *s2);
151
152 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
153 {
154         int status = req->status;
155         if (!status)
156                 return;
157         printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
158 }
159
160 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
161 {
162         int id;
163         struct usb_gadget *gadget = c->cdev->gadget;
164         struct f_fastboot *f_fb = func_to_fastboot(f);
165         const char *s;
166
167         /* DYNAMIC interface numbers assignments */
168         id = usb_interface_id(c, f);
169         if (id < 0)
170                 return id;
171         interface_desc.bInterfaceNumber = id;
172
173         id = usb_string_id(c->cdev);
174         if (id < 0)
175                 return id;
176         fastboot_string_defs[0].id = id;
177         interface_desc.iInterface = id;
178
179         f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
180         if (!f_fb->in_ep)
181                 return -ENODEV;
182         f_fb->in_ep->driver_data = c->cdev;
183
184         f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
185         if (!f_fb->out_ep)
186                 return -ENODEV;
187         f_fb->out_ep->driver_data = c->cdev;
188
189         f->descriptors = fb_fs_function;
190
191         if (gadget_is_dualspeed(gadget)) {
192                 /* Assume endpoint addresses are the same for both speeds */
193                 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
194                 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
195                 /* copy HS descriptors */
196                 f->hs_descriptors = fb_hs_function;
197         }
198
199         s = env_get("serial#");
200         if (s)
201                 g_dnl_set_serialnumber((char *)s);
202
203         return 0;
204 }
205
206 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
207 {
208         memset(fastboot_func, 0, sizeof(*fastboot_func));
209 }
210
211 static void fastboot_disable(struct usb_function *f)
212 {
213         struct f_fastboot *f_fb = func_to_fastboot(f);
214
215         usb_ep_disable(f_fb->out_ep);
216         usb_ep_disable(f_fb->in_ep);
217
218         if (f_fb->out_req) {
219                 free(f_fb->out_req->buf);
220                 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
221                 f_fb->out_req = NULL;
222         }
223         if (f_fb->in_req) {
224                 free(f_fb->in_req->buf);
225                 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
226                 f_fb->in_req = NULL;
227         }
228 }
229
230 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
231 {
232         struct usb_request *req;
233
234         req = usb_ep_alloc_request(ep, 0);
235         if (!req)
236                 return NULL;
237
238         req->length = EP_BUFFER_SIZE;
239         req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
240         if (!req->buf) {
241                 usb_ep_free_request(ep, req);
242                 return NULL;
243         }
244
245         memset(req->buf, 0, req->length);
246         return req;
247 }
248
249 static int fastboot_set_alt(struct usb_function *f,
250                             unsigned interface, unsigned alt)
251 {
252         int ret;
253         struct usb_composite_dev *cdev = f->config->cdev;
254         struct usb_gadget *gadget = cdev->gadget;
255         struct f_fastboot *f_fb = func_to_fastboot(f);
256         const struct usb_endpoint_descriptor *d;
257
258         debug("%s: func: %s intf: %d alt: %d\n",
259               __func__, f->name, interface, alt);
260
261         d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
262         ret = usb_ep_enable(f_fb->out_ep, d);
263         if (ret) {
264                 puts("failed to enable out ep\n");
265                 return ret;
266         }
267
268         f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
269         if (!f_fb->out_req) {
270                 puts("failed to alloc out req\n");
271                 ret = -EINVAL;
272                 goto err;
273         }
274         f_fb->out_req->complete = rx_handler_command;
275
276         d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
277         ret = usb_ep_enable(f_fb->in_ep, d);
278         if (ret) {
279                 puts("failed to enable in ep\n");
280                 goto err;
281         }
282
283         f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
284         if (!f_fb->in_req) {
285                 puts("failed alloc req in\n");
286                 ret = -EINVAL;
287                 goto err;
288         }
289         f_fb->in_req->complete = fastboot_complete;
290
291         ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
292         if (ret)
293                 goto err;
294
295         return 0;
296 err:
297         fastboot_disable(f);
298         return ret;
299 }
300
301 static int fastboot_add(struct usb_configuration *c)
302 {
303         struct f_fastboot *f_fb = fastboot_func;
304         int status;
305
306         debug("%s: cdev: 0x%p\n", __func__, c->cdev);
307
308         if (!f_fb) {
309                 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
310                 if (!f_fb)
311                         return -ENOMEM;
312
313                 fastboot_func = f_fb;
314                 memset(f_fb, 0, sizeof(*f_fb));
315         }
316
317         f_fb->usb_function.name = "f_fastboot";
318         f_fb->usb_function.bind = fastboot_bind;
319         f_fb->usb_function.unbind = fastboot_unbind;
320         f_fb->usb_function.set_alt = fastboot_set_alt;
321         f_fb->usb_function.disable = fastboot_disable;
322         f_fb->usb_function.strings = fastboot_strings;
323
324         status = usb_add_function(c, &f_fb->usb_function);
325         if (status) {
326                 free(f_fb);
327                 fastboot_func = f_fb;
328         }
329
330         return status;
331 }
332 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
333
334 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
335 {
336         struct usb_request *in_req = fastboot_func->in_req;
337         int ret;
338
339         memcpy(in_req->buf, buffer, buffer_size);
340         in_req->length = buffer_size;
341
342         usb_ep_dequeue(fastboot_func->in_ep, in_req);
343
344         ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
345         if (ret)
346                 printf("Error %d on queue\n", ret);
347         return 0;
348 }
349
350 static int fastboot_tx_write_str(const char *buffer)
351 {
352         return fastboot_tx_write(buffer, strlen(buffer));
353 }
354
355 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
356 {
357         do_reset(NULL, 0, 0, NULL);
358 }
359
360 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
361 {
362         char *cmd = req->buf;
363         if (!strcmp_l1("reboot-bootloader", cmd)) {
364                 if (fastboot_set_reboot_flag()) {
365                         fastboot_tx_write_str("FAILCannot set reboot flag");
366                         return;
367                 }
368         }
369         fastboot_func->in_req->complete = compl_do_reset;
370         fastboot_tx_write_str("OKAY");
371 }
372
373 static int strcmp_l1(const char *s1, const char *s2)
374 {
375         if (!s1 || !s2)
376                 return -1;
377         return strncmp(s1, s2, strlen(s1));
378 }
379
380 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
381 {
382         char *cmd = req->buf;
383         char response[FASTBOOT_RESPONSE_LEN];
384         const char *s;
385         size_t chars_left;
386
387         strcpy(response, "OKAY");
388         chars_left = sizeof(response) - strlen(response) - 1;
389
390         strsep(&cmd, ":");
391         if (!cmd) {
392                 pr_err("missing variable");
393                 fastboot_tx_write_str("FAILmissing var");
394                 return;
395         }
396
397         if (!strcmp_l1("version", cmd)) {
398                 strncat(response, FASTBOOT_VERSION, chars_left);
399         } else if (!strcmp_l1("bootloader-version", cmd)) {
400                 strncat(response, U_BOOT_VERSION, chars_left);
401         } else if (!strcmp_l1("downloadsize", cmd) ||
402                 !strcmp_l1("max-download-size", cmd)) {
403                 char str_num[12];
404
405                 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
406                 strncat(response, str_num, chars_left);
407         } else if (!strcmp_l1("serialno", cmd)) {
408                 s = env_get("serial#");
409                 if (s)
410                         strncat(response, s, chars_left);
411                 else
412                         strcpy(response, "FAILValue not set");
413         } else {
414                 char *envstr;
415
416                 envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
417                 if (!envstr) {
418                         fastboot_tx_write_str("FAILmalloc error");
419                         return;
420                 }
421
422                 sprintf(envstr, "fastboot.%s", cmd);
423                 s = env_get(envstr);
424                 if (s) {
425                         strncat(response, s, chars_left);
426                 } else {
427                         printf("WARNING: unknown variable: %s\n", cmd);
428                         strcpy(response, "FAILVariable not implemented");
429                 }
430
431                 free(envstr);
432         }
433         fastboot_tx_write_str(response);
434 }
435
436 static unsigned int rx_bytes_expected(struct usb_ep *ep)
437 {
438         int rx_remain = download_size - download_bytes;
439         unsigned int rem;
440         unsigned int maxpacket = ep->maxpacket;
441
442         if (rx_remain <= 0)
443                 return 0;
444         else if (rx_remain > EP_BUFFER_SIZE)
445                 return EP_BUFFER_SIZE;
446
447         /*
448          * Some controllers e.g. DWC3 don't like OUT transfers to be
449          * not ending in maxpacket boundary. So just make them happy by
450          * always requesting for integral multiple of maxpackets.
451          * This shouldn't bother controllers that don't care about it.
452          */
453         rem = rx_remain % maxpacket;
454         if (rem > 0)
455                 rx_remain = rx_remain + (maxpacket - rem);
456
457         return rx_remain;
458 }
459
460 #define BYTES_PER_DOT   0x20000
461 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
462 {
463         char response[FASTBOOT_RESPONSE_LEN];
464         unsigned int transfer_size = download_size - download_bytes;
465         const unsigned char *buffer = req->buf;
466         unsigned int buffer_size = req->actual;
467         unsigned int pre_dot_num, now_dot_num;
468
469         if (req->status != 0) {
470                 printf("Bad status: %d\n", req->status);
471                 return;
472         }
473
474         if (buffer_size < transfer_size)
475                 transfer_size = buffer_size;
476
477         memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
478                buffer, transfer_size);
479
480         pre_dot_num = download_bytes / BYTES_PER_DOT;
481         download_bytes += transfer_size;
482         now_dot_num = download_bytes / BYTES_PER_DOT;
483
484         if (pre_dot_num != now_dot_num) {
485                 putc('.');
486                 if (!(now_dot_num % 74))
487                         putc('\n');
488         }
489
490         /* Check if transfer is done */
491         if (download_bytes >= download_size) {
492                 /*
493                  * Reset global transfer variable, keep download_bytes because
494                  * it will be used in the next possible flashing command
495                  */
496                 download_size = 0;
497                 req->complete = rx_handler_command;
498                 req->length = EP_BUFFER_SIZE;
499
500                 strcpy(response, "OKAY");
501                 fastboot_tx_write_str(response);
502
503                 printf("\ndownloading of %d bytes finished\n", download_bytes);
504         } else {
505                 req->length = rx_bytes_expected(ep);
506         }
507
508         req->actual = 0;
509         usb_ep_queue(ep, req, 0);
510 }
511
512 static void cb_download(struct usb_ep *ep, struct usb_request *req)
513 {
514         char *cmd = req->buf;
515         char response[FASTBOOT_RESPONSE_LEN];
516
517         strsep(&cmd, ":");
518         download_size = simple_strtoul(cmd, NULL, 16);
519         download_bytes = 0;
520
521         printf("Starting download of %d bytes\n", download_size);
522
523         if (0 == download_size) {
524                 strcpy(response, "FAILdata invalid size");
525         } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
526                 download_size = 0;
527                 strcpy(response, "FAILdata too large");
528         } else {
529                 sprintf(response, "DATA%08x", download_size);
530                 req->complete = rx_handler_dl_image;
531                 req->length = rx_bytes_expected(ep);
532         }
533         fastboot_tx_write_str(response);
534 }
535
536 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
537 {
538         char boot_addr_start[12];
539         char *bootm_args[] = { "bootm", boot_addr_start, NULL };
540
541         puts("Booting kernel..\n");
542
543         sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
544         do_bootm(NULL, 0, 2, bootm_args);
545
546         /* This only happens if image is somehow faulty so we start over */
547         do_reset(NULL, 0, 0, NULL);
548 }
549
550 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
551 {
552         fastboot_func->in_req->complete = do_bootm_on_complete;
553         fastboot_tx_write_str("OKAY");
554 }
555
556 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
557 {
558         g_dnl_trigger_detach();
559 }
560
561 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
562 {
563         fastboot_func->in_req->complete = do_exit_on_complete;
564         fastboot_tx_write_str("OKAY");
565 }
566
567 #ifdef CONFIG_FASTBOOT_FLASH
568 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
569 {
570         char *cmd = req->buf;
571         char response[FASTBOOT_RESPONSE_LEN];
572
573         strsep(&cmd, ":");
574         if (!cmd) {
575                 pr_err("missing partition name");
576                 fastboot_tx_write_str("FAILmissing partition name");
577                 return;
578         }
579
580         fastboot_fail("no flash device defined", response);
581 #ifdef CONFIG_FASTBOOT_FLASH_MMC
582         fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
583                            download_bytes, response);
584 #endif
585 #ifdef CONFIG_FASTBOOT_FLASH_NAND
586         fb_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
587                             download_bytes, response);
588 #endif
589         fastboot_tx_write_str(response);
590 }
591 #endif
592
593 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
594 {
595         char *cmd = req->buf;
596 #ifdef CONFIG_FASTBOOT_FLASH_MMC
597         if (strncmp("format", cmd + 4, 6) == 0) {
598                 char cmdbuf[32];
599                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
600                         CONFIG_FASTBOOT_FLASH_MMC_DEV);
601                 if (run_command(cmdbuf, 0))
602                         fastboot_tx_write_str("FAIL");
603                 else
604                         fastboot_tx_write_str("OKAY");
605         } else
606 #endif
607         if (strncmp("unlock", cmd + 4, 8) == 0) {
608                 fastboot_tx_write_str("FAILnot implemented");
609         }
610         else {
611                 fastboot_tx_write_str("FAILunknown oem command");
612         }
613 }
614
615 #ifdef CONFIG_FASTBOOT_FLASH
616 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
617 {
618         char *cmd = req->buf;
619         char response[FASTBOOT_RESPONSE_LEN];
620
621         strsep(&cmd, ":");
622         if (!cmd) {
623                 pr_err("missing partition name");
624                 fastboot_tx_write_str("FAILmissing partition name");
625                 return;
626         }
627
628         fastboot_fail("no flash device defined", response);
629 #ifdef CONFIG_FASTBOOT_FLASH_MMC
630         fb_mmc_erase(cmd, response);
631 #endif
632 #ifdef CONFIG_FASTBOOT_FLASH_NAND
633         fb_nand_erase(cmd, response);
634 #endif
635         fastboot_tx_write_str(response);
636 }
637 #endif
638
639 struct cmd_dispatch_info {
640         char *cmd;
641         void (*cb)(struct usb_ep *ep, struct usb_request *req);
642 };
643
644 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
645         {
646                 .cmd = "reboot",
647                 .cb = cb_reboot,
648         }, {
649                 .cmd = "getvar:",
650                 .cb = cb_getvar,
651         }, {
652                 .cmd = "download:",
653                 .cb = cb_download,
654         }, {
655                 .cmd = "boot",
656                 .cb = cb_boot,
657         }, {
658                 .cmd = "continue",
659                 .cb = cb_continue,
660         },
661 #ifdef CONFIG_FASTBOOT_FLASH
662         {
663                 .cmd = "flash",
664                 .cb = cb_flash,
665         }, {
666                 .cmd = "erase",
667                 .cb = cb_erase,
668         },
669 #endif
670         {
671                 .cmd = "oem",
672                 .cb = cb_oem,
673         },
674 };
675
676 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
677 {
678         char *cmdbuf = req->buf;
679         void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
680         int i;
681
682         if (req->status != 0 || req->length == 0)
683                 return;
684
685         for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
686                 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
687                         func_cb = cmd_dispatch_info[i].cb;
688                         break;
689                 }
690         }
691
692         if (!func_cb) {
693                 pr_err("unknown command: %.*s", req->actual, cmdbuf);
694                 fastboot_tx_write_str("FAILunknown command");
695         } else {
696                 if (req->actual < req->length) {
697                         u8 *buf = (u8 *)req->buf;
698                         buf[req->actual] = 0;
699                         func_cb(ep, req);
700                 } else {
701                         pr_err("buffer overflow");
702                         fastboot_tx_write_str("FAILbuffer overflow");
703                 }
704         }
705
706         *cmdbuf = '\0';
707         req->actual = 0;
708         usb_ep_queue(ep, req, 0);
709 }