/*
* Renesas USB driver
*
* Copyright (C) 2011 Renesas Solutions Corp.
* Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/scatterlist.h>
#include "./common.h"
#include "./pipe.h"
#define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
#define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
#define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
#define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
/*
* packet initialize
*/
void usbhs_pkt_init(struct usbhs_pkt *pkt)
{
pkt->dma = DMA_ADDR_INVALID;
INIT_LIST_HEAD(&pkt->node);
}
/*
* packet control function
*/
static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
{
struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
struct device *dev = usbhs_priv_to_dev(priv);
dev_err(dev, "null handler\n");
return -EINVAL;
}
static struct usbhs_pkt_handle usbhsf_null_handler = {
.prepare = usbhsf_null_handle,
.try_run = usbhsf_null_handle,
};
void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
struct usbhs_pkt_handle *handler,
void *buf, int len, int zero)
{
struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
struct device *dev = usbhs_priv_to_dev(priv);
unsigned long flags;
/******************** spin lock ********************/
usbhs_lock(priv, flags);
if (!handler) {
dev_err(dev, "no handler function\n");
handler = &usbhsf_null_handler;
}
list_del_init(&pkt->node);
list_add_tail(&pkt->node, &pipe->list);
pkt->pipe = pipe;
pkt->buf = buf;
pkt->handler = handler;
pkt->length = len;
pkt->zero = zero;
pkt->actual = 0;
usbhs_unlock(priv, flags);
/******************** spin unlock ******************/
usbhs_pkt_start(pipe);
}
static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
{
list_del_init(&pkt->node);
}
static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
{
if (list_empty(&pipe->list))
return NULL;
return list_entry(pipe->list.next, struct usbhs_pkt, node);
}
struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
{
struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
unsigned long flags;
/******************** spin lock ********************/
usbhs_lock(priv, flags);
if (!pkt)
pkt = __usbhsf_pkt_get(pipe);
if (pkt)
__usbhsf_pkt_del(pkt);
usbhs_unlock(priv, flags);
/******************** spin unlock ******************/
return pkt;
}
int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
{
struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
struct usbhs_pkt *pkt;
struct device *dev = usbhs_priv_to_dev(priv);
int (*func)(struct usbhs_pkt *pkt, int *is_done);
unsigned long flags;
int ret = 0;
int is_done = 0;
/******************** spin lock ********************/
usbhs_lock(priv, flags);
pkt = __usbhsf_pkt_get(pipe);
if (!pkt)
goto __usbhs_pkt_handler_end;
switch (type) {
case USBHSF_PKT_PREPARE:
func = pkt->handler->prepare;
break;
case USBHSF_PKT_TRY_RUN:
func = pkt->handler->try_run;
break;
case USBHSF_PKT_DMA_DONE:
func = pkt->handler->dma_done;
break;
default:
dev_err(dev, "unknown pkt hander\n");
goto __usbhs_pkt_handler_end;
}
ret = func(pkt, &is_done);
if (is_done)
__usbhsf_pkt_del(pkt);
__usbhs_pkt_handler_end: