[][MAC80211][app][Fix atenl failed when using default bin eeprom]

[Description]
Fix atenl failed when using default bin eeprom

[Release-log]
N/A

Change-Id: I1fd4b161c7180e14a01fa804cf1272dd190ef050
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/6709323
diff --git a/feed/atenl/src/eeprom.c b/feed/atenl/src/eeprom.c
index 1c1cb6c..d18262e 100644
--- a/feed/atenl/src/eeprom.c
+++ b/feed/atenl/src/eeprom.c
@@ -9,60 +9,38 @@
 #define EEPROM_PART_SIZE 0x64000
 char *eeprom_file;
 
-static FILE *mtd_open(const char *mtd)
+static int
+atenl_create_file(struct atenl *an, bool flash_mode)
 {
-	char line[128], name[64];
-	FILE *fp;
-	int i;
-
-	fp = fopen("/proc/mtd", "r");
-	if (!fp)
-		return NULL;
-
-	snprintf(name, sizeof(name), "\"%s\"", mtd);
-	while (fgets(line, sizeof(line), fp)) {
-		if (!sscanf(line, "mtd%d:", &i) || !strstr(line, name))
-			continue;
+	char fname[64], buf[1024];
+	ssize_t w, len, max_len, total_len = 0;
+	int fd_ori, fd, ret;
 
-		snprintf(line, sizeof(line), "/dev/mtd%d", i);
-		fclose(fp);
-		return fopen(line, "r");
+	/* reserve space for pre-cal data in flash mode */
+	if (flash_mode) {
+		atenl_dbg("%s: init eeprom with flash mode\n", __func__);
+		max_len = EEPROM_PART_SIZE;
+	} else {
+		atenl_dbg("%s: init eeprom with efuse mode\n", __func__);
+		max_len = 0x1000;
 	}
-	fclose(fp);
-
-	return NULL;
-}
 
-static int
-atenl_flash_create_file(struct atenl *an)
-{
-#define READ_LEN_LIMIT	0x64000
-	char buf[1024];
-	ssize_t len, limit = 0;
-	FILE *f;
-	int fd, ret;
-
-	f = mtd_open(an->mtd_part);
-	if (!f) {
-		atenl_err("Failed to open MTD device\n");
+	snprintf(fname, sizeof(fname),
+		 "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom",
+		 get_band_val(an, 0, phy_idx));
+	fd_ori = open(fname, O_RDONLY);
+	if (fd_ori < 0)
 		return -1;
-	}
-	fseek(f, an->mtd_offset, SEEK_SET);
 
 	fd = open(eeprom_file, O_RDWR | O_CREAT | O_EXCL, 00644);
 	if (fd < 0)
 		goto out;
 
-	while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
-		ssize_t w;
-
+	while ((len = read(fd_ori, buf, sizeof(buf))) > 0) {
 retry:
 		w = write(fd, buf, len);
 		if (w > 0) {
-			limit += len;
-
-			if (limit >= READ_LEN_LIMIT)
-				break;
+			total_len += len;
 			continue;
 		}
 
@@ -76,53 +54,27 @@
 		goto out;
 	}
 
-	ret = lseek(fd, 0, SEEK_SET);
-	if (ret) {
-		fclose(f);
-		close(fd);
-		return ret;
-	}
-
-out:
-	fclose(f);
-	return fd;
-}
-
-static int
-atenl_efuse_create_file(struct atenl *an)
-{
-	char fname[64], buf[1024];
-	ssize_t len;
-	int fd_ori, fd, ret;
-
-	snprintf(fname, sizeof(fname),
-		"/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom", get_band_val(an, 0, phy_idx));
-	fd_ori = open(fname, O_RDONLY);
-	if (fd_ori < 0)
-		return -1;
-
-	fd = open(eeprom_file, O_RDWR | O_CREAT | O_EXCL, 00644);
-	if (fd < 0)
-		goto out;
-
-	while ((len = read(fd_ori, buf, sizeof(buf))) > 0) {
-		ssize_t w;
-
-retry:
+	/* reserve space for pre-cal data in flash mode */
+	len = sizeof(buf);
+	memset(buf, 0, len);
+	while (total_len < max_len) {
 		w = write(fd, buf, len);
-		if (w > 0)
-			continue;
 
-		if (errno == EINTR)
-			goto retry;
+		if (w > 0) {
+			total_len += len;
+			continue;
+		}
 
-		perror("write");
-		unlink(eeprom_file);
-		close(fd);
-		fd = -1;
-		goto out;
+		if (errno != EINTR) {
+			perror("write");
+			unlink(eeprom_file);
+			close(fd);
+			fd = -1;
+			goto out;
+		}
 	}
 
+
 	ret = lseek(fd, 0, SEEK_SET);
 	if (ret) {
 		close(fd_ori);
@@ -148,17 +100,8 @@
 {
 	int fd;
 
-	if (!atenl_eeprom_file_exists()) {
-		if (flash_mode)
-			atenl_dbg("%s: init eeprom with flash mode\n", __func__);
-		else
-			atenl_dbg("%s: init eeprom with efuse mode\n", __func__);
-
-		if (flash_mode)
-			return atenl_flash_create_file(an);
-
-		return atenl_efuse_create_file(an);
-	}
+	if (!atenl_eeprom_file_exists())
+		return atenl_create_file(an, flash_mode);
 
 	fd = open(eeprom_file, O_RDWR);
 	if (fd < 0)