blob: 86b75d8ae6bf873d0167ad456521ae1a2aa11268 [file] [log] [blame]
David Linee3fe402017-03-13 10:00:42 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_usb"
18
19#include <errno.h>
20#include <pthread.h>
21#include <stdlib.h>
22#include <cutils/log.h>
23#include <cutils/str_parms.h>
24#include <sys/ioctl.h>
Jiyong Park6431fe62017-06-29 15:15:58 +090025#include <unistd.h>
David Linee3fe402017-03-13 10:00:42 -070026#include <fcntl.h>
27#include <sys/stat.h>
28#include <system/audio.h>
29#include <tinyalsa/asoundlib.h>
30#include <audio_hw.h>
31#include <cutils/properties.h>
32#include <ctype.h>
33#include <math.h>
34
35#ifdef USB_TUNNEL_ENABLED
36#define USB_BUFF_SIZE 2048
37#define CHANNEL_NUMBER_STR "Channels: "
38#define PLAYBACK_PROFILE_STR "Playback:"
39#define CAPTURE_PROFILE_STR "Capture:"
40#define USB_SIDETONE_GAIN_STR "usb_sidetone_gain"
41#define ABS_SUB(A, B) (((A) > (B)) ? ((A) - (B)):((B) - (A)))
42#define SAMPLE_RATE_8000 8000
43#define SAMPLE_RATE_11025 11025
44/* TODO: dynamically populate supported sample rates */
45static uint32_t supported_sample_rates[] =
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080046 {192000, 176400, 96000, 88200, 64000, 48000, 44100};
47static uint32_t supported_sample_rates_mask[2];
48static const uint32_t MAX_SAMPLE_RATE_SIZE =
49 (sizeof(supported_sample_rates)/sizeof(supported_sample_rates[0]));
David Linee3fe402017-03-13 10:00:42 -070050
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080051// assert on sizeof bm v/s size of rates if needed
David Linee3fe402017-03-13 10:00:42 -070052
53enum usb_usecase_type{
54 USB_PLAYBACK = 0,
55 USB_CAPTURE,
56};
57
58enum {
59 USB_SIDETONE_ENABLE_INDEX = 0,
60 USB_SIDETONE_VOLUME_INDEX,
61 USB_SIDETONE_MAX_INDEX,
62};
63
64struct usb_device_config {
65 struct listnode list;
66 unsigned int bit_width;
67 unsigned int channel_count;
68 unsigned int rate_size;
69 unsigned int rates[MAX_SAMPLE_RATE_SIZE];
70};
71
72struct usb_card_config {
73 struct listnode list;
74 audio_devices_t usb_device_type;
75 int usb_card;
76 struct listnode usb_device_conf_list;
77 struct mixer *usb_snd_mixer;
78 int usb_sidetone_index[USB_SIDETONE_MAX_INDEX];
79 int usb_sidetone_vol_min;
80 int usb_sidetone_vol_max;
81};
82
83struct usb_module {
84 struct listnode usb_card_conf_list;
85 struct audio_device *adev;
86 int sidetone_gain;
87 bool is_capture_supported;
88};
89
90static struct usb_module *usbmod = NULL;
91static bool usb_audio_debug_enable = false;
92static int usb_sidetone_gain = 0;
93
94static const char * const usb_sidetone_enable_str[] = {
95 "Sidetone Playback Switch",
96 "Mic Playback Switch",
97};
98
99static const char * const usb_sidetone_volume_str[] = {
100 "Sidetone Playback Volume",
101 "Mic Playback Volume",
102};
103
104static void usb_mixer_print_enum(struct mixer_ctl *ctl)
105{
106 unsigned int num_enums;
107 unsigned int i;
108 const char *string;
109
110 num_enums = mixer_ctl_get_num_enums(ctl);
111
112 for (i = 0; i < num_enums; i++) {
113 string = mixer_ctl_get_enum_string(ctl, i);
114 ALOGI("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "", string);
115 }
116}
117
118static void usb_soundcard_detail_control(struct mixer *mixer, const char *control)
119{
120 struct mixer_ctl *ctl;
121 enum mixer_ctl_type type;
122 unsigned int num_values;
123 unsigned int i;
124 int min, max;
125
126 if (isdigit(control[0]))
127 ctl = mixer_get_ctl(mixer, atoi(control));
128 else
129 ctl = mixer_get_ctl_by_name(mixer, control);
130
131 if (!ctl) {
132 fprintf(stderr, "Invalid mixer control\n");
133 return;
134 }
135
136 type = mixer_ctl_get_type(ctl);
137 num_values = mixer_ctl_get_num_values(ctl);
138
139 ALOGV("%s:", mixer_ctl_get_name(ctl));
140
141 for (i = 0; i < num_values; i++) {
142 switch (type) {
143 case MIXER_CTL_TYPE_INT:
144 ALOGV(" %d", mixer_ctl_get_value(ctl, i));
145 break;
146 case MIXER_CTL_TYPE_BOOL:
147 ALOGV(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
148 break;
149 case MIXER_CTL_TYPE_ENUM:
150 usb_mixer_print_enum(ctl);
151 break;
152 case MIXER_CTL_TYPE_BYTE:
153 ALOGV(" 0x%02x", mixer_ctl_get_value(ctl, i));
154 break;
155 default:
156 ALOGV(" unknown");
157 break;
158 }
159 }
160
161 if (type == MIXER_CTL_TYPE_INT) {
162 min = mixer_ctl_get_range_min(ctl);
163 max = mixer_ctl_get_range_max(ctl);
164 ALOGV(" (range %d->%d)", min, max);
165 }
166}
167
168static void usb_soundcard_list_controls(struct mixer *mixer)
169{
170 struct mixer_ctl *ctl;
171 const char *name, *type;
172 unsigned int num_ctls, num_values;
173 unsigned int i;
174
175 num_ctls = mixer_get_num_ctls(mixer);
176
177 ALOGV("Number of controls: %d\n", num_ctls);
178
179 ALOGV("ctl\ttype\tnum\t%-40s value\n", "name");
180 for (i = 0; i < num_ctls; i++) {
181 ctl = mixer_get_ctl(mixer, i);
182 if (ctl != NULL) {
183 name = mixer_ctl_get_name(ctl);
184 type = mixer_ctl_get_type_string(ctl);
185 num_values = mixer_ctl_get_num_values(ctl);
186 ALOGV("%d\t%s\t%d\t%-40s", i, type, num_values, name);
187 if (name != NULL)
188 usb_soundcard_detail_control(mixer, name);
189 }
190 }
191}
192
193static int usb_set_dev_id_mixer_ctl(unsigned int usb_usecase_type, int card,
194 char *dev_mixer_ctl_name)
195{
196 struct mixer_ctl *ctl;
197 unsigned int dev_token;
198 const unsigned int pcm_device_number = 0;
199
200 /*
201 * usb_dev_token_id is 32 bit number and is defined as below:
202 * usb_sound_card_idx(31:16) | usb PCM device ID(15:8) | usb_usecase_type(7:0)
203 */
204 dev_token = (card << 16 ) |
205 (pcm_device_number << 8) | (usb_usecase_type & 0xFF);
206
207 ctl = mixer_get_ctl_by_name(usbmod->adev->mixer, dev_mixer_ctl_name);
208 if (!ctl) {
209 ALOGE("%s: Could not get ctl for mixer cmd - %s",
210 __func__, dev_mixer_ctl_name);
211 return -EINVAL;
212 }
213 mixer_ctl_set_value(ctl, 0, dev_token);
214
215 return 0;
216}
217
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800218static int usb_get_sample_rates(int type, char *rates_str,
David Linee3fe402017-03-13 10:00:42 -0700219 struct usb_device_config *config)
220{
221 uint32_t i;
222 char *next_sr_string, *temp_ptr;
223 uint32_t sr, min_sr, max_sr, sr_size = 0;
224
225 /* Sample rate string can be in any of the folloing two bit_widthes:
226 * Rates: 8000 - 48000 (continuous)
227 * Rates: 8000, 44100, 48000
228 * Support both the bit_widths
229 */
230 ALOGV("%s: rates_str %s", __func__, rates_str);
231 next_sr_string = strtok_r(rates_str, "Rates: ", &temp_ptr);
232 if (next_sr_string == NULL) {
233 ALOGE("%s: could not find min rates string", __func__);
234 return -EINVAL;
235 }
236 if (strstr(rates_str, "continuous") != NULL) {
237 min_sr = (uint32_t)atoi(next_sr_string);
238 next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
239 if (next_sr_string == NULL) {
240 ALOGE("%s: could not find max rates string", __func__);
241 return -EINVAL;
242 }
243 max_sr = (uint32_t)atoi(next_sr_string);
244
245 for (i = 0; i < MAX_SAMPLE_RATE_SIZE; i++) {
246 if (supported_sample_rates[i] >= min_sr &&
247 supported_sample_rates[i] <= max_sr) {
248 config->rates[sr_size++] = supported_sample_rates[i];
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800249 supported_sample_rates_mask[type] |= (1<<i);
David Linee3fe402017-03-13 10:00:42 -0700250 ALOGI_IF(usb_audio_debug_enable,
251 "%s: continuous sample rate supported_sample_rates[%d] %d",
252 __func__, i, supported_sample_rates[i]);
253 }
254 }
255 } else {
256 do {
257 sr = (uint32_t)atoi(next_sr_string);
258 for (i = 0; i < MAX_SAMPLE_RATE_SIZE; i++) {
259 if (supported_sample_rates[i] == sr) {
260 ALOGI_IF(usb_audio_debug_enable,
261 "%s: sr %d, supported_sample_rates[%d] %d -> matches!!",
262 __func__, sr, i, supported_sample_rates[i]);
263 config->rates[sr_size++] = supported_sample_rates[i];
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800264 supported_sample_rates_mask[type] |= (1<<i);
David Linee3fe402017-03-13 10:00:42 -0700265 }
266 }
267 next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
268 } while (next_sr_string != NULL);
269 }
270 config->rate_size = sr_size;
271 return 0;
272}
273
274static int usb_get_capability(int type,
275 struct usb_card_config *usb_card_info,
276 int card)
277{
278 int32_t size = 0;
279 int32_t fd=-1;
280 int32_t channels_no;
281 char *str_start = NULL;
282 char *str_end = NULL;
283 char *channel_start = NULL;
284 char *bit_width_start = NULL;
285 char *rates_str_start = NULL;
286 char *target = NULL;
287 char *read_buf = NULL;
288 char *rates_str = NULL;
289 char path[128];
290 int ret = 0;
291 char *bit_width_str = NULL;
292 struct usb_device_config * usb_device_info;
293 bool check = false;
Haynes Mathew George569b7482017-05-08 14:44:27 -0700294 int tries=5;
David Linee3fe402017-03-13 10:00:42 -0700295
296 memset(path, 0, sizeof(path));
297 ALOGV("%s: for %s", __func__, (type == USB_PLAYBACK) ?
298 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR);
299
300 /* TODO: convert the below to using alsa_utils */
301 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/stream0",
302 card);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700303 if (ret < 0) {
David Linee3fe402017-03-13 10:00:42 -0700304 ALOGE("%s: failed on snprintf (%d) to path %s\n",
305 __func__, ret, path);
306 goto done;
307 }
308
Haynes Mathew George569b7482017-05-08 14:44:27 -0700309 while (tries--) {
310 if (access(path, F_OK) < 0) {
311 ALOGW("stream %s doesn't exist retrying\n", path);
312 sleep(1);
313 continue;
314 }
315 }
316
David Linee3fe402017-03-13 10:00:42 -0700317 fd = open(path, O_RDONLY);
318 if (fd <0) {
319 ALOGE("%s: error failed to open config file %s error: %d\n",
320 __func__, path, errno);
321 ret = -EINVAL;
322 goto done;
323 }
324
325 read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
326
327 if (!read_buf) {
328 ALOGE("Failed to create read_buf");
329 ret = -ENOMEM;
330 goto done;
331 }
332
333 if(read(fd, read_buf, USB_BUFF_SIZE) < 0) {
334 ALOGE("file read error\n");
335 goto done;
336 }
337 str_start = strstr(read_buf, ((type == USB_PLAYBACK) ?
338 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
339 if (str_start == NULL) {
340 ALOGE("%s: error %s section not found in usb config file",
341 __func__, ((type == USB_PLAYBACK) ?
342 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
343 ret = -EINVAL;
344 goto done;
345 }
346 str_end = strstr(read_buf, ((type == USB_PLAYBACK) ?
347 CAPTURE_PROFILE_STR : PLAYBACK_PROFILE_STR));
348 if (str_end > str_start)
349 check = true;
350
351 ALOGV("%s: usb_config = %s, check %d\n", __func__, str_start, check);
352
353 while (str_start != NULL) {
354 str_start = strstr(str_start, "Altset");
355 if ((str_start == NULL) || (check && (str_start >= str_end))) {
356 ALOGV("%s: done parsing %s\n", __func__, str_start);
357 break;
358 }
359 ALOGV("%s: remaining string %s\n", __func__, str_start);
360 str_start += sizeof("Altset");
361 usb_device_info = calloc(1, sizeof(struct usb_device_config));
362 if (usb_device_info == NULL) {
363 ALOGE("%s: error unable to allocate memory",
364 __func__);
365 ret = -ENOMEM;
366 break;
367 }
368 /* Bit bit_width parsing */
369 bit_width_start = strstr(str_start, "Format: ");
370 if (bit_width_start == NULL) {
371 ALOGI("%s: Could not find bit_width string", __func__);
372 free(usb_device_info);
373 continue;
374 }
375 target = strchr(bit_width_start, '\n');
376 if (target == NULL) {
377 ALOGI("%s:end of line not found", __func__);
378 free(usb_device_info);
379 continue;
380 }
381 size = target - bit_width_start;
382 if ((bit_width_str = (char *)malloc(size + 1)) == NULL) {
383 ALOGE("%s: unable to allocate memory to hold bit width strings",
384 __func__);
385 ret = -EINVAL;
386 free(usb_device_info);
387 break;
388 }
389 memcpy(bit_width_str, bit_width_start, size);
390 bit_width_str[size] = '\0';
391 if (strstr(bit_width_str, "S16_LE"))
392 usb_device_info->bit_width = 16;
393 else if (strstr(bit_width_str, "S24_LE"))
394 usb_device_info->bit_width = 24;
395 else if (strstr(bit_width_str, "S24_3LE"))
396 usb_device_info->bit_width = 24;
397 else if (strstr(bit_width_str, "S32_LE"))
398 usb_device_info->bit_width = 32;
399
400 if (bit_width_str)
401 free(bit_width_str);
402
403 /* channels parsing */
404 channel_start = strstr(str_start, CHANNEL_NUMBER_STR);
405 if (channel_start == NULL) {
406 ALOGI("%s: could not find Channels string", __func__);
407 free(usb_device_info);
408 continue;
409 }
410 channels_no = atoi(channel_start + strlen(CHANNEL_NUMBER_STR));
411 usb_device_info->channel_count = channels_no;
412
413 /* Sample rates parsing */
414 rates_str_start = strstr(str_start, "Rates: ");
415 if (rates_str_start == NULL) {
416 ALOGI("%s: cant find rates string", __func__);
417 free(usb_device_info);
418 continue;
419 }
420 target = strchr(rates_str_start, '\n');
421 if (target == NULL) {
422 ALOGI("%s: end of line not found", __func__);
423 free(usb_device_info);
424 continue;
425 }
426 size = target - rates_str_start;
427 if ((rates_str = (char *)malloc(size + 1)) == NULL) {
428 ALOGE("%s: unable to allocate memory to hold sample rate strings",
429 __func__);
430 ret = -EINVAL;
431 free(usb_device_info);
432 break;
433 }
434 memcpy(rates_str, rates_str_start, size);
435 rates_str[size] = '\0';
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800436 ret = usb_get_sample_rates(type, rates_str, usb_device_info);
David Linee3fe402017-03-13 10:00:42 -0700437 if (rates_str)
438 free(rates_str);
439 if (ret < 0) {
440 ALOGE("%s: error unable to get sample rate values",
441 __func__);
442 free(usb_device_info);
443 continue;
444 }
445 /* Add to list if every field is valid */
446 list_add_tail(&usb_card_info->usb_device_conf_list,
447 &usb_device_info->list);
448 }
449
450done:
451 if (fd >= 0) close(fd);
452 if (read_buf) free(read_buf);
453 return ret;
454}
455
456static int usb_get_device_playback_config(struct usb_card_config *usb_card_info,
457 int card)
458{
459 int ret;
460
461 /* get capabilities */
462 if ((ret = usb_get_capability(USB_PLAYBACK, usb_card_info, card))) {
463 ALOGE("%s: could not get Playback capabilities from usb device",
464 __func__);
465 goto exit;
466 }
467 usb_set_dev_id_mixer_ctl(USB_PLAYBACK, card, "USB_AUDIO_RX dev_token");
468
469exit:
470
471 return ret;
472}
473
474static int usb_get_device_capture_config(struct usb_card_config *usb_card_info,
475 int card)
476{
477 int ret;
478
479 /* get capabilities */
480 if ((ret = usb_get_capability(USB_CAPTURE, usb_card_info, card))) {
481 ALOGE("%s: could not get Playback capabilities from usb device",
482 __func__);
483 goto exit;
484 }
485 usb_set_dev_id_mixer_ctl(USB_CAPTURE, card, "USB_AUDIO_TX dev_token");
486
487exit:
488 return ret;
489}
490
491static void usb_get_sidetone_mixer(struct usb_card_config *usb_card_info)
492{
493 struct mixer_ctl *ctl;
494 unsigned int index;
495
496 for (index = 0; index < USB_SIDETONE_MAX_INDEX; index++)
497 usb_card_info->usb_sidetone_index[index] = -1;
498
499 usb_card_info->usb_snd_mixer = mixer_open(usb_card_info->usb_card);
500 for (index = 0;
501 index < sizeof(usb_sidetone_enable_str)/sizeof(usb_sidetone_enable_str[0]);
502 index++) {
503 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
504 usb_sidetone_enable_str[index]);
505 if (ctl) {
506 usb_card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX] = index;
507 /* Disable device sidetone by default */
508 mixer_ctl_set_value(ctl, 0, false);
vivek mehta90933872017-06-15 18:04:39 -0700509 ALOGV("%s:: sidetone mixer Control found(%s) ... disabling by default",
510 __func__, usb_sidetone_enable_str[index]);
David Linee3fe402017-03-13 10:00:42 -0700511 break;
512 }
513 }
vivek mehta90933872017-06-15 18:04:39 -0700514#ifdef USB_SIDETONE_VOLUME
David Linee3fe402017-03-13 10:00:42 -0700515 for (index = 0;
516 index < sizeof(usb_sidetone_volume_str)/sizeof(usb_sidetone_volume_str[0]);
517 index++) {
518 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
519 usb_sidetone_volume_str[index]);
520 if (ctl) {
521 usb_card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX] = index;
522 usb_card_info->usb_sidetone_vol_min = mixer_ctl_get_range_min(ctl);
523 usb_card_info->usb_sidetone_vol_max = mixer_ctl_get_range_max(ctl);
524 break;
525 }
526 }
vivek mehta90933872017-06-15 18:04:39 -0700527#endif // USB_SIDETONE_VOLUME
David Linee3fe402017-03-13 10:00:42 -0700528 if ((usb_card_info->usb_snd_mixer != NULL) && (usb_audio_debug_enable))
529 usb_soundcard_list_controls(usb_card_info->usb_snd_mixer);
530
531 return;
532}
533
Haynes Mathew George569b7482017-05-08 14:44:27 -0700534static inline bool usb_output_device(audio_devices_t device) {
535 // ignore accessory for now
536 if (device == AUDIO_DEVICE_OUT_USB_ACCESSORY) {
537 return false;
538 }
539 return audio_is_usb_out_device(device);
540}
541
542static inline bool usb_input_device(audio_devices_t device) {
543 // ignore accessory for now
544 if (device == AUDIO_DEVICE_IN_USB_ACCESSORY) {
545 return false;
546 }
547 return audio_is_usb_in_device(device);
548}
549
David Linee3fe402017-03-13 10:00:42 -0700550static bool usb_valid_device(audio_devices_t device)
551{
Haynes Mathew George569b7482017-05-08 14:44:27 -0700552 return usb_output_device(device) ||
553 usb_input_device(device);
David Linee3fe402017-03-13 10:00:42 -0700554}
555
556static void usb_print_active_device(void){
557 struct listnode *node_i, *node_j;
558 struct usb_device_config *dev_info;
559 struct usb_card_config *card_info;
560 unsigned int i;
561
562 ALOGI("%s", __func__);
563 list_for_each(node_i, &usbmod->usb_card_conf_list) {
564 card_info = node_to_item(node_i, struct usb_card_config, list);
565 ALOGI("%s: card_dev_type (0x%x), card_no(%d)",
566 __func__, card_info->usb_device_type, card_info->usb_card);
567 list_for_each(node_j, &card_info->usb_device_conf_list) {
568 dev_info = node_to_item(node_j, struct usb_device_config, list);
569 ALOGI("%s: bit-width(%d) channel(%d)",
570 __func__, dev_info->bit_width, dev_info->channel_count);
571 for (i = 0; i < dev_info->rate_size; i++)
572 ALOGI("%s: rate %d", __func__, dev_info->rates[i]);
573 }
574 }
575}
576
577static bool usb_get_best_match_for_bit_width(
578 struct listnode *dev_list,
579 unsigned int stream_bit_width,
580 unsigned int *bit_width)
581{
582 struct listnode *node_i;
583 struct usb_device_config *dev_info;
584 unsigned int candidate = 0;
585
586 list_for_each(node_i, dev_list) {
587 dev_info = node_to_item(node_i, struct usb_device_config, list);
588 ALOGI_IF(usb_audio_debug_enable,
589 "%s: USB bw(%d), stream bw(%d), candidate(%d)",
590 __func__, dev_info->bit_width,
591 stream_bit_width, candidate);
592 if (dev_info->bit_width == stream_bit_width) {
593 *bit_width = dev_info->bit_width;
594 ALOGV("%s: Found match bit-width (%d)",
595 __func__, dev_info->bit_width);
596 goto exit;
597 } else if (candidate == 0) {
598 candidate = dev_info->bit_width;
599 }
600 /*
601 * If stream bit is 24, USB supports both 16 bit and 32 bit, then
602 * higher bit width 32 is picked up instead of 16-bit
603 */
604 else if (ABS_SUB(stream_bit_width, dev_info->bit_width) <
605 ABS_SUB(stream_bit_width, candidate)) {
606 candidate = dev_info->bit_width;
607 }
608 else if ((ABS_SUB(stream_bit_width, dev_info->bit_width) ==
609 ABS_SUB(stream_bit_width, candidate)) &&
610 (dev_info->bit_width > candidate)) {
611 candidate = dev_info->bit_width;
612 }
613 }
614 ALOGV("%s: No match found, use the best candidate bw(%d)",
615 __func__, candidate);
616 *bit_width = candidate;
617exit:
618 return true;
619}
620
621static bool usb_get_best_match_for_channels(
622 struct listnode *dev_list,
623 unsigned int bit_width,
624 unsigned int stream_ch,
625 unsigned int *channel_count)
626{
627 struct listnode *node_i;
628 struct usb_device_config *dev_info;
629 unsigned int candidate = 0;
630
631 list_for_each(node_i, dev_list) {
632 dev_info = node_to_item(node_i, struct usb_device_config, list);
633 ALOGI_IF(usb_audio_debug_enable,
634 "%s: USB ch(%d)bw(%d), stream ch(%d)bw(%d), candidate(%d)",
635 __func__, dev_info->channel_count, dev_info->bit_width,
636 stream_ch, bit_width, candidate);
637 if (dev_info->bit_width != bit_width)
638 continue;
639 if (dev_info->channel_count== stream_ch) {
640 *channel_count = dev_info->channel_count;
641 ALOGV("%s: Found match channels (%d)",
642 __func__, dev_info->channel_count);
643 goto exit;
644 } else if (candidate == 0)
645 candidate = dev_info->channel_count;
646 /*
647 * If stream channel is 4, USB supports both 3 and 5, then
648 * higher channel 5 is picked up instead of 3
649 */
650 else if (ABS_SUB(stream_ch, dev_info->channel_count) <
651 ABS_SUB(stream_ch, candidate)) {
652 candidate = dev_info->channel_count;
653 } else if ((ABS_SUB(stream_ch, dev_info->channel_count) ==
654 ABS_SUB(stream_ch, candidate)) &&
655 (dev_info->channel_count > candidate)) {
656 candidate = dev_info->channel_count;
657 }
658 }
659 ALOGV("%s: No match found, use the best candidate ch(%d)",
660 __func__, candidate);
661 *channel_count = candidate;
662exit:
663 return true;
664
665}
666
667static bool usb_sample_rate_multiple(
668 unsigned int stream_sample_rate,
669 unsigned int base)
670{
671 return (((stream_sample_rate / base) * base) == stream_sample_rate);
672}
673
674static bool usb_find_sample_rate_candidate(unsigned int base,
675 unsigned stream_rate,
676 unsigned int usb_rate,
677 unsigned int cur_candidate,
678 unsigned int *update_candidate)
679{
680 /* For sample rate, we should consider fracational sample rate as high priority.
681 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
682 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
683 */
684 if (!usb_sample_rate_multiple(cur_candidate, base) &&
685 usb_sample_rate_multiple(usb_rate, base)) {
686 *update_candidate = usb_rate;
687 } else if (usb_sample_rate_multiple(cur_candidate, base) &&
688 usb_sample_rate_multiple(usb_rate, base)) {
689 if (ABS_SUB(stream_rate, usb_rate) <
690 ABS_SUB(stream_rate, cur_candidate)) {
691 *update_candidate = usb_rate;
692 } else if ((ABS_SUB(stream_rate, usb_rate) ==
693 ABS_SUB(stream_rate, cur_candidate)) &&
694 (usb_rate > cur_candidate)) {
695 *update_candidate = usb_rate;
696 }
697 } else if (!usb_sample_rate_multiple(cur_candidate, base) &&
698 !usb_sample_rate_multiple(usb_rate, base)) {
699 if (ABS_SUB(stream_rate, usb_rate) <
700 ABS_SUB(stream_rate, cur_candidate)) {
701 *update_candidate = usb_rate;
702 } else if ((ABS_SUB(stream_rate, usb_rate) ==
703 ABS_SUB(stream_rate, cur_candidate)) &&
704 (usb_rate > cur_candidate)) {
705 *update_candidate = usb_rate;
706 }
707 }
708 return true;
709}
710
711static bool usb_get_best_match_for_sample_rate(
712 struct listnode *dev_list,
713 unsigned int bit_width,
714 unsigned int channel_count,
715 unsigned int stream_sample_rate,
716 unsigned int *sr)
717{
718 struct listnode *node_i;
719 struct usb_device_config *dev_info;
720 unsigned int candidate = 48000;
721 unsigned int base = SAMPLE_RATE_8000;
722 bool multiple_8k = usb_sample_rate_multiple(stream_sample_rate, base);
723 unsigned int i;
724
725 ALOGV("%s: stm ch(%d)bw(%d)sr(%d), stream sample multiple of 8kHz(%d)",
726 __func__, channel_count, bit_width, stream_sample_rate, multiple_8k);
727
728 list_for_each(node_i, dev_list) {
729 dev_info = node_to_item(node_i, struct usb_device_config, list);
730 ALOGI_IF(usb_audio_debug_enable,
731 "%s: USB ch(%d)bw(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
732 __func__, dev_info->channel_count, dev_info->bit_width,
733 channel_count, bit_width, stream_sample_rate, candidate);
734 if ((dev_info->bit_width != bit_width) || dev_info->channel_count != channel_count)
735 continue;
736
737 candidate = 0;
738 for (i = 0; i < dev_info->rate_size; i++) {
739 ALOGI_IF(usb_audio_debug_enable,
740 "%s: USB ch(%d)bw(%d)sr(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
741 __func__, dev_info->channel_count,
742 dev_info->bit_width, dev_info->rates[i],
743 channel_count, bit_width, stream_sample_rate, candidate);
744 if (stream_sample_rate == dev_info->rates[i]) {
745 *sr = dev_info->rates[i];
746 ALOGV("%s: Found match sample rate (%d)",
747 __func__, dev_info->rates[i]);
748 goto exit;
749 } else if (candidate == 0) {
750 candidate = dev_info->rates[i];
751 /*
752 * For sample rate, we should consider fracational sample rate as high priority.
753 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
754 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
755 */
756 } else if (multiple_8k) {
757 usb_find_sample_rate_candidate(SAMPLE_RATE_8000,
758 stream_sample_rate,
759 dev_info->rates[i],
760 candidate,
761 &candidate);
762 } else {
763 usb_find_sample_rate_candidate(SAMPLE_RATE_11025,
764 stream_sample_rate,
765 dev_info->rates[i],
766 candidate,
767 &candidate);
768 }
769 }
770 }
771 ALOGV("%s: No match found, use the best candidate sr(%d)",
772 __func__, candidate);
773 *sr = candidate;
774exit:
775 return true;
776}
777
778static bool usb_audio_backend_apply_policy(struct listnode *dev_list,
779 unsigned int *bit_width,
780 unsigned int *sample_rate,
781 unsigned int *channel_count)
782{
783 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) channels (%d)",
784 __func__, *bit_width, *sample_rate, *channel_count);
785 if (list_empty(dev_list)) {
786 *sample_rate = 48000;
787 *bit_width = 16;
788 *channel_count = 2;
789 ALOGE("%s: list is empty,fall back to default setting", __func__);
790 goto exit;
791 }
792 usb_get_best_match_for_bit_width(dev_list, *bit_width, bit_width);
793 usb_get_best_match_for_channels(dev_list,
794 *bit_width,
795 *channel_count,
796 channel_count);
797 usb_get_best_match_for_sample_rate(dev_list,
798 *bit_width,
799 *channel_count,
800 *sample_rate,
801 sample_rate);
802exit:
803 ALOGV("%s: Updated sample rate per profile: bit-width(%d) rate(%d) chs(%d)",
804 __func__, *bit_width, *sample_rate, *channel_count);
805 return true;
806}
807
808static int usb_get_sidetone_gain(struct usb_card_config *card_info)
809{
810 int gain = card_info->usb_sidetone_vol_min + usbmod->sidetone_gain;
811 if (gain > card_info->usb_sidetone_vol_max)
812 gain = card_info->usb_sidetone_vol_max;
813 return gain;
814}
815
816void audio_extn_usb_set_sidetone_gain(struct str_parms *parms,
817 char *value, int len)
818{
819 int err;
820
821 err = str_parms_get_str(parms, USB_SIDETONE_GAIN_STR,
822 value, len);
823 if (err >= 0) {
824 usb_sidetone_gain = pow(10.0, (float)(atoi(value))/10.0);
825 ALOGV("%s: sidetone gain(%s) decimal %d",
826 __func__, value, usb_sidetone_gain);
827 str_parms_del(parms, USB_SIDETONE_GAIN_STR);
828 }
829 return;
830}
831
832int audio_extn_usb_enable_sidetone(int device, bool enable)
833{
834 int ret = -ENODEV;
835 struct listnode *node_i;
836 struct usb_card_config *card_info;
837 int i;
838 ALOGV("%s: card_dev_type (0x%x), sidetone enable(%d)",
839 __func__, device, enable);
840
841 list_for_each(node_i, &usbmod->usb_card_conf_list) {
842 card_info = node_to_item(node_i, struct usb_card_config, list);
843 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
844 __func__, card_info->usb_device_type, card_info->usb_card);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700845 if (usb_output_device(card_info->usb_device_type)) {
David Linee3fe402017-03-13 10:00:42 -0700846 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX]) != -1) {
847 struct mixer_ctl *ctl = mixer_get_ctl_by_name(
848 card_info->usb_snd_mixer,
849 usb_sidetone_enable_str[i]);
850 if (ctl)
851 mixer_ctl_set_value(ctl, 0, enable);
852 else
853 break;
854
vivek mehta90933872017-06-15 18:04:39 -0700855#ifdef USB_SIDETONE_VOLUME
David Linee3fe402017-03-13 10:00:42 -0700856 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX]) != -1) {
857 ctl = mixer_get_ctl_by_name(
858 card_info->usb_snd_mixer,
859 usb_sidetone_volume_str[i]);
860 if (ctl == NULL)
861 ALOGV("%s: sidetone gain mixer command is not found",
862 __func__);
863 else if (enable)
864 mixer_ctl_set_value(ctl, 0,
865 usb_get_sidetone_gain(card_info));
866 }
vivek mehta90933872017-06-15 18:04:39 -0700867#endif // USB_SIDETONE_VOLUME
David Linee3fe402017-03-13 10:00:42 -0700868 ret = 0;
869 break;
870 }
871 }
872 }
873 return ret;
874}
875
876bool audio_extn_usb_is_config_supported(unsigned int *bit_width,
877 unsigned int *sample_rate,
878 unsigned int *channel_count,
879 bool is_playback)
880{
881 struct listnode *node_i;
882 struct usb_card_config *card_info;
883
884 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) ch(%d) is_playback(%d)",
885 __func__, *bit_width, *sample_rate, *channel_count, is_playback);
886 list_for_each(node_i, &usbmod->usb_card_conf_list) {
887 card_info = node_to_item(node_i, struct usb_card_config, list);
888 ALOGI_IF(usb_audio_debug_enable,
889 "%s: card_dev_type (0x%x), card_no(%d)",
890 __func__, card_info->usb_device_type, card_info->usb_card);
891 /* Currently only apply the first playback sound card configuration */
Haynes Mathew George569b7482017-05-08 14:44:27 -0700892 if ((is_playback && usb_output_device(card_info->usb_device_type)) ||
893 (!is_playback && usb_input_device(card_info->usb_device_type))) {
David Linee3fe402017-03-13 10:00:42 -0700894 usb_audio_backend_apply_policy(&card_info->usb_device_conf_list,
895 bit_width,
896 sample_rate,
897 channel_count);
898 break;
899 }
900 }
901 ALOGV("%s: updated: bit-width(%d) sample_rate(%d) channels (%d)",
902 __func__, *bit_width, *sample_rate, *channel_count);
903
904 return true;
905}
906
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800907#define _MAX(x, y) (((x) >= (y)) ? (x) : (y))
908#define _MIN(x, y) (((x) <= (y)) ? (x) : (y))
909
Haynes Mathew George569b7482017-05-08 14:44:27 -0700910int audio_extn_usb_get_max_channels(bool is_playback)
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800911{
912 struct listnode *node_i, *node_j;
913 struct usb_device_config *dev_info;
914 struct usb_card_config *card_info;
915 unsigned int max_ch = 1;
916 list_for_each(node_i, &usbmod->usb_card_conf_list) {
917 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700918 if (usb_output_device(card_info->usb_device_type) && !is_playback)
919 continue;
920 else if (usb_input_device(card_info->usb_device_type) && is_playback)
921 continue;
922
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800923 list_for_each(node_j, &card_info->usb_device_conf_list) {
924 dev_info = node_to_item(node_j, struct usb_device_config, list);
925 max_ch = _MAX(max_ch, dev_info->channel_count);
926 }
927 }
928
929 return max_ch;
930}
931
Haynes Mathew George569b7482017-05-08 14:44:27 -0700932int audio_extn_usb_get_max_bit_width(bool is_playback)
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800933{
934 struct listnode *node_i, *node_j;
935 struct usb_device_config *dev_info;
936 struct usb_card_config *card_info;
937 unsigned int max_bw = 16;
938 list_for_each(node_i, &usbmod->usb_card_conf_list) {
939 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700940 if (usb_output_device(card_info->usb_device_type) && !is_playback)
941 continue;
942 else if (usb_input_device(card_info->usb_device_type) && is_playback)
943 continue;
944
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800945 list_for_each(node_j, &card_info->usb_device_conf_list) {
946 dev_info = node_to_item(node_j, struct usb_device_config, list);
947 max_bw = _MAX(max_bw, dev_info->bit_width);
948 }
949 }
950
951 return max_bw;
952}
953
Haynes Mathew George569b7482017-05-08 14:44:27 -0700954int audio_extn_usb_sup_sample_rates(bool is_playback,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800955 uint32_t *sample_rates,
956 uint32_t sample_rate_size)
957{
958 struct listnode *node_i, *node_j;
959 struct usb_device_config *dev_info;
960 struct usb_card_config *card_info;
961
Haynes Mathew George569b7482017-05-08 14:44:27 -0700962 int type = is_playback ? USB_PLAYBACK : USB_CAPTURE;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800963
964 ALOGV("%s supported_sample_rates_mask 0x%x", __func__, supported_sample_rates_mask[type]);
965 uint32_t bm = supported_sample_rates_mask[type];
966 uint32_t tries = _MIN(sample_rate_size, (uint32_t)__builtin_popcount(bm));
967
968 int i = 0;
969 while (tries--) {
970 int idx = __builtin_ffs(bm) - 1;
971 sample_rates[i++] = supported_sample_rates[idx];
972 bm &= ~(1<<idx);
973 }
974
975 return i;
976}
977
David Linee3fe402017-03-13 10:00:42 -0700978bool audio_extn_usb_is_capture_supported()
979{
980 if (usbmod == NULL) {
981 ALOGE("%s: USB device object is NULL", __func__);
982 return false;
983 }
984 ALOGV("%s: capture_supported %d",__func__,usbmod->is_capture_supported);
985 return usbmod->is_capture_supported;
986}
987
988void audio_extn_usb_add_device(audio_devices_t device, int card)
989{
990 struct usb_card_config *usb_card_info;
991 char check_debug_enable[PROPERTY_VALUE_MAX];
992 struct listnode *node_i;
993
994 property_get("audio.usb.enable.debug", check_debug_enable, NULL);
995 if (atoi(check_debug_enable)) {
996 usb_audio_debug_enable = true;
997 }
998
999 ALOGI_IF(usb_audio_debug_enable,
1000 "%s: parameters device(0x%x), card(%d)",
1001 __func__, device, card);
1002 if (usbmod == NULL) {
1003 ALOGE("%s: USB device object is NULL", __func__);
1004 goto exit;
1005 }
1006
1007 if (!(usb_valid_device(device)) || (card < 0)) {
1008 ALOGE("%s:device(0x%x), card(%d)",
1009 __func__, device, card);
1010 goto exit;
1011 }
1012
1013 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1014 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
1015 ALOGI_IF(usb_audio_debug_enable,
1016 "%s: list has capability for card_dev_type (0x%x), card_no(%d)",
1017 __func__, usb_card_info->usb_device_type, usb_card_info->usb_card);
1018 /* If we have cached the capability */
1019 if ((usb_card_info->usb_device_type == device) && (usb_card_info->usb_card == card)) {
1020 ALOGV("%s: capability for device(0x%x), card(%d) is cached, no need to update",
1021 __func__, device, card);
1022 goto exit;
1023 }
1024 }
1025 usb_card_info = calloc(1, sizeof(struct usb_card_config));
1026 if (usb_card_info == NULL) {
1027 ALOGE("%s: error unable to allocate memory",
1028 __func__);
1029 goto exit;
1030 }
1031 list_init(&usb_card_info->usb_device_conf_list);
Haynes Mathew George569b7482017-05-08 14:44:27 -07001032 if (usb_output_device(device)) {
David Linee3fe402017-03-13 10:00:42 -07001033 if (!usb_get_device_playback_config(usb_card_info, card)){
1034 usb_card_info->usb_card = card;
Haynes Mathew George569b7482017-05-08 14:44:27 -07001035 usb_card_info->usb_device_type = device;
David Linee3fe402017-03-13 10:00:42 -07001036 usb_get_sidetone_mixer(usb_card_info);
1037 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1038 goto exit;
1039 }
Haynes Mathew George569b7482017-05-08 14:44:27 -07001040 } else if (usb_input_device(device)) {
David Linee3fe402017-03-13 10:00:42 -07001041 if (!usb_get_device_capture_config(usb_card_info, card)) {
1042 usb_card_info->usb_card = card;
Haynes Mathew George569b7482017-05-08 14:44:27 -07001043 usb_card_info->usb_device_type = device;
David Linee3fe402017-03-13 10:00:42 -07001044 usbmod->is_capture_supported = true;
1045 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1046 goto exit;
1047 }
Haynes Mathew George569b7482017-05-08 14:44:27 -07001048 } else {
1049 ALOGW("%s: unknown device 0x%x", __func__, device);
David Linee3fe402017-03-13 10:00:42 -07001050 }
1051 /* free memory in error case */
1052 if (usb_card_info != NULL)
1053 free(usb_card_info);
1054exit:
1055 if (usb_audio_debug_enable)
1056 usb_print_active_device();
1057 return;
1058}
1059
1060void audio_extn_usb_remove_device(audio_devices_t device, int card)
1061{
1062 struct listnode *node_i, *temp_i;
1063 struct listnode *node_j, *temp_j;
1064 struct usb_device_config *dev_info;
1065 struct usb_card_config *card_info;
1066 unsigned int i;
1067
1068 ALOGV("%s: device(0x%x), card(%d)",
1069 __func__, device, card);
1070
1071 if (usbmod == NULL) {
1072 ALOGE("%s: USB device object is NULL", __func__);
1073 goto exit;
1074 }
1075
1076 if (!(usb_valid_device(device)) || (card < 0)) {
1077 ALOGE("%s: Invalid parameters device(0x%x), card(%d)",
1078 __func__, device, card);
1079 goto exit;
1080 }
1081 list_for_each_safe(node_i, temp_i, &usbmod->usb_card_conf_list) {
1082 card_info = node_to_item(node_i, struct usb_card_config, list);
1083 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1084 __func__, card_info->usb_device_type, card_info->usb_card);
1085 if ((device == card_info->usb_device_type) && (card == card_info->usb_card)){
1086 list_for_each_safe(node_j, temp_j, &card_info->usb_device_conf_list) {
1087 dev_info = node_to_item(node_j, struct usb_device_config, list);
1088 ALOGV("%s: bit-width(%d) channel(%d)",
1089 __func__, dev_info->bit_width, dev_info->channel_count);
1090 for (i = 0; i < dev_info->rate_size; i++)
1091 ALOGV("%s: rate %d", __func__, dev_info->rates[i]);
1092
1093 list_remove(node_j);
1094 free(node_to_item(node_j, struct usb_device_config, list));
1095 }
1096 list_remove(node_i);
Haynes Mathew George569b7482017-05-08 14:44:27 -07001097 if (card_info->usb_snd_mixer) {
1098 mixer_close(card_info->usb_snd_mixer);
1099 }
David Linee3fe402017-03-13 10:00:42 -07001100 free(node_to_item(node_i, struct usb_card_config, list));
1101 }
1102 }
1103 usbmod->is_capture_supported = false;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -08001104 supported_sample_rates_mask[USB_PLAYBACK] = 0;
1105 supported_sample_rates_mask[USB_CAPTURE] = 0;
David Linee3fe402017-03-13 10:00:42 -07001106exit:
1107 if (usb_audio_debug_enable)
1108 usb_print_active_device();
1109
1110 return;
1111}
1112
1113void audio_extn_usb_init(void *adev)
1114{
1115 if (usbmod == NULL) {
1116 usbmod = calloc(1, sizeof(struct usb_module));
1117 if (usbmod == NULL) {
1118 ALOGE("%s: error unable to allocate memory", __func__);
1119 goto exit;
1120 }
1121 } else {
1122 memset(usbmod, 0, sizeof(*usbmod));
1123 }
1124
1125 list_init(&usbmod->usb_card_conf_list);
1126 usbmod->adev = (struct audio_device*)adev;
1127 usbmod->sidetone_gain = usb_sidetone_gain;
1128 usbmod->is_capture_supported = false;
1129exit:
1130 return;
1131}
1132
1133void audio_extn_usb_deinit(void)
1134{
1135 if (NULL != usbmod){
1136 free(usbmod);
1137 usbmod = NULL;
1138 }
1139}
1140#endif /*USB_HEADSET_ENABLED end*/