xref: /linux/drivers/usb/gadget/udc/aspeed-vhub/epn.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
17ecca2a4SBenjamin Herrenschmidt // SPDX-License-Identifier: GPL-2.0+
27ecca2a4SBenjamin Herrenschmidt /*
37ecca2a4SBenjamin Herrenschmidt  * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
47ecca2a4SBenjamin Herrenschmidt  *
57ecca2a4SBenjamin Herrenschmidt  * epn.c - Generic endpoints management
67ecca2a4SBenjamin Herrenschmidt  *
77ecca2a4SBenjamin Herrenschmidt  * Copyright 2017 IBM Corporation
87ecca2a4SBenjamin Herrenschmidt  */
97ecca2a4SBenjamin Herrenschmidt 
107ecca2a4SBenjamin Herrenschmidt #include <linux/kernel.h>
117ecca2a4SBenjamin Herrenschmidt #include <linux/module.h>
127ecca2a4SBenjamin Herrenschmidt #include <linux/platform_device.h>
137ecca2a4SBenjamin Herrenschmidt #include <linux/delay.h>
147ecca2a4SBenjamin Herrenschmidt #include <linux/ioport.h>
157ecca2a4SBenjamin Herrenschmidt #include <linux/slab.h>
167ecca2a4SBenjamin Herrenschmidt #include <linux/errno.h>
177ecca2a4SBenjamin Herrenschmidt #include <linux/list.h>
187ecca2a4SBenjamin Herrenschmidt #include <linux/interrupt.h>
197ecca2a4SBenjamin Herrenschmidt #include <linux/proc_fs.h>
207ecca2a4SBenjamin Herrenschmidt #include <linux/prefetch.h>
217ecca2a4SBenjamin Herrenschmidt #include <linux/clk.h>
227ecca2a4SBenjamin Herrenschmidt #include <linux/usb/gadget.h>
237ecca2a4SBenjamin Herrenschmidt #include <linux/of.h>
247ecca2a4SBenjamin Herrenschmidt #include <linux/regmap.h>
257ecca2a4SBenjamin Herrenschmidt #include <linux/dma-mapping.h>
267ecca2a4SBenjamin Herrenschmidt 
277ecca2a4SBenjamin Herrenschmidt #include "vhub.h"
287ecca2a4SBenjamin Herrenschmidt 
297ecca2a4SBenjamin Herrenschmidt #define EXTRA_CHECKS
307ecca2a4SBenjamin Herrenschmidt 
317ecca2a4SBenjamin Herrenschmidt #ifdef EXTRA_CHECKS
327ecca2a4SBenjamin Herrenschmidt #define CHECK(ep, expr, fmt...)					\
337ecca2a4SBenjamin Herrenschmidt 	do {							\
347ecca2a4SBenjamin Herrenschmidt 		if (!(expr)) EPDBG(ep, "CHECK:" fmt);		\
357ecca2a4SBenjamin Herrenschmidt 	} while(0)
367ecca2a4SBenjamin Herrenschmidt #else
377ecca2a4SBenjamin Herrenschmidt #define CHECK(ep, expr, fmt...)	do { } while(0)
387ecca2a4SBenjamin Herrenschmidt #endif
397ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_kick(struct ast_vhub_ep * ep,struct ast_vhub_req * req)407ecca2a4SBenjamin Herrenschmidt static void ast_vhub_epn_kick(struct ast_vhub_ep *ep, struct ast_vhub_req *req)
417ecca2a4SBenjamin Herrenschmidt {
427ecca2a4SBenjamin Herrenschmidt 	unsigned int act = req->req.actual;
437ecca2a4SBenjamin Herrenschmidt 	unsigned int len = req->req.length;
447ecca2a4SBenjamin Herrenschmidt 	unsigned int chunk;
457ecca2a4SBenjamin Herrenschmidt 
467ecca2a4SBenjamin Herrenschmidt 	/* There should be no DMA ongoing */
477ecca2a4SBenjamin Herrenschmidt 	WARN_ON(req->active);
487ecca2a4SBenjamin Herrenschmidt 
497ecca2a4SBenjamin Herrenschmidt 	/* Calculate next chunk size */
507ecca2a4SBenjamin Herrenschmidt 	chunk = len - act;
517ecca2a4SBenjamin Herrenschmidt 	if (chunk > ep->ep.maxpacket)
527ecca2a4SBenjamin Herrenschmidt 		chunk = ep->ep.maxpacket;
537ecca2a4SBenjamin Herrenschmidt 	else if ((chunk < ep->ep.maxpacket) || !req->req.zero)
547ecca2a4SBenjamin Herrenschmidt 		req->last_desc = 1;
557ecca2a4SBenjamin Herrenschmidt 
567ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "kick req %p act=%d/%d chunk=%d last=%d\n",
577ecca2a4SBenjamin Herrenschmidt 	       req, act, len, chunk, req->last_desc);
587ecca2a4SBenjamin Herrenschmidt 
597ecca2a4SBenjamin Herrenschmidt 	/* If DMA unavailable, using staging EP buffer */
607ecca2a4SBenjamin Herrenschmidt 	if (!req->req.dma) {
617ecca2a4SBenjamin Herrenschmidt 
627ecca2a4SBenjamin Herrenschmidt 		/* For IN transfers, copy data over first */
63bb286336SBenjamin Herrenschmidt 		if (ep->epn.is_in) {
647ecca2a4SBenjamin Herrenschmidt 			memcpy(ep->buf, req->req.buf + act, chunk);
65bb286336SBenjamin Herrenschmidt 			vhub_dma_workaround(ep->buf);
66bb286336SBenjamin Herrenschmidt 		}
677ecca2a4SBenjamin Herrenschmidt 		writel(ep->buf_dma, ep->epn.regs + AST_VHUB_EP_DESC_BASE);
68bb286336SBenjamin Herrenschmidt 	} else {
69bb286336SBenjamin Herrenschmidt 		if (ep->epn.is_in)
70bb286336SBenjamin Herrenschmidt 			vhub_dma_workaround(req->req.buf);
717ecca2a4SBenjamin Herrenschmidt 		writel(req->req.dma + act, ep->epn.regs + AST_VHUB_EP_DESC_BASE);
72bb286336SBenjamin Herrenschmidt 	}
737ecca2a4SBenjamin Herrenschmidt 
747ecca2a4SBenjamin Herrenschmidt 	/* Start DMA */
757ecca2a4SBenjamin Herrenschmidt 	req->active = true;
767ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_EP_DMA_SET_TX_SIZE(chunk),
777ecca2a4SBenjamin Herrenschmidt 	       ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
787ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_EP_DMA_SET_TX_SIZE(chunk) | VHUB_EP_DMA_SINGLE_KICK,
797ecca2a4SBenjamin Herrenschmidt 	       ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
807ecca2a4SBenjamin Herrenschmidt }
817ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_handle_ack(struct ast_vhub_ep * ep)827ecca2a4SBenjamin Herrenschmidt static void ast_vhub_epn_handle_ack(struct ast_vhub_ep *ep)
837ecca2a4SBenjamin Herrenschmidt {
847ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_req *req;
857ecca2a4SBenjamin Herrenschmidt 	unsigned int len;
86*83045e19SHenry Tian 	int status = 0;
877ecca2a4SBenjamin Herrenschmidt 	u32 stat;
887ecca2a4SBenjamin Herrenschmidt 
897ecca2a4SBenjamin Herrenschmidt 	/* Read EP status */
907ecca2a4SBenjamin Herrenschmidt 	stat = readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
917ecca2a4SBenjamin Herrenschmidt 
927ecca2a4SBenjamin Herrenschmidt 	/* Grab current request if any */
937ecca2a4SBenjamin Herrenschmidt 	req = list_first_entry_or_null(&ep->queue, struct ast_vhub_req, queue);
947ecca2a4SBenjamin Herrenschmidt 
957ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "ACK status=%08x is_in=%d, req=%p (active=%d)\n",
967ecca2a4SBenjamin Herrenschmidt 	       stat, ep->epn.is_in, req, req ? req->active : 0);
977ecca2a4SBenjamin Herrenschmidt 
987ecca2a4SBenjamin Herrenschmidt 	/* In absence of a request, bail out, must have been dequeued */
997ecca2a4SBenjamin Herrenschmidt 	if (!req)
1007ecca2a4SBenjamin Herrenschmidt 		return;
1017ecca2a4SBenjamin Herrenschmidt 
1027ecca2a4SBenjamin Herrenschmidt 	/*
1037ecca2a4SBenjamin Herrenschmidt 	 * Request not active, move on to processing queue, active request
1047ecca2a4SBenjamin Herrenschmidt 	 * was probably dequeued
1057ecca2a4SBenjamin Herrenschmidt 	 */
1067ecca2a4SBenjamin Herrenschmidt 	if (!req->active)
1077ecca2a4SBenjamin Herrenschmidt 		goto next_chunk;
1087ecca2a4SBenjamin Herrenschmidt 
1097ecca2a4SBenjamin Herrenschmidt 	/* Check if HW has moved on */
1107ecca2a4SBenjamin Herrenschmidt 	if (VHUB_EP_DMA_RPTR(stat) != 0) {
1117ecca2a4SBenjamin Herrenschmidt 		EPDBG(ep, "DMA read pointer not 0 !\n");
1127ecca2a4SBenjamin Herrenschmidt 		return;
1137ecca2a4SBenjamin Herrenschmidt 	}
1147ecca2a4SBenjamin Herrenschmidt 
1157ecca2a4SBenjamin Herrenschmidt 	/* No current DMA ongoing */
1167ecca2a4SBenjamin Herrenschmidt 	req->active = false;
1177ecca2a4SBenjamin Herrenschmidt 
1184d8cd616SMatteo Croce 	/* Grab length out of HW */
1197ecca2a4SBenjamin Herrenschmidt 	len = VHUB_EP_DMA_TX_SIZE(stat);
1207ecca2a4SBenjamin Herrenschmidt 
1217ecca2a4SBenjamin Herrenschmidt 	/* If not using DMA, copy data out if needed */
122*83045e19SHenry Tian 	if (!req->req.dma && !ep->epn.is_in && len) {
123*83045e19SHenry Tian 		if (req->req.actual + len > req->req.length) {
124*83045e19SHenry Tian 			req->last_desc = 1;
125*83045e19SHenry Tian 			status = -EOVERFLOW;
126*83045e19SHenry Tian 			goto done;
127*83045e19SHenry Tian 		} else {
1287ecca2a4SBenjamin Herrenschmidt 			memcpy(req->req.buf + req->req.actual, ep->buf, len);
129*83045e19SHenry Tian 		}
130*83045e19SHenry Tian 	}
1317ecca2a4SBenjamin Herrenschmidt 	/* Adjust size */
1327ecca2a4SBenjamin Herrenschmidt 	req->req.actual += len;
1337ecca2a4SBenjamin Herrenschmidt 
1347ecca2a4SBenjamin Herrenschmidt 	/* Check for short packet */
1357ecca2a4SBenjamin Herrenschmidt 	if (len < ep->ep.maxpacket)
1367ecca2a4SBenjamin Herrenschmidt 		req->last_desc = 1;
1377ecca2a4SBenjamin Herrenschmidt 
138*83045e19SHenry Tian done:
1397ecca2a4SBenjamin Herrenschmidt 	/* That's it ? complete the request and pick a new one */
1407ecca2a4SBenjamin Herrenschmidt 	if (req->last_desc >= 0) {
141*83045e19SHenry Tian 		ast_vhub_done(ep, req, status);
1427ecca2a4SBenjamin Herrenschmidt 		req = list_first_entry_or_null(&ep->queue, struct ast_vhub_req,
1437ecca2a4SBenjamin Herrenschmidt 					       queue);
1447ecca2a4SBenjamin Herrenschmidt 
1457ecca2a4SBenjamin Herrenschmidt 		/*
1467ecca2a4SBenjamin Herrenschmidt 		 * Due to lock dropping inside "done" the next request could
1477ecca2a4SBenjamin Herrenschmidt 		 * already be active, so check for that and bail if needed.
1487ecca2a4SBenjamin Herrenschmidt 		 */
1497ecca2a4SBenjamin Herrenschmidt 		if (!req || req->active)
1507ecca2a4SBenjamin Herrenschmidt 			return;
1517ecca2a4SBenjamin Herrenschmidt 	}
1527ecca2a4SBenjamin Herrenschmidt 
1537ecca2a4SBenjamin Herrenschmidt  next_chunk:
1547ecca2a4SBenjamin Herrenschmidt 	ast_vhub_epn_kick(ep, req);
1557ecca2a4SBenjamin Herrenschmidt }
1567ecca2a4SBenjamin Herrenschmidt 
ast_vhub_count_free_descs(struct ast_vhub_ep * ep)1577ecca2a4SBenjamin Herrenschmidt static inline unsigned int ast_vhub_count_free_descs(struct ast_vhub_ep *ep)
1587ecca2a4SBenjamin Herrenschmidt {
1597ecca2a4SBenjamin Herrenschmidt 	/*
1607ecca2a4SBenjamin Herrenschmidt 	 * d_next == d_last means descriptor list empty to HW,
1617ecca2a4SBenjamin Herrenschmidt 	 * thus we can only have AST_VHUB_DESCS_COUNT-1 descriptors
1627ecca2a4SBenjamin Herrenschmidt 	 * in the list
1637ecca2a4SBenjamin Herrenschmidt 	 */
1647ecca2a4SBenjamin Herrenschmidt 	return (ep->epn.d_last + AST_VHUB_DESCS_COUNT - ep->epn.d_next - 1) &
1657ecca2a4SBenjamin Herrenschmidt 		(AST_VHUB_DESCS_COUNT - 1);
1667ecca2a4SBenjamin Herrenschmidt }
1677ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_kick_desc(struct ast_vhub_ep * ep,struct ast_vhub_req * req)1687ecca2a4SBenjamin Herrenschmidt static void ast_vhub_epn_kick_desc(struct ast_vhub_ep *ep,
1697ecca2a4SBenjamin Herrenschmidt 				   struct ast_vhub_req *req)
1707ecca2a4SBenjamin Herrenschmidt {
171bb286336SBenjamin Herrenschmidt 	struct ast_vhub_desc *desc = NULL;
1727ecca2a4SBenjamin Herrenschmidt 	unsigned int act = req->act_count;
1737ecca2a4SBenjamin Herrenschmidt 	unsigned int len = req->req.length;
1747ecca2a4SBenjamin Herrenschmidt 	unsigned int chunk;
1757ecca2a4SBenjamin Herrenschmidt 
1767ecca2a4SBenjamin Herrenschmidt 	/* Mark request active if not already */
1777ecca2a4SBenjamin Herrenschmidt 	req->active = true;
1787ecca2a4SBenjamin Herrenschmidt 
1797ecca2a4SBenjamin Herrenschmidt 	/* If the request was already completely written, do nothing */
1807ecca2a4SBenjamin Herrenschmidt 	if (req->last_desc >= 0)
1817ecca2a4SBenjamin Herrenschmidt 		return;
1827ecca2a4SBenjamin Herrenschmidt 
1837ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "kick act=%d/%d chunk_max=%d free_descs=%d\n",
1847ecca2a4SBenjamin Herrenschmidt 	       act, len, ep->epn.chunk_max, ast_vhub_count_free_descs(ep));
1857ecca2a4SBenjamin Herrenschmidt 
1867ecca2a4SBenjamin Herrenschmidt 	/* While we can create descriptors */
1877ecca2a4SBenjamin Herrenschmidt 	while (ast_vhub_count_free_descs(ep) && req->last_desc < 0) {
1887ecca2a4SBenjamin Herrenschmidt 		unsigned int d_num;
1897ecca2a4SBenjamin Herrenschmidt 
1907ecca2a4SBenjamin Herrenschmidt 		/* Grab next free descriptor */
1917ecca2a4SBenjamin Herrenschmidt 		d_num = ep->epn.d_next;
1927ecca2a4SBenjamin Herrenschmidt 		desc = &ep->epn.descs[d_num];
1937ecca2a4SBenjamin Herrenschmidt 		ep->epn.d_next = (d_num + 1) & (AST_VHUB_DESCS_COUNT - 1);
1947ecca2a4SBenjamin Herrenschmidt 
1957ecca2a4SBenjamin Herrenschmidt 		/* Calculate next chunk size */
1967ecca2a4SBenjamin Herrenschmidt 		chunk = len - act;
1977ecca2a4SBenjamin Herrenschmidt 		if (chunk <= ep->epn.chunk_max) {
1987ecca2a4SBenjamin Herrenschmidt 			/*
1997ecca2a4SBenjamin Herrenschmidt 			 * Is this the last packet ? Because of having up to 8
2007ecca2a4SBenjamin Herrenschmidt 			 * packets in a descriptor we can't just compare "chunk"
2017ecca2a4SBenjamin Herrenschmidt 			 * with ep.maxpacket. We have to see if it's a multiple
2027ecca2a4SBenjamin Herrenschmidt 			 * of it to know if we have to send a zero packet.
2037ecca2a4SBenjamin Herrenschmidt 			 * Sadly that involves a modulo which is a bit expensive
2047ecca2a4SBenjamin Herrenschmidt 			 * but probably still better than not doing it.
2057ecca2a4SBenjamin Herrenschmidt 			 */
2067ecca2a4SBenjamin Herrenschmidt 			if (!chunk || !req->req.zero || (chunk % ep->ep.maxpacket) != 0)
2077ecca2a4SBenjamin Herrenschmidt 				req->last_desc = d_num;
2087ecca2a4SBenjamin Herrenschmidt 		} else {
2097ecca2a4SBenjamin Herrenschmidt 			chunk = ep->epn.chunk_max;
2107ecca2a4SBenjamin Herrenschmidt 		}
2117ecca2a4SBenjamin Herrenschmidt 
2127ecca2a4SBenjamin Herrenschmidt 		EPVDBG(ep, " chunk: act=%d/%d chunk=%d last=%d desc=%d free=%d\n",
2137ecca2a4SBenjamin Herrenschmidt 		       act, len, chunk, req->last_desc, d_num,
2147ecca2a4SBenjamin Herrenschmidt 		       ast_vhub_count_free_descs(ep));
2157ecca2a4SBenjamin Herrenschmidt 
2167ecca2a4SBenjamin Herrenschmidt 		/* Populate descriptor */
2177ecca2a4SBenjamin Herrenschmidt 		desc->w0 = cpu_to_le32(req->req.dma + act);
2187ecca2a4SBenjamin Herrenschmidt 
2197ecca2a4SBenjamin Herrenschmidt 		/* Interrupt if end of request or no more descriptors */
2207ecca2a4SBenjamin Herrenschmidt 
2217ecca2a4SBenjamin Herrenschmidt 		/*
2227ecca2a4SBenjamin Herrenschmidt 		 * TODO: Be smarter about it, if we don't have enough
2237ecca2a4SBenjamin Herrenschmidt 		 * descriptors request an interrupt before queue empty
2247ecca2a4SBenjamin Herrenschmidt 		 * or so in order to be able to populate more before
2257ecca2a4SBenjamin Herrenschmidt 		 * the HW runs out. This isn't a problem at the moment
2267ecca2a4SBenjamin Herrenschmidt 		 * as we use 256 descriptors and only put at most one
2277ecca2a4SBenjamin Herrenschmidt 		 * request in the ring.
2287ecca2a4SBenjamin Herrenschmidt 		 */
2297ecca2a4SBenjamin Herrenschmidt 		desc->w1 = cpu_to_le32(VHUB_DSC1_IN_SET_LEN(chunk));
2307ecca2a4SBenjamin Herrenschmidt 		if (req->last_desc >= 0 || !ast_vhub_count_free_descs(ep))
2317ecca2a4SBenjamin Herrenschmidt 			desc->w1 |= cpu_to_le32(VHUB_DSC1_IN_INTERRUPT);
2327ecca2a4SBenjamin Herrenschmidt 
2337ecca2a4SBenjamin Herrenschmidt 		/* Account packet */
2347ecca2a4SBenjamin Herrenschmidt 		req->act_count = act = act + chunk;
2357ecca2a4SBenjamin Herrenschmidt 	}
2367ecca2a4SBenjamin Herrenschmidt 
237bb286336SBenjamin Herrenschmidt 	if (likely(desc))
238bb286336SBenjamin Herrenschmidt 		vhub_dma_workaround(desc);
239bb286336SBenjamin Herrenschmidt 
2407ecca2a4SBenjamin Herrenschmidt 	/* Tell HW about new descriptors */
2417ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_EP_DMA_SET_CPU_WPTR(ep->epn.d_next),
2427ecca2a4SBenjamin Herrenschmidt 	       ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
2437ecca2a4SBenjamin Herrenschmidt 
2447ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "HW kicked, d_next=%d dstat=%08x\n",
2457ecca2a4SBenjamin Herrenschmidt 	       ep->epn.d_next, readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS));
2467ecca2a4SBenjamin Herrenschmidt }
2477ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_handle_ack_desc(struct ast_vhub_ep * ep)2487ecca2a4SBenjamin Herrenschmidt static void ast_vhub_epn_handle_ack_desc(struct ast_vhub_ep *ep)
2497ecca2a4SBenjamin Herrenschmidt {
2507ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_req *req;
2517ecca2a4SBenjamin Herrenschmidt 	unsigned int len, d_last;
2527ecca2a4SBenjamin Herrenschmidt 	u32 stat, stat1;
2537ecca2a4SBenjamin Herrenschmidt 
2547ecca2a4SBenjamin Herrenschmidt 	/* Read EP status, workaround HW race */
2557ecca2a4SBenjamin Herrenschmidt 	do {
2567ecca2a4SBenjamin Herrenschmidt 		stat = readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
2577ecca2a4SBenjamin Herrenschmidt 		stat1 = readl(ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
2587ecca2a4SBenjamin Herrenschmidt 	} while(stat != stat1);
2597ecca2a4SBenjamin Herrenschmidt 
2607ecca2a4SBenjamin Herrenschmidt 	/* Extract RPTR */
2617ecca2a4SBenjamin Herrenschmidt 	d_last = VHUB_EP_DMA_RPTR(stat);
2627ecca2a4SBenjamin Herrenschmidt 
2637ecca2a4SBenjamin Herrenschmidt 	/* Grab current request if any */
2647ecca2a4SBenjamin Herrenschmidt 	req = list_first_entry_or_null(&ep->queue, struct ast_vhub_req, queue);
2657ecca2a4SBenjamin Herrenschmidt 
2667ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "ACK status=%08x is_in=%d ep->d_last=%d..%d\n",
2677ecca2a4SBenjamin Herrenschmidt 	       stat, ep->epn.is_in, ep->epn.d_last, d_last);
2687ecca2a4SBenjamin Herrenschmidt 
2697ecca2a4SBenjamin Herrenschmidt 	/* Check all completed descriptors */
2707ecca2a4SBenjamin Herrenschmidt 	while (ep->epn.d_last != d_last) {
2717ecca2a4SBenjamin Herrenschmidt 		struct ast_vhub_desc *desc;
2727ecca2a4SBenjamin Herrenschmidt 		unsigned int d_num;
2737ecca2a4SBenjamin Herrenschmidt 		bool is_last_desc;
2747ecca2a4SBenjamin Herrenschmidt 
2757ecca2a4SBenjamin Herrenschmidt 		/* Grab next completed descriptor */
2767ecca2a4SBenjamin Herrenschmidt 		d_num = ep->epn.d_last;
2777ecca2a4SBenjamin Herrenschmidt 		desc = &ep->epn.descs[d_num];
2787ecca2a4SBenjamin Herrenschmidt 		ep->epn.d_last = (d_num + 1) & (AST_VHUB_DESCS_COUNT - 1);
2797ecca2a4SBenjamin Herrenschmidt 
2807ecca2a4SBenjamin Herrenschmidt 		/* Grab len out of descriptor */
2817ecca2a4SBenjamin Herrenschmidt 		len = VHUB_DSC1_IN_LEN(le32_to_cpu(desc->w1));
2827ecca2a4SBenjamin Herrenschmidt 
2837ecca2a4SBenjamin Herrenschmidt 		EPVDBG(ep, " desc %d len=%d req=%p (act=%d)\n",
2847ecca2a4SBenjamin Herrenschmidt 		       d_num, len, req, req ? req->active : 0);
2857ecca2a4SBenjamin Herrenschmidt 
2867ecca2a4SBenjamin Herrenschmidt 		/* If no active request pending, move on */
2877ecca2a4SBenjamin Herrenschmidt 		if (!req || !req->active)
2887ecca2a4SBenjamin Herrenschmidt 			continue;
2897ecca2a4SBenjamin Herrenschmidt 
2907ecca2a4SBenjamin Herrenschmidt 		/* Adjust size */
2917ecca2a4SBenjamin Herrenschmidt 		req->req.actual += len;
2927ecca2a4SBenjamin Herrenschmidt 
2937ecca2a4SBenjamin Herrenschmidt 		/* Is that the last chunk ? */
2947ecca2a4SBenjamin Herrenschmidt 		is_last_desc = req->last_desc == d_num;
2957ecca2a4SBenjamin Herrenschmidt 		CHECK(ep, is_last_desc == (len < ep->ep.maxpacket ||
2967ecca2a4SBenjamin Herrenschmidt 					   (req->req.actual >= req->req.length &&
2977ecca2a4SBenjamin Herrenschmidt 					    !req->req.zero)),
2987ecca2a4SBenjamin Herrenschmidt 		      "Last packet discrepancy: last_desc=%d len=%d r.act=%d "
2997ecca2a4SBenjamin Herrenschmidt 		      "r.len=%d r.zero=%d mp=%d\n",
3007ecca2a4SBenjamin Herrenschmidt 		      is_last_desc, len, req->req.actual, req->req.length,
3017ecca2a4SBenjamin Herrenschmidt 		      req->req.zero, ep->ep.maxpacket);
3027ecca2a4SBenjamin Herrenschmidt 
3037ecca2a4SBenjamin Herrenschmidt 		if (is_last_desc) {
3047ecca2a4SBenjamin Herrenschmidt 			/*
3057ecca2a4SBenjamin Herrenschmidt 			 * Because we can only have one request at a time
3067ecca2a4SBenjamin Herrenschmidt 			 * in our descriptor list in this implementation,
3077ecca2a4SBenjamin Herrenschmidt 			 * d_last and ep->d_last should now be equal
3087ecca2a4SBenjamin Herrenschmidt 			 */
3097ecca2a4SBenjamin Herrenschmidt 			CHECK(ep, d_last == ep->epn.d_last,
3107ecca2a4SBenjamin Herrenschmidt 			      "DMA read ptr mismatch %d vs %d\n",
3117ecca2a4SBenjamin Herrenschmidt 			      d_last, ep->epn.d_last);
3127ecca2a4SBenjamin Herrenschmidt 
3137ecca2a4SBenjamin Herrenschmidt 			/* Note: done will drop and re-acquire the lock */
3147ecca2a4SBenjamin Herrenschmidt 			ast_vhub_done(ep, req, 0);
3157ecca2a4SBenjamin Herrenschmidt 			req = list_first_entry_or_null(&ep->queue,
3167ecca2a4SBenjamin Herrenschmidt 						       struct ast_vhub_req,
3177ecca2a4SBenjamin Herrenschmidt 						       queue);
3187ecca2a4SBenjamin Herrenschmidt 			break;
3197ecca2a4SBenjamin Herrenschmidt 		}
3207ecca2a4SBenjamin Herrenschmidt 	}
3217ecca2a4SBenjamin Herrenschmidt 
3227ecca2a4SBenjamin Herrenschmidt 	/* More work ? */
3237ecca2a4SBenjamin Herrenschmidt 	if (req)
3247ecca2a4SBenjamin Herrenschmidt 		ast_vhub_epn_kick_desc(ep, req);
3257ecca2a4SBenjamin Herrenschmidt }
3267ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_ack_irq(struct ast_vhub_ep * ep)3277ecca2a4SBenjamin Herrenschmidt void ast_vhub_epn_ack_irq(struct ast_vhub_ep *ep)
3287ecca2a4SBenjamin Herrenschmidt {
3297ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.desc_mode)
3307ecca2a4SBenjamin Herrenschmidt 		ast_vhub_epn_handle_ack_desc(ep);
3317ecca2a4SBenjamin Herrenschmidt 	else
3327ecca2a4SBenjamin Herrenschmidt 		ast_vhub_epn_handle_ack(ep);
3337ecca2a4SBenjamin Herrenschmidt }
3347ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_queue(struct usb_ep * u_ep,struct usb_request * u_req,gfp_t gfp_flags)3357ecca2a4SBenjamin Herrenschmidt static int ast_vhub_epn_queue(struct usb_ep* u_ep, struct usb_request *u_req,
3367ecca2a4SBenjamin Herrenschmidt 			      gfp_t gfp_flags)
3377ecca2a4SBenjamin Herrenschmidt {
3387ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_req *req = to_ast_req(u_req);
3397ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep = to_ast_ep(u_ep);
3407ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = ep->vhub;
3417ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
3427ecca2a4SBenjamin Herrenschmidt 	bool empty;
3437ecca2a4SBenjamin Herrenschmidt 	int rc;
3447ecca2a4SBenjamin Herrenschmidt 
3457ecca2a4SBenjamin Herrenschmidt 	/* Paranoid checks */
3467ecca2a4SBenjamin Herrenschmidt 	if (!u_req || !u_req->complete || !u_req->buf) {
3477ecca2a4SBenjamin Herrenschmidt 		dev_warn(&vhub->pdev->dev, "Bogus EPn request ! u_req=%p\n", u_req);
3487ecca2a4SBenjamin Herrenschmidt 		if (u_req) {
3497ecca2a4SBenjamin Herrenschmidt 			dev_warn(&vhub->pdev->dev, "complete=%p internal=%d\n",
3507ecca2a4SBenjamin Herrenschmidt 				 u_req->complete, req->internal);
3517ecca2a4SBenjamin Herrenschmidt 		}
3527ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
3537ecca2a4SBenjamin Herrenschmidt 	}
3547ecca2a4SBenjamin Herrenschmidt 
3557ecca2a4SBenjamin Herrenschmidt 	/* Endpoint enabled ? */
3567ecca2a4SBenjamin Herrenschmidt 	if (!ep->epn.enabled || !u_ep->desc || !ep->dev || !ep->d_idx ||
357ef9d3468SBenjamin Herrenschmidt 	    !ep->dev->enabled) {
3583c168909SColin Ian King 		EPDBG(ep, "Enqueuing request on wrong or disabled EP\n");
3597ecca2a4SBenjamin Herrenschmidt 		return -ESHUTDOWN;
3607ecca2a4SBenjamin Herrenschmidt 	}
3617ecca2a4SBenjamin Herrenschmidt 
3627ecca2a4SBenjamin Herrenschmidt 	/* Map request for DMA if possible. For now, the rule for DMA is
3637ecca2a4SBenjamin Herrenschmidt 	 * that:
3647ecca2a4SBenjamin Herrenschmidt 	 *
3657ecca2a4SBenjamin Herrenschmidt 	 *  * For single stage mode (no descriptors):
3667ecca2a4SBenjamin Herrenschmidt 	 *
3677ecca2a4SBenjamin Herrenschmidt 	 *   - The buffer is aligned to a 8 bytes boundary (HW requirement)
3687ecca2a4SBenjamin Herrenschmidt 	 *   - For a OUT endpoint, the request size is a multiple of the EP
3697ecca2a4SBenjamin Herrenschmidt 	 *     packet size (otherwise the controller will DMA past the end
3707ecca2a4SBenjamin Herrenschmidt 	 *     of the buffer if the host is sending a too long packet).
3717ecca2a4SBenjamin Herrenschmidt 	 *
3727ecca2a4SBenjamin Herrenschmidt 	 *  * For descriptor mode (tx only for now), always.
3737ecca2a4SBenjamin Herrenschmidt 	 *
3747ecca2a4SBenjamin Herrenschmidt 	 * We could relax the latter by making the decision to use the bounce
3757ecca2a4SBenjamin Herrenschmidt 	 * buffer based on the size of a given *segment* of the request rather
3767ecca2a4SBenjamin Herrenschmidt 	 * than the whole request.
3777ecca2a4SBenjamin Herrenschmidt 	 */
3787ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.desc_mode ||
3797ecca2a4SBenjamin Herrenschmidt 	    ((((unsigned long)u_req->buf & 7) == 0) &&
3807ecca2a4SBenjamin Herrenschmidt 	     (ep->epn.is_in || !(u_req->length & (u_ep->maxpacket - 1))))) {
381bd4d6070STao Ren 		rc = usb_gadget_map_request_by_dev(&vhub->pdev->dev, u_req,
3827ecca2a4SBenjamin Herrenschmidt 					    ep->epn.is_in);
3837ecca2a4SBenjamin Herrenschmidt 		if (rc) {
3847ecca2a4SBenjamin Herrenschmidt 			dev_warn(&vhub->pdev->dev,
3857ecca2a4SBenjamin Herrenschmidt 				 "Request mapping failure %d\n", rc);
3867ecca2a4SBenjamin Herrenschmidt 			return rc;
3877ecca2a4SBenjamin Herrenschmidt 		}
3887ecca2a4SBenjamin Herrenschmidt 	} else
3897ecca2a4SBenjamin Herrenschmidt 		u_req->dma = 0;
3907ecca2a4SBenjamin Herrenschmidt 
3917ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "enqueue req @%p\n", req);
3927ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, " l=%d dma=0x%x zero=%d noshort=%d noirq=%d is_in=%d\n",
3937ecca2a4SBenjamin Herrenschmidt 	       u_req->length, (u32)u_req->dma, u_req->zero,
3947ecca2a4SBenjamin Herrenschmidt 	       u_req->short_not_ok, u_req->no_interrupt,
3957ecca2a4SBenjamin Herrenschmidt 	       ep->epn.is_in);
3967ecca2a4SBenjamin Herrenschmidt 
3977ecca2a4SBenjamin Herrenschmidt 	/* Initialize request progress fields */
3987ecca2a4SBenjamin Herrenschmidt 	u_req->status = -EINPROGRESS;
3997ecca2a4SBenjamin Herrenschmidt 	u_req->actual = 0;
4007ecca2a4SBenjamin Herrenschmidt 	req->act_count = 0;
4017ecca2a4SBenjamin Herrenschmidt 	req->active = false;
4027ecca2a4SBenjamin Herrenschmidt 	req->last_desc = -1;
4037ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
4047ecca2a4SBenjamin Herrenschmidt 	empty = list_empty(&ep->queue);
4057ecca2a4SBenjamin Herrenschmidt 
4067ecca2a4SBenjamin Herrenschmidt 	/* Add request to list and kick processing if empty */
4077ecca2a4SBenjamin Herrenschmidt 	list_add_tail(&req->queue, &ep->queue);
4087ecca2a4SBenjamin Herrenschmidt 	if (empty) {
4097ecca2a4SBenjamin Herrenschmidt 		if (ep->epn.desc_mode)
4107ecca2a4SBenjamin Herrenschmidt 			ast_vhub_epn_kick_desc(ep, req);
4117ecca2a4SBenjamin Herrenschmidt 		else
4127ecca2a4SBenjamin Herrenschmidt 			ast_vhub_epn_kick(ep, req);
4137ecca2a4SBenjamin Herrenschmidt 	}
4147ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
4157ecca2a4SBenjamin Herrenschmidt 
4167ecca2a4SBenjamin Herrenschmidt 	return 0;
4177ecca2a4SBenjamin Herrenschmidt }
4187ecca2a4SBenjamin Herrenschmidt 
ast_vhub_stop_active_req(struct ast_vhub_ep * ep,bool restart_ep)4197ecca2a4SBenjamin Herrenschmidt static void ast_vhub_stop_active_req(struct ast_vhub_ep *ep,
4207ecca2a4SBenjamin Herrenschmidt 				     bool restart_ep)
4217ecca2a4SBenjamin Herrenschmidt {
4227ecca2a4SBenjamin Herrenschmidt 	u32 state, reg, loops;
4237ecca2a4SBenjamin Herrenschmidt 
4247ecca2a4SBenjamin Herrenschmidt 	/* Stop DMA activity */
4254e0dcf62SRyan Chen 	if (ep->epn.desc_mode)
4264e0dcf62SRyan Chen 		writel(VHUB_EP_DMA_CTRL_RESET, ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
4274e0dcf62SRyan Chen 	else
4287ecca2a4SBenjamin Herrenschmidt 		writel(0, ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
4297ecca2a4SBenjamin Herrenschmidt 
4307ecca2a4SBenjamin Herrenschmidt 	/* Wait for it to complete */
4317ecca2a4SBenjamin Herrenschmidt 	for (loops = 0; loops < 1000; loops++) {
4327ecca2a4SBenjamin Herrenschmidt 		state = readl(ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
4337ecca2a4SBenjamin Herrenschmidt 		state = VHUB_EP_DMA_PROC_STATUS(state);
4347ecca2a4SBenjamin Herrenschmidt 		if (state == EP_DMA_PROC_RX_IDLE ||
4357ecca2a4SBenjamin Herrenschmidt 		    state == EP_DMA_PROC_TX_IDLE)
4367ecca2a4SBenjamin Herrenschmidt 			break;
4377ecca2a4SBenjamin Herrenschmidt 		udelay(1);
4387ecca2a4SBenjamin Herrenschmidt 	}
4397ecca2a4SBenjamin Herrenschmidt 	if (loops >= 1000)
4407ecca2a4SBenjamin Herrenschmidt 		dev_warn(&ep->vhub->pdev->dev, "Timeout waiting for DMA\n");
4417ecca2a4SBenjamin Herrenschmidt 
4427ecca2a4SBenjamin Herrenschmidt 	/* If we don't have to restart the endpoint, that's it */
4437ecca2a4SBenjamin Herrenschmidt 	if (!restart_ep)
4447ecca2a4SBenjamin Herrenschmidt 		return;
4457ecca2a4SBenjamin Herrenschmidt 
4467ecca2a4SBenjamin Herrenschmidt 	/* Restart the endpoint */
4477ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.desc_mode) {
4487ecca2a4SBenjamin Herrenschmidt 		/*
4497ecca2a4SBenjamin Herrenschmidt 		 * Take out descriptors by resetting the DMA read
4507ecca2a4SBenjamin Herrenschmidt 		 * pointer to be equal to the CPU write pointer.
4517ecca2a4SBenjamin Herrenschmidt 		 *
4527ecca2a4SBenjamin Herrenschmidt 		 * Note: If we ever support creating descriptors for
4537ecca2a4SBenjamin Herrenschmidt 		 * requests that aren't the head of the queue, we
4547ecca2a4SBenjamin Herrenschmidt 		 * may have to do something more complex here,
4557ecca2a4SBenjamin Herrenschmidt 		 * especially if the request being taken out is
4567ecca2a4SBenjamin Herrenschmidt 		 * not the current head descriptors.
4577ecca2a4SBenjamin Herrenschmidt 		 */
4587ecca2a4SBenjamin Herrenschmidt 		reg = VHUB_EP_DMA_SET_RPTR(ep->epn.d_next) |
4597ecca2a4SBenjamin Herrenschmidt 			VHUB_EP_DMA_SET_CPU_WPTR(ep->epn.d_next);
4607ecca2a4SBenjamin Herrenschmidt 		writel(reg, ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
4617ecca2a4SBenjamin Herrenschmidt 
4627ecca2a4SBenjamin Herrenschmidt 		/* Then turn it back on */
4637ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.dma_conf,
4647ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
4657ecca2a4SBenjamin Herrenschmidt 	} else {
4667ecca2a4SBenjamin Herrenschmidt 		/* Single mode: just turn it back on */
4677ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.dma_conf,
4687ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
4697ecca2a4SBenjamin Herrenschmidt 	}
4707ecca2a4SBenjamin Herrenschmidt }
4717ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_dequeue(struct usb_ep * u_ep,struct usb_request * u_req)4727ecca2a4SBenjamin Herrenschmidt static int ast_vhub_epn_dequeue(struct usb_ep* u_ep, struct usb_request *u_req)
4737ecca2a4SBenjamin Herrenschmidt {
4747ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep = to_ast_ep(u_ep);
4757ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = ep->vhub;
4766163d499SJakob Koschel 	struct ast_vhub_req *req = NULL, *iter;
4777ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
4787ecca2a4SBenjamin Herrenschmidt 	int rc = -EINVAL;
4797ecca2a4SBenjamin Herrenschmidt 
4807ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
4817ecca2a4SBenjamin Herrenschmidt 
4827ecca2a4SBenjamin Herrenschmidt 	/* Make sure it's actually queued on this endpoint */
4836163d499SJakob Koschel 	list_for_each_entry(iter, &ep->queue, queue) {
4846163d499SJakob Koschel 		if (&iter->req != u_req)
4856163d499SJakob Koschel 			continue;
4866163d499SJakob Koschel 		req = iter;
4877ecca2a4SBenjamin Herrenschmidt 		break;
4887ecca2a4SBenjamin Herrenschmidt 	}
4897ecca2a4SBenjamin Herrenschmidt 
4906163d499SJakob Koschel 	if (req) {
4917ecca2a4SBenjamin Herrenschmidt 		EPVDBG(ep, "dequeue req @%p active=%d\n",
4927ecca2a4SBenjamin Herrenschmidt 		       req, req->active);
4937ecca2a4SBenjamin Herrenschmidt 		if (req->active)
4947ecca2a4SBenjamin Herrenschmidt 			ast_vhub_stop_active_req(ep, true);
4957ecca2a4SBenjamin Herrenschmidt 		ast_vhub_done(ep, req, -ECONNRESET);
4967ecca2a4SBenjamin Herrenschmidt 		rc = 0;
4977ecca2a4SBenjamin Herrenschmidt 	}
4987ecca2a4SBenjamin Herrenschmidt 
4997ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
5007ecca2a4SBenjamin Herrenschmidt 	return rc;
5017ecca2a4SBenjamin Herrenschmidt }
5027ecca2a4SBenjamin Herrenschmidt 
ast_vhub_update_epn_stall(struct ast_vhub_ep * ep)5037ecca2a4SBenjamin Herrenschmidt void ast_vhub_update_epn_stall(struct ast_vhub_ep *ep)
5047ecca2a4SBenjamin Herrenschmidt {
5057ecca2a4SBenjamin Herrenschmidt 	u32 reg;
5067ecca2a4SBenjamin Herrenschmidt 
5077ecca2a4SBenjamin Herrenschmidt 	if (WARN_ON(ep->d_idx == 0))
5087ecca2a4SBenjamin Herrenschmidt 		return;
5097ecca2a4SBenjamin Herrenschmidt 	reg = readl(ep->epn.regs + AST_VHUB_EP_CONFIG);
5107ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.stalled || ep->epn.wedged)
5117ecca2a4SBenjamin Herrenschmidt 		reg |= VHUB_EP_CFG_STALL_CTRL;
5127ecca2a4SBenjamin Herrenschmidt 	else
5137ecca2a4SBenjamin Herrenschmidt 		reg &= ~VHUB_EP_CFG_STALL_CTRL;
5147ecca2a4SBenjamin Herrenschmidt 	writel(reg, ep->epn.regs + AST_VHUB_EP_CONFIG);
5157ecca2a4SBenjamin Herrenschmidt 
5167ecca2a4SBenjamin Herrenschmidt 	if (!ep->epn.stalled && !ep->epn.wedged)
5177ecca2a4SBenjamin Herrenschmidt 		writel(VHUB_EP_TOGGLE_SET_EPNUM(ep->epn.g_idx),
5187ecca2a4SBenjamin Herrenschmidt 		       ep->vhub->regs + AST_VHUB_EP_TOGGLE);
5197ecca2a4SBenjamin Herrenschmidt }
5207ecca2a4SBenjamin Herrenschmidt 
ast_vhub_set_halt_and_wedge(struct usb_ep * u_ep,bool halt,bool wedge)5217ecca2a4SBenjamin Herrenschmidt static int ast_vhub_set_halt_and_wedge(struct usb_ep* u_ep, bool halt,
5227ecca2a4SBenjamin Herrenschmidt 				      bool wedge)
5237ecca2a4SBenjamin Herrenschmidt {
5247ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep = to_ast_ep(u_ep);
5257ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = ep->vhub;
5267ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
5277ecca2a4SBenjamin Herrenschmidt 
5287ecca2a4SBenjamin Herrenschmidt 	EPDBG(ep, "Set halt (%d) & wedge (%d)\n", halt, wedge);
5297ecca2a4SBenjamin Herrenschmidt 
5307ecca2a4SBenjamin Herrenschmidt 	if (!u_ep || !u_ep->desc)
5317ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
5327ecca2a4SBenjamin Herrenschmidt 	if (ep->d_idx == 0)
5337ecca2a4SBenjamin Herrenschmidt 		return 0;
5347ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.is_iso)
5357ecca2a4SBenjamin Herrenschmidt 		return -EOPNOTSUPP;
5367ecca2a4SBenjamin Herrenschmidt 
5377ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
5387ecca2a4SBenjamin Herrenschmidt 
5397ecca2a4SBenjamin Herrenschmidt 	/* Fail with still-busy IN endpoints */
5407ecca2a4SBenjamin Herrenschmidt 	if (halt && ep->epn.is_in && !list_empty(&ep->queue)) {
5417ecca2a4SBenjamin Herrenschmidt 		spin_unlock_irqrestore(&vhub->lock, flags);
5427ecca2a4SBenjamin Herrenschmidt 		return -EAGAIN;
5437ecca2a4SBenjamin Herrenschmidt 	}
5447ecca2a4SBenjamin Herrenschmidt 	ep->epn.stalled = halt;
5457ecca2a4SBenjamin Herrenschmidt 	ep->epn.wedged = wedge;
5467ecca2a4SBenjamin Herrenschmidt 	ast_vhub_update_epn_stall(ep);
5477ecca2a4SBenjamin Herrenschmidt 
5487ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
5497ecca2a4SBenjamin Herrenschmidt 
5507ecca2a4SBenjamin Herrenschmidt 	return 0;
5517ecca2a4SBenjamin Herrenschmidt }
5527ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_set_halt(struct usb_ep * u_ep,int value)5537ecca2a4SBenjamin Herrenschmidt static int ast_vhub_epn_set_halt(struct usb_ep *u_ep, int value)
5547ecca2a4SBenjamin Herrenschmidt {
5557ecca2a4SBenjamin Herrenschmidt 	return ast_vhub_set_halt_and_wedge(u_ep, value != 0, false);
5567ecca2a4SBenjamin Herrenschmidt }
5577ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_set_wedge(struct usb_ep * u_ep)5587ecca2a4SBenjamin Herrenschmidt static int ast_vhub_epn_set_wedge(struct usb_ep *u_ep)
5597ecca2a4SBenjamin Herrenschmidt {
5607ecca2a4SBenjamin Herrenschmidt 	return ast_vhub_set_halt_and_wedge(u_ep, true, true);
5617ecca2a4SBenjamin Herrenschmidt }
5627ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_disable(struct usb_ep * u_ep)5637ecca2a4SBenjamin Herrenschmidt static int ast_vhub_epn_disable(struct usb_ep* u_ep)
5647ecca2a4SBenjamin Herrenschmidt {
5657ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep = to_ast_ep(u_ep);
5667ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = ep->vhub;
5677ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
5687ecca2a4SBenjamin Herrenschmidt 	u32 imask, ep_ier;
5697ecca2a4SBenjamin Herrenschmidt 
5707ecca2a4SBenjamin Herrenschmidt 	EPDBG(ep, "Disabling !\n");
5717ecca2a4SBenjamin Herrenschmidt 
5727ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
5737ecca2a4SBenjamin Herrenschmidt 
5747ecca2a4SBenjamin Herrenschmidt 	ep->epn.enabled = false;
5757ecca2a4SBenjamin Herrenschmidt 
5767ecca2a4SBenjamin Herrenschmidt 	/* Stop active DMA if any */
5777ecca2a4SBenjamin Herrenschmidt 	ast_vhub_stop_active_req(ep, false);
5787ecca2a4SBenjamin Herrenschmidt 
5797ecca2a4SBenjamin Herrenschmidt 	/* Disable endpoint */
5807ecca2a4SBenjamin Herrenschmidt 	writel(0, ep->epn.regs + AST_VHUB_EP_CONFIG);
5817ecca2a4SBenjamin Herrenschmidt 
5827ecca2a4SBenjamin Herrenschmidt 	/* Disable ACK interrupt */
5837ecca2a4SBenjamin Herrenschmidt 	imask = VHUB_EP_IRQ(ep->epn.g_idx);
5847ecca2a4SBenjamin Herrenschmidt 	ep_ier = readl(vhub->regs + AST_VHUB_EP_ACK_IER);
5857ecca2a4SBenjamin Herrenschmidt 	ep_ier &= ~imask;
5867ecca2a4SBenjamin Herrenschmidt 	writel(ep_ier, vhub->regs + AST_VHUB_EP_ACK_IER);
5877ecca2a4SBenjamin Herrenschmidt 	writel(imask, vhub->regs + AST_VHUB_EP_ACK_ISR);
5887ecca2a4SBenjamin Herrenschmidt 
5897ecca2a4SBenjamin Herrenschmidt 	/* Nuke all pending requests */
5907ecca2a4SBenjamin Herrenschmidt 	ast_vhub_nuke(ep, -ESHUTDOWN);
5917ecca2a4SBenjamin Herrenschmidt 
5927ecca2a4SBenjamin Herrenschmidt 	/* No more descriptor associated with request */
5937ecca2a4SBenjamin Herrenschmidt 	ep->ep.desc = NULL;
5947ecca2a4SBenjamin Herrenschmidt 
5957ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
5967ecca2a4SBenjamin Herrenschmidt 
5977ecca2a4SBenjamin Herrenschmidt 	return 0;
5987ecca2a4SBenjamin Herrenschmidt }
5997ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_enable(struct usb_ep * u_ep,const struct usb_endpoint_descriptor * desc)6007ecca2a4SBenjamin Herrenschmidt static int ast_vhub_epn_enable(struct usb_ep* u_ep,
6017ecca2a4SBenjamin Herrenschmidt 			       const struct usb_endpoint_descriptor *desc)
6027ecca2a4SBenjamin Herrenschmidt {
6037ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep = to_ast_ep(u_ep);
6047ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_dev *dev;
6057ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub;
6067ecca2a4SBenjamin Herrenschmidt 	u16 maxpacket, type;
6077ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
6087ecca2a4SBenjamin Herrenschmidt 	u32 ep_conf, ep_ier, imask;
6097ecca2a4SBenjamin Herrenschmidt 
6107ecca2a4SBenjamin Herrenschmidt 	/* Check arguments */
6117ecca2a4SBenjamin Herrenschmidt 	if (!u_ep || !desc)
6127ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
6137ecca2a4SBenjamin Herrenschmidt 
6147ecca2a4SBenjamin Herrenschmidt 	maxpacket = usb_endpoint_maxp(desc);
6157ecca2a4SBenjamin Herrenschmidt 	if (!ep->d_idx || !ep->dev ||
6167ecca2a4SBenjamin Herrenschmidt 	    desc->bDescriptorType != USB_DT_ENDPOINT ||
6177ecca2a4SBenjamin Herrenschmidt 	    maxpacket == 0 || maxpacket > ep->ep.maxpacket) {
6187ecca2a4SBenjamin Herrenschmidt 		EPDBG(ep, "Invalid EP enable,d_idx=%d,dev=%p,type=%d,mp=%d/%d\n",
6197ecca2a4SBenjamin Herrenschmidt 		      ep->d_idx, ep->dev, desc->bDescriptorType,
6207ecca2a4SBenjamin Herrenschmidt 		      maxpacket, ep->ep.maxpacket);
6217ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
6227ecca2a4SBenjamin Herrenschmidt 	}
6237ecca2a4SBenjamin Herrenschmidt 	if (ep->d_idx != usb_endpoint_num(desc)) {
6247ecca2a4SBenjamin Herrenschmidt 		EPDBG(ep, "EP number mismatch !\n");
6257ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
6267ecca2a4SBenjamin Herrenschmidt 	}
6277ecca2a4SBenjamin Herrenschmidt 
6287ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.enabled) {
6297ecca2a4SBenjamin Herrenschmidt 		EPDBG(ep, "Already enabled\n");
6307ecca2a4SBenjamin Herrenschmidt 		return -EBUSY;
6317ecca2a4SBenjamin Herrenschmidt 	}
6327ecca2a4SBenjamin Herrenschmidt 	dev = ep->dev;
6337ecca2a4SBenjamin Herrenschmidt 	vhub = ep->vhub;
6347ecca2a4SBenjamin Herrenschmidt 
6357ecca2a4SBenjamin Herrenschmidt 	/* Check device state */
6367ecca2a4SBenjamin Herrenschmidt 	if (!dev->driver) {
6377ecca2a4SBenjamin Herrenschmidt 		EPDBG(ep, "Bogus device state: driver=%p speed=%d\n",
6387ecca2a4SBenjamin Herrenschmidt 		       dev->driver, dev->gadget.speed);
6397ecca2a4SBenjamin Herrenschmidt 		return -ESHUTDOWN;
6407ecca2a4SBenjamin Herrenschmidt 	}
6417ecca2a4SBenjamin Herrenschmidt 
6427ecca2a4SBenjamin Herrenschmidt 	/* Grab some info from the descriptor */
6437ecca2a4SBenjamin Herrenschmidt 	ep->epn.is_in = usb_endpoint_dir_in(desc);
6447ecca2a4SBenjamin Herrenschmidt 	ep->ep.maxpacket = maxpacket;
6457ecca2a4SBenjamin Herrenschmidt 	type = usb_endpoint_type(desc);
6467ecca2a4SBenjamin Herrenschmidt 	ep->epn.d_next = ep->epn.d_last = 0;
6477ecca2a4SBenjamin Herrenschmidt 	ep->epn.is_iso = false;
6487ecca2a4SBenjamin Herrenschmidt 	ep->epn.stalled = false;
6497ecca2a4SBenjamin Herrenschmidt 	ep->epn.wedged = false;
6507ecca2a4SBenjamin Herrenschmidt 
6517ecca2a4SBenjamin Herrenschmidt 	EPDBG(ep, "Enabling [%s] %s num %d maxpacket=%d\n",
6524d537f37SChunfeng Yun 	      ep->epn.is_in ? "in" : "out", usb_ep_type_string(type),
6537ecca2a4SBenjamin Herrenschmidt 	      usb_endpoint_num(desc), maxpacket);
6547ecca2a4SBenjamin Herrenschmidt 
6557ecca2a4SBenjamin Herrenschmidt 	/* Can we use DMA descriptor mode ? */
6567ecca2a4SBenjamin Herrenschmidt 	ep->epn.desc_mode = ep->epn.descs && ep->epn.is_in;
6577ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.desc_mode)
6587ecca2a4SBenjamin Herrenschmidt 		memset(ep->epn.descs, 0, 8 * AST_VHUB_DESCS_COUNT);
6597ecca2a4SBenjamin Herrenschmidt 
6607ecca2a4SBenjamin Herrenschmidt 	/*
6617ecca2a4SBenjamin Herrenschmidt 	 * Large send function can send up to 8 packets from
6627ecca2a4SBenjamin Herrenschmidt 	 * one descriptor with a limit of 4095 bytes.
6637ecca2a4SBenjamin Herrenschmidt 	 */
6647ecca2a4SBenjamin Herrenschmidt 	ep->epn.chunk_max = ep->ep.maxpacket;
6657ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.is_in) {
6667ecca2a4SBenjamin Herrenschmidt 		ep->epn.chunk_max <<= 3;
6677ecca2a4SBenjamin Herrenschmidt 		while (ep->epn.chunk_max > 4095)
6687ecca2a4SBenjamin Herrenschmidt 			ep->epn.chunk_max -= ep->ep.maxpacket;
6697ecca2a4SBenjamin Herrenschmidt 	}
6707ecca2a4SBenjamin Herrenschmidt 
6717ecca2a4SBenjamin Herrenschmidt 	switch(type) {
6727ecca2a4SBenjamin Herrenschmidt 	case USB_ENDPOINT_XFER_CONTROL:
6737ecca2a4SBenjamin Herrenschmidt 		EPDBG(ep, "Only one control endpoint\n");
6747ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
6757ecca2a4SBenjamin Herrenschmidt 	case USB_ENDPOINT_XFER_INT:
6767ecca2a4SBenjamin Herrenschmidt 		ep_conf = VHUB_EP_CFG_SET_TYPE(EP_TYPE_INT);
6777ecca2a4SBenjamin Herrenschmidt 		break;
6787ecca2a4SBenjamin Herrenschmidt 	case USB_ENDPOINT_XFER_BULK:
6797ecca2a4SBenjamin Herrenschmidt 		ep_conf = VHUB_EP_CFG_SET_TYPE(EP_TYPE_BULK);
6807ecca2a4SBenjamin Herrenschmidt 		break;
6817ecca2a4SBenjamin Herrenschmidt 	case USB_ENDPOINT_XFER_ISOC:
6827ecca2a4SBenjamin Herrenschmidt 		ep_conf = VHUB_EP_CFG_SET_TYPE(EP_TYPE_ISO);
6837ecca2a4SBenjamin Herrenschmidt 		ep->epn.is_iso = true;
6847ecca2a4SBenjamin Herrenschmidt 		break;
6857ecca2a4SBenjamin Herrenschmidt 	default:
6867ecca2a4SBenjamin Herrenschmidt 		return -EINVAL;
6877ecca2a4SBenjamin Herrenschmidt 	}
6887ecca2a4SBenjamin Herrenschmidt 
6897ecca2a4SBenjamin Herrenschmidt 	/* Encode the rest of the EP config register */
6907ecca2a4SBenjamin Herrenschmidt 	if (maxpacket < 1024)
6917ecca2a4SBenjamin Herrenschmidt 		ep_conf |= VHUB_EP_CFG_SET_MAX_PKT(maxpacket);
6927ecca2a4SBenjamin Herrenschmidt 	if (!ep->epn.is_in)
6937ecca2a4SBenjamin Herrenschmidt 		ep_conf |= VHUB_EP_CFG_DIR_OUT;
6947ecca2a4SBenjamin Herrenschmidt 	ep_conf |= VHUB_EP_CFG_SET_EP_NUM(usb_endpoint_num(desc));
6957ecca2a4SBenjamin Herrenschmidt 	ep_conf |= VHUB_EP_CFG_ENABLE;
6967ecca2a4SBenjamin Herrenschmidt 	ep_conf |= VHUB_EP_CFG_SET_DEV(dev->index + 1);
6977ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "config=%08x\n", ep_conf);
6987ecca2a4SBenjamin Herrenschmidt 
6997ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
7007ecca2a4SBenjamin Herrenschmidt 
7017ecca2a4SBenjamin Herrenschmidt 	/* Disable HW and reset DMA */
7027ecca2a4SBenjamin Herrenschmidt 	writel(0, ep->epn.regs + AST_VHUB_EP_CONFIG);
7037ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_EP_DMA_CTRL_RESET,
7047ecca2a4SBenjamin Herrenschmidt 	       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
7057ecca2a4SBenjamin Herrenschmidt 
7067ecca2a4SBenjamin Herrenschmidt 	/* Configure and enable */
7077ecca2a4SBenjamin Herrenschmidt 	writel(ep_conf, ep->epn.regs + AST_VHUB_EP_CONFIG);
7087ecca2a4SBenjamin Herrenschmidt 
7097ecca2a4SBenjamin Herrenschmidt 	if (ep->epn.desc_mode) {
7107ecca2a4SBenjamin Herrenschmidt 		/* Clear DMA status, including the DMA read ptr */
7117ecca2a4SBenjamin Herrenschmidt 		writel(0, ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
7127ecca2a4SBenjamin Herrenschmidt 
7137ecca2a4SBenjamin Herrenschmidt 		/* Set descriptor base */
7147ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.descs_dma,
7157ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DESC_BASE);
7167ecca2a4SBenjamin Herrenschmidt 
7177ecca2a4SBenjamin Herrenschmidt 		/* Set base DMA config value */
7187ecca2a4SBenjamin Herrenschmidt 		ep->epn.dma_conf = VHUB_EP_DMA_DESC_MODE;
7197ecca2a4SBenjamin Herrenschmidt 		if (ep->epn.is_in)
7207ecca2a4SBenjamin Herrenschmidt 			ep->epn.dma_conf |= VHUB_EP_DMA_IN_LONG_MODE;
7217ecca2a4SBenjamin Herrenschmidt 
7227ecca2a4SBenjamin Herrenschmidt 		/* First reset and disable all operations */
7237ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.dma_conf | VHUB_EP_DMA_CTRL_RESET,
7247ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
7257ecca2a4SBenjamin Herrenschmidt 
7267ecca2a4SBenjamin Herrenschmidt 		/* Enable descriptor mode */
7277ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.dma_conf,
7287ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
7297ecca2a4SBenjamin Herrenschmidt 	} else {
7307ecca2a4SBenjamin Herrenschmidt 		/* Set base DMA config value */
7317ecca2a4SBenjamin Herrenschmidt 		ep->epn.dma_conf = VHUB_EP_DMA_SINGLE_STAGE;
7327ecca2a4SBenjamin Herrenschmidt 
7337ecca2a4SBenjamin Herrenschmidt 		/* Reset and switch to single stage mode */
7347ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.dma_conf | VHUB_EP_DMA_CTRL_RESET,
7357ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
7367ecca2a4SBenjamin Herrenschmidt 		writel(ep->epn.dma_conf,
7377ecca2a4SBenjamin Herrenschmidt 		       ep->epn.regs + AST_VHUB_EP_DMA_CTLSTAT);
7387ecca2a4SBenjamin Herrenschmidt 		writel(0, ep->epn.regs + AST_VHUB_EP_DESC_STATUS);
7397ecca2a4SBenjamin Herrenschmidt 	}
7407ecca2a4SBenjamin Herrenschmidt 
7417ecca2a4SBenjamin Herrenschmidt 	/* Cleanup data toggle just in case */
7427ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_EP_TOGGLE_SET_EPNUM(ep->epn.g_idx),
7437ecca2a4SBenjamin Herrenschmidt 	       vhub->regs + AST_VHUB_EP_TOGGLE);
7447ecca2a4SBenjamin Herrenschmidt 
7457ecca2a4SBenjamin Herrenschmidt 	/* Cleanup and enable ACK interrupt */
7467ecca2a4SBenjamin Herrenschmidt 	imask = VHUB_EP_IRQ(ep->epn.g_idx);
7477ecca2a4SBenjamin Herrenschmidt 	writel(imask, vhub->regs + AST_VHUB_EP_ACK_ISR);
7487ecca2a4SBenjamin Herrenschmidt 	ep_ier = readl(vhub->regs + AST_VHUB_EP_ACK_IER);
7497ecca2a4SBenjamin Herrenschmidt 	ep_ier |= imask;
7507ecca2a4SBenjamin Herrenschmidt 	writel(ep_ier, vhub->regs + AST_VHUB_EP_ACK_IER);
7517ecca2a4SBenjamin Herrenschmidt 
7527ecca2a4SBenjamin Herrenschmidt 	/* Woot, we are online ! */
7537ecca2a4SBenjamin Herrenschmidt 	ep->epn.enabled = true;
7547ecca2a4SBenjamin Herrenschmidt 
7557ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
7567ecca2a4SBenjamin Herrenschmidt 
7577ecca2a4SBenjamin Herrenschmidt 	return 0;
7587ecca2a4SBenjamin Herrenschmidt }
7597ecca2a4SBenjamin Herrenschmidt 
ast_vhub_epn_dispose(struct usb_ep * u_ep)7607ecca2a4SBenjamin Herrenschmidt static void ast_vhub_epn_dispose(struct usb_ep *u_ep)
7617ecca2a4SBenjamin Herrenschmidt {
7627ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep = to_ast_ep(u_ep);
7637ecca2a4SBenjamin Herrenschmidt 
7647ecca2a4SBenjamin Herrenschmidt 	if (WARN_ON(!ep->dev || !ep->d_idx))
7657ecca2a4SBenjamin Herrenschmidt 		return;
7667ecca2a4SBenjamin Herrenschmidt 
7677ecca2a4SBenjamin Herrenschmidt 	EPDBG(ep, "Releasing endpoint\n");
7687ecca2a4SBenjamin Herrenschmidt 
7697ecca2a4SBenjamin Herrenschmidt 	/* Take it out of the EP list */
7707ecca2a4SBenjamin Herrenschmidt 	list_del_init(&ep->ep.ep_list);
7717ecca2a4SBenjamin Herrenschmidt 
7727ecca2a4SBenjamin Herrenschmidt 	/* Mark the address free in the device */
7737ecca2a4SBenjamin Herrenschmidt 	ep->dev->epns[ep->d_idx - 1] = NULL;
7747ecca2a4SBenjamin Herrenschmidt 
7757ecca2a4SBenjamin Herrenschmidt 	/* Free name & DMA buffers */
7767ecca2a4SBenjamin Herrenschmidt 	kfree(ep->ep.name);
7777ecca2a4SBenjamin Herrenschmidt 	ep->ep.name = NULL;
7787ecca2a4SBenjamin Herrenschmidt 	dma_free_coherent(&ep->vhub->pdev->dev,
7797ecca2a4SBenjamin Herrenschmidt 			  AST_VHUB_EPn_MAX_PACKET +
7807ecca2a4SBenjamin Herrenschmidt 			  8 * AST_VHUB_DESCS_COUNT,
7817ecca2a4SBenjamin Herrenschmidt 			  ep->buf, ep->buf_dma);
7827ecca2a4SBenjamin Herrenschmidt 	ep->buf = NULL;
7837ecca2a4SBenjamin Herrenschmidt 	ep->epn.descs = NULL;
7847ecca2a4SBenjamin Herrenschmidt 
7857ecca2a4SBenjamin Herrenschmidt 	/* Mark free */
7867ecca2a4SBenjamin Herrenschmidt 	ep->dev = NULL;
7877ecca2a4SBenjamin Herrenschmidt }
7887ecca2a4SBenjamin Herrenschmidt 
7897ecca2a4SBenjamin Herrenschmidt static const struct usb_ep_ops ast_vhub_epn_ops = {
7907ecca2a4SBenjamin Herrenschmidt 	.enable		= ast_vhub_epn_enable,
7917ecca2a4SBenjamin Herrenschmidt 	.disable	= ast_vhub_epn_disable,
7927ecca2a4SBenjamin Herrenschmidt 	.dispose	= ast_vhub_epn_dispose,
7937ecca2a4SBenjamin Herrenschmidt 	.queue		= ast_vhub_epn_queue,
7947ecca2a4SBenjamin Herrenschmidt 	.dequeue	= ast_vhub_epn_dequeue,
7957ecca2a4SBenjamin Herrenschmidt 	.set_halt	= ast_vhub_epn_set_halt,
7967ecca2a4SBenjamin Herrenschmidt 	.set_wedge	= ast_vhub_epn_set_wedge,
7977ecca2a4SBenjamin Herrenschmidt 	.alloc_request	= ast_vhub_alloc_request,
7987ecca2a4SBenjamin Herrenschmidt 	.free_request	= ast_vhub_free_request,
7997ecca2a4SBenjamin Herrenschmidt };
8007ecca2a4SBenjamin Herrenschmidt 
ast_vhub_alloc_epn(struct ast_vhub_dev * d,u8 addr)8017ecca2a4SBenjamin Herrenschmidt struct ast_vhub_ep *ast_vhub_alloc_epn(struct ast_vhub_dev *d, u8 addr)
8027ecca2a4SBenjamin Herrenschmidt {
8037ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = d->vhub;
8047ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_ep *ep;
8057ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
8067ecca2a4SBenjamin Herrenschmidt 	int i;
8077ecca2a4SBenjamin Herrenschmidt 
8087ecca2a4SBenjamin Herrenschmidt 	/* Find a free one (no device) */
8097ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
810487bc828STao Ren 	for (i = 0; i < vhub->max_epns; i++)
8117ecca2a4SBenjamin Herrenschmidt 		if (vhub->epns[i].dev == NULL)
8127ecca2a4SBenjamin Herrenschmidt 			break;
813487bc828STao Ren 	if (i >= vhub->max_epns) {
8147ecca2a4SBenjamin Herrenschmidt 		spin_unlock_irqrestore(&vhub->lock, flags);
8157ecca2a4SBenjamin Herrenschmidt 		return NULL;
8167ecca2a4SBenjamin Herrenschmidt 	}
8177ecca2a4SBenjamin Herrenschmidt 
8187ecca2a4SBenjamin Herrenschmidt 	/* Set it up */
8197ecca2a4SBenjamin Herrenschmidt 	ep = &vhub->epns[i];
8207ecca2a4SBenjamin Herrenschmidt 	ep->dev = d;
8217ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
8227ecca2a4SBenjamin Herrenschmidt 
8237ecca2a4SBenjamin Herrenschmidt 	DDBG(d, "Allocating gen EP %d for addr %d\n", i, addr);
8247ecca2a4SBenjamin Herrenschmidt 	INIT_LIST_HEAD(&ep->queue);
8257ecca2a4SBenjamin Herrenschmidt 	ep->d_idx = addr;
8267ecca2a4SBenjamin Herrenschmidt 	ep->vhub = vhub;
8277ecca2a4SBenjamin Herrenschmidt 	ep->ep.ops = &ast_vhub_epn_ops;
8287ecca2a4SBenjamin Herrenschmidt 	ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", addr);
8297ecca2a4SBenjamin Herrenschmidt 	d->epns[addr-1] = ep;
8307ecca2a4SBenjamin Herrenschmidt 	ep->epn.g_idx = i;
8317ecca2a4SBenjamin Herrenschmidt 	ep->epn.regs = vhub->regs + 0x200 + (i * 0x10);
8327ecca2a4SBenjamin Herrenschmidt 
8337ecca2a4SBenjamin Herrenschmidt 	ep->buf = dma_alloc_coherent(&vhub->pdev->dev,
8347ecca2a4SBenjamin Herrenschmidt 				     AST_VHUB_EPn_MAX_PACKET +
8357ecca2a4SBenjamin Herrenschmidt 				     8 * AST_VHUB_DESCS_COUNT,
8367ecca2a4SBenjamin Herrenschmidt 				     &ep->buf_dma, GFP_KERNEL);
8377ecca2a4SBenjamin Herrenschmidt 	if (!ep->buf) {
8387ecca2a4SBenjamin Herrenschmidt 		kfree(ep->ep.name);
8397ecca2a4SBenjamin Herrenschmidt 		ep->ep.name = NULL;
8407ecca2a4SBenjamin Herrenschmidt 		return NULL;
8417ecca2a4SBenjamin Herrenschmidt 	}
8427ecca2a4SBenjamin Herrenschmidt 	ep->epn.descs = ep->buf + AST_VHUB_EPn_MAX_PACKET;
8437ecca2a4SBenjamin Herrenschmidt 	ep->epn.descs_dma = ep->buf_dma + AST_VHUB_EPn_MAX_PACKET;
8447ecca2a4SBenjamin Herrenschmidt 
8457ecca2a4SBenjamin Herrenschmidt 	usb_ep_set_maxpacket_limit(&ep->ep, AST_VHUB_EPn_MAX_PACKET);
8467ecca2a4SBenjamin Herrenschmidt 	list_add_tail(&ep->ep.ep_list, &d->gadget.ep_list);
8477ecca2a4SBenjamin Herrenschmidt 	ep->ep.caps.type_iso = true;
8487ecca2a4SBenjamin Herrenschmidt 	ep->ep.caps.type_bulk = true;
8497ecca2a4SBenjamin Herrenschmidt 	ep->ep.caps.type_int = true;
8507ecca2a4SBenjamin Herrenschmidt 	ep->ep.caps.dir_in = true;
8517ecca2a4SBenjamin Herrenschmidt 	ep->ep.caps.dir_out = true;
8527ecca2a4SBenjamin Herrenschmidt 
8537ecca2a4SBenjamin Herrenschmidt 	return ep;
8547ecca2a4SBenjamin Herrenschmidt }
855