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>
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/gadget.h>
20 #include <linux/usb/composite.h>
21 #include <linux/compiler.h>
24 #define FASTBOOT_INTERFACE_CLASS 0xff
25 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
26 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
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)
32 #define EP_BUFFER_SIZE 4096
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.
40 struct usb_function usb_function;
42 /* IN/OUT EP's and corresponding requests */
43 struct usb_ep *in_ep, *out_ep;
44 struct usb_request *in_req, *out_req;
47 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
49 return container_of(f, struct f_fastboot, usb_function);
52 static struct f_fastboot *fastboot_func;
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),
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),
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),
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),
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,
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,
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,
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)
114 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
120 * static strings, in UTF-8
122 static const char fastboot_name[] = "Android Fastboot";
124 static struct usb_string fastboot_string_defs[] = {
125 [0].s = fastboot_name,
126 { } /* end of list */
129 static struct usb_gadget_strings stringtab_fastboot = {
130 .language = 0x0409, /* en-us */
131 .strings = fastboot_string_defs,
134 static struct usb_gadget_strings *fastboot_strings[] = {
139 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
141 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
143 int status = req->status;
146 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
149 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
152 struct usb_gadget *gadget = c->cdev->gadget;
153 struct f_fastboot *f_fb = func_to_fastboot(f);
156 /* DYNAMIC interface numbers assignments */
157 id = usb_interface_id(c, f);
160 interface_desc.bInterfaceNumber = id;
162 id = usb_string_id(c->cdev);
165 fastboot_string_defs[0].id = id;
166 interface_desc.iInterface = id;
168 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
171 f_fb->in_ep->driver_data = c->cdev;
173 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
176 f_fb->out_ep->driver_data = c->cdev;
178 f->descriptors = fb_fs_function;
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;
188 s = env_get("serial#");
190 g_dnl_set_serialnumber((char *)s);
195 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
197 memset(fastboot_func, 0, sizeof(*fastboot_func));
200 static void fastboot_disable(struct usb_function *f)
202 struct f_fastboot *f_fb = func_to_fastboot(f);
204 usb_ep_disable(f_fb->out_ep);
205 usb_ep_disable(f_fb->in_ep);
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;
213 free(f_fb->in_req->buf);
214 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
219 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
221 struct usb_request *req;
223 req = usb_ep_alloc_request(ep, 0);
227 req->length = EP_BUFFER_SIZE;
228 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
230 usb_ep_free_request(ep, req);
234 memset(req->buf, 0, req->length);
238 static int fastboot_set_alt(struct usb_function *f,
239 unsigned interface, unsigned alt)
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;
247 debug("%s: func: %s intf: %d alt: %d\n",
248 __func__, f->name, interface, alt);
250 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
251 ret = usb_ep_enable(f_fb->out_ep, d);
253 puts("failed to enable out ep\n");
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");
263 f_fb->out_req->complete = rx_handler_command;
265 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
266 ret = usb_ep_enable(f_fb->in_ep, d);
268 puts("failed to enable in ep\n");
272 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
274 puts("failed alloc req in\n");
278 f_fb->in_req->complete = fastboot_complete;
280 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
290 static int fastboot_add(struct usb_configuration *c)
292 struct f_fastboot *f_fb = fastboot_func;
295 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
298 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
302 fastboot_func = f_fb;
303 memset(f_fb, 0, sizeof(*f_fb));
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;
313 status = usb_add_function(c, &f_fb->usb_function);
316 fastboot_func = f_fb;
321 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
323 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
325 struct usb_request *in_req = fastboot_func->in_req;
328 memcpy(in_req->buf, buffer, buffer_size);
329 in_req->length = buffer_size;
331 usb_ep_dequeue(fastboot_func->in_ep, in_req);
333 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
335 printf("Error %d on queue\n", ret);
339 static int fastboot_tx_write_str(const char *buffer)
341 return fastboot_tx_write(buffer, strlen(buffer));
344 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
346 do_reset(NULL, 0, 0, NULL);
349 static unsigned int rx_bytes_expected(struct usb_ep *ep)
351 int rx_remain = fastboot_data_remaining();
353 unsigned int maxpacket = ep->maxpacket;
357 else if (rx_remain > EP_BUFFER_SIZE)
358 return EP_BUFFER_SIZE;
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.
366 rem = rx_remain % maxpacket;
368 rx_remain = rx_remain + (maxpacket - rem);
373 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
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;
380 if (req->status != 0) {
381 printf("Bad status: %d\n", req->status);
385 if (buffer_size < transfer_size)
386 transfer_size = buffer_size;
388 fastboot_data_download(buffer, transfer_size, response);
390 fastboot_tx_write_str(response);
391 } else if (!fastboot_data_remaining()) {
392 fastboot_data_complete(response);
395 * Reset global transfer variable
397 req->complete = rx_handler_command;
398 req->length = EP_BUFFER_SIZE;
400 fastboot_tx_write_str(response);
402 req->length = rx_bytes_expected(ep);
406 usb_ep_queue(ep, req, 0);
409 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
411 g_dnl_trigger_detach();
414 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
417 do_exit_on_complete(ep, req);
420 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
422 char *cmdbuf = req->buf;
423 char response[FASTBOOT_RESPONSE_LEN] = {0};
426 if (req->status != 0 || req->length == 0)
429 if (req->actual < req->length) {
430 cmdbuf[req->actual] = '\0';
431 cmd = fastboot_handle_command(cmdbuf, response);
433 pr_err("buffer overflow");
434 fastboot_fail("buffer overflow", response);
437 if (!strncmp("DATA", response, 4)) {
438 req->complete = rx_handler_dl_image;
439 req->length = rx_bytes_expected(ep);
442 fastboot_tx_write_str(response);
444 if (!strncmp("OKAY", response, 4)) {
446 case FASTBOOT_COMMAND_BOOT:
447 fastboot_func->in_req->complete = do_bootm_on_complete;
450 case FASTBOOT_COMMAND_CONTINUE:
451 fastboot_func->in_req->complete = do_exit_on_complete;
454 case FASTBOOT_COMMAND_REBOOT:
455 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
456 fastboot_func->in_req->complete = compl_do_reset;
463 usb_ep_queue(ep, req, 0);