devfs.c 10.0 KB
Newer Older
Y
yiyue.fang 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
Y
yiyue.fang 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
Y
yiyue.fang 已提交
5 6 7
 *
 * Change Logs:
 * Date           Author       Notes
B
BernardXiong 已提交
8
 * 2018-02-11     Bernard      Ignore O_CREAT flag in open.
Y
yiyue.fang 已提交
9
 */
G
guo 已提交
10
#include <rthw.h>
11
#include <rtthread.h>
B
bernard 已提交
12 13
#include <rtdevice.h>

14 15
#include <dfs.h>
#include <dfs_fs.h>
B
bernard 已提交
16
#include <dfs_file.h>
17 18 19 20 21

#include "devfs.h"

struct device_dirent
{
Y
yiyue.fang 已提交
22 23 24
    rt_device_t *devices;
    rt_uint16_t read_index;
    rt_uint16_t device_count;
25 26
};

27
int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
28
{
B
bernard 已提交
29
    return RT_EOK;
30 31
}

32 33 34 35 36 37 38 39 40 41
int dfs_device_fs_statfs(struct dfs_filesystem *fs, struct statfs *buf)
{
    buf->f_bsize  = 512;
    buf->f_blocks = 2048 * 64; // 64M
    buf->f_bfree  = buf->f_blocks;
    buf->f_bavail = buf->f_bfree;

    return RT_EOK;
}

42
int dfs_device_fs_ioctl(struct dfs_file *file, int cmd, void *args)
43
{
Y
yiyue.fang 已提交
44 45
    rt_err_t result;
    rt_device_t dev_id;
46

Y
yiyue.fang 已提交
47
    RT_ASSERT(file != RT_NULL);
48

Y
yiyue.fang 已提交
49
    /* get device handler */
G
guo 已提交
50
    dev_id = (rt_device_t)file->vnode->data;
Y
yiyue.fang 已提交
51
    RT_ASSERT(dev_id != RT_NULL);
52

Z
zhkag 已提交
53 54 55
    if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
        return -RT_ENOSYS;

Y
yiyue.fang 已提交
56 57 58
    /* close device handler */
    result = rt_device_control(dev_id, cmd, args);
    if (result == RT_EOK)
B
bernard 已提交
59
        return RT_EOK;
60

B
bernard 已提交
61
    return result;
62 63
}

64
int dfs_device_fs_read(struct dfs_file *file, void *buf, size_t count)
65
{
Y
yiyue.fang 已提交
66 67
    int result;
    rt_device_t dev_id;
68

Y
yiyue.fang 已提交
69
    RT_ASSERT(file != RT_NULL);
70

Y
yiyue.fang 已提交
71
    /* get device handler */
G
guo 已提交
72
    dev_id = (rt_device_t)file->vnode->data;
Y
yiyue.fang 已提交
73
    RT_ASSERT(dev_id != RT_NULL);
74

Z
zhkag 已提交
75 76 77
    if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
        return -RT_ENOSYS;

Y
yiyue.fang 已提交
78 79 80
    /* read device data */
    result = rt_device_read(dev_id, file->pos, buf, count);
    file->pos += result;
81

Y
yiyue.fang 已提交
82
    return result;
83 84
}

85
int dfs_device_fs_write(struct dfs_file *file, const void *buf, size_t count)
86
{
Y
yiyue.fang 已提交
87 88
    int result;
    rt_device_t dev_id;
89

Y
yiyue.fang 已提交
90
    RT_ASSERT(file != RT_NULL);
91

Y
yiyue.fang 已提交
92
    /* get device handler */
G
guo 已提交
93
    dev_id = (rt_device_t)file->vnode->data;
Y
yiyue.fang 已提交
94
    RT_ASSERT(dev_id != RT_NULL);
95

Z
zhkag 已提交
96 97 98
    if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
        return -RT_ENOSYS;

Y
yiyue.fang 已提交
99 100 101
    /* read device data */
    result = rt_device_write(dev_id, file->pos, buf, count);
    file->pos += result;
102

Y
yiyue.fang 已提交
103
    return result;
104 105
}

