1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _DEVICE_VFS_H_
6 #define _DEVICE_VFS_H_
7 
8 #include <errno.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 #include <aos/kernel.h>
14 #include <drivers/mutex.h>
15 #include <drivers/u_ld.h>
16 #include <drivers/platform/u_platform_bus.h>
17 
18 #define __weak __attribute__((weak))
19 
20 #ifndef USER_SPACE_DRIVER
aos_ipc_copy(void * dst,void * src,size_t len)21 static inline void *aos_ipc_copy(void *dst, void *src, size_t len) {
22 
23     memcpy(dst, src, len);
24     return dst;
25 }
26 #endif
27 
28 /**
29  * subsys device type list
30  */
31 enum SUBSYS_BUS_TYPE {
32     BUS_TYPE_PLATFORM,
33     BUS_TYPE_SPI,
34     BUS_TYPE_I2C,
35     BUS_TYPE_USB,
36     BUS_TYPE_SDIO,
37     BUS_TYPE_MAX
38 };
39 
40 typedef struct file_ops subsys_file_ops_t;
41 
42 /**
43  * type and name are mandaotry, type is set to BUS_TYPE_PLATFORM if no bus is defined in "enum SUBSYS_BUS_TYPE"
44  * delay_init should be set to 1 if the application scenario is time sensitive
45  * if delay_init is set to true
46  *  * subsys_drv's init/probe won't be called during driver init procedure;
47  *  * init/probe will be called only when open request comes
48  */
49 struct subsys_dev {
50     int permission;
51     bool delay_init;
52     enum SUBSYS_BUS_TYPE type;
53     void *user_data;
54     char *node_name;
55 };
56 
57 /**
58  * drv_name - subsystem driver name
59  *
60  * here we provide 2 ind of driver ops:
61  *  1. legacy driver API - for legacy rtos driver ops
62  *  2. linux-like drivers - for drivers' written with concept of Linux
63  * user can select either 1st or 2nd way, when both ops sets are provided, legacy driver will be used
64  *
65  * init - driver's initialization function callback, this API is called only once per device
66  * deinit - driver's deinitialization function callback, this API is called only when device is removed when calling aos_dev_unreg
67  * pm - for power managerment function
68  *
69  * probe - same function as init
70  * remove - same function as deinit
71  * shutdown/suspend/resume - power management related behavior's API
72  */
73 struct subsys_drv {
74     char *drv_name;
75 
76     /* for legacy drivers */
77     int (*init)(struct u_platform_device *_dev);
78     int (*deinit)(struct u_platform_device *_dev);
79     int (*pm)(struct u_platform_device *dev, u_pm_ops_t state);
80 
81     /* for linux-like drivers */
82     int (*probe)(struct u_platform_device *);
83     int (*remove)(struct u_platform_device *);
84     void (*shutdown)(struct u_platform_device *);
85     int (*suspend)(struct u_platform_device *, int /*pm_message_t */state);
86     int (*resume)(struct u_platform_device *);
87 };
88 
89 /**
90  *
91  * register devices and corresponding driver into device/driver module
92  *
93  * @param sdev  - device identifier, name and type must be carefully set
94  * @param sfops - file operations API
95  * @param sdrv  - device driver operations callback
96  *              - init/probe will be called when device exist
97  *              - deinit/remove will be called when aos_dev_unreg is called
98  * @return 0 if device register success; return negative error no. if device register fails
99  */
100 int aos_dev_reg(struct subsys_dev *sdev, subsys_file_ops_t *sfops, struct subsys_drv* sdrv);
101 
102 /**
103  * This function is not used for the moment
104  * register remote devices/driver into device/driver module, only support rpc mode
105  *
106  * @param sdev  - device identifier, name and type must be carefully set
107  * @param sfops - file operations API
108  * @param sdrv  - device driver operations callback
109  *              - probe will be called when device exist
110  *              - remove will be called when aos_dev_unreg is called
111  * @return 0 if device register success; return negative error no. if device register fails
112  */
113 int aos_remote_dev_reg(struct subsys_dev *sdev, subsys_file_ops_t *sfops, struct subsys_drv* sdrv);
114 
115 /**
116  *
117  * register device array
118  *
119  * @param sdev pointer to devices array be register
120  * @param size device array size
121  * @param sfops file operations API
122  * @param sdrv  - device driver operations callback
123  *              - probe will be called when device exist
124  *              - remove will be called when aos_dev_unreg is called
125  * @return 0 for success; negative for failure
126  */
127 int aos_devs_reg(struct subsys_dev *sdev[], int size, subsys_file_ops_t *sfops, struct subsys_drv* sdrv);
128 
129 /**
130  * unregister device driver
131  *
132  * @param sdev - device identifier, name and type must be carefully set
133  *
134  * @return 0 if device register success; return negative error no. if device register fails
135  */
136 int aos_dev_unreg(struct subsys_dev *sdev);
137 
138 /**
139  * unregister device array
140  *
141  * @param sdev - pointer to devices array be register
142  * @param size - device array size
143  *
144  * @return 0 if device register success; return negative error no. if device register fails
145  */
146 int aos_devs_unreg(struct subsys_dev *sdev[], int size);
147 
148 #endif //_DEVICE_VFS_H_
149