• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "spi_if.h"
10 #include "devsvc_manager_clnt.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "securec.h"
14 #include "spi_core.h"
15 
16 #define HDF_LOG_TAG spi_if
17 #define HOST_NAME_LEN 32
18 
19 struct SpiObject {
20     struct SpiCntlr *cntlr;
21     uint32_t csNum;
22 };
23 
SpiGetCntlrByBusNum(uint32_t num)24 static struct SpiCntlr *SpiGetCntlrByBusNum(uint32_t num)
25 {
26     int ret;
27     char *name = NULL;
28     struct SpiCntlr *cntlr = NULL;
29 
30     name = (char *)OsalMemCalloc(HOST_NAME_LEN + 1);
31     if (name == NULL) {
32         return NULL;
33     }
34     ret = snprintf_s(name, HOST_NAME_LEN + 1, HOST_NAME_LEN, "HDF_PLATFORM_SPI_%u", num);
35     if (ret < 0) {
36         HDF_LOGE("%s: snprintf_s failed", __func__);
37         OsalMemFree(name);
38         return NULL;
39     }
40     cntlr = (struct SpiCntlr *)DevSvcManagerClntGetService(name);
41     OsalMemFree(name);
42     return cntlr;
43 }
44 
SpiTransfer(DevHandle handle,struct SpiMsg * msgs,uint32_t count)45 int32_t SpiTransfer(DevHandle handle, struct SpiMsg *msgs, uint32_t count)
46 {
47     struct SpiObject *obj = NULL;
48 
49     if (handle == NULL) {
50         return HDF_ERR_INVALID_PARAM;
51     }
52     obj = (struct SpiObject *)handle;
53     return SpiCntlrTransfer(obj->cntlr, obj->csNum, msgs, count);
54 }
55 
SpiRead(DevHandle handle,uint8_t * buf,uint32_t len)56 int32_t SpiRead(DevHandle handle, uint8_t *buf, uint32_t len)
57 {
58     struct SpiMsg msg = {0};
59 
60     msg.wbuf = NULL;
61     msg.rbuf = buf;
62     msg.len = len;
63     return SpiTransfer(handle, &msg, 1);
64 }
65 
SpiWrite(DevHandle handle,uint8_t * buf,uint32_t len)66 int32_t SpiWrite(DevHandle handle, uint8_t *buf, uint32_t len)
67 {
68     struct SpiMsg msg = {0};
69 
70     msg.wbuf = buf;
71     msg.rbuf = NULL;
72     msg.len = len;
73     return SpiTransfer(handle, &msg, 1);
74 }
75 
SpiSetCfg(DevHandle handle,struct SpiCfg * cfg)76 int32_t SpiSetCfg(DevHandle handle, struct SpiCfg *cfg)
77 {
78     struct SpiObject *obj = NULL;
79 
80     if (handle == NULL) {
81         return HDF_ERR_INVALID_OBJECT;
82     }
83     obj = (struct SpiObject *)handle;
84     return SpiCntlrSetCfg(obj->cntlr, obj->csNum, cfg);
85 }
86 
SpiGetCfg(DevHandle handle,struct SpiCfg * cfg)87 int32_t SpiGetCfg(DevHandle handle, struct SpiCfg *cfg)
88 {
89     struct SpiObject *obj = NULL;
90 
91     if (handle == NULL) {
92         return HDF_ERR_INVALID_OBJECT;
93     }
94     obj = (struct SpiObject *)handle;
95     return SpiCntlrGetCfg(obj->cntlr, obj->csNum, cfg);
96 }
97 
SpiClose(DevHandle handle)98 void SpiClose(DevHandle handle)
99 {
100     int32_t ret;
101     struct SpiObject *obj = NULL;
102 
103     if (handle == NULL) {
104         HDF_LOGE("%s: handle is NULL", __func__);
105         return;
106     }
107 
108     obj = (struct SpiObject *)handle;
109     ret = SpiCntlrClose(obj->cntlr, obj->csNum);
110     if (ret != HDF_SUCCESS) {
111         HDF_LOGE("%s: error, ret is %d", __func__, ret);
112     }
113     OsalMemFree(handle);
114 }
115 
SpiOpen(const struct SpiDevInfo * info)116 DevHandle SpiOpen(const struct SpiDevInfo *info)
117 {
118     int32_t ret;
119     struct SpiObject *object = NULL;
120     struct SpiCntlr *cntlr = NULL;
121 
122     if (info == NULL) {
123         return NULL;
124     }
125     cntlr = SpiGetCntlrByBusNum(info->busNum);
126     if (cntlr == NULL) {
127         HDF_LOGE("%s: cntlr is null", __func__);
128         return NULL;
129     }
130 
131     object = (struct SpiObject *)OsalMemCalloc(sizeof(*object));
132     if (object == NULL) {
133         HDF_LOGE("%s: object malloc error", __func__);
134         return NULL;
135     }
136 
137     ret = SpiCntlrOpen(cntlr, info->csNum);
138     if (ret != HDF_SUCCESS) {
139         HDF_LOGE("%s: SpiCntlrOpen error, ret is %d", __func__, ret);
140         OsalMemFree(object);
141         return NULL;
142     }
143 
144     object->cntlr = cntlr;
145     object->csNum = info->csNum;
146     return (DevHandle)object;
147 }
148