guozhanxin 3 vuotta sitten
vanhempi
commit
57974beea0

+ 49 - 9
components/dfs/filesystems/devfs/devfs.c

@@ -181,6 +181,20 @@ int dfs_device_fs_open(struct dfs_fd *file)
 
         return RT_EOK;
     }
+#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->fnode->path[1], 0) == RT_NULL)
+        {
+            return -EEXIST;
+        }
+    }
+#endif
 
     device = rt_device_find(&file->fnode->path[1]);
     if (device == RT_NULL)
@@ -223,6 +237,25 @@ int dfs_device_fs_open(struct dfs_fd *file)
     return -EIO;
 }
 
+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;
+}
+
 int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
 {
     /* stat root directory */
@@ -258,6 +291,8 @@ int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat
                 st->st_mode |= S_IFBLK;
             else if (dev_id->type == RT_Device_Class_Pipe)
                 st->st_mode |= S_IFIFO;
+            else if (dev_id->type == RT_Device_Class_Bus)
+                st->st_mode |= S_IFDIR;
             else
                 st->st_mode |= S_IFREG;
 
@@ -292,7 +327,14 @@ int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t co
         object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
 
         d = dirp + index;
-        d->d_type = DT_REG;
+        if ((((rt_device_t)object)->type) == RT_Device_Class_Bus)
+        {
+            d->d_type = DT_DIR;
+        }
+        else
+        {
+            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);
@@ -328,20 +370,18 @@ static const struct dfs_filesystem_ops _device_fs =
     "devfs",
     DFS_FS_FLAG_DEFAULT,
     &_device_fops,
-
     dfs_device_fs_mount,
-    RT_NULL,
-    RT_NULL,
-    RT_NULL,
-
-    RT_NULL,
+    RT_NULL, /*unmount*/
+    RT_NULL, /*mkfs*/
+    RT_NULL, /*statfs*/
+    dfs_device_fs_unlink,
     dfs_device_fs_stat,
-    RT_NULL,
+    RT_NULL, /*rename*/
 };
 
 int devfs_init(void)
 {
-    /* register rom file system */
+    /* register device file system */
     dfs_register(&_device_fs);
 
     return 0;

+ 4 - 0
components/drivers/Kconfig

@@ -515,6 +515,10 @@ if RT_USING_INPUT_CAPTURE
         default 100
 endif
 
+config RT_USING_DEV_BUS
+    bool "Using Device Bus device drivers"
+    default n
+
 menuconfig RT_USING_WIFI
     bool "Using Wi-Fi framework"
     default n

+ 18 - 0
components/drivers/include/drivers/rt_dev_bus.h

@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-10-13     flybreak     the first version
+ */
+
+#ifndef __RT_DEV_BUS_H__
+#define __RT_DEV_BUS_H__
+#include <rtthread.h>
+
+rt_device_t rt_device_bus_create(char *name, int attach_size);
+rt_err_t rt_device_bus_destroy(rt_device_t dev);
+
+#endif /* __RT_BUS_H__ */

+ 4 - 0
components/drivers/include/rtdevice.h

@@ -147,6 +147,10 @@ extern "C" {
 #include "drivers/lcd.h"
 #endif
 
+#ifdef RT_USING_DEV_BUS
+#include "drivers/rt_dev_bus.h"
+#endif
+
 #ifdef __cplusplus
 }
 #endif

+ 3 - 0
components/drivers/misc/SConscript

@@ -23,6 +23,9 @@ if GetDepend(['RT_USING_PULSE_ENCODER']):
 if GetDepend(['RT_USING_INPUT_CAPTURE']):
     src = src + ['rt_inputcapture.c']
 
+if GetDepend(['RT_USING_DEV_BUS']):
+    src = src + ['rt_dev_bus.c']
+
 if GetDepend(['RT_USING_NULL']):
     src = src + ['rt_null.c']
 

+ 80 - 0
components/drivers/misc/rt_dev_bus.c

@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-10-13     flybreak     the first version
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#include <string.h>
+#include <stdlib.h>
+
+#define DBG_TAG "dev_bus"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+#if defined(RT_USING_POSIX)
+#include <unistd.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <sys/ioctl.h>
+#include <dfs_file.h>
+
+static int bus_fops_open(struct dfs_fd *fd)
+{
+    LOG_D("bus fops open");
+    return 0;
+}
+
+static int bus_fops_close(struct dfs_fd *fd)
+{
+    LOG_D("bus fops close");
+    return 0;
+}
+
+static const struct dfs_file_ops bus_fops =
+{
+    bus_fops_open,
+    bus_fops_close,
+    RT_NULL,
+    RT_NULL,
+    RT_NULL,
+    RT_NULL,
+    RT_NULL,
+    RT_NULL,
+    RT_NULL,
+};
+#endif
+
+rt_device_t rt_device_bus_create(char *name, int attach_size)
+{
+    rt_err_t result = RT_EOK;
+    rt_device_t dev = rt_device_create(RT_Device_Class_Bus, 0);
+
+    result = rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
+    if (result < 0)
+    {
+        rt_kprintf("dev bus [%s] register failed!, ret=%d\n", name, result);
+        return RT_NULL;
+    }
+#if defined(RT_USING_POSIX)
+    dev->fops = &bus_fops;
+#endif
+
+    LOG_D("bus create");
+    return dev;
+}
+
+rt_err_t rt_device_bus_destroy(rt_device_t dev)
+{
+    rt_device_unregister(dev);
+    dev->parent.type = RT_Object_Class_Device;
+    rt_device_destroy(dev);
+    LOG_D("bus destroy");
+    return RT_EOK;
+}

+ 1 - 0
components/finsh/cmd.c

@@ -811,6 +811,7 @@ static char *const device_type_str[] =
     "Sensor Device",
     "Touch Device",
     "Phy Device",
+    "Bus Device",
     "Unknown"
 };
 

+ 1 - 0
include/rtdef.h

@@ -997,6 +997,7 @@ enum rt_device_class_type
     RT_Device_Class_Sensor,                             /**< Sensor device */
     RT_Device_Class_Touch,                              /**< Touch device */
     RT_Device_Class_PHY,                                /**< PHY device */
+    RT_Device_Class_Bus,                                /**< Bus device */
     RT_Device_Class_Unknown                             /**< unknown device */
 };