// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
/*
 * Copyright (c) 2022 MediaTek Inc.
 */

#include "mdrv_adapter_dbg.h"

#ifdef _DEBUG_CI2_
struct file *mdrv_adapter_file_open(const char *filename, int flags, mode_t mode)
{
	struct file *file;

	mm_segment_t old_fs = get_fs();
	set_fs(KERNEL_DS);
	file = filp_open(filename, flags, (umode_t)mode);
	if(IS_ERR_OR_NULL(file))
		file = NULL;
	set_fs(old_fs);
	return file;
}

int mdrv_adapter_file_close(struct file *file)
{
	int ret;

	mm_segment_t old_fs = get_fs();
	set_fs(KERNEL_DS);
	ret = filp_close(file, NULL);
	set_fs(old_fs);
	return ret;
}

int mdrv_adapter_file_write(struct file *file, char *buf, size_t size)
{
	loff_t pos;

	mm_segment_t old_fs = get_fs();
	set_fs(KERNEL_DS);
	pos = vfs_llseek(file, 0, SEEK_END);
	set_fs(old_fs);
	return kernel_write(file, buf, size, &pos);
}
#else
struct file *mdrv_adapter_file_open(const char *filename, int flags, mode_t mode) {return NULL;}
int mdrv_adapter_file_close(struct file *file) {return 0;}
int mdrv_adapter_file_write(struct file *file, char *buf, size_t size) {return 0;}
#endif
