thordown: Use plain udevice for UDC controller interaction

Convert to plain udevice interaction with UDC controller
device, avoid the use of UDC uclass dev_array .

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
diff --git a/drivers/usb/gadget/f_thor.c b/drivers/usb/gadget/f_thor.c
index 47ef55b..3caa4c3 100644
--- a/drivers/usb/gadget/f_thor.c
+++ b/drivers/usb/gadget/f_thor.c
@@ -15,9 +15,10 @@
  */
 
 #include <command.h>
-#include <errno.h>
 #include <common.h>
 #include <console.h>
+#include <dm.h>
+#include <errno.h>
 #include <init.h>
 #include <log.h>
 #include <malloc.h>
@@ -34,9 +35,9 @@
 
 #include "f_thor.h"
 
-static void thor_tx_data(unsigned char *data, int len);
+static void thor_tx_data(struct udevice *udc, unsigned char *data, int len);
 static void thor_set_dma(void *addr, int len);
-static int thor_rx_data(void);
+static int thor_rx_data(struct udevice *udc);
 
 static struct f_thor *thor_func;
 static inline struct f_thor *func_to_thor(struct usb_function *f)
@@ -56,15 +57,15 @@
 static unsigned long long int thor_file_size;
 static int alt_setting_num;
 
-static void send_rsp(const struct rsp_box *rsp)
+static void send_rsp(struct udevice *udc, const struct rsp_box *rsp)
 {
 	memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
-	thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
+	thor_tx_data(udc, thor_tx_data_buf, sizeof(struct rsp_box));
 
 	debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
 }
 
-static void send_data_rsp(s32 ack, s32 count)
+static void send_data_rsp(struct udevice *udc, s32 ack, s32 count)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
 				 sizeof(struct data_rsp_box));
@@ -73,12 +74,12 @@
 	rsp->count = count;
 
 	memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
-	thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
+	thor_tx_data(udc, thor_tx_data_buf, sizeof(struct data_rsp_box));
 
 	debug("-DATA RSP: %d, %d\n", ack, count);
 }
 
-static int process_rqt_info(const struct rqt_box *rqt)
+static int process_rqt_info(struct udevice *udc, const struct rqt_box *rqt)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
 	memset(rsp, 0, sizeof(struct rsp_box));
@@ -111,11 +112,11 @@
 		return -EINVAL;
 	}
 
-	send_rsp(rsp);
+	send_rsp(udc, rsp);
 	return true;
 }
 
-static int process_rqt_cmd(const struct rqt_box *rqt)
+static int process_rqt_cmd(struct udevice *udc, const struct rqt_box *rqt)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
 	memset(rsp, 0, sizeof(struct rsp_box));
@@ -126,7 +127,7 @@
 	switch (rqt->rqt_data) {
 	case RQT_CMD_REBOOT:
 		debug("TARGET RESET\n");
-		send_rsp(rsp);
+		send_rsp(udc, rsp);
 		g_dnl_unregister();
 		dfu_free_entities();
 #ifdef CONFIG_THOR_RESET_OFF
@@ -136,7 +137,7 @@
 		break;
 	case RQT_CMD_POWEROFF:
 	case RQT_CMD_EFSCLEAR:
-		send_rsp(rsp);
+		send_rsp(udc, rsp);
 	default:
 		printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
 		return -EINVAL;
@@ -145,7 +146,8 @@
 	return true;
 }
 
-static long long int download_head(unsigned long long total,
+static long long int download_head(struct udevice *udc,
+				   unsigned long long total,
 				   unsigned int packet_size,
 				   long long int *left,
 				   int *cnt)
@@ -166,7 +168,7 @@
 	while (total - rcv_cnt >= packet_size) {
 		thor_set_dma(buf, packet_size);
 		buf += packet_size;
-		ret_rcv = thor_rx_data();
+		ret_rcv = thor_rx_data(udc);
 		if (ret_rcv < 0)
 			return ret_rcv;
 		rcv_cnt += ret_rcv;
@@ -184,7 +186,7 @@
 			}
 			buf = transfer_buffer;
 		}
-		send_data_rsp(0, ++usb_pkt_cnt);
+		send_data_rsp(udc, 0, ++usb_pkt_cnt);
 	}
 
 	/* Calculate the amount of data to arrive from PC (in bytes) */
@@ -200,11 +202,11 @@
 
 	if (left_to_rcv) {
 		thor_set_dma(buf, packet_size);
-		ret_rcv = thor_rx_data();
+		ret_rcv = thor_rx_data(udc);
 		if (ret_rcv < 0)
 			return ret_rcv;
 		rcv_cnt += ret_rcv;
-		send_data_rsp(0, ++usb_pkt_cnt);
+		send_data_rsp(udc, 0, ++usb_pkt_cnt);
 	}
 
 	debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
