hal: miscellaneous fixes
Fixes for the following issues reported by KW
21725, 21726, 21727, 21737, 21738, 21739, 21740, 21750,
21751, 21752, 25317, 30602, 32620, 36778, 41817, 41819,
50942, 54468, 54470, 54479, 55569, 54481, 55570, 55571,
58485, 85112, 85122, 85123
Change-Id: I9abef07db7ccdc19789a201eb268a97e1b360cad
diff --git a/hal/audio_extn/usb.c b/hal/audio_extn/usb.c
index bc83709..a4ea134 100644
--- a/hal/audio_extn/usb.c
+++ b/hal/audio_extn/usb.c
@@ -159,12 +159,16 @@
char *buffer;
int32_t err = 1;
int32_t size = 0;
- int32_t fd, i, channels_playback;
- char *read_buf, *str_start, *channel_start, *rates_str, *rates_str_for_val,
- *rates_str_start, *next_sr_str, *test, *next_sr_string, *temp_ptr;
+ int32_t fd=-1, i, channels_playback;
+ char *str_start, *channel_start, *rates_str_start, *next_sr_str,
+ *next_sr_string, *temp_ptr;
struct stat st;
- int *rates_supported;
+ char *read_buf = NULL;
+ char *rates_str = NULL;
+ char *rates_str_for_val = NULL;
+ int *rates_supported = NULL;
char path[128];
+ int ret = 0;
memset(&st, 0x0, sizeof(struct stat));
*sample_rate = 0;
@@ -175,45 +179,49 @@
if (fd <0) {
ALOGE("%s: error failed to open config file %s error: %d\n",
__func__, path, errno);
- close(fd);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
if (fstat(fd, &st) < 0) {
ALOGE("%s: error failed to stat %s error %d\n",
__func__, path, errno);
- close(fd);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
file_size = st.st_size;
read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
+
+ if (!read_buf) {
+ ALOGE("Failed to create read_buf");
+ ret = -ENOMEM;
+ goto done;
+ }
+
err = read(fd, read_buf, USB_BUFF_SIZE);
str_start = strstr(read_buf, type);
if (str_start == NULL) {
ALOGE("%s: error %s section not found in usb config file",
__func__, type);
- close(fd);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
channel_start = strstr(str_start, "Channels:");
if (channel_start == NULL) {
ALOGE("%s: error could not find Channels information", __func__);
- close(fd);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
channel_start = strstr(channel_start, " ");
if (channel_start == NULL) {
ALOGE("%s: error channel section not found in usb config file",
__func__);
- close(fd);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
channels_playback = atoi(channel_start);
@@ -227,44 +235,38 @@
rates_str_start = strstr(str_start, "Rates:");
if (rates_str_start == NULL) {
ALOGE("%s: error cant find rates information", __func__);
- close(fd);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
rates_str_start = strstr(rates_str_start, " ");
if (rates_str_start == NULL) {
ALOGE("%s: error channel section not found in usb config file",
__func__);
- close(fd);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
char *target = strchr(rates_str_start, '\n');
if (target == NULL) {
ALOGE("%s: error end of line not found", __func__);
- close(fd);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
size = target - rates_str_start;
if ((rates_str = (char *)malloc(size + 1)) == NULL) {
ALOGE("%s: error unable to allocate memory to hold sample rate strings",
__func__);
- close(fd);
- free(read_buf);
- return -ENOMEM;
+ ret = -EINVAL;
+ goto done;
}
if ((rates_str_for_val = (char *)malloc(size + 1)) == NULL) {
ALOGE("%s: error unable to allocate memory to hold sample rate string",
__func__);
- close(fd);
- free(rates_str);
- free(read_buf);
- return -ENOMEM;
+ ret = -EINVAL;
+ goto done;
}
memcpy(rates_str, rates_str_start, size);
@@ -275,23 +277,23 @@
size = usb_get_numof_rates(rates_str);
if (!size) {
ALOGE("%s: error could not get rate size, returning", __func__);
- close(fd);
- free(rates_str_for_val);
- free(rates_str);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
rates_supported = (int *)malloc(sizeof(int) * size);
+
+ if (!rates_supported) {
+ ALOGE("couldn't allocate mem for rates_supported");
+ ret = -EINVAL;
+ goto done;
+ }
+
next_sr_string = strtok_r(rates_str_for_val, " ,", &temp_ptr);
if (next_sr_string == NULL) {
ALOGE("%s: error could not get first rate val", __func__);
- close(fd);
- free(rates_str_for_val);
- free(rates_str);
- free(rates_supported);
- free(read_buf);
- return -EINVAL;
+ ret = -EINVAL;
+ goto done;
}
rates_supported[0] = atoi(next_sr_string);
@@ -299,6 +301,10 @@
__func__, rates_supported[0]);
for (i = 1; i<size; i++) {
next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
+ if (next_sr_string == NULL) {
+ rates_supported[i] = -1; // fill in an invalid sr for the rest
+ continue;
+ }
rates_supported[i] = atoi(next_sr_string);
ALOGD("rates_supported[%d] for playback: %d",i, rates_supported[i]);
}
@@ -317,12 +323,13 @@
}
ALOGD("%s: sample_rate: %d", __func__, *sample_rate);
- close(fd);
- free(rates_str_for_val);
- free(rates_str);
- free(rates_supported);
- free(read_buf);
- return 0;
+done:
+ if (fd >= 0) close(fd);
+ if (rates_str_for_val) free(rates_str_for_val);
+ if (rates_str) free(rates_str);
+ if (rates_supported) free(rates_supported);
+ if (read_buf) free(read_buf);
+ return ret;
}
static int32_t usb_playback_entry(void *adev)
@@ -387,7 +394,6 @@
if(usbmod->proxy_pcm_playback_handle
&& !pcm_is_ready(usbmod->proxy_pcm_playback_handle)){
pcm_close(usbmod->proxy_pcm_playback_handle);
- usbmod->proxy_pcm_playback_handle = NULL;
proxy_open_retry_count--;
usleep(USB_PROXY_OPEN_WAIT_TIME * 1000);
ALOGE("%s: pcm_open for proxy failed retrying = %d",
@@ -507,7 +513,6 @@
if(usbmod->proxy_pcm_record_handle
&& !pcm_is_ready(usbmod->proxy_pcm_record_handle)){
pcm_close(usbmod->proxy_pcm_record_handle);
- usbmod->proxy_pcm_record_handle = NULL;
proxy_open_retry_count--;
usleep(USB_PROXY_OPEN_WAIT_TIME * 1000);
ALOGE("%s: pcm_open for proxy(recording) failed retrying = %d",