Merge tag 'dwc3-for-v3.4' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 2 Mar 2012 23:56:33 +0000 (15:56 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 2 Mar 2012 23:56:33 +0000 (15:56 -0800)
usb: dwc3: changes for v3.4 merge window

Here are the changes for v3.4 merge window.

It includes a new glue layer for Samsung's Exynos platform, a simplification of
memory management on DWC3 driver by using dev_xxx functions, a few
optimizations to IRQ handling by dropping memcpy() and using bitshifts, a fix
for TI's OMAP5430 TX Fifo Allocation, two fixes on USB2 test mode
implementation (one on debugfs and one on ep0), and several minor changes such
as whitespace cleanups, simplification of a few parts of the code, decreasing a
long delay to something a bit saner, dropping a header which was included twice
and so on.

The highlight on this merge is the support for Samsung's Exynos platform,
increasing the number of different users for this driver to three.

Note that Samsung Exynos glue layer will only compile on platforms which
provide implementation for the clk API for now. Once Samsung supports
pm_runtime, that limitation can be dropped from the Makefile.

Conflicts:
drivers/usb/dwc3/gadget.c

1  2 
drivers/usb/dwc3/core.h
drivers/usb/dwc3/ep0.c
drivers/usb/dwc3/gadget.c
drivers/usb/dwc3/gadget.h

diff --combined drivers/usb/dwc3/core.h
index a72f42ffbbee9e44dbad8250bc9c2c182fcc6f65,f124643353bb6f95ff620c5e75ab656680d50d3b..6c7945b4cad323f6019919e40c68b2b0991d9867
  /* Bit fields */
  
  /* Global Configuration Register */
- #define DWC3_GCTL_PWRDNSCALE(n)       (n << 19)
+ #define DWC3_GCTL_PWRDNSCALE(n)       ((n) << 19)
  #define DWC3_GCTL_U2RSTECN    (1 << 16)
- #define DWC3_GCTL_RAMCLKSEL(x)        ((x & DWC3_GCTL_CLK_MASK) << 6)
+ #define DWC3_GCTL_RAMCLKSEL(x)        (((x) & DWC3_GCTL_CLK_MASK) << 6)
  #define DWC3_GCTL_CLK_BUS     (0)
  #define DWC3_GCTL_CLK_PIPE    (1)
  #define DWC3_GCTL_CLK_PIPEHALF        (2)
  #define DWC3_GCTL_CLK_MASK    (3)
  
  #define DWC3_GCTL_PRTCAP(n)   (((n) & (3 << 12)) >> 12)
- #define DWC3_GCTL_PRTCAPDIR(n)        (n << 12)
+ #define DWC3_GCTL_PRTCAPDIR(n)        ((n) << 12)
  #define DWC3_GCTL_PRTCAP_HOST 1
  #define DWC3_GCTL_PRTCAP_DEVICE       2
  #define DWC3_GCTL_PRTCAP_OTG  3
  
  #define DWC3_GCTL_CORESOFTRESET       (1 << 11)
- #define DWC3_GCTL_SCALEDOWN(n)        (n << 4)
+ #define DWC3_GCTL_SCALEDOWN(n)        ((n) << 4)
+ #define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3)
  #define DWC3_GCTL_DISSCRAMBLE (1 << 3)
  #define DWC3_GCTL_DSBLCLKGTNG (1 << 0)
  
  #define DWC3_GUSB3PIPECTL_PHYSOFTRST (1 << 31)
  #define DWC3_GUSB3PIPECTL_SUSPHY (1 << 17)
  
+ /* Global TX Fifo Size Register */
+ #define DWC3_GTXFIFOSIZ_TXFDEF(n) ((n) & 0xffff)
+ #define DWC3_GTXFIFOSIZ_TXFSTADDR(n) ((n) & 0xffff0000)
  /* Global HWPARAMS1 Register */
- #define DWC3_GHWPARAMS1_EN_PWROPT(n)  ((n & (3 << 24)) >> 24)
+ #define DWC3_GHWPARAMS1_EN_PWROPT(n)  (((n) & (3 << 24)) >> 24)
  #define DWC3_GHWPARAMS1_EN_PWROPT_NO  0
  #define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1
  
  
  #define DWC3_DCTL_APPL1RES    (1 << 23)
  
+ #define DWC3_DCTL_TRGTULST_MASK       (0x0f << 17)
+ #define DWC3_DCTL_TRGTULST(n) ((n) << 17)
+ #define DWC3_DCTL_TRGTULST_U2 (DWC3_DCTL_TRGTULST(2))
+ #define DWC3_DCTL_TRGTULST_U3 (DWC3_DCTL_TRGTULST(3))
+ #define DWC3_DCTL_TRGTULST_SS_DIS (DWC3_DCTL_TRGTULST(4))
+ #define DWC3_DCTL_TRGTULST_RX_DET (DWC3_DCTL_TRGTULST(5))
+ #define DWC3_DCTL_TRGTULST_SS_INACT (DWC3_DCTL_TRGTULST(6))
  #define DWC3_DCTL_INITU2ENA   (1 << 12)
  #define DWC3_DCTL_ACCEPTU2ENA (1 << 11)
  #define DWC3_DCTL_INITU1ENA   (1 << 10)
  
  /* Device Endpoint Command Register */
  #define DWC3_DEPCMD_PARAM_SHIFT               16
- #define DWC3_DEPCMD_PARAM(x)          (x << DWC3_DEPCMD_PARAM_SHIFT)
- #define DWC3_DEPCMD_GET_RSC_IDX(x)    ((x >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
+ #define DWC3_DEPCMD_PARAM(x)          ((x) << DWC3_DEPCMD_PARAM_SHIFT)
+ #define DWC3_DEPCMD_GET_RSC_IDX(x)     (((x) >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
  #define DWC3_DEPCMD_STATUS_MASK               (0x0f << 12)
- #define DWC3_DEPCMD_STATUS(x)         ((x & DWC3_DEPCMD_STATUS_MASK) >> 12)
+ #define DWC3_DEPCMD_STATUS(x)         (((x) & DWC3_DEPCMD_STATUS_MASK) >> 12)
  #define DWC3_DEPCMD_HIPRI_FORCERM     (1 << 11)
  #define DWC3_DEPCMD_CMDACT            (1 << 10)
  #define DWC3_DEPCMD_CMDIOC            (1 << 8)
  
  /* Structures */
  
- struct dwc3_trb_hw;
+ struct dwc3_trb;
  
  /**
   * struct dwc3_event_buffer - Software event buffer representation
@@@ -343,7 -357,7 +357,7 @@@ struct dwc3_ep 
        struct list_head        request_list;
        struct list_head        req_queued;
  
-       struct dwc3_trb_hw      *trb_pool;
+       struct dwc3_trb         *trb_pool;
        dma_addr_t              trb_pool_dma;
        u32                     free_slot;
        u32                     busy_slot;
@@@ -418,102 -432,49 +432,49 @@@ enum dwc3_device_state 
        DWC3_CONFIGURED_STATE,
  };
  
- /**
-  * struct dwc3_trb - transfer request block
-  * @bpl: lower 32bit of the buffer
-  * @bph: higher 32bit of the buffer
-  * @length: buffer size (up to 16mb - 1)
-  * @pcm1: packet count m1
-  * @trbsts: trb status
-  *    0 = ok
-  *    1 = missed isoc
-  *    2 = setup pending
-  * @hwo: hardware owner of descriptor
-  * @lst: last trb
-  * @chn: chain buffers
-  * @csp: continue on short packets (only supported on isoc eps)
-  * @trbctl: trb control
-  *    1 = normal
-  *    2 = control-setup
-  *    3 = control-status-2
-  *    4 = control-status-3
-  *    5 = control-data (first trb of data stage)
-  *    6 = isochronous-first (first trb of service interval)
-  *    7 = isochronous
-  *    8 = link trb
-  *    others = reserved
-  * @isp_imi: interrupt on short packet / interrupt on missed isoc
-  * @ioc: interrupt on complete
-  * @sid_sofn: Stream ID / SOF Number
-  */
- struct dwc3_trb {
-       u64             bplh;
-       union {
-               struct {
-                       u32             length:24;
-                       u32             pcm1:2;
-                       u32             reserved27_26:2;
-                       u32             trbsts:4;
- #define DWC3_TRB_STS_OKAY                       0
- #define DWC3_TRB_STS_MISSED_ISOC                1
- #define DWC3_TRB_STS_SETUP_PENDING              2
-               };
-               u32 len_pcm;
-       };
-       union {
-               struct {
-                       u32             hwo:1;
-                       u32             lst:1;
-                       u32             chn:1;
-                       u32             csp:1;
-                       u32             trbctl:6;
-                       u32             isp_imi:1;
-                       u32             ioc:1;
-                       u32             reserved13_12:2;
-                       u32             sid_sofn:16;
-                       u32             reserved31_30:2;
-               };
-               u32 control;
-       };
- } __packed;
+ /* TRB Length, PCM and Status */
+ #define DWC3_TRB_SIZE_MASK    (0x00ffffff)
+ #define DWC3_TRB_SIZE_LENGTH(n)       ((n) & DWC3_TRB_SIZE_MASK)
+ #define DWC3_TRB_SIZE_PCM1(n) (((n) & 0x03) << 24)
+ #define DWC3_TRB_SIZE_TRBSTS(n)       (((n) & (0x0f << 28) >> 28))
+ #define DWC3_TRBSTS_OK                        0
+ #define DWC3_TRBSTS_MISSED_ISOC               1
+ #define DWC3_TRBSTS_SETUP_PENDING     2
+ /* TRB Control */
+ #define DWC3_TRB_CTRL_HWO             (1 << 0)
+ #define DWC3_TRB_CTRL_LST             (1 << 1)
+ #define DWC3_TRB_CTRL_CHN             (1 << 2)
+ #define DWC3_TRB_CTRL_CSP             (1 << 3)
+ #define DWC3_TRB_CTRL_TRBCTL(n)               (((n) & 0x3f) << 4)
+ #define DWC3_TRB_CTRL_ISP_IMI         (1 << 10)
+ #define DWC3_TRB_CTRL_IOC             (1 << 11)
+ #define DWC3_TRB_CTRL_SID_SOFN(n)     (((n) & 0xffff) << 14)
+ #define DWC3_TRBCTL_NORMAL            DWC3_TRB_CTRL_TRBCTL(1)
+ #define DWC3_TRBCTL_CONTROL_SETUP     DWC3_TRB_CTRL_TRBCTL(2)
+ #define DWC3_TRBCTL_CONTROL_STATUS2   DWC3_TRB_CTRL_TRBCTL(3)
+ #define DWC3_TRBCTL_CONTROL_STATUS3   DWC3_TRB_CTRL_TRBCTL(4)
+ #define DWC3_TRBCTL_CONTROL_DATA      DWC3_TRB_CTRL_TRBCTL(5)
+ #define DWC3_TRBCTL_ISOCHRONOUS_FIRST DWC3_TRB_CTRL_TRBCTL(6)
+ #define DWC3_TRBCTL_ISOCHRONOUS               DWC3_TRB_CTRL_TRBCTL(7)
+ #define DWC3_TRBCTL_LINK_TRB          DWC3_TRB_CTRL_TRBCTL(8)
  
  /**
-  * struct dwc3_trb_hw - transfer request block (hw format)
+  * struct dwc3_trb - transfer request block (hw format)
   * @bpl: DW0-3
   * @bph: DW4-7
   * @size: DW8-B
   * @trl: DWC-F
   */
- struct dwc3_trb_hw {
-       __le32          bpl;
-       __le32          bph;
-       __le32          size;
-       __le32          ctrl;
+ struct dwc3_trb {
+       u32             bpl;
+       u32             bph;
+       u32             size;
+       u32             ctrl;
  } __packed;
  
- static inline void dwc3_trb_to_hw(struct dwc3_trb *nat, struct dwc3_trb_hw *hw)
- {
-       hw->bpl = cpu_to_le32(lower_32_bits(nat->bplh));
-       hw->bph = cpu_to_le32(upper_32_bits(nat->bplh));
-       hw->size = cpu_to_le32p(&nat->len_pcm);
-       /* HWO is written last */
-       hw->ctrl = cpu_to_le32p(&nat->control);
- }
- static inline void dwc3_trb_to_nat(struct dwc3_trb_hw *hw, struct dwc3_trb *nat)
- {
-       u64 bplh;
-       bplh = le32_to_cpup(&hw->bpl);
-       bplh |= (u64) le32_to_cpup(&hw->bph) << 32;
-       nat->bplh = bplh;
-       nat->len_pcm = le32_to_cpup(&hw->size);
-       nat->control = le32_to_cpup(&hw->ctrl);
- }
  /**
   * dwc3_hwparams - copy of HWPARAMS registers
   * @hwparams0 - GHWPARAMS0
@@@ -546,8 -507,13 +507,13 @@@ struct dwc3_hwparams 
  #define DWC3_MODE_DRD         2
  #define DWC3_MODE_HUB         3
  
+ #define DWC3_MDWIDTH(n)               (((n) & 0xff00) >> 8)
  /* HWPARAMS1 */
- #define DWC3_NUM_INT(n)       (((n) & (0x3f << 15)) >> 15)
+ #define DWC3_NUM_INT(n)               (((n) & (0x3f << 15)) >> 15)
+ /* HWPARAMS7 */
+ #define DWC3_RAM1_DEPTH(n)    ((n) & 0xffff)
  
  struct dwc3_request {
        struct usb_request      request;
        struct dwc3_ep          *dep;
  
        u8                      epnum;
-       struct dwc3_trb_hw      *trb;
+       struct dwc3_trb         *trb;
        dma_addr_t              trb_dma;
  
        unsigned                direction:1;
   * @ctrl_req_addr: dma address of ctrl_req
   * @ep0_trb: dma address of ep0_trb
   * @ep0_usb_req: dummy req used while handling STD USB requests
 - * @setup_buf_addr: dma address of setup_buf
   * @ep0_bounce_addr: dma address of ep0_bounce
   * @lock: for synchronizing
   * @dev: pointer to our struct device
   * @ep0_expect_in: true when we expect a DATA IN transfer
   * @start_config_issued: true when StartConfig command has been issued
   * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
+  * @needs_fifo_resize: not all users might want fifo resizing, flag it
+  * @resize_fifos: tells us it's ok to reconfigure our TxFIFO sizes.
   * @ep0_next_event: hold the next expected event
   * @ep0state: state of endpoint zero
   * @link_state: link state
   */
  struct dwc3 {
        struct usb_ctrlrequest  *ctrl_req;
-       struct dwc3_trb_hw      *ep0_trb;
+       struct dwc3_trb         *ep0_trb;
        void                    *ep0_bounce;
        u8                      *setup_buf;
        dma_addr_t              ctrl_req_addr;
        dma_addr_t              ep0_trb_addr;
 -      dma_addr_t              setup_buf_addr;
        dma_addr_t              ep0_bounce_addr;
        struct dwc3_request     ep0_usb_req;
        /* device lock */
        unsigned                start_config_issued:1;
        unsigned                setup_packet_pending:1;
        unsigned                delayed_status:1;
+       unsigned                needs_fifo_resize:1;
+       unsigned                resize_fifos:1;
  
        enum dwc3_ep0_next      ep0_next_event;
        enum dwc3_ep0_state     ep0state;
  
        struct dwc3_hwparams    hwparams;
        struct dentry           *root;
+       u8                      test_mode;
+       u8                      test_mode_nr;
  };
  
  /* -------------------------------------------------------------------------- */
  
- #define DWC3_TRBSTS_OK                        0
- #define DWC3_TRBSTS_MISSED_ISOC               1
- #define DWC3_TRBSTS_SETUP_PENDING     2
- #define DWC3_TRBCTL_NORMAL            1
- #define DWC3_TRBCTL_CONTROL_SETUP     2
- #define DWC3_TRBCTL_CONTROL_STATUS2   3
- #define DWC3_TRBCTL_CONTROL_STATUS3   4
- #define DWC3_TRBCTL_CONTROL_DATA      5
- #define DWC3_TRBCTL_ISOCHRONOUS_FIRST 6
- #define DWC3_TRBCTL_ISOCHRONOUS               7
- #define DWC3_TRBCTL_LINK_TRB          8
  /* -------------------------------------------------------------------------- */
  
  struct dwc3_event_type {
@@@ -717,9 -679,14 +677,14 @@@ struct dwc3_event_depevt 
        u32     endpoint_event:4;
        u32     reserved11_10:2;
        u32     status:4;
- #define DEPEVT_STATUS_BUSERR    (1 << 0)
- #define DEPEVT_STATUS_SHORT     (1 << 1)
- #define DEPEVT_STATUS_IOC       (1 << 2)
+ /* Within XferNotReady */
+ #define DEPEVT_STATUS_TRANSFER_ACTIVE (1 << 3)
+ /* Within XferComplete */
+ #define DEPEVT_STATUS_BUSERR  (1 << 0)
+ #define DEPEVT_STATUS_SHORT   (1 << 1)
+ #define DEPEVT_STATUS_IOC     (1 << 2)
  #define DEPEVT_STATUS_LST     (1 << 3)
  
  /* Stream event only */
@@@ -805,6 -772,7 +770,7 @@@ union dwc3_event 
  
  /* prototypes */
  void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
+ int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc);
  
  int dwc3_host_init(struct dwc3 *dwc);
  void dwc3_host_exit(struct dwc3 *dwc);
diff --combined drivers/usb/dwc3/ep0.c
index d5c568e91874834b5cc1369164946eda16e136be,73efdf6938a2acb92c0e246cc52e98b62a21188b..25910e251c04b4fb7ce0ef8bb9685573e859b9b9
@@@ -76,8 -76,7 +76,7 @@@ static int dwc3_ep0_start_trans(struct 
                u32 len, u32 type)
  {
        struct dwc3_gadget_ep_cmd_params params;
-       struct dwc3_trb_hw              *trb_hw;
-       struct dwc3_trb                 trb;
+       struct dwc3_trb                 *trb;
        struct dwc3_ep                  *dep;
  
        int                             ret;
                return 0;
        }
  
-       trb_hw = dwc->ep0_trb;
-       memset(&trb, 0, sizeof(trb));
+       trb = dwc->ep0_trb;
  
-       trb.trbctl = type;
-       trb.bplh = buf_dma;
-       trb.length = len;
+       trb->bpl = lower_32_bits(buf_dma);
+       trb->bph = upper_32_bits(buf_dma);
+       trb->size = len;
+       trb->ctrl = type;
  
-       trb.hwo = 1;
-       trb.lst = 1;
-       trb.ioc = 1;
-       trb.isp_imi = 1;
-       dwc3_trb_to_hw(&trb, trb_hw);
+       trb->ctrl |= (DWC3_TRB_CTRL_HWO
+                       | DWC3_TRB_CTRL_LST
+                       | DWC3_TRB_CTRL_IOC
+                       | DWC3_TRB_CTRL_ISP_IMI);
  
        memset(&params, 0, sizeof(params));
        params.param0 = upper_32_bits(dwc->ep0_trb_addr);
@@@ -302,7 -299,7 +299,7 @@@ static int dwc3_ep0_handle_status(struc
        dep = dwc->eps[0];
        dwc->ep0_usb_req.dep = dep;
        dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
 -      dwc->ep0_usb_req.request.dma = dwc->setup_buf_addr;
 +      dwc->ep0_usb_req.request.buf = dwc->setup_buf;
        dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
  
        return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
@@@ -315,9 -312,7 +312,7 @@@ static int dwc3_ep0_handle_feature(stru
        u32                     recip;
        u32                     wValue;
        u32                     wIndex;
-       u32                     reg;
        int                     ret;
-       u32                     mode;
  
        wValue = le16_to_cpu(ctrl->wValue);
        wIndex = le16_to_cpu(ctrl->wIndex);
                        if (!set)
                                return -EINVAL;
  
-                       mode = wIndex >> 8;
-                       reg = dwc3_readl(dwc->regs, DWC3_DCTL);
-                       reg &= ~DWC3_DCTL_TSTCTRL_MASK;
-                       switch (mode) {
-                       case TEST_J:
-                       case TEST_K:
-                       case TEST_SE0_NAK:
-                       case TEST_PACKET:
-                       case TEST_FORCE_EN:
-                               reg |= mode << 1;
-                               break;
-                       default:
-                               return -EINVAL;
-                       }
-                       dwc3_writel(dwc->regs, DWC3_DCTL, reg);
-                       break;
-               default:
-                       return -EINVAL;
+                       dwc->test_mode_nr = wIndex >> 8;
+                       dwc->test_mode = true;
                }
                break;
  
        case USB_RECIP_ENDPOINT:
                switch (wValue) {
                case USB_ENDPOINT_HALT:
-                       dep =  dwc3_wIndex_to_dep(dwc, wIndex);
+                       dep = dwc3_wIndex_to_dep(dwc, wIndex);
                        if (!dep)
                                return -EINVAL;
                        ret = __dwc3_gadget_ep_set_halt(dep, set);
@@@ -470,8 -448,11 +448,11 @@@ static int dwc3_ep0_set_config(struct d
        case DWC3_ADDRESS_STATE:
                ret = dwc3_ep0_delegate_req(dwc, ctrl);
                /* if the cfg matches and the cfg is non zero */
-               if (!ret && cfg)
+               if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
                        dwc->dev_state = DWC3_CONFIGURED_STATE;
+                       dwc->resize_fifos = true;
+                       dev_dbg(dwc->dev, "resize fifos flag SET\n");
+               }
                break;
  
        case DWC3_CONFIGURED_STATE:
@@@ -560,9 -541,10 +541,10 @@@ static void dwc3_ep0_complete_data(stru
  {
        struct dwc3_request     *r = NULL;
        struct usb_request      *ur;
-       struct dwc3_trb         trb;
+       struct dwc3_trb         *trb;
        struct dwc3_ep          *ep0;
        u32                     transferred;
+       u32                     length;
        u8                      epnum;
  
        epnum = event->endpoint_number;
        r = next_request(&ep0->request_list);
        ur = &r->request;
  
-       dwc3_trb_to_nat(dwc->ep0_trb, &trb);
+       trb = dwc->ep0_trb;
+       length = trb->size & DWC3_TRB_SIZE_MASK;
  
        if (dwc->ep0_bounced) {
                transferred = min_t(u32, ur->length,
-                               ep0->endpoint.maxpacket - trb.length);
+                               ep0->endpoint.maxpacket - length);
                memcpy(ur->buf, dwc->ep0_bounce, transferred);
                dwc->ep0_bounced = false;
        } else {
-               transferred = ur->length - trb.length;
+               transferred = ur->length - length;
                ur->actual += transferred;
        }
  
@@@ -614,6 -596,17 +596,17 @@@ static void dwc3_ep0_complete_req(struc
                dwc3_gadget_giveback(dep, r, 0);
        }
  
+       if (dwc->test_mode) {
+               int ret;
+               ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
+               if (ret < 0) {
+                       dev_dbg(dwc->dev, "Invalid Test #%d\n",
+                                       dwc->test_mode_nr);
+                       dwc3_ep0_stall_and_restart(dwc);
+               }
+       }
        dwc->ep0state = EP0_SETUP_PHASE;
        dwc3_ep0_out_start(dwc);
  }
@@@ -624,6 -617,7 +617,7 @@@ static void dwc3_ep0_xfer_complete(stru
        struct dwc3_ep          *dep = dwc->eps[event->endpoint_number];
  
        dep->flags &= ~DWC3_EP_BUSY;
+       dep->res_trans_idx = 0;
        dwc->setup_packet_pending = false;
  
        switch (dwc->ep0state) {
@@@ -679,12 -673,7 +673,12 @@@ static void dwc3_ep0_do_control_data(st
                                DWC3_TRBCTL_CONTROL_DATA);
        } else if ((req->request.length % dep->endpoint.maxpacket)
                        && (event->endpoint_number == 0)) {
 -              dwc3_map_buffer_to_dma(req);
 +              ret = usb_gadget_map_request(&dwc->gadget, &req->request,
 +                              event->endpoint_number);
 +              if (ret) {
 +                      dev_dbg(dwc->dev, "failed to map request\n");
 +                      return;
 +              }
  
                WARN_ON(req->request.length > dep->endpoint.maxpacket);
  
                                dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
                                DWC3_TRBCTL_CONTROL_DATA);
        } else {
 -              dwc3_map_buffer_to_dma(req);
 +              ret = usb_gadget_map_request(&dwc->gadget, &req->request,
 +                              event->endpoint_number);
 +              if (ret) {
 +                      dev_dbg(dwc->dev, "failed to map request\n");
 +                      return;
 +              }
  
                ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
                                req->request.dma, req->request.length,
@@@ -730,6 -714,12 +724,12 @@@ static void dwc3_ep0_do_control_status(
  {
        struct dwc3_ep          *dep = dwc->eps[epnum];
  
+       if (dwc->resize_fifos) {
+               dev_dbg(dwc->dev, "starting to resize fifos\n");
+               dwc3_gadget_resize_tx_fifos(dwc);
+               dwc->resize_fifos = 0;
+       }
        WARN_ON(dwc3_ep0_start_control_status(dep));
  }
  
index 1009e7e47a248f5f4b492889c8997ac6555a47ba,da317f6a8bce0bb3821ad659cc3c0148a54c174b..5255fe975ea1c5aa9e2ffecccd80618d5ad4332b
  #include "gadget.h"
  #include "io.h"
  
 -#define       DMA_ADDR_INVALID        (~(dma_addr_t)0)
 -
+ /**
+  * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
+  * @dwc: pointer to our context structure
+  * @mode: the mode to set (J, K SE0 NAK, Force Enable)
+  *
+  * Caller should take care of locking. This function will
+  * return 0 on success or -EINVAL if wrong Test Selector
+  * is passed
+  */
+ int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
+ {
+       u32             reg;
+       reg = dwc3_readl(dwc->regs, DWC3_DCTL);
+       reg &= ~DWC3_DCTL_TSTCTRL_MASK;
+       switch (mode) {
+       case TEST_J:
+       case TEST_K:
+       case TEST_SE0_NAK:
+       case TEST_PACKET:
+       case TEST_FORCE_EN:
+               reg |= mode << 1;
+               break;
+       default:
+               return -EINVAL;
+       }
+       dwc3_writel(dwc->regs, DWC3_DCTL, reg);
+       return 0;
+ }
+ /**
+  * dwc3_gadget_set_link_state - Sets USB Link to a particular State
+  * @dwc: pointer to our context structure
+  * @state: the state to put link into
+  *
+  * Caller should take care of locking. This function will
+  * return 0 on success or -ETIMEDOUT.
+  */
+ int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
+ {
+       int             retries = 10000;
+       u32             reg;
+       reg = dwc3_readl(dwc->regs, DWC3_DCTL);
+       reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
+       /* set requested state */
+       reg |= DWC3_DCTL_ULSTCHNGREQ(state);
+       dwc3_writel(dwc->regs, DWC3_DCTL, reg);
+       /* wait for a change in DSTS */
+       while (--retries) {
+               reg = dwc3_readl(dwc->regs, DWC3_DSTS);
+               if (DWC3_DSTS_USBLNKST(reg) == state)
+                       return 0;
+               udelay(5);
+       }
+       dev_vdbg(dwc->dev, "link state change request timed out\n");
+       return -ETIMEDOUT;
+ }
+ /**
+  * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
+  * @dwc: pointer to our context structure
+  *
+  * This function will a best effort FIFO allocation in order
+  * to improve FIFO usage and throughput, while still allowing
+  * us to enable as many endpoints as possible.
+  *
+  * Keep in mind that this operation will be highly dependent
+  * on the configured size for RAM1 - which contains TxFifo -,
+  * the amount of endpoints enabled on coreConsultant tool, and
+  * the width of the Master Bus.
+  *
+  * In the ideal world, we would always be able to satisfy the
+  * following equation:
+  *
+  * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
+  * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
+  *
+  * Unfortunately, due to many variables that's not always the case.
+  */
+ int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
+ {
+       int             last_fifo_depth = 0;
+       int             ram1_depth;
+       int             fifo_size;
+       int             mdwidth;
+       int             num;
+       if (!dwc->needs_fifo_resize)
+               return 0;
+       ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
+       mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
+       /* MDWIDTH is represented in bits, we need it in bytes */
+       mdwidth >>= 3;
+       /*
+        * FIXME For now we will only allocate 1 wMaxPacketSize space
+        * for each enabled endpoint, later patches will come to
+        * improve this algorithm so that we better use the internal
+        * FIFO space
+        */
+       for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
+               struct dwc3_ep  *dep = dwc->eps[num];
+               int             fifo_number = dep->number >> 1;
+               int             mult = 1;
+               int             tmp;
+               if (!(dep->number & 1))
+                       continue;
+               if (!(dep->flags & DWC3_EP_ENABLED))
+                       continue;
+               if (usb_endpoint_xfer_bulk(dep->desc)
+                               || usb_endpoint_xfer_isoc(dep->desc))
+                       mult = 3;
+               /*
+                * REVISIT: the following assumes we will always have enough
+                * space available on the FIFO RAM for all possible use cases.
+                * Make sure that's true somehow and change FIFO allocation
+                * accordingly.
+                *
+                * If we have Bulk or Isochronous endpoints, we want
+                * them to be able to be very, very fast. So we're giving
+                * those endpoints a fifo_size which is enough for 3 full
+                * packets
+                */
+               tmp = mult * (dep->endpoint.maxpacket + mdwidth);
+               tmp += mdwidth;
+               fifo_size = DIV_ROUND_UP(tmp, mdwidth);
+               fifo_size |= (last_fifo_depth << 16);
+               dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
+                               dep->name, last_fifo_depth, fifo_size & 0xffff);
+               dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
+                               fifo_size);
+               last_fifo_depth += (fifo_size & 0xffff);
+       }
+       return 0;
+ }
 -void dwc3_map_buffer_to_dma(struct dwc3_request *req)
 -{
 -      struct dwc3                     *dwc = req->dep->dwc;
 -
 -      if (req->request.length == 0) {
 -              /* req->request.dma = dwc->setup_buf_addr; */
 -              return;
 -      }
 -
 -      if (req->request.num_sgs) {
 -              int     mapped;
 -
 -              mapped = dma_map_sg(dwc->dev, req->request.sg,
 -                              req->request.num_sgs,
 -                              req->direction ? DMA_TO_DEVICE
 -                              : DMA_FROM_DEVICE);
 -              if (mapped < 0) {
 -                      dev_err(dwc->dev, "failed to map SGs\n");
 -                      return;
 -              }
 -
 -              req->request.num_mapped_sgs = mapped;
 -              return;
 -      }
 -
 -      if (req->request.dma == DMA_ADDR_INVALID) {
 -              req->request.dma = dma_map_single(dwc->dev, req->request.buf,
 -                              req->request.length, req->direction
 -                              ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
 -              req->mapped = true;
 -      }
 -}
 -
 -void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
 -{
 -      struct dwc3                     *dwc = req->dep->dwc;
 -
 -      if (req->request.length == 0) {
 -              req->request.dma = DMA_ADDR_INVALID;
 -              return;
 -      }
 -
 -      if (req->request.num_mapped_sgs) {
 -              req->request.dma = DMA_ADDR_INVALID;
 -              dma_unmap_sg(dwc->dev, req->request.sg,
 -                              req->request.num_mapped_sgs,
 -                              req->direction ? DMA_TO_DEVICE
 -                              : DMA_FROM_DEVICE);
 -
 -              req->request.num_mapped_sgs = 0;
 -              return;
 -      }
 -
 -      if (req->mapped) {
 -              dma_unmap_single(dwc->dev, req->request.dma,
 -                              req->request.length, req->direction
 -                              ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
 -              req->mapped = 0;
 -              req->request.dma = DMA_ADDR_INVALID;
 -      }
 -}
 -
  void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
                int status)
  {
        if (req->request.status == -EINPROGRESS)
                req->request.status = status;
  
 -      dwc3_unmap_buffer_from_dma(req);
 +      usb_gadget_unmap_request(&dwc->gadget, &req->request,
 +                      req->direction);
  
        dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
                        req, dep->name, req->request.actual,
                        req->request.length, status);
  
        spin_unlock(&dwc->lock);
 -      req->request.complete(&req->dep->endpoint, &req->request);
 +      req->request.complete(&dep->endpoint, &req->request);
        spin_lock(&dwc->lock);
  }
  
@@@ -156,7 -377,7 +314,7 @@@ int dwc3_send_gadget_ep_cmd(struct dwc
  }
  
  static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
-               struct dwc3_trb_hw *trb)
+               struct dwc3_trb *trb)
  {
        u32             offset = (char *) trb - (char *) dep->trb_pool;
  
@@@ -305,9 -526,8 +463,8 @@@ static int __dwc3_gadget_ep_enable(stru
                return ret;
  
        if (!(dep->flags & DWC3_EP_ENABLED)) {
-               struct dwc3_trb_hw      *trb_st_hw;
-               struct dwc3_trb_hw      *trb_link_hw;
-               struct dwc3_trb         trb_link;
+               struct dwc3_trb *trb_st_hw;
+               struct dwc3_trb *trb_link;
  
                ret = dwc3_gadget_set_xfer_resource(dwc, dep);
                if (ret)
  
                memset(&trb_link, 0, sizeof(trb_link));
  
-               /* Link TRB for ISOC. The HWO but is never reset */
+               /* Link TRB for ISOC. The HWO bit is never reset */
                trb_st_hw = &dep->trb_pool[0];
  
-               trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
-               trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
-               trb_link.hwo = true;
+               trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  
-               trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
-               dwc3_trb_to_hw(&trb_link, trb_link_hw);
+               trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
+               trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
+               trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
+               trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
        }
  
        return 0;
@@@ -377,7 -597,6 +534,7 @@@ static int __dwc3_gadget_ep_disable(str
  
        dep->stream_capable = false;
        dep->desc = NULL;
 +      dep->endpoint.desc = NULL;
        dep->comp_desc = NULL;
        dep->type = 0;
        dep->flags = 0;
@@@ -423,16 -642,16 +580,16 @@@ static int dwc3_gadget_ep_enable(struc
  
        switch (usb_endpoint_type(desc)) {
        case USB_ENDPOINT_XFER_CONTROL:
-               strncat(dep->name, "-control", sizeof(dep->name));
+               strlcat(dep->name, "-control", sizeof(dep->name));
                break;
        case USB_ENDPOINT_XFER_ISOC:
-               strncat(dep->name, "-isoc", sizeof(dep->name));
+               strlcat(dep->name, "-isoc", sizeof(dep->name));
                break;
        case USB_ENDPOINT_XFER_BULK:
-               strncat(dep->name, "-bulk", sizeof(dep->name));
+               strlcat(dep->name, "-bulk", sizeof(dep->name));
                break;
        case USB_ENDPOINT_XFER_INT:
-               strncat(dep->name, "-int", sizeof(dep->name));
+               strlcat(dep->name, "-int", sizeof(dep->name));
                break;
        default:
                dev_err(dwc->dev, "invalid endpoint transfer type\n");
@@@ -500,6 -719,7 +657,6 @@@ static struct usb_request *dwc3_gadget_
  
        req->epnum      = dep->number;
        req->dep        = dep;
 -      req->request.dma = DMA_ADDR_INVALID;
  
        return &req->request;
  }
@@@ -522,8 -742,7 +679,7 @@@ static void dwc3_prepare_one_trb(struc
                unsigned length, unsigned last, unsigned chain)
  {
        struct dwc3             *dwc = dep->dwc;
-       struct dwc3_trb_hw      *trb_hw;
-       struct dwc3_trb         trb;
+       struct dwc3_trb         *trb;
  
        unsigned int            cur_slot;
  
                        length, last ? " last" : "",
                        chain ? " chain" : "");
  
-       trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
+       trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
        cur_slot = dep->free_slot;
        dep->free_slot++;
  
                        usb_endpoint_xfer_isoc(dep->desc))
                return;
  
-       memset(&trb, 0, sizeof(trb));
        if (!req->trb) {
                dwc3_gadget_move_request_queued(req);
-               req->trb = trb_hw;
-               req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
+               req->trb = trb;
+               req->trb_dma = dwc3_trb_dma_offset(dep, trb);
        }
  
-       if (usb_endpoint_xfer_isoc(dep->desc)) {
-               trb.isp_imi = true;
-               trb.csp = true;
-       } else {
-               trb.chn = chain;
-               trb.lst = last;
-       }
-       if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
-               trb.sid_sofn = req->request.stream_id;
+       trb->size = DWC3_TRB_SIZE_LENGTH(length);
+       trb->bpl = lower_32_bits(dma);
+       trb->bph = upper_32_bits(dma);
  
        switch (usb_endpoint_type(dep->desc)) {
        case USB_ENDPOINT_XFER_CONTROL:
-               trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
+               trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
                break;
  
        case USB_ENDPOINT_XFER_ISOC:
-               trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
+               trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  
                /* IOC every DWC3_TRB_NUM / 4 so we can refill */
                if (!(cur_slot % (DWC3_TRB_NUM / 4)))
-                       trb.ioc = last;
+                       trb->ctrl |= DWC3_TRB_CTRL_IOC;
                break;
  
        case USB_ENDPOINT_XFER_BULK:
        case USB_ENDPOINT_XFER_INT:
-               trb.trbctl = DWC3_TRBCTL_NORMAL;
+               trb->ctrl = DWC3_TRBCTL_NORMAL;
                break;
        default:
                /*
                BUG();
        }
  
-       trb.length      = length;
-       trb.bplh        = dma;
-       trb.hwo         = true;
+       if (usb_endpoint_xfer_isoc(dep->desc)) {
+               trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
+               trb->ctrl |= DWC3_TRB_CTRL_CSP;
+       } else {
+               if (chain)
+                       trb->ctrl |= DWC3_TRB_CTRL_CHN;
+               if (last)
+                       trb->ctrl |= DWC3_TRB_CTRL_LST;
+       }
  
-       dwc3_trb_to_hw(&trb, trb_hw);
+       if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
+               trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
+       trb->ctrl |= DWC3_TRB_CTRL_HWO;
  }
  
  /*
   * @dep: endpoint for which requests are being prepared
   * @starting: true if the endpoint is idle and no requests are queued.
   *
-  * The functions goes through the requests list and setups TRBs for the
-  * transfers. The functions returns once there are not more TRBs available or
-  * it run out of requests.
+  * The function goes through the requests list and sets up TRBs for the
+  * transfers. The function returns once there are no more TRBs available or
+  * it runs out of requests.
   */
  static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
  {
        struct dwc3_request     *req, *n;
        u32                     trbs_left;
+       u32                     max;
        unsigned int            last_one = 0;
  
        BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
        /* the first request must not be queued */
        trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
  
+       /* Can't wrap around on a non-isoc EP since there's no link TRB */
+       if (!usb_endpoint_xfer_isoc(dep->desc)) {
+               max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
+               if (trbs_left > max)
+                       trbs_left = max;
+       }
        /*
-        * if busy & slot are equal than it is either full or empty. If we are
-        * starting to proceed requests then we are empty. Otherwise we ar
+        * If busy & slot are equal than it is either full or empty. If we are
+        * starting to process requests then we are empty. Otherwise we are
         * full and don't do anything
         */
        if (!trbs_left) {
                 * In case we start from scratch, we queue the ISOC requests
                 * starting from slot 1. This is done because we use ring
                 * buffer and have no LST bit to stop us. Instead, we place
-                * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
+                * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
                 * after the first request so we start at slot 1 and have
                 * 7 requests proceed before we hit the first IOC.
                 * Other transfer types don't use the ring buffer and are
                                length = sg_dma_len(s);
                                dma = sg_dma_address(s);
  
-                               if (i == (request->num_mapped_sgs - 1)
-                                               || sg_is_last(s)) {
+                               if (i == (request->num_mapped_sgs - 1) ||
+                                               sg_is_last(s)) {
                                        last_one = true;
                                        chain = false;
                                }
@@@ -729,8 -958,7 +895,7 @@@ static int __dwc3_gadget_kick_transfer(
                dwc3_prepare_trbs(dep, start_new);
  
                /*
-                * req points to the first request where HWO changed
-                * from 0 to 1
+                * req points to the first request where HWO changed from 0 to 1
                 */
                req = next_request(&dep->req_queued);
        }
                /*
                 * FIXME we need to iterate over the list of requests
                 * here and stop, unmap, free and del each of the linked
-                * requests instead of we do now.
+                * requests instead of what we do now.
                 */
 -              dwc3_unmap_buffer_from_dma(req);
 +              usb_gadget_unmap_request(&dwc->gadget, &req->request,
 +                              req->direction);
                list_del(&req->list);
                return ret;
        }
  
  static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  {
 +      struct dwc3             *dwc = dep->dwc;
 +      int                     ret;
 +
        req->request.actual     = 0;
        req->request.status     = -EINPROGRESS;
        req->direction          = dep->direction;
         * particular token from the Host side.
         *
         * This will also avoid Host cancelling URBs due to too
-        * many NACKs.
+        * many NAKs.
         */
 -      dwc3_map_buffer_to_dma(req);
 +      ret = usb_gadget_map_request(&dwc->gadget, &req->request,
 +                      dep->direction);
 +      if (ret)
 +              return ret;
 +
        list_add_tail(&req->list, &dep->request_list);
  
        /*
                int start_trans;
  
                start_trans = 1;
-               if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
-                               dep->flags & DWC3_EP_BUSY)
+               if (usb_endpoint_xfer_isoc(dep->desc) &&
+                               (dep->flags & DWC3_EP_BUSY))
                        start_trans = 0;
  
-               ret =  __dwc3_gadget_kick_transfer(dep, 0, start_trans);
+               ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
                if (ret && ret != -EBUSY) {
                        struct dwc3     *dwc = dep->dwc;
  
@@@ -976,8 -1196,12 +1141,12 @@@ out
  static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  {
        struct dwc3_ep                  *dep = to_dwc3_ep(ep);
+       struct dwc3                     *dwc = dep->dwc;
+       unsigned long                   flags;
  
+       spin_lock_irqsave(&dwc->lock, flags);
        dep->flags |= DWC3_EP_WEDGE;
+       spin_unlock_irqrestore(&dwc->lock, flags);
  
        return dwc3_gadget_ep_set_halt(ep, 1);
  }
@@@ -1067,26 -1291,20 +1236,20 @@@ static int dwc3_gadget_wakeup(struct us
                goto out;
        }
  
-       reg = dwc3_readl(dwc->regs, DWC3_DCTL);
-       /*
-        * Switch link state to Recovery. In HS/FS/LS this means
-        * RemoteWakeup Request
-        */
-       reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
-       dwc3_writel(dwc->regs, DWC3_DCTL, reg);
-       /* wait for at least 2000us */
-       usleep_range(2000, 2500);
+       ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
+       if (ret < 0) {
+               dev_err(dwc->dev, "failed to put link in Recovery\n");
+               goto out;
+       }
  
        /* write zeroes to Link Change Request */
        reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
        dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  
-       /* pool until Link State change to ON */
+       /* poll until Link State changes to ON */
        timeout = jiffies + msecs_to_jiffies(100);
  
-       while (!(time_after(jiffies, timeout))) {
+       while (!time_after(jiffies, timeout)) {
                reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  
                /* in HS, means ON */
@@@ -1109,8 -1327,11 +1272,11 @@@ static int dwc3_gadget_set_selfpowered(
                int is_selfpowered)
  {
        struct dwc3             *dwc = gadget_to_dwc(g);
+       unsigned long           flags;
  
+       spin_lock_irqsave(&dwc->lock, flags);
        dwc->is_selfpowered = !!is_selfpowered;
+       spin_unlock_irqrestore(&dwc->lock, flags);
  
        return 0;
  }
@@@ -1121,10 -1342,13 +1287,13 @@@ static void dwc3_gadget_run_stop(struc
        u32                     timeout = 500;
  
        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
-       if (is_on)
-               reg |= DWC3_DCTL_RUN_STOP;
-       else
+       if (is_on) {
+               reg &= ~DWC3_DCTL_TRGTULST_MASK;
+               reg |= (DWC3_DCTL_RUN_STOP
+                               | DWC3_DCTL_TRGTULST_RX_DET);
+       } else {
                reg &= ~DWC3_DCTL_RUN_STOP;
+       }
  
        dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  
@@@ -1331,7 -1555,7 +1500,7 @@@ static int dwc3_cleanup_done_reqs(struc
                const struct dwc3_event_depevt *event, int status)
  {
        struct dwc3_request     *req;
-       struct dwc3_trb         trb;
+       struct dwc3_trb         *trb;
        unsigned int            count;
        unsigned int            s_pkt = 0;
  
                        return 1;
                }
  
-               dwc3_trb_to_nat(req->trb, &trb);
+               trb = req->trb;
  
-               if (trb.hwo && status != -ESHUTDOWN)
+               if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
                        /*
                         * We continue despite the error. There is not much we
-                        * can do. If we don't clean in up we loop for ever. If
-                        * we skip the TRB than it gets overwritten reused after
-                        * a while since we use them in a ring buffer. a BUG()
-                        * would help. Lets hope that if this occures, someone
+                        * can do. If we don't clean it up we loop forever. If
+                        * we skip the TRB then it gets overwritten after a
+                        * while since we use them in a ring buffer. A BUG()
+                        * would help. Lets hope that if this occurs, someone
                         * fixes the root cause instead of looking away :)
                         */
                        dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
                                        dep->name, req->trb);
-               count = trb.length;
+               count = trb->size & DWC3_TRB_SIZE_MASK;
  
                if (dep->direction) {
                        if (count) {
                dwc3_gadget_giveback(dep, req, status);
                if (s_pkt)
                        break;
-               if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
+               if ((event->status & DEPEVT_STATUS_LST) &&
+                               (trb->ctrl & DWC3_TRB_CTRL_LST))
                        break;
-               if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
+               if ((event->status & DEPEVT_STATUS_IOC) &&
+                               (trb->ctrl & DWC3_TRB_CTRL_IOC))
                        break;
        } while (1);
  
-       if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
+       if ((event->status & DEPEVT_STATUS_IOC) &&
+                       (trb->ctrl & DWC3_TRB_CTRL_IOC))
                return 0;
        return 1;
  }
@@@ -1400,11 -1627,9 +1572,9 @@@ static void dwc3_endpoint_transfer_comp
        if (event->status & DEPEVT_STATUS_BUSERR)
                status = -ECONNRESET;
  
-       clean_busy =  dwc3_cleanup_done_reqs(dwc, dep, event, status);
-       if (clean_busy) {
+       clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
+       if (clean_busy)
                dep->flags &= ~DWC3_EP_BUSY;
-               dep->res_trans_idx = 0;
-       }
  
        /*
         * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
                struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  {
-       u32 uf;
+       u32 uf, mask;
  
        if (list_empty(&dep->request_list)) {
                dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
                return;
        }
  
-       if (event->parameters) {
-               u32 mask;
-               mask = ~(dep->interval - 1);
-               uf = event->parameters & mask;
-               /* 4 micro frames in the future */
-               uf += dep->interval * 4;
-       } else {
-               uf = 0;
-       }
+       mask = ~(dep->interval - 1);
+       uf = event->parameters & mask;
+       /* 4 micro frames in the future */
+       uf += dep->interval * 4;
  
        __dwc3_gadget_kick_transfer(dep, uf, 1);
  }
@@@ -1464,8 -1683,8 +1628,8 @@@ static void dwc3_process_ep_cmd_complet
        struct dwc3_event_depevt mod_ev = *event;
  
        /*
-        * We were asked to remove one requests. It is possible that this
-        * request and a few other were started together and have the same
+        * We were asked to remove one request. It is possible that this
+        * request and a few others were started together and have the same
         * transfer index. Since we stopped the complete endpoint we don't
         * know how many requests were already completed (and not yet)
         * reported and how could be done (later). We purge them all until
        mod_ev.status = DEPEVT_STATUS_LST;
        dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
        dep->flags &= ~DWC3_EP_BUSY;
-       /* pending requets are ignored and are queued on XferNotReady */
+       /* pending requests are ignored and are queued on XferNotReady */
  }
  
  static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
@@@ -1515,6 -1734,8 +1679,8 @@@ static void dwc3_endpoint_interrupt(str
  
        switch (event->endpoint_event) {
        case DWC3_DEPEVT_XFERCOMPLETE:
+               dep->res_trans_idx = 0;
                if (usb_endpoint_xfer_isoc(dep->desc)) {
                        dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
                                        dep->name);
                        int ret;
  
                        dev_vdbg(dwc->dev, "%s: reason %s\n",
-                                       dep->name, event->status
+                                       dep->name, event->status &
+                                       DEPEVT_STATUS_TRANSFER_ACTIVE
                                        ? "Transfer Active"
                                        : "Transfer Not Active");
  
@@@ -1750,6 -1972,7 +1917,7 @@@ static void dwc3_gadget_reset_interrupt
        reg = dwc3_readl(dwc->regs, DWC3_DCTL);
        reg &= ~DWC3_DCTL_TSTCTRL_MASK;
        dwc3_writel(dwc->regs, DWC3_DCTL, reg);
+       dwc->test_mode = false;
  
        dwc3_stop_active_transfers(dwc);
        dwc3_clear_stall_all_ep(dwc);
@@@ -2027,7 -2250,8 +2195,8 @@@ static irqreturn_t dwc3_process_event_b
        while (left > 0) {
                union dwc3_event event;
  
-               memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
+               event.raw = *(u32 *) (evt->buf + evt->lpos);
                dwc3_process_event_entry(dwc, &event);
                /*
                 * XXX we wrap around correctly to the next entry as almost all
@@@ -2068,7 -2292,7 +2237,7 @@@ static irqreturn_t dwc3_interrupt(int i
  
  /**
   * dwc3_gadget_init - Initializes gadget related registers
-  * @dwc: Pointer to out controller context structure
+  * @dwc: pointer to our controller context structure
   *
   * Returns 0 on success otherwise negative errno.
   */
@@@ -2094,8 -2318,9 +2263,8 @@@ int __devinit dwc3_gadget_init(struct d
                goto err1;
        }
  
 -      dwc->setup_buf = dma_alloc_coherent(dwc->dev,
 -                      sizeof(*dwc->setup_buf) * 2,
 -                      &dwc->setup_buf_addr, GFP_KERNEL);
 +      dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
 +                      GFP_KERNEL);
        if (!dwc->setup_buf) {
                dev_err(dwc->dev, "failed to allocate setup buffer\n");
                ret = -ENOMEM;
@@@ -2186,7 -2411,8 +2355,7 @@@ err4
                        dwc->ep0_bounce_addr);
  
  err3:
 -      dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
 -                      dwc->setup_buf, dwc->setup_buf_addr);
 +      kfree(dwc->setup_buf);
  
  err2:
        dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
@@@ -2215,7 -2441,8 +2384,7 @@@ void dwc3_gadget_exit(struct dwc3 *dwc
        dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
                        dwc->ep0_bounce_addr);
  
 -      dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
 -                      dwc->setup_buf, dwc->setup_buf_addr);
 +      kfree(dwc->setup_buf);
  
        dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
                        dwc->ep0_trb, dwc->ep0_trb_addr);
index 12f1e104977ff2a655eb02917f9fa2aea73656a3,152b6de0649d3115663361ce4b8d5fd1cc3fd6cb..a8600084348cf2013c07564740e364959876d0a2
@@@ -100,6 -100,9 +100,9 @@@ static inline void dwc3_gadget_move_req
  void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
                int status);
  
+ int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode);
+ int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state);
  void dwc3_ep0_interrupt(struct dwc3 *dwc,
                const struct dwc3_event_depevt *event);
  void dwc3_ep0_out_start(struct dwc3 *dwc);
@@@ -108,6 -111,8 +111,6 @@@ int dwc3_gadget_ep0_queue(struct usb_e
  int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value);
  int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
                unsigned cmd, struct dwc3_gadget_ep_cmd_params *params);
 -void dwc3_map_buffer_to_dma(struct dwc3_request *req);
 -void dwc3_unmap_buffer_from_dma(struct dwc3_request *req);
  
  /**
   * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW