1 // SPDX-License-Identifier: GPL-2.0+
3 * f_dfu.c -- Device Firmware Update USB function
5 * Copyright (C) 2012 Samsung Electronics
6 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7 * Lukasz Majewski <l.majewski@samsung.com>
9 * Based on OpenMoko u-boot: drivers/usb/usbdfu.c
10 * (C) 2007 by OpenMoko, Inc.
11 * Author: Harald Welte <laforge@openmoko.org>
13 * based on existing SAM7DFU code from OpenPCD:
14 * (C) Copyright 2006 by Harald Welte <hwelte at hmw-consulting.de>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/composite.h>
30 struct usb_function usb_function;
32 struct usb_descriptor_header **function;
33 struct usb_string *strings;
35 /* when configured, we have one config */
38 enum dfu_state dfu_state;
39 unsigned int dfu_status;
41 /* Send/received block number is handy for data integrity check */
43 unsigned int poll_timeout;
46 struct dfu_entity *dfu_defer_flush;
48 typedef int (*dfu_state_fn) (struct f_dfu *,
49 const struct usb_ctrlrequest *,
51 struct usb_request *);
53 static inline struct f_dfu *func_to_dfu(struct usb_function *f)
55 return container_of(f, struct f_dfu, usb_function);
58 static 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 |
66 .wTransferSize = DFU_USB_BUFSIZ,
67 .bcdDFUVersion = __constant_cpu_to_le16(0x0110),
70 static struct usb_interface_descriptor dfu_intf_runtime = {
71 .bLength = sizeof dfu_intf_runtime,
72 .bDescriptorType = USB_DT_INTERFACE,
74 .bInterfaceClass = USB_CLASS_APP_SPEC,
75 .bInterfaceSubClass = 1,
76 .bInterfaceProtocol = 1,
77 /* .iInterface = DYNAMIC */
80 static struct usb_descriptor_header *dfu_runtime_descs[] = {
81 (struct usb_descriptor_header *) &dfu_intf_runtime,
85 static const char dfu_name[] = "Device Firmware Upgrade";
88 * static strings, in UTF-8
90 * dfu_generic configuration
92 static struct usb_string strings_dfu_generic[] = {
97 static struct usb_gadget_strings stringtab_dfu_generic = {
98 .language = 0x0409, /* en-us */
99 .strings = strings_dfu_generic,
102 static struct usb_gadget_strings *dfu_generic_strings[] = {
103 &stringtab_dfu_generic,
108 * usb_function specific
110 static struct usb_gadget_strings stringtab_dfu = {
111 .language = 0x0409, /* en-us */
115 * assigned during initialization,
116 * depends on number of flash entities
121 static struct usb_gadget_strings *dfu_strings[] = {
126 static void dfu_set_poll_timeout(struct dfu_status *dstat, unsigned int ms)
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
133 * This permits the device to vary the delay depending on its need to
134 * erase or program the memory
138 unsigned char *p = (unsigned char *)&ms;
140 if (!ms || (ms & ~DFU_POLL_TIMEOUT_MASK)) {
141 dstat->bwPollTimeout[0] = 0;
142 dstat->bwPollTimeout[1] = 0;
143 dstat->bwPollTimeout[2] = 0;
148 dstat->bwPollTimeout[0] = *p++;
149 dstat->bwPollTimeout[1] = *p++;
150 dstat->bwPollTimeout[2] = *p;
153 /*-------------------------------------------------------------------------*/
155 static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
157 struct f_dfu *f_dfu = req->context;
160 ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
161 req->actual, f_dfu->blk_seq_num);
163 f_dfu->dfu_status = DFU_STATUS_errUNKNOWN;
164 f_dfu->dfu_state = DFU_STATE_dfuERROR;
168 static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req)
170 struct f_dfu *f_dfu = req->context;
171 dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting));
174 static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu)
176 return dfu->poll_timeout ? dfu->poll_timeout(dfu) :
177 DFU_MANIFEST_POLL_TIMEOUT;
180 static int handle_getstatus(struct usb_request *req)
182 struct dfu_status *dstat = (struct dfu_status *)req->buf;
183 struct f_dfu *f_dfu = req->context;
184 struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting);
186 dfu_set_poll_timeout(dstat, 0);
188 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;
193 case DFU_STATE_dfuMANIFEST_SYNC:
194 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
196 case DFU_STATE_dfuMANIFEST:
197 dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu));
203 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);
208 /* send status response */
209 dstat->bStatus = f_dfu->dfu_status;
210 dstat->bState = f_dfu->dfu_state;
213 return sizeof(struct dfu_status);
216 static int handle_getstate(struct usb_request *req)
218 struct f_dfu *f_dfu = req->context;
220 ((u8 *)req->buf)[0] = f_dfu->dfu_state;
224 static inline void to_dfu_mode(struct f_dfu *f_dfu)
226 f_dfu->usb_function.strings = dfu_strings;
227 f_dfu->usb_function.hs_descriptors = f_dfu->function;
228 f_dfu->usb_function.descriptors = f_dfu->function;
229 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
232 static inline void to_runtime_mode(struct f_dfu *f_dfu)
234 f_dfu->usb_function.strings = NULL;
235 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
236 f_dfu->usb_function.descriptors = dfu_runtime_descs;
239 static int handle_upload(struct usb_request *req, u16 len)
241 struct f_dfu *f_dfu = req->context;
243 return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
244 req->length, f_dfu->blk_seq_num);
247 static int handle_dnload(struct usb_gadget *gadget, u16 len)
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;
254 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
256 req->complete = dnload_request_complete;
261 /*-------------------------------------------------------------------------*/
262 /* DFU state machine */
263 static 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)
270 switch (ctrl->bRequest) {
271 case USB_REQ_DFU_GETSTATUS:
272 value = handle_getstatus(req);
274 case USB_REQ_DFU_GETSTATE:
275 value = 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 value = handle_getstatus(req);
301 case USB_REQ_DFU_GETSTATE:
302 value = handle_getstate(req);
305 f_dfu->dfu_state = DFU_STATE_appIDLE;
313 static 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)
318 u16 w_value = le16_to_cpu(ctrl->wValue);
319 u16 len = le16_to_cpu(ctrl->wLength);
322 switch (ctrl->bRequest) {
323 case USB_REQ_DFU_DNLOAD:
325 f_dfu->dfu_state = DFU_STATE_dfuERROR;
329 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
330 f_dfu->blk_seq_num = w_value;
331 value = handle_dnload(gadget, len);
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);
338 case USB_REQ_DFU_ABORT:
342 case USB_REQ_DFU_GETSTATUS:
343 value = handle_getstatus(req);
345 case USB_REQ_DFU_GETSTATE:
346 value = handle_getstate(req);
348 case USB_REQ_DFU_DETACH:
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
358 DFU_STATE_dfuMANIFEST_WAIT_RST;
359 to_runtime_mode(f_dfu);
360 f_dfu->dfu_state = DFU_STATE_appIDLE;
362 g_dnl_trigger_detach();
365 f_dfu->dfu_state = DFU_STATE_dfuERROR;
373 static 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)
380 switch (ctrl->bRequest) {
381 case USB_REQ_DFU_GETSTATUS:
382 value = handle_getstatus(req);
384 case USB_REQ_DFU_GETSTATE:
385 value = handle_getstate(req);
388 f_dfu->dfu_state = DFU_STATE_dfuERROR;
396 static 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)
403 switch (ctrl->bRequest) {
404 case USB_REQ_DFU_GETSTATUS:
405 value = handle_getstatus(req);
408 f_dfu->dfu_state = DFU_STATE_dfuERROR;
416 static 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)
421 u16 w_value = le16_to_cpu(ctrl->wValue);
422 u16 len = le16_to_cpu(ctrl->wLength);
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);
431 case USB_REQ_DFU_ABORT:
432 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
435 case USB_REQ_DFU_GETSTATUS:
436 value = handle_getstatus(req);
438 case USB_REQ_DFU_GETSTATE:
439 value = handle_getstate(req);
442 f_dfu->dfu_state = DFU_STATE_dfuERROR;
450 static 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)
457 switch (ctrl->bRequest) {
458 case USB_REQ_DFU_GETSTATUS:
459 /* We're MainfestationTolerant */
460 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST;
461 value = handle_getstatus(req);
462 f_dfu->blk_seq_num = 0;
463 req->complete = dnload_request_flush;
465 case USB_REQ_DFU_GETSTATE:
466 value = handle_getstate(req);
469 f_dfu->dfu_state = DFU_STATE_dfuERROR;
477 static 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)
484 switch (ctrl->bRequest) {
485 case USB_REQ_DFU_GETSTATUS:
486 /* We're MainfestationTolerant */
487 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
488 value = handle_getstatus(req);
489 f_dfu->blk_seq_num = 0;
490 puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
492 case USB_REQ_DFU_GETSTATE:
493 value = handle_getstate(req);
496 f_dfu->dfu_state = DFU_STATE_dfuERROR;
503 static 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)
508 u16 w_value = le16_to_cpu(ctrl->wValue);
509 u16 len = le16_to_cpu(ctrl->wLength);
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;
520 case USB_REQ_DFU_ABORT:
521 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
525 case USB_REQ_DFU_GETSTATUS:
526 value = handle_getstatus(req);
528 case USB_REQ_DFU_GETSTATE:
529 value = handle_getstate(req);
532 f_dfu->dfu_state = DFU_STATE_dfuERROR;
540 static 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)
547 switch (ctrl->bRequest) {
548 case USB_REQ_DFU_GETSTATUS:
549 value = handle_getstatus(req);
551 case USB_REQ_DFU_GETSTATE:
552 value = handle_getstate(req);
554 case USB_REQ_DFU_CLRSTATUS:
555 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
556 f_dfu->dfu_status = DFU_STATUS_OK;
561 f_dfu->dfu_state = DFU_STATE_dfuERROR;
569 static 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 */
577 state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */
578 NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
579 state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
580 state_dfu_error /* DFU_STATE_dfuERROR */
584 dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
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);
592 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
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);
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);
604 } else /* DFU specific request */
605 value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
609 req->zero = value < len;
610 value = usb_ep_queue(gadget->ep0, req, 0);
612 debug("ep_queue --> %d\n", value);
620 /*-------------------------------------------------------------------------*/
623 dfu_prepare_strings(struct f_dfu *f_dfu, int n)
625 struct dfu_entity *de = NULL;
628 f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
632 for (i = 0; i < n; ++i) {
633 de = dfu_get_entity(i);
634 f_dfu->strings[i].s = de->name;
637 f_dfu->strings[i].id = 0;
638 f_dfu->strings[i].s = NULL;
643 static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
645 struct usb_interface_descriptor *d;
648 f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2);
649 if (!f_dfu->function)
652 for (i = 0; i < n; ++i) {
653 d = calloc(sizeof(*d), 1);
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;
665 f_dfu->function[i] = (struct usb_descriptor_header *)d;
668 /* add DFU Functional Descriptor */
669 f_dfu->function[i] = calloc(sizeof(dfu_func), 1);
670 if (!f_dfu->function[i])
672 memcpy(f_dfu->function[i], &dfu_func, sizeof(dfu_func));
675 f_dfu->function[i] = NULL;
681 free(f_dfu->function[--i]);
682 f_dfu->function[i] = NULL;
684 free(f_dfu->function);
689 static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
691 struct usb_composite_dev *cdev = c->cdev;
692 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;
727 s = env_get("serial#");
729 g_dnl_set_serialnumber((char *)s);
735 static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
737 struct f_dfu *f_dfu = func_to_dfu(f);
738 int alt_num = dfu_get_alt_number();
741 if (f_dfu->strings) {
744 f_dfu->strings[--i].s = NULL;
746 free(f_dfu->strings);
749 if (f_dfu->function) {
752 free(f_dfu->function[--i]);
753 f_dfu->function[i] = NULL;
755 free(f_dfu->function);
761 static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
763 struct f_dfu *f_dfu = func_to_dfu(f);
765 debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
767 f_dfu->altsetting = alt;
768 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
769 f_dfu->dfu_status = DFU_STATUS_OK;
774 static int __dfu_get_alt(struct usb_function *f, unsigned intf)
776 struct f_dfu *f_dfu = func_to_dfu(f);
778 return f_dfu->altsetting;
781 /* TODO: is this really what we need here? */
782 static void dfu_disable(struct usb_function *f)
784 struct f_dfu *f_dfu = func_to_dfu(f);
785 if (f_dfu->config == 0)
788 debug("%s: reset config\n", __func__);
793 static int dfu_bind_config(struct usb_configuration *c)
798 f_dfu = calloc(sizeof(*f_dfu), 1);
801 f_dfu->usb_function.name = "dfu";
802 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
803 f_dfu->usb_function.descriptors = dfu_runtime_descs;
804 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;
807 f_dfu->usb_function.get_alt = __dfu_get_alt;
808 f_dfu->usb_function.disable = dfu_disable;
809 f_dfu->usb_function.strings = dfu_generic_strings;
810 f_dfu->usb_function.setup = dfu_handle;
811 f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT;
813 status = usb_add_function(c, &f_dfu->usb_function);
820 int dfu_add(struct usb_configuration *c)
824 id = usb_string_id(c->cdev);
827 strings_dfu_generic[0].id = id;
828 dfu_intf_runtime.iInterface = id;
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);
833 return dfu_bind_config(c);
836 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add);