blob: 43d1daf768f0634783680663eb612ca03f2ae8ea [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
Simon Wilson19957a32012-04-06 16:17:12 -0700378 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800379 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700380 int card = -1;
381 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700382
Paul McLean65ec72b2015-02-18 09:13:24 -0800383 if (!parse_card_device_params(kvpairs, &card, &device)) {
384 // nothing to do
385 return ret_value;
386 }
387
Paul McLean994ac072016-06-02 15:33:24 -0600388 stream_lock(&out->lock);
Paul McLean65ec72b2015-02-18 09:13:24 -0800389 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600390 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700391
Paul McLean65ec72b2015-02-18 09:13:24 -0800392 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700393 /* cannot read pcm device info if playback is active */
394 if (!out->standby)
395 ret_value = -ENOSYS;
396 else {
397 int saved_card = out->profile->card;
398 int saved_device = out->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700399 out->adev->out_profile.card = card;
400 out->adev->out_profile.device = device;
401 ret_value = profile_read_device_info(&out->adev->out_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700402 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700403 out->adev->out_profile.card = saved_card;
404 out->adev->out_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700405 }
406 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800407 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700408
Paul McLean994ac072016-06-02 15:33:24 -0600409 device_unlock(out->adev);
410 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700411
Paul McLeaneedc92e2013-12-19 15:46:15 -0800412 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700413}
414
Paul McLeanf62d75e2014-07-11 15:14:19 -0700415static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
416{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700417 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600418 stream_lock(&out->lock);
419 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700420
421 char * params_str = device_get_parameters(out->profile, keys);
422
Paul McLean994ac072016-06-02 15:33:24 -0600423 device_unlock(out->adev);
424 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700425 return params_str;
426}
427
Simon Wilson19957a32012-04-06 16:17:12 -0700428static uint32_t out_get_latency(const struct audio_stream_out *stream)
429{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700430 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
431 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700432}
433
Paul McLean30f41852014-04-16 15:44:20 -0700434static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700435{
436 return -ENOSYS;
437}
438
Paul McLeaneedc92e2013-12-19 15:46:15 -0800439/* must be called with hw device and output stream mutexes locked */
440static int start_output_stream(struct stream_out *out)
441{
Paul McLean65ec72b2015-02-18 09:13:24 -0800442 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800443
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700444 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800445}
446
447static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700448{
449 int ret;
450 struct stream_out *out = (struct stream_out *)stream;
451
Paul McLean994ac072016-06-02 15:33:24 -0600452 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700453 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600454 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700455 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600456 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700457 if (ret != 0) {
458 goto err;
459 }
460 out->standby = false;
461 }
Eric Laurent05333d42014-08-04 20:29:17 -0700462
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700463 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700464 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800465 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700466 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
467 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700468 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700469 /* allocate buffer */
470 const size_t required_conversion_buffer_size =
471 bytes * num_device_channels / num_req_channels;
472 if (required_conversion_buffer_size > out->conversion_buffer_size) {
473 out->conversion_buffer_size = required_conversion_buffer_size;
474 out->conversion_buffer = realloc(out->conversion_buffer,
475 out->conversion_buffer_size);
476 }
477 /* convert data */
478 const audio_format_t audio_format = out_get_format(&(out->stream.common));
479 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800480 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700481 adjust_channels(write_buff, num_req_channels,
482 out->conversion_buffer, num_device_channels,
483 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800484 write_buff = out->conversion_buffer;
485 }
486
Paul McLeaneedc92e2013-12-19 15:46:15 -0800487 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700488 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800489 }
Simon Wilson19957a32012-04-06 16:17:12 -0700490
Paul McLean994ac072016-06-02 15:33:24 -0600491 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700492
493 return bytes;
494
495err:
Paul McLean994ac072016-06-02 15:33:24 -0600496 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700497 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700498 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700499 out_get_sample_rate(&stream->common));
500 }
501
502 return bytes;
503}
504
Paul McLean30f41852014-04-16 15:44:20 -0700505static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700506{
507 return -EINVAL;
508}
509
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700510static int out_get_presentation_position(const struct audio_stream_out *stream,
511 uint64_t *frames, struct timespec *timestamp)
512{
Andy Hungc9515ce2015-08-04 15:05:19 -0700513 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600514 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700515
516 const alsa_device_proxy *proxy = &out->proxy;
517 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
518
Paul McLean994ac072016-06-02 15:33:24 -0600519 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700520 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700521}
522
Simon Wilson19957a32012-04-06 16:17:12 -0700523static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
524{
525 return 0;
526}
527
528static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
529{
530 return 0;
531}
532
Paul McLean30f41852014-04-16 15:44:20 -0700533static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700534{
535 return -EINVAL;
536}
537
Paul McLean76dba682016-06-01 12:29:11 -0600538static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700539 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600540 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700541 audio_output_flags_t flags,
542 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700543 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700544 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700545{
Paul McLean76dba682016-06-01 12:29:11 -0600546 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
547 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800548
Simon Wilson19957a32012-04-06 16:17:12 -0700549 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700550
551 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600552 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700553 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600554 }
Simon Wilson19957a32012-04-06 16:17:12 -0700555
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700556 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700557 out->stream.common.get_sample_rate = out_get_sample_rate;
558 out->stream.common.set_sample_rate = out_set_sample_rate;
559 out->stream.common.get_buffer_size = out_get_buffer_size;
560 out->stream.common.get_channels = out_get_channels;
561 out->stream.common.get_format = out_get_format;
562 out->stream.common.set_format = out_set_format;
563 out->stream.common.standby = out_standby;
564 out->stream.common.dump = out_dump;
565 out->stream.common.set_parameters = out_set_parameters;
566 out->stream.common.get_parameters = out_get_parameters;
567 out->stream.common.add_audio_effect = out_add_audio_effect;
568 out->stream.common.remove_audio_effect = out_remove_audio_effect;
569 out->stream.get_latency = out_get_latency;
570 out->stream.set_volume = out_set_volume;
571 out->stream.write = out_write;
572 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700573 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700574 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
575
Paul McLean994ac072016-06-02 15:33:24 -0600576 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700577
Paul McLean76dba682016-06-01 12:29:11 -0600578 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600579 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600580 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700581
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700582 // build this to hand to the alsa_device_proxy
583 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700584 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800585
Paul McLean0f1753e2014-12-02 12:36:45 -0700586 /* Pull out the card/device pair */
Andy Hung4e6a1c02017-10-27 20:31:46 -0700587 parse_card_device_params(address, &out->adev->out_profile.card, &out->adev->out_profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700588
Andy Hung4e6a1c02017-10-27 20:31:46 -0700589 profile_read_device_info(&out->adev->out_profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700590
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700591 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800592
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700593 /* Rate */
594 if (config->sample_rate == 0) {
595 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
596 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
597 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800598 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700599 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
600 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800601 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800602
Paul McLean76dba682016-06-01 12:29:11 -0600603 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600604 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600605
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700606 /* Format */
607 if (config->format == AUDIO_FORMAT_DEFAULT) {
608 proxy_config.format = profile_get_default_format(out->profile);
609 config->format = audio_format_from_pcm_format(proxy_config.format);
610 } else {
611 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
612 if (profile_is_format_valid(out->profile, fmt)) {
613 proxy_config.format = fmt;
614 } else {
615 proxy_config.format = profile_get_default_format(out->profile);
616 config->format = audio_format_from_pcm_format(proxy_config.format);
617 ret = -EINVAL;
618 }
619 }
620
621 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600622 bool calc_mask = false;
623 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
624 /* query case */
625 out->hal_channel_count = profile_get_default_channel_count(out->profile);
626 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700627 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600628 /* explicit case */
629 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700630 }
Paul McLean698dbd72015-11-03 12:24:30 -0800631
Paul McLean64345f82016-06-06 13:26:05 -0600632 /* The Framework is currently limited to no more than this number of channels */
633 if (out->hal_channel_count > FCC_8) {
634 out->hal_channel_count = FCC_8;
635 calc_mask = true;
636 }
637
638 if (calc_mask) {
639 /* need to calculate the mask from channel count either because this is the query case
640 * or the specified mask isn't valid for this device, or is more then the FW can handle */
641 config->channel_mask = out->hal_channel_count <= FCC_2
642 /* position mask for mono and stereo*/
643 ? audio_channel_out_mask_from_count(out->hal_channel_count)
644 /* otherwise indexed */
645 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
646 }
647
Andy Hung182ddc72015-05-05 23:37:30 -0700648 out->hal_channel_mask = config->channel_mask;
649
Paul McLean64345f82016-06-06 13:26:05 -0600650 // Validate the "logical" channel count against support in the "actual" profile.
651 // if they differ, choose the "actual" number of channels *closest* to the "logical".
652 // and store THAT in proxy_config.channels
653 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700654 proxy_prepare(&out->proxy, out->profile, &proxy_config);
655
656 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
657 ret = 0;
658
Paul McLeaneedc92e2013-12-19 15:46:15 -0800659 out->conversion_buffer = NULL;
660 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700661
662 out->standby = true;
663
Paul McLean6a75e4e2016-05-25 14:09:02 -0600664 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600665 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600666
Simon Wilson19957a32012-04-06 16:17:12 -0700667 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700668
669 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700670
671err_open:
672 free(out);
673 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800674 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700675}
676
Paul McLean76dba682016-06-01 12:29:11 -0600677static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700678 struct audio_stream_out *stream)
679{
680 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800681 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700682
Paul McLean76dba682016-06-01 12:29:11 -0600683 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600684
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700685 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700686 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800687
688 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700689
Paul McLeaneedc92e2013-12-19 15:46:15 -0800690 out->conversion_buffer = NULL;
691 out->conversion_buffer_size = 0;
692
Paul McLean994ac072016-06-02 15:33:24 -0600693 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600694 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600695 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600696
Simon Wilson19957a32012-04-06 16:17:12 -0700697 free(stream);
698}
699
Paul McLean76dba682016-06-01 12:29:11 -0600700static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700701 const struct audio_config *config)
702{
703 /* TODO This needs to be calculated based on format/channels/rate */
704 return 320;
705}
706
707/*
708 * IN functions
709 */
710static uint32_t in_get_sample_rate(const struct audio_stream *stream)
711{
712 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
713 ALOGV("in_get_sample_rate() = %d", rate);
714 return rate;
715}
716
717static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
718{
719 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
720 return -ENOSYS;
721}
722
723static size_t in_get_buffer_size(const struct audio_stream *stream)
724{
725 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700726 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700727}
728
729static uint32_t in_get_channels(const struct audio_stream *stream)
730{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700731 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700732 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700733}
734
735static audio_format_t in_get_format(const struct audio_stream *stream)
736{
Andy Hung780f1f82015-04-22 22:14:39 -0700737 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
738 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
739 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700740}
741
742static int in_set_format(struct audio_stream *stream, audio_format_t format)
743{
744 ALOGV("in_set_format(%d) - NOPE", format);
745
746 return -ENOSYS;
747}
748
749static int in_standby(struct audio_stream *stream)
750{
751 struct stream_in *in = (struct stream_in *)stream;
752
Paul McLean994ac072016-06-02 15:33:24 -0600753 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700754 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600755 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700756 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600757 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700758 in->standby = true;
759 }
760
Paul McLean994ac072016-06-02 15:33:24 -0600761 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700762
763 return 0;
764}
765
766static int in_dump(const struct audio_stream *stream, int fd)
767{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600768 const struct stream_in* in_stream = (const struct stream_in*)stream;
769 if (in_stream != NULL) {
770 dprintf(fd, "Input Profile:\n");
771 profile_dump(in_stream->profile, fd);
772
773 dprintf(fd, "Input Proxy:\n");
774 proxy_dump(&in_stream->proxy, fd);
775 }
776
777 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700778}
779
780static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
781{
Paul McLean65ec72b2015-02-18 09:13:24 -0800782 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700783
784 struct stream_in *in = (struct stream_in *)stream;
785
786 char value[32];
787 int param_val;
788 int routing = 0;
789 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700790 int card = -1;
791 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700792
Paul McLean65ec72b2015-02-18 09:13:24 -0800793 if (!parse_card_device_params(kvpairs, &card, &device)) {
794 // nothing to do
795 return ret_value;
796 }
797
Paul McLean994ac072016-06-02 15:33:24 -0600798 stream_lock(&in->lock);
799 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700800
Paul McLean2c6196f2014-08-20 16:50:25 -0700801 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700802 /* cannot read pcm device info if playback is active, or more than one open stream */
803 if (!in->standby || in->adev->inputs_open > 1)
Eric Laurent05333d42014-08-04 20:29:17 -0700804 ret_value = -ENOSYS;
805 else {
806 int saved_card = in->profile->card;
807 int saved_device = in->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700808 in->adev->in_profile.card = card;
809 in->adev->in_profile.device = device;
810 ret_value = profile_read_device_info(&in->adev->in_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700811 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700812 in->adev->in_profile.card = saved_card;
813 in->adev->in_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700814 }
815 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700816 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700817
Paul McLean994ac072016-06-02 15:33:24 -0600818 device_unlock(in->adev);
819 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700820
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700821 return ret_value;
822}
823
824static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
825{
826 struct stream_in *in = (struct stream_in *)stream;
827
Paul McLean994ac072016-06-02 15:33:24 -0600828 stream_lock(&in->lock);
829 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700830
831 char * params_str = device_get_parameters(in->profile, keys);
832
Paul McLean994ac072016-06-02 15:33:24 -0600833 device_unlock(in->adev);
834 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700835
836 return params_str;
837}
838
839static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
840{
841 return 0;
842}
843
844static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
845{
846 return 0;
847}
848
849static int in_set_gain(struct audio_stream_in *stream, float gain)
850{
851 return 0;
852}
853
854/* must be called with hw device and output stream mutexes locked */
855static int start_input_stream(struct stream_in *in)
856{
Paul McLean1d585cc2016-05-24 11:28:10 -0600857 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700858
859 return proxy_open(&in->proxy);
860}
861
862/* TODO mutex stuff here (see out_write) */
863static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
864{
865 size_t num_read_buff_bytes = 0;
866 void * read_buff = buffer;
867 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800868 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700869
870 struct stream_in * in = (struct stream_in *)stream;
871
Paul McLean994ac072016-06-02 15:33:24 -0600872 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700873 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600874 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700875 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600876 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700877 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700878 goto err;
879 }
880 in->standby = false;
881 }
882
Andy Hung4e6a1c02017-10-27 20:31:46 -0700883 const alsa_device_profile *profile = in->profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700884
885 /*
886 * OK, we need to figure out how much data to read to be able to output the requested
887 * number of bytes in the HAL format (16-bit, stereo).
888 */
889 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700890 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
891 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700892
893 if (num_device_channels != num_req_channels) {
894 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
895 }
896
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700897 /* Setup/Realloc the conversion buffer (if necessary). */
898 if (num_read_buff_bytes != bytes) {
899 if (num_read_buff_bytes > in->conversion_buffer_size) {
900 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
901 (and do these conversions themselves) */
902 in->conversion_buffer_size = num_read_buff_bytes;
903 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
904 }
905 read_buff = in->conversion_buffer;
906 }
907
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800908 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
909 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700910 if (num_device_channels != num_req_channels) {
911 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
912
913 out_buff = buffer;
914 /* Num Channels conversion */
915 if (num_device_channels != num_req_channels) {
916 audio_format_t audio_format = in_get_format(&(in->stream.common));
917 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
918
919 num_read_buff_bytes =
920 adjust_channels(read_buff, num_device_channels,
921 out_buff, num_req_channels,
922 sample_size_in_bytes, num_read_buff_bytes);
923 }
924 }
Eric Laurent253def92014-09-14 12:18:18 -0700925
Paul McLean76dba682016-06-01 12:29:11 -0600926 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
927 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700928 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530929 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800930 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700931 }
932
933err:
Paul McLean994ac072016-06-02 15:33:24 -0600934 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700935 return num_read_buff_bytes;
936}
937
938static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
939{
940 return 0;
941}
942
Paul McLean76dba682016-06-01 12:29:11 -0600943static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700944 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600945 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946 struct audio_config *config,
947 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700948 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -0700949 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -0700950 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700951{
Paul McLean1d585cc2016-05-24 11:28:10 -0600952 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700953 config->sample_rate, config->channel_mask, config->format);
954
Andy Hungb10ce6d2017-10-27 19:37:58 -0700955 /* Pull out the card/device pair */
956 int32_t card, device;
957 if (!parse_card_device_params(address, &card, &device)) {
958 ALOGW("%s fail - invalid address %s", __func__, address);
959 *stream_in = NULL;
960 return -EINVAL;
961 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700962
Andy Hungb10ce6d2017-10-27 19:37:58 -0700963 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -0600964 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700965 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700966 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600967 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700968
969 /* setup function pointers */
970 in->stream.common.get_sample_rate = in_get_sample_rate;
971 in->stream.common.set_sample_rate = in_set_sample_rate;
972 in->stream.common.get_buffer_size = in_get_buffer_size;
973 in->stream.common.get_channels = in_get_channels;
974 in->stream.common.get_format = in_get_format;
975 in->stream.common.set_format = in_set_format;
976 in->stream.common.standby = in_standby;
977 in->stream.common.dump = in_dump;
978 in->stream.common.set_parameters = in_set_parameters;
979 in->stream.common.get_parameters = in_get_parameters;
980 in->stream.common.add_audio_effect = in_add_audio_effect;
981 in->stream.common.remove_audio_effect = in_remove_audio_effect;
982
983 in->stream.set_gain = in_set_gain;
984 in->stream.read = in_read;
985 in->stream.get_input_frames_lost = in_get_input_frames_lost;
986
Paul McLean994ac072016-06-02 15:33:24 -0600987 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700988
Paul McLean76dba682016-06-01 12:29:11 -0600989 in->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600990 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991
Paul McLean76dba682016-06-01 12:29:11 -0600992 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700993
994 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700995 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700996
Andy Hungb10ce6d2017-10-27 19:37:58 -0700997 int ret = 0;
998 /* Check if an input stream is already open */
999 if (in->adev->inputs_open > 0) {
1000 if (!profile_is_cached_for(in->profile, card, device)) {
1001 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1002 __func__, card, device);
1003 ret = -EINVAL;
1004 }
1005 } else {
1006 /* Read input profile only if necessary */
Andy Hung4e6a1c02017-10-27 20:31:46 -07001007 in->adev->in_profile.card = card;
1008 in->adev->in_profile.device = device;
1009 if (!profile_read_device_info(&in->adev->in_profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001010 ALOGW("%s fail - cannot read profile", __func__);
1011 ret = -EINVAL;
1012 }
1013 }
1014 if (ret != 0) {
1015 device_unlock(in->adev);
1016 free(in);
1017 *stream_in = NULL;
1018 return ret;
1019 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001020
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001021 /* Rate */
1022 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -06001023 config->sample_rate = profile_get_default_sample_rate(in->profile);
1024 }
1025
Paul McLean76dba682016-06-01 12:29:11 -06001026 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
1027 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
1028 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
1029 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Eric Laurent981f7742017-05-03 12:44:26 -07001031 proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001032 } else {
1033 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
1034 ret = -EINVAL;
1035 }
Paul McLean994ac072016-06-02 15:33:24 -06001036 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001037
1038 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001039 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -07001040 proxy_config.format = profile_get_default_format(in->profile);
1041 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001042 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001043 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1044 if (profile_is_format_valid(in->profile, fmt)) {
1045 proxy_config.format = fmt;
1046 } else {
1047 proxy_config.format = profile_get_default_format(in->profile);
1048 config->format = audio_format_from_pcm_format(proxy_config.format);
1049 ret = -EINVAL;
1050 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001051 }
1052
Paul McLean2cfd81b2014-09-15 12:32:23 -07001053 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001054 bool calc_mask = false;
1055 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1056 /* query case */
1057 in->hal_channel_count = profile_get_default_channel_count(in->profile);
1058 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001059 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001060 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001061 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1062 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001063
Paul McLean64345f82016-06-06 13:26:05 -06001064 /* The Framework is currently limited to no more than this number of channels */
1065 if (in->hal_channel_count > FCC_8) {
1066 in->hal_channel_count = FCC_8;
1067 calc_mask = true;
1068 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001069
Paul McLean64345f82016-06-06 13:26:05 -06001070 if (calc_mask) {
1071 /* need to calculate the mask from channel count either because this is the query case
1072 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1073 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1074 /* position mask for mono & stereo */
1075 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1076 /* otherwise indexed */
1077 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001078
Paul McLean64345f82016-06-06 13:26:05 -06001079 // if we change the mask...
1080 if (in->hal_channel_mask != config->channel_mask &&
1081 config->channel_mask != AUDIO_CHANNEL_NONE) {
1082 config->channel_mask = in->hal_channel_mask;
1083 ret = -EINVAL;
1084 }
1085 } else {
1086 in->hal_channel_mask = config->channel_mask;
1087 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001088
Paul McLean64345f82016-06-06 13:26:05 -06001089 if (ret == 0) {
1090 // Validate the "logical" channel count against support in the "actual" profile.
1091 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1092 // and store THAT in proxy_config.channels
1093 proxy_config.channels =
1094 profile_get_closest_channel_count(in->profile, in->hal_channel_count);
Eric Laurent981f7742017-05-03 12:44:26 -07001095 ret = proxy_prepare(&in->proxy, in->profile, &proxy_config);
1096 if (ret == 0) {
1097 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001098
Eric Laurent981f7742017-05-03 12:44:26 -07001099 in->conversion_buffer = NULL;
1100 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001101
Eric Laurent981f7742017-05-03 12:44:26 -07001102 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001103
Eric Laurent981f7742017-05-03 12:44:26 -07001104 /* Save this for adev_dump() */
1105 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1106 } else {
1107 ALOGW("proxy_prepare error %d", ret);
1108 unsigned channel_count = proxy_get_channel_count(&in->proxy);
1109 config->channel_mask = channel_count <= FCC_2
1110 ? audio_channel_in_mask_from_count(channel_count)
1111 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1112 config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy));
1113 config->sample_rate = proxy_get_sample_rate(&in->proxy);
1114 }
1115 }
Paul McLean64345f82016-06-06 13:26:05 -06001116
Eric Laurent981f7742017-05-03 12:44:26 -07001117 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001118 // Deallocate this stream on error, because AudioFlinger won't call
1119 // adev_close_input_stream() in this case.
1120 *stream_in = NULL;
1121 free(in);
1122 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001123
Andy Hungb10ce6d2017-10-27 19:37:58 -07001124 device_lock(in->adev);
1125 ++in->adev->inputs_open;
1126 device_unlock(in->adev);
1127
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001128 return ret;
1129}
1130
Paul McLean76dba682016-06-01 12:29:11 -06001131static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1132 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001133{
1134 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001135 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1136
Paul McLean76dba682016-06-01 12:29:11 -06001137 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001138
Andy Hungb10ce6d2017-10-27 19:37:58 -07001139 device_lock(in->adev);
1140 --in->adev->inputs_open;
1141 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1142 "invalid inputs_open: %d", in->adev->inputs_open);
1143 device_unlock(in->adev);
1144
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001145 /* Close the pcm device */
1146 in_standby(&stream->common);
1147
1148 free(in->conversion_buffer);
1149
1150 free(stream);
1151}
1152
1153/*
1154 * ADEV Functions
1155 */
Paul McLean76dba682016-06-01 12:29:11 -06001156static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001157{
1158 return 0;
1159}
1160
Paul McLean76dba682016-06-01 12:29:11 -06001161static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001162{
1163 return strdup("");
1164}
1165
Paul McLean76dba682016-06-01 12:29:11 -06001166static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001167{
1168 return 0;
1169}
1170
Paul McLean76dba682016-06-01 12:29:11 -06001171static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001172{
1173 return -ENOSYS;
1174}
1175
Paul McLean76dba682016-06-01 12:29:11 -06001176static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001177{
1178 return -ENOSYS;
1179}
1180
Paul McLean76dba682016-06-01 12:29:11 -06001181static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001182{
1183 return 0;
1184}
1185
Paul McLean76dba682016-06-01 12:29:11 -06001186static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001187{
Paul McLean76dba682016-06-01 12:29:11 -06001188 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001189 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001190 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001191 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001192 return -ENOSYS;
1193}
1194
Paul McLean76dba682016-06-01 12:29:11 -06001195static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001196{
1197 return -ENOSYS;
1198}
1199
Paul McLean76dba682016-06-01 12:29:11 -06001200static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001201{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001202 dprintf(fd, "\nUSB audio module:\n");
1203
1204 struct audio_device* adev = (struct audio_device*)device;
1205 const int kNumRetries = 3;
1206 const int kSleepTimeMS = 500;
1207
Paul McLean994ac072016-06-02 15:33:24 -06001208 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001209 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001210 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001211 sleep(kSleepTimeMS);
1212 retry--;
1213 }
1214
1215 if (retry > 0) {
1216 if (list_empty(&adev->output_stream_list)) {
1217 dprintf(fd, " No output streams.\n");
1218 } else {
1219 struct listnode* node;
1220 list_for_each(node, &adev->output_stream_list) {
1221 struct audio_stream* stream =
1222 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1223 out_dump(stream, fd);
1224 }
1225 }
1226
1227 if (list_empty(&adev->input_stream_list)) {
1228 dprintf(fd, "\n No input streams.\n");
1229 } else {
1230 struct listnode* node;
1231 list_for_each(node, &adev->input_stream_list) {
1232 struct audio_stream* stream =
1233 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1234 in_dump(stream, fd);
1235 }
1236 }
1237
Paul McLean994ac072016-06-02 15:33:24 -06001238 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001239 } else {
1240 // Couldn't lock
1241 dprintf(fd, " Could not obtain device lock.\n");
1242 }
1243
Simon Wilson19957a32012-04-06 16:17:12 -07001244 return 0;
1245}
1246
1247static int adev_close(hw_device_t *device)
1248{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001249 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001250 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001251
Simon Wilson19957a32012-04-06 16:17:12 -07001252 return 0;
1253}
1254
Paul McLean30f41852014-04-16 15:44:20 -07001255static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001256{
Simon Wilson19957a32012-04-06 16:17:12 -07001257 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1258 return -EINVAL;
1259
Paul McLeaneedc92e2013-12-19 15:46:15 -08001260 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001261 if (!adev)
1262 return -ENOMEM;
1263
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001264 profile_init(&adev->out_profile, PCM_OUT);
1265 profile_init(&adev->in_profile, PCM_IN);
1266
Paul McLean6a75e4e2016-05-25 14:09:02 -06001267 list_init(&adev->output_stream_list);
1268 list_init(&adev->input_stream_list);
1269
Simon Wilson19957a32012-04-06 16:17:12 -07001270 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001271 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001272 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001273 adev->hw_device.common.close = adev_close;
1274
Simon Wilson19957a32012-04-06 16:17:12 -07001275 adev->hw_device.init_check = adev_init_check;
1276 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1277 adev->hw_device.set_master_volume = adev_set_master_volume;
1278 adev->hw_device.set_mode = adev_set_mode;
1279 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1280 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1281 adev->hw_device.set_parameters = adev_set_parameters;
1282 adev->hw_device.get_parameters = adev_get_parameters;
1283 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1284 adev->hw_device.open_output_stream = adev_open_output_stream;
1285 adev->hw_device.close_output_stream = adev_close_output_stream;
1286 adev->hw_device.open_input_stream = adev_open_input_stream;
1287 adev->hw_device.close_input_stream = adev_close_input_stream;
1288 adev->hw_device.dump = adev_dump;
1289
1290 *device = &adev->hw_device.common;
1291
1292 return 0;
1293}
1294
1295static struct hw_module_methods_t hal_module_methods = {
1296 .open = adev_open,
1297};
1298
1299struct audio_module HAL_MODULE_INFO_SYM = {
1300 .common = {
1301 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001302 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1303 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001304 .id = AUDIO_HARDWARE_MODULE_ID,
1305 .name = "USB audio HW HAL",
1306 .author = "The Android Open Source Project",
1307 .methods = &hal_module_methods,
1308 },
1309};