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 char fb_ext_prop_name[] = "DeviceInterfaceGUID";
50 static char fb_ext_prop_data[] = "{4866319A-F4D6-4374-93B9-DC2DEB361BA9}";
52 static struct usb_os_desc_ext_prop fb_ext_prop = {
53 .type = 1, /* NUL-terminated Unicode String (REG_SZ) */
54 .name = fb_ext_prop_name,
55 .data = fb_ext_prop_data,
58 /* 16 bytes of "Compatible ID" and "Subcompatible ID" */
59 static char fb_cid[16] = {'W', 'I', 'N', 'U', 'S', 'B'};
60 static struct usb_os_desc fb_os_desc = {
61 .ext_compat_id = fb_cid,
64 static struct usb_os_desc_table fb_os_desc_table = {
65 .os_desc = &fb_os_desc,
68 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
70 return container_of(f, struct f_fastboot, usb_function);
73 static struct f_fastboot *fastboot_func;
75 static struct usb_endpoint_descriptor fs_ep_in = {
76 .bLength = USB_DT_ENDPOINT_SIZE,
77 .bDescriptorType = USB_DT_ENDPOINT,
78 .bEndpointAddress = USB_DIR_IN,
79 .bmAttributes = USB_ENDPOINT_XFER_BULK,
80 .wMaxPacketSize = cpu_to_le16(64),
83 static struct usb_endpoint_descriptor fs_ep_out = {
84 .bLength = USB_DT_ENDPOINT_SIZE,
85 .bDescriptorType = USB_DT_ENDPOINT,
86 .bEndpointAddress = USB_DIR_OUT,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
88 .wMaxPacketSize = cpu_to_le16(64),
91 static struct usb_endpoint_descriptor hs_ep_in = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_BULK,
96 .wMaxPacketSize = cpu_to_le16(512),
99 static struct usb_endpoint_descriptor hs_ep_out = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
102 .bEndpointAddress = USB_DIR_OUT,
103 .bmAttributes = USB_ENDPOINT_XFER_BULK,
104 .wMaxPacketSize = cpu_to_le16(512),
107 static struct usb_interface_descriptor interface_desc = {
108 .bLength = USB_DT_INTERFACE_SIZE,
109 .bDescriptorType = USB_DT_INTERFACE,
110 .bInterfaceNumber = 0x00,
111 .bAlternateSetting = 0x00,
112 .bNumEndpoints = 0x02,
113 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
114 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
115 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
118 static struct usb_descriptor_header *fb_fs_function[] = {
119 (struct usb_descriptor_header *)&interface_desc,
120 (struct usb_descriptor_header *)&fs_ep_in,
121 (struct usb_descriptor_header *)&fs_ep_out,
125 static struct usb_descriptor_header *fb_hs_function[] = {
126 (struct usb_descriptor_header *)&interface_desc,
127 (struct usb_descriptor_header *)&hs_ep_in,
128 (struct usb_descriptor_header *)&hs_ep_out,
133 static struct usb_endpoint_descriptor ss_ep_in = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = cpu_to_le16(1024),
141 static struct usb_endpoint_descriptor ss_ep_out = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = USB_DIR_OUT,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = cpu_to_le16(1024),
149 static struct usb_ss_ep_comp_descriptor fb_ss_bulk_comp_desc = {
150 .bLength = sizeof(fb_ss_bulk_comp_desc),
151 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
154 static struct usb_descriptor_header *fb_ss_function[] = {
155 (struct usb_descriptor_header *)&interface_desc,
156 (struct usb_descriptor_header *)&ss_ep_in,
157 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
158 (struct usb_descriptor_header *)&ss_ep_out,
159 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
163 static struct usb_endpoint_descriptor *
164 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
165 struct usb_endpoint_descriptor *hs,
166 struct usb_endpoint_descriptor *ss)
168 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER)
171 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
177 * static strings, in UTF-8
179 static const char fastboot_name[] = "Android Fastboot";
181 static struct usb_string fastboot_string_defs[] = {
182 [0].s = fastboot_name,
183 { } /* end of list */
186 static struct usb_gadget_strings stringtab_fastboot = {
187 .language = 0x0409, /* en-us */
188 .strings = fastboot_string_defs,
191 static struct usb_gadget_strings *fastboot_strings[] = {
196 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
198 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
200 int status = req->status;
203 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
206 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
209 struct usb_gadget *gadget = c->cdev->gadget;
210 struct f_fastboot *f_fb = func_to_fastboot(f);
213 /* DYNAMIC interface numbers assignments */
214 id = usb_interface_id(c, f);
217 interface_desc.bInterfaceNumber = id;
219 /* Enable OS and Extended Properties Feature Descriptor */
220 c->cdev->use_os_string = 1;
221 f->os_desc_table = &fb_os_desc_table;
223 f->os_desc_table->if_id = id;
224 INIT_LIST_HEAD(&fb_os_desc.ext_prop);
225 fb_ext_prop.name_len = strlen(fb_ext_prop.name) * 2 + 2;
226 fb_os_desc.ext_prop_len = 10 + fb_ext_prop.name_len;
227 fb_os_desc.ext_prop_count = 1;
228 fb_ext_prop.data_len = strlen(fb_ext_prop.data) * 2 + 2;
229 fb_os_desc.ext_prop_len += fb_ext_prop.data_len + 4;
230 list_add_tail(&fb_ext_prop.entry, &fb_os_desc.ext_prop);
232 id = usb_string_id(c->cdev);
235 fastboot_string_defs[0].id = id;
236 interface_desc.iInterface = id;
238 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
241 f_fb->in_ep->driver_data = c->cdev;
243 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
246 f_fb->out_ep->driver_data = c->cdev;
248 f->descriptors = fb_fs_function;
250 if (gadget_is_dualspeed(gadget)) {
251 /* Assume endpoint addresses are the same for both speeds */
252 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
253 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
254 /* copy HS descriptors */
255 f->hs_descriptors = fb_hs_function;
258 if (gadget_is_superspeed(gadget)) {
259 ss_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
260 ss_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
261 f->ss_descriptors = fb_ss_function;
264 s = env_get("serial#");
266 g_dnl_set_serialnumber((char *)s);
271 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
273 f->os_desc_table = NULL;
274 list_del(&fb_os_desc.ext_prop);
275 memset(fastboot_func, 0, sizeof(*fastboot_func));
278 static void fastboot_disable(struct usb_function *f)
280 struct f_fastboot *f_fb = func_to_fastboot(f);
282 usb_ep_disable(f_fb->out_ep);
283 usb_ep_disable(f_fb->in_ep);
286 free(f_fb->out_req->buf);
287 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
288 f_fb->out_req = NULL;
291 free(f_fb->in_req->buf);
292 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
297 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
299 struct usb_request *req;
301 req = usb_ep_alloc_request(ep, 0);
305 req->length = EP_BUFFER_SIZE;
306 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
308 usb_ep_free_request(ep, req);
312 memset(req->buf, 0, req->length);
316 static int fastboot_set_alt(struct usb_function *f,
317 unsigned interface, unsigned alt)
320 struct usb_composite_dev *cdev = f->config->cdev;
321 struct usb_gadget *gadget = cdev->gadget;
322 struct f_fastboot *f_fb = func_to_fastboot(f);
323 const struct usb_endpoint_descriptor *d;
325 debug("%s: func: %s intf: %d alt: %d\n",
326 __func__, f->name, interface, alt);
328 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out, &ss_ep_out);
329 ret = usb_ep_enable(f_fb->out_ep, d);
331 puts("failed to enable out ep\n");
335 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
336 if (!f_fb->out_req) {
337 puts("failed to alloc out req\n");
341 f_fb->out_req->complete = rx_handler_command;
343 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in, &ss_ep_in);
344 ret = usb_ep_enable(f_fb->in_ep, d);
346 puts("failed to enable in ep\n");
350 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
352 puts("failed alloc req in\n");
356 f_fb->in_req->complete = fastboot_complete;
358 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
368 static int fastboot_add(struct usb_configuration *c)
370 struct f_fastboot *f_fb = fastboot_func;
373 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
376 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
380 fastboot_func = f_fb;
381 memset(f_fb, 0, sizeof(*f_fb));
384 f_fb->usb_function.name = "f_fastboot";
385 f_fb->usb_function.bind = fastboot_bind;
386 f_fb->usb_function.unbind = fastboot_unbind;
387 f_fb->usb_function.set_alt = fastboot_set_alt;
388 f_fb->usb_function.disable = fastboot_disable;
389 f_fb->usb_function.strings = fastboot_strings;
391 status = usb_add_function(c, &f_fb->usb_function);
394 fastboot_func = NULL;
399 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
401 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
403 struct usb_request *in_req = fastboot_func->in_req;
406 memcpy(in_req->buf, buffer, buffer_size);
407 in_req->length = buffer_size;
409 usb_ep_dequeue(fastboot_func->in_ep, in_req);
411 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
413 printf("Error %d on queue\n", ret);
417 static int fastboot_tx_write_str(const char *buffer)
419 return fastboot_tx_write(buffer, strlen(buffer));
422 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
425 do_reset(NULL, 0, 0, NULL);
428 static unsigned int rx_bytes_expected(struct usb_ep *ep)
430 int rx_remain = fastboot_data_remaining();
432 unsigned int maxpacket = usb_endpoint_maxp(ep->desc);
436 else if (rx_remain > EP_BUFFER_SIZE)
437 return EP_BUFFER_SIZE;
440 * Some controllers e.g. DWC3 don't like OUT transfers to be
441 * not ending in maxpacket boundary. So just make them happy by
442 * always requesting for integral multiple of maxpackets.
443 * This shouldn't bother controllers that don't care about it.
445 rem = rx_remain % maxpacket;
447 rx_remain = rx_remain + (maxpacket - rem);
452 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
454 char response[FASTBOOT_RESPONSE_LEN] = {0};
455 unsigned int transfer_size = fastboot_data_remaining();
456 const unsigned char *buffer = req->buf;
457 unsigned int buffer_size = req->actual;
459 if (req->status != 0) {
460 printf("Bad status: %d\n", req->status);
464 if (buffer_size < transfer_size)
465 transfer_size = buffer_size;
467 fastboot_data_download(buffer, transfer_size, response);
469 fastboot_tx_write_str(response);
470 } else if (!fastboot_data_remaining()) {
471 fastboot_data_complete(response);
474 * Reset global transfer variable
476 req->complete = rx_handler_command;
477 req->length = EP_BUFFER_SIZE;
479 fastboot_tx_write_str(response);
481 req->length = rx_bytes_expected(ep);
485 usb_ep_queue(ep, req, 0);
488 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
490 g_dnl_trigger_detach();
493 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
496 do_exit_on_complete(ep, req);
499 static void do_acmd_complete(struct usb_ep *ep, struct usb_request *req)
501 /* When usb dequeue complete will be called
502 * Need status value before call run_command.
503 * otherwise, host can't get last message.
505 if (req->status == 0)
506 fastboot_acmd_complete();
509 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
511 char *cmdbuf = req->buf;
512 char response[FASTBOOT_RESPONSE_LEN] = {0};
515 if (req->status != 0 || req->length == 0)
518 if (req->actual < req->length) {
519 cmdbuf[req->actual] = '\0';
520 cmd = fastboot_handle_command(cmdbuf, response);
522 pr_err("buffer overflow");
523 fastboot_fail("buffer overflow", response);
526 if (!strncmp("DATA", response, 4)) {
527 req->complete = rx_handler_dl_image;
528 req->length = rx_bytes_expected(ep);
531 if (!strncmp("OKAY", response, 4)) {
533 case FASTBOOT_COMMAND_BOOT:
534 fastboot_func->in_req->complete = do_bootm_on_complete;
537 case FASTBOOT_COMMAND_CONTINUE:
538 fastboot_func->in_req->complete = do_exit_on_complete;
541 case FASTBOOT_COMMAND_REBOOT:
542 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
543 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
544 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
545 fastboot_func->in_req->complete = compl_do_reset;
547 case FASTBOOT_COMMAND_ACMD:
548 if (CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT))
549 fastboot_func->in_req->complete = do_acmd_complete;
554 fastboot_tx_write_str(response);
558 usb_ep_queue(ep, req, 0);