blob: 034640820c4b8777271d87a7c6c7952537cbe319 [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
17#define LOG_TAG "usb_audio_hw"
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"
54#include "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 McLeanc88e6ae2014-07-16 09:48:34 -0700187static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800188{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700189 ALOGV("usb:audio_hw::device_get_parameters() keys:%s", keys);
Paul McLeane32cbc12014-06-25 10:42:07 -0700190
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700191 if (profile->card < 0 || profile->device < 0) {
192 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800193 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700194
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700195 struct str_parms *query = str_parms_create_str(keys);
196 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700197
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700198 /* These keys are from hardware/libhardware/include/audio.h */
199 /* supported sample rates */
200 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
201 char* rates_list = profile_get_sample_rate_strs(profile);
202 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
203 rates_list);
204 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700205 }
206
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700207 /* supported channel counts */
208 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
209 char* channels_list = profile_get_channel_count_strs(profile);
210 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
211 channels_list);
212 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700213 }
214
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700215 /* supported sample formats */
216 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
217 char * format_params = profile_get_format_strs(profile);
218 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
219 format_params);
220 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700221 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700222 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700223
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700224 char* result_str = str_parms_to_str(result);
225 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700226
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700227 ALOGV("usb:audio_hw::device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700228
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700229 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800230}
231
232/*
233 * HAl Functions
234 */
Simon Wilson19957a32012-04-06 16:17:12 -0700235/**
236 * NOTE: when multiple mutexes have to be acquired, always respect the
237 * following order: hw device > out stream
238 */
239
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700240/*
241 * OUT functions
242 */
Simon Wilson19957a32012-04-06 16:17:12 -0700243static uint32_t out_get_sample_rate(const struct audio_stream *stream)
244{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700245 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
246 ALOGV("out_get_sample_rate() = %d", rate);
247 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700248}
249
250static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
251{
252 return 0;
253}
254
255static size_t out_get_buffer_size(const struct audio_stream *stream)
256{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700257 const struct stream_out* out = (const struct stream_out*)stream;
258 size_t buffer_size =
259 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700260 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700261}
262
263static uint32_t out_get_channels(const struct audio_stream *stream)
264{
Andy Hung03576be2014-07-21 21:16:45 -0700265 const struct stream_out *out = (const struct stream_out*)stream;
266 return audio_channel_out_mask_from_count(out->hal_channel_count);
Simon Wilson19957a32012-04-06 16:17:12 -0700267}
268
269static audio_format_t out_get_format(const struct audio_stream *stream)
270{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700271 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
272 * Relies on the framework to provide data in the specified format.
273 * This could change in the future.
274 */
275 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
276 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700277 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700278}
279
280static int out_set_format(struct audio_stream *stream, audio_format_t format)
281{
282 return 0;
283}
284
285static int out_standby(struct audio_stream *stream)
286{
287 struct stream_out *out = (struct stream_out *)stream;
288
289 pthread_mutex_lock(&out->dev->lock);
290 pthread_mutex_lock(&out->lock);
291
292 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700293 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700294 out->standby = true;
295 }
296
297 pthread_mutex_unlock(&out->lock);
298 pthread_mutex_unlock(&out->dev->lock);
299
300 return 0;
301}
302
303static int out_dump(const struct audio_stream *stream, int fd)
304{
305 return 0;
306}
307
308static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
309{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800310 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
311
Simon Wilson19957a32012-04-06 16:17:12 -0700312 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700313
Simon Wilson19957a32012-04-06 16:17:12 -0700314 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800315 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700316 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800317 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700318 int card = -1;
319 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700320
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700321 struct str_parms * parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700322 pthread_mutex_lock(&out->dev->lock);
323 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700324
Paul McLeaneedc92e2013-12-19 15:46:15 -0800325 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700326 if (param_val >= 0)
327 card = atoi(value);
Simon Wilson19957a32012-04-06 16:17:12 -0700328
Paul McLeaneedc92e2013-12-19 15:46:15 -0800329 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700330 if (param_val >= 0)
331 device = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800332
Paul McLean2c6196f2014-08-20 16:50:25 -0700333 if (card >= 0 && device >= 0 && !profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700334 /* cannot read pcm device info if playback is active */
335 if (!out->standby)
336 ret_value = -ENOSYS;
337 else {
338 int saved_card = out->profile->card;
339 int saved_device = out->profile->device;
340 out->profile->card = card;
341 out->profile->device = device;
342 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
343 if (ret_value != 0) {
344 out->profile->card = saved_card;
345 out->profile->device = saved_device;
346 }
347 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800348 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700349
Paul McLeanf62d75e2014-07-11 15:14:19 -0700350 pthread_mutex_unlock(&out->lock);
351 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700352 str_parms_destroy(parms);
353
Paul McLeaneedc92e2013-12-19 15:46:15 -0800354 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700355}
356
Paul McLeanf62d75e2014-07-11 15:14:19 -0700357static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
358{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700359 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700360 pthread_mutex_lock(&out->dev->lock);
361 pthread_mutex_lock(&out->lock);
362
363 char * params_str = device_get_parameters(out->profile, keys);
364
365 pthread_mutex_unlock(&out->lock);
366 pthread_mutex_unlock(&out->dev->lock);
367
368 return params_str;
369}
370
Simon Wilson19957a32012-04-06 16:17:12 -0700371static uint32_t out_get_latency(const struct audio_stream_out *stream)
372{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700373 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
374 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700375}
376
Paul McLean30f41852014-04-16 15:44:20 -0700377static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700378{
379 return -ENOSYS;
380}
381
Paul McLeaneedc92e2013-12-19 15:46:15 -0800382/* must be called with hw device and output stream mutexes locked */
383static int start_output_stream(struct stream_out *out)
384{
Paul McLean30f41852014-04-16 15:44:20 -0700385 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -0700386 out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800387
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700388 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800389}
390
391static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700392{
393 int ret;
394 struct stream_out *out = (struct stream_out *)stream;
395
396 pthread_mutex_lock(&out->dev->lock);
397 pthread_mutex_lock(&out->lock);
398 if (out->standby) {
399 ret = start_output_stream(out);
400 if (ret != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700401 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700402 goto err;
403 }
404 out->standby = false;
405 }
Eric Laurent05333d42014-08-04 20:29:17 -0700406 pthread_mutex_unlock(&out->dev->lock);
407
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700408 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700409 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800410 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700411 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
412 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700413 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700414 /* allocate buffer */
415 const size_t required_conversion_buffer_size =
416 bytes * num_device_channels / num_req_channels;
417 if (required_conversion_buffer_size > out->conversion_buffer_size) {
418 out->conversion_buffer_size = required_conversion_buffer_size;
419 out->conversion_buffer = realloc(out->conversion_buffer,
420 out->conversion_buffer_size);
421 }
422 /* convert data */
423 const audio_format_t audio_format = out_get_format(&(out->stream.common));
424 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800425 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700426 adjust_channels(write_buff, num_req_channels,
427 out->conversion_buffer, num_device_channels,
428 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800429 write_buff = out->conversion_buffer;
430 }
431
Paul McLeaneedc92e2013-12-19 15:46:15 -0800432 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700433 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800434 }
Simon Wilson19957a32012-04-06 16:17:12 -0700435
436 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700437
438 return bytes;
439
440err:
441 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700442 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700443 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700444 out_get_sample_rate(&stream->common));
445 }
446
447 return bytes;
448}
449
Paul McLean30f41852014-04-16 15:44:20 -0700450static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700451{
452 return -EINVAL;
453}
454
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700455static int out_get_presentation_position(const struct audio_stream_out *stream,
456 uint64_t *frames, struct timespec *timestamp)
457{
458 /* FIXME - This needs to be implemented */
459 return -EINVAL;
460}
461
Simon Wilson19957a32012-04-06 16:17:12 -0700462static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
463{
464 return 0;
465}
466
467static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
468{
469 return 0;
470}
471
Paul McLean30f41852014-04-16 15:44:20 -0700472static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700473{
474 return -EINVAL;
475}
476
477static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700478 audio_io_handle_t handle,
479 audio_devices_t devices,
480 audio_output_flags_t flags,
481 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700482 struct audio_stream_out **stream_out,
483 const char *address __unused)
Simon Wilson19957a32012-04-06 16:17:12 -0700484{
Paul McLean30f41852014-04-16 15:44:20 -0700485 ALOGV("usb:audio_hw::out adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X",
Paul McLeaneedc92e2013-12-19 15:46:15 -0800486 handle, devices, flags);
487
Simon Wilson19957a32012-04-06 16:17:12 -0700488 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800489
Simon Wilson19957a32012-04-06 16:17:12 -0700490 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700491
492 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
493 if (!out)
494 return -ENOMEM;
495
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700496 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700497 out->stream.common.get_sample_rate = out_get_sample_rate;
498 out->stream.common.set_sample_rate = out_set_sample_rate;
499 out->stream.common.get_buffer_size = out_get_buffer_size;
500 out->stream.common.get_channels = out_get_channels;
501 out->stream.common.get_format = out_get_format;
502 out->stream.common.set_format = out_set_format;
503 out->stream.common.standby = out_standby;
504 out->stream.common.dump = out_dump;
505 out->stream.common.set_parameters = out_set_parameters;
506 out->stream.common.get_parameters = out_get_parameters;
507 out->stream.common.add_audio_effect = out_add_audio_effect;
508 out->stream.common.remove_audio_effect = out_remove_audio_effect;
509 out->stream.get_latency = out_get_latency;
510 out->stream.set_volume = out_set_volume;
511 out->stream.write = out_write;
512 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700513 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700514 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
515
516 out->dev = adev;
517
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700518 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700519
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700520 // build this to hand to the alsa_device_proxy
521 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700522 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800523
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700524 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800525
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700526 /* Rate */
527 if (config->sample_rate == 0) {
528 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
529 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
530 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800531 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700532 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
533 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800534 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800535
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700536 /* Format */
537 if (config->format == AUDIO_FORMAT_DEFAULT) {
538 proxy_config.format = profile_get_default_format(out->profile);
539 config->format = audio_format_from_pcm_format(proxy_config.format);
540 } else {
541 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
542 if (profile_is_format_valid(out->profile, fmt)) {
543 proxy_config.format = fmt;
544 } else {
545 proxy_config.format = profile_get_default_format(out->profile);
546 config->format = audio_format_from_pcm_format(proxy_config.format);
547 ret = -EINVAL;
548 }
549 }
550
551 /* Channels */
Andy Hung03576be2014-07-21 21:16:45 -0700552 unsigned proposed_channel_count = profile_get_default_channel_count(out->profile);
553 if (k_force_channels) {
554 proposed_channel_count = k_force_channels;
555 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
556 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700557 }
Andy Hung03576be2014-07-21 21:16:45 -0700558 /* we can expose any channel count mask, and emulate internally. */
559 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
560 out->hal_channel_count = proposed_channel_count;
561 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
562 * and we emulate any channel count discrepancies in out_write(). */
563 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700564
565 proxy_prepare(&out->proxy, out->profile, &proxy_config);
566
567 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
568 ret = 0;
569
Paul McLeaneedc92e2013-12-19 15:46:15 -0800570 out->conversion_buffer = NULL;
571 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700572
573 out->standby = true;
574
575 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700576
577 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700578
579err_open:
580 free(out);
581 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800582 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700583}
584
585static void adev_close_output_stream(struct audio_hw_device *dev,
586 struct audio_stream_out *stream)
587{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800588 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700589 struct stream_out *out = (struct stream_out *)stream;
590
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700591 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700592 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800593
594 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700595
Paul McLeaneedc92e2013-12-19 15:46:15 -0800596 out->conversion_buffer = NULL;
597 out->conversion_buffer_size = 0;
598
Simon Wilson19957a32012-04-06 16:17:12 -0700599 free(stream);
600}
601
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700602static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
603 const struct audio_config *config)
604{
605 /* TODO This needs to be calculated based on format/channels/rate */
606 return 320;
607}
608
609/*
610 * IN functions
611 */
612static uint32_t in_get_sample_rate(const struct audio_stream *stream)
613{
614 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
615 ALOGV("in_get_sample_rate() = %d", rate);
616 return rate;
617}
618
619static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
620{
621 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
622 return -ENOSYS;
623}
624
625static size_t in_get_buffer_size(const struct audio_stream *stream)
626{
627 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700628 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700629}
630
631static uint32_t in_get_channels(const struct audio_stream *stream)
632{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700633 const struct stream_in *in = (const struct stream_in*)stream;
634 return audio_channel_in_mask_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700635}
636
637static audio_format_t in_get_format(const struct audio_stream *stream)
638{
639 /* TODO Here is the code we need when we support arbitrary input formats
640 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
641 * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
642 * ALOGV("in_get_format() = %d", format);
643 * return format;
644 */
645 /* Input only supports PCM16 */
646 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
647 rewrite this to return the ACTUAL channel format (above) */
648 return AUDIO_FORMAT_PCM_16_BIT;
649}
650
651static int in_set_format(struct audio_stream *stream, audio_format_t format)
652{
653 ALOGV("in_set_format(%d) - NOPE", format);
654
655 return -ENOSYS;
656}
657
658static int in_standby(struct audio_stream *stream)
659{
660 struct stream_in *in = (struct stream_in *)stream;
661
662 pthread_mutex_lock(&in->dev->lock);
663 pthread_mutex_lock(&in->lock);
664
665 if (!in->standby) {
666 proxy_close(&in->proxy);
667 in->standby = true;
668 }
669
670 pthread_mutex_unlock(&in->lock);
671 pthread_mutex_unlock(&in->dev->lock);
672
673 return 0;
674}
675
676static int in_dump(const struct audio_stream *stream, int fd)
677{
678 return 0;
679}
680
681static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
682{
683 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
684
685 struct stream_in *in = (struct stream_in *)stream;
686
687 char value[32];
688 int param_val;
689 int routing = 0;
690 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700691 int card = -1;
692 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700693
694 struct str_parms * parms = str_parms_create_str(kvpairs);
695
696 pthread_mutex_lock(&in->dev->lock);
697 pthread_mutex_lock(&in->lock);
698
Paul McLean2c6196f2014-08-20 16:50:25 -0700699 /* Device Connection Message ("card=1,device=0") */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700700 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700701 if (param_val >= 0)
702 card = atoi(value);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700703
704 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700705 if (param_val >= 0)
706 device = atoi(value);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700707
Paul McLean2c6196f2014-08-20 16:50:25 -0700708 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700709 /* cannot read pcm device info if playback is active */
710 if (!in->standby)
711 ret_value = -ENOSYS;
712 else {
713 int saved_card = in->profile->card;
714 int saved_device = in->profile->device;
715 in->profile->card = card;
716 in->profile->device = device;
717 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
718 if (ret_value != 0) {
719 in->profile->card = saved_card;
720 in->profile->device = saved_device;
721 }
722 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700723 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700724
725 pthread_mutex_unlock(&in->lock);
726 pthread_mutex_unlock(&in->dev->lock);
727
728 str_parms_destroy(parms);
729
730 return ret_value;
731}
732
733static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
734{
735 struct stream_in *in = (struct stream_in *)stream;
736
737 pthread_mutex_lock(&in->dev->lock);
738 pthread_mutex_lock(&in->lock);
739
740 char * params_str = device_get_parameters(in->profile, keys);
741
742 pthread_mutex_unlock(&in->lock);
743 pthread_mutex_unlock(&in->dev->lock);
744
745 return params_str;
746}
747
748static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
749{
750 return 0;
751}
752
753static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
754{
755 return 0;
756}
757
758static int in_set_gain(struct audio_stream_in *stream, float gain)
759{
760 return 0;
761}
762
763/* must be called with hw device and output stream mutexes locked */
764static int start_input_stream(struct stream_in *in)
765{
766 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
767 in->profile->card, in->profile->device);
768
769 return proxy_open(&in->proxy);
770}
771
772/* TODO mutex stuff here (see out_write) */
773static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
774{
775 size_t num_read_buff_bytes = 0;
776 void * read_buff = buffer;
777 void * out_buff = buffer;
778
779 struct stream_in * in = (struct stream_in *)stream;
780
781 pthread_mutex_lock(&in->dev->lock);
782 pthread_mutex_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700783 if (in->standby) {
784 if (start_input_stream(in) != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700785 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700786 goto err;
787 }
788 in->standby = false;
789 }
Eric Laurent05333d42014-08-04 20:29:17 -0700790 pthread_mutex_unlock(&in->dev->lock);
791
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700792
793 alsa_device_profile * profile = in->profile;
794
795 /*
796 * OK, we need to figure out how much data to read to be able to output the requested
797 * number of bytes in the HAL format (16-bit, stereo).
798 */
799 num_read_buff_bytes = bytes;
800 int num_device_channels = proxy_get_channel_count(&in->proxy);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700801 int num_req_channels = in->hal_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700802
803 if (num_device_channels != num_req_channels) {
804 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
805 }
806
807 enum pcm_format format = proxy_get_format(&in->proxy);
808 if (format == PCM_FORMAT_S24_3LE) {
809 /* 24-bit USB device */
810 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
811 } else if (format == PCM_FORMAT_S32_LE) {
812 /* 32-bit USB device */
813 num_read_buff_bytes = num_read_buff_bytes * 2;
814 }
815
816 /* Setup/Realloc the conversion buffer (if necessary). */
817 if (num_read_buff_bytes != bytes) {
818 if (num_read_buff_bytes > in->conversion_buffer_size) {
819 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
820 (and do these conversions themselves) */
821 in->conversion_buffer_size = num_read_buff_bytes;
822 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
823 }
824 read_buff = in->conversion_buffer;
825 }
826
827 if (proxy_read(&in->proxy, read_buff, num_read_buff_bytes) == 0) {
828 /*
829 * Do any conversions necessary to send the data in the format specified to/by the HAL
830 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
831 */
832 if (format != PCM_FORMAT_S16_LE) {
833 /* we need to convert */
834 if (num_device_channels != num_req_channels) {
835 out_buff = read_buff;
836 }
837
838 if (format == PCM_FORMAT_S24_3LE) {
839 num_read_buff_bytes =
840 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
841 } else if (format == PCM_FORMAT_S32_LE) {
842 num_read_buff_bytes =
843 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
844 } else {
845 goto err;
846 }
847 }
848
849 if (num_device_channels != num_req_channels) {
850 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
851
852 out_buff = buffer;
853 /* Num Channels conversion */
854 if (num_device_channels != num_req_channels) {
855 audio_format_t audio_format = in_get_format(&(in->stream.common));
856 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
857
858 num_read_buff_bytes =
859 adjust_channels(read_buff, num_device_channels,
860 out_buff, num_req_channels,
861 sample_size_in_bytes, num_read_buff_bytes);
862 }
863 }
Eric Laurent253def92014-09-14 12:18:18 -0700864
865 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
866 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
867 memset(buffer, 0, num_read_buff_bytes);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700868 }
869
870err:
871 pthread_mutex_unlock(&in->lock);
872
873 return num_read_buff_bytes;
874}
875
876static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
877{
878 return 0;
879}
880
881static int adev_open_input_stream(struct audio_hw_device *dev,
882 audio_io_handle_t handle,
883 audio_devices_t devices,
884 struct audio_config *config,
885 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700886 audio_input_flags_t flags __unused,
887 const char *address __unused,
888 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700889{
890 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
891 config->sample_rate, config->channel_mask, config->format);
892
893 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
894 int ret = 0;
895
896 if (in == NULL)
897 return -ENOMEM;
898
899 /* setup function pointers */
900 in->stream.common.get_sample_rate = in_get_sample_rate;
901 in->stream.common.set_sample_rate = in_set_sample_rate;
902 in->stream.common.get_buffer_size = in_get_buffer_size;
903 in->stream.common.get_channels = in_get_channels;
904 in->stream.common.get_format = in_get_format;
905 in->stream.common.set_format = in_set_format;
906 in->stream.common.standby = in_standby;
907 in->stream.common.dump = in_dump;
908 in->stream.common.set_parameters = in_set_parameters;
909 in->stream.common.get_parameters = in_get_parameters;
910 in->stream.common.add_audio_effect = in_add_audio_effect;
911 in->stream.common.remove_audio_effect = in_remove_audio_effect;
912
913 in->stream.set_gain = in_set_gain;
914 in->stream.read = in_read;
915 in->stream.get_input_frames_lost = in_get_input_frames_lost;
916
917 in->dev = (struct audio_device *)dev;
918
919 in->profile = &in->dev->in_profile;
920
921 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700922 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700923
924 /* Rate */
925 if (config->sample_rate == 0) {
926 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
927 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
928 proxy_config.rate = config->sample_rate;
929 } else {
930 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
931 ret = -EINVAL;
932 }
933
934 /* Format */
935 /* until the framework supports format conversion, just take what it asks for
936 * i.e. AUDIO_FORMAT_PCM_16_BIT */
937 if (config->format == AUDIO_FORMAT_DEFAULT) {
938 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
939 * formats */
940 config->format = AUDIO_FORMAT_PCM_16_BIT;
941 proxy_config.format = PCM_FORMAT_S16_LE;
942 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
943 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
944 * formats */
945 proxy_config.format = PCM_FORMAT_S16_LE;
946 } else {
947 /* When the framework support other formats, validate here */
948 config->format = AUDIO_FORMAT_PCM_16_BIT;
949 proxy_config.format = PCM_FORMAT_S16_LE;
950 ret = -EINVAL;
951 }
952
Paul McLean2cfd81b2014-09-15 12:32:23 -0700953 /* Channels */
954 unsigned proposed_channel_count = profile_get_default_channel_count(in->profile);
955 if (k_force_channels) {
956 proposed_channel_count = k_force_channels;
957 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
958 proposed_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700959 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700960
Paul McLean2cfd81b2014-09-15 12:32:23 -0700961 /* we can expose any channel count mask, and emulate internally. */
962 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
963 in->hal_channel_count = proposed_channel_count;
964 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700965 proxy_prepare(&in->proxy, in->profile, &proxy_config);
966
967 in->standby = true;
968
969 in->conversion_buffer = NULL;
970 in->conversion_buffer_size = 0;
971
972 *stream_in = &in->stream;
973
974 return ret;
975}
976
977static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
978{
979 struct stream_in *in = (struct stream_in *)stream;
980
981 /* Close the pcm device */
982 in_standby(&stream->common);
983
984 free(in->conversion_buffer);
985
986 free(stream);
987}
988
989/*
990 * ADEV Functions
991 */
Simon Wilson19957a32012-04-06 16:17:12 -0700992static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
993{
Paul McLean2c6196f2014-08-20 16:50:25 -0700994 ALOGV("audio_hw:usb adev_set_parameters(%s)", kvpairs);
995
996 struct audio_device * adev = (struct audio_device *)dev;
997
998 char value[32];
999 int param_val;
1000
1001 struct str_parms * parms = str_parms_create_str(kvpairs);
1002
1003 /* Check for the "disconnect" message */
1004 param_val = str_parms_get_str(parms, "disconnect", value, sizeof(value));
1005 if (param_val >= 0) {
1006 audio_devices_t device = (audio_devices_t)atoi(value);
1007
1008 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1009 int alsa_card = param_val >= 0 ? atoi(value) : -1;
1010
1011 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1012 int alsa_device = param_val >= 0 ? atoi(value) : -1;
1013
1014 if (alsa_card >= 0 && alsa_device >= 0) {
1015 /* "decache" the profile */
1016 pthread_mutex_lock(&adev->lock);
1017 if (device == AUDIO_DEVICE_OUT_USB_DEVICE &&
1018 profile_is_cached_for(&adev->out_profile, alsa_card, alsa_device)) {
1019 profile_decache(&adev->out_profile);
1020 }
1021 if (device == AUDIO_DEVICE_IN_USB_DEVICE &&
1022 profile_is_cached_for(&adev->in_profile, alsa_card, alsa_device)) {
1023 profile_decache(&adev->in_profile);
1024 }
1025 pthread_mutex_unlock(&adev->lock);
1026 }
1027 }
1028
Simon Wilson19957a32012-04-06 16:17:12 -07001029 return 0;
1030}
1031
Paul McLean30f41852014-04-16 15:44:20 -07001032static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001033{
1034 return strdup("");
1035}
1036
1037static int adev_init_check(const struct audio_hw_device *dev)
1038{
1039 return 0;
1040}
1041
1042static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1043{
1044 return -ENOSYS;
1045}
1046
1047static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1048{
1049 return -ENOSYS;
1050}
1051
1052static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1053{
1054 return 0;
1055}
1056
1057static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1058{
Eric Laurent253def92014-09-14 12:18:18 -07001059 struct audio_device * adev = (struct audio_device *)dev;
1060 pthread_mutex_lock(&adev->lock);
1061 adev->mic_muted = state;
1062 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001063 return -ENOSYS;
1064}
1065
1066static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1067{
1068 return -ENOSYS;
1069}
1070
Simon Wilson19957a32012-04-06 16:17:12 -07001071static int adev_dump(const audio_hw_device_t *device, int fd)
1072{
1073 return 0;
1074}
1075
1076static int adev_close(hw_device_t *device)
1077{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001078 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001079 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001080
Simon Wilson19957a32012-04-06 16:17:12 -07001081 return 0;
1082}
1083
Paul McLean30f41852014-04-16 15:44:20 -07001084static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001085{
Simon Wilson19957a32012-04-06 16:17:12 -07001086 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1087 return -EINVAL;
1088
Paul McLeaneedc92e2013-12-19 15:46:15 -08001089 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001090 if (!adev)
1091 return -ENOMEM;
1092
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001093 profile_init(&adev->out_profile, PCM_OUT);
1094 profile_init(&adev->in_profile, PCM_IN);
1095
Simon Wilson19957a32012-04-06 16:17:12 -07001096 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001097 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001098 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001099 adev->hw_device.common.close = adev_close;
1100
Simon Wilson19957a32012-04-06 16:17:12 -07001101 adev->hw_device.init_check = adev_init_check;
1102 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1103 adev->hw_device.set_master_volume = adev_set_master_volume;
1104 adev->hw_device.set_mode = adev_set_mode;
1105 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1106 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1107 adev->hw_device.set_parameters = adev_set_parameters;
1108 adev->hw_device.get_parameters = adev_get_parameters;
1109 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1110 adev->hw_device.open_output_stream = adev_open_output_stream;
1111 adev->hw_device.close_output_stream = adev_close_output_stream;
1112 adev->hw_device.open_input_stream = adev_open_input_stream;
1113 adev->hw_device.close_input_stream = adev_close_input_stream;
1114 adev->hw_device.dump = adev_dump;
1115
1116 *device = &adev->hw_device.common;
1117
1118 return 0;
1119}
1120
1121static struct hw_module_methods_t hal_module_methods = {
1122 .open = adev_open,
1123};
1124
1125struct audio_module HAL_MODULE_INFO_SYM = {
1126 .common = {
1127 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001128 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1129 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001130 .id = AUDIO_HARDWARE_MODULE_ID,
1131 .name = "USB audio HW HAL",
1132 .author = "The Android Open Source Project",
1133 .methods = &hal_module_methods,
1134 },
1135};