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