From 47984e75d74a9a303096933161e6094cba86b8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Thu, 23 Jun 2022 17:06:35 +0800 Subject: [PATCH 01/10] add driver device mode --- include/rt_driver.h | 32 ++++++++++ include/rtdef.h | 3 +- include/rtthread.h | 5 +- src/drvier.c | 150 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 include/rt_driver.h create mode 100644 src/drvier.c diff --git a/include/rt_driver.h b/include/rt_driver.h new file mode 100644 index 0000000000..dc38a98dba --- /dev/null +++ b/include/rt_driver.h @@ -0,0 +1,32 @@ +#ifndef __RT_DRIVER_H__ +#define __RT_DRIVER_H__ + +#include + +#define RT_DRIVER_MATCH_DTS (1<<0) + +struct rt_device_id +{ + const char *compatible; + void *data; +}; + +struct rt_driver +{ + struct rt_object parent; + const struct rt_device_ops *dev_ops; + const struct filesystem_ops *fops; + const char *name; + enum rt_device_class_type dev_type; + int device_priv_data_size; + int device_size; + int flag; + int total_device_num; + const struct rt_device_id *dev_match; + int (*probe)(struct rt_device *dev); + int (*init)(struct rt_device *dev); + const void *ops; /* driver-specific operations */ +}; +typedef struct rt_driver *rt_driver_t; + +#endif \ No newline at end of file diff --git a/include/rtdef.h b/include/rtdef.h index 27b2b21e3c..f6c9a282c6 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -1093,7 +1093,8 @@ typedef struct rt_wqueue rt_wqueue_t; struct rt_device { struct rt_object parent; /**< inherit from rt_object */ - + const struct rt_driver *drv; + void *fdt_node; enum rt_device_class_type type; /**< device type */ rt_uint16_t flag; /**< device flag */ rt_uint16_t open_flag; /**< device open flag */ diff --git a/include/rtthread.h b/include/rtthread.h index 4581cb1f22..0c897c51d0 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -24,7 +24,7 @@ #include #include #include - +#include #ifdef __cplusplus extern "C" { #endif @@ -518,6 +518,9 @@ rt_size_t rt_device_write(rt_device_t dev, rt_size_t size); rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg); +rt_err_t rt_driver_register(rt_driver_t drv); +rt_err_t rt_driver_unregister(rt_driver_t drv); + /**@}*/ #endif diff --git a/src/drvier.c b/src/drvier.c new file mode 100644 index 0000000000..7340f7b7f8 --- /dev/null +++ b/src/drvier.c @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#if defined(RT_USING_POSIX) +#include /* for wqueue_init */ +#endif + +#define MAX_COMPATIBLE_NUM 10 + +/** + * This function bind drvier and device + * + * @param driver the pointer of driver structure + * @param device the pointer of device structure + * @param node the pointer of fdt node structure + * + * @return the error code, RT_EOK on successfully. + */ +rt_err_t rt_device_bind(rt_driver_t driver, rt_device_t device, void *node) +{ + if((!driver) || (!device)) + { + return -RT_ERROR; + } + + device->drv = driver; + device->ops = driver->dev_ops; + device->fdt_node = node; + + return RT_EOK; +} + +/** + * This function create rt_device and init the device + * + * @param driver the pointer of driver structure + * @param device_num how many device should be create + * @param ftd_node_list ftd node list + * + * @return the error code, RT_EOK on successfully. + */ +rt_err_t rt_device_create_and_init(rt_driver_t drv,int device_num,struct dtb_node **ftd_node_list) +{ + int i = 0; + int ret = -1; + rt_device_t device; + for(i = 0; i < device_num; i ++) + { + device = (rt_device_t)rt_malloc(drv->device_size); + if(device == RT_NULL) + { + return -RT_ERROR; + } + rt_memset(device,0,drv->device_size); + if(drv->device_priv_data_size != 0) + { + device->user_data = (void *)(rt_malloc(drv->device_priv_data_size)); + if(device->user_data == RT_NULL) + { + rt_free(device); + return -RT_ERROR; + } + rt_memset(device->user_data,0,drv->device_priv_data_size); + } + + device->device_id = i; + rt_strncpy(device->parent.name,drv->name,rt_strlen(drv->name)); + rt_sprintf(device->parent.name + rt_strlen(drv->name),"%d",i); + + rt_device_bind(drv,device,ftd_node_list[i]); + + if(device->drv->probe) + { + ret = device->drv->probe((rt_device_t)device); + } + if(device->drv->init) + { + ret = device->drv->init((rt_device_t)device); + } + + } + return ret; +} +/** + * This function registers a device driver with specified name. + * + * @param dev the pointer of driver structure + * @param flags the capabilities flag of device + * + * @return the error code, RT_EOK on successfully. + */ +rt_err_t rt_driver_register(rt_driver_t drv) +{ + struct dtb_node *root_node = RT_NULL; + struct dtb_node* node_list[MAX_COMPATIBLE_NUM]; + int ret,i,device_num; + if (drv == RT_NULL) + { + return -RT_ERROR; + } + + root_node = get_dtb_node_head(); + if(drv->dev_match->compatible != RT_NULL) + { + ret = fdt_find_all_active_compatible_node(root_node,drv->dev_match->compatible,node_list,MAX_COMPATIBLE_NUM,&device_num); + if(!ret) + { + } + } + else + { + device_num = drv->total_device_num; + for(i = 0; i < device_num; i++) + { + node_list[i] = RT_NULL; + } + } + + if(!device_num) + { + rt_kprintf("can not match compatible device\n"); + } + ret = rt_device_create_and_init(drv,device_num,node_list); + + return ret; +} + +RTM_EXPORT(rt_driver_register); + +/** + * This function removes a previously registered device driver + * + * @param dev the pointer of device driver structure + * + * @return the error code, RT_EOK on successfully. + */ +rt_err_t rt_driver_unregister(rt_driver_t drv) +{ + /*todo*/ + return RT_EOK; +} +RTM_EXPORT(rt_driver_unregister); + -- Gitee From 899cc573f7e865d76e45e695cd0d526e7e8877aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Thu, 23 Jun 2022 17:25:38 +0800 Subject: [PATCH 02/10] modify code style --- src/drvier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/drvier.c b/src/drvier.c index 7340f7b7f8..815b2f64c4 100644 --- a/src/drvier.c +++ b/src/drvier.c @@ -113,7 +113,7 @@ rt_err_t rt_driver_register(rt_driver_t drv) if(!ret) { } - } + } else { device_num = drv->total_device_num; -- Gitee From 43a21e863e57962974a2fdb795c5817d76dab78b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Thu, 23 Jun 2022 18:33:42 +0800 Subject: [PATCH 03/10] modify support rtos --- include/rt_driver.h | 2 ++ src/{drvier.c => driver.c} | 4 ++++ 2 files changed, 6 insertions(+) rename src/{drvier.c => driver.c} (97%) diff --git a/include/rt_driver.h b/include/rt_driver.h index dc38a98dba..0f5cd766b1 100644 --- a/include/rt_driver.h +++ b/include/rt_driver.h @@ -14,7 +14,9 @@ struct rt_device_id struct rt_driver { struct rt_object parent; +#ifdef RT_USING_DEVICE_OPS const struct rt_device_ops *dev_ops; +#endif const struct filesystem_ops *fops; const char *name; enum rt_device_class_type dev_type; diff --git a/src/drvier.c b/src/driver.c similarity index 97% rename from src/drvier.c rename to src/driver.c index 815b2f64c4..3fe17abf70 100644 --- a/src/drvier.c +++ b/src/driver.c @@ -31,7 +31,9 @@ rt_err_t rt_device_bind(rt_driver_t driver, rt_device_t device, void *node) } device->drv = driver; +#ifdef RT_USING_DEVICE_OPS device->ops = driver->dev_ops; +#endif device->fdt_node = node; return RT_EOK; @@ -109,10 +111,12 @@ rt_err_t rt_driver_register(rt_driver_t drv) root_node = get_dtb_node_head(); if(drv->dev_match->compatible != RT_NULL) { +#ifdef PKG_USING_FDT ret = fdt_find_all_active_compatible_node(root_node,drv->dev_match->compatible,node_list,MAX_COMPATIBLE_NUM,&device_num); if(!ret) { } +#endif } else { -- Gitee From 6db57a01171aa2e7b11ae8c618e5ff52e458032d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Fri, 24 Jun 2022 14:37:43 +0800 Subject: [PATCH 04/10] modify driver mode --- include/rt_driver.h | 3 +- include/rtthread.h | 8 ++- src/device.c | 92 ++++++++++++++++++++++++++ src/driver.c | 156 ++++++++++++++++++-------------------------- 4 files changed, 161 insertions(+), 98 deletions(-) diff --git a/include/rt_driver.h b/include/rt_driver.h index 0f5cd766b1..087bbbd70a 100644 --- a/include/rt_driver.h +++ b/include/rt_driver.h @@ -13,7 +13,6 @@ struct rt_device_id struct rt_driver { - struct rt_object parent; #ifdef RT_USING_DEVICE_OPS const struct rt_device_ops *dev_ops; #endif @@ -23,10 +22,10 @@ struct rt_driver int device_priv_data_size; int device_size; int flag; - int total_device_num; const struct rt_device_id *dev_match; int (*probe)(struct rt_device *dev); int (*init)(struct rt_device *dev); + int (*remove)(struct rt_device *dev); const void *ops; /* driver-specific operations */ }; typedef struct rt_driver *rt_driver_t; diff --git a/include/rtthread.h b/include/rtthread.h index 0c897c51d0..888aafffc8 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -518,8 +518,12 @@ rt_size_t rt_device_write(rt_device_t dev, rt_size_t size); rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg); -rt_err_t rt_driver_register(rt_driver_t drv); -rt_err_t rt_driver_unregister(rt_driver_t drv); +rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *node); +rt_device_t rt_device_create_since_driver(rt_driver_t drv,int device_id); +rt_err_t rt_device_probe_and_init(rt_device_t device); +rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id); +rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num); +rt_err_t rt_driver_unregister(const rt_driver_t drv); /**@}*/ #endif diff --git a/src/device.c b/src/device.c index 32c06b380c..8c150c7e1f 100644 --- a/src/device.c +++ b/src/device.c @@ -477,4 +477,96 @@ rt_device_set_tx_complete(rt_device_t dev, } RTM_EXPORT(rt_device_set_tx_complete); +/** + * This function bind drvier and device + * + * @param driver the pointer of driver structure + * @param device the pointer of device structure + * @param node the pointer of fdt node structure + * + * @return the error code, RT_EOK on successfully. + */ +rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *node) +{ + if((!driver) || (!device)) + { + return -RT_EINVAL; + } + + device->drv = driver; +#ifdef RT_USING_DEVICE_OPS + device->ops = driver->dev_ops; +#endif + device->fdt_node = node; + + return RT_EOK; +} +RTM_EXPORT(rt_device_driver_bind); + +/** + * This function create rt_device according to driver infomation + * + * @param drv the pointer of driver structure + * @param device_id specify the ID of the rt_device + * + * @return the error code, RT_EOK on successfully. + */ +rt_device_t rt_device_create_since_driver(rt_driver_t drv,int device_id) +{ + rt_device_t device; + if (!drv) + { + return RT_NULL; + } + + device = (rt_device_t)rt_calloc(1,drv->device_size); + if(device == RT_NULL) + { + return RT_NULL; + } + if(drv->device_priv_data_size != 0) + { + device->user_data = (void *)(rt_calloc(1,drv->device_priv_data_size)); + if(device->user_data == RT_NULL) + { + rt_free(device); + return RT_NULL; + } + } + + device->device_id = device_id; + rt_snprintf(device->parent.name, sizeof(device->parent.name), "%s%d", drv->name, device_id); + return device; +} +RTM_EXPORT(rt_device_create_since_driver); + +/** + * This function rt_device probe and init + * + * @param device the pointer of rt_device structure + * @return the error code, RT_EOK on successfully. + */ +rt_err_t rt_device_probe_and_init(rt_device_t device) +{ + int ret = -RT_ERROR; + if (!device) + { + return -RT_EINVAL; + } + if(!device->drv) + { + return -RT_ERROR; + } + if(device->drv->probe) + { + ret = device->drv->probe((rt_device_t)device); + } + if(device->drv->init) + { + ret = device->drv->init((rt_device_t)device); + } + return ret; +} +RTM_EXPORT(rt_device_probe_and_init); + #endif diff --git a/src/driver.c b/src/driver.c index 3fe17abf70..1aff78ea29 100644 --- a/src/driver.c +++ b/src/driver.c @@ -6,137 +6,105 @@ #include #include -#include -#include #if defined(RT_USING_POSIX) #include /* for wqueue_init */ #endif -#define MAX_COMPATIBLE_NUM 10 - /** - * This function bind drvier and device + * This function registers a device driver with specified name. * - * @param driver the pointer of driver structure - * @param device the pointer of device structure - * @param node the pointer of fdt node structure + * @param drv the pointer of driver structure + * @param device_id the id of the device * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_device_bind(rt_driver_t driver, rt_device_t device, void *node) +rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id) { - if((!driver) || (!device)) + rt_device_t device; + int ret; + if (!drv) + { + return -RT_EINVAL; + } + device = rt_device_create_since_driver(drv,device_id); + if(!device) { return -RT_ERROR; } + ret = rt_device_driver_bind(device,drv,RT_NULL); + if(ret != 0) + { + return -RT_ERROR; + } + ret = rt_device_probe_and_init(device); + if(ret != 0) + { + return -RT_ERROR; + } + return ret; +} - device->drv = driver; -#ifdef RT_USING_DEVICE_OPS - device->ops = driver->dev_ops; -#endif - device->fdt_node = node; - - return RT_EOK; -} +RTM_EXPORT(rt_driver_device_match_with_id); +#ifdef PKG_USING_FDT /** - * This function create rt_device and init the device - * - * @param driver the pointer of driver structure - * @param device_num how many device should be create - * @param ftd_node_list ftd node list + * This function registers a device driver with specified name. * + * @param drv the pointer of driver structure + * @param from_node eth entry ftd node + * @param max_dev_num the max device support + * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_device_create_and_init(rt_driver_t drv,int device_num,struct dtb_node **ftd_node_list) +rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num) { - int i = 0; - int ret = -1; + struct dtb_node** node_list; rt_device_t device; - for(i = 0; i < device_num; i ++) + int ret,i; + int active_dev_num = 0; + if ((!drv)||(!drv->dev_match)||(!drv->dev_match->compatible)||(!from_node)) { - device = (rt_device_t)rt_malloc(drv->device_size); - if(device == RT_NULL) - { - return -RT_ERROR; - } - rt_memset(device,0,drv->device_size); - if(drv->device_priv_data_size != 0) - { - device->user_data = (void *)(rt_malloc(drv->device_priv_data_size)); - if(device->user_data == RT_NULL) - { - rt_free(device); - return -RT_ERROR; - } - rt_memset(device->user_data,0,drv->device_priv_data_size); - } - - device->device_id = i; - rt_strncpy(device->parent.name,drv->name,rt_strlen(drv->name)); - rt_sprintf(device->parent.name + rt_strlen(drv->name),"%d",i); - - rt_device_bind(drv,device,ftd_node_list[i]); - - if(device->drv->probe) - { - ret = device->drv->probe((rt_device_t)device); - } - if(device->drv->init) - { - ret = device->drv->init((rt_device_t)device); - } + return -RT_EINVAL; + } + node_list = rt_calloc(max_dev_num,sizeof(void *)); + if(!node_list) + { + return -RT_ERROR; } - return ret; -} -/** - * This function registers a device driver with specified name. - * - * @param dev the pointer of driver structure - * @param flags the capabilities flag of device - * - * @return the error code, RT_EOK on successfully. - */ -rt_err_t rt_driver_register(rt_driver_t drv) -{ - struct dtb_node *root_node = RT_NULL; - struct dtb_node* node_list[MAX_COMPATIBLE_NUM]; - int ret,i,device_num; - if (drv == RT_NULL) + + ret = fdt_find_all_active_compatible_node(from_node,drv->dev_match->compatible,node_list,max_dev_num,&active_dev_num); + if((ret != 0) || (!active_dev_num)) { return -RT_ERROR; } - - root_node = get_dtb_node_head(); - if(drv->dev_match->compatible != RT_NULL) + + for(i = 0; i < active_dev_num; i ++) { -#ifdef PKG_USING_FDT - ret = fdt_find_all_active_compatible_node(root_node,drv->dev_match->compatible,node_list,MAX_COMPATIBLE_NUM,&device_num); - if(!ret) + device = rt_device_create_since_driver(drv,i); + if(!device) { + return -RT_ERROR; } -#endif - } - else - { - device_num = drv->total_device_num; - for(i = 0; i < device_num; i++) + + ret = rt_device_driver_bind(device,drv,node_list[i]); + if(ret != 0) { - node_list[i] = RT_NULL; + return -RT_ERROR; + } + ret = rt_device_probe_and_init(device); + if(ret != 0) + { + return -RT_ERROR; } - } - if(!device_num) - { - rt_kprintf("can not match compatible device\n"); } - ret = rt_device_create_and_init(drv,device_num,node_list); return ret; } -RTM_EXPORT(rt_driver_register); +RTM_EXPORT(rt_driver_device_match_with_dtb); +#endif /** * This function removes a previously registered device driver @@ -145,7 +113,7 @@ RTM_EXPORT(rt_driver_register); * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_driver_unregister(rt_driver_t drv) +rt_err_t rt_driver_unregister(const rt_driver_t drv) { /*todo*/ return RT_EOK; -- Gitee From f4e4f488d9bbee2a2086d7427a2ad6fdbecc7774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Fri, 24 Jun 2022 15:07:18 +0800 Subject: [PATCH 05/10] add Macro for driver device mode --- include/rtdef.h | 2 ++ include/rtthread.h | 4 ++-- src/Kconfig | 9 +++++++++ src/SConscript | 3 +++ src/device.c | 3 ++- src/driver.c | 3 ++- 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/rtdef.h b/include/rtdef.h index f6c9a282c6..e89166c663 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -1093,8 +1093,10 @@ typedef struct rt_wqueue rt_wqueue_t; struct rt_device { struct rt_object parent; /**< inherit from rt_object */ +#ifdef RT_USING_DDM const struct rt_driver *drv; void *fdt_node; +#endif enum rt_device_class_type type; /**< device type */ rt_uint16_t flag; /**< device flag */ rt_uint16_t open_flag; /**< device open flag */ diff --git a/include/rtthread.h b/include/rtthread.h index 888aafffc8..0cca041e7c 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -517,14 +517,14 @@ rt_size_t rt_device_write(rt_device_t dev, const void *buffer, rt_size_t size); rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg); - +#ifdef RT_USING_DDM rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *node); rt_device_t rt_device_create_since_driver(rt_driver_t drv,int device_id); rt_err_t rt_device_probe_and_init(rt_device_t device); rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id); rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num); rt_err_t rt_driver_unregister(const rt_driver_t drv); - +#endif /**@}*/ #endif diff --git a/src/Kconfig b/src/Kconfig index fd59cdd34d..55627a656f 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -347,4 +347,13 @@ config RT_VER_NUM help RT-Thread version number +config RT_USING_DDM + bool "Enable rt device driver mode" + default n + help + This option should be selected by machines which have an SMP- + capable CPU. + The only effect of this option is to make the SMP-related + options available to the user for configuration. + endmenu diff --git a/src/SConscript b/src/SConscript index 82c3b8aaf1..cae7c1d730 100644 --- a/src/SConscript +++ b/src/SConscript @@ -29,6 +29,9 @@ if GetDepend('RT_USING_DEVICE') == False: if GetDepend('RT_USING_SMP') == False: SrcRemove(src, ['cpu.c']) +if GetDepend('RT_USING_DDM') == False: + SrcRemove(src, ['driver.c']) + group = DefineGroup('Kernel', src, depend = [''], CPPPATH = CPPPATH) Return('group') diff --git a/src/device.c b/src/device.c index 8c150c7e1f..f4b4024d34 100644 --- a/src/device.c +++ b/src/device.c @@ -477,6 +477,7 @@ rt_device_set_tx_complete(rt_device_t dev, } RTM_EXPORT(rt_device_set_tx_complete); +#ifdef RT_USING_DDM /** * This function bind drvier and device * @@ -568,5 +569,5 @@ rt_err_t rt_device_probe_and_init(rt_device_t device) return ret; } RTM_EXPORT(rt_device_probe_and_init); - +#endif #endif diff --git a/src/driver.c b/src/driver.c index 1aff78ea29..0b0ea824aa 100644 --- a/src/driver.c +++ b/src/driver.c @@ -72,7 +72,7 @@ rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,i { return -RT_ERROR; } - + ret = fdt_find_all_active_compatible_node(from_node,drv->dev_match->compatible,node_list,max_dev_num,&active_dev_num); if((ret != 0) || (!active_dev_num)) { @@ -100,6 +100,7 @@ rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,i } + rt_free(node_list); return ret; } -- Gitee From 6485a37bf4b272e048afce29ef4c8d25ecc17fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Fri, 24 Jun 2022 15:52:50 +0800 Subject: [PATCH 06/10] modify driver device kconfig --- src/Kconfig | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Kconfig b/src/Kconfig index 55627a656f..a42134285d 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -350,10 +350,5 @@ config RT_VER_NUM config RT_USING_DDM bool "Enable rt device driver mode" default n - help - This option should be selected by machines which have an SMP- - capable CPU. - The only effect of this option is to make the SMP-related - options available to the user for configuration. - + endmenu -- Gitee From 2526346ed8fb46d1c797fda094e41bf423217471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Wed, 29 Jun 2022 17:59:36 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9dtb=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 2 +- src/device.c | 2 +- src/driver.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/rtdef.h b/include/rtdef.h index e89166c663..7f763e7f7a 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -1095,7 +1095,7 @@ struct rt_device struct rt_object parent; /**< inherit from rt_object */ #ifdef RT_USING_DDM const struct rt_driver *drv; - void *fdt_node; + void *dtb_node; #endif enum rt_device_class_type type; /**< device type */ rt_uint16_t flag; /**< device flag */ diff --git a/src/device.c b/src/device.c index f4b4024d34..94772d7406 100644 --- a/src/device.c +++ b/src/device.c @@ -498,7 +498,7 @@ rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *nod #ifdef RT_USING_DEVICE_OPS device->ops = driver->dev_ops; #endif - device->fdt_node = node; + device->dtb_node = node; return RT_EOK; } diff --git a/src/driver.c b/src/driver.c index 0b0ea824aa..c81473fb01 100644 --- a/src/driver.c +++ b/src/driver.c @@ -5,7 +5,7 @@ */ #include -#include +#include #if defined(RT_USING_POSIX) #include /* for wqueue_init */ #endif @@ -73,7 +73,7 @@ rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,i return -RT_ERROR; } - ret = fdt_find_all_active_compatible_node(from_node,drv->dev_match->compatible,node_list,max_dev_num,&active_dev_num); + ret = dtb_node_find_all_active_compatible_node(from_node,drv->dev_match->compatible,node_list,max_dev_num,&active_dev_num); if((ret != 0) || (!active_dev_num)) { return -RT_ERROR; -- Gitee From ae41ff6d0831fecd485d865052e543ddc749ae74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Wed, 29 Jun 2022 19:33:59 +0800 Subject: [PATCH 08/10] modify dirver device struct --- include/rt_driver.h | 33 --------------------------------- include/rtdef.h | 28 +++++++++++++++++++++++++++- include/rtthread.h | 11 +++++------ 3 files changed, 32 insertions(+), 40 deletions(-) delete mode 100644 include/rt_driver.h diff --git a/include/rt_driver.h b/include/rt_driver.h deleted file mode 100644 index 087bbbd70a..0000000000 --- a/include/rt_driver.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __RT_DRIVER_H__ -#define __RT_DRIVER_H__ - -#include - -#define RT_DRIVER_MATCH_DTS (1<<0) - -struct rt_device_id -{ - const char *compatible; - void *data; -}; - -struct rt_driver -{ -#ifdef RT_USING_DEVICE_OPS - const struct rt_device_ops *dev_ops; -#endif - const struct filesystem_ops *fops; - const char *name; - enum rt_device_class_type dev_type; - int device_priv_data_size; - int device_size; - int flag; - const struct rt_device_id *dev_match; - int (*probe)(struct rt_device *dev); - int (*init)(struct rt_device *dev); - int (*remove)(struct rt_device *dev); - const void *ops; /* driver-specific operations */ -}; -typedef struct rt_driver *rt_driver_t; - -#endif \ No newline at end of file diff --git a/include/rtdef.h b/include/rtdef.h index 7f763e7f7a..55c36708b5 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -1093,7 +1093,7 @@ typedef struct rt_wqueue rt_wqueue_t; struct rt_device { struct rt_object parent; /**< inherit from rt_object */ -#ifdef RT_USING_DDM +#ifdef RT_USING_DM const struct rt_driver *drv; void *dtb_node; #endif @@ -1128,6 +1128,32 @@ struct rt_device void *user_data; /**< device private data */ }; +#define RT_DRIVER_MATCH_DTS (1<<0) +struct rt_device_id +{ + const char *compatible; + void *data; +}; + +struct rt_driver +{ +#ifdef RT_USING_DEVICE_OPS + const struct rt_device_ops *dev_ops; +#endif + const struct filesystem_ops *fops; + const char *name; + enum rt_device_class_type dev_type; + int device_priv_data_size; + int device_size; + int flag; + const struct rt_device_id *dev_match; + int (*probe)(struct rt_device *dev); + int (*probe_init)(struct rt_device *dev); + int (*remove)(struct rt_device *dev); + const void *ops; /* driver-specific operations */ +}; +typedef struct rt_driver *rt_driver_t; + /** * Notify structure */ diff --git a/include/rtthread.h b/include/rtthread.h index 0cca041e7c..44768567cc 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -24,7 +24,6 @@ #include #include #include -#include #ifdef __cplusplus extern "C" { #endif @@ -517,13 +516,13 @@ rt_size_t rt_device_write(rt_device_t dev, const void *buffer, rt_size_t size); rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg); -#ifdef RT_USING_DDM -rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *node); +#ifdef RT_USING_DM +rt_err_t rt_device_bind_driver(rt_device_t device, rt_driver_t driver, void *node); rt_device_t rt_device_create_since_driver(rt_driver_t drv,int device_id); rt_err_t rt_device_probe_and_init(rt_device_t device); -rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id); -rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num); -rt_err_t rt_driver_unregister(const rt_driver_t drv); + +rt_err_t rt_driver_match_with_id(const rt_driver_t drv,int device_id); +rt_err_t rt_driver_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num); #endif /**@}*/ #endif -- Gitee From a4f041d9146120898291c7f270fdb9793d5bdc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Wed, 29 Jun 2022 19:56:07 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Kconfig | 4 ++-- src/SConscript | 2 +- src/device.c | 10 +++++----- src/driver.c | 36 +++++++++++------------------------- 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/Kconfig b/src/Kconfig index a42134285d..90ab1840d9 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -347,8 +347,8 @@ config RT_VER_NUM help RT-Thread version number -config RT_USING_DDM - bool "Enable rt device driver mode" +config RT_USING_DM + bool "Enable rt device driver model" default n endmenu diff --git a/src/SConscript b/src/SConscript index cae7c1d730..bf59d0bc93 100644 --- a/src/SConscript +++ b/src/SConscript @@ -29,7 +29,7 @@ if GetDepend('RT_USING_DEVICE') == False: if GetDepend('RT_USING_SMP') == False: SrcRemove(src, ['cpu.c']) -if GetDepend('RT_USING_DDM') == False: +if GetDepend('RT_USING_DM') == False: SrcRemove(src, ['driver.c']) group = DefineGroup('Kernel', src, depend = [''], CPPPATH = CPPPATH) diff --git a/src/device.c b/src/device.c index 94772d7406..88c7680a48 100644 --- a/src/device.c +++ b/src/device.c @@ -477,7 +477,7 @@ rt_device_set_tx_complete(rt_device_t dev, } RTM_EXPORT(rt_device_set_tx_complete); -#ifdef RT_USING_DDM +#ifdef RT_USING_DM /** * This function bind drvier and device * @@ -487,7 +487,7 @@ RTM_EXPORT(rt_device_set_tx_complete); * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *node) +rt_err_t rt_device_bind_driver(rt_device_t device, rt_driver_t driver, void *node) { if((!driver) || (!device)) { @@ -502,7 +502,7 @@ rt_err_t rt_device_driver_bind(rt_device_t device, rt_driver_t driver, void *nod return RT_EOK; } -RTM_EXPORT(rt_device_driver_bind); +RTM_EXPORT(rt_device_bind_driver); /** * This function create rt_device according to driver infomation @@ -562,9 +562,9 @@ rt_err_t rt_device_probe_and_init(rt_device_t device) { ret = device->drv->probe((rt_device_t)device); } - if(device->drv->init) + if(device->drv->probe_init) { - ret = device->drv->init((rt_device_t)device); + ret = device->drv->probe_init((rt_device_t)device); } return ret; } diff --git a/src/driver.c b/src/driver.c index c81473fb01..9562168476 100644 --- a/src/driver.c +++ b/src/driver.c @@ -5,20 +5,22 @@ */ #include +#ifdef PKG_USING_FDT #include +#endif #if defined(RT_USING_POSIX) #include /* for wqueue_init */ #endif /** - * This function registers a device driver with specified name. + * This function driver device match with id * * @param drv the pointer of driver structure * @param device_id the id of the device * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id) +rt_err_t rt_driver_match_with_id(const rt_driver_t drv,int device_id) { rt_device_t device; int ret; @@ -31,7 +33,7 @@ rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id) { return -RT_ERROR; } - ret = rt_device_driver_bind(device,drv,RT_NULL); + ret = rt_device_bind_driver(device,drv,RT_NULL); if(ret != 0) { return -RT_ERROR; @@ -44,19 +46,19 @@ rt_err_t rt_driver_device_match_with_id(const rt_driver_t drv,int device_id) return ret; } -RTM_EXPORT(rt_driver_device_match_with_id); +RTM_EXPORT(rt_driver_match_with_id); #ifdef PKG_USING_FDT /** - * This function registers a device driver with specified name. + * This function driver device match with dtb_node * * @param drv the pointer of driver structure - * @param from_node eth entry ftd node + * @param from_node dtb node entry * @param max_dev_num the max device support * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num) +rt_err_t rt_driver_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num) { struct dtb_node** node_list; rt_device_t device; @@ -87,7 +89,7 @@ rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,i return -RT_ERROR; } - ret = rt_device_driver_bind(device,drv,node_list[i]); + ret = rt_device_bind_driver(device,drv,node_list[i]); if(ret != 0) { return -RT_ERROR; @@ -97,27 +99,11 @@ rt_err_t rt_driver_device_match_with_dtb(const rt_driver_t drv,void *from_node,i { return -RT_ERROR; } - } - rt_free(node_list); return ret; } -RTM_EXPORT(rt_driver_device_match_with_dtb); +RTM_EXPORT(rt_driver_match_with_dtb); #endif -/** - * This function removes a previously registered device driver - * - * @param dev the pointer of device driver structure - * - * @return the error code, RT_EOK on successfully. - */ -rt_err_t rt_driver_unregister(const rt_driver_t drv) -{ - /*todo*/ - return RT_EOK; -} -RTM_EXPORT(rt_driver_unregister); - -- Gitee From d048cc10246b44679aadcb1b67b464659c7d6d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E8=B6=85?= Date: Wed, 29 Jun 2022 20:00:29 +0800 Subject: [PATCH 10/10] remove the tab --- include/rtdef.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/rtdef.h b/include/rtdef.h index 55c36708b5..bdfe3cdff0 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -1131,15 +1131,15 @@ struct rt_device #define RT_DRIVER_MATCH_DTS (1<<0) struct rt_device_id { - const char *compatible; - void *data; + const char *compatible; + void *data; }; struct rt_driver { #ifdef RT_USING_DEVICE_OPS const struct rt_device_ops *dev_ops; -#endif +#endif const struct filesystem_ops *fops; const char *name; enum rt_device_class_type dev_type; @@ -1147,10 +1147,10 @@ struct rt_driver int device_size; int flag; const struct rt_device_id *dev_match; - int (*probe)(struct rt_device *dev); + int (*probe)(struct rt_device *dev); int (*probe_init)(struct rt_device *dev); int (*remove)(struct rt_device *dev); - const void *ops; /* driver-specific operations */ + const void *ops; /* driver-specific operations */ }; typedef struct rt_driver *rt_driver_t; -- Gitee