blob: 2814e37e99cc90e5290281c3c265b5d987e001fb [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -07001/*
2 * Copyright (C) 2012 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
Paul McLean9ab869a2015-01-13 09:37:06 -080017#define LOG_TAG "modules.usbaudio.audio_hal"
Paul McLeancf611912014-04-28 13:03:18 -070018/*#define LOG_NDEBUG 0*/
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070025#include <sys/time.h>
Simon Wilson19957a32012-04-06 16:17:12 -070026
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <log/log.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028#include <cutils/str_parms.h>
29#include <cutils/properties.h>
30
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070032#include <hardware/audio_alsaops.h>
33#include <hardware/hardware.h>
34
35#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070036
37#include <tinyalsa/asoundlib.h>
38
Paul McLeanc2201152014-07-16 13:46:07 -070039#include <audio_utils/channels.h>
40
Andy Hung03576be2014-07-21 21:16:45 -070041/* FOR TESTING:
42 * Set k_force_channels to force the number of channels to present to AudioFlinger.
43 * 0 disables (this is default: present the device channels to AudioFlinger).
44 * 2 forces to legacy stereo mode.
45 *
46 * Others values can be tried (up to 8).
47 * TODO: AudioFlinger cannot support more than 8 active output channels
48 * at this time, so limiting logic needs to be put here or communicated from above.
49 */
50static const unsigned k_force_channels = 0;
51
Paul McLeanc88e6ae2014-07-16 09:48:34 -070052#include "alsa_device_profile.h"
53#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080054#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070055
Paul McLeanc88e6ae2014-07-16 09:48:34 -070056#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070057
Simon Wilson19957a32012-04-06 16:17:12 -070058struct audio_device {
59 struct audio_hw_device hw_device;
60
61 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080062
63 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070064 alsa_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080065
66 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070067 alsa_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080068
Eric Laurent253def92014-09-14 12:18:18 -070069 bool mic_muted;
70
Simon Wilson19957a32012-04-06 16:17:12 -070071 bool standby;
72};
73
74struct stream_out {
75 struct audio_stream_out stream;
76
Paul McLeaneedc92e2013-12-19 15:46:15 -080077 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080078 bool standby;
79
Paul McLeanc88e6ae2014-07-16 09:48:34 -070080 struct audio_device *dev; /* hardware information - only using this for the lock */
81
82 alsa_device_profile * profile;
83 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080084
Andy Hung03576be2014-07-21 21:16:45 -070085 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
86 * This may differ from the device channel count when
87 * the device is not compatible with AudioFlinger
88 * capabilities, e.g. exposes too many channels or
89 * too few channels. */
Paul McLeaneedc92e2013-12-19 15:46:15 -080090 void * conversion_buffer; /* any conversions are put into here
91 * they could come from here too if
92 * there was a previous conversion */
93 size_t conversion_buffer_size; /* in bytes */
94};
95
Paul McLeaneedc92e2013-12-19 15:46:15 -080096struct stream_in {
97 struct audio_stream_in stream;
98
Simon Wilson19957a32012-04-06 16:17:12 -070099 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Simon Wilson19957a32012-04-06 16:17:12 -0700100 bool standby;
101
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700102 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800103
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700104 alsa_device_profile * profile;
105 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700106
Paul McLean2cfd81b2014-09-15 12:32:23 -0700107 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
108 * This may differ from the device channel count when
109 * the device is not compatible with AudioFlinger
110 * capabilities, e.g. exposes too many channels or
111 * too few channels. */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700112 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
Paul McLean30f41852014-04-16 15:44:20 -0700113 void * conversion_buffer; /* any conversions are put into here
114 * they could come from here too if
115 * there was a previous conversion */
116 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700117};
118
Paul McLeaneedc92e2013-12-19 15:46:15 -0800119/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800120 * Data Conversions
121 */
122/*
Paul McLean30f41852014-04-16 15:44:20 -0700123 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
124 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800125 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700126 * out_buff points to the buffer to receive converted PCM16LE LE samples.
127 * returns
128 * the number of BYTES of output data.
129 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
130 * support PCM24_3LE (24-bit, packed).
131 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700132 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700133 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700134 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700135static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
136 short * out_buff)
137{
Paul McLean30f41852014-04-16 15:44:20 -0700138 /*
139 * Move from front to back so that the conversion can be done in-place
140 * i.e. in_buff == out_buff
141 */
142 /* we need 2 bytes in the output for every 3 bytes in the input */
143 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700144 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700145 size_t src_smpl_index;
146 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
147 src_ptr++; /* lowest-(skip)-byte */
148 *dst_ptr++ = *src_ptr++; /* low-byte */
149 *dst_ptr++ = *src_ptr++; /* high-byte */
150 }
151
152 /* return number of *bytes* generated: */
153 return num_in_samples * 2;
154}
155
156/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700157 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
158 * in_buff points to the buffer of PCM32 samples
159 * num_in_samples size of input buffer in SAMPLES
160 * out_buff points to the buffer to receive converted PCM16LE LE samples.
161 * returns
162 * the number of BYTES of output data.
163 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
164 * support PCM_FORMAT_S32_LE (32-bit).
165 * NOTE:
166 * This conversion is safe to do in-place (in_buff == out_buff).
167 * TODO Move this to a utilities module.
168 */
169static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
170{
171 /*
172 * Move from front to back so that the conversion can be done in-place
173 * i.e. in_buff == out_buff
174 */
175
176 short * dst_ptr = out_buff;
177 const int32_t* src_ptr = in_buff;
178 size_t src_smpl_index;
179 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
180 *dst_ptr++ = *src_ptr++ >> 16;
181 }
182
183 /* return number of *bytes* generated: */
184 return num_in_samples * 2;
185}
186
Paul McLean0f1753e2014-12-02 12:36:45 -0700187/*
188 * Extract the card and device numbers from the supplied key/value pairs.
189 * kvpairs A null-terminated string containing the key/value pairs or card and device.
190 * i.e. "card=1;device=42"
191 * card A pointer to a variable to receive the parsed-out card number.
192 * device A pointer to a variable to receive the parsed-out device number.
193 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
194 * associated key/value pair is not found in the provided string.
195 */
196static void parse_card_device_params(const char *kvpairs, int *card, int *device)
197{
198 struct str_parms * parms = str_parms_create_str(kvpairs);
199 char value[32];
200 int param_val;
201
202 // initialize to "undefined" state.
203 *card = -1;
204 *device = -1;
205
206 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
207 if (param_val >= 0) {
208 *card = atoi(value);
209 }
210
211 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
212 if (param_val >= 0) {
213 *device = atoi(value);
214 }
215
216 str_parms_destroy(parms);
217}
218
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700219static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800220{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700221 if (profile->card < 0 || profile->device < 0) {
222 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800223 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700224
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700225 struct str_parms *query = str_parms_create_str(keys);
226 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700227
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700228 /* These keys are from hardware/libhardware/include/audio.h */
229 /* supported sample rates */
230 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
231 char* rates_list = profile_get_sample_rate_strs(profile);
232 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
233 rates_list);
234 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700235 }
236
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700237 /* supported channel counts */
238 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
239 char* channels_list = profile_get_channel_count_strs(profile);
240 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
241 channels_list);
242 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700243 }
244
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700245 /* supported sample formats */
246 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
247 char * format_params = profile_get_format_strs(profile);
248 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
249 format_params);
250 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700251 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700252 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700253
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700254 char* result_str = str_parms_to_str(result);
255 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700256
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700257 ALOGV("usb:audio_hw::device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700258
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700259 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800260}
261
262/*
263 * HAl Functions
264 */
Simon Wilson19957a32012-04-06 16:17:12 -0700265/**
266 * NOTE: when multiple mutexes have to be acquired, always respect the
267 * following order: hw device > out stream
268 */
269
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700270/*
271 * OUT functions
272 */
Simon Wilson19957a32012-04-06 16:17:12 -0700273static uint32_t out_get_sample_rate(const struct audio_stream *stream)
274{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700275 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
276 ALOGV("out_get_sample_rate() = %d", rate);
277 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700278}
279
280static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
281{
282 return 0;
283}
284
285static size_t out_get_buffer_size(const struct audio_stream *stream)
286{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700287 const struct stream_out* out = (const struct stream_out*)stream;
288 size_t buffer_size =
289 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700290 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700291}
292
293static uint32_t out_get_channels(const struct audio_stream *stream)
294{
Andy Hung03576be2014-07-21 21:16:45 -0700295 const struct stream_out *out = (const struct stream_out*)stream;
296 return audio_channel_out_mask_from_count(out->hal_channel_count);
Simon Wilson19957a32012-04-06 16:17:12 -0700297}
298
299static audio_format_t out_get_format(const struct audio_stream *stream)
300{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700301 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
302 * Relies on the framework to provide data in the specified format.
303 * This could change in the future.
304 */
305 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
306 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700307 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700308}
309
310static int out_set_format(struct audio_stream *stream, audio_format_t format)
311{
312 return 0;
313}
314
315static int out_standby(struct audio_stream *stream)
316{
317 struct stream_out *out = (struct stream_out *)stream;
318
319 pthread_mutex_lock(&out->dev->lock);
320 pthread_mutex_lock(&out->lock);
321
322 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700323 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700324 out->standby = true;
325 }
326
327 pthread_mutex_unlock(&out->lock);
328 pthread_mutex_unlock(&out->dev->lock);
329
330 return 0;
331}
332
333static int out_dump(const struct audio_stream *stream, int fd)
334{
335 return 0;
336}
337
338static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
339{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800340 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
341
Simon Wilson19957a32012-04-06 16:17:12 -0700342 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700343
Simon Wilson19957a32012-04-06 16:17:12 -0700344 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800345 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700346 int card = -1;
347 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700348
Paul McLeanf62d75e2014-07-11 15:14:19 -0700349 pthread_mutex_lock(&out->dev->lock);
350 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700351
Paul McLean0f1753e2014-12-02 12:36:45 -0700352 parse_card_device_params(kvpairs, &card, &device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800353
Paul McLean2c6196f2014-08-20 16:50:25 -0700354 if (card >= 0 && device >= 0 && !profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700355 /* cannot read pcm device info if playback is active */
356 if (!out->standby)
357 ret_value = -ENOSYS;
358 else {
359 int saved_card = out->profile->card;
360 int saved_device = out->profile->device;
361 out->profile->card = card;
362 out->profile->device = device;
363 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
364 if (ret_value != 0) {
365 out->profile->card = saved_card;
366 out->profile->device = saved_device;
367 }
368 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800369 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700370
Paul McLeanf62d75e2014-07-11 15:14:19 -0700371 pthread_mutex_unlock(&out->lock);
372 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700373
Paul McLeaneedc92e2013-12-19 15:46:15 -0800374 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700375}
376
Paul McLeanf62d75e2014-07-11 15:14:19 -0700377static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
378{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700379 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700380 pthread_mutex_lock(&out->dev->lock);
381 pthread_mutex_lock(&out->lock);
382
383 char * params_str = device_get_parameters(out->profile, keys);
384
385 pthread_mutex_unlock(&out->lock);
386 pthread_mutex_unlock(&out->dev->lock);
387
388 return params_str;
389}
390
Simon Wilson19957a32012-04-06 16:17:12 -0700391static uint32_t out_get_latency(const struct audio_stream_out *stream)
392{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700393 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
394 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700395}
396
Paul McLean30f41852014-04-16 15:44:20 -0700397static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700398{
399 return -ENOSYS;
400}
401
Paul McLeaneedc92e2013-12-19 15:46:15 -0800402/* must be called with hw device and output stream mutexes locked */
403static int start_output_stream(struct stream_out *out)
404{
Paul McLean30f41852014-04-16 15:44:20 -0700405 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -0700406 out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800407
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700408 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800409}
410
411static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700412{
413 int ret;
414 struct stream_out *out = (struct stream_out *)stream;
415
416 pthread_mutex_lock(&out->dev->lock);
417 pthread_mutex_lock(&out->lock);
418 if (out->standby) {
419 ret = start_output_stream(out);
420 if (ret != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700421 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700422 goto err;
423 }
424 out->standby = false;
425 }
Eric Laurent05333d42014-08-04 20:29:17 -0700426 pthread_mutex_unlock(&out->dev->lock);
427
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700428 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700429 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800430 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700431 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
432 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700433 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700434 /* allocate buffer */
435 const size_t required_conversion_buffer_size =
436 bytes * num_device_channels / num_req_channels;
437 if (required_conversion_buffer_size > out->conversion_buffer_size) {
438 out->conversion_buffer_size = required_conversion_buffer_size;
439 out->conversion_buffer = realloc(out->conversion_buffer,
440 out->conversion_buffer_size);
441 }
442 /* convert data */
443 const audio_format_t audio_format = out_get_format(&(out->stream.common));
444 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800445 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700446 adjust_channels(write_buff, num_req_channels,
447 out->conversion_buffer, num_device_channels,
448 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800449 write_buff = out->conversion_buffer;
450 }
451
Paul McLeaneedc92e2013-12-19 15:46:15 -0800452 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700453 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800454 }
Simon Wilson19957a32012-04-06 16:17:12 -0700455
456 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700457
458 return bytes;
459
460err:
461 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700462 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700463 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700464 out_get_sample_rate(&stream->common));
465 }
466
467 return bytes;
468}
469
Paul McLean30f41852014-04-16 15:44:20 -0700470static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700471{
472 return -EINVAL;
473}
474
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700475static int out_get_presentation_position(const struct audio_stream_out *stream,
476 uint64_t *frames, struct timespec *timestamp)
477{
478 /* FIXME - This needs to be implemented */
479 return -EINVAL;
480}
481
Simon Wilson19957a32012-04-06 16:17:12 -0700482static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
483{
484 return 0;
485}
486
487static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
488{
489 return 0;
490}
491
Paul McLean30f41852014-04-16 15:44:20 -0700492static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700493{
494 return -EINVAL;
495}
496
497static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700498 audio_io_handle_t handle,
499 audio_devices_t devices,
500 audio_output_flags_t flags,
501 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700502 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700503 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700504{
Paul McLean0f1753e2014-12-02 12:36:45 -0700505 ALOGV("usb:audio_hw::out adev_open_output_stream()"
506 "handle:0x%X, device:0x%X, flags:0x%X, addr:%s",
507 handle, devices, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800508
Simon Wilson19957a32012-04-06 16:17:12 -0700509 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800510
Simon Wilson19957a32012-04-06 16:17:12 -0700511 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700512
513 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
514 if (!out)
515 return -ENOMEM;
516
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700517 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700518 out->stream.common.get_sample_rate = out_get_sample_rate;
519 out->stream.common.set_sample_rate = out_set_sample_rate;
520 out->stream.common.get_buffer_size = out_get_buffer_size;
521 out->stream.common.get_channels = out_get_channels;
522 out->stream.common.get_format = out_get_format;
523 out->stream.common.set_format = out_set_format;
524 out->stream.common.standby = out_standby;
525 out->stream.common.dump = out_dump;
526 out->stream.common.set_parameters = out_set_parameters;
527 out->stream.common.get_parameters = out_get_parameters;
528 out->stream.common.add_audio_effect = out_add_audio_effect;
529 out->stream.common.remove_audio_effect = out_remove_audio_effect;
530 out->stream.get_latency = out_get_latency;
531 out->stream.set_volume = out_set_volume;
532 out->stream.write = out_write;
533 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700534 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700535 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
536
537 out->dev = adev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700538 pthread_mutex_lock(&adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700539 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700540
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700541 // build this to hand to the alsa_device_proxy
542 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700543 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800544
Paul McLean0f1753e2014-12-02 12:36:45 -0700545 /* Pull out the card/device pair */
546 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
547
548 profile_read_device_info(out->profile);
549
550 pthread_mutex_unlock(&adev->lock);
551
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700552 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800553
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700554 /* Rate */
555 if (config->sample_rate == 0) {
556 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
557 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
558 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800559 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700560 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
561 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800562 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800563
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700564 /* Format */
565 if (config->format == AUDIO_FORMAT_DEFAULT) {
566 proxy_config.format = profile_get_default_format(out->profile);
567 config->format = audio_format_from_pcm_format(proxy_config.format);
568 } else {
569 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
570 if (profile_is_format_valid(out->profile, fmt)) {
571 proxy_config.format = fmt;
572 } else {
573 proxy_config.format = profile_get_default_format(out->profile);
574 config->format = audio_format_from_pcm_format(proxy_config.format);
575 ret = -EINVAL;
576 }
577 }
578
579 /* Channels */
Andy Hung03576be2014-07-21 21:16:45 -0700580 unsigned proposed_channel_count = profile_get_default_channel_count(out->profile);
581 if (k_force_channels) {
582 proposed_channel_count = k_force_channels;
583 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
584 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700585 }
Andy Hung03576be2014-07-21 21:16:45 -0700586 /* we can expose any channel count mask, and emulate internally. */
587 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
588 out->hal_channel_count = proposed_channel_count;
589 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
590 * and we emulate any channel count discrepancies in out_write(). */
591 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700592
593 proxy_prepare(&out->proxy, out->profile, &proxy_config);
594
595 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
596 ret = 0;
597
Paul McLeaneedc92e2013-12-19 15:46:15 -0800598 out->conversion_buffer = NULL;
599 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700600
601 out->standby = true;
602
603 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700604
605 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700606
607err_open:
608 free(out);
609 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800610 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700611}
612
613static void adev_close_output_stream(struct audio_hw_device *dev,
614 struct audio_stream_out *stream)
615{
616 struct stream_out *out = (struct stream_out *)stream;
Paul McLean0f1753e2014-12-02 12:36:45 -0700617 ALOGV("usb:audio_hw::out adev_close_output_stream(c:%d d:%d)",
618 out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700619
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700620 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700621 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800622
623 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700624
Paul McLeaneedc92e2013-12-19 15:46:15 -0800625 out->conversion_buffer = NULL;
626 out->conversion_buffer_size = 0;
627
Simon Wilson19957a32012-04-06 16:17:12 -0700628 free(stream);
629}
630
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700631static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
632 const struct audio_config *config)
633{
634 /* TODO This needs to be calculated based on format/channels/rate */
635 return 320;
636}
637
638/*
639 * IN functions
640 */
641static uint32_t in_get_sample_rate(const struct audio_stream *stream)
642{
643 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
644 ALOGV("in_get_sample_rate() = %d", rate);
645 return rate;
646}
647
648static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
649{
650 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
651 return -ENOSYS;
652}
653
654static size_t in_get_buffer_size(const struct audio_stream *stream)
655{
656 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700657 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700658}
659
660static uint32_t in_get_channels(const struct audio_stream *stream)
661{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700662 const struct stream_in *in = (const struct stream_in*)stream;
663 return audio_channel_in_mask_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700664}
665
666static audio_format_t in_get_format(const struct audio_stream *stream)
667{
668 /* TODO Here is the code we need when we support arbitrary input formats
669 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
670 * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
671 * ALOGV("in_get_format() = %d", format);
672 * return format;
673 */
674 /* Input only supports PCM16 */
675 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
676 rewrite this to return the ACTUAL channel format (above) */
677 return AUDIO_FORMAT_PCM_16_BIT;
678}
679
680static int in_set_format(struct audio_stream *stream, audio_format_t format)
681{
682 ALOGV("in_set_format(%d) - NOPE", format);
683
684 return -ENOSYS;
685}
686
687static int in_standby(struct audio_stream *stream)
688{
689 struct stream_in *in = (struct stream_in *)stream;
690
691 pthread_mutex_lock(&in->dev->lock);
692 pthread_mutex_lock(&in->lock);
693
694 if (!in->standby) {
695 proxy_close(&in->proxy);
696 in->standby = true;
697 }
698
699 pthread_mutex_unlock(&in->lock);
700 pthread_mutex_unlock(&in->dev->lock);
701
702 return 0;
703}
704
705static int in_dump(const struct audio_stream *stream, int fd)
706{
707 return 0;
708}
709
710static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
711{
712 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
713
714 struct stream_in *in = (struct stream_in *)stream;
715
716 char value[32];
717 int param_val;
718 int routing = 0;
719 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700720 int card = -1;
721 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700722
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700723 pthread_mutex_lock(&in->dev->lock);
724 pthread_mutex_lock(&in->lock);
725
Paul McLean0f1753e2014-12-02 12:36:45 -0700726 parse_card_device_params(kvpairs, &card, &device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700727
Paul McLean2c6196f2014-08-20 16:50:25 -0700728 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700729 /* cannot read pcm device info if playback is active */
730 if (!in->standby)
731 ret_value = -ENOSYS;
732 else {
733 int saved_card = in->profile->card;
734 int saved_device = in->profile->device;
735 in->profile->card = card;
736 in->profile->device = device;
737 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
738 if (ret_value != 0) {
739 in->profile->card = saved_card;
740 in->profile->device = saved_device;
741 }
742 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700743 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700744
745 pthread_mutex_unlock(&in->lock);
746 pthread_mutex_unlock(&in->dev->lock);
747
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700748 return ret_value;
749}
750
751static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
752{
753 struct stream_in *in = (struct stream_in *)stream;
754
755 pthread_mutex_lock(&in->dev->lock);
756 pthread_mutex_lock(&in->lock);
757
758 char * params_str = device_get_parameters(in->profile, keys);
759
760 pthread_mutex_unlock(&in->lock);
761 pthread_mutex_unlock(&in->dev->lock);
762
763 return params_str;
764}
765
766static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
767{
768 return 0;
769}
770
771static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
772{
773 return 0;
774}
775
776static int in_set_gain(struct audio_stream_in *stream, float gain)
777{
778 return 0;
779}
780
781/* must be called with hw device and output stream mutexes locked */
782static int start_input_stream(struct stream_in *in)
783{
784 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
785 in->profile->card, in->profile->device);
786
787 return proxy_open(&in->proxy);
788}
789
790/* TODO mutex stuff here (see out_write) */
791static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
792{
793 size_t num_read_buff_bytes = 0;
794 void * read_buff = buffer;
795 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800796 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700797
798 struct stream_in * in = (struct stream_in *)stream;
799
800 pthread_mutex_lock(&in->dev->lock);
801 pthread_mutex_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700802 if (in->standby) {
803 if (start_input_stream(in) != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700804 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700805 goto err;
806 }
807 in->standby = false;
808 }
Eric Laurent05333d42014-08-04 20:29:17 -0700809 pthread_mutex_unlock(&in->dev->lock);
810
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700811
812 alsa_device_profile * profile = in->profile;
813
814 /*
815 * OK, we need to figure out how much data to read to be able to output the requested
816 * number of bytes in the HAL format (16-bit, stereo).
817 */
818 num_read_buff_bytes = bytes;
819 int num_device_channels = proxy_get_channel_count(&in->proxy);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700820 int num_req_channels = in->hal_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700821
822 if (num_device_channels != num_req_channels) {
823 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
824 }
825
826 enum pcm_format format = proxy_get_format(&in->proxy);
827 if (format == PCM_FORMAT_S24_3LE) {
828 /* 24-bit USB device */
829 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
830 } else if (format == PCM_FORMAT_S32_LE) {
831 /* 32-bit USB device */
832 num_read_buff_bytes = num_read_buff_bytes * 2;
833 }
834
835 /* Setup/Realloc the conversion buffer (if necessary). */
836 if (num_read_buff_bytes != bytes) {
837 if (num_read_buff_bytes > in->conversion_buffer_size) {
838 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
839 (and do these conversions themselves) */
840 in->conversion_buffer_size = num_read_buff_bytes;
841 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
842 }
843 read_buff = in->conversion_buffer;
844 }
845
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800846 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
847 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700848 /*
849 * Do any conversions necessary to send the data in the format specified to/by the HAL
850 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
851 */
852 if (format != PCM_FORMAT_S16_LE) {
853 /* we need to convert */
854 if (num_device_channels != num_req_channels) {
855 out_buff = read_buff;
856 }
857
858 if (format == PCM_FORMAT_S24_3LE) {
859 num_read_buff_bytes =
860 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
861 } else if (format == PCM_FORMAT_S32_LE) {
862 num_read_buff_bytes =
863 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
864 } else {
Viswanath L8c7e1112014-10-31 13:07:39 +0530865 LOG_ALWAYS_FATAL("Unsupported format");
866 num_read_buff_bytes = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700867 goto err;
868 }
869 }
870
871 if (num_device_channels != num_req_channels) {
872 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
873
874 out_buff = buffer;
875 /* Num Channels conversion */
876 if (num_device_channels != num_req_channels) {
877 audio_format_t audio_format = in_get_format(&(in->stream.common));
878 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
879
880 num_read_buff_bytes =
881 adjust_channels(read_buff, num_device_channels,
882 out_buff, num_req_channels,
883 sample_size_in_bytes, num_read_buff_bytes);
884 }
885 }
Eric Laurent253def92014-09-14 12:18:18 -0700886
887 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
888 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
889 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530890 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800891 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700892 }
893
894err:
895 pthread_mutex_unlock(&in->lock);
896
897 return num_read_buff_bytes;
898}
899
900static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
901{
902 return 0;
903}
904
905static int adev_open_input_stream(struct audio_hw_device *dev,
906 audio_io_handle_t handle,
907 audio_devices_t devices,
908 struct audio_config *config,
909 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700910 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700911 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700912 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700913{
914 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
915 config->sample_rate, config->channel_mask, config->format);
916
917 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
918 int ret = 0;
919
920 if (in == NULL)
921 return -ENOMEM;
922
923 /* setup function pointers */
924 in->stream.common.get_sample_rate = in_get_sample_rate;
925 in->stream.common.set_sample_rate = in_set_sample_rate;
926 in->stream.common.get_buffer_size = in_get_buffer_size;
927 in->stream.common.get_channels = in_get_channels;
928 in->stream.common.get_format = in_get_format;
929 in->stream.common.set_format = in_set_format;
930 in->stream.common.standby = in_standby;
931 in->stream.common.dump = in_dump;
932 in->stream.common.set_parameters = in_set_parameters;
933 in->stream.common.get_parameters = in_get_parameters;
934 in->stream.common.add_audio_effect = in_add_audio_effect;
935 in->stream.common.remove_audio_effect = in_remove_audio_effect;
936
937 in->stream.set_gain = in_set_gain;
938 in->stream.read = in_read;
939 in->stream.get_input_frames_lost = in_get_input_frames_lost;
940
941 in->dev = (struct audio_device *)dev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700942 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700943
944 in->profile = &in->dev->in_profile;
945
946 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700947 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948
Paul McLean0f1753e2014-12-02 12:36:45 -0700949 /* Pull out the card/device pair */
950 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
951
952 profile_read_device_info(in->profile);
953 pthread_mutex_unlock(&in->dev->lock);
954
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700955 /* Rate */
956 if (config->sample_rate == 0) {
957 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
958 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
959 proxy_config.rate = config->sample_rate;
960 } else {
961 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
962 ret = -EINVAL;
963 }
964
965 /* Format */
966 /* until the framework supports format conversion, just take what it asks for
967 * i.e. AUDIO_FORMAT_PCM_16_BIT */
968 if (config->format == AUDIO_FORMAT_DEFAULT) {
969 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
970 * formats */
971 config->format = AUDIO_FORMAT_PCM_16_BIT;
972 proxy_config.format = PCM_FORMAT_S16_LE;
973 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
974 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
975 * formats */
976 proxy_config.format = PCM_FORMAT_S16_LE;
977 } else {
978 /* When the framework support other formats, validate here */
979 config->format = AUDIO_FORMAT_PCM_16_BIT;
980 proxy_config.format = PCM_FORMAT_S16_LE;
981 ret = -EINVAL;
982 }
983
Paul McLean2cfd81b2014-09-15 12:32:23 -0700984 /* Channels */
985 unsigned proposed_channel_count = profile_get_default_channel_count(in->profile);
986 if (k_force_channels) {
987 proposed_channel_count = k_force_channels;
988 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
989 proposed_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700990 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991
Paul McLean2cfd81b2014-09-15 12:32:23 -0700992 /* we can expose any channel count mask, and emulate internally. */
993 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
994 in->hal_channel_count = proposed_channel_count;
995 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700996 proxy_prepare(&in->proxy, in->profile, &proxy_config);
997
998 in->standby = true;
999
1000 in->conversion_buffer = NULL;
1001 in->conversion_buffer_size = 0;
1002
1003 *stream_in = &in->stream;
1004
1005 return ret;
1006}
1007
1008static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
1009{
1010 struct stream_in *in = (struct stream_in *)stream;
1011
1012 /* Close the pcm device */
1013 in_standby(&stream->common);
1014
1015 free(in->conversion_buffer);
1016
1017 free(stream);
1018}
1019
1020/*
1021 * ADEV Functions
1022 */
Simon Wilson19957a32012-04-06 16:17:12 -07001023static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1024{
Paul McLean2c6196f2014-08-20 16:50:25 -07001025 ALOGV("audio_hw:usb adev_set_parameters(%s)", kvpairs);
1026
1027 struct audio_device * adev = (struct audio_device *)dev;
1028
1029 char value[32];
1030 int param_val;
1031
1032 struct str_parms * parms = str_parms_create_str(kvpairs);
1033
1034 /* Check for the "disconnect" message */
1035 param_val = str_parms_get_str(parms, "disconnect", value, sizeof(value));
1036 if (param_val >= 0) {
1037 audio_devices_t device = (audio_devices_t)atoi(value);
1038
1039 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1040 int alsa_card = param_val >= 0 ? atoi(value) : -1;
1041
1042 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1043 int alsa_device = param_val >= 0 ? atoi(value) : -1;
1044
1045 if (alsa_card >= 0 && alsa_device >= 0) {
1046 /* "decache" the profile */
1047 pthread_mutex_lock(&adev->lock);
1048 if (device == AUDIO_DEVICE_OUT_USB_DEVICE &&
1049 profile_is_cached_for(&adev->out_profile, alsa_card, alsa_device)) {
1050 profile_decache(&adev->out_profile);
1051 }
1052 if (device == AUDIO_DEVICE_IN_USB_DEVICE &&
1053 profile_is_cached_for(&adev->in_profile, alsa_card, alsa_device)) {
1054 profile_decache(&adev->in_profile);
1055 }
1056 pthread_mutex_unlock(&adev->lock);
1057 }
1058 }
1059
soon1.choic3571572014-12-18 16:03:43 +09001060 str_parms_destroy(parms);
1061
Simon Wilson19957a32012-04-06 16:17:12 -07001062 return 0;
1063}
1064
Paul McLean30f41852014-04-16 15:44:20 -07001065static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001066{
1067 return strdup("");
1068}
1069
1070static int adev_init_check(const struct audio_hw_device *dev)
1071{
1072 return 0;
1073}
1074
1075static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1076{
1077 return -ENOSYS;
1078}
1079
1080static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1081{
1082 return -ENOSYS;
1083}
1084
1085static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1086{
1087 return 0;
1088}
1089
1090static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1091{
Eric Laurent253def92014-09-14 12:18:18 -07001092 struct audio_device * adev = (struct audio_device *)dev;
1093 pthread_mutex_lock(&adev->lock);
1094 adev->mic_muted = state;
1095 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001096 return -ENOSYS;
1097}
1098
1099static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1100{
1101 return -ENOSYS;
1102}
1103
Simon Wilson19957a32012-04-06 16:17:12 -07001104static int adev_dump(const audio_hw_device_t *device, int fd)
1105{
1106 return 0;
1107}
1108
1109static int adev_close(hw_device_t *device)
1110{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001111 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001112 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001113
Simon Wilson19957a32012-04-06 16:17:12 -07001114 return 0;
1115}
1116
Paul McLean30f41852014-04-16 15:44:20 -07001117static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001118{
Simon Wilson19957a32012-04-06 16:17:12 -07001119 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1120 return -EINVAL;
1121
Paul McLeaneedc92e2013-12-19 15:46:15 -08001122 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001123 if (!adev)
1124 return -ENOMEM;
1125
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001126 profile_init(&adev->out_profile, PCM_OUT);
1127 profile_init(&adev->in_profile, PCM_IN);
1128
Simon Wilson19957a32012-04-06 16:17:12 -07001129 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001130 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001131 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001132 adev->hw_device.common.close = adev_close;
1133
Simon Wilson19957a32012-04-06 16:17:12 -07001134 adev->hw_device.init_check = adev_init_check;
1135 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1136 adev->hw_device.set_master_volume = adev_set_master_volume;
1137 adev->hw_device.set_mode = adev_set_mode;
1138 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1139 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1140 adev->hw_device.set_parameters = adev_set_parameters;
1141 adev->hw_device.get_parameters = adev_get_parameters;
1142 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1143 adev->hw_device.open_output_stream = adev_open_output_stream;
1144 adev->hw_device.close_output_stream = adev_close_output_stream;
1145 adev->hw_device.open_input_stream = adev_open_input_stream;
1146 adev->hw_device.close_input_stream = adev_close_input_stream;
1147 adev->hw_device.dump = adev_dump;
1148
1149 *device = &adev->hw_device.common;
1150
1151 return 0;
1152}
1153
1154static struct hw_module_methods_t hal_module_methods = {
1155 .open = adev_open,
1156};
1157
1158struct audio_module HAL_MODULE_INFO_SYM = {
1159 .common = {
1160 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001161 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1162 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001163 .id = AUDIO_HARDWARE_MODULE_ID,
1164 .name = "USB audio HW HAL",
1165 .author = "The Android Open Source Project",
1166 .methods = &hal_module_methods,
1167 },
1168};