106
int dfs_device_fs_close(struct dfs_file *file)
107
{
Y
yiyue.fang 已提交
108 109
    rt_err_t result;
    rt_device_t dev_id;
110

Y
yiyue.fang 已提交
111
    RT_ASSERT(file != RT_NULL);
G
guo 已提交
112 113 114 115 116 117
    RT_ASSERT(file->vnode->ref_count > 0);

    if (file->vnode->ref_count > 1)
    {
        return 0;
    }
118

Z
zhkag 已提交
119
    if (file->vnode->type == FT_DIRECTORY && (file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
Y
yiyue.fang 已提交
120 121
    {
        struct device_dirent *root_dirent;
122

G
guo 已提交
123
        root_dirent = (struct device_dirent *)file->vnode->data;
Y
yiyue.fang 已提交
124
        RT_ASSERT(root_dirent != RT_NULL);
125

Y
yiyue.fang 已提交
126 127
        /* release dirent */
        rt_free(root_dirent);
B
bernard 已提交
128
        return RT_EOK;
Y
yiyue.fang 已提交
129
    }
130

Y
yiyue.fang 已提交
131
    /* get device handler */
G
guo 已提交
132
    dev_id = (rt_device_t)file->vnode->data;
Y
yiyue.fang 已提交
133
    RT_ASSERT(dev_id != RT_NULL);
134

Y
yiyue.fang 已提交
135 136 137 138
    /* close device handler */
    result = rt_device_close(dev_id);
    if (result == RT_EOK)
    {
G
guo 已提交
139
        file->vnode->data = RT_NULL;
140

B
bernard 已提交
141
        return RT_EOK;
Y
yiyue.fang 已提交
142
    }
143

B
bernard 已提交
144
    return -EIO;
145 146
}

147
int dfs_device_fs_open(struct dfs_file *file)
148
{
149
    rt_err_t result;
Y
yiyue.fang 已提交
150 151
    rt_device_t device;

G
guo 已提交
152 153 154 155 156 157
    RT_ASSERT(file->vnode->ref_count > 0);
    if (file->vnode->ref_count > 1)
    {
        file->pos = 0;
        return 0;
    }
Y
yiyue.fang 已提交
158
    /* open root directory */
G
guo 已提交
159
    if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0') &&
B
bernard 已提交
160
        (file->flags & O_DIRECTORY))
Y
yiyue.fang 已提交
161 162 163 164 165 166
    {
        struct rt_object *object;
        struct rt_list_node *node;
        struct rt_object_information *information;
        struct device_dirent *root_dirent;
        rt_uint32_t count = 0;
167

Y
yiyue.fang 已提交
168 169 170 171
        /* lock scheduler */
        rt_enter_critical();

        /* traverse device object */
172 173
        information = rt_object_get_information(RT_Object_Class_Device);
        RT_ASSERT(information != RT_NULL);
Y
yiyue.fang 已提交
174 175 176 177
        for (node = information->object_list.next; node != &(information->object_list); node = node->next)
        {
            count ++;
        }
178
        rt_exit_critical();
Y
yiyue.fang 已提交
179

180 181
        root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
                      count * sizeof(rt_device_t));
Y
yiyue.fang 已提交
182 183
        if (root_dirent != RT_NULL)
        {
184 185 186
            /* lock scheduler */
            rt_enter_critical();

Y
yiyue.fang 已提交
187 188 189 190 191 192 193
            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)
            {
194 195 196 197 198 199
                /* avoid memory write through */
                if (count == root_dirent->device_count)
                {
                    rt_kprintf("warning: There are newly added devices that are not displayed!");
                    break;
                }
Y
yiyue.fang 已提交
200 201 202 203
                object = rt_list_entry(node, struct rt_object, list);
                root_dirent->devices[count] = (rt_device_t)object;
                count ++;
            }
204
            rt_exit_critical();
Y
yiyue.fang 已提交
205 206 207
        }

        /* set data */
G
guo 已提交
208
        file->vnode->data = root_dirent;
209

B
bernard 已提交
210
        return RT_EOK;
Y
yiyue.fang 已提交
211
    }
