xref: /openbmc/linux/drivers/usb/mtu3/mtu3_gadget_ep0.c (revision 288ee3c36255d0c2d32830313f527e1474a09e9f)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2df2069acSChunfeng Yun /*
3df2069acSChunfeng Yun  * mtu3_gadget_ep0.c - MediaTek USB3 DRD peripheral driver ep0 handling
4df2069acSChunfeng Yun  *
5df2069acSChunfeng Yun  * Copyright (c) 2016 MediaTek Inc.
6df2069acSChunfeng Yun  *
7df2069acSChunfeng Yun  * Author:  Chunfeng.Yun <chunfeng.yun@mediatek.com>
8df2069acSChunfeng Yun  */
9df2069acSChunfeng Yun 
10fe7c994aSChunfeng Yun #include <linux/usb/composite.h>
11fe7c994aSChunfeng Yun 
12df2069acSChunfeng Yun #include "mtu3.h"
13df2069acSChunfeng Yun 
14df2069acSChunfeng Yun /* ep0 is always mtu3->in_eps[0] */
15df2069acSChunfeng Yun #define	next_ep0_request(mtu)	next_request((mtu)->ep0)
16df2069acSChunfeng Yun 
17df2069acSChunfeng Yun /* for high speed test mode; see USB 2.0 spec 7.1.20 */
18df2069acSChunfeng Yun static const u8 mtu3_test_packet[53] = {
19df2069acSChunfeng Yun 	/* implicit SYNC then DATA0 to start */
20df2069acSChunfeng Yun 
21df2069acSChunfeng Yun 	/* JKJKJKJK x9 */
22df2069acSChunfeng Yun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23df2069acSChunfeng Yun 	/* JJKKJJKK x8 */
24df2069acSChunfeng Yun 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
25df2069acSChunfeng Yun 	/* JJJJKKKK x8 */
26df2069acSChunfeng Yun 	0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
27df2069acSChunfeng Yun 	/* JJJJJJJKKKKKKK x8 */
28df2069acSChunfeng Yun 	0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
29df2069acSChunfeng Yun 	/* JJJJJJJK x8 */
30df2069acSChunfeng Yun 	0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
31df2069acSChunfeng Yun 	/* JKKKKKKK x10, JK */
32df2069acSChunfeng Yun 	0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e,
33df2069acSChunfeng Yun 	/* implicit CRC16 then EOP to end */
34df2069acSChunfeng Yun };
35df2069acSChunfeng Yun 
36df2069acSChunfeng Yun static char *decode_ep0_state(struct mtu3 *mtu)
37df2069acSChunfeng Yun {
38df2069acSChunfeng Yun 	switch (mtu->ep0_state) {
39df2069acSChunfeng Yun 	case MU3D_EP0_STATE_SETUP:
40df2069acSChunfeng Yun 		return "SETUP";
41df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX:
42df2069acSChunfeng Yun 		return "IN";
43df2069acSChunfeng Yun 	case MU3D_EP0_STATE_RX:
44df2069acSChunfeng Yun 		return "OUT";
45df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX_END:
46df2069acSChunfeng Yun 		return "TX-END";
47df2069acSChunfeng Yun 	case MU3D_EP0_STATE_STALL:
48df2069acSChunfeng Yun 		return "STALL";
49df2069acSChunfeng Yun 	default:
50df2069acSChunfeng Yun 		return "??";
51df2069acSChunfeng Yun 	}
52df2069acSChunfeng Yun }
53df2069acSChunfeng Yun 
54df2069acSChunfeng Yun static void ep0_req_giveback(struct mtu3 *mtu, struct usb_request *req)
55df2069acSChunfeng Yun {
56df2069acSChunfeng Yun 	mtu3_req_complete(mtu->ep0, req, 0);
57df2069acSChunfeng Yun }
58df2069acSChunfeng Yun 
59df2069acSChunfeng Yun static int
60df2069acSChunfeng Yun forward_to_driver(struct mtu3 *mtu, const struct usb_ctrlrequest *setup)
61df2069acSChunfeng Yun __releases(mtu->lock)
62df2069acSChunfeng Yun __acquires(mtu->lock)
63df2069acSChunfeng Yun {
64df2069acSChunfeng Yun 	int ret;
65df2069acSChunfeng Yun 
66df2069acSChunfeng Yun 	if (!mtu->gadget_driver)
67df2069acSChunfeng Yun 		return -EOPNOTSUPP;
68df2069acSChunfeng Yun 
69df2069acSChunfeng Yun 	spin_unlock(&mtu->lock);
70df2069acSChunfeng Yun 	ret = mtu->gadget_driver->setup(&mtu->g, setup);
71df2069acSChunfeng Yun 	spin_lock(&mtu->lock);
72df2069acSChunfeng Yun 
73df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s ret %d\n", __func__, ret);
74df2069acSChunfeng Yun 	return ret;
75df2069acSChunfeng Yun }
76df2069acSChunfeng Yun 
77df2069acSChunfeng Yun static void ep0_write_fifo(struct mtu3_ep *mep, const u8 *src, u16 len)
78df2069acSChunfeng Yun {
79df2069acSChunfeng Yun 	void __iomem *fifo = mep->mtu->mac_base + U3D_FIFO0;
80df2069acSChunfeng Yun 	u16 index = 0;
81df2069acSChunfeng Yun 
82df2069acSChunfeng Yun 	dev_dbg(mep->mtu->dev, "%s: ep%din, len=%d, buf=%p\n",
83df2069acSChunfeng Yun 		__func__, mep->epnum, len, src);
84df2069acSChunfeng Yun 
85df2069acSChunfeng Yun 	if (len >= 4) {
86df2069acSChunfeng Yun 		iowrite32_rep(fifo, src, len >> 2);
87df2069acSChunfeng Yun 		index = len & ~0x03;
88df2069acSChunfeng Yun 	}
89df2069acSChunfeng Yun 	if (len & 0x02) {
90df2069acSChunfeng Yun 		writew(*(u16 *)&src[index], fifo);
91df2069acSChunfeng Yun 		index += 2;
92df2069acSChunfeng Yun 	}
93df2069acSChunfeng Yun 	if (len & 0x01)
94df2069acSChunfeng Yun 		writeb(src[index], fifo);
95df2069acSChunfeng Yun }
96df2069acSChunfeng Yun 
97df2069acSChunfeng Yun static void ep0_read_fifo(struct mtu3_ep *mep, u8 *dst, u16 len)
98df2069acSChunfeng Yun {
99df2069acSChunfeng Yun 	void __iomem *fifo = mep->mtu->mac_base + U3D_FIFO0;
100df2069acSChunfeng Yun 	u32 value;
101df2069acSChunfeng Yun 	u16 index = 0;
102df2069acSChunfeng Yun 
103df2069acSChunfeng Yun 	dev_dbg(mep->mtu->dev, "%s: ep%dout len=%d buf=%p\n",
104df2069acSChunfeng Yun 		 __func__, mep->epnum, len, dst);
105df2069acSChunfeng Yun 
106df2069acSChunfeng Yun 	if (len >= 4) {
107df2069acSChunfeng Yun 		ioread32_rep(fifo, dst, len >> 2);
108df2069acSChunfeng Yun 		index = len & ~0x03;
109df2069acSChunfeng Yun 	}
110df2069acSChunfeng Yun 	if (len & 0x3) {
111df2069acSChunfeng Yun 		value = readl(fifo);
112df2069acSChunfeng Yun 		memcpy(&dst[index], &value, len & 0x3);
113df2069acSChunfeng Yun 	}
114df2069acSChunfeng Yun 
115df2069acSChunfeng Yun }
116df2069acSChunfeng Yun 
117df2069acSChunfeng Yun static void ep0_load_test_packet(struct mtu3 *mtu)
118df2069acSChunfeng Yun {
119df2069acSChunfeng Yun 	/*
120df2069acSChunfeng Yun 	 * because the length of test packet is less than max packet of HS ep0,
121df2069acSChunfeng Yun 	 * write it into fifo directly.
122df2069acSChunfeng Yun 	 */
123df2069acSChunfeng Yun 	ep0_write_fifo(mtu->ep0, mtu3_test_packet, sizeof(mtu3_test_packet));
124df2069acSChunfeng Yun }
125df2069acSChunfeng Yun 
126df2069acSChunfeng Yun /*
127df2069acSChunfeng Yun  * A. send STALL for setup transfer without data stage:
128df2069acSChunfeng Yun  *		set SENDSTALL and SETUPPKTRDY at the same time;
129df2069acSChunfeng Yun  * B. send STALL for other cases:
130df2069acSChunfeng Yun  *		set SENDSTALL only.
131df2069acSChunfeng Yun  */
132df2069acSChunfeng Yun static void ep0_stall_set(struct mtu3_ep *mep0, bool set, u32 pktrdy)
133df2069acSChunfeng Yun {
134df2069acSChunfeng Yun 	struct mtu3 *mtu = mep0->mtu;
135df2069acSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
136df2069acSChunfeng Yun 	u32 csr;
137df2069acSChunfeng Yun 
138df2069acSChunfeng Yun 	/* EP0_SENTSTALL is W1C */
139df2069acSChunfeng Yun 	csr = mtu3_readl(mbase, U3D_EP0CSR) & EP0_W1C_BITS;
140df2069acSChunfeng Yun 	if (set)
141df2069acSChunfeng Yun 		csr |= EP0_SENDSTALL | pktrdy;
142df2069acSChunfeng Yun 	else
143df2069acSChunfeng Yun 		csr = (csr & ~EP0_SENDSTALL) | EP0_SENTSTALL;
144df2069acSChunfeng Yun 	mtu3_writel(mtu->mac_base, U3D_EP0CSR, csr);
145df2069acSChunfeng Yun 
146fe7c994aSChunfeng Yun 	mtu->delayed_status = false;
147df2069acSChunfeng Yun 	mtu->ep0_state = MU3D_EP0_STATE_SETUP;
148df2069acSChunfeng Yun 
149df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "ep0: %s STALL, ep0_state: %s\n",
150df2069acSChunfeng Yun 		set ? "SEND" : "CLEAR", decode_ep0_state(mtu));
151df2069acSChunfeng Yun }
152df2069acSChunfeng Yun 
153df2069acSChunfeng Yun static int ep0_queue(struct mtu3_ep *mep0, struct mtu3_request *mreq);
154df2069acSChunfeng Yun 
155df2069acSChunfeng Yun static void ep0_dummy_complete(struct usb_ep *ep, struct usb_request *req)
156df2069acSChunfeng Yun {}
157df2069acSChunfeng Yun 
158a29de31bSChunfeng Yun static void ep0_set_sel_complete(struct usb_ep *ep, struct usb_request *req)
159a29de31bSChunfeng Yun {
160a29de31bSChunfeng Yun 	struct mtu3_request *mreq;
161a29de31bSChunfeng Yun 	struct mtu3 *mtu;
162a29de31bSChunfeng Yun 	struct usb_set_sel_req sel;
163a29de31bSChunfeng Yun 
164a29de31bSChunfeng Yun 	memcpy(&sel, req->buf, sizeof(sel));
165a29de31bSChunfeng Yun 
166a29de31bSChunfeng Yun 	mreq = to_mtu3_request(req);
167a29de31bSChunfeng Yun 	mtu = mreq->mtu;
168a29de31bSChunfeng Yun 	dev_dbg(mtu->dev, "u1sel:%d, u1pel:%d, u2sel:%d, u2pel:%d\n",
169a29de31bSChunfeng Yun 		sel.u1_sel, sel.u1_pel, sel.u2_sel, sel.u2_pel);
170a29de31bSChunfeng Yun }
171a29de31bSChunfeng Yun 
172a29de31bSChunfeng Yun /* queue data stage to handle 6 byte SET_SEL request */
173a29de31bSChunfeng Yun static int ep0_set_sel(struct mtu3 *mtu, struct usb_ctrlrequest *setup)
174a29de31bSChunfeng Yun {
175a29de31bSChunfeng Yun 	int ret;
176a29de31bSChunfeng Yun 	u16 length = le16_to_cpu(setup->wLength);
177a29de31bSChunfeng Yun 
178a29de31bSChunfeng Yun 	if (unlikely(length != 6)) {
179a29de31bSChunfeng Yun 		dev_err(mtu->dev, "%s wrong wLength:%d\n",
180a29de31bSChunfeng Yun 			__func__, length);
181a29de31bSChunfeng Yun 		return -EINVAL;
182a29de31bSChunfeng Yun 	}
183a29de31bSChunfeng Yun 
184a29de31bSChunfeng Yun 	mtu->ep0_req.mep = mtu->ep0;
185a29de31bSChunfeng Yun 	mtu->ep0_req.request.length = 6;
186a29de31bSChunfeng Yun 	mtu->ep0_req.request.buf = mtu->setup_buf;
187a29de31bSChunfeng Yun 	mtu->ep0_req.request.complete = ep0_set_sel_complete;
188a29de31bSChunfeng Yun 	ret = ep0_queue(mtu->ep0, &mtu->ep0_req);
189a29de31bSChunfeng Yun 
190a29de31bSChunfeng Yun 	return ret < 0 ? ret : 1;
191a29de31bSChunfeng Yun }
192a29de31bSChunfeng Yun 
193df2069acSChunfeng Yun static int
194df2069acSChunfeng Yun ep0_get_status(struct mtu3 *mtu, const struct usb_ctrlrequest *setup)
195df2069acSChunfeng Yun {
196df2069acSChunfeng Yun 	struct mtu3_ep *mep = NULL;
197df2069acSChunfeng Yun 	int handled = 1;
198df2069acSChunfeng Yun 	u8 result[2] = {0, 0};
199df2069acSChunfeng Yun 	u8 epnum = 0;
200df2069acSChunfeng Yun 	int is_in;
201df2069acSChunfeng Yun 
202df2069acSChunfeng Yun 	switch (setup->bRequestType & USB_RECIP_MASK) {
203df2069acSChunfeng Yun 	case USB_RECIP_DEVICE:
204df2069acSChunfeng Yun 		result[0] = mtu->is_self_powered << USB_DEVICE_SELF_POWERED;
205df2069acSChunfeng Yun 		result[0] |= mtu->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
2064d79e042SChunfeng Yun 
2074d79e042SChunfeng Yun 		if (mtu->g.speed >= USB_SPEED_SUPER) {
208a29de31bSChunfeng Yun 			result[0] |= mtu->u1_enable << USB_DEV_STAT_U1_ENABLED;
209a29de31bSChunfeng Yun 			result[0] |= mtu->u2_enable << USB_DEV_STAT_U2_ENABLED;
210a29de31bSChunfeng Yun 		}
211a29de31bSChunfeng Yun 
212a29de31bSChunfeng Yun 		dev_dbg(mtu->dev, "%s result=%x, U1=%x, U2=%x\n", __func__,
213a29de31bSChunfeng Yun 			result[0], mtu->u1_enable, mtu->u2_enable);
214a29de31bSChunfeng Yun 
215df2069acSChunfeng Yun 		break;
216df2069acSChunfeng Yun 	case USB_RECIP_INTERFACE:
217df2069acSChunfeng Yun 		break;
218df2069acSChunfeng Yun 	case USB_RECIP_ENDPOINT:
219df2069acSChunfeng Yun 		epnum = (u8) le16_to_cpu(setup->wIndex);
220df2069acSChunfeng Yun 		is_in = epnum & USB_DIR_IN;
221df2069acSChunfeng Yun 		epnum &= USB_ENDPOINT_NUMBER_MASK;
222df2069acSChunfeng Yun 
223df2069acSChunfeng Yun 		if (epnum >= mtu->num_eps) {
224df2069acSChunfeng Yun 			handled = -EINVAL;
225df2069acSChunfeng Yun 			break;
226df2069acSChunfeng Yun 		}
227df2069acSChunfeng Yun 		if (!epnum)
228df2069acSChunfeng Yun 			break;
229df2069acSChunfeng Yun 
230df2069acSChunfeng Yun 		mep = (is_in ? mtu->in_eps : mtu->out_eps) + epnum;
231df2069acSChunfeng Yun 		if (!mep->desc) {
232df2069acSChunfeng Yun 			handled = -EINVAL;
233df2069acSChunfeng Yun 			break;
234df2069acSChunfeng Yun 		}
235df2069acSChunfeng Yun 		if (mep->flags & MTU3_EP_STALL)
236df2069acSChunfeng Yun 			result[0] |= 1 << USB_ENDPOINT_HALT;
237df2069acSChunfeng Yun 
238df2069acSChunfeng Yun 		break;
239df2069acSChunfeng Yun 	default:
240df2069acSChunfeng Yun 		/* class, vendor, etc ... delegate */
241df2069acSChunfeng Yun 		handled = 0;
242df2069acSChunfeng Yun 		break;
243df2069acSChunfeng Yun 	}
244df2069acSChunfeng Yun 
245df2069acSChunfeng Yun 	if (handled > 0) {
246df2069acSChunfeng Yun 		int ret;
247df2069acSChunfeng Yun 
248df2069acSChunfeng Yun 		/* prepare a data stage for GET_STATUS */
249df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "get_status=%x\n", *(u16 *)result);
250df2069acSChunfeng Yun 		memcpy(mtu->setup_buf, result, sizeof(result));
251df2069acSChunfeng Yun 		mtu->ep0_req.mep = mtu->ep0;
252df2069acSChunfeng Yun 		mtu->ep0_req.request.length = 2;
253df2069acSChunfeng Yun 		mtu->ep0_req.request.buf = &mtu->setup_buf;
254df2069acSChunfeng Yun 		mtu->ep0_req.request.complete = ep0_dummy_complete;
255df2069acSChunfeng Yun 		ret = ep0_queue(mtu->ep0, &mtu->ep0_req);
256df2069acSChunfeng Yun 		if (ret < 0)
257df2069acSChunfeng Yun 			handled = ret;
258df2069acSChunfeng Yun 	}
259df2069acSChunfeng Yun 	return handled;
260df2069acSChunfeng Yun }
261df2069acSChunfeng Yun 
262df2069acSChunfeng Yun static int handle_test_mode(struct mtu3 *mtu, struct usb_ctrlrequest *setup)
263df2069acSChunfeng Yun {
264df2069acSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
265df2069acSChunfeng Yun 	int handled = 1;
266df2069acSChunfeng Yun 
267df2069acSChunfeng Yun 	switch (le16_to_cpu(setup->wIndex) >> 8) {
268df2069acSChunfeng Yun 	case TEST_J:
269df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "TEST_J\n");
270df2069acSChunfeng Yun 		mtu->test_mode_nr = TEST_J_MODE;
271df2069acSChunfeng Yun 		break;
272df2069acSChunfeng Yun 	case TEST_K:
273df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "TEST_K\n");
274df2069acSChunfeng Yun 		mtu->test_mode_nr = TEST_K_MODE;
275df2069acSChunfeng Yun 		break;
276df2069acSChunfeng Yun 	case TEST_SE0_NAK:
277df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "TEST_SE0_NAK\n");
278df2069acSChunfeng Yun 		mtu->test_mode_nr = TEST_SE0_NAK_MODE;
279df2069acSChunfeng Yun 		break;
280df2069acSChunfeng Yun 	case TEST_PACKET:
281df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "TEST_PACKET\n");
282df2069acSChunfeng Yun 		mtu->test_mode_nr = TEST_PACKET_MODE;
283df2069acSChunfeng Yun 		break;
284df2069acSChunfeng Yun 	default:
285df2069acSChunfeng Yun 		handled = -EINVAL;
286df2069acSChunfeng Yun 		goto out;
287df2069acSChunfeng Yun 	}
288df2069acSChunfeng Yun 
289df2069acSChunfeng Yun 	mtu->test_mode = true;
290df2069acSChunfeng Yun 
291df2069acSChunfeng Yun 	/* no TX completion interrupt, and need restart platform after test */
292df2069acSChunfeng Yun 	if (mtu->test_mode_nr == TEST_PACKET_MODE)
293df2069acSChunfeng Yun 		ep0_load_test_packet(mtu);
294df2069acSChunfeng Yun 
295df2069acSChunfeng Yun 	mtu3_writel(mbase, U3D_USB2_TEST_MODE, mtu->test_mode_nr);
296df2069acSChunfeng Yun 
297df2069acSChunfeng Yun 	mtu->ep0_state = MU3D_EP0_STATE_SETUP;
298df2069acSChunfeng Yun 
299df2069acSChunfeng Yun out:
300df2069acSChunfeng Yun 	return handled;
301df2069acSChunfeng Yun }
302df2069acSChunfeng Yun 
303df2069acSChunfeng Yun static int ep0_handle_feature_dev(struct mtu3 *mtu,
304df2069acSChunfeng Yun 		struct usb_ctrlrequest *setup, bool set)
305df2069acSChunfeng Yun {
306a29de31bSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
307df2069acSChunfeng Yun 	int handled = -EINVAL;
308a29de31bSChunfeng Yun 	u32 lpc;
309df2069acSChunfeng Yun 
310df2069acSChunfeng Yun 	switch (le16_to_cpu(setup->wValue)) {
311df2069acSChunfeng Yun 	case USB_DEVICE_REMOTE_WAKEUP:
312df2069acSChunfeng Yun 		mtu->may_wakeup = !!set;
313df2069acSChunfeng Yun 		handled = 1;
314df2069acSChunfeng Yun 		break;
315df2069acSChunfeng Yun 	case USB_DEVICE_TEST_MODE:
316df2069acSChunfeng Yun 		if (!set || (mtu->g.speed != USB_SPEED_HIGH) ||
317df2069acSChunfeng Yun 			(le16_to_cpu(setup->wIndex) & 0xff))
318df2069acSChunfeng Yun 			break;
319df2069acSChunfeng Yun 
320df2069acSChunfeng Yun 		handled = handle_test_mode(mtu, setup);
321df2069acSChunfeng Yun 		break;
322a29de31bSChunfeng Yun 	case USB_DEVICE_U1_ENABLE:
3234d79e042SChunfeng Yun 		if (mtu->g.speed < USB_SPEED_SUPER ||
324a29de31bSChunfeng Yun 		    mtu->g.state != USB_STATE_CONFIGURED)
325a29de31bSChunfeng Yun 			break;
326a29de31bSChunfeng Yun 
327a29de31bSChunfeng Yun 		lpc = mtu3_readl(mbase, U3D_LINK_POWER_CONTROL);
328a29de31bSChunfeng Yun 		if (set)
329a29de31bSChunfeng Yun 			lpc |= SW_U1_ACCEPT_ENABLE;
330a29de31bSChunfeng Yun 		else
331a29de31bSChunfeng Yun 			lpc &= ~SW_U1_ACCEPT_ENABLE;
332a29de31bSChunfeng Yun 		mtu3_writel(mbase, U3D_LINK_POWER_CONTROL, lpc);
333a29de31bSChunfeng Yun 
334a29de31bSChunfeng Yun 		mtu->u1_enable = !!set;
335a29de31bSChunfeng Yun 		handled = 1;
336a29de31bSChunfeng Yun 		break;
337a29de31bSChunfeng Yun 	case USB_DEVICE_U2_ENABLE:
3384d79e042SChunfeng Yun 		if (mtu->g.speed < USB_SPEED_SUPER ||
339a29de31bSChunfeng Yun 		    mtu->g.state != USB_STATE_CONFIGURED)
340a29de31bSChunfeng Yun 			break;
341a29de31bSChunfeng Yun 
342a29de31bSChunfeng Yun 		lpc = mtu3_readl(mbase, U3D_LINK_POWER_CONTROL);
343a29de31bSChunfeng Yun 		if (set)
344a29de31bSChunfeng Yun 			lpc |= SW_U2_ACCEPT_ENABLE;
345a29de31bSChunfeng Yun 		else
346a29de31bSChunfeng Yun 			lpc &= ~SW_U2_ACCEPT_ENABLE;
347a29de31bSChunfeng Yun 		mtu3_writel(mbase, U3D_LINK_POWER_CONTROL, lpc);
348a29de31bSChunfeng Yun 
349a29de31bSChunfeng Yun 		mtu->u2_enable = !!set;
350a29de31bSChunfeng Yun 		handled = 1;
351a29de31bSChunfeng Yun 		break;
352df2069acSChunfeng Yun 	default:
353df2069acSChunfeng Yun 		handled = -EINVAL;
354df2069acSChunfeng Yun 		break;
355df2069acSChunfeng Yun 	}
356df2069acSChunfeng Yun 	return handled;
357df2069acSChunfeng Yun }
358df2069acSChunfeng Yun 
359df2069acSChunfeng Yun static int ep0_handle_feature(struct mtu3 *mtu,
360df2069acSChunfeng Yun 		struct usb_ctrlrequest *setup, bool set)
361df2069acSChunfeng Yun {
362df2069acSChunfeng Yun 	struct mtu3_ep *mep;
363df2069acSChunfeng Yun 	int handled = -EINVAL;
364df2069acSChunfeng Yun 	int is_in;
365df2069acSChunfeng Yun 	u16 value;
366df2069acSChunfeng Yun 	u16 index;
367df2069acSChunfeng Yun 	u8 epnum;
368df2069acSChunfeng Yun 
369df2069acSChunfeng Yun 	value = le16_to_cpu(setup->wValue);
370df2069acSChunfeng Yun 	index = le16_to_cpu(setup->wIndex);
371df2069acSChunfeng Yun 
372df2069acSChunfeng Yun 	switch (setup->bRequestType & USB_RECIP_MASK) {
373df2069acSChunfeng Yun 	case USB_RECIP_DEVICE:
374df2069acSChunfeng Yun 		handled = ep0_handle_feature_dev(mtu, setup, set);
375df2069acSChunfeng Yun 		break;
376a29de31bSChunfeng Yun 	case USB_RECIP_INTERFACE:
377a29de31bSChunfeng Yun 		/* superspeed only */
3784d79e042SChunfeng Yun 		if (value == USB_INTRF_FUNC_SUSPEND &&
3794d79e042SChunfeng Yun 		    mtu->g.speed >= USB_SPEED_SUPER) {
380a29de31bSChunfeng Yun 			/*
381a29de31bSChunfeng Yun 			 * forward the request because function drivers
382a29de31bSChunfeng Yun 			 * should handle it
383a29de31bSChunfeng Yun 			 */
384a29de31bSChunfeng Yun 			handled = 0;
385a29de31bSChunfeng Yun 		}
386a29de31bSChunfeng Yun 		break;
387df2069acSChunfeng Yun 	case USB_RECIP_ENDPOINT:
388df2069acSChunfeng Yun 		epnum = index & USB_ENDPOINT_NUMBER_MASK;
389df2069acSChunfeng Yun 		if (epnum == 0 || epnum >= mtu->num_eps ||
390df2069acSChunfeng Yun 			value != USB_ENDPOINT_HALT)
391df2069acSChunfeng Yun 			break;
392df2069acSChunfeng Yun 
393df2069acSChunfeng Yun 		is_in = index & USB_DIR_IN;
394df2069acSChunfeng Yun 		mep = (is_in ? mtu->in_eps : mtu->out_eps) + epnum;
395df2069acSChunfeng Yun 		if (!mep->desc)
396df2069acSChunfeng Yun 			break;
397df2069acSChunfeng Yun 
398df2069acSChunfeng Yun 		handled = 1;
399df2069acSChunfeng Yun 		/* ignore request if endpoint is wedged */
400df2069acSChunfeng Yun 		if (mep->wedged)
401df2069acSChunfeng Yun 			break;
402df2069acSChunfeng Yun 
403df2069acSChunfeng Yun 		mtu3_ep_stall_set(mep, set);
404df2069acSChunfeng Yun 		break;
405df2069acSChunfeng Yun 	default:
406df2069acSChunfeng Yun 		/* class, vendor, etc ... delegate */
407df2069acSChunfeng Yun 		handled = 0;
408df2069acSChunfeng Yun 		break;
409df2069acSChunfeng Yun 	}
410df2069acSChunfeng Yun 	return handled;
411df2069acSChunfeng Yun }
412df2069acSChunfeng Yun 
413df2069acSChunfeng Yun /*
414df2069acSChunfeng Yun  * handle all control requests can be handled
415df2069acSChunfeng Yun  * returns:
416df2069acSChunfeng Yun  *	negative errno - error happened
417df2069acSChunfeng Yun  *	zero - need delegate SETUP to gadget driver
418df2069acSChunfeng Yun  *	positive - already handled
419df2069acSChunfeng Yun  */
420df2069acSChunfeng Yun static int handle_standard_request(struct mtu3 *mtu,
421df2069acSChunfeng Yun 			  struct usb_ctrlrequest *setup)
422df2069acSChunfeng Yun {
423df2069acSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
424df2069acSChunfeng Yun 	enum usb_device_state state = mtu->g.state;
425df2069acSChunfeng Yun 	int handled = -EINVAL;
426df2069acSChunfeng Yun 	u32 dev_conf;
427df2069acSChunfeng Yun 	u16 value;
428df2069acSChunfeng Yun 
429df2069acSChunfeng Yun 	value = le16_to_cpu(setup->wValue);
430df2069acSChunfeng Yun 
431df2069acSChunfeng Yun 	/* the gadget driver handles everything except what we must handle */
432df2069acSChunfeng Yun 	switch (setup->bRequest) {
433df2069acSChunfeng Yun 	case USB_REQ_SET_ADDRESS:
434df2069acSChunfeng Yun 		/* change it after the status stage */
435df2069acSChunfeng Yun 		mtu->address = (u8) (value & 0x7f);
436df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "set address to 0x%x\n", mtu->address);
437df2069acSChunfeng Yun 
438df2069acSChunfeng Yun 		dev_conf = mtu3_readl(mbase, U3D_DEVICE_CONF);
439df2069acSChunfeng Yun 		dev_conf &= ~DEV_ADDR_MSK;
440df2069acSChunfeng Yun 		dev_conf |= DEV_ADDR(mtu->address);
441df2069acSChunfeng Yun 		mtu3_writel(mbase, U3D_DEVICE_CONF, dev_conf);
442df2069acSChunfeng Yun 
443df2069acSChunfeng Yun 		if (mtu->address)
444df2069acSChunfeng Yun 			usb_gadget_set_state(&mtu->g, USB_STATE_ADDRESS);
445df2069acSChunfeng Yun 		else
446df2069acSChunfeng Yun 			usb_gadget_set_state(&mtu->g, USB_STATE_DEFAULT);
447df2069acSChunfeng Yun 
448df2069acSChunfeng Yun 		handled = 1;
449df2069acSChunfeng Yun 		break;
450df2069acSChunfeng Yun 	case USB_REQ_SET_CONFIGURATION:
451df2069acSChunfeng Yun 		if (state == USB_STATE_ADDRESS) {
452df2069acSChunfeng Yun 			usb_gadget_set_state(&mtu->g,
453df2069acSChunfeng Yun 					USB_STATE_CONFIGURED);
454df2069acSChunfeng Yun 		} else if (state == USB_STATE_CONFIGURED) {
455df2069acSChunfeng Yun 			/*
456df2069acSChunfeng Yun 			 * USB2 spec sec 9.4.7, if wValue is 0 then dev
457df2069acSChunfeng Yun 			 * is moved to addressed state
458df2069acSChunfeng Yun 			 */
459df2069acSChunfeng Yun 			if (!value)
460df2069acSChunfeng Yun 				usb_gadget_set_state(&mtu->g,
461df2069acSChunfeng Yun 						USB_STATE_ADDRESS);
462df2069acSChunfeng Yun 		}
463df2069acSChunfeng Yun 		handled = 0;
464df2069acSChunfeng Yun 		break;
465df2069acSChunfeng Yun 	case USB_REQ_CLEAR_FEATURE:
466df2069acSChunfeng Yun 		handled = ep0_handle_feature(mtu, setup, 0);
467df2069acSChunfeng Yun 		break;
468df2069acSChunfeng Yun 	case USB_REQ_SET_FEATURE:
469df2069acSChunfeng Yun 		handled = ep0_handle_feature(mtu, setup, 1);
470df2069acSChunfeng Yun 		break;
471df2069acSChunfeng Yun 	case USB_REQ_GET_STATUS:
472df2069acSChunfeng Yun 		handled = ep0_get_status(mtu, setup);
473df2069acSChunfeng Yun 		break;
474a29de31bSChunfeng Yun 	case USB_REQ_SET_SEL:
475a29de31bSChunfeng Yun 		handled = ep0_set_sel(mtu, setup);
476a29de31bSChunfeng Yun 		break;
477df2069acSChunfeng Yun 	case USB_REQ_SET_ISOCH_DELAY:
478df2069acSChunfeng Yun 		handled = 1;
479df2069acSChunfeng Yun 		break;
480df2069acSChunfeng Yun 	default:
481df2069acSChunfeng Yun 		/* delegate SET_CONFIGURATION, etc */
482df2069acSChunfeng Yun 		handled = 0;
483df2069acSChunfeng Yun 	}
484df2069acSChunfeng Yun 
485df2069acSChunfeng Yun 	return handled;
486df2069acSChunfeng Yun }
487df2069acSChunfeng Yun 
488df2069acSChunfeng Yun /* receive an data packet (OUT) */
489df2069acSChunfeng Yun static void ep0_rx_state(struct mtu3 *mtu)
490df2069acSChunfeng Yun {
491df2069acSChunfeng Yun 	struct mtu3_request *mreq;
492df2069acSChunfeng Yun 	struct usb_request *req;
493df2069acSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
494df2069acSChunfeng Yun 	u32 maxp;
495df2069acSChunfeng Yun 	u32 csr;
496df2069acSChunfeng Yun 	u16 count = 0;
497df2069acSChunfeng Yun 
498df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s\n", __func__);
499df2069acSChunfeng Yun 
500df2069acSChunfeng Yun 	csr = mtu3_readl(mbase, U3D_EP0CSR) & EP0_W1C_BITS;
501df2069acSChunfeng Yun 	mreq = next_ep0_request(mtu);
502df2069acSChunfeng Yun 	req = &mreq->request;
503df2069acSChunfeng Yun 
504df2069acSChunfeng Yun 	/* read packet and ack; or stall because of gadget driver bug */
505df2069acSChunfeng Yun 	if (req) {
506df2069acSChunfeng Yun 		void *buf = req->buf + req->actual;
507df2069acSChunfeng Yun 		unsigned int len = req->length - req->actual;
508df2069acSChunfeng Yun 
509df2069acSChunfeng Yun 		/* read the buffer */
510df2069acSChunfeng Yun 		count = mtu3_readl(mbase, U3D_RXCOUNT0);
511df2069acSChunfeng Yun 		if (count > len) {
512df2069acSChunfeng Yun 			req->status = -EOVERFLOW;
513df2069acSChunfeng Yun 			count = len;
514df2069acSChunfeng Yun 		}
515df2069acSChunfeng Yun 		ep0_read_fifo(mtu->ep0, buf, count);
516df2069acSChunfeng Yun 		req->actual += count;
517df2069acSChunfeng Yun 		csr |= EP0_RXPKTRDY;
518df2069acSChunfeng Yun 
519df2069acSChunfeng Yun 		maxp = mtu->g.ep0->maxpacket;
520df2069acSChunfeng Yun 		if (count < maxp || req->actual == req->length) {
521df2069acSChunfeng Yun 			mtu->ep0_state = MU3D_EP0_STATE_SETUP;
522df2069acSChunfeng Yun 			dev_dbg(mtu->dev, "ep0 state: %s\n",
523df2069acSChunfeng Yun 				decode_ep0_state(mtu));
524df2069acSChunfeng Yun 
525df2069acSChunfeng Yun 			csr |= EP0_DATAEND;
526df2069acSChunfeng Yun 		} else {
527df2069acSChunfeng Yun 			req = NULL;
528df2069acSChunfeng Yun 		}
529df2069acSChunfeng Yun 	} else {
530df2069acSChunfeng Yun 		csr |= EP0_RXPKTRDY | EP0_SENDSTALL;
531df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "%s: SENDSTALL\n", __func__);
532df2069acSChunfeng Yun 	}
533df2069acSChunfeng Yun 
534df2069acSChunfeng Yun 	mtu3_writel(mbase, U3D_EP0CSR, csr);
535df2069acSChunfeng Yun 
536df2069acSChunfeng Yun 	/* give back the request if have received all data */
537df2069acSChunfeng Yun 	if (req)
538df2069acSChunfeng Yun 		ep0_req_giveback(mtu, req);
539df2069acSChunfeng Yun 
540df2069acSChunfeng Yun }
541df2069acSChunfeng Yun 
542df2069acSChunfeng Yun /* transmitting to the host (IN) */
543df2069acSChunfeng Yun static void ep0_tx_state(struct mtu3 *mtu)
544df2069acSChunfeng Yun {
545df2069acSChunfeng Yun 	struct mtu3_request *mreq = next_ep0_request(mtu);
546df2069acSChunfeng Yun 	struct usb_request *req;
547df2069acSChunfeng Yun 	u32 csr;
548df2069acSChunfeng Yun 	u8 *src;
549*288ee3c3SChunfeng Yun 	u32 count;
550df2069acSChunfeng Yun 	u32 maxp;
551df2069acSChunfeng Yun 
552df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s\n", __func__);
553df2069acSChunfeng Yun 
554df2069acSChunfeng Yun 	if (!mreq)
555df2069acSChunfeng Yun 		return;
556df2069acSChunfeng Yun 
557df2069acSChunfeng Yun 	maxp = mtu->g.ep0->maxpacket;
558df2069acSChunfeng Yun 	req = &mreq->request;
559df2069acSChunfeng Yun 
560df2069acSChunfeng Yun 	/* load the data */
561df2069acSChunfeng Yun 	src = (u8 *)req->buf + req->actual;
562df2069acSChunfeng Yun 	count = min(maxp, req->length - req->actual);
563df2069acSChunfeng Yun 	if (count)
564df2069acSChunfeng Yun 		ep0_write_fifo(mtu->ep0, src, count);
565df2069acSChunfeng Yun 
566df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s act=%d, len=%d, cnt=%d, maxp=%d zero=%d\n",
567df2069acSChunfeng Yun 		 __func__, req->actual, req->length, count, maxp, req->zero);
568df2069acSChunfeng Yun 
569df2069acSChunfeng Yun 	req->actual += count;
570df2069acSChunfeng Yun 
571df2069acSChunfeng Yun 	if ((count < maxp)
572df2069acSChunfeng Yun 		|| ((req->actual == req->length) && !req->zero))
573df2069acSChunfeng Yun 		mtu->ep0_state = MU3D_EP0_STATE_TX_END;
574df2069acSChunfeng Yun 
575df2069acSChunfeng Yun 	/* send it out, triggering a "txpktrdy cleared" irq */
576df2069acSChunfeng Yun 	csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR) & EP0_W1C_BITS;
577df2069acSChunfeng Yun 	mtu3_writel(mtu->mac_base, U3D_EP0CSR, csr | EP0_TXPKTRDY);
578df2069acSChunfeng Yun 
579df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s ep0csr=0x%x\n", __func__,
580df2069acSChunfeng Yun 		mtu3_readl(mtu->mac_base, U3D_EP0CSR));
581df2069acSChunfeng Yun }
582df2069acSChunfeng Yun 
583df2069acSChunfeng Yun static void ep0_read_setup(struct mtu3 *mtu, struct usb_ctrlrequest *setup)
584df2069acSChunfeng Yun {
585df2069acSChunfeng Yun 	struct mtu3_request *mreq;
586df2069acSChunfeng Yun 	u32 count;
587df2069acSChunfeng Yun 	u32 csr;
588df2069acSChunfeng Yun 
589df2069acSChunfeng Yun 	csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR) & EP0_W1C_BITS;
590df2069acSChunfeng Yun 	count = mtu3_readl(mtu->mac_base, U3D_RXCOUNT0);
591df2069acSChunfeng Yun 
592df2069acSChunfeng Yun 	ep0_read_fifo(mtu->ep0, (u8 *)setup, count);
593df2069acSChunfeng Yun 
594df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "SETUP req%02x.%02x v%04x i%04x l%04x\n",
595df2069acSChunfeng Yun 		 setup->bRequestType, setup->bRequest,
596df2069acSChunfeng Yun 		 le16_to_cpu(setup->wValue), le16_to_cpu(setup->wIndex),
597df2069acSChunfeng Yun 		 le16_to_cpu(setup->wLength));
598df2069acSChunfeng Yun 
599df2069acSChunfeng Yun 	/* clean up any leftover transfers */
600df2069acSChunfeng Yun 	mreq = next_ep0_request(mtu);
601df2069acSChunfeng Yun 	if (mreq)
602df2069acSChunfeng Yun 		ep0_req_giveback(mtu, &mreq->request);
603df2069acSChunfeng Yun 
604df2069acSChunfeng Yun 	if (le16_to_cpu(setup->wLength) == 0) {
605df2069acSChunfeng Yun 		;	/* no data stage, nothing to do */
606df2069acSChunfeng Yun 	} else if (setup->bRequestType & USB_DIR_IN) {
607df2069acSChunfeng Yun 		mtu3_writel(mtu->mac_base, U3D_EP0CSR,
608df2069acSChunfeng Yun 			csr | EP0_SETUPPKTRDY | EP0_DPHTX);
609df2069acSChunfeng Yun 		mtu->ep0_state = MU3D_EP0_STATE_TX;
610df2069acSChunfeng Yun 	} else {
611df2069acSChunfeng Yun 		mtu3_writel(mtu->mac_base, U3D_EP0CSR,
612df2069acSChunfeng Yun 			(csr | EP0_SETUPPKTRDY) & (~EP0_DPHTX));
613df2069acSChunfeng Yun 		mtu->ep0_state = MU3D_EP0_STATE_RX;
614df2069acSChunfeng Yun 	}
615df2069acSChunfeng Yun }
616df2069acSChunfeng Yun 
617df2069acSChunfeng Yun static int ep0_handle_setup(struct mtu3 *mtu)
618df2069acSChunfeng Yun __releases(mtu->lock)
619df2069acSChunfeng Yun __acquires(mtu->lock)
620df2069acSChunfeng Yun {
621df2069acSChunfeng Yun 	struct usb_ctrlrequest setup;
622df2069acSChunfeng Yun 	struct mtu3_request *mreq;
623df2069acSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
624df2069acSChunfeng Yun 	int handled = 0;
625df2069acSChunfeng Yun 
626df2069acSChunfeng Yun 	ep0_read_setup(mtu, &setup);
627df2069acSChunfeng Yun 
628df2069acSChunfeng Yun 	if ((setup.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
629df2069acSChunfeng Yun 		handled = handle_standard_request(mtu, &setup);
630df2069acSChunfeng Yun 
631df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "handled %d, ep0_state: %s\n",
632df2069acSChunfeng Yun 		 handled, decode_ep0_state(mtu));
633df2069acSChunfeng Yun 
634df2069acSChunfeng Yun 	if (handled < 0)
635df2069acSChunfeng Yun 		goto stall;
636df2069acSChunfeng Yun 	else if (handled > 0)
637df2069acSChunfeng Yun 		goto finish;
638df2069acSChunfeng Yun 
639df2069acSChunfeng Yun 	handled = forward_to_driver(mtu, &setup);
640df2069acSChunfeng Yun 	if (handled < 0) {
641df2069acSChunfeng Yun stall:
642df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "%s stall (%d)\n", __func__, handled);
643df2069acSChunfeng Yun 
644df2069acSChunfeng Yun 		ep0_stall_set(mtu->ep0, true,
645df2069acSChunfeng Yun 			le16_to_cpu(setup.wLength) ? 0 : EP0_SETUPPKTRDY);
646df2069acSChunfeng Yun 
647df2069acSChunfeng Yun 		return 0;
648df2069acSChunfeng Yun 	}
649df2069acSChunfeng Yun 
650df2069acSChunfeng Yun finish:
651df2069acSChunfeng Yun 	if (mtu->test_mode) {
652df2069acSChunfeng Yun 		;	/* nothing to do */
653fe7c994aSChunfeng Yun 	} else if (handled == USB_GADGET_DELAYED_STATUS) {
654fe7c994aSChunfeng Yun 		/* handle the delay STATUS phase till receive ep_queue on ep0 */
655fe7c994aSChunfeng Yun 		mtu->delayed_status = true;
656df2069acSChunfeng Yun 	} else if (le16_to_cpu(setup.wLength) == 0) { /* no data stage */
657df2069acSChunfeng Yun 
658df2069acSChunfeng Yun 		mtu3_writel(mbase, U3D_EP0CSR,
659df2069acSChunfeng Yun 			(mtu3_readl(mbase, U3D_EP0CSR) & EP0_W1C_BITS)
660df2069acSChunfeng Yun 			| EP0_SETUPPKTRDY | EP0_DATAEND);
661df2069acSChunfeng Yun 
662df2069acSChunfeng Yun 		/* complete zlp request directly */
663df2069acSChunfeng Yun 		mreq = next_ep0_request(mtu);
664df2069acSChunfeng Yun 		if (mreq && !mreq->request.length)
665df2069acSChunfeng Yun 			ep0_req_giveback(mtu, &mreq->request);
666df2069acSChunfeng Yun 	}
667df2069acSChunfeng Yun 
668df2069acSChunfeng Yun 	return 0;
669df2069acSChunfeng Yun }
670df2069acSChunfeng Yun 
671df2069acSChunfeng Yun irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu)
672df2069acSChunfeng Yun {
673df2069acSChunfeng Yun 	void __iomem *mbase = mtu->mac_base;
674df2069acSChunfeng Yun 	struct mtu3_request *mreq;
675df2069acSChunfeng Yun 	u32 int_status;
676df2069acSChunfeng Yun 	irqreturn_t ret = IRQ_NONE;
677df2069acSChunfeng Yun 	u32 csr;
678df2069acSChunfeng Yun 	u32 len;
679df2069acSChunfeng Yun 
680df2069acSChunfeng Yun 	int_status = mtu3_readl(mbase, U3D_EPISR);
681df2069acSChunfeng Yun 	int_status &= mtu3_readl(mbase, U3D_EPIER);
682df2069acSChunfeng Yun 	mtu3_writel(mbase, U3D_EPISR, int_status); /* W1C */
683df2069acSChunfeng Yun 
684df2069acSChunfeng Yun 	/* only handle ep0's */
685df2069acSChunfeng Yun 	if (!(int_status & EP0ISR))
686df2069acSChunfeng Yun 		return IRQ_NONE;
687df2069acSChunfeng Yun 
688df2069acSChunfeng Yun 	csr = mtu3_readl(mbase, U3D_EP0CSR);
689df2069acSChunfeng Yun 
690df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s csr=0x%x\n", __func__, csr);
691df2069acSChunfeng Yun 
692df2069acSChunfeng Yun 	/* we sent a stall.. need to clear it now.. */
693df2069acSChunfeng Yun 	if (csr & EP0_SENTSTALL) {
694df2069acSChunfeng Yun 		ep0_stall_set(mtu->ep0, false, 0);
695df2069acSChunfeng Yun 		csr = mtu3_readl(mbase, U3D_EP0CSR);
696df2069acSChunfeng Yun 		ret = IRQ_HANDLED;
697df2069acSChunfeng Yun 	}
698df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "ep0_state: %s\n", decode_ep0_state(mtu));
699df2069acSChunfeng Yun 
700df2069acSChunfeng Yun 	switch (mtu->ep0_state) {
701df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX:
702df2069acSChunfeng Yun 		/* irq on clearing txpktrdy */
703df2069acSChunfeng Yun 		if ((csr & EP0_FIFOFULL) == 0) {
704df2069acSChunfeng Yun 			ep0_tx_state(mtu);
705df2069acSChunfeng Yun 			ret = IRQ_HANDLED;
706df2069acSChunfeng Yun 		}
707df2069acSChunfeng Yun 		break;
708df2069acSChunfeng Yun 	case MU3D_EP0_STATE_RX:
709df2069acSChunfeng Yun 		/* irq on set rxpktrdy */
710df2069acSChunfeng Yun 		if (csr & EP0_RXPKTRDY) {
711df2069acSChunfeng Yun 			ep0_rx_state(mtu);
712df2069acSChunfeng Yun 			ret = IRQ_HANDLED;
713df2069acSChunfeng Yun 		}
714df2069acSChunfeng Yun 		break;
715df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX_END:
716df2069acSChunfeng Yun 		mtu3_writel(mbase, U3D_EP0CSR,
717df2069acSChunfeng Yun 			(csr & EP0_W1C_BITS) | EP0_DATAEND);
718df2069acSChunfeng Yun 
719df2069acSChunfeng Yun 		mreq = next_ep0_request(mtu);
720df2069acSChunfeng Yun 		if (mreq)
721df2069acSChunfeng Yun 			ep0_req_giveback(mtu, &mreq->request);
722df2069acSChunfeng Yun 
723df2069acSChunfeng Yun 		mtu->ep0_state = MU3D_EP0_STATE_SETUP;
724df2069acSChunfeng Yun 		ret = IRQ_HANDLED;
725df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "ep0_state: %s\n", decode_ep0_state(mtu));
726df2069acSChunfeng Yun 		break;
727df2069acSChunfeng Yun 	case MU3D_EP0_STATE_SETUP:
728df2069acSChunfeng Yun 		if (!(csr & EP0_SETUPPKTRDY))
729df2069acSChunfeng Yun 			break;
730df2069acSChunfeng Yun 
731df2069acSChunfeng Yun 		len = mtu3_readl(mbase, U3D_RXCOUNT0);
732df2069acSChunfeng Yun 		if (len != 8) {
733df2069acSChunfeng Yun 			dev_err(mtu->dev, "SETUP packet len %d != 8 ?\n", len);
734df2069acSChunfeng Yun 			break;
735df2069acSChunfeng Yun 		}
736df2069acSChunfeng Yun 
737df2069acSChunfeng Yun 		ep0_handle_setup(mtu);
738df2069acSChunfeng Yun 		ret = IRQ_HANDLED;
739df2069acSChunfeng Yun 		break;
740df2069acSChunfeng Yun 	default:
741df2069acSChunfeng Yun 		/* can't happen */
742df2069acSChunfeng Yun 		ep0_stall_set(mtu->ep0, true, 0);
743df2069acSChunfeng Yun 		WARN_ON(1);
744df2069acSChunfeng Yun 		break;
745df2069acSChunfeng Yun 	}
746df2069acSChunfeng Yun 
747df2069acSChunfeng Yun 	return ret;
748df2069acSChunfeng Yun }
749df2069acSChunfeng Yun 
750df2069acSChunfeng Yun 
751df2069acSChunfeng Yun static int mtu3_ep0_enable(struct usb_ep *ep,
752df2069acSChunfeng Yun 	const struct usb_endpoint_descriptor *desc)
753df2069acSChunfeng Yun {
754df2069acSChunfeng Yun 	/* always enabled */
755df2069acSChunfeng Yun 	return -EINVAL;
756df2069acSChunfeng Yun }
757df2069acSChunfeng Yun 
758df2069acSChunfeng Yun static int mtu3_ep0_disable(struct usb_ep *ep)
759df2069acSChunfeng Yun {
760df2069acSChunfeng Yun 	/* always enabled */
761df2069acSChunfeng Yun 	return -EINVAL;
762df2069acSChunfeng Yun }
763df2069acSChunfeng Yun 
764df2069acSChunfeng Yun static int ep0_queue(struct mtu3_ep *mep, struct mtu3_request *mreq)
765df2069acSChunfeng Yun {
766df2069acSChunfeng Yun 	struct mtu3 *mtu = mep->mtu;
767df2069acSChunfeng Yun 
768df2069acSChunfeng Yun 	mreq->mtu = mtu;
769df2069acSChunfeng Yun 	mreq->request.actual = 0;
770df2069acSChunfeng Yun 	mreq->request.status = -EINPROGRESS;
771df2069acSChunfeng Yun 
772df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s %s (ep0_state: %s), len#%d\n", __func__,
773df2069acSChunfeng Yun 		mep->name, decode_ep0_state(mtu), mreq->request.length);
774df2069acSChunfeng Yun 
775df2069acSChunfeng Yun 	switch (mtu->ep0_state) {
776df2069acSChunfeng Yun 	case MU3D_EP0_STATE_SETUP:
777df2069acSChunfeng Yun 	case MU3D_EP0_STATE_RX:	/* control-OUT data */
778df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX:	/* control-IN data */
779df2069acSChunfeng Yun 		break;
780df2069acSChunfeng Yun 	default:
781df2069acSChunfeng Yun 		dev_err(mtu->dev, "%s, error in ep0 state %s\n", __func__,
782df2069acSChunfeng Yun 			decode_ep0_state(mtu));
783df2069acSChunfeng Yun 		return -EINVAL;
784df2069acSChunfeng Yun 	}
785df2069acSChunfeng Yun 
786fe7c994aSChunfeng Yun 	if (mtu->delayed_status) {
787fe7c994aSChunfeng Yun 		u32 csr;
788fe7c994aSChunfeng Yun 
789fe7c994aSChunfeng Yun 		mtu->delayed_status = false;
790fe7c994aSChunfeng Yun 		csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR) & EP0_W1C_BITS;
791fe7c994aSChunfeng Yun 		csr |= EP0_SETUPPKTRDY | EP0_DATAEND;
792fe7c994aSChunfeng Yun 		mtu3_writel(mtu->mac_base, U3D_EP0CSR, csr);
793fe7c994aSChunfeng Yun 		/* needn't giveback the request for handling delay STATUS */
794fe7c994aSChunfeng Yun 		return 0;
795fe7c994aSChunfeng Yun 	}
796fe7c994aSChunfeng Yun 
797fe7c994aSChunfeng Yun 	if (!list_empty(&mep->req_list))
798fe7c994aSChunfeng Yun 		return -EBUSY;
799fe7c994aSChunfeng Yun 
800df2069acSChunfeng Yun 	list_add_tail(&mreq->list, &mep->req_list);
801df2069acSChunfeng Yun 
802df2069acSChunfeng Yun 	/* sequence #1, IN ... start writing the data */
803df2069acSChunfeng Yun 	if (mtu->ep0_state == MU3D_EP0_STATE_TX)
804df2069acSChunfeng Yun 		ep0_tx_state(mtu);
805df2069acSChunfeng Yun 
806df2069acSChunfeng Yun 	return 0;
807df2069acSChunfeng Yun }
808df2069acSChunfeng Yun 
809df2069acSChunfeng Yun static int mtu3_ep0_queue(struct usb_ep *ep,
810df2069acSChunfeng Yun 	struct usb_request *req, gfp_t gfp)
811df2069acSChunfeng Yun {
812df2069acSChunfeng Yun 	struct mtu3_ep *mep;
813df2069acSChunfeng Yun 	struct mtu3_request *mreq;
814df2069acSChunfeng Yun 	struct mtu3 *mtu;
815df2069acSChunfeng Yun 	unsigned long flags;
816df2069acSChunfeng Yun 	int ret = 0;
817df2069acSChunfeng Yun 
818df2069acSChunfeng Yun 	if (!ep || !req)
819df2069acSChunfeng Yun 		return -EINVAL;
820df2069acSChunfeng Yun 
821df2069acSChunfeng Yun 	mep = to_mtu3_ep(ep);
822df2069acSChunfeng Yun 	mtu = mep->mtu;
823df2069acSChunfeng Yun 	mreq = to_mtu3_request(req);
824df2069acSChunfeng Yun 
825df2069acSChunfeng Yun 	spin_lock_irqsave(&mtu->lock, flags);
826df2069acSChunfeng Yun 	ret = ep0_queue(mep, mreq);
827df2069acSChunfeng Yun 	spin_unlock_irqrestore(&mtu->lock, flags);
828df2069acSChunfeng Yun 	return ret;
829df2069acSChunfeng Yun }
830df2069acSChunfeng Yun 
831df2069acSChunfeng Yun static int mtu3_ep0_dequeue(struct usb_ep *ep, struct usb_request *req)
832df2069acSChunfeng Yun {
833df2069acSChunfeng Yun 	/* we just won't support this */
834df2069acSChunfeng Yun 	return -EINVAL;
835df2069acSChunfeng Yun }
836df2069acSChunfeng Yun 
837df2069acSChunfeng Yun static int mtu3_ep0_halt(struct usb_ep *ep, int value)
838df2069acSChunfeng Yun {
839df2069acSChunfeng Yun 	struct mtu3_ep *mep;
840df2069acSChunfeng Yun 	struct mtu3 *mtu;
841df2069acSChunfeng Yun 	unsigned long flags;
842df2069acSChunfeng Yun 	int ret = 0;
843df2069acSChunfeng Yun 
844df2069acSChunfeng Yun 	if (!ep || !value)
845df2069acSChunfeng Yun 		return -EINVAL;
846df2069acSChunfeng Yun 
847df2069acSChunfeng Yun 	mep = to_mtu3_ep(ep);
848df2069acSChunfeng Yun 	mtu = mep->mtu;
849df2069acSChunfeng Yun 
850df2069acSChunfeng Yun 	dev_dbg(mtu->dev, "%s\n", __func__);
851df2069acSChunfeng Yun 
852df2069acSChunfeng Yun 	spin_lock_irqsave(&mtu->lock, flags);
853df2069acSChunfeng Yun 
854df2069acSChunfeng Yun 	if (!list_empty(&mep->req_list)) {
855df2069acSChunfeng Yun 		ret = -EBUSY;
856df2069acSChunfeng Yun 		goto cleanup;
857df2069acSChunfeng Yun 	}
858df2069acSChunfeng Yun 
859df2069acSChunfeng Yun 	switch (mtu->ep0_state) {
860df2069acSChunfeng Yun 	/*
861df2069acSChunfeng Yun 	 * stalls are usually issued after parsing SETUP packet, either
862df2069acSChunfeng Yun 	 * directly in irq context from setup() or else later.
863df2069acSChunfeng Yun 	 */
864df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX:
865df2069acSChunfeng Yun 	case MU3D_EP0_STATE_TX_END:
866df2069acSChunfeng Yun 	case MU3D_EP0_STATE_RX:
867df2069acSChunfeng Yun 	case MU3D_EP0_STATE_SETUP:
868df2069acSChunfeng Yun 		ep0_stall_set(mtu->ep0, true, 0);
869df2069acSChunfeng Yun 		break;
870df2069acSChunfeng Yun 	default:
871df2069acSChunfeng Yun 		dev_dbg(mtu->dev, "ep0 can't halt in state %s\n",
872df2069acSChunfeng Yun 			decode_ep0_state(mtu));
873df2069acSChunfeng Yun 		ret = -EINVAL;
874df2069acSChunfeng Yun 	}
875df2069acSChunfeng Yun 
876df2069acSChunfeng Yun cleanup:
877df2069acSChunfeng Yun 	spin_unlock_irqrestore(&mtu->lock, flags);
878df2069acSChunfeng Yun 	return ret;
879df2069acSChunfeng Yun }
880df2069acSChunfeng Yun 
881df2069acSChunfeng Yun const struct usb_ep_ops mtu3_ep0_ops = {
882df2069acSChunfeng Yun 	.enable = mtu3_ep0_enable,
883df2069acSChunfeng Yun 	.disable = mtu3_ep0_disable,
884df2069acSChunfeng Yun 	.alloc_request = mtu3_alloc_request,
885df2069acSChunfeng Yun 	.free_request = mtu3_free_request,
886df2069acSChunfeng Yun 	.queue = mtu3_ep0_queue,
887df2069acSChunfeng Yun 	.dequeue = mtu3_ep0_dequeue,
888df2069acSChunfeng Yun 	.set_halt = mtu3_ep0_halt,
889df2069acSChunfeng Yun };
890