Skip to content

Commit de57d85

Browse files
committed
drivers: usb: udc: stm32: test HAL return value
Add missing test of some HAL functions return value;. Signed-off-by: Etienne Carriere <[email protected]>
1 parent 8c13c07 commit de57d85

File tree

1 file changed

+60
-20
lines changed

1 file changed

+60
-20
lines changed

drivers/usb/udc/udc_stm32.c

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,21 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
190190
const struct device *dev = priv->dev;
191191
const struct udc_stm32_config *cfg = dev->config;
192192
struct udc_ep_config *ep;
193+
HAL_StatusTypeDef __maybe_unused hal_ret;
193194

194195
/* Re-Enable control endpoints */
195196
ep = udc_get_ep_cfg(dev, USB_CONTROL_EP_OUT);
196197
if (ep && ep->stat.enabled) {
197-
HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_OUT, cfg->ep0_mps,
198-
EP_TYPE_CTRL);
198+
hal_ret = HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_OUT, cfg->ep0_mps,
199+
EP_TYPE_CTRL);
200+
__ASSERT_NO_MSG(hal_ret == HAL_OK);
199201
}
200202

201203
ep = udc_get_ep_cfg(dev, USB_CONTROL_EP_IN);
202204
if (ep && ep->stat.enabled) {
203-
HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_IN, cfg->ep0_mps,
204-
EP_TYPE_CTRL);
205+
hal_ret = HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_IN, cfg->ep0_mps,
206+
EP_TYPE_CTRL);
207+
__ASSERT_NO_MSG(hal_ret == HAL_OK);
205208
}
206209

207210
udc_set_suspended(dev, false);
@@ -271,7 +274,9 @@ static int usbd_ctrl_feed_dout(const struct device *dev, const size_t length)
271274

272275
k_fifo_put(&cfg->fifo, buf);
273276

274-
HAL_PCD_EP_Receive(&priv->pcd, cfg->addr, buf->data, buf->size);
277+
if (HAL_PCD_EP_Receive(&priv->pcd, cfg->addr, buf->data, buf->size) != HAL_OK) {
278+
return -EIO;
279+
}
275280

276281
return 0;
277282
}
@@ -280,8 +285,10 @@ static void udc_stm32_flush_tx_fifo(const struct device *dev)
280285
{
281286
struct udc_stm32_data *priv = udc_get_private(dev);
282287
struct udc_ep_config *cfg = udc_get_ep_cfg(dev, USB_CONTROL_EP_OUT);
288+
HAL_StatusTypeDef __maybe_unused hal_ret;
283289

284290
HAL_PCD_EP_Receive(&priv->pcd, cfg->addr, NULL, 0);
291+
__ASSERT_NO_MSG(hal_ret == HAL_OK);
285292
}
286293

