]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/usb/gadget/f_fastboot.c
command: Remove the cmd_tbl_t typedef
[thirdparty/u-boot.git] / drivers / usb / gadget / f_fastboot.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3aab70af
SS
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>
3aab70af 11 */
09140113 12#include <command.h>
593cbd93 13#include <config.h>
3aab70af 14#include <common.h>
7b51b576 15#include <env.h>
3aab70af 16#include <errno.h>
3c8f98f5 17#include <fastboot.h>
3aab70af
SS
18#include <malloc.h>
19#include <linux/usb/ch9.h>
20#include <linux/usb/gadget.h>
21#include <linux/usb/composite.h>
22#include <linux/compiler.h>
3aab70af
SS
23#include <g_dnl.h>
24
3aab70af
SS
25#define FASTBOOT_INTERFACE_CLASS 0xff
26#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
27#define FASTBOOT_INTERFACE_PROTOCOL 0x03
28
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)
32
3aab70af 33#define EP_BUFFER_SIZE 4096
ac484c5a
RQ
34/*
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.
38 */
3aab70af
SS
39
40struct f_fastboot {
41 struct usb_function usb_function;
42
593cbd93 43 /* IN/OUT EP's and corresponding requests */
3aab70af
SS
44 struct usb_ep *in_ep, *out_ep;
45 struct usb_request *in_req, *out_req;
46};
47
48static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
49{
50 return container_of(f, struct f_fastboot, usb_function);
51}
52
53static struct f_fastboot *fastboot_func;
3aab70af
SS
54
55static 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,
718156ad 60 .wMaxPacketSize = cpu_to_le16(64),
3aab70af
SS
61};
62
63static 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,
718156ad
RQ
68 .wMaxPacketSize = cpu_to_le16(64),
69};
70
71static 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),
3aab70af
SS
77};
78
79static 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,
718156ad 84 .wMaxPacketSize = cpu_to_le16(512),
3aab70af
SS
85};
86
87static 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,
96};
97
718156ad 98static struct usb_descriptor_header *fb_fs_function[] = {
3aab70af
SS
99 (struct usb_descriptor_header *)&interface_desc,
100 (struct usb_descriptor_header *)&fs_ep_in,
718156ad
RQ
101 (struct usb_descriptor_header *)&fs_ep_out,
102};
103
104static struct usb_descriptor_header *fb_hs_function[] = {
105 (struct usb_descriptor_header *)&interface_desc,
106 (struct usb_descriptor_header *)&hs_ep_in,
3aab70af
SS
107 (struct usb_descriptor_header *)&hs_ep_out,
108 NULL,
109};
110
8b704a0e
RQ
111static struct usb_endpoint_descriptor *
112fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
113 struct usb_endpoint_descriptor *hs)
114{
115 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
116 return hs;
117 return fs;
118}
119
3aab70af
SS
120/*
121 * static strings, in UTF-8
122 */
123static const char fastboot_name[] = "Android Fastboot";
124
125static struct usb_string fastboot_string_defs[] = {
126 [0].s = fastboot_name,
127 { } /* end of list */
128};
129
130static struct usb_gadget_strings stringtab_fastboot = {
131 .language = 0x0409, /* en-us */
132 .strings = fastboot_string_defs,
133};
134
135static struct usb_gadget_strings *fastboot_strings[] = {
136 &stringtab_fastboot,
137 NULL,
138};
139
140static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
141
142static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
143{
144 int status = req->status;
145 if (!status)
146 return;
147 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
148}
149
150static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
151{
152 int id;
153 struct usb_gadget *gadget = c->cdev->gadget;
154 struct f_fastboot *f_fb = func_to_fastboot(f);
537cd072 155 const char *s;
3aab70af
SS
156
157 /* DYNAMIC interface numbers assignments */
158 id = usb_interface_id(c, f);
159 if (id < 0)
160 return id;
161 interface_desc.bInterfaceNumber = id;
162
163 id = usb_string_id(c->cdev);
164 if (id < 0)
165 return id;
166 fastboot_string_defs[0].id = id;
167 interface_desc.iInterface = id;
168
169 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
170 if (!f_fb->in_ep)
171 return -ENODEV;
172 f_fb->in_ep->driver_data = c->cdev;
173
174 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
175 if (!f_fb->out_ep)
176 return -ENODEV;
177 f_fb->out_ep->driver_data = c->cdev;
178
718156ad
RQ
179 f->descriptors = fb_fs_function;
180
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;
187 }
3aab70af 188
00caae6d 189 s = env_get("serial#");
537cd072
DK
190 if (s)
191 g_dnl_set_serialnumber((char *)s);
192
3aab70af
SS
193 return 0;
194}
195
196static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
197{
198 memset(fastboot_func, 0, sizeof(*fastboot_func));
199}
200
201static void fastboot_disable(struct usb_function *f)
202{
203 struct f_fastboot *f_fb = func_to_fastboot(f);
204
205 usb_ep_disable(f_fb->out_ep);
206 usb_ep_disable(f_fb->in_ep);
207
208 if (f_fb->out_req) {
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;
212 }
213 if (f_fb->in_req) {
214 free(f_fb->in_req->buf);
215 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
216 f_fb->in_req = NULL;
217 }
218}
219
220static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
221{
222 struct usb_request *req;
223
224 req = usb_ep_alloc_request(ep, 0);
225 if (!req)
226 return NULL;
227
228 req->length = EP_BUFFER_SIZE;
229 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
230 if (!req->buf) {
231 usb_ep_free_request(ep, req);
232 return NULL;
233 }
234
235 memset(req->buf, 0, req->length);
236 return req;
237}
238
239static int fastboot_set_alt(struct usb_function *f,
240 unsigned interface, unsigned alt)
241{
242 int ret;
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);
8b704a0e 246 const struct usb_endpoint_descriptor *d;
3aab70af
SS
247
248 debug("%s: func: %s intf: %d alt: %d\n",
249 __func__, f->name, interface, alt);
250
8b704a0e
RQ
251 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
252 ret = usb_ep_enable(f_fb->out_ep, d);
3aab70af
SS
253 if (ret) {
254 puts("failed to enable out ep\n");
255 return ret;
256 }
257
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");
261 ret = -EINVAL;
262 goto err;
263 }
264 f_fb->out_req->complete = rx_handler_command;
265
8b704a0e
RQ
266 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
267 ret = usb_ep_enable(f_fb->in_ep, d);
3aab70af
SS
268 if (ret) {
269 puts("failed to enable in ep\n");
270 goto err;
271 }
272
273 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
274 if (!f_fb->in_req) {
275 puts("failed alloc req in\n");
276 ret = -EINVAL;
277 goto err;
278 }
279 f_fb->in_req->complete = fastboot_complete;
280
281 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
282 if (ret)
283 goto err;
284
285 return 0;
286err:
287 fastboot_disable(f);
288 return ret;
289}
290
291static int fastboot_add(struct usb_configuration *c)
292{
293 struct f_fastboot *f_fb = fastboot_func;
294 int status;
295
296 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
297
298 if (!f_fb) {
299 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
300 if (!f_fb)
301 return -ENOMEM;
302
303 fastboot_func = f_fb;
304 memset(f_fb, 0, sizeof(*f_fb));
305 }
306
307 f_fb->usb_function.name = "f_fastboot";
3aab70af
SS
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;
313
314 status = usb_add_function(c, &f_fb->usb_function);
315 if (status) {
316 free(f_fb);
317 fastboot_func = f_fb;
318 }
319
320 return status;
321}
322DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
323
593cbd93 324static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
3aab70af
SS
325{
326 struct usb_request *in_req = fastboot_func->in_req;
327 int ret;
328
329 memcpy(in_req->buf, buffer, buffer_size);
330 in_req->length = buffer_size;
bc9071c9
PK
331
332 usb_ep_dequeue(fastboot_func->in_ep, in_req);
333
3aab70af
SS
334 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
335 if (ret)
336 printf("Error %d on queue\n", ret);
337 return 0;
338}
339
340static int fastboot_tx_write_str(const char *buffer)
341{
342 return fastboot_tx_write(buffer, strlen(buffer));
343}
344
345static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
346{
347 do_reset(NULL, 0, 0, NULL);
348}
349
ac484c5a 350static unsigned int rx_bytes_expected(struct usb_ep *ep)
3aab70af 351{
65c96757 352 int rx_remain = fastboot_data_remaining();
ac484c5a
RQ
353 unsigned int rem;
354 unsigned int maxpacket = ep->maxpacket;
355
356 if (rx_remain <= 0)
3aab70af 357 return 0;
ac484c5a 358 else if (rx_remain > EP_BUFFER_SIZE)
3aab70af 359 return EP_BUFFER_SIZE;
ac484c5a
RQ
360
361 /*
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.
366 */
367 rem = rx_remain % maxpacket;
368 if (rem > 0)
9e4b510d 369 rx_remain = rx_remain + (maxpacket - rem);
ac484c5a 370
3aab70af
SS
371 return rx_remain;
372}
373
3aab70af
SS
374static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
375{
65c96757
AK
376 char response[FASTBOOT_RESPONSE_LEN] = {0};
377 unsigned int transfer_size = fastboot_data_remaining();
3aab70af
SS
378 const unsigned char *buffer = req->buf;
379 unsigned int buffer_size = req->actual;
380
381 if (req->status != 0) {
382 printf("Bad status: %d\n", req->status);
383 return;
384 }
385
386 if (buffer_size < transfer_size)
387 transfer_size = buffer_size;
388
65c96757
AK
389 fastboot_data_download(buffer, transfer_size, response);
390 if (response[0]) {
391 fastboot_tx_write_str(response);
392 } else if (!fastboot_data_remaining()) {
393 fastboot_data_complete(response);
3aab70af 394
3aab70af 395 /*
65c96757 396 * Reset global transfer variable
3aab70af 397 */
3aab70af
SS
398 req->complete = rx_handler_command;
399 req->length = EP_BUFFER_SIZE;
400
3aab70af 401 fastboot_tx_write_str(response);
3aab70af 402 } else {
ac484c5a 403 req->length = rx_bytes_expected(ep);
3aab70af
SS
404 }
405
3aab70af
SS
406 req->actual = 0;
407 usb_ep_queue(ep, req, 0);
408}
409
267abc62
RH
410static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
411{
412 g_dnl_trigger_detach();
413}
414
65c96757 415static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
267abc62 416{
65c96757
AK
417 fastboot_boot();
418 do_exit_on_complete(ep, req);
267abc62
RH
419}
420
65c96757 421static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
d1b5ed07 422{
65c96757
AK
423 char *cmdbuf = req->buf;
424 char response[FASTBOOT_RESPONSE_LEN] = {0};
425 int cmd = -1;
d1b5ed07 426
65c96757 427 if (req->status != 0 || req->length == 0)
d1b5ed07 428 return;
d1b5ed07 429
65c96757
AK
430 if (req->actual < req->length) {
431 cmdbuf[req->actual] = '\0';
432 cmd = fastboot_handle_command(cmdbuf, response);
433 } else {
434 pr_err("buffer overflow");
435 fastboot_fail("buffer overflow", response);
de195620 436 }
de195620 437
65c96757
AK
438 if (!strncmp("DATA", response, 4)) {
439 req->complete = rx_handler_dl_image;
440 req->length = rx_bytes_expected(ep);
89792381
DK
441 }
442
89792381 443 fastboot_tx_write_str(response);
89792381 444
65c96757
AK
445 if (!strncmp("OKAY", response, 4)) {
446 switch (cmd) {
447 case FASTBOOT_COMMAND_BOOT:
448 fastboot_func->in_req->complete = do_bootm_on_complete;
449 break;
94b385fa 450
65c96757
AK
451 case FASTBOOT_COMMAND_CONTINUE:
452 fastboot_func->in_req->complete = do_exit_on_complete;
3aab70af 453 break;
3aab70af 454
65c96757
AK
455 case FASTBOOT_COMMAND_REBOOT:
456 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
457 fastboot_func->in_req->complete = compl_do_reset;
458 break;
e2140588 459 }
593cbd93 460 }
3aab70af 461
94b385fa
PK
462 *cmdbuf = '\0';
463 req->actual = 0;
464 usb_ep_queue(ep, req, 0);
3aab70af 465}