-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs_file.c
124 lines (98 loc) · 2.42 KB
/
rs_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SPDX-License-Identifier: GPL-2.0-or-later
/**
* Copyright (C) [2022-2023] Renesas Electronics Corporation and/or its affiliates.
*/
////////////////////////////////////////////////////////////////////////////////
/// INCLUDE
#include <linux/version.h>
#include <linux/uaccess.h>
#include "rs_file.h"
////////////////////////////////////////////////////////////////////////////////
/// MACRO DEFITION
#ifndef CONFIG_SET_FS
#define F_FS_INIT()
#define F_FS_SET()
#define F_FS_RST()
#else
#define F_FS_INIT() static mm_segment_t fs = 0;
#define F_FS_SET() \
{ \
fs = get_fs(); \
set_fs(KERNEL_DS); \
}
#define F_FS_RST() set_fs(fs)
#endif
////////////////////////////////////////////////////////////////////////////////
/// TYPE DEFITION
////////////////////////////////////////////////////////////////////////////////
/// LOCAL VARIABLE
////////////////////////////////////////////////////////////////////////////////
/// LOCAL FUNCTION
////////////////////////////////////////////////////////////////////////////////
/// GLOBAL FUNCTION
s32 rs_f_open(struct rs_file *file, const u8 *file_path, s32 flags, s32 mode)
{
s32 ret = -1;
F_FS_INIT();
if (file != NULL) {
F_FS_SET();
file->file = filp_open((const char *)file_path, (int)flags, mode);
F_FS_RST();
if (file->file != NULL) {
ret = 0;
file->flags = flags;
file->mode = mode;
} else {
ret = -1;
}
}
return ret;
}
s32 rs_f_close(struct rs_file *file)
{
s32 ret = -1;
F_FS_INIT();
if ((file != NULL) && (file->file != NULL)) {
F_FS_SET();
ret = filp_close(file->file, NULL);
F_FS_RST();
if (ret == 0) {
file->file = NULL;
file->flags = -1;
file->mode = -1;
}
}
return ret;
}
s32 rs_f_read(struct rs_file *file, u8 *buf, s32 buf_len)
{
s32 ret = -1;
loff_t pos = 0;
F_FS_INIT();
if ((file != NULL) && (file->file != NULL) && (buf != NULL)) {
pos = file->pos;
F_FS_SET();
ret = kernel_read(file->file, (char __user *)buf, buf_len, &pos);
F_FS_RST();
if (ret >= 0) {
file->pos = (s32)pos;
}
}
return ret;
}
s32 rs_f_write(struct rs_file *file, const u8 *buf, s32 buf_len)
{
s32 ret = -1;
loff_t pos = 0;
F_FS_INIT();
if ((file != NULL) && (file->file != NULL) && (buf != NULL)) {
pos = file->pos;
F_FS_SET();
ret = kernel_write(file->file, (const char __user *)buf, buf_len, &pos);
F_FS_RST();
if (ret >= 0) {
file->pos = (s32)pos;
}
}
return ret;
}