diff mbox series

[RFC,v1,220/256] cl8k: add utils/file.c

Message ID 20210617160223.160998-221-viktor.barna@celeno.com
State New
Headers show
Series wireless: cl8k driver for Celeno IEEE 802.11ax devices | expand

Commit Message

Viktor Barna June 17, 2021, 4:01 p.m. UTC
From: Viktor Barna <viktor.barna@celeno.com>

(Part of the split. Please, take a look at the cover letter for more
details).

Signed-off-by: Viktor Barna <viktor.barna@celeno.com>
---
 drivers/net/wireless/celeno/cl8k/utils/file.c | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 drivers/net/wireless/celeno/cl8k/utils/file.c

--
2.30.0
diff mbox series

Patch

diff --git a/drivers/net/wireless/celeno/cl8k/utils/file.c b/drivers/net/wireless/celeno/cl8k/utils/file.c
new file mode 100644
index 000000000000..b42d78f386e4
--- /dev/null
+++ b/drivers/net/wireless/celeno/cl8k/utils/file.c
@@ -0,0 +1,52 @@ 
+// SPDX-License-Identifier: MIT
+/* Copyright(c) 2019-2021, Celeno Communications Ltd. */
+
+#include "utils/file.h"
+#include "reg/reg_access.h"
+#include <linux/firmware.h>
+#include <linux/fs.h>
+#include <linux/vmalloc.h>
+
+size_t cl_file_open_and_read(struct cl_chip *chip, const char *filename,
+                            char **buf)
+{
+       const struct firmware *fw;
+       size_t size = 0;
+       int ret = 0;
+       char path_name[CL_PATH_MAX] = {0};
+
+       snprintf(path_name, sizeof(path_name), "cl8k/%s", filename);
+       ret = request_firmware(&fw, path_name, chip->dev);
+
+       if (ret) {
+               cl_dbg_chip_err(chip, "request_firmware %s failed\n",
+                               path_name);
+               return 0;
+       }
+
+       if (!fw || !fw->data) {
+               cl_dbg_chip_err(chip, "Invalid firmware %s\n", path_name);
+               goto out;
+       }
+
+       size = fw->size;
+
+       /*
+        * Add one byte with a '\0' so that string manipulation functions
+        * used for parsing these files can find the string '\0' terminator.
+        * Make sure size is aligned to 4.
+        */
+       *buf = kzalloc(ALIGN(size + 1, 4), GFP_KERNEL);
+       if (!(*buf)) {
+               size = 0;
+               goto out;
+       }
+
+       memcpy(*buf, fw->data, size);
+
+out:
+       release_firmware(fw);
+
+       return size;
+}
+