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>
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 */
45 typedef int (*dfu_state_fn) (struct f_dfu *,
46 const struct usb_ctrlrequest *,
48 struct usb_request *);
50 static inline struct f_dfu *func_to_dfu(struct usb_function *f)
52 return container_of(f, struct f_dfu, usb_function);
55 static const struct dfu_function_descriptor dfu_func = {
56 .bLength = sizeof dfu_func,
57 .bDescriptorType = DFU_DT_FUNC,
58 .bmAttributes = DFU_BIT_WILL_DETACH |
59 DFU_BIT_MANIFESTATION_TOLERANT |
63 .wTransferSize = DFU_USB_BUFSIZ,
64 .bcdDFUVersion = __constant_cpu_to_le16(0x0110),
67 static struct usb_interface_descriptor dfu_intf_runtime = {
68 .bLength = sizeof dfu_intf_runtime,
69 .bDescriptorType = USB_DT_INTERFACE,
71 .bInterfaceClass = USB_CLASS_APP_SPEC,
72 .bInterfaceSubClass = 1,
73 .bInterfaceProtocol = 1,
74 /* .iInterface = DYNAMIC */
77 static struct usb_descriptor_header *dfu_runtime_descs[] = {
78 (struct usb_descriptor_header *) &dfu_intf_runtime,
82 static const struct usb_qualifier_descriptor dev_qualifier = {
83 .bLength = sizeof dev_qualifier,
84 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
85 .bcdUSB = __constant_cpu_to_le16(0x0200),
86 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
87 .bNumConfigurations = 1,
90 static const char dfu_name[] = "Device Firmware Upgrade";
93 * static strings, in UTF-8
95 * dfu_generic configuration
97 static struct usb_string strings_dfu_generic[] = {
102 static struct usb_gadget_strings stringtab_dfu_generic = {
103 .language = 0x0409, /* en-us */
104 .strings = strings_dfu_generic,
107 static struct usb_gadget_strings *dfu_generic_strings[] = {
108 &stringtab_dfu_generic,
113 * usb_function specific
115 static struct usb_gadget_strings stringtab_dfu = {
116 .language = 0x0409, /* en-us */
120 * assigned during initialization,
121 * depends on number of flash entities
126 static struct usb_gadget_strings *dfu_strings[] = {
131 /*-------------------------------------------------------------------------*/
133 static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req)
135 struct f_dfu *f_dfu = req->context;
137 dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
138 req->length, f_dfu->blk_seq_num);
140 if (req->length == 0)
141 puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n");
144 static void handle_getstatus(struct usb_request *req)
146 struct dfu_status *dstat = (struct dfu_status *)req->buf;
147 struct f_dfu *f_dfu = req->context;
149 switch (f_dfu->dfu_state) {
150 case DFU_STATE_dfuDNLOAD_SYNC:
151 case DFU_STATE_dfuDNBUSY:
152 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
154 case DFU_STATE_dfuMANIFEST_SYNC:
160 /* send status response */
161 dstat->bStatus = f_dfu->dfu_status;
162 dstat->bwPollTimeout[0] = 0;
163 dstat->bwPollTimeout[1] = 0;
164 dstat->bwPollTimeout[2] = 0;
165 dstat->bState = f_dfu->dfu_state;
169 static void handle_getstate(struct usb_request *req)
171 struct f_dfu *f_dfu = req->context;
173 ((u8 *)req->buf)[0] = f_dfu->dfu_state;
174 req->actual = sizeof(u8);
177 static inline void to_dfu_mode(struct f_dfu *f_dfu)
179 f_dfu->usb_function.strings = dfu_strings;
180 f_dfu->usb_function.hs_descriptors = f_dfu->function;
181 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
184 static inline void to_runtime_mode(struct f_dfu *f_dfu)
186 f_dfu->usb_function.strings = NULL;
187 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
190 static int handle_upload(struct usb_request *req, u16 len)
192 struct f_dfu *f_dfu = req->context;
194 return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf,
195 req->length, f_dfu->blk_seq_num);
198 static int handle_dnload(struct usb_gadget *gadget, u16 len)
200 struct usb_composite_dev *cdev = get_gadget_data(gadget);
201 struct usb_request *req = cdev->req;
202 struct f_dfu *f_dfu = req->context;
205 f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
207 req->complete = dnload_request_complete;
212 /*-------------------------------------------------------------------------*/
213 /* DFU state machine */
214 static int state_app_idle(struct f_dfu *f_dfu,
215 const struct usb_ctrlrequest *ctrl,
216 struct usb_gadget *gadget,
217 struct usb_request *req)
221 switch (ctrl->bRequest) {
222 case USB_REQ_DFU_GETSTATUS:
223 handle_getstatus(req);
224 value = RET_STAT_LEN;
226 case USB_REQ_DFU_GETSTATE:
227 handle_getstate(req);
229 case USB_REQ_DFU_DETACH:
230 f_dfu->dfu_state = DFU_STATE_appDETACH;
242 static int state_app_detach(struct f_dfu *f_dfu,
243 const struct usb_ctrlrequest *ctrl,
244 struct usb_gadget *gadget,
245 struct usb_request *req)
249 switch (ctrl->bRequest) {
250 case USB_REQ_DFU_GETSTATUS:
251 handle_getstatus(req);
252 value = RET_STAT_LEN;
254 case USB_REQ_DFU_GETSTATE:
255 handle_getstate(req);
258 f_dfu->dfu_state = DFU_STATE_appIDLE;
266 static int state_dfu_idle(struct f_dfu *f_dfu,
267 const struct usb_ctrlrequest *ctrl,
268 struct usb_gadget *gadget,
269 struct usb_request *req)
271 u16 w_value = le16_to_cpu(ctrl->wValue);
272 u16 len = le16_to_cpu(ctrl->wLength);
275 switch (ctrl->bRequest) {
276 case USB_REQ_DFU_DNLOAD:
278 f_dfu->dfu_state = DFU_STATE_dfuERROR;
282 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
283 f_dfu->blk_seq_num = w_value;
284 value = handle_dnload(gadget, len);
286 case USB_REQ_DFU_UPLOAD:
287 f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
288 f_dfu->blk_seq_num = 0;
289 value = handle_upload(req, len);
291 case USB_REQ_DFU_ABORT:
295 case USB_REQ_DFU_GETSTATUS:
296 handle_getstatus(req);
297 value = RET_STAT_LEN;
299 case USB_REQ_DFU_GETSTATE:
300 handle_getstate(req);
302 case USB_REQ_DFU_DETACH:
304 * Proprietary extension: 'detach' from idle mode and
305 * get back to runtime mode in case of USB Reset. As
306 * much as I dislike this, we just can't use every USB
307 * bus reset to switch back to runtime mode, since at
308 * least the Linux USB stack likes to send a number of
312 DFU_STATE_dfuMANIFEST_WAIT_RST;
313 to_runtime_mode(f_dfu);
314 f_dfu->dfu_state = DFU_STATE_appIDLE;
319 f_dfu->dfu_state = DFU_STATE_dfuERROR;
327 static int state_dfu_dnload_sync(struct f_dfu *f_dfu,
328 const struct usb_ctrlrequest *ctrl,
329 struct usb_gadget *gadget,
330 struct usb_request *req)
334 switch (ctrl->bRequest) {
335 case USB_REQ_DFU_GETSTATUS:
336 handle_getstatus(req);
337 value = RET_STAT_LEN;
339 case USB_REQ_DFU_GETSTATE:
340 handle_getstate(req);
343 f_dfu->dfu_state = DFU_STATE_dfuERROR;
351 static int state_dfu_dnbusy(struct f_dfu *f_dfu,
352 const struct usb_ctrlrequest *ctrl,
353 struct usb_gadget *gadget,
354 struct usb_request *req)
358 switch (ctrl->bRequest) {
359 case USB_REQ_DFU_GETSTATUS:
360 handle_getstatus(req);
361 value = RET_STAT_LEN;
364 f_dfu->dfu_state = DFU_STATE_dfuERROR;
372 static int state_dfu_dnload_idle(struct f_dfu *f_dfu,
373 const struct usb_ctrlrequest *ctrl,
374 struct usb_gadget *gadget,
375 struct usb_request *req)
377 u16 w_value = le16_to_cpu(ctrl->wValue);
378 u16 len = le16_to_cpu(ctrl->wLength);
381 switch (ctrl->bRequest) {
382 case USB_REQ_DFU_DNLOAD:
383 f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
384 f_dfu->blk_seq_num = w_value;
385 value = handle_dnload(gadget, len);
387 case USB_REQ_DFU_ABORT:
388 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
391 case USB_REQ_DFU_GETSTATUS:
392 handle_getstatus(req);
393 value = RET_STAT_LEN;
395 case USB_REQ_DFU_GETSTATE:
396 handle_getstate(req);
399 f_dfu->dfu_state = DFU_STATE_dfuERROR;
407 static int state_dfu_manifest_sync(struct f_dfu *f_dfu,
408 const struct usb_ctrlrequest *ctrl,
409 struct usb_gadget *gadget,
410 struct usb_request *req)
414 switch (ctrl->bRequest) {
415 case USB_REQ_DFU_GETSTATUS:
416 /* We're MainfestationTolerant */
417 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
418 handle_getstatus(req);
419 f_dfu->blk_seq_num = 0;
420 value = RET_STAT_LEN;
422 case USB_REQ_DFU_GETSTATE:
423 handle_getstate(req);
426 f_dfu->dfu_state = DFU_STATE_dfuERROR;
434 static int state_dfu_upload_idle(struct f_dfu *f_dfu,
435 const struct usb_ctrlrequest *ctrl,
436 struct usb_gadget *gadget,
437 struct usb_request *req)
439 u16 w_value = le16_to_cpu(ctrl->wValue);
440 u16 len = le16_to_cpu(ctrl->wLength);
443 switch (ctrl->bRequest) {
444 case USB_REQ_DFU_UPLOAD:
445 /* state transition if less data then requested */
446 f_dfu->blk_seq_num = w_value;
447 value = handle_upload(req, len);
448 if (value >= 0 && value < len)
449 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
451 case USB_REQ_DFU_ABORT:
452 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
456 case USB_REQ_DFU_GETSTATUS:
457 handle_getstatus(req);
458 value = RET_STAT_LEN;
460 case USB_REQ_DFU_GETSTATE:
461 handle_getstate(req);
464 f_dfu->dfu_state = DFU_STATE_dfuERROR;
472 static int state_dfu_error(struct f_dfu *f_dfu,
473 const struct usb_ctrlrequest *ctrl,
474 struct usb_gadget *gadget,
475 struct usb_request *req)
479 switch (ctrl->bRequest) {
480 case USB_REQ_DFU_GETSTATUS:
481 handle_getstatus(req);
482 value = RET_STAT_LEN;
484 case USB_REQ_DFU_GETSTATE:
485 handle_getstate(req);
487 case USB_REQ_DFU_CLRSTATUS:
488 f_dfu->dfu_state = DFU_STATE_dfuIDLE;
489 f_dfu->dfu_status = DFU_STATUS_OK;
494 f_dfu->dfu_state = DFU_STATE_dfuERROR;
502 static dfu_state_fn dfu_state[] = {
503 state_app_idle, /* DFU_STATE_appIDLE */
504 state_app_detach, /* DFU_STATE_appDETACH */
505 state_dfu_idle, /* DFU_STATE_dfuIDLE */
506 state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */
507 state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */
508 state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */
509 state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */
510 NULL, /* DFU_STATE_dfuMANIFEST */
511 NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */
512 state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */
513 state_dfu_error /* DFU_STATE_dfuERROR */
517 dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
519 struct usb_gadget *gadget = f->config->cdev->gadget;
520 struct usb_request *req = f->config->cdev->req;
521 struct f_dfu *f_dfu = f->config->cdev->req->context;
522 u16 len = le16_to_cpu(ctrl->wLength);
523 u16 w_value = le16_to_cpu(ctrl->wValue);
525 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
527 debug("w_value: 0x%x len: 0x%x\n", w_value, len);
528 debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n",
529 req_type, ctrl->bRequest, f_dfu->dfu_state);
531 if (req_type == USB_TYPE_STANDARD) {
532 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
533 (w_value >> 8) == DFU_DT_FUNC) {
534 value = min(len, (u16) sizeof(dfu_func));
535 memcpy(req->buf, &dfu_func, value);
537 } else /* DFU specific request */
538 value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req);
542 req->zero = value < len;
543 value = usb_ep_queue(gadget->ep0, req, 0);
545 debug("ep_queue --> %d\n", value);
553 /*-------------------------------------------------------------------------*/
556 dfu_prepare_strings(struct f_dfu *f_dfu, int n)
558 struct dfu_entity *de = NULL;
561 f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);
565 for (i = 0; i < n; ++i) {
566 de = dfu_get_entity(i);
567 f_dfu->strings[i].s = de->name;
570 f_dfu->strings[i].id = 0;
571 f_dfu->strings[i].s = NULL;
577 f_dfu->strings[--i].s = NULL;
579 free(f_dfu->strings);
584 static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
586 struct usb_interface_descriptor *d;
589 f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 1);
590 if (!f_dfu->function)
593 for (i = 0; i < n; ++i) {
594 d = calloc(sizeof(*d), 1);
598 d->bLength = sizeof(*d);
599 d->bDescriptorType = USB_DT_INTERFACE;
600 d->bAlternateSetting = i;
601 d->bNumEndpoints = 0;
602 d->bInterfaceClass = USB_CLASS_APP_SPEC;
603 d->bInterfaceSubClass = 1;
604 d->bInterfaceProtocol = 2;
606 f_dfu->function[i] = (struct usb_descriptor_header *)d;
608 f_dfu->function[i] = NULL;
614 free(f_dfu->function[--i]);
615 f_dfu->function[i] = NULL;
617 free(f_dfu->function);
622 static int dfu_bind(struct usb_configuration *c, struct usb_function *f)
624 struct usb_composite_dev *cdev = c->cdev;
625 struct f_dfu *f_dfu = func_to_dfu(f);
626 int alt_num = dfu_get_alt_number();
629 id = usb_interface_id(c, f);
632 dfu_intf_runtime.bInterfaceNumber = id;
634 f_dfu->dfu_state = DFU_STATE_appIDLE;
635 f_dfu->dfu_status = DFU_STATUS_OK;
637 rv = dfu_prepare_function(f_dfu, alt_num);
641 rv = dfu_prepare_strings(f_dfu, alt_num);
644 for (i = 0; i < alt_num; i++) {
645 id = usb_string_id(cdev);
648 f_dfu->strings[i].id = id;
649 ((struct usb_interface_descriptor *)f_dfu->function[i])
655 stringtab_dfu.strings = f_dfu->strings;
657 cdev->req->context = f_dfu;
663 static void dfu_unbind(struct usb_configuration *c, struct usb_function *f)
665 struct f_dfu *f_dfu = func_to_dfu(f);
666 int alt_num = dfu_get_alt_number();
669 if (f_dfu->strings) {
672 f_dfu->strings[--i].s = NULL;
674 free(f_dfu->strings);
677 if (f_dfu->function) {
680 free(f_dfu->function[--i]);
681 f_dfu->function[i] = NULL;
683 free(f_dfu->function);
689 static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
691 struct f_dfu *f_dfu = func_to_dfu(f);
693 debug("%s: intf:%d alt:%d\n", __func__, intf, alt);
695 f_dfu->altsetting = alt;
700 /* TODO: is this really what we need here? */
701 static void dfu_disable(struct usb_function *f)
703 struct f_dfu *f_dfu = func_to_dfu(f);
704 if (f_dfu->config == 0)
707 debug("%s: reset config\n", __func__);
712 static int dfu_bind_config(struct usb_configuration *c)
717 f_dfu = calloc(sizeof(*f_dfu), 1);
720 f_dfu->usb_function.name = "dfu";
721 f_dfu->usb_function.hs_descriptors = dfu_runtime_descs;
722 f_dfu->usb_function.bind = dfu_bind;
723 f_dfu->usb_function.unbind = dfu_unbind;
724 f_dfu->usb_function.set_alt = dfu_set_alt;
725 f_dfu->usb_function.disable = dfu_disable;
726 f_dfu->usb_function.strings = dfu_generic_strings,
727 f_dfu->usb_function.setup = dfu_handle,
729 status = usb_add_function(c, &f_dfu->usb_function);
736 int dfu_add(struct usb_configuration *c)
740 id = usb_string_id(c->cdev);
743 strings_dfu_generic[0].id = id;
744 dfu_intf_runtime.iInterface = id;
746 debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__,
747 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
749 return dfu_bind_config(c);