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