1 /*
2 // Copyright (C) 2022 Beken Corporation
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 */
15 #include <common/bk_include.h>
16 #include "bk_arm_arch.h"
17
18 #include "bk_drv_model.h"
19 #include "drv_model.h"
20 #include <os/mem.h>
21 #include <os/str.h>
22 #include <os/os.h>
23
24
25 static DRV_DEV_S drv_dev_tbl[DD_MAX_DEV] = {
26 {0},
27 };
28
drv_model_init(void)29 UINT32 drv_model_init(void)
30 {
31 os_memset(drv_dev_tbl, 0, sizeof(drv_dev_tbl));
32
33 return DRV_SUCCESS;
34 }
35
drv_model_uninit(void)36 UINT32 drv_model_uninit(void)
37 {
38 os_memset(drv_dev_tbl, 0, sizeof(drv_dev_tbl));
39
40 return DRV_SUCCESS;
41 }
42
ddev_check_handle(DD_HANDLE handle)43 UINT32 ddev_check_handle(DD_HANDLE handle)
44 {
45 UINT32 magic;
46 UINT32 idx;
47
48 magic = handle & DD_HANDLE_MAGIC_MASK;
49 idx = handle & DD_HANDLE_ID_MASK;
50 if ((DD_HANDLE_MAGIC_WORD == magic)
51 && (idx < DD_MAX_DEV))
52 return DRV_SUCCESS;
53 else
54 return DRV_FAILURE;
55 }
56
ddev_make_handle(UINT32 idx)57 DD_HANDLE ddev_make_handle(UINT32 idx)
58 {
59 UINT32 handle = DD_HANDLE_UNVALID;
60
61 if (idx >= DD_MAX_DEV)
62 goto make_exit;
63
64 handle = idx + DD_HANDLE_MAGIC_WORD;
65
66 make_exit:
67 return handle;
68 }
69
ddev_get_id_from_handle(DD_HANDLE handle)70 UINT32 ddev_get_id_from_handle(DD_HANDLE handle)
71 {
72 UINT32 magic;
73 UINT32 idx;
74
75 magic = handle & DD_HANDLE_MAGIC_MASK;
76 idx = handle & DD_HANDLE_ID_MASK;
77
78 if ((magic != DD_HANDLE_MAGIC_WORD) || (idx >= DD_DEV_TYPE_END))
79 return DD_ID_UNVALID;
80
81 return idx;
82 }
83
ddev_open(dd_device_type dev,UINT32 * status,UINT32 op_flag)84 DD_HANDLE ddev_open(dd_device_type dev, UINT32 *status, UINT32 op_flag)
85 {
86 UINT32 idx;
87 UINT32 handle;
88 DRV_DEV_PTR dev_ptr;
89 DD_OPERATIONS *operation;
90
91 GLOBAL_INT_DECLARATION();
92
93 handle = DD_HANDLE_UNVALID;
94 idx = ddev_get_id_from_handle(dev);
95 if (DD_ID_UNVALID == idx)
96 return DRV_FAILURE;
97 *status = DRV_FAILURE;
98
99 dev_ptr = &drv_dev_tbl[idx];
100 if (DD_STATE_OPENED == dev_ptr->state)
101 handle = ddev_make_handle(idx);
102 else if (DD_STATE_CLOSED == dev_ptr->state) {
103 handle = ddev_make_handle(idx);
104
105 operation = dev_ptr->op;
106 if (operation && (operation->open))
107 *status = (operation->open)(op_flag);
108
109 GLOBAL_INT_DISABLE();
110 dev_ptr->state = DD_STATE_OPENED;
111 dev_ptr->use_cnt = 0;
112 GLOBAL_INT_RESTORE();
113 } else {
114 }
115
116 GLOBAL_INT_DISABLE();
117 dev_ptr->use_cnt ++;
118 GLOBAL_INT_RESTORE();
119
120 return handle;
121 }
122
ddev_close(DD_HANDLE handle)123 UINT32 ddev_close(DD_HANDLE handle)
124 {
125 UINT32 idx;
126 DRV_DEV_PTR dev_ptr;
127 DD_OPERATIONS *operation;
128 GLOBAL_INT_DECLARATION();
129
130 idx = ddev_get_id_from_handle(handle);
131 if (DD_ID_UNVALID == idx)
132 return DRV_FAILURE;
133
134 dev_ptr = &drv_dev_tbl[idx];
135
136 GLOBAL_INT_DISABLE();
137 dev_ptr->use_cnt --;
138 GLOBAL_INT_RESTORE();
139
140 if (0 == dev_ptr->use_cnt) {
141 operation = dev_ptr->op;
142 if (operation && (operation->close))
143 (operation->close)();
144
145 GLOBAL_INT_DISABLE();
146 dev_ptr->state = DD_STATE_CLOSED;
147 GLOBAL_INT_RESTORE();
148 }
149
150 return DRV_SUCCESS;
151 }
152
ddev_read(DD_HANDLE handle,char * user_buf,UINT32 count,UINT32 op_flag)153 UINT32 ddev_read(DD_HANDLE handle, char *user_buf, UINT32 count, UINT32 op_flag)
154 {
155 UINT32 idx;
156 UINT32 status;
157 DRV_DEV_PTR dev_ptr;
158 DD_OPERATIONS *operation;
159
160 idx = ddev_get_id_from_handle(handle);
161 if (DD_ID_UNVALID == idx)
162 return DRV_FAILURE;
163
164 status = DRV_FAILURE;
165 dev_ptr = &drv_dev_tbl[idx];
166 operation = dev_ptr->op;
167 if (operation && (operation->read))
168 status = (operation->read)(user_buf, count, op_flag);
169
170 return status;
171 }
172
ddev_write(DD_HANDLE handle,char * user_buf,UINT32 count,UINT32 op_flag)173 UINT32 ddev_write(DD_HANDLE handle, char *user_buf, UINT32 count, UINT32 op_flag)
174 {
175 UINT32 idx;
176 UINT32 status;
177 DRV_DEV_PTR dev_ptr;
178 DD_OPERATIONS *operation;
179
180 idx = ddev_get_id_from_handle(handle);
181 if (DD_ID_UNVALID == idx)
182 return DRV_FAILURE;
183
184 status = DRV_FAILURE;
185 dev_ptr = &drv_dev_tbl[idx];
186 operation = dev_ptr->op;
187 if (operation && (operation->write))
188 status = (operation->write)(user_buf, count, op_flag);
189
190 return status;
191 }
192
ddev_control(DD_HANDLE handle,UINT32 cmd,VOID * param)193 UINT32 ddev_control(DD_HANDLE handle, UINT32 cmd, VOID *param)
194 {
195 UINT32 idx;
196 UINT32 status;
197 DRV_DEV_PTR dev_ptr;
198 DD_OPERATIONS *operation;
199
200 idx = ddev_get_id_from_handle(handle);
201 if (DD_ID_UNVALID == idx)
202 return DRV_FAILURE;
203
204 status = DRV_FAILURE;
205 dev_ptr = &drv_dev_tbl[idx];
206 operation = dev_ptr->op;
207 if (operation && (operation->control))
208 status = (operation->control)(cmd, param);
209
210 return status;
211 }
212
ddev_register_dev(DD_HANDLE handle,DD_OPERATIONS * optr)213 UINT32 ddev_register_dev(DD_HANDLE handle, DD_OPERATIONS *optr)
214 {
215 UINT32 idx;
216 DRV_DEV_PTR dev_ptr;
217
218 if (!optr)
219 return DRV_FAILURE;
220
221 idx = ddev_get_id_from_handle(handle);
222 if (DD_ID_UNVALID != idx) {
223 dev_ptr = &drv_dev_tbl[idx];
224 if (DD_STATE_NODEVICE == dev_ptr->state) {
225 // dev_ptr->name = dev_name;
226 dev_ptr->op = optr;
227 dev_ptr->state = DD_STATE_CLOSED;
228 } else
229 return DRV_FAILURE;
230 } else
231 return DRV_FAILURE;
232
233
234 return DRV_SUCCESS;
235 }
236
ddev_unregister_dev(DD_HANDLE handle)237 UINT32 ddev_unregister_dev(DD_HANDLE handle)
238 {
239 UINT32 idx;
240 DRV_DEV_PTR dev_ptr;
241
242 idx = ddev_get_id_from_handle(handle);
243 if (DD_ID_UNVALID != idx) {
244 dev_ptr = &drv_dev_tbl[idx];
245 // dev_ptr->name = 0;
246 dev_ptr->op = 0;
247 dev_ptr->state = DD_STATE_NODEVICE;
248 }
249
250 return DRV_SUCCESS;
251 }
252
253
254 // EOF
255