device_core.h 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2020-2021 Alibaba Group Holding Limited
 */

#ifndef AOS_DEVICE_CORE_H
#define AOS_DEVICE_CORE_H

#include <aos/kernel.h>
#include <aos/list.h>
#include <k_rbtree.h>
11 12 13 14 15
#include <aos/device.h>
#ifdef AOS_COMP_DEVFS
#include <aos/devfs.h>
#endif
#if defined(CONFIG_DRV_CORE) && CONFIG_DRV_CORE != 0
16
#include <drivers/u_ld.h>
17
#endif
18

19
struct aos_dev_ops;
20 21 22 23

typedef struct aos_dev {
    aos_dev_type_t type;
    uint32_t id;
24
    const struct aos_dev_ops *ops;
25 26
#ifdef AOS_COMP_DEVFS
    aos_devfs_node_t devfs_node;
27
#endif
28 29 30 31 32 33
    struct k_rbtree_node_t rb_node;
    aos_sem_t rb_sem;
    aos_mutex_t mutex;
    uint32_t ref_count;
} aos_dev_t;

34 35 36 37 38 39
typedef struct aos_dev_ops {
    void (*unregister)(aos_dev_t *);
    aos_status_t (*get)(aos_dev_ref_t *);
    void (*put)(aos_dev_ref_t *);
} aos_dev_ops_t;

40 41
#define aos_dev_lock(dev)               do { (void)aos_mutex_lock(&(dev)->mutex, AOS_WAIT_FOREVER); } while (0)
#define aos_dev_unlock(dev)             do { (void)aos_mutex_unlock(&(dev)->mutex); } while (0)
42 43 44 45 46 47 48
#define aos_dev_ref_is_first(ref)       ((ref)->dev->ref_count == 0)
#define aos_dev_ref_is_last(ref)        ((ref)->dev->ref_count == 0)

#ifdef __cplusplus
extern "C" {
#endif

49 50 51
#if !(defined(CONFIG_DRV_CORE) && CONFIG_DRV_CORE != 0)
aos_status_t aos_dev_core_init(void);
#endif
52 53 54 55 56 57 58 59 60
aos_status_t aos_dev_register(aos_dev_t *dev);
aos_status_t aos_dev_unregister(aos_dev_type_t type, uint32_t id);
aos_status_t aos_dev_ref(aos_dev_ref_t *ref, aos_dev_t *dev);

#ifdef __cplusplus
}
#endif

#endif /* AOS_DEVICE_CORE_H */