XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
device.c
浏览该文件的文档.
1
21#include <xwcd/ds/standard.h>
22#include <string.h>
23#include <xwos/lib/xwaop.h>
24#include <xwos/lib/bclst.h>
27#include <xwos/osal/lock/mtx.h>
28#include <xwcd/ds/xwds.h>
29#include <xwcd/ds/object.h>
30#include <xwcd/ds/device.h>
31
37 .remove = xwds_device_vop_remove,
38 .start = xwds_device_vop_start,
40#if defined(XWCDCFG_ds_PM) && (1 == XWCDCFG_ds_PM)
41 .suspend = xwds_device_vop_suspend,
42 .resume = xwds_device_vop_resume,
43#endif
44};
45
46/******** ******** ******** constructor & destructor ******** ******** ********/
49{
51 dev->vop = &xwds_dev_vop;
52 dev->ds = NULL;
53}
54
57{
59}
60
61/******** ******** base virtual operations ******** ********/
68{
69 const struct xwds_driver * drv;
70 xwer_t rc;
71
72 drv = dev->drv;
73 if ((drv) && (drv->probe)) {
74 rc = drv->probe(dev);
75 } else {
76 rc = XWOK;
77 }
78 return rc;
79}
80
87{
88 const struct xwds_driver * drv;
89 xwer_t rc;
90
91 drv = dev->drv;
92 if ((drv) && (drv->remove)) {
93 rc = drv->remove(dev);
94 } else {
95 rc = XWOK;
96 }
97 return rc;
98}
99
106{
107 const struct xwds_driver * drv;
108 xwer_t rc;
109
110 drv = dev->drv;
111 if ((drv) && (drv->start)) {
112 rc = drv->start(dev);
113 } else {
114 rc = XWOK;
115 }
116 return rc;
117}
118
125{
126 const struct xwds_driver * drv;
127 xwer_t rc;
128
129 drv = dev->drv;
130 if ((drv) && (drv->stop)) {
131 rc = drv->stop(dev);
132 } else {
133 rc = XWOK;
134 }
135 return rc;
136}
137
138#if defined(XWCDCFG_ds_PM) && (1 == XWCDCFG_ds_PM)
145{
146 const struct xwds_driver * drv;
147 xwer_t rc;
148
149 drv = dev->drv;
150 if ((drv) && (drv->suspend)) {
151 rc = drv->suspend(dev);
152 } else {
153 rc = XWOK;
154 }
155 return rc;
156}
157
164{
165 const struct xwds_driver * drv;
166 xwer_t rc;
167
168 drv = dev->drv;
169 if ((drv) && (drv->resume)) {
170 rc = drv->resume(dev);
171 } else {
172 rc = XWOK;
173 }
174 return rc;
175}
176#endif
177
178/******** ******** ******** APIs ******** ******** ********/
181{
182 struct xwds_device * dev;
183 struct xwds * ds;
184 const struct xwds_virtual_operation * vop;
185 xwer_t rc;
186
187 dev = obj;
188 ds = dev->ds;
189 if (ds) {
190 xwds_obj_del(ds, &dev->obj);
191 }
192 dev->ds = NULL;
193
194 /* remove device */
195 vop = dev->vop;
196 if (vop && vop->remove) {
197 rc = vop->remove(dev);
198 } else {
199 rc = xwds_device_vop_remove(dev);
200 }
201 if (rc < 0) {
202 goto err_dev_vop_remove;
203 }
204 return XWOK;
205
206err_dev_vop_remove:
207 xwds_obj_add(ds, &dev->obj);
208 dev->ds = ds;
209 return rc;
210}
211
213xwer_t xwds_device_probe(struct xwds * ds, struct xwds_device * dev,
214 xwobj_gc_f gcfunc)
215{
216 const struct xwds_virtual_operation * vop;
217 xwer_t rc;
218
219 XWDS_VALIDATE(ds, "nullptr", -EFAULT);
220 XWDS_VALIDATE(dev, "nullptr", -EFAULT);
221
222 rc = xwds_obj_probe(&dev->obj, gcfunc);
223 if (rc < 0) {
224 goto err_obj_probe;
225 }
226 /* refcnt == XWDS_OBJ_REF_SHUTDOWN */
227
228 /* probe device */
229 vop = dev->vop;
230 if (vop && vop->probe) {
231 rc = vop->probe(dev);
232 } else {
233 rc = xwds_device_vop_probe(dev);
234 }
235 if (rc < 0) {
236 goto err_dev_vop_probe;
237 }
238
239 /* add to device stack */
240 rc = xwds_obj_add(ds, &dev->obj);
241 if (rc < 0) {
242 goto err_xwds_obj_add;
243 }
244 dev->ds = ds;
245
246 return XWOK;
247
248err_xwds_obj_add:
249 if ((vop) && (vop->remove)) {
250 vop->remove(dev);
251 } else {
253 }
254err_dev_vop_probe:
256err_obj_probe:
257 return rc;
258}
259
262{
263 XWDS_VALIDATE(dev, "nullptr", -EFAULT);
264
265 return xwds_obj_remove(&dev->obj);
266}
267
270{
271 const struct xwds_virtual_operation * vop;
272 xwer_t rc;
273
274 XWDS_VALIDATE(dev, "nullptr", -EFAULT);
275
276 rc = xwds_obj_start(&dev->obj);
277 if (rc < 0) {
278 goto err_obj_start;
279 }
280 /* refcnt == XWDS_OBJ_REF_RUNNING */
281
282 /* start device */
283 vop = dev->vop;
284 if (vop && vop->start) {
285 rc = vop->start(dev);
286 } else {
287 rc = xwds_device_vop_start(dev);
288 }
289 if (rc < 0) {
290 goto err_dev_vop_start;
291 }
292 return XWOK;
293
294err_dev_vop_start:
295 xwds_obj_stop(&dev->obj);
296err_obj_start:
297 return rc;
298}
299
302{
303 const struct xwds_virtual_operation * vop;
304 xwer_t rc;
305
306 XWDS_VALIDATE(dev, "nullptr", -EFAULT);
307
308 rc = xwds_obj_stop(&dev->obj);
309 if (rc < 0) {
310 goto err_obj_stop;
311 }
312 /* refcnt == XWDS_OBJ_REF_SHUTDOWN */
313
314 /* stop device */
315 vop = dev->vop;
316 if (vop && vop->stop) {
317 rc = vop->stop(dev);
318 } else {
319 rc = xwds_device_vop_stop(dev);
320 }
321 if (rc < 0) {
322 goto err_dev_vop_stop;
323 }
324 return XWOK;
325
326err_dev_vop_stop:
327 xwds_obj_start(&dev->obj);
328err_obj_stop:
329 return rc;
330}
331
332#if defined(XWCDCFG_ds_PM) && (1 == XWCDCFG_ds_PM)
335{
336 const struct xwds_virtual_operation * vop;
337 xwer_t rc;
338
339 XWDS_VALIDATE(dev, "nullptr", -EFAULT);
340
341 rc = xwds_obj_suspend(&dev->obj);
342 if (rc < 0) {
343 goto err_obj_suspend;
344 }
345 /* refcnt == XWDS_OBJ_REF_SUSPEND */
346
347 /* suspend device */
348 vop = dev->vop;
349 if (vop && vop->suspend) {
350 rc = vop->suspend(dev);
351 } else {
352 rc = xwds_device_vop_suspend(dev);
353 }
354 if (rc < 0) {
355 goto err_dev_vop_suspend;
356 }
357 return XWOK;
358
359err_dev_vop_suspend:
360 xwds_obj_resume(&dev->obj);
361err_obj_suspend:
362 return rc;
363}
364
367{
368 const struct xwds_virtual_operation * vop;
369 xwer_t rc;
370
371 XWDS_VALIDATE(dev, "nullptr", -EFAULT);
372
373 rc = xwds_obj_resume(&dev->obj);
374 if (rc < 0) {
375 goto err_obj_resume;
376 }
377 /* refcnt == XWDS_OBJ_REF_RUNNING */
378
379 /* resume device */
380 vop = dev->vop;
381 if (vop && vop->resume) {
382 rc = vop->resume(dev);
383 } else {
384 rc = xwds_device_vop_resume(dev);
385 }
386 if (rc < 0) {
387 goto err_dev_vop_resume;
388 }
389 return XWOK;
390
391err_dev_vop_resume:
392 xwds_obj_suspend(&dev->obj);
393err_obj_resume:
394 return rc;
395}
396
398xwer_t xwds_device_suspend_all(struct xwds * ds, bool ign_err)
399{
400 struct xwds_device * c, * n;
401 xwreg_t cpuirq;
402 xwer_t rc;
403
404 XWDS_VALIDATE(ds, "nullptr", -EFAULT);
405
406 rc = XWOK;
410 rc = xwds_device_suspend(c);
412 if (rc < 0) {
413 if (ign_err) {
414 rc = XWOK;
415 } else {
416 break;
417 }
418 }
419 }
421 return rc;
422}
423
425xwer_t xwds_device_resume_all(struct xwds * ds, bool ign_err)
426{
427 struct xwds_device * c, * n;
428 xwreg_t cpuirq;
429 xwer_t rc;
430
431 XWDS_VALIDATE(ds, "nullptr", -EFAULT);
432
433 rc = XWOK;
437 rc = xwds_device_resume(c);
439 if (rc < 0) {
440 if (ign_err) {
441 rc = XWOK;
442 } else {
443 break;
444 }
445 }
446 }
448 return rc;
449}
450#endif
XWOS通用库:双循环链表
xwer_t xwds_device_gc(void *obj)
Definition device.c:180
const struct xwds_virtual_operation xwds_dev_vop
实现面向对象“多态”的函数表
Definition device.c:35
玄武设备栈:设备基类
xwer_t xwds_device_vop_start(struct xwds_device *dev)
设备基本操作函数:启动设备
Definition device.c:105
xwer_t xwds_device_suspend_all(struct xwds *ds, bool ign_err)
XWDS API:暂停所有设备
Definition device.c:398
void xwds_device_construct(struct xwds_device *dev)
XWDS API:设备的构造函数
Definition device.c:48
void xwds_device_destruct(struct xwds_device *dev)
XWDS API:设备的析构函数
Definition device.c:56
#define xwds_itr_next_device_safe(ds, p, n)
向前迭代每个设备(可安全删除设备)
Definition device.h:58
xwer_t xwds_device_probe(struct xwds *ds, struct xwds_device *dev, xwobj_gc_f gcfunc)
XWDS API:探测设备
Definition device.c:213
xwer_t xwds_device_stop(struct xwds_device *dev)
XWDS API:停止设备
Definition device.c:301
xwer_t xwds_device_resume_all(struct xwds *ds, bool ign_err)
XWDS API:继续所有设备
Definition device.c:425
xwer_t xwds_device_vop_stop(struct xwds_device *dev)
设备基本操作函数:停止设备
Definition device.c:124
xwer_t xwds_device_suspend(struct xwds_device *dev)
XWDS API:暂停设备
Definition device.c:334
#define xwds_itr_prev_device_safe(ds, p, n)
向后迭代每个设备(可安全删除设备)
Definition device.h:65
xwer_t xwds_device_vop_remove(struct xwds_device *dev)
设备基本操作函数:删除设备
Definition device.c:86
xwer_t xwds_device_remove(struct xwds_device *dev)
XWDS API:删除设备
Definition device.c:261
xwer_t xwds_device_vop_resume(struct xwds_device *dev)
设备基本操作函数:继续设备
Definition device.c:163
xwer_t xwds_device_resume(struct xwds_device *dev)
XWDS API:继续设备
Definition device.c:366
xwer_t xwds_device_start(struct xwds_device *dev)
XWDS API:启动设备
Definition device.c:269
xwer_t xwds_device_vop_probe(struct xwds_device *dev)
设备基本操作函数:探测设备
Definition device.c:67
xwer_t xwds_device_vop_suspend(struct xwds_device *dev)
设备基本操作函数:暂停设备
Definition device.c:144
xwer_t xwds_obj_add(struct xwds *ds, struct xwds_object *obj)
Definition object.c:172
xwer_t xwds_obj_probe(struct xwds_object *obj, xwobj_gc_f gcfunc)
Definition object.c:58
xwer_t xwds_obj_remove(struct xwds_object *obj)
Definition object.c:64
void xwds_obj_destruct(struct xwds_object *obj)
Definition object.c:38
void xwds_obj_construct(struct xwds_object *obj)
Definition object.c:30
xwer_t xwds_obj_del(struct xwds *ds, struct xwds_object *obj)
Definition object.c:190
xwer_t xwds_obj_start(struct xwds_object *obj)
Definition object.c:82
xwer_t xwds_obj_resume(struct xwds_object *obj)
Definition object.c:124
xwer_t xwds_obj_suspend(struct xwds_object *obj)
Definition object.c:110
xwer_t xwds_obj_stop(struct xwds_object *obj)
Definition object.c:96
#define __xwds_vop
Definition standard.h:36
#define __xwds_code
Definition standard.h:31
#define __xwds_api
Definition standard.h:33
#define XWDS_VALIDATE(exp, errstr,...)
Definition standard.h:51
#define __xwds_rodata
Definition standard.h:38
#define EFAULT
Bad address
Definition errno.h:44
#define XWOK
No error
Definition errno.h:182
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
xwptr_t xwreg_t
Definition type.h:409
xwer_t xwos_object_rawput(struct xwos_object *obj)
减少对象的引用计数,不触发垃圾回收函数
Definition object.c:344
xwer_t(* xwobj_gc_f)(struct xwos_object *)
垃圾回收函数指针类型
Definition object.h:42
static void xwos_sqlk_wr_lock_cpuirqsv(struct xwos_sqlk *sql, xwreg_t *cpuirq)
XWOS API:开启写临界区,保存本地CPU的中断标志并关闭
Definition seqlock.h:580
static void xwos_sqlk_wr_unlock_cpuirqrs(struct xwos_sqlk *sql, xwreg_t cpuirq)
XWOS API:关闭写临界区,恢复本地CPU的中断标志
Definition seqlock.h:613
操作系统抽象层:互斥锁
操作系统抽象层:顺序锁
操作系统抽象层:自旋锁
设备(所有设备的基类)
Definition device.h:127
const struct xwds_driver * drv
Definition device.h:133
const struct xwds_virtual_operation * vop
Definition device.h:138
struct xwds * ds
Definition device.h:137
struct xwds_object obj
Definition device.h:128
基本驱动函数表(所有设备驱动的基类)
Definition device.h:113
xwer_t(* start)(struct xwds_device *)
Definition device.h:118
xwer_t(* resume)(struct xwds_device *)
Definition device.h:121
xwer_t(* probe)(struct xwds_device *)
Definition device.h:116
xwer_t(* suspend)(struct xwds_device *)
Definition device.h:120
xwer_t(* stop)(struct xwds_device *)
Definition device.h:119
xwer_t(* remove)(struct xwds_device *)
Definition device.h:117
struct xwos_object xwobj
Definition object.h:50
基本操作的虚函数表(类似C++的虚函数表)
Definition device.h:99
xwer_t(* suspend)(struct xwds_device *)
Definition device.h:105
xwer_t(* start)(struct xwds_device *)
Definition device.h:102
xwer_t(* stop)(struct xwds_device *)
Definition device.h:103
xwer_t(* remove)(struct xwds_device *)
Definition device.h:101
xwer_t(* probe)(struct xwds_device *)
Definition device.h:100
xwer_t(* resume)(struct xwds_device *)
Definition device.h:106
设备栈
Definition xwds.h:37
struct xwos_sqlk devlistlock
Definition xwds.h:39
XWOS通用库:原子操作
玄武设备栈:对象
玄武设备栈:顶级头文件
玄武设备栈:电源管理框架