22 * Copyright (C) 2020-2021 Alibaba Group Holding Limited
33 */
44
5- #include <string.h>
65#include <aos/device_core.h>
76
87#define REF_COUNT_PENDING UINT32_MAX
1110static struct k_rbtree_root_t device_map = RBT_ROOT ;
1211static aos_mutex_t device_map_mutex ;
1312
13+ static aos_status_t device_core_init (void )
14+ {
15+ return aos_mutex_new (& device_map_mutex );
16+ }
17+
18+ #if defined(CONFIG_DRV_CORE ) && CONFIG_DRV_CORE != 0
19+ static int dev_core_init (void )
20+ {
21+ return (int )device_core_init ();
22+ }
23+ CORE_DRIVER_ENTRY (dev_core_init )
24+ #else
25+ aos_status_t aos_dev_core_init (void )
26+ {
27+ return device_core_init ();
28+ }
29+ #endif
30+
1431static void device_map_lock (void )
1532{
1633 (void )aos_mutex_lock (& device_map_mutex , AOS_WAIT_FOREVER );
@@ -96,37 +113,6 @@ static void remove_device(aos_dev_t *dev)
96113 k_rbtree_erase (& dev -> rb_node , & device_map );
97114}
98115
99- #ifdef AOS_COMP_VFS
100- static aos_status_t add_to_vfs (aos_dev_t * dev )
101- {
102- const char prefix [] = "/dev/" ;
103- char path [sizeof (prefix ) - 1 + sizeof (dev -> vfs_helper .name )];
104-
105- if (dev -> vfs_helper .name [0 ] == '\0' || !dev -> vfs_helper .ops )
106- return 0 ;
107-
108- memcpy (path , prefix , sizeof (prefix ) - 1 );
109- strncpy (& path [sizeof (prefix ) - 1 ], dev -> vfs_helper .name , sizeof (dev -> vfs_helper .name ) - 1 );
110- path [sizeof (path ) - 1 ] = '\0' ;
111-
112- return aos_register_driver (path , dev -> vfs_helper .ops , dev );
113- }
114-
115- static void remove_from_vfs (aos_dev_t * dev )
116- {
117- const char prefix [] = "/dev/" ;
118- char path [sizeof (prefix ) - 1 + sizeof (dev -> vfs_helper .name )];
119-
120- if (dev -> vfs_helper .name [0 ] == '\0' || !dev -> vfs_helper .ops )
121- return ;
122-
123- memcpy (path , prefix , sizeof (prefix ) - 1 );
124- strncpy (& path [sizeof (prefix ) - 1 ], dev -> vfs_helper .name , sizeof (dev -> vfs_helper .name ) - 1 );
125- path [sizeof (path ) - 1 ] = '\0' ;
126- (void )aos_unregister_driver (path );
127- }
128- #endif /* AOS_COMP_VFS */
129-
130116aos_status_t aos_dev_register (aos_dev_t * dev )
131117{
132118 aos_status_t ret ;
@@ -157,15 +143,17 @@ aos_status_t aos_dev_register(aos_dev_t *dev)
157143 insert_device (dev );
158144 device_map_unlock ();
159145
160- #ifdef AOS_COMP_VFS
161- ret = add_to_vfs (dev );
162- if (ret ) {
163- device_map_lock ();
164- remove_device (dev );
165- device_map_unlock ();
166- aos_mutex_free (& dev -> mutex );
167- aos_sem_free (& dev -> rb_sem );
168- return ret ;
146+ #ifdef AOS_COMP_DEVFS
147+ if (aos_devfs_node_is_valid (& dev -> devfs_node )) {
148+ ret = aos_devfs_add_node (& dev -> devfs_node );
149+ if (ret ) {
150+ device_map_lock ();
151+ remove_device (dev );
152+ device_map_unlock ();
153+ aos_mutex_free (& dev -> mutex );
154+ aos_sem_free (& dev -> rb_sem );
155+ return ret ;
156+ }
169157 }
170158#endif
171159
@@ -179,7 +167,6 @@ aos_status_t aos_dev_register(aos_dev_t *dev)
179167aos_status_t aos_dev_unregister (aos_dev_type_t type , uint32_t id )
180168{
181169 aos_dev_t * dev ;
182- aos_status_t ret ;
183170
184171 device_map_lock ();
185172 dev = find_device (type , id );
@@ -206,8 +193,9 @@ aos_status_t aos_dev_unregister(aos_dev_type_t type, uint32_t id)
206193
207194 aos_dev_unlock (dev );
208195 device_allow_removal (dev );
209- #ifdef AOS_COMP_VFS
210- remove_from_vfs (dev );
196+ #ifdef AOS_COMP_DEVFS
197+ if (aos_devfs_node_is_valid (& dev -> devfs_node ))
198+ (void )aos_devfs_remove_node (& dev -> devfs_node );
211199#endif
212200 device_map_lock ();
213201 device_wait_removal (dev );
@@ -302,10 +290,3 @@ void aos_dev_put(aos_dev_ref_t *ref)
302290 aos_dev_unlock (ref -> dev );
303291 aos_dev_ref_init (ref );
304292}
305-
306- static int device_core_init (void )
307- {
308- return (int )aos_mutex_new (& device_map_mutex );
309- }
310-
311- CORE_DRIVER_ENTRY (device_core_init )
0 commit comments