1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2009
4 * Windriver, <www.windriver.com>
9 * Copyright 2014 Linaro, Ltd.
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/composite.h>
22 #include <linux/compiler.h>
25 #define FASTBOOT_INTERFACE_CLASS 0xff
26 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
27 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
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)
33 #define EP_BUFFER_SIZE 4096
35 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
36 * (64 or 512 or 1024), else we break on certain controllers like DWC3
37 * that expect bulk OUT requests to be divisible by maxpacket size.
41 struct usb_function usb_function;
43 /* IN/OUT EP's and corresponding requests */
44 struct usb_ep *in_ep, *out_ep;
45 struct usb_request *in_req, *out_req;
48 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
50 return container_of(f, struct f_fastboot, usb_function);
53 static struct f_fastboot *fastboot_func;
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 = cpu_to_le16(64),
63 static struct usb_endpoint_descriptor fs_ep_out = {
64 .bLength = USB_DT_ENDPOINT_SIZE,
65 .bDescriptorType = USB_DT_ENDPOINT,
66 .bEndpointAddress = USB_DIR_OUT,
67 .bmAttributes = USB_ENDPOINT_XFER_BULK,
68 .wMaxPacketSize = cpu_to_le16(64),
71 static struct usb_endpoint_descriptor hs_ep_in = {
72 .bLength = USB_DT_ENDPOINT_SIZE,
73 .bDescriptorType = USB_DT_ENDPOINT,
74 .bEndpointAddress = USB_DIR_IN,
75 .bmAttributes = USB_ENDPOINT_XFER_BULK,
76 .wMaxPacketSize = cpu_to_le16(512),
79 static struct usb_endpoint_descriptor hs_ep_out = {
80 .bLength = USB_DT_ENDPOINT_SIZE,
81 .bDescriptorType = USB_DT_ENDPOINT,
82 .bEndpointAddress = USB_DIR_OUT,
83 .bmAttributes = USB_ENDPOINT_XFER_BULK,
84 .wMaxPacketSize = cpu_to_le16(512),
87 static struct usb_interface_descriptor interface_desc = {
88 .bLength = USB_DT_INTERFACE_SIZE,
89 .bDescriptorType = USB_DT_INTERFACE,
90 .bInterfaceNumber = 0x00,
91 .bAlternateSetting = 0x00,
92 .bNumEndpoints = 0x02,
93 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
94 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
95 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
98 static struct usb_descriptor_header *fb_fs_function[] = {
99 (struct usb_descriptor_header *)&interface_desc,
100 (struct usb_descriptor_header *)&fs_ep_in,
101 (struct usb_descriptor_header *)&fs_ep_out,
104 static struct usb_descriptor_header *fb_hs_function[] = {
105 (struct usb_descriptor_header *)&interface_desc,
106 (struct usb_descriptor_header *)&hs_ep_in,
107 (struct usb_descriptor_header *)&hs_ep_out,
111 static struct usb_endpoint_descriptor *
112 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
113 struct usb_endpoint_descriptor *hs)
115 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
121 * static strings, in UTF-8
123 static const char fastboot_name[] = "Android Fastboot";
125 static struct usb_string fastboot_string_defs[] = {
126 [0].s = fastboot_name,
127 { } /* end of list */
130 static struct usb_gadget_strings stringtab_fastboot = {
131 .language = 0x0409, /* en-us */
132 .strings = fastboot_string_defs,
135 static struct usb_gadget_strings *fastboot_strings[] = {
140 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
142 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
144 int status = req->status;
147 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
150 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
153 struct usb_gadget *gadget = c->cdev->gadget;
154 struct f_fastboot *f_fb = func_to_fastboot(f);
157 /* DYNAMIC interface numbers assignments */
158 id = usb_interface_id(c, f);
161 interface_desc.bInterfaceNumber = id;
163 id = usb_string_id(c->cdev);
166 fastboot_string_defs[0].id = id;
167 interface_desc.iInterface = id;
169 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
172 f_fb->in_ep->driver_data = c->cdev;
174 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
177 f_fb->out_ep->driver_data = c->cdev;
179 f->descriptors = fb_fs_function;
181 if (gadget_is_dualspeed(gadget)) {
182 /* Assume endpoint addresses are the same for both speeds */
183 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
184 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
185 /* copy HS descriptors */
186 f->hs_descriptors = fb_hs_function;
189 s = env_get("serial#");
191 g_dnl_set_serialnumber((char *)s);
196 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
198 memset(fastboot_func, 0, sizeof(*fastboot_func));
201 static void fastboot_disable(struct usb_function *f)
203 struct f_fastboot *f_fb = func_to_fastboot(f);
205 usb_ep_disable(f_fb->out_ep);
206 usb_ep_disable(f_fb->in_ep);
209 free(f_fb->out_req->buf);
210 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
211 f_fb->out_req = NULL;
214 free(f_fb->in_req->buf);
215 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
220 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
222 struct usb_request *req;
224 req = usb_ep_alloc_request(ep, 0);
228 req->length = EP_BUFFER_SIZE;
229 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
231 usb_ep_free_request(ep, req);
235 memset(req->buf, 0, req->length);
239 static int fastboot_set_alt(struct usb_function *f,
240 unsigned interface, unsigned alt)
243 struct usb_composite_dev *cdev = f->config->cdev;
244 struct usb_gadget *gadget = cdev->gadget;
245 struct f_fastboot *f_fb = func_to_fastboot(f);
246 const struct usb_endpoint_descriptor *d;
248 debug("%s: func: %s intf: %d alt: %d\n",
249 __func__, f->name, interface, alt);
251 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
252 ret = usb_ep_enable(f_fb->out_ep, d);
254 puts("failed to enable out ep\n");
258 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
259 if (!f_fb->out_req) {
260 puts("failed to alloc out req\n");
264 f_fb->out_req->complete = rx_handler_command;
266 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
267 ret = usb_ep_enable(f_fb->in_ep, d);
269 puts("failed to enable in ep\n");
273 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
275 puts("failed alloc req in\n");
279 f_fb->in_req->complete = fastboot_complete;
281 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
291 static int fastboot_add(struct usb_configuration *c)
293 struct f_fastboot *f_fb = fastboot_func;
296 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
299 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
303 fastboot_func = f_fb;
304 memset(f_fb, 0, sizeof(*f_fb));
307 f_fb->usb_function.name = "f_fastboot";
308 f_fb->usb_function.bind = fastboot_bind;
309 f_fb->usb_function.unbind = fastboot_unbind;
310 f_fb->usb_function.set_alt = fastboot_set_alt;
311 f_fb->usb_function.disable = fastboot_disable;
312 f_fb->usb_function.strings = fastboot_strings;
314 status = usb_add_function(c, &f_fb->usb_function);
317 fastboot_func = f_fb;
322 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
324 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
326 struct usb_request *in_req = fastboot_func->in_req;
329 memcpy(in_req->buf, buffer, buffer_size);
330 in_req->length = buffer_size;
332 usb_ep_dequeue(fastboot_func->in_ep, in_req);
334 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
336 printf("Error %d on queue\n", ret);
340 static int fastboot_tx_write_str(const char *buffer)
342 return fastboot_tx_write(buffer, strlen(buffer));
345 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
347 do_reset(NULL, 0, 0, NULL);
350 static unsigned int rx_bytes_expected(struct usb_ep *ep)
352 int rx_remain = fastboot_data_remaining();
354 unsigned int maxpacket = ep->maxpacket;
358 else if (rx_remain > EP_BUFFER_SIZE)
359 return EP_BUFFER_SIZE;
362 * Some controllers e.g. DWC3 don't like OUT transfers to be
363 * not ending in maxpacket boundary. So just make them happy by
364 * always requesting for integral multiple of maxpackets.
365 * This shouldn't bother controllers that don't care about it.
367 rem = rx_remain % maxpacket;
369 rx_remain = rx_remain + (maxpacket - rem);
374 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
376 char response[FASTBOOT_RESPONSE_LEN] = {0};
377 unsigned int transfer_size = fastboot_data_remaining();
378 const unsigned char *buffer = req->buf;
379 unsigned int buffer_size = req->actual;
381 if (req->status != 0) {
382 printf("Bad status: %d\n", req->status);
386 if (buffer_size < transfer_size)
387 transfer_size = buffer_size;
389 fastboot_data_download(buffer, transfer_size, response);
391 fastboot_tx_write_str(response);
392 } else if (!fastboot_data_remaining()) {
393 fastboot_data_complete(response);
396 * Reset global transfer variable
398 req->complete = rx_handler_command;
399 req->length = EP_BUFFER_SIZE;
401 fastboot_tx_write_str(response);
403 req->length = rx_bytes_expected(ep);
407 usb_ep_queue(ep, req, 0);
410 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
412 g_dnl_trigger_detach();
415 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
418 do_exit_on_complete(ep, req);
421 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
423 char *cmdbuf = req->buf;
424 char response[FASTBOOT_RESPONSE_LEN] = {0};
427 if (req->status != 0 || req->length == 0)
430 if (req->actual < req->length) {
431 cmdbuf[req->actual] = '\0';
432 cmd = fastboot_handle_command(cmdbuf, response);
434 pr_err("buffer overflow");
435 fastboot_fail("buffer overflow", response);
438 if (!strncmp("DATA", response, 4)) {
439 req->complete = rx_handler_dl_image;
440 req->length = rx_bytes_expected(ep);
443 fastboot_tx_write_str(response);
445 if (!strncmp("OKAY", response, 4)) {
447 case FASTBOOT_COMMAND_BOOT:
448 fastboot_func->in_req->complete = do_bootm_on_complete;
451 case FASTBOOT_COMMAND_CONTINUE:
452 fastboot_func->in_req->complete = do_exit_on_complete;
455 case FASTBOOT_COMMAND_REBOOT:
456 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
457 fastboot_func->in_req->complete = compl_do_reset;
464 usb_ep_queue(ep, req, 0);