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