blob: 4e4c70d578202e154a49e127f4999bf50b56b87d [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;
Haynes Mathew George569b7482017-05-08 14:44:27 -0700293 int tries=5;
David Linee3fe402017-03-13 10:00:42 -0700294
295 memset(path, 0, sizeof(path));
296 ALOGV("%s: for %s", __func__, (type == USB_PLAYBACK) ?
297 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR);
298
299 /* TODO: convert the below to using alsa_utils */
300 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/stream0",
301 card);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700302 if (ret < 0) {
David Linee3fe402017-03-13 10:00:42 -0700303 ALOGE("%s: failed on snprintf (%d) to path %s\n",
304 __func__, ret, path);
305 goto done;
306 }
307
Haynes Mathew George569b7482017-05-08 14:44:27 -0700308 while (tries--) {
309 if (access(path, F_OK) < 0) {
310 ALOGW("stream %s doesn't exist retrying\n", path);
311 sleep(1);
312 continue;
313 }
314 }
315
David Linee3fe402017-03-13 10:00:42 -0700316 fd = open(path, O_RDONLY);
317 if (fd <0) {
318 ALOGE("%s: error failed to open config file %s error: %d\n",
319 __func__, path, errno);
320 ret = -EINVAL;
321 goto done;
322 }
323
324 read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
325
326 if (!read_buf) {
327 ALOGE("Failed to create read_buf");
328 ret = -ENOMEM;
329 goto done;
330 }
331
332 if(read(fd, read_buf, USB_BUFF_SIZE) < 0) {
333 ALOGE("file read error\n");
334 goto done;
335 }
336 str_start = strstr(read_buf, ((type == USB_PLAYBACK) ?
337 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
338 if (str_start == NULL) {
339 ALOGE("%s: error %s section not found in usb config file",
340 __func__, ((type == USB_PLAYBACK) ?
341 PLAYBACK_PROFILE_STR : CAPTURE_PROFILE_STR));
342 ret = -EINVAL;
343 goto done;
344 }
345 str_end = strstr(read_buf, ((type == USB_PLAYBACK) ?
346 CAPTURE_PROFILE_STR : PLAYBACK_PROFILE_STR));
347 if (str_end > str_start)
348 check = true;
349
350 ALOGV("%s: usb_config = %s, check %d\n", __func__, str_start, check);
351
352 while (str_start != NULL) {
353 str_start = strstr(str_start, "Altset");
354 if ((str_start == NULL) || (check && (str_start >= str_end))) {
355 ALOGV("%s: done parsing %s\n", __func__, str_start);
356 break;
357 }
358 ALOGV("%s: remaining string %s\n", __func__, str_start);
359 str_start += sizeof("Altset");
360 usb_device_info = calloc(1, sizeof(struct usb_device_config));
361 if (usb_device_info == NULL) {
362 ALOGE("%s: error unable to allocate memory",
363 __func__);
364 ret = -ENOMEM;
365 break;
366 }
367 /* Bit bit_width parsing */
368 bit_width_start = strstr(str_start, "Format: ");
369 if (bit_width_start == NULL) {
370 ALOGI("%s: Could not find bit_width string", __func__);
371 free(usb_device_info);
372 continue;
373 }
374 target = strchr(bit_width_start, '\n');
375 if (target == NULL) {
376 ALOGI("%s:end of line not found", __func__);
377 free(usb_device_info);
378 continue;
379 }
380 size = target - bit_width_start;
381 if ((bit_width_str = (char *)malloc(size + 1)) == NULL) {
382 ALOGE("%s: unable to allocate memory to hold bit width strings",
383 __func__);
384 ret = -EINVAL;
385 free(usb_device_info);
386 break;
387 }
388 memcpy(bit_width_str, bit_width_start, size);
389 bit_width_str[size] = '\0';
390 if (strstr(bit_width_str, "S16_LE"))
391 usb_device_info->bit_width = 16;
392 else if (strstr(bit_width_str, "S24_LE"))
393 usb_device_info->bit_width = 24;
394 else if (strstr(bit_width_str, "S24_3LE"))
395 usb_device_info->bit_width = 24;
396 else if (strstr(bit_width_str, "S32_LE"))
397 usb_device_info->bit_width = 32;
398
399 if (bit_width_str)
400 free(bit_width_str);
401
402 /* channels parsing */
403 channel_start = strstr(str_start, CHANNEL_NUMBER_STR);
404 if (channel_start == NULL) {
405 ALOGI("%s: could not find Channels string", __func__);
406 free(usb_device_info);
407 continue;
408 }
409 channels_no = atoi(channel_start + strlen(CHANNEL_NUMBER_STR));
410 usb_device_info->channel_count = channels_no;
411
412 /* Sample rates parsing */
413 rates_str_start = strstr(str_start, "Rates: ");
414 if (rates_str_start == NULL) {
415 ALOGI("%s: cant find rates string", __func__);
416 free(usb_device_info);
417 continue;
418 }
419 target = strchr(rates_str_start, '\n');
420 if (target == NULL) {
421 ALOGI("%s: end of line not found", __func__);
422 free(usb_device_info);
423 continue;
424 }
425 size = target - rates_str_start;
426 if ((rates_str = (char *)malloc(size + 1)) == NULL) {
427 ALOGE("%s: unable to allocate memory to hold sample rate strings",
428 __func__);
429 ret = -EINVAL;
430 free(usb_device_info);
431 break;
432 }
433 memcpy(rates_str, rates_str_start, size);
434 rates_str[size] = '\0';
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800435 ret = usb_get_sample_rates(type, rates_str, usb_device_info);
David Linee3fe402017-03-13 10:00:42 -0700436 if (rates_str)
437 free(rates_str);
438 if (ret < 0) {
439 ALOGE("%s: error unable to get sample rate values",
440 __func__);
441 free(usb_device_info);
442 continue;
443 }
444 /* Add to list if every field is valid */
445 list_add_tail(&usb_card_info->usb_device_conf_list,
446 &usb_device_info->list);
447 }
448
449done:
450 if (fd >= 0) close(fd);
451 if (read_buf) free(read_buf);
452 return ret;
453}
454
455static int usb_get_device_playback_config(struct usb_card_config *usb_card_info,
456 int card)
457{
458 int ret;
459
460 /* get capabilities */
461 if ((ret = usb_get_capability(USB_PLAYBACK, usb_card_info, card))) {
462 ALOGE("%s: could not get Playback capabilities from usb device",
463 __func__);
464 goto exit;
465 }
466 usb_set_dev_id_mixer_ctl(USB_PLAYBACK, card, "USB_AUDIO_RX dev_token");
467
468exit:
469
470 return ret;
471}
472
473static int usb_get_device_capture_config(struct usb_card_config *usb_card_info,
474 int card)
475{
476 int ret;
477
478 /* get capabilities */
479 if ((ret = usb_get_capability(USB_CAPTURE, usb_card_info, card))) {
480 ALOGE("%s: could not get Playback capabilities from usb device",
481 __func__);
482 goto exit;
483 }
484 usb_set_dev_id_mixer_ctl(USB_CAPTURE, card, "USB_AUDIO_TX dev_token");
485
486exit:
487 return ret;
488}
489
490static void usb_get_sidetone_mixer(struct usb_card_config *usb_card_info)
491{
492 struct mixer_ctl *ctl;
493 unsigned int index;
494
495 for (index = 0; index < USB_SIDETONE_MAX_INDEX; index++)
496 usb_card_info->usb_sidetone_index[index] = -1;
497
498 usb_card_info->usb_snd_mixer = mixer_open(usb_card_info->usb_card);
499 for (index = 0;
500 index < sizeof(usb_sidetone_enable_str)/sizeof(usb_sidetone_enable_str[0]);
501 index++) {
502 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
503 usb_sidetone_enable_str[index]);
504 if (ctl) {
505 usb_card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX] = index;
506 /* Disable device sidetone by default */
507 mixer_ctl_set_value(ctl, 0, false);
508 break;
509 }
510 }
511 for (index = 0;
512 index < sizeof(usb_sidetone_volume_str)/sizeof(usb_sidetone_volume_str[0]);
513 index++) {
514 ctl = mixer_get_ctl_by_name(usb_card_info->usb_snd_mixer,
515 usb_sidetone_volume_str[index]);
516 if (ctl) {
517 usb_card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX] = index;
518 usb_card_info->usb_sidetone_vol_min = mixer_ctl_get_range_min(ctl);
519 usb_card_info->usb_sidetone_vol_max = mixer_ctl_get_range_max(ctl);
520 break;
521 }
522 }
523
524 if ((usb_card_info->usb_snd_mixer != NULL) && (usb_audio_debug_enable))
525 usb_soundcard_list_controls(usb_card_info->usb_snd_mixer);
526
527 return;
528}
529
Haynes Mathew George569b7482017-05-08 14:44:27 -0700530static inline bool usb_output_device(audio_devices_t device) {
531 // ignore accessory for now
532 if (device == AUDIO_DEVICE_OUT_USB_ACCESSORY) {
533 return false;
534 }
535 return audio_is_usb_out_device(device);
536}
537
538static inline bool usb_input_device(audio_devices_t device) {
539 // ignore accessory for now
540 if (device == AUDIO_DEVICE_IN_USB_ACCESSORY) {
541 return false;
542 }
543 return audio_is_usb_in_device(device);
544}
545
David Linee3fe402017-03-13 10:00:42 -0700546static bool usb_valid_device(audio_devices_t device)
547{
Haynes Mathew George569b7482017-05-08 14:44:27 -0700548 return usb_output_device(device) ||
549 usb_input_device(device);
David Linee3fe402017-03-13 10:00:42 -0700550}
551
552static void usb_print_active_device(void){
553 struct listnode *node_i, *node_j;
554 struct usb_device_config *dev_info;
555 struct usb_card_config *card_info;
556 unsigned int i;
557
558 ALOGI("%s", __func__);
559 list_for_each(node_i, &usbmod->usb_card_conf_list) {
560 card_info = node_to_item(node_i, struct usb_card_config, list);
561 ALOGI("%s: card_dev_type (0x%x), card_no(%d)",
562 __func__, card_info->usb_device_type, card_info->usb_card);
563 list_for_each(node_j, &card_info->usb_device_conf_list) {
564 dev_info = node_to_item(node_j, struct usb_device_config, list);
565 ALOGI("%s: bit-width(%d) channel(%d)",
566 __func__, dev_info->bit_width, dev_info->channel_count);
567 for (i = 0; i < dev_info->rate_size; i++)
568 ALOGI("%s: rate %d", __func__, dev_info->rates[i]);
569 }
570 }
571}
572
573static bool usb_get_best_match_for_bit_width(
574 struct listnode *dev_list,
575 unsigned int stream_bit_width,
576 unsigned int *bit_width)
577{
578 struct listnode *node_i;
579 struct usb_device_config *dev_info;
580 unsigned int candidate = 0;
581
582 list_for_each(node_i, dev_list) {
583 dev_info = node_to_item(node_i, struct usb_device_config, list);
584 ALOGI_IF(usb_audio_debug_enable,
585 "%s: USB bw(%d), stream bw(%d), candidate(%d)",
586 __func__, dev_info->bit_width,
587 stream_bit_width, candidate);
588 if (dev_info->bit_width == stream_bit_width) {
589 *bit_width = dev_info->bit_width;
590 ALOGV("%s: Found match bit-width (%d)",
591 __func__, dev_info->bit_width);
592 goto exit;
593 } else if (candidate == 0) {
594 candidate = dev_info->bit_width;
595 }
596 /*
597 * If stream bit is 24, USB supports both 16 bit and 32 bit, then
598 * higher bit width 32 is picked up instead of 16-bit
599 */
600 else if (ABS_SUB(stream_bit_width, dev_info->bit_width) <
601 ABS_SUB(stream_bit_width, candidate)) {
602 candidate = dev_info->bit_width;
603 }
604 else if ((ABS_SUB(stream_bit_width, dev_info->bit_width) ==
605 ABS_SUB(stream_bit_width, candidate)) &&
606 (dev_info->bit_width > candidate)) {
607 candidate = dev_info->bit_width;
608 }
609 }
610 ALOGV("%s: No match found, use the best candidate bw(%d)",
611 __func__, candidate);
612 *bit_width = candidate;
613exit:
614 return true;
615}
616
617static bool usb_get_best_match_for_channels(
618 struct listnode *dev_list,
619 unsigned int bit_width,
620 unsigned int stream_ch,
621 unsigned int *channel_count)
622{
623 struct listnode *node_i;
624 struct usb_device_config *dev_info;
625 unsigned int candidate = 0;
626
627 list_for_each(node_i, dev_list) {
628 dev_info = node_to_item(node_i, struct usb_device_config, list);
629 ALOGI_IF(usb_audio_debug_enable,
630 "%s: USB ch(%d)bw(%d), stream ch(%d)bw(%d), candidate(%d)",
631 __func__, dev_info->channel_count, dev_info->bit_width,
632 stream_ch, bit_width, candidate);
633 if (dev_info->bit_width != bit_width)
634 continue;
635 if (dev_info->channel_count== stream_ch) {
636 *channel_count = dev_info->channel_count;
637 ALOGV("%s: Found match channels (%d)",
638 __func__, dev_info->channel_count);
639 goto exit;
640 } else if (candidate == 0)
641 candidate = dev_info->channel_count;
642 /*
643 * If stream channel is 4, USB supports both 3 and 5, then
644 * higher channel 5 is picked up instead of 3
645 */
646 else if (ABS_SUB(stream_ch, dev_info->channel_count) <
647 ABS_SUB(stream_ch, candidate)) {
648 candidate = dev_info->channel_count;
649 } else if ((ABS_SUB(stream_ch, dev_info->channel_count) ==
650 ABS_SUB(stream_ch, candidate)) &&
651 (dev_info->channel_count > candidate)) {
652 candidate = dev_info->channel_count;
653 }
654 }
655 ALOGV("%s: No match found, use the best candidate ch(%d)",
656 __func__, candidate);
657 *channel_count = candidate;
658exit:
659 return true;
660
661}
662
663static bool usb_sample_rate_multiple(
664 unsigned int stream_sample_rate,
665 unsigned int base)
666{
667 return (((stream_sample_rate / base) * base) == stream_sample_rate);
668}
669
670static bool usb_find_sample_rate_candidate(unsigned int base,
671 unsigned stream_rate,
672 unsigned int usb_rate,
673 unsigned int cur_candidate,
674 unsigned int *update_candidate)
675{
676 /* For sample rate, we should consider fracational sample rate as high priority.
677 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
678 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
679 */
680 if (!usb_sample_rate_multiple(cur_candidate, base) &&
681 usb_sample_rate_multiple(usb_rate, base)) {
682 *update_candidate = usb_rate;
683 } else if (usb_sample_rate_multiple(cur_candidate, base) &&
684 usb_sample_rate_multiple(usb_rate, base)) {
685 if (ABS_SUB(stream_rate, usb_rate) <
686 ABS_SUB(stream_rate, cur_candidate)) {
687 *update_candidate = usb_rate;
688 } else if ((ABS_SUB(stream_rate, usb_rate) ==
689 ABS_SUB(stream_rate, cur_candidate)) &&
690 (usb_rate > cur_candidate)) {
691 *update_candidate = usb_rate;
692 }
693 } else if (!usb_sample_rate_multiple(cur_candidate, base) &&
694 !usb_sample_rate_multiple(usb_rate, base)) {
695 if (ABS_SUB(stream_rate, usb_rate) <
696 ABS_SUB(stream_rate, cur_candidate)) {
697 *update_candidate = usb_rate;
698 } else if ((ABS_SUB(stream_rate, usb_rate) ==
699 ABS_SUB(stream_rate, cur_candidate)) &&
700 (usb_rate > cur_candidate)) {
701 *update_candidate = usb_rate;
702 }
703 }
704 return true;
705}
706
707static bool usb_get_best_match_for_sample_rate(
708 struct listnode *dev_list,
709 unsigned int bit_width,
710 unsigned int channel_count,
711 unsigned int stream_sample_rate,
712 unsigned int *sr)
713{
714 struct listnode *node_i;
715 struct usb_device_config *dev_info;
716 unsigned int candidate = 48000;
717 unsigned int base = SAMPLE_RATE_8000;
718 bool multiple_8k = usb_sample_rate_multiple(stream_sample_rate, base);
719 unsigned int i;
720
721 ALOGV("%s: stm ch(%d)bw(%d)sr(%d), stream sample multiple of 8kHz(%d)",
722 __func__, channel_count, bit_width, stream_sample_rate, multiple_8k);
723
724 list_for_each(node_i, dev_list) {
725 dev_info = node_to_item(node_i, struct usb_device_config, list);
726 ALOGI_IF(usb_audio_debug_enable,
727 "%s: USB ch(%d)bw(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
728 __func__, dev_info->channel_count, dev_info->bit_width,
729 channel_count, bit_width, stream_sample_rate, candidate);
730 if ((dev_info->bit_width != bit_width) || dev_info->channel_count != channel_count)
731 continue;
732
733 candidate = 0;
734 for (i = 0; i < dev_info->rate_size; i++) {
735 ALOGI_IF(usb_audio_debug_enable,
736 "%s: USB ch(%d)bw(%d)sr(%d), stm ch(%d)bw(%d)sr(%d), candidate(%d)",
737 __func__, dev_info->channel_count,
738 dev_info->bit_width, dev_info->rates[i],
739 channel_count, bit_width, stream_sample_rate, candidate);
740 if (stream_sample_rate == dev_info->rates[i]) {
741 *sr = dev_info->rates[i];
742 ALOGV("%s: Found match sample rate (%d)",
743 __func__, dev_info->rates[i]);
744 goto exit;
745 } else if (candidate == 0) {
746 candidate = dev_info->rates[i];
747 /*
748 * For sample rate, we should consider fracational sample rate as high priority.
749 * For example, if the stream is 88.2kHz and USB device support both 44.1kH and
750 * 48kHz sample rate, we should pick 44.1kHz instead of 48kHz
751 */
752 } else if (multiple_8k) {
753 usb_find_sample_rate_candidate(SAMPLE_RATE_8000,
754 stream_sample_rate,
755 dev_info->rates[i],
756 candidate,
757 &candidate);
758 } else {
759 usb_find_sample_rate_candidate(SAMPLE_RATE_11025,
760 stream_sample_rate,
761 dev_info->rates[i],
762 candidate,
763 &candidate);
764 }
765 }
766 }
767 ALOGV("%s: No match found, use the best candidate sr(%d)",
768 __func__, candidate);
769 *sr = candidate;
770exit:
771 return true;
772}
773
774static bool usb_audio_backend_apply_policy(struct listnode *dev_list,
775 unsigned int *bit_width,
776 unsigned int *sample_rate,
777 unsigned int *channel_count)
778{
779 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) channels (%d)",
780 __func__, *bit_width, *sample_rate, *channel_count);
781 if (list_empty(dev_list)) {
782 *sample_rate = 48000;
783 *bit_width = 16;
784 *channel_count = 2;
785 ALOGE("%s: list is empty,fall back to default setting", __func__);
786 goto exit;
787 }
788 usb_get_best_match_for_bit_width(dev_list, *bit_width, bit_width);
789 usb_get_best_match_for_channels(dev_list,
790 *bit_width,
791 *channel_count,
792 channel_count);
793 usb_get_best_match_for_sample_rate(dev_list,
794 *bit_width,
795 *channel_count,
796 *sample_rate,
797 sample_rate);
798exit:
799 ALOGV("%s: Updated sample rate per profile: bit-width(%d) rate(%d) chs(%d)",
800 __func__, *bit_width, *sample_rate, *channel_count);
801 return true;
802}
803
804static int usb_get_sidetone_gain(struct usb_card_config *card_info)
805{
806 int gain = card_info->usb_sidetone_vol_min + usbmod->sidetone_gain;
807 if (gain > card_info->usb_sidetone_vol_max)
808 gain = card_info->usb_sidetone_vol_max;
809 return gain;
810}
811
812void audio_extn_usb_set_sidetone_gain(struct str_parms *parms,
813 char *value, int len)
814{
815 int err;
816
817 err = str_parms_get_str(parms, USB_SIDETONE_GAIN_STR,
818 value, len);
819 if (err >= 0) {
820 usb_sidetone_gain = pow(10.0, (float)(atoi(value))/10.0);
821 ALOGV("%s: sidetone gain(%s) decimal %d",
822 __func__, value, usb_sidetone_gain);
823 str_parms_del(parms, USB_SIDETONE_GAIN_STR);
824 }
825 return;
826}
827
828int audio_extn_usb_enable_sidetone(int device, bool enable)
829{
830 int ret = -ENODEV;
831 struct listnode *node_i;
832 struct usb_card_config *card_info;
833 int i;
834 ALOGV("%s: card_dev_type (0x%x), sidetone enable(%d)",
835 __func__, device, enable);
836
837 list_for_each(node_i, &usbmod->usb_card_conf_list) {
838 card_info = node_to_item(node_i, struct usb_card_config, list);
839 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
840 __func__, card_info->usb_device_type, card_info->usb_card);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700841 if (usb_output_device(card_info->usb_device_type)) {
David Linee3fe402017-03-13 10:00:42 -0700842 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX]) != -1) {
843 struct mixer_ctl *ctl = mixer_get_ctl_by_name(
844 card_info->usb_snd_mixer,
845 usb_sidetone_enable_str[i]);
846 if (ctl)
847 mixer_ctl_set_value(ctl, 0, enable);
848 else
849 break;
850
851 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX]) != -1) {
852 ctl = mixer_get_ctl_by_name(
853 card_info->usb_snd_mixer,
854 usb_sidetone_volume_str[i]);
855 if (ctl == NULL)
856 ALOGV("%s: sidetone gain mixer command is not found",
857 __func__);
858 else if (enable)
859 mixer_ctl_set_value(ctl, 0,
860 usb_get_sidetone_gain(card_info));
861 }
862 ret = 0;
863 break;
864 }
865 }
866 }
867 return ret;
868}
869
870bool audio_extn_usb_is_config_supported(unsigned int *bit_width,
871 unsigned int *sample_rate,
872 unsigned int *channel_count,
873 bool is_playback)
874{
875 struct listnode *node_i;
876 struct usb_card_config *card_info;
877
878 ALOGV("%s: from stream: bit-width(%d) sample_rate(%d) ch(%d) is_playback(%d)",
879 __func__, *bit_width, *sample_rate, *channel_count, is_playback);
880 list_for_each(node_i, &usbmod->usb_card_conf_list) {
881 card_info = node_to_item(node_i, struct usb_card_config, list);
882 ALOGI_IF(usb_audio_debug_enable,
883 "%s: card_dev_type (0x%x), card_no(%d)",
884 __func__, card_info->usb_device_type, card_info->usb_card);
885 /* Currently only apply the first playback sound card configuration */
Haynes Mathew George569b7482017-05-08 14:44:27 -0700886 if ((is_playback && usb_output_device(card_info->usb_device_type)) ||
887 (!is_playback && usb_input_device(card_info->usb_device_type))) {
David Linee3fe402017-03-13 10:00:42 -0700888 usb_audio_backend_apply_policy(&card_info->usb_device_conf_list,
889 bit_width,
890 sample_rate,
891 channel_count);
892 break;
893 }
894 }
895 ALOGV("%s: updated: bit-width(%d) sample_rate(%d) channels (%d)",
896 __func__, *bit_width, *sample_rate, *channel_count);
897
898 return true;
899}
900
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800901#define _MAX(x, y) (((x) >= (y)) ? (x) : (y))
902#define _MIN(x, y) (((x) <= (y)) ? (x) : (y))
903
Haynes Mathew George569b7482017-05-08 14:44:27 -0700904int audio_extn_usb_get_max_channels(bool is_playback)
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800905{
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_ch = 1;
910 list_for_each(node_i, &usbmod->usb_card_conf_list) {
911 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700912 if (usb_output_device(card_info->usb_device_type) && !is_playback)
913 continue;
914 else if (usb_input_device(card_info->usb_device_type) && is_playback)
915 continue;
916
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800917 list_for_each(node_j, &card_info->usb_device_conf_list) {
918 dev_info = node_to_item(node_j, struct usb_device_config, list);
919 max_ch = _MAX(max_ch, dev_info->channel_count);
920 }
921 }
922
923 return max_ch;
924}
925
Haynes Mathew George569b7482017-05-08 14:44:27 -0700926int audio_extn_usb_get_max_bit_width(bool is_playback)
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800927{
928 struct listnode *node_i, *node_j;
929 struct usb_device_config *dev_info;
930 struct usb_card_config *card_info;
931 unsigned int max_bw = 16;
932 list_for_each(node_i, &usbmod->usb_card_conf_list) {
933 card_info = node_to_item(node_i, struct usb_card_config, list);
Haynes Mathew George569b7482017-05-08 14:44:27 -0700934 if (usb_output_device(card_info->usb_device_type) && !is_playback)
935 continue;
936 else if (usb_input_device(card_info->usb_device_type) && is_playback)
937 continue;
938
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800939 list_for_each(node_j, &card_info->usb_device_conf_list) {
940 dev_info = node_to_item(node_j, struct usb_device_config, list);
941 max_bw = _MAX(max_bw, dev_info->bit_width);
942 }
943 }
944
945 return max_bw;
946}
947
Haynes Mathew George569b7482017-05-08 14:44:27 -0700948int audio_extn_usb_sup_sample_rates(bool is_playback,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800949 uint32_t *sample_rates,
950 uint32_t sample_rate_size)
951{
952 struct listnode *node_i, *node_j;
953 struct usb_device_config *dev_info;
954 struct usb_card_config *card_info;
955
Haynes Mathew George569b7482017-05-08 14:44:27 -0700956 int type = is_playback ? USB_PLAYBACK : USB_CAPTURE;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800957
958 ALOGV("%s supported_sample_rates_mask 0x%x", __func__, supported_sample_rates_mask[type]);
959 uint32_t bm = supported_sample_rates_mask[type];
960 uint32_t tries = _MIN(sample_rate_size, (uint32_t)__builtin_popcount(bm));
961
962 int i = 0;
963 while (tries--) {
964 int idx = __builtin_ffs(bm) - 1;
965 sample_rates[i++] = supported_sample_rates[idx];
966 bm &= ~(1<<idx);
967 }
968
969 return i;
970}
971
David Linee3fe402017-03-13 10:00:42 -0700972bool audio_extn_usb_is_capture_supported()
973{
974 if (usbmod == NULL) {
975 ALOGE("%s: USB device object is NULL", __func__);
976 return false;
977 }
978 ALOGV("%s: capture_supported %d",__func__,usbmod->is_capture_supported);
979 return usbmod->is_capture_supported;
980}
981
982void audio_extn_usb_add_device(audio_devices_t device, int card)
983{
984 struct usb_card_config *usb_card_info;
985 char check_debug_enable[PROPERTY_VALUE_MAX];
986 struct listnode *node_i;
987
988 property_get("audio.usb.enable.debug", check_debug_enable, NULL);
989 if (atoi(check_debug_enable)) {
990 usb_audio_debug_enable = true;
991 }
992
993 ALOGI_IF(usb_audio_debug_enable,
994 "%s: parameters device(0x%x), card(%d)",
995 __func__, device, card);
996 if (usbmod == NULL) {
997 ALOGE("%s: USB device object is NULL", __func__);
998 goto exit;
999 }
1000
1001 if (!(usb_valid_device(device)) || (card < 0)) {
1002 ALOGE("%s:device(0x%x), card(%d)",
1003 __func__, device, card);
1004 goto exit;
1005 }
1006
1007 list_for_each(node_i, &usbmod->usb_card_conf_list) {
1008 usb_card_info = node_to_item(node_i, struct usb_card_config, list);
1009 ALOGI_IF(usb_audio_debug_enable,
1010 "%s: list has capability for card_dev_type (0x%x), card_no(%d)",
1011 __func__, usb_card_info->usb_device_type, usb_card_info->usb_card);
1012 /* If we have cached the capability */
1013 if ((usb_card_info->usb_device_type == device) && (usb_card_info->usb_card == card)) {
1014 ALOGV("%s: capability for device(0x%x), card(%d) is cached, no need to update",
1015 __func__, device, card);
1016 goto exit;
1017 }
1018 }
1019 usb_card_info = calloc(1, sizeof(struct usb_card_config));
1020 if (usb_card_info == NULL) {
1021 ALOGE("%s: error unable to allocate memory",
1022 __func__);
1023 goto exit;
1024 }
1025 list_init(&usb_card_info->usb_device_conf_list);
Haynes Mathew George569b7482017-05-08 14:44:27 -07001026 if (usb_output_device(device)) {
David Linee3fe402017-03-13 10:00:42 -07001027 if (!usb_get_device_playback_config(usb_card_info, card)){
1028 usb_card_info->usb_card = card;
Haynes Mathew George569b7482017-05-08 14:44:27 -07001029 usb_card_info->usb_device_type = device;
David Linee3fe402017-03-13 10:00:42 -07001030 usb_get_sidetone_mixer(usb_card_info);
1031 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1032 goto exit;
1033 }
Haynes Mathew George569b7482017-05-08 14:44:27 -07001034 } else if (usb_input_device(device)) {
David Linee3fe402017-03-13 10:00:42 -07001035 if (!usb_get_device_capture_config(usb_card_info, card)) {
1036 usb_card_info->usb_card = card;
Haynes Mathew George569b7482017-05-08 14:44:27 -07001037 usb_card_info->usb_device_type = device;
David Linee3fe402017-03-13 10:00:42 -07001038 usbmod->is_capture_supported = true;
1039 list_add_tail(&usbmod->usb_card_conf_list, &usb_card_info->list);
1040 goto exit;
1041 }
Haynes Mathew George569b7482017-05-08 14:44:27 -07001042 } else {
1043 ALOGW("%s: unknown device 0x%x", __func__, device);
David Linee3fe402017-03-13 10:00:42 -07001044 }
1045 /* free memory in error case */
1046 if (usb_card_info != NULL)
1047 free(usb_card_info);
1048exit:
1049 if (usb_audio_debug_enable)
1050 usb_print_active_device();
1051 return;
1052}
1053
1054void audio_extn_usb_remove_device(audio_devices_t device, int card)
1055{
1056 struct listnode *node_i, *temp_i;
1057 struct listnode *node_j, *temp_j;
1058 struct usb_device_config *dev_info;
1059 struct usb_card_config *card_info;
1060 unsigned int i;
1061
1062 ALOGV("%s: device(0x%x), card(%d)",
1063 __func__, device, card);
1064
1065 if (usbmod == NULL) {
1066 ALOGE("%s: USB device object is NULL", __func__);
1067 goto exit;
1068 }
1069
1070 if (!(usb_valid_device(device)) || (card < 0)) {
1071 ALOGE("%s: Invalid parameters device(0x%x), card(%d)",
1072 __func__, device, card);
1073 goto exit;
1074 }
1075 list_for_each_safe(node_i, temp_i, &usbmod->usb_card_conf_list) {
1076 card_info = node_to_item(node_i, struct usb_card_config, list);
1077 ALOGV("%s: card_dev_type (0x%x), card_no(%d)",
1078 __func__, card_info->usb_device_type, card_info->usb_card);
1079 if ((device == card_info->usb_device_type) && (card == card_info->usb_card)){
1080 list_for_each_safe(node_j, temp_j, &card_info->usb_device_conf_list) {
1081 dev_info = node_to_item(node_j, struct usb_device_config, list);
1082 ALOGV("%s: bit-width(%d) channel(%d)",
1083 __func__, dev_info->bit_width, dev_info->channel_count);
1084 for (i = 0; i < dev_info->rate_size; i++)
1085 ALOGV("%s: rate %d", __func__, dev_info->rates[i]);
1086
1087 list_remove(node_j);
1088 free(node_to_item(node_j, struct usb_device_config, list));
1089 }
1090 list_remove(node_i);
Haynes Mathew George569b7482017-05-08 14:44:27 -07001091 if (card_info->usb_snd_mixer) {
1092 mixer_close(card_info->usb_snd_mixer);
1093 }
David Linee3fe402017-03-13 10:00:42 -07001094 free(node_to_item(node_i, struct usb_card_config, list));
1095 }
1096 }
1097 usbmod->is_capture_supported = false;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -08001098 supported_sample_rates_mask[USB_PLAYBACK] = 0;
1099 supported_sample_rates_mask[USB_CAPTURE] = 0;
David Linee3fe402017-03-13 10:00:42 -07001100exit:
1101 if (usb_audio_debug_enable)
1102 usb_print_active_device();
1103
1104 return;
1105}
1106
1107void audio_extn_usb_init(void *adev)
1108{
1109 if (usbmod == NULL) {
1110 usbmod = calloc(1, sizeof(struct usb_module));
1111 if (usbmod == NULL) {
1112 ALOGE("%s: error unable to allocate memory", __func__);
1113 goto exit;
1114 }
1115 } else {
1116 memset(usbmod, 0, sizeof(*usbmod));
1117 }
1118
1119 list_init(&usbmod->usb_card_conf_list);
1120 usbmod->adev = (struct audio_device*)adev;
1121 usbmod->sidetone_gain = usb_sidetone_gain;
1122 usbmod->is_capture_supported = false;
1123exit:
1124 return;
1125}
1126
1127void audio_extn_usb_deinit(void)
1128{
1129 if (NULL != usbmod){
1130 free(usbmod);
1131 usbmod = NULL;
1132 }
1133}
1134#endif /*USB_HEADSET_ENABLED end*/