1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <linux/fs.h>
20 #include <asm/uaccess.h>
21 #include "hi_osal.h" /* because of ioctl redefine, hi_osal.h should not be the first included file */
22
klib_fopen(const char * filename,int flags,int mode)23 struct file *klib_fopen(const char *filename, int flags, int mode)
24 {
25 struct file *filp = filp_open(filename, flags, mode);
26 return (IS_ERR(filp)) ? NULL : filp;
27 }
28
klib_fclose(struct file * filp)29 void klib_fclose(struct file *filp)
30 {
31 if (filp != NULL) {
32 filp_close(filp, NULL);
33 }
34 return;
35 }
36
klib_fwrite(const char * buf,int len,struct file * filp)37 int klib_fwrite(const char *buf, int len, struct file *filp)
38 {
39 if (filp == NULL) {
40 return -ENOENT;
41 }
42
43 return __kernel_write(filp, buf, len, &filp->f_pos);
44 }
45
klib_fread(char * buf,unsigned int len,struct file * filp)46 int klib_fread(char *buf, unsigned int len, struct file *filp)
47 {
48 if (filp == NULL) {
49 return -ENOENT;
50 }
51
52 return kernel_read(filp, (void __user*)buf, len, &filp->f_pos);
53 }
54
osal_klib_fopen(const char * filename,int flags,int mode)55 void *osal_klib_fopen(const char *filename, int flags, int mode)
56 {
57 return (void *)klib_fopen(filename, flags, mode);
58 }
59 EXPORT_SYMBOL(osal_klib_fopen);
60
osal_klib_fclose(void * filp)61 void osal_klib_fclose(void *filp)
62 {
63 klib_fclose((struct file *)filp);
64 }
65 EXPORT_SYMBOL(osal_klib_fclose);
66
osal_klib_fwrite(const char * buf,int len,void * filp)67 int osal_klib_fwrite(const char *buf, int len, void *filp)
68 {
69 return klib_fwrite(buf, len, (struct file *)filp);
70 }
71 EXPORT_SYMBOL(osal_klib_fwrite);
72
osal_klib_fread(char * buf,unsigned int len,void * filp)73 int osal_klib_fread(char *buf, unsigned int len, void *filp)
74 {
75 return klib_fread(buf, len, (struct file *)filp);
76 }
77 EXPORT_SYMBOL(osal_klib_fread);
78
79