2 * f_dfu.c -- Device Firmware Update USB function
4 * Copyright (C) 2012 Samsung Electronics
5 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
6 * Lukasz Majewski <l.majewski@samsung.com>
8 * Based on OpenMoko u-boot: drivers/usb/usbdfu.c
9 * (C) 2007 by OpenMoko, Inc.
10 * Author: Harald Welte <laforge@openmoko.org>
12 * based on existing SAM7DFU code from OpenPCD:
13 * (C) Copyright 2006 by Harald Welte <hwelte at hmw-consulting.de>
15 * SPDX-License-Identifier: GPL-2.0+
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/composite.h>
31 struct usb_function usb_function;
33 struct usb_descriptor_header **function;
34 struct usb_string *strings;
36 /* when configured, we have one config */
39 enum dfu_state dfu_state;
40 unsigned int dfu_status;
42 /* Send/received block number is handy for data integrity check */
44 unsigned int poll_timeout;
47 struct dfu_entity *dfu_defer_flush;
49 typedef int (*dfu_state_fn) (struct f_dfu *,
50 const struct usb_ctrlrequest *,
52 struct usb_request *);
54 static inline struct f_dfu *func_to_dfu(struct usb_function *f)
56 return container_of(f, struct f_dfu, usb_function);
59 static 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 |
67 .wTransferSize = DFU_USB_BUFSIZ,
68 .bcdDFUVersion = __constant_cpu_to_le16(0x0110),
71 static struct usb_interface_descriptor dfu_intf_runtime = {
72 .bLength = sizeof dfu_intf_runtime,
73 .bDescriptorType = USB_DT_INTERFACE,
75 .bInterfaceClass = USB_CLASS_APP_SPEC,
76 .bInterfaceSubClass = 1,
77 .bInterfaceProtocol = 1,
78 /* .iInterface = DYNAMIC */
81 static struct usb_descriptor_header *dfu_runtime_descs[] = {
82 (struct usb_descriptor_header *) &dfu_intf_runtime,
86 static const char dfu_name[] = "Device Firmware Upgrade";
89 * static strings, in UTF-8
91 * dfu_generic configuration
93 static struct usb_string strings_dfu_generic[] = {
98 static struct usb_gadget_strings stringtab_dfu_generic = {
99 .language = 0x0409, /* en-us */
100 .strings = strings_dfu_generic,
103 static struct usb_gadget_strings *dfu_generic_strings[] = {
104 &stringtab_dfu_generic,
109 * usb_function specific
111 static struct usb_gadget_strings stringtab_dfu = {
112 .language = 0x0409, /* en-us */
116 * assigned during initialization,
117 * depends on number of flash entities
122 static struct usb_gadget_strings *dfu_strings[] = {
127 static void dfu_set_poll_timeout(struct dfu_status *dstat, unsigned int ms)
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
134 * This permits the device to vary the delay depending on its need to
135 * erase or program the memory
139 unsigned char *p = (unsigned char *)&ms;
141 if (!ms || (ms & ~DFU_POLL_TIMEOUT_MASK)) {
142 dstat->bwPollTimeout[0] = 0;
143 dstat->bwPollTimeout[1] = 0;
144 dstat->bwPollTimeout[2] = 0;
149 dstat->bwPollTimeout[0] = *p++;
150 dstat->bwPollTimeout[1] = *p++;
151 dstat->bwPollTimeout[2] = *p;
154 /*-------------------------------------------------------------------------*/
156 static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
158 struct f_dfu *f_dfu = req->context;
161 ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
162 req->length, f_dfu->blk_seq_num);
164 f_dfu->dfu_status = DFU_STATUS_errUNKNOWN;
165 f_dfu->dfu_state = DFU_STATE_dfuERROR;
169 static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req)
171 struct f_dfu *f_dfu = req->context;
172 dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting));
175 static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu)
177 return dfu->poll_timeout ? dfu->poll_timeout(dfu) :
178 DFU_MANIFEST_POLL_TIMEOUT;
181 static void handle_getstatus(struct usb_request *req)
183 struct dfu_status *dstat = (struct dfu_status *)req->buf;
184 struct f_dfu *f_dfu = req->context;
185 struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting);
187 dfu_set_poll_timeout(dstat, 0);
189 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;
194 case DFU_STATE_dfuMANIFEST_SYNC:
195 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
197 case DFU_STATE_dfuMANIFEST:
198 dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu));
204 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);
209 /* send status response */
210 dstat->bStatus = f_dfu->dfu_status;
211 dstat->bState = f_dfu->dfu_state;
215 static void handle_getstate(struct usb_request *req)
217 struct f_dfu *f_dfu = req->context;
219 ((u8 *)req->buf)[0] = f_dfu->dfu_state;
220 req->actual = sizeof(u8);
223 static inline void to_dfu_mode(struct f_dfu *f_dfu)
225 f_dfu->usb_function.strings = dfu_strings;
226 f_dfu->usb_function.hs_descriptors = f_dfu->function;
227 f_dfu->usb_function.descriptors = f_dfu->function;
228 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
231 static inline void to_runtime_mode(struct f_dfu *f_dfu)
233 f_dfu->usb_function.strings = NULL;
234 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
235 f_dfu->usb_function.descriptors = dfu_runtime_descs;
238 static int handle_upload(struct usb_request *req, u16 len)
240 struct f_dfu *f_dfu = req->context;
242 return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
243 req->length, f_dfu->blk_seq_num);
246 static int handle_dnload(struct usb_gadget *gadget, u16 len)
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;
253 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
255 req->complete = dnload_request_complete;
260 /*-------------------------------------------------------------------------*/
261 /* DFU state machine */
262 static 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)
269 switch (ctrl->bRequest) {
270 case USB_REQ_DFU_GETSTATUS:
271 handle_getstatus(req);
272 value = RET_STAT_LEN;
274 case USB_REQ_DFU_GETSTATE:
275 handle_getstate(req);
277 case USB_REQ_DFU_DETACH:
278 f_dfu->dfu_state = DFU_STATE_appDETACH;
290 static 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)
297 switch (ctrl->bRequest) {
298 case USB_REQ_DFU_GETSTATUS:
299 handle_getstatus(req);
300 value = RET_STAT_LEN;
302 case USB_REQ_DFU_GETSTATE:
303 handle_getstate(req);
306 f_dfu->dfu_state = DFU_STATE_appIDLE;
314 static 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)
319 u16 w_value = le16_to_cpu(ctrl->wValue);
320 u16 len = le16_to_cpu(ctrl->wLength);
323 switch (ctrl->bRequest) {
324 case USB_REQ_DFU_DNLOAD:
326 f_dfu->dfu_state = DFU_STATE_dfuERROR;
330 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
331 f_dfu->blk_seq_num = w_value;
332 value = handle_dnload(gadget, len);
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);
339 case USB_REQ_DFU_ABORT:
343 case USB_REQ_DFU_GETSTATUS:
344 handle_getstatus(req);
345 value = RET_STAT_LEN;
347 case USB_REQ_DFU_GETSTATE:
348 handle_getstate(req);
350 case USB_REQ_DFU_DETACH:
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
360 DFU_STATE_dfuMANIFEST_WAIT_RST;
361 to_runtime_mode(f_dfu);
362 f_dfu->dfu_state = DFU_STATE_appIDLE;
364 g_dnl_trigger_detach();
367 f_dfu->dfu_state = DFU_STATE_dfuERROR;
375 static 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)
382 switch (ctrl->bRequest) {
383 case USB_REQ_DFU_GETSTATUS:
384 handle_getstatus(req);
385 value = RET_STAT_LEN;
387 case USB_REQ_DFU_GETSTATE:
388 handle_getstate(req);
391 f_dfu->dfu_state = DFU_STATE_dfuERROR;
399 static 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)
406 switch (ctrl->bRequest) {
407 case USB_REQ_DFU_GETSTATUS:
408 handle_getstatus(req);
409 value = RET_STAT_LEN;
412 f_dfu->dfu_state = DFU_STATE_dfuERROR;
420 static 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)
425 u16 w_value = le16_to_cpu(ctrl->wValue);
426 u16 len = le16_to_cpu(ctrl->wLength);
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);
435 case USB_REQ_DFU_ABORT:
436 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
439 case USB_REQ_DFU_GETSTATUS:
440 handle_getstatus(req);
441 value = RET_STAT_LEN;
443 case USB_REQ_DFU_GETSTATE:
444 handle_getstate(req);
447 f_dfu->dfu_state = DFU_STATE_dfuERROR;
455 static 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)
462 switch (ctrl->bRequest) {
463 case USB_REQ_DFU_GETSTATUS:
464 /* We're MainfestationTolerant */
465 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
466 handle_getstatus(req);
467 f_dfu->blk_seq_num = 0;
468 value = RET_STAT_LEN;
469 req->complete = dnload_request_flush;
471 case USB_REQ_DFU_GETSTATE:
472 handle_getstate(req);
475 f_dfu->dfu_state = DFU_STATE_dfuERROR;
483 static 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)
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");
499 case USB_REQ_DFU_GETSTATE:
500 handle_getstate(req);
503 f_dfu->dfu_state = DFU_STATE_dfuERROR;
510 static 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)
515 u16 w_value = le16_to_cpu(ctrl->wValue);
516 u16 len = le16_to_cpu(ctrl->wLength);
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;
527 case USB_REQ_DFU_ABORT:
528 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
532 case USB_REQ_DFU_GETSTATUS:
533 handle_getstatus(req);
534 value = RET_STAT_LEN;
536 case USB_REQ_DFU_GETSTATE:
537 handle_getstate(req);
540 f_dfu->dfu_state = DFU_STATE_dfuERROR;
548 static 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)
555 switch (ctrl->bRequest) {
556 case USB_REQ_DFU_GETSTATUS:
557 handle_getstatus(req);
558 value = RET_STAT_LEN;
560 case USB_REQ_DFU_GETSTATE:
561 handle_getstate(req);
563 case USB_REQ_DFU_CLRSTATUS:
564 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
565 f_dfu->dfu_status = DFU_STATUS_OK;
570 f_dfu->dfu_state = DFU_STATE_dfuERROR;
578 static 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 */
586 state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
587 NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
588 state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
589 state_dfu_error /* DFU_STATE_dfuERROR */
593 dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
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);
601 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
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);
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);
613 } else /* DFU specific request */
614 value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
618 req->zero = value < len;
619 value = usb_ep_queue(gadget->ep0, req, 0);
621 debug("ep_queue --> %d\n", value);
629 /*-------------------------------------------------------------------------*/
632 dfu_prepare_strings(struct f_dfu *f_dfu, int n)
634 struct dfu_entity *de = NULL;
637 f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
641 for (i = 0; i < n; ++i) {
642 de = dfu_get_entity(i);
643 f_dfu->strings[i].s = de->name;
646 f_dfu->strings[i].id = 0;
647 f_dfu->strings[i].s = NULL;
652 static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
654 struct usb_interface_descriptor *d;
657 f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 1);
658 if (!f_dfu->function)
661 for (i = 0; i < n; ++i) {
662 d = calloc(sizeof(*d), 1);
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;
674 f_dfu->function[i] = (struct usb_descriptor_header *)d;
676 f_dfu->function[i] = NULL;
682 free(f_dfu->function[--i]);
683 f_dfu->function[i] = NULL;
685 free(f_dfu->function);
690 static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
692 struct usb_composite_dev *cdev = c->cdev;
693 struct f_dfu *f_dfu = func_to_dfu(f);
694 int alt_num = dfu_get_alt_number();
697 id = usb_interface_id(c, f);
700 dfu_intf_runtime.bInterfaceNumber = id;
702 f_dfu->dfu_state = DFU_STATE_appIDLE;
703 f_dfu->dfu_status = DFU_STATUS_OK;
705 rv = dfu_prepare_function(f_dfu, alt_num);
709 rv = dfu_prepare_strings(f_dfu, alt_num);
712 for (i = 0; i < alt_num; i++) {
713 id = usb_string_id(cdev);
716 f_dfu->strings[i].id = id;
717 ((struct usb_interface_descriptor *)f_dfu->function[i])
723 stringtab_dfu.strings = f_dfu->strings;
725 cdev->req->context = f_dfu;
731 static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
733 struct f_dfu *f_dfu = func_to_dfu(f);
734 int alt_num = dfu_get_alt_number();
737 if (f_dfu->strings) {
740 f_dfu->strings[--i].s = NULL;
742 free(f_dfu->strings);
745 if (f_dfu->function) {
748 free(f_dfu->function[--i]);
749 f_dfu->function[i] = NULL;
751 free(f_dfu->function);
757 static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
759 struct f_dfu *f_dfu = func_to_dfu(f);
761 debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
763 f_dfu->altsetting = alt;
764 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
765 f_dfu->dfu_status = DFU_STATUS_OK;
770 static int __dfu_get_alt(struct usb_function *f, unsigned intf)
772 struct f_dfu *f_dfu = func_to_dfu(f);
774 return f_dfu->altsetting;
777 /* TODO: is this really what we need here? */
778 static void dfu_disable(struct usb_function *f)
780 struct f_dfu *f_dfu = func_to_dfu(f);
781 if (f_dfu->config == 0)
784 debug("%s: reset config\n", __func__);
789 static int dfu_bind_config(struct usb_configuration *c)
794 f_dfu = calloc(sizeof(*f_dfu), 1);
797 f_dfu->usb_function.name = "dfu";
798 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
799 f_dfu->usb_function.descriptors = dfu_runtime_descs;
800 f_dfu->usb_function.bind = dfu_bind;
801 f_dfu->usb_function.unbind = dfu_unbind;
802 f_dfu->usb_function.set_alt = dfu_set_alt;
803 f_dfu->usb_function.get_alt = __dfu_get_alt;
804 f_dfu->usb_function.disable = dfu_disable;
805 f_dfu->usb_function.strings = dfu_generic_strings;
806 f_dfu->usb_function.setup = dfu_handle;
807 f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
809 status = usb_add_function(c, &f_dfu->usb_function);
816 int dfu_add(struct usb_configuration *c)
820 id = usb_string_id(c->cdev);
823 strings_dfu_generic[0].id = id;
824 dfu_intf_runtime.iInterface = id;
826 debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
827 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
829 return dfu_bind_config(c);
832 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);