blob: 746a3c9e42f1bfd5cb7ae4216418176e9d2dbd80 [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 McLean1d585cc2016-05-24 11:28:10 -060047/* Lock play & record samples rates at or above this threshold */
48#define RATELOCK_THRESHOLD 96000
49
Simon Wilson19957a32012-04-06 16:17:12 -070050struct audio_device {
51 struct audio_hw_device hw_device;
52
53 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080054
55 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070056 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060057 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080058
59 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070060 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060061 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080062
Paul McLean1d585cc2016-05-24 11:28:10 -060063 /* lock input & output sample rates */
64 /*FIXME - How do we address multiple output streams? */
65 uint32_t device_sample_rate;
66
Eric Laurent253def92014-09-14 12:18:18 -070067 bool mic_muted;
68
Simon Wilson19957a32012-04-06 16:17:12 -070069 bool standby;
Andy Hungb10ce6d2017-10-27 19:37:58 -070070
71 int32_t inputs_open; /* number of input streams currently open. */
Simon Wilson19957a32012-04-06 16:17:12 -070072};
73
Paul McLean994ac072016-06-02 15:33:24 -060074struct stream_lock {
75 pthread_mutex_t lock; /* see note below on mutex acquisition order */
76 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
77};
78
Simon Wilson19957a32012-04-06 16:17:12 -070079struct stream_out {
80 struct audio_stream_out stream;
81
Paul McLean994ac072016-06-02 15:33:24 -060082 struct stream_lock lock;
83
Paul McLeaneedc92e2013-12-19 15:46:15 -080084 bool standby;
85
Paul McLean76dba682016-06-01 12:29:11 -060086 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070087
Andy Hung4e6a1c02017-10-27 20:31:46 -070088 const alsa_device_profile *profile; /* Points to the alsa_device_profile in the audio_device.
89 * Const, so modifications go through adev->out_profile
90 * and thus should have the hardware lock and ensure
91 * stream is not active and no other open output streams.
92 */
93
Paul McLeanc88e6ae2014-07-16 09:48:34 -070094 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080095
Andy Hung03576be2014-07-21 21:16:45 -070096 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
97 * This may differ from the device channel count when
98 * the device is not compatible with AudioFlinger
99 * capabilities, e.g. exposes too many channels or
100 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600101 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
102 * so the proxy doesn't have a channel_mask, but
103 * audio HALs need to talk about channel masks
104 * so expose the one calculated by
105 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700106
Paul McLean6a75e4e2016-05-25 14:09:02 -0600107 struct listnode list_node;
108
Paul McLeaneedc92e2013-12-19 15:46:15 -0800109 void * conversion_buffer; /* any conversions are put into here
110 * they could come from here too if
111 * there was a previous conversion */
112 size_t conversion_buffer_size; /* in bytes */
113};
114
Paul McLeaneedc92e2013-12-19 15:46:15 -0800115struct stream_in {
116 struct audio_stream_in stream;
117
Paul McLean994ac072016-06-02 15:33:24 -0600118 struct stream_lock lock;
119
Simon Wilson19957a32012-04-06 16:17:12 -0700120 bool standby;
121
Paul McLean76dba682016-06-01 12:29:11 -0600122 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800123
Andy Hung4e6a1c02017-10-27 20:31:46 -0700124 const alsa_device_profile *profile; /* Points to the alsa_device_profile in the audio_device.
125 * Const, so modifications go through adev->out_profile
126 * and thus should have the hardware lock and ensure
127 * stream is not active and no other open input streams.
128 */
129
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700130 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700131
Paul McLean2cfd81b2014-09-15 12:32:23 -0700132 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
133 * This may differ from the device channel count when
134 * the device is not compatible with AudioFlinger
135 * capabilities, e.g. exposes too many channels or
136 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600137 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
138 * so the proxy doesn't have a channel_mask, but
139 * audio HALs need to talk about channel masks
140 * so expose the one calculated by
141 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700142
Paul McLean6a75e4e2016-05-25 14:09:02 -0600143 struct listnode list_node;
144
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700145 /* 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 -0700146 void * conversion_buffer; /* any conversions are put into here
147 * they could come from here too if
148 * there was a previous conversion */
149 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700150};
151
Paul McLean994ac072016-06-02 15:33:24 -0600152/*
153 * Locking Helpers
154 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800155/*
Eric Laurent70318092015-06-19 17:49:17 -0700156 * NOTE: when multiple mutexes have to be acquired, always take the
157 * stream_in or stream_out mutex first, followed by the audio_device mutex.
158 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
159 * higher priority playback or capture thread.
160 */
161
Paul McLean994ac072016-06-02 15:33:24 -0600162static void stream_lock_init(struct stream_lock *lock) {
163 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
164 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
165}
166
167static void stream_lock(struct stream_lock *lock) {
168 pthread_mutex_lock(&lock->pre_lock);
169 pthread_mutex_lock(&lock->lock);
170 pthread_mutex_unlock(&lock->pre_lock);
171}
172
173static void stream_unlock(struct stream_lock *lock) {
174 pthread_mutex_unlock(&lock->lock);
175}
176
177static void device_lock(struct audio_device *adev) {
178 pthread_mutex_lock(&adev->lock);
179}
180
181static int device_try_lock(struct audio_device *adev) {
182 return pthread_mutex_trylock(&adev->lock);
183}
184
185static void device_unlock(struct audio_device *adev) {
186 pthread_mutex_unlock(&adev->lock);
187}
188
189/*
190 * streams list management
191 */
192static void adev_add_stream_to_list(
193 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
194 device_lock(adev);
195
196 list_add_tail(list, stream_node);
197
198 device_unlock(adev);
199}
200
201static void adev_remove_stream_from_list(
202 struct audio_device* adev, struct listnode* stream_node) {
203 device_lock(adev);
204
205 list_remove(stream_node);
206
207 device_unlock(adev);
208}
209
Eric Laurent70318092015-06-19 17:49:17 -0700210/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700211 * Extract the card and device numbers from the supplied key/value pairs.
212 * kvpairs A null-terminated string containing the key/value pairs or card and device.
213 * i.e. "card=1;device=42"
214 * card A pointer to a variable to receive the parsed-out card number.
215 * device A pointer to a variable to receive the parsed-out device number.
216 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
217 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800218 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700219 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800220static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700221{
222 struct str_parms * parms = str_parms_create_str(kvpairs);
223 char value[32];
224 int param_val;
225
226 // initialize to "undefined" state.
227 *card = -1;
228 *device = -1;
229
230 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
231 if (param_val >= 0) {
232 *card = atoi(value);
233 }
234
235 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
236 if (param_val >= 0) {
237 *device = atoi(value);
238 }
239
240 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800241
242 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700243}
244
Andy Hung4e6a1c02017-10-27 20:31:46 -0700245static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800246{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700247 if (profile->card < 0 || profile->device < 0) {
248 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800249 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700250
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700251 struct str_parms *query = str_parms_create_str(keys);
252 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700253
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700254 /* These keys are from hardware/libhardware/include/audio.h */
255 /* supported sample rates */
256 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
257 char* rates_list = profile_get_sample_rate_strs(profile);
258 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
259 rates_list);
260 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700261 }
262
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700263 /* supported channel counts */
264 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
265 char* channels_list = profile_get_channel_count_strs(profile);
266 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
267 channels_list);
268 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700269 }
270
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700271 /* supported sample formats */
272 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
273 char * format_params = profile_get_format_strs(profile);
274 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
275 format_params);
276 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700277 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700278 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700279
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700280 char* result_str = str_parms_to_str(result);
281 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700282
Paul McLean65ec72b2015-02-18 09:13:24 -0800283 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700284
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700285 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800286}
287
288/*
289 * HAl Functions
290 */
Simon Wilson19957a32012-04-06 16:17:12 -0700291/**
292 * NOTE: when multiple mutexes have to be acquired, always respect the
293 * following order: hw device > out stream
294 */
295
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700296/*
297 * OUT functions
298 */
Simon Wilson19957a32012-04-06 16:17:12 -0700299static uint32_t out_get_sample_rate(const struct audio_stream *stream)
300{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700301 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
302 ALOGV("out_get_sample_rate() = %d", rate);
303 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700304}
305
306static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
307{
308 return 0;
309}
310
311static size_t out_get_buffer_size(const struct audio_stream *stream)
312{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700313 const struct stream_out* out = (const struct stream_out*)stream;
314 size_t buffer_size =
315 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700316 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700317}
318
319static uint32_t out_get_channels(const struct audio_stream *stream)
320{
Andy Hung03576be2014-07-21 21:16:45 -0700321 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700322 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700323}
324
325static audio_format_t out_get_format(const struct audio_stream *stream)
326{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700327 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
328 * Relies on the framework to provide data in the specified format.
329 * This could change in the future.
330 */
331 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
332 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700333 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700334}
335
336static int out_set_format(struct audio_stream *stream, audio_format_t format)
337{
338 return 0;
339}
340
341static int out_standby(struct audio_stream *stream)
342{
343 struct stream_out *out = (struct stream_out *)stream;
344
Paul McLean994ac072016-06-02 15:33:24 -0600345 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700346 if (!out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600347 device_lock(out->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700348 proxy_close(&out->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600349 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700350 out->standby = true;
351 }
Paul McLean994ac072016-06-02 15:33:24 -0600352 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700353 return 0;
354}
355
Paul McLean6a75e4e2016-05-25 14:09:02 -0600356static int out_dump(const struct audio_stream *stream, int fd) {
357 const struct stream_out* out_stream = (const struct stream_out*) stream;
358
359 if (out_stream != NULL) {
360 dprintf(fd, "Output Profile:\n");
361 profile_dump(out_stream->profile, fd);
362
363 dprintf(fd, "Output Proxy:\n");
364 proxy_dump(&out_stream->proxy, fd);
365 }
366
Simon Wilson19957a32012-04-06 16:17:12 -0700367 return 0;
368}
369
370static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
371{
Paul McLean65ec72b2015-02-18 09:13:24 -0800372 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800373
Simon Wilson19957a32012-04-06 16:17:12 -0700374 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700375
Paul McLeaneedc92e2013-12-19 15:46:15 -0800376 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700377 int card = -1;
378 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700379
Paul McLean65ec72b2015-02-18 09:13:24 -0800380 if (!parse_card_device_params(kvpairs, &card, &device)) {
381 // nothing to do
382 return ret_value;
383 }
384
Paul McLean994ac072016-06-02 15:33:24 -0600385 stream_lock(&out->lock);
Paul McLean65ec72b2015-02-18 09:13:24 -0800386 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600387 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700388
Paul McLean65ec72b2015-02-18 09:13:24 -0800389 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700390 /* cannot read pcm device info if playback is active */
391 if (!out->standby)
392 ret_value = -ENOSYS;
393 else {
394 int saved_card = out->profile->card;
395 int saved_device = out->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700396 out->adev->out_profile.card = card;
397 out->adev->out_profile.device = device;
398 ret_value = profile_read_device_info(&out->adev->out_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700399 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700400 out->adev->out_profile.card = saved_card;
401 out->adev->out_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700402 }
403 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800404 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700405
Paul McLean994ac072016-06-02 15:33:24 -0600406 device_unlock(out->adev);
407 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700408
Paul McLeaneedc92e2013-12-19 15:46:15 -0800409 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700410}
411
Paul McLeanf62d75e2014-07-11 15:14:19 -0700412static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
413{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700414 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600415 stream_lock(&out->lock);
416 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700417
418 char * params_str = device_get_parameters(out->profile, keys);
419
Paul McLean994ac072016-06-02 15:33:24 -0600420 device_unlock(out->adev);
421 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700422 return params_str;
423}
424
Simon Wilson19957a32012-04-06 16:17:12 -0700425static uint32_t out_get_latency(const struct audio_stream_out *stream)
426{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700427 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
428 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700429}
430
Paul McLean30f41852014-04-16 15:44:20 -0700431static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700432{
433 return -ENOSYS;
434}
435
Paul McLeaneedc92e2013-12-19 15:46:15 -0800436/* must be called with hw device and output stream mutexes locked */
437static int start_output_stream(struct stream_out *out)
438{
Paul McLean65ec72b2015-02-18 09:13:24 -0800439 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800440
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700441 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800442}
443
444static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700445{
446 int ret;
447 struct stream_out *out = (struct stream_out *)stream;
448
Paul McLean994ac072016-06-02 15:33:24 -0600449 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700450 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600451 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700452 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600453 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700454 if (ret != 0) {
455 goto err;
456 }
457 out->standby = false;
458 }
Eric Laurent05333d42014-08-04 20:29:17 -0700459
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700460 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700461 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800462 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700463 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
464 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700465 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700466 /* allocate buffer */
467 const size_t required_conversion_buffer_size =
468 bytes * num_device_channels / num_req_channels;
469 if (required_conversion_buffer_size > out->conversion_buffer_size) {
470 out->conversion_buffer_size = required_conversion_buffer_size;
471 out->conversion_buffer = realloc(out->conversion_buffer,
472 out->conversion_buffer_size);
473 }
474 /* convert data */
475 const audio_format_t audio_format = out_get_format(&(out->stream.common));
476 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800477 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700478 adjust_channels(write_buff, num_req_channels,
479 out->conversion_buffer, num_device_channels,
480 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800481 write_buff = out->conversion_buffer;
482 }
483
Paul McLeaneedc92e2013-12-19 15:46:15 -0800484 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700485 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800486 }
Simon Wilson19957a32012-04-06 16:17:12 -0700487
Paul McLean994ac072016-06-02 15:33:24 -0600488 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700489
490 return bytes;
491
492err:
Paul McLean994ac072016-06-02 15:33:24 -0600493 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700494 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700495 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700496 out_get_sample_rate(&stream->common));
497 }
498
499 return bytes;
500}
501
Paul McLean30f41852014-04-16 15:44:20 -0700502static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700503{
504 return -EINVAL;
505}
506
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700507static int out_get_presentation_position(const struct audio_stream_out *stream,
508 uint64_t *frames, struct timespec *timestamp)
509{
Andy Hungc9515ce2015-08-04 15:05:19 -0700510 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600511 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700512
513 const alsa_device_proxy *proxy = &out->proxy;
514 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
515
Paul McLean994ac072016-06-02 15:33:24 -0600516 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700517 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700518}
519
Simon Wilson19957a32012-04-06 16:17:12 -0700520static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
521{
522 return 0;
523}
524
525static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
526{
527 return 0;
528}
529
Paul McLean30f41852014-04-16 15:44:20 -0700530static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700531{
532 return -EINVAL;
533}
534
Paul McLean76dba682016-06-01 12:29:11 -0600535static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700536 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600537 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700538 audio_output_flags_t flags,
539 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700540 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700541 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700542{
Paul McLean76dba682016-06-01 12:29:11 -0600543 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
544 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800545
Simon Wilson19957a32012-04-06 16:17:12 -0700546 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700547
548 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600549 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700550 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600551 }
Simon Wilson19957a32012-04-06 16:17:12 -0700552
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700553 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700554 out->stream.common.get_sample_rate = out_get_sample_rate;
555 out->stream.common.set_sample_rate = out_set_sample_rate;
556 out->stream.common.get_buffer_size = out_get_buffer_size;
557 out->stream.common.get_channels = out_get_channels;
558 out->stream.common.get_format = out_get_format;
559 out->stream.common.set_format = out_set_format;
560 out->stream.common.standby = out_standby;
561 out->stream.common.dump = out_dump;
562 out->stream.common.set_parameters = out_set_parameters;
563 out->stream.common.get_parameters = out_get_parameters;
564 out->stream.common.add_audio_effect = out_add_audio_effect;
565 out->stream.common.remove_audio_effect = out_remove_audio_effect;
566 out->stream.get_latency = out_get_latency;
567 out->stream.set_volume = out_set_volume;
568 out->stream.write = out_write;
569 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700570 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700571 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
572
Paul McLean994ac072016-06-02 15:33:24 -0600573 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700574
Paul McLean76dba682016-06-01 12:29:11 -0600575 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600576 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600577 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700578
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700579 // build this to hand to the alsa_device_proxy
580 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700581 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800582
Paul McLean0f1753e2014-12-02 12:36:45 -0700583 /* Pull out the card/device pair */
Andy Hung4e6a1c02017-10-27 20:31:46 -0700584 parse_card_device_params(address, &out->adev->out_profile.card, &out->adev->out_profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700585
Andy Hung4e6a1c02017-10-27 20:31:46 -0700586 profile_read_device_info(&out->adev->out_profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700587
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700588 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800589
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700590 /* Rate */
591 if (config->sample_rate == 0) {
592 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
593 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
594 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800595 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700596 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
597 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800598 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800599
Paul McLean76dba682016-06-01 12:29:11 -0600600 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600601 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600602
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700603 /* Format */
604 if (config->format == AUDIO_FORMAT_DEFAULT) {
605 proxy_config.format = profile_get_default_format(out->profile);
606 config->format = audio_format_from_pcm_format(proxy_config.format);
607 } else {
608 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
609 if (profile_is_format_valid(out->profile, fmt)) {
610 proxy_config.format = fmt;
611 } else {
612 proxy_config.format = profile_get_default_format(out->profile);
613 config->format = audio_format_from_pcm_format(proxy_config.format);
614 ret = -EINVAL;
615 }
616 }
617
618 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600619 bool calc_mask = false;
620 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
621 /* query case */
622 out->hal_channel_count = profile_get_default_channel_count(out->profile);
623 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700624 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600625 /* explicit case */
626 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700627 }
Paul McLean698dbd72015-11-03 12:24:30 -0800628
Paul McLean64345f82016-06-06 13:26:05 -0600629 /* The Framework is currently limited to no more than this number of channels */
630 if (out->hal_channel_count > FCC_8) {
631 out->hal_channel_count = FCC_8;
632 calc_mask = true;
633 }
634
635 if (calc_mask) {
636 /* need to calculate the mask from channel count either because this is the query case
637 * or the specified mask isn't valid for this device, or is more then the FW can handle */
638 config->channel_mask = out->hal_channel_count <= FCC_2
639 /* position mask for mono and stereo*/
640 ? audio_channel_out_mask_from_count(out->hal_channel_count)
641 /* otherwise indexed */
642 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
643 }
644
Andy Hung182ddc72015-05-05 23:37:30 -0700645 out->hal_channel_mask = config->channel_mask;
646
Paul McLean64345f82016-06-06 13:26:05 -0600647 // Validate the "logical" channel count against support in the "actual" profile.
648 // if they differ, choose the "actual" number of channels *closest* to the "logical".
649 // and store THAT in proxy_config.channels
650 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700651 proxy_prepare(&out->proxy, out->profile, &proxy_config);
652
Paul McLean96c4d302017-12-05 09:50:26 -0800653 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
654 * So clear any errors that may have occurred above.
655 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700656 ret = 0;
657
Paul McLeaneedc92e2013-12-19 15:46:15 -0800658 out->conversion_buffer = NULL;
659 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700660
661 out->standby = true;
662
Paul McLean6a75e4e2016-05-25 14:09:02 -0600663 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600664 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600665
Simon Wilson19957a32012-04-06 16:17:12 -0700666 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700667
668 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700669}
670
Paul McLean76dba682016-06-01 12:29:11 -0600671static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700672 struct audio_stream_out *stream)
673{
674 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800675 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700676
Paul McLean76dba682016-06-01 12:29:11 -0600677 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600678
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700679 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700680 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800681
682 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700683
Paul McLeaneedc92e2013-12-19 15:46:15 -0800684 out->conversion_buffer = NULL;
685 out->conversion_buffer_size = 0;
686
Paul McLean994ac072016-06-02 15:33:24 -0600687 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600688 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600689 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600690
Simon Wilson19957a32012-04-06 16:17:12 -0700691 free(stream);
692}
693
Paul McLean76dba682016-06-01 12:29:11 -0600694static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700695 const struct audio_config *config)
696{
697 /* TODO This needs to be calculated based on format/channels/rate */
698 return 320;
699}
700
701/*
702 * IN functions
703 */
704static uint32_t in_get_sample_rate(const struct audio_stream *stream)
705{
706 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
707 ALOGV("in_get_sample_rate() = %d", rate);
708 return rate;
709}
710
711static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
712{
713 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
714 return -ENOSYS;
715}
716
717static size_t in_get_buffer_size(const struct audio_stream *stream)
718{
719 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700720 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700721}
722
723static uint32_t in_get_channels(const struct audio_stream *stream)
724{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700725 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700726 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700727}
728
729static audio_format_t in_get_format(const struct audio_stream *stream)
730{
Andy Hung780f1f82015-04-22 22:14:39 -0700731 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
732 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
733 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700734}
735
736static int in_set_format(struct audio_stream *stream, audio_format_t format)
737{
738 ALOGV("in_set_format(%d) - NOPE", format);
739
740 return -ENOSYS;
741}
742
743static int in_standby(struct audio_stream *stream)
744{
745 struct stream_in *in = (struct stream_in *)stream;
746
Paul McLean994ac072016-06-02 15:33:24 -0600747 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700748 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600749 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700750 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600751 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700752 in->standby = true;
753 }
754
Paul McLean994ac072016-06-02 15:33:24 -0600755 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700756
757 return 0;
758}
759
760static int in_dump(const struct audio_stream *stream, int fd)
761{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600762 const struct stream_in* in_stream = (const struct stream_in*)stream;
763 if (in_stream != NULL) {
764 dprintf(fd, "Input Profile:\n");
765 profile_dump(in_stream->profile, fd);
766
767 dprintf(fd, "Input Proxy:\n");
768 proxy_dump(&in_stream->proxy, fd);
769 }
770
771 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700772}
773
774static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
775{
Paul McLean65ec72b2015-02-18 09:13:24 -0800776 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700777
778 struct stream_in *in = (struct stream_in *)stream;
779
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700780 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700781 int card = -1;
782 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700783
Paul McLean65ec72b2015-02-18 09:13:24 -0800784 if (!parse_card_device_params(kvpairs, &card, &device)) {
785 // nothing to do
786 return ret_value;
787 }
788
Paul McLean994ac072016-06-02 15:33:24 -0600789 stream_lock(&in->lock);
790 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700791
Paul McLean2c6196f2014-08-20 16:50:25 -0700792 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700793 /* cannot read pcm device info if playback is active, or more than one open stream */
794 if (!in->standby || in->adev->inputs_open > 1)
Eric Laurent05333d42014-08-04 20:29:17 -0700795 ret_value = -ENOSYS;
796 else {
797 int saved_card = in->profile->card;
798 int saved_device = in->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700799 in->adev->in_profile.card = card;
800 in->adev->in_profile.device = device;
801 ret_value = profile_read_device_info(&in->adev->in_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700802 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700803 in->adev->in_profile.card = saved_card;
804 in->adev->in_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700805 }
806 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700807 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700808
Paul McLean994ac072016-06-02 15:33:24 -0600809 device_unlock(in->adev);
810 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700811
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700812 return ret_value;
813}
814
815static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
816{
817 struct stream_in *in = (struct stream_in *)stream;
818
Paul McLean994ac072016-06-02 15:33:24 -0600819 stream_lock(&in->lock);
820 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700821
822 char * params_str = device_get_parameters(in->profile, keys);
823
Paul McLean994ac072016-06-02 15:33:24 -0600824 device_unlock(in->adev);
825 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700826
827 return params_str;
828}
829
830static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
831{
832 return 0;
833}
834
835static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
836{
837 return 0;
838}
839
840static int in_set_gain(struct audio_stream_in *stream, float gain)
841{
842 return 0;
843}
844
845/* must be called with hw device and output stream mutexes locked */
846static int start_input_stream(struct stream_in *in)
847{
Paul McLean1d585cc2016-05-24 11:28:10 -0600848 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700849
850 return proxy_open(&in->proxy);
851}
852
853/* TODO mutex stuff here (see out_write) */
854static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
855{
856 size_t num_read_buff_bytes = 0;
857 void * read_buff = buffer;
858 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800859 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700860
861 struct stream_in * in = (struct stream_in *)stream;
862
Paul McLean994ac072016-06-02 15:33:24 -0600863 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700864 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600865 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700866 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600867 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700868 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700869 goto err;
870 }
871 in->standby = false;
872 }
873
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700874 /*
875 * OK, we need to figure out how much data to read to be able to output the requested
876 * number of bytes in the HAL format (16-bit, stereo).
877 */
878 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700879 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
880 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700881
882 if (num_device_channels != num_req_channels) {
883 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
884 }
885
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700886 /* Setup/Realloc the conversion buffer (if necessary). */
887 if (num_read_buff_bytes != bytes) {
888 if (num_read_buff_bytes > in->conversion_buffer_size) {
889 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
890 (and do these conversions themselves) */
891 in->conversion_buffer_size = num_read_buff_bytes;
892 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
893 }
894 read_buff = in->conversion_buffer;
895 }
896
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800897 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
898 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700899 if (num_device_channels != num_req_channels) {
900 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
901
902 out_buff = buffer;
903 /* Num Channels conversion */
904 if (num_device_channels != num_req_channels) {
905 audio_format_t audio_format = in_get_format(&(in->stream.common));
906 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
907
908 num_read_buff_bytes =
909 adjust_channels(read_buff, num_device_channels,
910 out_buff, num_req_channels,
911 sample_size_in_bytes, num_read_buff_bytes);
912 }
913 }
Eric Laurent253def92014-09-14 12:18:18 -0700914
Paul McLean76dba682016-06-01 12:29:11 -0600915 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
916 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700917 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530918 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800919 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700920 }
921
922err:
Paul McLean994ac072016-06-02 15:33:24 -0600923 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700924 return num_read_buff_bytes;
925}
926
927static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
928{
929 return 0;
930}
931
Andy Hungfa6b4a62018-06-04 19:14:22 -0700932static int in_get_capture_position(const struct audio_stream_in *stream,
933 int64_t *frames, int64_t *time)
934{
935 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
936 stream_lock(&in->lock);
937
938 const alsa_device_proxy *proxy = &in->proxy;
939 const int ret = proxy_get_capture_position(proxy, frames, time);
940
941 stream_unlock(&in->lock);
942 return ret;
943}
944
Paul McLean76dba682016-06-01 12:29:11 -0600945static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600947 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948 struct audio_config *config,
949 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700950 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -0700951 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -0700952 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700953{
Paul McLean1d585cc2016-05-24 11:28:10 -0600954 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700955 config->sample_rate, config->channel_mask, config->format);
956
Andy Hungb10ce6d2017-10-27 19:37:58 -0700957 /* Pull out the card/device pair */
958 int32_t card, device;
959 if (!parse_card_device_params(address, &card, &device)) {
960 ALOGW("%s fail - invalid address %s", __func__, address);
961 *stream_in = NULL;
962 return -EINVAL;
963 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700964
Andy Hungb10ce6d2017-10-27 19:37:58 -0700965 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -0600966 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700967 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700968 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600969 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700970
971 /* setup function pointers */
972 in->stream.common.get_sample_rate = in_get_sample_rate;
973 in->stream.common.set_sample_rate = in_set_sample_rate;
974 in->stream.common.get_buffer_size = in_get_buffer_size;
975 in->stream.common.get_channels = in_get_channels;
976 in->stream.common.get_format = in_get_format;
977 in->stream.common.set_format = in_set_format;
978 in->stream.common.standby = in_standby;
979 in->stream.common.dump = in_dump;
980 in->stream.common.set_parameters = in_set_parameters;
981 in->stream.common.get_parameters = in_get_parameters;
982 in->stream.common.add_audio_effect = in_add_audio_effect;
983 in->stream.common.remove_audio_effect = in_remove_audio_effect;
984
985 in->stream.set_gain = in_set_gain;
986 in->stream.read = in_read;
987 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -0700988 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700989
Paul McLean994ac072016-06-02 15:33:24 -0600990 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700991
Paul McLean76dba682016-06-01 12:29:11 -0600992 in->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600993 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700994
Paul McLean76dba682016-06-01 12:29:11 -0600995 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700996
997 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700998 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700999
Andy Hungb10ce6d2017-10-27 19:37:58 -07001000 int ret = 0;
1001 /* Check if an input stream is already open */
1002 if (in->adev->inputs_open > 0) {
1003 if (!profile_is_cached_for(in->profile, card, device)) {
1004 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1005 __func__, card, device);
1006 ret = -EINVAL;
1007 }
1008 } else {
1009 /* Read input profile only if necessary */
Andy Hung4e6a1c02017-10-27 20:31:46 -07001010 in->adev->in_profile.card = card;
1011 in->adev->in_profile.device = device;
1012 if (!profile_read_device_info(&in->adev->in_profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001013 ALOGW("%s fail - cannot read profile", __func__);
1014 ret = -EINVAL;
1015 }
1016 }
1017 if (ret != 0) {
1018 device_unlock(in->adev);
1019 free(in);
1020 *stream_in = NULL;
1021 return ret;
1022 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001023
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001024 /* Rate */
1025 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -06001026 config->sample_rate = profile_get_default_sample_rate(in->profile);
1027 }
1028
Paul McLean76dba682016-06-01 12:29:11 -06001029 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
1030 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
1031 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
1032 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001033 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Eric Laurent981f7742017-05-03 12:44:26 -07001034 proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001035 } else {
1036 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
1037 ret = -EINVAL;
1038 }
Paul McLean994ac072016-06-02 15:33:24 -06001039 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001040
1041 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001042 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -07001043 proxy_config.format = profile_get_default_format(in->profile);
1044 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001045 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001046 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1047 if (profile_is_format_valid(in->profile, fmt)) {
1048 proxy_config.format = fmt;
1049 } else {
1050 proxy_config.format = profile_get_default_format(in->profile);
1051 config->format = audio_format_from_pcm_format(proxy_config.format);
1052 ret = -EINVAL;
1053 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001054 }
1055
Paul McLean2cfd81b2014-09-15 12:32:23 -07001056 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001057 bool calc_mask = false;
1058 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1059 /* query case */
1060 in->hal_channel_count = profile_get_default_channel_count(in->profile);
1061 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001062 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001063 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001064 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1065 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001066
Paul McLean64345f82016-06-06 13:26:05 -06001067 /* The Framework is currently limited to no more than this number of channels */
1068 if (in->hal_channel_count > FCC_8) {
1069 in->hal_channel_count = FCC_8;
1070 calc_mask = true;
1071 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001072
Paul McLean64345f82016-06-06 13:26:05 -06001073 if (calc_mask) {
1074 /* need to calculate the mask from channel count either because this is the query case
1075 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1076 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1077 /* position mask for mono & stereo */
1078 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1079 /* otherwise indexed */
1080 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001081
Paul McLean64345f82016-06-06 13:26:05 -06001082 // if we change the mask...
1083 if (in->hal_channel_mask != config->channel_mask &&
1084 config->channel_mask != AUDIO_CHANNEL_NONE) {
1085 config->channel_mask = in->hal_channel_mask;
1086 ret = -EINVAL;
1087 }
1088 } else {
1089 in->hal_channel_mask = config->channel_mask;
1090 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001091
Paul McLean64345f82016-06-06 13:26:05 -06001092 if (ret == 0) {
1093 // Validate the "logical" channel count against support in the "actual" profile.
1094 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1095 // and store THAT in proxy_config.channels
1096 proxy_config.channels =
1097 profile_get_closest_channel_count(in->profile, in->hal_channel_count);
Eric Laurent981f7742017-05-03 12:44:26 -07001098 ret = proxy_prepare(&in->proxy, in->profile, &proxy_config);
1099 if (ret == 0) {
1100 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001101
Eric Laurent981f7742017-05-03 12:44:26 -07001102 in->conversion_buffer = NULL;
1103 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001104
Eric Laurent981f7742017-05-03 12:44:26 -07001105 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001106
Eric Laurent981f7742017-05-03 12:44:26 -07001107 /* Save this for adev_dump() */
1108 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1109 } else {
1110 ALOGW("proxy_prepare error %d", ret);
1111 unsigned channel_count = proxy_get_channel_count(&in->proxy);
1112 config->channel_mask = channel_count <= FCC_2
1113 ? audio_channel_in_mask_from_count(channel_count)
1114 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1115 config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy));
1116 config->sample_rate = proxy_get_sample_rate(&in->proxy);
1117 }
1118 }
Paul McLean64345f82016-06-06 13:26:05 -06001119
Eric Laurent981f7742017-05-03 12:44:26 -07001120 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001121 // Deallocate this stream on error, because AudioFlinger won't call
1122 // adev_close_input_stream() in this case.
1123 *stream_in = NULL;
1124 free(in);
1125 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001126
Andy Hungb10ce6d2017-10-27 19:37:58 -07001127 device_lock(in->adev);
1128 ++in->adev->inputs_open;
1129 device_unlock(in->adev);
1130
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001131 return ret;
1132}
1133
Paul McLean76dba682016-06-01 12:29:11 -06001134static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1135 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001136{
1137 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001138 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1139
Paul McLean76dba682016-06-01 12:29:11 -06001140 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001141
Andy Hungb10ce6d2017-10-27 19:37:58 -07001142 device_lock(in->adev);
1143 --in->adev->inputs_open;
1144 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1145 "invalid inputs_open: %d", in->adev->inputs_open);
1146 device_unlock(in->adev);
1147
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001148 /* Close the pcm device */
1149 in_standby(&stream->common);
1150
1151 free(in->conversion_buffer);
1152
1153 free(stream);
1154}
1155
1156/*
1157 * ADEV Functions
1158 */
Paul McLean76dba682016-06-01 12:29:11 -06001159static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001160{
1161 return 0;
1162}
1163
Paul McLean76dba682016-06-01 12:29:11 -06001164static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001165{
1166 return strdup("");
1167}
1168
Paul McLean76dba682016-06-01 12:29:11 -06001169static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001170{
1171 return 0;
1172}
1173
Paul McLean76dba682016-06-01 12:29:11 -06001174static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001175{
1176 return -ENOSYS;
1177}
1178
Paul McLean76dba682016-06-01 12:29:11 -06001179static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001180{
1181 return -ENOSYS;
1182}
1183
Paul McLean76dba682016-06-01 12:29:11 -06001184static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001185{
1186 return 0;
1187}
1188
Paul McLean76dba682016-06-01 12:29:11 -06001189static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001190{
Paul McLean76dba682016-06-01 12:29:11 -06001191 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001192 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001193 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001194 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001195 return -ENOSYS;
1196}
1197
Paul McLean76dba682016-06-01 12:29:11 -06001198static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001199{
1200 return -ENOSYS;
1201}
1202
Paul McLean76dba682016-06-01 12:29:11 -06001203static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001204{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001205 dprintf(fd, "\nUSB audio module:\n");
1206
1207 struct audio_device* adev = (struct audio_device*)device;
1208 const int kNumRetries = 3;
1209 const int kSleepTimeMS = 500;
1210
Paul McLean994ac072016-06-02 15:33:24 -06001211 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001212 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001213 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001214 sleep(kSleepTimeMS);
1215 retry--;
1216 }
1217
1218 if (retry > 0) {
1219 if (list_empty(&adev->output_stream_list)) {
1220 dprintf(fd, " No output streams.\n");
1221 } else {
1222 struct listnode* node;
1223 list_for_each(node, &adev->output_stream_list) {
1224 struct audio_stream* stream =
1225 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1226 out_dump(stream, fd);
1227 }
1228 }
1229
1230 if (list_empty(&adev->input_stream_list)) {
1231 dprintf(fd, "\n No input streams.\n");
1232 } else {
1233 struct listnode* node;
1234 list_for_each(node, &adev->input_stream_list) {
1235 struct audio_stream* stream =
1236 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1237 in_dump(stream, fd);
1238 }
1239 }
1240
Paul McLean994ac072016-06-02 15:33:24 -06001241 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001242 } else {
1243 // Couldn't lock
1244 dprintf(fd, " Could not obtain device lock.\n");
1245 }
1246
Simon Wilson19957a32012-04-06 16:17:12 -07001247 return 0;
1248}
1249
1250static int adev_close(hw_device_t *device)
1251{
Simon Wilson19957a32012-04-06 16:17:12 -07001252 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001253
Simon Wilson19957a32012-04-06 16:17:12 -07001254 return 0;
1255}
1256
Paul McLean30f41852014-04-16 15:44:20 -07001257static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001258{
Simon Wilson19957a32012-04-06 16:17:12 -07001259 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1260 return -EINVAL;
1261
Paul McLeaneedc92e2013-12-19 15:46:15 -08001262 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001263 if (!adev)
1264 return -ENOMEM;
1265
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001266 profile_init(&adev->out_profile, PCM_OUT);
1267 profile_init(&adev->in_profile, PCM_IN);
1268
Paul McLean6a75e4e2016-05-25 14:09:02 -06001269 list_init(&adev->output_stream_list);
1270 list_init(&adev->input_stream_list);
1271
Simon Wilson19957a32012-04-06 16:17:12 -07001272 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001273 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001274 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001275 adev->hw_device.common.close = adev_close;
1276
Simon Wilson19957a32012-04-06 16:17:12 -07001277 adev->hw_device.init_check = adev_init_check;
1278 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1279 adev->hw_device.set_master_volume = adev_set_master_volume;
1280 adev->hw_device.set_mode = adev_set_mode;
1281 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1282 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1283 adev->hw_device.set_parameters = adev_set_parameters;
1284 adev->hw_device.get_parameters = adev_get_parameters;
1285 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1286 adev->hw_device.open_output_stream = adev_open_output_stream;
1287 adev->hw_device.close_output_stream = adev_close_output_stream;
1288 adev->hw_device.open_input_stream = adev_open_input_stream;
1289 adev->hw_device.close_input_stream = adev_close_input_stream;
1290 adev->hw_device.dump = adev_dump;
1291
1292 *device = &adev->hw_device.common;
1293
1294 return 0;
1295}
1296
1297static struct hw_module_methods_t hal_module_methods = {
1298 .open = adev_open,
1299};
1300
1301struct audio_module HAL_MODULE_INFO_SYM = {
1302 .common = {
1303 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001304 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1305 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001306 .id = AUDIO_HARDWARE_MODULE_ID,
1307 .name = "USB audio HW HAL",
1308 .author = "The Android Open Source Project",
1309 .methods = &hal_module_methods,
1310 },
1311};