1 /*
2  * Copyright (c) 2020-2023 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 "i2c_dev.h"
32 #include <fs/driver.h>
33 #include "i2c_core.h"
34 #include "osal_mem.h"
35 #include "user_copy.h"
36 
37 #define I2C_FS_MODE                 0660
38 #define I2C_RDWR_IOCTL_MAX_MSGS     42
39 #define I2C_BUF_MAX                 8192
40 #define I2C_NAME_SIZE               32
41 #define I2C_CNTLR_MAX               32
42 
43 struct I2cClient {
44     DevHandle handle;
45     struct I2cHost *host;
46     uint16_t addr;
47     uint16_t flags;
48 };
49 
I2cMsgInitFromUser(struct I2cMsg * kMsgs,struct i2c_msg * uMsgs)50 static inline int32_t I2cMsgInitFromUser(struct I2cMsg *kMsgs, struct i2c_msg *uMsgs)
51 {
52     int32_t ret;
53 
54     kMsgs->len = uMsgs->len;
55     kMsgs->addr = uMsgs->addr;
56     kMsgs->flags = uMsgs->flags;
57     if ((uMsgs->flags & I2C_M_RD) != 0) {
58         return HDF_SUCCESS;
59     }
60     ret = LOS_CopyToKernel((void *)(kMsgs->buf), kMsgs->len, (void *)(uMsgs->buf), uMsgs->len);
61     return (ret == LOS_OK) ? HDF_SUCCESS : HDF_ERR_IO;
62 }
63 
I2cMsgBackToUser(struct I2cMsg * kMsgs,struct i2c_msg * uMsgs)64 static inline int32_t I2cMsgBackToUser(struct I2cMsg *kMsgs, struct i2c_msg *uMsgs)
65 {
66     int32_t ret;
67 
68     if ((kMsgs->flags & I2C_M_RD) == 0) {
69         return HDF_SUCCESS;
70     }
71     ret = LOS_CopyFromKernel((void *)(uMsgs->buf), uMsgs->len, (void *)(kMsgs->buf), kMsgs->len);
72     return (ret == LOS_OK) ? HDF_SUCCESS : HDF_ERR_IO;
73 }
74 
I2cMsgsCopyBackToUser(I2cIoctlWrap * wrap,struct I2cMsg * kMsgs,struct i2c_msg * uMsgs)75 static inline int32_t I2cMsgsCopyBackToUser(I2cIoctlWrap *wrap,
76     struct I2cMsg *kMsgs, struct i2c_msg *uMsgs)
77 {
78     int ret;
79     unsigned int i;
80 
81     for (i = 0; i < wrap->nmsgs; i++) {
82         ret = I2cMsgBackToUser(&kMsgs[i], &uMsgs[i]);
83         if (ret != HDF_SUCCESS) {
84             break;
85         }
86     }
87     return ret;
88 }
89 
I2cMsgsDestroy(struct I2cMsg * kMsgs,struct i2c_msg * uMsgs)90 static void I2cMsgsDestroy(struct I2cMsg *kMsgs, struct i2c_msg *uMsgs)
91 {
92     if (kMsgs != NULL) {
93         if (kMsgs[0].buf != NULL) {
94             OsalMemFree(kMsgs[0].buf);
95             kMsgs[0].buf = NULL;
96         }
97     }
98     if (uMsgs != NULL) {
99         OsalMemFree(uMsgs);
100     }
101 }
102 
I2cMsgsCreateFromUser(I2cIoctlWrap * wrap,struct I2cMsg ** kMsgsPp,struct i2c_msg ** uMsgsPp)103 static int32_t I2cMsgsCreateFromUser(I2cIoctlWrap *wrap,
104     struct I2cMsg **kMsgsPp, struct i2c_msg **uMsgsPp)
105 {
106     int32_t ret;
107     size_t i;
108     size_t bufLen;
109     struct i2c_msg *uMsgs = NULL;
110     struct I2cMsg *kMsgs = NULL;
111     uint8_t *dataBuf = NULL;
112 
113     uMsgs = (struct i2c_msg *)OsalMemCalloc((sizeof(*uMsgs) + sizeof(*kMsgs)) * wrap->nmsgs);
114     if (uMsgs == NULL) {
115         return HDF_ERR_MALLOC_FAIL;
116     }
117     kMsgs = (struct I2cMsg *)((uint8_t *)uMsgs + sizeof(*uMsgs) * wrap->nmsgs);
118 
119     ret = LOS_CopyToKernel((void *)uMsgs, sizeof(*uMsgs) * wrap->nmsgs,
120         (void *)wrap->msgs, sizeof(*uMsgs) * wrap->nmsgs);
121     if (ret != LOS_OK) {
122         HDF_LOGE("I2cMsgsCreateFromUser: copy msgs from user fail!");
123         goto __ERR__;
124     }
125 
126     for (i = 0, bufLen = 0; i < wrap->nmsgs; i++) {
127         if (uMsgs[i].buf == NULL || uMsgs[i].len == 0) {
128             ret = HDF_ERR_INVALID_PARAM;
129             goto __ERR__;
130         }
131         bufLen += uMsgs[i].len;
132     }
133     if (bufLen >= I2C_BUF_MAX) {
134         HDF_LOGE("I2cMsgsCreateFromUser: buf too long:%u", bufLen);
135         ret = HDF_ERR_INVALID_PARAM;
136         goto __ERR__;
137     }
138 
139     dataBuf = (uint8_t *)OsalMemCalloc(bufLen);
140     if (dataBuf == NULL) {
141         ret = HDF_ERR_MALLOC_FAIL;
142         goto __ERR__;
143     }
144 
145     for (i = 0; i < wrap->nmsgs; i++) {
146         kMsgs[i].buf = dataBuf;
147         dataBuf += uMsgs[i].len;
148         ret = I2cMsgInitFromUser(&kMsgs[i], &uMsgs[i]);
149         if (ret != HDF_SUCCESS) {
150             goto __ERR__;
151         }
152     }
153 
154     *kMsgsPp = kMsgs;
155     *uMsgsPp = uMsgs;
156     return HDF_SUCCESS;
157 
158 __ERR__:
159     I2cMsgsDestroy(kMsgs, uMsgs);
160     return ret;
161 }
162 
I2cCntlrRead(DevHandle handle,uint16_t addr,uint8_t * buf,int16_t len,uint16_t flags)163 static int32_t I2cCntlrRead(DevHandle handle, uint16_t addr,
164     uint8_t *buf, int16_t len, uint16_t flags)
165 {
166     int32_t ret;
167     struct I2cMsg msg;
168 
169     msg.addr = addr;
170     msg.buf = buf;
171     msg.len = (uint16_t)len;
172     msg.flags = flags | I2C_FLAG_READ;
173 
174     ret = I2cTransfer(handle, &msg, 1);
175     return (ret == 1) ? HDF_SUCCESS : ret;
176 }
177 
I2cFsRead(struct file * filep,char * buf,size_t count)178 static ssize_t I2cFsRead(struct file *filep, char *buf, size_t count)
179 {
180     int32_t ret;
181     uint8_t *kbuf = NULL;
182     struct I2cClient *client = filep->f_priv;
183 
184     if (client == NULL) {
185         HDF_LOGE("I2cFsRead: client is null!");
186         return 0;
187     }
188 
189     kbuf = (uint8_t *)OsalMemCalloc(count);
190     if (kbuf == NULL) {
191         HDF_LOGE("I2cFsRead: malloc kbuf fail!");
192         return 0;
193     }
194 
195     ret = I2cCntlrRead(client->handle, client->addr, kbuf, count, client->flags);
196     PLAT_LOGV("I2cFsRead: I2cRead called, ret: %d!", ret);
197 
198     if (ret == HDF_SUCCESS) {
199         if (LOS_CopyFromKernel(buf, count, kbuf, count) != LOS_OK) {
200             HDF_LOGE("I2cFsRead: copy from kernel fail, ret: %d!", ret);
201             ret = HDF_ERR_IO;
202         }
203     }
204     OsalMemFree(kbuf);
205     return (ret == HDF_SUCCESS) ? count : 0;
206 }
207 
I2cCntlrWrite(DevHandle handle,uint16_t addr,const uint8_t * buf,int16_t len,uint16_t flags)208 static int32_t I2cCntlrWrite(DevHandle handle, uint16_t addr,
209     const uint8_t *buf, int16_t len, uint16_t flags)
210 {
211     int32_t ret;
212     struct I2cMsg msg;
213 
214     msg.addr = addr;
215     msg.buf = (uint8_t *)buf;
216     msg.len = (uint16_t)len;
217     msg.flags = flags & (~I2C_FLAG_READ);
218 
219     ret = I2cTransfer(handle, &msg, 1);
220     return (ret == 1) ? HDF_SUCCESS : ret;
221 }
222 
I2cFsWrite(struct file * filep,const char * buf,size_t count)223 static ssize_t I2cFsWrite(struct file *filep, const char *buf, size_t count)
224 {
225     int32_t ret;
226     uint8_t *kbuf = NULL;
227     struct I2cClient *client = filep->f_priv;
228 
229     if (client == NULL) {
230         HDF_LOGE("I2cFsWrite: client is null!");
231         return 0;
232     }
233 
234     kbuf = (uint8_t *)OsalMemCalloc(count);
235     if (kbuf == NULL) {
236         HDF_LOGE("I2cFsWrite: malloc kbuf fail!");
237         return 0;
238     }
239     if (LOS_CopyToKernel(kbuf, count, buf, count) != LOS_OK) {
240         HDF_LOGE("I2cFsWrite: copy to kernel fail!");
241         OsalMemFree(kbuf);
242         return 0;
243     }
244 
245     ret = I2cCntlrWrite(client->handle, client->addr, kbuf, count, client->flags);
246     PLAT_LOGV("I2cFsWrite: I2cWrite called, ret: %d!", ret);
247 
248     OsalMemFree(kbuf);
249     return (ret == HDF_SUCCESS) ? count : 0;
250 }
251 
I2cIoctlReadWrite(const struct I2cClient * client,const void * arg)252 static int I2cIoctlReadWrite(const struct I2cClient *client, const void *arg)
253 {
254     int ret;
255     I2cIoctlWrap wrap;
256     struct i2c_msg *uMsgs = NULL;
257     struct I2cMsg *kMsgs = NULL;
258 
259     if (arg == NULL) {
260         HDF_LOGE("I2cIoctlReadWrite: arg is null!");
261         return HDF_ERR_INVALID_PARAM;
262     }
263 
264     ret = LOS_CopyToKernel(&wrap, sizeof(wrap), (void *)arg, sizeof(wrap));
265     if (ret != LOS_OK) {
266         HDF_LOGE("I2cIoctlReadWrite: copy wrap fail!");
267         return HDF_ERR_IO;
268     }
269 
270     if (wrap.msgs == NULL || wrap.nmsgs == 0 || wrap.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS) {
271         HDF_LOGE("I2cIoctlReadWrite: wrap msgs is null or invalid num:%u!", wrap.nmsgs);
272         return HDF_ERR_INVALID_PARAM;
273     }
274 
275     ret = I2cMsgsCreateFromUser(&wrap, &kMsgs, &uMsgs);
276     if (ret != HDF_SUCCESS) {
277         HDF_LOGE("I2cIoctlReadWrite: recreate msgs fail!");
278         return ret;
279     }
280 
281     ret = I2cTransfer(client->handle, kMsgs, wrap.nmsgs);
282     PLAT_LOGV("I2cIoctlReadWrite: I2cTransfer called, ret: %d!", ret);
283     if ((unsigned int)ret == wrap.nmsgs) {
284         ret = I2cMsgsCopyBackToUser(&wrap, kMsgs, uMsgs);
285         if (ret != HDF_SUCCESS) {
286             HDF_LOGE("I2cIoctlReadWrite: copy back fail, ret: %d!", ret);
287         }
288     } else {
289         HDF_LOGE("I2cIoctlReadWrite: transfer fail, ret:%d, nmsgs:%u!", ret, wrap.nmsgs);
290     }
291 
292     I2cMsgsDestroy(kMsgs, uMsgs);
293     return ret;
294 }
295 
I2cFsIoctl(struct file * filep,int cmd,unsigned long arg)296 static int I2cFsIoctl(struct file *filep, int cmd, unsigned long arg)
297 {
298     int retval = ENOERR;
299     struct I2cClient *client = filep->f_priv;
300 
301     switch (cmd) {
302         case IOCTL_CLIENT_FORCE:
303         case IOCTL_CLIENT:
304             if ((((client->flags & I2C_M_TEN) == 0) && arg > 0xfe) || (arg > 0x3ff)) {
305                 HDF_LOGE("I2cFsIoctl: not support arg(%0lu)!!!", arg);
306                 retval = -EINVAL;
307                 break;
308             }
309             client->addr = arg;
310             break;
311         case IOCTL_RDWR: {
312             retval = I2cIoctlReadWrite(client, (void *)(uintptr_t)arg);
313             break;
314         }
315         case IOCTL_16BIT_REG:
316             if (arg == 0) {
317                 client->flags &= ~I2C_M_16BIT_REG;
318             } else {
319                 client->flags |= I2C_M_16BIT_REG;
320             }
321             break;
322         case IOCTL_16BIT_DATA:
323             if (arg == 0) {
324                 client->flags &= ~I2C_M_16BIT_DATA;
325             } else {
326                 client->flags |= I2C_M_16BIT_DATA;
327             }
328             break;
329         case IOCTL_TENBIT:
330             if (arg == 0) {
331                 client->flags &= ~I2C_M_TEN;
332             } else {
333                 client->flags |= I2C_M_TEN;
334             }
335             break;
336         case IOCTL_PEC:
337         case IOCTL_RETRIES:
338         case IOCTL_TIMEOUT:
339         default:
340             HDF_LOGE("I2cFsIoctl: not support cmd(%0d)!!!", cmd);
341             retval = -EINVAL;
342     }
343     return retval;
344 }
345 
I2cFsOpen(struct file * filep)346 static int I2cFsOpen(struct file *filep)
347 {
348     DevHandle handle = NULL;
349     struct I2cClient *client = NULL;
350     struct drv_data *drvData = NULL;
351     int16_t id;
352 
353     if (filep == NULL || filep->f_vnode == NULL || filep->f_vnode->data == NULL) {
354         HDF_LOGE("I2cFsOpen: function parameter is null");
355         return -EINVAL;
356     }
357     drvData = (struct drv_data *)filep->f_vnode->data;
358     id = (int16_t)(uintptr_t)drvData->priv;
359 
360     handle = I2cOpen(id);
361     if (handle == NULL) {
362         HDF_LOGE("I2cFsOpen: fail to get host:%d handle!", id);
363         return -1;
364     }
365 
366     client = (struct I2cClient *)OsalMemCalloc(sizeof(*client));
367     if (client == NULL) {
368         HDF_LOGE("I2cFsOpen: fail to malloc client-%d!", id);
369         return -1;
370     }
371     client->handle = handle;
372 
373     /* record the client as file private data */
374     filep->f_priv = client;
375     return 0;
376 }
377 
I2cFsClose(struct file * filep)378 static int I2cFsClose(struct file *filep)
379 {
380     struct I2cClient *client = filep->f_priv;
381 
382     if (client == NULL) {
383         HDF_LOGE("I2cFsClose: has't opened!");
384         return 0;
385     }
386 
387     if (client->handle == NULL) {
388         return 0;
389     }
390     I2cClose(client->handle);
391     client->handle = NULL;
392     OsalMemFree(client);
393     client = NULL;
394 
395     filep->f_priv = NULL;
396     return 0;
397 }
398 
I2cFsMap(struct file * filep,LosVmMapRegion * region)399 static ssize_t I2cFsMap(struct file* filep, LosVmMapRegion *region)
400 {
401     size_t size = region->range.size;
402 
403     (void)filep;
404     PADDR_T paddr = region->pgOff << PAGE_SHIFT;
405     VADDR_T vaddr = region->range.base;
406     LosVmSpace *space = LOS_SpaceGet(vaddr);
407 
408     if ((space == NULL) || ((paddr >= SYS_MEM_BASE) && (paddr < SYS_MEM_END))) {
409         return -EINVAL;
410     }
411 
412     if (LOS_ArchMmuMap(&space->archMmu, vaddr, paddr, size >> PAGE_SHIFT, region->regionFlags) <= 0) {
413         return -EAGAIN;
414     }
415 
416     return 0;
417 }
418 
419 static const struct file_operations_vfs g_i2cFops = {
420     .open = I2cFsOpen,
421     .close = I2cFsClose,
422     .read = I2cFsRead,
423     .write = I2cFsWrite,
424     .ioctl = I2cFsIoctl,
425     .mmap = I2cFsMap,
426 };
427 
I2cAddVfsById(int16_t id)428 int32_t I2cAddVfsById(int16_t id)
429 {
430 #ifdef LOSCFG_FS_VFS
431     int32_t ret;
432     char *name = NULL;
433 
434     if (id < 0 || id >= I2C_CNTLR_MAX) {
435         HDF_LOGE("I2cAddVfsById: id:%d exceed max:%d", id, I2C_CNTLR_MAX);
436         return HDF_ERR_INVALID_PARAM;
437     }
438     name = (char *)OsalMemCalloc(I2C_NAME_SIZE);
439     if (name == NULL) {
440         HDF_LOGE("I2cAddVfsById: malloc name fail!");
441         return HDF_ERR_MALLOC_FAIL;
442     }
443     /* create /dev/i2c-x device files for the i2c adatpers */
444     ret = snprintf_s(name, I2C_NAME_SIZE, I2C_NAME_SIZE - 1, "/dev/i2c-%d", id);
445     if (ret < 0) {
446         HDF_LOGE("I2cAddVfsById: format name fail, ret: %d!", ret);
447         OsalMemFree(name);
448         return ret;
449     }
450     ret = register_driver(name, &g_i2cFops, I2C_FS_MODE, (void *)((uintptr_t)id));
451     if (ret != 0) {
452         HDF_LOGE("I2cAddVfsById: register %s fail, ret: %d!", name, ret);
453     }
454     OsalMemFree(name);
455     return ret;
456 #else /* LOSCFG_FS_VFS */
457     (void)id;
458     return HDF_SUCCESS;
459 #endif
460 }
461 
I2cRemoveVfsById(int16_t id)462 void I2cRemoveVfsById(int16_t id)
463 {
464 #ifdef LOSCFG_FS_VFS
465     int32_t ret;
466     char *name = NULL;
467 
468     if (id < 0 || id >= I2C_CNTLR_MAX) {
469         HDF_LOGE("I2cRemoveVfsById: id:%d exceed max:%d", id, I2C_CNTLR_MAX);
470         return;
471     }
472     name = (char *)OsalMemCalloc(I2C_NAME_SIZE);
473     if (name == NULL) {
474         HDF_LOGE("I2cRemoveVfsById: malloc name fail!");
475         return;
476     }
477     /* remove /dev/i2c-x device files for the i2c controllers */
478     ret = snprintf_s(name, I2C_NAME_SIZE, I2C_NAME_SIZE - 1, "/dev/i2c-%d", id);
479     if (ret < 0) {
480         HDF_LOGE("I2cRemoveVfsById: format name fail, ret: %d!", ret);
481         OsalMemFree(name);
482         return;
483     }
484     ret = unregister_driver(name);
485     if (ret != 0) {
486         HDF_LOGE("I2cRemoveVfsById: unregister %s fail!", name);
487     }
488     OsalMemFree(name);
489 #else
490     (void)id;
491 #endif /* LOSCFG_FS_VFS */
492 }
493