blob: 4a946e7f2252e7e36aff5dcc728dcdea4fc410ea [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "usb_audio_hw"
Paul McLeancf611912014-04-28 13:03:18 -070018/*#define LOG_NDEBUG 0*/
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070025#include <sys/time.h>
Simon Wilson19957a32012-04-06 16:17:12 -070026
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <log/log.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028#include <cutils/str_parms.h>
29#include <cutils/properties.h>
30
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070032#include <hardware/audio_alsaops.h>
33#include <hardware/hardware.h>
34
35#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070036
37#include <tinyalsa/asoundlib.h>
38
Paul McLeanc2201152014-07-16 13:46:07 -070039#include <audio_utils/channels.h>
40
Paul McLeanc88e6ae2014-07-16 09:48:34 -070041#include "alsa_device_profile.h"
42#include "alsa_device_proxy.h"
43#include "logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070044
Paul McLeanc88e6ae2014-07-16 09:48:34 -070045#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070046
Simon Wilson19957a32012-04-06 16:17:12 -070047struct audio_device {
48 struct audio_hw_device hw_device;
49
50 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080051
52 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070053 alsa_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080054
55 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070056 alsa_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080057
Simon Wilson19957a32012-04-06 16:17:12 -070058 bool standby;
59};
60
61struct stream_out {
62 struct audio_stream_out stream;
63
Paul McLeaneedc92e2013-12-19 15:46:15 -080064 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080065 bool standby;
66
Paul McLeanc88e6ae2014-07-16 09:48:34 -070067 struct audio_device *dev; /* hardware information - only using this for the lock */
68
69 alsa_device_profile * profile;
70 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080071
72 void * conversion_buffer; /* any conversions are put into here
73 * they could come from here too if
74 * there was a previous conversion */
75 size_t conversion_buffer_size; /* in bytes */
76};
77
Paul McLeaneedc92e2013-12-19 15:46:15 -080078struct stream_in {
79 struct audio_stream_in stream;
80
Simon Wilson19957a32012-04-06 16:17:12 -070081 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Simon Wilson19957a32012-04-06 16:17:12 -070082 bool standby;
83
Paul McLeanc88e6ae2014-07-16 09:48:34 -070084 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -080085
Paul McLeanc88e6ae2014-07-16 09:48:34 -070086 alsa_device_profile * profile;
87 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -070088
Paul McLeanc88e6ae2014-07-16 09:48:34 -070089 // not used?
90 // struct audio_config hal_pcm_config;
Paul McLeaneedc92e2013-12-19 15:46:15 -080091
Paul McLeanc88e6ae2014-07-16 09:48:34 -070092 /* 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 -070093 void * conversion_buffer; /* any conversions are put into here
94 * they could come from here too if
95 * there was a previous conversion */
96 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -070097};
98
Paul McLeaneedc92e2013-12-19 15:46:15 -080099/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800100 * Data Conversions
101 */
102/*
Paul McLean30f41852014-04-16 15:44:20 -0700103 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
104 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800105 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700106 * out_buff points to the buffer to receive converted PCM16LE LE samples.
107 * returns
108 * the number of BYTES of output data.
109 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
110 * support PCM24_3LE (24-bit, packed).
111 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700112 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700113 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700114 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700115static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
116 short * out_buff)
117{
Paul McLean30f41852014-04-16 15:44:20 -0700118 /*
119 * Move from front to back so that the conversion can be done in-place
120 * i.e. in_buff == out_buff
121 */
122 /* we need 2 bytes in the output for every 3 bytes in the input */
123 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700124 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700125 size_t src_smpl_index;
126 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
127 src_ptr++; /* lowest-(skip)-byte */
128 *dst_ptr++ = *src_ptr++; /* low-byte */
129 *dst_ptr++ = *src_ptr++; /* high-byte */
130 }
131
132 /* return number of *bytes* generated: */
133 return num_in_samples * 2;
134}
135
136/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700137 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
138 * in_buff points to the buffer of PCM32 samples
139 * num_in_samples size of input buffer in SAMPLES
140 * out_buff points to the buffer to receive converted PCM16LE LE samples.
141 * returns
142 * the number of BYTES of output data.
143 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
144 * support PCM_FORMAT_S32_LE (32-bit).
145 * NOTE:
146 * This conversion is safe to do in-place (in_buff == out_buff).
147 * TODO Move this to a utilities module.
148 */
149static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
150{
151 /*
152 * Move from front to back so that the conversion can be done in-place
153 * i.e. in_buff == out_buff
154 */
155
156 short * dst_ptr = out_buff;
157 const int32_t* src_ptr = in_buff;
158 size_t src_smpl_index;
159 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
160 *dst_ptr++ = *src_ptr++ >> 16;
161 }
162
163 /* return number of *bytes* generated: */
164 return num_in_samples * 2;
165}
166
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700167static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800168{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700169 ALOGV("usb:audio_hw::device_get_parameters() keys:%s", keys);
Paul McLeane32cbc12014-06-25 10:42:07 -0700170
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700171 if (profile->card < 0 || profile->device < 0) {
172 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800173 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700174
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700175 struct str_parms *query = str_parms_create_str(keys);
176 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700177
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700178 /* These keys are from hardware/libhardware/include/audio.h */
179 /* supported sample rates */
180 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
181 char* rates_list = profile_get_sample_rate_strs(profile);
182 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
183 rates_list);
184 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700185 }
186
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700187 /* supported channel counts */
188 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
189 char* channels_list = profile_get_channel_count_strs(profile);
190 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
191 channels_list);
192 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700193 }
194
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700195 /* supported sample formats */
196 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
197 char * format_params = profile_get_format_strs(profile);
198 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
199 format_params);
200 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700201 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700202 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700203
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700204 char* result_str = str_parms_to_str(result);
205 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700206
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700207 ALOGV("usb:audio_hw::device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700208
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700209 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800210}
211
212/*
213 * HAl Functions
214 */
Simon Wilson19957a32012-04-06 16:17:12 -0700215/**
216 * NOTE: when multiple mutexes have to be acquired, always respect the
217 * following order: hw device > out stream
218 */
219
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700220/*
221 * OUT functions
222 */
Simon Wilson19957a32012-04-06 16:17:12 -0700223static uint32_t out_get_sample_rate(const struct audio_stream *stream)
224{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700225 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
226 ALOGV("out_get_sample_rate() = %d", rate);
227 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700228}
229
230static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
231{
232 return 0;
233}
234
235static size_t out_get_buffer_size(const struct audio_stream *stream)
236{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700237 const struct stream_out* out = (const struct stream_out*)stream;
238 size_t buffer_size =
239 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
240 ALOGV("out_get_buffer_size() = %zu", buffer_size);
241 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700242}
243
244static uint32_t out_get_channels(const struct audio_stream *stream)
245{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700246 /*
247 * alsa_device_profile * profile = ((struct stream_out*)stream)->profile;
248 * unsigned channel_count = profile_get_channel_count(profile);
249 * uint32_t channel_mask = audio_channel_out_mask_from_count(channel_count);
250 * ALOGV("out_get_channels() = 0x%X count:%d", channel_mask, channel_count);
251 * return channel_mask;
252 */
253
254 /* Always Stereo for now. We will do *some* conversions in this HAL.
255 * TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
256 * rewrite this to return the ACTUAL channel format */
Simon Wilson19957a32012-04-06 16:17:12 -0700257 return AUDIO_CHANNEL_OUT_STEREO;
258}
259
260static audio_format_t out_get_format(const struct audio_stream *stream)
261{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700262 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
263 * Relies on the framework to provide data in the specified format.
264 * This could change in the future.
265 */
266 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
267 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
268 ALOGV("out_get_format() = %d", format);
269 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700270}
271
272static int out_set_format(struct audio_stream *stream, audio_format_t format)
273{
274 return 0;
275}
276
277static int out_standby(struct audio_stream *stream)
278{
279 struct stream_out *out = (struct stream_out *)stream;
280
281 pthread_mutex_lock(&out->dev->lock);
282 pthread_mutex_lock(&out->lock);
283
284 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700285 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700286 out->standby = true;
287 }
288
289 pthread_mutex_unlock(&out->lock);
290 pthread_mutex_unlock(&out->dev->lock);
291
292 return 0;
293}
294
295static int out_dump(const struct audio_stream *stream, int fd)
296{
297 return 0;
298}
299
300static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
301{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800302 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
303
Simon Wilson19957a32012-04-06 16:17:12 -0700304 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700305
Simon Wilson19957a32012-04-06 16:17:12 -0700306 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800307 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700308 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800309 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700310
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700311 struct str_parms * parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700312 pthread_mutex_lock(&out->dev->lock);
313 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700314
Paul McLeaneedc92e2013-12-19 15:46:15 -0800315 bool recache_device_params = false;
316 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
317 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700318 out->profile->card = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800319 recache_device_params = true;
320 }
Simon Wilson19957a32012-04-06 16:17:12 -0700321
Paul McLeaneedc92e2013-12-19 15:46:15 -0800322 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
323 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700324 out->profile->device = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800325 recache_device_params = true;
326 }
327
Paul McLeanf62d75e2014-07-11 15:14:19 -0700328 if (recache_device_params && out->profile->card >= 0 && out->profile->device >= 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700329 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800330 }
Simon Wilson19957a32012-04-06 16:17:12 -0700331
Paul McLeanf62d75e2014-07-11 15:14:19 -0700332 pthread_mutex_unlock(&out->lock);
333 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700334 str_parms_destroy(parms);
335
Paul McLeaneedc92e2013-12-19 15:46:15 -0800336 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700337}
338
Paul McLeanf62d75e2014-07-11 15:14:19 -0700339static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
340{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700341 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700342 pthread_mutex_lock(&out->dev->lock);
343 pthread_mutex_lock(&out->lock);
344
345 char * params_str = device_get_parameters(out->profile, keys);
346
347 pthread_mutex_unlock(&out->lock);
348 pthread_mutex_unlock(&out->dev->lock);
349
350 return params_str;
351}
352
Simon Wilson19957a32012-04-06 16:17:12 -0700353static uint32_t out_get_latency(const struct audio_stream_out *stream)
354{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700355 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
356 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700357}
358
Paul McLean30f41852014-04-16 15:44:20 -0700359static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700360{
361 return -ENOSYS;
362}
363
Paul McLeaneedc92e2013-12-19 15:46:15 -0800364/* must be called with hw device and output stream mutexes locked */
365static int start_output_stream(struct stream_out *out)
366{
Paul McLean30f41852014-04-16 15:44:20 -0700367 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -0700368 out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800369
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700370 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800371}
372
373static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700374{
375 int ret;
376 struct stream_out *out = (struct stream_out *)stream;
377
378 pthread_mutex_lock(&out->dev->lock);
379 pthread_mutex_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700380 pthread_mutex_unlock(&out->dev->lock);
381
Simon Wilson19957a32012-04-06 16:17:12 -0700382 if (out->standby) {
383 ret = start_output_stream(out);
384 if (ret != 0) {
385 goto err;
386 }
387 out->standby = false;
388 }
389
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700390 alsa_device_profile* profile = out->profile;
391 alsa_device_proxy* proxy = &out->proxy;
392
393 /*
394 * Setup conversion buffer
395 * compute maximum potential buffer size.
396 * * 2 for stereo -> quad conversion
397 * * 3/2 for 16bit -> 24 bit conversion
398 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700399 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700400 if (required_conversion_buffer_size > out->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700401 /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
402 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -0700403 out->conversion_buffer_size = required_conversion_buffer_size;
404 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
405 }
406
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700407 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800408 int num_write_buff_bytes = bytes;
409
410 /*
411 * Num Channels conversion
412 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700413 int num_device_channels = proxy_get_channel_count(proxy);
414 int num_req_channels = 2; /* always for now */
415
Paul McLean30f41852014-04-16 15:44:20 -0700416 if (num_device_channels != num_req_channels) {
Paul McLeaneb192972014-07-11 16:29:41 -0700417 audio_format_t audio_format = out_get_format(&(out->stream.common));
418 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800419 num_write_buff_bytes =
Paul McLeaneb192972014-07-11 16:29:41 -0700420 adjust_channels(write_buff, num_req_channels,
421 out->conversion_buffer, num_device_channels,
422 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800423 write_buff = out->conversion_buffer;
424 }
425
Paul McLeaneedc92e2013-12-19 15:46:15 -0800426 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700427 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800428 }
Simon Wilson19957a32012-04-06 16:17:12 -0700429
430 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700431
432 return bytes;
433
434err:
435 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700436 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700437 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700438 out_get_sample_rate(&stream->common));
439 }
440
441 return bytes;
442}
443
Paul McLean30f41852014-04-16 15:44:20 -0700444static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700445{
446 return -EINVAL;
447}
448
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700449static int out_get_presentation_position(const struct audio_stream_out *stream,
450 uint64_t *frames, struct timespec *timestamp)
451{
452 /* FIXME - This needs to be implemented */
453 return -EINVAL;
454}
455
Simon Wilson19957a32012-04-06 16:17:12 -0700456static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
457{
458 return 0;
459}
460
461static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
462{
463 return 0;
464}
465
Paul McLean30f41852014-04-16 15:44:20 -0700466static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700467{
468 return -EINVAL;
469}
470
471static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700472 audio_io_handle_t handle,
473 audio_devices_t devices,
474 audio_output_flags_t flags,
475 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -0700476 struct audio_stream_out **stream_out)
477{
Paul McLean30f41852014-04-16 15:44:20 -0700478 ALOGV("usb:audio_hw::out adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X",
Paul McLeaneedc92e2013-12-19 15:46:15 -0800479 handle, devices, flags);
480
Simon Wilson19957a32012-04-06 16:17:12 -0700481 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800482
Simon Wilson19957a32012-04-06 16:17:12 -0700483 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700484
485 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
486 if (!out)
487 return -ENOMEM;
488
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700489 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700490 out->stream.common.get_sample_rate = out_get_sample_rate;
491 out->stream.common.set_sample_rate = out_set_sample_rate;
492 out->stream.common.get_buffer_size = out_get_buffer_size;
493 out->stream.common.get_channels = out_get_channels;
494 out->stream.common.get_format = out_get_format;
495 out->stream.common.set_format = out_set_format;
496 out->stream.common.standby = out_standby;
497 out->stream.common.dump = out_dump;
498 out->stream.common.set_parameters = out_set_parameters;
499 out->stream.common.get_parameters = out_get_parameters;
500 out->stream.common.add_audio_effect = out_add_audio_effect;
501 out->stream.common.remove_audio_effect = out_remove_audio_effect;
502 out->stream.get_latency = out_get_latency;
503 out->stream.set_volume = out_set_volume;
504 out->stream.write = out_write;
505 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700506 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700507 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
508
509 out->dev = adev;
510
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700511 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700512
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700513 // build this to hand to the alsa_device_proxy
514 struct pcm_config proxy_config;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800515
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700516 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800517
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700518 /* Rate */
519 if (config->sample_rate == 0) {
520 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
521 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
522 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800523 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700524 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
525 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800526 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800527
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700528 /* Format */
529 if (config->format == AUDIO_FORMAT_DEFAULT) {
530 proxy_config.format = profile_get_default_format(out->profile);
531 config->format = audio_format_from_pcm_format(proxy_config.format);
532 } else {
533 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
534 if (profile_is_format_valid(out->profile, fmt)) {
535 proxy_config.format = fmt;
536 } else {
537 proxy_config.format = profile_get_default_format(out->profile);
538 config->format = audio_format_from_pcm_format(proxy_config.format);
539 ret = -EINVAL;
540 }
541 }
542
543 /* Channels */
544 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
545 /* This will be needed when the framework supports non-stereo output */
546 /* config->channel_mask =
547 * audio_channel_out_mask_from_count(profile_get_default_channel_count(out->profile));
548 */
549 proxy_config.channels = profile_get_default_channel_count(out->profile);
550 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
551 } else {
552 /* This will be needed when the framework supports non-stereo output */
553 /*
554 * unsigned channel_count = audio_channel_count_from_out_mask(config->channel_mask);
555 * if (profile_is_channel_count_valid(out->profile, channel_count)) {
556 * proxy_set_channel_count(out->proxy, channel_count);
557 * } else {
558 * config->channel_mask =
559 * audio_channel_out_mask_from_count(proxy_get_channel_count(out->proxy));
560 * ret = -EINVAL;
561 * }
562 */
563 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
564 proxy_config.channels = profile_get_default_channel_count(out->profile);
565 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
566 ret = -EINVAL;
567 } else {
568 proxy_config.channels = profile_get_default_channel_count(out->profile);
569 }
570 }
571
572 proxy_prepare(&out->proxy, out->profile, &proxy_config);
573
574 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
575 ret = 0;
576
Paul McLeaneedc92e2013-12-19 15:46:15 -0800577 out->conversion_buffer = NULL;
578 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700579
580 out->standby = true;
581
582 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700583
584 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700585
586err_open:
587 free(out);
588 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800589 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700590}
591
592static void adev_close_output_stream(struct audio_hw_device *dev,
593 struct audio_stream_out *stream)
594{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800595 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700596 struct stream_out *out = (struct stream_out *)stream;
597
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700598 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700599 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800600
601 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700602
Paul McLeaneedc92e2013-12-19 15:46:15 -0800603 out->conversion_buffer = NULL;
604 out->conversion_buffer_size = 0;
605
Simon Wilson19957a32012-04-06 16:17:12 -0700606 free(stream);
607}
608
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700609static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
610 const struct audio_config *config)
611{
612 /* TODO This needs to be calculated based on format/channels/rate */
613 return 320;
614}
615
616/*
617 * IN functions
618 */
619static uint32_t in_get_sample_rate(const struct audio_stream *stream)
620{
621 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
622 ALOGV("in_get_sample_rate() = %d", rate);
623 return rate;
624}
625
626static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
627{
628 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
629 return -ENOSYS;
630}
631
632static size_t in_get_buffer_size(const struct audio_stream *stream)
633{
634 const struct stream_in * in = ((const struct stream_in*)stream);
635 size_t buffer_size =
636 proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
637 ALOGV("in_get_buffer_size() = %zd", buffer_size);
638
639 return buffer_size;
640}
641
642static uint32_t in_get_channels(const struct audio_stream *stream)
643{
644 /* TODO Here is the code we need when we support arbitrary channel counts
645 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
646 * unsigned channel_count = proxy_get_channel_count(proxy);
647 * uint32_t channel_mask = audio_channel_in_mask_from_count(channel_count);
648 * ALOGV("in_get_channels() = 0x%X count:%d", channel_mask, channel_count);
649 * return channel_mask;
650 */
651 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
652 rewrite this to return the ACTUAL channel format */
653 return AUDIO_CHANNEL_IN_STEREO;
654}
655
656static audio_format_t in_get_format(const struct audio_stream *stream)
657{
658 /* TODO Here is the code we need when we support arbitrary input formats
659 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
660 * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
661 * ALOGV("in_get_format() = %d", format);
662 * return format;
663 */
664 /* Input only supports PCM16 */
665 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
666 rewrite this to return the ACTUAL channel format (above) */
667 return AUDIO_FORMAT_PCM_16_BIT;
668}
669
670static int in_set_format(struct audio_stream *stream, audio_format_t format)
671{
672 ALOGV("in_set_format(%d) - NOPE", format);
673
674 return -ENOSYS;
675}
676
677static int in_standby(struct audio_stream *stream)
678{
679 struct stream_in *in = (struct stream_in *)stream;
680
681 pthread_mutex_lock(&in->dev->lock);
682 pthread_mutex_lock(&in->lock);
683
684 if (!in->standby) {
685 proxy_close(&in->proxy);
686 in->standby = true;
687 }
688
689 pthread_mutex_unlock(&in->lock);
690 pthread_mutex_unlock(&in->dev->lock);
691
692 return 0;
693}
694
695static int in_dump(const struct audio_stream *stream, int fd)
696{
697 return 0;
698}
699
700static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
701{
702 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
703
704 struct stream_in *in = (struct stream_in *)stream;
705
706 char value[32];
707 int param_val;
708 int routing = 0;
709 int ret_value = 0;
710
711 struct str_parms * parms = str_parms_create_str(kvpairs);
712
713 pthread_mutex_lock(&in->dev->lock);
714 pthread_mutex_lock(&in->lock);
715
716 bool recache_device_params = false;
717
718 /* Card/Device */
719 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
720 if (param_val >= 0) {
721 in->profile->card = atoi(value);
722 recache_device_params = true;
723 }
724
725 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
726 if (param_val >= 0) {
727 in->profile->device = atoi(value);
728 recache_device_params = true;
729 }
730
731 if (recache_device_params && in->profile->card >= 0 && in->profile->device >= 0) {
732 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
733 }
734
735 pthread_mutex_unlock(&in->lock);
736 pthread_mutex_unlock(&in->dev->lock);
737
738 str_parms_destroy(parms);
739
740 return ret_value;
741}
742
743static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
744{
745 struct stream_in *in = (struct stream_in *)stream;
746
747 pthread_mutex_lock(&in->dev->lock);
748 pthread_mutex_lock(&in->lock);
749
750 char * params_str = device_get_parameters(in->profile, keys);
751
752 pthread_mutex_unlock(&in->lock);
753 pthread_mutex_unlock(&in->dev->lock);
754
755 return params_str;
756}
757
758static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
759{
760 return 0;
761}
762
763static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
764{
765 return 0;
766}
767
768static int in_set_gain(struct audio_stream_in *stream, float gain)
769{
770 return 0;
771}
772
773/* must be called with hw device and output stream mutexes locked */
774static int start_input_stream(struct stream_in *in)
775{
776 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
777 in->profile->card, in->profile->device);
778
779 return proxy_open(&in->proxy);
780}
781
782/* TODO mutex stuff here (see out_write) */
783static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
784{
785 size_t num_read_buff_bytes = 0;
786 void * read_buff = buffer;
787 void * out_buff = buffer;
788
789 struct stream_in * in = (struct stream_in *)stream;
790
791 pthread_mutex_lock(&in->dev->lock);
792 pthread_mutex_lock(&in->lock);
793 pthread_mutex_unlock(&in->dev->lock);
794
795 if (in->standby) {
796 if (start_input_stream(in) != 0) {
797 goto err;
798 }
799 in->standby = false;
800 }
801
802 alsa_device_profile * profile = in->profile;
803
804 /*
805 * OK, we need to figure out how much data to read to be able to output the requested
806 * number of bytes in the HAL format (16-bit, stereo).
807 */
808 num_read_buff_bytes = bytes;
809 int num_device_channels = proxy_get_channel_count(&in->proxy);
810 int num_req_channels = 2; /* always, for now */
811
812 if (num_device_channels != num_req_channels) {
813 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
814 }
815
816 enum pcm_format format = proxy_get_format(&in->proxy);
817 if (format == PCM_FORMAT_S24_3LE) {
818 /* 24-bit USB device */
819 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
820 } else if (format == PCM_FORMAT_S32_LE) {
821 /* 32-bit USB device */
822 num_read_buff_bytes = num_read_buff_bytes * 2;
823 }
824
825 /* Setup/Realloc the conversion buffer (if necessary). */
826 if (num_read_buff_bytes != bytes) {
827 if (num_read_buff_bytes > in->conversion_buffer_size) {
828 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
829 (and do these conversions themselves) */
830 in->conversion_buffer_size = num_read_buff_bytes;
831 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
832 }
833 read_buff = in->conversion_buffer;
834 }
835
836 if (proxy_read(&in->proxy, read_buff, num_read_buff_bytes) == 0) {
837 /*
838 * Do any conversions necessary to send the data in the format specified to/by the HAL
839 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
840 */
841 if (format != PCM_FORMAT_S16_LE) {
842 /* we need to convert */
843 if (num_device_channels != num_req_channels) {
844 out_buff = read_buff;
845 }
846
847 if (format == PCM_FORMAT_S24_3LE) {
848 num_read_buff_bytes =
849 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
850 } else if (format == PCM_FORMAT_S32_LE) {
851 num_read_buff_bytes =
852 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
853 } else {
854 goto err;
855 }
856 }
857
858 if (num_device_channels != num_req_channels) {
859 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
860
861 out_buff = buffer;
862 /* Num Channels conversion */
863 if (num_device_channels != num_req_channels) {
864 audio_format_t audio_format = in_get_format(&(in->stream.common));
865 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
866
867 num_read_buff_bytes =
868 adjust_channels(read_buff, num_device_channels,
869 out_buff, num_req_channels,
870 sample_size_in_bytes, num_read_buff_bytes);
871 }
872 }
873 }
874
875err:
876 pthread_mutex_unlock(&in->lock);
877
878 return num_read_buff_bytes;
879}
880
881static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
882{
883 return 0;
884}
885
886static int adev_open_input_stream(struct audio_hw_device *dev,
887 audio_io_handle_t handle,
888 audio_devices_t devices,
889 struct audio_config *config,
890 struct audio_stream_in **stream_in,
891 audio_input_flags_t flags __unused)
892{
893 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
894 config->sample_rate, config->channel_mask, config->format);
895
896 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
897 int ret = 0;
898
899 if (in == NULL)
900 return -ENOMEM;
901
902 /* setup function pointers */
903 in->stream.common.get_sample_rate = in_get_sample_rate;
904 in->stream.common.set_sample_rate = in_set_sample_rate;
905 in->stream.common.get_buffer_size = in_get_buffer_size;
906 in->stream.common.get_channels = in_get_channels;
907 in->stream.common.get_format = in_get_format;
908 in->stream.common.set_format = in_set_format;
909 in->stream.common.standby = in_standby;
910 in->stream.common.dump = in_dump;
911 in->stream.common.set_parameters = in_set_parameters;
912 in->stream.common.get_parameters = in_get_parameters;
913 in->stream.common.add_audio_effect = in_add_audio_effect;
914 in->stream.common.remove_audio_effect = in_remove_audio_effect;
915
916 in->stream.set_gain = in_set_gain;
917 in->stream.read = in_read;
918 in->stream.get_input_frames_lost = in_get_input_frames_lost;
919
920 in->dev = (struct audio_device *)dev;
921
922 in->profile = &in->dev->in_profile;
923
924 struct pcm_config proxy_config;
925
926 /* Rate */
927 if (config->sample_rate == 0) {
928 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
929 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
930 proxy_config.rate = config->sample_rate;
931 } else {
932 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
933 ret = -EINVAL;
934 }
935
936 /* Format */
937 /* until the framework supports format conversion, just take what it asks for
938 * i.e. AUDIO_FORMAT_PCM_16_BIT */
939 if (config->format == AUDIO_FORMAT_DEFAULT) {
940 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
941 * formats */
942 config->format = AUDIO_FORMAT_PCM_16_BIT;
943 proxy_config.format = PCM_FORMAT_S16_LE;
944 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
945 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
946 * formats */
947 proxy_config.format = PCM_FORMAT_S16_LE;
948 } else {
949 /* When the framework support other formats, validate here */
950 config->format = AUDIO_FORMAT_PCM_16_BIT;
951 proxy_config.format = PCM_FORMAT_S16_LE;
952 ret = -EINVAL;
953 }
954
955 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
956 /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input
957 * formats */
958 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
959
960 } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
961 /* allow only stereo capture for now */
962 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
963 ret = -EINVAL;
964 }
965 // proxy_config.channels = 0; /* don't change */
966 proxy_config.channels = profile_get_default_channel_count(in->profile);
967
968 proxy_prepare(&in->proxy, in->profile, &proxy_config);
969
970 in->standby = true;
971
972 in->conversion_buffer = NULL;
973 in->conversion_buffer_size = 0;
974
975 *stream_in = &in->stream;
976
977 return ret;
978}
979
980static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
981{
982 struct stream_in *in = (struct stream_in *)stream;
983
984 /* Close the pcm device */
985 in_standby(&stream->common);
986
987 free(in->conversion_buffer);
988
989 free(stream);
990}
991
992/*
993 * ADEV Functions
994 */
Simon Wilson19957a32012-04-06 16:17:12 -0700995static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
996{
997 return 0;
998}
999
Paul McLean30f41852014-04-16 15:44:20 -07001000static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001001{
1002 return strdup("");
1003}
1004
1005static int adev_init_check(const struct audio_hw_device *dev)
1006{
1007 return 0;
1008}
1009
1010static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1011{
1012 return -ENOSYS;
1013}
1014
1015static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1016{
1017 return -ENOSYS;
1018}
1019
1020static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1021{
1022 return 0;
1023}
1024
1025static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1026{
1027 return -ENOSYS;
1028}
1029
1030static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1031{
1032 return -ENOSYS;
1033}
1034
Simon Wilson19957a32012-04-06 16:17:12 -07001035static int adev_dump(const audio_hw_device_t *device, int fd)
1036{
1037 return 0;
1038}
1039
1040static int adev_close(hw_device_t *device)
1041{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001042 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001043 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001044
Simon Wilson19957a32012-04-06 16:17:12 -07001045 return 0;
1046}
1047
Paul McLean30f41852014-04-16 15:44:20 -07001048static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001049{
Simon Wilson19957a32012-04-06 16:17:12 -07001050 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1051 return -EINVAL;
1052
Paul McLeaneedc92e2013-12-19 15:46:15 -08001053 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001054 if (!adev)
1055 return -ENOMEM;
1056
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001057 profile_init(&adev->out_profile, PCM_OUT);
1058 profile_init(&adev->in_profile, PCM_IN);
1059
Simon Wilson19957a32012-04-06 16:17:12 -07001060 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001061 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001062 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001063 adev->hw_device.common.close = adev_close;
1064
Simon Wilson19957a32012-04-06 16:17:12 -07001065 adev->hw_device.init_check = adev_init_check;
1066 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1067 adev->hw_device.set_master_volume = adev_set_master_volume;
1068 adev->hw_device.set_mode = adev_set_mode;
1069 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1070 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1071 adev->hw_device.set_parameters = adev_set_parameters;
1072 adev->hw_device.get_parameters = adev_get_parameters;
1073 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1074 adev->hw_device.open_output_stream = adev_open_output_stream;
1075 adev->hw_device.close_output_stream = adev_close_output_stream;
1076 adev->hw_device.open_input_stream = adev_open_input_stream;
1077 adev->hw_device.close_input_stream = adev_close_input_stream;
1078 adev->hw_device.dump = adev_dump;
1079
1080 *device = &adev->hw_device.common;
1081
1082 return 0;
1083}
1084
1085static struct hw_module_methods_t hal_module_methods = {
1086 .open = adev_open,
1087};
1088
1089struct audio_module HAL_MODULE_INFO_SYM = {
1090 .common = {
1091 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001092 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1093 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001094 .id = AUDIO_HARDWARE_MODULE_ID,
1095 .name = "USB audio HW HAL",
1096 .author = "The Android Open Source Project",
1097 .methods = &hal_module_methods,
1098 },
1099};