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