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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 20 | #include <malloc.h> |
| 21 | |
| 22 | #include <linux/usb/ch9.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 23 | #include <linux/usb/gadget.h> |
| 24 | #include <linux/usb/composite.h> |
| 25 | |
| 26 | #include <dfu.h> |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 27 | #include <g_dnl.h> |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 28 | #include "f_dfu.h" |
| 29 | |
| 30 | struct 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 Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 44 | unsigned int poll_timeout; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Lukasz Majewski | 365a70d | 2016-01-28 16:14:49 +0100 | [diff] [blame] | 47 | struct dfu_entity *dfu_defer_flush; |
| 48 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 49 | typedef int (*dfu_state_fn) (struct f_dfu *, |
| 50 | const struct usb_ctrlrequest *, |
| 51 | struct usb_gadget *, |
| 52 | struct usb_request *); |
| 53 | |
| 54 | static inline struct f_dfu *func_to_dfu(struct usb_function *f) |
| 55 | { |
| 56 | return container_of(f, struct f_dfu, usb_function); |
| 57 | } |
| 58 | |
| 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 | |
| 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 | |
| 71 | static 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 | |
| 81 | static struct usb_descriptor_header *dfu_runtime_descs[] = { |
| 82 | (struct usb_descriptor_header *) &dfu_intf_runtime, |
| 83 | NULL, |
| 84 | }; |
| 85 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 86 | static const char dfu_name[] = "Device Firmware Upgrade"; |
| 87 | |
| 88 | /* |
| 89 | * static strings, in UTF-8 |
| 90 | * |
| 91 | * dfu_generic configuration |
| 92 | */ |
| 93 | static struct usb_string strings_dfu_generic[] = { |
| 94 | [0].s = dfu_name, |
| 95 | { } /* end of list */ |
| 96 | }; |
| 97 | |
| 98 | static struct usb_gadget_strings stringtab_dfu_generic = { |
| 99 | .language = 0x0409, /* en-us */ |
| 100 | .strings = strings_dfu_generic, |
| 101 | }; |
| 102 | |
| 103 | static struct usb_gadget_strings *dfu_generic_strings[] = { |
| 104 | &stringtab_dfu_generic, |
| 105 | NULL, |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * usb_function specific |
| 110 | */ |
| 111 | static 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 | |
| 122 | static struct usb_gadget_strings *dfu_strings[] = { |
| 123 | &stringtab_dfu, |
| 124 | NULL, |
| 125 | }; |
| 126 | |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 127 | static 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 Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 154 | /*-------------------------------------------------------------------------*/ |
| 155 | |
| 156 | static void dnload_request_complete(struct usb_ep *ep, struct usb_request *req) |
| 157 | { |
| 158 | struct f_dfu *f_dfu = req->context; |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 159 | int ret; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 160 | |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 161 | ret = dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf, |
Felipe Balbi | c729786 | 2017-02-10 19:32:31 +0300 | [diff] [blame] | 162 | req->actual, f_dfu->blk_seq_num); |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 163 | if (ret) { |
| 164 | f_dfu->dfu_status = DFU_STATUS_errUNKNOWN; |
| 165 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 166 | } |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 167 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 168 | |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 169 | static void dnload_request_flush(struct usb_ep *ep, struct usb_request *req) |
| 170 | { |
| 171 | struct f_dfu *f_dfu = req->context; |
Lukasz Majewski | 365a70d | 2016-01-28 16:14:49 +0100 | [diff] [blame] | 172 | dfu_set_defer_flush(dfu_get_entity(f_dfu->altsetting)); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 175 | static 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 | |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 181 | static int handle_getstatus(struct usb_request *req) |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 182 | { |
| 183 | struct dfu_status *dstat = (struct dfu_status *)req->buf; |
| 184 | struct f_dfu *f_dfu = req->context; |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 185 | struct dfu_entity *dfu = dfu_get_entity(f_dfu->altsetting); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 186 | |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 187 | dfu_set_poll_timeout(dstat, 0); |
| 188 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 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; |
| 193 | break; |
| 194 | case DFU_STATE_dfuMANIFEST_SYNC: |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 195 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 196 | break; |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 197 | case DFU_STATE_dfuMANIFEST: |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 198 | dfu_set_poll_timeout(dstat, dfu_get_manifest_timeout(dfu)); |
| 199 | break; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 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); |
| 208 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 209 | /* send status response */ |
| 210 | dstat->bStatus = f_dfu->dfu_status; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 211 | dstat->bState = f_dfu->dfu_state; |
| 212 | dstat->iString = 0; |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 213 | |
| 214 | return sizeof(struct dfu_status); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 217 | static int handle_getstate(struct usb_request *req) |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 218 | { |
| 219 | struct f_dfu *f_dfu = req->context; |
| 220 | |
| 221 | ((u8 *)req->buf)[0] = f_dfu->dfu_state; |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 222 | return sizeof(u8); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static inline void to_dfu_mode(struct f_dfu *f_dfu) |
| 226 | { |
| 227 | f_dfu->usb_function.strings = dfu_strings; |
| 228 | f_dfu->usb_function.hs_descriptors = f_dfu->function; |
Heiko Schocher | 3e3a136 | 2014-09-10 08:55:00 +0200 | [diff] [blame] | 229 | f_dfu->usb_function.descriptors = f_dfu->function; |
Heiko Schocher | 5890e83 | 2013-06-25 13:59:29 +0200 | [diff] [blame] | 230 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static inline void to_runtime_mode(struct f_dfu *f_dfu) |
| 234 | { |
| 235 | f_dfu->usb_function.strings = NULL; |
| 236 | f_dfu->usb_function.hs_descriptors = dfu_runtime_descs; |
Heiko Schocher | 3e3a136 | 2014-09-10 08:55:00 +0200 | [diff] [blame] | 237 | f_dfu->usb_function.descriptors = dfu_runtime_descs; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static int handle_upload(struct usb_request *req, u16 len) |
| 241 | { |
| 242 | struct f_dfu *f_dfu = req->context; |
| 243 | |
| 244 | return dfu_read(dfu_get_entity(f_dfu->altsetting), req->buf, |
| 245 | req->length, f_dfu->blk_seq_num); |
| 246 | } |
| 247 | |
| 248 | static int handle_dnload(struct usb_gadget *gadget, u16 len) |
| 249 | { |
| 250 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 251 | struct usb_request *req = cdev->req; |
| 252 | struct f_dfu *f_dfu = req->context; |
| 253 | |
| 254 | if (len == 0) |
| 255 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC; |
| 256 | |
| 257 | req->complete = dnload_request_complete; |
| 258 | |
| 259 | return len; |
| 260 | } |
| 261 | |
| 262 | /*-------------------------------------------------------------------------*/ |
| 263 | /* DFU state machine */ |
| 264 | static int state_app_idle(struct f_dfu *f_dfu, |
| 265 | const struct usb_ctrlrequest *ctrl, |
| 266 | struct usb_gadget *gadget, |
| 267 | struct usb_request *req) |
| 268 | { |
| 269 | int value = 0; |
| 270 | |
| 271 | switch (ctrl->bRequest) { |
| 272 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 273 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 274 | break; |
| 275 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 276 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 277 | break; |
| 278 | case USB_REQ_DFU_DETACH: |
| 279 | f_dfu->dfu_state = DFU_STATE_appDETACH; |
| 280 | to_dfu_mode(f_dfu); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 281 | value = RET_ZLP; |
| 282 | break; |
| 283 | default: |
| 284 | value = RET_STALL; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | return value; |
| 289 | } |
| 290 | |
| 291 | static int state_app_detach(struct f_dfu *f_dfu, |
| 292 | const struct usb_ctrlrequest *ctrl, |
| 293 | struct usb_gadget *gadget, |
| 294 | struct usb_request *req) |
| 295 | { |
| 296 | int value = 0; |
| 297 | |
| 298 | switch (ctrl->bRequest) { |
| 299 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 300 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 301 | break; |
| 302 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 303 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 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 | |
| 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) |
| 318 | { |
| 319 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 320 | u16 len = le16_to_cpu(ctrl->wLength); |
| 321 | int value = 0; |
| 322 | |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 323 | len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len; |
| 324 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 325 | switch (ctrl->bRequest) { |
| 326 | case USB_REQ_DFU_DNLOAD: |
Hugo SIMELIERE | 06ce62d | 2022-11-30 09:29:16 +0100 | [diff] [blame] | 327 | if (!(ctrl->bRequestType & USB_DIR_IN)) { |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 328 | if (len == 0) { |
| 329 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 330 | value = RET_STALL; |
| 331 | break; |
| 332 | } |
| 333 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC; |
| 334 | f_dfu->blk_seq_num = w_value; |
| 335 | value = handle_dnload(gadget, len); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 336 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 337 | break; |
| 338 | case USB_REQ_DFU_UPLOAD: |
Hugo SIMELIERE | 06ce62d | 2022-11-30 09:29:16 +0100 | [diff] [blame] | 339 | if (ctrl->bRequestType & USB_DIR_IN) { |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 340 | f_dfu->dfu_state = DFU_STATE_dfuUPLOAD_IDLE; |
| 341 | f_dfu->blk_seq_num = 0; |
| 342 | value = handle_upload(req, len); |
| 343 | if (value >= 0 && value < len) |
| 344 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 345 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 346 | break; |
| 347 | case USB_REQ_DFU_ABORT: |
| 348 | /* no zlp? */ |
| 349 | value = RET_ZLP; |
| 350 | break; |
| 351 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 352 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 353 | break; |
| 354 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 355 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 356 | break; |
| 357 | case USB_REQ_DFU_DETACH: |
| 358 | /* |
| 359 | * Proprietary extension: 'detach' from idle mode and |
| 360 | * get back to runtime mode in case of USB Reset. As |
| 361 | * much as I dislike this, we just can't use every USB |
| 362 | * bus reset to switch back to runtime mode, since at |
| 363 | * least the Linux USB stack likes to send a number of |
| 364 | * resets in a row :( |
| 365 | */ |
| 366 | f_dfu->dfu_state = |
| 367 | DFU_STATE_dfuMANIFEST_WAIT_RST; |
| 368 | to_runtime_mode(f_dfu); |
| 369 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 370 | |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 371 | g_dnl_trigger_detach(); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 372 | break; |
| 373 | default: |
| 374 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 375 | value = RET_STALL; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | return value; |
| 380 | } |
| 381 | |
| 382 | static int state_dfu_dnload_sync(struct f_dfu *f_dfu, |
| 383 | const struct usb_ctrlrequest *ctrl, |
| 384 | struct usb_gadget *gadget, |
| 385 | struct usb_request *req) |
| 386 | { |
| 387 | int value = 0; |
| 388 | |
| 389 | switch (ctrl->bRequest) { |
| 390 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 391 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 392 | break; |
| 393 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 394 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 395 | break; |
| 396 | default: |
| 397 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 398 | value = RET_STALL; |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | return value; |
| 403 | } |
| 404 | |
| 405 | static int state_dfu_dnbusy(struct f_dfu *f_dfu, |
| 406 | const struct usb_ctrlrequest *ctrl, |
| 407 | struct usb_gadget *gadget, |
| 408 | struct usb_request *req) |
| 409 | { |
| 410 | int value = 0; |
| 411 | |
| 412 | switch (ctrl->bRequest) { |
| 413 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 414 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 415 | break; |
| 416 | default: |
| 417 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 418 | value = RET_STALL; |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | return value; |
| 423 | } |
| 424 | |
| 425 | static int state_dfu_dnload_idle(struct f_dfu *f_dfu, |
| 426 | const struct usb_ctrlrequest *ctrl, |
| 427 | struct usb_gadget *gadget, |
| 428 | struct usb_request *req) |
| 429 | { |
| 430 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 431 | u16 len = le16_to_cpu(ctrl->wLength); |
| 432 | int value = 0; |
| 433 | |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 434 | len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len; |
| 435 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 436 | switch (ctrl->bRequest) { |
| 437 | case USB_REQ_DFU_DNLOAD: |
Hugo SIMELIERE | 06ce62d | 2022-11-30 09:29:16 +0100 | [diff] [blame] | 438 | if (!(ctrl->bRequestType & USB_DIR_IN)) { |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 439 | f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_SYNC; |
| 440 | f_dfu->blk_seq_num = w_value; |
| 441 | value = handle_dnload(gadget, len); |
| 442 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 443 | break; |
| 444 | case USB_REQ_DFU_ABORT: |
| 445 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 446 | value = RET_ZLP; |
| 447 | break; |
| 448 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 449 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 450 | break; |
| 451 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 452 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 453 | break; |
| 454 | default: |
| 455 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 456 | value = RET_STALL; |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | return value; |
| 461 | } |
| 462 | |
| 463 | static int state_dfu_manifest_sync(struct f_dfu *f_dfu, |
| 464 | const struct usb_ctrlrequest *ctrl, |
| 465 | struct usb_gadget *gadget, |
| 466 | struct usb_request *req) |
| 467 | { |
| 468 | int value = 0; |
| 469 | |
| 470 | switch (ctrl->bRequest) { |
| 471 | case USB_REQ_DFU_GETSTATUS: |
| 472 | /* We're MainfestationTolerant */ |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 473 | f_dfu->dfu_state = DFU_STATE_dfuMANIFEST; |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 474 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 475 | f_dfu->blk_seq_num = 0; |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 476 | req->complete = dnload_request_flush; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 477 | break; |
| 478 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 479 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 480 | break; |
| 481 | default: |
| 482 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 483 | value = RET_STALL; |
| 484 | break; |
| 485 | } |
| 486 | |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 487 | return value; |
| 488 | } |
| 489 | |
| 490 | static int state_dfu_manifest(struct f_dfu *f_dfu, |
| 491 | const struct usb_ctrlrequest *ctrl, |
| 492 | struct usb_gadget *gadget, |
| 493 | struct usb_request *req) |
| 494 | { |
| 495 | int value = 0; |
| 496 | |
| 497 | switch (ctrl->bRequest) { |
| 498 | case USB_REQ_DFU_GETSTATUS: |
| 499 | /* We're MainfestationTolerant */ |
| 500 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 501 | value = handle_getstatus(req); |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 502 | f_dfu->blk_seq_num = 0; |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 503 | puts("DOWNLOAD ... OK\nCtrl+C to exit ...\n"); |
| 504 | break; |
| 505 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 506 | value = handle_getstate(req); |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 507 | break; |
| 508 | default: |
| 509 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 510 | value = RET_STALL; |
| 511 | break; |
| 512 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 513 | return value; |
| 514 | } |
| 515 | |
| 516 | static int state_dfu_upload_idle(struct f_dfu *f_dfu, |
| 517 | const struct usb_ctrlrequest *ctrl, |
| 518 | struct usb_gadget *gadget, |
| 519 | struct usb_request *req) |
| 520 | { |
| 521 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 522 | u16 len = le16_to_cpu(ctrl->wLength); |
| 523 | int value = 0; |
| 524 | |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 525 | len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len; |
| 526 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 527 | switch (ctrl->bRequest) { |
| 528 | case USB_REQ_DFU_UPLOAD: |
Hugo SIMELIERE | 06ce62d | 2022-11-30 09:29:16 +0100 | [diff] [blame] | 529 | if (ctrl->bRequestType & USB_DIR_IN) { |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 530 | /* state transition if less data then requested */ |
| 531 | f_dfu->blk_seq_num = w_value; |
| 532 | value = handle_upload(req, len); |
| 533 | if (value >= 0 && value < len) |
| 534 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 535 | } |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 536 | break; |
| 537 | case USB_REQ_DFU_ABORT: |
| 538 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 539 | /* no zlp? */ |
| 540 | value = RET_ZLP; |
| 541 | break; |
| 542 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 543 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 544 | break; |
| 545 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 546 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 547 | break; |
| 548 | default: |
| 549 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 550 | value = RET_STALL; |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | return value; |
| 555 | } |
| 556 | |
| 557 | static int state_dfu_error(struct f_dfu *f_dfu, |
| 558 | const struct usb_ctrlrequest *ctrl, |
| 559 | struct usb_gadget *gadget, |
| 560 | struct usb_request *req) |
| 561 | { |
| 562 | int value = 0; |
| 563 | |
| 564 | switch (ctrl->bRequest) { |
| 565 | case USB_REQ_DFU_GETSTATUS: |
Patrick Delaunay | e652af8 | 2016-12-16 18:41:32 +0100 | [diff] [blame] | 566 | value = handle_getstatus(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 567 | break; |
| 568 | case USB_REQ_DFU_GETSTATE: |
Patrick Delaunay | 16bb408 | 2016-12-16 18:41:31 +0100 | [diff] [blame] | 569 | value = handle_getstate(req); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 570 | break; |
| 571 | case USB_REQ_DFU_CLRSTATUS: |
| 572 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 573 | f_dfu->dfu_status = DFU_STATUS_OK; |
| 574 | /* no zlp? */ |
| 575 | value = RET_ZLP; |
| 576 | break; |
| 577 | default: |
| 578 | f_dfu->dfu_state = DFU_STATE_dfuERROR; |
| 579 | value = RET_STALL; |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | return value; |
| 584 | } |
| 585 | |
| 586 | static dfu_state_fn dfu_state[] = { |
| 587 | state_app_idle, /* DFU_STATE_appIDLE */ |
| 588 | state_app_detach, /* DFU_STATE_appDETACH */ |
| 589 | state_dfu_idle, /* DFU_STATE_dfuIDLE */ |
| 590 | state_dfu_dnload_sync, /* DFU_STATE_dfuDNLOAD_SYNC */ |
| 591 | state_dfu_dnbusy, /* DFU_STATE_dfuDNBUSY */ |
| 592 | state_dfu_dnload_idle, /* DFU_STATE_dfuDNLOAD_IDLE */ |
| 593 | state_dfu_manifest_sync, /* DFU_STATE_dfuMANIFEST_SYNC */ |
Heiko Schocher | e1ba151 | 2014-03-18 08:09:56 +0100 | [diff] [blame] | 594 | state_dfu_manifest, /* DFU_STATE_dfuMANIFEST */ |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 595 | NULL, /* DFU_STATE_dfuMANIFEST_WAIT_RST */ |
| 596 | state_dfu_upload_idle, /* DFU_STATE_dfuUPLOAD_IDLE */ |
| 597 | state_dfu_error /* DFU_STATE_dfuERROR */ |
| 598 | }; |
| 599 | |
| 600 | static int |
| 601 | dfu_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 602 | { |
| 603 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 604 | struct usb_request *req = f->config->cdev->req; |
| 605 | struct f_dfu *f_dfu = f->config->cdev->req->context; |
| 606 | u16 len = le16_to_cpu(ctrl->wLength); |
| 607 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 608 | int value = 0; |
| 609 | u8 req_type = ctrl->bRequestType & USB_TYPE_MASK; |
| 610 | |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 611 | len = len > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : len; |
| 612 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 613 | debug("w_value: 0x%x len: 0x%x\n", w_value, len); |
| 614 | debug("req_type: 0x%x ctrl->bRequest: 0x%x f_dfu->dfu_state: 0x%x\n", |
| 615 | req_type, ctrl->bRequest, f_dfu->dfu_state); |
| 616 | |
Andy Shevchenko | f4f2db7 | 2020-01-29 17:23:14 +0200 | [diff] [blame] | 617 | #ifdef CONFIG_DFU_TIMEOUT |
| 618 | /* Forbid aborting by timeout. Next dfu command may update this */ |
| 619 | dfu_set_timeout(0); |
| 620 | #endif |
| 621 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 622 | if (req_type == USB_TYPE_STANDARD) { |
| 623 | if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR && |
| 624 | (w_value >> 8) == DFU_DT_FUNC) { |
| 625 | value = min(len, (u16) sizeof(dfu_func)); |
| 626 | memcpy(req->buf, &dfu_func, value); |
| 627 | } |
| 628 | } else /* DFU specific request */ |
| 629 | value = dfu_state[f_dfu->dfu_state] (f_dfu, ctrl, gadget, req); |
| 630 | |
| 631 | if (value >= 0) { |
Venkatesh Yadav Abbarapu | 402cbd2 | 2022-11-03 09:37:48 +0530 | [diff] [blame] | 632 | req->length = value > DFU_USB_BUFSIZ ? DFU_USB_BUFSIZ : value; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 633 | req->zero = value < len; |
| 634 | value = usb_ep_queue(gadget->ep0, req, 0); |
| 635 | if (value < 0) { |
| 636 | debug("ep_queue --> %d\n", value); |
| 637 | req->status = 0; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return value; |
| 642 | } |
| 643 | |
| 644 | /*-------------------------------------------------------------------------*/ |
| 645 | |
| 646 | static int |
| 647 | dfu_prepare_strings(struct f_dfu *f_dfu, int n) |
| 648 | { |
| 649 | struct dfu_entity *de = NULL; |
| 650 | int i = 0; |
| 651 | |
| 652 | f_dfu->strings = calloc(sizeof(struct usb_string), n + 1); |
| 653 | if (!f_dfu->strings) |
Peng Fan | 46ed88c | 2016-05-03 10:25:22 +0800 | [diff] [blame] | 654 | return -ENOMEM; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 655 | |
| 656 | for (i = 0; i < n; ++i) { |
| 657 | de = dfu_get_entity(i); |
| 658 | f_dfu->strings[i].s = de->name; |
| 659 | } |
| 660 | |
| 661 | f_dfu->strings[i].id = 0; |
| 662 | f_dfu->strings[i].s = NULL; |
| 663 | |
| 664 | return 0; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static int dfu_prepare_function(struct f_dfu *f_dfu, int n) |
| 668 | { |
| 669 | struct usb_interface_descriptor *d; |
| 670 | int i = 0; |
| 671 | |
Patrick Delaunay | bf2a415 | 2016-12-08 18:10:49 +0100 | [diff] [blame] | 672 | f_dfu->function = calloc(sizeof(struct usb_descriptor_header *), n + 2); |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 673 | if (!f_dfu->function) |
| 674 | goto enomem; |
| 675 | |
| 676 | for (i = 0; i < n; ++i) { |
| 677 | d = calloc(sizeof(*d), 1); |
| 678 | if (!d) |
| 679 | goto enomem; |
| 680 | |
| 681 | d->bLength = sizeof(*d); |
| 682 | d->bDescriptorType = USB_DT_INTERFACE; |
| 683 | d->bAlternateSetting = i; |
| 684 | d->bNumEndpoints = 0; |
| 685 | d->bInterfaceClass = USB_CLASS_APP_SPEC; |
| 686 | d->bInterfaceSubClass = 1; |
| 687 | d->bInterfaceProtocol = 2; |
| 688 | |
| 689 | f_dfu->function[i] = (struct usb_descriptor_header *)d; |
| 690 | } |
Patrick Delaunay | bf2a415 | 2016-12-08 18:10:49 +0100 | [diff] [blame] | 691 | |
| 692 | /* add DFU Functional Descriptor */ |
| 693 | f_dfu->function[i] = calloc(sizeof(dfu_func), 1); |
| 694 | if (!f_dfu->function[i]) |
| 695 | goto enomem; |
| 696 | memcpy(f_dfu->function[i], &dfu_func, sizeof(dfu_func)); |
| 697 | |
| 698 | i++; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 699 | f_dfu->function[i] = NULL; |
| 700 | |
| 701 | return 0; |
| 702 | |
| 703 | enomem: |
| 704 | while (i) { |
| 705 | free(f_dfu->function[--i]); |
| 706 | f_dfu->function[i] = NULL; |
| 707 | } |
| 708 | free(f_dfu->function); |
| 709 | |
| 710 | return -ENOMEM; |
| 711 | } |
| 712 | |
| 713 | static int dfu_bind(struct usb_configuration *c, struct usb_function *f) |
| 714 | { |
| 715 | struct usb_composite_dev *cdev = c->cdev; |
| 716 | struct f_dfu *f_dfu = func_to_dfu(f); |
Felipe Balbi | d1c34e4 | 2017-02-22 11:22:38 +0200 | [diff] [blame] | 717 | const char *s; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 718 | int alt_num = dfu_get_alt_number(); |
| 719 | int rv, id, i; |
| 720 | |
| 721 | id = usb_interface_id(c, f); |
| 722 | if (id < 0) |
| 723 | return id; |
| 724 | dfu_intf_runtime.bInterfaceNumber = id; |
| 725 | |
| 726 | f_dfu->dfu_state = DFU_STATE_appIDLE; |
| 727 | f_dfu->dfu_status = DFU_STATUS_OK; |
| 728 | |
| 729 | rv = dfu_prepare_function(f_dfu, alt_num); |
| 730 | if (rv) |
| 731 | goto error; |
| 732 | |
| 733 | rv = dfu_prepare_strings(f_dfu, alt_num); |
| 734 | if (rv) |
| 735 | goto error; |
| 736 | for (i = 0; i < alt_num; i++) { |
| 737 | id = usb_string_id(cdev); |
| 738 | if (id < 0) |
| 739 | return id; |
| 740 | f_dfu->strings[i].id = id; |
| 741 | ((struct usb_interface_descriptor *)f_dfu->function[i]) |
| 742 | ->iInterface = id; |
| 743 | } |
| 744 | |
Heiko Schocher | 5890e83 | 2013-06-25 13:59:29 +0200 | [diff] [blame] | 745 | to_dfu_mode(f_dfu); |
| 746 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 747 | stringtab_dfu.strings = f_dfu->strings; |
| 748 | |
| 749 | cdev->req->context = f_dfu; |
| 750 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 751 | s = env_get("serial#"); |
Felipe Balbi | d1c34e4 | 2017-02-22 11:22:38 +0200 | [diff] [blame] | 752 | if (s) |
| 753 | g_dnl_set_serialnumber((char *)s); |
| 754 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 755 | error: |
| 756 | return rv; |
| 757 | } |
| 758 | |
| 759 | static void dfu_unbind(struct usb_configuration *c, struct usb_function *f) |
| 760 | { |
| 761 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 762 | int alt_num = dfu_get_alt_number(); |
| 763 | int i; |
| 764 | |
| 765 | if (f_dfu->strings) { |
| 766 | i = alt_num; |
| 767 | while (i) |
| 768 | f_dfu->strings[--i].s = NULL; |
| 769 | |
| 770 | free(f_dfu->strings); |
| 771 | } |
| 772 | |
| 773 | if (f_dfu->function) { |
| 774 | i = alt_num; |
Ralph Siemsen | 4cceff6 | 2019-06-28 10:42:02 -0400 | [diff] [blame] | 775 | i++; /* free DFU Functional Descriptor */ |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 776 | while (i) { |
| 777 | free(f_dfu->function[--i]); |
| 778 | f_dfu->function[i] = NULL; |
| 779 | } |
| 780 | free(f_dfu->function); |
| 781 | } |
| 782 | |
| 783 | free(f_dfu); |
| 784 | } |
| 785 | |
| 786 | static int dfu_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 787 | { |
| 788 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 789 | |
| 790 | debug("%s: intf:%d alt:%d\n", __func__, intf, alt); |
| 791 | |
| 792 | f_dfu->altsetting = alt; |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 793 | f_dfu->dfu_state = DFU_STATE_dfuIDLE; |
| 794 | f_dfu->dfu_status = DFU_STATUS_OK; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
Stefan Roese | 5a6b873 | 2015-01-09 14:54:56 +0100 | [diff] [blame] | 799 | static int __dfu_get_alt(struct usb_function *f, unsigned intf) |
| 800 | { |
| 801 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 802 | |
| 803 | return f_dfu->altsetting; |
| 804 | } |
| 805 | |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 806 | /* TODO: is this really what we need here? */ |
| 807 | static void dfu_disable(struct usb_function *f) |
| 808 | { |
| 809 | struct f_dfu *f_dfu = func_to_dfu(f); |
| 810 | if (f_dfu->config == 0) |
| 811 | return; |
| 812 | |
| 813 | debug("%s: reset config\n", __func__); |
| 814 | |
| 815 | f_dfu->config = 0; |
| 816 | } |
| 817 | |
| 818 | static int dfu_bind_config(struct usb_configuration *c) |
| 819 | { |
| 820 | struct f_dfu *f_dfu; |
| 821 | int status; |
| 822 | |
| 823 | f_dfu = calloc(sizeof(*f_dfu), 1); |
| 824 | if (!f_dfu) |
| 825 | return -ENOMEM; |
| 826 | f_dfu->usb_function.name = "dfu"; |
| 827 | f_dfu->usb_function.hs_descriptors = dfu_runtime_descs; |
Heiko Schocher | 3e3a136 | 2014-09-10 08:55:00 +0200 | [diff] [blame] | 828 | f_dfu->usb_function.descriptors = dfu_runtime_descs; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 829 | f_dfu->usb_function.bind = dfu_bind; |
| 830 | f_dfu->usb_function.unbind = dfu_unbind; |
| 831 | f_dfu->usb_function.set_alt = dfu_set_alt; |
Stefan Roese | 5a6b873 | 2015-01-09 14:54:56 +0100 | [diff] [blame] | 832 | f_dfu->usb_function.get_alt = __dfu_get_alt; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 833 | f_dfu->usb_function.disable = dfu_disable; |
Lukasz Majewski | 1abe47d | 2013-12-09 16:20:15 +0100 | [diff] [blame] | 834 | f_dfu->usb_function.strings = dfu_generic_strings; |
| 835 | f_dfu->usb_function.setup = dfu_handle; |
Lukasz Majewski | 2bb115f | 2013-12-09 16:20:14 +0100 | [diff] [blame] | 836 | f_dfu->poll_timeout = DFU_DEFAULT_POLL_TIMEOUT; |
Lukasz Majewski | b7a15bf | 2012-08-06 14:41:06 +0200 | [diff] [blame] | 837 | |
| 838 | status = usb_add_function(c, &f_dfu->usb_function); |
| 839 | if (status) |
| 840 | free(f_dfu); |
| 841 | |
| 842 | return status; |
| 843 | } |
| 844 | |
| 845 | int dfu_add(struct usb_configuration *c) |
| 846 | { |
| 847 | int id; |
| 848 | |
| 849 | id = usb_string_id(c->cdev); |
| 850 | if (id < 0) |
| 851 | return id; |
| 852 | strings_dfu_generic[0].id = id; |
| 853 | dfu_intf_runtime.iInterface = id; |
| 854 | |
| 855 | debug("%s: cdev: 0x%p gadget:0x%p gadget->ep0: 0x%p\n", __func__, |
| 856 | c->cdev, c->cdev->gadget, c->cdev->gadget->ep0); |
| 857 | |
| 858 | return dfu_bind_config(c); |
| 859 | } |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 860 | |
| 861 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_dfu, dfu_add); |