287294
static int udc_stm32_tx(const struct device *dev, struct udc_ep_config *epcfg,
@@ -432,6 +439,7 @@ static void handle_msg_data_in(struct udc_stm32_data *priv, uint8_t epnum)
432439
struct udc_ep_config *epcfg;
433440
uint8_t ep = epnum | USB_EP_DIR_IN;
434441
struct net_buf *buf;
442+
HAL_StatusTypeDef hal_ret;
435443

436444
LOG_DBG("DataIn ep 0x%02x", ep);
437445

@@ -447,7 +455,12 @@ static void handle_msg_data_in(struct udc_stm32_data *priv, uint8_t epnum)
447455
const struct udc_stm32_config *cfg = dev->config;
448456
uint32_t len = MIN(cfg->ep0_mps, buf->len);
449457

450-
HAL_PCD_EP_Transmit(&priv->pcd, ep, buf->data, len);
458+
hal_ret = HAL_PCD_EP_Transmit(&priv->pcd, ep, buf->data, len);
459+
if (hal_ret != HAL_OK) {
460+
LOG_ERR("HAL_PCD_EP_Transmit failed: %d", hal_ret);
461+
__ASSERT_NO_MSG(0);
462+
return;
463+
}
451464

452465
buf->len -= len;
453466
buf->data += len;
@@ -457,7 +470,11 @@ static void handle_msg_data_in(struct udc_stm32_data *priv, uint8_t epnum)
457470

458471
if (udc_ep_buf_has_zlp(buf)) {
459472
udc_ep_buf_clear_zlp(buf);
460-
HAL_PCD_EP_Transmit(&priv->pcd, ep, buf->data, 0);
473+
hal_ret = HAL_PCD_EP_Transmit(&priv->pcd, ep, buf->data, 0);
474+
if (hal_ret != HAL_OK) {
475+
LOG_ERR("HAL_PCD_EP_Transmit failed: %d", hal_ret);
476+
__ASSERT_NO_MSG(0);
477+
}
461478

462479
return;
463480
}
@@ -497,6 +514,7 @@ static void handle_msg_setup(struct udc_stm32_data *priv)
497514
{
498515
struct usb_setup_packet *setup = (void *)priv->pcd.Setup;
499516
const struct device *dev = priv->dev;
517+
HAL_StatusTypeDef hal_ret;
500518
struct net_buf *buf;
501519
int err;
502520

@@ -518,7 +536,11 @@ static void handle_msg_setup(struct udc_stm32_data *priv)
518536

519537
if ((setup->bmRequestType == 0) && (setup->bRequest == USB_SREQ_SET_ADDRESS)) {
520538
/* HAL requires we set the address before submitting status */
521-
HAL_PCD_SetAddress(&priv->pcd, setup->wValue);
539+
hal_ret = HAL_PCD_SetAddress(&priv->pcd, setup->wValue);
540+
if (hal_ret != HAL_OK) {
541+
LOG_ERR("HAL_PCD_SetAddress() fialed: %d", hal_ret);
542+
__ASSERT_NO_MSG(0);
543+
}
522544
}
523545

524546
if (udc_ctrl_stage_is_data_out(dev)) {
@@ -592,18 +614,22 @@ int udc_stm32_init(const struct device *dev)
592614
return -EIO;
593615
}
594616

595-
HAL_PCD_Stop(&priv->pcd);
617+
if (HAL_PCD_Stop(&priv->pcd) != HAL_OK) {
618+
return -EIO;
619+
}
596620

597621
return 0;
598622
}
599623

600624
#if defined(USB) || defined(USB_DRD_FS)
601-
static inline void udc_stm32_mem_init(const struct device *dev)
625+
static int udc_stm32_mem_init(const struct device *dev)
602626
{
603627
struct udc_stm32_data *priv = udc_get_private(dev);
604628
const struct udc_stm32_config *cfg = dev->config;
605629

606630
priv->occupied_mem = cfg->pma_offset;
631+
632+
return 0;
607633
}
608634

609635
static int udc_stm32_ep_mem_config(const struct device *dev,
@@ -627,15 +653,16 @@ static int udc_stm32_ep_mem_config(const struct device *dev,
627653
}
628654

629655
/* Configure PMA offset for the endpoint */
630-
HAL_PCDEx_PMAConfig(&priv->pcd, ep->addr, PCD_SNG_BUF,
631-
priv->occupied_mem);
656+
if (HAL_PCDEx_PMAConfig(&priv->pcd, ep->addr, PCD_SNG_BUF, priv->occupied_mem) != HAL_OK) {
657+
return -EIO;
658+
}
632659

633660
priv->occupied_mem += size;
634661

635662
return 0;
636663
}
637664
#else
638-
static void udc_stm32_mem_init(const struct device *dev)
665+
static int udc_stm32_mem_init(const struct device *dev)
639666
{
640667
struct udc_stm32_data *priv = udc_get_private(dev);
641668
const struct udc_stm32_config *cfg = dev->config;
@@ -646,23 +673,29 @@ static void udc_stm32_mem_init(const struct device *dev)
646673
if (cfg->ep_mps % 4 || cfg->ep0_mps % 4) {
647674
LOG_ERR("Not a 32-bit word multiple: ep0(%u)|ep(%u)",
648675
cfg->ep0_mps, cfg->ep_mps);
649-
return;
676+
return -EINVAL;
650677
}
651678

652679
/* The documentation is not clear at all about RX FiFo size requirement,
653680
* 160 has been selected through trial and error.
654681
*/
655682
words = MAX(160, cfg->ep_mps / 4);
656-
HAL_PCDEx_SetRxFiFo(&priv->pcd, words);
683+
if (HAL_PCDEx_SetRxFiFo(&priv->pcd, words) != HAL_OK) {
684+
return -EIO;
685+
}
657686
priv->occupied_mem = words * 4;
658687

659688
/* For EP0 TX, reserve only one MPS */
660-
HAL_PCDEx_SetTxFiFo(&priv->pcd, 0, cfg->ep0_mps / 4);
689+
if (HAL_PCDEx_SetTxFiFo(&priv->pcd, 0, cfg->ep0_mps / 4) != HAL_OK) {
690+
return -EIO;
691+
}
661692
priv->occupied_mem += cfg->ep0_mps;
662693

663694
/* Reset TX allocs */
664695
for (unsigned int i = 1U; i < cfg->num_endpoints; i++) {
665-
HAL_PCDEx_SetTxFiFo(&priv->pcd, i, 0);
696+
if (HAL_PCDEx_SetTxFiFo(&priv->pcd, i, 0) != HAL_OK) {
697+
return -EIO;
698+
}
666699
}
667700
}
668701

@@ -685,7 +718,9 @@ static int udc_stm32_ep_mem_config(const struct device *dev,
685718
if (priv->occupied_mem >= (words * 4)) {
686719
priv->occupied_mem -= (words * 4);
687720
}
688-
HAL_PCDEx_SetTxFiFo(&priv->pcd, USB_EP_GET_IDX(ep->addr), 0);
721+
if (HAL_PCDEx_SetTxFiFo(&priv->pcd, USB_EP_GET_IDX(ep->addr), 0) != HAL_OK) {
722+
return -EIO;
723+
}
689724
return 0;
690725
}
691726

@@ -694,7 +729,9 @@ static int udc_stm32_ep_mem_config(const struct device *dev,
694729
return -ENOMEM;
695730
}
696731

697-
HAL_PCDEx_SetTxFiFo(&priv->pcd, USB_EP_GET_IDX(ep->addr), words);
732+
if (HAL_PCDEx_SetTxFiFo(&priv->pcd, USB_EP_GET_IDX(ep->addr), words) != HAL_OK) {
733+
return -EIO;
734+
}
698735

699736
priv->occupied_mem += words * 4;
700737

@@ -711,7 +748,10 @@ static int udc_stm32_enable(const struct device *dev)
711748

712749
LOG_DBG("Enable UDC");
713750

714-
udc_stm32_mem_init(dev);
751+
ret = udc_stm32_mem_init(dev);
752+
if (ret != 0) {
753+
return ret;
754+
}
715755

716756
status = HAL_PCD_Start(&priv->pcd);
717757
if (status != HAL_OK) {

0 commit comments

Comments
 (0)