linux/drivers/usb/dwc3/gadget.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
   4 *
   5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
   6 *
   7 * Authors: Felipe Balbi <balbi@ti.com>,
   8 *          Sebastian Andrzej Siewior <bigeasy@linutronix.de>
   9 */
  10
  11#include <linux/kernel.h>
  12#include <linux/delay.h>
  13#include <linux/slab.h>
  14#include <linux/spinlock.h>
  15#include <linux/platform_device.h>
  16#include <linux/pm_runtime.h>
  17#include <linux/interrupt.h>
  18#include <linux/io.h>
  19#include <linux/list.h>
  20#include <linux/dma-mapping.h>
  21
  22#include <linux/usb/ch9.h>
  23#include <linux/usb/gadget.h>
  24
  25#include "debug.h"
  26#include "core.h"
  27#include "gadget.h"
  28#include "io.h"
  29
  30#define DWC3_ALIGN_FRAME(d, n)  (((d)->frame_number + ((d)->interval * (n))) \
  31                                        & ~((d)->interval - 1))
  32
  33/**
  34 * dwc3_gadget_set_test_mode - enables usb2 test modes
  35 * @dwc: pointer to our context structure
  36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  37 *
  38 * Caller should take care of locking. This function will return 0 on
  39 * success or -EINVAL if wrong Test Selector is passed.
  40 */
  41int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  42{
  43        u32             reg;
  44
  45        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  46        reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  47
  48        switch (mode) {
  49        case USB_TEST_J:
  50        case USB_TEST_K:
  51        case USB_TEST_SE0_NAK:
  52        case USB_TEST_PACKET:
  53        case USB_TEST_FORCE_ENABLE:
  54                reg |= mode << 1;
  55                break;
  56        default:
  57                return -EINVAL;
  58        }
  59
  60        dwc3_gadget_dctl_write_safe(dwc, reg);
  61
  62        return 0;
  63}
  64
  65/**
  66 * dwc3_gadget_get_link_state - gets current state of usb link
  67 * @dwc: pointer to our context structure
  68 *
  69 * Caller should take care of locking. This function will
  70 * return the link state on success (>= 0) or -ETIMEDOUT.
  71 */
  72int dwc3_gadget_get_link_state(struct dwc3 *dwc)
  73{
  74        u32             reg;
  75
  76        reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  77
  78        return DWC3_DSTS_USBLNKST(reg);
  79}
  80
  81/**
  82 * dwc3_gadget_set_link_state - sets usb link to a particular state
  83 * @dwc: pointer to our context structure
  84 * @state: the state to put link into
  85 *
  86 * Caller should take care of locking. This function will
  87 * return 0 on success or -ETIMEDOUT.
  88 */
  89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  90{
  91        int             retries = 10000;
  92        u32             reg;
  93
  94        /*
  95         * Wait until device controller is ready. Only applies to 1.94a and
  96         * later RTL.
  97         */
  98        if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
  99                while (--retries) {
 100                        reg = dwc3_readl(dwc->regs, DWC3_DSTS);
 101                        if (reg & DWC3_DSTS_DCNRD)
 102                                udelay(5);
 103                        else
 104                                break;
 105                }
 106
 107                if (retries <= 0)
 108                        return -ETIMEDOUT;
 109        }
 110
 111        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
 112        reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
 113
 114        /* set no action before sending new link state change */
 115        dwc3_writel(dwc->regs, DWC3_DCTL, reg);
 116
 117        /* set requested state */
 118        reg |= DWC3_DCTL_ULSTCHNGREQ(state);
 119        dwc3_writel(dwc->regs, DWC3_DCTL, reg);
 120
 121        /*
 122         * The following code is racy when called from dwc3_gadget_wakeup,
 123         * and is not needed, at least on newer versions
 124         */
 125        if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
 126                return 0;
 127
 128        /* wait for a change in DSTS */
 129        retries = 10000;
 130        while (--retries) {
 131                reg = dwc3_readl(dwc->regs, DWC3_DSTS);
 132
 133                if (DWC3_DSTS_USBLNKST(reg) == state)
 134                        return 0;
 135
 136                udelay(5);
 137        }
 138
 139        return -ETIMEDOUT;
 140}
 141
 142/**
 143 * dwc3_ep_inc_trb - increment a trb index.
 144 * @index: Pointer to the TRB index to increment.
 145 *
 146 * The index should never point to the link TRB. After incrementing,
 147 * if it is point to the link TRB, wrap around to the beginning. The
 148 * link TRB is always at the last TRB entry.
 149 */
 150static void dwc3_ep_inc_trb(u8 *index)
 151{
 152        (*index)++;
 153        if (*index == (DWC3_TRB_NUM - 1))
 154                *index = 0;
 155}
 156
 157/**
 158 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
 159 * @dep: The endpoint whose enqueue pointer we're incrementing
 160 */
 161static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
 162{
 163        dwc3_ep_inc_trb(&dep->trb_enqueue);
 164}
 165
 166/**
 167 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
 168 * @dep: The endpoint whose enqueue pointer we're incrementing
 169 */
 170static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
 171{
 172        dwc3_ep_inc_trb(&dep->trb_dequeue);
 173}
 174
 175static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
 176                struct dwc3_request *req, int status)
 177{
 178        struct dwc3                     *dwc = dep->dwc;
 179
 180        list_del(&req->list);
 181        req->remaining = 0;
 182        req->needs_extra_trb = false;
 183
 184        if (req->request.status == -EINPROGRESS)
 185                req->request.status = status;
 186
 187        if (req->trb)
 188                usb_gadget_unmap_request_by_dev(dwc->sysdev,
 189                                &req->request, req->direction);
 190
 191        req->trb = NULL;
 192        trace_dwc3_gadget_giveback(req);
 193
 194        if (dep->number > 1)
 195                pm_runtime_put(dwc->dev);
 196}
 197
 198/**
 199 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
 200 * @dep: The endpoint to whom the request belongs to
 201 * @req: The request we're giving back
 202 * @status: completion code for the request
 203 *
 204 * Must be called with controller's lock held and interrupts disabled. This
 205 * function will unmap @req and call its ->complete() callback to notify upper
 206 * layers that it has completed.
 207 */
 208void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
 209                int status)
 210{
 211        struct dwc3                     *dwc = dep->dwc;
 212
 213        if (dep->stream_capable && timer_pending(&req->stream_timeout_timer))
 214                del_timer(&req->stream_timeout_timer);
 215
 216        dwc3_gadget_del_and_unmap_request(dep, req, status);
 217        req->status = DWC3_REQUEST_STATUS_COMPLETED;
 218
 219        spin_unlock(&dwc->lock);
 220        usb_gadget_giveback_request(&dep->endpoint, &req->request);
 221        spin_lock(&dwc->lock);
 222}
 223
 224/**
 225 * dwc3_send_gadget_generic_command - issue a generic command for the controller
 226 * @dwc: pointer to the controller context
 227 * @cmd: the command to be issued
 228 * @param: command parameter
 229 *
 230 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
 231 * and wait for its completion.
 232 */
 233int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
 234                u32 param)
 235{
 236        u32             timeout = 500;
 237        int             status = 0;
 238        int             ret = 0;
 239        u32             reg;
 240
 241        dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
 242        dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
 243
 244        do {
 245                reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
 246                if (!(reg & DWC3_DGCMD_CMDACT)) {
 247                        status = DWC3_DGCMD_STATUS(reg);
 248                        if (status)
 249                                ret = -EINVAL;
 250                        break;
 251                }
 252        } while (--timeout);
 253
 254        if (!timeout) {
 255                ret = -ETIMEDOUT;
 256                status = -ETIMEDOUT;
 257        }
 258
 259        trace_dwc3_gadget_generic_cmd(cmd, param, status);
 260
 261        return ret;
 262}
 263
 264static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
 265
 266/**
 267 * dwc3_send_gadget_ep_cmd - issue an endpoint command
 268 * @dep: the endpoint to which the command is going to be issued
 269 * @cmd: the command to be issued
 270 * @params: parameters to the command
 271 *
 272 * Caller should handle locking. This function will issue @cmd with given
 273 * @params to @dep and wait for its completion.
 274 */
 275int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
 276                struct dwc3_gadget_ep_cmd_params *params)
 277{
 278        const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
 279        struct dwc3             *dwc = dep->dwc;
 280        u32                     timeout = 5000;
 281        u32                     saved_config = 0;
 282        u32                     reg;
 283
 284        int                     cmd_status = 0;
 285        int                     ret = -EINVAL;
 286
 287        /*
 288         * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
 289         * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
 290         * endpoint command.
 291         *
 292         * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
 293         * settings. Restore them after the command is completed.
 294         *
 295         * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
 296         */
 297        if (dwc->gadget->speed <= USB_SPEED_HIGH) {
 298                reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
 299                if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
 300                        saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
 301                        reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
 302                }
 303
 304                if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
 305                        saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
 306                        reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
 307                }
 308
 309                if (saved_config)
 310                        dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
 311        }
 312
 313        if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
 314                int             needs_wakeup;
 315
 316                needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
 317                                dwc->link_state == DWC3_LINK_STATE_U2 ||
 318                                dwc->link_state == DWC3_LINK_STATE_U3);
 319
 320                if (unlikely(needs_wakeup)) {
 321                        ret = __dwc3_gadget_wakeup(dwc);
 322                        dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
 323                                        ret);
 324                }
 325        }
 326
 327        dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
 328        dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
 329        dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
 330
 331        /*
 332         * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
 333         * not relying on XferNotReady, we can make use of a special "No
 334         * Response Update Transfer" command where we should clear both CmdAct
 335         * and CmdIOC bits.
 336         *
 337         * With this, we don't need to wait for command completion and can
 338         * straight away issue further commands to the endpoint.
 339         *
 340         * NOTICE: We're making an assumption that control endpoints will never
 341         * make use of Update Transfer command. This is a safe assumption
 342         * because we can never have more than one request at a time with
 343         * Control Endpoints. If anybody changes that assumption, this chunk
 344         * needs to be updated accordingly.
 345         */
 346        if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
 347                        !usb_endpoint_xfer_isoc(desc))
 348                cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
 349        else
 350                cmd |= DWC3_DEPCMD_CMDACT;
 351
 352        dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
 353        do {
 354                reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
 355                if (!(reg & DWC3_DEPCMD_CMDACT)) {
 356                        cmd_status = DWC3_DEPCMD_STATUS(reg);
 357
 358                        switch (cmd_status) {
 359                        case 0:
 360                                ret = 0;
 361                                break;
 362                        case DEPEVT_TRANSFER_NO_RESOURCE:
 363                                dev_WARN(dwc->dev, "No resource for %s\n",
 364                                         dep->name);
 365                                ret = -EINVAL;
 366                                break;
 367                        case DEPEVT_TRANSFER_BUS_EXPIRY:
 368                                /*
 369                                 * SW issues START TRANSFER command to
 370                                 * isochronous ep with future frame interval. If
 371                                 * future interval time has already passed when
 372                                 * core receives the command, it will respond
 373                                 * with an error status of 'Bus Expiry'.
 374                                 *
 375                                 * Instead of always returning -EINVAL, let's
 376                                 * give a hint to the gadget driver that this is
 377                                 * the case by returning -EAGAIN.
 378                                 */
 379                                ret = -EAGAIN;
 380                                break;
 381                        default:
 382                                dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
 383                        }
 384
 385                        break;
 386                }
 387        } while (--timeout);
 388
 389        if (timeout == 0) {
 390                ret = -ETIMEDOUT;
 391                cmd_status = -ETIMEDOUT;
 392        }
 393
 394        trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
 395
 396        if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
 397                if (ret == 0)
 398                        dep->flags |= DWC3_EP_TRANSFER_STARTED;
 399
 400                if (ret != -ETIMEDOUT)
 401                        dwc3_gadget_ep_get_transfer_index(dep);
 402        }
 403
 404        if (saved_config) {
 405                reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
 406                reg |= saved_config;
 407                dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
 408        }
 409
 410        return ret;
 411}
 412
 413static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
 414{
 415        struct dwc3 *dwc = dep->dwc;
 416        struct dwc3_gadget_ep_cmd_params params;
 417        u32 cmd = DWC3_DEPCMD_CLEARSTALL;
 418
 419        /*
 420         * As of core revision 2.60a the recommended programming model
 421         * is to set the ClearPendIN bit when issuing a Clear Stall EP
 422         * command for IN endpoints. This is to prevent an issue where
 423         * some (non-compliant) hosts may not send ACK TPs for pending
 424         * IN transfers due to a mishandled error condition. Synopsys
 425         * STAR 9000614252.
 426         */
 427        if (dep->direction &&
 428            !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
 429            (dwc->gadget->speed >= USB_SPEED_SUPER))
 430                cmd |= DWC3_DEPCMD_CLEARPENDIN;
 431
 432        memset(&params, 0, sizeof(params));
 433
 434        return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
 435}
 436
 437dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
 438                struct dwc3_trb *trb)
 439{
 440        u32             offset = (char *) trb - (char *) dep->trb_pool;
 441
 442        return dep->trb_pool_dma + offset;
 443}
 444
 445static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
 446{
 447        struct dwc3             *dwc = dep->dwc;
 448
 449        if (dep->trb_pool)
 450                return 0;
 451
 452        dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
 453                        sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
 454                        &dep->trb_pool_dma, GFP_KERNEL);
 455        if (!dep->trb_pool) {
 456                dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
 457                                dep->name);
 458                return -ENOMEM;
 459        }
 460
 461        return 0;
 462}
 463
 464static void dwc3_free_trb_pool(struct dwc3_ep *dep)
 465{
 466        struct dwc3             *dwc = dep->dwc;
 467
 468        dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
 469                        dep->trb_pool, dep->trb_pool_dma);
 470
 471        dep->trb_pool = NULL;
 472        dep->trb_pool_dma = 0;
 473}
 474
 475static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
 476{
 477        struct dwc3_gadget_ep_cmd_params params;
 478
 479        memset(&params, 0x00, sizeof(params));
 480
 481        params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
 482
 483        return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
 484                        &params);
 485}
 486
 487/**
 488 * dwc3_gadget_start_config - configure ep resources
 489 * @dep: endpoint that is being enabled
 490 *
 491 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
 492 * completion, it will set Transfer Resource for all available endpoints.
 493 *
 494 * The assignment of transfer resources cannot perfectly follow the data book
 495 * due to the fact that the controller driver does not have all knowledge of the
 496 * configuration in advance. It is given this information piecemeal by the
 497 * composite gadget framework after every SET_CONFIGURATION and
 498 * SET_INTERFACE. Trying to follow the databook programming model in this
 499 * scenario can cause errors. For two reasons:
 500 *
 501 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
 502 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
 503 * incorrect in the scenario of multiple interfaces.
 504 *
 505 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
 506 * endpoint on alt setting (8.1.6).
 507 *
 508 * The following simplified method is used instead:
 509 *
 510 * All hardware endpoints can be assigned a transfer resource and this setting
 511 * will stay persistent until either a core reset or hibernation. So whenever we
 512 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
 513 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
 514 * guaranteed that there are as many transfer resources as endpoints.
 515 *
 516 * This function is called for each endpoint when it is being enabled but is
 517 * triggered only when called for EP0-out, which always happens first, and which
 518 * should only happen in one of the above conditions.
 519 */
 520static int dwc3_gadget_start_config(struct dwc3_ep *dep)
 521{
 522        struct dwc3_gadget_ep_cmd_params params;
 523        struct dwc3             *dwc;
 524        u32                     cmd;
 525        int                     i;
 526        int                     ret;
 527
 528        if (dep->number)
 529                return 0;
 530
 531        memset(&params, 0x00, sizeof(params));
 532        cmd = DWC3_DEPCMD_DEPSTARTCFG;
 533        dwc = dep->dwc;
 534
 535        ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
 536        if (ret)
 537                return ret;
 538
 539        for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
 540                dep = dwc->eps[i];
 541
 542                if (!dep)
 543                        continue;
 544
 545                ret = dwc3_gadget_set_xfer_resource(dep);
 546                if (ret)
 547                        return ret;
 548        }
 549
 550        return 0;
 551}
 552
 553static void stream_timeout_function(struct timer_list *arg)
 554{
 555        struct dwc3_request     *req = from_timer(req, arg,
 556                                                stream_timeout_timer);
 557        struct dwc3_ep          *dep = req->dep;
 558        struct dwc3             *dwc = dep->dwc;
 559        unsigned long           flags;
 560
 561        spin_lock_irqsave(&dwc->lock, flags);
 562        dwc3_stop_active_transfer(dep, true, true);
 563        __dwc3_gadget_kick_transfer(dep);
 564        spin_unlock_irqrestore(&dwc->lock, flags);
 565}
 566
 567static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
 568{
 569        const struct usb_ss_ep_comp_descriptor *comp_desc;
 570        const struct usb_endpoint_descriptor *desc;
 571        struct dwc3_gadget_ep_cmd_params params;
 572        struct dwc3 *dwc = dep->dwc;
 573
 574        comp_desc = dep->endpoint.comp_desc;
 575        desc = dep->endpoint.desc;
 576
 577        memset(&params, 0x00, sizeof(params));
 578
 579        params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
 580                | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
 581
 582        /* Burst size is only needed in SuperSpeed mode */
 583        if (dwc->gadget->speed >= USB_SPEED_SUPER) {
 584                u32 burst = dep->endpoint.maxburst;
 585
 586                params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
 587        }
 588
 589        params.param0 |= action;
 590        if (action == DWC3_DEPCFG_ACTION_RESTORE)
 591                params.param2 |= dep->saved_state;
 592
 593        if (usb_endpoint_xfer_control(desc))
 594                params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
 595
 596        if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
 597                params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
 598
 599        if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
 600                params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
 601                        | DWC3_DEPCFG_XFER_COMPLETE_EN
 602                        | DWC3_DEPCFG_STREAM_EVENT_EN;
 603                dep->stream_capable = true;
 604        }
 605
 606        if (!usb_endpoint_xfer_control(desc))
 607                params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
 608
 609        /*
 610         * We are doing 1:1 mapping for endpoints, meaning
 611         * Physical Endpoints 2 maps to Logical Endpoint 2 and
 612         * so on. We consider the direction bit as part of the physical
 613         * endpoint number. So USB endpoint 0x81 is 0x03.
 614         */
 615        params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
 616
 617        /*
 618         * We must use the lower 16 TX FIFOs even though
 619         * HW might have more
 620         */
 621        if (dep->direction)
 622                params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
 623
 624        if (desc->bInterval) {
 625                params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
 626                dep->interval = 1 << (desc->bInterval - 1);
 627        }
 628
 629        return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
 630}
 631
 632/**
 633 * __dwc3_gadget_ep_enable - initializes a hw endpoint
 634 * @dep: endpoint to be initialized
 635 * @action: one of INIT, MODIFY or RESTORE
 636 *
 637 * Caller should take care of locking. Execute all necessary commands to
 638 * initialize a HW endpoint so it can be used by a gadget driver.
 639 */
 640int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
 641{
 642        const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
 643        struct dwc3             *dwc = dep->dwc;
 644
 645        u32                     reg;
 646        int                     ret;
 647
 648        if (!(dep->flags & DWC3_EP_ENABLED) || dwc->is_hibernated) {
 649                ret = dwc3_gadget_start_config(dep);
 650                if (ret)
 651                        return ret;
 652        }
 653
 654        ret = dwc3_gadget_set_ep_config(dep, action);
 655        if (ret)
 656                return ret;
 657
 658        if (!(dep->flags & DWC3_EP_ENABLED) || dwc->is_hibernated) {
 659                struct dwc3_trb *trb_st_hw;
 660                struct dwc3_trb *trb_link;
 661
 662                dep->type = usb_endpoint_type(desc);
 663                dep->flags |= DWC3_EP_ENABLED;
 664
 665                reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
 666                reg |= DWC3_DALEPENA_EP(dep->number);
 667                dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
 668
 669                if (usb_endpoint_xfer_control(desc))
 670                        goto out;
 671
 672                if (!dwc->is_hibernated) {
 673                        /* Initialize the TRB ring */
 674                        dep->trb_dequeue = 0;
 675                        dep->trb_enqueue = 0;
 676                        memset(dep->trb_pool, 0,
 677                               sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
 678                }
 679
 680                /* Link TRB. The HWO bit is never reset */
 681                trb_st_hw = &dep->trb_pool[0];
 682
 683                trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
 684                trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
 685                trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
 686                trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
 687                trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
 688        }
 689
 690        /*
 691         * Issue StartTransfer here with no-op TRB so we can always rely on No
 692         * Response Update Transfer command.
 693         */
 694        if ((usb_endpoint_xfer_bulk(desc) || usb_endpoint_xfer_int(desc)) &&
 695             !dwc->is_hibernated) {
 696                struct dwc3_gadget_ep_cmd_params params;
 697                struct dwc3_trb *trb;
 698                dma_addr_t trb_dma;
 699                u32 cmd;
 700
 701                memset(&params, 0, sizeof(params));
 702                trb = &dep->trb_pool[0];
 703                trb_dma = dwc3_trb_dma_offset(dep, trb);
 704
 705                params.param0 = upper_32_bits(trb_dma);
 706                params.param1 = lower_32_bits(trb_dma);
 707
 708                cmd = DWC3_DEPCMD_STARTTRANSFER;
 709
 710                ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
 711                if (ret < 0)
 712                        return ret;
 713
 714                if (dep->stream_capable) {
 715                        /*
 716                         * For streams, at start, there maybe a race where the
 717                         * host primes the endpoint before the function driver
 718                         * queues a request to initiate a stream. In that case,
 719                         * the controller will not see the prime to generate the
 720                         * ERDY and start stream. To workaround this, issue a
 721                         * no-op TRB as normal, but end it immediately. As a
 722                         * result, when the function driver queues the request,
 723                         * the next START_TRANSFER command will cause the
 724                         * controller to generate an ERDY to initiate the
 725                         * stream.
 726                         */
 727                        dwc3_stop_active_transfer(dep, true, true);
 728
 729                        /*
 730                         * All stream eps will reinitiate stream on NoStream
 731                         * rejection until we can determine that the host can
 732                         * prime after the first transfer.
 733                         */
 734                        dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
 735                }
 736        }
 737
 738out:
 739        trace_dwc3_gadget_ep_enable(dep);
 740
 741        return 0;
 742}
 743
 744static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
 745{
 746        struct dwc3_request             *req;
 747
 748        dwc3_stop_active_transfer(dep, true, false);
 749
 750        /* - giveback all requests to gadget driver */
 751        while (!list_empty(&dep->started_list)) {
 752                req = next_request(&dep->started_list);
 753
 754                dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
 755        }
 756
 757        while (!list_empty(&dep->pending_list)) {
 758                req = next_request(&dep->pending_list);
 759
 760                dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
 761        }
 762
 763        while (!list_empty(&dep->cancelled_list)) {
 764                req = next_request(&dep->cancelled_list);
 765
 766                dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
 767        }
 768}
 769
 770/**
 771 * __dwc3_gadget_ep_disable - disables a hw endpoint
 772 * @dep: the endpoint to disable
 773 *
 774 * This function undoes what __dwc3_gadget_ep_enable did and also removes
 775 * requests which are currently being processed by the hardware and those which
 776 * are not yet scheduled.
 777 *
 778 * Caller should take care of locking.
 779 */
 780int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
 781{
 782        struct dwc3             *dwc = dep->dwc;
 783        u32                     reg;
 784
 785        trace_dwc3_gadget_ep_disable(dep);
 786
 787        dwc3_remove_requests(dwc, dep);
 788
 789        /* make sure HW endpoint isn't stalled */
 790        if (dep->flags & DWC3_EP_STALL)
 791                __dwc3_gadget_ep_set_halt(dep, 0, false);
 792
 793        reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
 794        reg &= ~DWC3_DALEPENA_EP(dep->number);
 795        dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
 796
 797        dep->stream_capable = false;
 798        dep->type = 0;
 799        dep->flags = 0;
 800
 801        /* Clear out the ep descriptors for non-ep0 */
 802        if (dep->number > 1) {
 803                dep->endpoint.comp_desc = NULL;
 804                dep->endpoint.desc = NULL;
 805        }
 806
 807        return 0;
 808}
 809
 810/* -------------------------------------------------------------------------- */
 811
 812static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
 813                const struct usb_endpoint_descriptor *desc)
 814{
 815        return -EINVAL;
 816}
 817
 818static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
 819{
 820        return -EINVAL;
 821}
 822
 823/* -------------------------------------------------------------------------- */
 824
 825static int dwc3_gadget_ep_enable(struct usb_ep *ep,
 826                const struct usb_endpoint_descriptor *desc)
 827{
 828        struct dwc3_ep                  *dep;
 829        struct dwc3                     *dwc;
 830        unsigned long                   flags;
 831        int                             ret;
 832
 833        if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
 834                pr_debug("dwc3: invalid parameters\n");
 835                return -EINVAL;
 836        }
 837
 838        if (!desc->wMaxPacketSize) {
 839                pr_debug("dwc3: missing wMaxPacketSize\n");
 840                return -EINVAL;
 841        }
 842
 843        dep = to_dwc3_ep(ep);
 844        dwc = dep->dwc;
 845
 846        if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
 847                                        "%s is already enabled\n",
 848                                        dep->name))
 849                return 0;
 850
 851        spin_lock_irqsave(&dwc->lock, flags);
 852        ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
 853        spin_unlock_irqrestore(&dwc->lock, flags);
 854
 855        return ret;
 856}
 857
 858static int dwc3_gadget_ep_disable(struct usb_ep *ep)
 859{
 860        struct dwc3_ep                  *dep;
 861        struct dwc3                     *dwc;
 862        unsigned long                   flags;
 863        int                             ret;
 864
 865        if (!ep) {
 866                pr_debug("dwc3: invalid parameters\n");
 867                return -EINVAL;
 868        }
 869
 870        dep = to_dwc3_ep(ep);
 871        dwc = dep->dwc;
 872
 873        if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
 874                                        "%s is already disabled\n",
 875                                        dep->name))
 876                return 0;
 877
 878        spin_lock_irqsave(&dwc->lock, flags);
 879        ret = __dwc3_gadget_ep_disable(dep);
 880        spin_unlock_irqrestore(&dwc->lock, flags);
 881
 882        return ret;
 883}
 884
 885static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
 886                gfp_t gfp_flags)
 887{
 888        struct dwc3_request             *req;
 889        struct dwc3_ep                  *dep = to_dwc3_ep(ep);
 890
 891        req = kzalloc(sizeof(*req), gfp_flags);
 892        if (!req)
 893                return NULL;
 894
 895        req->direction  = dep->direction;
 896        req->epnum      = dep->number;
 897        req->dep        = dep;
 898        req->status     = DWC3_REQUEST_STATUS_UNKNOWN;
 899
 900        trace_dwc3_alloc_request(req);
 901
 902        return &req->request;
 903}
 904
 905static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
 906                struct usb_request *request)
 907{
 908        struct dwc3_request             *req = to_dwc3_request(request);
 909
 910        trace_dwc3_free_request(req);
 911        kfree(req);
 912}
 913
 914/**
 915 * dwc3_ep_prev_trb - returns the previous TRB in the ring
 916 * @dep: The endpoint with the TRB ring
 917 * @index: The index of the current TRB in the ring
 918 *
 919 * Returns the TRB prior to the one pointed to by the index. If the
 920 * index is 0, we will wrap backwards, skip the link TRB, and return
 921 * the one just before that.
 922 */
 923static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
 924{
 925        u8 tmp = index;
 926
 927        if (!tmp)
 928                tmp = DWC3_TRB_NUM - 1;
 929
 930        return &dep->trb_pool[tmp - 1];
 931}
 932
 933static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
 934{
 935        struct dwc3_trb         *tmp;
 936        u8                      trbs_left;
 937
 938        /*
 939         * If enqueue & dequeue are equal than it is either full or empty.
 940         *
 941         * One way to know for sure is if the TRB right before us has HWO bit
 942         * set or not. If it has, then we're definitely full and can't fit any
 943         * more transfers in our ring.
 944         */
 945        if (dep->trb_enqueue == dep->trb_dequeue) {
 946                tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
 947                if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
 948                        return 0;
 949
 950                return DWC3_TRB_NUM - 1;
 951        }
 952
 953        trbs_left = dep->trb_dequeue - dep->trb_enqueue;
 954        trbs_left &= (DWC3_TRB_NUM - 1);
 955
 956        if (dep->trb_dequeue < dep->trb_enqueue)
 957                trbs_left--;
 958
 959        return trbs_left;
 960}
 961
 962static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
 963                dma_addr_t dma, unsigned int length, unsigned int chain,
 964                unsigned int node, unsigned int stream_id,
 965                unsigned int short_not_ok, unsigned int no_interrupt,
 966                unsigned int is_last, bool must_interrupt)
 967{
 968        struct dwc3             *dwc = dep->dwc;
 969        struct usb_gadget       *gadget = dwc->gadget;
 970        enum usb_device_speed   speed = gadget->speed;
 971
 972        trb->size = DWC3_TRB_SIZE_LENGTH(length);
 973        trb->bpl = lower_32_bits(dma);
 974        trb->bph = upper_32_bits(dma);
 975
 976        switch (usb_endpoint_type(dep->endpoint.desc)) {
 977        case USB_ENDPOINT_XFER_CONTROL:
 978                trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
 979                break;
 980
 981        case USB_ENDPOINT_XFER_ISOC:
 982                if (!node) {
 983                        trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
 984
 985                        /*
 986                         * USB Specification 2.0 Section 5.9.2 states that: "If
 987                         * there is only a single transaction in the microframe,
 988                         * only a DATA0 data packet PID is used.  If there are
 989                         * two transactions per microframe, DATA1 is used for
 990                         * the first transaction data packet and DATA0 is used
 991                         * for the second transaction data packet.  If there are
 992                         * three transactions per microframe, DATA2 is used for
 993                         * the first transaction data packet, DATA1 is used for
 994                         * the second, and DATA0 is used for the third."
 995                         *
 996                         * IOW, we should satisfy the following cases:
 997                         *
 998                         * 1) length <= maxpacket
 999                         *      - DATA0
1000                         *
1001                         * 2) maxpacket < length <= (2 * maxpacket)
1002                         *      - DATA1, DATA0
1003                         *
1004                         * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1005                         *      - DATA2, DATA1, DATA0
1006                         */
1007                        if (speed == USB_SPEED_HIGH) {
1008                                struct usb_ep *ep = &dep->endpoint;
1009                                unsigned int mult = 2;
1010                                unsigned int maxp = usb_endpoint_maxp(ep->desc);
1011
1012                                if (length <= (2 * maxp))
1013                                        mult--;
1014
1015                                if (length <= maxp)
1016                                        mult--;
1017
1018                                trb->size |= DWC3_TRB_SIZE_PCM1(mult);
1019                        }
1020                } else {
1021                        trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
1022                }
1023
1024                /* always enable Interrupt on Missed ISOC */
1025                trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1026                break;
1027
1028        case USB_ENDPOINT_XFER_BULK:
1029        case USB_ENDPOINT_XFER_INT:
1030                trb->ctrl = DWC3_TRBCTL_NORMAL;
1031                break;
1032        default:
1033                /*
1034                 * This is only possible with faulty memory because we
1035                 * checked it already :)
1036                 */
1037                dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1038                                usb_endpoint_type(dep->endpoint.desc));
1039        }
1040
1041        /*
1042         * Enable Continue on Short Packet
1043         * when endpoint is not a stream capable
1044         */
1045        if (usb_endpoint_dir_out(dep->endpoint.desc)) {
1046                if (!dep->stream_capable)
1047                        trb->ctrl |= DWC3_TRB_CTRL_CSP;
1048
1049                if (short_not_ok)
1050                        trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1051        }
1052
1053        if ((!no_interrupt && !chain) || must_interrupt)
1054                trb->ctrl |= DWC3_TRB_CTRL_IOC;
1055
1056        if (chain)
1057                trb->ctrl |= DWC3_TRB_CTRL_CHN;
1058        else if (dep->stream_capable && is_last)
1059                trb->ctrl |= DWC3_TRB_CTRL_LST;
1060
1061        if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1062                trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1063
1064        trb->ctrl |= DWC3_TRB_CTRL_HWO;
1065
1066        dwc3_ep_inc_enq(dep);
1067
1068        trace_dwc3_prepare_trb(dep, trb);
1069}
1070
1071/**
1072 * dwc3_prepare_one_trb - setup one TRB from one request
1073 * @dep: endpoint for which this request is prepared
1074 * @req: dwc3_request pointer
1075 * @trb_length: buffer size of the TRB
1076 * @chain: should this TRB be chained to the next?
1077 * @node: only for isochronous endpoints. First TRB needs different type.
1078 * @use_bounce_buffer: set to use bounce buffer
1079 * @must_interrupt: set to interrupt on TRB completion
1080 */
1081static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1082                struct dwc3_request *req, unsigned int trb_length,
1083                unsigned int chain, unsigned int node, bool use_bounce_buffer,
1084                bool must_interrupt)
1085{
1086        struct dwc3_trb         *trb;
1087        dma_addr_t              dma;
1088        unsigned int            stream_id = req->request.stream_id;
1089        unsigned int            short_not_ok = req->request.short_not_ok;
1090        unsigned int            no_interrupt = req->request.no_interrupt;
1091        unsigned int            is_last = req->request.is_last;
1092
1093        if (use_bounce_buffer)
1094                dma = dep->dwc->bounce_addr;
1095        else if (req->request.num_sgs > 0)
1096                dma = sg_dma_address(req->start_sg);
1097        else
1098                dma = req->request.dma;
1099
1100        trb = &dep->trb_pool[dep->trb_enqueue];
1101
1102        if (!req->trb) {
1103                dwc3_gadget_move_started_request(req);
1104                req->trb = trb;
1105                req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1106        }
1107
1108        req->num_trbs++;
1109
1110        __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
1111                        stream_id, short_not_ok, no_interrupt, is_last,
1112                        must_interrupt);
1113}
1114
1115static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1116{
1117        unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1118        unsigned int rem = req->request.length % maxp;
1119
1120        if ((req->request.length && req->request.zero && !rem &&
1121                        !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1122                        (!req->direction && rem))
1123                return true;
1124
1125        return false;
1126}
1127
1128/**
1129 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1130 * @dep: The endpoint that the request belongs to
1131 * @req: The request to prepare
1132 * @entry_length: The last SG entry size
1133 * @node: Indicates whether this is not the first entry (for isoc only)
1134 *
1135 * Return the number of TRBs prepared.
1136 */
1137static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1138                struct dwc3_request *req, unsigned int entry_length,
1139                unsigned int node)
1140{
1141        unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1142        unsigned int rem = req->request.length % maxp;
1143        unsigned int num_trbs = 1;
1144
1145        if (dwc3_needs_extra_trb(dep, req))
1146                num_trbs++;
1147
1148        if (dwc3_calc_trbs_left(dep) < num_trbs)
1149                return 0;
1150
1151        req->needs_extra_trb = num_trbs > 1;
1152
1153        /* Prepare a normal TRB */
1154        if (req->direction || req->request.length)
1155                dwc3_prepare_one_trb(dep, req, entry_length,
1156                                req->needs_extra_trb, node, false, false);
1157
1158        /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1159        if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1160                dwc3_prepare_one_trb(dep, req,
1161                                req->direction ? 0 : maxp - rem,
1162                                false, 1, true, false);
1163
1164        return num_trbs;
1165}
1166
1167static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
1168                struct dwc3_request *req)
1169{
1170        struct scatterlist *sg = req->start_sg;
1171        struct scatterlist *s;
1172        int             i;
1173        unsigned int length = req->request.length;
1174        unsigned int remaining = req->request.num_mapped_sgs
1175                - req->num_queued_sgs;
1176        unsigned int num_trbs = req->num_trbs;
1177        bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
1178
1179        /*
1180         * If we resume preparing the request, then get the remaining length of
1181         * the request and resume where we left off.
1182         */
1183        for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1184                length -= sg_dma_len(s);
1185
1186        for_each_sg(sg, s, remaining, i) {
1187                unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
1188                unsigned int trb_length;
1189                bool must_interrupt = false;
1190                bool last_sg = false;
1191
1192                trb_length = min_t(unsigned int, length, sg_dma_len(s));
1193
1194                length -= trb_length;
1195
1196                /*
1197                 * IOMMU driver is coalescing the list of sgs which shares a
1198                 * page boundary into one and giving it to USB driver. With
1199                 * this the number of sgs mapped is not equal to the number of
1200                 * sgs passed. So mark the chain bit to false if it isthe last
1201                 * mapped sg.
1202                 */
1203                if ((i == remaining - 1) || !length)
1204                        last_sg = true;
1205
1206                if (!num_trbs_left)
1207                        break;
1208
1209                if (last_sg) {
1210                        if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1211                                break;
1212                } else {
1213                        /*
1214                         * Look ahead to check if we have enough TRBs for the
1215                         * next SG entry. If not, set interrupt on this TRB to
1216                         * resume preparing the next SG entry when more TRBs are
1217                         * free.
1218                         */
1219                        if (num_trbs_left == 1 || (needs_extra_trb &&
1220                                        num_trbs_left <= 2 &&
1221                                        sg_dma_len(sg_next(s)) >= length))
1222                                must_interrupt = true;
1223
1224                        dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1225                                        must_interrupt);
1226                }
1227
1228                /*
1229                 * There can be a situation where all sgs in sglist are not
1230                 * queued because of insufficient trb number. To handle this
1231                 * case, update start_sg to next sg to be queued, so that
1232                 * we have free trbs we can continue queuing from where we
1233                 * previously stopped
1234                 */
1235                if (!last_sg)
1236                        req->start_sg = sg_next(s);
1237
1238                req->num_queued_sgs++;
1239
1240                /*
1241                 * The number of pending SG entries may not correspond to the
1242                 * number of mapped SG entries. If all the data are queued, then
1243                 * don't include unused SG entries.
1244                 */
1245                if (length == 0) {
1246                        req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1247                        break;
1248                }
1249
1250                if (must_interrupt)
1251                        break;
1252        }
1253
1254        return req->num_trbs - num_trbs;
1255}
1256
1257static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
1258                struct dwc3_request *req)
1259{
1260        return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
1261}
1262
1263/*
1264 * dwc3_prepare_trbs - setup TRBs from requests
1265 * @dep: endpoint for which requests are being prepared
1266 *
1267 * The function goes through the requests list and sets up TRBs for the
1268 * transfers. The function returns once there are no more TRBs available or
1269 * it runs out of requests.
1270 *
1271 * Returns the number of TRBs prepared or negative errno.
1272 */
1273static int dwc3_prepare_trbs(struct dwc3_ep *dep)
1274{
1275        struct dwc3_request     *req, *n;
1276        int                     ret = 0;
1277
1278        BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1279
1280        /*
1281         * We can get in a situation where there's a request in the started list
1282         * but there weren't enough TRBs to fully kick it in the first time
1283         * around, so it has been waiting for more TRBs to be freed up.
1284         *
1285         * In that case, we should check if we have a request with pending_sgs
1286         * in the started list and prepare TRBs for that request first,
1287         * otherwise we will prepare TRBs completely out of order and that will
1288         * break things.
1289         */
1290        list_for_each_entry(req, &dep->started_list, list) {
1291                if (req->num_pending_sgs > 0) {
1292                        ret = dwc3_prepare_trbs_sg(dep, req);
1293                        if (!ret || req->num_pending_sgs)
1294                                return ret;
1295                }
1296
1297                if (!dwc3_calc_trbs_left(dep))
1298                        return ret;
1299
1300                /*
1301                 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1302                 * burst capability may try to read and use TRBs beyond the
1303                 * active transfer instead of stopping.
1304                 */
1305                if (dep->stream_capable && req->request.is_last)
1306                        return ret;
1307        }
1308
1309        list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1310                struct dwc3     *dwc = dep->dwc;
1311
1312                ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1313                                                    dep->direction);
1314                if (ret)
1315                        return ret;
1316
1317                req->sg                 = req->request.sg;
1318                req->start_sg           = req->sg;
1319                req->num_queued_sgs     = 0;
1320                req->num_pending_sgs    = req->request.num_mapped_sgs;
1321
1322                if (req->num_pending_sgs > 0) {
1323                        ret = dwc3_prepare_trbs_sg(dep, req);
1324                        if (req->num_pending_sgs)
1325                                return ret;
1326                } else {
1327                        ret = dwc3_prepare_trbs_linear(dep, req);
1328                }
1329
1330                if (!ret || !dwc3_calc_trbs_left(dep))
1331                        return ret;
1332
1333                /*
1334                 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1335                 * burst capability may try to read and use TRBs beyond the
1336                 * active transfer instead of stopping.
1337                 */
1338                if (dep->stream_capable && req->request.is_last)
1339                        return ret;
1340        }
1341
1342        return ret;
1343}
1344
1345static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1346
1347int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1348{
1349        struct dwc3_gadget_ep_cmd_params params;
1350        struct dwc3_request             *req;
1351        int                             starting;
1352        int                             ret;
1353        u32                             cmd;
1354
1355        /*
1356         * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1357         * This happens when we need to stop and restart a transfer such as in
1358         * the case of reinitiating a stream or retrying an isoc transfer.
1359         */
1360        ret = dwc3_prepare_trbs(dep);
1361        if (ret < 0)
1362                return ret;
1363
1364        starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1365
1366        /*
1367         * If there's no new TRB prepared and we don't need to restart a
1368         * transfer, there's no need to update the transfer.
1369         */
1370        if (!ret && !starting)
1371                return ret;
1372
1373        req = next_request(&dep->started_list);
1374        if (!req) {
1375                dep->flags |= DWC3_EP_PENDING_REQUEST;
1376                return 0;
1377        }
1378
1379        memset(&params, 0, sizeof(params));
1380
1381        if (starting) {
1382                params.param0 = upper_32_bits(req->trb_dma);
1383                params.param1 = lower_32_bits(req->trb_dma);
1384                cmd = DWC3_DEPCMD_STARTTRANSFER;
1385
1386                if (dep->stream_capable)
1387                        cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1388
1389                if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1390                        cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1391        } else {
1392                cmd = DWC3_DEPCMD_UPDATETRANSFER |
1393                        DWC3_DEPCMD_PARAM(dep->resource_index);
1394        }
1395
1396        ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1397        if (ret < 0) {
1398                struct dwc3_request *tmp;
1399
1400                if (ret == -EAGAIN)
1401                        return ret;
1402
1403                dwc3_stop_active_transfer(dep, true, true);
1404
1405                list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1406                        dwc3_gadget_move_cancelled_request(req);
1407
1408                /* If ep isn't started, then there's no end transfer pending */
1409                if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1410                        dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1411
1412                return ret;
1413        }
1414
1415        if (dep->stream_capable && req->request.is_last)
1416                dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1417
1418        if (starting && dep->stream_capable) {
1419                req->stream_timeout_timer.expires = jiffies +
1420                                msecs_to_jiffies(STREAM_TIMEOUT_MS);
1421                mod_timer(&req->stream_timeout_timer,
1422                          req->stream_timeout_timer.expires);
1423        }
1424
1425        return 0;
1426}
1427
1428static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1429{
1430        u32                     reg;
1431
1432        reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1433        return DWC3_DSTS_SOFFN(reg);
1434}
1435
1436/**
1437 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1438 * @dep: isoc endpoint
1439 *
1440 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1441 * microframe number reported by the XferNotReady event for the future frame
1442 * number to start the isoc transfer.
1443 *
1444 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1445 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1446 * XferNotReady event are invalid. The driver uses this number to schedule the
1447 * isochronous transfer and passes it to the START TRANSFER command. Because
1448 * this number is invalid, the command may fail. If BIT[15:14] matches the
1449 * internal 16-bit microframe, the START TRANSFER command will pass and the
1450 * transfer will start at the scheduled time, if it is off by 1, the command
1451 * will still pass, but the transfer will start 2 seconds in the future. For all
1452 * other conditions, the START TRANSFER command will fail with bus-expiry.
1453 *
1454 * In order to workaround this issue, we can test for the correct combination of
1455 * BIT[15:14] by sending START TRANSFER commands with different values of
1456 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1457 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1458 * As the result, within the 4 possible combinations for BIT[15:14], there will
1459 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1460 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1461 * value is the correct combination.
1462 *
1463 * Since there are only 4 outcomes and the results are ordered, we can simply
1464 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1465 * deduce the smaller successful combination.
1466 *
1467 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1468 * of BIT[15:14]. The correct combination is as follow:
1469 *
1470 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1471 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1472 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1473 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1474 *
1475 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1476 * endpoints.
1477 */
1478static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1479{
1480        int cmd_status = 0;
1481        bool test0;
1482        bool test1;
1483
1484        while (dep->combo_num < 2) {
1485                struct dwc3_gadget_ep_cmd_params params;
1486                u32 test_frame_number;
1487                u32 cmd;
1488
1489                /*
1490                 * Check if we can start isoc transfer on the next interval or
1491                 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1492                 */
1493                test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
1494                test_frame_number |= dep->combo_num << 14;
1495                test_frame_number += max_t(u32, 4, dep->interval);
1496
1497                params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1498                params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1499                params.param2 = 0;
1500
1501                cmd = DWC3_DEPCMD_STARTTRANSFER;
1502                cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1503                cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1504
1505                /* Redo if some other failure beside bus-expiry is received */
1506                if (cmd_status && cmd_status != -EAGAIN) {
1507                        dep->start_cmd_status = 0;
1508                        dep->combo_num = 0;
1509                        return 0;
1510                }
1511
1512                /* Store the first test status */
1513                if (dep->combo_num == 0)
1514                        dep->start_cmd_status = cmd_status;
1515
1516                dep->combo_num++;
1517
1518                /*
1519                 * End the transfer if the START_TRANSFER command is successful
1520                 * to wait for the next XferNotReady to test the command again
1521                 */
1522                if (cmd_status == 0) {
1523                        dwc3_stop_active_transfer(dep, true, true);
1524                        return 0;
1525                }
1526        }
1527
1528        /* test0 and test1 are both completed at this point */
1529        test0 = (dep->start_cmd_status == 0);
1530        test1 = (cmd_status == 0);
1531
1532        if (!test0 && test1)
1533                dep->combo_num = 1;
1534        else if (!test0 && !test1)
1535                dep->combo_num = 2;
1536        else if (test0 && !test1)
1537                dep->combo_num = 3;
1538        else if (test0 && test1)
1539                dep->combo_num = 0;
1540
1541        dep->frame_number &= DWC3_FRNUMBER_MASK;
1542        dep->frame_number |= dep->combo_num << 14;
1543        dep->frame_number += max_t(u32, 4, dep->interval);
1544
1545        /* Reinitialize test variables */
1546        dep->start_cmd_status = 0;
1547        dep->combo_num = 0;
1548
1549        return __dwc3_gadget_kick_transfer(dep);
1550}
1551
1552static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1553{
1554        const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
1555        struct dwc3 *dwc = dep->dwc;
1556        int ret;
1557        int i;
1558
1559        if (list_empty(&dep->pending_list) &&
1560            list_empty(&dep->started_list)) {
1561                dep->flags |= DWC3_EP_PENDING_REQUEST;
1562                return -EAGAIN;
1563        }
1564
1565        if (!dwc->dis_start_transfer_quirk &&
1566            (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1567             DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1568                if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
1569                        return dwc3_gadget_start_isoc_quirk(dep);
1570        }
1571
1572        if (desc->bInterval <= 14 &&
1573            dwc->gadget->speed >= USB_SPEED_HIGH) {
1574                u32 frame = __dwc3_gadget_get_frame(dwc);
1575                bool rollover = frame <
1576                                (dep->frame_number & DWC3_FRNUMBER_MASK);
1577
1578                /*
1579                 * frame_number is set from XferNotReady and may be already
1580                 * out of date. DSTS only provides the lower 14 bit of the
1581                 * current frame number. So add the upper two bits of
1582                 * frame_number and handle a possible rollover.
1583                 * This will provide the correct frame_number unless more than
1584                 * rollover has happened since XferNotReady.
1585                 */
1586
1587                dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1588                                     frame;
1589                if (rollover)
1590                        dep->frame_number += BIT(14);
1591        }
1592
1593        for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1594                dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1595
1596                ret = __dwc3_gadget_kick_transfer(dep);
1597                if (ret != -EAGAIN)
1598                        break;
1599        }
1600
1601        /*
1602         * After a number of unsuccessful start attempts due to bus-expiry
1603         * status, issue END_TRANSFER command and retry on the next XferNotReady
1604         * event.
1605         */
1606        if (ret == -EAGAIN) {
1607                struct dwc3_gadget_ep_cmd_params params;
1608                u32 cmd;
1609
1610                cmd = DWC3_DEPCMD_ENDTRANSFER |
1611                        DWC3_DEPCMD_CMDIOC |
1612                        DWC3_DEPCMD_PARAM(dep->resource_index);
1613
1614                dep->resource_index = 0;
1615                memset(&params, 0, sizeof(params));
1616
1617                ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1618                if (!ret)
1619                        dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1620        }
1621
1622        return ret;
1623}
1624
1625static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc);
1626static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1627{
1628        struct dwc3             *dwc = dep->dwc;
1629
1630        if (!dep->endpoint.desc || !dwc->pullups_connected) {
1631                dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1632                                dep->name);
1633                return -ESHUTDOWN;
1634        }
1635
1636        if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1637                                &req->request, req->dep->name))
1638                return -EINVAL;
1639
1640        if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1641                                "%s: request %pK already in flight\n",
1642                                dep->name, &req->request))
1643                return -EINVAL;
1644
1645        pm_runtime_get(dwc->dev);
1646
1647        req->request.actual     = 0;
1648        req->request.status     = -EINPROGRESS;
1649
1650        if (dep->stream_capable)
1651                timer_setup(&req->stream_timeout_timer,
1652                            stream_timeout_function, 0);
1653
1654        trace_dwc3_ep_queue(req);
1655
1656        list_add_tail(&req->list, &dep->pending_list);
1657        req->status = DWC3_REQUEST_STATUS_QUEUED;
1658
1659        if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1660                return 0;
1661
1662        /*
1663         * Start the transfer only after the END_TRANSFER is completed
1664         * and endpoint STALL is cleared.
1665         */
1666        if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1667            (dep->flags & DWC3_EP_WEDGE) ||
1668            (dep->flags & DWC3_EP_STALL)) {
1669                dep->flags |= DWC3_EP_DELAY_START;
1670                return 0;
1671        }
1672
1673        /* If core is hibernated, need to wakeup (remote wakeup) */
1674        if (dwc->is_hibernated) {
1675                dwc->force_hiber_wake = true;
1676                dwc3_gadget_exit_hibernation(dwc);
1677                dwc->force_hiber_wake = false;
1678        }
1679
1680        /*
1681         * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1682         * wait for a XferNotReady event so we will know what's the current
1683         * (micro-)frame number.
1684         *
1685         * Without this trick, we are very, very likely gonna get Bus Expiry
1686         * errors which will force us issue EndTransfer command.
1687         */
1688        if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1689                if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1690                                !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1691                        return 0;
1692
1693                if (dep->flags & DWC3_EP_PENDING_REQUEST) {
1694                        if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1695                                /*
1696                                 * If there are not entries in request list
1697                                 * then PENDING flag would be set, so that END
1698                                 * TRANSFER is issued when an entry is added
1699                                 * into request list.
1700                                 */
1701                                dwc3_stop_active_transfer(dep, true, true);
1702                                dep->flags = DWC3_EP_ENABLED;
1703                        }
1704
1705                        /* Rest is taken care by DWC3_DEPEVT_XFERNOTREADY */
1706                        return 0;
1707                }
1708        }
1709
1710        return __dwc3_gadget_kick_transfer(dep);
1711}
1712
1713static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1714        gfp_t gfp_flags)
1715{
1716        struct dwc3_request             *req = to_dwc3_request(request);
1717        struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1718        struct dwc3                     *dwc = dep->dwc;
1719
1720        unsigned long                   flags;
1721
1722        int                             ret;
1723
1724        spin_lock_irqsave(&dwc->lock, flags);
1725        ret = __dwc3_gadget_ep_queue(dep, req);
1726        spin_unlock_irqrestore(&dwc->lock, flags);
1727
1728        return ret;
1729}
1730
1731static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1732{
1733        int i;
1734
1735        /* If req->trb is not set, then the request has not started */
1736        if (!req->trb)
1737                return;
1738
1739        /*
1740         * If request was already started, this means we had to
1741         * stop the transfer. With that we also need to ignore
1742         * all TRBs used by the request, however TRBs can only
1743         * be modified after completion of END_TRANSFER
1744         * command. So what we do here is that we wait for
1745         * END_TRANSFER completion and only after that, we jump
1746         * over TRBs by clearing HWO and incrementing dequeue
1747         * pointer.
1748         */
1749        for (i = 0; i < req->num_trbs; i++) {
1750                struct dwc3_trb *trb;
1751
1752                trb = &dep->trb_pool[dep->trb_dequeue];
1753                trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1754                dwc3_ep_inc_deq(dep);
1755        }
1756
1757        req->num_trbs = 0;
1758}
1759
1760static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1761{
1762        struct dwc3_request             *req;
1763        struct dwc3_request             *tmp;
1764
1765        list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1766                dwc3_gadget_ep_skip_trbs(dep, req);
1767                dwc3_gadget_giveback(dep, req, -ECONNRESET);
1768        }
1769}
1770
1771static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1772                struct usb_request *request)
1773{
1774        struct dwc3_request             *req = to_dwc3_request(request);
1775        struct dwc3_request             *r = NULL;
1776
1777        struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1778        struct dwc3                     *dwc = dep->dwc;
1779
1780        unsigned long                   flags;
1781        int                             ret = 0;
1782
1783        trace_dwc3_ep_dequeue(req);
1784
1785        spin_lock_irqsave(&dwc->lock, flags);
1786
1787        if (dep->stream_capable && timer_pending(&req->stream_timeout_timer))
1788                del_timer(&req->stream_timeout_timer);
1789
1790        list_for_each_entry(r, &dep->cancelled_list, list) {
1791                if (r == req)
1792                        goto out;
1793        }
1794
1795        list_for_each_entry(r, &dep->pending_list, list) {
1796                if (r == req) {
1797                        dwc3_gadget_giveback(dep, req, -ECONNRESET);
1798                        goto out;
1799                }
1800        }
1801
1802        list_for_each_entry(r, &dep->started_list, list) {
1803                if (r == req) {
1804                        struct dwc3_request *t;
1805
1806                        /* wait until it is processed */
1807                        dwc3_stop_active_transfer(dep, true, true);
1808
1809                        /*
1810                         * Remove any started request if the transfer is
1811                         * cancelled.
1812                         */
1813                        list_for_each_entry_safe(r, t, &dep->started_list, list)
1814                                dwc3_gadget_move_cancelled_request(r);
1815
1816                        goto out;
1817                }
1818        }
1819
1820        dev_err(dwc->dev, "request %pK was not queued to %s\n",
1821                request, ep->name);
1822        ret = -EINVAL;
1823out:
1824        spin_unlock_irqrestore(&dwc->lock, flags);
1825
1826        return ret;
1827}
1828
1829int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1830{
1831        struct dwc3_gadget_ep_cmd_params        params;
1832        struct dwc3                             *dwc = dep->dwc;
1833        struct dwc3_request                     *req;
1834        struct dwc3_request                     *tmp;
1835        int                                     ret;
1836
1837        if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1838                dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1839                return -EINVAL;
1840        }
1841
1842        memset(&params, 0x00, sizeof(params));
1843
1844        if (value) {
1845                struct dwc3_trb *trb;
1846
1847                unsigned int transfer_in_flight;
1848                unsigned int started;
1849
1850                if (dep->number > 1)
1851                        trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1852                else
1853                        trb = &dwc->ep0_trb[dep->trb_enqueue];
1854
1855                transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1856                started = !list_empty(&dep->started_list);
1857
1858                if (!protocol && ((dep->direction && transfer_in_flight) ||
1859                                (!dep->direction && started))) {
1860                        return -EAGAIN;
1861                }
1862
1863                ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1864                                &params);
1865                if (ret)
1866                        dev_err(dwc->dev, "failed to set STALL on %s\n",
1867                                        dep->name);
1868                else
1869                        dep->flags |= DWC3_EP_STALL;
1870        } else {
1871                /*
1872                 * Don't issue CLEAR_STALL command to control endpoints. The
1873                 * controller automatically clears the STALL when it receives
1874                 * the SETUP token.
1875                 */
1876                if (dep->number <= 1) {
1877                        dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1878                        return 0;
1879                }
1880
1881                dwc3_stop_active_transfer(dep, true, true);
1882
1883                list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1884                        dwc3_gadget_move_cancelled_request(req);
1885
1886                if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1887                        dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1888                        return 0;
1889                }
1890
1891                dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1892
1893                ret = dwc3_send_clear_stall_ep_cmd(dep);
1894                if (ret) {
1895                        dev_err(dwc->dev, "failed to clear STALL on %s\n",
1896                                        dep->name);
1897                        return ret;
1898                }
1899
1900                dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1901
1902                if ((dep->flags & DWC3_EP_DELAY_START) &&
1903                    !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1904                        __dwc3_gadget_kick_transfer(dep);
1905
1906                dep->flags &= ~DWC3_EP_DELAY_START;
1907        }
1908
1909        return ret;
1910}
1911
1912static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1913{
1914        struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1915        struct dwc3                     *dwc = dep->dwc;
1916
1917        unsigned long                   flags;
1918
1919        int                             ret;
1920
1921        spin_lock_irqsave(&dwc->lock, flags);
1922        ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1923        spin_unlock_irqrestore(&dwc->lock, flags);
1924
1925        return ret;
1926}
1927
1928static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1929{
1930        struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1931        struct dwc3                     *dwc = dep->dwc;
1932        unsigned long                   flags;
1933        int                             ret;
1934
1935        spin_lock_irqsave(&dwc->lock, flags);
1936        dep->flags |= DWC3_EP_WEDGE;
1937
1938        if (dep->number == 0 || dep->number == 1)
1939                ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1940        else
1941                ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1942        spin_unlock_irqrestore(&dwc->lock, flags);
1943
1944        return ret;
1945}
1946
1947/* -------------------------------------------------------------------------- */
1948
1949static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1950        .bLength        = USB_DT_ENDPOINT_SIZE,
1951        .bDescriptorType = USB_DT_ENDPOINT,
1952        .bmAttributes   = USB_ENDPOINT_XFER_CONTROL,
1953};
1954
1955static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1956        .enable         = dwc3_gadget_ep0_enable,
1957        .disable        = dwc3_gadget_ep0_disable,
1958        .alloc_request  = dwc3_gadget_ep_alloc_request,
1959        .free_request   = dwc3_gadget_ep_free_request,
1960        .queue          = dwc3_gadget_ep0_queue,
1961        .dequeue        = dwc3_gadget_ep_dequeue,
1962        .set_halt       = dwc3_gadget_ep0_set_halt,
1963        .set_wedge      = dwc3_gadget_ep_set_wedge,
1964};
1965
1966static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1967        .enable         = dwc3_gadget_ep_enable,
1968        .disable        = dwc3_gadget_ep_disable,
1969        .alloc_request  = dwc3_gadget_ep_alloc_request,
1970        .free_request   = dwc3_gadget_ep_free_request,
1971        .queue          = dwc3_gadget_ep_queue,
1972        .dequeue        = dwc3_gadget_ep_dequeue,
1973        .set_halt       = dwc3_gadget_ep_set_halt,
1974        .set_wedge      = dwc3_gadget_ep_set_wedge,
1975};
1976
1977/* -------------------------------------------------------------------------- */
1978
1979static int dwc3_gadget_get_frame(struct usb_gadget *g)
1980{
1981        struct dwc3             *dwc = gadget_to_dwc(g);
1982
1983        return __dwc3_gadget_get_frame(dwc);
1984}
1985
1986static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1987{
1988        int                     retries;
1989
1990        int                     ret;
1991        u32                     reg;
1992
1993        u8                      link_state;
1994
1995        /*
1996         * According to the Databook Remote wakeup request should
1997         * be issued only when the device is in early suspend state.
1998         *
1999         * We can check that via USB Link State bits in DSTS register.
2000         */
2001        reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2002
2003        link_state = DWC3_DSTS_USBLNKST(reg);
2004
2005        switch (link_state) {
2006        case DWC3_LINK_STATE_RESET:
2007        case DWC3_LINK_STATE_RX_DET:    /* in HS, means Early Suspend */
2008        case DWC3_LINK_STATE_U3:        /* in HS, means SUSPEND */
2009        case DWC3_LINK_STATE_RESUME:
2010                break;
2011        default:
2012                return -EINVAL;
2013        }
2014
2015        ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2016        if (ret < 0) {
2017                dev_err(dwc->dev, "failed to put link in Recovery\n");
2018                return ret;
2019        }
2020
2021        /* Recent versions do this automatically */
2022        if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
2023                /* write zeroes to Link Change Request */
2024                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2025                reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2026                dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2027        }
2028
2029        /* poll until Link State changes to ON */
2030        retries = 20000;
2031
2032        while (retries--) {
2033                reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2034
2035                /* in HS, means ON */
2036                if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2037                        break;
2038        }
2039
2040        if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2041                dev_err(dwc->dev, "failed to send remote wakeup\n");
2042                return -EINVAL;
2043        }
2044
2045        return 0;
2046}
2047
2048static int dwc3_gadget_wakeup(struct usb_gadget *g)
2049{
2050        struct dwc3             *dwc = gadget_to_dwc(g);
2051        unsigned long           flags;
2052        int                     ret;
2053
2054        spin_lock_irqsave(&dwc->lock, flags);
2055        ret = __dwc3_gadget_wakeup(dwc);
2056        spin_unlock_irqrestore(&dwc->lock, flags);
2057
2058        return ret;
2059}
2060
2061static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2062                int is_selfpowered)
2063{
2064        struct dwc3             *dwc = gadget_to_dwc(g);
2065        unsigned long           flags;
2066
2067        spin_lock_irqsave(&dwc->lock, flags);
2068        g->is_selfpowered = !!is_selfpowered;
2069        spin_unlock_irqrestore(&dwc->lock, flags);
2070
2071        return 0;
2072}
2073
2074static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2075{
2076        u32 epnum;
2077
2078        for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2079                struct dwc3_ep *dep;
2080
2081                dep = dwc->eps[epnum];
2082                if (!dep)
2083                        continue;
2084
2085                dwc3_remove_requests(dwc, dep);
2086        }
2087}
2088
2089int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
2090{
2091        u32                     reg;
2092        u32                     timeout = 500;
2093
2094        if (pm_runtime_suspended(dwc->dev))
2095                return 0;
2096
2097        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2098        if (is_on) {
2099                if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
2100                        reg &= ~DWC3_DCTL_TRGTULST_MASK;
2101                        reg |= DWC3_DCTL_TRGTULST_RX_DET;
2102                }
2103
2104                if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
2105                        reg &= ~DWC3_DCTL_KEEP_CONNECT;
2106                reg |= DWC3_DCTL_RUN_STOP;
2107
2108                if (dwc->has_hibernation)
2109                        reg |= DWC3_DCTL_KEEP_CONNECT;
2110
2111                dwc->pullups_connected = true;
2112        } else {
2113                reg &= ~DWC3_DCTL_RUN_STOP;
2114
2115                if (dwc->has_hibernation && !suspend)
2116                        reg &= ~DWC3_DCTL_KEEP_CONNECT;
2117
2118                dwc->pullups_connected = false;
2119        }
2120
2121        dwc3_gadget_dctl_write_safe(dwc, reg);
2122
2123        do {
2124                reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2125                reg &= DWC3_DSTS_DEVCTRLHLT;
2126        } while (--timeout && !(!is_on ^ !reg));
2127
2128        if (!timeout)
2129                return -ETIMEDOUT;
2130
2131        return 0;
2132}
2133
2134void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2135static void __dwc3_gadget_stop(struct dwc3 *dwc);
2136
2137static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2138{
2139        struct dwc3             *dwc = gadget_to_dwc(g);
2140        unsigned long           flags;
2141        int                     ret;
2142
2143        is_on = !!is_on;
2144
2145        /*
2146         * Per databook, when we want to stop the gadget, if a control transfer
2147         * is still in process, complete it and get the core into setup phase.
2148         */
2149        if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2150                reinit_completion(&dwc->ep0_in_setup);
2151
2152                ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2153                                msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2154                if (ret == 0) {
2155                        dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2156                        return -ETIMEDOUT;
2157                }
2158        }
2159
2160        /*
2161         * Synchronize any pending event handling before executing the controller
2162         * halt routine.
2163         */
2164        if (!is_on) {
2165                dwc3_gadget_disable_irq(dwc);
2166                synchronize_irq(dwc->irq_gadget);
2167        }
2168
2169        spin_lock_irqsave(&dwc->lock, flags);
2170
2171        if (!is_on) {
2172                u32 count;
2173
2174                /*
2175                 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2176                 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2177                 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2178                 * command for any active transfers" before clearing the RunStop
2179                 * bit.
2180                 */
2181                dwc3_stop_active_transfers(dwc);
2182                __dwc3_gadget_stop(dwc);
2183
2184                /*
2185                 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2186                 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2187                 * "software needs to acknowledge the events that are generated
2188                 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2189                 * to be set to '1'."
2190                 */
2191                count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2192                count &= DWC3_GEVNTCOUNT_MASK;
2193                if (count > 0) {
2194                        dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2195                        dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2196                                                dwc->ev_buf->length;
2197                }
2198        }
2199
2200        ret = dwc3_gadget_run_stop(dwc, is_on, false);
2201        spin_unlock_irqrestore(&dwc->lock, flags);
2202
2203        return ret;
2204}
2205
2206void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2207{
2208        u32                     reg;
2209
2210        /* Enable all but Start and End of Frame IRQs */
2211        reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2212                        DWC3_DEVTEN_EVNTOVERFLOWEN |
2213                        DWC3_DEVTEN_CMDCMPLTEN |
2214                        DWC3_DEVTEN_ERRTICERREN |
2215                        DWC3_DEVTEN_WKUPEVTEN |
2216                        DWC3_DEVTEN_CONNECTDONEEN |
2217                        DWC3_DEVTEN_USBRSTEN |
2218                        DWC3_DEVTEN_DISCONNEVTEN);
2219
2220        /* Enable hibernation IRQ */
2221        if (dwc->has_hibernation)
2222                reg |= DWC3_DEVTEN_HIBERNATIONREQEVTEN;
2223
2224        if (DWC3_VER_IS_PRIOR(DWC3, 250A))
2225                reg |= DWC3_DEVTEN_ULSTCNGEN;
2226
2227        dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2228}
2229
2230void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2231{
2232        /* mask all interrupts */
2233        dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2234}
2235
2236static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
2237static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
2238
2239/**
2240 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2241 * @dwc: pointer to our context structure
2242 *
2243 * The following looks like complex but it's actually very simple. In order to
2244 * calculate the number of packets we can burst at once on OUT transfers, we're
2245 * gonna use RxFIFO size.
2246 *
2247 * To calculate RxFIFO size we need two numbers:
2248 * MDWIDTH = size, in bits, of the internal memory bus
2249 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2250 *
2251 * Given these two numbers, the formula is simple:
2252 *
2253 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2254 *
2255 * 24 bytes is for 3x SETUP packets
2256 * 16 bytes is a clock domain crossing tolerance
2257 *
2258 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2259 */
2260static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2261{
2262        u32 ram2_depth;
2263        u32 mdwidth;
2264        u32 nump;
2265        u32 reg;
2266
2267        ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2268        mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2269        if (DWC3_IP_IS(DWC32))
2270                mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2271
2272        nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2273        nump = min_t(u32, nump, 16);
2274
2275        /* update NumP */
2276        reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2277        reg &= ~DWC3_DCFG_NUMP_MASK;
2278        reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2279        dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2280}
2281
2282static int __dwc3_gadget_start(struct dwc3 *dwc)
2283{
2284        struct dwc3_ep          *dep;
2285        int                     ret = 0;
2286        u32                     reg;
2287
2288        /*
2289         * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2290         * the core supports IMOD, disable it.
2291         */
2292        if (dwc->imod_interval) {
2293                dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2294                dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2295        } else if (dwc3_has_imod(dwc)) {
2296                dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2297        }
2298
2299        /*
2300         * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2301         * field instead of letting dwc3 itself calculate that automatically.
2302         *
2303         * This way, we maximize the chances that we'll be able to get several
2304         * bursts of data without going through any sort of endpoint throttling.
2305         */
2306        reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2307        if (DWC3_IP_IS(DWC3))
2308                reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2309        else
2310                reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2311
2312        dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2313
2314        dwc3_gadget_setup_nump(dwc);
2315
2316        /* For OTG mode, check if the core is currently in Host mode.
2317         * This is not an error condition as there are times when the core is
2318         * working as host and kernel is told to initiate bind operation with
2319         * gadget class driver module.
2320         * The below remaining operations are handled in OTG driver whenever
2321         * required.
2322         */
2323        if (dwc3_readl(dwc->regs, DWC3_GSTS) & DWC3_GSTS_CUR_MODE)
2324                return 0;
2325
2326        /* Start with SuperSpeed Default */
2327        dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2328
2329        dep = dwc->eps[0];
2330        ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2331        if (ret) {
2332                dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2333                goto err0;
2334        }
2335
2336        dep = dwc->eps[1];
2337        ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2338        if (ret) {
2339                dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2340                goto err1;
2341        }
2342
2343        /* begin to receive SETUP packets */
2344        dwc->ep0state = EP0_SETUP_PHASE;
2345        dwc->link_state = DWC3_LINK_STATE_SS_DIS;
2346        dwc3_ep0_out_start(dwc);
2347
2348        dwc3_gadget_enable_irq(dwc);
2349
2350        return 0;
2351
2352err1:
2353        __dwc3_gadget_ep_disable(dwc->eps[0]);
2354
2355err0:
2356        return ret;
2357}
2358
2359static irqreturn_t dwc3_wakeup_interrupt(int irq, void *_dwc);
2360static int dwc3_gadget_start(struct usb_gadget *g,
2361                struct usb_gadget_driver *driver)
2362{
2363        struct dwc3             *dwc = gadget_to_dwc(g);
2364        unsigned long           flags;
2365        int                     ret = 0;
2366        int                     irq;
2367
2368        irq = dwc->irq_gadget;
2369        ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2370                        IRQF_SHARED, "dwc3", dwc->ev_buf);
2371        if (ret) {
2372                dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2373                                irq, ret);
2374                goto err0;
2375        }
2376
2377        /* Look for wakeup interrupt if hibernation is supported */
2378        if (dwc->has_hibernation) {
2379                irq = dwc->irq_wakeup;
2380                ret = devm_request_irq(dwc->dev, irq, dwc3_wakeup_interrupt,
2381                                       IRQF_SHARED, "usb-wakeup", dwc);
2382
2383                if (ret) {
2384                        dev_err(dwc->dev, "failed to request wakeup irq #%d --> %d\n",
2385                                irq, ret);
2386                        goto err0;
2387                }
2388        }
2389
2390        spin_lock_irqsave(&dwc->lock, flags);
2391        if (dwc->gadget_driver) {
2392                dev_err(dwc->dev, "%s is already bound to %s\n",
2393                                dwc->gadget->name,
2394                                dwc->gadget_driver->driver.name);
2395                ret = -EBUSY;
2396                goto err1;
2397        }
2398
2399        dwc->gadget_driver      = driver;
2400
2401        if (pm_runtime_active(dwc->dev))
2402                __dwc3_gadget_start(dwc);
2403
2404        spin_unlock_irqrestore(&dwc->lock, flags);
2405
2406        return 0;
2407
2408err1:
2409        spin_unlock_irqrestore(&dwc->lock, flags);
2410        free_irq(dwc->irq_gadget, dwc);
2411
2412err0:
2413        return ret;
2414}
2415
2416static void __dwc3_gadget_stop(struct dwc3 *dwc)
2417{
2418        dwc3_gadget_disable_irq(dwc);
2419        __dwc3_gadget_ep_disable(dwc->eps[0]);
2420        __dwc3_gadget_ep_disable(dwc->eps[1]);
2421}
2422
2423static int dwc3_gadget_stop(struct usb_gadget *g)
2424{
2425        struct dwc3             *dwc = gadget_to_dwc(g);
2426        unsigned long           flags;
2427
2428        spin_lock_irqsave(&dwc->lock, flags);
2429
2430        if (pm_runtime_suspended(dwc->dev))
2431                goto out;
2432
2433        __dwc3_gadget_stop(dwc);
2434
2435out:
2436        dwc->gadget_driver      = NULL;
2437        spin_unlock_irqrestore(&dwc->lock, flags);
2438
2439        free_irq(dwc->irq_gadget, dwc->ev_buf);
2440
2441        return 0;
2442}
2443
2444static void dwc3_gadget_config_params(struct usb_gadget *g,
2445                                      struct usb_dcd_config_params *params)
2446{
2447        struct dwc3             *dwc = gadget_to_dwc(g);
2448
2449        params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2450        params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2451
2452        /* Recommended BESL */
2453        if (!dwc->dis_enblslpm_quirk) {
2454                /*
2455                 * If the recommended BESL baseline is 0 or if the BESL deep is
2456                 * less than 2, Microsoft's Windows 10 host usb stack will issue
2457                 * a usb reset immediately after it receives the extended BOS
2458                 * descriptor and the enumeration will fail. To maintain
2459                 * compatibility with the Windows' usb stack, let's set the
2460                 * recommended BESL baseline to 1 and clamp the BESL deep to be
2461                 * within 2 to 15.
2462                 */
2463                params->besl_baseline = 1;
2464                if (dwc->is_utmi_l1_suspend)
2465                        params->besl_deep =
2466                                clamp_t(u8, dwc->hird_threshold, 2, 15);
2467        }
2468
2469        /* U1 Device exit Latency */
2470        if (dwc->dis_u1_entry_quirk)
2471                params->bU1devExitLat = 0;
2472        else
2473                params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2474
2475        /* U2 Device exit Latency */
2476        if (dwc->dis_u2_entry_quirk)
2477                params->bU2DevExitLat = 0;
2478        else
2479                params->bU2DevExitLat =
2480                                cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2481}
2482
2483static void dwc3_gadget_set_speed(struct usb_gadget *g,
2484                                  enum usb_device_speed speed)
2485{
2486        struct dwc3             *dwc = gadget_to_dwc(g);
2487        unsigned long           flags;
2488        u32                     reg;
2489
2490        spin_lock_irqsave(&dwc->lock, flags);
2491        reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2492        reg &= ~(DWC3_DCFG_SPEED_MASK);
2493
2494        /*
2495         * WORKAROUND: DWC3 revision < 2.20a have an issue
2496         * which would cause metastability state on Run/Stop
2497         * bit if we try to force the IP to USB2-only mode.
2498         *
2499         * Because of that, we cannot configure the IP to any
2500         * speed other than the SuperSpeed
2501         *
2502         * Refers to:
2503         *
2504         * STAR#9000525659: Clock Domain Crossing on DCTL in
2505         * USB 2.0 Mode
2506         */
2507        if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2508            !dwc->dis_metastability_quirk) {
2509                reg |= DWC3_DCFG_SUPERSPEED;
2510        } else {
2511                switch (speed) {
2512                case USB_SPEED_LOW:
2513                        reg |= DWC3_DCFG_LOWSPEED;
2514                        break;
2515                case USB_SPEED_FULL:
2516                        reg |= DWC3_DCFG_FULLSPEED;
2517                        break;
2518                case USB_SPEED_HIGH:
2519                        reg |= DWC3_DCFG_HIGHSPEED;
2520                        break;
2521                case USB_SPEED_SUPER:
2522                        reg |= DWC3_DCFG_SUPERSPEED;
2523                        break;
2524                case USB_SPEED_SUPER_PLUS:
2525                        if (DWC3_IP_IS(DWC3))
2526                                reg |= DWC3_DCFG_SUPERSPEED;
2527                        else
2528                                reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2529                        break;
2530                default:
2531                        dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2532
2533                        if (DWC3_IP_IS(DWC3))
2534                                reg |= DWC3_DCFG_SUPERSPEED;
2535                        else
2536                                reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2537                }
2538        }
2539        dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2540
2541        spin_unlock_irqrestore(&dwc->lock, flags);
2542}
2543
2544static const struct usb_gadget_ops dwc3_gadget_ops = {
2545        .get_frame              = dwc3_gadget_get_frame,
2546        .wakeup                 = dwc3_gadget_wakeup,
2547        .set_selfpowered        = dwc3_gadget_set_selfpowered,
2548        .pullup                 = dwc3_gadget_pullup,
2549        .udc_start              = dwc3_gadget_start,
2550        .udc_stop               = dwc3_gadget_stop,
2551        .udc_set_speed          = dwc3_gadget_set_speed,
2552        .get_config_params      = dwc3_gadget_config_params,
2553};
2554
2555/* -------------------------------------------------------------------------- */
2556
2557static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2558{
2559        struct dwc3 *dwc = dep->dwc;
2560
2561        usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2562        dep->endpoint.maxburst = 1;
2563        dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2564        if (!dep->direction)
2565                dwc->gadget->ep0 = &dep->endpoint;
2566
2567        dep->endpoint.caps.type_control = true;
2568
2569        return 0;
2570}
2571
2572static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2573{
2574        struct dwc3 *dwc = dep->dwc;
2575        int mdwidth;
2576        int size;
2577
2578        mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2579        if (DWC3_IP_IS(DWC32))
2580                mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2581
2582        /* MDWIDTH is represented in bits, we need it in bytes */
2583        mdwidth /= 8;
2584
2585        size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2586        if (DWC3_IP_IS(DWC3))
2587                size = DWC3_GTXFIFOSIZ_TXFDEP(size);
2588        else
2589                size = DWC31_GTXFIFOSIZ_TXFDEP(size);
2590
2591        /* FIFO Depth is in MDWDITH bytes. Multiply */
2592        size *= mdwidth;
2593
2594        /*
2595         * To meet performance requirement, a minimum TxFIFO size of 3x
2596         * MaxPacketSize is recommended for endpoints that support burst and a
2597         * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2598         * support burst. Use those numbers and we can calculate the max packet
2599         * limit as below.
2600         */
2601        if (dwc->maximum_speed >= USB_SPEED_SUPER)
2602                size /= 3;
2603        else
2604                size /= 2;
2605
2606        usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2607
2608        dep->endpoint.max_streams = 16;
2609        dep->endpoint.ops = &dwc3_gadget_ep_ops;
2610        list_add_tail(&dep->endpoint.ep_list,
2611                        &dwc->gadget->ep_list);
2612        dep->endpoint.caps.type_iso = true;
2613        dep->endpoint.caps.type_bulk = true;
2614        dep->endpoint.caps.type_int = true;
2615
2616        return dwc3_alloc_trb_pool(dep);
2617}
2618
2619static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2620{
2621        struct dwc3 *dwc = dep->dwc;
2622        int mdwidth;
2623        int size;
2624
2625        mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2626        if (DWC3_IP_IS(DWC32))
2627                mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2628
2629        /* MDWIDTH is represented in bits, convert to bytes */
2630        mdwidth /= 8;
2631
2632        /* All OUT endpoints share a single RxFIFO space */
2633        size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2634        if (DWC3_IP_IS(DWC3))
2635                size = DWC3_GRXFIFOSIZ_RXFDEP(size);
2636        else
2637                size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2638
2639        /* FIFO depth is in MDWDITH bytes */
2640        size *= mdwidth;
2641
2642        /*
2643         * To meet performance requirement, a minimum recommended RxFIFO size
2644         * is defined as follow:
2645         * RxFIFO size >= (3 x MaxPacketSize) +
2646         * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2647         *
2648         * Then calculate the max packet limit as below.
2649         */
2650        size -= (3 * 8) + 16;
2651        if (size < 0)
2652                size = 0;
2653        else
2654                size /= 3;
2655
2656        usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2657        dep->endpoint.max_streams = 16;
2658        dep->endpoint.ops = &dwc3_gadget_ep_ops;
2659        list_add_tail(&dep->endpoint.ep_list,
2660                        &dwc->gadget->ep_list);
2661        dep->endpoint.caps.type_iso = true;
2662        dep->endpoint.caps.type_bulk = true;
2663        dep->endpoint.caps.type_int = true;
2664
2665        return dwc3_alloc_trb_pool(dep);
2666}
2667
2668static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2669{
2670        struct dwc3_ep                  *dep;
2671        bool                            direction = epnum & 1;
2672        int                             ret;
2673        u8                              num = epnum >> 1;
2674
2675        dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2676        if (!dep)
2677                return -ENOMEM;
2678
2679        dep->dwc = dwc;
2680        dep->number = epnum;
2681        dep->direction = direction;
2682        dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2683        dwc->eps[epnum] = dep;
2684        dep->combo_num = 0;
2685        dep->start_cmd_status = 0;
2686
2687        snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2688                        direction ? "in" : "out");
2689
2690        dep->endpoint.name = dep->name;
2691
2692        if (!(dep->number > 1)) {
2693                dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2694                dep->endpoint.comp_desc = NULL;
2695        }
2696
2697        if (num == 0)
2698                ret = dwc3_gadget_init_control_endpoint(dep);
2699        else if (direction)
2700                ret = dwc3_gadget_init_in_endpoint(dep);
2701        else
2702                ret = dwc3_gadget_init_out_endpoint(dep);
2703
2704        if (ret)
2705                return ret;
2706
2707        dep->endpoint.caps.dir_in = direction;
2708        dep->endpoint.caps.dir_out = !direction;
2709
2710        INIT_LIST_HEAD(&dep->pending_list);
2711        INIT_LIST_HEAD(&dep->started_list);
2712        INIT_LIST_HEAD(&dep->cancelled_list);
2713
2714        return 0;
2715}
2716
2717static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2718{
2719        u8                              epnum;
2720
2721        INIT_LIST_HEAD(&dwc->gadget->ep_list);
2722
2723        for (epnum = 0; epnum < total; epnum++) {
2724                int                     ret;
2725
2726                ret = dwc3_gadget_init_endpoint(dwc, epnum);
2727                if (ret)
2728                        return ret;
2729        }
2730
2731        return 0;
2732}
2733
2734static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2735{
2736        struct dwc3_ep                  *dep;
2737        u8                              epnum;
2738
2739        for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2740                dep = dwc->eps[epnum];
2741                if (!dep)
2742                        continue;
2743                /*
2744                 * Physical endpoints 0 and 1 are special; they form the
2745                 * bi-directional USB endpoint 0.
2746                 *
2747                 * For those two physical endpoints, we don't allocate a TRB
2748                 * pool nor do we add them the endpoints list. Due to that, we
2749                 * shouldn't do these two operations otherwise we would end up
2750                 * with all sorts of bugs when removing dwc3.ko.
2751                 */
2752                if (epnum != 0 && epnum != 1) {
2753                        dwc3_free_trb_pool(dep);
2754                        list_del(&dep->endpoint.ep_list);
2755                }
2756
2757                kfree(dep);
2758        }
2759}
2760
2761/* -------------------------------------------------------------------------- */
2762
2763static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2764                struct dwc3_request *req, struct dwc3_trb *trb,
2765                const struct dwc3_event_depevt *event, int status, int chain)
2766{
2767        unsigned int            count;
2768
2769        dwc3_ep_inc_deq(dep);
2770
2771        trace_dwc3_complete_trb(dep, trb);
2772        req->num_trbs--;
2773
2774        /*
2775         * If we're in the middle of series of chained TRBs and we
2776         * receive a short transfer along the way, DWC3 will skip
2777         * through all TRBs including the last TRB in the chain (the
2778         * where CHN bit is zero. DWC3 will also avoid clearing HWO
2779         * bit and SW has to do it manually.
2780         *
2781         * We're going to do that here to avoid problems of HW trying
2782         * to use bogus TRBs for transfers.
2783         */
2784        if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2785                trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2786
2787        /*
2788         * For isochronous transfers, the first TRB in a service interval must
2789         * have the Isoc-First type. Track and report its interval frame number.
2790         */
2791        if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2792            (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2793                unsigned int frame_number;
2794
2795                frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2796                frame_number &= ~(dep->interval - 1);
2797                req->request.frame_number = frame_number;
2798        }
2799
2800        /*
2801         * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2802         * this TRB points to the bounce buffer address, it's a MPS alignment
2803         * TRB. Don't add it to req->remaining calculation.
2804         */
2805        if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2806            trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
2807                trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2808                return 1;
2809        }
2810
2811        count = trb->size & DWC3_TRB_SIZE_MASK;
2812        req->remaining += count;
2813
2814        if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2815                return 1;
2816
2817        if (event->status & DEPEVT_STATUS_SHORT && !chain)
2818                return 1;
2819
2820        if ((event->status & DEPEVT_STATUS_IOC) &&
2821            (trb->ctrl & DWC3_TRB_CTRL_IOC))
2822                return 1;
2823
2824        if ((event->status & DEPEVT_STATUS_LST) &&
2825            (trb->ctrl & DWC3_TRB_CTRL_LST))
2826                return 1;
2827
2828        return 0;
2829}
2830
2831static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2832                struct dwc3_request *req, const struct dwc3_event_depevt *event,
2833                int status)
2834{
2835        struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2836        struct scatterlist *sg = req->sg;
2837        struct scatterlist *s;
2838        unsigned int pending = req->num_pending_sgs;
2839        unsigned int i;
2840        int ret = 0;
2841
2842        for_each_sg(sg, s, pending, i) {
2843                trb = &dep->trb_pool[dep->trb_dequeue];
2844
2845                req->sg = sg_next(s);
2846                req->num_pending_sgs--;
2847
2848                ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2849                                trb, event, status, true);
2850                if (ret)
2851                        break;
2852        }
2853
2854        return ret;
2855}
2856
2857static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2858                struct dwc3_request *req, const struct dwc3_event_depevt *event,
2859                int status)
2860{
2861        struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2862
2863        return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2864                        event, status, false);
2865}
2866
2867static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2868{
2869        return req->num_pending_sgs == 0;
2870}
2871
2872static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2873                const struct dwc3_event_depevt *event,
2874                struct dwc3_request *req, int status)
2875{
2876        int ret;
2877
2878        if (req->num_pending_sgs)
2879                ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2880                                status);
2881        else
2882                ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2883                                status);
2884
2885        req->request.actual = req->request.length - req->remaining;
2886
2887        if (!dwc3_gadget_ep_request_completed(req))
2888                goto out;
2889
2890        if (req->needs_extra_trb) {
2891                ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2892                                status);
2893                req->needs_extra_trb = false;
2894        }
2895
2896        req->request.actual = req->request.length - req->remaining;
2897
2898        if ((!dwc3_gadget_ep_request_completed(req) &&
2899             req->num_pending_sgs) || req->num_pending_sgs) {
2900                if (!(event->status &
2901                        (DEPEVT_STATUS_SHORT | DEPEVT_STATUS_LST))) {
2902                        __dwc3_gadget_kick_transfer(dep);
2903                        goto out;
2904                }
2905        }
2906
2907        dwc3_gadget_giveback(dep, req, status);
2908
2909out:
2910        return ret;
2911}
2912
2913static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2914                const struct dwc3_event_depevt *event, int status)
2915{
2916        struct dwc3_request     *req;
2917        struct dwc3_request     *tmp;
2918
2919        list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2920                int ret;
2921
2922                ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2923                                req, status);
2924                if (ret)
2925                        break;
2926        }
2927}
2928
2929static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2930{
2931        struct dwc3_request     *req;
2932
2933        if (!list_empty(&dep->pending_list))
2934                return true;
2935
2936        /*
2937         * We only need to check the first entry of the started list. We can
2938         * assume the completed requests are removed from the started list.
2939         */
2940        req = next_request(&dep->started_list);
2941        if (!req)
2942                return false;
2943
2944        return !dwc3_gadget_ep_request_completed(req);
2945}
2946
2947static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2948                const struct dwc3_event_depevt *event)
2949{
2950        dep->frame_number = event->parameters;
2951}
2952
2953static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2954                const struct dwc3_event_depevt *event, int status)
2955{
2956        struct dwc3             *dwc = dep->dwc;
2957        bool                    no_started_trb = true;
2958
2959        dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2960
2961        if (dep->stream_capable && !list_empty(&dep->started_list))
2962                __dwc3_gadget_kick_transfer(dep);
2963
2964        if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2965                goto out;
2966
2967        if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2968            list_empty(&dep->started_list)) {
2969                if (list_empty(&dep->pending_list))
2970                        /*
2971                         * If there is no entry in request list then do
2972                         * not issue END TRANSFER now. Just set PENDING
2973                         * flag, so that END TRANSFER is issued when an
2974                         * entry is added into request list.
2975                         */
2976                        dep->flags |= DWC3_EP_PENDING_REQUEST;
2977                else if (status == -EXDEV)
2978                        dwc3_stop_active_transfer(dep, true, true);
2979        } else if (dwc3_gadget_ep_should_continue(dep))
2980                if (__dwc3_gadget_kick_transfer(dep) == 0)
2981                        no_started_trb = false;
2982
2983out:
2984        /*
2985         * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2986         * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2987         */
2988        if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
2989                u32             reg;
2990                int             i;
2991
2992                for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2993                        dep = dwc->eps[i];
2994
2995                        if (!(dep->flags & DWC3_EP_ENABLED))
2996                                continue;
2997
2998                        if (!list_empty(&dep->started_list))
2999                                return no_started_trb;
3000                }
3001
3002                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3003                reg |= dwc->u1u2;
3004                dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3005
3006                dwc->u1u2 = 0;
3007        }
3008
3009        return no_started_trb;
3010}
3011
3012static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3013                const struct dwc3_event_depevt *event)
3014{
3015        int status = 0;
3016
3017        if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3018                dwc3_gadget_endpoint_frame_from_event(dep, event);
3019
3020        if (event->status & DEPEVT_STATUS_BUSERR)
3021                status = -ECONNRESET;
3022
3023        if ((event->status & DEPEVT_STATUS_MISSED_ISOC) &&
3024            usb_endpoint_xfer_isoc(dep->endpoint.desc))
3025                status = -EXDEV;
3026
3027        dwc3_gadget_endpoint_trbs_complete(dep, event, status);
3028}
3029
3030static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3031                const struct dwc3_event_depevt *event)
3032{
3033        int status = 0;
3034
3035        dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3036
3037        if (event->status & DEPEVT_STATUS_BUSERR)
3038                status = -ECONNRESET;
3039
3040        if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3041                dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
3042}
3043
3044static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3045                const struct dwc3_event_depevt *event)
3046{
3047        dwc3_gadget_endpoint_frame_from_event(dep, event);
3048
3049        /*
3050         * The XferNotReady event is generated only once before the endpoint
3051         * starts. It will be generated again when END_TRANSFER command is
3052         * issued. For some controller versions, the XferNotReady event may be
3053         * generated while the END_TRANSFER command is still in process. Ignore
3054         * it and wait for the next XferNotReady event after the command is
3055         * completed.
3056         */
3057        if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3058                return;
3059
3060        (void) __dwc3_gadget_start_isoc(dep);
3061}
3062
3063static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3064                const struct dwc3_event_depevt *event)
3065{
3066        u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3067
3068        if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3069                return;
3070
3071        dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3072        dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3073        dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3074
3075        if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3076                struct dwc3 *dwc = dep->dwc;
3077
3078                dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3079                if (dwc3_send_clear_stall_ep_cmd(dep)) {
3080                        struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3081
3082                        dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3083                        if (dwc->delayed_status)
3084                                __dwc3_gadget_ep0_set_halt(ep0, 1);
3085                        return;
3086                }
3087
3088                dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3089                if (dwc->delayed_status)
3090                        dwc3_ep0_send_delayed_status(dwc);
3091        }
3092
3093        if ((dep->flags & DWC3_EP_DELAY_START) &&
3094            !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3095                __dwc3_gadget_kick_transfer(dep);
3096
3097        dep->flags &= ~DWC3_EP_DELAY_START;
3098}
3099
3100static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3101                const struct dwc3_event_depevt *event)
3102{
3103        struct dwc3             *dwc = dep->dwc;
3104        struct dwc3_request     *req;
3105        u8                      stream_id;
3106
3107        if (event->status == DEPEVT_STREAMEVT_FOUND) {
3108                stream_id = event->parameters;
3109
3110                /* Check for req matching the streamid and delete the timer */
3111                list_for_each_entry(req, &dep->started_list, list) {
3112                        if (req->request.stream_id == stream_id) {
3113                                if (timer_pending(&req->stream_timeout_timer))
3114                                        del_timer(&req->stream_timeout_timer);
3115                                break;
3116                        }
3117                }
3118
3119                dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3120
3121                goto out;
3122        }
3123
3124        /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3125        switch (event->parameters) {
3126        case DEPEVT_STREAM_PRIME:
3127                /*
3128                 * If the host can properly transition the endpoint state from
3129                 * idle to prime after a NoStream rejection, there's no need to
3130                 * force restarting the endpoint to reinitiate the stream. To
3131                 * simplify the check, assume the host follows the USB spec if
3132                 * it primed the endpoint more than once.
3133                 */
3134                if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3135                        if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3136                                dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3137                        else
3138                                dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3139                }
3140
3141                break;
3142        case DEPEVT_STREAM_NOSTREAM:
3143                if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3144                    !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3145                    !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3146                        break;
3147
3148                /*
3149                 * If the host rejects a stream due to no active stream, by the
3150                 * USB and xHCI spec, the endpoint will be put back to idle
3151                 * state. When the host is ready (buffer added/updated), it will
3152                 * prime the endpoint to inform the usb device controller. This
3153                 * triggers the device controller to issue ERDY to restart the
3154                 * stream. However, some hosts don't follow this and keep the
3155                 * endpoint in the idle state. No prime will come despite host
3156                 * streams are updated, and the device controller will not be
3157                 * triggered to generate ERDY to move the next stream data. To
3158                 * workaround this and maintain compatibility with various
3159                 * hosts, force to reinitate the stream until the host is ready
3160                 * instead of waiting for the host to prime the endpoint.
3161                 */
3162                if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3163                        unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3164
3165                        dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3166                } else {
3167                        dep->flags |= DWC3_EP_DELAY_START;
3168                        dwc3_stop_active_transfer(dep, true, true);
3169                        return;
3170                }
3171                break;
3172        }
3173
3174out:
3175        dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3176}
3177
3178static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3179                const struct dwc3_event_depevt *event)
3180{
3181        struct dwc3_ep          *dep;
3182        u8                      epnum = event->endpoint_number;
3183
3184        dep = dwc->eps[epnum];
3185
3186        if (!(dep->flags & DWC3_EP_ENABLED)) {
3187                if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
3188                        return;
3189
3190                /* Handle only EPCMDCMPLT when EP disabled */
3191                if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3192                        return;
3193        }
3194
3195        if (epnum == 0 || epnum == 1) {
3196                dwc3_ep0_interrupt(dwc, event);
3197                return;
3198        }
3199
3200        switch (event->endpoint_event) {
3201        case DWC3_DEPEVT_XFERINPROGRESS:
3202                dwc3_gadget_endpoint_transfer_in_progress(dep, event);
3203                break;
3204        case DWC3_DEPEVT_XFERNOTREADY:
3205                dwc3_gadget_endpoint_transfer_not_ready(dep, event);
3206                break;
3207        case DWC3_DEPEVT_EPCMDCMPLT:
3208                dwc3_gadget_endpoint_command_complete(dep, event);
3209                break;
3210        case DWC3_DEPEVT_XFERCOMPLETE:
3211                if (!dep->stream_capable)
3212                        break;
3213                dwc3_gadget_endpoint_transfer_complete(dep, event);
3214                break;
3215        case DWC3_DEPEVT_STREAMEVT:
3216                dwc3_gadget_endpoint_stream_event(dep, event);
3217                break;
3218        case DWC3_DEPEVT_RXTXFIFOEVT:
3219                break;
3220        }
3221}
3222
3223static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3224{
3225        if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3226                spin_unlock(&dwc->lock);
3227                dwc->gadget_driver->disconnect(dwc->gadget);
3228                spin_lock(&dwc->lock);
3229        }
3230}
3231
3232static void dwc3_suspend_gadget(struct dwc3 *dwc)
3233{
3234        if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
3235                spin_unlock(&dwc->lock);
3236                dwc->gadget_driver->suspend(dwc->gadget);
3237                spin_lock(&dwc->lock);
3238        }
3239}
3240
3241static void dwc3_resume_gadget(struct dwc3 *dwc)
3242{
3243        if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3244                spin_unlock(&dwc->lock);
3245                dwc->gadget_driver->resume(dwc->gadget);
3246                spin_lock(&dwc->lock);
3247        }
3248}
3249
3250static void dwc3_reset_gadget(struct dwc3 *dwc)
3251{
3252        if (!dwc->gadget_driver)
3253                return;
3254
3255        if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
3256                spin_unlock(&dwc->lock);
3257                usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
3258                spin_lock(&dwc->lock);
3259        }
3260}
3261
3262void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
3263{
3264        struct dwc3_gadget_ep_cmd_params params;
3265        u32 cmd;
3266        int ret;
3267
3268        if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3269            (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
3270                return;
3271
3272        /*
3273         * NOTICE: We are violating what the Databook says about the
3274         * EndTransfer command. Ideally we would _always_ wait for the
3275         * EndTransfer Command Completion IRQ, but that's causing too
3276         * much trouble synchronizing between us and gadget driver.
3277         *
3278         * We have discussed this with the IP Provider and it was
3279         * suggested to giveback all requests here.
3280         *
3281         * Note also that a similar handling was tested by Synopsys
3282         * (thanks a lot Paul) and nothing bad has come out of it.
3283         * In short, what we're doing is issuing EndTransfer with
3284         * CMDIOC bit set and delay kicking transfer until the
3285         * EndTransfer command had completed.
3286         *
3287         * As of IP version 3.10a of the DWC_usb3 IP, the controller
3288         * supports a mode to work around the above limitation. The
3289         * software can poll the CMDACT bit in the DEPCMD register
3290         * after issuing a EndTransfer command. This mode is enabled
3291         * by writing GUCTL2[14]. This polling is already done in the
3292         * dwc3_send_gadget_ep_cmd() function so if the mode is
3293         * enabled, the EndTransfer command will have completed upon
3294         * returning from this function.
3295         *
3296         * This mode is NOT available on the DWC_usb31 IP.
3297         */
3298
3299        cmd = DWC3_DEPCMD_ENDTRANSFER;
3300        cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3301        cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
3302        cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3303        memset(&params, 0, sizeof(params));
3304        ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
3305        WARN_ON_ONCE(ret);
3306
3307        /*
3308         * when transfer is stopped with force rm bit false, it can be
3309         * restarted by passing resource_index in params; don't loose it
3310         */
3311        if (force)
3312                dep->resource_index = 0;
3313
3314        /*
3315         * The END_TRANSFER command will cause the controller to generate a
3316         * NoStream Event, and it's not due to the host DP NoStream rejection.
3317         * Ignore the next NoStream event.
3318         */
3319        if (dep->stream_capable)
3320                dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3321
3322        if (!interrupt)
3323                dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3324        else
3325                dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
3326        /*
3327         * when transfer is stopped with force rm bit false, it can be
3328         * restarted by passing resource_index in params; don't loose it
3329         */
3330        if (force)
3331                dep->resource_index = 0;
3332}
3333
3334static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3335{
3336        u32 epnum;
3337
3338        for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3339                struct dwc3_ep *dep;
3340                int ret;
3341
3342                dep = dwc->eps[epnum];
3343                if (!dep)
3344                        continue;
3345
3346                if (!(dep->flags & DWC3_EP_STALL))
3347                        continue;
3348
3349                dep->flags &= ~DWC3_EP_STALL;
3350
3351                ret = dwc3_send_clear_stall_ep_cmd(dep);
3352                WARN_ON_ONCE(ret);
3353        }
3354}
3355
3356static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3357{
3358        int                     reg;
3359
3360        dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3361
3362        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3363        reg &= ~DWC3_DCTL_INITU1ENA;
3364        reg &= ~DWC3_DCTL_INITU2ENA;
3365        dwc3_gadget_dctl_write_safe(dwc, reg);
3366
3367        dwc3_disconnect_gadget(dwc);
3368
3369        /* In USB 2.0, to avoid hibernation interrupt at the time of connection
3370         * clear DWC3_DCTL_KEEP_CONNECT bit.
3371         */
3372        if (dwc->has_hibernation) {
3373                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3374                reg &= ~DWC3_DCTL_KEEP_CONNECT;
3375                dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3376        }
3377
3378        dwc->gadget->speed = USB_SPEED_UNKNOWN;
3379        dwc->setup_packet_pending = false;
3380        usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
3381
3382        dwc->connected = false;
3383}
3384
3385static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3386{
3387        u32                     reg;
3388
3389        dwc->connected = true;
3390
3391        /*
3392         * WORKAROUND: DWC3 revisions <1.88a have an issue which
3393         * would cause a missing Disconnect Event if there's a
3394         * pending Setup Packet in the FIFO.
3395         *
3396         * There's no suggested workaround on the official Bug
3397         * report, which states that "unless the driver/application
3398         * is doing any special handling of a disconnect event,
3399         * there is no functional issue".
3400         *
3401         * Unfortunately, it turns out that we _do_ some special
3402         * handling of a disconnect event, namely complete all
3403         * pending transfers, notify gadget driver of the
3404         * disconnection, and so on.
3405         *
3406         * Our suggested workaround is to follow the Disconnect
3407         * Event steps here, instead, based on a setup_packet_pending
3408         * flag. Such flag gets set whenever we have a SETUP_PENDING
3409         * status for EP0 TRBs and gets cleared on XferComplete for the
3410         * same endpoint.
3411         *
3412         * Refers to:
3413         *
3414         * STAR#9000466709: RTL: Device : Disconnect event not
3415         * generated if setup packet pending in FIFO
3416         */
3417        if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
3418                if (dwc->setup_packet_pending)
3419                        dwc3_gadget_disconnect_interrupt(dwc);
3420        }
3421
3422        dwc3_reset_gadget(dwc);
3423        /*
3424         * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3425         * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3426         * needs to ensure that it sends "a DEPENDXFER command for any active
3427         * transfers."
3428         */
3429        dwc3_stop_active_transfers(dwc);
3430
3431        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3432        reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3433        dwc3_gadget_dctl_write_safe(dwc, reg);
3434        dwc->test_mode = false;
3435        dwc3_clear_stall_all_ep(dwc);
3436
3437        /* Reset device address to zero */
3438        reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3439        reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3440        dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3441}
3442
3443static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3444{
3445        struct dwc3_ep          *dep;
3446        int                     ret;
3447        u32                     reg;
3448        u8                      speed;
3449
3450        reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3451        speed = reg & DWC3_DSTS_CONNECTSPD;
3452        dwc->speed = speed;
3453
3454        /*
3455         * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3456         * each time on Connect Done.
3457         *
3458         * Currently we always use the reset value. If any platform
3459         * wants to set this to a different value, we need to add a
3460         * setting and update GCTL.RAMCLKSEL here.
3461         */
3462
3463        switch (speed) {
3464        case DWC3_DSTS_SUPERSPEED_PLUS:
3465                dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3466                dwc->gadget->ep0->maxpacket = 512;
3467                dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
3468                break;
3469        case DWC3_DSTS_SUPERSPEED:
3470                /*
3471                 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3472                 * would cause a missing USB3 Reset event.
3473                 *
3474                 * In such situations, we should force a USB3 Reset
3475                 * event by calling our dwc3_gadget_reset_interrupt()
3476                 * routine.
3477                 *
3478                 * Refers to:
3479                 *
3480                 * STAR#9000483510: RTL: SS : USB3 reset event may
3481                 * not be generated always when the link enters poll
3482                 */
3483                if (DWC3_VER_IS_PRIOR(DWC3, 190A))
3484                        dwc3_gadget_reset_interrupt(dwc);
3485
3486                dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3487                dwc->gadget->ep0->maxpacket = 512;
3488                dwc->gadget->speed = USB_SPEED_SUPER;
3489                break;
3490        case DWC3_DSTS_HIGHSPEED:
3491                dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3492                dwc->gadget->ep0->maxpacket = 64;
3493                dwc->gadget->speed = USB_SPEED_HIGH;
3494                break;
3495        case DWC3_DSTS_FULLSPEED:
3496                dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3497                dwc->gadget->ep0->maxpacket = 64;
3498                dwc->gadget->speed = USB_SPEED_FULL;
3499                break;
3500        case DWC3_DSTS_LOWSPEED:
3501                dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3502                dwc->gadget->ep0->maxpacket = 8;
3503                dwc->gadget->speed = USB_SPEED_LOW;
3504                break;
3505        }
3506
3507        dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
3508
3509        /* Enable USB2 LPM Capability */
3510
3511        if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
3512            (speed != DWC3_DSTS_SUPERSPEED) &&
3513            (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
3514                reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3515                reg |= DWC3_DCFG_LPM_CAP;
3516                dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3517
3518                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3519                reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3520
3521                reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3522                                            (dwc->is_utmi_l1_suspend << 4));
3523
3524                /*
3525                 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3526                 * DCFG.LPMCap is set, core responses with an ACK and the
3527                 * BESL value in the LPM token is less than or equal to LPM
3528                 * NYET threshold.
3529                 */
3530                WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
3531                                "LPM Erratum not available on dwc3 revisions < 2.40a\n");
3532
3533                if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
3534                        reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
3535
3536                dwc3_gadget_dctl_write_safe(dwc, reg);
3537        } else {
3538                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3539                reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3540                dwc3_gadget_dctl_write_safe(dwc, reg);
3541        }
3542
3543        dep = dwc->eps[0];
3544        ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
3545        if (ret) {
3546                dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3547                return;
3548        }
3549
3550        dep = dwc->eps[1];
3551        ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
3552        if (ret) {
3553                dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3554                return;
3555        }
3556
3557        /*
3558         * In USB 2.0, to avoid hibernation interrupt at the time of connection
3559         * set DWC3_DCTL_KEEP_CONNECT bit here
3560         */
3561        if (dwc->has_hibernation) {
3562                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3563                reg |= DWC3_DCTL_KEEP_CONNECT;
3564                dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3565
3566                /*
3567                 * WORKAROUND: In USB 2.0, before connection done early
3568                 * hibernation interrupt occurred. To avoid early hibernation
3569                 * event for gadget mode set DWC3_GCTL_GBLHIBERNATIONEN bit
3570                 * after connection done instead of global core setup in
3571                 * dwc3 core.
3572                 */
3573                reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3574                reg |= DWC3_GCTL_GBLHIBERNATIONEN;
3575                dwc3_writel(dwc->regs, DWC3_GCTL, reg);
3576        }
3577
3578        /*
3579         * Configure PHY via GUSB3PIPECTLn if required.
3580         *
3581         * Update GTXFIFOSIZn
3582         *
3583         * In both cases reset values should be sufficient.
3584         */
3585}
3586
3587static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3588{
3589        /* Take core out of low power mode. */
3590        if (dwc->is_hibernated)
3591                dwc3_gadget_exit_hibernation(dwc);
3592
3593        if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3594                spin_unlock(&dwc->lock);
3595                dwc->gadget_driver->resume(dwc->gadget);
3596                spin_lock(&dwc->lock);
3597        }
3598}
3599
3600static irqreturn_t dwc3_wakeup_interrupt(int irq, void *_dwc)
3601{
3602        struct dwc3 *dwc = (struct dwc3 *)_dwc;
3603
3604        spin_lock(&dwc->lock);
3605        dwc3_gadget_wakeup_interrupt(dwc);
3606        spin_unlock(&dwc->lock);
3607
3608        return IRQ_HANDLED;
3609}
3610
3611static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3612                unsigned int evtinfo)
3613{
3614        enum dwc3_link_state    next = evtinfo & DWC3_LINK_STATE_MASK;
3615        unsigned int            pwropt;
3616
3617        /*
3618         * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3619         * Hibernation mode enabled which would show up when device detects
3620         * host-initiated U3 exit.
3621         *
3622         * In that case, device will generate a Link State Change Interrupt
3623         * from U3 to RESUME which is only necessary if Hibernation is
3624         * configured in.
3625         *
3626         * There are no functional changes due to such spurious event and we
3627         * just need to ignore it.
3628         *
3629         * Refers to:
3630         *
3631         * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3632         * operational mode
3633         */
3634        pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3635        if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
3636                        (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3637                if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3638                                (next == DWC3_LINK_STATE_RESUME)) {
3639                        return;
3640                }
3641        }
3642
3643        /*
3644         * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3645         * on the link partner, the USB session might do multiple entry/exit
3646         * of low power states before a transfer takes place.
3647         *
3648         * Due to this problem, we might experience lower throughput. The
3649         * suggested workaround is to disable DCTL[12:9] bits if we're
3650         * transitioning from U1/U2 to U0 and enable those bits again
3651         * after a transfer completes and there are no pending transfers
3652         * on any of the enabled endpoints.
3653         *
3654         * This is the first half of that workaround.
3655         *
3656         * Refers to:
3657         *
3658         * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3659         * core send LGO_Ux entering U0
3660         */
3661        if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
3662                if (next == DWC3_LINK_STATE_U0) {
3663                        u32     u1u2;
3664                        u32     reg;
3665
3666                        switch (dwc->link_state) {
3667                        case DWC3_LINK_STATE_U1:
3668                        case DWC3_LINK_STATE_U2:
3669                                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3670                                u1u2 = reg & (DWC3_DCTL_INITU2ENA
3671                                                | DWC3_DCTL_ACCEPTU2ENA
3672                                                | DWC3_DCTL_INITU1ENA
3673                                                | DWC3_DCTL_ACCEPTU1ENA);
3674
3675                                if (!dwc->u1u2)
3676                                        dwc->u1u2 = reg & u1u2;
3677
3678                                reg &= ~u1u2;
3679
3680                                dwc3_gadget_dctl_write_safe(dwc, reg);
3681                                break;
3682                        default:
3683                                /* do nothing */
3684                                break;
3685                        }
3686                }
3687        }
3688
3689        switch (next) {
3690        case DWC3_LINK_STATE_U1:
3691                if (dwc->speed == USB_SPEED_SUPER)
3692                        dwc3_suspend_gadget(dwc);
3693                break;
3694        case DWC3_LINK_STATE_U2:
3695        case DWC3_LINK_STATE_U3:
3696                dwc3_suspend_gadget(dwc);
3697                break;
3698        case DWC3_LINK_STATE_RESUME:
3699                dwc3_resume_gadget(dwc);
3700                break;
3701        default:
3702                /* do nothing */
3703                break;
3704        }
3705
3706        dwc->link_state = next;
3707}
3708
3709static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3710                                          unsigned int evtinfo)
3711{
3712        enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3713
3714        if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3715                dwc3_suspend_gadget(dwc);
3716
3717        dwc->link_state = next;
3718}
3719
3720static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3721                unsigned int evtinfo)
3722{
3723        unsigned int is_ss = evtinfo & BIT(4);
3724
3725        /*
3726         * WORKAROUND: DWC3 revison 2.20a with hibernation support
3727         * have a known issue which can cause USB CV TD.9.23 to fail
3728         * randomly.
3729         *
3730         * Because of this issue, core could generate bogus hibernation
3731         * events which SW needs to ignore.
3732         *
3733         * Refers to:
3734         *
3735         * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3736         * Device Fallback from SuperSpeed
3737         */
3738        if ((!!is_ss ^ (dwc->speed == USB_SPEED_SUPER)) &&
3739            (!(dwc->has_hibernation)))
3740                return;
3741
3742        /* enter hibernation here */
3743        dwc3_gadget_enter_hibernation(dwc);
3744}
3745
3746static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3747                const struct dwc3_event_devt *event)
3748{
3749        switch (event->type) {
3750        case DWC3_DEVICE_EVENT_DISCONNECT:
3751                dwc3_gadget_disconnect_interrupt(dwc);
3752                break;
3753        case DWC3_DEVICE_EVENT_RESET:
3754                dwc3_gadget_reset_interrupt(dwc);
3755                break;
3756        case DWC3_DEVICE_EVENT_CONNECT_DONE:
3757                dwc3_gadget_conndone_interrupt(dwc);
3758                break;
3759        case DWC3_DEVICE_EVENT_WAKEUP:
3760                dwc3_gadget_wakeup_interrupt(dwc);
3761                break;
3762        case DWC3_DEVICE_EVENT_HIBER_REQ:
3763                if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3764                                        "unexpected hibernation event\n"))
3765                        break;
3766
3767                dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3768                break;
3769        case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3770                dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3771                break;
3772        case DWC3_DEVICE_EVENT_EOPF:
3773                /* It changed to be suspend event for version 2.30a and above */
3774                if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
3775                        /*
3776                         * Ignore suspend event until the gadget enters into
3777                         * USB_STATE_CONFIGURED state.
3778                         */
3779                        if (dwc->gadget->state >= USB_STATE_CONFIGURED)
3780                                dwc3_gadget_suspend_interrupt(dwc,
3781                                                event->event_info);
3782                }
3783                break;
3784        case DWC3_DEVICE_EVENT_SOF:
3785        case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3786        case DWC3_DEVICE_EVENT_CMD_CMPL:
3787        case DWC3_DEVICE_EVENT_OVERFLOW:
3788                break;
3789        default:
3790                dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3791        }
3792}
3793
3794static void dwc3_process_event_entry(struct dwc3 *dwc,
3795                const union dwc3_event *event)
3796{
3797        trace_dwc3_event(event->raw, dwc);
3798
3799        if (!event->type.is_devspec)
3800                dwc3_endpoint_interrupt(dwc, &event->depevt);
3801        else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3802                dwc3_gadget_interrupt(dwc, &event->devt);
3803        else
3804                dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3805}
3806
3807static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3808{
3809        struct dwc3 *dwc = evt->dwc;
3810        irqreturn_t ret = IRQ_NONE;
3811        int left;
3812        u32 reg;
3813
3814        left = evt->count;
3815
3816        if (!(evt->flags & DWC3_EVENT_PENDING))
3817                return IRQ_NONE;
3818
3819        while (left > 0) {
3820                union dwc3_event event;
3821
3822                event.raw = *(u32 *) (evt->cache + evt->lpos);
3823
3824                dwc3_process_event_entry(dwc, &event);
3825
3826                /*
3827                 * FIXME we wrap around correctly to the next entry as
3828                 * almost all entries are 4 bytes in size. There is one
3829                 * entry which has 12 bytes which is a regular entry
3830                 * followed by 8 bytes data. ATM I don't know how
3831                 * things are organized if we get next to the a
3832                 * boundary so I worry about that once we try to handle
3833                 * that.
3834                 */
3835                evt->lpos = (evt->lpos + 4) % evt->length;
3836                left -= 4;
3837
3838                /* Stop processing any events after core is hibernated */
3839                if (dwc->is_hibernated)
3840                        break;
3841        }
3842
3843        evt->count = 0;
3844        evt->flags &= ~DWC3_EVENT_PENDING;
3845        ret = IRQ_HANDLED;
3846
3847        /* Prevent interrupt generation when hibernated */
3848        if (!dwc->is_hibernated) {
3849                /* Unmask interrupt */
3850                reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3851                reg &= ~DWC3_GEVNTSIZ_INTMASK;
3852                dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3853        }
3854
3855        if (dwc->imod_interval) {
3856                dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3857                dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3858        }
3859
3860        return ret;
3861}
3862
3863static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3864{
3865        struct dwc3_event_buffer *evt = _evt;
3866        struct dwc3 *dwc = evt->dwc;
3867        unsigned long flags;
3868        irqreturn_t ret = IRQ_NONE;
3869
3870        spin_lock_irqsave(&dwc->lock, flags);
3871        ret = dwc3_process_event_buf(evt);
3872        spin_unlock_irqrestore(&dwc->lock, flags);
3873
3874        return ret;
3875}
3876
3877static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3878{
3879        struct dwc3 *dwc = evt->dwc;
3880        u32 amount;
3881        u32 count;
3882        u32 reg;
3883
3884        if (pm_runtime_suspended(dwc->dev)) {
3885                pm_runtime_get(dwc->dev);
3886                disable_irq_nosync(dwc->irq_gadget);
3887                dwc->pending_events = true;
3888                return IRQ_HANDLED;
3889        }
3890
3891        /* Stop processing events after hibernated */
3892        if (dwc->is_hibernated)
3893                return IRQ_HANDLED;
3894
3895        /*
3896         * With PCIe legacy interrupt, test shows that top-half irq handler can
3897         * be called again after HW interrupt deassertion. Check if bottom-half
3898         * irq event handler completes before caching new event to prevent
3899         * losing events.
3900         */
3901        if (evt->flags & DWC3_EVENT_PENDING)
3902                return IRQ_HANDLED;
3903
3904        count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3905        count &= DWC3_GEVNTCOUNT_MASK;
3906        if (!count)
3907                return IRQ_NONE;
3908
3909        evt->count = count;
3910        evt->flags |= DWC3_EVENT_PENDING;
3911
3912        /* Mask interrupt */
3913        reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3914        reg |= DWC3_GEVNTSIZ_INTMASK;
3915        dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3916
3917        amount = min(count, evt->length - evt->lpos);
3918        memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3919
3920        if (amount < count)
3921                memcpy(evt->cache, evt->buf, count - amount);
3922
3923        dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3924
3925        return IRQ_WAKE_THREAD;
3926}
3927
3928static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3929{
3930        struct dwc3_event_buffer        *evt = _evt;
3931
3932        return dwc3_check_event_buf(evt);
3933}
3934
3935static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3936{
3937        struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3938        int irq, irq_hiber;
3939
3940        irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
3941        if (irq > 0)
3942                goto out;
3943
3944        if (irq == -EPROBE_DEFER)
3945                goto out;
3946
3947        irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
3948        if (irq > 0)
3949                goto out;
3950
3951        if (irq == -EPROBE_DEFER)
3952                goto out;
3953
3954        irq = platform_get_irq(dwc3_pdev, 0);
3955        if (irq > 0)
3956                goto out;
3957
3958        if (!irq)
3959                irq = -EINVAL;
3960
3961out:
3962        /* look for wakeup interrupt if hibernation is supported */
3963        if (dwc->has_hibernation) {
3964                irq_hiber = platform_get_irq_byname(dwc3_pdev, "hiber");
3965                if (irq_hiber > 0) {
3966                        dwc->irq_wakeup = irq_hiber;
3967                } else {
3968                        irq_hiber = platform_get_irq(dwc3_pdev, 2);
3969                        if (irq_hiber > 0)
3970                                dwc->irq_wakeup = irq_hiber;
3971                }
3972        }
3973
3974        return irq;
3975}
3976
3977static void dwc_gadget_release(struct device *dev)
3978{
3979        struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3980
3981        kfree(gadget);
3982}
3983
3984/**
3985 * dwc3_gadget_init - initializes gadget related registers
3986 * @dwc: pointer to our controller context structure
3987 *
3988 * Returns 0 on success otherwise negative errno.
3989 */
3990int dwc3_gadget_init(struct dwc3 *dwc)
3991{
3992        int ret;
3993        int irq;
3994        struct device *dev;
3995
3996        irq = dwc3_gadget_get_irq(dwc);
3997        if (irq < 0) {
3998                ret = irq;
3999                goto err0;
4000        }
4001
4002        dwc->irq_gadget = irq;
4003
4004        if (dwc->dr_mode == USB_DR_MODE_OTG) {
4005                struct usb_phy *phy;
4006
4007                /* Switch otg to peripheral mode */
4008                phy = usb_get_phy(USB_PHY_TYPE_USB3);
4009                if (!IS_ERR(phy)) {
4010                        if (phy && phy->otg)
4011                                otg_set_peripheral(phy->otg,
4012                                                (struct usb_gadget *)1);
4013                        usb_put_phy(phy);
4014                }
4015        }
4016
4017        dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
4018                                          sizeof(*dwc->ep0_trb) * 2,
4019                                          &dwc->ep0_trb_addr, GFP_KERNEL);
4020        if (!dwc->ep0_trb) {
4021                dev_err(dwc->dev, "failed to allocate ep0 trb\n");
4022                ret = -ENOMEM;
4023                goto err0;
4024        }
4025
4026        dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
4027        if (!dwc->setup_buf) {
4028                ret = -ENOMEM;
4029                goto err1;
4030        }
4031
4032        dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
4033                        &dwc->bounce_addr, GFP_KERNEL);
4034        if (!dwc->bounce) {
4035                ret = -ENOMEM;
4036                goto err2;
4037        }
4038
4039        init_completion(&dwc->ep0_in_setup);
4040        dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4041        if (!dwc->gadget) {
4042                ret = -ENOMEM;
4043                goto err3;
4044        }
4045
4046
4047        usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
4048        dev                             = &dwc->gadget->dev;
4049        dev->platform_data              = dwc;
4050        dwc->gadget->ops                = &dwc3_gadget_ops;
4051        dwc->gadget->speed              = USB_SPEED_UNKNOWN;
4052        dwc->gadget->sg_supported       = true;
4053        dwc->gadget->name               = "dwc3-gadget";
4054        dwc->gadget->lpm_capable        = true;
4055
4056        /*
4057         * FIXME We might be setting max_speed to <SUPER, however versions
4058         * <2.20a of dwc3 have an issue with metastability (documented
4059         * elsewhere in this driver) which tells us we can't set max speed to
4060         * anything lower than SUPER.
4061         *
4062         * Because gadget.max_speed is only used by composite.c and function
4063         * drivers (i.e. it won't go into dwc3's registers) we are allowing this
4064         * to happen so we avoid sending SuperSpeed Capability descriptor
4065         * together with our BOS descriptor as that could confuse host into
4066         * thinking we can handle super speed.
4067         *
4068         * Note that, in fact, we won't even support GetBOS requests when speed
4069         * is less than super speed because we don't have means, yet, to tell
4070         * composite.c that we are USB 2.0 + LPM ECN.
4071         */
4072        if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
4073            !dwc->dis_metastability_quirk)
4074                dev_info(dwc->dev, "changing max_speed on rev %08x\n",
4075                                dwc->revision);
4076
4077        dwc->gadget->max_speed          = dwc->maximum_speed;
4078
4079        /*
4080         * REVISIT: Here we should clear all pending IRQs to be
4081         * sure we're starting from a well known location.
4082         */
4083
4084        ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
4085        if (ret)
4086                goto err4;
4087
4088        ret = usb_add_gadget(dwc->gadget);
4089        if (ret) {
4090                dev_err(dwc->dev, "failed to add gadget\n");
4091                goto err5;
4092        }
4093
4094        if (dwc->dr_mode == USB_DR_MODE_OTG) {
4095                struct usb_phy *phy;
4096
4097                phy = usb_get_phy(USB_PHY_TYPE_USB3);
4098                if (!IS_ERR(phy)) {
4099                        if (phy && phy->otg) {
4100                                ret = otg_set_peripheral(phy->otg,
4101                                                dwc->gadget);
4102                                if (ret) {
4103                                        dev_err(dwc->dev,
4104                                        "otg_set_peripheral failed\n");
4105                                        usb_put_phy(phy);
4106                                        goto err5;
4107                                }
4108                        } else {
4109                                usb_put_phy(phy);
4110                        }
4111                }
4112        }
4113
4114        dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
4115
4116        return 0;
4117
4118err5:
4119        dwc3_gadget_free_endpoints(dwc);
4120err4:
4121        usb_put_gadget(dwc->gadget);
4122err3:
4123        dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4124                        dwc->bounce_addr);
4125
4126err2:
4127        kfree(dwc->setup_buf);
4128
4129err1:
4130        dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4131                        dwc->ep0_trb, dwc->ep0_trb_addr);
4132
4133err0:
4134        return ret;
4135}
4136
4137/* -------------------------------------------------------------------------- */
4138
4139void dwc3_gadget_exit(struct dwc3 *dwc)
4140{
4141        usb_del_gadget_udc(dwc->gadget);
4142        dwc3_gadget_free_endpoints(dwc);
4143        dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4144                          dwc->bounce_addr);
4145        kfree(dwc->setup_buf);
4146        dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4147                          dwc->ep0_trb, dwc->ep0_trb_addr);
4148}
4149
4150int dwc3_gadget_suspend(struct dwc3 *dwc)
4151{
4152        if (!dwc->gadget_driver)
4153                return 0;
4154
4155        dwc3_gadget_run_stop(dwc, false, false);
4156        dwc3_disconnect_gadget(dwc);
4157        __dwc3_gadget_stop(dwc);
4158
4159        return 0;
4160}
4161
4162int dwc3_gadget_resume(struct dwc3 *dwc)
4163{
4164        int                     ret, reg;
4165
4166        if (!dwc->gadget_driver)
4167                return 0;
4168
4169        ret = __dwc3_gadget_start(dwc);
4170        if (ret < 0)
4171                goto err0;
4172
4173        ret = dwc3_gadget_run_stop(dwc, true, false);
4174        if (ret < 0)
4175                goto err1;
4176
4177        /*
4178         * In USB 2.0, to avoid hibernation interrupt at the time of connection
4179         * set DWC3_DCTL_KEEP_CONNECT bit.
4180         */
4181        if (dwc->has_hibernation) {
4182                reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4183                reg |= DWC3_DCTL_KEEP_CONNECT;
4184                dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4185        }
4186
4187        return 0;
4188
4189err1:
4190        __dwc3_gadget_stop(dwc);
4191
4192err0:
4193        return ret;
4194}
4195
4196void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4197{
4198        if (dwc->pending_events) {
4199                dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4200                dwc->pending_events = false;
4201                enable_irq(dwc->irq_gadget);
4202        }
4203}
4204