1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "virtual_serial.h"
33 #include "fcntl.h"
34 #ifdef LOSCFG_FILE_MODE
35 #include "stdarg.h"
36 #endif
37 #ifdef LOSCFG_FS_VFS
38 #include "console.h"
39 #include "fs/driver.h"
40 #endif
41
42 STATIC volatile UINT32 g_serialType = 0;
43 STATIC struct file g_serialFilep;
44
45
SerialTypeGet(VOID)46 UINT32 SerialTypeGet(VOID)
47 {
48 return g_serialType;
49 }
50
SerialTypeSet(const CHAR * deviceName)51 STATIC VOID SerialTypeSet(const CHAR *deviceName)
52 {
53 if (!strncmp(deviceName, SERIAL_UARTDEV, strlen(SERIAL_UARTDEV))) {
54 g_serialType = SERIAL_TYPE_UART_DEV;
55 } else if (!strncmp(deviceName, SERIAL_TTYGS0, strlen(SERIAL_TTYGS0))) {
56 g_serialType = SERIAL_TYPE_USBTTY_DEV;
57 }
58 }
59
SerialOpen(struct file * filep)60 STATIC INT32 SerialOpen(struct file *filep)
61 {
62 INT32 ret;
63 struct file *privFilep = NULL;
64 const struct file_operations_vfs *fileOps = NULL;
65
66 ret = GetFilepOps(filep, &privFilep, &fileOps);
67 if (ret != ENOERR) {
68 ret = EINVAL;
69 goto ERROUT;
70 }
71
72 ret = FilepOpen(privFilep, fileOps);
73 if (ret < 0) {
74 ret = EPERM;
75 goto ERROUT;
76 }
77
78 if (g_serialType == SERIAL_TYPE_UART_DEV) {
79 HalIrqUnmask(NUM_HAL_INTERRUPT_UART);
80 }
81 return ENOERR;
82
83 ERROUT:
84 set_errno(ret);
85 return VFS_ERROR;
86 }
87
SerialClose(struct file * filep)88 STATIC INT32 SerialClose(struct file *filep)
89 {
90 (VOID)filep;
91
92 if (g_serialType == SERIAL_TYPE_UART_DEV) {
93 HalIrqMask(NUM_HAL_INTERRUPT_UART);
94 }
95 #if defined(LOSCFG_DRIVERS_USB_SERIAL_GADGET) || defined(LOSCFG_DRIVERS_USB_ETH_SER_GADGET)
96 else if (g_serialType == SERIAL_TYPE_USBTTY_DEV) {
97 userial_mask_set(0);
98 }
99 #endif
100
101 return ENOERR;
102 }
103
SerialRead(struct file * filep,CHAR * buffer,size_t bufLen)104 STATIC ssize_t SerialRead(struct file *filep, CHAR *buffer, size_t bufLen)
105 {
106 INT32 ret;
107 struct file *privFilep = NULL;
108 const struct file_operations_vfs *fileOps = NULL;
109
110 ret = GetFilepOps(filep, &privFilep, &fileOps);
111 if (ret != ENOERR) {
112 ret = -EINVAL;
113 goto ERROUT;
114 }
115
116 ret = FilepRead(privFilep, fileOps, buffer, bufLen);
117 if (ret < 0) {
118 goto ERROUT;
119 }
120 return ret;
121
122 ERROUT:
123 set_errno(-ret);
124 return VFS_ERROR;
125 }
126
127 /* Note: do not add print function in this module! */
SerialWrite(struct file * filep,const CHAR * buffer,size_t bufLen)128 STATIC ssize_t SerialWrite(struct file *filep, const CHAR *buffer, size_t bufLen)
129 {
130 INT32 ret;
131 struct file *privFilep = NULL;
132 const struct file_operations_vfs *fileOps = NULL;
133
134 ret = GetFilepOps(filep, &privFilep, &fileOps);
135 if (ret != ENOERR) {
136 ret = -EINVAL;
137 goto ERROUT;
138 }
139
140 ret = FilepWrite(privFilep, fileOps, buffer, bufLen);
141 if (ret < 0) {
142 goto ERROUT;
143 }
144 return ret;
145
146 ERROUT:
147 set_errno(-ret);
148 return VFS_ERROR;
149 }
150
SerialIoctl(struct file * filep,INT32 cmd,unsigned long arg)151 STATIC INT32 SerialIoctl(struct file *filep, INT32 cmd, unsigned long arg)
152 {
153 INT32 ret;
154 struct file *privFilep = NULL;
155 const struct file_operations_vfs *fileOps = NULL;
156
157 ret = GetFilepOps(filep, &privFilep, &fileOps);
158 if (ret != ENOERR) {
159 ret = -EINVAL;
160 goto ERROUT;
161 }
162
163 ret = FilepIoctl(privFilep, fileOps, cmd, arg);
164 if (ret < 0) {
165 goto ERROUT;
166 }
167 return ret;
168
169 ERROUT:
170 set_errno(-ret);
171 return VFS_ERROR;
172 }
173
SerialPoll(struct file * filep,poll_table * fds)174 STATIC INT32 SerialPoll(struct file *filep, poll_table *fds)
175 {
176 INT32 ret;
177 struct file *privFilep = NULL;
178 const struct file_operations_vfs *fileOps = NULL;
179
180 ret = GetFilepOps(filep, &privFilep, &fileOps);
181 if (ret != ENOERR) {
182 ret = -EINVAL;
183 goto ERROUT;
184 }
185 ret = FilepPoll(privFilep, fileOps, fds);
186 if (ret < 0) {
187 goto ERROUT;
188 }
189 return ret;
190
191 ERROUT:
192 set_errno(-ret);
193 return VFS_ERROR;
194 }
195
196 STATIC const struct file_operations_vfs g_serialDevOps = {
197 SerialOpen, /* open */
198 SerialClose, /* close */
199 SerialRead, /* read */
200 SerialWrite,
201 NULL,
202 SerialIoctl,
203 NULL,
204 #ifndef CONFIG_DISABLE_POLL
205 SerialPoll,
206 #endif
207 NULL,
208 };
209
virtual_serial_init(const CHAR * deviceName)210 INT32 virtual_serial_init(const CHAR *deviceName)
211 {
212 INT32 ret;
213 struct Vnode *vnode = NULL;
214
215 if (deviceName == NULL) {
216 ret = EINVAL;
217 goto ERROUT;
218 }
219
220 SerialTypeSet(deviceName);
221
222 VnodeHold();
223 ret = VnodeLookup(deviceName, &vnode, V_DUMMY);
224 if (ret != LOS_OK) {
225 ret = EACCES;
226 goto ERROUT;
227 }
228
229 (VOID)memset_s(&g_serialFilep, sizeof(struct file), 0, sizeof(struct file));
230 g_serialFilep.f_oflags = O_RDWR;
231 g_serialFilep.f_vnode = vnode;
232 g_serialFilep.ops = ((struct drv_data *)vnode->data)->ops;
233
234 if (g_serialFilep.ops->open != NULL) {
235 (VOID)g_serialFilep.ops->open(&g_serialFilep);
236 } else {
237 ret = EFAULT;
238 PRINTK("virtual_serial_init %s open is NULL\n", deviceName);
239 goto ERROUT;
240 }
241 (VOID)register_driver(SERIAL, &g_serialDevOps, DEFFILEMODE, &g_serialFilep);
242
243 VnodeDrop();
244 return ENOERR;
245
246 ERROUT:
247 VnodeDrop();
248 set_errno(ret);
249 return VFS_ERROR;
250 }
251
virtual_serial_deinit(VOID)252 INT32 virtual_serial_deinit(VOID)
253 {
254 return unregister_driver(SERIAL);
255 }
256
257