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