usb: renesas_usbhs: divide data transfer functions
[pandora-kernel.git] / drivers / usb / renesas_usbhs / mod_gadget.c
index 9a5ac02..4a1d1fc 100644 (file)
@@ -27,6 +27,7 @@
 struct usbhsg_request {
        struct usb_request      req;
        struct list_head        node;
+       struct usbhs_pkt        pkt;
 };
 
 #define EP_NAME_SIZE 8
@@ -110,6 +111,10 @@ struct usbhsg_recip_handle {
 #define usbhsg_pipe_to_uep(p)          ((p)->mod_private)
 #define usbhsg_is_dcp(u)               ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
 
+#define usbhsg_ureq_to_pkt(u)          (&(u)->pkt)
+#define usbhsg_pkt_to_ureq(i)  \
+       container_of(i, struct usbhsg_request, pkt)
+
 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
 
 /* status */
@@ -118,6 +123,35 @@ struct usbhsg_recip_handle {
 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
 #define usbhsg_status_has(gp, b) (gp->status &   b)
 
+/*
+ *             usbhsg_trylock
+ *
+ * This driver don't use spin_try_lock
+ * to avoid warning of CONFIG_DEBUG_SPINLOCK
+ */
+static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
+                                 unsigned long *flags)
+{
+       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+
+       /* check spin lock status
+        * to avoid deadlock/nest */
+       if (spin_is_locked(lock))
+               return NULL;
+
+       spin_lock_irqsave(lock, *flags);
+
+       return lock;
+}
+
+static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
+{
+       if (!lock)
+               return;
+
+       spin_unlock_irqrestore(lock, *flags);
+}
+
 /*
  *             list push/pop
  */
@@ -159,9 +193,8 @@ static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
        struct device *dev = usbhsg_gpriv_to_dev(gpriv);
        struct usbhsg_request *ureq;
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
-       int is_locked;
        int ret = 0;
 
        if (!uep->handler) {
@@ -179,7 +212,7 @@ static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
         *   - usb_request :: complete
         *
         * But the caller of this function need not care about spinlock.
-        * This function is using spin_trylock_irqsave for it.
+        * This function is using usbhsg_trylock for it.
         * if "is_locked" is 1, this mean this function lock it.
         * but if it is 0, this mean it is already under spin lock.
         * see also
@@ -188,7 +221,8 @@ static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
         */
 
        /******************  spin try lock *******************/
-       is_locked = spin_trylock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
+
        ureq = usbhsg_queue_get(uep);
        if (ureq) {
                if (prepare)
@@ -196,8 +230,7 @@ static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
                else
                        ret = uep->handler->try_run(uep, ureq);
        }
-       if (is_locked)
-               spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ******************/
 
        return ret;
@@ -228,7 +261,7 @@ static void usbhsg_queue_pop(struct usbhsg_uep *uep,
         * It mean "usb_ep_ops :: queue" which is using spinlock is called
         * under spinlock.
         *
-        * To avoid dead-lock, this driver is using spin_trylock.
+        * To avoid dead-lock, this driver is using usbhsg_trylock.
         *   CAUTION [*endpoint queue*]
         *   CAUTION [*queue handler*]
         */
@@ -291,38 +324,32 @@ static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
        return 0;
 }
 
-static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
-                                     struct usbhsg_request *ureq)
+/*
+ *             packet send hander
+ */
+static void usbhsg_try_run_send_packet_bh(struct usbhs_pkt *pkt)
 {
-       struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
+       struct usbhs_pipe *pipe = pkt->pipe;
+       struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
+       struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
        struct usb_request *req = &ureq->req;
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
        struct device *dev = usbhsg_gpriv_to_dev(gpriv);
-       void *buf;
-       int remainder, send;
+       int remainder, send, maxp;
        int is_done = 0;
        int enable;
-       int maxp;
 
-       /*
-        *********  assume under spin lock  *********
-        */
-
-       maxp            = usbhs_pipe_get_maxpacket(pipe);
-       buf             = req->buf    + req->actual;
-       remainder       = req->length - req->actual;
-
-       send = usbhs_fifo_write(pipe, buf, remainder);
+       maxp            = pkt->maxp;
+       send            = pkt->actual;
+       remainder       = pkt->length;
 
        /*
-        * send < 0 : pipe busy
         * send = 0 : send zero packet
         * send > 0 : send data
         *
         * send <= max_packet
         */
-       if (send > 0)
-               req->actual += send;
+       req->actual += send;
 
        /* send all packet ? */
        if (send < remainder)
@@ -343,13 +370,6 @@ static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
        enable = !is_done;
        uep->handler->irq_mask(uep, enable);
 
-       /*
-        * usbhs_fifo_enable execute
-        *  - after callback_update,
-        *  - before queue_pop / stage_end
-        */
-       usbhs_fifo_enable(pipe);
-
        /*
         * all data were sent ?
         */
@@ -361,6 +381,30 @@ static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
 
                usbhsg_queue_pop(uep, ureq, 0);
        }
+}
+
+static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
+                                     struct usbhsg_request *ureq)
+{
+       struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
+       struct usb_request *req = &ureq->req;
+       struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
+       int ret;
+
+       /*
+        *********  assume under spin lock  *********
+        */
+
+       usbhs_pkt_update(pkt, pipe,
+                        req->buf    + req->actual,
+                        req->length - req->actual);
+
+       ret = usbhs_fifo_write(pkt);
+       if (ret < 0) {
+               /* pipe is busy.
+                * retry in interrupt */
+               uep->handler->irq_mask(uep, 1);
+       }
 
        return 0;
 }