G
guo 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225
#ifdef RT_USING_DEV_BUS
    else if (file->flags & O_CREAT)
    {
        if (!(file->flags & O_DIRECTORY))
        {
            return -ENOSYS;
        }
        /* regester bus device */
        if (rt_device_bus_create(&file->vnode->path[1], 0) == RT_NULL)
        {
            return -EEXIST;
        }
    }
#endif
Y
yiyue.fang 已提交
226

G
guo 已提交
227
    device = rt_device_find(&file->vnode->path[1]);
Y
yiyue.fang 已提交
228
    if (device == RT_NULL)
G
guo 已提交
229
    {
B
bernard 已提交
230
        return -ENODEV;
G
guo 已提交
231
    }
Y
yiyue.fang 已提交
232

233
#ifdef RT_USING_POSIX_DEVIO
B
bernard 已提交
234
    if (device->fops)
235
    {
B
bernard 已提交
236
        /* use device fops */
G
guo 已提交
237 238
        file->vnode->fops = device->fops;
        file->vnode->data = (void *)device;
B
bernard 已提交
239 240

        /* use fops */
G
guo 已提交
241
        if (file->vnode->fops->open)
B
bernard 已提交
242
        {
G
guo 已提交
243
            result = file->vnode->fops->open(file);
B
bernard 已提交
244 245
            if (result == RT_EOK || result == -RT_ENOSYS)
            {
G
guo 已提交
246
                file->vnode->type = FT_DEVICE;
B
bernard 已提交
247 248 249 250 251
                return 0;
            }
        }
    }
    else
252
#endif /* RT_USING_POSIX_DEVIO */
B
bernard 已提交
253 254 255 256
    {
        result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
        if (result == RT_EOK || result == -RT_ENOSYS)
        {
G
guo 已提交
257 258
            file->vnode->data = device;
            file->vnode->type = FT_DEVICE;
B
bernard 已提交
259 260
            return RT_EOK;
        }
261 262
    }

G
guo 已提交
263
    file->vnode->data = RT_NULL;
264
    /* open device failed. */
B
bernard 已提交
265
    return -EIO;
266 267
}

G
guo 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
int dfs_device_fs_unlink(struct dfs_filesystem *fs, const char *path)
{
#ifdef RT_USING_DEV_BUS
    rt_device_t dev_id;

    dev_id = rt_device_find(&path[1]);
    if (dev_id == RT_NULL)
    {
        return -1;
    }
    if (dev_id->type != RT_Device_Class_Bus)
    {
        return -1;
    }
    rt_device_bus_destroy(dev_id);
#endif
    return RT_EOK;
}

