1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2009
4 * Windriver, <www.windriver.com>
5 * Tom Rix <Tom.Rix@windriver.com>
7 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * Copyright 2014 Linaro, Ltd.
10 * Rob Herring <robh@kernel.org>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/composite.h>
23 #include <linux/compiler.h>
26 #define FASTBOOT_INTERFACE_CLASS 0xff
27 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
28 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
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)
34 #define EP_BUFFER_SIZE 4096
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.
42 struct usb_function usb_function;
44 /* IN/OUT EP's and corresponding requests */
45 struct usb_ep *in_ep, *out_ep;
46 struct usb_request *in_req, *out_req;
49 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
51 return container_of(f, struct f_fastboot, usb_function);
54 static struct f_fastboot *fastboot_func;
56 static struct usb_endpoint_descriptor fs_ep_in = {
57 .bLength = USB_DT_ENDPOINT_SIZE,
58 .bDescriptorType = USB_DT_ENDPOINT,
59 .bEndpointAddress = USB_DIR_IN,
60 .bmAttributes = USB_ENDPOINT_XFER_BULK,
61 .wMaxPacketSize = cpu_to_le16(64),
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 = cpu_to_le16(64),
72 static struct usb_endpoint_descriptor hs_ep_in = {
73 .bLength = USB_DT_ENDPOINT_SIZE,
74 .bDescriptorType = USB_DT_ENDPOINT,
75 .bEndpointAddress = USB_DIR_IN,
76 .bmAttributes = USB_ENDPOINT_XFER_BULK,
77 .wMaxPacketSize = cpu_to_le16(512),
80 static struct usb_endpoint_descriptor hs_ep_out = {
81 .bLength = USB_DT_ENDPOINT_SIZE,
82 .bDescriptorType = USB_DT_ENDPOINT,
83 .bEndpointAddress = USB_DIR_OUT,
84 .bmAttributes = USB_ENDPOINT_XFER_BULK,
85 .wMaxPacketSize = cpu_to_le16(512),
88 static struct usb_interface_descriptor interface_desc = {
89 .bLength = USB_DT_INTERFACE_SIZE,
90 .bDescriptorType = USB_DT_INTERFACE,
91 .bInterfaceNumber = 0x00,
92 .bAlternateSetting = 0x00,
93 .bNumEndpoints = 0x02,
94 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
95 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
96 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
99 static struct usb_descriptor_header *fb_fs_function[] = {
100 (struct usb_descriptor_header *)&interface_desc,
101 (struct usb_descriptor_header *)&fs_ep_in,
102 (struct usb_descriptor_header *)&fs_ep_out,
105 static struct usb_descriptor_header *fb_hs_function[] = {
106 (struct usb_descriptor_header *)&interface_desc,
107 (struct usb_descriptor_header *)&hs_ep_in,
108 (struct usb_descriptor_header *)&hs_ep_out,
112 static struct usb_endpoint_descriptor *
113 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
114 struct usb_endpoint_descriptor *hs)
116 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
122 * static strings, in UTF-8
124 static const char fastboot_name[] = "Android Fastboot";
126 static struct usb_string fastboot_string_defs[] = {
127 [0].s = fastboot_name,
128 { } /* end of list */
131 static struct usb_gadget_strings stringtab_fastboot = {
132 .language = 0x0409, /* en-us */
133 .strings = fastboot_string_defs,
136 static struct usb_gadget_strings *fastboot_strings[] = {
141 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
143 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
145 int status = req->status;
148 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
151 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
154 struct usb_gadget *gadget = c->cdev->gadget;
155 struct f_fastboot *f_fb = func_to_fastboot(f);
158 /* DYNAMIC interface numbers assignments */
159 id = usb_interface_id(c, f);
162 interface_desc.bInterfaceNumber = id;
164 id = usb_string_id(c->cdev);
167 fastboot_string_defs[0].id = id;
168 interface_desc.iInterface = id;
170 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
173 f_fb->in_ep->driver_data = c->cdev;
175 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
178 f_fb->out_ep->driver_data = c->cdev;
180 f->descriptors = fb_fs_function;
182 if (gadget_is_dualspeed(gadget)) {
183 /* Assume endpoint addresses are the same for both speeds */
184 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
185 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
186 /* copy HS descriptors */
187 f->hs_descriptors = fb_hs_function;
190 s = env_get("serial#");
192 g_dnl_set_serialnumber((char *)s);
197 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
199 memset(fastboot_func, 0, sizeof(*fastboot_func));
202 static void fastboot_disable(struct usb_function *f)
204 struct f_fastboot *f_fb = func_to_fastboot(f);
206 usb_ep_disable(f_fb->out_ep);
207 usb_ep_disable(f_fb->in_ep);
210 free(f_fb->out_req->buf);
211 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
212 f_fb->out_req = NULL;
215 free(f_fb->in_req->buf);
216 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
221 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
223 struct usb_request *req;
225 req = usb_ep_alloc_request(ep, 0);
229 req->length = EP_BUFFER_SIZE;
230 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
232 usb_ep_free_request(ep, req);
236 memset(req->buf, 0, req->length);
240 static int fastboot_set_alt(struct usb_function *f,
241 unsigned interface, unsigned alt)
244 struct usb_composite_dev *cdev = f->config->cdev;
245 struct usb_gadget *gadget = cdev->gadget;
246 struct f_fastboot *f_fb = func_to_fastboot(f);
247 const struct usb_endpoint_descriptor *d;
249 debug("%s: func: %s intf: %d alt: %d\n",
250 __func__, f->name, interface, alt);
252 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
253 ret = usb_ep_enable(f_fb->out_ep, d);
255 puts("failed to enable out ep\n");
259 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
260 if (!f_fb->out_req) {
261 puts("failed to alloc out req\n");
265 f_fb->out_req->complete = rx_handler_command;
267 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
268 ret = usb_ep_enable(f_fb->in_ep, d);
270 puts("failed to enable in ep\n");
274 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
276 puts("failed alloc req in\n");
280 f_fb->in_req->complete = fastboot_complete;
282 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
292 static int fastboot_add(struct usb_configuration *c)
294 struct f_fastboot *f_fb = fastboot_func;
297 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
300 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
304 fastboot_func = f_fb;
305 memset(f_fb, 0, sizeof(*f_fb));
308 f_fb->usb_function.name = "f_fastboot";
309 f_fb->usb_function.bind = fastboot_bind;
310 f_fb->usb_function.unbind = fastboot_unbind;
311 f_fb->usb_function.set_alt = fastboot_set_alt;
312 f_fb->usb_function.disable = fastboot_disable;
313 f_fb->usb_function.strings = fastboot_strings;
315 status = usb_add_function(c, &f_fb->usb_function);
318 fastboot_func = f_fb;
323 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
325 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
327 struct usb_request *in_req = fastboot_func->in_req;
330 memcpy(in_req->buf, buffer, buffer_size);
331 in_req->length = buffer_size;
333 usb_ep_dequeue(fastboot_func->in_ep, in_req);
335 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
337 printf("Error %d on queue\n", ret);
341 static int fastboot_tx_write_str(const char *buffer)
343 return fastboot_tx_write(buffer, strlen(buffer));
346 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
348 do_reset(NULL, 0, 0, NULL);
351 static unsigned int rx_bytes_expected(struct usb_ep *ep)
353 int rx_remain = fastboot_data_remaining();
355 unsigned int maxpacket = ep->maxpacket;
359 else if (rx_remain > EP_BUFFER_SIZE)
360 return EP_BUFFER_SIZE;
363 * Some controllers e.g. DWC3 don't like OUT transfers to be
364 * not ending in maxpacket boundary. So just make them happy by
365 * always requesting for integral multiple of maxpackets.
366 * This shouldn't bother controllers that don't care about it.
368 rem = rx_remain % maxpacket;
370 rx_remain = rx_remain + (maxpacket - rem);
375 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
377 char response[FASTBOOT_RESPONSE_LEN] = {0};
378 unsigned int transfer_size = fastboot_data_remaining();
379 const unsigned char *buffer = req->buf;
380 unsigned int buffer_size = req->actual;
382 if (req->status != 0) {
383 printf("Bad status: %d\n", req->status);
387 if (buffer_size < transfer_size)
388 transfer_size = buffer_size;
390 fastboot_data_download(buffer, transfer_size, response);
392 fastboot_tx_write_str(response);
393 } else if (!fastboot_data_remaining()) {
394 fastboot_data_complete(response);
397 * Reset global transfer variable
399 req->complete = rx_handler_command;
400 req->length = EP_BUFFER_SIZE;
402 fastboot_tx_write_str(response);
404 req->length = rx_bytes_expected(ep);
408 usb_ep_queue(ep, req, 0);
411 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
413 g_dnl_trigger_detach();
416 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
419 do_exit_on_complete(ep, req);
422 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
424 char *cmdbuf = req->buf;
425 char response[FASTBOOT_RESPONSE_LEN] = {0};
428 if (req->status != 0 || req->length == 0)
431 if (req->actual < req->length) {
432 cmdbuf[req->actual] = '\0';
433 cmd = fastboot_handle_command(cmdbuf, response);
435 pr_err("buffer overflow");
436 fastboot_fail("buffer overflow", response);
439 if (!strncmp("DATA", response, 4)) {
440 req->complete = rx_handler_dl_image;
441 req->length = rx_bytes_expected(ep);
444 fastboot_tx_write_str(response);
446 if (!strncmp("OKAY", response, 4)) {
448 case FASTBOOT_COMMAND_BOOT:
449 fastboot_func->in_req->complete = do_bootm_on_complete;
452 case FASTBOOT_COMMAND_CONTINUE:
453 fastboot_func->in_req->complete = do_exit_on_complete;
456 case FASTBOOT_COMMAND_REBOOT:
457 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
458 fastboot_func->in_req->complete = compl_do_reset;
465 usb_ep_queue(ep, req, 0);