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