blob: d9bb6114206663b1b1eb483322a0b5212c5f0ea5 [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,
Eric Laurentf5e24692014-07-27 16:14:57 -0700476 struct audio_stream_out **stream_out,
477 const char *address __unused)
Simon Wilson19957a32012-04-06 16:17:12 -0700478{
Paul McLean30f41852014-04-16 15:44:20 -0700479 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 -0800480 handle, devices, flags);
481
Simon Wilson19957a32012-04-06 16:17:12 -0700482 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800483
Simon Wilson19957a32012-04-06 16:17:12 -0700484 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700485
486 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
487 if (!out)
488 return -ENOMEM;
489
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700490 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700491 out->stream.common.get_sample_rate = out_get_sample_rate;
492 out->stream.common.set_sample_rate = out_set_sample_rate;
493 out->stream.common.get_buffer_size = out_get_buffer_size;
494 out->stream.common.get_channels = out_get_channels;
495 out->stream.common.get_format = out_get_format;
496 out->stream.common.set_format = out_set_format;
497 out->stream.common.standby = out_standby;
498 out->stream.common.dump = out_dump;
499 out->stream.common.set_parameters = out_set_parameters;
500 out->stream.common.get_parameters = out_get_parameters;
501 out->stream.common.add_audio_effect = out_add_audio_effect;
502 out->stream.common.remove_audio_effect = out_remove_audio_effect;
503 out->stream.get_latency = out_get_latency;
504 out->stream.set_volume = out_set_volume;
505 out->stream.write = out_write;
506 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700507 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700508 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
509
510 out->dev = adev;
511
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700512 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700513
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700514 // build this to hand to the alsa_device_proxy
515 struct pcm_config proxy_config;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800516
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700517 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800518
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700519 /* Rate */
520 if (config->sample_rate == 0) {
521 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
522 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
523 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800524 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700525 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
526 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800527 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800528
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700529 /* Format */
530 if (config->format == AUDIO_FORMAT_DEFAULT) {
531 proxy_config.format = profile_get_default_format(out->profile);
532 config->format = audio_format_from_pcm_format(proxy_config.format);
533 } else {
534 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
535 if (profile_is_format_valid(out->profile, fmt)) {
536 proxy_config.format = fmt;
537 } else {
538 proxy_config.format = profile_get_default_format(out->profile);
539 config->format = audio_format_from_pcm_format(proxy_config.format);
540 ret = -EINVAL;
541 }
542 }
543
544 /* Channels */
545 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
546 /* This will be needed when the framework supports non-stereo output */
547 /* config->channel_mask =
548 * audio_channel_out_mask_from_count(profile_get_default_channel_count(out->profile));
549 */
550 proxy_config.channels = profile_get_default_channel_count(out->profile);
551 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
552 } else {
553 /* This will be needed when the framework supports non-stereo output */
554 /*
555 * unsigned channel_count = audio_channel_count_from_out_mask(config->channel_mask);
556 * if (profile_is_channel_count_valid(out->profile, channel_count)) {
557 * proxy_set_channel_count(out->proxy, channel_count);
558 * } else {
559 * config->channel_mask =
560 * audio_channel_out_mask_from_count(proxy_get_channel_count(out->proxy));
561 * ret = -EINVAL;
562 * }
563 */
564 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
565 proxy_config.channels = profile_get_default_channel_count(out->profile);
566 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
567 ret = -EINVAL;
568 } else {
569 proxy_config.channels = profile_get_default_channel_count(out->profile);
570 }
571 }
572
573 proxy_prepare(&out->proxy, out->profile, &proxy_config);
574
575 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
576 ret = 0;
577
Paul McLeaneedc92e2013-12-19 15:46:15 -0800578 out->conversion_buffer = NULL;
579 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700580
581 out->standby = true;
582
583 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700584
585 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700586
587err_open:
588 free(out);
589 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800590 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700591}
592
593static void adev_close_output_stream(struct audio_hw_device *dev,
594 struct audio_stream_out *stream)
595{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800596 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700597 struct stream_out *out = (struct stream_out *)stream;
598
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700599 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700600 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800601
602 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700603
Paul McLeaneedc92e2013-12-19 15:46:15 -0800604 out->conversion_buffer = NULL;
605 out->conversion_buffer_size = 0;
606
Simon Wilson19957a32012-04-06 16:17:12 -0700607 free(stream);
608}
609
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700610static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
611 const struct audio_config *config)
612{
613 /* TODO This needs to be calculated based on format/channels/rate */
614 return 320;
615}
616
617/*
618 * IN functions
619 */
620static uint32_t in_get_sample_rate(const struct audio_stream *stream)
621{
622 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
623 ALOGV("in_get_sample_rate() = %d", rate);
624 return rate;
625}
626
627static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
628{
629 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
630 return -ENOSYS;
631}
632
633static size_t in_get_buffer_size(const struct audio_stream *stream)
634{
635 const struct stream_in * in = ((const struct stream_in*)stream);
636 size_t buffer_size =
637 proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
638 ALOGV("in_get_buffer_size() = %zd", buffer_size);
639
640 return buffer_size;
641}
642
643static uint32_t in_get_channels(const struct audio_stream *stream)
644{
645 /* TODO Here is the code we need when we support arbitrary channel counts
646 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
647 * unsigned channel_count = proxy_get_channel_count(proxy);
648 * uint32_t channel_mask = audio_channel_in_mask_from_count(channel_count);
649 * ALOGV("in_get_channels() = 0x%X count:%d", channel_mask, channel_count);
650 * return channel_mask;
651 */
652 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
653 rewrite this to return the ACTUAL channel format */
654 return AUDIO_CHANNEL_IN_STEREO;
655}
656
657static audio_format_t in_get_format(const struct audio_stream *stream)
658{
659 /* TODO Here is the code we need when we support arbitrary input formats
660 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
661 * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
662 * ALOGV("in_get_format() = %d", format);
663 * return format;
664 */
665 /* Input only supports PCM16 */
666 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
667 rewrite this to return the ACTUAL channel format (above) */
668 return AUDIO_FORMAT_PCM_16_BIT;
669}
670
671static int in_set_format(struct audio_stream *stream, audio_format_t format)
672{
673 ALOGV("in_set_format(%d) - NOPE", format);
674
675 return -ENOSYS;
676}
677
678static int in_standby(struct audio_stream *stream)
679{
680 struct stream_in *in = (struct stream_in *)stream;
681
682 pthread_mutex_lock(&in->dev->lock);
683 pthread_mutex_lock(&in->lock);
684
685 if (!in->standby) {
686 proxy_close(&in->proxy);
687 in->standby = true;
688 }
689
690 pthread_mutex_unlock(&in->lock);
691 pthread_mutex_unlock(&in->dev->lock);
692
693 return 0;
694}
695
696static int in_dump(const struct audio_stream *stream, int fd)
697{
698 return 0;
699}
700
701static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
702{
703 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
704
705 struct stream_in *in = (struct stream_in *)stream;
706
707 char value[32];
708 int param_val;
709 int routing = 0;
710 int ret_value = 0;
711
712 struct str_parms * parms = str_parms_create_str(kvpairs);
713
714 pthread_mutex_lock(&in->dev->lock);
715 pthread_mutex_lock(&in->lock);
716
717 bool recache_device_params = false;
718
719 /* Card/Device */
720 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
721 if (param_val >= 0) {
722 in->profile->card = atoi(value);
723 recache_device_params = true;
724 }
725
726 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
727 if (param_val >= 0) {
728 in->profile->device = atoi(value);
729 recache_device_params = true;
730 }
731
732 if (recache_device_params && in->profile->card >= 0 && in->profile->device >= 0) {
733 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
734 }
735
736 pthread_mutex_unlock(&in->lock);
737 pthread_mutex_unlock(&in->dev->lock);
738
739 str_parms_destroy(parms);
740
741 return ret_value;
742}
743
744static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
745{
746 struct stream_in *in = (struct stream_in *)stream;
747
748 pthread_mutex_lock(&in->dev->lock);
749 pthread_mutex_lock(&in->lock);
750
751 char * params_str = device_get_parameters(in->profile, keys);
752
753 pthread_mutex_unlock(&in->lock);
754 pthread_mutex_unlock(&in->dev->lock);
755
756 return params_str;
757}
758
759static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
760{
761 return 0;
762}
763
764static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
765{
766 return 0;
767}
768
769static int in_set_gain(struct audio_stream_in *stream, float gain)
770{
771 return 0;
772}
773
774/* must be called with hw device and output stream mutexes locked */
775static int start_input_stream(struct stream_in *in)
776{
777 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
778 in->profile->card, in->profile->device);
779
780 return proxy_open(&in->proxy);
781}
782
783/* TODO mutex stuff here (see out_write) */
784static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
785{
786 size_t num_read_buff_bytes = 0;
787 void * read_buff = buffer;
788 void * out_buff = buffer;
789
790 struct stream_in * in = (struct stream_in *)stream;
791
792 pthread_mutex_lock(&in->dev->lock);
793 pthread_mutex_lock(&in->lock);
794 pthread_mutex_unlock(&in->dev->lock);
795
796 if (in->standby) {
797 if (start_input_stream(in) != 0) {
798 goto err;
799 }
800 in->standby = false;
801 }
802
803 alsa_device_profile * profile = in->profile;
804
805 /*
806 * OK, we need to figure out how much data to read to be able to output the requested
807 * number of bytes in the HAL format (16-bit, stereo).
808 */
809 num_read_buff_bytes = bytes;
810 int num_device_channels = proxy_get_channel_count(&in->proxy);
811 int num_req_channels = 2; /* always, for now */
812
813 if (num_device_channels != num_req_channels) {
814 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
815 }
816
817 enum pcm_format format = proxy_get_format(&in->proxy);
818 if (format == PCM_FORMAT_S24_3LE) {
819 /* 24-bit USB device */
820 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
821 } else if (format == PCM_FORMAT_S32_LE) {
822 /* 32-bit USB device */
823 num_read_buff_bytes = num_read_buff_bytes * 2;
824 }
825
826 /* Setup/Realloc the conversion buffer (if necessary). */
827 if (num_read_buff_bytes != bytes) {
828 if (num_read_buff_bytes > in->conversion_buffer_size) {
829 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
830 (and do these conversions themselves) */
831 in->conversion_buffer_size = num_read_buff_bytes;
832 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
833 }
834 read_buff = in->conversion_buffer;
835 }
836
837 if (proxy_read(&in->proxy, read_buff, num_read_buff_bytes) == 0) {
838 /*
839 * Do any conversions necessary to send the data in the format specified to/by the HAL
840 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
841 */
842 if (format != PCM_FORMAT_S16_LE) {
843 /* we need to convert */
844 if (num_device_channels != num_req_channels) {
845 out_buff = read_buff;
846 }
847
848 if (format == PCM_FORMAT_S24_3LE) {
849 num_read_buff_bytes =
850 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
851 } else if (format == PCM_FORMAT_S32_LE) {
852 num_read_buff_bytes =
853 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
854 } else {
855 goto err;
856 }
857 }
858
859 if (num_device_channels != num_req_channels) {
860 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
861
862 out_buff = buffer;
863 /* Num Channels conversion */
864 if (num_device_channels != num_req_channels) {
865 audio_format_t audio_format = in_get_format(&(in->stream.common));
866 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
867
868 num_read_buff_bytes =
869 adjust_channels(read_buff, num_device_channels,
870 out_buff, num_req_channels,
871 sample_size_in_bytes, num_read_buff_bytes);
872 }
873 }
874 }
875
876err:
877 pthread_mutex_unlock(&in->lock);
878
879 return num_read_buff_bytes;
880}
881
882static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
883{
884 return 0;
885}
886
887static int adev_open_input_stream(struct audio_hw_device *dev,
888 audio_io_handle_t handle,
889 audio_devices_t devices,
890 struct audio_config *config,
891 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700892 audio_input_flags_t flags __unused,
893 const char *address __unused,
894 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700895{
896 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
897 config->sample_rate, config->channel_mask, config->format);
898
899 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
900 int ret = 0;
901
902 if (in == NULL)
903 return -ENOMEM;
904
905 /* setup function pointers */
906 in->stream.common.get_sample_rate = in_get_sample_rate;
907 in->stream.common.set_sample_rate = in_set_sample_rate;
908 in->stream.common.get_buffer_size = in_get_buffer_size;
909 in->stream.common.get_channels = in_get_channels;
910 in->stream.common.get_format = in_get_format;
911 in->stream.common.set_format = in_set_format;
912 in->stream.common.standby = in_standby;
913 in->stream.common.dump = in_dump;
914 in->stream.common.set_parameters = in_set_parameters;
915 in->stream.common.get_parameters = in_get_parameters;
916 in->stream.common.add_audio_effect = in_add_audio_effect;
917 in->stream.common.remove_audio_effect = in_remove_audio_effect;
918
919 in->stream.set_gain = in_set_gain;
920 in->stream.read = in_read;
921 in->stream.get_input_frames_lost = in_get_input_frames_lost;
922
923 in->dev = (struct audio_device *)dev;
924
925 in->profile = &in->dev->in_profile;
926
927 struct pcm_config proxy_config;
928
929 /* Rate */
930 if (config->sample_rate == 0) {
931 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
932 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
933 proxy_config.rate = config->sample_rate;
934 } else {
935 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
936 ret = -EINVAL;
937 }
938
939 /* Format */
940 /* until the framework supports format conversion, just take what it asks for
941 * i.e. AUDIO_FORMAT_PCM_16_BIT */
942 if (config->format == AUDIO_FORMAT_DEFAULT) {
943 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
944 * formats */
945 config->format = AUDIO_FORMAT_PCM_16_BIT;
946 proxy_config.format = PCM_FORMAT_S16_LE;
947 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
948 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
949 * formats */
950 proxy_config.format = PCM_FORMAT_S16_LE;
951 } else {
952 /* When the framework support other formats, validate here */
953 config->format = AUDIO_FORMAT_PCM_16_BIT;
954 proxy_config.format = PCM_FORMAT_S16_LE;
955 ret = -EINVAL;
956 }
957
958 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
959 /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input
960 * formats */
961 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
962
963 } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
964 /* allow only stereo capture for now */
965 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
966 ret = -EINVAL;
967 }
968 // proxy_config.channels = 0; /* don't change */
969 proxy_config.channels = profile_get_default_channel_count(in->profile);
970
971 proxy_prepare(&in->proxy, in->profile, &proxy_config);
972
973 in->standby = true;
974
975 in->conversion_buffer = NULL;
976 in->conversion_buffer_size = 0;
977
978 *stream_in = &in->stream;
979
980 return ret;
981}
982
983static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
984{
985 struct stream_in *in = (struct stream_in *)stream;
986
987 /* Close the pcm device */
988 in_standby(&stream->common);
989
990 free(in->conversion_buffer);
991
992 free(stream);
993}
994
995/*
996 * ADEV Functions
997 */
Simon Wilson19957a32012-04-06 16:17:12 -0700998static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
999{
1000 return 0;
1001}
1002
Paul McLean30f41852014-04-16 15:44:20 -07001003static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001004{
1005 return strdup("");
1006}
1007
1008static int adev_init_check(const struct audio_hw_device *dev)
1009{
1010 return 0;
1011}
1012
1013static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1014{
1015 return -ENOSYS;
1016}
1017
1018static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1019{
1020 return -ENOSYS;
1021}
1022
1023static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1024{
1025 return 0;
1026}
1027
1028static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1029{
1030 return -ENOSYS;
1031}
1032
1033static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1034{
1035 return -ENOSYS;
1036}
1037
Simon Wilson19957a32012-04-06 16:17:12 -07001038static int adev_dump(const audio_hw_device_t *device, int fd)
1039{
1040 return 0;
1041}
1042
1043static int adev_close(hw_device_t *device)
1044{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001045 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001046 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001047
Simon Wilson19957a32012-04-06 16:17:12 -07001048 return 0;
1049}
1050
Paul McLean30f41852014-04-16 15:44:20 -07001051static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001052{
Simon Wilson19957a32012-04-06 16:17:12 -07001053 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1054 return -EINVAL;
1055
Paul McLeaneedc92e2013-12-19 15:46:15 -08001056 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001057 if (!adev)
1058 return -ENOMEM;
1059
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001060 profile_init(&adev->out_profile, PCM_OUT);
1061 profile_init(&adev->in_profile, PCM_IN);
1062
Simon Wilson19957a32012-04-06 16:17:12 -07001063 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001064 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001065 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001066 adev->hw_device.common.close = adev_close;
1067
Simon Wilson19957a32012-04-06 16:17:12 -07001068 adev->hw_device.init_check = adev_init_check;
1069 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1070 adev->hw_device.set_master_volume = adev_set_master_volume;
1071 adev->hw_device.set_mode = adev_set_mode;
1072 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1073 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1074 adev->hw_device.set_parameters = adev_set_parameters;
1075 adev->hw_device.get_parameters = adev_get_parameters;
1076 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1077 adev->hw_device.open_output_stream = adev_open_output_stream;
1078 adev->hw_device.close_output_stream = adev_close_output_stream;
1079 adev->hw_device.open_input_stream = adev_open_input_stream;
1080 adev->hw_device.close_input_stream = adev_close_input_stream;
1081 adev->hw_device.dump = adev_dump;
1082
1083 *device = &adev->hw_device.common;
1084
1085 return 0;
1086}
1087
1088static struct hw_module_methods_t hal_module_methods = {
1089 .open = adev_open,
1090};
1091
1092struct audio_module HAL_MODULE_INFO_SYM = {
1093 .common = {
1094 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001095 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1096 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001097 .id = AUDIO_HARDWARE_MODULE_ID,
1098 .name = "USB audio HW HAL",
1099 .author = "The Android Open Source Project",
1100 .methods = &hal_module_methods,
1101 },
1102};