blob: ef8bb0340d0ce4149f4b05416a0bf66b6e8f8d70 [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>
Tri Vo00a86732017-06-27 11:33:36 -070026#include <unistd.h>
Simon Wilson19957a32012-04-06 16:17:12 -070027
Mark Salyzyn88e458a2014-04-28 12:30:44 -070028#include <log/log.h>
Paul McLean6a75e4e2016-05-25 14:09:02 -060029#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070030#include <cutils/str_parms.h>
31#include <cutils/properties.h>
32
Simon Wilson19957a32012-04-06 16:17:12 -070033#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070034#include <hardware/audio_alsaops.h>
35#include <hardware/hardware.h>
36
37#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070038
39#include <tinyalsa/asoundlib.h>
40
Paul McLeanc2201152014-07-16 13:46:07 -070041#include <audio_utils/channels.h>
42
Paul McLeanc88e6ae2014-07-16 09:48:34 -070043#include "alsa_device_profile.h"
44#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080045#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070046
Paul McLeanc88e6ae2014-07-16 09:48:34 -070047#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070048
Paul McLean1d585cc2016-05-24 11:28:10 -060049/* Lock play & record samples rates at or above this threshold */
50#define RATELOCK_THRESHOLD 96000
51
Simon Wilson19957a32012-04-06 16:17:12 -070052struct audio_device {
53 struct audio_hw_device hw_device;
54
55 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080056
57 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070058 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060059 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080060
61 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070062 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060063 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080064
Paul McLean1d585cc2016-05-24 11:28:10 -060065 /* lock input & output sample rates */
66 /*FIXME - How do we address multiple output streams? */
67 uint32_t device_sample_rate;
68
Eric Laurent253def92014-09-14 12:18:18 -070069 bool mic_muted;
70
Simon Wilson19957a32012-04-06 16:17:12 -070071 bool standby;
Andy Hungb10ce6d2017-10-27 19:37:58 -070072
73 int32_t inputs_open; /* number of input streams currently open. */
Simon Wilson19957a32012-04-06 16:17:12 -070074};
75
Paul McLean994ac072016-06-02 15:33:24 -060076struct stream_lock {
77 pthread_mutex_t lock; /* see note below on mutex acquisition order */
78 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
79};
80
Simon Wilson19957a32012-04-06 16:17:12 -070081struct stream_out {
82 struct audio_stream_out stream;
83
Paul McLean994ac072016-06-02 15:33:24 -060084 struct stream_lock lock;
85
Paul McLeaneedc92e2013-12-19 15:46:15 -080086 bool standby;
87
Paul McLean76dba682016-06-01 12:29:11 -060088 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070089
Andy Hung4e6a1c02017-10-27 20:31:46 -070090 const alsa_device_profile *profile; /* Points to the alsa_device_profile in the audio_device.
91 * Const, so modifications go through adev->out_profile
92 * and thus should have the hardware lock and ensure
93 * stream is not active and no other open output streams.
94 */
95
Paul McLeanc88e6ae2014-07-16 09:48:34 -070096 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080097
Andy Hung03576be2014-07-21 21:16:45 -070098 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
99 * This may differ from the device channel count when
100 * the device is not compatible with AudioFlinger
101 * capabilities, e.g. exposes too many channels or
102 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600103 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
104 * so the proxy doesn't have a channel_mask, but
105 * audio HALs need to talk about channel masks
106 * so expose the one calculated by
107 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700108
Paul McLean6a75e4e2016-05-25 14:09:02 -0600109 struct listnode list_node;
110
Paul McLeaneedc92e2013-12-19 15:46:15 -0800111 void * conversion_buffer; /* any conversions are put into here
112 * they could come from here too if
113 * there was a previous conversion */
114 size_t conversion_buffer_size; /* in bytes */
115};
116
Paul McLeaneedc92e2013-12-19 15:46:15 -0800117struct stream_in {
118 struct audio_stream_in stream;
119
Paul McLean994ac072016-06-02 15:33:24 -0600120 struct stream_lock lock;
121
Simon Wilson19957a32012-04-06 16:17:12 -0700122 bool standby;
123
Paul McLean76dba682016-06-01 12:29:11 -0600124 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800125
Andy Hung4e6a1c02017-10-27 20:31:46 -0700126 const alsa_device_profile *profile; /* Points to the alsa_device_profile in the audio_device.
127 * Const, so modifications go through adev->out_profile
128 * and thus should have the hardware lock and ensure
129 * stream is not active and no other open input streams.
130 */
131
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700132 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700133
Paul McLean2cfd81b2014-09-15 12:32:23 -0700134 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
135 * This may differ from the device channel count when
136 * the device is not compatible with AudioFlinger
137 * capabilities, e.g. exposes too many channels or
138 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600139 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
140 * so the proxy doesn't have a channel_mask, but
141 * audio HALs need to talk about channel masks
142 * so expose the one calculated by
143 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700144
Paul McLean6a75e4e2016-05-25 14:09:02 -0600145 struct listnode list_node;
146
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700147 /* 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 -0700148 void * conversion_buffer; /* any conversions are put into here
149 * they could come from here too if
150 * there was a previous conversion */
151 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700152};
153
Paul McLean994ac072016-06-02 15:33:24 -0600154/*
155 * Locking Helpers
156 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800157/*
Eric Laurent70318092015-06-19 17:49:17 -0700158 * NOTE: when multiple mutexes have to be acquired, always take the
159 * stream_in or stream_out mutex first, followed by the audio_device mutex.
160 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
161 * higher priority playback or capture thread.
162 */
163
Paul McLean994ac072016-06-02 15:33:24 -0600164static void stream_lock_init(struct stream_lock *lock) {
165 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
166 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
167}
168
169static void stream_lock(struct stream_lock *lock) {
170 pthread_mutex_lock(&lock->pre_lock);
171 pthread_mutex_lock(&lock->lock);
172 pthread_mutex_unlock(&lock->pre_lock);
173}
174
175static void stream_unlock(struct stream_lock *lock) {
176 pthread_mutex_unlock(&lock->lock);
177}
178
179static void device_lock(struct audio_device *adev) {
180 pthread_mutex_lock(&adev->lock);
181}
182
183static int device_try_lock(struct audio_device *adev) {
184 return pthread_mutex_trylock(&adev->lock);
185}
186
187static void device_unlock(struct audio_device *adev) {
188 pthread_mutex_unlock(&adev->lock);
189}
190
191/*
192 * streams list management
193 */
194static void adev_add_stream_to_list(
195 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
196 device_lock(adev);
197
198 list_add_tail(list, stream_node);
199
200 device_unlock(adev);
201}
202
203static void adev_remove_stream_from_list(
204 struct audio_device* adev, struct listnode* stream_node) {
205 device_lock(adev);
206
207 list_remove(stream_node);
208
209 device_unlock(adev);
210}
211
Eric Laurent70318092015-06-19 17:49:17 -0700212/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700213 * Extract the card and device numbers from the supplied key/value pairs.
214 * kvpairs A null-terminated string containing the key/value pairs or card and device.
215 * i.e. "card=1;device=42"
216 * card A pointer to a variable to receive the parsed-out card number.
217 * device A pointer to a variable to receive the parsed-out device number.
218 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
219 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800220 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700221 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800222static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700223{
224 struct str_parms * parms = str_parms_create_str(kvpairs);
225 char value[32];
226 int param_val;
227
228 // initialize to "undefined" state.
229 *card = -1;
230 *device = -1;
231
232 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
233 if (param_val >= 0) {
234 *card = atoi(value);
235 }
236
237 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
238 if (param_val >= 0) {
239 *device = atoi(value);
240 }
241
242 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800243
244 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700245}
246
Andy Hung4e6a1c02017-10-27 20:31:46 -0700247static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800248{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700249 if (profile->card < 0 || profile->device < 0) {
250 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800251 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700252
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700253 struct str_parms *query = str_parms_create_str(keys);
254 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700255
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700256 /* These keys are from hardware/libhardware/include/audio.h */
257 /* supported sample rates */
258 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
259 char* rates_list = profile_get_sample_rate_strs(profile);
260 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
261 rates_list);
262 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700263 }
264
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700265 /* supported channel counts */
266 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
267 char* channels_list = profile_get_channel_count_strs(profile);
268 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
269 channels_list);
270 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700271 }
272
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700273 /* supported sample formats */
274 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
275 char * format_params = profile_get_format_strs(profile);
276 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
277 format_params);
278 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700279 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700280 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700281
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700282 char* result_str = str_parms_to_str(result);
283 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700284
Paul McLean65ec72b2015-02-18 09:13:24 -0800285 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700286
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700287 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800288}
289
290/*
291 * HAl Functions
292 */
Simon Wilson19957a32012-04-06 16:17:12 -0700293/**
294 * NOTE: when multiple mutexes have to be acquired, always respect the
295 * following order: hw device > out stream
296 */
297
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700298/*
299 * OUT functions
300 */
Simon Wilson19957a32012-04-06 16:17:12 -0700301static uint32_t out_get_sample_rate(const struct audio_stream *stream)
302{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700303 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
304 ALOGV("out_get_sample_rate() = %d", rate);
305 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700306}
307
308static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
309{
310 return 0;
311}
312
313static size_t out_get_buffer_size(const struct audio_stream *stream)
314{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700315 const struct stream_out* out = (const struct stream_out*)stream;
316 size_t buffer_size =
317 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700318 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700319}
320
321static uint32_t out_get_channels(const struct audio_stream *stream)
322{
Andy Hung03576be2014-07-21 21:16:45 -0700323 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700324 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700325}
326
327static audio_format_t out_get_format(const struct audio_stream *stream)
328{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700329 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
330 * Relies on the framework to provide data in the specified format.
331 * This could change in the future.
332 */
333 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
334 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700335 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700336}
337
338static int out_set_format(struct audio_stream *stream, audio_format_t format)
339{
340 return 0;
341}
342
343static int out_standby(struct audio_stream *stream)
344{
345 struct stream_out *out = (struct stream_out *)stream;
346
Paul McLean994ac072016-06-02 15:33:24 -0600347 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700348 if (!out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600349 device_lock(out->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700350 proxy_close(&out->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600351 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700352 out->standby = true;
353 }
Paul McLean994ac072016-06-02 15:33:24 -0600354 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700355 return 0;
356}
357
Paul McLean6a75e4e2016-05-25 14:09:02 -0600358static int out_dump(const struct audio_stream *stream, int fd) {
359 const struct stream_out* out_stream = (const struct stream_out*) stream;
360
361 if (out_stream != NULL) {
362 dprintf(fd, "Output Profile:\n");
363 profile_dump(out_stream->profile, fd);
364
365 dprintf(fd, "Output Proxy:\n");
366 proxy_dump(&out_stream->proxy, fd);
367 }
368
Simon Wilson19957a32012-04-06 16:17:12 -0700369 return 0;
370}
371
372static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
373{
Paul McLean65ec72b2015-02-18 09:13:24 -0800374 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800375
Simon Wilson19957a32012-04-06 16:17:12 -0700376 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700377
Paul McLeaneedc92e2013-12-19 15:46:15 -0800378 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700379 int card = -1;
380 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700381
Paul McLean65ec72b2015-02-18 09:13:24 -0800382 if (!parse_card_device_params(kvpairs, &card, &device)) {
383 // nothing to do
384 return ret_value;
385 }
386
Paul McLean994ac072016-06-02 15:33:24 -0600387 stream_lock(&out->lock);
Paul McLean65ec72b2015-02-18 09:13:24 -0800388 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600389 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700390
Paul McLean65ec72b2015-02-18 09:13:24 -0800391 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700392 /* cannot read pcm device info if playback is active */
393 if (!out->standby)
394 ret_value = -ENOSYS;
395 else {
396 int saved_card = out->profile->card;
397 int saved_device = out->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700398 out->adev->out_profile.card = card;
399 out->adev->out_profile.device = device;
400 ret_value = profile_read_device_info(&out->adev->out_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700401 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700402 out->adev->out_profile.card = saved_card;
403 out->adev->out_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700404 }
405 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800406 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700407
Paul McLean994ac072016-06-02 15:33:24 -0600408 device_unlock(out->adev);
409 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700410
Paul McLeaneedc92e2013-12-19 15:46:15 -0800411 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700412}
413
Paul McLeanf62d75e2014-07-11 15:14:19 -0700414static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
415{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700416 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600417 stream_lock(&out->lock);
418 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700419
420 char * params_str = device_get_parameters(out->profile, keys);
421
Paul McLean994ac072016-06-02 15:33:24 -0600422 device_unlock(out->adev);
423 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700424 return params_str;
425}
426
Simon Wilson19957a32012-04-06 16:17:12 -0700427static uint32_t out_get_latency(const struct audio_stream_out *stream)
428{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700429 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
430 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700431}
432
Paul McLean30f41852014-04-16 15:44:20 -0700433static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700434{
435 return -ENOSYS;
436}
437
Paul McLeaneedc92e2013-12-19 15:46:15 -0800438/* must be called with hw device and output stream mutexes locked */
439static int start_output_stream(struct stream_out *out)
440{
Paul McLean65ec72b2015-02-18 09:13:24 -0800441 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800442
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700443 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800444}
445
446static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700447{
448 int ret;
449 struct stream_out *out = (struct stream_out *)stream;
450
Paul McLean994ac072016-06-02 15:33:24 -0600451 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700452 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600453 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700454 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600455 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700456 if (ret != 0) {
457 goto err;
458 }
459 out->standby = false;
460 }
Eric Laurent05333d42014-08-04 20:29:17 -0700461
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700462 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700463 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800464 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700465 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
466 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700467 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700468 /* allocate buffer */
469 const size_t required_conversion_buffer_size =
470 bytes * num_device_channels / num_req_channels;
471 if (required_conversion_buffer_size > out->conversion_buffer_size) {
472 out->conversion_buffer_size = required_conversion_buffer_size;
473 out->conversion_buffer = realloc(out->conversion_buffer,
474 out->conversion_buffer_size);
475 }
476 /* convert data */
477 const audio_format_t audio_format = out_get_format(&(out->stream.common));
478 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800479 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700480 adjust_channels(write_buff, num_req_channels,
481 out->conversion_buffer, num_device_channels,
482 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800483 write_buff = out->conversion_buffer;
484 }
485
Paul McLeaneedc92e2013-12-19 15:46:15 -0800486 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700487 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800488 }
Simon Wilson19957a32012-04-06 16:17:12 -0700489
Paul McLean994ac072016-06-02 15:33:24 -0600490 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700491
492 return bytes;
493
494err:
Paul McLean994ac072016-06-02 15:33:24 -0600495 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700496 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700497 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700498 out_get_sample_rate(&stream->common));
499 }
500
501 return bytes;
502}
503
Paul McLean30f41852014-04-16 15:44:20 -0700504static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700505{
506 return -EINVAL;
507}
508
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700509static int out_get_presentation_position(const struct audio_stream_out *stream,
510 uint64_t *frames, struct timespec *timestamp)
511{
Andy Hungc9515ce2015-08-04 15:05:19 -0700512 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600513 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700514
515 const alsa_device_proxy *proxy = &out->proxy;
516 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
517
Paul McLean994ac072016-06-02 15:33:24 -0600518 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700519 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700520}
521
Simon Wilson19957a32012-04-06 16:17:12 -0700522static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
523{
524 return 0;
525}
526
527static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
528{
529 return 0;
530}
531
Paul McLean30f41852014-04-16 15:44:20 -0700532static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700533{
534 return -EINVAL;
535}
536
Paul McLean76dba682016-06-01 12:29:11 -0600537static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700538 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600539 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700540 audio_output_flags_t flags,
541 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700542 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700543 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700544{
Paul McLean76dba682016-06-01 12:29:11 -0600545 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
546 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800547
Simon Wilson19957a32012-04-06 16:17:12 -0700548 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700549
550 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600551 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700552 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600553 }
Simon Wilson19957a32012-04-06 16:17:12 -0700554
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700555 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700556 out->stream.common.get_sample_rate = out_get_sample_rate;
557 out->stream.common.set_sample_rate = out_set_sample_rate;
558 out->stream.common.get_buffer_size = out_get_buffer_size;
559 out->stream.common.get_channels = out_get_channels;
560 out->stream.common.get_format = out_get_format;
561 out->stream.common.set_format = out_set_format;
562 out->stream.common.standby = out_standby;
563 out->stream.common.dump = out_dump;
564 out->stream.common.set_parameters = out_set_parameters;
565 out->stream.common.get_parameters = out_get_parameters;
566 out->stream.common.add_audio_effect = out_add_audio_effect;
567 out->stream.common.remove_audio_effect = out_remove_audio_effect;
568 out->stream.get_latency = out_get_latency;
569 out->stream.set_volume = out_set_volume;
570 out->stream.write = out_write;
571 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700572 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700573 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
574
Paul McLean994ac072016-06-02 15:33:24 -0600575 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700576
Paul McLean76dba682016-06-01 12:29:11 -0600577 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600578 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600579 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700580
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700581 // build this to hand to the alsa_device_proxy
582 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700583 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800584
Paul McLean0f1753e2014-12-02 12:36:45 -0700585 /* Pull out the card/device pair */
Andy Hung4e6a1c02017-10-27 20:31:46 -0700586 parse_card_device_params(address, &out->adev->out_profile.card, &out->adev->out_profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700587
Andy Hung4e6a1c02017-10-27 20:31:46 -0700588 profile_read_device_info(&out->adev->out_profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700589
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700590 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800591
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700592 /* Rate */
593 if (config->sample_rate == 0) {
594 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
595 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
596 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800597 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700598 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
599 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800600 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800601
Paul McLean76dba682016-06-01 12:29:11 -0600602 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600603 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600604
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700605 /* Format */
606 if (config->format == AUDIO_FORMAT_DEFAULT) {
607 proxy_config.format = profile_get_default_format(out->profile);
608 config->format = audio_format_from_pcm_format(proxy_config.format);
609 } else {
610 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
611 if (profile_is_format_valid(out->profile, fmt)) {
612 proxy_config.format = fmt;
613 } else {
614 proxy_config.format = profile_get_default_format(out->profile);
615 config->format = audio_format_from_pcm_format(proxy_config.format);
616 ret = -EINVAL;
617 }
618 }
619
620 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600621 bool calc_mask = false;
622 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
623 /* query case */
624 out->hal_channel_count = profile_get_default_channel_count(out->profile);
625 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700626 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600627 /* explicit case */
628 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700629 }
Paul McLean698dbd72015-11-03 12:24:30 -0800630
Paul McLean64345f82016-06-06 13:26:05 -0600631 /* The Framework is currently limited to no more than this number of channels */
632 if (out->hal_channel_count > FCC_8) {
633 out->hal_channel_count = FCC_8;
634 calc_mask = true;
635 }
636
637 if (calc_mask) {
638 /* need to calculate the mask from channel count either because this is the query case
639 * or the specified mask isn't valid for this device, or is more then the FW can handle */
640 config->channel_mask = out->hal_channel_count <= FCC_2
641 /* position mask for mono and stereo*/
642 ? audio_channel_out_mask_from_count(out->hal_channel_count)
643 /* otherwise indexed */
644 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
645 }
646
Andy Hung182ddc72015-05-05 23:37:30 -0700647 out->hal_channel_mask = config->channel_mask;
648
Paul McLean64345f82016-06-06 13:26:05 -0600649 // Validate the "logical" channel count against support in the "actual" profile.
650 // if they differ, choose the "actual" number of channels *closest* to the "logical".
651 // and store THAT in proxy_config.channels
652 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700653 proxy_prepare(&out->proxy, out->profile, &proxy_config);
654
Paul McLean96c4d302017-12-05 09:50:26 -0800655 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
656 * So clear any errors that may have occurred above.
657 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700658 ret = 0;
659
Paul McLeaneedc92e2013-12-19 15:46:15 -0800660 out->conversion_buffer = NULL;
661 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700662
663 out->standby = true;
664
Paul McLean6a75e4e2016-05-25 14:09:02 -0600665 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600666 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600667
Simon Wilson19957a32012-04-06 16:17:12 -0700668 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700669
670 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700671}
672
Paul McLean76dba682016-06-01 12:29:11 -0600673static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700674 struct audio_stream_out *stream)
675{
676 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800677 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700678
Paul McLean76dba682016-06-01 12:29:11 -0600679 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600680
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700681 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700682 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800683
684 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700685
Paul McLeaneedc92e2013-12-19 15:46:15 -0800686 out->conversion_buffer = NULL;
687 out->conversion_buffer_size = 0;
688
Paul McLean994ac072016-06-02 15:33:24 -0600689 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600690 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600691 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600692
Simon Wilson19957a32012-04-06 16:17:12 -0700693 free(stream);
694}
695
Paul McLean76dba682016-06-01 12:29:11 -0600696static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700697 const struct audio_config *config)
698{
699 /* TODO This needs to be calculated based on format/channels/rate */
700 return 320;
701}
702
703/*
704 * IN functions
705 */
706static uint32_t in_get_sample_rate(const struct audio_stream *stream)
707{
708 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
709 ALOGV("in_get_sample_rate() = %d", rate);
710 return rate;
711}
712
713static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
714{
715 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
716 return -ENOSYS;
717}
718
719static size_t in_get_buffer_size(const struct audio_stream *stream)
720{
721 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700722 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700723}
724
725static uint32_t in_get_channels(const struct audio_stream *stream)
726{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700727 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700728 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700729}
730
731static audio_format_t in_get_format(const struct audio_stream *stream)
732{
Andy Hung780f1f82015-04-22 22:14:39 -0700733 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
734 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
735 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700736}
737
738static int in_set_format(struct audio_stream *stream, audio_format_t format)
739{
740 ALOGV("in_set_format(%d) - NOPE", format);
741
742 return -ENOSYS;
743}
744
745static int in_standby(struct audio_stream *stream)
746{
747 struct stream_in *in = (struct stream_in *)stream;
748
Paul McLean994ac072016-06-02 15:33:24 -0600749 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700750 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600751 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700752 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600753 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700754 in->standby = true;
755 }
756
Paul McLean994ac072016-06-02 15:33:24 -0600757 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700758
759 return 0;
760}
761
762static int in_dump(const struct audio_stream *stream, int fd)
763{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600764 const struct stream_in* in_stream = (const struct stream_in*)stream;
765 if (in_stream != NULL) {
766 dprintf(fd, "Input Profile:\n");
767 profile_dump(in_stream->profile, fd);
768
769 dprintf(fd, "Input Proxy:\n");
770 proxy_dump(&in_stream->proxy, fd);
771 }
772
773 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700774}
775
776static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
777{
Paul McLean65ec72b2015-02-18 09:13:24 -0800778 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700779
780 struct stream_in *in = (struct stream_in *)stream;
781
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700782 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700783 int card = -1;
784 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700785
Paul McLean65ec72b2015-02-18 09:13:24 -0800786 if (!parse_card_device_params(kvpairs, &card, &device)) {
787 // nothing to do
788 return ret_value;
789 }
790
Paul McLean994ac072016-06-02 15:33:24 -0600791 stream_lock(&in->lock);
792 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700793
Paul McLean2c6196f2014-08-20 16:50:25 -0700794 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700795 /* cannot read pcm device info if playback is active, or more than one open stream */
796 if (!in->standby || in->adev->inputs_open > 1)
Eric Laurent05333d42014-08-04 20:29:17 -0700797 ret_value = -ENOSYS;
798 else {
799 int saved_card = in->profile->card;
800 int saved_device = in->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700801 in->adev->in_profile.card = card;
802 in->adev->in_profile.device = device;
803 ret_value = profile_read_device_info(&in->adev->in_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700804 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700805 in->adev->in_profile.card = saved_card;
806 in->adev->in_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700807 }
808 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700809 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700810
Paul McLean994ac072016-06-02 15:33:24 -0600811 device_unlock(in->adev);
812 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700813
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700814 return ret_value;
815}
816
817static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
818{
819 struct stream_in *in = (struct stream_in *)stream;
820
Paul McLean994ac072016-06-02 15:33:24 -0600821 stream_lock(&in->lock);
822 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700823
824 char * params_str = device_get_parameters(in->profile, keys);
825
Paul McLean994ac072016-06-02 15:33:24 -0600826 device_unlock(in->adev);
827 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700828
829 return params_str;
830}
831
832static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
833{
834 return 0;
835}
836
837static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
838{
839 return 0;
840}
841
842static int in_set_gain(struct audio_stream_in *stream, float gain)
843{
844 return 0;
845}
846
847/* must be called with hw device and output stream mutexes locked */
848static int start_input_stream(struct stream_in *in)
849{
Paul McLean1d585cc2016-05-24 11:28:10 -0600850 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700851
852 return proxy_open(&in->proxy);
853}
854
855/* TODO mutex stuff here (see out_write) */
856static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
857{
858 size_t num_read_buff_bytes = 0;
859 void * read_buff = buffer;
860 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800861 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700862
863 struct stream_in * in = (struct stream_in *)stream;
864
Paul McLean994ac072016-06-02 15:33:24 -0600865 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700866 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600867 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700868 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600869 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700870 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700871 goto err;
872 }
873 in->standby = false;
874 }
875
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700876 /*
877 * OK, we need to figure out how much data to read to be able to output the requested
878 * number of bytes in the HAL format (16-bit, stereo).
879 */
880 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700881 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
882 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700883
884 if (num_device_channels != num_req_channels) {
885 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
886 }
887
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700888 /* Setup/Realloc the conversion buffer (if necessary). */
889 if (num_read_buff_bytes != bytes) {
890 if (num_read_buff_bytes > in->conversion_buffer_size) {
891 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
892 (and do these conversions themselves) */
893 in->conversion_buffer_size = num_read_buff_bytes;
894 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
895 }
896 read_buff = in->conversion_buffer;
897 }
898
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800899 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
900 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700901 if (num_device_channels != num_req_channels) {
902 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
903
904 out_buff = buffer;
905 /* Num Channels conversion */
906 if (num_device_channels != num_req_channels) {
907 audio_format_t audio_format = in_get_format(&(in->stream.common));
908 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
909
910 num_read_buff_bytes =
911 adjust_channels(read_buff, num_device_channels,
912 out_buff, num_req_channels,
913 sample_size_in_bytes, num_read_buff_bytes);
914 }
915 }
Eric Laurent253def92014-09-14 12:18:18 -0700916
Paul McLean76dba682016-06-01 12:29:11 -0600917 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
918 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700919 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530920 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800921 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700922 }
923
924err:
Paul McLean994ac072016-06-02 15:33:24 -0600925 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700926 return num_read_buff_bytes;
927}
928
929static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
930{
931 return 0;
932}
933
Paul McLean76dba682016-06-01 12:29:11 -0600934static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700935 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600936 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700937 struct audio_config *config,
938 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700939 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -0700940 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -0700941 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700942{
Paul McLean1d585cc2016-05-24 11:28:10 -0600943 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700944 config->sample_rate, config->channel_mask, config->format);
945
Andy Hungb10ce6d2017-10-27 19:37:58 -0700946 /* Pull out the card/device pair */
947 int32_t card, device;
948 if (!parse_card_device_params(address, &card, &device)) {
949 ALOGW("%s fail - invalid address %s", __func__, address);
950 *stream_in = NULL;
951 return -EINVAL;
952 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700953
Andy Hungb10ce6d2017-10-27 19:37:58 -0700954 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -0600955 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700956 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700957 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600958 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700959
960 /* setup function pointers */
961 in->stream.common.get_sample_rate = in_get_sample_rate;
962 in->stream.common.set_sample_rate = in_set_sample_rate;
963 in->stream.common.get_buffer_size = in_get_buffer_size;
964 in->stream.common.get_channels = in_get_channels;
965 in->stream.common.get_format = in_get_format;
966 in->stream.common.set_format = in_set_format;
967 in->stream.common.standby = in_standby;
968 in->stream.common.dump = in_dump;
969 in->stream.common.set_parameters = in_set_parameters;
970 in->stream.common.get_parameters = in_get_parameters;
971 in->stream.common.add_audio_effect = in_add_audio_effect;
972 in->stream.common.remove_audio_effect = in_remove_audio_effect;
973
974 in->stream.set_gain = in_set_gain;
975 in->stream.read = in_read;
976 in->stream.get_input_frames_lost = in_get_input_frames_lost;
977
Paul McLean994ac072016-06-02 15:33:24 -0600978 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700979
Paul McLean76dba682016-06-01 12:29:11 -0600980 in->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600981 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700982
Paul McLean76dba682016-06-01 12:29:11 -0600983 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700984
985 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700986 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700987
Andy Hungb10ce6d2017-10-27 19:37:58 -0700988 int ret = 0;
989 /* Check if an input stream is already open */
990 if (in->adev->inputs_open > 0) {
991 if (!profile_is_cached_for(in->profile, card, device)) {
992 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
993 __func__, card, device);
994 ret = -EINVAL;
995 }
996 } else {
997 /* Read input profile only if necessary */
Andy Hung4e6a1c02017-10-27 20:31:46 -0700998 in->adev->in_profile.card = card;
999 in->adev->in_profile.device = device;
1000 if (!profile_read_device_info(&in->adev->in_profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001001 ALOGW("%s fail - cannot read profile", __func__);
1002 ret = -EINVAL;
1003 }
1004 }
1005 if (ret != 0) {
1006 device_unlock(in->adev);
1007 free(in);
1008 *stream_in = NULL;
1009 return ret;
1010 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001011
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001012 /* Rate */
1013 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -06001014 config->sample_rate = profile_get_default_sample_rate(in->profile);
1015 }
1016
Paul McLean76dba682016-06-01 12:29:11 -06001017 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
1018 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
1019 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
1020 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001021 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Eric Laurent981f7742017-05-03 12:44:26 -07001022 proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001023 } else {
1024 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
1025 ret = -EINVAL;
1026 }
Paul McLean994ac072016-06-02 15:33:24 -06001027 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001028
1029 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -07001031 proxy_config.format = profile_get_default_format(in->profile);
1032 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001033 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001034 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1035 if (profile_is_format_valid(in->profile, fmt)) {
1036 proxy_config.format = fmt;
1037 } else {
1038 proxy_config.format = profile_get_default_format(in->profile);
1039 config->format = audio_format_from_pcm_format(proxy_config.format);
1040 ret = -EINVAL;
1041 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001042 }
1043
Paul McLean2cfd81b2014-09-15 12:32:23 -07001044 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001045 bool calc_mask = false;
1046 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1047 /* query case */
1048 in->hal_channel_count = profile_get_default_channel_count(in->profile);
1049 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001050 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001051 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001052 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1053 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001054
Paul McLean64345f82016-06-06 13:26:05 -06001055 /* The Framework is currently limited to no more than this number of channels */
1056 if (in->hal_channel_count > FCC_8) {
1057 in->hal_channel_count = FCC_8;
1058 calc_mask = true;
1059 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001060
Paul McLean64345f82016-06-06 13:26:05 -06001061 if (calc_mask) {
1062 /* need to calculate the mask from channel count either because this is the query case
1063 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1064 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1065 /* position mask for mono & stereo */
1066 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1067 /* otherwise indexed */
1068 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001069
Paul McLean64345f82016-06-06 13:26:05 -06001070 // if we change the mask...
1071 if (in->hal_channel_mask != config->channel_mask &&
1072 config->channel_mask != AUDIO_CHANNEL_NONE) {
1073 config->channel_mask = in->hal_channel_mask;
1074 ret = -EINVAL;
1075 }
1076 } else {
1077 in->hal_channel_mask = config->channel_mask;
1078 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001079
Paul McLean64345f82016-06-06 13:26:05 -06001080 if (ret == 0) {
1081 // Validate the "logical" channel count against support in the "actual" profile.
1082 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1083 // and store THAT in proxy_config.channels
1084 proxy_config.channels =
1085 profile_get_closest_channel_count(in->profile, in->hal_channel_count);
Eric Laurent981f7742017-05-03 12:44:26 -07001086 ret = proxy_prepare(&in->proxy, in->profile, &proxy_config);
1087 if (ret == 0) {
1088 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001089
Eric Laurent981f7742017-05-03 12:44:26 -07001090 in->conversion_buffer = NULL;
1091 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001092
Eric Laurent981f7742017-05-03 12:44:26 -07001093 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001094
Eric Laurent981f7742017-05-03 12:44:26 -07001095 /* Save this for adev_dump() */
1096 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1097 } else {
1098 ALOGW("proxy_prepare error %d", ret);
1099 unsigned channel_count = proxy_get_channel_count(&in->proxy);
1100 config->channel_mask = channel_count <= FCC_2
1101 ? audio_channel_in_mask_from_count(channel_count)
1102 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1103 config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy));
1104 config->sample_rate = proxy_get_sample_rate(&in->proxy);
1105 }
1106 }
Paul McLean64345f82016-06-06 13:26:05 -06001107
Eric Laurent981f7742017-05-03 12:44:26 -07001108 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001109 // Deallocate this stream on error, because AudioFlinger won't call
1110 // adev_close_input_stream() in this case.
1111 *stream_in = NULL;
1112 free(in);
1113 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001114
Andy Hungb10ce6d2017-10-27 19:37:58 -07001115 device_lock(in->adev);
1116 ++in->adev->inputs_open;
1117 device_unlock(in->adev);
1118
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001119 return ret;
1120}
1121
Paul McLean76dba682016-06-01 12:29:11 -06001122static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1123 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001124{
1125 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001126 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1127
Paul McLean76dba682016-06-01 12:29:11 -06001128 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001129
Andy Hungb10ce6d2017-10-27 19:37:58 -07001130 device_lock(in->adev);
1131 --in->adev->inputs_open;
1132 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1133 "invalid inputs_open: %d", in->adev->inputs_open);
1134 device_unlock(in->adev);
1135
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001136 /* Close the pcm device */
1137 in_standby(&stream->common);
1138
1139 free(in->conversion_buffer);
1140
1141 free(stream);
1142}
1143
1144/*
1145 * ADEV Functions
1146 */
Paul McLean76dba682016-06-01 12:29:11 -06001147static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001148{
1149 return 0;
1150}
1151
Paul McLean76dba682016-06-01 12:29:11 -06001152static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001153{
1154 return strdup("");
1155}
1156
Paul McLean76dba682016-06-01 12:29:11 -06001157static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001158{
1159 return 0;
1160}
1161
Paul McLean76dba682016-06-01 12:29:11 -06001162static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001163{
1164 return -ENOSYS;
1165}
1166
Paul McLean76dba682016-06-01 12:29:11 -06001167static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001168{
1169 return -ENOSYS;
1170}
1171
Paul McLean76dba682016-06-01 12:29:11 -06001172static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001173{
1174 return 0;
1175}
1176
Paul McLean76dba682016-06-01 12:29:11 -06001177static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001178{
Paul McLean76dba682016-06-01 12:29:11 -06001179 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001180 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001181 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001182 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001183 return -ENOSYS;
1184}
1185
Paul McLean76dba682016-06-01 12:29:11 -06001186static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001187{
1188 return -ENOSYS;
1189}
1190
Paul McLean76dba682016-06-01 12:29:11 -06001191static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001192{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001193 dprintf(fd, "\nUSB audio module:\n");
1194
1195 struct audio_device* adev = (struct audio_device*)device;
1196 const int kNumRetries = 3;
1197 const int kSleepTimeMS = 500;
1198
Paul McLean994ac072016-06-02 15:33:24 -06001199 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001200 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001201 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001202 sleep(kSleepTimeMS);
1203 retry--;
1204 }
1205
1206 if (retry > 0) {
1207 if (list_empty(&adev->output_stream_list)) {
1208 dprintf(fd, " No output streams.\n");
1209 } else {
1210 struct listnode* node;
1211 list_for_each(node, &adev->output_stream_list) {
1212 struct audio_stream* stream =
1213 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1214 out_dump(stream, fd);
1215 }
1216 }
1217
1218 if (list_empty(&adev->input_stream_list)) {
1219 dprintf(fd, "\n No input streams.\n");
1220 } else {
1221 struct listnode* node;
1222 list_for_each(node, &adev->input_stream_list) {
1223 struct audio_stream* stream =
1224 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1225 in_dump(stream, fd);
1226 }
1227 }
1228
Paul McLean994ac072016-06-02 15:33:24 -06001229 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001230 } else {
1231 // Couldn't lock
1232 dprintf(fd, " Could not obtain device lock.\n");
1233 }
1234
Simon Wilson19957a32012-04-06 16:17:12 -07001235 return 0;
1236}
1237
1238static int adev_close(hw_device_t *device)
1239{
Simon Wilson19957a32012-04-06 16:17:12 -07001240 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001241
Simon Wilson19957a32012-04-06 16:17:12 -07001242 return 0;
1243}
1244
Paul McLean30f41852014-04-16 15:44:20 -07001245static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001246{
Simon Wilson19957a32012-04-06 16:17:12 -07001247 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1248 return -EINVAL;
1249
Paul McLeaneedc92e2013-12-19 15:46:15 -08001250 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001251 if (!adev)
1252 return -ENOMEM;
1253
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001254 profile_init(&adev->out_profile, PCM_OUT);
1255 profile_init(&adev->in_profile, PCM_IN);
1256
Paul McLean6a75e4e2016-05-25 14:09:02 -06001257 list_init(&adev->output_stream_list);
1258 list_init(&adev->input_stream_list);
1259
Simon Wilson19957a32012-04-06 16:17:12 -07001260 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001261 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001262 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001263 adev->hw_device.common.close = adev_close;
1264
Simon Wilson19957a32012-04-06 16:17:12 -07001265 adev->hw_device.init_check = adev_init_check;
1266 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1267 adev->hw_device.set_master_volume = adev_set_master_volume;
1268 adev->hw_device.set_mode = adev_set_mode;
1269 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1270 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1271 adev->hw_device.set_parameters = adev_set_parameters;
1272 adev->hw_device.get_parameters = adev_get_parameters;
1273 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1274 adev->hw_device.open_output_stream = adev_open_output_stream;
1275 adev->hw_device.close_output_stream = adev_close_output_stream;
1276 adev->hw_device.open_input_stream = adev_open_input_stream;
1277 adev->hw_device.close_input_stream = adev_close_input_stream;
1278 adev->hw_device.dump = adev_dump;
1279
1280 *device = &adev->hw_device.common;
1281
1282 return 0;
1283}
1284
1285static struct hw_module_methods_t hal_module_methods = {
1286 .open = adev_open,
1287};
1288
1289struct audio_module HAL_MODULE_INFO_SYM = {
1290 .common = {
1291 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001292 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1293 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001294 .id = AUDIO_HARDWARE_MODULE_ID,
1295 .name = "USB audio HW HAL",
1296 .author = "The Android Open Source Project",
1297 .methods = &hal_module_methods,
1298 },
1299};