blob: 95f8ea995a09f1acc2d8bc539eb5c51f2e83129c [file] [log] [blame]
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001/*
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002 * Copyright (c) 2013, 2016-2020, 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
Mingshu Pange688f6f2020-05-07 13:14:06 +0800380int usb_get_service_interval(bool playback,
381 unsigned long *service_interval)
382{
383 const char *ctl_name = "USB_AUDIO_RX service_interval";
384 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
385 ctl_name);
386
387 if (!playback) {
388 ALOGE("%s not valid for capture", __func__);
389 return -1;
390 }
391
392 if (!ctl) {
393 ALOGV("%s: could not get mixer %s", __func__, ctl_name);
394 return -1;
395 }
396
397 *service_interval = mixer_ctl_get_value(ctl, 0);
398 return 0;
399}
400
401int usb_set_service_interval(bool playback,
402 unsigned long service_interval,
403 bool *reconfig)
404{
405 *reconfig = false;
406 unsigned long current_service_interval = 0;
407 const char *ctl_name = "USB_AUDIO_RX service_interval";
408 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
409 ctl_name);
410
411 if (!playback) {
412 ALOGE("%s not valid for capture", __func__);
413 return -1;
414 }
415
416 if (!ctl) {
417 ALOGV("%s: could not get mixer %s", __func__, ctl_name);
418 return -1;
419 }
420
421 if (usb_get_service_interval(playback,
422 &current_service_interval) != 0) {
423 ALOGE("%s Unable to get current service interval", __func__);
424 return -1;
425 }
426
427 if (current_service_interval != service_interval) {
428 mixer_ctl_set_value(ctl, 0, service_interval);
429 *reconfig = usbmod->usb_reconfig = true;
430 }
431 else
432 *reconfig = usbmod->usb_reconfig = false;
433 return 0;
434}
435
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800436static int get_usb_service_interval(const char *interval_str_start,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700437 struct usb_device_config *usb_device_info)
438{
439 unsigned long interval = 0;
440 char time_unit[8] = {0};
441 int multiplier = 0;
442
443 char *eol = strchr(interval_str_start, '\n');
444 if (!eol) {
445 ALOGE("%s: No EOL found", __func__);
446 return -1;
447 }
448 char *tmp = (char *)calloc(1, eol-interval_str_start+1);
449 if (!tmp) {
450 ALOGE("%s: failed to allocate tmp", __func__);
451 return -1;
452 }
453 memcpy(tmp, interval_str_start, eol-interval_str_start);
Zhou Song8c05a3c2019-06-18 22:57:11 +0800454 tmp[eol-interval_str_start] = '\0';
Garmond Leung5fd0b552018-04-17 11:56:12 -0700455 sscanf(tmp, "%lu %2s", &interval, &time_unit[0]);
456 if (!strcmp(time_unit, "us")) {
457 multiplier = 1;
458 } else if (!strcmp(time_unit, "ms")) {
459 multiplier = 1000;
460 } else if (!strcmp(time_unit, "s")) {
461 multiplier = 1000000;
462 } else {
463 ALOGE("%s: unknown time_unit %s, assume default", __func__, time_unit);
464 interval = DEFAULT_SERVICE_INTERVAL_US;
465 multiplier = 1;
466 }
467 interval *= multiplier;
468 ALOGD("%s: set service_interval_us %lu", __func__, interval);
469 usb_device_info->service_interval_us = interval;
470 free(tmp);
471 return 0;
472}
473
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800474static int usb_get_capability(int type,
475 struct usb_card_config *usb_card_info,
476 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700477{
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700478 int32_t size = 0;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800479 int32_t fd=-1;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800480 int32_t channels_no;
Kuirong Wange9894162016-08-26 15:16:39 -0700481 char *str_start = NULL;
482 char *str_end = NULL;
483 char *channel_start = NULL;
484 char *bit_width_start = NULL;
485 char *rates_str_start = NULL;
486 char *target = NULL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700487 char *read_buf = NULL;
488 char *rates_str = NULL;
Garmond Leung5fd0b552018-04-17 11:56:12 -0700489 char *interval_str_start = NULL;
Kuirong Wange9894162016-08-26 15:16:39 -0700490 char path[128];
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700491 int ret = 0;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800492 char *bit_width_str = NULL;
493 struct usb_device_config * usb_device_info;
Kuirong Wange9894162016-08-26 15:16:39 -0700494 bool check = false;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700495
Kuirong Wange9894162016-08-26 15:16:39 -0700496 memset(path, 0, sizeof(path));
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800497 ALOGV("%s: for %s", __func__, (type == USB_PLAYBACK) ?
498 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR);
499
Kuirong Wange9894162016-08-26 15:16:39 -0700500 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/stream0",
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800501 card);
Kuirong Wange9894162016-08-26 15:16:39 -0700502 if(ret < 0) {
503 ALOGE("%s: failed on snprintf (%d) to path %s\n",
504 __func__, ret, path);
505 goto done;
506 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700507
508 fd = open(path, O_RDONLY);
509 if (fd <0) {
510 ALOGE("%s: error failed to open config file %s error: %d\n",
511 __func__, path, errno);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700512 ret = -EINVAL;
513 goto done;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700514 }
515
Apoorv Raghuvanshidad3b782014-01-29 15:31:32 -0800516 read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700517
518 if (!read_buf) {
519 ALOGE("Failed to create read_buf");
520 ret = -ENOMEM;
521 goto done;
522 }
523
Kuirong Wange9894162016-08-26 15:16:39 -0700524 if(read(fd, read_buf, USB_BUFF_SIZE) < 0) {
525 ALOGE("file read error\n");
526 goto done;
527 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800528 str_start = strstr(read_buf, ((type == USB_PLAYBACK) ?
529 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700530 if (str_start == NULL) {
531 ALOGE("%s: error %s section not found in usb config file",
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800532 __func__, ((type == USB_PLAYBACK) ?
533 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700534 ret = -EINVAL;
535 goto done;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700536 }
Kuirong Wange9894162016-08-26 15:16:39 -0700537 str_end = strstr(read_buf, ((type == USB_PLAYBACK) ?
538 CAPTURE_PROFILE_STR : PLAYBACK_PROFILE_STR));
539 if (str_end > str_start)
540 check = true;
541
542 ALOGV("%s: usb_config = %s, check %d\n", __func__, str_start, check);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700543
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800544 while (str_start != NULL) {
Kuirong Wange9894162016-08-26 15:16:39 -0700545 str_start = strstr(str_start, "Altset");
546 if ((str_start == NULL) || (check && (str_start >= str_end))) {
547 ALOGV("%s: done parsing %s\n", __func__, str_start);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800548 break;
549 }
Kuirong Wange9894162016-08-26 15:16:39 -0700550 ALOGV("%s: remaining string %s\n", __func__, str_start);
551 str_start += sizeof("Altset");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800552 usb_device_info = calloc(1, sizeof(struct usb_device_config));
553 if (usb_device_info == NULL) {
554 ALOGE("%s: error unable to allocate memory",
555 __func__);
556 ret = -ENOMEM;
557 break;
558 }
Garmond Leung5fd0b552018-04-17 11:56:12 -0700559 usb_device_info->type = type;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800560 /* Bit bit_width parsing */
561 bit_width_start = strstr(str_start, "Format: ");
562 if (bit_width_start == NULL) {
563 ALOGI("%s: Could not find bit_width string", __func__);
564 free(usb_device_info);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700565 continue;
566 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800567 target = strchr(bit_width_start, '\n');
568 if (target == NULL) {
569 ALOGI("%s:end of line not found", __func__);
570 free(usb_device_info);
571 continue;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700572 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800573 size = target - bit_width_start;
574 if ((bit_width_str = (char *)malloc(size + 1)) == NULL) {
575 ALOGE("%s: unable to allocate memory to hold bit width strings",
576 __func__);
577 ret = -EINVAL;
578 free(usb_device_info);
579 break;
580 }
581 memcpy(bit_width_str, bit_width_start, size);
582 bit_width_str[size] = '\0';
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700583
584 const char * formats[] = { "S32", "S24_3", "S24", "S16" };
585 const int bit_width[] = { 32, 24, 24, 16};
586 for (size_t i = 0; i < ARRAY_SIZE(formats); i++) {
587 const char * s = strstr(bit_width_str, formats[i]);
588 if (s) {
589 usb_device_info->bit_width = bit_width[i];
590 usb_card_info->endian = strstr(s, "BE") ? 1 : 0;
591 break;
592 }
593 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800594
595 if (bit_width_str)
596 free(bit_width_str);
597
598 /* channels parsing */
599 channel_start = strstr(str_start, CHANNEL_NUMBER_STR);
600 if (channel_start == NULL) {
601 ALOGI("%s: could not find Channels string", __func__);
602 free(usb_device_info);
603 continue;
604 }
605 channels_no = atoi(channel_start + strlen(CHANNEL_NUMBER_STR));
606 usb_device_info->channels = channels_no;
607
608 /* Sample rates parsing */
609 rates_str_start = strstr(str_start, "Rates: ");
610 if (rates_str_start == NULL) {
611 ALOGI("%s: cant find rates string", __func__);
612 free(usb_device_info);
613 continue;
614 }
615 target = strchr(rates_str_start, '\n');
616 if (target == NULL) {
617 ALOGI("%s: end of line not found", __func__);
618 free(usb_device_info);
619 continue;
620 }
621 size = target - rates_str_start;
622 if ((rates_str = (char *)malloc(size + 1)) == NULL) {
623 ALOGE("%s: unable to allocate memory to hold sample rate strings",
624 __func__);
625 ret = -EINVAL;
626 free(usb_device_info);
627 break;
628 }
629 memcpy(rates_str, rates_str_start, size);
630 rates_str[size] = '\0';
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700631 ret = usb_get_sample_rates(type, rates_str, usb_device_info);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800632 if (rates_str)
633 free(rates_str);
634 if (ret < 0) {
635 ALOGI("%s: error unable to get sample rate values",
636 __func__);
637 free(usb_device_info);
638 continue;
639 }
Garmond Leung5fd0b552018-04-17 11:56:12 -0700640 // Data packet interval is an optional field.
641 // Assume 0ms interval if this cannot be read
642 // LPASS USB and HLOS USB will figure out the default to use
Mingshu Pange688f6f2020-05-07 13:14:06 +0800643 bool reconfig = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -0700644 usb_device_info->service_interval_us = DEFAULT_SERVICE_INTERVAL_US;
645 interval_str_start = strstr(str_start, DATA_PACKET_INTERVAL_STR);
646 if (interval_str_start != NULL) {
647 interval_str_start += strlen(DATA_PACKET_INTERVAL_STR);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800648 ret = get_usb_service_interval(interval_str_start, usb_device_info);
Garmond Leung5fd0b552018-04-17 11:56:12 -0700649 if (ret < 0) {
650 ALOGE("%s: error unable to get service interval, assume default",
651 __func__);
652 }
653 }
Mingshu Pange688f6f2020-05-07 13:14:06 +0800654 usb_set_service_interval(true /*playback*/,
655 usb_device_info->service_interval_us,
656 &reconfig);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800657 /* Add to list if every field is valid */
658 list_add_tail(&usb_card_info->usb_device_conf_list,
659 &usb_device_info->list);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700660 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700661
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700662done:
663 if (fd >= 0) close(fd);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700664 if (read_buf) free(read_buf);
665 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700666}
667
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800668static int usb_get_device_pb_config(struct usb_card_config *usb_card_info,
669 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700670{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800671 int ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700672
673 /* get capabilities */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800674 if ((ret = usb_get_capability(USB_PLAYBACK, usb_card_info, card))) {
675 ALOGE("%s: could not get Playback capabilities from usb device",
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700676 __func__);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800677 goto exit;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700678 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800679 usb_set_dev_id_mixer_ctl(USB_PLAYBACK, card, "USB_AUDIO_RX dev_token");
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700680 usb_set_endian_mixer_ctl(usb_card_info->endian, "USB_AUDIO_RX endian");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800681exit:
682
683 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700684}
685
Carter Hsu32a62362018-10-15 15:01:42 -0700686static int usb_get_usbid(struct usb_card_config *usb_card_info,
687 int card)
688{
689 int32_t fd=-1;
690 char path[128];
691 int ret = 0;
Weiyin Jiang0d985872019-06-13 15:47:21 +0800692 char *saveptr = NULL;
Carter Hsu32a62362018-10-15 15:01:42 -0700693
694 memset(usb_card_info->usbid, 0, sizeof(usb_card_info->usbid));
695
696 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid",
697 card);
698
699 if (ret < 0) {
700 ALOGE("%s: failed on snprintf (%d) to path %s\n",
701 __func__, ret, path);
702 goto done;
703 }
704
705 fd = open(path, O_RDONLY);
706 if (fd < 0) {
707 ALOGE("%s: error failed to open file %s error: %d\n",
708 __func__, path, errno);
709 ret = -EINVAL;
710 goto done;
711 }
712
713 if (read(fd, usb_card_info->usbid, USBID_SIZE - 1) < 0) {
714 ALOGE("file read error\n");
715 ret = -EINVAL;
716 usb_card_info->usbid[0] = '\0';
717 goto done;
718 }
719
Weiyin Jiang0d985872019-06-13 15:47:21 +0800720 strtok_r(usb_card_info->usbid, "\n", &saveptr);
Carter Hsu32a62362018-10-15 15:01:42 -0700721
722done:
723 if (fd >= 0)
724 close(fd);
725
726 return ret;
727}
728
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800729static int usb_get_device_cap_config(struct usb_card_config *usb_card_info,
730 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700731{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800732 int ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700733
734 /* get capabilities */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800735 if ((ret = usb_get_capability(USB_CAPTURE, usb_card_info, card))) {
736 ALOGE("%s: could not get Playback capabilities from usb device",
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700737 __func__);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800738 goto exit;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700739 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800740 usb_set_dev_id_mixer_ctl(USB_CAPTURE, card, "USB_AUDIO_TX dev_token");
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700741 usb_set_endian_mixer_ctl(usb_card_info->endian, "USB_AUDIO_TX endian");
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700742
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800743exit:
744 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700745}
746
Kuirong Wang1cad7142016-05-24 15:21:56 -0700747static void usb_get_sidetone_mixer(struct usb_card_config *usb_card_info)
748{
749 struct mixer_ctl *ctl;
750 unsigned int index;
751
752 for (index = 0; index < USB_SIDETONE_MAX_INDEX; index++)
753 usb_card_info->usb_sidetone_index[index] = -1;
754
755 usb_card_info->usb_snd_mixer = mixer_open(usb_card_info->usb_card);
756 for (index = 0;
757 index < sizeof(usb_sidetone_enable_str)/sizeof(usb_sidetone_enable_str[0]);
758 index++) {
759 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
760 usb_sidetone_enable_str[index]);
761 if (ctl) {
762 usb_card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX] = index;
763 /* Disable device sidetone by default */
764 mixer_ctl_set_value(ctl, 0, false);
Aalique Grahame22e49102018-12-18 14:23:57 -0800765 ALOGV("%s: sidetone mixer Control found(%s) ... disabling by default",
766 __func__, usb_sidetone_enable_str[index]);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700767 break;
768 }
769 }
Aalique Grahame22e49102018-12-18 14:23:57 -0800770
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800771 usb_get_sidetone_volume(usb_card_info);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700772
773 if ((usb_card_info->usb_snd_mixer != NULL) && (usb_audio_debug_enable))
774 usb_soundcard_list_controls(usb_card_info->usb_snd_mixer);
775
776 return;
777}
778
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700779static inline bool usb_output_device(audio_devices_t device) {
780 // ignore accessory for now
781 if (device == AUDIO_DEVICE_OUT_USB_ACCESSORY)
782 return false;
783 return audio_is_usb_out_device(device);
784}
785
786static inline bool usb_input_device(audio_devices_t device) {
787 // ignore accessory for now
788 if (device == AUDIO_DEVICE_IN_USB_ACCESSORY)
789 return false;
790 return audio_is_usb_in_device(device);
791}
792
793static bool usb_valid_device(audio_devices_t device)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700794{
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700795 return usb_output_device(device) ||
796 usb_input_device(device);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800797}
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700798
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800799static void usb_print_active_device(void){
800 struct listnode *node_i, *node_j;
801 struct usb_device_config *dev_info;
802 struct usb_card_config *card_info;
803 unsigned int i;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700804
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800805 ALOGI("%s", __func__);
806 list_for_each(node_i, &usbmod->usb_card_conf_list) {
807 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700808 ALOGI("%s: card_dev_type (0x%x), card_no(%d), %s",
809 __func__, card_info->usb_device_type,
810 card_info->usb_card, card_info->endian ? "BE" : "LE");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800811 list_for_each(node_j, &card_info->usb_device_conf_list) {
812 dev_info = node_to_item(node_j, struct usb_device_config, list);
813 ALOGI("%s: bit-width(%d) channel(%d)",
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700814 __func__, dev_info->bit_width, dev_info->channels);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800815 for (i = 0; i < dev_info->rate_size; i++)
816 ALOGI("%s: rate %d", __func__, dev_info->rates[i]);
817 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700818 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800819}
820
821static bool usb_get_best_match_for_bit_width(
822 struct listnode *dev_list,
823 unsigned int stream_bit_width,
824 unsigned int *bit_width)
825{
826 struct listnode *node_i;
827 struct usb_device_config *dev_info;
828 unsigned int candidate = 0;
829
830 list_for_each(node_i, dev_list) {
831 dev_info = node_to_item(node_i, struct usb_device_config, list);
832 ALOGI_IF(usb_audio_debug_enable,
833 "%s: USB bw(%d), stream bw(%d), candidate(%d)",
834 __func__, dev_info->bit_width,
835 stream_bit_width, candidate);
836 if (dev_info->bit_width == stream_bit_width) {
837 *bit_width = dev_info->bit_width;
838 ALOGV("%s: Found match bit-width (%d)",
839 __func__, dev_info->bit_width);
840 goto exit;
841 } else if (candidate == 0) {
842 candidate = dev_info->bit_width;
843 }
844 /*
845 * If stream bit is 24, USB supports both 16 bit and 32 bit, then
846 * higher bit width 32 is picked up instead of 16-bit
847 */
848 else if (ABS_SUB(stream_bit_width, dev_info->bit_width) <
849 ABS_SUB(stream_bit_width, candidate)) {
850 candidate = dev_info->bit_width;
851 }
852 else if ((ABS_SUB(stream_bit_width, dev_info->bit_width) ==
853 ABS_SUB(stream_bit_width, candidate)) &&
854 (dev_info->bit_width > candidate)) {
855 candidate = dev_info->bit_width;
856 }
857 }
858 ALOGV("%s: No match found, use the best candidate bw(%d)",
859 __func__, candidate);
860 *bit_width = candidate;
861exit:
862 return true;
863}
864
865static bool usb_get_best_match_for_channels(
866 struct listnode *dev_list,
867 unsigned int bit_width,
868 unsigned int stream_ch,
869 unsigned int *ch)
870{
871 struct listnode *node_i;
872 struct usb_device_config *dev_info;
873 unsigned int candidate = 0;
874
875 list_for_each(node_i, dev_list) {
876 dev_info = node_to_item(node_i, struct usb_device_config, list);
877 ALOGI_IF(usb_audio_debug_enable,
878 "%s: USB ch(%d)bw(%d), stream ch(%d)bw(%d), candidate(%d)",
879 __func__, dev_info->channels, dev_info->bit_width,
880 stream_ch, bit_width, candidate);
881 if (dev_info->bit_width != bit_width)
882 continue;
883 if (dev_info->channels== stream_ch) {
884 *ch = dev_info->channels;
885 ALOGV("%s: Found match channels (%d)",
886 __func__, dev_info->channels);
887 goto exit;
888 } else if (candidate == 0)
889 candidate = dev_info->channels;
890 /*
891 * If stream channel is 4, USB supports both 3 and 5, then
892 * higher channel 5 is picked up instead of 3
893 */
894 else if (ABS_SUB(stream_ch, dev_info->channels) <
895 ABS_SUB(stream_ch, candidate)) {
896 candidate = dev_info->channels;
897 } else if ((ABS_SUB(stream_ch, dev_info->channels) ==
898 ABS_SUB(stream_ch, candidate)) &&
899 (dev_info->channels > candidate)) {
900 candidate = dev_info->channels;
901 }
902 }
903 ALOGV("%s: No match found, use the best candidate ch(%d)",
904 __func__, candidate);
905 *ch = candidate;
906exit:
907 return true;
908
909}
910
911static bool usb_sample_rate_multiple(
912 unsigned int stream_sample_rate,
913 unsigned int base)
914{
915 return (((stream_sample_rate / base) * base) == stream_sample_rate);
916}
917
918static bool usb_find_sample_rate_candidate(unsigned int base,
919 unsigned stream_rate,
920 unsigned int usb_rate,
921 unsigned int cur_candidate,
922 unsigned int *update_candidate)
923{
924 /* For sample rate, we should consider fracational sample rate as high priority.
925 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
926 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
927 */
928 if (!usb_sample_rate_multiple(cur_candidate, base) &&
929 usb_sample_rate_multiple(usb_rate, base)) {
930 *update_candidate = usb_rate;
931 } else if (usb_sample_rate_multiple(cur_candidate, base) &&
932 usb_sample_rate_multiple(usb_rate, base)) {
933 if (ABS_SUB(stream_rate, usb_rate) <
934 ABS_SUB(stream_rate, cur_candidate)) {
935 *update_candidate = usb_rate;
936 } else if ((ABS_SUB(stream_rate, usb_rate) ==
937 ABS_SUB(stream_rate, cur_candidate)) &&
938 (usb_rate > cur_candidate)) {
939 *update_candidate = usb_rate;
940 }
941 } else if (!usb_sample_rate_multiple(cur_candidate, base) &&
942 !usb_sample_rate_multiple(usb_rate, base)) {
943 if (ABS_SUB(stream_rate, usb_rate) <
944 ABS_SUB(stream_rate, cur_candidate)) {
945 *update_candidate = usb_rate;
946 } else if ((ABS_SUB(stream_rate, usb_rate) ==
947 ABS_SUB(stream_rate, cur_candidate)) &&
948 (usb_rate > cur_candidate)) {
949 *update_candidate = usb_rate;
950 }
951 }
952 return true;
953}
954
955static bool usb_get_best_match_for_sample_rate(
956 struct listnode *dev_list,
957 unsigned int bit_width,
958 unsigned int ch,
959 unsigned int stream_sample_rate,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700960 unsigned int *sr,
961 unsigned int service_interval,
962 bool do_service_interval_check)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800963{
964 struct listnode *node_i;
965 struct usb_device_config *dev_info;
966 unsigned int candidate = 48000;
967 unsigned int base = SAMPLE_RATE_8000;
968 bool multiple_8k = usb_sample_rate_multiple(stream_sample_rate, base);
969 unsigned int i;
970
971 ALOGV("%s: stm ch(%d)bw(%d)sr(%d), stream sample multiple of 8kHz(%d)",
972 __func__, ch, bit_width, stream_sample_rate, multiple_8k);
973
974 list_for_each(node_i, dev_list) {
975 dev_info = node_to_item(node_i, struct usb_device_config, list);
976 ALOGI_IF(usb_audio_debug_enable,
977 "%s: USB ch(%d)bw(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
978 __func__, dev_info->channels, dev_info->bit_width,
979 ch, bit_width, stream_sample_rate, candidate);
Garmond Leung5fd0b552018-04-17 11:56:12 -0700980
981 if ((dev_info->bit_width != bit_width) ||
982 (dev_info->channels != ch) ||
983 (do_service_interval_check && (dev_info->service_interval_us !=
984 service_interval)))
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800985 continue;
986
987 candidate = 0;
988 for (i = 0; i < dev_info->rate_size; i++) {
989 ALOGI_IF(usb_audio_debug_enable,
990 "%s: USB ch(%d)bw(%d)sr(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
991 __func__, dev_info->channels,
992 dev_info->bit_width, dev_info->rates[i],
993 ch, bit_width, stream_sample_rate, candidate);
994 if (stream_sample_rate == dev_info->rates[i]) {
995 *sr = dev_info->rates[i];
996 ALOGV("%s: Found match sample rate (%d)",
997 __func__, dev_info->rates[i]);
998 goto exit;
999 } else if (candidate == 0) {
1000 candidate = dev_info->rates[i];
1001 /*
1002 * For sample rate, we should consider fracational sample rate as high priority.
1003 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
1004 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
1005 */
1006 } else if (multiple_8k) {
1007 usb_find_sample_rate_candidate(SAMPLE_RATE_8000,
1008 stream_sample_rate,
1009 dev_info->rates[i],
1010 candidate,
1011 &candidate);
1012 } else {
1013 usb_find_sample_rate_candidate(SAMPLE_RATE_11025,
1014 stream_sample_rate,
1015 dev_info->rates[i],
1016 candidate,
1017 &candidate);
1018 }
1019 }
1020 }
1021 ALOGV("%s: No match found, use the best candidate sr(%d)",
1022 __func__, candidate);
1023 *sr = candidate;
1024exit:
1025 return true;
1026}
1027
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001028static bool usb_audio_backend_apply_policy(struct listnode *dev_list,
1029 unsigned int *bit_width,
1030 unsigned int *sample_rate,
Ashish Jainb26edfb2016-08-25 00:10:11 +05301031 unsigned int *ch)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001032{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001033 bool is_usb_supported = true;
1034
1035 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) channels (%d)",
Ashish Jainb26edfb2016-08-25 00:10:11 +05301036 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001037 if (list_empty(dev_list)) {
1038 *sample_rate = 48000;
1039 *bit_width = 16;
Ashish Jainb26edfb2016-08-25 00:10:11 +05301040 *ch = 2;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001041 ALOGI("%s: list is empty,fall back to default setting", __func__);
1042 goto exit;
1043 }
1044 usb_get_best_match_for_bit_width(dev_list, *bit_width, bit_width);
1045 usb_get_best_match_for_channels(dev_list,
1046 *bit_width,
Ashish Jainb26edfb2016-08-25 00:10:11 +05301047 *ch,
1048 ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001049 usb_get_best_match_for_sample_rate(dev_list,
1050 *bit_width,
Ashish Jainb26edfb2016-08-25 00:10:11 +05301051 *ch,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001052 *sample_rate,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001053 sample_rate,
1054 0 /*service int*/,
1055 false /*do service int check*/);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001056exit:
1057 ALOGV("%s: Updated sample rate per profile: bit-width(%d) rate(%d) chs(%d)",
Ashish Jainb26edfb2016-08-25 00:10:11 +05301058 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001059 return is_usb_supported;
1060}
1061
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001062void usb_set_sidetone_gain(struct str_parms *parms,
Kuirong Wang1cad7142016-05-24 15:21:56 -07001063 char *value, int len)
1064{
1065 int err;
1066
1067 err = str_parms_get_str(parms, USB_SIDETONE_GAIN_STR,
1068 value, len);
1069 if (err >= 0) {
1070 usb_sidetone_gain = pow(10.0, (float)(atoi(value))/10.0);
1071 ALOGV("%s: sidetone gain(%s) decimal %d",
1072 __func__, value, usb_sidetone_gain);
1073 str_parms_del(parms, USB_SIDETONE_GAIN_STR);
1074 }
1075 return;
1076}
1077
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001078int usb_enable_sidetone(int device, bool enable)
Kuirong Wang1cad7142016-05-24 15:21:56 -07001079{
1080 int ret = -ENODEV;
1081 struct listnode *node_i;
1082 struct usb_card_config *card_info;
1083 int i;
1084 ALOGV("%s: card_dev_type (0x%x), sidetone enable(%d)",
1085 __func__, device, enable);
1086
1087 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1088 card_info = node_to_item(node_i, struct usb_card_config, list);
1089 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1090 __func__, card_info->usb_device_type, card_info->usb_card);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001091 if (usb_output_device(card_info->usb_device_type)) {
Kuirong Wang1cad7142016-05-24 15:21:56 -07001092 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX]) != -1) {
1093 struct mixer_ctl *ctl = mixer_get_ctl_by_name(
1094 card_info->usb_snd_mixer,
1095 usb_sidetone_enable_str[i]);
1096 if (ctl)
1097 mixer_ctl_set_value(ctl, 0, enable);
1098 else
1099 break;
1100
1101 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX]) != -1) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001102 usb_set_sidetone_volume(card_info, enable, i);
Kuirong Wang1cad7142016-05-24 15:21:56 -07001103 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001104
Kuirong Wang1cad7142016-05-24 15:21:56 -07001105 ret = 0;
1106 break;
1107 }
1108 }
1109 }
1110 return ret;
1111}
1112
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001113bool usb_is_config_supported(unsigned int *bit_width,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001114 unsigned int *sample_rate,
Kuirong Wange9894162016-08-26 15:16:39 -07001115 unsigned int *ch,
1116 bool is_playback)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001117{
1118 struct listnode *node_i;
1119 struct usb_card_config *card_info;
1120 bool is_usb_supported = false;
1121
Kuirong Wang27152a12016-11-11 10:20:30 -08001122 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) ch(%d) is_playback(%d)",
1123 __func__, *bit_width, *sample_rate, *ch, is_playback);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001124 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1125 card_info = node_to_item(node_i, struct usb_card_config, list);
1126 ALOGI_IF(usb_audio_debug_enable,
1127 "%s: card_dev_type (0x%x), card_no(%d)",
1128 __func__, card_info->usb_device_type, card_info->usb_card);
1129 /* Currently only apply the first playback sound card configuration */
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001130 if ((is_playback && usb_output_device(card_info->usb_device_type)) ||
1131 (!is_playback && usb_input_device(card_info->usb_device_type))){
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001132 is_usb_supported = usb_audio_backend_apply_policy(
1133 &card_info->usb_device_conf_list,
1134 bit_width,
1135 sample_rate,
1136 ch);
1137 break;
1138 }
1139 }
Ashish Jainb26edfb2016-08-25 00:10:11 +05301140 ALOGV("%s: updated: bit-width(%d) sample_rate(%d) channels (%d)",
1141 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001142
1143 return is_usb_supported;
1144}
1145
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001146int usb_get_max_channels(bool is_playback)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001147{
1148 struct listnode *node_i, *node_j;
1149 struct usb_device_config *dev_info;
1150 struct usb_card_config *card_info;
1151 unsigned int max_ch = 1;
1152 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1153 card_info = node_to_item(node_i, struct usb_card_config, list);
1154 if (usb_output_device(card_info->usb_device_type) && !is_playback)
1155 continue;
1156 else if (usb_input_device(card_info->usb_device_type) && is_playback)
1157 continue;
1158
1159 list_for_each(node_j, &card_info->usb_device_conf_list) {
1160 dev_info = node_to_item(node_j, struct usb_device_config, list);
1161 max_ch = _MAX(max_ch, dev_info->channels);
1162 }
1163 }
1164
1165 return max_ch;
1166}
1167
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001168int usb_get_max_bit_width(bool is_playback)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001169{
1170 struct listnode *node_i, *node_j;
1171 struct usb_device_config *dev_info;
1172 struct usb_card_config *card_info;
1173 unsigned int max_bw = 16;
1174 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1175 card_info = node_to_item(node_i, struct usb_card_config, list);
1176 if (usb_output_device(card_info->usb_device_type) && !is_playback)
1177 continue;
1178 else if (usb_input_device(card_info->usb_device_type) && is_playback)
1179 continue;
1180
1181 list_for_each(node_j, &card_info->usb_device_conf_list) {
1182 dev_info = node_to_item(node_j, struct usb_device_config, list);
1183 max_bw = _MAX(max_bw, dev_info->bit_width);
1184 }
1185 }
1186
1187 return max_bw;
1188}
1189
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001190int usb_get_sup_sample_rates(bool is_playback,
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001191 uint32_t *sample_rates,
1192 uint32_t sample_rate_size)
1193{
1194 int type = is_playback ? USB_PLAYBACK : USB_CAPTURE;
1195
1196 ALOGV("%s supported_sample_rates_mask 0x%x", __func__, supported_sample_rates_mask[type]);
1197 uint32_t bm = supported_sample_rates_mask[type];
1198 uint32_t tries = _MIN(sample_rate_size, (uint32_t)__builtin_popcount(bm));
1199
1200 int i = 0;
Vikram Panduranga5c51bff2019-09-13 16:43:44 -07001201 while (tries) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001202 int idx = __builtin_ffs(bm) - 1;
1203 sample_rates[i++] = supported_sample_rates[idx];
1204 bm &= ~(1<<idx);
Vikram Panduranga5c51bff2019-09-13 16:43:44 -07001205 tries--;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001206 }
1207
1208 return i;
1209}
1210
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001211bool usb_is_capture_supported()
Ashish Jain3e37a702016-11-25 12:27:15 +05301212{
1213 if (usbmod == NULL) {
1214 ALOGE("%s: USB device object is NULL", __func__);
1215 return false;
1216 }
1217 ALOGV("%s: capture_supported %d",__func__,usbmod->is_capture_supported);
1218 return usbmod->is_capture_supported;
1219}
1220
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001221bool usb_is_tunnel_supported()
Zhou Song6f862822017-11-06 17:27:57 +08001222{
1223 return true;
1224}
1225
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001226void usb_add_device(audio_devices_t device, int card)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001227{
1228 struct usb_card_config *usb_card_info;
1229 char check_debug_enable[PROPERTY_VALUE_MAX];
Kuirong Wang27152a12016-11-11 10:20:30 -08001230 struct listnode *node_i;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001231
Manisha Agarwalc75a0202019-12-06 18:48:25 +05301232 if (property_get("vendor.audio.usb.enable.debug",
1233 check_debug_enable, NULL) > 0) {
Aalique Grahame22e49102018-12-18 14:23:57 -08001234 if (atoi(check_debug_enable))
1235 usb_audio_debug_enable = true;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001236 }
1237
1238 ALOGI_IF(usb_audio_debug_enable,
1239 "%s: parameters device(0x%x), card(%d)",
1240 __func__, device, card);
1241 if (usbmod == NULL) {
1242 ALOGE("%s: USB device object is NULL", __func__);
1243 goto exit;
1244 }
1245
1246 if (!(usb_valid_device(device)) || (card < 0)) {
1247 ALOGE("%s:device(0x%x), card(%d)",
1248 __func__, device, card);
1249 goto exit;
1250 }
1251
Kuirong Wang27152a12016-11-11 10:20:30 -08001252 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1253 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
1254 ALOGI_IF(usb_audio_debug_enable,
1255 "%s: list has capability for card_dev_type (0x%x), card_no(%d)",
1256 __func__, usb_card_info->usb_device_type, usb_card_info->usb_card);
1257 /* If we have cached the capability */
1258 if ((usb_card_info->usb_device_type == device) && (usb_card_info->usb_card == card)) {
1259 ALOGV("%s: capability for device(0x%x), card(%d) is cached, no need to update",
1260 __func__, device, card);
1261 goto exit;
1262 }
1263 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001264 usb_card_info = calloc(1, sizeof(struct usb_card_config));
1265 if (usb_card_info == NULL) {
1266 ALOGE("%s: error unable to allocate memory",
1267 __func__);
1268 goto exit;
1269 }
1270 list_init(&usb_card_info->usb_device_conf_list);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001271 if (usb_output_device(device)) {
Carter Hsu32a62362018-10-15 15:01:42 -07001272 if (usb_get_usbid(usb_card_info, card) < 0) {
1273 ALOGE("parse card %d usbid fail", card);
1274 }
1275
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001276 if (!usb_get_device_pb_config(usb_card_info, card)){
1277 usb_card_info->usb_card = card;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001278 usb_card_info->usb_device_type = device;
Kuirong Wang1cad7142016-05-24 15:21:56 -07001279 usb_get_sidetone_mixer(usb_card_info);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001280 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1281 goto exit;
1282 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001283 } else if (usb_input_device(device)) {
Carter Hsu32a62362018-10-15 15:01:42 -07001284 if (usb_get_usbid(usb_card_info, card) < 0) {
1285 ALOGE("parse card %d usbid fail", card);
1286 }
1287
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001288 if (!usb_get_device_cap_config(usb_card_info, card)) {
1289 usb_card_info->usb_card = card;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001290 usb_card_info->usb_device_type = device;
Ashish Jain3e37a702016-11-25 12:27:15 +05301291 usbmod->is_capture_supported = true;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001292 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1293 goto exit;
1294 }
1295 }
1296 /* free memory in error case */
1297 if (usb_card_info != NULL)
1298 free(usb_card_info);
1299exit:
1300 if (usb_audio_debug_enable)
1301 usb_print_active_device();
1302 return;
1303}
1304
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001305void usb_remove_device(audio_devices_t device, int card)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001306{
1307 struct listnode *node_i, *temp_i;
1308 struct listnode *node_j, *temp_j;
1309 struct usb_device_config *dev_info;
1310 struct usb_card_config *card_info;
1311 unsigned int i;
1312
1313 ALOGV("%s: device(0x%x), card(%d)",
1314 __func__, device, card);
1315
1316 if (usbmod == NULL) {
1317 ALOGE("%s: USB device object is NULL", __func__);
1318 goto exit;
1319 }
1320
1321 if (!(usb_valid_device(device)) || (card < 0)) {
1322 ALOGE("%s: Invalid parameters device(0x%x), card(%d)",
1323 __func__, device, card);
1324 goto exit;
1325 }
1326 list_for_each_safe(node_i, temp_i, &usbmod->usb_card_conf_list) {
1327 card_info = node_to_item(node_i, struct usb_card_config, list);
1328 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1329 __func__, card_info->usb_device_type, card_info->usb_card);
1330 if ((device == card_info->usb_device_type) && (card == card_info->usb_card)){
1331 list_for_each_safe(node_j, temp_j, &card_info->usb_device_conf_list) {
1332 dev_info = node_to_item(node_j, struct usb_device_config, list);
1333 ALOGV("%s: bit-width(%d) channel(%d)",
1334 __func__, dev_info->bit_width, dev_info->channels);
1335 for (i = 0; i < dev_info->rate_size; i++)
1336 ALOGV("%s: rate %d", __func__, dev_info->rates[i]);
1337
1338 list_remove(node_j);
1339 free(node_to_item(node_j, struct usb_device_config, list));
1340 }
1341 list_remove(node_i);
1342 free(node_to_item(node_i, struct usb_card_config, list));
1343 }
1344 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001345 if (audio_is_usb_in_device(device)) { // XXX not sure if we need to check for card
1346 usbmod->is_capture_supported = false;
1347 supported_sample_rates_mask[USB_CAPTURE] = 0;
1348 } else
1349 supported_sample_rates_mask[USB_PLAYBACK] = 0;
1350
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001351exit:
1352 if (usb_audio_debug_enable)
1353 usb_print_active_device();
1354
1355 return;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001356}
1357
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001358bool usb_alive(int card) {
Kevin Rocardfce19002017-08-07 19:21:36 -07001359 char path[PATH_MAX] = {0};
1360 // snprintf should never fail
1361 (void) snprintf(path, sizeof(path), "/proc/asound/card%u/stream0", card);
1362 return access(path, F_OK) == 0;
1363}
1364
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001365unsigned long usb_find_service_interval(bool min,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001366 bool playback) {
1367 struct usb_card_config *card_info = NULL;
1368 struct usb_device_config *dev_info = NULL;
1369 struct listnode *node_i = NULL;
1370 struct listnode *node_j = NULL;
Aalique Grahame22e49102018-12-18 14:23:57 -08001371 unsigned long interval_us = min ? ULONG_MAX : 0;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001372 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1373 card_info = node_to_item(node_i, struct usb_card_config, list);
1374 list_for_each(node_j, &card_info->usb_device_conf_list) {
1375 dev_info = node_to_item(node_j, struct usb_device_config, list);
1376 bool match = (playback && (dev_info->type == USB_PLAYBACK)) ||
1377 (!playback && (dev_info->type == USB_CAPTURE));
1378 if (match) {
1379 interval_us = min ?
1380 _MIN(interval_us, dev_info->service_interval_us) :
1381 _MAX(interval_us, dev_info->service_interval_us);
1382 }
1383 }
1384 break;
1385 }
1386 return interval_us;
1387}
1388
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001389int usb_altset_for_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001390 unsigned long service_interval,
1391 uint32_t *bit_width,
1392 uint32_t *sample_rate,
1393 uint32_t *channels)
1394{
1395 struct usb_card_config *card_info = NULL;
Aalique Grahame22e49102018-12-18 14:23:57 -08001396 struct usb_device_config *dev_info = NULL;
1397 struct listnode *node_i = NULL;
1398 struct listnode *node_j = NULL;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001399 uint32_t bw = 0;
1400 uint32_t ch = 0;
1401 uint32_t sr = 0;
1402
1403 if (service_interval == 0)
1404 return 0;
1405 /* not a valid service interval to search for */
1406
1407#define FIND_BEST_MATCH(local_var, field, cond) \
1408 list_for_each(node_i, &usbmod->usb_card_conf_list) { \
1409 /* Currently only apply the first playback sound card configuration */ \
1410 card_info = node_to_item(node_i, struct usb_card_config, list); \
1411 list_for_each(node_j, &card_info->usb_device_conf_list) { \
1412 dev_info = node_to_item(node_j, struct usb_device_config, list); \
1413 bool match = (playback && (dev_info->type == USB_PLAYBACK)) || \
1414 (!playback && (dev_info->type == USB_CAPTURE)); \
1415 if (match && (cond)) { \
Garmond Leung003d9ea2018-08-14 17:05:08 -07001416 if (dev_info->field == *field) { \
1417 local_var = dev_info->field; \
1418 break; \
1419 } \
Garmond Leung5fd0b552018-04-17 11:56:12 -07001420 local_var = _MAX(local_var, dev_info->field); \
1421 } \
1422 } \
1423 break; \
1424 }
1425
1426 FIND_BEST_MATCH(bw, bit_width, dev_info->service_interval_us == service_interval);
1427 FIND_BEST_MATCH(ch, channels, \
1428 dev_info->service_interval_us == service_interval && \
1429 dev_info->bit_width == bw);
Garmond Leung003d9ea2018-08-14 17:05:08 -07001430 sr = *sample_rate;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001431 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1432 /* Currently only apply the first playback sound card configuration */
1433 card_info = node_to_item(node_i, struct usb_card_config, list);
1434 if ((playback && usb_output_device(card_info->usb_device_type)) ||
1435 (!playback && usb_input_device(card_info->usb_device_type))) {
1436 usb_get_best_match_for_sample_rate(&card_info->usb_device_conf_list,
1437 bw, ch, sr, &sr,
1438 service_interval,
1439 true);
1440 }
1441 break;
1442 }
1443
1444#define SET_OR_RETURN_ON_ERROR(arg, local_var, cond) \
1445 if (local_var != (cond)) arg = local_var; else return -1;
1446
1447 SET_OR_RETURN_ON_ERROR(*bit_width, bw, 0);
1448 SET_OR_RETURN_ON_ERROR(*sample_rate, sr, 0);
1449 SET_OR_RETURN_ON_ERROR(*channels, ch, 0);
1450 return 0;
1451#undef FIND_BEST_MATCH
1452#undef SET_OR_RETURN_ON_ERROR
1453}
1454
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001455int usb_check_and_set_svc_int(struct audio_usecase *uc_info,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001456 bool starting_output_stream)
1457{
1458 struct listnode *node = NULL;
1459 struct audio_usecase *usecase = uc_info;
1460 bool reconfig = false;
1461 bool burst_mode = true;
1462 unsigned long service_interval = 0;
1463 struct audio_device *adev = usbmod->adev;
1464
1465 ALOGV("%s: enter:", __func__);
1466
1467 if ((starting_output_stream == true &&
1468 ((uc_info->id == USECASE_AUDIO_PLAYBACK_MMAP) ||
1469 (uc_info->id == USECASE_AUDIO_PLAYBACK_ULL))) ||
1470 (voice_is_call_state_active(usbmod->adev))) {
1471 burst_mode = false;
1472 } else {
1473 /* set if the valid usecase do not already exist */
1474 list_for_each(node, &adev->usecase_list) {
1475 usecase = node_to_item(node, struct audio_usecase, list);
1476 if (usecase->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001477 is_usb_out_device_type(&usecase->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07001478 switch (usecase->id) {
1479 case USECASE_AUDIO_PLAYBACK_MMAP:
1480 case USECASE_AUDIO_PLAYBACK_ULL:
1481 {
1482 if (uc_info != usecase) {
1483 //another ULL stream exists
1484 ALOGV("%s: another ULL Stream in active use-case list burst mode = false.", __func__);
1485 burst_mode = false;
1486 } else {
1487 ALOGV("%s:current ULL uc is the same as incoming uc_info \
1488 which means we are stopping the output stream, \
1489 we don't want to set burst mode to false", __func__);
1490 }
1491 break;
1492 }
1493 default:
1494 break;
1495 }
1496 }
1497 }
1498 }
1499
1500 ALOGV("%s: burst mode(%d).", __func__,burst_mode);
1501
1502 service_interval =
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001503 usb_find_service_interval(!burst_mode, true /*playback*/);
Garmond Leung5fd0b552018-04-17 11:56:12 -07001504
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001505 usb_set_service_interval(true /*playback*/,
Yunfei Zhang7728dce2018-10-31 10:20:52 +08001506 service_interval,
1507 &reconfig);
Garmond Leung5fd0b552018-04-17 11:56:12 -07001508
1509 /* no change or not supported or no active usecases */
1510 if (reconfig)
1511 return -1;
1512 return 0;
1513}
1514
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001515bool usb_is_reconfig_req()
Garmond Leung5fd0b552018-04-17 11:56:12 -07001516{
1517 return usbmod->usb_reconfig;
1518}
1519
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001520void usb_set_reconfig(bool is_required)
Garmond Leung5fd0b552018-04-17 11:56:12 -07001521{
1522 usbmod->usb_reconfig = is_required;
1523}
1524
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001525bool usb_connected(struct str_parms *parms) {
Sharad Sangled0a50b22018-04-05 23:28:32 +05301526 int card = -1;
1527 struct listnode *node_i = NULL;
1528 struct usb_card_config *usb_card_info = NULL;
1529 bool usb_connected = false;
1530
Weiyin Jiang63831272018-06-28 11:41:01 +08001531 if ((parms != NULL) && str_parms_get_int(parms, "card", &card) >= 0) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001532 usb_connected = usb_alive(card);
Sharad Sangled0a50b22018-04-05 23:28:32 +05301533 } else {
1534 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1535 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001536 if (usb_alive(usb_card_info->usb_card)) {
Sharad Sangled0a50b22018-04-05 23:28:32 +05301537 usb_connected = true;
1538 break;
1539 }
1540 }
1541 }
1542 return usb_connected;
1543}
1544
Carter Hsu32a62362018-10-15 15:01:42 -07001545char *usb_usbid()
1546{
1547 struct usb_card_config *card_info;
1548
1549 if (usbmod == NULL)
1550 return NULL;
1551
1552 if (list_empty(&usbmod->usb_card_conf_list))
1553 return NULL;
1554
1555 card_info = node_to_item(list_head(&usbmod->usb_card_conf_list),\
1556 struct usb_card_config, list);
1557
1558 return strdup(card_info->usbid);
1559}
1560
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001561void usb_init(void *adev)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001562{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001563 if (usbmod == NULL) {
1564 usbmod = calloc(1, sizeof(struct usb_module));
1565 if (usbmod == NULL) {
1566 ALOGE("%s: error unable to allocate memory", __func__);
1567 goto exit;
1568 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001569 } else {
1570 memset(usbmod, 0, sizeof(*usbmod));
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001571 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001572
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001573 list_init(&usbmod->usb_card_conf_list);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001574 usbmod->adev = (struct audio_device*)adev;
Kuirong Wang1cad7142016-05-24 15:21:56 -07001575 usbmod->sidetone_gain = usb_sidetone_gain;
Ashish Jain3e37a702016-11-25 12:27:15 +05301576 usbmod->is_capture_supported = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001577 usbmod->usb_reconfig = false;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001578exit:
1579 return;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001580}
1581
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001582void usb_deinit(void)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001583{
1584 if (NULL != usbmod){
1585 free(usbmod);
1586 usbmod = NULL;
1587 }
1588}