• 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 "sdio_if.h"
10 #include "hdf_base.h"
11 #include "hdf_log.h"
12 #include "mmc_sdio.h"
13 #include "osal_mem.h"
14 #include "securec.h"
15 
16 #define HDF_LOG_TAG sdio_if_c
17 
SdioDeviceGetFromHandle(DevHandle handle,struct SdioDevice ** sdio)18 static int32_t SdioDeviceGetFromHandle(DevHandle handle, struct SdioDevice **sdio)
19 {
20     struct MmcDevice *mmc = NULL;
21 
22     if (handle == NULL) {
23         return HDF_ERR_INVALID_OBJECT;
24     }
25 
26     if (sdio == NULL) {
27         return HDF_ERR_INVALID_PARAM;
28     }
29 
30     mmc = MmcCntlrGetDevice((struct MmcCntlr *)handle);
31     if (mmc == NULL) {
32         return HDF_PLT_ERR_NO_DEV;
33     }
34     if (mmc->type != MMC_DEV_SDIO && mmc->type != MMC_DEV_COMBO) {
35         MmcDevicePut(mmc);
36         return HDF_PLT_ERR_DEV_TYPE;
37     }
38 
39     *sdio = (struct SdioDevice *)mmc;
40     return HDF_SUCCESS;
41 }
42 
SdioOpen(int16_t mmcBusNum,struct SdioFunctionConfig * config)43 DevHandle SdioOpen(int16_t mmcBusNum, struct SdioFunctionConfig *config)
44 {
45     int32_t ret;
46     struct MmcCntlr *cntlr = NULL;
47     struct SdioDevice *sdio = NULL;
48     DevHandle handle = NULL;
49 
50     if (config == NULL) {
51         HDF_LOGE("SdioOpen: config can't be null!");
52         return NULL;
53     }
54 
55     handle = MmcOpen(mmcBusNum);
56     if (handle == NULL) {
57         HDF_LOGE("SdioOpen: SdioGetCntlrByBusNum fail!");
58         return NULL;
59     }
60     cntlr = (struct MmcCntlr *)handle;
61     if (cntlr != NULL && cntlr->ops != NULL && cntlr->ops->rescanSdioDev != NULL) {
62         ret = cntlr->ops->rescanSdioDev(cntlr);
63         if (ret != HDF_SUCCESS) {
64             HDF_LOGE("SdioOpen: sdio rescan fail!");
65             MmcClose(handle);
66             return NULL;
67         }
68     }
69 
70     ret = SdioDeviceGetFromHandle(handle, &sdio);
71     if (ret != HDF_SUCCESS) {
72         HDF_LOGE("SdioOpen: get sdio dev fail!");
73         MmcClose(handle);
74         return NULL;
75     }
76     ret = SdioDeviceFindFunction(sdio, config);
77     MmcDevicePut((struct MmcDevice *)sdio);
78     if (ret != HDF_SUCCESS) {
79         HDF_LOGE("SdioOpen: set function fail!");
80         MmcClose(handle);
81         return NULL;
82     }
83 
84     return (DevHandle)cntlr;
85 }
86 
SdioClose(DevHandle handle)87 void SdioClose(DevHandle handle)
88 {
89     (void)handle;
90 }
91 
SdioReadBytes(DevHandle handle,uint8_t * data,uint32_t addr,uint32_t size)92 int32_t SdioReadBytes(DevHandle handle, uint8_t *data, uint32_t addr, uint32_t size)
93 {
94     int32_t ret;
95     struct SdioDevice *sdio = NULL;
96 
97     ret = SdioDeviceGetFromHandle(handle, &sdio);
98     if (ret != HDF_SUCCESS) {
99         HDF_LOGE("SdioReadBytes: get sdio dev fail!");
100         return ret;
101     }
102     ret = SdioDeviceIncrAddrReadBytes(sdio, data, addr, size);
103     MmcDevicePut((struct MmcDevice *)sdio);
104     return ret;
105 }
106 
SdioWriteBytes(DevHandle handle,uint8_t * data,uint32_t addr,uint32_t size)107 int32_t SdioWriteBytes(DevHandle handle, uint8_t *data, uint32_t addr, uint32_t size)
108 {
109     int32_t ret;
110     struct SdioDevice *sdio = NULL;
111 
112     ret = SdioDeviceGetFromHandle(handle, &sdio);
113     if (ret != HDF_SUCCESS) {
114         HDF_LOGE("SdioWriteBytes: get sdio dev fail!");
115         return ret;
116     }
117     ret = SdioDeviceIncrAddrWriteBytes(sdio, data, addr, size);
118     MmcDevicePut((struct MmcDevice *)sdio);
119     return ret;
120 }
121 
SdioReadBytesFromFixedAddr(DevHandle handle,uint8_t * data,uint32_t addr,uint32_t size,uint32_t scatterLen)122 int32_t SdioReadBytesFromFixedAddr(DevHandle handle, uint8_t *data,
123     uint32_t addr, uint32_t size, uint32_t scatterLen)
124 {
125     int32_t ret;
126     struct SdioDevice *sdio = NULL;
127 
128     ret = SdioDeviceGetFromHandle(handle, &sdio);
129     if (ret != HDF_SUCCESS) {
130         HDF_LOGE("SdioReadBytesFromFixedAddr: get sdio dev fail!");
131         return ret;
132     }
133     ret = SdioDeviceFixedAddrReadBytes(sdio, data, addr, size, scatterLen);
134     MmcDevicePut((struct MmcDevice *)sdio);
135     return ret;
136 }
137 
SdioWriteBytesToFixedAddr(DevHandle handle,uint8_t * data,uint32_t addr,uint32_t size,uint32_t scatterLen)138 int32_t SdioWriteBytesToFixedAddr(DevHandle handle, uint8_t *data,
139     uint32_t addr, uint32_t size, uint32_t scatterLen)
140 {
141     int32_t ret;
142     struct SdioDevice *sdio = NULL;
143 
144     ret = SdioDeviceGetFromHandle(handle, &sdio);
145     if (ret != HDF_SUCCESS) {
146         HDF_LOGE("SdioWriteBytesToFixedAddr: get sdio dev fail!");
147         return ret;
148     }
149     ret = SdioDeviceFixedAddrWriteBytes(sdio, data, addr, size, scatterLen);
150     MmcDevicePut((struct MmcDevice *)sdio);
151     return ret;
152 }
153 
SdioReadBytesFromFunc0(DevHandle handle,uint8_t * data,uint32_t addr,uint32_t size)154 int32_t SdioReadBytesFromFunc0(DevHandle handle, uint8_t *data, uint32_t addr, uint32_t size)
155 {
156     int32_t ret;
157     struct SdioDevice *sdio = NULL;
158 
159     ret = SdioDeviceGetFromHandle(handle, &sdio);
160     if (ret != HDF_SUCCESS) {
161         HDF_LOGE("SdioReadBytesFromFunc0: get sdio dev fail!");
162         return ret;
163     }
164     ret = SdioDeviceFunc0ReadBytes(sdio, data, addr, size);
165     MmcDevicePut((struct MmcDevice *)sdio);
166     return ret;
167 }
168 
SdioWriteBytesToFunc0(DevHandle handle,uint8_t * data,uint32_t addr,uint32_t size)169 int32_t SdioWriteBytesToFunc0(DevHandle handle, uint8_t *data, uint32_t addr, uint32_t size)
170 {
171     int32_t ret;
172     struct SdioDevice *sdio = NULL;
173 
174     ret = SdioDeviceGetFromHandle(handle, &sdio);
175     if (ret != HDF_SUCCESS) {
176         HDF_LOGE("SdioWriteBytesToFunc0: get sdio dev fail!");
177         return ret;
178     }
179     ret = SdioDeviceFunc0WriteBytes(sdio, data, addr, size);
180     MmcDevicePut((struct MmcDevice *)sdio);
181     return ret;
182 }
183 
SdioSetBlockSize(DevHandle handle,uint32_t blockSize)184 int32_t SdioSetBlockSize(DevHandle handle, uint32_t blockSize)
185 {
186     int32_t ret;
187     struct SdioDevice *sdio = NULL;
188 
189     ret = SdioDeviceGetFromHandle(handle, &sdio);
190     if (ret != HDF_SUCCESS) {
191         HDF_LOGE("SdioSetBlockSize: get sdio dev fail!");
192         return ret;
193     }
194     ret = SdioDeviceSetBlockSize(sdio, blockSize);
195     MmcDevicePut((struct MmcDevice *)sdio);
196     return ret;
197 }
198 
SdioGetCommonInfo(DevHandle handle,SdioCommonInfo * info,SdioCommonInfoType infoType)199 int32_t SdioGetCommonInfo(DevHandle handle, SdioCommonInfo *info, SdioCommonInfoType infoType)
200 {
201     int32_t ret;
202     struct SdioDevice *sdio = NULL;
203 
204     ret = SdioDeviceGetFromHandle(handle, &sdio);
205     if (ret != HDF_SUCCESS) {
206         HDF_LOGE("SdioGetCommonInfo: get sdio dev fail!");
207         return ret;
208     }
209     ret = SdioDeviceGetCommonInfo(sdio, info, infoType);
210     MmcDevicePut((struct MmcDevice *)sdio);
211     return ret;
212 }
213 
SdioSetCommonInfo(DevHandle handle,SdioCommonInfo * info,SdioCommonInfoType infoType)214 int32_t SdioSetCommonInfo(DevHandle handle, SdioCommonInfo *info, SdioCommonInfoType infoType)
215 {
216     int32_t ret;
217     struct SdioDevice *sdio = NULL;
218 
219     ret = SdioDeviceGetFromHandle(handle, &sdio);
220     if (ret != HDF_SUCCESS) {
221         HDF_LOGE("SdioSetCommonInfo: get sdio dev fail!");
222         return ret;
223     }
224     ret = SdioDeviceSetCommonInfo(sdio, info, infoType);
225     MmcDevicePut((struct MmcDevice *)sdio);
226     return ret;
227 }
228 
SdioFlushData(DevHandle handle)229 int32_t SdioFlushData(DevHandle handle)
230 {
231     int32_t ret;
232     struct SdioDevice *sdio = NULL;
233 
234     ret = SdioDeviceGetFromHandle(handle, &sdio);
235     if (ret != HDF_SUCCESS) {
236         HDF_LOGE("SdioFlushData: get sdio dev fail!");
237         return ret;
238     }
239     ret = SdioDeviceFlushData(sdio);
240     MmcDevicePut((struct MmcDevice *)sdio);
241     return ret;
242 }
243 
SdioClaimHost(DevHandle handle)244 void SdioClaimHost(DevHandle handle)
245 {
246     int32_t ret;
247     struct SdioDevice *sdio = NULL;
248 
249     ret = SdioDeviceGetFromHandle(handle, &sdio);
250     if (ret != HDF_SUCCESS) {
251         HDF_LOGE("SdioClaimHost: get sdio dev fail!");
252         return;
253     }
254     ret = SdioDeviceClaimHost(sdio);
255     if (ret != HDF_SUCCESS) {
256         HDF_LOGE("SdioClaimHost: claim host fail!");
257     }
258     MmcDevicePut((struct MmcDevice *)sdio);
259 }
260 
SdioReleaseHost(DevHandle handle)261 void SdioReleaseHost(DevHandle handle)
262 {
263     int32_t ret;
264     struct SdioDevice *sdio = NULL;
265 
266     ret = SdioDeviceGetFromHandle(handle, &sdio);
267     if (ret != HDF_SUCCESS) {
268         HDF_LOGE("SdioReleaseHost: get sdio dev fail!");
269         return;
270     }
271     ret = SdioDeviceReleaseHost(sdio);
272     if (ret != HDF_SUCCESS) {
273         HDF_LOGE("SdioReleaseHost: claim host fail!");
274     }
275     MmcDevicePut((struct MmcDevice *)sdio);
276 }
277 
SdioEnableFunc(DevHandle handle)278 int32_t SdioEnableFunc(DevHandle handle)
279 {
280     int32_t ret;
281     struct SdioDevice *sdio = NULL;
282 
283     ret = SdioDeviceGetFromHandle(handle, &sdio);
284     if (ret != HDF_SUCCESS) {
285         HDF_LOGE("SdioEnableFunc: get sdio dev fail!");
286         return ret;
287     }
288     ret = SdioDeviceEnableFunc(sdio);
289     MmcDevicePut((struct MmcDevice *)sdio);
290     return ret;
291 }
292 
SdioDisableFunc(DevHandle handle)293 int32_t SdioDisableFunc(DevHandle handle)
294 {
295     int32_t ret;
296     struct SdioDevice *sdio = NULL;
297 
298     ret = SdioDeviceGetFromHandle(handle, &sdio);
299     if (ret != HDF_SUCCESS) {
300         HDF_LOGE("SdioDisableFunc: get sdio dev fail!");
301         return ret;
302     }
303     ret = SdioDeviceDisableFunc(sdio);
304     MmcDevicePut((struct MmcDevice *)sdio);
305     return ret;
306 }
307 
SdioClaimIrq(DevHandle handle,SdioIrqHandler * irqHandler)308 int32_t SdioClaimIrq(DevHandle handle, SdioIrqHandler *irqHandler)
309 {
310     int32_t ret;
311     struct SdioDevice *sdio = NULL;
312 
313     ret = SdioDeviceGetFromHandle(handle, &sdio);
314     if (ret != HDF_SUCCESS) {
315         HDF_LOGE("SdioClaimIrq: get sdio dev fail!");
316         return ret;
317     }
318     ret = SdioDeviceClaimIrq(sdio, irqHandler);
319     MmcDevicePut((struct MmcDevice *)sdio);
320     return ret;
321 }
322 
SdioReleaseIrq(DevHandle handle)323 int32_t SdioReleaseIrq(DevHandle handle)
324 {
325     int32_t ret;
326     struct SdioDevice *sdio = NULL;
327 
328     ret = SdioDeviceGetFromHandle(handle, &sdio);
329     if (ret != HDF_SUCCESS) {
330         HDF_LOGE("SdioReleaseIrq: get sdio dev fail!");
331         return ret;
332     }
333     ret = SdioDeviceReleaseIrq(sdio);
334     MmcDevicePut((struct MmcDevice *)sdio);
335     return ret;
336 }
337