Merge branch 'fpga' of git://www.denx.de/git/u-boot-microblaze
[platform/kernel/u-boot.git] / drivers / usb / gadget / f_fastboot.c
1 /*
2  * (C) Copyright 2008 - 2009
3  * Windriver, <www.windriver.com>
4  * Tom Rix <Tom.Rix@windriver.com>
5  *
6  * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7  *
8  * Copyright 2014 Linaro, Ltd.
9  * Rob Herring <robh@kernel.org>
10  *
11  * SPDX-License-Identifier:     GPL-2.0+
12  */
13 #include <common.h>
14 #include <errno.h>
15 #include <malloc.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/composite.h>
19 #include <linux/compiler.h>
20 #include <version.h>
21 #include <g_dnl.h>
22
23 #define FASTBOOT_VERSION                "0.4"
24
25 #define FASTBOOT_INTERFACE_CLASS        0xff
26 #define FASTBOOT_INTERFACE_SUB_CLASS    0x42
27 #define FASTBOOT_INTERFACE_PROTOCOL     0x03
28
29 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
30 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
31 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
32
33 /* The 64 defined bytes plus \0 */
34 #define RESPONSE_LEN    (64 + 1)
35
36 #define EP_BUFFER_SIZE                  4096
37
38 struct f_fastboot {
39         struct usb_function usb_function;
40
41         /* IN/OUT EP's and correspoinding requests */
42         struct usb_ep *in_ep, *out_ep;
43         struct usb_request *in_req, *out_req;
44 };
45
46 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
47 {
48         return container_of(f, struct f_fastboot, usb_function);
49 }
50
51 static struct f_fastboot *fastboot_func;
52 static unsigned int download_size;
53 static unsigned int download_bytes;
54
55 static struct usb_endpoint_descriptor fs_ep_in = {
56         .bLength            = USB_DT_ENDPOINT_SIZE,
57         .bDescriptorType    = USB_DT_ENDPOINT,
58         .bEndpointAddress   = USB_DIR_IN,
59         .bmAttributes       = USB_ENDPOINT_XFER_BULK,
60         .wMaxPacketSize     = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
61         .bInterval          = 0x00,
62 };
63
64 static struct usb_endpoint_descriptor fs_ep_out = {
65         .bLength                = USB_DT_ENDPOINT_SIZE,
66         .bDescriptorType        = USB_DT_ENDPOINT,
67         .bEndpointAddress       = USB_DIR_OUT,
68         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
69         .wMaxPacketSize         = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
70         .bInterval              = 0x00,
71 };
72
73 static struct usb_endpoint_descriptor hs_ep_out = {
74         .bLength                = USB_DT_ENDPOINT_SIZE,
75         .bDescriptorType        = USB_DT_ENDPOINT,
76         .bEndpointAddress       = USB_DIR_OUT,
77         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
78         .wMaxPacketSize         = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
79         .bInterval              = 0x00,
80 };
81
82 static struct usb_interface_descriptor interface_desc = {
83         .bLength                = USB_DT_INTERFACE_SIZE,
84         .bDescriptorType        = USB_DT_INTERFACE,
85         .bInterfaceNumber       = 0x00,
86         .bAlternateSetting      = 0x00,
87         .bNumEndpoints          = 0x02,
88         .bInterfaceClass        = FASTBOOT_INTERFACE_CLASS,
89         .bInterfaceSubClass     = FASTBOOT_INTERFACE_SUB_CLASS,
90         .bInterfaceProtocol     = FASTBOOT_INTERFACE_PROTOCOL,
91 };
92
93 static struct usb_descriptor_header *fb_runtime_descs[] = {
94         (struct usb_descriptor_header *)&interface_desc,
95         (struct usb_descriptor_header *)&fs_ep_in,
96         (struct usb_descriptor_header *)&hs_ep_out,
97         NULL,
98 };
99
100 /*
101  * static strings, in UTF-8
102  */
103 static const char fastboot_name[] = "Android Fastboot";
104
105 static struct usb_string fastboot_string_defs[] = {
106         [0].s = fastboot_name,
107         {  }                    /* end of list */
108 };
109
110 static struct usb_gadget_strings stringtab_fastboot = {
111         .language       = 0x0409,       /* en-us */
112         .strings        = fastboot_string_defs,
113 };
114
115 static struct usb_gadget_strings *fastboot_strings[] = {
116         &stringtab_fastboot,
117         NULL,
118 };
119
120 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
121
122 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
123 {
124         int status = req->status;
125         if (!status)
126                 return;
127         printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
128 }
129
130 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
131 {
132         int id;
133         struct usb_gadget *gadget = c->cdev->gadget;
134         struct f_fastboot *f_fb = func_to_fastboot(f);
135
136         /* DYNAMIC interface numbers assignments */
137         id = usb_interface_id(c, f);
138         if (id < 0)
139                 return id;
140         interface_desc.bInterfaceNumber = id;
141
142         id = usb_string_id(c->cdev);
143         if (id < 0)
144                 return id;
145         fastboot_string_defs[0].id = id;
146         interface_desc.iInterface = id;
147
148         f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
149         if (!f_fb->in_ep)
150                 return -ENODEV;
151         f_fb->in_ep->driver_data = c->cdev;
152
153         f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
154         if (!f_fb->out_ep)
155                 return -ENODEV;
156         f_fb->out_ep->driver_data = c->cdev;
157
158         hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
159
160         return 0;
161 }
162
163 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
164 {
165         memset(fastboot_func, 0, sizeof(*fastboot_func));
166 }
167
168 static void fastboot_disable(struct usb_function *f)
169 {
170         struct f_fastboot *f_fb = func_to_fastboot(f);
171
172         usb_ep_disable(f_fb->out_ep);
173         usb_ep_disable(f_fb->in_ep);
174
175         if (f_fb->out_req) {
176                 free(f_fb->out_req->buf);
177                 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
178                 f_fb->out_req = NULL;
179         }
180         if (f_fb->in_req) {
181                 free(f_fb->in_req->buf);
182                 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
183                 f_fb->in_req = NULL;
184         }
185 }
186
187 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
188 {
189         struct usb_request *req;
190
191         req = usb_ep_alloc_request(ep, 0);
192         if (!req)
193                 return NULL;
194
195         req->length = EP_BUFFER_SIZE;
196         req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
197         if (!req->buf) {
198                 usb_ep_free_request(ep, req);
199                 return NULL;
200         }
201
202         memset(req->buf, 0, req->length);
203         return req;
204 }
205
206 static int fastboot_set_alt(struct usb_function *f,
207                             unsigned interface, unsigned alt)
208 {
209         int ret;
210         struct usb_composite_dev *cdev = f->config->cdev;
211         struct usb_gadget *gadget = cdev->gadget;
212         struct f_fastboot *f_fb = func_to_fastboot(f);
213
214         debug("%s: func: %s intf: %d alt: %d\n",
215               __func__, f->name, interface, alt);
216
217         /* make sure we don't enable the ep twice */
218         if (gadget->speed == USB_SPEED_HIGH)
219                 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
220         else
221                 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
222         if (ret) {
223                 puts("failed to enable out ep\n");
224                 return ret;
225         }
226
227         f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
228         if (!f_fb->out_req) {
229                 puts("failed to alloc out req\n");
230                 ret = -EINVAL;
231                 goto err;
232         }
233         f_fb->out_req->complete = rx_handler_command;
234
235         ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
236         if (ret) {
237                 puts("failed to enable in ep\n");
238                 goto err;
239         }
240
241         f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
242         if (!f_fb->in_req) {
243                 puts("failed alloc req in\n");
244                 ret = -EINVAL;
245                 goto err;
246         }
247         f_fb->in_req->complete = fastboot_complete;
248
249         ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
250         if (ret)
251                 goto err;
252
253         return 0;
254 err:
255         fastboot_disable(f);
256         return ret;
257 }
258
259 static int fastboot_add(struct usb_configuration *c)
260 {
261         struct f_fastboot *f_fb = fastboot_func;
262         int status;
263
264         debug("%s: cdev: 0x%p\n", __func__, c->cdev);
265
266         if (!f_fb) {
267                 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
268                 if (!f_fb)
269                         return -ENOMEM;
270
271                 fastboot_func = f_fb;
272                 memset(f_fb, 0, sizeof(*f_fb));
273         }
274
275         f_fb->usb_function.name = "f_fastboot";
276         f_fb->usb_function.hs_descriptors = fb_runtime_descs;
277         f_fb->usb_function.bind = fastboot_bind;
278         f_fb->usb_function.unbind = fastboot_unbind;
279         f_fb->usb_function.set_alt = fastboot_set_alt;
280         f_fb->usb_function.disable = fastboot_disable;
281         f_fb->usb_function.strings = fastboot_strings;
282
283         status = usb_add_function(c, &f_fb->usb_function);
284         if (status) {
285                 free(f_fb);
286                 fastboot_func = f_fb;
287         }
288
289         return status;
290 }
291 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
292
293 int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
294 {
295         struct usb_request *in_req = fastboot_func->in_req;
296         int ret;
297
298         memcpy(in_req->buf, buffer, buffer_size);
299         in_req->length = buffer_size;
300         ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
301         if (ret)
302                 printf("Error %d on queue\n", ret);
303         return 0;
304 }
305
306 static int fastboot_tx_write_str(const char *buffer)
307 {
308         return fastboot_tx_write(buffer, strlen(buffer));
309 }
310
311 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
312 {
313         do_reset(NULL, 0, 0, NULL);
314 }
315
316 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
317 {
318         fastboot_func->in_req->complete = compl_do_reset;
319         fastboot_tx_write_str("OKAY");
320 }
321
322 static int strcmp_l1(const char *s1, const char *s2)
323 {
324         if (!s1 || !s2)
325                 return -1;
326         return strncmp(s1, s2, strlen(s1));
327 }
328
329 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
330 {
331         char *cmd = req->buf;
332         char response[RESPONSE_LEN];
333         const char *s;
334
335         strcpy(response, "OKAY");
336         strsep(&cmd, ":");
337         if (!cmd) {
338                 fastboot_tx_write_str("FAILmissing var");
339                 return;
340         }
341
342         if (!strcmp_l1("version", cmd)) {
343                 strncat(response, FASTBOOT_VERSION, sizeof(response));
344         } else if (!strcmp_l1("bootloader-version", cmd)) {
345                 strncat(response, U_BOOT_VERSION, sizeof(response));
346         } else if (!strcmp_l1("downloadsize", cmd)) {
347                 char str_num[12];
348
349                 sprintf(str_num, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
350                 strncat(response, str_num, sizeof(response));
351         } else if (!strcmp_l1("serialno", cmd)) {
352                 s = getenv("serial#");
353                 if (s)
354                         strncat(response, s, sizeof(response));
355                 else
356                         strcpy(response, "FAILValue not set");
357         } else {
358                 strcpy(response, "FAILVariable not implemented");
359         }
360         fastboot_tx_write_str(response);
361 }
362
363 static unsigned int rx_bytes_expected(void)
364 {
365         int rx_remain = download_size - download_bytes;
366         if (rx_remain < 0)
367                 return 0;
368         if (rx_remain > EP_BUFFER_SIZE)
369                 return EP_BUFFER_SIZE;
370         return rx_remain;
371 }
372
373 #define BYTES_PER_DOT   0x20000
374 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
375 {
376         char response[RESPONSE_LEN];
377         unsigned int transfer_size = download_size - download_bytes;
378         const unsigned char *buffer = req->buf;
379         unsigned int buffer_size = req->actual;
380
381         if (req->status != 0) {
382                 printf("Bad status: %d\n", req->status);
383                 return;
384         }
385
386         if (buffer_size < transfer_size)
387                 transfer_size = buffer_size;
388
389         memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
390                buffer, transfer_size);
391
392         download_bytes += transfer_size;
393
394         /* Check if transfer is done */
395         if (download_bytes >= download_size) {
396                 /*
397                  * Reset global transfer variable, keep download_bytes because
398                  * it will be used in the next possible flashing command
399                  */
400                 download_size = 0;
401                 req->complete = rx_handler_command;
402                 req->length = EP_BUFFER_SIZE;
403
404                 sprintf(response, "OKAY");
405                 fastboot_tx_write_str(response);
406
407                 printf("\ndownloading of %d bytes finished\n", download_bytes);
408         } else {
409                 req->length = rx_bytes_expected();
410                 if (req->length < ep->maxpacket)
411                         req->length = ep->maxpacket;
412         }
413
414         if (download_bytes && !(download_bytes % BYTES_PER_DOT)) {
415                 putc('.');
416                 if (!(download_bytes % (74 * BYTES_PER_DOT)))
417                         putc('\n');
418         }
419         req->actual = 0;
420         usb_ep_queue(ep, req, 0);
421 }
422
423 static void cb_download(struct usb_ep *ep, struct usb_request *req)
424 {
425         char *cmd = req->buf;
426         char response[RESPONSE_LEN];
427
428         strsep(&cmd, ":");
429         download_size = simple_strtoul(cmd, NULL, 16);
430         download_bytes = 0;
431
432         printf("Starting download of %d bytes\n", download_size);
433
434         if (0 == download_size) {
435                 sprintf(response, "FAILdata invalid size");
436         } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
437                 download_size = 0;
438                 sprintf(response, "FAILdata too large");
439         } else {
440                 sprintf(response, "DATA%08x", download_size);
441                 req->complete = rx_handler_dl_image;
442                 req->length = rx_bytes_expected();
443                 if (req->length < ep->maxpacket)
444                         req->length = ep->maxpacket;
445         }
446         fastboot_tx_write_str(response);
447 }
448
449 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
450 {
451         char boot_addr_start[12];
452         char *bootm_args[] = { "bootm", boot_addr_start, NULL };
453
454         puts("Booting kernel..\n");
455
456         sprintf(boot_addr_start, "0x%lx", load_addr);
457         do_bootm(NULL, 0, 2, bootm_args);
458
459         /* This only happens if image is somehow faulty so we start over */
460         do_reset(NULL, 0, 0, NULL);
461 }
462
463 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
464 {
465         fastboot_func->in_req->complete = do_bootm_on_complete;
466         fastboot_tx_write_str("OKAY");
467 }
468
469 struct cmd_dispatch_info {
470         char *cmd;
471         void (*cb)(struct usb_ep *ep, struct usb_request *req);
472 };
473
474 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
475         {
476                 .cmd = "reboot",
477                 .cb = cb_reboot,
478         }, {
479                 .cmd = "getvar:",
480                 .cb = cb_getvar,
481         }, {
482                 .cmd = "download:",
483                 .cb = cb_download,
484         }, {
485                 .cmd = "boot",
486                 .cb = cb_boot,
487         },
488 };
489
490 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
491 {
492         char *cmdbuf = req->buf;
493         void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
494         int i;
495
496         for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
497                 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
498                         func_cb = cmd_dispatch_info[i].cb;
499                         break;
500                 }
501         }
502
503         if (!func_cb)
504                 fastboot_tx_write_str("FAILunknown command");
505         else
506                 func_cb(ep, req);
507
508         if (req->status == 0) {
509                 *cmdbuf = '\0';
510                 req->actual = 0;
511                 usb_ep_queue(ep, req, 0);
512         }
513 }