• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "pin/pin_core.h"
10 #include "hdf_log.h"
11 #include "osal_mem.h"
12 #include "pin_if.h"
13 #include "platform_trace.h"
14 
15 #define HDF_LOG_TAG pin_core
16 
17 #define PIN_TRACE_BASIC_PARAM_NUM  2
18 #define PIN_TRACE_PARAM_GET_NUM    2
19 #define PIN_MAX_CNT_PER_CNTLR      32
20 
21 struct PinManager {
22     struct IDeviceIoService service;
23     struct HdfDeviceObject *device;
24     struct DListHead cntlrListHead;
25     OsalSpinlock listLock;
26     uint32_t irqSave;
27 };
28 
29 static struct PinManager *g_pinmanager = NULL;
30 
PinCntlrListGet(void)31 static struct DListHead *PinCntlrListGet(void)
32 {
33     int32_t ret;
34     static struct DListHead *head = NULL;
35 
36     if (head == NULL) {
37         head = &g_pinmanager->cntlrListHead;
38         DListHeadInit(head);
39     }
40     ret = OsalSpinLockIrqSave(&g_pinmanager->listLock, &g_pinmanager->irqSave);
41     if (ret != HDF_SUCCESS) {
42         HDF_LOGE("PinCntlrListGet: spin lock save fail!");
43         return NULL;
44     }
45     return head;
46 }
47 
PinCntlrListPut(void)48 static void PinCntlrListPut(void)
49 {
50     int32_t ret;
51     ret = OsalSpinUnlockIrqRestore(&g_pinmanager->listLock, &g_pinmanager->irqSave);
52     if (ret != HDF_SUCCESS) {
53         HDF_LOGE("PinCntlrListPut: spin lock restore fail!");
54         return;
55     }
56 }
57 
PinCntlrAdd(struct PinCntlr * cntlr)58 int32_t PinCntlrAdd(struct PinCntlr *cntlr)
59 {
60     struct DListHead *head = NULL;
61 
62     if (cntlr == NULL) {
63         HDF_LOGE("PinCntlrAdd: cntlr is null!");
64         return HDF_ERR_INVALID_OBJECT;
65     }
66     DListHeadInit(&cntlr->node);
67 
68     if (cntlr->method == NULL) {
69         HDF_LOGE("PinCntlrAdd: no method supplied!");
70         return HDF_ERR_INVALID_OBJECT;
71     }
72 
73     if (cntlr->pinCount >= PIN_MAX_CNT_PER_CNTLR) {
74         HDF_LOGE("PinCntlrAdd: invalid pinCount:%u!", cntlr->pinCount);
75         return HDF_ERR_INVALID_PARAM;
76     }
77 
78     OsalSpinInit(&cntlr->spin);
79 
80     head = PinCntlrListGet();
81     DListInsertTail(&cntlr->node, head);
82     PinCntlrListPut();
83     return HDF_SUCCESS;
84 }
85 
PinCntlrRemove(struct PinCntlr * cntlr)86 void PinCntlrRemove(struct PinCntlr *cntlr)
87 {
88     if (cntlr == NULL) {
89         HDF_LOGE("PinCntlrRemove: cntlr is null!");
90         return;
91     }
92 
93     (void)PinCntlrListGet();
94     DListRemove(&cntlr->node);
95     PinCntlrListPut();
96     (void)OsalSpinDestroy(&cntlr->spin);
97 }
98 
PinCntlrGetPinDescByName(const char * pinName)99 struct PinDesc *PinCntlrGetPinDescByName(const char *pinName)
100 {
101     struct DListHead *head = NULL;
102     struct PinCntlr *cntlr = NULL;
103     struct PinCntlr *tmp = NULL;
104     uint16_t num;
105 
106     if (pinName == NULL) {
107         HDF_LOGE("PinCntlrGetPinDescByName: pinName is null!");
108         return NULL;
109     }
110 
111     head = PinCntlrListGet();
112 
113     DLIST_FOR_EACH_ENTRY_SAFE(cntlr, tmp, head, struct PinCntlr, node) {
114         for (num = 0; num < cntlr->pinCount; num++) {
115             if (cntlr->pins[num].pinName == NULL) {
116                 continue;
117             }
118             if (!strcmp(cntlr->pins[num].pinName, pinName)) {
119                 PinCntlrListPut();
120                 HDF_LOGI("PinCntlrGetPinDescByName: cntlr->pins[%d].pinName is %s!", num, cntlr->pins[num].pinName);
121                 return &cntlr->pins[num];
122             }
123         }
124     }
125     PinCntlrListPut();
126     HDF_LOGE("PinCntlrGetPinDescByName: pinName:%s doesn't matching!", pinName);
127     return NULL;
128 }
129 
PinCntlrGetByNumber(uint16_t number)130 struct PinCntlr *PinCntlrGetByNumber(uint16_t number)
131 {
132     struct DListHead *head = NULL;
133     struct PinCntlr *cntlr = NULL;
134     struct PinCntlr *tmp = NULL;
135 
136     head = PinCntlrListGet();
137 
138     DLIST_FOR_EACH_ENTRY_SAFE(cntlr, tmp, head, struct PinCntlr, node) {
139         if (cntlr->number == number) {
140             PinCntlrListPut();
141             HDF_LOGI("PinCntlrGetByNumber: get cntlr by number success!");
142             return cntlr;
143         }
144     }
145     PinCntlrListPut();
146     HDF_LOGE("PinCntlrGetByNumber: get cntlr by number error!");
147     return NULL;
148 }
149 
PinCntlrGetByPin(const struct PinDesc * desc)150 struct PinCntlr *PinCntlrGetByPin(const struct PinDesc *desc)
151 {
152     struct DListHead *head = NULL;
153     struct PinCntlr *cntlr = NULL;
154     struct PinCntlr *tmp = NULL;
155     int32_t num;
156 
157     if (desc == NULL) {
158         HDF_LOGE("PinCntlrGetByPin: desc is null!");
159         return NULL;
160     }
161 
162     head = PinCntlrListGet();
163 
164     DLIST_FOR_EACH_ENTRY_SAFE(cntlr, tmp, head, struct PinCntlr, node) {
165         for (num = 0; num < cntlr->pinCount; num++) {
166             if (desc == &cntlr->pins[num]) {
167                 PinCntlrListPut();
168                 HDF_LOGI("PinCntlrGetByPin: get cntlr by desc success!");
169                 return cntlr;
170             }
171         }
172     }
173     PinCntlrListPut();
174     HDF_LOGE("PinCntlrGetByPin: pin:%s is not in any controllers!", desc->pinName);
175     return NULL;
176 }
177 
GetPinIndex(struct PinCntlr * cntlr,struct PinDesc * desc)178 static int32_t GetPinIndex(struct PinCntlr *cntlr, struct PinDesc *desc)
179 {
180     uint16_t index;
181     int32_t ret;
182 
183     for (index = 0; index < cntlr->pinCount; index++) {
184         if (cntlr->pins[index].pinName == NULL) {
185             HDF_LOGE("GetPinIndex: cntlr->pin[index].pinName is null!");
186             break;
187         }
188         ret = strcmp(cntlr->pins[index].pinName, desc->pinName);
189         if (ret == 0) {
190             HDF_LOGI("GetPinIndex: get pin index:%hu success!", index);
191             return (int32_t)index;
192         }
193     }
194     HDF_LOGE("GetPinIndex: get pin index fail!");
195     return HDF_ERR_INVALID_PARAM;
196 }
197 
PinCntlrPutPin(const struct PinDesc * desc)198 void PinCntlrPutPin(const struct PinDesc *desc)
199 {
200     (void)desc;
201 }
202 
PinCntlrSetPinPull(struct PinCntlr * cntlr,struct PinDesc * desc,enum PinPullType pullType)203 int32_t PinCntlrSetPinPull(struct PinCntlr *cntlr, struct PinDesc *desc, enum PinPullType pullType)
204 {
205     int32_t ret;
206     uint32_t index;
207 
208     if (cntlr == NULL) {
209         HDF_LOGE("PinCntlrSetPinPull: cntlr is null!!");
210         return HDF_ERR_INVALID_OBJECT;
211     }
212 
213     if (cntlr->method == NULL || cntlr->method->SetPinPull == NULL) {
214         HDF_LOGE("PinCntlrSetPinPull: method or SetPinPull is null!");
215         return HDF_ERR_NOT_SUPPORT;
216     }
217 
218     if (desc == NULL) {
219         HDF_LOGE("PinCntlrSetPinPull: desc is null!");
220         return HDF_ERR_INVALID_PARAM;
221     }
222 
223     index = (uint32_t)GetPinIndex(cntlr, desc);
224     if (index < HDF_SUCCESS) {
225         HDF_LOGE("PinCntlrSetPinPull: get pin index fail!");
226         return HDF_ERR_INVALID_PARAM;
227     }
228 
229     (void)OsalSpinLockIrqSave(&cntlr->spin, &g_pinmanager->irqSave);
230     ret = cntlr->method->SetPinPull(cntlr, index, pullType);
231     if (PlatformTraceStart() == HDF_SUCCESS) {
232         unsigned int infos[PIN_TRACE_BASIC_PARAM_NUM];
233         infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1] = cntlr->number;
234         infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1] = cntlr->pinCount;
235         PlatformTraceAddUintMsg(PLATFORM_TRACE_MODULE_PIN, PLATFORM_TRACE_MODULE_PIN_FUN_SET,
236             infos, PIN_TRACE_BASIC_PARAM_NUM);
237         PlatformTraceStop();
238     }
239     (void)OsalSpinUnlockIrqRestore(&cntlr->spin, &g_pinmanager->irqSave);
240     return ret;
241 }
242 
PinCntlrGetPinPull(struct PinCntlr * cntlr,struct PinDesc * desc,enum PinPullType * pullType)243 int32_t PinCntlrGetPinPull(struct PinCntlr *cntlr, struct PinDesc *desc, enum PinPullType *pullType)
244 {
245     int32_t ret;
246     uint32_t index;
247 
248     if (cntlr == NULL) {
249         HDF_LOGE("PinCntlrGetPinPull: cntlr is null!");
250         return HDF_ERR_INVALID_OBJECT;
251     }
252 
253     if (cntlr->method == NULL || cntlr->method->GetPinPull == NULL) {
254         HDF_LOGE("PinCntlrGetPinPull: method or GetPinPull is null!");
255         return HDF_ERR_NOT_SUPPORT;
256     }
257 
258     if (desc == NULL) {
259         HDF_LOGE("PinCntlrGetPinPull: desc is null!");
260         return HDF_ERR_INVALID_PARAM;
261     }
262 
263     if (pullType == NULL) {
264         HDF_LOGE("PinCntlrGetPinPull: pullType is null!");
265         return HDF_ERR_INVALID_PARAM;
266     }
267 
268     index = (uint32_t)GetPinIndex(cntlr, desc);
269     if (index < HDF_SUCCESS) {
270         HDF_LOGE("PinCntlrGetPinPull: get pin index fail!");
271         return HDF_ERR_INVALID_PARAM;
272     }
273 
274     (void)OsalSpinLockIrqSave(&cntlr->spin, &g_pinmanager->irqSave);
275     ret = cntlr->method->GetPinPull(cntlr, index, pullType);
276     if (PlatformTraceStart() == HDF_SUCCESS) {
277         unsigned int infos[PIN_TRACE_PARAM_GET_NUM];
278         infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1] = cntlr->number;
279         infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1] = cntlr->pinCount;
280         PlatformTraceAddUintMsg(PLATFORM_TRACE_MODULE_PIN, PLATFORM_TRACE_MODULE_PIN_FUN_GET,
281             infos, PIN_TRACE_PARAM_GET_NUM);
282         PlatformTraceStop();
283         PlatformTraceInfoDump();
284     }
285     (void)OsalSpinUnlockIrqRestore(&cntlr->spin, &g_pinmanager->irqSave);
286     return ret;
287 }
288 
PinCntlrSetPinStrength(struct PinCntlr * cntlr,struct PinDesc * desc,uint32_t strength)289 int32_t PinCntlrSetPinStrength(struct PinCntlr *cntlr, struct PinDesc *desc, uint32_t strength)
290 {
291     int32_t ret;
292     uint32_t index;
293 
294     if (cntlr == NULL) {
295         HDF_LOGE("PinCntlrSetPinStrength: cntlr is null!");
296         return HDF_ERR_INVALID_OBJECT;
297     }
298 
299     if (cntlr->method == NULL || cntlr->method->SetPinStrength == NULL) {
300         HDF_LOGE("PinCntlrSetPinStrength: method or SetStrength is null!");
301         return HDF_ERR_NOT_SUPPORT;
302     }
303 
304     if (desc == NULL) {
305         HDF_LOGE("PinCntlrSetPinStrength: desc is null!");
306         return HDF_ERR_INVALID_PARAM;
307     }
308 
309     index = (uint32_t)GetPinIndex(cntlr, desc);
310     if (index < HDF_SUCCESS) {
311         HDF_LOGE("PinCntlrSetPinStrength: get pin index fail!");
312         return HDF_ERR_INVALID_PARAM;
313     }
314 
315     (void)OsalSpinLockIrqSave(&cntlr->spin, &g_pinmanager->irqSave);
316     ret = cntlr->method->SetPinStrength(cntlr, index, strength);
317     (void)OsalSpinUnlockIrqRestore(&cntlr->spin, &g_pinmanager->irqSave);
318     return ret;
319 }
320 
PinCntlrGetPinStrength(struct PinCntlr * cntlr,struct PinDesc * desc,uint32_t * strength)321 int32_t PinCntlrGetPinStrength(struct PinCntlr *cntlr, struct PinDesc *desc, uint32_t *strength)
322 {
323     int32_t ret;
324     uint32_t index;
325 
326     if (cntlr == NULL) {
327         HDF_LOGE("PinCntlrGetPinStrength: cntlr is null!");
328         return HDF_ERR_INVALID_OBJECT;
329     }
330 
331     if (cntlr->method == NULL || cntlr->method->GetPinStrength == NULL) {
332         HDF_LOGE("PinCntlrGetPinStrength: method or GetStrength is null!");
333         return HDF_ERR_NOT_SUPPORT;
334     }
335 
336     if (desc == NULL) {
337         HDF_LOGE("PinCntlrGetPinStrength: desc is null!");
338         return HDF_ERR_INVALID_PARAM;
339     }
340 
341     if (strength == NULL) {
342         HDF_LOGE("PinCntlrGetPinStrength: strength is null!");
343         return HDF_ERR_INVALID_PARAM;
344     }
345 
346     index = (uint32_t)GetPinIndex(cntlr, desc);
347     if (index < HDF_SUCCESS) {
348         HDF_LOGE("PinCntlrGetPinStrength: get pin index fail!");
349         return HDF_ERR_INVALID_PARAM;
350     }
351 
352     (void)OsalSpinLockIrqSave(&cntlr->spin, &g_pinmanager->irqSave);
353     ret = cntlr->method->GetPinStrength(cntlr, index, strength);
354     (void)OsalSpinUnlockIrqRestore(&cntlr->spin, &g_pinmanager->irqSave);
355 
356     return ret;
357 }
358 
PinCntlrSetPinFunc(struct PinCntlr * cntlr,struct PinDesc * desc,const char * funcName)359 int32_t PinCntlrSetPinFunc(struct PinCntlr *cntlr, struct PinDesc *desc, const char *funcName)
360 {
361     int32_t ret;
362     uint32_t index;
363 
364     if (cntlr == NULL) {
365         HDF_LOGE("PinCntlrSetPinFunc: cntlr is null!");
366         return HDF_ERR_INVALID_OBJECT;
367     }
368 
369     if (cntlr->method == NULL || cntlr->method->SetPinFunc == NULL) {
370         HDF_LOGE("PinCntlrSetPinFunc: method or SetPinFunc is null!");
371         return HDF_ERR_NOT_SUPPORT;
372     }
373 
374     if (desc == NULL) {
375         HDF_LOGE("PinCntlrSetPinFunc: desc is null!");
376         return HDF_ERR_INVALID_PARAM;
377     }
378 
379     if (funcName == NULL) {
380         HDF_LOGE("PinCntlrSetPinFunc: funcName is null!");
381         return HDF_ERR_INVALID_PARAM;
382     }
383 
384     index = (uint32_t)GetPinIndex(cntlr, desc);
385     if (index < HDF_SUCCESS) {
386         HDF_LOGE("PinCntlrSetPinFunc: get pin index fail!");
387         return HDF_ERR_INVALID_PARAM;
388     }
389 
390     (void)OsalSpinLockIrqSave(&cntlr->spin, &g_pinmanager->irqSave);
391     ret = cntlr->method->SetPinFunc(cntlr, index, funcName);
392     (void)OsalSpinUnlockIrqRestore(&cntlr->spin, &g_pinmanager->irqSave);
393     return ret;
394 }
395 
PinCntlrGetPinFunc(struct PinCntlr * cntlr,struct PinDesc * desc,const char ** funcName)396 int32_t PinCntlrGetPinFunc(struct PinCntlr *cntlr, struct PinDesc *desc, const char **funcName)
397 {
398     int32_t ret;
399     uint32_t index;
400 
401     if (cntlr == NULL) {
402         HDF_LOGE("PinCntlrGetPinFunc: cntlr is null!");
403         return HDF_ERR_INVALID_OBJECT;
404     }
405 
406     if (cntlr->method == NULL || cntlr->method->GetPinFunc == NULL) {
407         HDF_LOGE("PinCntlrGetPinFunc: method or SetPinFunc is null!");
408         return HDF_ERR_NOT_SUPPORT;
409     }
410 
411     if (desc == NULL) {
412         HDF_LOGE("PinCntlrGetPinFunc: desc is null!");
413         return HDF_ERR_INVALID_PARAM;
414     }
415 
416     if (funcName == NULL) {
417         HDF_LOGE("PinCntlrGetPinFunc: funcName is null!");
418         return HDF_ERR_INVALID_PARAM;
419     }
420 
421     index = (uint32_t)GetPinIndex(cntlr, desc);
422     if (index < HDF_SUCCESS) {
423         HDF_LOGE("PinCntlrGetPinFunc: get pin index fail!");
424         return HDF_ERR_INVALID_PARAM;
425     }
426 
427     (void)OsalSpinLockIrqSave(&cntlr->spin, &g_pinmanager->irqSave);
428     ret = cntlr->method->GetPinFunc(cntlr, index, funcName);
429     (void)OsalSpinUnlockIrqRestore(&cntlr->spin, &g_pinmanager->irqSave);
430     return ret;
431 }
432 
HandleByData(struct HdfSBuf * data)433 static DevHandle HandleByData(struct HdfSBuf *data)
434 {
435     const char *pinNameData = NULL;
436 
437     pinNameData = HdfSbufReadString(data);
438     if (pinNameData == NULL) {
439         HDF_LOGE("HandleByData: pinNameData is null!");
440         return NULL;
441     }
442 
443     return PinCntlrGetPinDescByName(pinNameData);
444 }
445 
PinIoGet(struct HdfSBuf * data,struct HdfSBuf * reply)446 static int32_t PinIoGet(struct HdfSBuf *data, struct HdfSBuf *reply)
447 {
448     const char *pinNameData = NULL;
449     DevHandle handle = NULL;
450 
451     (void)reply;
452     if (data == NULL) {
453         HDF_LOGE("PinIoGet: data is null!");
454         return HDF_ERR_INVALID_PARAM;
455     }
456 
457     pinNameData = HdfSbufReadString(data);
458     if (pinNameData == NULL) {
459         HDF_LOGE("PinIoGet: pinNameData is null!");
460         return HDF_ERR_IO;
461     }
462 
463     handle = PinGet(pinNameData);
464     if (handle == NULL) {
465         HDF_LOGE("PinIoGet: get handle fail!");
466         return HDF_FAILURE;
467     }
468     return HDF_SUCCESS;
469 }
470 
PinIoPut(struct HdfSBuf * data,struct HdfSBuf * reply)471 static int32_t PinIoPut(struct HdfSBuf *data, struct HdfSBuf *reply)
472 {
473     DevHandle handle = NULL;
474 
475     (void)reply;
476     if (data == NULL) {
477         HDF_LOGE("PinIoPut: data is null!");
478         return HDF_ERR_INVALID_PARAM;
479     }
480 
481     handle = HandleByData(data);
482     if (handle == NULL) {
483         HDF_LOGE("PinIoPut: get handle fail!");
484         return HDF_ERR_INVALID_PARAM;
485     }
486     PinPut(handle);
487     return HDF_SUCCESS;
488 }
489 
PinIoSetPull(struct HdfSBuf * data,struct HdfSBuf * reply)490 static int32_t PinIoSetPull(struct HdfSBuf *data, struct HdfSBuf *reply)
491 {
492     int32_t ret;
493     uint32_t pullType;
494     DevHandle handle = NULL;
495 
496     (void)reply;
497     if (data == NULL) {
498         HDF_LOGE("PinIoSetPull: data is null!");
499         return HDF_ERR_INVALID_PARAM;
500     }
501 
502     handle = HandleByData(data);
503     if (handle == NULL) {
504         HDF_LOGE("PinIoSetPull: get handle fail!");
505         return HDF_ERR_INVALID_PARAM;
506     }
507     if (!HdfSbufReadUint32(data, &pullType)) {
508         HDF_LOGE("PinIoSetPull: read pin pulltype fail!");
509         return HDF_ERR_IO;
510     }
511 
512     ret = PinSetPull(handle, pullType);
513     if (ret != HDF_SUCCESS) {
514         HDF_LOGE("PinIoSetPull: pin set pull fail, ret: %d!", ret);
515         return ret;
516     }
517     return ret;
518 }
519 
PinIoGetPull(struct HdfSBuf * data,struct HdfSBuf * reply)520 static int32_t PinIoGetPull(struct HdfSBuf *data, struct HdfSBuf *reply)
521 {
522     int32_t ret;
523     uint32_t pullType;
524     DevHandle handle = NULL;
525 
526     if (data == NULL || reply == NULL) {
527         HDF_LOGE("PinIoGetPull: data or reply is null!");
528         return HDF_ERR_INVALID_PARAM;
529     }
530 
531     handle = HandleByData(data);
532     if (handle == NULL) {
533         HDF_LOGE("PinIoGetPull: get handle fail!");
534         return HDF_ERR_INVALID_PARAM;
535     }
536     ret = PinGetPull(handle, &pullType);
537     if (ret != HDF_SUCCESS) {
538         HDF_LOGE("PinIoGetPull: pin get pull fail, ret: %d!", ret);
539         return ret;
540     }
541 
542     if (!HdfSbufWriteUint32(reply, pullType)) {
543         HDF_LOGE("PinIoGetPull: write pin pulltype fail!");
544         return HDF_ERR_IO;
545     }
546     return ret;
547 }
548 
PinIoSetStrength(struct HdfSBuf * data,struct HdfSBuf * reply)549 static int32_t PinIoSetStrength(struct HdfSBuf *data, struct HdfSBuf *reply)
550 {
551     int32_t ret;
552     uint32_t strength;
553     DevHandle handle = NULL;
554 
555     (void)reply;
556     if (data == NULL) {
557         HDF_LOGE("PinIoSetStrength: data is null!");
558         return HDF_ERR_INVALID_PARAM;
559     }
560 
561     handle = HandleByData(data);
562     if (handle == NULL) {
563         HDF_LOGE("PinIoSetStrength: get handle fail!");
564         return HDF_ERR_INVALID_PARAM;
565     }
566 
567     if (!HdfSbufReadUint32(data, &strength)) {
568         HDF_LOGE("PinIoSetStrength: read pin strength fail!");
569         return HDF_ERR_IO;
570     }
571 
572     ret = PinSetStrength(handle, strength);
573     if (ret != HDF_SUCCESS) {
574         HDF_LOGE("PinIoSetStrength: pin set strength fail, ret: %d!", ret);
575         return ret;
576     }
577     return ret;
578 }
579 
PinIoGetStrength(struct HdfSBuf * data,struct HdfSBuf * reply)580 static int32_t PinIoGetStrength(struct HdfSBuf *data, struct HdfSBuf *reply)
581 {
582     int32_t ret;
583     uint32_t strength;
584     DevHandle handle = NULL;
585 
586     if (data == NULL || reply == NULL) {
587         HDF_LOGE("PinIoGetStrength: data or reply is null!");
588         return HDF_ERR_INVALID_PARAM;
589     }
590 
591     handle = HandleByData(data);
592     if (handle == NULL) {
593         HDF_LOGE("PinIoGetStrength: get handle fail!");
594         return HDF_ERR_INVALID_PARAM;
595     }
596     ret = PinGetStrength(handle, &strength);
597     if (ret != HDF_SUCCESS) {
598         HDF_LOGE("PinIoGetStrength: pin get strength fail, ret: %d!", ret);
599         return ret;
600     }
601 
602     if (!HdfSbufWriteUint32(reply, strength)) {
603         HDF_LOGE("PinIoGetStrength: write pin strength fail!");
604         return HDF_ERR_IO;
605     }
606     return ret;
607 }
608 
PinIoSetFunc(struct HdfSBuf * data,struct HdfSBuf * reply)609 static int32_t PinIoSetFunc(struct HdfSBuf *data, struct HdfSBuf *reply)
610 {
611     int32_t ret;
612     DevHandle handle = NULL;
613     const char *funcNameData = NULL;
614 
615     (void)reply;
616     if (data == NULL) {
617         HDF_LOGE("PinIoSetFunc: data is null!");
618         return HDF_ERR_INVALID_PARAM;
619     }
620 
621     handle = HandleByData(data);
622     if (handle == NULL) {
623         HDF_LOGE("PinIoSetFunc: get handle fail!");
624         return HDF_ERR_INVALID_PARAM;
625     }
626 
627     funcNameData = HdfSbufReadString(data);
628     if (funcNameData == NULL) {
629         HDF_LOGE("PinIoSetFunc: funcnamedata is null!");
630         return HDF_ERR_IO;
631     }
632 
633     ret = PinSetFunc(handle, funcNameData);
634     if (ret != HDF_SUCCESS) {
635         HDF_LOGE("PinIoSetFunc: pin set func fail, ret: %d!", ret);
636         return ret;
637     }
638     return ret;
639 }
640 
PinIoGetFunc(struct HdfSBuf * data,struct HdfSBuf * reply)641 static int32_t PinIoGetFunc(struct HdfSBuf *data, struct HdfSBuf *reply)
642 {
643     int32_t ret;
644     DevHandle handle = NULL;
645     char *funcName = NULL;
646 
647     if (data == NULL || reply == NULL) {
648         HDF_LOGE("PinIoGetFunc: data or reply is null!");
649         return HDF_ERR_INVALID_PARAM;
650     }
651 
652     handle = HandleByData(data);
653     if (handle == NULL) {
654         HDF_LOGE("PinIoGetFunc: get handle fail!");
655         return HDF_ERR_INVALID_PARAM;
656     }
657     ret = PinGetFunc(handle, (const char **)&funcName);
658     if (ret != HDF_SUCCESS) {
659         HDF_LOGE("PinIoGetFunc: pin get func fail, ret: %d!", ret);
660         return ret;
661     }
662 
663     if (!HdfSbufWriteString(reply, funcName)) {
664         HDF_LOGE("PinIoGetFunc: write pin funcName fail!");
665         return HDF_ERR_IO;
666     }
667     return ret;
668 }
669 
PinIoManagerDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)670 int32_t PinIoManagerDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
671 {
672     int32_t ret;
673 
674     (void)client;
675     switch (cmd) {
676         case PIN_IO_GET:
677             return PinIoGet(data, reply);
678         case PIN_IO_PUT:
679             return PinIoPut(data, reply);
680         case PIN_IO_SET_PULL:
681             return PinIoSetPull(data, reply);
682         case PIN_IO_GET_PULL:
683             return PinIoGetPull(data, reply);
684         case PIN_IO_SET_STRENGTH:
685             return PinIoSetStrength(data, reply);
686         case PIN_IO_GET_STRENGTH:
687             return PinIoGetStrength(data, reply);
688         case PIN_IO_SET_FUNC:
689             return PinIoSetFunc(data, reply);
690         case PIN_IO_GET_FUNC:
691             return PinIoGetFunc(data, reply);
692         default:
693             ret = HDF_ERR_NOT_SUPPORT;
694             HDF_LOGE("PinIoManagerDispatch: cmd %d is not support!", cmd);
695             break;
696     }
697     return ret;
698 }
699 
pinManagerBind(struct HdfDeviceObject * device)700 static int32_t pinManagerBind(struct HdfDeviceObject *device)
701 {
702     struct PinManager *manager = NULL;
703 
704     if (device == NULL) {
705         HDF_LOGE("pinManagerBind: device is null!");
706         return HDF_ERR_INVALID_OBJECT;
707     }
708 
709     manager = (struct PinManager *)OsalMemCalloc(sizeof(*manager));
710     if (manager == NULL) {
711         HDF_LOGE("pinManagerBind: malloc manager fail!");
712         return HDF_ERR_MALLOC_FAIL;
713     }
714 
715     manager->device = device;
716     device->service = &manager->service;
717     device->service->Dispatch = PinIoManagerDispatch;
718     g_pinmanager = manager;
719     HDF_LOGI("pinManagerBind: pin manager bind success!");
720     return HDF_SUCCESS;
721 }
722 
pinManagerInit(struct HdfDeviceObject * device)723 static int32_t pinManagerInit(struct HdfDeviceObject *device)
724 {
725     (void)device;
726     OsalSpinInit(&g_pinmanager->listLock);
727     return HDF_SUCCESS;
728 }
729 
pinManagerRelease(struct HdfDeviceObject * device)730 static void pinManagerRelease(struct HdfDeviceObject *device)
731 {
732     struct PinManager *manager = NULL;
733 
734     HDF_LOGI("pinManagerRelease: enter!");
735     if (device == NULL) {
736         HDF_LOGE("pinManagerRelease: device is null!");
737         return;
738     }
739     manager = (struct PinManager *)device->service;
740     if (manager == NULL) {
741         HDF_LOGE("pinManagerRelease: no manager binded!");
742         return;
743     }
744     OsalMemFree(manager);
745     g_pinmanager = NULL;
746 }
747 
748 struct HdfDriverEntry g_pinManagerEntry = {
749     .moduleVersion = 1,
750     .Bind = pinManagerBind,
751     .Init = pinManagerInit,
752     .Release = pinManagerRelease,
753     .moduleName = "HDF_PLATFORM_PIN_MANAGER",
754 };
755 HDF_INIT(g_pinManagerEntry);
756