@@ -380,35 +424,30 @@ static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
        return 0;
 }
 
-static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
-                                        struct usbhsg_request *ureq)
+/*
+ *             packet recv hander
+ */
+static void usbhsg_try_run_receive_packet_bh(struct usbhs_pkt *pkt)
 {
-       struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
+       struct usbhs_pipe *pipe = pkt->pipe;
+       struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
+       struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
        struct usb_request *req = &ureq->req;
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
        struct device *dev = usbhsg_gpriv_to_dev(gpriv);
-       void *buf;
-       int maxp;
-       int remainder, recv;
+       int remainder, recv, maxp;
        int is_done = 0;
 
-       /*
-        *********  assume under spin lock  *********
-        */
-
-       maxp            = usbhs_pipe_get_maxpacket(pipe);
-       buf             = req->buf    + req->actual;
-       remainder       = req->length - req->actual;
+       maxp            = pkt->maxp;
+       remainder       = pkt->length;
+       recv            = pkt->actual;
 
-       recv = usbhs_fifo_read(pipe, buf, remainder);
        /*
         * recv < 0  : pipe busy
         * recv >= 0 : receive data
         *
         * recv <= max_packet
         */
-       if (recv < 0)
-               return -EBUSY;
 
        /* update parameters */
        req->actual += recv;
@@ -426,11 +465,27 @@ static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
                int disable = 0;
 
                uep->handler->irq_mask(uep, disable);
-               usbhs_fifo_disable(pipe);
+               usbhs_pipe_disable(pipe);
                usbhsg_queue_pop(uep, ureq, 0);
        }
+}
 
-       return 0;
+static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
+                                        struct usbhsg_request *ureq)
+{
+       struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
+       struct usb_request *req = &ureq->req;
+       struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
+
+       /*
+        *********  assume under spin lock  *********
+        */
+
+       usbhs_pkt_update(pkt, pipe,
+                        req->buf    + req->actual,
+                        req->length - req->actual);
+
+       return usbhs_fifo_read(pkt);
 }
 
 static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
@@ -518,9 +573,9 @@ static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
        struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 
        if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
-               usbhs_fifo_disable(pipe);
+               usbhs_pipe_disable(pipe);
                usbhs_pipe_clear_sequence(pipe);
-               usbhs_fifo_enable(pipe);
+               usbhs_pipe_enable(pipe);
        }
 
        usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
@@ -555,6 +610,10 @@ static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
        char *msg;
 
        uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
+       if (!usbhsg_uep_to_pipe(uep)) {
+               dev_err(dev, "wrong recip request\n");
+               return -EINVAL;
+       }
 
        switch (recip) {
        case USB_RECIP_DEVICE:
@@ -663,7 +722,7 @@ static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
                ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
 
        if (ret < 0)
-               usbhs_fifo_stall(pipe);
+               usbhs_pipe_stall(pipe);
 
        return ret;
 }
@@ -771,7 +830,7 @@ static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
         *********  assume under spin lock  *********
         */
 
-       usbhs_fifo_disable(pipe);
+       usbhs_pipe_disable(pipe);
 
        /*
         * disable pipe irq
@@ -787,12 +846,18 @@ static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
                usbhsg_queue_pop(uep, ureq, -ECONNRESET);
        }
 
-       uep->pipe->mod_private  = NULL;
-       uep->pipe               = NULL;
-
        return 0;
 }
 
+static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
+{
+       int i;
+       struct usbhsg_uep *uep;
+
+       usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
+               uep->pipe = NULL;
+}
+
 /*
  *
  *             usb_ep_ops
@@ -805,12 +870,19 @@ static int usbhsg_ep_enable(struct usb_ep *ep,
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
        struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
        struct usbhs_pipe *pipe;
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
        int ret = -EIO;
 
+       /*
+        * if it already have pipe,
+        * nothing to do
+        */
+       if (uep->pipe)
+               return 0;
+
        /********************  spin lock ********************/
