1 /*
2 * Copyright (c) 2021-2022 GOODIX.
3 *
4 * This file 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 "app_uart.h"
10 #include "uart/uart_core.h"
11 #include "uart_if.h"
12 #include "device_resource_if.h"
13 #include "hdf_base.h"
14 #include "hdf_log.h"
15 #include "los_sem.h"
16 #include "osal_mem.h"
17
18 #define HDF_LOG_TAG uart_gr5xx
19
20 #define DEFAULT_BAUDRATE 115200
21 #define DEFAULT_DATABITS UART_ATTR_DATABIT_8
22 #define DEFAULT_STOPBITS UART_ATTR_STOPBIT_1
23 #define DEFAULT_PARITY UART_ATTR_PARITY_NONE
24 #define CONFIG_MAX_BAUDRATE 921600
25 #define TX_BUF_SIZE 0X100
26 #define UART_STATE_NOT_OPENED 0
27 #define UART_STATE_OPENING 1
28 #define UART_STATE_USEABLE 2
29 #define UART_STATE_SUSPENED 3
30 #define UART_FLG_DMA_RX (1 << 0)
31 #define UART_FLG_DMA_TX (1 << 1)
32 #define UART_FLG_RD_BLOCK (1 << 2)
33 #define UART_TRANS_TIMEOUT 1000
34
35 typedef int32_t (*app_uart_cfg_handler_t)(struct UartDriverData *udd);
36 struct UartDriverData {
37 uint32_t id;
38 uint32_t baudrate;
39 int32_t count;
40 struct UartAttribute attr;
41 app_uart_params_t params;
42 app_uart_evt_handler_t eventCallback;
43 app_uart_cfg_handler_t config;
44 app_uart_tx_buf_t txBuffer;
45 int32_t state;
46 uint32_t flags;
47 };
48
49 static uint32_t g_uartRxSem[APP_UART_ID_MAX];
50 static uint32_t g_uartTxMutex[APP_UART_ID_MAX];
51 static uint32_t g_uartRxMutex[APP_UART_ID_MAX];
52 static uint32_t g_rxNum[APP_UART_ID_MAX];
53
54 static void Uart0Callback(app_uart_evt_t *event);
55 static void Uart1Callback(app_uart_evt_t *event);
56
57 static const app_uart_evt_handler_t *g_evtHandler[APP_UART_ID_MAX] = {
58 Uart0Callback,
59 Uart1Callback
60 };
61
Uart0Callback(app_uart_evt_t * event)62 static void Uart0Callback(app_uart_evt_t *event)
63 {
64 if (event->type == APP_UART_EVT_RX_DATA) {
65 g_rxNum[APP_UART_ID_0] = event->data.size;
66 LOS_SemPost(g_uartRxSem[APP_UART_ID_0]);
67 } else if (event->type == APP_UART_EVT_ERROR) {
68 LOS_SemPost(g_uartRxSem[APP_UART_ID_0]);
69 }
70 }
71
Uart1Callback(app_uart_evt_t * event)72 static void Uart1Callback(app_uart_evt_t *event)
73 {
74 if (event->type == APP_UART_EVT_RX_DATA) {
75 g_rxNum[APP_UART_ID_1] = event->data.size;
76 LOS_SemPost(g_uartRxSem[APP_UART_ID_1]);
77 } else if (event->type == APP_UART_EVT_ERROR) {
78 LOS_SemPost(g_uartRxSem[APP_UART_ID_1]);
79 }
80 }
81
GetUartDataBits(uint32_t attrDataBits)82 static uint32_t GetUartDataBits(uint32_t attrDataBits)
83 {
84 uint32_t dataBits;
85
86 switch (attrDataBits) {
87 case UART_ATTR_DATABIT_5:
88 dataBits = UART_DATABITS_5;
89 break;
90 case UART_ATTR_DATABIT_6:
91 dataBits = UART_DATABITS_6;
92 break;
93 case UART_ATTR_DATABIT_7:
94 dataBits = UART_DATABITS_7;
95 break;
96 case UART_ATTR_DATABIT_8:
97 default:
98 dataBits = UART_DATABITS_8;
99 break;
100 }
101
102 return dataBits;
103 }
104
GetUartStopBits(uint32_t attrStopBits)105 static uint32_t GetUartStopBits(uint32_t attrStopBits)
106 {
107 uint32_t stopBits;
108
109 switch (attrStopBits) {
110 case UART_ATTR_STOPBIT_1:
111 stopBits = UART_STOPBITS_1;
112 break;
113 case UART_ATTR_STOPBIT_1P5:
114 stopBits = UART_STOPBITS_1_5;
115 break;
116 case UART_ATTR_STOPBIT_2:
117 stopBits = UART_STOPBITS_2;
118 break;
119 default:
120 stopBits = UART_STOPBITS_1;
121 break;
122 }
123
124 return stopBits;
125 }
126
GetUartParity(uint32_t attrParity)127 static uint32_t GetUartParity(uint32_t attrParity)
128 {
129 uint32_t parity;
130
131 switch (attrParity) {
132 case UART_ATTR_PARITY_NONE:
133 parity = UART_PARITY_NONE;
134 break;
135 case UART_ATTR_PARITY_ODD:
136 parity = UART_PARITY_ODD;
137 break;
138 case UART_ATTR_PARITY_EVEN:
139 parity = UART_PARITY_EVEN;
140 break;
141 default:
142 parity = UART_PARITY_NONE;
143 break;
144 }
145
146 return parity;
147 }
148
Gr5xxUartConfig(struct UartDriverData * udd)149 static int32_t Gr5xxUartConfig(struct UartDriverData *udd)
150 {
151 uint32_t ret;
152 app_uart_params_t *params = NULL;
153
154 if (udd == NULL) {
155 return HDF_FAILURE;
156 }
157
158 params = &udd->params;
159 params->id = udd->id;
160 params->init.baud_rate = udd->baudrate;
161
162 params->init.data_bits = GetUartDataBits(udd->attr.dataBits);
163 params->init.stop_bits = GetUartStopBits(udd->attr.stopBits);
164 params->init.parity = GetUartParity(udd->attr.parity);
165
166 ret = app_uart_init(params, udd->eventCallback, &udd->txBuffer);
167 if (ret != APP_DRV_SUCCESS) {
168 HDF_LOGE("%s , app uart init failed\r\n", __func__);
169 return HDF_FAILURE;
170 }
171
172 return HDF_SUCCESS;
173 }
174
UartHostDevRead(struct UartHost * host,uint8_t * data,uint32_t size)175 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size)
176 {
177 int32_t ret;
178 uint32_t uwRet = 0;
179 struct UartDriverData *udd = NULL;
180
181 if ((host == NULL) || (host->priv == NULL) || (data == NULL)) {
182 HDF_LOGE("%s: invalid parameter", __func__);
183 return HDF_ERR_INVALID_PARAM;
184 }
185 udd = (struct UartDriverData *)host->priv;
186 if (udd->state != UART_STATE_USEABLE) {
187 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
188 return HDF_FAILURE;
189 }
190
191 LOS_MuxPend(g_uartRxMutex[udd->id], LOS_WAIT_FOREVER);
192
193 g_rxNum[udd->id] = 0;
194 LOS_SemPend(g_uartRxSem[udd->id], 0);
195 ret = app_uart_receive_async(udd->id, data, size);
196 if (ret != APP_DRV_SUCCESS) {
197 HDF_LOGE("%s: uart_%d receive %d data failed", __func__, udd->id, size);
198 LOS_MuxPost(g_uartRxMutex[udd->id]);
199 return HDF_FAILURE;
200 }
201
202 uwRet = LOS_SemPend(g_uartRxSem[udd->id], LOS_WAIT_FOREVER);
203 if (uwRet != LOS_OK) {
204 HDF_LOGE("%s: uart_%d rx sem pend failed", __func__, udd->id);
205 LOS_MuxPost(g_uartRxMutex[udd->id]);
206 return HDF_FAILURE;
207 }
208
209 LOS_MuxPost(g_uartRxMutex[udd->id]);
210
211 return g_rxNum[udd->id];
212 }
213
UartHostDevWrite(struct UartHost * host,uint8_t * data,uint32_t size)214 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size)
215 {
216 int32_t ret;
217 struct UartDriverData *udd = NULL;
218
219 if ((host == NULL) || (host->priv == NULL) || (data == NULL)) {
220 HDF_LOGE("%s: invalid parameter", __func__);
221 return HDF_ERR_INVALID_PARAM;
222 }
223 udd = (struct UartDriverData *)host->priv;
224 if (udd->state != UART_STATE_USEABLE) {
225 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
226 return HDF_FAILURE;
227 }
228
229 LOS_MuxPend(g_uartTxMutex[udd->id], LOS_WAIT_FOREVER);
230 ret = app_uart_transmit_sync(udd->id, data, size, UART_TRANS_TIMEOUT);
231 if (ret != APP_DRV_SUCCESS) {
232 LOS_MuxPost(g_uartTxMutex[udd->id]);
233 HDF_LOGE("%s: uart_%d send %d data failed", __func__, udd->id, size);
234 return HDF_FAILURE;
235 }
236 LOS_MuxPost(g_uartTxMutex[udd->id]);
237
238 return HDF_SUCCESS;
239 }
240
UartHostDevGetBaud(struct UartHost * host,uint32_t * baudRate)241 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate)
242 {
243 struct UartDriverData *udd = NULL;
244
245 if (host == NULL || host->priv == NULL || baudRate == NULL) {
246 HDF_LOGE("%s: invalid parameter", __func__);
247 return HDF_ERR_INVALID_PARAM;
248 }
249
250 udd = (struct UartDriverData *)host->priv;
251 if (udd->state != UART_STATE_USEABLE) {
252 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
253 return HDF_FAILURE;
254 }
255 *baudRate = udd->baudrate;
256
257 return HDF_SUCCESS;
258 }
259
UartHostDevSetBaud(struct UartHost * host,uint32_t baudRate)260 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate)
261 {
262 struct UartDriverData *udd = NULL;
263
264 if (host == NULL || host->priv == NULL) {
265 HDF_LOGE("%s: invalid parameter", __func__);
266 return HDF_ERR_INVALID_PARAM;
267 }
268
269 udd = (struct UartDriverData *)host->priv;
270 if (udd->state != UART_STATE_USEABLE) {
271 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
272 return HDF_FAILURE;
273 }
274 if ((baudRate > 0) && (baudRate <= CONFIG_MAX_BAUDRATE)) {
275 udd->baudrate = baudRate;
276 if (udd->config == NULL) {
277 HDF_LOGE("%s: not support", __func__);
278 return HDF_ERR_NOT_SUPPORT;
279 }
280 if (udd->config(udd) != HDF_SUCCESS) {
281 HDF_LOGE("%s: config baudrate %d failed", __func__, baudRate);
282 return HDF_FAILURE;
283 }
284 } else {
285 HDF_LOGE("%s: invalid baudrate, which is:%d", __func__, baudRate);
286 return HDF_FAILURE;
287 }
288
289 return HDF_SUCCESS;
290 }
291
UartHostDevGetAttribute(struct UartHost * host,struct UartAttribute * attribute)292 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute)
293 {
294 struct UartDriverData *udd = NULL;
295
296 if (host == NULL || host->priv == NULL || attribute == NULL) {
297 HDF_LOGE("%s: invalid parameter", __func__);
298 return HDF_ERR_INVALID_PARAM;
299 }
300 udd = (struct UartDriverData *)host->priv;
301 if (udd->state != UART_STATE_USEABLE) {
302 return HDF_FAILURE;
303 }
304
305 *attribute = udd->attr;
306
307 return HDF_SUCCESS;
308 }
309
UartHostDevSetAttribute(struct UartHost * host,struct UartAttribute * attribute)310 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute)
311 {
312 struct UartDriverData *udd = NULL;
313
314 if (host == NULL || host->priv == NULL || attribute == NULL) {
315 HDF_LOGE("%s: invalid parameter", __func__);
316 return HDF_ERR_INVALID_PARAM;
317 }
318 udd = (struct UartDriverData *)host->priv;
319 if (udd->state != UART_STATE_USEABLE) {
320 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
321 return HDF_FAILURE;
322 }
323
324 udd->attr = *attribute;
325 if (udd->config == NULL) {
326 HDF_LOGE("%s: not support", __func__);
327 return HDF_ERR_NOT_SUPPORT;
328 }
329 if (udd->config(udd) != HDF_SUCCESS) {
330 HDF_LOGE("%s: config failed", __func__);
331 return HDF_FAILURE;
332 }
333
334 return HDF_SUCCESS;
335 }
336
UartHostDevSetTransMode(struct UartHost * host,enum UartTransMode mode)337 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode)
338 {
339 struct UartDriverData *udd = NULL;
340
341 if (host == NULL || host->priv == NULL) {
342 HDF_LOGE("%s: invalid parameter", __func__);
343 return HDF_ERR_INVALID_PARAM;
344 }
345
346 udd = (struct UartDriverData *)host->priv;
347 if (udd->state != UART_STATE_USEABLE) {
348 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
349 return HDF_FAILURE;
350 }
351 if (mode == UART_MODE_RD_BLOCK) {
352 udd->flags |= UART_FLG_RD_BLOCK;
353 } else if (mode == UART_MODE_RD_NONBLOCK) {
354 HDF_LOGE("%s: uart_%d only support block mode", __func__, udd->id);
355 return HDF_FAILURE;
356 }
357
358 return HDF_SUCCESS;
359 }
360
UartDevSemInit(uint32_t id)361 static int32_t UartDevSemInit(uint32_t id)
362 {
363 uint32_t uwRet = 0;
364
365 uwRet = LOS_BinarySemCreate(0, &g_uartRxSem[id]);
366 if (uwRet != LOS_OK) {
367 return HDF_FAILURE;
368 }
369
370 uwRet = LOS_MuxCreate(&g_uartTxMutex[id]);
371 if (uwRet != LOS_OK) {
372 return HDF_FAILURE;
373 }
374
375 uwRet = LOS_MuxCreate(&g_uartRxMutex[id]);
376 if (uwRet != LOS_OK) {
377 return HDF_FAILURE;
378 }
379
380 return HDF_SUCCESS;
381 }
382
UartDevSemDeinit(uint32_t id)383 static void UartDevSemDeinit(uint32_t id)
384 {
385 if (g_uartRxSem[id] != 0) {
386 LOS_SemDelete(g_uartRxSem[id]);
387 }
388
389 if (g_uartTxMutex[id] != 0) {
390 LOS_SemDelete(g_uartTxMutex[id]);
391 }
392
393 if (g_uartRxMutex[id] != 0) {
394 LOS_SemDelete(g_uartRxMutex[id]);
395 }
396
397 g_uartRxSem[id] = 0;
398 g_uartTxMutex[id] = 0;
399 g_uartRxMutex[id] = 0;
400 }
401
UartHostDevInit(struct UartHost * host)402 static int32_t UartHostDevInit(struct UartHost *host)
403 {
404 struct UartDriverData *udd = NULL;
405 uint32_t ret = 0;
406 uint8_t *ptxBuf = NULL;
407
408 if (host == NULL || host->priv == NULL) {
409 HDF_LOGE("%s: invalid parameter", __func__);
410 return HDF_ERR_INVALID_PARAM;
411 }
412
413 udd = (struct UartDriverData *)host->priv;
414 if (udd->id >= APP_UART_ID_MAX) {
415 HDF_LOGE("%s: uart id is greater than the maximum", __func__);
416 return HDF_ERR_INVALID_PARAM;
417 }
418
419 if (udd->state == UART_STATE_NOT_OPENED) {
420 udd->state = UART_STATE_OPENING;
421
422 ptxBuf = (uint8_t *)OsalMemCalloc(TX_BUF_SIZE);
423 if (ptxBuf == NULL) {
424 HDF_LOGE("%s: alloc tx buffer failed", __func__);
425 return HDF_ERR_MALLOC_FAIL;
426 }
427
428 ret = UartDevSemInit(udd->id);
429 if (ret != HDF_SUCCESS) {
430 HDF_LOGE("%s: uart semaphor init failed", __func__);
431 UartDevSemDeinit(udd->id);
432 return HDF_FAILURE;
433 }
434
435 udd->txBuffer.tx_buf = ptxBuf;
436 udd->txBuffer.tx_buf_size = TX_BUF_SIZE;
437 udd->eventCallback = g_evtHandler[udd->id];
438 udd->config = Gr5xxUartConfig;
439
440 if (udd->config(udd) != HDF_SUCCESS) {
441 UartDevSemDeinit(udd->id);
442 (void)OsalMemFree(udd->txBuffer.tx_buf);
443 udd->txBuffer.tx_buf = NULL;
444 return HDF_FAILURE;
445 }
446 }
447
448 udd->state = UART_STATE_USEABLE;
449 udd->count++;
450 return HDF_SUCCESS;
451 }
452
UartHostDevDeinit(struct UartHost * host)453 static int32_t UartHostDevDeinit(struct UartHost *host)
454 {
455 struct UartDriverData *udd = NULL;
456 if (host == NULL || host->priv == NULL) {
457 HDF_LOGE("%s: invalid parameter", __func__);
458 return HDF_ERR_INVALID_PARAM;
459 }
460
461 udd = (struct UartDriverData *)host->priv;
462 if ((--udd->count) != 0) {
463 return HDF_SUCCESS;
464 }
465
466 UartDevSemDeinit(udd->id);
467 if (udd->txBuffer.tx_buf != NULL) {
468 (void)OsalMemFree(udd->txBuffer.tx_buf);
469 udd->txBuffer.tx_buf = NULL;
470 }
471
472 udd->state = UART_STATE_NOT_OPENED;
473 return HDF_SUCCESS;
474 }
475
UartHostDevPollEvent(struct UartHost * host,void * filep,void * table)476 static int32_t UartHostDevPollEvent(struct UartHost *host, void *filep, void *table)
477 {
478 struct UartDriverData *udd = NULL;
479
480 if (host == NULL || host->priv == NULL) {
481 HDF_LOGE("%s: host is NULL", __func__);
482 return HDF_FAILURE;
483 }
484 udd = (struct UartDriverData *)host->priv;
485 if (udd->state != UART_STATE_USEABLE) {
486 HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
487 return HDF_FAILURE;
488 }
489
490 return 0;
491 }
492
493 struct UartHostMethod g_uartHostMethod = {
494 .Init = UartHostDevInit,
495 .Deinit = UartHostDevDeinit,
496 .Read = UartHostDevRead,
497 .Write = UartHostDevWrite,
498 .SetBaud = UartHostDevSetBaud,
499 .GetBaud = UartHostDevGetBaud,
500 .SetAttribute = UartHostDevSetAttribute,
501 .GetAttribute = UartHostDevGetAttribute,
502 .SetTransMode = UartHostDevSetTransMode,
503 .pollEvent = UartHostDevPollEvent,
504 };
505
UartGetPinConfigFromHcs(struct UartDriverData * udd,const struct DeviceResourceNode * node)506 static int32_t UartGetPinConfigFromHcs(struct UartDriverData *udd, const struct DeviceResourceNode *node)
507 {
508 uint32_t resourceData;
509 struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
510
511 if (iface == NULL || iface->GetUint32 == NULL) {
512 HDF_LOGE("%s: face is invalid", __func__);
513 return HDF_FAILURE;
514 }
515
516 if (iface->GetUint32(node, "pin_tx_type", &resourceData, 0) != HDF_SUCCESS) {
517 HDF_LOGE("%s: read pin_tx_type fail", __func__);
518 return HDF_FAILURE;
519 }
520 udd->params.pin_cfg.tx.type = resourceData;
521
522 if (iface->GetUint32(node, "pin_tx_pin", &resourceData, 0) != HDF_SUCCESS) {
523 HDF_LOGE("%s: read pin_tx_pin fail", __func__);
524 return HDF_FAILURE;
525 }
526 udd->params.pin_cfg.tx.pin = (1 << resourceData);
527
528 if (iface->GetUint32(node, "pin_tx_mux", &resourceData, 0) != HDF_SUCCESS) {
529 HDF_LOGE("%s: read pin_tx_pin fail", __func__);
530 return HDF_FAILURE;
531 }
532 udd->params.pin_cfg.tx.mux = resourceData;
533
534 if (iface->GetUint32(node, "pin_tx_pull", &resourceData, 0) != HDF_SUCCESS) {
535 HDF_LOGE("%s: read pin_tx_pin fail", __func__);
536 return HDF_FAILURE;
537 }
538 udd->params.pin_cfg.tx.pull = resourceData;
539
540 if (iface->GetUint32(node, "pin_rx_type", &resourceData, 0) != HDF_SUCCESS) {
541 HDF_LOGE("%s: read pin_rx_type fail", __func__);
542 return HDF_FAILURE;
543 }
544 udd->params.pin_cfg.rx.type = resourceData;
545
546 if (iface->GetUint32(node, "pin_rx_pin", &resourceData, 0) != HDF_SUCCESS) {
547 HDF_LOGE("%s: read pin_rx_pin fail", __func__);
548 return HDF_FAILURE;
549 }
550 udd->params.pin_cfg.rx.pin = (1 << resourceData);
551
552 if (iface->GetUint32(node, "pin_rx_mux", &resourceData, 0) != HDF_SUCCESS) {
553 HDF_LOGE("%s: read pin_rx_pin fail", __func__);
554 return HDF_FAILURE;
555 }
556 udd->params.pin_cfg.rx.mux = resourceData;
557
558 if (iface->GetUint32(node, "pin_rx_pull", &resourceData, 0) != HDF_SUCCESS) {
559 HDF_LOGE("%s: read pin_rx_pin fail", __func__);
560 return HDF_FAILURE;
561 }
562 udd->params.pin_cfg.rx.pull = resourceData;
563
564 return HDF_SUCCESS;
565 }
566
UartGetDefaultConfigFromHcs(struct UartDriverData * udd,const struct DeviceResourceNode * node)567 static int32_t UartGetDefaultConfigFromHcs(struct UartDriverData *udd, const struct DeviceResourceNode *node)
568 {
569 uint32_t resourceData;
570 struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
571
572 if (iface == NULL || iface->GetUint32 == NULL) {
573 HDF_LOGE("%s: face is invalid", __func__);
574 return HDF_FAILURE;
575 }
576 if (iface->GetUint32(node, "id", &resourceData, 0) != HDF_SUCCESS) {
577 HDF_LOGE("%s: read id fail", __func__);
578 return HDF_FAILURE;
579 }
580 udd->id = resourceData;
581
582 if (iface->GetUint32(node, "baudrate", &resourceData, 0) != HDF_SUCCESS) {
583 HDF_LOGE("%s: read baudrate fail", __func__);
584 return HDF_FAILURE;
585 }
586 udd->baudrate = resourceData;
587
588 if (iface->GetUint32(node, "use_mode_type", &resourceData, 0) != HDF_SUCCESS) {
589 HDF_LOGE("%s: read use_mode_type fail", __func__);
590 return HDF_FAILURE;
591 }
592 udd->params.use_mode.type = resourceData;
593
594 if (iface->GetUint32(node, "use_mode_tx_dma_ch", &resourceData, 0) != HDF_SUCCESS) {
595 HDF_LOGE("%s: read use_mode_tx_dma_ch fail", __func__);
596 return HDF_FAILURE;
597 }
598 udd->params.use_mode.tx_dma_channel = resourceData;
599
600 if (iface->GetUint32(node, "use_mode_rx_dma_ch", &resourceData, 0) != HDF_SUCCESS) {
601 HDF_LOGE("%s: read use_mode_rx_dma_ch fail", __func__);
602 return HDF_FAILURE;
603 }
604 udd->params.use_mode.rx_dma_channel = resourceData;
605
606 if (iface->GetUint32(node, "rx_timeout_mode", &resourceData, 0) != HDF_SUCCESS) {
607 HDF_LOGE("%s: read rx_timeout_mode fail", __func__);
608 return HDF_FAILURE;
609 }
610 udd->params.init.rx_timeout_mode = resourceData;
611
612 return HDF_SUCCESS;
613 }
614
UartDevAttach(struct UartHost * host,struct HdfDeviceObject * device)615 static int32_t UartDevAttach(struct UartHost *host, struct HdfDeviceObject *device)
616 {
617 int32_t ret;
618 struct UartDriverData *udd = NULL;
619
620 if (device->property == NULL) {
621 HDF_LOGE("%s: property is null", __func__);
622 return HDF_FAILURE;
623 }
624 udd = (struct UartDriverData *)OsalMemCalloc(sizeof(*udd));
625 if (udd == NULL) {
626 HDF_LOGE("%s: OsalMemCalloc udd error", __func__);
627 return HDF_ERR_MALLOC_FAIL;
628 }
629
630 ret = UartGetDefaultConfigFromHcs(udd, device->property);
631 if (ret != HDF_SUCCESS || udd->id >= APP_UART_ID_MAX) {
632 (void)OsalMemFree(udd);
633 return HDF_FAILURE;
634 }
635
636 ret = UartGetPinConfigFromHcs(udd, device->property);
637 if (ret != HDF_SUCCESS) {
638 (void)OsalMemFree(udd);
639 return HDF_FAILURE;
640 }
641
642 udd->state = UART_STATE_NOT_OPENED;
643 udd->config = NULL;
644 udd->eventCallback = NULL;
645 udd->count = 0;
646
647 udd->params.id = udd->id;
648 udd->params.init.baud_rate = udd->baudrate;
649 udd->attr.dataBits = DEFAULT_DATABITS;
650 udd->attr.stopBits = DEFAULT_STOPBITS;
651 udd->attr.parity = DEFAULT_PARITY;
652
653 host->priv = udd;
654 host->num = udd->id;
655
656 return HDF_SUCCESS;
657 }
658
Gr55xxDetach(struct UartHost * host)659 static void Gr55xxDetach(struct UartHost *host)
660 {
661 struct UartDriverData *udd = NULL;
662
663 if (host->priv == NULL) {
664 HDF_LOGE("%s: invalid parameter", __func__);
665 return;
666 }
667 udd = (struct UartDriverData *)host->priv;
668 if (udd->state != UART_STATE_NOT_OPENED) {
669 HDF_LOGE("%s: uart driver data state invalid", __func__);
670 return;
671 }
672
673 (void)OsalMemFree(udd);
674 host->priv = NULL;
675 }
676
HdfUartDeviceBind(struct HdfDeviceObject * device)677 static int32_t HdfUartDeviceBind(struct HdfDeviceObject *device)
678 {
679 if (device == NULL) {
680 return HDF_ERR_INVALID_OBJECT;
681 }
682 return (UartHostCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS;
683 }
684
HdfUartDeviceInit(struct HdfDeviceObject * device)685 int32_t HdfUartDeviceInit(struct HdfDeviceObject *device)
686 {
687 int32_t ret;
688 struct UartHost *host = NULL;
689
690 if (device == NULL) {
691 HDF_LOGE("%s: device is null", __func__);
692 return HDF_ERR_INVALID_OBJECT;
693 }
694 host = UartHostFromDevice(device);
695 if (host == NULL) {
696 HDF_LOGE("%s: host is null", __func__);
697 return HDF_FAILURE;
698 }
699 ret = UartDevAttach(host, device);
700 if (ret != HDF_SUCCESS) {
701 HDF_LOGE("%s: attach error", __func__);
702 return HDF_FAILURE;
703 }
704 host->method = &g_uartHostMethod;
705 return ret;
706 }
707
HdfUartDeviceRelease(struct HdfDeviceObject * device)708 void HdfUartDeviceRelease(struct HdfDeviceObject *device)
709 {
710 struct UartHost *host = NULL;
711
712 if (device == NULL) {
713 HDF_LOGE("%s: device is null", __func__);
714 return;
715 }
716 host = UartHostFromDevice(device);
717 if (host == NULL) {
718 HDF_LOGE("%s: host is null", __func__);
719 return;
720 }
721 if (host->priv != NULL) {
722 Gr55xxDetach(host);
723 }
724 UartHostDestroy(host);
725 }
726
727 struct HdfDriverEntry g_hdfUartDevice = {
728 .moduleVersion = 1,
729 .moduleName = "HDF_PLATFORM_UART",
730 .Bind = HdfUartDeviceBind,
731 .Init = HdfUartDeviceInit,
732 .Release = HdfUartDeviceRelease,
733 };
734
735 HDF_INIT(g_hdfUartDevice);
736