xref: /OK3568_Linux_fs/u-boot/drivers/usb/gadget/f_thor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * f_thor.c -- USB TIZEN THOR Downloader gadget function
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2013 Samsung Electronics
5*4882a593Smuzhiyun  * Lukasz Majewski <l.majewski@samsung.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Based on code from:
8*4882a593Smuzhiyun  * git://review.tizen.org/kernel/u-boot
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Developed by:
11*4882a593Smuzhiyun  * Copyright (C) 2009 Samsung Electronics
12*4882a593Smuzhiyun  * Minkyu Kang <mk7.kang@samsung.com>
13*4882a593Smuzhiyun  * Sanghee Kim <sh0130.kim@samsung.com>
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <errno.h>
19*4882a593Smuzhiyun #include <common.h>
20*4882a593Smuzhiyun #include <console.h>
21*4882a593Smuzhiyun #include <malloc.h>
22*4882a593Smuzhiyun #include <memalign.h>
23*4882a593Smuzhiyun #include <version.h>
24*4882a593Smuzhiyun #include <linux/usb/ch9.h>
25*4882a593Smuzhiyun #include <linux/usb/gadget.h>
26*4882a593Smuzhiyun #include <linux/usb/composite.h>
27*4882a593Smuzhiyun #include <linux/usb/cdc.h>
28*4882a593Smuzhiyun #include <g_dnl.h>
29*4882a593Smuzhiyun #include <dfu.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include "f_thor.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static void thor_tx_data(unsigned char *data, int len);
34*4882a593Smuzhiyun static void thor_set_dma(void *addr, int len);
35*4882a593Smuzhiyun static int thor_rx_data(void);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static struct f_thor *thor_func;
func_to_thor(struct usb_function * f)38*4882a593Smuzhiyun static inline struct f_thor *func_to_thor(struct usb_function *f)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	return container_of(f, struct f_thor, usb_function);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
44*4882a593Smuzhiyun 			  sizeof(struct rsp_box));
45*4882a593Smuzhiyun DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
46*4882a593Smuzhiyun 			  sizeof(struct rqt_box));
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /* ********************************************************** */
49*4882a593Smuzhiyun /*         THOR protocol - transmission handling	      */
50*4882a593Smuzhiyun /* ********************************************************** */
51*4882a593Smuzhiyun DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
52*4882a593Smuzhiyun static unsigned long long int thor_file_size;
53*4882a593Smuzhiyun static int alt_setting_num;
54*4882a593Smuzhiyun 
send_rsp(const struct rsp_box * rsp)55*4882a593Smuzhiyun static void send_rsp(const struct rsp_box *rsp)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
58*4882a593Smuzhiyun 	thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
send_data_rsp(s32 ack,s32 count)63*4882a593Smuzhiyun static void send_data_rsp(s32 ack, s32 count)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
66*4882a593Smuzhiyun 				 sizeof(struct data_rsp_box));
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	rsp->ack = ack;
69*4882a593Smuzhiyun 	rsp->count = count;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
72*4882a593Smuzhiyun 	thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	debug("-DATA RSP: %d, %d\n", ack, count);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
process_rqt_info(const struct rqt_box * rqt)77*4882a593Smuzhiyun static int process_rqt_info(const struct rqt_box *rqt)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
80*4882a593Smuzhiyun 	memset(rsp, 0, sizeof(struct rsp_box));
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	rsp->rsp = rqt->rqt;
83*4882a593Smuzhiyun 	rsp->rsp_data = rqt->rqt_data;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	switch (rqt->rqt_data) {
86*4882a593Smuzhiyun 	case RQT_INFO_VER_PROTOCOL:
87*4882a593Smuzhiyun 		rsp->int_data[0] = VER_PROTOCOL_MAJOR;
88*4882a593Smuzhiyun 		rsp->int_data[1] = VER_PROTOCOL_MINOR;
89*4882a593Smuzhiyun 		break;
90*4882a593Smuzhiyun 	case RQT_INIT_VER_HW:
91*4882a593Smuzhiyun 		snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]),
92*4882a593Smuzhiyun 			 "%x", checkboard());
93*4882a593Smuzhiyun 		break;
94*4882a593Smuzhiyun 	case RQT_INIT_VER_BOOT:
95*4882a593Smuzhiyun 		sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
96*4882a593Smuzhiyun 		break;
97*4882a593Smuzhiyun 	case RQT_INIT_VER_KERNEL:
98*4882a593Smuzhiyun 		sprintf(rsp->str_data[0], "%s", "k unknown");
99*4882a593Smuzhiyun 		break;
100*4882a593Smuzhiyun 	case RQT_INIT_VER_PLATFORM:
101*4882a593Smuzhiyun 		sprintf(rsp->str_data[0], "%s", "p unknown");
102*4882a593Smuzhiyun 		break;
103*4882a593Smuzhiyun 	case RQT_INIT_VER_CSC:
104*4882a593Smuzhiyun 		sprintf(rsp->str_data[0], "%s", "c unknown");
105*4882a593Smuzhiyun 		break;
106*4882a593Smuzhiyun 	default:
107*4882a593Smuzhiyun 		return -EINVAL;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	send_rsp(rsp);
111*4882a593Smuzhiyun 	return true;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
process_rqt_cmd(const struct rqt_box * rqt)114*4882a593Smuzhiyun static int process_rqt_cmd(const struct rqt_box *rqt)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
117*4882a593Smuzhiyun 	memset(rsp, 0, sizeof(struct rsp_box));
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	rsp->rsp = rqt->rqt;
120*4882a593Smuzhiyun 	rsp->rsp_data = rqt->rqt_data;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	switch (rqt->rqt_data) {
123*4882a593Smuzhiyun 	case RQT_CMD_REBOOT:
124*4882a593Smuzhiyun 		debug("TARGET RESET\n");
125*4882a593Smuzhiyun 		send_rsp(rsp);
126*4882a593Smuzhiyun 		g_dnl_unregister();
127*4882a593Smuzhiyun 		dfu_free_entities();
128*4882a593Smuzhiyun #ifdef CONFIG_THOR_RESET_OFF
129*4882a593Smuzhiyun 		return RESET_DONE;
130*4882a593Smuzhiyun #endif
131*4882a593Smuzhiyun 		run_command("reset", 0);
132*4882a593Smuzhiyun 		break;
133*4882a593Smuzhiyun 	case RQT_CMD_POWEROFF:
134*4882a593Smuzhiyun 	case RQT_CMD_EFSCLEAR:
135*4882a593Smuzhiyun 		send_rsp(rsp);
136*4882a593Smuzhiyun 	default:
137*4882a593Smuzhiyun 		printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
138*4882a593Smuzhiyun 		return -EINVAL;
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	return true;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
download_head(unsigned long long total,unsigned int packet_size,long long int * left,int * cnt)144*4882a593Smuzhiyun static long long int download_head(unsigned long long total,
145*4882a593Smuzhiyun 				   unsigned int packet_size,
146*4882a593Smuzhiyun 				   long long int *left,
147*4882a593Smuzhiyun 				   int *cnt)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	long long int rcv_cnt = 0, left_to_rcv, ret_rcv;
150*4882a593Smuzhiyun 	struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
151*4882a593Smuzhiyun 	void *transfer_buffer = dfu_get_buf(dfu_entity);
152*4882a593Smuzhiyun 	void *buf = transfer_buffer;
153*4882a593Smuzhiyun 	int usb_pkt_cnt = 0, ret;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/*
156*4882a593Smuzhiyun 	 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
157*4882a593Smuzhiyun 	 * the medium.
158*4882a593Smuzhiyun 	 * The packet response is sent on the purpose after successful data
159*4882a593Smuzhiyun 	 * chunk write. There is a room for improvement when asynchronous write
160*4882a593Smuzhiyun 	 * is performed.
161*4882a593Smuzhiyun 	 */
162*4882a593Smuzhiyun 	while (total - rcv_cnt >= packet_size) {
163*4882a593Smuzhiyun 		thor_set_dma(buf, packet_size);
164*4882a593Smuzhiyun 		buf += packet_size;
165*4882a593Smuzhiyun 		ret_rcv = thor_rx_data();
166*4882a593Smuzhiyun 		if (ret_rcv < 0)
167*4882a593Smuzhiyun 			return ret_rcv;
168*4882a593Smuzhiyun 		rcv_cnt += ret_rcv;
169*4882a593Smuzhiyun 		debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt,
170*4882a593Smuzhiyun 		      rcv_cnt, *cnt);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) {
173*4882a593Smuzhiyun 			ret = dfu_write(dfu_get_entity(alt_setting_num),
174*4882a593Smuzhiyun 					transfer_buffer, THOR_STORE_UNIT_SIZE,
175*4882a593Smuzhiyun 					(*cnt)++);
176*4882a593Smuzhiyun 			if (ret) {
177*4882a593Smuzhiyun 				pr_err("DFU write failed [%d] cnt: %d",
178*4882a593Smuzhiyun 				      ret, *cnt);
179*4882a593Smuzhiyun 				return ret;
180*4882a593Smuzhiyun 			}
181*4882a593Smuzhiyun 			buf = transfer_buffer;
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 		send_data_rsp(0, ++usb_pkt_cnt);
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	/* Calculate the amount of data to arrive from PC (in bytes) */
187*4882a593Smuzhiyun 	left_to_rcv = total - rcv_cnt;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/*
190*4882a593Smuzhiyun 	 * Calculate number of data already received. but not yet stored
191*4882a593Smuzhiyun 	 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
192*4882a593Smuzhiyun 	 */
193*4882a593Smuzhiyun 	*left = left_to_rcv + buf - transfer_buffer;
194*4882a593Smuzhiyun 	debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__,
195*4882a593Smuzhiyun 	      *left, left_to_rcv, buf);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (left_to_rcv) {
198*4882a593Smuzhiyun 		thor_set_dma(buf, packet_size);
199*4882a593Smuzhiyun 		ret_rcv = thor_rx_data();
200*4882a593Smuzhiyun 		if (ret_rcv < 0)
201*4882a593Smuzhiyun 			return ret_rcv;
202*4882a593Smuzhiyun 		rcv_cnt += ret_rcv;
203*4882a593Smuzhiyun 		send_data_rsp(0, ++usb_pkt_cnt);
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	return rcv_cnt;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
download_tail(long long int left,int cnt)211*4882a593Smuzhiyun static int download_tail(long long int left, int cnt)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	struct dfu_entity *dfu_entity;
214*4882a593Smuzhiyun 	void *transfer_buffer;
215*4882a593Smuzhiyun 	int ret;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	dfu_entity = dfu_get_entity(alt_setting_num);
220*4882a593Smuzhiyun 	if (!dfu_entity) {
221*4882a593Smuzhiyun 		pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
222*4882a593Smuzhiyun 		return -ENOENT;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	transfer_buffer = dfu_get_buf(dfu_entity);
226*4882a593Smuzhiyun 	if (!transfer_buffer) {
227*4882a593Smuzhiyun 		pr_err("Transfer buffer not allocated!");
228*4882a593Smuzhiyun 		return -ENXIO;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	if (left) {
232*4882a593Smuzhiyun 		ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
233*4882a593Smuzhiyun 		if (ret) {
234*4882a593Smuzhiyun 			pr_err("DFU write failed [%d]: left: %llu", ret, left);
235*4882a593Smuzhiyun 			return ret;
236*4882a593Smuzhiyun 		}
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/*
240*4882a593Smuzhiyun 	 * To store last "packet" or write file from buffer to filesystem
241*4882a593Smuzhiyun 	 * DFU storage backend requires dfu_flush
242*4882a593Smuzhiyun 	 *
243*4882a593Smuzhiyun 	 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
244*4882a593Smuzhiyun 	 * need fo call dfu_free_buf() is needed.
245*4882a593Smuzhiyun 	 */
246*4882a593Smuzhiyun 	ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
247*4882a593Smuzhiyun 	if (ret)
248*4882a593Smuzhiyun 		pr_err("DFU flush failed!");
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	return ret;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
process_rqt_download(const struct rqt_box * rqt)253*4882a593Smuzhiyun static long long int process_rqt_download(const struct rqt_box *rqt)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
256*4882a593Smuzhiyun 	static long long int left, ret_head;
257*4882a593Smuzhiyun 	int file_type, ret = 0;
258*4882a593Smuzhiyun 	static int cnt;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	memset(rsp, 0, sizeof(struct rsp_box));
261*4882a593Smuzhiyun 	rsp->rsp = rqt->rqt;
262*4882a593Smuzhiyun 	rsp->rsp_data = rqt->rqt_data;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	switch (rqt->rqt_data) {
265*4882a593Smuzhiyun 	case RQT_DL_INIT:
266*4882a593Smuzhiyun 		thor_file_size = (unsigned long long int)rqt->int_data[0] +
267*4882a593Smuzhiyun 				 (((unsigned long long int)rqt->int_data[1])
268*4882a593Smuzhiyun 				  << 32);
269*4882a593Smuzhiyun 		debug("INIT: total %llu bytes\n", thor_file_size);
270*4882a593Smuzhiyun 		break;
271*4882a593Smuzhiyun 	case RQT_DL_FILE_INFO:
272*4882a593Smuzhiyun 		file_type = rqt->int_data[0];
273*4882a593Smuzhiyun 		if (file_type == FILE_TYPE_PIT) {
274*4882a593Smuzhiyun 			puts("PIT table file - not supported\n");
275*4882a593Smuzhiyun 			rsp->ack = -ENOTSUPP;
276*4882a593Smuzhiyun 			ret = rsp->ack;
277*4882a593Smuzhiyun 			break;
278*4882a593Smuzhiyun 		}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		thor_file_size = (unsigned long long int)rqt->int_data[1] +
281*4882a593Smuzhiyun 				 (((unsigned long long int)rqt->int_data[2])
282*4882a593Smuzhiyun 				  << 32);
283*4882a593Smuzhiyun 		memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
284*4882a593Smuzhiyun 		f_name[F_NAME_BUF_SIZE] = '\0';
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
287*4882a593Smuzhiyun 		      f_name, 0, thor_file_size, file_type);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		rsp->int_data[0] = THOR_PACKET_SIZE;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 		alt_setting_num = dfu_get_alt(f_name);
292*4882a593Smuzhiyun 		if (alt_setting_num < 0) {
293*4882a593Smuzhiyun 			pr_err("Alt setting [%d] to write not found!",
294*4882a593Smuzhiyun 			      alt_setting_num);
295*4882a593Smuzhiyun 			rsp->ack = -ENODEV;
296*4882a593Smuzhiyun 			ret = rsp->ack;
297*4882a593Smuzhiyun 		}
298*4882a593Smuzhiyun 		break;
299*4882a593Smuzhiyun 	case RQT_DL_FILE_START:
300*4882a593Smuzhiyun 		send_rsp(rsp);
301*4882a593Smuzhiyun 		ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
302*4882a593Smuzhiyun 					 &left, &cnt);
303*4882a593Smuzhiyun 		if (ret_head < 0) {
304*4882a593Smuzhiyun 			left = 0;
305*4882a593Smuzhiyun 			cnt = 0;
306*4882a593Smuzhiyun 		}
307*4882a593Smuzhiyun 		return ret_head;
308*4882a593Smuzhiyun 	case RQT_DL_FILE_END:
309*4882a593Smuzhiyun 		debug("DL FILE_END\n");
310*4882a593Smuzhiyun 		rsp->ack = download_tail(left, cnt);
311*4882a593Smuzhiyun 		ret = rsp->ack;
312*4882a593Smuzhiyun 		left = 0;
313*4882a593Smuzhiyun 		cnt = 0;
314*4882a593Smuzhiyun 		break;
315*4882a593Smuzhiyun 	case RQT_DL_EXIT:
316*4882a593Smuzhiyun 		debug("DL EXIT\n");
317*4882a593Smuzhiyun 		break;
318*4882a593Smuzhiyun 	default:
319*4882a593Smuzhiyun 		pr_err("Operation not supported: %d", rqt->rqt_data);
320*4882a593Smuzhiyun 		ret = -ENOTSUPP;
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	send_rsp(rsp);
324*4882a593Smuzhiyun 	return ret;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
process_data(void)327*4882a593Smuzhiyun static int process_data(void)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
330*4882a593Smuzhiyun 	int ret = -EINVAL;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	switch (rqt->rqt) {
337*4882a593Smuzhiyun 	case RQT_INFO:
338*4882a593Smuzhiyun 		ret = process_rqt_info(rqt);
339*4882a593Smuzhiyun 		break;
340*4882a593Smuzhiyun 	case RQT_CMD:
341*4882a593Smuzhiyun 		ret = process_rqt_cmd(rqt);
342*4882a593Smuzhiyun 		break;
343*4882a593Smuzhiyun 	case RQT_DL:
344*4882a593Smuzhiyun 		ret = (int) process_rqt_download(rqt);
345*4882a593Smuzhiyun 		break;
346*4882a593Smuzhiyun 	case RQT_UL:
347*4882a593Smuzhiyun 		puts("RQT: UPLOAD not supported!\n");
348*4882a593Smuzhiyun 		break;
349*4882a593Smuzhiyun 	default:
350*4882a593Smuzhiyun 		pr_err("unknown request (%d)", rqt->rqt);
351*4882a593Smuzhiyun 	}
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	return ret;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun /* ********************************************************** */
357*4882a593Smuzhiyun /*         THOR USB Function				      */
358*4882a593Smuzhiyun /* ********************************************************** */
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun static inline struct usb_endpoint_descriptor *
ep_desc(struct usb_gadget * g,struct usb_endpoint_descriptor * hs,struct usb_endpoint_descriptor * fs)361*4882a593Smuzhiyun ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
362*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *fs)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
365*4882a593Smuzhiyun 		return hs;
366*4882a593Smuzhiyun 	return fs;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun static struct usb_interface_descriptor thor_downloader_intf_data = {
370*4882a593Smuzhiyun 	.bLength =		sizeof(thor_downloader_intf_data),
371*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_INTERFACE,
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	.bNumEndpoints =	2,
374*4882a593Smuzhiyun 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
375*4882a593Smuzhiyun };
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun static struct usb_endpoint_descriptor fs_in_desc = {
378*4882a593Smuzhiyun 	.bLength =		USB_DT_ENDPOINT_SIZE,
379*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_ENDPOINT,
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	.bEndpointAddress =	USB_DIR_IN,
382*4882a593Smuzhiyun 	.bmAttributes =	USB_ENDPOINT_XFER_BULK,
383*4882a593Smuzhiyun };
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun static struct usb_endpoint_descriptor fs_out_desc = {
386*4882a593Smuzhiyun 	.bLength =		USB_DT_ENDPOINT_SIZE,
387*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_ENDPOINT,
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	.bEndpointAddress =	USB_DIR_OUT,
390*4882a593Smuzhiyun 	.bmAttributes =	USB_ENDPOINT_XFER_BULK,
391*4882a593Smuzhiyun };
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /* CDC configuration */
394*4882a593Smuzhiyun static struct usb_interface_descriptor thor_downloader_intf_int = {
395*4882a593Smuzhiyun 	.bLength =		sizeof(thor_downloader_intf_int),
396*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_INTERFACE,
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	.bNumEndpoints =	1,
399*4882a593Smuzhiyun 	.bInterfaceClass =	USB_CLASS_COMM,
400*4882a593Smuzhiyun 	 /* 0x02 Abstract Line Control Model */
401*4882a593Smuzhiyun 	.bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
402*4882a593Smuzhiyun 	/* 0x01 Common AT commands */
403*4882a593Smuzhiyun 	.bInterfaceProtocol =   USB_CDC_ACM_PROTO_AT_V25TER,
404*4882a593Smuzhiyun };
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun static struct usb_cdc_header_desc thor_downloader_cdc_header = {
407*4882a593Smuzhiyun 	.bLength         =    sizeof(thor_downloader_cdc_header),
408*4882a593Smuzhiyun 	.bDescriptorType =    0x24, /* CS_INTERFACE */
409*4882a593Smuzhiyun 	.bDescriptorSubType = 0x00,
410*4882a593Smuzhiyun 	.bcdCDC =             0x0110,
411*4882a593Smuzhiyun };
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
414*4882a593Smuzhiyun 	.bLength         =    sizeof(thor_downloader_cdc_call),
415*4882a593Smuzhiyun 	.bDescriptorType =    0x24, /* CS_INTERFACE */
416*4882a593Smuzhiyun 	.bDescriptorSubType = 0x01,
417*4882a593Smuzhiyun 	.bmCapabilities =     0x00,
418*4882a593Smuzhiyun 	.bDataInterface =     0x01,
419*4882a593Smuzhiyun };
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
422*4882a593Smuzhiyun 	.bLength         =    sizeof(thor_downloader_cdc_abstract),
423*4882a593Smuzhiyun 	.bDescriptorType =    0x24, /* CS_INTERFACE */
424*4882a593Smuzhiyun 	.bDescriptorSubType = 0x02,
425*4882a593Smuzhiyun 	.bmCapabilities =     0x00,
426*4882a593Smuzhiyun };
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun static struct usb_cdc_union_desc thor_downloader_cdc_union = {
429*4882a593Smuzhiyun 	.bLength         =     sizeof(thor_downloader_cdc_union),
430*4882a593Smuzhiyun 	.bDescriptorType =     0x24, /* CS_INTERFACE */
431*4882a593Smuzhiyun 	.bDescriptorSubType =  USB_CDC_UNION_TYPE,
432*4882a593Smuzhiyun };
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun static struct usb_endpoint_descriptor fs_int_desc = {
435*4882a593Smuzhiyun 	.bLength = USB_DT_ENDPOINT_SIZE,
436*4882a593Smuzhiyun 	.bDescriptorType = USB_DT_ENDPOINT,
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	.bEndpointAddress = 3 | USB_DIR_IN,
439*4882a593Smuzhiyun 	.bmAttributes = USB_ENDPOINT_XFER_INT,
440*4882a593Smuzhiyun 	.wMaxPacketSize = __constant_cpu_to_le16(16),
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	.bInterval = 0x9,
443*4882a593Smuzhiyun };
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun static struct usb_interface_assoc_descriptor
446*4882a593Smuzhiyun thor_iad_descriptor = {
447*4882a593Smuzhiyun 	.bLength =		sizeof(thor_iad_descriptor),
448*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_INTERFACE_ASSOCIATION,
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	.bFirstInterface =	0,
451*4882a593Smuzhiyun 	.bInterfaceCount =	2,	/* control + data */
452*4882a593Smuzhiyun 	.bFunctionClass =	USB_CLASS_COMM,
453*4882a593Smuzhiyun 	.bFunctionSubClass =	USB_CDC_SUBCLASS_ACM,
454*4882a593Smuzhiyun 	.bFunctionProtocol =	USB_CDC_PROTO_NONE,
455*4882a593Smuzhiyun };
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun static struct usb_endpoint_descriptor hs_in_desc = {
458*4882a593Smuzhiyun 	.bLength =		USB_DT_ENDPOINT_SIZE,
459*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_ENDPOINT,
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	.bmAttributes =	USB_ENDPOINT_XFER_BULK,
462*4882a593Smuzhiyun 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
463*4882a593Smuzhiyun };
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun static struct usb_endpoint_descriptor hs_out_desc = {
466*4882a593Smuzhiyun 	.bLength =		USB_DT_ENDPOINT_SIZE,
467*4882a593Smuzhiyun 	.bDescriptorType =	USB_DT_ENDPOINT,
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	.bmAttributes =	USB_ENDPOINT_XFER_BULK,
470*4882a593Smuzhiyun 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
471*4882a593Smuzhiyun };
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun static struct usb_endpoint_descriptor hs_int_desc = {
474*4882a593Smuzhiyun 	.bLength = USB_DT_ENDPOINT_SIZE,
475*4882a593Smuzhiyun 	.bDescriptorType = USB_DT_ENDPOINT,
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	.bmAttributes = USB_ENDPOINT_XFER_INT,
478*4882a593Smuzhiyun 	.wMaxPacketSize = __constant_cpu_to_le16(16),
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	.bInterval = 0x9,
481*4882a593Smuzhiyun };
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /*
484*4882a593Smuzhiyun  * This attribute vendor descriptor is necessary for correct operation with
485*4882a593Smuzhiyun  * Windows version of THOR download program
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  * It prevents windows driver from sending zero lenght packet (ZLP) after
488*4882a593Smuzhiyun  * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
489*4882a593Smuzhiyun  */
490*4882a593Smuzhiyun static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
491*4882a593Smuzhiyun 	.bLength =              sizeof(thor_downloader_cdc_av),
492*4882a593Smuzhiyun 	.bDescriptorType =      0x24,
493*4882a593Smuzhiyun 	.bDescriptorSubType =   0x80,
494*4882a593Smuzhiyun 	.DAUType =              0x0002,
495*4882a593Smuzhiyun 	.DAULength =            0x0001,
496*4882a593Smuzhiyun 	.DAUValue =             0x00,
497*4882a593Smuzhiyun };
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
500*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_iad_descriptor,
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_intf_int,
503*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_cdc_header,
504*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_cdc_call,
505*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
506*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_cdc_union,
507*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&hs_int_desc,
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_intf_data,
510*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&thor_downloader_cdc_av,
511*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&hs_in_desc,
512*4882a593Smuzhiyun 	(struct usb_descriptor_header *)&hs_out_desc,
513*4882a593Smuzhiyun 	NULL,
514*4882a593Smuzhiyun };
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
alloc_ep_req(struct usb_ep * ep,unsigned length)517*4882a593Smuzhiyun static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	struct usb_request *req;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	req = usb_ep_alloc_request(ep, 0);
522*4882a593Smuzhiyun 	if (!req)
523*4882a593Smuzhiyun 		return req;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	req->length = length;
526*4882a593Smuzhiyun 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
527*4882a593Smuzhiyun 	if (!req->buf) {
528*4882a593Smuzhiyun 		usb_ep_free_request(ep, req);
529*4882a593Smuzhiyun 		req = NULL;
530*4882a593Smuzhiyun 	}
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	return req;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
thor_rx_data(void)535*4882a593Smuzhiyun static int thor_rx_data(void)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
538*4882a593Smuzhiyun 	int data_to_rx, tmp, status;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	data_to_rx = dev->out_req->length;
541*4882a593Smuzhiyun 	tmp = data_to_rx;
542*4882a593Smuzhiyun 	do {
543*4882a593Smuzhiyun 		dev->out_req->length = data_to_rx;
544*4882a593Smuzhiyun 		debug("dev->out_req->length:%d dev->rxdata:%d\n",
545*4882a593Smuzhiyun 		      dev->out_req->length, dev->rxdata);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 		status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
548*4882a593Smuzhiyun 		if (status) {
549*4882a593Smuzhiyun 			pr_err("kill %s:  resubmit %d bytes --> %d",
550*4882a593Smuzhiyun 			      dev->out_ep->name, dev->out_req->length, status);
551*4882a593Smuzhiyun 			usb_ep_set_halt(dev->out_ep);
552*4882a593Smuzhiyun 			return -EAGAIN;
553*4882a593Smuzhiyun 		}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 		while (!dev->rxdata) {
556*4882a593Smuzhiyun 			usb_gadget_handle_interrupts(0);
557*4882a593Smuzhiyun 			if (ctrlc())
558*4882a593Smuzhiyun 				return -1;
559*4882a593Smuzhiyun 		}
560*4882a593Smuzhiyun 		dev->rxdata = 0;
561*4882a593Smuzhiyun 		data_to_rx -= dev->out_req->actual;
562*4882a593Smuzhiyun 	} while (data_to_rx);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	return tmp;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
thor_tx_data(unsigned char * data,int len)567*4882a593Smuzhiyun static void thor_tx_data(unsigned char *data, int len)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
570*4882a593Smuzhiyun 	unsigned char *ptr = dev->in_req->buf;
571*4882a593Smuzhiyun 	int status;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	memset(ptr, 0, len);
574*4882a593Smuzhiyun 	memcpy(ptr, data, len);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	dev->in_req->length = len;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
579*4882a593Smuzhiyun 	      dev->in_req->length, sizeof(data));
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
582*4882a593Smuzhiyun 	if (status) {
583*4882a593Smuzhiyun 		pr_err("kill %s:  resubmit %d bytes --> %d",
584*4882a593Smuzhiyun 		      dev->in_ep->name, dev->in_req->length, status);
585*4882a593Smuzhiyun 		usb_ep_set_halt(dev->in_ep);
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	/* Wait until tx interrupt received */
589*4882a593Smuzhiyun 	while (!dev->txdata)
590*4882a593Smuzhiyun 		usb_gadget_handle_interrupts(0);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	dev->txdata = 0;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun 
thor_rx_tx_complete(struct usb_ep * ep,struct usb_request * req)595*4882a593Smuzhiyun static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
598*4882a593Smuzhiyun 	int status = req->status;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
601*4882a593Smuzhiyun 	switch (status) {
602*4882a593Smuzhiyun 	case 0:
603*4882a593Smuzhiyun 		if (ep == dev->out_ep)
604*4882a593Smuzhiyun 			dev->rxdata = 1;
605*4882a593Smuzhiyun 		else
606*4882a593Smuzhiyun 			dev->txdata = 1;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 		break;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	/* this endpoint is normally active while we're configured */
611*4882a593Smuzhiyun 	case -ECONNABORTED:		/* hardware forced ep reset */
612*4882a593Smuzhiyun 	case -ECONNRESET:		/* request dequeued */
613*4882a593Smuzhiyun 	case -ESHUTDOWN:		/* disconnect from host */
614*4882a593Smuzhiyun 	case -EREMOTEIO:                /* short read */
615*4882a593Smuzhiyun 	case -EOVERFLOW:
616*4882a593Smuzhiyun 		pr_err("ERROR:%d", status);
617*4882a593Smuzhiyun 		break;
618*4882a593Smuzhiyun 	}
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	debug("%s complete --> %d, %d/%d\n", ep->name,
621*4882a593Smuzhiyun 	      status, req->actual, req->length);
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun 
thor_setup_complete(struct usb_ep * ep,struct usb_request * req)624*4882a593Smuzhiyun static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	if (req->status || req->actual != req->length)
627*4882a593Smuzhiyun 		debug("setup complete --> %d, %d/%d\n",
628*4882a593Smuzhiyun 		      req->status, req->actual, req->length);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun static int
thor_func_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)632*4882a593Smuzhiyun thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
635*4882a593Smuzhiyun 	struct usb_request *req = dev->req;
636*4882a593Smuzhiyun 	struct usb_gadget *gadget = dev->gadget;
637*4882a593Smuzhiyun 	int value = 0;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	u16 len = le16_to_cpu(ctrl->wLength);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
642*4882a593Smuzhiyun 	      ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
643*4882a593Smuzhiyun 	      ctrl->wLength);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	switch (ctrl->bRequest) {
646*4882a593Smuzhiyun 	case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
647*4882a593Smuzhiyun 		value = 0;
648*4882a593Smuzhiyun 		break;
649*4882a593Smuzhiyun 	case USB_CDC_REQ_SET_LINE_CODING:
650*4882a593Smuzhiyun 		value = len;
651*4882a593Smuzhiyun 		/* Line Coding set done = configuration done */
652*4882a593Smuzhiyun 		thor_func->dev->configuration_done = 1;
653*4882a593Smuzhiyun 		break;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	default:
656*4882a593Smuzhiyun 		pr_err("thor_setup: unknown request: %d", ctrl->bRequest);
657*4882a593Smuzhiyun 	}
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	if (value >= 0) {
660*4882a593Smuzhiyun 		req->length = value;
661*4882a593Smuzhiyun 		req->zero = value < len;
662*4882a593Smuzhiyun 		value = usb_ep_queue(gadget->ep0, req, 0);
663*4882a593Smuzhiyun 		if (value < 0) {
664*4882a593Smuzhiyun 			debug("%s: ep_queue: %d\n", __func__, value);
665*4882a593Smuzhiyun 			req->status = 0;
666*4882a593Smuzhiyun 		}
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	return value;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun /* Specific to the THOR protocol */
thor_set_dma(void * addr,int len)673*4882a593Smuzhiyun static void thor_set_dma(void *addr, int len)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
678*4882a593Smuzhiyun 	debug("addr:%p, len:%d\n", addr, len);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	dev->out_req->buf = addr;
681*4882a593Smuzhiyun 	dev->out_req->length = len;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
thor_init(void)684*4882a593Smuzhiyun int thor_init(void)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	/* Wait for a device enumeration and configuration settings */
689*4882a593Smuzhiyun 	debug("THOR enumeration/configuration setting....\n");
690*4882a593Smuzhiyun 	while (!dev->configuration_done)
691*4882a593Smuzhiyun 		usb_gadget_handle_interrupts(0);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	thor_set_dma(thor_rx_data_buf, strlen("THOR"));
694*4882a593Smuzhiyun 	/* detect the download request from Host PC */
695*4882a593Smuzhiyun 	if (thor_rx_data() < 0) {
696*4882a593Smuzhiyun 		printf("%s: Data not received!\n", __func__);
697*4882a593Smuzhiyun 		return -1;
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
701*4882a593Smuzhiyun 		puts("Download request from the Host PC\n");
702*4882a593Smuzhiyun 		udelay(30 * 1000); /* 30 ms */
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 		strcpy((char *)thor_tx_data_buf, "ROHT");
705*4882a593Smuzhiyun 		thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
706*4882a593Smuzhiyun 	} else {
707*4882a593Smuzhiyun 		puts("Wrong reply information\n");
708*4882a593Smuzhiyun 		return -1;
709*4882a593Smuzhiyun 	}
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	return 0;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
thor_handle(void)714*4882a593Smuzhiyun int thor_handle(void)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	int ret;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	/* receive the data from Host PC */
719*4882a593Smuzhiyun 	while (1) {
720*4882a593Smuzhiyun 		thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
721*4882a593Smuzhiyun 		ret = thor_rx_data();
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 		if (ret > 0) {
724*4882a593Smuzhiyun 			ret = process_data();
725*4882a593Smuzhiyun #ifdef CONFIG_THOR_RESET_OFF
726*4882a593Smuzhiyun 			if (ret == RESET_DONE)
727*4882a593Smuzhiyun 				break;
728*4882a593Smuzhiyun #endif
729*4882a593Smuzhiyun 			if (ret < 0)
730*4882a593Smuzhiyun 				return ret;
731*4882a593Smuzhiyun 		} else {
732*4882a593Smuzhiyun 			printf("%s: No data received!\n", __func__);
733*4882a593Smuzhiyun 			break;
734*4882a593Smuzhiyun 		}
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	return 0;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
free_ep_req(struct usb_ep * ep,struct usb_request * req)740*4882a593Smuzhiyun static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	if (req->buf)
743*4882a593Smuzhiyun 		free(req->buf);
744*4882a593Smuzhiyun 	usb_ep_free_request(ep, req);
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
thor_func_bind(struct usb_configuration * c,struct usb_function * f)747*4882a593Smuzhiyun static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	struct usb_gadget *gadget = c->cdev->gadget;
750*4882a593Smuzhiyun 	struct f_thor *f_thor = func_to_thor(f);
751*4882a593Smuzhiyun 	struct thor_dev *dev;
752*4882a593Smuzhiyun 	struct usb_ep *ep;
753*4882a593Smuzhiyun 	int status;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	thor_func = f_thor;
756*4882a593Smuzhiyun 	dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
757*4882a593Smuzhiyun 	if (!dev)
758*4882a593Smuzhiyun 		return -ENOMEM;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	memset(dev, 0, sizeof(*dev));
761*4882a593Smuzhiyun 	dev->gadget = gadget;
762*4882a593Smuzhiyun 	f_thor->dev = dev;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
765*4882a593Smuzhiyun 	      __func__, c, f);
766*4882a593Smuzhiyun 	debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	/* EP0  */
769*4882a593Smuzhiyun 	/* preallocate control response and buffer */
770*4882a593Smuzhiyun 	dev->req = usb_ep_alloc_request(gadget->ep0, 0);
771*4882a593Smuzhiyun 	if (!dev->req) {
772*4882a593Smuzhiyun 		status = -ENOMEM;
773*4882a593Smuzhiyun 		goto fail;
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun 	dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
776*4882a593Smuzhiyun 				 THOR_PACKET_SIZE);
777*4882a593Smuzhiyun 	if (!dev->req->buf) {
778*4882a593Smuzhiyun 		status = -ENOMEM;
779*4882a593Smuzhiyun 		goto fail;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	dev->req->complete = thor_setup_complete;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	/* DYNAMIC interface numbers assignments */
785*4882a593Smuzhiyun 	status = usb_interface_id(c, f);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	if (status < 0)
788*4882a593Smuzhiyun 		goto fail;
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	thor_downloader_intf_int.bInterfaceNumber = status;
791*4882a593Smuzhiyun 	thor_downloader_cdc_union.bMasterInterface0 = status;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	status = usb_interface_id(c, f);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	if (status < 0)
796*4882a593Smuzhiyun 		goto fail;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	thor_downloader_intf_data.bInterfaceNumber = status;
799*4882a593Smuzhiyun 	thor_downloader_cdc_union.bSlaveInterface0 = status;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	/* allocate instance-specific endpoints */
802*4882a593Smuzhiyun 	ep = usb_ep_autoconfig(gadget, &fs_in_desc);
803*4882a593Smuzhiyun 	if (!ep) {
804*4882a593Smuzhiyun 		status = -ENODEV;
805*4882a593Smuzhiyun 		goto fail;
806*4882a593Smuzhiyun 	}
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	if (gadget_is_dualspeed(gadget)) {
809*4882a593Smuzhiyun 		hs_in_desc.bEndpointAddress =
810*4882a593Smuzhiyun 				fs_in_desc.bEndpointAddress;
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	dev->in_ep = ep; /* Store IN EP for enabling @ setup */
814*4882a593Smuzhiyun 	ep->driver_data = dev;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	ep = usb_ep_autoconfig(gadget, &fs_out_desc);
817*4882a593Smuzhiyun 	if (!ep) {
818*4882a593Smuzhiyun 		status = -ENODEV;
819*4882a593Smuzhiyun 		goto fail;
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (gadget_is_dualspeed(gadget))
823*4882a593Smuzhiyun 		hs_out_desc.bEndpointAddress =
824*4882a593Smuzhiyun 				fs_out_desc.bEndpointAddress;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
827*4882a593Smuzhiyun 	ep->driver_data = dev;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	ep = usb_ep_autoconfig(gadget, &fs_int_desc);
830*4882a593Smuzhiyun 	if (!ep) {
831*4882a593Smuzhiyun 		status = -ENODEV;
832*4882a593Smuzhiyun 		goto fail;
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	dev->int_ep = ep;
836*4882a593Smuzhiyun 	ep->driver_data = dev;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	if (gadget_is_dualspeed(gadget)) {
839*4882a593Smuzhiyun 		hs_int_desc.bEndpointAddress =
840*4882a593Smuzhiyun 				fs_int_desc.bEndpointAddress;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 		f->hs_descriptors = (struct usb_descriptor_header **)
843*4882a593Smuzhiyun 			&hs_thor_downloader_function;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 		if (!f->hs_descriptors)
846*4882a593Smuzhiyun 			goto fail;
847*4882a593Smuzhiyun 	}
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	debug("%s: out_ep:%p out_req:%p\n", __func__,
850*4882a593Smuzhiyun 	      dev->out_ep, dev->out_req);
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	return 0;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun  fail:
855*4882a593Smuzhiyun 	if (dev->req)
856*4882a593Smuzhiyun 		free_ep_req(gadget->ep0, dev->req);
857*4882a593Smuzhiyun 	free(dev);
858*4882a593Smuzhiyun 	return status;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
thor_unbind(struct usb_configuration * c,struct usb_function * f)861*4882a593Smuzhiyun static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun 	struct f_thor *f_thor = func_to_thor(f);
864*4882a593Smuzhiyun 	struct thor_dev *dev = f_thor->dev;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	free_ep_req(dev->gadget->ep0, dev->req);
867*4882a593Smuzhiyun 	free(dev);
868*4882a593Smuzhiyun 	memset(thor_func, 0, sizeof(*thor_func));
869*4882a593Smuzhiyun 	thor_func = NULL;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
thor_func_disable(struct usb_function * f)872*4882a593Smuzhiyun static void thor_func_disable(struct usb_function *f)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	struct f_thor *f_thor = func_to_thor(f);
875*4882a593Smuzhiyun 	struct thor_dev *dev = f_thor->dev;
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	debug("%s:\n", __func__);
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	/* Avoid freeing memory when ep is still claimed */
880*4882a593Smuzhiyun 	if (dev->in_ep->driver_data) {
881*4882a593Smuzhiyun 		usb_ep_disable(dev->in_ep);
882*4882a593Smuzhiyun 		free_ep_req(dev->in_ep, dev->in_req);
883*4882a593Smuzhiyun 		dev->in_ep->driver_data = NULL;
884*4882a593Smuzhiyun 	}
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	if (dev->out_ep->driver_data) {
887*4882a593Smuzhiyun 		usb_ep_disable(dev->out_ep);
888*4882a593Smuzhiyun 		usb_ep_free_request(dev->out_ep, dev->out_req);
889*4882a593Smuzhiyun 		dev->out_ep->driver_data = NULL;
890*4882a593Smuzhiyun 	}
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	if (dev->int_ep->driver_data) {
893*4882a593Smuzhiyun 		usb_ep_disable(dev->int_ep);
894*4882a593Smuzhiyun 		dev->int_ep->driver_data = NULL;
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun 
thor_eps_setup(struct usb_function * f)898*4882a593Smuzhiyun static int thor_eps_setup(struct usb_function *f)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	struct usb_composite_dev *cdev = f->config->cdev;
901*4882a593Smuzhiyun 	struct usb_gadget *gadget = cdev->gadget;
902*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
903*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *d;
904*4882a593Smuzhiyun 	struct usb_request *req;
905*4882a593Smuzhiyun 	struct usb_ep *ep;
906*4882a593Smuzhiyun 	int result;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	ep = dev->in_ep;
909*4882a593Smuzhiyun 	d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
910*4882a593Smuzhiyun 	debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	result = usb_ep_enable(ep, d);
913*4882a593Smuzhiyun 	if (result)
914*4882a593Smuzhiyun 		goto err;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	ep->driver_data = cdev; /* claim */
917*4882a593Smuzhiyun 	req = alloc_ep_req(ep, THOR_PACKET_SIZE);
918*4882a593Smuzhiyun 	if (!req) {
919*4882a593Smuzhiyun 		result = -EIO;
920*4882a593Smuzhiyun 		goto err_disable_in_ep;
921*4882a593Smuzhiyun 	}
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	memset(req->buf, 0, req->length);
924*4882a593Smuzhiyun 	req->complete = thor_rx_tx_complete;
925*4882a593Smuzhiyun 	dev->in_req = req;
926*4882a593Smuzhiyun 	ep = dev->out_ep;
927*4882a593Smuzhiyun 	d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
928*4882a593Smuzhiyun 	debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	result = usb_ep_enable(ep, d);
931*4882a593Smuzhiyun 	if (result)
932*4882a593Smuzhiyun 		goto err_free_in_req;
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	ep->driver_data = cdev; /* claim */
935*4882a593Smuzhiyun 	req = usb_ep_alloc_request(ep, 0);
936*4882a593Smuzhiyun 	if (!req) {
937*4882a593Smuzhiyun 		result = -EIO;
938*4882a593Smuzhiyun 		goto err_disable_out_ep;
939*4882a593Smuzhiyun 	}
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	req->complete = thor_rx_tx_complete;
942*4882a593Smuzhiyun 	dev->out_req = req;
943*4882a593Smuzhiyun 	/* ACM control EP */
944*4882a593Smuzhiyun 	ep = dev->int_ep;
945*4882a593Smuzhiyun 	d = ep_desc(gadget, &hs_int_desc, &fs_int_desc);
946*4882a593Smuzhiyun 	debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	result = usb_ep_enable(ep, d);
949*4882a593Smuzhiyun 	if (result)
950*4882a593Smuzhiyun 		goto err;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	ep->driver_data = cdev;	/* claim */
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	return 0;
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun  err_disable_out_ep:
957*4882a593Smuzhiyun 	usb_ep_disable(dev->out_ep);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun  err_free_in_req:
960*4882a593Smuzhiyun 	free_ep_req(dev->in_ep, dev->in_req);
961*4882a593Smuzhiyun 	dev->in_req = NULL;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun  err_disable_in_ep:
964*4882a593Smuzhiyun 	usb_ep_disable(dev->in_ep);
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun  err:
967*4882a593Smuzhiyun 	return result;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
thor_func_set_alt(struct usb_function * f,unsigned intf,unsigned alt)970*4882a593Smuzhiyun static int thor_func_set_alt(struct usb_function *f,
971*4882a593Smuzhiyun 			     unsigned intf, unsigned alt)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun 	struct thor_dev *dev = thor_func->dev;
974*4882a593Smuzhiyun 	int result;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	debug("%s: func: %s intf: %d alt: %d\n",
977*4882a593Smuzhiyun 	      __func__, f->name, intf, alt);
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	switch (intf) {
980*4882a593Smuzhiyun 	case 0:
981*4882a593Smuzhiyun 		debug("ACM INTR interface\n");
982*4882a593Smuzhiyun 		break;
983*4882a593Smuzhiyun 	case 1:
984*4882a593Smuzhiyun 		debug("Communication Data interface\n");
985*4882a593Smuzhiyun 		result = thor_eps_setup(f);
986*4882a593Smuzhiyun 		if (result)
987*4882a593Smuzhiyun 			pr_err("%s: EPs setup failed!", __func__);
988*4882a593Smuzhiyun 		dev->configuration_done = 1;
989*4882a593Smuzhiyun 		break;
990*4882a593Smuzhiyun 	}
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	return 0;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun 
thor_func_init(struct usb_configuration * c)995*4882a593Smuzhiyun static int thor_func_init(struct usb_configuration *c)
996*4882a593Smuzhiyun {
997*4882a593Smuzhiyun 	struct f_thor *f_thor;
998*4882a593Smuzhiyun 	int status;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
1003*4882a593Smuzhiyun 	if (!f_thor)
1004*4882a593Smuzhiyun 		return -ENOMEM;
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	memset(f_thor, 0, sizeof(*f_thor));
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	f_thor->usb_function.name = "f_thor";
1009*4882a593Smuzhiyun 	f_thor->usb_function.bind = thor_func_bind;
1010*4882a593Smuzhiyun 	f_thor->usb_function.unbind = thor_unbind;
1011*4882a593Smuzhiyun 	f_thor->usb_function.setup = thor_func_setup;
1012*4882a593Smuzhiyun 	f_thor->usb_function.set_alt = thor_func_set_alt;
1013*4882a593Smuzhiyun 	f_thor->usb_function.disable = thor_func_disable;
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	status = usb_add_function(c, &f_thor->usb_function);
1016*4882a593Smuzhiyun 	if (status)
1017*4882a593Smuzhiyun 		free(f_thor);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	return status;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
thor_add(struct usb_configuration * c)1022*4882a593Smuzhiyun int thor_add(struct usb_configuration *c)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun 	debug("%s:\n", __func__);
1025*4882a593Smuzhiyun 	return thor_func_init(c);
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);
1029