Skip to content

Commit 581c20e

Browse files
nordic-krchnashif
authored andcommitted
drivers: uart: Cleanup not supported API handling
Fixed discrepancy between documentation and actual returned error code when API is not enabled or not supported by a device. Added detection of case when device does not implement uart_callback_set but ASYNC api is enabled. Returning -ENOSYS in that case. Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
1 parent b4b1212 commit 581c20e

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

include/drivers/uart.h

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,16 @@ __subsystem struct uart_driver_api {
447447
/**
448448
* @brief Set event handler function.
449449
*
450+
* Since it is mandatory to set callback to use other asynchronous functions,
451+
* it can be used to detect if the device supports asynchronous API. Remaining
452+
* API does not have that detection.
453+
*
450454
* @param dev UART device structure.
451455
* @param callback Event handler.
452456
* @param user_data Data to pass to event handler function.
453457
*
454-
* @retval -ENOTSUP If not supported.
458+
* @retval -ENOSYS If not supported by the device.
459+
* @retval -ENOTSUP If API not enabled.
455460
* @retval 0 If successful, negative errno code otherwise.
456461
*/
457462
static inline int uart_callback_set(const struct device *dev,
@@ -462,6 +467,10 @@ static inline int uart_callback_set(const struct device *dev,
462467
const struct uart_driver_api *api =
463468
(const struct uart_driver_api *)dev->api;
464469

470+
if (api->callback_set == NULL) {
471+
return -ENOSYS;
472+
}
473+
465474
return api->callback_set(dev, callback, user_data);
466475
#else
467476
return -ENOTSUP;
@@ -637,6 +646,7 @@ static inline int z_impl_uart_rx_disable(const struct device *dev)
637646
*
638647
* @retval uart_rx_stop_reason If error during receiving occurred.
639648
* @retval 0 Otherwise.
649+
* @retval -ENOSYS If this function is not supported.
640650
*/
641651
__syscall int uart_err_check(const struct device *dev);
642652

@@ -648,6 +658,7 @@ static inline int z_impl_uart_err_check(const struct device *dev)
648658
if (api->err_check == NULL) {
649659
return -ENOSYS;
650660
}
661+
651662
return api->err_check(dev);
652663
}
653664

@@ -661,7 +672,7 @@ static inline int z_impl_uart_err_check(const struct device *dev)
661672
* @retval 0 If a character arrived.
662673
* @retval -1 If no character was available to read (i.e., the UART
663674
* input buffer was empty).
664-
* @retval -ENOTSUP If the operation is not supported.
675+
* @retval -ENOSYS If the operation is not supported.
665676
* @retval -EBUSY If reception was enabled using uart_rx_enabled
666677
*/
667678
__syscall int uart_poll_in(const struct device *dev, unsigned char *p_char);
@@ -672,6 +683,10 @@ static inline int z_impl_uart_poll_in(const struct device *dev,
672683
const struct uart_driver_api *api =
673684
(const struct uart_driver_api *)dev->api;
674685

686+
if (api->poll_in == NULL) {
687+
return -ENOSYS;
688+
}
689+
675690
return api->poll_in(dev, p_char);
676691
}
677692

@@ -737,7 +752,7 @@ static inline int z_impl_uart_configure(const struct device *dev,
737752
* @param dev UART device structure.
738753
* @param cfg UART configuration structure.
739754
*
740-
* @retval -ENOTSUP If driver does not support getting current configuration.
755+
* @retval -ENOSYS If driver does not support getting current configuration.
741756
* @retval 0 If successful, negative errno code otherwise.
742757
*/
743758
__syscall int uart_config_get(const struct device *dev,
@@ -754,7 +769,6 @@ static inline int z_impl_uart_config_get(const struct device *dev,
754769
}
755770

756771
return api->config_get(dev, cfg);
757-
758772
}
759773

760774
/**
@@ -774,6 +788,8 @@ static inline int z_impl_uart_config_get(const struct device *dev,
774788
* @param size Number of bytes to send.
775789
*
776790
* @return Number of bytes sent.
791+
* @retval -ENOSYS if this function is not supported
792+
* @retval -ENOTSUP if API is not enabled.
777793
*/
778794
static inline int uart_fifo_fill(const struct device *dev,
779795
const uint8_t *tx_data,
@@ -783,12 +799,14 @@ static inline int uart_fifo_fill(const struct device *dev,
783799
const struct uart_driver_api *api =
784800
(const struct uart_driver_api *)dev->api;
785801

786-
if (api->fifo_fill != NULL) {
787-
return api->fifo_fill(dev, tx_data, size);
802+
if (api->fifo_fill == NULL) {
803+
return -ENOSYS;
788804
}
805+
806+
return api->fifo_fill(dev, tx_data, size);
789807
#endif
790808

791-
return 0;
809+
return -ENOTSUP;
792810
}
793811

794812
/**
@@ -812,6 +830,8 @@ static inline int uart_fifo_fill(const struct device *dev,
812830
* @param size Container size.
813831
*
814832
* @return Number of bytes read.
833+
* @retval -ENOSYS if this function is not supported.
834+
* @retval -ENOTSUP if API is not enabled.
815835
*/
816836
static inline int uart_fifo_read(const struct device *dev, uint8_t *rx_data,
817837
const int size)
@@ -820,12 +840,14 @@ static inline int uart_fifo_read(const struct device *dev, uint8_t *rx_data,
820840
const struct uart_driver_api *api =
821841
(const struct uart_driver_api *)dev->api;
822842

823-
if (api->fifo_read != NULL) {
824-
return api->fifo_read(dev, rx_data, size);
843+
if (api->fifo_read == NULL) {
844+
return -ENOSYS;
825845
}
846+
847+
return api->fifo_read(dev, rx_data, size);
826848
#endif
827849

828-
return 0;
850+
return -ENOTSUP;
829851
}
830852

831853
/**
@@ -882,20 +904,24 @@ static inline void z_impl_uart_irq_tx_disable(const struct device *dev)
882904
* @param dev UART device structure.
883905
*
884906
* @retval 1 If at least one char can be written to UART.
885-
* @retval 0 Otherwise.
907+
* @retval 0 If device is not ready to write a new byte.
908+
* @retval -ENOSYS if this function is not supported.
909+
* @retval -ENOTSUP if API is not enabled.
886910
*/
887911
static inline int uart_irq_tx_ready(const struct device *dev)
888912
{
889913
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
890914
const struct uart_driver_api *api =
891915
(const struct uart_driver_api *)dev->api;
892916

893-
if (api->irq_tx_ready != NULL) {
894-
return api->irq_tx_ready(dev);
917+
if (api->irq_tx_ready == NULL) {
918+
return -ENOSYS;
895919
}
920+
921+
return api->irq_tx_ready(dev);
896922
#endif
897923

898-
return 0;
924+
return -ENOTSUP;
899925
}
900926

901927
/**
@@ -955,8 +981,9 @@ static inline void z_impl_uart_irq_rx_disable(const struct device *dev)
955981
* @param dev UART device structure.
956982
*
957983
* @retval 1 If nothing remains to be transmitted.
958-
* @retval 0 Otherwise.
959-
* @retval -ENOTSUP if this function is not supported
984+
* @retval 0 If transmission is not completed.
985+
* @retval -ENOSYS if this function is not supported.
986+
* @retval -ENOTSUP if API is not enabled.
960987
*/
961988
static inline int uart_irq_tx_complete(const struct device *dev)
962989
{
@@ -990,8 +1017,9 @@ static inline int uart_irq_tx_complete(const struct device *dev)
9901017
* @param dev UART device structure.
9911018
*
9921019
* @retval 1 If a received char is ready.
993-
* @retval 0 Otherwise.
994-
* @retval -ENOTSUP if this function is not supported
1020+
* @retval 0 If a received char is not ready.
1021+
* @retval -ENOSYS if this function is not supported.
1022+
* @retval -ENOTSUP if API is not enabled.
9951023
*/
9961024
static inline int uart_irq_rx_ready(const struct device *dev)
9971025
{
@@ -1005,7 +1033,7 @@ static inline int uart_irq_rx_ready(const struct device *dev)
10051033
return api->irq_rx_ready(dev);
10061034
#endif
10071035

1008-
return 0;
1036+
return -ENOTSUP;
10091037
}
10101038
/**
10111039
* @brief Enable error interrupt.
@@ -1056,7 +1084,9 @@ static inline void z_impl_uart_irq_err_disable(const struct device *dev)
10561084
* @param dev UART device structure.
10571085
*
10581086
* @retval 1 If an IRQ is pending.
1059-
* @retval 0 Otherwise.
1087+
* @retval 0 If an IRQ is not pending.
1088+
* @retval -ENOSYS if this function is not supported.
1089+
* @retval -ENOTSUP if API is not enabled.
10601090
*/
10611091
__syscall int uart_irq_is_pending(const struct device *dev);
10621092

@@ -1071,7 +1101,7 @@ static inline int z_impl_uart_irq_is_pending(const struct device *dev)
10711101
}
10721102
return api->irq_is_pending(dev);
10731103
#endif
1074-
return 0;
1104+
return -ENOTSUP;
10751105
}
10761106

10771107
/**
@@ -1095,7 +1125,9 @@ static inline int z_impl_uart_irq_is_pending(const struct device *dev)
10951125
*
10961126
* @param dev UART device structure.
10971127
*
1098-
* @retval 1 Always.
1128+
* @retval -ENOSYS if this function is not supported.
1129+
* @retval -ENOTSUP if API is not enabled.
1130+
* @retval 1 On success.
10991131
*/
11001132
__syscall int uart_irq_update(const struct device *dev);
11011133

@@ -1110,7 +1142,7 @@ static inline int z_impl_uart_irq_update(const struct device *dev)
11101142
}
11111143
return api->irq_update(dev);
11121144
#endif
1113-
return 0;
1145+
return -ENOTSUP;
11141146
}
11151147

11161148
/**
@@ -1166,7 +1198,9 @@ static inline void uart_irq_callback_set(const struct device *dev,
11661198
* @param val Value to set to the line control.
11671199
*
11681200
* @retval 0 If successful.
1169-
* @retval failed Otherwise.
1201+
* @retval -ENOSYS if this function is not supported.
1202+
* @retval -ENOTSUP if API is not enabled.
1203+
* @retval negative value if failed.
11701204
*/
11711205
__syscall int uart_line_ctrl_set(const struct device *dev,
11721206
uint32_t ctrl, uint32_t val);
@@ -1195,7 +1229,9 @@ static inline int z_impl_uart_line_ctrl_set(const struct device *dev,
11951229
* @param val Pointer to variable where to store the line control value.
11961230
*
11971231
* @retval 0 If successful.
1198-
* @retval failed Otherwise.
1232+
* @retval -ENOSYS if this function is not supported.
1233+
* @retval -ENOTSUP if API is not enabled.
1234+
* @retval negative value if failed.
11991235
*/
12001236
__syscall int uart_line_ctrl_get(const struct device *dev, uint32_t ctrl,
12011237
uint32_t *val);
@@ -1227,7 +1263,9 @@ static inline int z_impl_uart_line_ctrl_get(const struct device *dev,
12271263
* @param p Parameter to the command.
12281264
*
12291265
* @retval 0 If successful.
1230-
* @retval failed Otherwise.
1266+
* @retval -ENOSYS if this function is not supported.
1267+
* @retval -ENOTSUP if API is not enabled.
1268+
* @retval negative value if failed.
12311269
*/
12321270
__syscall int uart_drv_cmd(const struct device *dev, uint32_t cmd, uint32_t p);
12331271

0 commit comments

Comments
 (0)