From c98577de4fcf3bfb410c10a05b6633fb6581afdc Mon Sep 17 00:00:00 2001
From: Gary Bisson <gary.bisson@boundarydevices.com>
Date: Wed, 27 Dec 2017 10:55:52 +0100
Subject: [PATCH] hciattach_rome: simplify baudrate setting

Signed-off-by: Gary Bisson <gary.bisson@boundarydevices.com>
---
 tools/hciattach_rome.c | 70 +++++++++++++++++++-------------------------------
 tools/hciattach_rome.h | 21 ---------------
 2 files changed, 26 insertions(+), 65 deletions(-)

diff --git a/tools/hciattach_rome.c b/tools/hciattach_rome.c
index 2586dbc77..34e7edfb5 100644
--- a/tools/hciattach_rome.c
+++ b/tools/hciattach_rome.c
@@ -908,60 +908,49 @@ int read_bd_address(unsigned char *bdaddr)
 	return 0;
 }
 
-int isSpeedValid(int speed, int *local_baud_rate, int *controller_baud_rate)
+int isSpeedValid(int speed, unsigned char *baud_rate)
 {
 	switch(speed) {
 	case 9600:
-		*local_baud_rate = USERIAL_BAUD_9600;
-		*controller_baud_rate = BAUDRATE_9600;
+		*baud_rate = BAUDRATE_9600;
 		break;
 	case 19200:
-		*local_baud_rate = USERIAL_BAUD_19200;
-		*controller_baud_rate = BAUDRATE_19200;
+		*baud_rate = BAUDRATE_19200;
 		break;
 	case 57600:
-		*local_baud_rate = USERIAL_BAUD_57600;
-		*controller_baud_rate = BAUDRATE_57600;
+		*baud_rate = BAUDRATE_57600;
 		break;
 	case 115200:
-		*local_baud_rate = USERIAL_BAUD_115200;
-		*controller_baud_rate = BAUDRATE_115200;
+		*baud_rate = BAUDRATE_115200;
 		break;
 	case 230400:
-		*local_baud_rate = USERIAL_BAUD_230400;
-		*controller_baud_rate = BAUDRATE_230400;
+		*baud_rate = BAUDRATE_230400;
 		break;
 	case 460800:
-		*local_baud_rate = USERIAL_BAUD_460800;
-		*controller_baud_rate = BAUDRATE_460800;
+		*baud_rate = BAUDRATE_460800;
 		break;
 	case 921600:
-		*local_baud_rate = USERIAL_BAUD_921600;
-		*controller_baud_rate = BAUDRATE_921600;
+		*baud_rate = BAUDRATE_921600;
 		break;
 	case 1000000:
-		*local_baud_rate = USERIAL_BAUD_1M;
-		*controller_baud_rate = BAUDRATE_1000000;
+		*baud_rate = BAUDRATE_1000000;
 		break;
 	case 2000000:
-		*local_baud_rate = USERIAL_BAUD_2M;
-		*controller_baud_rate = BAUDRATE_2000000;
+		*baud_rate = BAUDRATE_2000000;
 		break;
 	case 3000000:
-		*local_baud_rate = USERIAL_BAUD_3M;
-		*controller_baud_rate = BAUDRATE_3000000;
+		*baud_rate = BAUDRATE_3000000;
 		break;
 	case 4000000:
-		*local_baud_rate = USERIAL_BAUD_4M;
-		*controller_baud_rate = BAUDRATE_4000000;
+		*baud_rate = BAUDRATE_4000000;
 		break;
 	case 300:
 	case 600:
 	case 1200:
 	case 2400:
 	default:
-		fprintf(stderr, "Invalid baud rate passed!\n");
-		*local_baud_rate = *controller_baud_rate = -1;
+		fprintf(stderr, "Invalid baudrate, default to 115200!\n");
+		*baud_rate = BAUDRATE_115200;
 		break;
 	}
 	return -1;
@@ -1633,7 +1622,7 @@ static void flow_control(int fd, int opt)
 }
 
 
-int rome_set_baudrate_req(int fd, int local_baud_rate, int controller_baud_rate)
+int rome_set_baudrate_req(int fd, unsigned char baud_rate)
 {
 	int size, err = 0;
 	unsigned char cmd[HCI_MAX_CMD_SIZE];
@@ -1647,7 +1636,7 @@ int rome_set_baudrate_req(int fd, int local_baud_rate, int controller_baud_rate)
 	cmd[0]  = HCI_COMMAND_PKT;
 	cmd_hdr->opcode = cmd_opcode_pack(HCI_VENDOR_CMD_OGF, EDL_SET_BAUDRATE_CMD_OCF);
 	cmd_hdr->plen     = VSC_SET_BAUDRATE_REQ_LEN;
-	cmd[4]  = controller_baud_rate;
+	cmd[4]  = baud_rate;
 
 	/* Total length of the packet to be sent to the Controller */
 	size = (HCI_CMD_IND + HCI_COMMAND_HDR_SIZE + VSC_SET_BAUDRATE_REQ_LEN);
@@ -1662,7 +1651,7 @@ int rome_set_baudrate_req(int fd, int local_baud_rate, int controller_baud_rate)
 		goto error;
 	}
 	/* Change Local UART baudrate to high speed UART */
