Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 - 2009 |
| 4 | * Windriver, <www.windriver.com> |
| 5 | * Tom Rix <Tom.Rix@windriver.com> |
| 6 | * |
| 7 | * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
| 9 | * Copyright 2014 Linaro, Ltd. |
| 10 | * Rob Herring <robh@kernel.org> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 11 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 12 | #include <command.h> |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 13 | #include <config.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 14 | #include <common.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 15 | #include <env.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 16 | #include <errno.h> |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 17 | #include <fastboot.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 19 | #include <malloc.h> |
| 20 | #include <linux/usb/ch9.h> |
| 21 | #include <linux/usb/gadget.h> |
| 22 | #include <linux/usb/composite.h> |
| 23 | #include <linux/compiler.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 24 | #include <g_dnl.h> |
| 25 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 26 | #define FASTBOOT_INTERFACE_CLASS 0xff |
| 27 | #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 |
| 28 | #define FASTBOOT_INTERFACE_PROTOCOL 0x03 |
| 29 | |
| 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) |
| 33 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 34 | #define EP_BUFFER_SIZE 4096 |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 35 | /* |
| 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. |
| 39 | */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 40 | |
| 41 | struct f_fastboot { |
| 42 | struct usb_function usb_function; |
| 43 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 44 | /* IN/OUT EP's and corresponding requests */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 45 | struct usb_ep *in_ep, *out_ep; |
| 46 | struct usb_request *in_req, *out_req; |
| 47 | }; |
| 48 | |
Li Jun | 2afadd6 | 2021-01-25 21:43:51 +0800 | [diff] [blame] | 49 | static char fb_ext_prop_name[] = "DeviceInterfaceGUID"; |
| 50 | static char fb_ext_prop_data[] = "{4866319A-F4D6-4374-93B9-DC2DEB361BA9}"; |
| 51 | |
| 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, |
| 56 | }; |
| 57 | |
| 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, |
| 62 | }; |
| 63 | |
| 64 | static struct usb_os_desc_table fb_os_desc_table = { |
| 65 | .os_desc = &fb_os_desc, |
| 66 | }; |
| 67 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 68 | static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) |
| 69 | { |
| 70 | return container_of(f, struct f_fastboot, usb_function); |
| 71 | } |
| 72 | |
| 73 | static struct f_fastboot *fastboot_func; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 74 | |
| 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, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 80 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
| 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, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 88 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 89 | }; |
| 90 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 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), |
| 97 | }; |
| 98 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 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, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 104 | .wMaxPacketSize = cpu_to_le16(512), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 105 | }; |
| 106 | |
| 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, |
| 116 | }; |
| 117 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 118 | static struct usb_descriptor_header *fb_fs_function[] = { |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 119 | (struct usb_descriptor_header *)&interface_desc, |
| 120 | (struct usb_descriptor_header *)&fs_ep_in, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 121 | (struct usb_descriptor_header *)&fs_ep_out, |
| 122 | }; |
| 123 | |
| 124 | static struct usb_descriptor_header *fb_hs_function[] = { |
| 125 | (struct usb_descriptor_header *)&interface_desc, |
| 126 | (struct usb_descriptor_header *)&hs_ep_in, |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 127 | (struct usb_descriptor_header *)&hs_ep_out, |
| 128 | NULL, |
| 129 | }; |
| 130 | |
Li Jun | 394d009 | 2021-01-25 21:43:55 +0800 | [diff] [blame] | 131 | /* Super speed */ |
| 132 | static struct usb_endpoint_descriptor ss_ep_in = { |
| 133 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 134 | .bDescriptorType = USB_DT_ENDPOINT, |
| 135 | .bEndpointAddress = USB_DIR_IN, |
| 136 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 137 | .wMaxPacketSize = cpu_to_le16(1024), |
| 138 | }; |
| 139 | |
| 140 | static struct usb_endpoint_descriptor ss_ep_out = { |
| 141 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 142 | .bDescriptorType = USB_DT_ENDPOINT, |
| 143 | .bEndpointAddress = USB_DIR_OUT, |
| 144 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 145 | .wMaxPacketSize = cpu_to_le16(1024), |
| 146 | }; |
| 147 | |
| 148 | static struct usb_ss_ep_comp_descriptor fb_ss_bulk_comp_desc = { |
| 149 | .bLength = sizeof(fb_ss_bulk_comp_desc), |
| 150 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 151 | }; |
| 152 | |
| 153 | static struct usb_descriptor_header *fb_ss_function[] = { |
| 154 | (struct usb_descriptor_header *)&interface_desc, |
| 155 | (struct usb_descriptor_header *)&ss_ep_in, |
| 156 | (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc, |
| 157 | (struct usb_descriptor_header *)&ss_ep_out, |
| 158 | (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc, |
| 159 | NULL, |
| 160 | }; |
| 161 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 162 | static struct usb_endpoint_descriptor * |
| 163 | fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
Li Jun | 394d009 | 2021-01-25 21:43:55 +0800 | [diff] [blame] | 164 | struct usb_endpoint_descriptor *hs, |
| 165 | struct usb_endpoint_descriptor *ss) |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 166 | { |
Li Jun | 394d009 | 2021-01-25 21:43:55 +0800 | [diff] [blame] | 167 | if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER) |
| 168 | return ss; |
| 169 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 170 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 171 | return hs; |
| 172 | return fs; |
| 173 | } |
| 174 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 175 | /* |
| 176 | * static strings, in UTF-8 |
| 177 | */ |
| 178 | static const char fastboot_name[] = "Android Fastboot"; |
| 179 | |
| 180 | static struct usb_string fastboot_string_defs[] = { |
| 181 | [0].s = fastboot_name, |
| 182 | { } /* end of list */ |
| 183 | }; |
| 184 | |
| 185 | static struct usb_gadget_strings stringtab_fastboot = { |
| 186 | .language = 0x0409, /* en-us */ |
| 187 | .strings = fastboot_string_defs, |
| 188 | }; |
| 189 | |
| 190 | static struct usb_gadget_strings *fastboot_strings[] = { |
| 191 | &stringtab_fastboot, |
| 192 | NULL, |
| 193 | }; |
| 194 | |
| 195 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); |
| 196 | |
| 197 | static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) |
| 198 | { |
| 199 | int status = req->status; |
| 200 | if (!status) |
| 201 | return; |
| 202 | printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); |
| 203 | } |
| 204 | |
| 205 | static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) |
| 206 | { |
| 207 | int id; |
| 208 | struct usb_gadget *gadget = c->cdev->gadget; |
| 209 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Dileep Katta | 447c975 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 210 | const char *s; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 211 | |
| 212 | /* DYNAMIC interface numbers assignments */ |
| 213 | id = usb_interface_id(c, f); |
| 214 | if (id < 0) |
| 215 | return id; |
| 216 | interface_desc.bInterfaceNumber = id; |
| 217 | |
Li Jun | 2afadd6 | 2021-01-25 21:43:51 +0800 | [diff] [blame] | 218 | /* Enable OS and Extended Properties Feature Descriptor */ |
| 219 | c->cdev->use_os_string = 1; |
| 220 | f->os_desc_table = &fb_os_desc_table; |
| 221 | f->os_desc_n = 1; |
| 222 | f->os_desc_table->if_id = id; |
| 223 | INIT_LIST_HEAD(&fb_os_desc.ext_prop); |
| 224 | fb_ext_prop.name_len = strlen(fb_ext_prop.name) * 2 + 2; |
| 225 | fb_os_desc.ext_prop_len = 10 + fb_ext_prop.name_len; |
| 226 | fb_os_desc.ext_prop_count = 1; |
| 227 | fb_ext_prop.data_len = strlen(fb_ext_prop.data) * 2 + 2; |
| 228 | fb_os_desc.ext_prop_len += fb_ext_prop.data_len + 4; |
| 229 | list_add_tail(&fb_ext_prop.entry, &fb_os_desc.ext_prop); |
| 230 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 231 | id = usb_string_id(c->cdev); |
| 232 | if (id < 0) |
| 233 | return id; |
| 234 | fastboot_string_defs[0].id = id; |
| 235 | interface_desc.iInterface = id; |
| 236 | |
| 237 | f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); |
| 238 | if (!f_fb->in_ep) |
| 239 | return -ENODEV; |
| 240 | f_fb->in_ep->driver_data = c->cdev; |
| 241 | |
| 242 | f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); |
| 243 | if (!f_fb->out_ep) |
| 244 | return -ENODEV; |
| 245 | f_fb->out_ep->driver_data = c->cdev; |
| 246 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 247 | f->descriptors = fb_fs_function; |
| 248 | |
| 249 | if (gadget_is_dualspeed(gadget)) { |
| 250 | /* Assume endpoint addresses are the same for both speeds */ |
| 251 | hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; |
| 252 | hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; |
| 253 | /* copy HS descriptors */ |
| 254 | f->hs_descriptors = fb_hs_function; |
| 255 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 256 | |
Li Jun | 394d009 | 2021-01-25 21:43:55 +0800 | [diff] [blame] | 257 | if (gadget_is_superspeed(gadget)) { |
| 258 | ss_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; |
| 259 | ss_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; |
| 260 | f->ss_descriptors = fb_ss_function; |
| 261 | } |
| 262 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 263 | s = env_get("serial#"); |
Dileep Katta | 447c975 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 264 | if (s) |
| 265 | g_dnl_set_serialnumber((char *)s); |
| 266 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) |
| 271 | { |
Li Jun | 2afadd6 | 2021-01-25 21:43:51 +0800 | [diff] [blame] | 272 | f->os_desc_table = NULL; |
| 273 | list_del(&fb_os_desc.ext_prop); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 274 | memset(fastboot_func, 0, sizeof(*fastboot_func)); |
| 275 | } |
| 276 | |
| 277 | static void fastboot_disable(struct usb_function *f) |
| 278 | { |
| 279 | struct f_fastboot *f_fb = func_to_fastboot(f); |
| 280 | |
| 281 | usb_ep_disable(f_fb->out_ep); |
| 282 | usb_ep_disable(f_fb->in_ep); |
| 283 | |
| 284 | if (f_fb->out_req) { |
| 285 | free(f_fb->out_req->buf); |
| 286 | usb_ep_free_request(f_fb->out_ep, f_fb->out_req); |
| 287 | f_fb->out_req = NULL; |
| 288 | } |
| 289 | if (f_fb->in_req) { |
| 290 | free(f_fb->in_req->buf); |
| 291 | usb_ep_free_request(f_fb->in_ep, f_fb->in_req); |
| 292 | f_fb->in_req = NULL; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static struct usb_request *fastboot_start_ep(struct usb_ep *ep) |
| 297 | { |
| 298 | struct usb_request *req; |
| 299 | |
| 300 | req = usb_ep_alloc_request(ep, 0); |
| 301 | if (!req) |
| 302 | return NULL; |
| 303 | |
| 304 | req->length = EP_BUFFER_SIZE; |
| 305 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); |
| 306 | if (!req->buf) { |
| 307 | usb_ep_free_request(ep, req); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | memset(req->buf, 0, req->length); |
| 312 | return req; |
| 313 | } |
| 314 | |
| 315 | static int fastboot_set_alt(struct usb_function *f, |
| 316 | unsigned interface, unsigned alt) |
| 317 | { |
| 318 | int ret; |
| 319 | struct usb_composite_dev *cdev = f->config->cdev; |
| 320 | struct usb_gadget *gadget = cdev->gadget; |
| 321 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 322 | const struct usb_endpoint_descriptor *d; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 323 | |
| 324 | debug("%s: func: %s intf: %d alt: %d\n", |
| 325 | __func__, f->name, interface, alt); |
| 326 | |
Li Jun | 394d009 | 2021-01-25 21:43:55 +0800 | [diff] [blame] | 327 | d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out, &ss_ep_out); |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 328 | ret = usb_ep_enable(f_fb->out_ep, d); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 329 | if (ret) { |
| 330 | puts("failed to enable out ep\n"); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | f_fb->out_req = fastboot_start_ep(f_fb->out_ep); |
| 335 | if (!f_fb->out_req) { |
| 336 | puts("failed to alloc out req\n"); |
| 337 | ret = -EINVAL; |
| 338 | goto err; |
| 339 | } |
| 340 | f_fb->out_req->complete = rx_handler_command; |
| 341 | |
Li Jun | 394d009 | 2021-01-25 21:43:55 +0800 | [diff] [blame] | 342 | d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in, &ss_ep_in); |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 343 | ret = usb_ep_enable(f_fb->in_ep, d); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 344 | if (ret) { |
| 345 | puts("failed to enable in ep\n"); |
| 346 | goto err; |
| 347 | } |
| 348 | |
| 349 | f_fb->in_req = fastboot_start_ep(f_fb->in_ep); |
| 350 | if (!f_fb->in_req) { |
| 351 | puts("failed alloc req in\n"); |
| 352 | ret = -EINVAL; |
| 353 | goto err; |
| 354 | } |
| 355 | f_fb->in_req->complete = fastboot_complete; |
| 356 | |
| 357 | ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); |
| 358 | if (ret) |
| 359 | goto err; |
| 360 | |
| 361 | return 0; |
| 362 | err: |
| 363 | fastboot_disable(f); |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | static int fastboot_add(struct usb_configuration *c) |
| 368 | { |
| 369 | struct f_fastboot *f_fb = fastboot_func; |
| 370 | int status; |
| 371 | |
| 372 | debug("%s: cdev: 0x%p\n", __func__, c->cdev); |
| 373 | |
| 374 | if (!f_fb) { |
| 375 | f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); |
| 376 | if (!f_fb) |
| 377 | return -ENOMEM; |
| 378 | |
| 379 | fastboot_func = f_fb; |
| 380 | memset(f_fb, 0, sizeof(*f_fb)); |
| 381 | } |
| 382 | |
| 383 | f_fb->usb_function.name = "f_fastboot"; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 384 | f_fb->usb_function.bind = fastboot_bind; |
| 385 | f_fb->usb_function.unbind = fastboot_unbind; |
| 386 | f_fb->usb_function.set_alt = fastboot_set_alt; |
| 387 | f_fb->usb_function.disable = fastboot_disable; |
| 388 | f_fb->usb_function.strings = fastboot_strings; |
| 389 | |
| 390 | status = usb_add_function(c, &f_fb->usb_function); |
| 391 | if (status) { |
| 392 | free(f_fb); |
Andy Shevchenko | ab81b2a | 2020-12-03 17:32:05 +0200 | [diff] [blame] | 393 | fastboot_func = NULL; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | return status; |
| 397 | } |
| 398 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); |
| 399 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 400 | static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 401 | { |
| 402 | struct usb_request *in_req = fastboot_func->in_req; |
| 403 | int ret; |
| 404 | |
| 405 | memcpy(in_req->buf, buffer, buffer_size); |
| 406 | in_req->length = buffer_size; |
Paul Kocialkowski | 2032bfd | 2015-07-04 16:46:16 +0200 | [diff] [blame] | 407 | |
| 408 | usb_ep_dequeue(fastboot_func->in_ep, in_req); |
| 409 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 410 | ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); |
| 411 | if (ret) |
| 412 | printf("Error %d on queue\n", ret); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int fastboot_tx_write_str(const char *buffer) |
| 417 | { |
| 418 | return fastboot_tx_write(buffer, strlen(buffer)); |
| 419 | } |
| 420 | |
| 421 | static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) |
| 422 | { |
| 423 | do_reset(NULL, 0, 0, NULL); |
| 424 | } |
| 425 | |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 426 | static unsigned int rx_bytes_expected(struct usb_ep *ep) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 427 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 428 | int rx_remain = fastboot_data_remaining(); |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 429 | unsigned int rem; |
Li Jun | a4b9f4d | 2021-01-25 21:43:59 +0800 | [diff] [blame] | 430 | unsigned int maxpacket = usb_endpoint_maxp(ep->desc); |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 431 | |
| 432 | if (rx_remain <= 0) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 433 | return 0; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 434 | else if (rx_remain > EP_BUFFER_SIZE) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 435 | return EP_BUFFER_SIZE; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Some controllers e.g. DWC3 don't like OUT transfers to be |
| 439 | * not ending in maxpacket boundary. So just make them happy by |
| 440 | * always requesting for integral multiple of maxpackets. |
| 441 | * This shouldn't bother controllers that don't care about it. |
| 442 | */ |
| 443 | rem = rx_remain % maxpacket; |
| 444 | if (rem > 0) |
Dileep Katta | 50b4c17 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 445 | rx_remain = rx_remain + (maxpacket - rem); |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 446 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 447 | return rx_remain; |
| 448 | } |
| 449 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 450 | static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) |
| 451 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 452 | char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 453 | unsigned int transfer_size = fastboot_data_remaining(); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 454 | const unsigned char *buffer = req->buf; |
| 455 | unsigned int buffer_size = req->actual; |
| 456 | |
| 457 | if (req->status != 0) { |
| 458 | printf("Bad status: %d\n", req->status); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (buffer_size < transfer_size) |
| 463 | transfer_size = buffer_size; |
| 464 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 465 | fastboot_data_download(buffer, transfer_size, response); |
| 466 | if (response[0]) { |
| 467 | fastboot_tx_write_str(response); |
| 468 | } else if (!fastboot_data_remaining()) { |
| 469 | fastboot_data_complete(response); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 470 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 471 | /* |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 472 | * Reset global transfer variable |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 473 | */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 474 | req->complete = rx_handler_command; |
| 475 | req->length = EP_BUFFER_SIZE; |
| 476 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 477 | fastboot_tx_write_str(response); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 478 | } else { |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 479 | req->length = rx_bytes_expected(ep); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 480 | } |
| 481 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 482 | req->actual = 0; |
| 483 | usb_ep_queue(ep, req, 0); |
| 484 | } |
| 485 | |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 486 | static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 487 | { |
| 488 | g_dnl_trigger_detach(); |
| 489 | } |
| 490 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 491 | static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 492 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 493 | fastboot_boot(); |
| 494 | do_exit_on_complete(ep, req); |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 495 | } |
| 496 | |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 497 | #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) |
| 498 | static void do_acmd_complete(struct usb_ep *ep, struct usb_request *req) |
| 499 | { |
| 500 | /* When usb dequeue complete will be called |
| 501 | * Need status value before call run_command. |
| 502 | * otherwise, host can't get last message. |
| 503 | */ |
| 504 | if (req->status == 0) |
| 505 | fastboot_acmd_complete(); |
| 506 | } |
| 507 | #endif |
| 508 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 509 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 510 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 511 | char *cmdbuf = req->buf; |
| 512 | char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 513 | int cmd = -1; |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 514 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 515 | if (req->status != 0 || req->length == 0) |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 516 | return; |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 517 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 518 | if (req->actual < req->length) { |
| 519 | cmdbuf[req->actual] = '\0'; |
| 520 | cmd = fastboot_handle_command(cmdbuf, response); |
| 521 | } else { |
| 522 | pr_err("buffer overflow"); |
| 523 | fastboot_fail("buffer overflow", response); |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 524 | } |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 525 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 526 | if (!strncmp("DATA", response, 4)) { |
| 527 | req->complete = rx_handler_dl_image; |
| 528 | req->length = rx_bytes_expected(ep); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 529 | } |
| 530 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 531 | if (!strncmp("OKAY", response, 4)) { |
| 532 | switch (cmd) { |
| 533 | case FASTBOOT_COMMAND_BOOT: |
| 534 | fastboot_func->in_req->complete = do_bootm_on_complete; |
| 535 | break; |
Paul Kocialkowski | 4a126b6 | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 536 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 537 | case FASTBOOT_COMMAND_CONTINUE: |
| 538 | fastboot_func->in_req->complete = do_exit_on_complete; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 539 | break; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 540 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 541 | case FASTBOOT_COMMAND_REBOOT: |
| 542 | case FASTBOOT_COMMAND_REBOOT_BOOTLOADER: |
Roman Kovalivskyi | b30b97b | 2020-07-28 23:35:33 +0300 | [diff] [blame] | 543 | case FASTBOOT_COMMAND_REBOOT_FASTBOOTD: |
| 544 | case FASTBOOT_COMMAND_REBOOT_RECOVERY: |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 545 | fastboot_func->in_req->complete = compl_do_reset; |
| 546 | break; |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 547 | #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) |
| 548 | case FASTBOOT_COMMAND_ACMD: |
| 549 | fastboot_func->in_req->complete = do_acmd_complete; |
| 550 | break; |
| 551 | #endif |
Eric Nelson | 3d626cd | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 552 | } |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 553 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 554 | |
yurii.pidhornyi | 5953ffa | 2020-08-20 18:41:18 +0300 | [diff] [blame] | 555 | fastboot_tx_write_str(response); |
| 556 | |
Paul Kocialkowski | 4a126b6 | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 557 | *cmdbuf = '\0'; |
| 558 | req->actual = 0; |
| 559 | usb_ep_queue(ep, req, 0); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 560 | } |