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