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 "los_dev_quickstart.h"
33 #include "fcntl.h"
34 #include "linux/kernel.h"
35 #include "los_process_pri.h"
36 #include "fs/file.h"
37 #include "fs/driver.h"
38
39 EVENT_CB_S g_qsEvent;
40 static SysteminitHook g_systemInitFunc[QS_STAGE_CNT] = {0};
41 static char g_callOnce[QS_STAGE_CNT] = {0};
42
QuickstartOpen(struct file * filep)43 static int QuickstartOpen(struct file *filep)
44 {
45 return 0;
46 }
47
QuickstartClose(struct file * filep)48 static int QuickstartClose(struct file *filep)
49 {
50 return 0;
51 }
52
QuickstartNotify(unsigned int events)53 static int QuickstartNotify(unsigned int events)
54 {
55 int ret = LOS_EventWrite((PEVENT_CB_S)&g_qsEvent, events);
56 if (ret != 0) {
57 PRINT_ERR("%s,%d:0x%x\n", __FUNCTION__, __LINE__, ret);
58 ret = -EINVAL;
59 }
60 return ret;
61 }
62
63 #define WAITLIMIT 300000 /* 5min = 5*60*1000*1tick(1ms) */
64
QuickstartListen(unsigned long arg)65 static int QuickstartListen(unsigned long arg)
66 {
67 QuickstartListenArgs args;
68 if (copy_from_user(&args, (QuickstartListenArgs __user *)arg, sizeof(QuickstartListenArgs)) != LOS_OK) {
69 PRINT_ERR("%s,%d,failed!\n", __FUNCTION__, __LINE__);
70 return -EINVAL;
71 }
72 if (args.wait > WAITLIMIT) {
73 args.wait = WAITLIMIT;
74 PRINT_ERR("%s wait arg is too longer, set to WAITLIMIT!\n", __FUNCTION__);
75 }
76 int ret = LOS_EventRead((PEVENT_CB_S)&g_qsEvent, args.events, LOS_WAITMODE_AND | LOS_WAITMODE_CLR, args.wait);
77 if (ret != args.events && ret != 0) { /* 0: nowait is normal case */
78 PRINT_ERR("%s,%d:0x%x\n", __FUNCTION__, __LINE__, ret);
79 ret = -EINVAL;
80 }
81 return ret;
82 }
83
QuickstartHookRegister(LosSysteminitHook hooks)84 void QuickstartHookRegister(LosSysteminitHook hooks)
85 {
86 for (int i = 0; i < QS_STAGE_CNT; i++) {
87 g_systemInitFunc[i] = hooks.func[i];
88 }
89 }
90
QuickstartStageWorking(unsigned int level)91 static int QuickstartStageWorking(unsigned int level)
92 {
93 if ((level < QS_STAGE_CNT) && (g_callOnce[level] == 0) && (g_systemInitFunc[level] != NULL)) {
94 g_callOnce[level] = 1; /* 1: Already called */
95 g_systemInitFunc[level]();
96 } else {
97 PRINT_WARN("Trigger quickstart,but doing nothing!!\n");
98 }
99 return 0;
100 }
101
QuickstartDevUnlink(struct Vnode * node)102 static int QuickstartDevUnlink(struct Vnode *node)
103 {
104 (void)node;
105 return unregister_driver(QUICKSTART_NODE);
106 }
107
QuickstartIoctl(struct file * filep,int cmd,unsigned long arg)108 static ssize_t QuickstartIoctl(struct file *filep, int cmd, unsigned long arg)
109 {
110 ssize_t ret;
111 if (cmd == QUICKSTART_NOTIFY) {
112 return QuickstartNotify(arg);
113 }
114
115 if (OsGetUserInitProcessID() != LOS_GetCurrProcessID()) {
116 PRINT_ERR("Permission denios!\n");
117 return -EACCES;
118 }
119 switch (cmd) {
120 case QUICKSTART_LISTEN:
121 ret = QuickstartListen(arg);
122 break;
123 default:
124 ret = QuickstartStageWorking(cmd - QUICKSTART_STAGE(QS_STAGE1)); /* ioctl cmd converted to stage level */
125 break;
126 }
127 return ret;
128 }
129
130 static const struct file_operations_vfs g_quickstartDevOps = {
131 .open = QuickstartOpen, /* open */
132 .close = QuickstartClose, /* close */
133 .ioctl = QuickstartIoctl, /* ioctl */
134 .unlink = QuickstartDevUnlink, /* unlink */
135 };
136
QuickstartDevRegister(void)137 int QuickstartDevRegister(void)
138 {
139 LOS_EventInit(&g_qsEvent);
140 return register_driver(QUICKSTART_NODE, &g_quickstartDevOps, 0644, 0); /* 0644: file mode */
141 }
142
143