Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 2 | /* |
| 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 Majewski | 7daece8 | 2013-07-05 11:40:47 +0200 | [diff] [blame] | 9 | * 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 Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 17 | #include <env.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 20 | #include <log.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 21 | #include <malloc.h> |
| 22 | |
| 23 | #include <linux/usb/ch9.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 24 | #include <linux/usb/gadget.h> |
| 25 | #include <linux/usb/composite.h> |
| 26 | |
| 27 | #include <dfu.h> |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 28 | #include <g_dnl.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 29 | #include "f_dfu.h" |
| 30 | |
| 31 | struct f_dfu { |
| 32 | struct usb_function usb_function; |
| 33 | |
| 34 | struct usb_descriptor_header **function; |
| 35 | struct usb_string *strings; |
| 36 | |
| 37 | /* when configured, we have one config */ |
| 38 | u8 config; |
| 39 | u8 altsetting; |
| 40 | enum dfu_state dfu_state; |
| 41 | unsigned int dfu_status; |
| 42 | |
| 43 | /* Send/received block number is handy for data integrity check */ |
| 44 | int blk_seq_num; |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 45 | unsigned int poll_timeout; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Lukasz Majewski | 365a70d | 2016-01-28 16:14:49 +0100 | [diff] [blame] | 48 | struct dfu_entity *dfu_defer_flush; |
| 49 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 50 | typedef int (*dfu_state_fn) (struct f_dfu *, |
| 51 | const struct usb_ctrlrequest *, |
| 52 | struct usb_gadget *, |
| 53 | struct usb_request *); |
| 54 | |
| 55 | static inline struct f_dfu *func_to_dfu(struct usb_function *f) |
| 56 | { |
| 57 | return container_of(f, struct f_dfu, usb_function); |
| 58 | } |
| 59 | |
| 60 | static const struct dfu_function_descriptor dfu_func = { |
| 61 | .bLength = sizeof dfu_func, |
| 62 | .bDescriptorType = DFU_DT_FUNC, |
| 63 | .bmAttributes = DFU_BIT_WILL_DETACH | |
| 64 | DFU_BIT_MANIFESTATION_TOLERANT | |
| 65 | DFU_BIT_CAN_UPLOAD | |
| 66 | DFU_BIT_CAN_DNLOAD, |
| 67 | .wDetachTimeOut = 0, |
| 68 | .wTransferSize = DFU_USB_BUFSIZ, |
| 69 | .bcdDFUVersion = __constant_cpu_to_le16(0x0110), |
| 70 | }; |
| 71 | |
| 72 | static struct usb_interface_descriptor dfu_intf_runtime = { |
| 73 | .bLength = sizeof dfu_intf_runtime, |
| 74 | .bDescriptorType = USB_DT_INTERFACE, |
| 75 | .bNumEndpoints = 0, |
| 76 | .bInterfaceClass = USB_CLASS_APP_SPEC, |
| 77 | .bInterfaceSubClass = 1, |
| 78 | .bInterfaceProtocol = 1, |
| 79 | /* .iInterface = DYNAMIC */ |
| 80 | }; |
| 81 | |
| 82 | static struct usb_descriptor_header *dfu_runtime_descs[] = { |
| 83 | (struct usb_descriptor_header *) &dfu_intf_runtime, |
| 84 | NULL, |
| 85 | }; |
| 86 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 87 | static const char dfu_name[] = "Device Firmware Upgrade"; |
| 88 | |
| 89 | /* |
| 90 | * static strings, in UTF-8 |
| 91 | * |
| 92 | * dfu_generic configuration |
| 93 | */ |
| 94 | static struct usb_string strings_dfu_generic[] = { |
| 95 | [0].s = dfu_name, |
| 96 | { } /* end of list */ |
| 97 | }; |
| 98 | |
| 99 | static struct usb_gadget_strings stringtab_dfu_generic = { |
| 100 | .language = 0x0409, /* en-us */ |
| 101 | .strings = strings_dfu_generic, |
| 102 | }; |
| 103 | |
| 104 | static struct usb_gadget_strings *dfu_generic_strings[] = { |
| 105 | &stringtab_dfu_generic, |
| 106 | NULL, |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * usb_function specific |
| 111 | */ |
| 112 | static struct usb_gadget_strings stringtab_dfu = { |
| 113 | .language = 0x0409, /* en-us */ |
| 114 | /* |
| 115 | * .strings |
| 116 | * |
| 117 | * assigned during initialization, |
| 118 | * depends on number of flash entities |
| 119 | * |
| 120 | */ |
| 121 | }; |
| 122 | |
| 123 | static struct usb_gadget_strings *dfu_strings[] = { |
| 124 | &stringtab_dfu, |
| 125 | NULL, |
| 126 | }; |
| 127 | |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 128 | static void dfu_set_poll_timeout(struct dfu_status *dstat, unsigned int ms) |
| 129 | { |
| 130 | /* |
| 131 | * The bwPollTimeout DFU_GETSTATUS request payload provides information |
| 132 | * about minimum time, in milliseconds, that the host should wait before |
| 133 | * sending a subsequent DFU_GETSTATUS request |
| 134 | * |
| 135 | * This permits the device to vary the delay depending on its need to |
| 136 | * erase or program the memory |
| 137 | * |
| 138 | */ |
| 139 | |
| 140 | unsigned char *p = (unsigned char *)&ms; |
| 141 | |
| 142 | if (!ms || (ms & ~DFU_POLL_TIMEOUT_MASK)) { |
| 143 | dstat->bwPollTimeout[0] = 0; |
| 144 | dstat->bwPollTimeout[1] = 0; |
| 145 | dstat->bwPollTimeout[2] = 0; |
| 146 | |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | dstat->bwPollTimeout[0] = *p++; |
| 151 | dstat->bwPollTimeout[1] = *p++; |
| 152 | dstat->bwPollTimeout[2] = *p; |
| 153 | } |
| 154 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 155 | /*-------------------------------------------------------------------------*/ |
| 156 | |
| 157 | static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req) |
| 158 | { |
| 159 | struct f_dfu *f_dfu = req->context; |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 160 | int ret; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 161 | |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 162 | ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf, |
Felipe Balbi | c729786 | 2017-02-10 19:32:31 +0300 | [diff] [blame] | 163 | req->actual, f_dfu->blk_seq_num); |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 164 | if (ret) { |
| 165 | f_dfu->dfu_status = DFU_STATUS_errUNKNOWN; |
| 166 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 167 | } |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 168 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 169 | |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 170 | static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req) |
| 171 | { |
| 172 | struct f_dfu *f_dfu = req->context; |
Lukasz Majewski | 365a70d | 2016-01-28 16:14:49 +0100 | [diff] [blame] | 173 | dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting)); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 176 | static inline int dfu_get_manifest_timeout(struct dfu_entity *dfu) |
| 177 | { |
| 178 | return dfu->poll_timeout ? dfu->poll_timeout(dfu) : |
| 179 | DFU_MANIFEST_POLL_TIMEOUT; |
| 180 | } |
| 181 | |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 182 | static int handle_getstatus(struct usb_request *req) |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 183 | { |
| 184 | struct dfu_status *dstat = (struct dfu_status *)req->buf; |
| 185 | struct f_dfu *f_dfu = req->context; |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 186 | struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 187 | |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 188 | dfu_set_poll_timeout(dstat, 0); |
| 189 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 190 | switch (f_dfu->dfu_state) { |
| 191 | case DFU_STATE_dfuDNLOAD_SYNC: |
| 192 | case DFU_STATE_dfuDNBUSY: |
| 193 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE; |
| 194 | break; |
| 195 | case DFU_STATE_dfuMANIFEST_SYNC: |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 196 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 197 | break; |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 198 | case DFU_STATE_dfuMANIFEST: |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 199 | dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu)); |
| 200 | break; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 201 | default: |
| 202 | break; |
| 203 | } |
| 204 | |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 205 | if (f_dfu->poll_timeout) |
| 206 | if (!(f_dfu->blk_seq_num % |
| 207 | (dfu_get_buf_size() / DFU_USB_BUFSIZ))) |
| 208 | dfu_set_poll_timeout(dstat, f_dfu->poll_timeout); |
| 209 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 210 | /* send status response */ |
| 211 | dstat->bStatus = f_dfu->dfu_status; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 212 | dstat->bState = f_dfu->dfu_state; |
| 213 | dstat->iString = 0; |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 214 | |
| 215 | return sizeof(struct dfu_status); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 218 | static int handle_getstate(struct usb_request *req) |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 219 | { |
| 220 | struct f_dfu *f_dfu = req->context; |
| 221 | |
| 222 | ((u8 *)req->buf)[0] = f_dfu->dfu_state; |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 223 | return sizeof(u8); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static inline void to_dfu_mode(struct f_dfu *f_dfu) |
| 227 | { |
| 228 | f_dfu->usb_function.strings = dfu_strings; |
| 229 | f_dfu->usb_function.hs_descriptors = f_dfu->function; |
Heiko Schocher | 3e3a136 | 2014-09-10 08:55:00 +0200 | [diff] [blame] | 230 | f_dfu->usb_function.descriptors = f_dfu->function; |
Heiko Schocher | 5890e83 | 2013-06-25 13:59:29 +0200 | [diff] [blame] | 231 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static inline void to_runtime_mode(struct f_dfu *f_dfu) |
| 235 | { |
| 236 | f_dfu->usb_function.strings = NULL; |
| 237 | f_dfu->usb_function.hs_descriptors = dfu_runtime_descs; |
Heiko Schocher | 3e3a136 | 2014-09-10 08:55:00 +0200 | [diff] [blame] | 238 | f_dfu->usb_function.descriptors = dfu_runtime_descs; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int handle_upload(struct usb_request *req, u16 len) |
| 242 | { |
| 243 | struct f_dfu *f_dfu = req->context; |
| 244 | |
| 245 | return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf, |
| 246 | req->length, f_dfu->blk_seq_num); |
| 247 | } |
| 248 | |
| 249 | static int handle_dnload(struct usb_gadget *gadget, u16 len) |
| 250 | { |
| 251 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 252 | struct usb_request *req = cdev->req; |
| 253 | struct f_dfu *f_dfu = req->context; |
| 254 | |
| 255 | if (len == 0) |
| 256 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC; |
| 257 | |
| 258 | req->complete = dnload_request_complete; |
| 259 | |
| 260 | return len; |
| 261 | } |
| 262 | |
| 263 | /*-------------------------------------------------------------------------*/ |
| 264 | /* DFU state machine */ |
| 265 | static int state_app_idle(struct f_dfu *f_dfu, |
| 266 | const struct usb_ctrlrequest *ctrl, |
| 267 | struct usb_gadget *gadget, |
| 268 | struct usb_request *req) |
| 269 | { |
| 270 | int value = 0; |
| 271 | |
| 272 | switch (ctrl->bRequest) { |
| 273 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 274 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 275 | break; |
| 276 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 277 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 278 | break; |
| 279 | case USB_REQ_DFU_DETACH: |
| 280 | f_dfu->dfu_state = DFU_STATE_appDETACH; |
| 281 | to_dfu_mode(f_dfu); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 282 | value = RET_ZLP; |
| 283 | break; |
| 284 | default: |
| 285 | value = RET_STALL; |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | return value; |
| 290 | } |
| 291 | |
| 292 | static int state_app_detach(struct f_dfu *f_dfu, |
| 293 | const struct usb_ctrlrequest *ctrl, |
| 294 | struct usb_gadget *gadget, |
| 295 | struct usb_request *req) |
| 296 | { |
| 297 | int value = 0; |
| 298 | |
| 299 | switch (ctrl->bRequest) { |
| 300 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 301 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 302 | break; |
| 303 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 304 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 305 | break; |
| 306 | default: |
| 307 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
| 308 | value = RET_STALL; |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | return value; |
| 313 | } |
| 314 | |
| 315 | static int state_dfu_idle(struct f_dfu *f_dfu, |
| 316 | const struct usb_ctrlrequest *ctrl, |
| 317 | struct usb_gadget *gadget, |
| 318 | struct usb_request *req) |
| 319 | { |
| 320 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 321 | u16 len = le16_to_cpu(ctrl->wLength); |
| 322 | int value = 0; |
| 323 | |
| 324 | switch (ctrl->bRequest) { |
| 325 | case USB_REQ_DFU_DNLOAD: |
| 326 | if (len == 0) { |
| 327 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 328 | value = RET_STALL; |
| 329 | break; |
| 330 | } |
| 331 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC; |
| 332 | f_dfu->blk_seq_num = w_value; |
| 333 | value = handle_dnload(gadget, len); |
| 334 | break; |
| 335 | case USB_REQ_DFU_UPLOAD: |
| 336 | f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE; |
| 337 | f_dfu->blk_seq_num = 0; |
| 338 | value = handle_upload(req, len); |
Patrick Delaunay | 455542c | 2021-10-13 17:01:37 +0200 | [diff] [blame] | 339 | if (value >= 0 && value < len) |
| 340 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 341 | break; |
| 342 | case USB_REQ_DFU_ABORT: |
| 343 | /* no zlp? */ |
| 344 | value = RET_ZLP; |
| 345 | break; |
| 346 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 347 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 348 | break; |
| 349 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 350 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 351 | break; |
| 352 | case USB_REQ_DFU_DETACH: |
| 353 | /* |
| 354 | * Proprietary extension: 'detach' from idle mode and |
| 355 | * get back to runtime mode in case of USB Reset. As |
| 356 | * much as I dislike this, we just can't use every USB |
| 357 | * bus reset to switch back to runtime mode, since at |
| 358 | * least the Linux USB stack likes to send a number of |
| 359 | * resets in a row :( |
| 360 | */ |
| 361 | f_dfu->dfu_state = |
| 362 | DFU_STATE_dfuMANIFEST_WAIT_RST; |
| 363 | to_runtime_mode(f_dfu); |
| 364 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 365 | |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 366 | g_dnl_trigger_detach(); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 367 | break; |
| 368 | default: |
| 369 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 370 | value = RET_STALL; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | return value; |
| 375 | } |
| 376 | |
| 377 | static int state_dfu_dnload_sync(struct f_dfu *f_dfu, |
| 378 | const struct usb_ctrlrequest *ctrl, |
| 379 | struct usb_gadget *gadget, |
| 380 | struct usb_request *req) |
| 381 | { |
| 382 | int value = 0; |
| 383 | |
| 384 | switch (ctrl->bRequest) { |
| 385 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 386 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 387 | break; |
| 388 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 389 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 390 | break; |
| 391 | default: |
| 392 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 393 | value = RET_STALL; |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | return value; |
| 398 | } |
| 399 | |
| 400 | static int state_dfu_dnbusy(struct f_dfu *f_dfu, |
| 401 | const struct usb_ctrlrequest *ctrl, |
| 402 | struct usb_gadget *gadget, |
| 403 | struct usb_request *req) |
| 404 | { |
| 405 | int value = 0; |
| 406 | |
| 407 | switch (ctrl->bRequest) { |
| 408 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 409 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 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 | |
| 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) |
| 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: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 440 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 441 | break; |
| 442 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 443 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 444 | break; |
| 445 | default: |
| 446 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 447 | value = RET_STALL; |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | return value; |
| 452 | } |
| 453 | |
| 454 | static int state_dfu_manifest_sync(struct f_dfu *f_dfu, |
| 455 | const struct usb_ctrlrequest *ctrl, |
| 456 | struct usb_gadget *gadget, |
| 457 | struct usb_request *req) |
| 458 | { |
| 459 | int value = 0; |
| 460 | |
| 461 | switch (ctrl->bRequest) { |
| 462 | case USB_REQ_DFU_GETSTATUS: |
| 463 | /* We're MainfestationTolerant */ |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 464 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST; |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 465 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 466 | f_dfu->blk_seq_num = 0; |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 467 | req->complete = dnload_request_flush; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 468 | break; |
| 469 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 470 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 471 | break; |
| 472 | default: |
| 473 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 474 | value = RET_STALL; |
| 475 | break; |
| 476 | } |
| 477 | |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 478 | return value; |
| 479 | } |
| 480 | |
| 481 | static int state_dfu_manifest(struct f_dfu *f_dfu, |
| 482 | const struct usb_ctrlrequest *ctrl, |
| 483 | struct usb_gadget *gadget, |
| 484 | struct usb_request *req) |
| 485 | { |
| 486 | int value = 0; |
| 487 | |
| 488 | switch (ctrl->bRequest) { |
| 489 | case USB_REQ_DFU_GETSTATUS: |
| 490 | /* We're MainfestationTolerant */ |
| 491 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 492 | value = handle_getstatus(req); |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 493 | f_dfu->blk_seq_num = 0; |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 494 | puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n"); |
| 495 | break; |
| 496 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 497 | value = handle_getstate(req); |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 498 | break; |
| 499 | default: |
| 500 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 501 | value = RET_STALL; |
| 502 | break; |
| 503 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 504 | return value; |
| 505 | } |
| 506 | |
| 507 | static int state_dfu_upload_idle(struct f_dfu *f_dfu, |
| 508 | const struct usb_ctrlrequest *ctrl, |
| 509 | struct usb_gadget *gadget, |
| 510 | struct usb_request *req) |
| 511 | { |
| 512 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 513 | u16 len = le16_to_cpu(ctrl->wLength); |
| 514 | int value = 0; |
| 515 | |
| 516 | switch (ctrl->bRequest) { |
| 517 | case USB_REQ_DFU_UPLOAD: |
| 518 | /* state transition if less data then requested */ |
| 519 | f_dfu->blk_seq_num = w_value; |
| 520 | value = handle_upload(req, len); |
| 521 | if (value >= 0 && value < len) |
| 522 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 523 | break; |
| 524 | case USB_REQ_DFU_ABORT: |
| 525 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 526 | /* no zlp? */ |
| 527 | value = RET_ZLP; |
| 528 | break; |
| 529 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 530 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 531 | break; |
| 532 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 533 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 534 | break; |
| 535 | default: |
| 536 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 537 | value = RET_STALL; |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | return value; |
| 542 | } |
| 543 | |
| 544 | static int state_dfu_error(struct f_dfu *f_dfu, |
| 545 | const struct usb_ctrlrequest *ctrl, |
| 546 | struct usb_gadget *gadget, |
| 547 | struct usb_request *req) |
| 548 | { |
| 549 | int value = 0; |
| 550 | |
| 551 | switch (ctrl->bRequest) { |
| 552 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 553 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 554 | break; |
| 555 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 556 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 557 | break; |
| 558 | case USB_REQ_DFU_CLRSTATUS: |
| 559 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 560 | f_dfu->dfu_status = DFU_STATUS_OK; |
| 561 | /* no zlp? */ |
| 562 | value = RET_ZLP; |
| 563 | break; |
| 564 | default: |
| 565 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 566 | value = RET_STALL; |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | return value; |
| 571 | } |
| 572 | |
| 573 | static dfu_state_fn dfu_state[] = { |
| 574 | state_app_idle, /* DFU_STATE_appIDLE */ |
| 575 | state_app_detach, /* DFU_STATE_appDETACH */ |
| 576 | state_dfu_idle, /* DFU_STATE_dfuIDLE */ |
| 577 | state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */ |
| 578 | state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */ |
| 579 | state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */ |
| 580 | state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */ |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 581 | state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */ |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 582 | NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */ |
| 583 | state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */ |
| 584 | state_dfu_error /* DFU_STATE_dfuERROR */ |
| 585 | }; |
| 586 | |
| 587 | static int |
| 588 | dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 589 | { |
| 590 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 591 | struct usb_request *req = f->config->cdev->req; |
| 592 | struct f_dfu *f_dfu = f->config->cdev->req->context; |
| 593 | u16 len = le16_to_cpu(ctrl->wLength); |
| 594 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 595 | int value = 0; |
| 596 | u8 req_type = ctrl->bRequestType & USB_TYPE_MASK; |
| 597 | |
| 598 | debug("w_value: 0x%x len: 0x%x\n", w_value, len); |
| 599 | debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n", |
| 600 | req_type, ctrl->bRequest, f_dfu->dfu_state); |
| 601 | |
Andy Shevchenko | f4f2db7 | 2020-01-29 17:23:14 +0200 | [diff] [blame] | 602 | #ifdef CONFIG_DFU_TIMEOUT |
| 603 | /* Forbid aborting by timeout. Next dfu command may update this */ |
| 604 | dfu_set_timeout(0); |
| 605 | #endif |
| 606 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 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 | |
| 631 | static int |
| 632 | dfu_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 Fan | 46ed88c | 2016-05-03 10:25:22 +0800 | [diff] [blame] | 639 | return -ENOMEM; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 640 | |
| 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 Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static int dfu_prepare_function(struct f_dfu *f_dfu, int n) |
| 653 | { |
| 654 | struct usb_interface_descriptor *d; |
| 655 | int i = 0; |
| 656 | |
Patrick Delaunay | bf2a415 | 2016-12-08 18:10:49 +0100 | [diff] [blame] | 657 | f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 658 | 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 Delaunay | bf2a415 | 2016-12-08 18:10:49 +0100 | [diff] [blame] | 676 | |
| 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 Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 684 | f_dfu->function[i] = NULL; |
| 685 | |
| 686 | return 0; |
| 687 | |
| 688 | enomem: |
| 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 | |
| 698 | static 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); |
Felipe Balbi | d1c34e4 | 2017-02-22 11:22:38 +0200 | [diff] [blame] | 702 | const char *s; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 703 | int alt_num = dfu_get_alt_number(); |
| 704 | int rv, id, i; |
| 705 | |
| 706 | id = usb_interface_id(c, f); |
| 707 | if (id < 0) |
| 708 | return id; |
| 709 | dfu_intf_runtime.bInterfaceNumber = id; |
| 710 | |
| 711 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
| 712 | f_dfu->dfu_status = DFU_STATUS_OK; |
| 713 | |
| 714 | rv = dfu_prepare_function(f_dfu, alt_num); |
| 715 | if (rv) |
| 716 | goto error; |
| 717 | |
| 718 | rv = dfu_prepare_strings(f_dfu, alt_num); |
| 719 | if (rv) |
| 720 | goto error; |
| 721 | for (i = 0; i < alt_num; i++) { |
| 722 | id = usb_string_id(cdev); |
| 723 | if (id < 0) |
| 724 | return id; |
| 725 | f_dfu->strings[i].id = id; |
| 726 | ((struct usb_interface_descriptor *)f_dfu->function[i]) |
| 727 | ->iInterface = id; |
| 728 | } |
| 729 | |
Heiko Schocher | 5890e83 | 2013-06-25 13:59:29 +0200 | [diff] [blame] | 730 | to_dfu_mode(f_dfu); |
| 731 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 732 | stringtab_dfu.strings = f_dfu->strings; |
| 733 | |
| 734 | cdev->req->context = f_dfu; |
| 735 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 736 | s = env_get("serial#"); |
Felipe Balbi | d1c34e4 | 2017-02-22 11:22:38 +0200 | [diff] [blame] | 737 | if (s) |
| 738 | g_dnl_set_serialnumber((char *)s); |
| 739 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 740 | error: |
| 741 | return rv; |
| 742 | } |
| 743 | |
| 744 | static void dfu_unbind(struct usb_configuration *c, struct usb_function *f) |
| 745 | { |
| 746 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 747 | int alt_num = dfu_get_alt_number(); |
| 748 | int i; |
| 749 | |
| 750 | if (f_dfu->strings) { |
| 751 | i = alt_num; |
| 752 | while (i) |
| 753 | f_dfu->strings[--i].s = NULL; |
| 754 | |
| 755 | free(f_dfu->strings); |
| 756 | } |
| 757 | |
| 758 | if (f_dfu->function) { |
| 759 | i = alt_num; |
Ralph Siemsen | 4cceff6 | 2019-06-28 10:42:02 -0400 | [diff] [blame] | 760 | i++; /* free DFU Functional Descriptor */ |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 761 | while (i) { |
| 762 | free(f_dfu->function[--i]); |
| 763 | f_dfu->function[i] = NULL; |
| 764 | } |
| 765 | free(f_dfu->function); |
| 766 | } |
| 767 | |
| 768 | free(f_dfu); |
| 769 | } |
| 770 | |
| 771 | static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 772 | { |
| 773 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 774 | |
| 775 | debug("%s: intf:%d alt:%d\n", __func__, intf, alt); |
| 776 | |
| 777 | f_dfu->altsetting = alt; |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 778 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 779 | f_dfu->dfu_status = DFU_STATUS_OK; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
Stefan Roese | 5a6b873 | 2015-01-09 14:54:56 +0100 | [diff] [blame] | 784 | static int __dfu_get_alt(struct usb_function *f, unsigned intf) |
| 785 | { |
| 786 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 787 | |
| 788 | return f_dfu->altsetting; |
| 789 | } |
| 790 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 791 | /* TODO: is this really what we need here? */ |
| 792 | static void dfu_disable(struct usb_function *f) |
| 793 | { |
| 794 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 795 | if (f_dfu->config == 0) |
| 796 | return; |
| 797 | |
| 798 | debug("%s: reset config\n", __func__); |
| 799 | |
| 800 | f_dfu->config = 0; |
| 801 | } |
| 802 | |
| 803 | static int dfu_bind_config(struct usb_configuration *c) |
| 804 | { |
| 805 | struct f_dfu *f_dfu; |
| 806 | int status; |
| 807 | |
| 808 | f_dfu = calloc(sizeof(*f_dfu), 1); |
| 809 | if (!f_dfu) |
| 810 | return -ENOMEM; |
| 811 | f_dfu->usb_function.name = "dfu"; |
| 812 | f_dfu->usb_function.hs_descriptors = dfu_runtime_descs; |
Heiko Schocher | 3e3a136 | 2014-09-10 08:55:00 +0200 | [diff] [blame] | 813 | f_dfu->usb_function.descriptors = dfu_runtime_descs; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 814 | f_dfu->usb_function.bind = dfu_bind; |
| 815 | f_dfu->usb_function.unbind = dfu_unbind; |
| 816 | f_dfu->usb_function.set_alt = dfu_set_alt; |
Stefan Roese | 5a6b873 | 2015-01-09 14:54:56 +0100 | [diff] [blame] | 817 | f_dfu->usb_function.get_alt = __dfu_get_alt; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 818 | f_dfu->usb_function.disable = dfu_disable; |
Lukasz Majewski | 1abe47d | 2013-12-09 16:20:15 +0100 | [diff] [blame] | 819 | f_dfu->usb_function.strings = dfu_generic_strings; |
| 820 | f_dfu->usb_function.setup = dfu_handle; |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 821 | f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 822 | |
| 823 | status = usb_add_function(c, &f_dfu->usb_function); |
| 824 | if (status) |
| 825 | free(f_dfu); |
| 826 | |
| 827 | return status; |
| 828 | } |
| 829 | |
| 830 | int dfu_add(struct usb_configuration *c) |
| 831 | { |
| 832 | int id; |
| 833 | |
| 834 | id = usb_string_id(c->cdev); |
| 835 | if (id < 0) |
| 836 | return id; |
| 837 | strings_dfu_generic[0].id = id; |
| 838 | dfu_intf_runtime.iInterface = id; |
| 839 | |
| 840 | debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__, |
| 841 | c->cdev, c->cdev->gadget, c->cdev->gadget->ep0); |
| 842 | |
| 843 | return dfu_bind_config(c); |
| 844 | } |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 845 | |
| 846 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add); |