• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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     (void)table;
481     (void)filep;
482     if (host == NULL || host->priv == NULL) {
483         HDF_LOGE("%s: host is NULL", __func__);
484         return HDF_FAILURE;
485     }
486     udd = (struct UartDriverData *)host->priv;
487     if (udd->state != UART_STATE_USEABLE) {
488         HDF_LOGE("%s: uart_%d not useable", __func__, udd->id);
489         return HDF_FAILURE;
490     }
491 
492     return 0;
493 }
494 
495 struct UartHostMethod g_uartHostMethod = {
496     .Init = UartHostDevInit,
497     .Deinit = UartHostDevDeinit,
498     .Read = UartHostDevRead,
499     .Write = UartHostDevWrite,
500     .SetBaud = UartHostDevSetBaud,
501     .GetBaud = UartHostDevGetBaud,
502     .SetAttribute = UartHostDevSetAttribute,
503     .GetAttribute = UartHostDevGetAttribute,
504     .SetTransMode = UartHostDevSetTransMode,
505     .pollEvent = UartHostDevPollEvent,
506 };
507 
UartGetPinConfigFromHcs(struct UartDriverData * udd,const struct DeviceResourceNode * node)508 static int32_t UartGetPinConfigFromHcs(struct UartDriverData *udd, const struct DeviceResourceNode *node)
509 {
510     uint32_t resourceData;
511     struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
512 
513     if (iface == NULL || iface->GetUint32 == NULL) {
514         HDF_LOGE("%s: face is invalid", __func__);
515         return HDF_FAILURE;
516     }
517 
518     if (iface->GetUint32(node, "pin_tx_type", &resourceData, 0) != HDF_SUCCESS) {
519         HDF_LOGE("%s: read pin_tx_type fail", __func__);
520         return HDF_FAILURE;
521     }
522     udd->params.pin_cfg.tx.type = resourceData;
523 
524     if (iface->GetUint32(node, "pin_tx_pin", &resourceData, 0) != HDF_SUCCESS) {
525         HDF_LOGE("%s: read pin_tx_pin fail", __func__);
526         return HDF_FAILURE;
527     }
528     udd->params.pin_cfg.tx.pin = (1 << resourceData);
529 
530     if (iface->GetUint32(node, "pin_tx_mux", &resourceData, 0) != HDF_SUCCESS) {
531         HDF_LOGE("%s: read pin_tx_pin fail", __func__);
532         return HDF_FAILURE;
533     }
534     udd->params.pin_cfg.tx.mux = resourceData;
535 
536     if (iface->GetUint32(node, "pin_tx_pull", &resourceData, 0) != HDF_SUCCESS) {
537         HDF_LOGE("%s: read pin_tx_pin fail", __func__);
538         return HDF_FAILURE;
539     }
540     udd->params.pin_cfg.tx.pull = resourceData;
541 
542     if (iface->GetUint32(node, "pin_rx_type", &resourceData, 0) != HDF_SUCCESS) {
543         HDF_LOGE("%s: read pin_rx_type fail", __func__);
544         return HDF_FAILURE;
545     }
546     udd->params.pin_cfg.rx.type = resourceData;
547 
548     if (iface->GetUint32(node, "pin_rx_pin", &resourceData, 0) != HDF_SUCCESS) {
549         HDF_LOGE("%s: read pin_rx_pin fail", __func__);
550         return HDF_FAILURE;
551     }
552     udd->params.pin_cfg.rx.pin = (1 << resourceData);
553 
554     if (iface->GetUint32(node, "pin_rx_mux", &resourceData, 0) != HDF_SUCCESS) {
555         HDF_LOGE("%s: read pin_rx_pin fail", __func__);
556         return HDF_FAILURE;
557     }
558     udd->params.pin_cfg.rx.mux = resourceData;
559 
560     if (iface->GetUint32(node, "pin_rx_pull", &resourceData, 0) != HDF_SUCCESS) {
561         HDF_LOGE("%s: read pin_rx_pin fail", __func__);
562         return HDF_FAILURE;
563     }
564     udd->params.pin_cfg.rx.pull = resourceData;
565 
566     return HDF_SUCCESS;
567 }
568 
UartGetDefaultConfigFromHcs(struct UartDriverData * udd,const struct DeviceResourceNode * node)569 static int32_t UartGetDefaultConfigFromHcs(struct UartDriverData *udd, const struct DeviceResourceNode *node)
570 {
571     uint32_t resourceData;
572     struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
573 
574     if (iface == NULL || iface->GetUint32 == NULL) {
575         HDF_LOGE("%s: face is invalid", __func__);
576         return HDF_FAILURE;
577     }
578     if (iface->GetUint32(node, "id", &resourceData, 0) != HDF_SUCCESS) {
579         HDF_LOGE("%s: read id fail", __func__);
580         return HDF_FAILURE;
581     }
582     udd->id = resourceData;
583 
584     if (iface->GetUint32(node, "baudrate", &resourceData, 0) != HDF_SUCCESS) {
585         HDF_LOGE("%s: read baudrate fail", __func__);
586         return HDF_FAILURE;
587     }
588     udd->baudrate = resourceData;
589 
590     if (iface->GetUint32(node, "use_mode_type", &resourceData, 0) != HDF_SUCCESS) {
591         HDF_LOGE("%s: read use_mode_type fail", __func__);
592         return HDF_FAILURE;
593     }
594     udd->params.use_mode.type = resourceData;
595 
596     if (iface->GetUint32(node, "use_mode_tx_dma_ch", &resourceData, 0) != HDF_SUCCESS) {
597         HDF_LOGE("%s: read use_mode_tx_dma_ch fail", __func__);
598         return HDF_FAILURE;
599     }
600     udd->params.use_mode.tx_dma_channel = resourceData;
601 
602     if (iface->GetUint32(node, "use_mode_rx_dma_ch", &resourceData, 0) != HDF_SUCCESS) {
603         HDF_LOGE("%s: read use_mode_rx_dma_ch fail", __func__);
604         return HDF_FAILURE;
605     }
606     udd->params.use_mode.rx_dma_channel = resourceData;
607 
608     if (iface->GetUint32(node, "rx_timeout_mode", &resourceData, 0) != HDF_SUCCESS) {
609         HDF_LOGE("%s: read rx_timeout_mode fail", __func__);
610         return HDF_FAILURE;
611     }
612     udd->params.init.rx_timeout_mode = resourceData;
613 
614     return HDF_SUCCESS;
615 }
616 
UartDevAttach(struct UartHost * host,struct HdfDeviceObject * device)617 static int32_t UartDevAttach(struct UartHost *host, struct HdfDeviceObject *device)
618 {
619     int32_t ret;
620     struct UartDriverData *udd = NULL;
621 
622     if (device->property == NULL) {
623         HDF_LOGE("%s: property is null", __func__);
624         return HDF_FAILURE;
625     }
626     udd = (struct UartDriverData *)OsalMemCalloc(sizeof(*udd));
627     if (udd == NULL) {
628         HDF_LOGE("%s: OsalMemCalloc udd error", __func__);
629         return HDF_ERR_MALLOC_FAIL;
630     }
631 
632     ret = UartGetDefaultConfigFromHcs(udd, device->property);
633     if (ret != HDF_SUCCESS || udd->id >= APP_UART_ID_MAX) {
634         (void)OsalMemFree(udd);
635         return HDF_FAILURE;
636     }
637 
638     ret = UartGetPinConfigFromHcs(udd, device->property);
639     if (ret != HDF_SUCCESS) {
640         (void)OsalMemFree(udd);
641         return HDF_FAILURE;
642     }
643 
644     udd->state = UART_STATE_NOT_OPENED;
645     udd->config = NULL;
646     udd->eventCallback = NULL;
647     udd->count = 0;
648 
649     udd->params.id = udd->id;
650     udd->params.init.baud_rate = udd->baudrate;
651     udd->attr.dataBits = DEFAULT_DATABITS;
652     udd->attr.stopBits = DEFAULT_STOPBITS;
653     udd->attr.parity = DEFAULT_PARITY;
654 
655     host->priv = udd;
656     host->num = udd->id;
657 
658     return HDF_SUCCESS;
659 }
660 
Gr55xxDetach(struct UartHost * host)661 static void Gr55xxDetach(struct UartHost *host)
662 {
663     struct UartDriverData *udd = NULL;
664 
665     if (host->priv == NULL) {
666         HDF_LOGE("%s: invalid parameter", __func__);
667         return;
668     }
669     udd = (struct UartDriverData *)host->priv;
670     if (udd->state != UART_STATE_NOT_OPENED) {
671         HDF_LOGE("%s: uart driver data state invalid", __func__);
672         return;
673     }
674 
675     (void)OsalMemFree(udd);
676     host->priv = NULL;
677 }
678 
HdfUartDeviceBind(struct HdfDeviceObject * device)679 static int32_t HdfUartDeviceBind(struct HdfDeviceObject *device)
680 {
681     if (device == NULL) {
682         return HDF_ERR_INVALID_OBJECT;
683     }
684     return (UartHostCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS;
685 }
686 
HdfUartDeviceInit(struct HdfDeviceObject * device)687 static int32_t HdfUartDeviceInit(struct HdfDeviceObject *device)
688 {
689     int32_t ret;
690     struct UartHost *host = NULL;
691 
692     if (device == NULL) {
693         HDF_LOGE("%s: device is null", __func__);
694         return HDF_ERR_INVALID_OBJECT;
695     }
696     host = UartHostFromDevice(device);
697     if (host == NULL) {
698         HDF_LOGE("%s: host is null", __func__);
699         return HDF_FAILURE;
700     }
701     ret = UartDevAttach(host, device);
702     if (ret != HDF_SUCCESS) {
703         HDF_LOGE("%s: attach error", __func__);
704         return HDF_FAILURE;
705     }
706     host->method = &g_uartHostMethod;
707     return ret;
708 }
709 
HdfUartDeviceRelease(struct HdfDeviceObject * device)710 static void HdfUartDeviceRelease(struct HdfDeviceObject *device)
711 {
712     struct UartHost *host = NULL;
713 
714     if (device == NULL) {
715         HDF_LOGE("%s: device is null", __func__);
716         return;
717     }
718     host = UartHostFromDevice(device);
719     if (host == NULL) {
720         HDF_LOGE("%s: host is null", __func__);
721         return;
722     }
723     if (host->priv != NULL) {
724         Gr55xxDetach(host);
725     }
726     UartHostDestroy(host);
727 }
728 
729 struct HdfDriverEntry g_hdfUartDevice = {
730     .moduleVersion = 1,
731     .moduleName = "HDF_PLATFORM_UART",
732     .Bind = HdfUartDeviceBind,
733     .Init = HdfUartDeviceInit,
734     .Release = HdfUartDeviceRelease,
735 };
736 
737 HDF_INIT(g_hdfUartDevice);
738