devfs.c
8.09 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-02-11 Bernard Ignore O_CREAT flag in open.
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#include "devfs.h"
struct device_dirent
{
rt_device_t *devices;
rt_uint16_t read_index;
rt_uint16_t device_count;
};
int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
return RT_EOK;
}
int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
{
rt_err_t result;
rt_device_t dev_id;
RT_ASSERT(file != RT_NULL);
/* get device handler */
dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL);
/* close device handler */
result = rt_device_control(dev_id, cmd, args);
if (result == RT_EOK)
return RT_EOK;
return result;
}
int dfs_device_fs_read(struct dfs_fd *file, void *buf, size_t count)
{
int result;
rt_device_t dev_id;
RT_ASSERT(file != RT_NULL);
/* get device handler */
dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL);
/* read device data */
result = rt_device_read(dev_id, file->pos, buf, count);
file->pos += result;
return result;
}
int dfs_device_fs_write(struct dfs_fd *file, const void *buf, size_t count)
{
int result;
rt_device_t dev_id;
RT_ASSERT(file != RT_NULL);
/* get device handler */
dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL);
/* read device data */
result = rt_device_write(dev_id, file->pos, buf, count);
file->pos += result;
return result;
}
int dfs_device_fs_close(struct dfs_fd *file)
{
rt_err_t result;
rt_device_t dev_id;
RT_ASSERT(file != RT_NULL);
if (file->type == FT_DIRECTORY)
{
struct device_dirent *root_dirent;
root_dirent = (struct device_dirent *)file->data;
RT_ASSERT(root_dirent != RT_NULL);
/* release dirent */
rt_free(root_dirent);
return RT_EOK;
}
/* get device handler */
dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL);
/* close device handler */
result = rt_device_close(dev_id);
if (result == RT_EOK)
{
file->data = RT_NULL;
return RT_EOK;
}
return -EIO;
}
int dfs_device_fs_open(struct dfs_fd *file)
{
rt_err_t result;
rt_device_t device;
/* open root directory */
if ((file->path[0] == '/') && (file->path[1] == '\0') &&
(file->flags & O_DIRECTORY))
{
struct rt_object *object;
struct rt_list_node *node;
struct rt_object_information *information;
struct device_dirent *root_dirent;
rt_uint32_t count = 0;
/* lock scheduler */
rt_enter_critical();
/* traverse device object */
information = rt_object_get_information(RT_Object_Class_Device);
RT_ASSERT(information != RT_NULL);
for (node = information->object_list.next; node != &(information->object_list); node = node->next)
{
count ++;
}
rt_exit_critical();
root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
count * sizeof(rt_device_t));
if (root_dirent != RT_NULL)
{
/* lock scheduler */
rt_enter_critical();
root_dirent->devices = (rt_device_t *)(root_dirent + 1);
root_dirent->read_index = 0;
root_dirent->device_count = count;
count = 0;
/* get all device node */
for (node = information->object_list.next; node != &(information->object_list); node = node->next)
{
/* avoid memory write through */
if (count == root_dirent->device_count)
{
rt_kprintf("warning: There are newly added devices that are not displayed!");
break;
}
object = rt_list_entry(node, struct rt_object, list);
root_dirent->devices[count] = (rt_device_t)object;
count ++;
}
rt_exit_critical();
}
/* set data */
file->data = root_dirent;
return RT_EOK;
}
device = rt_device_find(&file->path[1]);
if (device == RT_NULL)
return -ENODEV;
#ifdef RT_USING_POSIX_DEVIO
if (device->fops)
{
/* use device fops */
file->fops = device->fops;
file->data = (void *)device;
/* use fops */
if (file->fops->open)
{
result = file->fops->open(file);
if (result == RT_EOK || result == -RT_ENOSYS)
{
file->type = FT_DEVICE;
return 0;
}
}
}
else
#endif /* RT_USING_POSIX_DEVIO */
{
result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
if (result == RT_EOK || result == -RT_ENOSYS)
{
file->data = device;
file->type = FT_DEVICE;
return RT_EOK;
}
}
file->data = RT_NULL;
/* open device failed. */
return -EIO;
}
int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{
/* stat root directory */
if ((path[0] == '/') && (path[1] == '\0'))
{
st->st_dev = 0;
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP | S_IWOTH;
st->st_mode &= ~S_IFREG;
st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
st->st_size = 0;
st->st_mtime = 0;
return RT_EOK;
}
else
{
rt_device_t dev_id;
dev_id = rt_device_find(&path[1]);
if (dev_id != RT_NULL)
{
st->st_dev = 0;
st->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP | S_IWOTH;
if (dev_id->type == RT_Device_Class_Char)
st->st_mode |= S_IFCHR;
else if (dev_id->type == RT_Device_Class_Block)
st->st_mode |= S_IFBLK;
else if (dev_id->type == RT_Device_Class_Pipe)
st->st_mode |= S_IFIFO;
else
st->st_mode |= S_IFREG;
st->st_size = 0;
st->st_mtime = 0;
return RT_EOK;
}
}
return -ENOENT;
}
int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
{
rt_uint32_t index;
rt_object_t object;
struct dirent *d;
struct device_dirent *root_dirent;
root_dirent = (struct device_dirent *)file->data;
RT_ASSERT(root_dirent != RT_NULL);
/* make integer count */
count = (count / sizeof(struct dirent));
if (count == 0)
return -EINVAL;
for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
index ++)
{
object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
d = dirp + index;
d->d_type = DT_REG;
d->d_namlen = RT_NAME_MAX;
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
}
root_dirent->read_index += index;
return index * sizeof(struct dirent);
}
static int dfs_device_fs_poll(struct dfs_fd *fd, struct rt_pollreq *req)
{
int mask = 0;
return mask;
}
static const struct dfs_file_ops _device_fops =
{
dfs_device_fs_open,
dfs_device_fs_close,
dfs_device_fs_ioctl,
dfs_device_fs_read,
dfs_device_fs_write,
RT_NULL, /* flush */
RT_NULL, /* lseek */
dfs_device_fs_getdents,
dfs_device_fs_poll,
};
static const struct dfs_filesystem_ops _device_fs =
{
"devfs",
DFS_FS_FLAG_DEFAULT,
&_device_fops,
dfs_device_fs_mount,
RT_NULL, /*unmount*/
RT_NULL, /*mkfs*/
RT_NULL, /*statfs*/
RT_NULL, /*unlink*/
dfs_device_fs_stat,
RT_NULL, /*rename*/
};
int devfs_init(void)
{
/* register device file system */
dfs_register(&_device_fs);
return 0;
}