• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: OS Abstract Layer.
15  */
16 
17 /**
18  * @defgroup osal_device osal_device
19  */
20 #ifndef __OSAL_DEVICE_H__
21 #define __OSAL_DEVICE_H__
22 
23 #include "schedule/osal_wait.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 #define OSAL_POLLIN 0x0001U
32 #define OSAL_POLLPRI 0x0002U
33 #define OSAL_POLLOUT 0x0004U
34 #define OSAL_POLLERR 0x0008U
35 #define OSAL_POLLHUP 0x0010U
36 #define OSAL_POLLNVAL 0x0020U
37 #define OSAL_POLLRDNORM 0x0040U
38 #define OSAL_POLLRDBAND 0x0080U
39 #define OSAL_POLLWRNORM 0x0100U
40 
41 typedef struct osal_poll_ {
42     void *poll_table;
43     void *data;
44 } osal_poll;
45 
46 typedef struct osal_ioctl_cmd_ {
47     unsigned int cmd;
48     int (*handler)(unsigned int cmd, void *arg, void *private_data);
49 } osal_ioctl_cmd;
50 
51 typedef struct osal_vm_ {
52     void *vm;
53 } osal_vm;
54 
55 typedef struct osal_fileops_ {
56     int (*open)(void *private_data);
57     int (*read)(char *buf, int size, long *offset, void *private_data);
58     int (*write)(const char *buf, int size, long *offset, void *private_data);
59     long (*llseek)(long offset, int whence, void *private_data);
60     int (*release)(void *private_data);
61     unsigned int (*poll)(osal_poll *osal_poll, void *private_data);
62     int (*mmap)(osal_vm *vm, unsigned long start, unsigned long end, unsigned long vm_pgoff, void *private_data);
63     int (*fasync)(int fd, void *filp, int mode);
64     osal_ioctl_cmd *cmd_list;
65     unsigned int cmd_cnt;
66 } osal_fileops;
67 
68 typedef struct osal_pmops_ {
69     int (*pm_suspend)(void *private_data);
70     int (*pm_resume_early)(void *private_data);
71     int (*pm_resume)(void *private_data);
72     int (*pm_lowpower_enter)(void *private_data);
73     int (*pm_lowpower_exit)(void *private_data);
74     int (*pm_poweroff)(void *private_data);
75     void *private_data;
76 } osal_pmops;
77 
78 #define OSAL_DEV_NAME_LEN 32
79 typedef struct osal_dev_ {
80     char name[OSAL_DEV_NAME_LEN];
81     int minor;
82     unsigned int parent_minor;
83     osal_fileops *fops;
84     osal_pmops *pmops;
85     void *dev;
86     void *owner;
87 } osal_dev;
88 
89 #define OSAL_NOCACHE 0
90 #define OSAL_CACHE 1
91 
92 typedef enum osal_kobject_action_ {
93     OSAL_KOBJ_ADD,
94     OSAL_KOBJ_REMOVE,
95     OSAL_KOBJ_CHANGE,
96     OSAL_KOBJ_MOVE,
97     OSAL_KOBJ_ONLINE,
98     OSAL_KOBJ_OFFLINE,
99     OSAL_KOBJ_BIND,
100     OSAL_KOBJ_UNBIND,
101     OSAL_KOBJ_MAX
102 } osal_kobject_action;
103 
104 /**
105  * @ingroup osal_device
106  * @brief Invoke the low-power callback function of all devices.
107  *
108  * @par Description:
109  * Invoke the low-power callback function of all devices,
110  *
111  * @par Support System:
112  * linux.
113  */
114 void osal_pm_lowpower_enter(void);
115 
116 /**
117  * @ingroup osal_device
118  * @brief Invoke the low-power-exit callback function of all devices.
119  *
120  * @par Description:
121  * Invoke the low-power-exit callback function of all devices.
122  *
123  * @par Support System:
124  * linux.
125  */
126 void osal_pm_lowpower_exit(void);
127 
128 /**
129  * @ingroup osal_device
130  * @brief Returns a pointer of the osal_dev type for which memory is applied.
131  *
132  * @attention Must be freed with osal_dev_destroy.
133  *
134  * @param name [in] The name of device.
135  *
136  * @return Pointer to osal_dev.
137  *
138  * @par Support System:
139  * linux liteos freertos.
140  */
141 osal_dev *osal_dev_create(const char *name);
142 
143 /**
144  * @ingroup osal_device
145  * @brief Free the dev's memory.
146  *
147  * @par Description:
148  * Free the dev's memory that created by osal_dev_create.
149  * @param dev [in] The result of osal_dev_create.
150  * @attention this api may free dev,it should be from osal_dev_create.caller should set NULL to dev after call api
151  *
152  * @return OSAL_SUCCESS/OSAL_FAILURE
153  *
154  * @par Support System:
155  * linux liteos freertos.
156  */
157 int osal_dev_destroy(osal_dev *dev);
158 
159 /**
160  * @ingroup osal_device
161  * @brief register device.
162  *
163  * @attention The input pool parameter must be created by func osal_dev_create.
164  *
165  * @return OSAL_SUCCESS/OSAL_FAILURE
166  *
167  * @par Support System:
168  * linux liteos freertos.
169  */
170 int osal_dev_register(osal_dev *dev);
171 
172 /**
173  * @ingroup osal_device
174  * @brief unregister device.
175  *
176  * @attention The input pool parameter must be created by func osal_dev_create.
177  *
178  * @par Support System:
179  * linux liteos freertos.
180  */
181 void osal_dev_unregister(osal_dev *dev);
182 
183 /**
184  * @ingroup osal_device
185  * @brief Set the device to wake up asynchronously.
186  *
187  * @par Description:
188  * Set the device to wake up asynchronously.
189  *
190  * @param minor [in] The minor of device.
191  *
192  * @par Support System:
193  * linux.
194  */
195 void osal_device_set_async(unsigned int minor);
196 
197 /**
198  * @ingroup osal_device
199  * @brief Adds the current process to the wait list specified by the wait parameter.
200  *
201  * @par Support System:
202  * linux liteos.
203  */
204 void osal_poll_wait(osal_poll *table, osal_wait *wait);
205 
206 /**
207  * @ingroup osal_device
208  * @brief Wakes up the current process from the wait list specified by the wait parameter.
209  *
210  * @par Support System:
211  * liteos.
212  */
213 void osal_notify_poll(osal_wait *wait);
214 
215 /**
216  * @ingroup osal_device
217  * @brief remap kernel memory to userspace.
218  *
219  * @return The value 0 indicates success, and other values indicate failure.
220  *
221  * @par Support System:
222  * linux liteos.
223  */
224 int osal_remap_pfn_range(osal_vm *vm, unsigned long addr, unsigned long pfn, unsigned long size);
225 
226 /**
227  * @ingroup osal_device
228  * @brief try to freeze the current task.
229  *
230  * @return True is returned for success and false is returned for failure.
231  *
232  * @par Support System:
233  * linux liteos.
234  */
235 int osal_try_to_freeze(void);
236 
237 /**
238  * @ingroup osal_device
239  * @brief make current task freezable and try to freeze the current task.
240  *
241  * @return True is returned for success and false is returned for failure.
242  *
243  * @par Support System:
244  * linux liteos.
245  */
246 int osal_set_freezable(void);
247 
248 /**
249  * @ingroup osal_device
250  * @brief send an uevent with environmental data.
251  *
252  * @return Returns 0 if kobject_uevent_env() is completed with success or the corresponding error when it fails.
253  *
254  * @par Support System:
255  * linux liteos.
256  */
257 int osal_kobject_uevent_env(osal_dev *dev, osal_kobject_action action, char *envp[]);
258 
259 /**
260  * @ingroup osal_device
261  * @brief Setting up the character device driver's fasync queue.
262  *
263  * @par Description:
264  * Setting up the character device driver's fasync queue.
265  *
266  * @return It returns negative on error, 0 if it did no changes and positive if it added/deleted the entry.
267  *
268  * @par Support System:
269  * linux.
270  */
271 int osal_fasync_helper(int fd, void *filp, int mode, void **fapp);
272 
273 /**
274  * @ingroup osal_device
275  *
276  * @brief Send asynchronous notifications.
277  *
278  * @par Description:
279  * The SIGIO signal is sent to the application layer,
280  * and the application layer triggers the function corresponding to the SIGIO signal.
281  *
282  * @par Support System:
283  * linux.
284  */
285 void osal_fasync_notify(void **fapp, int sig, int band);
286 
287 /**
288  * @ingroup osal_device
289  * @brief get nocached prot_page.
290  *
291  * @par Description:
292  * get nocached prot_page.
293  *
294  * @par Support System:
295  * linux.
296  */
297 void osal_pgprot_noncached(osal_vm *vm);
298 
299 /**
300  * @ingroup osal_device
301  * @brief get cached prot_page.
302  *
303  * @par Description:
304  * get cached prot_page.
305  *
306  * @par Support System:
307  * linux.
308  */
309 void osal_pgprot_cached(osal_vm *vm);
310 
311 /**
312  * @ingroup osal_device
313  * @brief get writecombine prot_page.
314  *
315  * @par Description:
316  * get writecombine prot_page.
317  *
318  * @par Support System:
319  * linux.
320  */
321 void osal_pgprot_writecombine(osal_vm *vm);
322 
323 typedef struct {
324     unsigned long a0;
325     unsigned long a1;
326     unsigned long a2;
327     unsigned long a3;
328     unsigned long a4;
329     unsigned long a5;
330     unsigned long a6;
331     unsigned long a7;
332 } osal_smccc_info;
333 
334 typedef struct {
335     unsigned long a0;
336     unsigned long a1;
337     unsigned long a2;
338     unsigned long a3;
339 } osal_smccc_res;
340 
341 /**
342  * @ingroup osal_device
343  * @brief make SMC calls.
344  *
345  * @par Description:
346  * This function is used to make SMC calls following SMC Calling Convention.
347  *
348  * @par Support System:
349  * linux.
350  */
351 void osal_smccc_smc(const osal_smccc_info *info, osal_smccc_res *res);
352 
353 /**
354  * @ingroup osal_device
355  * @brief open_device.
356  *
357  * @par Description:
358  * Invoke the callback function open set by the caller.
359  *
360  * @return return the device file's fd.
361  *
362  * @par Support System:
363  * linux(userspace).
364  */
365 int osal_opendev(const char *path, int flag, ...);
366 
367 /**
368  * @ingroup osal_device
369  * @brief close device.
370  *
371  * @par Description:
372  * Call the callback function release given by caller.
373  *
374  * @return OSAL_SUCCESS/OSAL_FAILURE
375  *
376  * @par Support System:
377  * linux(userspace).
378  */
379 int osal_closedev(int fd);
380 
381 /**
382  * @ingroup osal_device
383  * @brief read device.
384  *
385  * @par Description:
386  * Call the callback function read given by caller.
387  *
388  * @return return the callback functon's return value, or -1 when fd is incorrect.
389  *
390  * @par Support System:
391  * linux(userspace).
392  */
393 int osal_readdev(int fd, void *buf, unsigned long count);
394 
395 /**
396  * @ingroup osal_device
397  * @brief write device.
398  *
399  * @par Description:
400  * Call the callback function write given by caller.
401  *
402  * @return return the callback functon's return value, or -1 when fd is incorrect.
403  *
404  * @par Support System:
405  * linux(userspace).
406  */
407 int osal_writedev(int fd, const void *buf, unsigned long count);
408 
409 /**
410  * @ingroup osal_device
411  * @brief Perform other operations on the device.
412  *
413  * @par Description:
414  * Call the callback function osal_ioctl_cmd->handler given by caller.
415  *
416  * @return return the callback functon's return value, or -1 when fd is incorrect.
417  *
418  * @par Support System:
419  * linux(userspace).
420  */
421 int osal_ioctldev(int fd, unsigned int cmd, ...);
422 
423 // //////////////////////////// userspace interface //////////////////////////////
424 /**
425  * @ingroup osal_device
426  * @brief osal linux userspace init.
427  *
428  * @par Description:
429  * osal linux userspace init.
430  *
431  * @return OSAL_SUCCESS/OSAL_FAILURE
432  *
433  * @par Support System:
434  * linux(userspace).
435  */
436 int osal_init(void);
437 
438 /**
439  * @ingroup osal_device
440  * @brief osal linux userspace exit.
441  *
442  * @par Description:
443  * osal linux userspace exit.
444  *
445  * @par Support System:
446  * linux(userspace).
447  */
448 void osal_exit(void);
449 
450 #ifdef __cplusplus
451 #if __cplusplus
452 }
453 #endif
454 #endif
455 #endif /* __OSAL_DEVICE_H__ */