blob: 73b32f8ae5eb91adfa3e4d34c67f608c495373fe [file] [log] [blame]
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +02001/*
2 * f_dfu.c -- Device Firmware Update USB function
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
6 * Lukasz Majewski <l.majewski@samsung.com>
7 *
Ɓukasz Majewski7daece82013-07-05 11:40:47 +02008 * Based on OpenMoko u-boot: drivers/usb/usbdfu.c
9 * (C) 2007 by OpenMoko, Inc.
10 * Author: Harald Welte <laforge@openmoko.org>
11 *
12 * based on existing SAM7DFU code from OpenPCD:
13 * (C) Copyright 2006 by Harald Welte <hwelte at hmw-consulting.de>
14 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020015 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +020016 */
17
18#include <errno.h>
19#include <common.h>
20#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,
162 req->length, f_dfu->blk_seq_num);
163 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
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200181static void handle_getstatus(struct usb_request *req)
182{
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;
213}
214
215static void handle_getstate(struct usb_request *req)
216{
217 struct f_dfu *f_dfu = req->context;
218
219 ((u8 *)req->buf)[0] = f_dfu->dfu_state;
220 req->actual = sizeof(u8);
221}
222
223static inline void to_dfu_mode(struct f_dfu *f_dfu)
224{
225 f_dfu->usb_function.strings = dfu_strings;
226 f_dfu->usb_function.hs_descriptors = f_dfu->function;
Heiko Schocher3e3a1362014-09-10 08:55:00 +0200227 f_dfu->usb_function.descriptors = f_dfu->function;
Heiko Schocher5890e832013-06-25 13:59:29 +0200228 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200229}
230
231static inline void to_runtime_mode(struct f_dfu *f_dfu)
232{
233 f_dfu->usb_function.strings = NULL;
234 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
Heiko Schocher3e3a1362014-09-10 08:55:00 +0200235 f_dfu->usb_function.descriptors = dfu_runtime_descs;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200236}
237
238static int handle_upload(struct usb_request *req, u16 len)
239{
240 struct f_dfu *f_dfu = req->context;
241
242 return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
243 req->length, f_dfu->blk_seq_num);
244}
245
246static int handle_dnload(struct usb_gadget *gadget, u16 len)
247{
248 struct usb_composite_dev *cdev = get_gadget_data(gadget);
249 struct usb_request *req = cdev->req;
250 struct f_dfu *f_dfu = req->context;
251
252 if (len == 0)
253 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
254
255 req->complete = dnload_request_complete;
256
257 return len;
258}
259
260/*-------------------------------------------------------------------------*/
261/* DFU state machine */
262static int state_app_idle(struct f_dfu *f_dfu,
263 const struct usb_ctrlrequest *ctrl,
264 struct usb_gadget *gadget,
265 struct usb_request *req)
266{
267 int value = 0;
268
269 switch (ctrl->bRequest) {
270 case USB_REQ_DFU_GETSTATUS:
271 handle_getstatus(req);
272 value = RET_STAT_LEN;
273 break;
274 case USB_REQ_DFU_GETSTATE:
275 handle_getstate(req);
276 break;
277 case USB_REQ_DFU_DETACH:
278 f_dfu->dfu_state = DFU_STATE_appDETACH;
279 to_dfu_mode(f_dfu);
Lukasz Majewskib7a15bf2012-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:
299 handle_getstatus(req);
300 value = RET_STAT_LEN;
301 break;
302 case USB_REQ_DFU_GETSTATE:
303 handle_getstate(req);
304 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
323 switch (ctrl->bRequest) {
324 case USB_REQ_DFU_DNLOAD:
325 if (len == 0) {
326 f_dfu->dfu_state = DFU_STATE_dfuERROR;
327 value = RET_STALL;
328 break;
329 }
330 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
331 f_dfu->blk_seq_num = w_value;
332 value = handle_dnload(gadget, len);
333 break;
334 case USB_REQ_DFU_UPLOAD:
335 f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
336 f_dfu->blk_seq_num = 0;
337 value = handle_upload(req, len);
338 break;
339 case USB_REQ_DFU_ABORT:
340 /* no zlp? */
341 value = RET_ZLP;
342 break;
343 case USB_REQ_DFU_GETSTATUS:
344 handle_getstatus(req);
345 value = RET_STAT_LEN;
346 break;
347 case USB_REQ_DFU_GETSTATE:
348 handle_getstate(req);
349 break;
350 case USB_REQ_DFU_DETACH:
351 /*
352 * Proprietary extension: 'detach' from idle mode and
353 * get back to runtime mode in case of USB Reset. As
354 * much as I dislike this, we just can't use every USB
355 * bus reset to switch back to runtime mode, since at
356 * least the Linux USB stack likes to send a number of
357 * resets in a row :(
358 */
359 f_dfu->dfu_state =
360 DFU_STATE_dfuMANIFEST_WAIT_RST;
361 to_runtime_mode(f_dfu);
362 f_dfu->dfu_state = DFU_STATE_appIDLE;
Lukasz Majewski2968aa22013-07-18 13:19:14 +0200363
Rob Herringd38f24e2014-12-10 14:43:03 -0600364 g_dnl_trigger_detach();
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200365 break;
366 default:
367 f_dfu->dfu_state = DFU_STATE_dfuERROR;
368 value = RET_STALL;
369 break;
370 }
371
372 return value;
373}
374
375static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
376 const struct usb_ctrlrequest *ctrl,
377 struct usb_gadget *gadget,
378 struct usb_request *req)
379{
380 int value = 0;
381
382 switch (ctrl->bRequest) {
383 case USB_REQ_DFU_GETSTATUS:
384 handle_getstatus(req);
385 value = RET_STAT_LEN;
386 break;
387 case USB_REQ_DFU_GETSTATE:
388 handle_getstate(req);
389 break;
390 default:
391 f_dfu->dfu_state = DFU_STATE_dfuERROR;
392 value = RET_STALL;
393 break;
394 }
395
396 return value;
397}
398
399static int state_dfu_dnbusy(struct f_dfu *f_dfu,
400 const struct usb_ctrlrequest *ctrl,
401 struct usb_gadget *gadget,
402 struct usb_request *req)
403{
404 int value = 0;
405
406 switch (ctrl->bRequest) {
407 case USB_REQ_DFU_GETSTATUS:
408 handle_getstatus(req);
409 value = RET_STAT_LEN;
410 break;
411 default:
412 f_dfu->dfu_state = DFU_STATE_dfuERROR;
413 value = RET_STALL;
414 break;
415 }
416
417 return value;
418}
419
420static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
421 const struct usb_ctrlrequest *ctrl,
422 struct usb_gadget *gadget,
423 struct usb_request *req)
424{
425 u16 w_value = le16_to_cpu(ctrl->wValue);
426 u16 len = le16_to_cpu(ctrl->wLength);
427 int value = 0;
428
429 switch (ctrl->bRequest) {
430 case USB_REQ_DFU_DNLOAD:
431 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
432 f_dfu->blk_seq_num = w_value;
433 value = handle_dnload(gadget, len);
434 break;
435 case USB_REQ_DFU_ABORT:
436 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
437 value = RET_ZLP;
438 break;
439 case USB_REQ_DFU_GETSTATUS:
440 handle_getstatus(req);
441 value = RET_STAT_LEN;
442 break;
443 case USB_REQ_DFU_GETSTATE:
444 handle_getstate(req);
445 break;
446 default:
447 f_dfu->dfu_state = DFU_STATE_dfuERROR;
448 value = RET_STALL;
449 break;
450 }
451
452 return value;
453}
454
455static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
456 const struct usb_ctrlrequest *ctrl,
457 struct usb_gadget *gadget,
458 struct usb_request *req)
459{
460 int value = 0;
461
462 switch (ctrl->bRequest) {
463 case USB_REQ_DFU_GETSTATUS:
464 /* We're MainfestationTolerant */
Heiko Schochere1ba1512014-03-18 08:09:56 +0100465 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200466 handle_getstatus(req);
467 f_dfu->blk_seq_num = 0;
468 value = RET_STAT_LEN;
Heiko Schochere1ba1512014-03-18 08:09:56 +0100469 req->complete = dnload_request_flush;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200470 break;
471 case USB_REQ_DFU_GETSTATE:
472 handle_getstate(req);
473 break;
474 default:
475 f_dfu->dfu_state = DFU_STATE_dfuERROR;
476 value = RET_STALL;
477 break;
478 }
479
Heiko Schochere1ba1512014-03-18 08:09:56 +0100480 return value;
481}
482
483static int state_dfu_manifest(struct f_dfu *f_dfu,
484 const struct usb_ctrlrequest *ctrl,
485 struct usb_gadget *gadget,
486 struct usb_request *req)
487{
488 int value = 0;
489
490 switch (ctrl->bRequest) {
491 case USB_REQ_DFU_GETSTATUS:
492 /* We're MainfestationTolerant */
493 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
494 handle_getstatus(req);
495 f_dfu->blk_seq_num = 0;
496 value = RET_STAT_LEN;
497 puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
498 break;
499 case USB_REQ_DFU_GETSTATE:
500 handle_getstate(req);
501 break;
502 default:
503 f_dfu->dfu_state = DFU_STATE_dfuERROR;
504 value = RET_STALL;
505 break;
506 }
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200507 return value;
508}
509
510static int state_dfu_upload_idle(struct f_dfu *f_dfu,
511 const struct usb_ctrlrequest *ctrl,
512 struct usb_gadget *gadget,
513 struct usb_request *req)
514{
515 u16 w_value = le16_to_cpu(ctrl->wValue);
516 u16 len = le16_to_cpu(ctrl->wLength);
517 int value = 0;
518
519 switch (ctrl->bRequest) {
520 case USB_REQ_DFU_UPLOAD:
521 /* state transition if less data then requested */
522 f_dfu->blk_seq_num = w_value;
523 value = handle_upload(req, len);
524 if (value >= 0 && value < len)
525 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
526 break;
527 case USB_REQ_DFU_ABORT:
528 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
529 /* no zlp? */
530 value = RET_ZLP;
531 break;
532 case USB_REQ_DFU_GETSTATUS:
533 handle_getstatus(req);
534 value = RET_STAT_LEN;
535 break;
536 case USB_REQ_DFU_GETSTATE:
537 handle_getstate(req);
538 break;
539 default:
540 f_dfu->dfu_state = DFU_STATE_dfuERROR;
541 value = RET_STALL;
542 break;
543 }
544
545 return value;
546}
547
548static int state_dfu_error(struct f_dfu *f_dfu,
549 const struct usb_ctrlrequest *ctrl,
550 struct usb_gadget *gadget,
551 struct usb_request *req)
552{
553 int value = 0;
554
555 switch (ctrl->bRequest) {
556 case USB_REQ_DFU_GETSTATUS:
557 handle_getstatus(req);
558 value = RET_STAT_LEN;
559 break;
560 case USB_REQ_DFU_GETSTATE:
561 handle_getstate(req);
562 break;
563 case USB_REQ_DFU_CLRSTATUS:
564 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
565 f_dfu->dfu_status = DFU_STATUS_OK;
566 /* no zlp? */
567 value = RET_ZLP;
568 break;
569 default:
570 f_dfu->dfu_state = DFU_STATE_dfuERROR;
571 value = RET_STALL;
572 break;
573 }
574
575 return value;
576}
577
578static dfu_state_fn dfu_state[] = {
579 state_app_idle, /* DFU_STATE_appIDLE */
580 state_app_detach, /* DFU_STATE_appDETACH */
581 state_dfu_idle, /* DFU_STATE_dfuIDLE */
582 state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
583 state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
584 state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
585 state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
Heiko Schochere1ba1512014-03-18 08:09:56 +0100586 state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200587 NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
588 state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
589 state_dfu_error /* DFU_STATE_dfuERROR */
590};
591
592static int
593dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
594{
595 struct usb_gadget *gadget = f->config->cdev->gadget;
596 struct usb_request *req = f->config->cdev->req;
597 struct f_dfu *f_dfu = f->config->cdev->req->context;
598 u16 len = le16_to_cpu(ctrl->wLength);
599 u16 w_value = le16_to_cpu(ctrl->wValue);
600 int value = 0;
601 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
602
603 debug("w_value: 0x%x len: 0x%x\n", w_value, len);
604 debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
605 req_type, ctrl->bRequest, f_dfu->dfu_state);
606
607 if (req_type == USB_TYPE_STANDARD) {
608 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
609 (w_value >> 8) == DFU_DT_FUNC) {
610 value = min(len, (u16) sizeof(dfu_func));
611 memcpy(req->buf, &dfu_func, value);
612 }
613 } else /* DFU specific request */
614 value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
615
616 if (value >= 0) {
617 req->length = value;
618 req->zero = value < len;
619 value = usb_ep_queue(gadget->ep0, req, 0);
620 if (value < 0) {
621 debug("ep_queue --> %d\n", value);
622 req->status = 0;
623 }
624 }
625
626 return value;
627}
628
629/*-------------------------------------------------------------------------*/
630
631static int
632dfu_prepare_strings(struct f_dfu *f_dfu, int n)
633{
634 struct dfu_entity *de = NULL;
635 int i = 0;
636
637 f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
638 if (!f_dfu->strings)
Peng Fan46ed88c2016-05-03 10:25:22 +0800639 return -ENOMEM;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200640
641 for (i = 0; i < n; ++i) {
642 de = dfu_get_entity(i);
643 f_dfu->strings[i].s = de->name;
644 }
645
646 f_dfu->strings[i].id = 0;
647 f_dfu->strings[i].s = NULL;
648
649 return 0;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200650}
651
652static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
653{
654 struct usb_interface_descriptor *d;
655 int i = 0;
656
Patrick Delaunaybf2a4152016-12-08 18:10:49 +0100657 f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2);
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200658 if (!f_dfu->function)
659 goto enomem;
660
661 for (i = 0; i < n; ++i) {
662 d = calloc(sizeof(*d), 1);
663 if (!d)
664 goto enomem;
665
666 d->bLength = sizeof(*d);
667 d->bDescriptorType = USB_DT_INTERFACE;
668 d->bAlternateSetting = i;
669 d->bNumEndpoints = 0;
670 d->bInterfaceClass = USB_CLASS_APP_SPEC;
671 d->bInterfaceSubClass = 1;
672 d->bInterfaceProtocol = 2;
673
674 f_dfu->function[i] = (struct usb_descriptor_header *)d;
675 }
Patrick Delaunaybf2a4152016-12-08 18:10:49 +0100676
677 /* add DFU Functional Descriptor */
678 f_dfu->function[i] = calloc(sizeof(dfu_func), 1);
679 if (!f_dfu->function[i])
680 goto enomem;
681 memcpy(f_dfu->function[i], &dfu_func, sizeof(dfu_func));
682
683 i++;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200684 f_dfu->function[i] = NULL;
685
686 return 0;
687
688enomem:
689 while (i) {
690 free(f_dfu->function[--i]);
691 f_dfu->function[i] = NULL;
692 }
693 free(f_dfu->function);
694
695 return -ENOMEM;
696}
697
698static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
699{
700 struct usb_composite_dev *cdev = c->cdev;
701 struct f_dfu *f_dfu = func_to_dfu(f);
702 int alt_num = dfu_get_alt_number();
703 int rv, id, i;
704
705 id = usb_interface_id(c, f);
706 if (id < 0)
707 return id;
708 dfu_intf_runtime.bInterfaceNumber = id;
709
710 f_dfu->dfu_state = DFU_STATE_appIDLE;
711 f_dfu->dfu_status = DFU_STATUS_OK;
712
713 rv = dfu_prepare_function(f_dfu, alt_num);
714 if (rv)
715 goto error;
716
717 rv = dfu_prepare_strings(f_dfu, alt_num);
718 if (rv)
719 goto error;
720 for (i = 0; i < alt_num; i++) {
721 id = usb_string_id(cdev);
722 if (id < 0)
723 return id;
724 f_dfu->strings[i].id = id;
725 ((struct usb_interface_descriptor *)f_dfu->function[i])
726 ->iInterface = id;
727 }
728
Heiko Schocher5890e832013-06-25 13:59:29 +0200729 to_dfu_mode(f_dfu);
730
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200731 stringtab_dfu.strings = f_dfu->strings;
732
733 cdev->req->context = f_dfu;
734
735error:
736 return rv;
737}
738
739static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
740{
741 struct f_dfu *f_dfu = func_to_dfu(f);
742 int alt_num = dfu_get_alt_number();
743 int i;
744
745 if (f_dfu->strings) {
746 i = alt_num;
747 while (i)
748 f_dfu->strings[--i].s = NULL;
749
750 free(f_dfu->strings);
751 }
752
753 if (f_dfu->function) {
754 i = alt_num;
755 while (i) {
756 free(f_dfu->function[--i]);
757 f_dfu->function[i] = NULL;
758 }
759 free(f_dfu->function);
760 }
761
762 free(f_dfu);
763}
764
765static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
766{
767 struct f_dfu *f_dfu = func_to_dfu(f);
768
769 debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
770
771 f_dfu->altsetting = alt;
Stephen Warren7f450232014-06-11 12:48:08 -0600772 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
773 f_dfu->dfu_status = DFU_STATUS_OK;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200774
775 return 0;
776}
777
Stefan Roese5a6b8732015-01-09 14:54:56 +0100778static int __dfu_get_alt(struct usb_function *f, unsigned intf)
779{
780 struct f_dfu *f_dfu = func_to_dfu(f);
781
782 return f_dfu->altsetting;
783}
784
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200785/* TODO: is this really what we need here? */
786static void dfu_disable(struct usb_function *f)
787{
788 struct f_dfu *f_dfu = func_to_dfu(f);
789 if (f_dfu->config == 0)
790 return;
791
792 debug("%s: reset config\n", __func__);
793
794 f_dfu->config = 0;
795}
796
797static int dfu_bind_config(struct usb_configuration *c)
798{
799 struct f_dfu *f_dfu;
800 int status;
801
802 f_dfu = calloc(sizeof(*f_dfu), 1);
803 if (!f_dfu)
804 return -ENOMEM;
805 f_dfu->usb_function.name = "dfu";
806 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
Heiko Schocher3e3a1362014-09-10 08:55:00 +0200807 f_dfu->usb_function.descriptors = dfu_runtime_descs;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200808 f_dfu->usb_function.bind = dfu_bind;
809 f_dfu->usb_function.unbind = dfu_unbind;
810 f_dfu->usb_function.set_alt = dfu_set_alt;
Stefan Roese5a6b8732015-01-09 14:54:56 +0100811 f_dfu->usb_function.get_alt = __dfu_get_alt;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200812 f_dfu->usb_function.disable = dfu_disable;
Lukasz Majewski1abe47d2013-12-09 16:20:15 +0100813 f_dfu->usb_function.strings = dfu_generic_strings;
814 f_dfu->usb_function.setup = dfu_handle;
Lukasz Majewski2bb115f2013-12-09 16:20:14 +0100815 f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
Lukasz Majewskib7a15bf2012-08-06 14:41:06 +0200816
817 status = usb_add_function(c, &f_dfu->usb_function);
818 if (status)
819 free(f_dfu);
820
821 return status;
822}
823
824int dfu_add(struct usb_configuration *c)
825{
826 int id;
827
828 id = usb_string_id(c->cdev);
829 if (id < 0)
830 return id;
831 strings_dfu_generic[0].id = id;
832 dfu_intf_runtime.iInterface = id;
833
834 debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
835 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
836
837 return dfu_bind_config(c);
838}
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +0200839
840DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);