-       spin_lock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
 
        pipe = usbhs_pipe_malloc(priv, desc);
        if (pipe) {
@@ -825,7 +897,8 @@ static int usbhsg_ep_enable(struct usb_ep *ep,
 
                ret = 0;
        }
-       spin_unlock_irqrestore(lock, flags);
+
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ******************/
 
        return ret;
@@ -835,14 +908,16 @@ static int usbhsg_ep_disable(struct usb_ep *ep)
 {
        struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
        int ret;
 
        /********************  spin lock ********************/
-       spin_lock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
+
        ret = usbhsg_pipe_disable(uep);
-       spin_unlock_irqrestore(lock, flags);
+
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ******************/
 
        return ret;
@@ -877,10 +952,9 @@ static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
        struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
        struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
        int ret = 0;
-       int is_locked;
 
        /*
         * CAUTION [*endpoint queue*]
@@ -891,7 +965,7 @@ static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
         * it is already under spinlock on this driver.
         * but it is called frm usb driver, this function should call spinlock.
         *
-        * This function is using spin_trylock_irqsave to solve this issue.
+        * This function is using usbshg_trylock to solve this issue.
         * if "is_locked" is 1, this mean this function lock it.
         * but if it is 0, this mean it is already under spin lock.
         * see also
@@ -900,7 +974,7 @@ static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
         */
 
        /********************  spin lock ********************/
-       is_locked = spin_trylock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
 
        /* param check */
        if (usbhsg_is_not_connected(gpriv)      ||
@@ -910,8 +984,7 @@ static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
        else
                usbhsg_queue_push(uep, ureq);
 
-       if (is_locked)
-               spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ******************/
 
        usbhsg_queue_prepare(uep);
@@ -924,9 +997,8 @@ static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
        struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
        struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
-       int is_locked;
 
        /*
         * see
@@ -936,12 +1008,11 @@ static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
         */
 
        /********************  spin lock ********************/
-       is_locked = spin_trylock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
 
        usbhsg_queue_pop(uep, ureq, -ECONNRESET);
 
-       if (is_locked)
-               spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ******************/
 
        return 0;
@@ -953,10 +1024,9 @@ static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
        struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
        struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
        struct device *dev = usbhsg_gpriv_to_dev(gpriv);
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
        int ret = -EAGAIN;
-       int is_locked;
 
        /*
         * see
@@ -966,16 +1036,16 @@ static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
         */
 
        /********************  spin lock ********************/
-       is_locked = spin_trylock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
        if (!usbhsg_queue_get(uep)) {
 
                dev_dbg(dev, "set halt %d (pipe %d)\n",
                        halt, usbhs_pipe_number(pipe));
 
                if (halt)
-                       usbhs_fifo_stall(pipe);
+                       usbhs_pipe_stall(pipe);
                else
-                       usbhs_fifo_disable(pipe);
+                       usbhs_pipe_disable(pipe);
 
                if (halt && wedge)
                        usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
@@ -985,8 +1055,7 @@ static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
                ret = 0;
        }
 
-       if (is_locked)
-               spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ******************/
 
        return ret;
@@ -1025,11 +1094,11 @@ static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
        struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
        struct usbhs_mod *mod = usbhs_mod_get_current(priv);
        struct device *dev = usbhs_priv_to_dev(priv);
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
 
        /********************  spin lock ********************/
-       spin_lock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
 
        /*
         * enable interrupt and systems if ready
@@ -1044,7 +1113,10 @@ static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
        /*
         * pipe initialize and enable DCP
         */
-       usbhs_pipe_init(priv);
+       usbhs_pipe_init(priv,
+                       usbhsg_try_run_send_packet_bh,
+                       usbhsg_try_run_receive_packet_bh);
+       usbhsg_uep_init(gpriv);
        usbhsg_dcp_enable(dcp);
 
        /*
@@ -1069,7 +1141,7 @@ static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
        usbhs_irq_callback_update(priv, mod);
 
 usbhsg_try_start_unlock:
-       spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ********************/
 
        return 0;
@@ -1081,11 +1153,11 @@ static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
        struct usbhs_mod *mod = usbhs_mod_get_current(priv);
        struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
        struct device *dev = usbhs_priv_to_dev(priv);
-       spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
+       spinlock_t *lock;
        unsigned long flags;
 
        /********************  spin lock ********************/
-       spin_lock_irqsave(lock, flags);
+       lock = usbhsg_trylock(gpriv, &flags);
 
        /*
         * disable interrupt and systems if 1st try
@@ -1113,7 +1185,7 @@ static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
        usbhs_sys_function_ctrl(priv, 0);
        usbhs_sys_usb_ctrl(priv, 0);
 
-       spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
        /********************  spin unlock ********************/
 
        if (gpriv->driver &&
@@ -1125,7 +1197,7 @@ static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
        return 0;
 
 usbhsg_try_stop_unlock:
-       spin_unlock_irqrestore(lock, flags);
+       usbhsg_unlock(lock, &flags);
 
        return 0;
 }