blob: ca8b36e077bc52f5ff433ac3b78b200f5a98b1cb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +02002/*
3 * f_dfu.c -- Device Firmware Update USB function
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7 * Lukasz Majewski <l.majewski@samsung.com>
8 *
Ɓukasz Majewski7daece82013-07-05 11:40:47 +02009 * Based on OpenMoko u-boot: drivers/usb/usbdfu.c
10 * (C) 2007 by OpenMoko, Inc.
11 * Author: Harald Welte <laforge@openmoko.org>
12 *
13 * based on existing SAM7DFU code from OpenPCD:
14 * (C) Copyright 2006 by Harald Welte <hwelte at hmw-consulting.de>
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020015 */
16
Simon Glass0af6e2d2019-08-01 09:46:52 -060017#include <env.h>
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020018#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020020#include <malloc.h>
21
22#include <linux/usb/ch9.h>
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020023#include <linux/usb/gadget.h>
24#include <linux/usb/composite.h>
25
26#include <dfu.h>
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +020027#include <g_dnl.h>
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020028#include "f_dfu.h"
29
30struct f_dfu {
31 struct usb_function usb_function;
32
33 struct usb_descriptor_header **function;
34 struct usb_string *strings;
35
36 /* when configured, we have one config */
37 u8 config;
38 u8 altsetting;
39 enum dfu_state dfu_state;
40 unsigned int dfu_status;
41
42 /* Send/received block number is handy for data integrity check */
43 int blk_seq_num;
Lukasz Majewski2bb115f2013-12-09 16:20:14 +010044 unsigned int poll_timeout;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020045};
46
Lukasz Majewski365a70d2016-01-28 16:14:49 +010047struct dfu_entity *dfu_defer_flush;
48
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020049typedef int (*dfu_state_fn) (struct f_dfu *,
50 const struct usb_ctrlrequest *,
51 struct usb_gadget *,
52 struct usb_request *);
53
54static inline struct f_dfu *func_to_dfu(struct usb_function *f)
55{
56 return container_of(f, struct f_dfu, usb_function);
57}
58
59static const struct dfu_function_descriptor dfu_func = {
60 .bLength = sizeof dfu_func,
61 .bDescriptorType = DFU_DT_FUNC,
62 .bmAttributes = DFU_BIT_WILL_DETACH |
63 DFU_BIT_MANIFESTATION_TOLERANT |
64 DFU_BIT_CAN_UPLOAD |
65 DFU_BIT_CAN_DNLOAD,
66 .wDetachTimeOut = 0,
67 .wTransferSize = DFU_USB_BUFSIZ,
68 .bcdDFUVersion = __constant_cpu_to_le16(0x0110),
69};
70
71static struct usb_interface_descriptor dfu_intf_runtime = {
72 .bLength = sizeof dfu_intf_runtime,
73 .bDescriptorType = USB_DT_INTERFACE,
74 .bNumEndpoints = 0,
75 .bInterfaceClass = USB_CLASS_APP_SPEC,
76 .bInterfaceSubClass = 1,
77 .bInterfaceProtocol = 1,
78 /* .iInterface = DYNAMIC */
79};
80
81static struct usb_descriptor_header *dfu_runtime_descs[] = {
82 (struct usb_descriptor_header *) &dfu_intf_runtime,
83 NULL,
84};
85
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020086static const char dfu_name[] = "Device Firmware Upgrade";
87
88/*
89 * static strings, in UTF-8
90 *
91 * dfu_generic configuration
92 */
93static struct usb_string strings_dfu_generic[] = {
94 [0].s = dfu_name,
95 { } /* end of list */
96};
97
98static struct usb_gadget_strings stringtab_dfu_generic = {
99 .language = 0x0409, /* en-us */
100 .strings = strings_dfu_generic,
101};
102
103static struct usb_gadget_strings *dfu_generic_strings[] = {
104 &stringtab_dfu_generic,
105 NULL,
106};
107
108/*
109 * usb_function specific
110 */
111static struct usb_gadget_strings stringtab_dfu = {
112 .language = 0x0409, /* en-us */
113 /*
114 * .strings
115 *
116 * assigned during initialization,
117 * depends on number of flash entities
118 *
119 */
120};
121
122static struct usb_gadget_strings *dfu_strings[] = {
123 &stringtab_dfu,
124 NULL,
125};
126
Lukasz Majewski2bb115f2013-12-09 16:20:14 +0100127static void dfu_set_poll_timeout(struct dfu_status *dstat, unsigned int ms)
128{
129 /*
130 * The bwPollTimeout DFU_GETSTATUS request payload provides information
131 * about minimum time, in milliseconds, that the host should wait before
132 * sending a subsequent DFU_GETSTATUS request
133 *
134 * This permits the device to vary the delay depending on its need to
135 * erase or program the memory
136 *
137 */
138
139 unsigned char *p = (unsigned char *)&ms;
140
141 if (!ms || (ms & ~DFU_POLL_TIMEOUT_MASK)) {
142 dstat->bwPollTimeout[0] = 0;
143 dstat->bwPollTimeout[1] = 0;
144 dstat->bwPollTimeout[2] = 0;
145
146 return;
147 }
148
149 dstat->bwPollTimeout[0] = *p++;
150 dstat->bwPollTimeout[1] = *p++;
151 dstat->bwPollTimeout[2] = *p;
152}
153
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200154/*-------------------------------------------------------------------------*/
155
156static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
157{
158 struct f_dfu *f_dfu = req->context;
Stephen Warren7f450232014-06-11 12:48:08 -0600159 int ret;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200160
Stephen Warren7f450232014-06-11 12:48:08 -0600161 ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
Felipe Balbic7297862017-02-10 19:32:31 +0300162 req->actual, f_dfu->blk_seq_num);
Stephen Warren7f450232014-06-11 12:48:08 -0600163 if (ret) {
164 f_dfu->dfu_status = DFU_STATUS_errUNKNOWN;
165 f_dfu->dfu_state = DFU_STATE_dfuERROR;
166 }
Heiko Schochere1ba1512014-03-18 08:09:56 +0100167}
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200168
Heiko Schochere1ba1512014-03-18 08:09:56 +0100169static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req)
170{
171 struct f_dfu *f_dfu = req->context;
Lukasz Majewski365a70d2016-01-28 16:14:49 +0100172 dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting));
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200173}
174
Heiko Schocherb9488102014-04-11 07:59:47 +0200175static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu)
176{
177 return dfu->poll_timeout ? dfu->poll_timeout(dfu) :
178 DFU_MANIFEST_POLL_TIMEOUT;
179}
180
Patrick Delaunaye652af82016-12-16 18:41:32 +0100181static int handle_getstatus(struct usb_request *req)
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200182{
183 struct dfu_status *dstat = (struct dfu_status *)req->buf;
184 struct f_dfu *f_dfu = req->context;
Heiko Schocherb9488102014-04-11 07:59:47 +0200185 struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200186
Heiko Schochere1ba1512014-03-18 08:09:56 +0100187 dfu_set_poll_timeout(dstat, 0);
188
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200189 switch (f_dfu->dfu_state) {
190 case DFU_STATE_dfuDNLOAD_SYNC:
191 case DFU_STATE_dfuDNBUSY:
192 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
193 break;
194 case DFU_STATE_dfuMANIFEST_SYNC:
Heiko Schochere1ba1512014-03-18 08:09:56 +0100195 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200196 break;
Heiko Schochere1ba1512014-03-18 08:09:56 +0100197 case DFU_STATE_dfuMANIFEST:
Heiko Schocherb9488102014-04-11 07:59:47 +0200198 dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu));
199 break;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200200 default:
201 break;
202 }
203
Lukasz Majewski2bb115f2013-12-09 16:20:14 +0100204 if (f_dfu->poll_timeout)
205 if (!(f_dfu->blk_seq_num %
206 (dfu_get_buf_size() / DFU_USB_BUFSIZ)))
207 dfu_set_poll_timeout(dstat, f_dfu->poll_timeout);
208
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200209 /* send status response */
210 dstat->bStatus = f_dfu->dfu_status;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200211 dstat->bState = f_dfu->dfu_state;
212 dstat->iString = 0;
Patrick Delaunaye652af82016-12-16 18:41:32 +0100213
214 return sizeof(struct dfu_status);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200215}
216
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100217static int handle_getstate(struct usb_request *req)
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200218{
219 struct f_dfu *f_dfu = req->context;
220
221 ((u8 *)req->buf)[0] = f_dfu->dfu_state;
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100222 return sizeof(u8);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200223}
224
225static inline void to_dfu_mode(struct f_dfu *f_dfu)
226{
227 f_dfu->usb_function.strings = dfu_strings;
228 f_dfu->usb_function.hs_descriptors = f_dfu->function;
Heiko Schocher3e3a1362014-09-10 08:55:00 +0200229 f_dfu->usb_function.descriptors = f_dfu->function;
Heiko Schocher5890e832013-06-25 13:59:29 +0200230 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200231}
232
233static inline void to_runtime_mode(struct f_dfu *f_dfu)
234{
235 f_dfu->usb_function.strings = NULL;
236 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
Heiko Schocher3e3a1362014-09-10 08:55:00 +0200237 f_dfu->usb_function.descriptors = dfu_runtime_descs;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200238}
239
240static int handle_upload(struct usb_request *req, u16 len)
241{
242 struct f_dfu *f_dfu = req->context;
243
244 return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
245 req->length, f_dfu->blk_seq_num);
246}
247
248static int handle_dnload(struct usb_gadget *gadget, u16 len)
249{
250 struct usb_composite_dev *cdev = get_gadget_data(gadget);
251 struct usb_request *req = cdev->req;
252 struct f_dfu *f_dfu = req->context;
253
254 if (len == 0)
255 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
256
257 req->complete = dnload_request_complete;
258
259 return len;
260}
261
262/*-------------------------------------------------------------------------*/
263/* DFU state machine */
264static int state_app_idle(struct f_dfu *f_dfu,
265 const struct usb_ctrlrequest *ctrl,
266 struct usb_gadget *gadget,
267 struct usb_request *req)
268{
269 int value = 0;
270
271 switch (ctrl->bRequest) {
272 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100273 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200274 break;
275 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100276 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200277 break;
278 case USB_REQ_DFU_DETACH:
279 f_dfu->dfu_state = DFU_STATE_appDETACH;
280 to_dfu_mode(f_dfu);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200281 value = RET_ZLP;
282 break;
283 default:
284 value = RET_STALL;
285 break;
286 }
287
288 return value;
289}
290
291static int state_app_detach(struct f_dfu *f_dfu,
292 const struct usb_ctrlrequest *ctrl,
293 struct usb_gadget *gadget,
294 struct usb_request *req)
295{
296 int value = 0;
297
298 switch (ctrl->bRequest) {
299 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100300 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200301 break;
302 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100303 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200304 break;
305 default:
306 f_dfu->dfu_state = DFU_STATE_appIDLE;
307 value = RET_STALL;
308 break;
309 }
310
311 return value;
312}
313
314static int state_dfu_idle(struct f_dfu *f_dfu,
315 const struct usb_ctrlrequest *ctrl,
316 struct usb_gadget *gadget,
317 struct usb_request *req)
318{
319 u16 w_value = le16_to_cpu(ctrl->wValue);
320 u16 len = le16_to_cpu(ctrl->wLength);
321 int value = 0;
322
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530323 len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len;
324
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200325 switch (ctrl->bRequest) {
326 case USB_REQ_DFU_DNLOAD:
Hugo SIMELIERE06ce62d2022-11-30 09:29:16 +0100327 if (!(ctrl->bRequestType & USB_DIR_IN)) {
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530328 if (len == 0) {
329 f_dfu->dfu_state = DFU_STATE_dfuERROR;
330 value = RET_STALL;
331 break;
332 }
333 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
334 f_dfu->blk_seq_num = w_value;
335 value = handle_dnload(gadget, len);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200336 }
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200337 break;
338 case USB_REQ_DFU_UPLOAD:
Hugo SIMELIERE06ce62d2022-11-30 09:29:16 +0100339 if (ctrl->bRequestType & USB_DIR_IN) {
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530340 f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
341 f_dfu->blk_seq_num = 0;
342 value = handle_upload(req, len);
343 if (value >= 0 && value < len)
344 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
345 }
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200346 break;
347 case USB_REQ_DFU_ABORT:
348 /* no zlp? */
349 value = RET_ZLP;
350 break;
351 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100352 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200353 break;
354 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100355 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200356 break;
357 case USB_REQ_DFU_DETACH:
358 /*
359 * Proprietary extension: 'detach' from idle mode and
360 * get back to runtime mode in case of USB Reset. As
361 * much as I dislike this, we just can't use every USB
362 * bus reset to switch back to runtime mode, since at
363 * least the Linux USB stack likes to send a number of
364 * resets in a row :(
365 */
366 f_dfu->dfu_state =
367 DFU_STATE_dfuMANIFEST_WAIT_RST;
368 to_runtime_mode(f_dfu);
369 f_dfu->dfu_state = DFU_STATE_appIDLE;
Lukasz Majewski2968aa22013-07-18 13:19:14 +0200370
Rob Herringd38f24e2014-12-10 14:43:03 -0600371 g_dnl_trigger_detach();
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200372 break;
373 default:
374 f_dfu->dfu_state = DFU_STATE_dfuERROR;
375 value = RET_STALL;
376 break;
377 }
378
379 return value;
380}
381
382static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
383 const struct usb_ctrlrequest *ctrl,
384 struct usb_gadget *gadget,
385 struct usb_request *req)
386{
387 int value = 0;
388
389 switch (ctrl->bRequest) {
390 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100391 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200392 break;
393 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100394 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200395 break;
396 default:
397 f_dfu->dfu_state = DFU_STATE_dfuERROR;
398 value = RET_STALL;
399 break;
400 }
401
402 return value;
403}
404
405static int state_dfu_dnbusy(struct f_dfu *f_dfu,
406 const struct usb_ctrlrequest *ctrl,
407 struct usb_gadget *gadget,
408 struct usb_request *req)
409{
410 int value = 0;
411
412 switch (ctrl->bRequest) {
413 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100414 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200415 break;
416 default:
417 f_dfu->dfu_state = DFU_STATE_dfuERROR;
418 value = RET_STALL;
419 break;
420 }
421
422 return value;
423}
424
425static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
426 const struct usb_ctrlrequest *ctrl,
427 struct usb_gadget *gadget,
428 struct usb_request *req)
429{
430 u16 w_value = le16_to_cpu(ctrl->wValue);
431 u16 len = le16_to_cpu(ctrl->wLength);
432 int value = 0;
433
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530434 len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len;
435
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200436 switch (ctrl->bRequest) {
437 case USB_REQ_DFU_DNLOAD:
Hugo SIMELIERE06ce62d2022-11-30 09:29:16 +0100438 if (!(ctrl->bRequestType & USB_DIR_IN)) {
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530439 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
440 f_dfu->blk_seq_num = w_value;
441 value = handle_dnload(gadget, len);
442 }
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200443 break;
444 case USB_REQ_DFU_ABORT:
445 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
446 value = RET_ZLP;
447 break;
448 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100449 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200450 break;
451 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100452 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200453 break;
454 default:
455 f_dfu->dfu_state = DFU_STATE_dfuERROR;
456 value = RET_STALL;
457 break;
458 }
459
460 return value;
461}
462
463static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
464 const struct usb_ctrlrequest *ctrl,
465 struct usb_gadget *gadget,
466 struct usb_request *req)
467{
468 int value = 0;
469
470 switch (ctrl->bRequest) {
471 case USB_REQ_DFU_GETSTATUS:
472 /* We're MainfestationTolerant */
Heiko Schochere1ba1512014-03-18 08:09:56 +0100473 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
Patrick Delaunaye652af82016-12-16 18:41:32 +0100474 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200475 f_dfu->blk_seq_num = 0;
Heiko Schochere1ba1512014-03-18 08:09:56 +0100476 req->complete = dnload_request_flush;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200477 break;
478 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100479 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200480 break;
481 default:
482 f_dfu->dfu_state = DFU_STATE_dfuERROR;
483 value = RET_STALL;
484 break;
485 }
486
Heiko Schochere1ba1512014-03-18 08:09:56 +0100487 return value;
488}
489
490static int state_dfu_manifest(struct f_dfu *f_dfu,
491 const struct usb_ctrlrequest *ctrl,
492 struct usb_gadget *gadget,
493 struct usb_request *req)
494{
495 int value = 0;
496
497 switch (ctrl->bRequest) {
498 case USB_REQ_DFU_GETSTATUS:
499 /* We're MainfestationTolerant */
500 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
Patrick Delaunaye652af82016-12-16 18:41:32 +0100501 value = handle_getstatus(req);
Heiko Schochere1ba1512014-03-18 08:09:56 +0100502 f_dfu->blk_seq_num = 0;
Heiko Schochere1ba1512014-03-18 08:09:56 +0100503 puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
504 break;
505 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100506 value = handle_getstate(req);
Heiko Schochere1ba1512014-03-18 08:09:56 +0100507 break;
508 default:
509 f_dfu->dfu_state = DFU_STATE_dfuERROR;
510 value = RET_STALL;
511 break;
512 }
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200513 return value;
514}
515
516static int state_dfu_upload_idle(struct f_dfu *f_dfu,
517 const struct usb_ctrlrequest *ctrl,
518 struct usb_gadget *gadget,
519 struct usb_request *req)
520{
521 u16 w_value = le16_to_cpu(ctrl->wValue);
522 u16 len = le16_to_cpu(ctrl->wLength);
523 int value = 0;
524
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530525 len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len;
526
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200527 switch (ctrl->bRequest) {
528 case USB_REQ_DFU_UPLOAD:
Hugo SIMELIERE06ce62d2022-11-30 09:29:16 +0100529 if (ctrl->bRequestType & USB_DIR_IN) {
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530530 /* state transition if less data then requested */
531 f_dfu->blk_seq_num = w_value;
532 value = handle_upload(req, len);
533 if (value >= 0 && value < len)
534 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
535 }
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200536 break;
537 case USB_REQ_DFU_ABORT:
538 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
539 /* no zlp? */
540 value = RET_ZLP;
541 break;
542 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100543 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200544 break;
545 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100546 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200547 break;
548 default:
549 f_dfu->dfu_state = DFU_STATE_dfuERROR;
550 value = RET_STALL;
551 break;
552 }
553
554 return value;
555}
556
557static int state_dfu_error(struct f_dfu *f_dfu,
558 const struct usb_ctrlrequest *ctrl,
559 struct usb_gadget *gadget,
560 struct usb_request *req)
561{
562 int value = 0;
563
564 switch (ctrl->bRequest) {
565 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunaye652af82016-12-16 18:41:32 +0100566 value = handle_getstatus(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200567 break;
568 case USB_REQ_DFU_GETSTATE:
Patrick Delaunay16bb4082016-12-16 18:41:31 +0100569 value = handle_getstate(req);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200570 break;
571 case USB_REQ_DFU_CLRSTATUS:
572 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
573 f_dfu->dfu_status = DFU_STATUS_OK;
574 /* no zlp? */
575 value = RET_ZLP;
576 break;
577 default:
578 f_dfu->dfu_state = DFU_STATE_dfuERROR;
579 value = RET_STALL;
580 break;
581 }
582
583 return value;
584}
585
586static dfu_state_fn dfu_state[] = {
587 state_app_idle, /* DFU_STATE_appIDLE */
588 state_app_detach, /* DFU_STATE_appDETACH */
589 state_dfu_idle, /* DFU_STATE_dfuIDLE */
590 state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
591 state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
592 state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
593 state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
Heiko Schochere1ba1512014-03-18 08:09:56 +0100594 state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200595 NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
596 state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
597 state_dfu_error /* DFU_STATE_dfuERROR */
598};
599
600static int
601dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
602{
603 struct usb_gadget *gadget = f->config->cdev->gadget;
604 struct usb_request *req = f->config->cdev->req;
605 struct f_dfu *f_dfu = f->config->cdev->req->context;
606 u16 len = le16_to_cpu(ctrl->wLength);
607 u16 w_value = le16_to_cpu(ctrl->wValue);
608 int value = 0;
609 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
610
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530611 len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len;
612
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200613 debug("w_value: 0x%x len: 0x%x\n", w_value, len);
614 debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
615 req_type, ctrl->bRequest, f_dfu->dfu_state);
616
Andy Shevchenkof4f2db72020-01-29 17:23:14 +0200617#ifdef CONFIG_DFU_TIMEOUT
618 /* Forbid aborting by timeout. Next dfu command may update this */
619 dfu_set_timeout(0);
620#endif
621
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200622 if (req_type == USB_TYPE_STANDARD) {
623 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
624 (w_value >> 8) == DFU_DT_FUNC) {
625 value = min(len, (u16) sizeof(dfu_func));
626 memcpy(req->buf, &dfu_func, value);
627 }
628 } else /* DFU specific request */
629 value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
630
631 if (value >= 0) {
Venkatesh Yadav Abbarapu402cbd22022-11-03 09:37:48 +0530632 req->length = value > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : value;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200633 req->zero = value < len;
634 value = usb_ep_queue(gadget->ep0, req, 0);
635 if (value < 0) {
636 debug("ep_queue --> %d\n", value);
637 req->status = 0;
638 }
639 }
640
641 return value;
642}
643
644/*-------------------------------------------------------------------------*/
645
646static int
647dfu_prepare_strings(struct f_dfu *f_dfu, int n)
648{
649 struct dfu_entity *de = NULL;
650 int i = 0;
651
652 f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
653 if (!f_dfu->strings)
Peng Fan46ed88c2016-05-03 10:25:22 +0800654 return -ENOMEM;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200655
656 for (i = 0; i < n; ++i) {
657 de = dfu_get_entity(i);
658 f_dfu->strings[i].s = de->name;
659 }
660
661 f_dfu->strings[i].id = 0;
662 f_dfu->strings[i].s = NULL;
663
664 return 0;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200665}
666
667static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
668{
669 struct usb_interface_descriptor *d;
670 int i = 0;
671
Patrick Delaunaybf2a4152016-12-08 18:10:49 +0100672 f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200673 if (!f_dfu->function)
674 goto enomem;
675
676 for (i = 0; i < n; ++i) {
677 d = calloc(sizeof(*d), 1);
678 if (!d)
679 goto enomem;
680
681 d->bLength = sizeof(*d);
682 d->bDescriptorType = USB_DT_INTERFACE;
683 d->bAlternateSetting = i;
684 d->bNumEndpoints = 0;
685 d->bInterfaceClass = USB_CLASS_APP_SPEC;
686 d->bInterfaceSubClass = 1;
687 d->bInterfaceProtocol = 2;
688
689 f_dfu->function[i] = (struct usb_descriptor_header *)d;
690 }
Patrick Delaunaybf2a4152016-12-08 18:10:49 +0100691
692 /* add DFU Functional Descriptor */
693 f_dfu->function[i] = calloc(sizeof(dfu_func), 1);
694 if (!f_dfu->function[i])
695 goto enomem;
696 memcpy(f_dfu->function[i], &dfu_func, sizeof(dfu_func));
697
698 i++;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200699 f_dfu->function[i] = NULL;
700
701 return 0;
702
703enomem:
704 while (i) {
705 free(f_dfu->function[--i]);
706 f_dfu->function[i] = NULL;
707 }
708 free(f_dfu->function);
709
710 return -ENOMEM;
711}
712
713static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
714{
715 struct usb_composite_dev *cdev = c->cdev;
716 struct f_dfu *f_dfu = func_to_dfu(f);
Felipe Balbid1c34e42017-02-22 11:22:38 +0200717 const char *s;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200718 int alt_num = dfu_get_alt_number();
719 int rv, id, i;
720
721 id = usb_interface_id(c, f);
722 if (id < 0)
723 return id;
724 dfu_intf_runtime.bInterfaceNumber = id;
725
726 f_dfu->dfu_state = DFU_STATE_appIDLE;
727 f_dfu->dfu_status = DFU_STATUS_OK;
728
729 rv = dfu_prepare_function(f_dfu, alt_num);
730 if (rv)
731 goto error;
732
733 rv = dfu_prepare_strings(f_dfu, alt_num);
734 if (rv)
735 goto error;
736 for (i = 0; i < alt_num; i++) {
737 id = usb_string_id(cdev);
738 if (id < 0)
739 return id;
740 f_dfu->strings[i].id = id;
741 ((struct usb_interface_descriptor *)f_dfu->function[i])
742 ->iInterface = id;
743 }
744
Heiko Schocher5890e832013-06-25 13:59:29 +0200745 to_dfu_mode(f_dfu);
746
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200747 stringtab_dfu.strings = f_dfu->strings;
748
749 cdev->req->context = f_dfu;
750
Simon Glass64b723f2017-08-03 12:22:12 -0600751 s = env_get("serial#");
Felipe Balbid1c34e42017-02-22 11:22:38 +0200752 if (s)
753 g_dnl_set_serialnumber((char *)s);
754
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200755error:
756 return rv;
757}
758
759static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
760{
761 struct f_dfu *f_dfu = func_to_dfu(f);
762 int alt_num = dfu_get_alt_number();
763 int i;
764
765 if (f_dfu->strings) {
766 i = alt_num;
767 while (i)
768 f_dfu->strings[--i].s = NULL;
769
770 free(f_dfu->strings);
771 }
772
773 if (f_dfu->function) {
774 i = alt_num;
Ralph Siemsen4cceff62019-06-28 10:42:02 -0400775 i++; /* free DFU Functional Descriptor */
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200776 while (i) {
777 free(f_dfu->function[--i]);
778 f_dfu->function[i] = NULL;
779 }
780 free(f_dfu->function);
781 }
782
783 free(f_dfu);
784}
785
786static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
787{
788 struct f_dfu *f_dfu = func_to_dfu(f);
789
790 debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
791
792 f_dfu->altsetting = alt;
Stephen Warren7f450232014-06-11 12:48:08 -0600793 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
794 f_dfu->dfu_status = DFU_STATUS_OK;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200795
796 return 0;
797}
798
Stefan Roese5a6b8732015-01-09 14:54:56 +0100799static int __dfu_get_alt(struct usb_function *f, unsigned intf)
800{
801 struct f_dfu *f_dfu = func_to_dfu(f);
802
803 return f_dfu->altsetting;
804}
805
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200806/* TODO: is this really what we need here? */
807static void dfu_disable(struct usb_function *f)
808{
809 struct f_dfu *f_dfu = func_to_dfu(f);
810 if (f_dfu->config == 0)
811 return;
812
813 debug("%s: reset config\n", __func__);
814
815 f_dfu->config = 0;
816}
817
818static int dfu_bind_config(struct usb_configuration *c)
819{
820 struct f_dfu *f_dfu;
821 int status;
822
823 f_dfu = calloc(sizeof(*f_dfu), 1);
824 if (!f_dfu)
825 return -ENOMEM;
826 f_dfu->usb_function.name = "dfu";
827 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
Heiko Schocher3e3a1362014-09-10 08:55:00 +0200828 f_dfu->usb_function.descriptors = dfu_runtime_descs;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200829 f_dfu->usb_function.bind = dfu_bind;
830 f_dfu->usb_function.unbind = dfu_unbind;
831 f_dfu->usb_function.set_alt = dfu_set_alt;
Stefan Roese5a6b8732015-01-09 14:54:56 +0100832 f_dfu->usb_function.get_alt = __dfu_get_alt;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200833 f_dfu->usb_function.disable = dfu_disable;
Lukasz Majewski1abe47d2013-12-09 16:20:15 +0100834 f_dfu->usb_function.strings = dfu_generic_strings;
835 f_dfu->usb_function.setup = dfu_handle;
Lukasz Majewski2bb115f2013-12-09 16:20:14 +0100836 f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200837
838 status = usb_add_function(c, &f_dfu->usb_function);
839 if (status)
840 free(f_dfu);
841
842 return status;
843}
844
845int dfu_add(struct usb_configuration *c)
846{
847 int id;
848
849 id = usb_string_id(c->cdev);
850 if (id < 0)
851 return id;
852 strings_dfu_generic[0].id = id;
853 dfu_intf_runtime.iInterface = id;
854
855 debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
856 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
857
858 return dfu_bind_config(c);
859}
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200860
861DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);