1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _AOS_DEVICE_H
6 #define _AOS_DEVICE_H
7 
8 #include <stdint.h>
9 #include <stddef.h>
10 #include <assert.h>
11 
12 #include "object.h"
13 
14 #include "aos/kernel.h"
15 #include "aos/vfs.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 //#define AOS_USING_DEVICE_OPS
22 
23 #define DEV_ASSERT(test)
24 #define AOS_DEVICE(device)            ((aos_device_t)device)
25 
26 /**
27  * device flags defitions
28  */
29 #define AOS_DEVICE_FLAG_DEACTIVATE       0x000           /**< device is not not initialized */
30 
31 #define AOS_DEVICE_FLAG_RDONLY           0x001           /**< read only */
32 #define AOS_DEVICE_FLAG_WRONLY           0x002           /**< write only */
33 #define AOS_DEVICE_FLAG_RDWR             0x003           /**< read and write */
34 
35 #define AOS_DEVICE_FLAG_REMOVABLE        0x004           /**< removable device */
36 #define AOS_DEVICE_FLAG_STANDALONE       0x008           /**< standalone device */
37 #define AOS_DEVICE_FLAG_ACTIVATED        0x010           /**< device is activated */
38 #define AOS_DEVICE_FLAG_SUSPENDED        0x020           /**< device is suspended */
39 #define AOS_DEVICE_FLAG_STREAM           0x040           /**< stream mode */
40 
41 #define AOS_DEVICE_FLAG_INT_RX           0x100           /**< INT mode on Rx */
42 #define AOS_DEVICE_FLAG_DMA_RX           0x200           /**< DMA mode on Rx */
43 #define AOS_DEVICE_FLAG_INT_TX           0x400           /**< INT mode on Tx */
44 #define AOS_DEVICE_FLAG_DMA_TX           0x800           /**< DMA mode on Tx */
45 
46 #define AOS_DEVICE_OFLAG_CLOSE           0x000           /**< device is closed */
47 #define AOS_DEVICE_OFLAG_RDONLY          0x001           /**< read only access */
48 #define AOS_DEVICE_OFLAG_WRONLY          0x002           /**< write only access */
49 #define AOS_DEVICE_OFLAG_RDWR            0x003           /**< read and write */
50 #define AOS_DEVICE_OFLAG_OPEN            0x008           /**< device is opened */
51 #define AOS_DEVICE_OFLAG_MASK            0xf0f           /**< mask of open flag */
52 
53 #define AOS_DEVICE_CTRL_CHAR_STREAM           0x10            /**< stream mode on char device */
54 #define AOS_DEVICE_CTRL_BLK_GETGEOME          0x10            /**< get geometry information   */
55 #define AOS_DEVICE_CTRL_BLK_SYNC              0x11            /**< flush data to block device */
56 #define AOS_DEVICE_CTRL_BLK_ERASE             0x12            /**< erase block on block device */
57 #define AOS_DEVICE_CTRL_BLK_AUTOREFRESH       0x13            /**< block device : enter/exit auto refresh mode */
58 #define AOS_DEVICE_CTRL_NETIF_GETMAC          0x10            /**< get mac address */
59 #define AOS_DEVICE_CTRL_MTD_FORMAT            0x10            /**< format a MTD device */
60 #define AOS_DEVICE_CTRL_RTC_GET_TIME          0x10            /**< get time */
61 #define AOS_DEVICE_CTRL_RTC_SET_TIME          0x11            /**< set time */
62 #define AOS_DEVICE_CTRL_RTC_GET_ALARM         0x12            /**< get alarm */
63 #define AOS_DEVICE_CTRL_RTC_SET_ALARM         0x13            /**< set alarm */
64 
65 enum aos_device_class_type
66 {
67     AOS_Device_Class_Char = 0,                           /**< character device */
68     AOS_Device_Class_Block,                              /**< block device */
69     AOS_Device_Class_NetIf,                              /**< net interface */
70     AOS_Device_Class_MTD,                                /**< memory device */
71     AOS_Device_Class_CAN,                                /**< CAN device */
72     AOS_Device_Class_RTC,                                /**< RTC device */
73     AOS_Device_Class_Sound,                              /**< Sound device */
74     AOS_Device_Class_Graphic,                            /**< Graphic device */
75     AOS_Device_Class_I2CBUS,                             /**< I2C bus device */
76     AOS_Device_Class_USBDevice,                          /**< USB slave device */
77     AOS_Device_Class_USBHost,                            /**< USB host bus */
78     AOS_Device_Class_SPIBUS,                             /**< SPI bus device */
79     AOS_Device_Class_SPIDevice,                          /**< SPI device */
80     AOS_Device_Class_SDIO,                               /**< SDIO bus device */
81     AOS_Device_Class_PM,                                 /**< PM pseudo device */
82     AOS_Device_Class_Pipe,                               /**< Pipe device */
83     AOS_Device_Class_Portal,                             /**< Portal device */
84     AOS_Device_Class_Timer,                              /**< Timer device */
85     AOS_Device_Class_Miscellaneous,                      /**< Miscellaneous device */
86     AOS_Device_Class_Sensor,                             /**< Sensor device */
87     AOS_Device_Class_Touch,                              /**< Touch device */
88     AOS_Device_Class_Unknown                             /**< unknown device */
89 };
90 
91 typedef struct aos_device * aos_device_t;
92 
93 /**
94  * Device structure
95  */
96 struct aos_device
97 {
98     struct aos_object          parent;                   /**< inherit from aos_object */
99 
100     enum aos_device_class_type type;                     /**< device type */
101     uint16_t                   flag;                     /**< device flag */
102     uint16_t                   open_flag;                /**< device open flag */
103 
104     uint8_t                ref_count;                /**< reference count */
105     uint8_t                device_id;                /**< 0 - 255 */
106 
107     /* device call back */
108     int (*rx_indicate)(aos_device_t dev, size_t size);
109     int (*tx_complete)(aos_device_t dev, void *buffer);
110 
111 #ifdef AOS_USING_DEVICE_OPS
112     const struct aos_device_ops *ops;
113 #else
114     /* common device interface */
115     int  (*init)     (aos_device_t dev);
116     int  (*open)     (aos_device_t dev, uint16_t oflag);
117     int  (*close)    (aos_device_t dev);
118     size_t (*read)   (aos_device_t dev, long pos, void *buffer, size_t size);
119     size_t (*write)  (aos_device_t dev, long pos, const void *buffer, size_t size);
120     int    (*control)(aos_device_t dev, int cmd, void *args);
121 #endif
122 
123 #if defined(AOS_USING_POSIX)
124     const struct file_ops *fops;
125 #endif
126 
127     void                     *user_data;                /**< device private data */
128     void                     *user_data2;                /**< device private data2 */
129 
130     void                     *wait_queue;
131 };
132 
133 #ifdef AOS_USING_DEVICE_OPS
134 struct aos_device_ops
135 {
136     /* common device interface */
137     long  (*init)   (aos_device_t dev);
138     long  (*open)   (aos_device_t dev, uint16_t oflag);
139     long  (*close)  (aos_device_t dev);
140     size_t (*read)   (aos_device_t dev, off_t pos, void *buffer, size_t size);
141     size_t (*write)  (aos_device_t dev, off_t pos, const void *buffer, size_t size);
142     long  (*control)(aos_device_t dev, int cmd, void *args);
143 };
144 #endif
145 
146 /**
147  * This function registers a device driver with specified name.
148  *
149  * @param dev the pointer of device driver structure
150  * @param name the device driver's name
151  * @param flags the capabilities flag of device
152  *
153  * @return the error code, 0 on initialization successfully.
154  */
155 int aos_device_register(aos_device_t dev,
156                         const char *name,
157                         uint16_t flags);
158 
159 /**
160  * This function removes a previously registered device driver
161  *
162  * @param dev the pointer of device driver structure
163  *
164  * @return the error code, 0 on successfully.
165  */
166 int aos_device_unregister(aos_device_t dev);
167 
168 /**
169  * This function finds a device driver by specified name.
170  *
171  * @param name the device driver's name
172  *
173  * @return the registered device driver on successful, or NULL on failure.
174  */
175 aos_device_t aos_device_find(const char *name);
176 
177 /**
178  * This function creates a device object with user data size.
179  *
180  * @param type, the kind type of this device object.
181  * @param attach_size, the size of user data.
182  *
183  * @return the allocated device object, or NULL when failed.
184  */
185 aos_device_t aos_device_create(int type, int attach_size);
186 
187 /**
188  * This function destroy the specific device object.
189  *
190  * @param dev, the specific device object.
191  */
192 void aos_device_destroy(aos_device_t dev);
193 
194 /**
195  * This function will initialize the specified device
196  *
197  * @param dev the pointer of device driver structure
198  *
199  * @return the result
200  */
201 int aos_device_init(aos_device_t dev);
202 
203 /**
204  * This function will open a device
205  *
206  * @param dev the pointer of device driver structure
207  * @param oflag the flags for device open
208  *
209  * @return the result
210  */
211 int aos_device_open(aos_device_t dev, uint16_t oflag);
212 
213 /**
214  * This function will close a device
215  *
216  * @param dev the pointer of device driver structure
217  *
218  * @return the result
219  */
220 int aos_device_close(aos_device_t dev);
221 
222 /**
223  * This function will read some data from a device.
224  *
225  * @param dev the pointer of device driver structure
226  * @param pos the position of reading
227  * @param buffer the data buffer to save read data
228  * @param size the size of buffer
229  *
230  * @return the actually read size on successful, otherwise negative returned.
231  *
232  * @note since 0.4.0, the unit of size/pos is a block for block device.
233  */
234 size_t aos_device_read(aos_device_t dev,
235                        long    pos,
236                        void       *buffer,
237                        size_t   size);
238 
239 /**
240  * This function will write some data to a device.
241  *
242  * @param dev the pointer of device driver structure
243  * @param pos the position of written
244  * @param buffer the data buffer to be written to device
245  * @param size the size of buffer
246  *
247  * @return the actually written size on successful, otherwise negative returned.
248  *
249  * @note since 0.4.0, the unit of size/pos is a block for block device.
250  */
251 size_t aos_device_write(aos_device_t dev,
252                        long    pos,
253                        const void *buffer,
254                        size_t   size);
255 
256 /**
257  * This function will perform a variety of control functions on devices.
258  *
259  * @param dev the pointer of device driver structure
260  * @param cmd the command sent to device
261  * @param arg the argument of command
262  *
263  * @return the result
264  */
265 int aos_device_control(aos_device_t dev, int cmd, void *arg);
266 
267 
268 /**
269  * This function will set the reception indication callback function. This callback function
270  * is invoked when this device receives data.
271  *
272  * @param dev the pointer of device driver structure
273  * @param rx_ind the indication callback function
274  *
275  * @return 0
276  */
277 int aos_device_set_rx_indicate(aos_device_t dev,
278                                int (*rx_ind)(aos_device_t dev, size_t size));
279 
280 /**
281  * This function will set the indication callback function when device has
282  * written data to physical hardware.
283  *
284  * @param dev the pointer of device driver structure
285  * @param tx_done the indication callback function
286  *
287  * @return 0
288  */
289 int aos_device_set_tx_complete(aos_device_t dev,
290                                int (*tx_done)(aos_device_t dev, void *buffer));
291 
292 
293 #ifdef __cplusplus
294 }
295 #endif
296 
297 
298 #endif
299