1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "gpio_dev.h"
32 #include "fcntl.h"
33 #include "fs/driver.h"
34 #include "gpio_core.h"
35 #include "gpio_if.h"
36 #include "hdf_base.h"
37 #include "hdf_log.h"
38 #include "osal_mem.h"
39 #include "user_copy.h"
40
GpioOpen(struct file * filep)41 static int GpioOpen(struct file *filep)
42 {
43 (void)filep;
44 return 0;
45 }
46
GpioClose(struct file * filep)47 static int GpioClose(struct file *filep)
48 {
49 (void)filep;
50 return 0;
51 }
52
GpioExecCmd(uint16_t gpio,struct GpioBitInfo * info,int cmd)53 static int GpioExecCmd(uint16_t gpio, struct GpioBitInfo *info, int cmd)
54 {
55 int32_t ret;
56 uint16_t tmp;
57
58 switch (cmd) {
59 case GPIO_SET_DIR:
60 ret = GpioSetDir(gpio, info->direction);
61 if (ret != HDF_SUCCESS) {
62 HDF_LOGE("%s: set dir fail:%d", __func__, ret);
63 return -1;
64 }
65 PLAT_LOGV("%s: gpio:%u set dir:%u done!", __func__, gpio, info->direction);
66 break;
67 case GPIO_GET_DIR:
68 ret = GpioGetDir(gpio, &tmp);
69 if (ret != HDF_SUCCESS) {
70 HDF_LOGE("%s: get dir fail:%d", __func__, ret);
71 return -1;
72 }
73 info->direction = (unsigned char)tmp;
74 PLAT_LOGV("%s: gpio:%u get dir:%u done!", __func__, gpio, info->direction);
75 break;
76 case GPIO_READ_BIT:
77 ret = GpioRead(gpio, &tmp);
78 if (ret != HDF_SUCCESS) {
79 HDF_LOGE("%s: read gpio fail:%d", __func__, ret);
80 return -1;
81 }
82 info->value = (unsigned char)tmp;
83 PLAT_LOGV("%s: gpio:%u read:%u done!", __func__, gpio, info->value);
84 break;
85 case GPIO_WRITE_BIT:
86 ret = GpioWrite(gpio, info->value);
87 if (ret != HDF_SUCCESS) {
88 HDF_LOGE("%s: write gpio fail:%d", __func__, ret);
89 return -1;
90 }
91 PLAT_LOGV("%s: gpio:%u write:%u done!", __func__, gpio, info->value);
92 break;
93 default:
94 ret = -1;
95 }
96 return 0;
97 }
98
GpioIoctl(struct file * filep,int cmd,unsigned long arg)99 static int GpioIoctl(struct file *filep, int cmd, unsigned long arg)
100 {
101 int ret;
102 uint16_t bitNum;
103 uint16_t gpio;
104 struct GpioBitInfo info = {0};
105 struct drv_data* drvData = (struct drv_data* )filep->f_vnode->data;
106 bitNum = (uint16_t)(uintptr_t)drvData->priv;
107
108 if (arg == 0) {
109 HDF_LOGE("%s arg is 0", __func__);
110 return HDF_ERR_INVALID_PARAM;
111 }
112
113 ret = LOS_CopyToKernel(&info, sizeof(struct GpioBitInfo),
114 (const VOID *)(uintptr_t)arg, sizeof(struct GpioBitInfo));
115 if (ret != 0) {
116 return -1;
117 }
118 gpio = info.groupnumber * bitNum + info.bitnumber;
119 PLAT_LOGV("%s: gn:%u, bn:%u, gpio:%u", __func__, info.groupnumber, info.bitnumber, gpio);
120
121 ret = GpioExecCmd(gpio, &info, cmd);
122 if (ret != 0) {
123 return -1;
124 }
125
126 if ((unsigned int)cmd == GPIO_GET_DIR || (unsigned int)cmd == GPIO_READ_BIT) {
127 ret = LOS_CopyFromKernel((VOID *)(uintptr_t)arg, sizeof(struct GpioBitInfo),
128 &info, sizeof(struct GpioBitInfo));
129 if (ret != 0) {
130 HDF_LOGE("%s: copy back fail:%d", __func__, ret);
131 return -1;
132 }
133 }
134 return 0;
135 }
136
137 static const struct file_operations_vfs g_gpioDevOps = {
138 .open = GpioOpen,
139 .close = GpioClose,
140 .ioctl = GpioIoctl,
141 };
142
143 #define GPIO_VFS_MODE 0660
144 #define GPIO_VFS_NAME "/dev/gpio"
GpioAddVfs(uint16_t bitNum)145 int32_t GpioAddVfs(uint16_t bitNum)
146 {
147 int ret;
148
149 ret = register_driver(GPIO_VFS_NAME, &g_gpioDevOps, GPIO_VFS_MODE, (void *)(uintptr_t)bitNum);
150 if (ret != 0) {
151 HDF_LOGE("%s: register vfs fail:%d", __func__, ret);
152 return HDF_FAILURE;
153 }
154 HDF_LOGI("%s: register vfs success!", __func__);
155 return HDF_SUCCESS;
156 }
157
GpioRemoveVfs(void)158 void GpioRemoveVfs(void)
159 {
160 int ret;
161
162 ret = unregister_driver(GPIO_VFS_NAME);
163 if (ret != 0) {
164 HDF_LOGE("%s: unregister vfs fail!", __func__);
165 }
166 }
167