@@ -254,7 +256,7 @@
 	return ret;
 }
 
-static long long int process_rqt_download(const struct rqt_box *rqt)
+static long long int process_rqt_download(struct udevice *udc, const struct rqt_box *rqt)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
 	static long long int left, ret_head;
@@ -301,8 +303,8 @@
 		}
 		break;
 	case RQT_DL_FILE_START:
-		send_rsp(rsp);
-		ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
+		send_rsp(udc, rsp);
+		ret_head = download_head(udc, thor_file_size, THOR_PACKET_SIZE,
 					 &left, &cnt);
 		if (ret_head < 0) {
 			left = 0;
@@ -324,11 +326,11 @@
 		ret = -ENOTSUPP;
 	}
 
-	send_rsp(rsp);
+	send_rsp(udc, rsp);
 	return ret;
 }
 
-static int process_data(void)
+static int process_data(struct udevice *udc)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
 	int ret = -EINVAL;
@@ -339,13 +341,13 @@
 
 	switch (rqt->rqt) {
 	case RQT_INFO:
-		ret = process_rqt_info(rqt);
+		ret = process_rqt_info(udc, rqt);
 		break;
 	case RQT_CMD:
-		ret = process_rqt_cmd(rqt);
+		ret = process_rqt_cmd(udc, rqt);
 		break;
 	case RQT_DL:
-		ret = (int) process_rqt_download(rqt);
+		ret = (int) process_rqt_download(udc, rqt);
 		break;
 	case RQT_UL:
 		puts("RQT: UPLOAD not supported!\n");
@@ -536,7 +538,7 @@
 	return req;
 }
 
-static int thor_rx_data(void)
+static int thor_rx_data(struct udevice *udc)
 {
 	struct thor_dev *dev = thor_func->dev;
 	int data_to_rx, tmp, status;
@@ -557,7 +559,7 @@
 		}
 
 		while (!dev->rxdata) {
-			usb_gadget_handle_interrupts(0);
+			dm_usb_gadget_handle_interrupts(udc);
 			if (ctrlc())
 				return -1;
 		}
@@ -568,7 +570,7 @@
 	return tmp;
 }
 
-static void thor_tx_data(unsigned char *data, int len)
+static void thor_tx_data(struct udevice *udc, unsigned char *data, int len)
 {
 	struct thor_dev *dev = thor_func->dev;
 	unsigned char *ptr = dev->in_req->buf;
@@ -591,7 +593,7 @@
 
 	/* Wait until tx interrupt received */
 	while (!dev->txdata)
-		usb_gadget_handle_interrupts(0);
+		dm_usb_gadget_handle_interrupts(udc);
 
 	dev->txdata = 0;
 }
@@ -685,18 +687,18 @@
 	dev->out_req->length = len;
 }
 
-int thor_init(void)
+int thor_init(struct udevice *udc)
 {
 	struct thor_dev *dev = thor_func->dev;
 
 	/* Wait for a device enumeration and configuration settings */
 	debug("THOR enumeration/configuration setting....\n");
 	while (!dev->configuration_done)
-		usb_gadget_handle_interrupts(0);
+		dm_usb_gadget_handle_interrupts(udc);
 
 	thor_set_dma(thor_rx_data_buf, strlen("THOR"));
 	/* detect the download request from Host PC */
-	if (thor_rx_data() < 0) {
+	if (thor_rx_data(udc) < 0) {
 		printf("%s: Data not received!\n", __func__);
 		return -1;
 	}
@@ -706,7 +708,7 @@
 		udelay(30 * 1000); /* 30 ms */
 
 		strcpy((char *)thor_tx_data_buf, "ROHT");
-		thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
+		thor_tx_data(udc, thor_tx_data_buf, strlen("ROHT"));
 	} else {
 		puts("Wrong reply information\n");
 		return -1;
@@ -715,17 +717,17 @@
 	return 0;
 }
 
-int thor_handle(void)
+int thor_handle(struct udevice *udc)
 {
 	int ret;
 
 	/* receive the data from Host PC */
 	while (1) {
 		thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
-		ret = thor_rx_data();
+		ret = thor_rx_data(udc);
 
 		if (ret > 0) {
-			ret = process_data();
+			ret = process_data(udc);
 #ifdef CONFIG_THOR_RESET_OFF
 			if (ret == RESET_DONE)
 				break;