blob: 911724a480faccf431685dd4640dc9b9fb11ce26 [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);
Zhou Song8c05a3c2019-06-18 22:57:11 +0800398 tmp[eol-interval_str_start] = '\0';
Garmond Leung5fd0b552018-04-17 11:56:12 -0700399 sscanf(tmp, "%lu %2s", &interval, &time_unit[0]);
400 if (!strcmp(time_unit, "us")) {
401 multiplier = 1;
402 } else if (!strcmp(time_unit, "ms")) {
403 multiplier = 1000;
404 } else if (!strcmp(time_unit, "s")) {
405 multiplier = 1000000;
406 } else {
407 ALOGE("%s: unknown time_unit %s, assume default", __func__, time_unit);
408 interval = DEFAULT_SERVICE_INTERVAL_US;
409 multiplier = 1;
410 }
411 interval *= multiplier;
412 ALOGD("%s: set service_interval_us %lu", __func__, interval);
413 usb_device_info->service_interval_us = interval;
414 free(tmp);
415 return 0;
416}
417
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800418static int usb_get_capability(int type,
419 struct usb_card_config *usb_card_info,
420 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700421{
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700422 int32_t size = 0;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800423 int32_t fd=-1;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800424 int32_t channels_no;
Kuirong Wange9894162016-08-26 15:16:39 -0700425 char *str_start = NULL;
426 char *str_end = NULL;
427 char *channel_start = NULL;
428 char *bit_width_start = NULL;
429 char *rates_str_start = NULL;
430 char *target = NULL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700431 char *read_buf = NULL;
432 char *rates_str = NULL;
Garmond Leung5fd0b552018-04-17 11:56:12 -0700433 char *interval_str_start = NULL;
Kuirong Wange9894162016-08-26 15:16:39 -0700434 char path[128];
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700435 int ret = 0;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800436 char *bit_width_str = NULL;
437 struct usb_device_config * usb_device_info;
Kuirong Wange9894162016-08-26 15:16:39 -0700438 bool check = false;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700439
Kuirong Wange9894162016-08-26 15:16:39 -0700440 memset(path, 0, sizeof(path));
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800441 ALOGV("%s: for %s", __func__, (type == USB_PLAYBACK) ?
442 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR);
443
Kuirong Wange9894162016-08-26 15:16:39 -0700444 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/stream0",
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800445 card);
Kuirong Wange9894162016-08-26 15:16:39 -0700446 if(ret < 0) {
447 ALOGE("%s: failed on snprintf (%d) to path %s\n",
448 __func__, ret, path);
449 goto done;
450 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700451
452 fd = open(path, O_RDONLY);
453 if (fd <0) {
454 ALOGE("%s: error failed to open config file %s error: %d\n",
455 __func__, path, errno);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700456 ret = -EINVAL;
457 goto done;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700458 }
459
Apoorv Raghuvanshidad3b782014-01-29 15:31:32 -0800460 read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700461
462 if (!read_buf) {
463 ALOGE("Failed to create read_buf");
464 ret = -ENOMEM;
465 goto done;
466 }
467
Kuirong Wange9894162016-08-26 15:16:39 -0700468 if(read(fd, read_buf, USB_BUFF_SIZE) < 0) {
469 ALOGE("file read error\n");
470 goto done;
471 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800472 str_start = strstr(read_buf, ((type == USB_PLAYBACK) ?
473 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700474 if (str_start == NULL) {
475 ALOGE("%s: error %s section not found in usb config file",
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800476 __func__, ((type == USB_PLAYBACK) ?
477 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700478 ret = -EINVAL;
479 goto done;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700480 }
Kuirong Wange9894162016-08-26 15:16:39 -0700481 str_end = strstr(read_buf, ((type == USB_PLAYBACK) ?
482 CAPTURE_PROFILE_STR : PLAYBACK_PROFILE_STR));
483 if (str_end > str_start)
484 check = true;
485
486 ALOGV("%s: usb_config = %s, check %d\n", __func__, str_start, check);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700487
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800488 while (str_start != NULL) {
Kuirong Wange9894162016-08-26 15:16:39 -0700489 str_start = strstr(str_start, "Altset");
490 if ((str_start == NULL) || (check && (str_start >= str_end))) {
491 ALOGV("%s: done parsing %s\n", __func__, str_start);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800492 break;
493 }
Kuirong Wange9894162016-08-26 15:16:39 -0700494 ALOGV("%s: remaining string %s\n", __func__, str_start);
495 str_start += sizeof("Altset");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800496 usb_device_info = calloc(1, sizeof(struct usb_device_config));
497 if (usb_device_info == NULL) {
498 ALOGE("%s: error unable to allocate memory",
499 __func__);
500 ret = -ENOMEM;
501 break;
502 }
Garmond Leung5fd0b552018-04-17 11:56:12 -0700503 usb_device_info->type = type;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800504 /* Bit bit_width parsing */
505 bit_width_start = strstr(str_start, "Format: ");
506 if (bit_width_start == NULL) {
507 ALOGI("%s: Could not find bit_width string", __func__);
508 free(usb_device_info);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700509 continue;
510 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800511 target = strchr(bit_width_start, '\n');
512 if (target == NULL) {
513 ALOGI("%s:end of line not found", __func__);
514 free(usb_device_info);
515 continue;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700516 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800517 size = target - bit_width_start;
518 if ((bit_width_str = (char *)malloc(size + 1)) == NULL) {
519 ALOGE("%s: unable to allocate memory to hold bit width strings",
520 __func__);
521 ret = -EINVAL;
522 free(usb_device_info);
523 break;
524 }
525 memcpy(bit_width_str, bit_width_start, size);
526 bit_width_str[size] = '\0';
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700527
528 const char * formats[] = { "S32", "S24_3", "S24", "S16" };
529 const int bit_width[] = { 32, 24, 24, 16};
530 for (size_t i = 0; i < ARRAY_SIZE(formats); i++) {
531 const char * s = strstr(bit_width_str, formats[i]);
532 if (s) {
533 usb_device_info->bit_width = bit_width[i];
534 usb_card_info->endian = strstr(s, "BE") ? 1 : 0;
535 break;
536 }
537 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800538
539 if (bit_width_str)
540 free(bit_width_str);
541
542 /* channels parsing */
543 channel_start = strstr(str_start, CHANNEL_NUMBER_STR);
544 if (channel_start == NULL) {
545 ALOGI("%s: could not find Channels string", __func__);
546 free(usb_device_info);
547 continue;
548 }
549 channels_no = atoi(channel_start + strlen(CHANNEL_NUMBER_STR));
550 usb_device_info->channels = channels_no;
551
552 /* Sample rates parsing */
553 rates_str_start = strstr(str_start, "Rates: ");
554 if (rates_str_start == NULL) {
555 ALOGI("%s: cant find rates string", __func__);
556 free(usb_device_info);
557 continue;
558 }
559 target = strchr(rates_str_start, '\n');
560 if (target == NULL) {
561 ALOGI("%s: end of line not found", __func__);
562 free(usb_device_info);
563 continue;
564 }
565 size = target - rates_str_start;
566 if ((rates_str = (char *)malloc(size + 1)) == NULL) {
567 ALOGE("%s: unable to allocate memory to hold sample rate strings",
568 __func__);
569 ret = -EINVAL;
570 free(usb_device_info);
571 break;
572 }
573 memcpy(rates_str, rates_str_start, size);
574 rates_str[size] = '\0';
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700575 ret = usb_get_sample_rates(type, rates_str, usb_device_info);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800576 if (rates_str)
577 free(rates_str);
578 if (ret < 0) {
579 ALOGI("%s: error unable to get sample rate values",
580 __func__);
581 free(usb_device_info);
582 continue;
583 }
Garmond Leung5fd0b552018-04-17 11:56:12 -0700584 // Data packet interval is an optional field.
585 // Assume 0ms interval if this cannot be read
586 // LPASS USB and HLOS USB will figure out the default to use
587 usb_device_info->service_interval_us = DEFAULT_SERVICE_INTERVAL_US;
588 interval_str_start = strstr(str_start, DATA_PACKET_INTERVAL_STR);
589 if (interval_str_start != NULL) {
590 interval_str_start += strlen(DATA_PACKET_INTERVAL_STR);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800591 ret = get_usb_service_interval(interval_str_start, usb_device_info);
Garmond Leung5fd0b552018-04-17 11:56:12 -0700592 if (ret < 0) {
593 ALOGE("%s: error unable to get service interval, assume default",
594 __func__);
595 }
596 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800597 /* Add to list if every field is valid */
598 list_add_tail(&usb_card_info->usb_device_conf_list,
599 &usb_device_info->list);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700600 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700601
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700602done:
603 if (fd >= 0) close(fd);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700604 if (read_buf) free(read_buf);
605 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700606}
607
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800608static int usb_get_device_pb_config(struct usb_card_config *usb_card_info,
609 int card)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700610{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800611 int ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700612
613 /* get capabilities */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800614 if ((ret = usb_get_capability(USB_PLAYBACK, usb_card_info, card))) {
615 ALOGE("%s: could not get Playback capabilities from usb device",
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700616 __func__);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800617 goto exit;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700618 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800619 usb_set_dev_id_mixer_ctl(USB_PLAYBACK, card, "USB_AUDIO_RX dev_token");
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700620 usb_set_endian_mixer_ctl(usb_card_info->endian, "USB_AUDIO_RX endian");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800621exit:
622
623 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700624}
625
Carter Hsu32a62362018-10-15 15:01:42 -0700626static int usb_get_usbid(struct usb_card_config *usb_card_info,
627 int card)
628{
629 int32_t fd=-1;
630 char path[128];
631 int ret = 0;
632
633 memset(usb_card_info->usbid, 0, sizeof(usb_card_info->usbid));
634
635 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid",
636 card);
637
638 if (ret < 0) {
639 ALOGE("%s: failed on snprintf (%d) to path %s\n",
640 __func__, ret, path);
641 goto done;
642 }
643
644 fd = open(path, O_RDONLY);
645 if (fd < 0) {
646 ALOGE("%s: error failed to open file %s error: %d\n",
647 __func__, path, errno);
648 ret = -EINVAL;
649 goto done;
650 }
651
652 if (read(fd, usb_card_info->usbid, USBID_SIZE - 1) < 0) {
653 ALOGE("file read error\n");
654 ret = -EINVAL;
655 usb_card_info->usbid[0] = '\0';
656 goto done;
657 }
658
659 strtok(usb_card_info->usbid, "\n");
660
661done:
662 if (fd >= 0)
663 close(fd);
664
665 return ret;
666}
667
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800668static int usb_get_device_cap_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_CAPTURE, 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_CAPTURE, card, "USB_AUDIO_TX dev_token");
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700680 usb_set_endian_mixer_ctl(usb_card_info->endian, "USB_AUDIO_TX endian");
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700681
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800682exit:
683 return ret;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700684}
685
Kuirong Wang1cad7142016-05-24 15:21:56 -0700686static void usb_get_sidetone_mixer(struct usb_card_config *usb_card_info)
687{
688 struct mixer_ctl *ctl;
689 unsigned int index;
690
691 for (index = 0; index < USB_SIDETONE_MAX_INDEX; index++)
692 usb_card_info->usb_sidetone_index[index] = -1;
693
694 usb_card_info->usb_snd_mixer = mixer_open(usb_card_info->usb_card);
695 for (index = 0;
696 index < sizeof(usb_sidetone_enable_str)/sizeof(usb_sidetone_enable_str[0]);
697 index++) {
698 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
699 usb_sidetone_enable_str[index]);
700 if (ctl) {
701 usb_card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX] = index;
702 /* Disable device sidetone by default */
703 mixer_ctl_set_value(ctl, 0, false);
Aalique Grahame22e49102018-12-18 14:23:57 -0800704 ALOGV("%s: sidetone mixer Control found(%s) ... disabling by default",
705 __func__, usb_sidetone_enable_str[index]);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700706 break;
707 }
708 }
Aalique Grahame22e49102018-12-18 14:23:57 -0800709
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800710 usb_get_sidetone_volume(usb_card_info);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700711
712 if ((usb_card_info->usb_snd_mixer != NULL) && (usb_audio_debug_enable))
713 usb_soundcard_list_controls(usb_card_info->usb_snd_mixer);
714
715 return;
716}
717
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700718static inline bool usb_output_device(audio_devices_t device) {
719 // ignore accessory for now
720 if (device == AUDIO_DEVICE_OUT_USB_ACCESSORY)
721 return false;
722 return audio_is_usb_out_device(device);
723}
724
725static inline bool usb_input_device(audio_devices_t device) {
726 // ignore accessory for now
727 if (device == AUDIO_DEVICE_IN_USB_ACCESSORY)
728 return false;
729 return audio_is_usb_in_device(device);
730}
731
732static bool usb_valid_device(audio_devices_t device)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700733{
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700734 return usb_output_device(device) ||
735 usb_input_device(device);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800736}
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700737
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800738static void usb_print_active_device(void){
739 struct listnode *node_i, *node_j;
740 struct usb_device_config *dev_info;
741 struct usb_card_config *card_info;
742 unsigned int i;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700743
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800744 ALOGI("%s", __func__);
745 list_for_each(node_i, &usbmod->usb_card_conf_list) {
746 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700747 ALOGI("%s: card_dev_type (0x%x), card_no(%d), %s",
748 __func__, card_info->usb_device_type,
749 card_info->usb_card, card_info->endian ? "BE" : "LE");
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800750 list_for_each(node_j, &card_info->usb_device_conf_list) {
751 dev_info = node_to_item(node_j, struct usb_device_config, list);
752 ALOGI("%s: bit-width(%d) channel(%d)",
Haynes Mathew Georgec8f816c2016-10-25 12:02:02 -0700753 __func__, dev_info->bit_width, dev_info->channels);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800754 for (i = 0; i < dev_info->rate_size; i++)
755 ALOGI("%s: rate %d", __func__, dev_info->rates[i]);
756 }
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700757 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800758}
759
760static bool usb_get_best_match_for_bit_width(
761 struct listnode *dev_list,
762 unsigned int stream_bit_width,
763 unsigned int *bit_width)
764{
765 struct listnode *node_i;
766 struct usb_device_config *dev_info;
767 unsigned int candidate = 0;
768
769 list_for_each(node_i, dev_list) {
770 dev_info = node_to_item(node_i, struct usb_device_config, list);
771 ALOGI_IF(usb_audio_debug_enable,
772 "%s: USB bw(%d), stream bw(%d), candidate(%d)",
773 __func__, dev_info->bit_width,
774 stream_bit_width, candidate);
775 if (dev_info->bit_width == stream_bit_width) {
776 *bit_width = dev_info->bit_width;
777 ALOGV("%s: Found match bit-width (%d)",
778 __func__, dev_info->bit_width);
779 goto exit;
780 } else if (candidate == 0) {
781 candidate = dev_info->bit_width;
782 }
783 /*
784 * If stream bit is 24, USB supports both 16 bit and 32 bit, then
785 * higher bit width 32 is picked up instead of 16-bit
786 */
787 else if (ABS_SUB(stream_bit_width, dev_info->bit_width) <
788 ABS_SUB(stream_bit_width, candidate)) {
789 candidate = dev_info->bit_width;
790 }
791 else if ((ABS_SUB(stream_bit_width, dev_info->bit_width) ==
792 ABS_SUB(stream_bit_width, candidate)) &&
793 (dev_info->bit_width > candidate)) {
794 candidate = dev_info->bit_width;
795 }
796 }
797 ALOGV("%s: No match found, use the best candidate bw(%d)",
798 __func__, candidate);
799 *bit_width = candidate;
800exit:
801 return true;
802}
803
804static bool usb_get_best_match_for_channels(
805 struct listnode *dev_list,
806 unsigned int bit_width,
807 unsigned int stream_ch,
808 unsigned int *ch)
809{
810 struct listnode *node_i;
811 struct usb_device_config *dev_info;
812 unsigned int candidate = 0;
813
814 list_for_each(node_i, dev_list) {
815 dev_info = node_to_item(node_i, struct usb_device_config, list);
816 ALOGI_IF(usb_audio_debug_enable,
817 "%s: USB ch(%d)bw(%d), stream ch(%d)bw(%d), candidate(%d)",
818 __func__, dev_info->channels, dev_info->bit_width,
819 stream_ch, bit_width, candidate);
820 if (dev_info->bit_width != bit_width)
821 continue;
822 if (dev_info->channels== stream_ch) {
823 *ch = dev_info->channels;
824 ALOGV("%s: Found match channels (%d)",
825 __func__, dev_info->channels);
826 goto exit;
827 } else if (candidate == 0)
828 candidate = dev_info->channels;
829 /*
830 * If stream channel is 4, USB supports both 3 and 5, then
831 * higher channel 5 is picked up instead of 3
832 */
833 else if (ABS_SUB(stream_ch, dev_info->channels) <
834 ABS_SUB(stream_ch, candidate)) {
835 candidate = dev_info->channels;
836 } else if ((ABS_SUB(stream_ch, dev_info->channels) ==
837 ABS_SUB(stream_ch, candidate)) &&
838 (dev_info->channels > candidate)) {
839 candidate = dev_info->channels;
840 }
841 }
842 ALOGV("%s: No match found, use the best candidate ch(%d)",
843 __func__, candidate);
844 *ch = candidate;
845exit:
846 return true;
847
848}
849
850static bool usb_sample_rate_multiple(
851 unsigned int stream_sample_rate,
852 unsigned int base)
853{
854 return (((stream_sample_rate / base) * base) == stream_sample_rate);
855}
856
857static bool usb_find_sample_rate_candidate(unsigned int base,
858 unsigned stream_rate,
859 unsigned int usb_rate,
860 unsigned int cur_candidate,
861 unsigned int *update_candidate)
862{
863 /* For sample rate, we should consider fracational sample rate as high priority.
864 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
865 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
866 */
867 if (!usb_sample_rate_multiple(cur_candidate, base) &&
868 usb_sample_rate_multiple(usb_rate, base)) {
869 *update_candidate = usb_rate;
870 } else if (usb_sample_rate_multiple(cur_candidate, base) &&
871 usb_sample_rate_multiple(usb_rate, base)) {
872 if (ABS_SUB(stream_rate, usb_rate) <
873 ABS_SUB(stream_rate, cur_candidate)) {
874 *update_candidate = usb_rate;
875 } else if ((ABS_SUB(stream_rate, usb_rate) ==
876 ABS_SUB(stream_rate, cur_candidate)) &&
877 (usb_rate > cur_candidate)) {
878 *update_candidate = usb_rate;
879 }
880 } else if (!usb_sample_rate_multiple(cur_candidate, base) &&
881 !usb_sample_rate_multiple(usb_rate, base)) {
882 if (ABS_SUB(stream_rate, usb_rate) <
883 ABS_SUB(stream_rate, cur_candidate)) {
884 *update_candidate = usb_rate;
885 } else if ((ABS_SUB(stream_rate, usb_rate) ==
886 ABS_SUB(stream_rate, cur_candidate)) &&
887 (usb_rate > cur_candidate)) {
888 *update_candidate = usb_rate;
889 }
890 }
891 return true;
892}
893
894static bool usb_get_best_match_for_sample_rate(
895 struct listnode *dev_list,
896 unsigned int bit_width,
897 unsigned int ch,
898 unsigned int stream_sample_rate,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700899 unsigned int *sr,
900 unsigned int service_interval,
901 bool do_service_interval_check)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800902{
903 struct listnode *node_i;
904 struct usb_device_config *dev_info;
905 unsigned int candidate = 48000;
906 unsigned int base = SAMPLE_RATE_8000;
907 bool multiple_8k = usb_sample_rate_multiple(stream_sample_rate, base);
908 unsigned int i;
909
910 ALOGV("%s: stm ch(%d)bw(%d)sr(%d), stream sample multiple of 8kHz(%d)",
911 __func__, ch, bit_width, stream_sample_rate, multiple_8k);
912
913 list_for_each(node_i, dev_list) {
914 dev_info = node_to_item(node_i, struct usb_device_config, list);
915 ALOGI_IF(usb_audio_debug_enable,
916 "%s: USB ch(%d)bw(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
917 __func__, dev_info->channels, dev_info->bit_width,
918 ch, bit_width, stream_sample_rate, candidate);
Garmond Leung5fd0b552018-04-17 11:56:12 -0700919
920 if ((dev_info->bit_width != bit_width) ||
921 (dev_info->channels != ch) ||
922 (do_service_interval_check && (dev_info->service_interval_us !=
923 service_interval)))
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800924 continue;
925
926 candidate = 0;
927 for (i = 0; i < dev_info->rate_size; i++) {
928 ALOGI_IF(usb_audio_debug_enable,
929 "%s: USB ch(%d)bw(%d)sr(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
930 __func__, dev_info->channels,
931 dev_info->bit_width, dev_info->rates[i],
932 ch, bit_width, stream_sample_rate, candidate);
933 if (stream_sample_rate == dev_info->rates[i]) {
934 *sr = dev_info->rates[i];
935 ALOGV("%s: Found match sample rate (%d)",
936 __func__, dev_info->rates[i]);
937 goto exit;
938 } else if (candidate == 0) {
939 candidate = dev_info->rates[i];
940 /*
941 * For sample rate, we should consider fracational sample rate as high priority.
942 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
943 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
944 */
945 } else if (multiple_8k) {
946 usb_find_sample_rate_candidate(SAMPLE_RATE_8000,
947 stream_sample_rate,
948 dev_info->rates[i],
949 candidate,
950 &candidate);
951 } else {
952 usb_find_sample_rate_candidate(SAMPLE_RATE_11025,
953 stream_sample_rate,
954 dev_info->rates[i],
955 candidate,
956 &candidate);
957 }
958 }
959 }
960 ALOGV("%s: No match found, use the best candidate sr(%d)",
961 __func__, candidate);
962 *sr = candidate;
963exit:
964 return true;
965}
966
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800967static bool usb_audio_backend_apply_policy(struct listnode *dev_list,
968 unsigned int *bit_width,
969 unsigned int *sample_rate,
Ashish Jainb26edfb2016-08-25 00:10:11 +0530970 unsigned int *ch)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800971{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800972 bool is_usb_supported = true;
973
974 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) channels (%d)",
Ashish Jainb26edfb2016-08-25 00:10:11 +0530975 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800976 if (list_empty(dev_list)) {
977 *sample_rate = 48000;
978 *bit_width = 16;
Ashish Jainb26edfb2016-08-25 00:10:11 +0530979 *ch = 2;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800980 ALOGI("%s: list is empty,fall back to default setting", __func__);
981 goto exit;
982 }
983 usb_get_best_match_for_bit_width(dev_list, *bit_width, bit_width);
984 usb_get_best_match_for_channels(dev_list,
985 *bit_width,
Ashish Jainb26edfb2016-08-25 00:10:11 +0530986 *ch,
987 ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800988 usb_get_best_match_for_sample_rate(dev_list,
989 *bit_width,
Ashish Jainb26edfb2016-08-25 00:10:11 +0530990 *ch,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800991 *sample_rate,
Garmond Leung5fd0b552018-04-17 11:56:12 -0700992 sample_rate,
993 0 /*service int*/,
994 false /*do service int check*/);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800995exit:
996 ALOGV("%s: Updated sample rate per profile: bit-width(%d) rate(%d) chs(%d)",
Ashish Jainb26edfb2016-08-25 00:10:11 +0530997 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -0800998 return is_usb_supported;
999}
1000
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001001void usb_set_sidetone_gain(struct str_parms *parms,
Kuirong Wang1cad7142016-05-24 15:21:56 -07001002 char *value, int len)
1003{
1004 int err;
1005
1006 err = str_parms_get_str(parms, USB_SIDETONE_GAIN_STR,
1007 value, len);
1008 if (err >= 0) {
1009 usb_sidetone_gain = pow(10.0, (float)(atoi(value))/10.0);
1010 ALOGV("%s: sidetone gain(%s) decimal %d",
1011 __func__, value, usb_sidetone_gain);
1012 str_parms_del(parms, USB_SIDETONE_GAIN_STR);
1013 }
1014 return;
1015}
1016
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001017int usb_enable_sidetone(int device, bool enable)
Kuirong Wang1cad7142016-05-24 15:21:56 -07001018{
1019 int ret = -ENODEV;
1020 struct listnode *node_i;
1021 struct usb_card_config *card_info;
1022 int i;
1023 ALOGV("%s: card_dev_type (0x%x), sidetone enable(%d)",
1024 __func__, device, enable);
1025
1026 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1027 card_info = node_to_item(node_i, struct usb_card_config, list);
1028 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1029 __func__, card_info->usb_device_type, card_info->usb_card);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001030 if (usb_output_device(card_info->usb_device_type)) {
Kuirong Wang1cad7142016-05-24 15:21:56 -07001031 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX]) != -1) {
1032 struct mixer_ctl *ctl = mixer_get_ctl_by_name(
1033 card_info->usb_snd_mixer,
1034 usb_sidetone_enable_str[i]);
1035 if (ctl)
1036 mixer_ctl_set_value(ctl, 0, enable);
1037 else
1038 break;
1039
1040 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX]) != -1) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001041 usb_set_sidetone_volume(card_info, enable, i);
Kuirong Wang1cad7142016-05-24 15:21:56 -07001042 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001043
Kuirong Wang1cad7142016-05-24 15:21:56 -07001044 ret = 0;
1045 break;
1046 }
1047 }
1048 }
1049 return ret;
1050}
1051
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001052bool usb_is_config_supported(unsigned int *bit_width,
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001053 unsigned int *sample_rate,
Kuirong Wange9894162016-08-26 15:16:39 -07001054 unsigned int *ch,
1055 bool is_playback)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001056{
1057 struct listnode *node_i;
1058 struct usb_card_config *card_info;
1059 bool is_usb_supported = false;
1060
Kuirong Wang27152a12016-11-11 10:20:30 -08001061 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) ch(%d) is_playback(%d)",
1062 __func__, *bit_width, *sample_rate, *ch, is_playback);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001063 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1064 card_info = node_to_item(node_i, struct usb_card_config, list);
1065 ALOGI_IF(usb_audio_debug_enable,
1066 "%s: card_dev_type (0x%x), card_no(%d)",
1067 __func__, card_info->usb_device_type, card_info->usb_card);
1068 /* Currently only apply the first playback sound card configuration */
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001069 if ((is_playback && usb_output_device(card_info->usb_device_type)) ||
1070 (!is_playback && usb_input_device(card_info->usb_device_type))){
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001071 is_usb_supported = usb_audio_backend_apply_policy(
1072 &card_info->usb_device_conf_list,
1073 bit_width,
1074 sample_rate,
1075 ch);
1076 break;
1077 }
1078 }
Ashish Jainb26edfb2016-08-25 00:10:11 +05301079 ALOGV("%s: updated: bit-width(%d) sample_rate(%d) channels (%d)",
1080 __func__, *bit_width, *sample_rate, *ch);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001081
1082 return is_usb_supported;
1083}
1084
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001085int usb_get_max_channels(bool is_playback)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001086{
1087 struct listnode *node_i, *node_j;
1088 struct usb_device_config *dev_info;
1089 struct usb_card_config *card_info;
1090 unsigned int max_ch = 1;
1091 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1092 card_info = node_to_item(node_i, struct usb_card_config, list);
1093 if (usb_output_device(card_info->usb_device_type) && !is_playback)
1094 continue;
1095 else if (usb_input_device(card_info->usb_device_type) && is_playback)
1096 continue;
1097
1098 list_for_each(node_j, &card_info->usb_device_conf_list) {
1099 dev_info = node_to_item(node_j, struct usb_device_config, list);
1100 max_ch = _MAX(max_ch, dev_info->channels);
1101 }
1102 }
1103
1104 return max_ch;
1105}
1106
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001107int usb_get_max_bit_width(bool is_playback)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001108{
1109 struct listnode *node_i, *node_j;
1110 struct usb_device_config *dev_info;
1111 struct usb_card_config *card_info;
1112 unsigned int max_bw = 16;
1113 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1114 card_info = node_to_item(node_i, struct usb_card_config, list);
1115 if (usb_output_device(card_info->usb_device_type) && !is_playback)
1116 continue;
1117 else if (usb_input_device(card_info->usb_device_type) && is_playback)
1118 continue;
1119
1120 list_for_each(node_j, &card_info->usb_device_conf_list) {
1121 dev_info = node_to_item(node_j, struct usb_device_config, list);
1122 max_bw = _MAX(max_bw, dev_info->bit_width);
1123 }
1124 }
1125
1126 return max_bw;
1127}
1128
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001129int usb_get_sup_sample_rates(bool is_playback,
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001130 uint32_t *sample_rates,
1131 uint32_t sample_rate_size)
1132{
1133 int type = is_playback ? USB_PLAYBACK : USB_CAPTURE;
1134
1135 ALOGV("%s supported_sample_rates_mask 0x%x", __func__, supported_sample_rates_mask[type]);
1136 uint32_t bm = supported_sample_rates_mask[type];
1137 uint32_t tries = _MIN(sample_rate_size, (uint32_t)__builtin_popcount(bm));
1138
1139 int i = 0;
1140 while (tries--) {
1141 int idx = __builtin_ffs(bm) - 1;
1142 sample_rates[i++] = supported_sample_rates[idx];
1143 bm &= ~(1<<idx);
1144 }
1145
1146 return i;
1147}
1148
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001149bool usb_is_capture_supported()
Ashish Jain3e37a702016-11-25 12:27:15 +05301150{
1151 if (usbmod == NULL) {
1152 ALOGE("%s: USB device object is NULL", __func__);
1153 return false;
1154 }
1155 ALOGV("%s: capture_supported %d",__func__,usbmod->is_capture_supported);
1156 return usbmod->is_capture_supported;
1157}
1158
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001159bool usb_is_tunnel_supported()
Zhou Song6f862822017-11-06 17:27:57 +08001160{
1161 return true;
1162}
1163
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001164void usb_add_device(audio_devices_t device, int card)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001165{
1166 struct usb_card_config *usb_card_info;
1167 char check_debug_enable[PROPERTY_VALUE_MAX];
Kuirong Wang27152a12016-11-11 10:20:30 -08001168 struct listnode *node_i;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001169
Aalique Grahame22e49102018-12-18 14:23:57 -08001170 if ((property_get("vendor.audio.usb.enable.debug",
1171 check_debug_enable, NULL) > 0) ||
1172 (property_get("audio.usb.enable.debug",
1173 check_debug_enable, NULL) > 0)) {
1174 if (atoi(check_debug_enable))
1175 usb_audio_debug_enable = true;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001176 }
1177
1178 ALOGI_IF(usb_audio_debug_enable,
1179 "%s: parameters device(0x%x), card(%d)",
1180 __func__, device, card);
1181 if (usbmod == NULL) {
1182 ALOGE("%s: USB device object is NULL", __func__);
1183 goto exit;
1184 }
1185
1186 if (!(usb_valid_device(device)) || (card < 0)) {
1187 ALOGE("%s:device(0x%x), card(%d)",
1188 __func__, device, card);
1189 goto exit;
1190 }
1191
Kuirong Wang27152a12016-11-11 10:20:30 -08001192 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1193 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
1194 ALOGI_IF(usb_audio_debug_enable,
1195 "%s: list has capability for card_dev_type (0x%x), card_no(%d)",
1196 __func__, usb_card_info->usb_device_type, usb_card_info->usb_card);
1197 /* If we have cached the capability */
1198 if ((usb_card_info->usb_device_type == device) && (usb_card_info->usb_card == card)) {
1199 ALOGV("%s: capability for device(0x%x), card(%d) is cached, no need to update",
1200 __func__, device, card);
1201 goto exit;
1202 }
1203 }
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001204 usb_card_info = calloc(1, sizeof(struct usb_card_config));
1205 if (usb_card_info == NULL) {
1206 ALOGE("%s: error unable to allocate memory",
1207 __func__);
1208 goto exit;
1209 }
1210 list_init(&usb_card_info->usb_device_conf_list);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001211 if (usb_output_device(device)) {
Carter Hsu32a62362018-10-15 15:01:42 -07001212 if (usb_get_usbid(usb_card_info, card) < 0) {
1213 ALOGE("parse card %d usbid fail", card);
1214 }
1215
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001216 if (!usb_get_device_pb_config(usb_card_info, card)){
1217 usb_card_info->usb_card = card;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001218 usb_card_info->usb_device_type = device;
Kuirong Wang1cad7142016-05-24 15:21:56 -07001219 usb_get_sidetone_mixer(usb_card_info);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001220 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1221 goto exit;
1222 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001223 } else if (usb_input_device(device)) {
Carter Hsu32a62362018-10-15 15:01:42 -07001224 if (usb_get_usbid(usb_card_info, card) < 0) {
1225 ALOGE("parse card %d usbid fail", card);
1226 }
1227
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001228 if (!usb_get_device_cap_config(usb_card_info, card)) {
1229 usb_card_info->usb_card = card;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001230 usb_card_info->usb_device_type = device;
Ashish Jain3e37a702016-11-25 12:27:15 +05301231 usbmod->is_capture_supported = true;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001232 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1233 goto exit;
1234 }
1235 }
1236 /* free memory in error case */
1237 if (usb_card_info != NULL)
1238 free(usb_card_info);
1239exit:
1240 if (usb_audio_debug_enable)
1241 usb_print_active_device();
1242 return;
1243}
1244
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001245void usb_remove_device(audio_devices_t device, int card)
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001246{
1247 struct listnode *node_i, *temp_i;
1248 struct listnode *node_j, *temp_j;
1249 struct usb_device_config *dev_info;
1250 struct usb_card_config *card_info;
1251 unsigned int i;
1252
1253 ALOGV("%s: device(0x%x), card(%d)",
1254 __func__, device, card);
1255
1256 if (usbmod == NULL) {
1257 ALOGE("%s: USB device object is NULL", __func__);
1258 goto exit;
1259 }
1260
1261 if (!(usb_valid_device(device)) || (card < 0)) {
1262 ALOGE("%s: Invalid parameters device(0x%x), card(%d)",
1263 __func__, device, card);
1264 goto exit;
1265 }
1266 list_for_each_safe(node_i, temp_i, &usbmod->usb_card_conf_list) {
1267 card_info = node_to_item(node_i, struct usb_card_config, list);
1268 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1269 __func__, card_info->usb_device_type, card_info->usb_card);
1270 if ((device == card_info->usb_device_type) && (card == card_info->usb_card)){
1271 list_for_each_safe(node_j, temp_j, &card_info->usb_device_conf_list) {
1272 dev_info = node_to_item(node_j, struct usb_device_config, list);
1273 ALOGV("%s: bit-width(%d) channel(%d)",
1274 __func__, dev_info->bit_width, dev_info->channels);
1275 for (i = 0; i < dev_info->rate_size; i++)
1276 ALOGV("%s: rate %d", __func__, dev_info->rates[i]);
1277
1278 list_remove(node_j);
1279 free(node_to_item(node_j, struct usb_device_config, list));
1280 }
1281 list_remove(node_i);
1282 free(node_to_item(node_i, struct usb_card_config, list));
1283 }
1284 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001285 if (audio_is_usb_in_device(device)) { // XXX not sure if we need to check for card
1286 usbmod->is_capture_supported = false;
1287 supported_sample_rates_mask[USB_CAPTURE] = 0;
1288 } else
1289 supported_sample_rates_mask[USB_PLAYBACK] = 0;
1290
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001291exit:
1292 if (usb_audio_debug_enable)
1293 usb_print_active_device();
1294
1295 return;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001296}
1297
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001298bool usb_alive(int card) {
Kevin Rocardfce19002017-08-07 19:21:36 -07001299 char path[PATH_MAX] = {0};
1300 // snprintf should never fail
1301 (void) snprintf(path, sizeof(path), "/proc/asound/card%u/stream0", card);
1302 return access(path, F_OK) == 0;
1303}
1304
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001305unsigned long usb_find_service_interval(bool min,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001306 bool playback) {
1307 struct usb_card_config *card_info = NULL;
1308 struct usb_device_config *dev_info = NULL;
1309 struct listnode *node_i = NULL;
1310 struct listnode *node_j = NULL;
Aalique Grahame22e49102018-12-18 14:23:57 -08001311 unsigned long interval_us = min ? ULONG_MAX : 0;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001312 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1313 card_info = node_to_item(node_i, struct usb_card_config, list);
1314 list_for_each(node_j, &card_info->usb_device_conf_list) {
1315 dev_info = node_to_item(node_j, struct usb_device_config, list);
1316 bool match = (playback && (dev_info->type == USB_PLAYBACK)) ||
1317 (!playback && (dev_info->type == USB_CAPTURE));
1318 if (match) {
1319 interval_us = min ?
1320 _MIN(interval_us, dev_info->service_interval_us) :
1321 _MAX(interval_us, dev_info->service_interval_us);
1322 }
1323 }
1324 break;
1325 }
1326 return interval_us;
1327}
1328
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001329int usb_altset_for_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001330 unsigned long service_interval,
1331 uint32_t *bit_width,
1332 uint32_t *sample_rate,
1333 uint32_t *channels)
1334{
1335 struct usb_card_config *card_info = NULL;
Aalique Grahame22e49102018-12-18 14:23:57 -08001336 struct usb_device_config *dev_info = NULL;
1337 struct listnode *node_i = NULL;
1338 struct listnode *node_j = NULL;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001339 uint32_t bw = 0;
1340 uint32_t ch = 0;
1341 uint32_t sr = 0;
1342
1343 if (service_interval == 0)
1344 return 0;
1345 /* not a valid service interval to search for */
1346
1347#define FIND_BEST_MATCH(local_var, field, cond) \
1348 list_for_each(node_i, &usbmod->usb_card_conf_list) { \
1349 /* Currently only apply the first playback sound card configuration */ \
1350 card_info = node_to_item(node_i, struct usb_card_config, list); \
1351 list_for_each(node_j, &card_info->usb_device_conf_list) { \
1352 dev_info = node_to_item(node_j, struct usb_device_config, list); \
1353 bool match = (playback && (dev_info->type == USB_PLAYBACK)) || \
1354 (!playback && (dev_info->type == USB_CAPTURE)); \
1355 if (match && (cond)) { \
Garmond Leung003d9ea2018-08-14 17:05:08 -07001356 if (dev_info->field == *field) { \
1357 local_var = dev_info->field; \
1358 break; \
1359 } \
Garmond Leung5fd0b552018-04-17 11:56:12 -07001360 local_var = _MAX(local_var, dev_info->field); \
1361 } \
1362 } \
1363 break; \
1364 }
1365
1366 FIND_BEST_MATCH(bw, bit_width, dev_info->service_interval_us == service_interval);
1367 FIND_BEST_MATCH(ch, channels, \
1368 dev_info->service_interval_us == service_interval && \
1369 dev_info->bit_width == bw);
Garmond Leung003d9ea2018-08-14 17:05:08 -07001370 sr = *sample_rate;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001371 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1372 /* Currently only apply the first playback sound card configuration */
1373 card_info = node_to_item(node_i, struct usb_card_config, list);
1374 if ((playback && usb_output_device(card_info->usb_device_type)) ||
1375 (!playback && usb_input_device(card_info->usb_device_type))) {
1376 usb_get_best_match_for_sample_rate(&card_info->usb_device_conf_list,
1377 bw, ch, sr, &sr,
1378 service_interval,
1379 true);
1380 }
1381 break;
1382 }
1383
1384#define SET_OR_RETURN_ON_ERROR(arg, local_var, cond) \
1385 if (local_var != (cond)) arg = local_var; else return -1;
1386
1387 SET_OR_RETURN_ON_ERROR(*bit_width, bw, 0);
1388 SET_OR_RETURN_ON_ERROR(*sample_rate, sr, 0);
1389 SET_OR_RETURN_ON_ERROR(*channels, ch, 0);
1390 return 0;
1391#undef FIND_BEST_MATCH
1392#undef SET_OR_RETURN_ON_ERROR
1393}
1394
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001395int usb_get_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001396 unsigned long *service_interval)
1397{
1398 const char *ctl_name = "USB_AUDIO_RX service_interval";
1399 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
1400 ctl_name);
1401
1402 if (!playback) {
1403 ALOGE("%s not valid for capture", __func__);
1404 return -1;
1405 }
1406
1407 if (!ctl) {
1408 ALOGV("%s: could not get mixer %s", __func__, ctl_name);
1409 return -1;
1410 }
1411
1412 *service_interval = mixer_ctl_get_value(ctl, 0);
1413 return 0;
1414}
1415
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001416int usb_set_service_interval(bool playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001417 unsigned long service_interval,
1418 bool *reconfig)
1419{
1420 *reconfig = false;
1421 unsigned long current_service_interval = 0;
1422 const char *ctl_name = "USB_AUDIO_RX service_interval";
1423 struct mixer_ctl *ctl = mixer_get_ctl_by_name(usbmod->adev->mixer,
1424 ctl_name);
1425
1426 if (!playback) {
1427 ALOGE("%s not valid for capture", __func__);
1428 return -1;
1429 }
1430
1431 if (!ctl) {
1432 ALOGV("%s: could not get mixer %s", __func__, ctl_name);
1433 return -1;
1434 }
1435
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001436 if (usb_get_service_interval(playback,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001437 &current_service_interval) != 0) {
1438 ALOGE("%s Unable to get current service interval", __func__);
1439 return -1;
1440 }
1441
1442 if (current_service_interval != service_interval) {
1443 mixer_ctl_set_value(ctl, 0, service_interval);
1444 *reconfig = usbmod->usb_reconfig = true;
1445 }
1446 else
1447 *reconfig = usbmod->usb_reconfig = false;
1448 return 0;
1449}
1450
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001451int usb_check_and_set_svc_int(struct audio_usecase *uc_info,
Garmond Leung5fd0b552018-04-17 11:56:12 -07001452 bool starting_output_stream)
1453{
1454 struct listnode *node = NULL;
1455 struct audio_usecase *usecase = uc_info;
1456 bool reconfig = false;
1457 bool burst_mode = true;
1458 unsigned long service_interval = 0;
1459 struct audio_device *adev = usbmod->adev;
1460
1461 ALOGV("%s: enter:", __func__);
1462
1463 if ((starting_output_stream == true &&
1464 ((uc_info->id == USECASE_AUDIO_PLAYBACK_MMAP) ||
1465 (uc_info->id == USECASE_AUDIO_PLAYBACK_ULL))) ||
1466 (voice_is_call_state_active(usbmod->adev))) {
1467 burst_mode = false;
1468 } else {
1469 /* set if the valid usecase do not already exist */
1470 list_for_each(node, &adev->usecase_list) {
1471 usecase = node_to_item(node, struct audio_usecase, list);
1472 if (usecase->type == PCM_PLAYBACK &&
1473 audio_is_usb_out_device(usecase->devices & AUDIO_DEVICE_OUT_ALL_USB )) {
1474 switch (usecase->id) {
1475 case USECASE_AUDIO_PLAYBACK_MMAP:
1476 case USECASE_AUDIO_PLAYBACK_ULL:
1477 {
1478 if (uc_info != usecase) {
1479 //another ULL stream exists
1480 ALOGV("%s: another ULL Stream in active use-case list burst mode = false.", __func__);
1481 burst_mode = false;
1482 } else {
1483 ALOGV("%s:current ULL uc is the same as incoming uc_info \
1484 which means we are stopping the output stream, \
1485 we don't want to set burst mode to false", __func__);
1486 }
1487 break;
1488 }
1489 default:
1490 break;
1491 }
1492 }
1493 }
1494 }
1495
1496 ALOGV("%s: burst mode(%d).", __func__,burst_mode);
1497
1498 service_interval =
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001499 usb_find_service_interval(!burst_mode, true /*playback*/);
Garmond Leung5fd0b552018-04-17 11:56:12 -07001500
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001501 usb_set_service_interval(true /*playback*/,
Yunfei Zhang7728dce2018-10-31 10:20:52 +08001502 service_interval,
1503 &reconfig);
Garmond Leung5fd0b552018-04-17 11:56:12 -07001504
1505 /* no change or not supported or no active usecases */
1506 if (reconfig)
1507 return -1;
1508 return 0;
1509}
1510
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001511bool usb_is_reconfig_req()
Garmond Leung5fd0b552018-04-17 11:56:12 -07001512{
1513 return usbmod->usb_reconfig;
1514}
1515
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001516void usb_set_reconfig(bool is_required)
Garmond Leung5fd0b552018-04-17 11:56:12 -07001517{
1518 usbmod->usb_reconfig = is_required;
1519}
1520
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001521bool usb_connected(struct str_parms *parms) {
Sharad Sangled0a50b22018-04-05 23:28:32 +05301522 int card = -1;
1523 struct listnode *node_i = NULL;
1524 struct usb_card_config *usb_card_info = NULL;
1525 bool usb_connected = false;
1526
Weiyin Jiang63831272018-06-28 11:41:01 +08001527 if ((parms != NULL) && str_parms_get_int(parms, "card", &card) >= 0) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001528 usb_connected = usb_alive(card);
Sharad Sangled0a50b22018-04-05 23:28:32 +05301529 } else {
1530 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1531 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001532 if (usb_alive(usb_card_info->usb_card)) {
Sharad Sangled0a50b22018-04-05 23:28:32 +05301533 usb_connected = true;
1534 break;
1535 }
1536 }
1537 }
1538 return usb_connected;
1539}
1540
Carter Hsu32a62362018-10-15 15:01:42 -07001541char *usb_usbid()
1542{
1543 struct usb_card_config *card_info;
1544
1545 if (usbmod == NULL)
1546 return NULL;
1547
1548 if (list_empty(&usbmod->usb_card_conf_list))
1549 return NULL;
1550
1551 card_info = node_to_item(list_head(&usbmod->usb_card_conf_list),\
1552 struct usb_card_config, list);
1553
1554 return strdup(card_info->usbid);
1555}
1556
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001557void usb_init(void *adev)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001558{
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001559 if (usbmod == NULL) {
1560 usbmod = calloc(1, sizeof(struct usb_module));
1561 if (usbmod == NULL) {
1562 ALOGE("%s: error unable to allocate memory", __func__);
1563 goto exit;
1564 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001565 } else {
1566 memset(usbmod, 0, sizeof(*usbmod));
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001567 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001568
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001569 list_init(&usbmod->usb_card_conf_list);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001570 usbmod->adev = (struct audio_device*)adev;
Kuirong Wang1cad7142016-05-24 15:21:56 -07001571 usbmod->sidetone_gain = usb_sidetone_gain;
Ashish Jain3e37a702016-11-25 12:27:15 +05301572 usbmod->is_capture_supported = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07001573 usbmod->usb_reconfig = false;
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001574exit:
1575 return;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001576}
1577
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001578void usb_deinit(void)
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001579{
1580 if (NULL != usbmod){
1581 free(usbmod);
1582 usbmod = NULL;
1583 }
1584}