Check if the lower half is initialized for af_channel and af_data

Signed-off-by: zhangkai25 <zhangkai25@xiaomi.com>
This commit is contained in:
zhangkai25 2024-09-27 19:44:22 +08:00 committed by Xiang Xiao
parent e73c32b848
commit 323b6bdea9
1 changed files with 27 additions and 9 deletions

View File

@ -729,7 +729,8 @@ static int adc_samples_on_read(FAR struct adc_dev_s *dev)
int adc_register(FAR const char *path, FAR struct adc_dev_s *dev)
{
FAR struct adc_fifo_s *fifo = &dev->ad_recv;
uint16_t fifosize;
bool alloc_channel = false;
bool alloc_data = false;
int ret;
DEBUGASSERT(path != NULL && dev != NULL);
@ -760,24 +761,37 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev)
/* Malloc for af_channale and af_data */
fifosize = fifo->af_fifosize;
if (fifosize == 0)
if (fifo->af_fifosize == 0)
{
fifo->af_fifosize = CONFIG_ADC_FIFOSIZE;
fifo->af_channel = kmm_malloc(fifo->af_fifosize);
}
if (fifo->af_channel == NULL)
{
fifo->af_channel = kmm_malloc(fifo->af_fifosize);
if (fifo->af_channel == NULL)
{
return -ENOMEM;
}
fifo->af_data = kmm_malloc(fifo->af_fifosize * 4);
alloc_channel = true;
}
if (fifo->af_data == NULL)
{
fifo->af_data = kmm_malloc(fifo->af_fifosize *
sizeof(*(fifo->af_data)));
if (fifo->af_data == NULL)
{
if (alloc_channel)
{
kmm_free(fifo->af_channel);
}
return -ENOMEM;
}
alloc_data = true;
}
/* Register the ADC character driver */
@ -785,9 +799,13 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev)
ret = register_driver(path, &g_adc_fops, 0444, dev);
if (ret < 0)
{
if (fifosize == 0)
if (alloc_channel)
{
kmm_free(fifo->af_channel);
}
if (alloc_data)
{
kmm_free(fifo->af_data);
}