-	userial_vendor_set_baud(local_baud_rate);
+	userial_vendor_set_baud(baud_rate);
 
 	/* Flow on after changing local uart baudrate */
 	flow_control(fd, MSM_ENABLE_FLOW_CTRL);
@@ -1735,7 +1724,8 @@ int qca_soc_init(int fd, int speed, char *bdaddr)
 {
 	int err = -1;
 	int ret = 0;
-	int size, local_baud_rate = 0, controller_baud_rate = 0;
+	int size;
+	unsigned char baud_rate = 0;
 
 	vnd_userial.fd = fd;
 
@@ -1787,7 +1777,7 @@ int qca_soc_init(int fd, int speed, char *bdaddr)
 		}
 
 		/* Change baud rate 115.2 kbps to 3Mbps*/
-		err = rome_hci_reset_req(fd, local_baud_rate);
+		err = rome_hci_reset_req(fd, baud_rate);
 		if (err < 0) {
 			fprintf(stderr, "HCI Reset Failed !!\n");
 			ret = -1;
@@ -1822,18 +1812,10 @@ int qca_soc_init(int fd, int speed, char *bdaddr)
 		rampatch_file_path = TF_RAMPATCH_TLV_1_0_1_PATH;
 		nvm_file_path = TF_NVM_TLV_1_0_1_PATH;
 download:
-		/* Check if user requested for 115200 kbps */
-		if (speed == 115200) {
-			local_baud_rate = USERIAL_BAUD_115200;
-			controller_baud_rate = BAUDRATE_115200;
-		}
-		else {
-			/* Change only if baud rate requested is valid or not */
-			isSpeedValid(speed, &local_baud_rate, &controller_baud_rate);
-			if (local_baud_rate < 0 || controller_baud_rate < 0) {
-				ret = -1;
-				goto error;
-			}
+		isSpeedValid(speed, &baud_rate);
+		if (baud_rate < 0) {
+			ret = -1;
+			goto error;
 		}
 
 		/* Donwload TLV files (rampatch, NVM) */
@@ -1851,7 +1833,7 @@ int qca_soc_init(int fd, int speed, char *bdaddr)
 		 * Overriding the baud rate value in NVM file with the user
 		 * requested baud rate, since default baud rate in NVM file is 3M.
 		 */
-		err = rome_set_baudrate_req(fd, local_baud_rate, controller_baud_rate);
+		err = rome_set_baudrate_req(fd, baud_rate);
 		if (err < 0) {
 			fprintf(stderr, "%s: Baud rate change failed!\n", __FUNCTION__);
 			ret = -1;
@@ -1859,7 +1841,7 @@ int qca_soc_init(int fd, int speed, char *bdaddr)
 		}
 
 		/* Perform HCI reset here*/
-		err = rome_hci_reset_req(fd, local_baud_rate);
+		err = rome_hci_reset_req(fd, baud_rate);
 		if (err < 0) {
 			fprintf(stderr, "HCI Reset Failed !!!\n");
 			ret = -1;
diff --git a/tools/hciattach_rome.h b/tools/hciattach_rome.h
index a356cc24f..2f5930c99 100644
--- a/tools/hciattach_rome.h
+++ b/tools/hciattach_rome.h
@@ -89,27 +89,6 @@ typedef struct
 #define USERIAL_BAUD_4M         15
 #define USERIAL_BAUD_AUTO       16
 
-/* Vendor specific baud rate values */
-#define UART_Baud_Rate_Baud_9600        4
-#define UART_Baud_Rate_Baud_19200       3
-#define UART_Baud_Rate_Baud_57600       1
-#define UART_Baud_Rate_Baud_115200      0
-#define UART_Baud_Rate_Baud_230400      5
-#define UART_Baud_Rate_Baud_460800      7
-#define UART_Baud_Rate_Baud_921600      10
-#define UART_Baud_Rate_Baud_1000000     11
-#define UART_Baud_Rate_Baud_2000000     13
-#define UART_Baud_Rate_Baud_3000000     14
-#define UART_Baud_Rate_Baud_4000000     15
-
-#define UART_Baud_Rate_Baud_250000      6
-#define UART_Baud_Rate_Baud_500000      8
-#define UART_Baud_Rate_Baud_720000      9
-#define UART_Baud_Rate_Baud_125000      12
-#define UART_Baud_Rate_Baud_1600000     16
-#define UART_Baud_Rate_Baud_3200000     17
-#define UART_Baud_Rate_Baud_3500000     18
-
 #ifndef FALSE
 #define FALSE  0
 #endif
