1 /* 2 * Copyright (C) 2020-2021 Alibaba Group Holding Limited 3 */ 4 5 #ifndef AOS_DEVICE_CORE_H 6 #define AOS_DEVICE_CORE_H 7 8 #include <aos/kernel.h> 9 #include <aos/list.h> 10 #include <k_rbtree.h> 11 #include <drivers/u_ld.h> 12 #ifdef AOS_COMP_VFS 13 #include <aos/vfs.h> 14 #endif 15 #include <aos/device.h> 16 17 struct aos_dev; 18 19 typedef struct { 20 void (*unregister)(struct aos_dev *); 21 aos_status_t (*get)(aos_dev_ref_t *); 22 void (*put)(aos_dev_ref_t *); 23 } aos_dev_ops_t; 24 25 #ifdef AOS_COMP_VFS 26 #define AOS_DEV_NAME_MAX_LEN 63 27 28 typedef struct { 29 char name[AOS_DEV_NAME_MAX_LEN + 1]; 30 const struct file_ops *ops; 31 } aos_dev_vfs_helper_t; 32 #endif 33 34 typedef struct aos_dev { 35 aos_dev_type_t type; 36 uint32_t id; 37 const aos_dev_ops_t *ops; 38 #ifdef AOS_COMP_VFS 39 aos_dev_vfs_helper_t vfs_helper; 40 #endif 41 struct k_rbtree_node_t rb_node; 42 aos_sem_t rb_sem; 43 aos_mutex_t mutex; 44 uint32_t ref_count; 45 } aos_dev_t; 46 47 #define aos_dev_lock(dev) do { (void)aos_mutex_lock(&(dev)->mutex, AOS_WAIT_FOREVER); } while (0) 48 #define aos_dev_unlock(dev) do { (void)aos_mutex_unlock(&(dev)->mutex); } while (0) 49 #define aos_dev_ref_is_first(ref) ((ref)->dev->ref_count == 0) 50 #define aos_dev_ref_is_last(ref) ((ref)->dev->ref_count == 0) 51 52 #ifdef __cplusplus 53 extern "C" { 54 #endif 55 56 aos_status_t aos_dev_register(aos_dev_t *dev); 57 aos_status_t aos_dev_unregister(aos_dev_type_t type, uint32_t id); 58 aos_status_t aos_dev_ref(aos_dev_ref_t *ref, aos_dev_t *dev); 59 60 #ifdef __cplusplus 61 } 62 #endif 63 64 #endif /* AOS_DEVICE_CORE_H */ 65