287
int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
288
{
Y
yiyue.fang 已提交
289 290 291 292 293
    /* stat root directory */
    if ((path[0] == '/') && (path[1] == '\0'))
    {
        st->st_dev = 0;

B
bernard 已提交
294
        st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
295
                      S_IWUSR | S_IWGRP | S_IWOTH;
B
bernard 已提交
296 297
        st->st_mode &= ~S_IFREG;
        st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
Y
yiyue.fang 已提交
298 299 300

        st->st_size  = 0;
        st->st_mtime = 0;
301

B
bernard 已提交
302
        return RT_EOK;
Y
yiyue.fang 已提交
303 304 305 306 307 308 309 310 311 312
    }
    else
    {
        rt_device_t dev_id;

        dev_id = rt_device_find(&path[1]);
        if (dev_id != RT_NULL)
        {
            st->st_dev = 0;

B
bernard 已提交
313
            st->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
314
                          S_IWUSR | S_IWGRP | S_IWOTH;
Y
yiyue.fang 已提交
315 316

            if (dev_id->type == RT_Device_Class_Char)
B
bernard 已提交
317
                st->st_mode |= S_IFCHR;
Y
yiyue.fang 已提交
318
            else if (dev_id->type == RT_Device_Class_Block)
B
bernard 已提交
319 320 321
                st->st_mode |= S_IFBLK;
            else if (dev_id->type == RT_Device_Class_Pipe)
                st->st_mode |= S_IFIFO;
G
guo 已提交
322 323
            else if (dev_id->type == RT_Device_Class_Bus)
                st->st_mode |= S_IFDIR;
Y
yiyue.fang 已提交
324
            else
B
bernard 已提交
325
                st->st_mode |= S_IFREG;
Y
yiyue.fang 已提交
326 327 328

            st->st_size  = 0;
            st->st_mtime = 0;
329

B
bernard 已提交
330
            return RT_EOK;
Y
yiyue.fang 已提交
331 332 333
        }
    }

B
bernard 已提交
334
    return -ENOENT;
335 336
}

337
int dfs_device_fs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
338
{
Y
yiyue.fang 已提交
339 340 341 342 343
    rt_uint32_t index;
    rt_object_t object;
    struct dirent *d;
    struct device_dirent *root_dirent;

G
guo 已提交
344
    root_dirent = (struct device_dirent *)file->vnode->data;
Y
yiyue.fang 已提交
345 346 347 348 349
    RT_ASSERT(root_dirent != RT_NULL);

    /* make integer count */
    count = (count / sizeof(struct dirent));
    if (count == 0)
B
bernard 已提交
350
        return -EINVAL;
Y
yiyue.fang 已提交
351

352
    for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
Y
yiyue.fang 已提交
353 354 355 356 357
        index ++)
    {
        object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];

        d = dirp + index;
G
guo 已提交
358 359 360 361 362 363 364 365
        if ((((rt_device_t)object)->type) == RT_Device_Class_Bus)
        {
            d->d_type = DT_DIR;
        }
        else
        {
            d->d_type = DT_REG;
        }
Y
yiyue.fang 已提交
366 367 368 369 370 371 372 373
        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);
374 375
}

376
static int dfs_device_fs_poll(struct dfs_file *fd, struct rt_pollreq *req)
377
{
B
bernard 已提交
378 379 380 381
    int mask = 0;

    return mask;
}
Y
yiyue.fang 已提交
382

383
static const struct dfs_file_ops _device_fops =
B
bernard 已提交
384
{
Y
yiyue.fang 已提交
385 386 387 388 389
    dfs_device_fs_open,
    dfs_device_fs_close,
    dfs_device_fs_ioctl,
    dfs_device_fs_read,
    dfs_device_fs_write,
B
bernard 已提交
390 391 392 393 394 395
    RT_NULL,                    /* flush */
    RT_NULL,                    /* lseek */
    dfs_device_fs_getdents,
    dfs_device_fs_poll,
};

396
static const struct dfs_filesystem_ops _device_fs =
B
bernard 已提交
397 398 399 400 401
{
    "devfs",
    DFS_FS_FLAG_DEFAULT,
    &_device_fops,
    dfs_device_fs_mount,
mysterywolf's avatar
mysterywolf 已提交
402 403
    RT_NULL, /*unmount*/
    RT_NULL, /*mkfs*/
404
    dfs_device_fs_statfs,
G
guo 已提交
405
    dfs_device_fs_unlink,
Y
yiyue.fang 已提交
406
    dfs_device_fs_stat,
mysterywolf's avatar
mysterywolf 已提交
407
    RT_NULL, /*rename*/
408 409 410 411
};

int devfs_init(void)
{
X
xingkong121 已提交
412
    /* register device file system */
Y
yiyue.fang 已提交
413
    dfs_register(&_device_fs);
414

Y
yiyue.fang 已提交
415
    return 0;
416
}