blob: 30ece524a8e142eb1fd8662eb36bdea0b1d5b842 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskib819ddb2012-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 Majewski81c065d2013-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 Majewskib819ddb2012-08-06 14:41:06 +020015 */
16
17#include <errno.h>
18#include <common.h>
19#include <malloc.h>
20
21#include <linux/usb/ch9.h>
Lukasz Majewskib819ddb2012-08-06 14:41:06 +020022#include <linux/usb/gadget.h>
23#include <linux/usb/composite.h>
24
25#include <dfu.h>
Mateusz Zalegac4d0e852014-04-28 21:13:28 +020026#include <g_dnl.h>
Lukasz Majewskib819ddb2012-08-06 14:41:06 +020027#include "f_dfu.h"
28
29struct f_dfu {
30 struct usb_function usb_function;
31
32 struct usb_descriptor_header **function;
33 struct usb_string *strings;
34
35 /* when configured, we have one config */
36 u8 config;
37 u8 altsetting;
38 enum dfu_state dfu_state;
39 unsigned int dfu_status;
40
41 /* Send/received block number is handy for data integrity check */
42 int blk_seq_num;
Lukasz Majewski33fac4a2013-12-09 16:20:14 +010043 unsigned int poll_timeout;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +020044};
45
Lukasz Majewskifc18f8d2016-01-28 16:14:49 +010046struct dfu_entity *dfu_defer_flush;
47
Lukasz Majewskib819ddb2012-08-06 14:41:06 +020048typedef int (*dfu_state_fn) (struct f_dfu *,
49 const struct usb_ctrlrequest *,
50 struct usb_gadget *,
51 struct usb_request *);
52
53static inline struct f_dfu *func_to_dfu(struct usb_function *f)
54{
55 return container_of(f, struct f_dfu, usb_function);
56}
57
58static const struct dfu_function_descriptor dfu_func = {
59 .bLength = sizeof dfu_func,
60 .bDescriptorType = DFU_DT_FUNC,
61 .bmAttributes = DFU_BIT_WILL_DETACH |
62 DFU_BIT_MANIFESTATION_TOLERANT |
63 DFU_BIT_CAN_UPLOAD |
64 DFU_BIT_CAN_DNLOAD,
65 .wDetachTimeOut = 0,
66 .wTransferSize = DFU_USB_BUFSIZ,
67 .bcdDFUVersion = __constant_cpu_to_le16(0x0110),
68};
69
70static struct usb_interface_descriptor dfu_intf_runtime = {
71 .bLength = sizeof dfu_intf_runtime,
72 .bDescriptorType = USB_DT_INTERFACE,
73 .bNumEndpoints = 0,
74 .bInterfaceClass = USB_CLASS_APP_SPEC,
75 .bInterfaceSubClass = 1,
76 .bInterfaceProtocol = 1,
77 /* .iInterface = DYNAMIC */
78};
79
80static struct usb_descriptor_header *dfu_runtime_descs[] = {
81 (struct usb_descriptor_header *) &dfu_intf_runtime,
82 NULL,
83};
84
Lukasz Majewskib819ddb2012-08-06 14:41:06 +020085static const char dfu_name[] = "Device Firmware Upgrade";
86
87/*
88 * static strings, in UTF-8
89 *
90 * dfu_generic configuration
91 */
92static struct usb_string strings_dfu_generic[] = {
93 [0].s = dfu_name,
94 { } /* end of list */
95};
96
97static struct usb_gadget_strings stringtab_dfu_generic = {
98 .language = 0x0409, /* en-us */
99 .strings = strings_dfu_generic,
100};
101
102static struct usb_gadget_strings *dfu_generic_strings[] = {
103 &stringtab_dfu_generic,
104 NULL,
105};
106
107/*
108 * usb_function specific
109 */
110static struct usb_gadget_strings stringtab_dfu = {
111 .language = 0x0409, /* en-us */
112 /*
113 * .strings
114 *
115 * assigned during initialization,
116 * depends on number of flash entities
117 *
118 */
119};
120
121static struct usb_gadget_strings *dfu_strings[] = {
122 &stringtab_dfu,
123 NULL,
124};
125
Lukasz Majewski33fac4a2013-12-09 16:20:14 +0100126static void dfu_set_poll_timeout(struct dfu_status *dstat, unsigned int ms)
127{
128 /*
129 * The bwPollTimeout DFU_GETSTATUS request payload provides information
130 * about minimum time, in milliseconds, that the host should wait before
131 * sending a subsequent DFU_GETSTATUS request
132 *
133 * This permits the device to vary the delay depending on its need to
134 * erase or program the memory
135 *
136 */
137
138 unsigned char *p = (unsigned char *)&ms;
139
140 if (!ms || (ms & ~DFU_POLL_TIMEOUT_MASK)) {
141 dstat->bwPollTimeout[0] = 0;
142 dstat->bwPollTimeout[1] = 0;
143 dstat->bwPollTimeout[2] = 0;
144
145 return;
146 }
147
148 dstat->bwPollTimeout[0] = *p++;
149 dstat->bwPollTimeout[1] = *p++;
150 dstat->bwPollTimeout[2] = *p;
151}
152
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200153/*-------------------------------------------------------------------------*/
154
155static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
156{
157 struct f_dfu *f_dfu = req->context;
Stephen Warren3ee95932014-06-11 12:48:08 -0600158 int ret;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200159
Stephen Warren3ee95932014-06-11 12:48:08 -0600160 ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
Felipe Balbi00e9d692017-02-10 19:32:31 +0300161 req->actual, f_dfu->blk_seq_num);
Stephen Warren3ee95932014-06-11 12:48:08 -0600162 if (ret) {
163 f_dfu->dfu_status = DFU_STATUS_errUNKNOWN;
164 f_dfu->dfu_state = DFU_STATE_dfuERROR;
165 }
Heiko Schocher001a8312014-03-18 08:09:56 +0100166}
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200167
Heiko Schocher001a8312014-03-18 08:09:56 +0100168static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req)
169{
170 struct f_dfu *f_dfu = req->context;
Lukasz Majewskifc18f8d2016-01-28 16:14:49 +0100171 dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting));
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200172}
173
Heiko Schocherfc25fa22014-04-11 07:59:47 +0200174static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu)
175{
176 return dfu->poll_timeout ? dfu->poll_timeout(dfu) :
177 DFU_MANIFEST_POLL_TIMEOUT;
178}
179
Patrick Delaunayd4287762016-12-16 18:41:32 +0100180static int handle_getstatus(struct usb_request *req)
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200181{
182 struct dfu_status *dstat = (struct dfu_status *)req->buf;
183 struct f_dfu *f_dfu = req->context;
Heiko Schocherfc25fa22014-04-11 07:59:47 +0200184 struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200185
Heiko Schocher001a8312014-03-18 08:09:56 +0100186 dfu_set_poll_timeout(dstat, 0);
187
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200188 switch (f_dfu->dfu_state) {
189 case DFU_STATE_dfuDNLOAD_SYNC:
190 case DFU_STATE_dfuDNBUSY:
191 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
192 break;
193 case DFU_STATE_dfuMANIFEST_SYNC:
Heiko Schocher001a8312014-03-18 08:09:56 +0100194 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200195 break;
Heiko Schocher001a8312014-03-18 08:09:56 +0100196 case DFU_STATE_dfuMANIFEST:
Heiko Schocherfc25fa22014-04-11 07:59:47 +0200197 dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu));
198 break;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200199 default:
200 break;
201 }
202
Lukasz Majewski33fac4a2013-12-09 16:20:14 +0100203 if (f_dfu->poll_timeout)
204 if (!(f_dfu->blk_seq_num %
205 (dfu_get_buf_size() / DFU_USB_BUFSIZ)))
206 dfu_set_poll_timeout(dstat, f_dfu->poll_timeout);
207
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200208 /* send status response */
209 dstat->bStatus = f_dfu->dfu_status;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200210 dstat->bState = f_dfu->dfu_state;
211 dstat->iString = 0;
Patrick Delaunayd4287762016-12-16 18:41:32 +0100212
213 return sizeof(struct dfu_status);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200214}
215
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100216static int handle_getstate(struct usb_request *req)
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200217{
218 struct f_dfu *f_dfu = req->context;
219
220 ((u8 *)req->buf)[0] = f_dfu->dfu_state;
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100221 return sizeof(u8);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200222}
223
224static inline void to_dfu_mode(struct f_dfu *f_dfu)
225{
226 f_dfu->usb_function.strings = dfu_strings;
227 f_dfu->usb_function.hs_descriptors = f_dfu->function;
Heiko Schocher9f3b8ed2014-09-10 08:55:00 +0200228 f_dfu->usb_function.descriptors = f_dfu->function;
Heiko Schocherad5f9772013-06-25 13:59:29 +0200229 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200230}
231
232static inline void to_runtime_mode(struct f_dfu *f_dfu)
233{
234 f_dfu->usb_function.strings = NULL;
235 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
Heiko Schocher9f3b8ed2014-09-10 08:55:00 +0200236 f_dfu->usb_function.descriptors = dfu_runtime_descs;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200237}
238
239static int handle_upload(struct usb_request *req, u16 len)
240{
241 struct f_dfu *f_dfu = req->context;
242
243 return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
244 req->length, f_dfu->blk_seq_num);
245}
246
247static int handle_dnload(struct usb_gadget *gadget, u16 len)
248{
249 struct usb_composite_dev *cdev = get_gadget_data(gadget);
250 struct usb_request *req = cdev->req;
251 struct f_dfu *f_dfu = req->context;
252
253 if (len == 0)
254 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
255
256 req->complete = dnload_request_complete;
257
258 return len;
259}
260
261/*-------------------------------------------------------------------------*/
262/* DFU state machine */
263static int state_app_idle(struct f_dfu *f_dfu,
264 const struct usb_ctrlrequest *ctrl,
265 struct usb_gadget *gadget,
266 struct usb_request *req)
267{
268 int value = 0;
269
270 switch (ctrl->bRequest) {
271 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100272 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200273 break;
274 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100275 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200276 break;
277 case USB_REQ_DFU_DETACH:
278 f_dfu->dfu_state = DFU_STATE_appDETACH;
279 to_dfu_mode(f_dfu);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200280 value = RET_ZLP;
281 break;
282 default:
283 value = RET_STALL;
284 break;
285 }
286
287 return value;
288}
289
290static int state_app_detach(struct f_dfu *f_dfu,
291 const struct usb_ctrlrequest *ctrl,
292 struct usb_gadget *gadget,
293 struct usb_request *req)
294{
295 int value = 0;
296
297 switch (ctrl->bRequest) {
298 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100299 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200300 break;
301 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100302 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200303 break;
304 default:
305 f_dfu->dfu_state = DFU_STATE_appIDLE;
306 value = RET_STALL;
307 break;
308 }
309
310 return value;
311}
312
313static int state_dfu_idle(struct f_dfu *f_dfu,
314 const struct usb_ctrlrequest *ctrl,
315 struct usb_gadget *gadget,
316 struct usb_request *req)
317{
318 u16 w_value = le16_to_cpu(ctrl->wValue);
319 u16 len = le16_to_cpu(ctrl->wLength);
320 int value = 0;
321
322 switch (ctrl->bRequest) {
323 case USB_REQ_DFU_DNLOAD:
324 if (len == 0) {
325 f_dfu->dfu_state = DFU_STATE_dfuERROR;
326 value = RET_STALL;
327 break;
328 }
329 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
330 f_dfu->blk_seq_num = w_value;
331 value = handle_dnload(gadget, len);
332 break;
333 case USB_REQ_DFU_UPLOAD:
334 f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
335 f_dfu->blk_seq_num = 0;
336 value = handle_upload(req, len);
337 break;
338 case USB_REQ_DFU_ABORT:
339 /* no zlp? */
340 value = RET_ZLP;
341 break;
342 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100343 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200344 break;
345 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100346 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200347 break;
348 case USB_REQ_DFU_DETACH:
349 /*
350 * Proprietary extension: 'detach' from idle mode and
351 * get back to runtime mode in case of USB Reset. As
352 * much as I dislike this, we just can't use every USB
353 * bus reset to switch back to runtime mode, since at
354 * least the Linux USB stack likes to send a number of
355 * resets in a row :(
356 */
357 f_dfu->dfu_state =
358 DFU_STATE_dfuMANIFEST_WAIT_RST;
359 to_runtime_mode(f_dfu);
360 f_dfu->dfu_state = DFU_STATE_appIDLE;
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +0200361
Rob Herringfe1b28c2014-12-10 14:43:03 -0600362 g_dnl_trigger_detach();
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200363 break;
364 default:
365 f_dfu->dfu_state = DFU_STATE_dfuERROR;
366 value = RET_STALL;
367 break;
368 }
369
370 return value;
371}
372
373static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
374 const struct usb_ctrlrequest *ctrl,
375 struct usb_gadget *gadget,
376 struct usb_request *req)
377{
378 int value = 0;
379
380 switch (ctrl->bRequest) {
381 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100382 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200383 break;
384 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100385 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200386 break;
387 default:
388 f_dfu->dfu_state = DFU_STATE_dfuERROR;
389 value = RET_STALL;
390 break;
391 }
392
393 return value;
394}
395
396static int state_dfu_dnbusy(struct f_dfu *f_dfu,
397 const struct usb_ctrlrequest *ctrl,
398 struct usb_gadget *gadget,
399 struct usb_request *req)
400{
401 int value = 0;
402
403 switch (ctrl->bRequest) {
404 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100405 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200406 break;
407 default:
408 f_dfu->dfu_state = DFU_STATE_dfuERROR;
409 value = RET_STALL;
410 break;
411 }
412
413 return value;
414}
415
416static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
417 const struct usb_ctrlrequest *ctrl,
418 struct usb_gadget *gadget,
419 struct usb_request *req)
420{
421 u16 w_value = le16_to_cpu(ctrl->wValue);
422 u16 len = le16_to_cpu(ctrl->wLength);
423 int value = 0;
424
425 switch (ctrl->bRequest) {
426 case USB_REQ_DFU_DNLOAD:
427 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
428 f_dfu->blk_seq_num = w_value;
429 value = handle_dnload(gadget, len);
430 break;
431 case USB_REQ_DFU_ABORT:
432 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
433 value = RET_ZLP;
434 break;
435 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100436 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200437 break;
438 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100439 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200440 break;
441 default:
442 f_dfu->dfu_state = DFU_STATE_dfuERROR;
443 value = RET_STALL;
444 break;
445 }
446
447 return value;
448}
449
450static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
451 const struct usb_ctrlrequest *ctrl,
452 struct usb_gadget *gadget,
453 struct usb_request *req)
454{
455 int value = 0;
456
457 switch (ctrl->bRequest) {
458 case USB_REQ_DFU_GETSTATUS:
459 /* We're MainfestationTolerant */
Heiko Schocher001a8312014-03-18 08:09:56 +0100460 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
Patrick Delaunayd4287762016-12-16 18:41:32 +0100461 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200462 f_dfu->blk_seq_num = 0;
Heiko Schocher001a8312014-03-18 08:09:56 +0100463 req->complete = dnload_request_flush;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200464 break;
465 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100466 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200467 break;
468 default:
469 f_dfu->dfu_state = DFU_STATE_dfuERROR;
470 value = RET_STALL;
471 break;
472 }
473
474 return value;
475}
476
Heiko Schocher001a8312014-03-18 08:09:56 +0100477static int state_dfu_manifest(struct f_dfu *f_dfu,
478 const struct usb_ctrlrequest *ctrl,
479 struct usb_gadget *gadget,
480 struct usb_request *req)
481{
482 int value = 0;
483
484 switch (ctrl->bRequest) {
485 case USB_REQ_DFU_GETSTATUS:
486 /* We're MainfestationTolerant */
487 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
Patrick Delaunayd4287762016-12-16 18:41:32 +0100488 value = handle_getstatus(req);
Heiko Schocher001a8312014-03-18 08:09:56 +0100489 f_dfu->blk_seq_num = 0;
Heiko Schocher001a8312014-03-18 08:09:56 +0100490 puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
491 break;
492 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100493 value = handle_getstate(req);
Heiko Schocher001a8312014-03-18 08:09:56 +0100494 break;
495 default:
496 f_dfu->dfu_state = DFU_STATE_dfuERROR;
497 value = RET_STALL;
498 break;
499 }
500 return value;
501}
502
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200503static int state_dfu_upload_idle(struct f_dfu *f_dfu,
504 const struct usb_ctrlrequest *ctrl,
505 struct usb_gadget *gadget,
506 struct usb_request *req)
507{
508 u16 w_value = le16_to_cpu(ctrl->wValue);
509 u16 len = le16_to_cpu(ctrl->wLength);
510 int value = 0;
511
512 switch (ctrl->bRequest) {
513 case USB_REQ_DFU_UPLOAD:
514 /* state transition if less data then requested */
515 f_dfu->blk_seq_num = w_value;
516 value = handle_upload(req, len);
517 if (value >= 0 && value < len)
518 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
519 break;
520 case USB_REQ_DFU_ABORT:
521 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
522 /* no zlp? */
523 value = RET_ZLP;
524 break;
525 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100526 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200527 break;
528 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100529 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200530 break;
531 default:
532 f_dfu->dfu_state = DFU_STATE_dfuERROR;
533 value = RET_STALL;
534 break;
535 }
536
537 return value;
538}
539
540static int state_dfu_error(struct f_dfu *f_dfu,
541 const struct usb_ctrlrequest *ctrl,
542 struct usb_gadget *gadget,
543 struct usb_request *req)
544{
545 int value = 0;
546
547 switch (ctrl->bRequest) {
548 case USB_REQ_DFU_GETSTATUS:
Patrick Delaunayd4287762016-12-16 18:41:32 +0100549 value = handle_getstatus(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200550 break;
551 case USB_REQ_DFU_GETSTATE:
Patrick Delaunayf11bb252016-12-16 18:41:31 +0100552 value = handle_getstate(req);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200553 break;
554 case USB_REQ_DFU_CLRSTATUS:
555 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
556 f_dfu->dfu_status = DFU_STATUS_OK;
557 /* no zlp? */
558 value = RET_ZLP;
559 break;
560 default:
561 f_dfu->dfu_state = DFU_STATE_dfuERROR;
562 value = RET_STALL;
563 break;
564 }
565
566 return value;
567}
568
569static dfu_state_fn dfu_state[] = {
570 state_app_idle, /* DFU_STATE_appIDLE */
571 state_app_detach, /* DFU_STATE_appDETACH */
572 state_dfu_idle, /* DFU_STATE_dfuIDLE */
573 state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
574 state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
575 state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
576 state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
Heiko Schocher001a8312014-03-18 08:09:56 +0100577 state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200578 NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
579 state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
580 state_dfu_error /* DFU_STATE_dfuERROR */
581};
582
583static int
584dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
585{
586 struct usb_gadget *gadget = f->config->cdev->gadget;
587 struct usb_request *req = f->config->cdev->req;
588 struct f_dfu *f_dfu = f->config->cdev->req->context;
589 u16 len = le16_to_cpu(ctrl->wLength);
590 u16 w_value = le16_to_cpu(ctrl->wValue);
591 int value = 0;
592 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
593
594 debug("w_value: 0x%x len: 0x%x\n", w_value, len);
595 debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
596 req_type, ctrl->bRequest, f_dfu->dfu_state);
597
598 if (req_type == USB_TYPE_STANDARD) {
599 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
600 (w_value >> 8) == DFU_DT_FUNC) {
601 value = min(len, (u16) sizeof(dfu_func));
602 memcpy(req->buf, &dfu_func, value);
603 }
604 } else /* DFU specific request */
605 value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
606
607 if (value >= 0) {
608 req->length = value;
609 req->zero = value < len;
610 value = usb_ep_queue(gadget->ep0, req, 0);
611 if (value < 0) {
612 debug("ep_queue --> %d\n", value);
613 req->status = 0;
614 }
615 }
616
617 return value;
618}
619
620/*-------------------------------------------------------------------------*/
621
622static int
623dfu_prepare_strings(struct f_dfu *f_dfu, int n)
624{
625 struct dfu_entity *de = NULL;
626 int i = 0;
627
628 f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
629 if (!f_dfu->strings)
Peng Fan12ff19d2016-05-03 10:25:22 +0800630 return -ENOMEM;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200631
632 for (i = 0; i < n; ++i) {
633 de = dfu_get_entity(i);
634 f_dfu->strings[i].s = de->name;
635 }
636
637 f_dfu->strings[i].id = 0;
638 f_dfu->strings[i].s = NULL;
639
640 return 0;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200641}
642
643static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
644{
645 struct usb_interface_descriptor *d;
646 int i = 0;
647
Patrick Delaunay89870122016-12-08 18:10:49 +0100648 f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2);
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200649 if (!f_dfu->function)
650 goto enomem;
651
652 for (i = 0; i < n; ++i) {
653 d = calloc(sizeof(*d), 1);
654 if (!d)
655 goto enomem;
656
657 d->bLength = sizeof(*d);
658 d->bDescriptorType = USB_DT_INTERFACE;
659 d->bAlternateSetting = i;
660 d->bNumEndpoints = 0;
661 d->bInterfaceClass = USB_CLASS_APP_SPEC;
662 d->bInterfaceSubClass = 1;
663 d->bInterfaceProtocol = 2;
664
665 f_dfu->function[i] = (struct usb_descriptor_header *)d;
666 }
Patrick Delaunay89870122016-12-08 18:10:49 +0100667
668 /* add DFU Functional Descriptor */
669 f_dfu->function[i] = calloc(sizeof(dfu_func), 1);
670 if (!f_dfu->function[i])
671 goto enomem;
672 memcpy(f_dfu->function[i], &dfu_func, sizeof(dfu_func));
673
674 i++;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200675 f_dfu->function[i] = NULL;
676
677 return 0;
678
679enomem:
680 while (i) {
681 free(f_dfu->function[--i]);
682 f_dfu->function[i] = NULL;
683 }
684 free(f_dfu->function);
685
686 return -ENOMEM;
687}
688
689static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
690{
691 struct usb_composite_dev *cdev = c->cdev;
692 struct f_dfu *f_dfu = func_to_dfu(f);
Felipe Balbi9bf9e8132017-02-22 11:22:38 +0200693 const char *s;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200694 int alt_num = dfu_get_alt_number();
695 int rv, id, i;
696
697 id = usb_interface_id(c, f);
698 if (id < 0)
699 return id;
700 dfu_intf_runtime.bInterfaceNumber = id;
701
702 f_dfu->dfu_state = DFU_STATE_appIDLE;
703 f_dfu->dfu_status = DFU_STATUS_OK;
704
705 rv = dfu_prepare_function(f_dfu, alt_num);
706 if (rv)
707 goto error;
708
709 rv = dfu_prepare_strings(f_dfu, alt_num);
710 if (rv)
711 goto error;
712 for (i = 0; i < alt_num; i++) {
713 id = usb_string_id(cdev);
714 if (id < 0)
715 return id;
716 f_dfu->strings[i].id = id;
717 ((struct usb_interface_descriptor *)f_dfu->function[i])
718 ->iInterface = id;
719 }
720
Heiko Schocherad5f9772013-06-25 13:59:29 +0200721 to_dfu_mode(f_dfu);
722
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200723 stringtab_dfu.strings = f_dfu->strings;
724
725 cdev->req->context = f_dfu;
726
Simon Glass00caae62017-08-03 12:22:12 -0600727 s = env_get("serial#");
Felipe Balbi9bf9e8132017-02-22 11:22:38 +0200728 if (s)
729 g_dnl_set_serialnumber((char *)s);
730
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200731error:
732 return rv;
733}
734
735static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
736{
737 struct f_dfu *f_dfu = func_to_dfu(f);
738 int alt_num = dfu_get_alt_number();
739 int i;
740
741 if (f_dfu->strings) {
742 i = alt_num;
743 while (i)
744 f_dfu->strings[--i].s = NULL;
745
746 free(f_dfu->strings);
747 }
748
749 if (f_dfu->function) {
750 i = alt_num;
751 while (i) {
752 free(f_dfu->function[--i]);
753 f_dfu->function[i] = NULL;
754 }
755 free(f_dfu->function);
756 }
757
758 free(f_dfu);
759}
760
761static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
762{
763 struct f_dfu *f_dfu = func_to_dfu(f);
764
765 debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
766
767 f_dfu->altsetting = alt;
Stephen Warren3ee95932014-06-11 12:48:08 -0600768 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
769 f_dfu->dfu_status = DFU_STATUS_OK;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200770
771 return 0;
772}
773
Stefan Roese1fd81b72015-01-09 14:54:56 +0100774static int __dfu_get_alt(struct usb_function *f, unsigned intf)
775{
776 struct f_dfu *f_dfu = func_to_dfu(f);
777
778 return f_dfu->altsetting;
779}
780
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200781/* TODO: is this really what we need here? */
782static void dfu_disable(struct usb_function *f)
783{
784 struct f_dfu *f_dfu = func_to_dfu(f);
785 if (f_dfu->config == 0)
786 return;
787
788 debug("%s: reset config\n", __func__);
789
790 f_dfu->config = 0;
791}
792
793static int dfu_bind_config(struct usb_configuration *c)
794{
795 struct f_dfu *f_dfu;
796 int status;
797
798 f_dfu = calloc(sizeof(*f_dfu), 1);
799 if (!f_dfu)
800 return -ENOMEM;
801 f_dfu->usb_function.name = "dfu";
802 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
Heiko Schocher9f3b8ed2014-09-10 08:55:00 +0200803 f_dfu->usb_function.descriptors = dfu_runtime_descs;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200804 f_dfu->usb_function.bind = dfu_bind;
805 f_dfu->usb_function.unbind = dfu_unbind;
806 f_dfu->usb_function.set_alt = dfu_set_alt;
Stefan Roese1fd81b72015-01-09 14:54:56 +0100807 f_dfu->usb_function.get_alt = __dfu_get_alt;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200808 f_dfu->usb_function.disable = dfu_disable;
Lukasz Majewski77b95042013-12-09 16:20:15 +0100809 f_dfu->usb_function.strings = dfu_generic_strings;
810 f_dfu->usb_function.setup = dfu_handle;
Lukasz Majewski33fac4a2013-12-09 16:20:14 +0100811 f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
Lukasz Majewskib819ddb2012-08-06 14:41:06 +0200812
813 status = usb_add_function(c, &f_dfu->usb_function);
814 if (status)
815 free(f_dfu);
816
817 return status;
818}
819
820int dfu_add(struct usb_configuration *c)
821{
822 int id;
823
824 id = usb_string_id(c->cdev);
825 if (id < 0)
826 return id;
827 strings_dfu_generic[0].id = id;
828 dfu_intf_runtime.iInterface = id;
829
830 debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
831 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
832
833 return dfu_bind_config(c);
834}
Mateusz Zalegac4d0e852014-04-28 21:13:28 +0200835
836DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);