blob: e6859fe31e5ab6beb3680da95a670df3d77a331d [file] [log] [blame]
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2013, 2016-2019 The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_usb"
21#define LOG_NDEBUG 0
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
25#include <pthread.h>
26#include <stdlib.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080027#include <log/log.h>
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -070028#include <cutils/str_parms.h>
29#include <sys/ioctl.h>
30#include <fcntl.h>
31#include <sys/stat.h>
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -070032#include <system/audio.h>
33#include <tinyalsa/asoundlib.h>
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080034#include <audio_hw.h>
35#include <cutils/properties.h>
Kuirong Wang1cad7142016-05-24 15:21:56 -070036#include <ctype.h>
37#include <math.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053038#include <unistd.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080039#include "audio_extn.h"
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -070040
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053041#ifdef DYNAMIC_LOG_ENABLED
42#include <log_xml_parser.h>
43#define LOG_MASK HAL_MOD_FILE_USB
44#include <log_utils.h>
45#endif
46
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080047#define USB_BUFF_SIZE 2048
48#define CHANNEL_NUMBER_STR "Channels: "
49#define PLAYBACK_PROFILE_STR "Playback:"
50#define CAPTURE_PROFILE_STR "Capture:"
Garmond Leung5fd0b552018-04-17 11:56:12 -070051#define DATA_PACKET_INTERVAL_STR "Data packet interval:"
Kuirong Wang1cad7142016-05-24 15:21:56 -070052#define USB_SIDETONE_GAIN_STR "usb_sidetone_gain"
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080053#define ABS_SUB(A, B) (((A) > (B)) ? ((A) - (B)):((B) - (A)))
54#define SAMPLE_RATE_8000 8000
55#define SAMPLE_RATE_11025 11025
Weiyin Jiangcba6f962018-03-18 11:52:05 +080056#define SAMPLE_RATE_192000 192000
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080057// Supported sample rates for USB
Carter Hsu32a62362018-10-15 15:01:42 -070058#define USBID_SIZE 16
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080059static uint32_t supported_sample_rates[] =
Haynes Mathew George484e8d22017-07-31 18:55:17 -070060 {384000, 352800, 192000, 176400, 96000, 88200, 64000, 48000, 44100, 32000, 22050, 16000, 11025, 8000};
61static uint32_t supported_sample_rates_mask[2];
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080062
63#define MAX_SAMPLE_RATE_SIZE sizeof(supported_sample_rates)/sizeof(supported_sample_rates[0])
64
Garmond Leung5fd0b552018-04-17 11:56:12 -070065#define DEFAULT_SERVICE_INTERVAL_US 0
66
Haynes Mathew George484e8d22017-07-31 18:55:17 -070067#define _MAX(x, y) (((x) >= (y)) ? (x) : (y))
68#define _MIN(x, y) (((x) <= (y)) ? (x) : (y))
69
Garmond Leung5fd0b552018-04-17 11:56:12 -070070typedef enum usb_usecase_type{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080071 USB_PLAYBACK = 0,
72 USB_CAPTURE,
Garmond Leung5fd0b552018-04-17 11:56:12 -070073} usb_usecase_type_t;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080074
Kuirong Wang1cad7142016-05-24 15:21:56 -070075enum {
76 USB_SIDETONE_ENABLE_INDEX = 0,
77 USB_SIDETONE_VOLUME_INDEX,
78 USB_SIDETONE_MAX_INDEX,
79};
80
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080081struct usb_device_config {
82 struct listnode list;
83 unsigned int bit_width;
84 unsigned int channels;
85 unsigned int rate_size;
86 unsigned int rates[MAX_SAMPLE_RATE_SIZE];
Garmond Leung5fd0b552018-04-17 11:56:12 -070087 unsigned long service_interval_us;
88 usb_usecase_type_t type;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -080089};
90
91struct usb_card_config {
92 struct listnode list;
93 audio_devices_t usb_device_type;
94 int usb_card;
95 struct listnode usb_device_conf_list;
Kuirong Wang1cad7142016-05-24 15:21:56 -070096 struct mixer *usb_snd_mixer;
97 int usb_sidetone_index[USB_SIDETONE_MAX_INDEX];
98 int usb_sidetone_vol_min;
99 int usb_sidetone_vol_max;
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700100 int endian;
Carter Hsu32a62362018-10-15 15:01:42 -0700101 char usbid[USBID_SIZE];
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800102};
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700103
104struct usb_module {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800105 struct listnode usb_card_conf_list;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700106 struct audio_device *adev;
Kuirong Wang1cad7142016-05-24 15:21:56 -0700107 int sidetone_gain;
Ashish Jain3e37a702016-11-25 12:27:15 +0530108 bool is_capture_supported;
Garmond Leung5fd0b552018-04-17 11:56:12 -0700109 bool usb_reconfig;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700110};
111
112static struct usb_module *usbmod = NULL;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800113static bool usb_audio_debug_enable = false;
Kuirong Wang1cad7142016-05-24 15:21:56 -0700114static int usb_sidetone_gain = 0;
115
116static const char * const usb_sidetone_enable_str[] = {
117 "Sidetone Playback Switch",
Kuirong Wang4ddd75f2016-09-21 11:20:31 -0700118 "Mic Playback Switch",
Kuirong Wang1cad7142016-05-24 15:21:56 -0700119};
120
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800121static const char * const usb_sidetone_volume_str[] = {
122 "Sidetone Playback Volume",
123 "Mic Playback Volume",
124};
125
126static int usb_get_sidetone_gain(struct usb_card_config *card_info)
127{
128 int gain = card_info->usb_sidetone_vol_min + usbmod->sidetone_gain;
129 if (gain > card_info->usb_sidetone_vol_max)
130 gain = card_info->usb_sidetone_vol_max;
131 return gain;
132}
133
134static void usb_get_sidetone_volume(struct usb_card_config *usb_card_info)
135{
136 struct mixer_ctl *ctl;
137 unsigned int index;
138
139 if (!audio_extn_usb_is_sidetone_volume_enabled())
140 return;
141
142 for (index = 0;
143 index < sizeof(usb_sidetone_volume_str)/sizeof(usb_sidetone_volume_str[0]);
144 index++) {
145 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
146 usb_sidetone_volume_str[index]);
147 if (ctl) {
148 usb_card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX] = index;
149 usb_card_info->usb_sidetone_vol_min = mixer_ctl_get_range_min(ctl);
150 usb_card_info->usb_sidetone_vol_max = mixer_ctl_get_range_max(ctl);
151 break;
152 }
153 }
154}
155
156static void usb_set_sidetone_volume(struct usb_card_config *usb_card_info,
157 bool enable, int index)
158{
159 struct mixer_ctl *ctl;
160
161 if (!audio_extn_usb_is_sidetone_volume_enabled())
162 return;
163
164 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
165 usb_sidetone_volume_str[index]);
166
167 if (ctl == NULL)
168 ALOGV("%s: sidetone gain mixer command is not found",
169 __func__);
170 else if (enable)
171 mixer_ctl_set_value(ctl, 0,
172 usb_get_sidetone_gain(usb_card_info));
173}
174
175
176
Kuirong Wang1cad7142016-05-24 15:21:56 -0700177static void usb_mixer_print_enum(struct mixer_ctl *ctl)
178{
179 unsigned int num_enums;
180 unsigned int i;
181 const char *string;
182
183 num_enums = mixer_ctl_get_num_enums(ctl);
184
185 for (i = 0; i < num_enums; i++) {
186 string = mixer_ctl_get_enum_string(ctl, i);
187 ALOGI("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "", string);
188 }
189}
190
191static void usb_soundcard_detail_control(struct mixer *mixer, const char *control)
192{
193 struct mixer_ctl *ctl;
194 enum mixer_ctl_type type;
195 unsigned int num_values;
196 unsigned int i;
197 int min, max;
198
199 if (isdigit(control[0]))
200 ctl = mixer_get_ctl(mixer, atoi(control));
201 else
202 ctl = mixer_get_ctl_by_name(mixer, control);
203
204 if (!ctl) {
205 fprintf(stderr, "Invalid mixer control\n");
206 return;
207 }
208
209 type = mixer_ctl_get_type(ctl);
210 num_values = mixer_ctl_get_num_values(ctl);
211
212 ALOGI("%s:", mixer_ctl_get_name(ctl));
213
214 for (i = 0; i < num_values; i++) {
215 switch (type) {
216 case MIXER_CTL_TYPE_INT:
217 ALOGI(" %d", mixer_ctl_get_value(ctl, i));
218 break;
219 case MIXER_CTL_TYPE_BOOL:
220 ALOGI(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
221 break;
222 case MIXER_CTL_TYPE_ENUM:
223 usb_mixer_print_enum(ctl);
224 break;
225 case MIXER_CTL_TYPE_BYTE:
226 ALOGI(" 0x%02x", mixer_ctl_get_value(ctl, i));
227 break;
228 default:
229 ALOGI(" unknown");
230 break;
231 }
232 }
233
234 if (type == MIXER_CTL_TYPE_INT) {
235 min = mixer_ctl_get_range_min(ctl);
236 max = mixer_ctl_get_range_max(ctl);
237 ALOGI(" (range %d->%d)", min, max);
238 }
239}
240
241static void usb_soundcard_list_controls(struct mixer *mixer)
242{
243 struct mixer_ctl *ctl;
244 const char *name, *type;
245 unsigned int num_ctls, num_values;
246 unsigned int i;
247
248 num_ctls = mixer_get_num_ctls(mixer);
249
250 ALOGI("Number of controls: %d\n", num_ctls);
251
252 ALOGI("ctl\ttype\tnum\t%-40s value\n", "name");
253 for (i = 0; i < num_ctls; i++) {
254 ctl = mixer_get_ctl(mixer, i);
255 if (ctl != NULL) {
256 name = mixer_ctl_get_name(ctl);
257 type = mixer_ctl_get_type_string(ctl);
258 num_values = mixer_ctl_get_num_values(ctl);
259 ALOGI("%d\t%s\t%d\t%-40s", i, type, num_values, name);
260 if (name != NULL)
261 usb_soundcard_detail_control(mixer, name);
262 }
263 }
264}
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700265
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800266static int usb_set_dev_id_mixer_ctl(unsigned int usb_usecase_type, int card,
267 char *dev_mixer_ctl_name)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700268{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800269 struct mixer_ctl *ctl;
270 unsigned int dev_token;
271 unsigned int pcm_device_number = 0;
272
273 /*
274 * usb_dev_token_id is 32 bit number and is defined as below:
275 * usb_sound_card_idx(31:16) | usb PCM device ID(15:8) | usb_usecase_type(7:0)
276 */
277 dev_token = (card << 16 ) |
278 (pcm_device_number << 8) | (usb_usecase_type & 0xFF);
279
280 ctl = mixer_get_ctl_by_name(usbmod->adev->mixer, dev_mixer_ctl_name);
281 if (!ctl) {
282 ALOGE("%s: Could not get ctl for mixer cmd - %s",
283 __func__, dev_mixer_ctl_name);
284 return -EINVAL;
285 }
286 mixer_ctl_set_value(ctl, 0, dev_token);
287
288 return 0;
289}
290
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700291static int usb_set_endian_mixer_ctl(int endian, char *endian_mixer_ctl_name)
292{
293 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
294 endian_mixer_ctl_name);
295 if (!ctl) {
296 ALOGE("%s: Could not get ctl for mixer cmd - %s",
297 __func__, endian_mixer_ctl_name);
298 return -EINVAL;
299 }
300
301 switch (endian) {
302 case 0:
303 case 1:
304 mixer_ctl_set_value(ctl, 0, endian);
305 break;
306 default:
307 ALOGW("%s: endianness(%d) not supported",
308 __func__, endian);
309 break;
310 }
311 return 0;
312}
313
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700314static int usb_get_sample_rates(int type, char *rates_str,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800315 struct usb_device_config *config)
316{
317 uint32_t i;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700318 char *next_sr_string, *temp_ptr;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800319 uint32_t sr, min_sr, max_sr, sr_size = 0;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700320
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800321 /* Sample rate string can be in any of the folloing two bit_widthes:
322 * Rates: 8000 - 48000 (continuous)
323 * Rates: 8000, 44100, 48000
324 * Support both the bit_widths
325 */
326 ALOGV("%s: rates_str %s", __func__, rates_str);
Kuirong Wang591a98a2016-06-27 12:41:41 -0700327 next_sr_string = strtok_r(rates_str, "Rates: ", &temp_ptr);
328 if (next_sr_string == NULL) {
329 ALOGE("%s: could not find min rates string", __func__);
330 return -EINVAL;
331 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800332 if (strstr(rates_str, "continuous") != NULL) {
Kuirong Wang591a98a2016-06-27 12:41:41 -0700333 min_sr = (uint32_t)atoi(next_sr_string);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700334 next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800335 if (next_sr_string == NULL) {
336 ALOGE("%s: could not find max rates string", __func__);
337 return -EINVAL;
338 }
Kuirong Wang591a98a2016-06-27 12:41:41 -0700339 max_sr = (uint32_t)atoi(next_sr_string);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800340
Kuirong Wang591a98a2016-06-27 12:41:41 -0700341 for (i = 0; i < MAX_SAMPLE_RATE_SIZE; i++) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800342 if (supported_sample_rates[i] >= min_sr &&
Kuirong Wang591a98a2016-06-27 12:41:41 -0700343 supported_sample_rates[i] <= max_sr) {
Weiyin Jiangcba6f962018-03-18 11:52:05 +0800344 // FIXME: we don't support >192KHz in recording path for now
345 if ((supported_sample_rates[i] > SAMPLE_RATE_192000) &&
346 (type == USB_CAPTURE))
347 continue;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800348 config->rates[sr_size++] = supported_sample_rates[i];
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700349 supported_sample_rates_mask[type] |= (1<<i);
Kuirong Wang591a98a2016-06-27 12:41:41 -0700350 ALOGI_IF(usb_audio_debug_enable,
351 "%s: continuous sample rate supported_sample_rates[%d] %d",
352 __func__, i, supported_sample_rates[i]);
353 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800354 }
Kuirong Wang591a98a2016-06-27 12:41:41 -0700355 } else {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800356 do {
357 sr = (uint32_t)atoi(next_sr_string);
Weiyin Jiangcba6f962018-03-18 11:52:05 +0800358 // FIXME: we don't support >192KHz in recording path for now
359 if ((sr > SAMPLE_RATE_192000) && (type == USB_CAPTURE)) {
360 next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
361 continue;
362 }
363
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800364 for (i = 0; i < MAX_SAMPLE_RATE_SIZE; i++) {
365 if (supported_sample_rates[i] == sr) {
366 ALOGI_IF(usb_audio_debug_enable,
367 "%s: sr %d, supported_sample_rates[%d] %d -> matches!!",
368 __func__, sr, i, supported_sample_rates[i]);
369 config->rates[sr_size++] = supported_sample_rates[i];
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700370 supported_sample_rates_mask[type] |= (1<<i);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800371 }
372 }
373 next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
374 } while (next_sr_string != NULL);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700375 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800376 config->rate_size = sr_size;
377 return 0;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700378}
379
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800380static int get_usb_service_interval(const char *interval_str_start,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700381 struct usb_device_config *usb_device_info)
382{
383 unsigned long interval = 0;
384 char time_unit[8] = {0};
385 int multiplier = 0;
386
387 char *eol = strchr(interval_str_start, '\n');
388 if (!eol) {
389 ALOGE("%s: No EOL found", __func__);
390 return -1;
391 }
392 char *tmp = (char *)calloc(1, eol-interval_str_start+1);
393 if (!tmp) {
394 ALOGE("%s: failed to allocate tmp", __func__);
395 return -1;
396 }
397 memcpy(tmp, interval_str_start, eol-interval_str_start);
398 sscanf(tmp, "%lu %2s", &interval, &time_unit[0]);
399 if (!strcmp(time_unit, "us")) {
400 multiplier = 1;
401 } else if (!strcmp(time_unit, "ms")) {
402 multiplier = 1000;
403 } else if (!strcmp(time_unit, "s")) {
404 multiplier = 1000000;
405 } else {
406 ALOGE("%s: unknown time_unit %s, assume default", __func__, time_unit);
407 interval = DEFAULT_SERVICE_INTERVAL_US;
408 multiplier = 1;
409 }
410 interval *= multiplier;
411 ALOGD("%s: set service_interval_us %lu", __func__, interval);
412 usb_device_info->service_interval_us = interval;
413 free(tmp);
414 return 0;
415}
416
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800417static int usb_get_capability(int type,
418 struct usb_card_config *usb_card_info,
419 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700420{
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700421 int32_t size = 0;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800422 int32_t fd=-1;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800423 int32_t channels_no;
Kuirong Wange9894162016-08-26 15:16:39 -0700424 char *str_start = NULL;
425 char *str_end = NULL;
426 char *channel_start = NULL;
427 char *bit_width_start = NULL;
428 char *rates_str_start = NULL;
429 char *target = NULL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700430 char *read_buf = NULL;
431 char *rates_str = NULL;
Garmond Leung5fd0b552018-04-17 11:56:12 -0700432 char *interval_str_start = NULL;
Kuirong Wange9894162016-08-26 15:16:39 -0700433 char path[128];
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700434 int ret = 0;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800435 char *bit_width_str = NULL;
436 struct usb_device_config * usb_device_info;
Kuirong Wange9894162016-08-26 15:16:39 -0700437 bool check = false;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700438
Kuirong Wange9894162016-08-26 15:16:39 -0700439 memset(path, 0, sizeof(path));
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800440 ALOGV("%s: for %s", __func__, (type == USB_PLAYBACK) ?
441 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR);
442
Kuirong Wange9894162016-08-26 15:16:39 -0700443 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/stream0",
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800444 card);
Kuirong Wange9894162016-08-26 15:16:39 -0700445 if(ret < 0) {
446 ALOGE("%s: failed on snprintf (%d) to path %s\n",
447 __func__, ret, path);
448 goto done;
449 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700450
451 fd = open(path, O_RDONLY);
452 if (fd <0) {
453 ALOGE("%s: error failed to open config file %s error: %d\n",
454 __func__, path, errno);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700455 ret = -EINVAL;
456 goto done;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700457 }
458
Apoorv Raghuvanshidad3b782014-01-29 15:31:32 -0800459 read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700460
461 if (!read_buf) {
462 ALOGE("Failed to create read_buf");
463 ret = -ENOMEM;
464 goto done;
465 }
466
Kuirong Wange9894162016-08-26 15:16:39 -0700467 if(read(fd, read_buf, USB_BUFF_SIZE) < 0) {
468 ALOGE("file read error\n");
469 goto done;
470 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800471 str_start = strstr(read_buf, ((type == USB_PLAYBACK) ?
472 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700473 if (str_start == NULL) {
474 ALOGE("%s: error %s section not found in usb config file",
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800475 __func__, ((type == USB_PLAYBACK) ?
476 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700477 ret = -EINVAL;
478 goto done;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700479 }
Kuirong Wange9894162016-08-26 15:16:39 -0700480 str_end = strstr(read_buf, ((type == USB_PLAYBACK) ?
481 CAPTURE_PROFILE_STR : PLAYBACK_PROFILE_STR));
482 if (str_end > str_start)
483 check = true;
484
485 ALOGV("%s: usb_config = %s, check %d\n", __func__, str_start, check);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700486
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800487 while (str_start != NULL) {
Kuirong Wange9894162016-08-26 15:16:39 -0700488 str_start = strstr(str_start, "Altset");
489 if ((str_start == NULL) || (check && (str_start >= str_end))) {
490 ALOGV("%s: done parsing %s\n", __func__, str_start);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800491 break;
492 }
Kuirong Wange9894162016-08-26 15:16:39 -0700493 ALOGV("%s: remaining string %s\n", __func__, str_start);
494 str_start += sizeof("Altset");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800495 usb_device_info = calloc(1, sizeof(struct usb_device_config));
496 if (usb_device_info == NULL) {
497 ALOGE("%s: error unable to allocate memory",
498 __func__);
499 ret = -ENOMEM;
500 break;
501 }
Garmond Leung5fd0b552018-04-17 11:56:12 -0700502 usb_device_info->type = type;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800503 /* Bit bit_width parsing */
504 bit_width_start = strstr(str_start, "Format: ");
505 if (bit_width_start == NULL) {
506 ALOGI("%s: Could not find bit_width string", __func__);
507 free(usb_device_info);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700508 continue;
509 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800510 target = strchr(bit_width_start, '\n');
511 if (target == NULL) {
512 ALOGI("%s:end of line not found", __func__);
513 free(usb_device_info);
514 continue;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700515 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800516 size = target - bit_width_start;
517 if ((bit_width_str = (char *)malloc(size + 1)) == NULL) {
518 ALOGE("%s: unable to allocate memory to hold bit width strings",
519 __func__);
520 ret = -EINVAL;
521 free(usb_device_info);
522 break;
523 }
524 memcpy(bit_width_str, bit_width_start, size);
525 bit_width_str[size] = '\0';
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700526
527 const char * formats[] = { "S32", "S24_3", "S24", "S16" };
528 const int bit_width[] = { 32, 24, 24, 16};
529 for (size_t i = 0; i < ARRAY_SIZE(formats); i++) {
530 const char * s = strstr(bit_width_str, formats[i]);
531 if (s) {
532 usb_device_info->bit_width = bit_width[i];
533 usb_card_info->endian = strstr(s, "BE") ? 1 : 0;
534 break;
535 }
536 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800537
538 if (bit_width_str)
539 free(bit_width_str);
540
541 /* channels parsing */
542 channel_start = strstr(str_start, CHANNEL_NUMBER_STR);
543 if (channel_start == NULL) {
544 ALOGI("%s: could not find Channels string", __func__);
545 free(usb_device_info);
546 continue;
547 }
548 channels_no = atoi(channel_start + strlen(CHANNEL_NUMBER_STR));
549 usb_device_info->channels = channels_no;
550
551 /* Sample rates parsing */
552 rates_str_start = strstr(str_start, "Rates: ");
553 if (rates_str_start == NULL) {
554 ALOGI("%s: cant find rates string", __func__);
555 free(usb_device_info);
556 continue;
557 }
558 target = strchr(rates_str_start, '\n');
559 if (target == NULL) {
560 ALOGI("%s: end of line not found", __func__);
561 free(usb_device_info);
562 continue;
563 }
564 size = target - rates_str_start;
565 if ((rates_str = (char *)malloc(size + 1)) == NULL) {
566 ALOGE("%s: unable to allocate memory to hold sample rate strings",
567 __func__);
568 ret = -EINVAL;
569 free(usb_device_info);
570 break;
571 }
572 memcpy(rates_str, rates_str_start, size);
573 rates_str[size] = '\0';
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700574 ret = usb_get_sample_rates(type, rates_str, usb_device_info);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800575 if (rates_str)
576 free(rates_str);
577 if (ret < 0) {
578 ALOGI("%s: error unable to get sample rate values",
579 __func__);
580 free(usb_device_info);
581 continue;
582 }
Garmond Leung5fd0b552018-04-17 11:56:12 -0700583 // Data packet interval is an optional field.
584 // Assume 0ms interval if this cannot be read
585 // LPASS USB and HLOS USB will figure out the default to use
586 usb_device_info->service_interval_us = DEFAULT_SERVICE_INTERVAL_US;
587 interval_str_start = strstr(str_start, DATA_PACKET_INTERVAL_STR);
588 if (interval_str_start != NULL) {
589 interval_str_start += strlen(DATA_PACKET_INTERVAL_STR);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800590 ret = get_usb_service_interval(interval_str_start, usb_device_info);
Garmond Leung5fd0b552018-04-17 11:56:12 -0700591 if (ret < 0) {
592 ALOGE("%s: error unable to get service interval, assume default",
593 __func__);
594 }
595 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800596 /* Add to list if every field is valid */
597 list_add_tail(&usb_card_info->usb_device_conf_list,
598 &usb_device_info->list);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700599 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700600
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700601done:
602 if (fd >= 0) close(fd);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700603 if (read_buf) free(read_buf);
604 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700605}
606
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800607static int usb_get_device_pb_config(struct usb_card_config *usb_card_info,
608 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700609{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800610 int ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700611
612 /* get capabilities */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800613 if ((ret = usb_get_capability(USB_PLAYBACK, usb_card_info, card))) {
614 ALOGE("%s: could not get Playback capabilities from usb device",
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700615 __func__);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800616 goto exit;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700617 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800618 usb_set_dev_id_mixer_ctl(USB_PLAYBACK, card, "USB_AUDIO_RX dev_token");
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700619 usb_set_endian_mixer_ctl(usb_card_info->endian, "USB_AUDIO_RX endian");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800620exit:
621
622 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700623}
624
Carter Hsu32a62362018-10-15 15:01:42 -0700625static int usb_get_usbid(struct usb_card_config *usb_card_info,
626 int card)
627{
628 int32_t fd=-1;
629 char path[128];
630 int ret = 0;
631
632 memset(usb_card_info->usbid, 0, sizeof(usb_card_info->usbid));
633
634 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid",
635 card);
636
637 if (ret < 0) {
638 ALOGE("%s: failed on snprintf (%d) to path %s\n",
639 __func__, ret, path);
640 goto done;
641 }
642
643 fd = open(path, O_RDONLY);
644 if (fd < 0) {
645 ALOGE("%s: error failed to open file %s error: %d\n",
646 __func__, path, errno);
647 ret = -EINVAL;
648 goto done;
649 }
650
651 if (read(fd, usb_card_info->usbid, USBID_SIZE - 1) < 0) {
652 ALOGE("file read error\n");
653 ret = -EINVAL;
654 usb_card_info->usbid[0] = '\0';
655 goto done;
656 }
657
658 strtok(usb_card_info->usbid, "\n");
659
660done:
661 if (fd >= 0)
662 close(fd);
663
664 return ret;
665}
666
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800667static int usb_get_device_cap_config(struct usb_card_config *usb_card_info,
668 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700669{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800670 int ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700671
672 /* get capabilities */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800673 if ((ret = usb_get_capability(USB_CAPTURE, usb_card_info, card))) {
674 ALOGE("%s: could not get Playback capabilities from usb device",
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700675 __func__);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800676 goto exit;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700677 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800678 usb_set_dev_id_mixer_ctl(USB_CAPTURE, card, "USB_AUDIO_TX dev_token");
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700679 usb_set_endian_mixer_ctl(usb_card_info->endian, "USB_AUDIO_TX endian");
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700680
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800681exit:
682 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700683}
684
Kuirong Wang1cad7142016-05-24 15:21:56 -0700685static void usb_get_sidetone_mixer(struct usb_card_config *usb_card_info)
686{
687 struct mixer_ctl *ctl;
688 unsigned int index;
689
690 for (index = 0; index < USB_SIDETONE_MAX_INDEX; index++)
691 usb_card_info->usb_sidetone_index[index] = -1;
692
693 usb_card_info->usb_snd_mixer = mixer_open(usb_card_info->usb_card);
694 for (index = 0;
695 index < sizeof(usb_sidetone_enable_str)/sizeof(usb_sidetone_enable_str[0]);
696 index++) {
697 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
698 usb_sidetone_enable_str[index]);
699 if (ctl) {
700 usb_card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX] = index;
701 /* Disable device sidetone by default */
702 mixer_ctl_set_value(ctl, 0, false);
Aalique Grahame22e49102018-12-18 14:23:57 -0800703 ALOGV("%s: sidetone mixer Control found(%s) ... disabling by default",
704 __func__, usb_sidetone_enable_str[index]);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700705 break;
706 }
707 }
Aalique Grahame22e49102018-12-18 14:23:57 -0800708
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800709 usb_get_sidetone_volume(usb_card_info);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700710
711 if ((usb_card_info->usb_snd_mixer != NULL) && (usb_audio_debug_enable))
712 usb_soundcard_list_controls(usb_card_info->usb_snd_mixer);
713
714 return;
715}
716
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700717static inline bool usb_output_device(audio_devices_t device) {
718 // ignore accessory for now
719 if (device == AUDIO_DEVICE_OUT_USB_ACCESSORY)
720 return false;
721 return audio_is_usb_out_device(device);
722}
723
724static inline bool usb_input_device(audio_devices_t device) {
725 // ignore accessory for now
726 if (device == AUDIO_DEVICE_IN_USB_ACCESSORY)
727 return false;
728 return audio_is_usb_in_device(device);
729}
730
731static bool usb_valid_device(audio_devices_t device)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700732{
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700733 return usb_output_device(device) ||
734 usb_input_device(device);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800735}
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700736
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800737static void usb_print_active_device(void){
738 struct listnode *node_i, *node_j;
739 struct usb_device_config *dev_info;
740 struct usb_card_config *card_info;
741 unsigned int i;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700742
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800743 ALOGI("%s", __func__);
744 list_for_each(node_i, &usbmod->usb_card_conf_list) {
745 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700746 ALOGI("%s: card_dev_type (0x%x), card_no(%d), %s",
747 __func__, card_info->usb_device_type,
748 card_info->usb_card, card_info->endian ? "BE" : "LE");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800749 list_for_each(node_j, &card_info->usb_device_conf_list) {
750 dev_info = node_to_item(node_j, struct usb_device_config, list);
751 ALOGI("%s: bit-width(%d) channel(%d)",
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700752 __func__, dev_info->bit_width, dev_info->channels);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800753 for (i = 0; i < dev_info->rate_size; i++)
754 ALOGI("%s: rate %d", __func__, dev_info->rates[i]);
755 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700756 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800757}
758
759static bool usb_get_best_match_for_bit_width(
760 struct listnode *dev_list,
761 unsigned int stream_bit_width,
762 unsigned int *bit_width)
763{
764 struct listnode *node_i;
765 struct usb_device_config *dev_info;
766 unsigned int candidate = 0;
767
768 list_for_each(node_i, dev_list) {
769 dev_info = node_to_item(node_i, struct usb_device_config, list);
770 ALOGI_IF(usb_audio_debug_enable,
771 "%s: USB bw(%d), stream bw(%d), candidate(%d)",
772 __func__, dev_info->bit_width,
773 stream_bit_width, candidate);
774 if (dev_info->bit_width == stream_bit_width) {
775 *bit_width = dev_info->bit_width;
776 ALOGV("%s: Found match bit-width (%d)",
777 __func__, dev_info->bit_width);
778 goto exit;
779 } else if (candidate == 0) {
780 candidate = dev_info->bit_width;
781 }
782 /*
783 * If stream bit is 24, USB supports both 16 bit and 32 bit, then
784 * higher bit width 32 is picked up instead of 16-bit
785 */
786 else if (ABS_SUB(stream_bit_width, dev_info->bit_width) <
787 ABS_SUB(stream_bit_width, candidate)) {
788 candidate = dev_info->bit_width;
789 }
790 else if ((ABS_SUB(stream_bit_width, dev_info->bit_width) ==
791 ABS_SUB(stream_bit_width, candidate)) &&
792 (dev_info->bit_width > candidate)) {
793 candidate = dev_info->bit_width;
794 }
795 }
796 ALOGV("%s: No match found, use the best candidate bw(%d)",
797 __func__, candidate);
798 *bit_width = candidate;
799exit:
800 return true;
801}
802
803static bool usb_get_best_match_for_channels(
804 struct listnode *dev_list,
805 unsigned int bit_width,
806 unsigned int stream_ch,
807 unsigned int *ch)
808{
809 struct listnode *node_i;
810 struct usb_device_config *dev_info;
811 unsigned int candidate = 0;
812
813 list_for_each(node_i, dev_list) {
814 dev_info = node_to_item(node_i, struct usb_device_config, list);
815 ALOGI_IF(usb_audio_debug_enable,
816 "%s: USB ch(%d)bw(%d), stream ch(%d)bw(%d), candidate(%d)",
817 __func__, dev_info->channels, dev_info->bit_width,
818 stream_ch, bit_width, candidate);
819 if (dev_info->bit_width != bit_width)
820 continue;
821 if (dev_info->channels== stream_ch) {
822 *ch = dev_info->channels;
823 ALOGV("%s: Found match channels (%d)",
824 __func__, dev_info->channels);
825 goto exit;
826 } else if (candidate == 0)
827 candidate = dev_info->channels;
828 /*
829 * If stream channel is 4, USB supports both 3 and 5, then
830 * higher channel 5 is picked up instead of 3
831 */
832 else if (ABS_SUB(stream_ch, dev_info->channels) <
833 ABS_SUB(stream_ch, candidate)) {
834 candidate = dev_info->channels;
835 } else if ((ABS_SUB(stream_ch, dev_info->channels) ==
836 ABS_SUB(stream_ch, candidate)) &&
837 (dev_info->channels > candidate)) {
838 candidate = dev_info->channels;
839 }
840 }
841 ALOGV("%s: No match found, use the best candidate ch(%d)",
842 __func__, candidate);
843 *ch = candidate;
844exit:
845 return true;
846
847}
848
849static bool usb_sample_rate_multiple(
850 unsigned int stream_sample_rate,
851 unsigned int base)
852{
853 return (((stream_sample_rate / base) * base) == stream_sample_rate);
854}
855
856static bool usb_find_sample_rate_candidate(unsigned int base,
857 unsigned stream_rate,
858 unsigned int usb_rate,
859 unsigned int cur_candidate,
860 unsigned int *update_candidate)
861{
862 /* For sample rate, we should consider fracational sample rate as high priority.
863 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
864 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
865 */
866 if (!usb_sample_rate_multiple(cur_candidate, base) &&
867 usb_sample_rate_multiple(usb_rate, base)) {
868 *update_candidate = usb_rate;
869 } else if (usb_sample_rate_multiple(cur_candidate, base) &&
870 usb_sample_rate_multiple(usb_rate, base)) {
871 if (ABS_SUB(stream_rate, usb_rate) <
872 ABS_SUB(stream_rate, cur_candidate)) {
873 *update_candidate = usb_rate;
874 } else if ((ABS_SUB(stream_rate, usb_rate) ==
875 ABS_SUB(stream_rate, cur_candidate)) &&
876 (usb_rate > cur_candidate)) {
877 *update_candidate = usb_rate;
878 }
879 } else if (!usb_sample_rate_multiple(cur_candidate, base) &&
880 !usb_sample_rate_multiple(usb_rate, base)) {
881 if (ABS_SUB(stream_rate, usb_rate) <
882 ABS_SUB(stream_rate, cur_candidate)) {
883 *update_candidate = usb_rate;
884 } else if ((ABS_SUB(stream_rate, usb_rate) ==
885 ABS_SUB(stream_rate, cur_candidate)) &&
886 (usb_rate > cur_candidate)) {
887 *update_candidate = usb_rate;
888 }
889 }
890 return true;
891}
892
893static bool usb_get_best_match_for_sample_rate(
894 struct listnode *dev_list,
895 unsigned int bit_width,
896 unsigned int ch,
897 unsigned int stream_sample_rate,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700898 unsigned int *sr,
899 unsigned int service_interval,
900 bool do_service_interval_check)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800901{
902 struct listnode *node_i;
903 struct usb_device_config *dev_info;
904 unsigned int candidate = 48000;
905 unsigned int base = SAMPLE_RATE_8000;
906 bool multiple_8k = usb_sample_rate_multiple(stream_sample_rate, base);
907 unsigned int i;
908
909 ALOGV("%s: stm ch(%d)bw(%d)sr(%d), stream sample multiple of 8kHz(%d)",
910 __func__, ch, bit_width, stream_sample_rate, multiple_8k);
911
912 list_for_each(node_i, dev_list) {
913 dev_info = node_to_item(node_i, struct usb_device_config, list);
914 ALOGI_IF(usb_audio_debug_enable,
915 "%s: USB ch(%d)bw(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
916 __func__, dev_info->channels, dev_info->bit_width,
917 ch, bit_width, stream_sample_rate, candidate);
Garmond Leung5fd0b552018-04-17 11:56:12 -0700918
919 if ((dev_info->bit_width != bit_width) ||
920 (dev_info->channels != ch) ||
921 (do_service_interval_check && (dev_info->service_interval_us !=
922 service_interval)))
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800923 continue;
924
925 candidate = 0;
926 for (i = 0; i < dev_info->rate_size; i++) {
927 ALOGI_IF(usb_audio_debug_enable,
928 "%s: USB ch(%d)bw(%d)sr(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
929 __func__, dev_info->channels,
930 dev_info->bit_width, dev_info->rates[i],
931 ch, bit_width, stream_sample_rate, candidate);
932 if (stream_sample_rate == dev_info->rates[i]) {
933 *sr = dev_info->rates[i];
934 ALOGV("%s: Found match sample rate (%d)",
935 __func__, dev_info->rates[i]);
936 goto exit;
937 } else if (candidate == 0) {
938 candidate = dev_info->rates[i];
939 /*
940 * For sample rate, we should consider fracational sample rate as high priority.
941 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
942 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
943 */
944 } else if (multiple_8k) {
945 usb_find_sample_rate_candidate(SAMPLE_RATE_8000,
946 stream_sample_rate,
947 dev_info->rates[i],
948 candidate,
949 &candidate);
950 } else {
951 usb_find_sample_rate_candidate(SAMPLE_RATE_11025,
952 stream_sample_rate,
953 dev_info->rates[i],
954 candidate,
955 &candidate);
956 }
957 }
958 }
959 ALOGV("%s: No match found, use the best candidate sr(%d)",
960 __func__, candidate);
961 *sr = candidate;
962exit:
963 return true;
964}
965
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800966static bool usb_audio_backend_apply_policy(struct listnode *dev_list,
967 unsigned int *bit_width,
968 unsigned int *sample_rate,
Ashish Jainb26edfb2016-08-25 00:10:11 +0530969 unsigned int *ch)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800970{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800971 bool is_usb_supported = true;
972
973 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) channels (%d)",
Ashish Jainb26edfb2016-08-25 00:10:11 +0530974 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800975 if (list_empty(dev_list)) {
976 *sample_rate = 48000;
977 *bit_width = 16;
Ashish Jainb26edfb2016-08-25 00:10:11 +0530978 *ch = 2;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800979 ALOGI("%s: list is empty,fall back to default setting", __func__);
980 goto exit;
981 }
982 usb_get_best_match_for_bit_width(dev_list, *bit_width, bit_width);
983 usb_get_best_match_for_channels(dev_list,
984 *bit_width,
Ashish Jainb26edfb2016-08-25 00:10:11 +0530985 *ch,
986 ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800987 usb_get_best_match_for_sample_rate(dev_list,
988 *bit_width,
Ashish Jainb26edfb2016-08-25 00:10:11 +0530989 *ch,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800990 *sample_rate,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700991 sample_rate,
992 0 /*service int*/,
993 false /*do service int check*/);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800994exit:
995 ALOGV("%s: Updated sample rate per profile: bit-width(%d) rate(%d) chs(%d)",
Ashish Jainb26edfb2016-08-25 00:10:11 +0530996 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800997 return is_usb_supported;
998}
999
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001000void usb_set_sidetone_gain(struct str_parms *parms,
Kuirong Wang1cad7142016-05-24 15:21:56 -07001001 char *value, int len)
1002{
1003 int err;
1004
1005 err = str_parms_get_str(parms, USB_SIDETONE_GAIN_STR,
1006 value, len);
1007 if (err >= 0) {
1008 usb_sidetone_gain = pow(10.0, (float)(atoi(value))/10.0);
1009 ALOGV("%s: sidetone gain(%s) decimal %d",
1010 __func__, value, usb_sidetone_gain);
1011 str_parms_del(parms, USB_SIDETONE_GAIN_STR);
1012 }
1013 return;
1014}
1015
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001016int usb_enable_sidetone(int device, bool enable)
Kuirong Wang1cad7142016-05-24 15:21:56 -07001017{
1018 int ret = -ENODEV;
1019 struct listnode *node_i;
1020 struct usb_card_config *card_info;
1021 int i;
1022 ALOGV("%s: card_dev_type (0x%x), sidetone enable(%d)",
1023 __func__, device, enable);
1024
1025 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1026 card_info = node_to_item(node_i, struct usb_card_config, list);
1027 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1028 __func__, card_info->usb_device_type, card_info->usb_card);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001029 if (usb_output_device(card_info->usb_device_type)) {
Kuirong Wang1cad7142016-05-24 15:21:56 -07001030 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX]) != -1) {
1031 struct mixer_ctl *ctl = mixer_get_ctl_by_name(
1032 card_info->usb_snd_mixer,
1033 usb_sidetone_enable_str[i]);
1034 if (ctl)
1035 mixer_ctl_set_value(ctl, 0, enable);
1036 else
1037 break;
1038
1039 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX]) != -1) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001040 usb_set_sidetone_volume(card_info, enable, i);
Kuirong Wang1cad7142016-05-24 15:21:56 -07001041 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001042
Kuirong Wang1cad7142016-05-24 15:21:56 -07001043 ret = 0;
1044 break;
1045 }
1046 }
1047 }
1048 return ret;
1049}
1050
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001051bool usb_is_config_supported(unsigned int *bit_width,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001052 unsigned int *sample_rate,
Kuirong Wange9894162016-08-26 15:16:39 -07001053 unsigned int *ch,
1054 bool is_playback)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001055{
1056 struct listnode *node_i;
1057 struct usb_card_config *card_info;
1058 bool is_usb_supported = false;
1059
Kuirong Wang27152a12016-11-11 10:20:30 -08001060 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) ch(%d) is_playback(%d)",
1061 __func__, *bit_width, *sample_rate, *ch, is_playback);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001062 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1063 card_info = node_to_item(node_i, struct usb_card_config, list);
1064 ALOGI_IF(usb_audio_debug_enable,
1065 "%s: card_dev_type (0x%x), card_no(%d)",
1066 __func__, card_info->usb_device_type, card_info->usb_card);
1067 /* Currently only apply the first playback sound card configuration */
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001068 if ((is_playback && usb_output_device(card_info->usb_device_type)) ||
1069 (!is_playback && usb_input_device(card_info->usb_device_type))){
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001070 is_usb_supported = usb_audio_backend_apply_policy(
1071 &card_info->usb_device_conf_list,
1072 bit_width,
1073 sample_rate,
1074 ch);
1075 break;
1076 }
1077 }
Ashish Jainb26edfb2016-08-25 00:10:11 +05301078 ALOGV("%s: updated: bit-width(%d) sample_rate(%d) channels (%d)",
1079 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001080
1081 return is_usb_supported;
1082}
1083
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001084int usb_get_max_channels(bool is_playback)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001085{
1086 struct listnode *node_i, *node_j;
1087 struct usb_device_config *dev_info;
1088 struct usb_card_config *card_info;
1089 unsigned int max_ch = 1;
1090 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1091 card_info = node_to_item(node_i, struct usb_card_config, list);
1092 if (usb_output_device(card_info->usb_device_type) && !is_playback)
1093 continue;
1094 else if (usb_input_device(card_info->usb_device_type) && is_playback)
1095 continue;
1096
1097 list_for_each(node_j, &card_info->usb_device_conf_list) {
1098 dev_info = node_to_item(node_j, struct usb_device_config, list);
1099 max_ch = _MAX(max_ch, dev_info->channels);
1100 }
1101 }
1102
1103 return max_ch;
1104}
1105
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001106int usb_get_max_bit_width(bool is_playback)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001107{
1108 struct listnode *node_i, *node_j;
1109 struct usb_device_config *dev_info;
1110 struct usb_card_config *card_info;
1111 unsigned int max_bw = 16;
1112 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1113 card_info = node_to_item(node_i, struct usb_card_config, list);
1114 if (usb_output_device(card_info->usb_device_type) && !is_playback)
1115 continue;
1116 else if (usb_input_device(card_info->usb_device_type) && is_playback)
1117 continue;
1118
1119 list_for_each(node_j, &card_info->usb_device_conf_list) {
1120 dev_info = node_to_item(node_j, struct usb_device_config, list);
1121 max_bw = _MAX(max_bw, dev_info->bit_width);
1122 }
1123 }
1124
1125 return max_bw;
1126}
1127
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001128int usb_get_sup_sample_rates(bool is_playback,
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001129 uint32_t *sample_rates,
1130 uint32_t sample_rate_size)
1131{
1132 int type = is_playback ? USB_PLAYBACK : USB_CAPTURE;
1133
1134 ALOGV("%s supported_sample_rates_mask 0x%x", __func__, supported_sample_rates_mask[type]);
1135 uint32_t bm = supported_sample_rates_mask[type];
1136 uint32_t tries = _MIN(sample_rate_size, (uint32_t)__builtin_popcount(bm));
1137
1138 int i = 0;
1139 while (tries--) {
1140 int idx = __builtin_ffs(bm) - 1;
1141 sample_rates[i++] = supported_sample_rates[idx];
1142 bm &= ~(1<<idx);
1143 }
1144
1145 return i;
1146}
1147
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001148bool usb_is_capture_supported()
Ashish Jain3e37a702016-11-25 12:27:15 +05301149{
1150 if (usbmod == NULL) {
1151 ALOGE("%s: USB device object is NULL", __func__);
1152 return false;
1153 }
1154 ALOGV("%s: capture_supported %d",__func__,usbmod->is_capture_supported);
1155 return usbmod->is_capture_supported;
1156}
1157
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001158bool usb_is_tunnel_supported()
Zhou Song6f862822017-11-06 17:27:57 +08001159{
1160 return true;
1161}
1162
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001163void usb_add_device(audio_devices_t device, int card)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001164{
1165 struct usb_card_config *usb_card_info;
1166 char check_debug_enable[PROPERTY_VALUE_MAX];
Kuirong Wang27152a12016-11-11 10:20:30 -08001167 struct listnode *node_i;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001168
Aalique Grahame22e49102018-12-18 14:23:57 -08001169 if ((property_get("vendor.audio.usb.enable.debug",
1170 check_debug_enable, NULL) > 0) ||
1171 (property_get("audio.usb.enable.debug",
1172 check_debug_enable, NULL) > 0)) {
1173 if (atoi(check_debug_enable))
1174 usb_audio_debug_enable = true;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001175 }
1176
1177 ALOGI_IF(usb_audio_debug_enable,
1178 "%s: parameters device(0x%x), card(%d)",
1179 __func__, device, card);
1180 if (usbmod == NULL) {
1181 ALOGE("%s: USB device object is NULL", __func__);
1182 goto exit;
1183 }
1184
1185 if (!(usb_valid_device(device)) || (card < 0)) {
1186 ALOGE("%s:device(0x%x), card(%d)",
1187 __func__, device, card);
1188 goto exit;
1189 }
1190
Kuirong Wang27152a12016-11-11 10:20:30 -08001191 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1192 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
1193 ALOGI_IF(usb_audio_debug_enable,
1194 "%s: list has capability for card_dev_type (0x%x), card_no(%d)",
1195 __func__, usb_card_info->usb_device_type, usb_card_info->usb_card);
1196 /* If we have cached the capability */
1197 if ((usb_card_info->usb_device_type == device) && (usb_card_info->usb_card == card)) {
1198 ALOGV("%s: capability for device(0x%x), card(%d) is cached, no need to update",
1199 __func__, device, card);
1200 goto exit;
1201 }
1202 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001203 usb_card_info = calloc(1, sizeof(struct usb_card_config));
1204 if (usb_card_info == NULL) {
1205 ALOGE("%s: error unable to allocate memory",
1206 __func__);
1207 goto exit;
1208 }
1209 list_init(&usb_card_info->usb_device_conf_list);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001210 if (usb_output_device(device)) {
Carter Hsu32a62362018-10-15 15:01:42 -07001211 if (usb_get_usbid(usb_card_info, card) < 0) {
1212 ALOGE("parse card %d usbid fail", card);
1213 }
1214
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001215 if (!usb_get_device_pb_config(usb_card_info, card)){
1216 usb_card_info->usb_card = card;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001217 usb_card_info->usb_device_type = device;
Kuirong Wang1cad7142016-05-24 15:21:56 -07001218 usb_get_sidetone_mixer(usb_card_info);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001219 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1220 goto exit;
1221 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001222 } else if (usb_input_device(device)) {
Carter Hsu32a62362018-10-15 15:01:42 -07001223 if (usb_get_usbid(usb_card_info, card) < 0) {
1224 ALOGE("parse card %d usbid fail", card);
1225 }
1226
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001227 if (!usb_get_device_cap_config(usb_card_info, card)) {
1228 usb_card_info->usb_card = card;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001229 usb_card_info->usb_device_type = device;
Ashish Jain3e37a702016-11-25 12:27:15 +05301230 usbmod->is_capture_supported = true;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001231 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1232 goto exit;
1233 }
1234 }
1235 /* free memory in error case */
1236 if (usb_card_info != NULL)
1237 free(usb_card_info);
1238exit:
1239 if (usb_audio_debug_enable)
1240 usb_print_active_device();
1241 return;
1242}
1243
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001244void usb_remove_device(audio_devices_t device, int card)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001245{
1246 struct listnode *node_i, *temp_i;
1247 struct listnode *node_j, *temp_j;
1248 struct usb_device_config *dev_info;
1249 struct usb_card_config *card_info;
1250 unsigned int i;
1251
1252 ALOGV("%s: device(0x%x), card(%d)",
1253 __func__, device, card);
1254
1255 if (usbmod == NULL) {
1256 ALOGE("%s: USB device object is NULL", __func__);
1257 goto exit;
1258 }
1259
1260 if (!(usb_valid_device(device)) || (card < 0)) {
1261 ALOGE("%s: Invalid parameters device(0x%x), card(%d)",
1262 __func__, device, card);
1263 goto exit;
1264 }
1265 list_for_each_safe(node_i, temp_i, &usbmod->usb_card_conf_list) {
1266 card_info = node_to_item(node_i, struct usb_card_config, list);
1267 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1268 __func__, card_info->usb_device_type, card_info->usb_card);
1269 if ((device == card_info->usb_device_type) && (card == card_info->usb_card)){
1270 list_for_each_safe(node_j, temp_j, &card_info->usb_device_conf_list) {
1271 dev_info = node_to_item(node_j, struct usb_device_config, list);
1272 ALOGV("%s: bit-width(%d) channel(%d)",
1273 __func__, dev_info->bit_width, dev_info->channels);
1274 for (i = 0; i < dev_info->rate_size; i++)
1275 ALOGV("%s: rate %d", __func__, dev_info->rates[i]);
1276
1277 list_remove(node_j);
1278 free(node_to_item(node_j, struct usb_device_config, list));
1279 }
1280 list_remove(node_i);
1281 free(node_to_item(node_i, struct usb_card_config, list));
1282 }
1283 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001284 if (audio_is_usb_in_device(device)) { // XXX not sure if we need to check for card
1285 usbmod->is_capture_supported = false;
1286 supported_sample_rates_mask[USB_CAPTURE] = 0;
1287 } else
1288 supported_sample_rates_mask[USB_PLAYBACK] = 0;
1289
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001290exit:
1291 if (usb_audio_debug_enable)
1292 usb_print_active_device();
1293
1294 return;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001295}
1296
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001297bool usb_alive(int card) {
Kevin Rocardfce19002017-08-07 19:21:36 -07001298 char path[PATH_MAX] = {0};
1299 // snprintf should never fail
1300 (void) snprintf(path, sizeof(path), "/proc/asound/card%u/stream0", card);
1301 return access(path, F_OK) == 0;
1302}
1303
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001304unsigned long usb_find_service_interval(bool min,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001305 bool playback) {
1306 struct usb_card_config *card_info = NULL;
1307 struct usb_device_config *dev_info = NULL;
1308 struct listnode *node_i = NULL;
1309 struct listnode *node_j = NULL;
Aalique Grahame22e49102018-12-18 14:23:57 -08001310 unsigned long interval_us = min ? ULONG_MAX : 0;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001311 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1312 card_info = node_to_item(node_i, struct usb_card_config, list);
1313 list_for_each(node_j, &card_info->usb_device_conf_list) {
1314 dev_info = node_to_item(node_j, struct usb_device_config, list);
1315 bool match = (playback && (dev_info->type == USB_PLAYBACK)) ||
1316 (!playback && (dev_info->type == USB_CAPTURE));
1317 if (match) {
1318 interval_us = min ?
1319 _MIN(interval_us, dev_info->service_interval_us) :
1320 _MAX(interval_us, dev_info->service_interval_us);
1321 }
1322 }
1323 break;
1324 }
1325 return interval_us;
1326}
1327
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001328int usb_altset_for_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001329 unsigned long service_interval,
1330 uint32_t *bit_width,
1331 uint32_t *sample_rate,
1332 uint32_t *channels)
1333{
1334 struct usb_card_config *card_info = NULL;
Aalique Grahame22e49102018-12-18 14:23:57 -08001335 struct usb_device_config *dev_info = NULL;
1336 struct listnode *node_i = NULL;
1337 struct listnode *node_j = NULL;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001338 uint32_t bw = 0;
1339 uint32_t ch = 0;
1340 uint32_t sr = 0;
1341
1342 if (service_interval == 0)
1343 return 0;
1344 /* not a valid service interval to search for */
1345
1346#define FIND_BEST_MATCH(local_var, field, cond) \
1347 list_for_each(node_i, &usbmod->usb_card_conf_list) { \
1348 /* Currently only apply the first playback sound card configuration */ \
1349 card_info = node_to_item(node_i, struct usb_card_config, list); \
1350 list_for_each(node_j, &card_info->usb_device_conf_list) { \
1351 dev_info = node_to_item(node_j, struct usb_device_config, list); \
1352 bool match = (playback && (dev_info->type == USB_PLAYBACK)) || \
1353 (!playback && (dev_info->type == USB_CAPTURE)); \
1354 if (match && (cond)) { \
Garmond Leung003d9ea2018-08-14 17:05:08 -07001355 if (dev_info->field == *field) { \
1356 local_var = dev_info->field; \
1357 break; \
1358 } \
Garmond Leung5fd0b552018-04-17 11:56:12 -07001359 local_var = _MAX(local_var, dev_info->field); \
1360 } \
1361 } \
1362 break; \
1363 }
1364
1365 FIND_BEST_MATCH(bw, bit_width, dev_info->service_interval_us == service_interval);
1366 FIND_BEST_MATCH(ch, channels, \
1367 dev_info->service_interval_us == service_interval && \
1368 dev_info->bit_width == bw);
Garmond Leung003d9ea2018-08-14 17:05:08 -07001369 sr = *sample_rate;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001370 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1371 /* Currently only apply the first playback sound card configuration */
1372 card_info = node_to_item(node_i, struct usb_card_config, list);
1373 if ((playback && usb_output_device(card_info->usb_device_type)) ||
1374 (!playback && usb_input_device(card_info->usb_device_type))) {
1375 usb_get_best_match_for_sample_rate(&card_info->usb_device_conf_list,
1376 bw, ch, sr, &sr,
1377 service_interval,
1378 true);
1379 }
1380 break;
1381 }
1382
1383#define SET_OR_RETURN_ON_ERROR(arg, local_var, cond) \
1384 if (local_var != (cond)) arg = local_var; else return -1;
1385
1386 SET_OR_RETURN_ON_ERROR(*bit_width, bw, 0);
1387 SET_OR_RETURN_ON_ERROR(*sample_rate, sr, 0);
1388 SET_OR_RETURN_ON_ERROR(*channels, ch, 0);
1389 return 0;
1390#undef FIND_BEST_MATCH
1391#undef SET_OR_RETURN_ON_ERROR
1392}
1393
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001394int usb_get_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001395 unsigned long *service_interval)
1396{
1397 const char *ctl_name = "USB_AUDIO_RX service_interval";
1398 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
1399 ctl_name);
1400
1401 if (!playback) {
1402 ALOGE("%s not valid for capture", __func__);
1403 return -1;
1404 }
1405
1406 if (!ctl) {
1407 ALOGV("%s: could not get mixer %s", __func__, ctl_name);
1408 return -1;
1409 }
1410
1411 *service_interval = mixer_ctl_get_value(ctl, 0);
1412 return 0;
1413}
1414
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001415int usb_set_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001416 unsigned long service_interval,
1417 bool *reconfig)
1418{
1419 *reconfig = false;
1420 unsigned long current_service_interval = 0;
1421 const char *ctl_name = "USB_AUDIO_RX service_interval";
1422 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
1423 ctl_name);
1424
1425 if (!playback) {
1426 ALOGE("%s not valid for capture", __func__);
1427 return -1;
1428 }
1429
1430 if (!ctl) {
1431 ALOGV("%s: could not get mixer %s", __func__, ctl_name);
1432 return -1;
1433 }
1434
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001435 if (usb_get_service_interval(playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001436 &current_service_interval) != 0) {
1437 ALOGE("%s Unable to get current service interval", __func__);
1438 return -1;
1439 }
1440
1441 if (current_service_interval != service_interval) {
1442 mixer_ctl_set_value(ctl, 0, service_interval);
1443 *reconfig = usbmod->usb_reconfig = true;
1444 }
1445 else
1446 *reconfig = usbmod->usb_reconfig = false;
1447 return 0;
1448}
1449
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001450int usb_check_and_set_svc_int(struct audio_usecase *uc_info,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001451 bool starting_output_stream)
1452{
1453 struct listnode *node = NULL;
1454 struct audio_usecase *usecase = uc_info;
1455 bool reconfig = false;
1456 bool burst_mode = true;
1457 unsigned long service_interval = 0;
1458 struct audio_device *adev = usbmod->adev;
1459
1460 ALOGV("%s: enter:", __func__);
1461
1462 if ((starting_output_stream == true &&
1463 ((uc_info->id == USECASE_AUDIO_PLAYBACK_MMAP) ||
1464 (uc_info->id == USECASE_AUDIO_PLAYBACK_ULL))) ||
1465 (voice_is_call_state_active(usbmod->adev))) {
1466 burst_mode = false;
1467 } else {
1468 /* set if the valid usecase do not already exist */
1469 list_for_each(node, &adev->usecase_list) {
1470 usecase = node_to_item(node, struct audio_usecase, list);
1471 if (usecase->type == PCM_PLAYBACK &&
1472 audio_is_usb_out_device(usecase->devices & AUDIO_DEVICE_OUT_ALL_USB )) {
1473 switch (usecase->id) {
1474 case USECASE_AUDIO_PLAYBACK_MMAP:
1475 case USECASE_AUDIO_PLAYBACK_ULL:
1476 {
1477 if (uc_info != usecase) {
1478 //another ULL stream exists
1479 ALOGV("%s: another ULL Stream in active use-case list burst mode = false.", __func__);
1480 burst_mode = false;
1481 } else {
1482 ALOGV("%s:current ULL uc is the same as incoming uc_info \
1483 which means we are stopping the output stream, \
1484 we don't want to set burst mode to false", __func__);
1485 }
1486 break;
1487 }
1488 default:
1489 break;
1490 }
1491 }
1492 }
1493 }
1494
1495 ALOGV("%s: burst mode(%d).", __func__,burst_mode);
1496
1497 service_interval =
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001498 usb_find_service_interval(!burst_mode, true /*playback*/);
Garmond Leung5fd0b552018-04-17 11:56:12 -07001499
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001500 usb_set_service_interval(true /*playback*/,
Yunfei Zhang7728dce2018-10-31 10:20:52 +08001501 service_interval,
1502 &reconfig);
Garmond Leung5fd0b552018-04-17 11:56:12 -07001503
1504 /* no change or not supported or no active usecases */
1505 if (reconfig)
1506 return -1;
1507 return 0;
1508}
1509
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001510bool usb_is_reconfig_req()
Garmond Leung5fd0b552018-04-17 11:56:12 -07001511{
1512 return usbmod->usb_reconfig;
1513}
1514
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001515void usb_set_reconfig(bool is_required)
Garmond Leung5fd0b552018-04-17 11:56:12 -07001516{
1517 usbmod->usb_reconfig = is_required;
1518}
1519
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001520bool usb_connected(struct str_parms *parms) {
Sharad Sangled0a50b22018-04-05 23:28:32 +05301521 int card = -1;
1522 struct listnode *node_i = NULL;
1523 struct usb_card_config *usb_card_info = NULL;
1524 bool usb_connected = false;
1525
Weiyin Jiang63831272018-06-28 11:41:01 +08001526 if ((parms != NULL) && str_parms_get_int(parms, "card", &card) >= 0) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001527 usb_connected = usb_alive(card);
Sharad Sangled0a50b22018-04-05 23:28:32 +05301528 } else {
1529 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1530 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001531 if (usb_alive(usb_card_info->usb_card)) {
Sharad Sangled0a50b22018-04-05 23:28:32 +05301532 usb_connected = true;
1533 break;
1534 }
1535 }
1536 }
1537 return usb_connected;
1538}
1539
Carter Hsu32a62362018-10-15 15:01:42 -07001540char *usb_usbid()
1541{
1542 struct usb_card_config *card_info;
1543
1544 if (usbmod == NULL)
1545 return NULL;
1546
1547 if (list_empty(&usbmod->usb_card_conf_list))
1548 return NULL;
1549
1550 card_info = node_to_item(list_head(&usbmod->usb_card_conf_list),\
1551 struct usb_card_config, list);
1552
1553 return strdup(card_info->usbid);
1554}
1555
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001556void usb_init(void *adev)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001557{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001558 if (usbmod == NULL) {
1559 usbmod = calloc(1, sizeof(struct usb_module));
1560 if (usbmod == NULL) {
1561 ALOGE("%s: error unable to allocate memory", __func__);
1562 goto exit;
1563 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001564 } else {
1565 memset(usbmod, 0, sizeof(*usbmod));
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001566 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001567
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001568 list_init(&usbmod->usb_card_conf_list);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001569 usbmod->adev = (struct audio_device*)adev;
Kuirong Wang1cad7142016-05-24 15:21:56 -07001570 usbmod->sidetone_gain = usb_sidetone_gain;
Ashish Jain3e37a702016-11-25 12:27:15 +05301571 usbmod->is_capture_supported = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001572 usbmod->usb_reconfig = false;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001573exit:
1574 return;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001575}
1576
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001577void usb_deinit(void)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001578{
1579 if (NULL != usbmod){
1580 free(usbmod);
1581 usbmod = NULL;
1582 }
1583}