1 /*
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 "modules.usbaudio.audio_hal"
18 /* #define LOG_NDEBUG 0 */
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <pthread.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 #include <unistd.h>
27
28 #include <log/log.h>
29 #include <cutils/list.h>
30 #include <cutils/str_parms.h>
31 #include <cutils/properties.h>
32
33 #include <hardware/audio.h>
34 #include <hardware/audio_alsaops.h>
35 #include <hardware/hardware.h>
36
37 #include <system/audio.h>
38
39 #include <tinyalsa/asoundlib.h>
40
41 #include <audio_utils/channels.h>
42
43 #include "alsa_device_profile.h"
44 #include "alsa_device_proxy.h"
45 #include "alsa_logging.h"
46
47 /* Lock play & record samples rates at or above this threshold */
48 #define RATELOCK_THRESHOLD 96000
49
50 #define max(a, b) ((a) > (b) ? (a) : (b))
51 #define min(a, b) ((a) < (b) ? (a) : (b))
52
53 struct audio_device {
54 struct audio_hw_device hw_device;
55
56 pthread_mutex_t lock; /* see note below on mutex acquisition order */
57
58 /* output */
59 struct listnode output_stream_list;
60
61 /* input */
62 struct listnode input_stream_list;
63
64 /* lock input & output sample rates */
65 /*FIXME - How do we address multiple output streams? */
66 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
67
68 bool mic_muted;
69
70 int32_t inputs_open; /* number of input streams currently open. */
71 };
72
73 struct stream_lock {
74 pthread_mutex_t lock; /* see note below on mutex acquisition order */
75 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
76 };
77
78 struct stream_out {
79 struct audio_stream_out stream;
80
81 struct stream_lock lock;
82
83 bool standby;
84
85 struct audio_device *adev; /* hardware information - only using this for the lock */
86
87 alsa_device_profile profile; /* The profile of the ALSA device connected to the stream.
88 */
89
90 alsa_device_proxy proxy; /* state of the stream */
91
92 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
93 * This may differ from the device channel count when
94 * the device is not compatible with AudioFlinger
95 * capabilities, e.g. exposes too many channels or
96 * too few channels. */
97 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
98 * so the proxy doesn't have a channel_mask, but
99 * audio HALs need to talk about channel masks
100 * so expose the one calculated by
101 * adev_open_output_stream */
102
103 struct listnode list_node;
104
105 void * conversion_buffer; /* any conversions are put into here
106 * they could come from here too if
107 * there was a previous conversion */
108 size_t conversion_buffer_size; /* in bytes */
109 };
110
111 struct stream_in {
112 struct audio_stream_in stream;
113
114 struct stream_lock lock;
115
116 bool standby;
117
118 struct audio_device *adev; /* hardware information - only using this for the lock */
119
120 alsa_device_profile profile; /* The profile of the ALSA device connected to the stream.
121 */
122
123 alsa_device_proxy proxy; /* state of the stream */
124
125 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
126 * This may differ from the device channel count when
127 * the device is not compatible with AudioFlinger
128 * capabilities, e.g. exposes too many channels or
129 * too few channels. */
130 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
131 * so the proxy doesn't have a channel_mask, but
132 * audio HALs need to talk about channel masks
133 * so expose the one calculated by
134 * adev_open_input_stream */
135
136 struct listnode list_node;
137
138 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
139 void * conversion_buffer; /* any conversions are put into here
140 * they could come from here too if
141 * there was a previous conversion */
142 size_t conversion_buffer_size; /* in bytes */
143 };
144
145 /*
146 * Locking Helpers
147 */
148 /*
149 * NOTE: when multiple mutexes have to be acquired, always take the
150 * stream_in or stream_out mutex first, followed by the audio_device mutex.
151 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
152 * higher priority playback or capture thread.
153 */
154
stream_lock_init(struct stream_lock * lock)155 static void stream_lock_init(struct stream_lock *lock) {
156 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
157 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
158 }
159
stream_lock(struct stream_lock * lock)160 static void stream_lock(struct stream_lock *lock) {
161 pthread_mutex_lock(&lock->pre_lock);
162 pthread_mutex_lock(&lock->lock);
163 pthread_mutex_unlock(&lock->pre_lock);
164 }
165
stream_unlock(struct stream_lock * lock)166 static void stream_unlock(struct stream_lock *lock) {
167 pthread_mutex_unlock(&lock->lock);
168 }
169
device_lock(struct audio_device * adev)170 static void device_lock(struct audio_device *adev) {
171 pthread_mutex_lock(&adev->lock);
172 }
173
device_try_lock(struct audio_device * adev)174 static int device_try_lock(struct audio_device *adev) {
175 return pthread_mutex_trylock(&adev->lock);
176 }
177
device_unlock(struct audio_device * adev)178 static void device_unlock(struct audio_device *adev) {
179 pthread_mutex_unlock(&adev->lock);
180 }
181
182 /*
183 * streams list management
184 */
adev_add_stream_to_list(struct audio_device * adev,struct listnode * list,struct listnode * stream_node)185 static void adev_add_stream_to_list(
186 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
187 device_lock(adev);
188
189 list_add_tail(list, stream_node);
190
191 device_unlock(adev);
192 }
193
adev_remove_stream_from_list(struct audio_device * adev,struct listnode * stream_node)194 static void adev_remove_stream_from_list(
195 struct audio_device* adev, struct listnode* stream_node) {
196 device_lock(adev);
197
198 list_remove(stream_node);
199
200 device_unlock(adev);
201 }
202
203 /*
204 * Extract the card and device numbers from the supplied key/value pairs.
205 * kvpairs A null-terminated string containing the key/value pairs or card and device.
206 * i.e. "card=1;device=42"
207 * card A pointer to a variable to receive the parsed-out card number.
208 * device A pointer to a variable to receive the parsed-out device number.
209 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
210 * associated key/value pair is not found in the provided string.
211 * Return true if the kvpairs string contain a card/device spec, false otherwise.
212 */
parse_card_device_params(const char * kvpairs,int * card,int * device)213 static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
214 {
215 struct str_parms * parms = str_parms_create_str(kvpairs);
216 char value[32];
217 int param_val;
218
219 // initialize to "undefined" state.
220 *card = -1;
221 *device = -1;
222
223 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
224 if (param_val >= 0) {
225 *card = atoi(value);
226 }
227
228 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
229 if (param_val >= 0) {
230 *device = atoi(value);
231 }
232
233 str_parms_destroy(parms);
234
235 return *card >= 0 && *device >= 0;
236 }
237
device_get_parameters(const alsa_device_profile * profile,const char * keys)238 static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
239 {
240 if (profile->card < 0 || profile->device < 0) {
241 return strdup("");
242 }
243
244 struct str_parms *query = str_parms_create_str(keys);
245 struct str_parms *result = str_parms_create();
246
247 /* These keys are from hardware/libhardware/include/audio.h */
248 /* supported sample rates */
249 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
250 char* rates_list = profile_get_sample_rate_strs(profile);
251 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
252 rates_list);
253 free(rates_list);
254 }
255
256 /* supported channel counts */
257 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
258 char* channels_list = profile_get_channel_count_strs(profile);
259 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
260 channels_list);
261 free(channels_list);
262 }
263
264 /* supported sample formats */
265 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
266 char * format_params = profile_get_format_strs(profile);
267 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
268 format_params);
269 free(format_params);
270 }
271 str_parms_destroy(query);
272
273 char* result_str = str_parms_to_str(result);
274 str_parms_destroy(result);
275
276 ALOGV("device_get_parameters = %s", result_str);
277
278 return result_str;
279 }
280
281 /*
282 * HAl Functions
283 */
284 /**
285 * NOTE: when multiple mutexes have to be acquired, always respect the
286 * following order: hw device > out stream
287 */
288
289 /*
290 * OUT functions
291 */
out_get_sample_rate(const struct audio_stream * stream)292 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
293 {
294 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
295 ALOGV("out_get_sample_rate() = %d", rate);
296 return rate;
297 }
298
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)299 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
300 {
301 return 0;
302 }
303
out_get_buffer_size(const struct audio_stream * stream)304 static size_t out_get_buffer_size(const struct audio_stream *stream)
305 {
306 const struct stream_out* out = (const struct stream_out*)stream;
307 size_t buffer_size =
308 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
309 return buffer_size;
310 }
311
out_get_channels(const struct audio_stream * stream)312 static uint32_t out_get_channels(const struct audio_stream *stream)
313 {
314 const struct stream_out *out = (const struct stream_out*)stream;
315 return out->hal_channel_mask;
316 }
317
out_get_format(const struct audio_stream * stream)318 static audio_format_t out_get_format(const struct audio_stream *stream)
319 {
320 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
321 * Relies on the framework to provide data in the specified format.
322 * This could change in the future.
323 */
324 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
325 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
326 return format;
327 }
328
out_set_format(struct audio_stream * stream,audio_format_t format)329 static int out_set_format(struct audio_stream *stream, audio_format_t format)
330 {
331 return 0;
332 }
333
out_standby(struct audio_stream * stream)334 static int out_standby(struct audio_stream *stream)
335 {
336 struct stream_out *out = (struct stream_out *)stream;
337
338 stream_lock(&out->lock);
339 if (!out->standby) {
340 proxy_close(&out->proxy);
341 out->standby = true;
342 }
343 stream_unlock(&out->lock);
344 return 0;
345 }
346
out_dump(const struct audio_stream * stream,int fd)347 static int out_dump(const struct audio_stream *stream, int fd) {
348 const struct stream_out* out_stream = (const struct stream_out*) stream;
349
350 if (out_stream != NULL) {
351 dprintf(fd, "Output Profile:\n");
352 profile_dump(&out_stream->profile, fd);
353
354 dprintf(fd, "Output Proxy:\n");
355 proxy_dump(&out_stream->proxy, fd);
356 }
357
358 return 0;
359 }
360
out_set_parameters(struct audio_stream * stream,const char * kvpairs)361 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
362 {
363 ALOGV("out_set_parameters() keys:%s", kvpairs);
364
365 struct stream_out *out = (struct stream_out *)stream;
366
367 int ret_value = 0;
368 int card = -1;
369 int device = -1;
370
371 if (!parse_card_device_params(kvpairs, &card, &device)) {
372 // nothing to do
373 return ret_value;
374 }
375
376 stream_lock(&out->lock);
377 if (!profile_is_cached_for(&out->profile, card, device)) {
378 /* cannot read pcm device info if playback is active */
379 if (!out->standby)
380 ret_value = -ENOSYS;
381 else {
382 int saved_card = out->profile.card;
383 int saved_device = out->profile.device;
384 out->profile.card = card;
385 out->profile.device = device;
386 ret_value = profile_read_device_info(&out->profile) ? 0 : -EINVAL;
387 if (ret_value != 0) {
388 out->profile.card = saved_card;
389 out->profile.device = saved_device;
390 }
391 }
392 }
393
394 stream_unlock(&out->lock);
395
396 return ret_value;
397 }
398
out_get_parameters(const struct audio_stream * stream,const char * keys)399 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
400 {
401 struct stream_out *out = (struct stream_out *)stream;
402 stream_lock(&out->lock);
403 char * params_str = device_get_parameters(&out->profile, keys);
404 stream_unlock(&out->lock);
405 return params_str;
406 }
407
out_get_latency(const struct audio_stream_out * stream)408 static uint32_t out_get_latency(const struct audio_stream_out *stream)
409 {
410 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
411 return proxy_get_latency(proxy);
412 }
413
out_set_volume(struct audio_stream_out * stream,float left,float right)414 static int out_set_volume(struct audio_stream_out *stream, float left, float right)
415 {
416 return -ENOSYS;
417 }
418
419 /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct stream_out * out)420 static int start_output_stream(struct stream_out *out)
421 {
422 ALOGV("start_output_stream(card:%d device:%d)", out->profile.card, out->profile.device);
423
424 return proxy_open(&out->proxy);
425 }
426
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)427 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
428 {
429 int ret;
430 struct stream_out *out = (struct stream_out *)stream;
431
432 stream_lock(&out->lock);
433 if (out->standby) {
434 ret = start_output_stream(out);
435 if (ret != 0) {
436 goto err;
437 }
438 out->standby = false;
439 }
440
441 alsa_device_proxy* proxy = &out->proxy;
442 const void * write_buff = buffer;
443 int num_write_buff_bytes = bytes;
444 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
445 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
446 if (num_device_channels != num_req_channels) {
447 /* allocate buffer */
448 const size_t required_conversion_buffer_size =
449 bytes * num_device_channels / num_req_channels;
450 if (required_conversion_buffer_size > out->conversion_buffer_size) {
451 out->conversion_buffer_size = required_conversion_buffer_size;
452 out->conversion_buffer = realloc(out->conversion_buffer,
453 out->conversion_buffer_size);
454 }
455 /* convert data */
456 const audio_format_t audio_format = out_get_format(&(out->stream.common));
457 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
458 num_write_buff_bytes =
459 adjust_channels(write_buff, num_req_channels,
460 out->conversion_buffer, num_device_channels,
461 sample_size_in_bytes, num_write_buff_bytes);
462 write_buff = out->conversion_buffer;
463 }
464
465 if (write_buff != NULL && num_write_buff_bytes != 0) {
466 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
467 }
468
469 stream_unlock(&out->lock);
470
471 return bytes;
472
473 err:
474 stream_unlock(&out->lock);
475 if (ret != 0) {
476 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
477 out_get_sample_rate(&stream->common));
478 }
479
480 return bytes;
481 }
482
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)483 static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
484 {
485 return -EINVAL;
486 }
487
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)488 static int out_get_presentation_position(const struct audio_stream_out *stream,
489 uint64_t *frames, struct timespec *timestamp)
490 {
491 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
492 stream_lock(&out->lock);
493
494 const alsa_device_proxy *proxy = &out->proxy;
495 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
496
497 stream_unlock(&out->lock);
498 return ret;
499 }
500
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)501 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
502 {
503 return 0;
504 }
505
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)506 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
507 {
508 return 0;
509 }
510
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)511 static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
512 {
513 return -EINVAL;
514 }
515
adev_open_output_stream(struct audio_hw_device * hw_dev,audio_io_handle_t handle,audio_devices_t devicesSpec __unused,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address)516 static int adev_open_output_stream(struct audio_hw_device *hw_dev,
517 audio_io_handle_t handle,
518 audio_devices_t devicesSpec __unused,
519 audio_output_flags_t flags,
520 struct audio_config *config,
521 struct audio_stream_out **stream_out,
522 const char *address /*__unused*/)
523 {
524 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
525 handle, devicesSpec, flags, address);
526
527 struct stream_out *out;
528
529 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
530 if (out == NULL) {
531 return -ENOMEM;
532 }
533
534 /* setup function pointers */
535 out->stream.common.get_sample_rate = out_get_sample_rate;
536 out->stream.common.set_sample_rate = out_set_sample_rate;
537 out->stream.common.get_buffer_size = out_get_buffer_size;
538 out->stream.common.get_channels = out_get_channels;
539 out->stream.common.get_format = out_get_format;
540 out->stream.common.set_format = out_set_format;
541 out->stream.common.standby = out_standby;
542 out->stream.common.dump = out_dump;
543 out->stream.common.set_parameters = out_set_parameters;
544 out->stream.common.get_parameters = out_get_parameters;
545 out->stream.common.add_audio_effect = out_add_audio_effect;
546 out->stream.common.remove_audio_effect = out_remove_audio_effect;
547 out->stream.get_latency = out_get_latency;
548 out->stream.set_volume = out_set_volume;
549 out->stream.write = out_write;
550 out->stream.get_render_position = out_get_render_position;
551 out->stream.get_presentation_position = out_get_presentation_position;
552 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
553
554 stream_lock_init(&out->lock);
555
556 out->adev = (struct audio_device *)hw_dev;
557
558 profile_init(&out->profile, PCM_OUT);
559
560 // build this to hand to the alsa_device_proxy
561 struct pcm_config proxy_config;
562 memset(&proxy_config, 0, sizeof(proxy_config));
563
564 /* Pull out the card/device pair */
565 parse_card_device_params(address, &out->profile.card, &out->profile.device);
566
567 profile_read_device_info(&out->profile);
568
569 int ret = 0;
570
571 /* Rate */
572 if (config->sample_rate == 0) {
573 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&out->profile);
574 } else if (profile_is_sample_rate_valid(&out->profile, config->sample_rate)) {
575 proxy_config.rate = config->sample_rate;
576 } else {
577 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&out->profile);
578 ret = -EINVAL;
579 }
580
581 /* TODO: This is a problem if the input does not support this rate */
582 device_lock(out->adev);
583 out->adev->device_sample_rate = config->sample_rate;
584 device_unlock(out->adev);
585
586 /* Format */
587 if (config->format == AUDIO_FORMAT_DEFAULT) {
588 proxy_config.format = profile_get_default_format(&out->profile);
589 config->format = audio_format_from_pcm_format(proxy_config.format);
590 } else {
591 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
592 if (profile_is_format_valid(&out->profile, fmt)) {
593 proxy_config.format = fmt;
594 } else {
595 proxy_config.format = profile_get_default_format(&out->profile);
596 config->format = audio_format_from_pcm_format(proxy_config.format);
597 ret = -EINVAL;
598 }
599 }
600
601 /* Channels */
602 bool calc_mask = false;
603 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
604 /* query case */
605 out->hal_channel_count = profile_get_default_channel_count(&out->profile);
606 calc_mask = true;
607 } else {
608 /* explicit case */
609 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
610 }
611
612 /* The Framework is currently limited to no more than this number of channels */
613 if (out->hal_channel_count > FCC_8) {
614 out->hal_channel_count = FCC_8;
615 calc_mask = true;
616 }
617
618 if (calc_mask) {
619 /* need to calculate the mask from channel count either because this is the query case
620 * or the specified mask isn't valid for this device, or is more then the FW can handle */
621 config->channel_mask = out->hal_channel_count <= FCC_2
622 /* position mask for mono and stereo*/
623 ? audio_channel_out_mask_from_count(out->hal_channel_count)
624 /* otherwise indexed */
625 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
626 }
627
628 out->hal_channel_mask = config->channel_mask;
629
630 // Validate the "logical" channel count against support in the "actual" profile.
631 // if they differ, choose the "actual" number of channels *closest* to the "logical".
632 // and store THAT in proxy_config.channels
633 proxy_config.channels =
634 profile_get_closest_channel_count(&out->profile, out->hal_channel_count);
635 proxy_prepare(&out->proxy, &out->profile, &proxy_config);
636
637 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
638 * So clear any errors that may have occurred above.
639 */
640 ret = 0;
641
642 out->conversion_buffer = NULL;
643 out->conversion_buffer_size = 0;
644
645 out->standby = true;
646
647 /* Save the stream for adev_dump() */
648 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
649
650 *stream_out = &out->stream;
651
652 return ret;
653 }
654
adev_close_output_stream(struct audio_hw_device * hw_dev,struct audio_stream_out * stream)655 static void adev_close_output_stream(struct audio_hw_device *hw_dev,
656 struct audio_stream_out *stream)
657 {
658 struct stream_out *out = (struct stream_out *)stream;
659 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile.card, out->profile.device);
660
661 /* Close the pcm device */
662 out_standby(&stream->common);
663
664 free(out->conversion_buffer);
665
666 out->conversion_buffer = NULL;
667 out->conversion_buffer_size = 0;
668
669 adev_remove_stream_from_list(out->adev, &out->list_node);
670
671 device_lock(out->adev);
672 out->adev->device_sample_rate = 0;
673 device_unlock(out->adev);
674
675 free(stream);
676 }
677
adev_get_input_buffer_size(const struct audio_hw_device * hw_dev,const struct audio_config * config)678 static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
679 const struct audio_config *config)
680 {
681 /* TODO This needs to be calculated based on format/channels/rate */
682 return 320;
683 }
684
685 /*
686 * IN functions
687 */
in_get_sample_rate(const struct audio_stream * stream)688 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
689 {
690 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
691 ALOGV("in_get_sample_rate() = %d", rate);
692 return rate;
693 }
694
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)695 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
696 {
697 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
698 return -ENOSYS;
699 }
700
in_get_buffer_size(const struct audio_stream * stream)701 static size_t in_get_buffer_size(const struct audio_stream *stream)
702 {
703 const struct stream_in * in = ((const struct stream_in*)stream);
704 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
705 }
706
in_get_channels(const struct audio_stream * stream)707 static uint32_t in_get_channels(const struct audio_stream *stream)
708 {
709 const struct stream_in *in = (const struct stream_in*)stream;
710 return in->hal_channel_mask;
711 }
712
in_get_format(const struct audio_stream * stream)713 static audio_format_t in_get_format(const struct audio_stream *stream)
714 {
715 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
716 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
717 return format;
718 }
719
in_set_format(struct audio_stream * stream,audio_format_t format)720 static int in_set_format(struct audio_stream *stream, audio_format_t format)
721 {
722 ALOGV("in_set_format(%d) - NOPE", format);
723
724 return -ENOSYS;
725 }
726
in_standby(struct audio_stream * stream)727 static int in_standby(struct audio_stream *stream)
728 {
729 struct stream_in *in = (struct stream_in *)stream;
730
731 stream_lock(&in->lock);
732 if (!in->standby) {
733 proxy_close(&in->proxy);
734 in->standby = true;
735 }
736 stream_unlock(&in->lock);
737
738 return 0;
739 }
740
in_dump(const struct audio_stream * stream,int fd)741 static int in_dump(const struct audio_stream *stream, int fd)
742 {
743 const struct stream_in* in_stream = (const struct stream_in*)stream;
744 if (in_stream != NULL) {
745 dprintf(fd, "Input Profile:\n");
746 profile_dump(&in_stream->profile, fd);
747
748 dprintf(fd, "Input Proxy:\n");
749 proxy_dump(&in_stream->proxy, fd);
750 }
751
752 return 0;
753 }
754
in_set_parameters(struct audio_stream * stream,const char * kvpairs)755 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
756 {
757 ALOGV("in_set_parameters() keys:%s", kvpairs);
758
759 struct stream_in *in = (struct stream_in *)stream;
760
761 int ret_value = 0;
762 int card = -1;
763 int device = -1;
764
765 if (!parse_card_device_params(kvpairs, &card, &device)) {
766 // nothing to do
767 return ret_value;
768 }
769
770 stream_lock(&in->lock);
771 device_lock(in->adev);
772
773 if (card >= 0 && device >= 0 && !profile_is_cached_for(&in->profile, card, device)) {
774 /* cannot read pcm device info if capture is active, or more than one open stream */
775 if (!in->standby || in->adev->inputs_open > 1)
776 ret_value = -ENOSYS;
777 else {
778 int saved_card = in->profile.card;
779 int saved_device = in->profile.device;
780 in->profile.card = card;
781 in->profile.device = device;
782 ret_value = profile_read_device_info(&in->profile) ? 0 : -EINVAL;
783 if (ret_value != 0) {
784 ALOGE("Can't read device profile. card:%d, device:%d", card, device);
785 in->profile.card = saved_card;
786 in->profile.device = saved_device;
787 }
788 }
789 }
790
791 device_unlock(in->adev);
792 stream_unlock(&in->lock);
793
794 return ret_value;
795 }
796
in_get_parameters(const struct audio_stream * stream,const char * keys)797 static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
798 {
799 struct stream_in *in = (struct stream_in *)stream;
800
801 stream_lock(&in->lock);
802 char * params_str = device_get_parameters(&in->profile, keys);
803 stream_unlock(&in->lock);
804
805 return params_str;
806 }
807
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)808 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
809 {
810 return 0;
811 }
812
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)813 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
814 {
815 return 0;
816 }
817
in_set_gain(struct audio_stream_in * stream,float gain)818 static int in_set_gain(struct audio_stream_in *stream, float gain)
819 {
820 return 0;
821 }
822
823 /* must be called with hw device and output stream mutexes locked */
start_input_stream(struct stream_in * in)824 static int start_input_stream(struct stream_in *in)
825 {
826 ALOGV("start_input_stream(card:%d device:%d)", in->profile.card, in->profile.device);
827
828 return proxy_open(&in->proxy);
829 }
830
831 /* TODO mutex stuff here (see out_write) */
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)832 static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
833 {
834 size_t num_read_buff_bytes = 0;
835 void * read_buff = buffer;
836 void * out_buff = buffer;
837 int ret = 0;
838
839 struct stream_in * in = (struct stream_in *)stream;
840
841 stream_lock(&in->lock);
842 if (in->standby) {
843 ret = start_input_stream(in);
844 if (ret != 0) {
845 goto err;
846 }
847 in->standby = false;
848 }
849
850 /*
851 * OK, we need to figure out how much data to read to be able to output the requested
852 * number of bytes in the HAL format (16-bit, stereo).
853 */
854 num_read_buff_bytes = bytes;
855 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
856 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
857
858 if (num_device_channels != num_req_channels) {
859 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
860 }
861
862 /* Setup/Realloc the conversion buffer (if necessary). */
863 if (num_read_buff_bytes != bytes) {
864 if (num_read_buff_bytes > in->conversion_buffer_size) {
865 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
866 (and do these conversions themselves) */
867 in->conversion_buffer_size = num_read_buff_bytes;
868 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
869 }
870 read_buff = in->conversion_buffer;
871 }
872
873 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
874 if (ret == 0) {
875 if (num_device_channels != num_req_channels) {
876 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
877
878 out_buff = buffer;
879 /* Num Channels conversion */
880 if (num_device_channels != num_req_channels) {
881 audio_format_t audio_format = in_get_format(&(in->stream.common));
882 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
883
884 num_read_buff_bytes =
885 adjust_channels(read_buff, num_device_channels,
886 out_buff, num_req_channels,
887 sample_size_in_bytes, num_read_buff_bytes);
888 }
889 }
890
891 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
892 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
893 memset(buffer, 0, num_read_buff_bytes);
894 } else {
895 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
896 }
897
898 err:
899 stream_unlock(&in->lock);
900 return num_read_buff_bytes;
901 }
902
in_get_input_frames_lost(struct audio_stream_in * stream)903 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
904 {
905 return 0;
906 }
907
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)908 static int in_get_capture_position(const struct audio_stream_in *stream,
909 int64_t *frames, int64_t *time)
910 {
911 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
912 stream_lock(&in->lock);
913
914 const alsa_device_proxy *proxy = &in->proxy;
915 const int ret = proxy_get_capture_position(proxy, frames, time);
916
917 stream_unlock(&in->lock);
918 return ret;
919 }
920
in_get_active_microphones(const struct audio_stream_in * stream,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)921 static int in_get_active_microphones(const struct audio_stream_in *stream,
922 struct audio_microphone_characteristic_t *mic_array,
923 size_t *mic_count) {
924 (void)stream;
925 (void)mic_array;
926 (void)mic_count;
927
928 return -ENOSYS;
929 }
930
in_set_microphone_direction(const struct audio_stream_in * stream,audio_microphone_direction_t dir)931 static int in_set_microphone_direction(const struct audio_stream_in *stream,
932 audio_microphone_direction_t dir) {
933 (void)stream;
934 (void)dir;
935 ALOGV("---- in_set_microphone_direction()");
936 return -ENOSYS;
937 }
938
in_set_microphone_field_dimension(const struct audio_stream_in * stream,float zoom)939 static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
940 (void)zoom;
941 ALOGV("---- in_set_microphone_field_dimension()");
942 return -ENOSYS;
943 }
944
adev_open_input_stream(struct audio_hw_device * hw_dev,audio_io_handle_t handle,audio_devices_t devicesSpec __unused,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address,audio_source_t source __unused)945 static int adev_open_input_stream(struct audio_hw_device *hw_dev,
946 audio_io_handle_t handle,
947 audio_devices_t devicesSpec __unused,
948 struct audio_config *config,
949 struct audio_stream_in **stream_in,
950 audio_input_flags_t flags __unused,
951 const char *address,
952 audio_source_t source __unused)
953 {
954 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
955 config->sample_rate, config->channel_mask, config->format);
956
957 /* 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 }
964
965 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
966 if (in == NULL) {
967 *stream_in = NULL;
968 return -ENOMEM;
969 }
970
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;
988 in->stream.get_capture_position = in_get_capture_position;
989
990 in->stream.get_active_microphones = in_get_active_microphones;
991 in->stream.set_microphone_direction = in_set_microphone_direction;
992 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
993
994 stream_lock_init(&in->lock);
995
996 in->adev = (struct audio_device *)hw_dev;
997
998 profile_init(&in->profile, PCM_IN);
999
1000 struct pcm_config proxy_config;
1001 memset(&proxy_config, 0, sizeof(proxy_config));
1002
1003 int ret = 0;
1004 device_lock(in->adev);
1005 int num_open_inputs = in->adev->inputs_open;
1006 device_unlock(in->adev);
1007
1008 /* Check if an input stream is already open */
1009 if (num_open_inputs > 0) {
1010 if (!profile_is_cached_for(&in->profile, card, device)) {
1011 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1012 __func__, card, device);
1013 ret = -EINVAL;
1014 }
1015 } else {
1016 /* Read input profile only if necessary */
1017 in->profile.card = card;
1018 in->profile.device = device;
1019 if (!profile_read_device_info(&in->profile)) {
1020 ALOGW("%s fail - cannot read profile", __func__);
1021 ret = -EINVAL;
1022 }
1023 }
1024 if (ret != 0) {
1025 free(in);
1026 *stream_in = NULL;
1027 return ret;
1028 }
1029
1030 /* Rate */
1031 int request_config_rate = config->sample_rate;
1032 if (config->sample_rate == 0) {
1033 config->sample_rate = profile_get_default_sample_rate(&in->profile);
1034 }
1035
1036 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
1037 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
1038 if (config->sample_rate != in->adev->device_sample_rate) {
1039 unsigned highest_rate = profile_get_highest_sample_rate(&in->profile);
1040 if (highest_rate == 0) {
1041 ret = -EINVAL; /* error with device */
1042 } else {
1043 proxy_config.rate = config->sample_rate =
1044 min(highest_rate, in->adev->device_sample_rate);
1045 if (request_config_rate != 0 && proxy_config.rate != config->sample_rate) {
1046 /* Changing the requested rate */
1047 ret = -EINVAL;
1048 } else {
1049 /* Everything AOK! */
1050 ret = 0;
1051 }
1052 }
1053 }
1054 } else if (profile_is_sample_rate_valid(&in->profile, config->sample_rate)) {
1055 proxy_config.rate = config->sample_rate;
1056 } else {
1057 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&in->profile);
1058 ret = -EINVAL;
1059 }
1060
1061 /* Format */
1062 if (config->format == AUDIO_FORMAT_DEFAULT) {
1063 proxy_config.format = profile_get_default_format(&in->profile);
1064 config->format = audio_format_from_pcm_format(proxy_config.format);
1065 } else {
1066 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1067 if (profile_is_format_valid(&in->profile, fmt)) {
1068 proxy_config.format = fmt;
1069 } else {
1070 proxy_config.format = profile_get_default_format(&in->profile);
1071 config->format = audio_format_from_pcm_format(proxy_config.format);
1072 ret = -EINVAL;
1073 }
1074 }
1075
1076 /* Channels */
1077 bool calc_mask = false;
1078 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1079 /* query case */
1080 in->hal_channel_count = profile_get_default_channel_count(&in->profile);
1081 calc_mask = true;
1082 } else {
1083 /* explicit case */
1084 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1085 }
1086
1087 /* The Framework is currently limited to no more than this number of channels */
1088 if (in->hal_channel_count > FCC_8) {
1089 in->hal_channel_count = FCC_8;
1090 calc_mask = true;
1091 }
1092
1093 if (calc_mask) {
1094 /* need to calculate the mask from channel count either because this is the query case
1095 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1096 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1097 /* position mask for mono & stereo */
1098 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1099 /* otherwise indexed */
1100 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
1101
1102 // if we change the mask...
1103 if (in->hal_channel_mask != config->channel_mask &&
1104 config->channel_mask != AUDIO_CHANNEL_NONE) {
1105 config->channel_mask = in->hal_channel_mask;
1106 ret = -EINVAL;
1107 }
1108 } else {
1109 in->hal_channel_mask = config->channel_mask;
1110 }
1111
1112 if (ret == 0) {
1113 // Validate the "logical" channel count against support in the "actual" profile.
1114 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1115 // and store THAT in proxy_config.channels
1116 proxy_config.channels =
1117 profile_get_closest_channel_count(&in->profile, in->hal_channel_count);
1118 ret = proxy_prepare(&in->proxy, &in->profile, &proxy_config);
1119 if (ret == 0) {
1120 in->standby = true;
1121
1122 in->conversion_buffer = NULL;
1123 in->conversion_buffer_size = 0;
1124
1125 *stream_in = &in->stream;
1126
1127 /* Save this for adev_dump() */
1128 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1129 } else {
1130 ALOGW("proxy_prepare error %d", ret);
1131 unsigned channel_count = proxy_get_channel_count(&in->proxy);
1132 config->channel_mask = channel_count <= FCC_2
1133 ? audio_channel_in_mask_from_count(channel_count)
1134 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1135 config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy));
1136 config->sample_rate = proxy_get_sample_rate(&in->proxy);
1137 }
1138 }
1139
1140 if (ret != 0) {
1141 // Deallocate this stream on error, because AudioFlinger won't call
1142 // adev_close_input_stream() in this case.
1143 *stream_in = NULL;
1144 free(in);
1145 }
1146
1147 device_lock(in->adev);
1148 ++in->adev->inputs_open;
1149 device_unlock(in->adev);
1150
1151 return ret;
1152 }
1153
adev_close_input_stream(struct audio_hw_device * hw_dev,struct audio_stream_in * stream)1154 static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1155 struct audio_stream_in *stream)
1156 {
1157 struct stream_in *in = (struct stream_in *)stream;
1158 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile.card, in->profile.device);
1159
1160 adev_remove_stream_from_list(in->adev, &in->list_node);
1161
1162 device_lock(in->adev);
1163 --in->adev->inputs_open;
1164 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1165 "invalid inputs_open: %d", in->adev->inputs_open);
1166 device_unlock(in->adev);
1167
1168 /* Close the pcm device */
1169 in_standby(&stream->common);
1170
1171 free(in->conversion_buffer);
1172
1173 free(stream);
1174 }
1175
1176 /*
1177 * ADEV Functions
1178 */
adev_set_parameters(struct audio_hw_device * hw_dev,const char * kvpairs)1179 static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
1180 {
1181 return 0;
1182 }
1183
adev_get_parameters(const struct audio_hw_device * hw_dev,const char * keys)1184 static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
1185 {
1186 return strdup("");
1187 }
1188
adev_init_check(const struct audio_hw_device * hw_dev)1189 static int adev_init_check(const struct audio_hw_device *hw_dev)
1190 {
1191 return 0;
1192 }
1193
adev_set_voice_volume(struct audio_hw_device * hw_dev,float volume)1194 static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
1195 {
1196 return -ENOSYS;
1197 }
1198
adev_set_master_volume(struct audio_hw_device * hw_dev,float volume)1199 static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
1200 {
1201 return -ENOSYS;
1202 }
1203
adev_set_mode(struct audio_hw_device * hw_dev,audio_mode_t mode)1204 static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
1205 {
1206 return 0;
1207 }
1208
adev_set_mic_mute(struct audio_hw_device * hw_dev,bool state)1209 static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
1210 {
1211 struct audio_device * adev = (struct audio_device *)hw_dev;
1212 device_lock(adev);
1213 adev->mic_muted = state;
1214 device_unlock(adev);
1215 return -ENOSYS;
1216 }
1217
adev_get_mic_mute(const struct audio_hw_device * hw_dev,bool * state)1218 static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
1219 {
1220 return -ENOSYS;
1221 }
1222
adev_dump(const struct audio_hw_device * device,int fd)1223 static int adev_dump(const struct audio_hw_device *device, int fd)
1224 {
1225 dprintf(fd, "\nUSB audio module:\n");
1226
1227 struct audio_device* adev = (struct audio_device*)device;
1228 const int kNumRetries = 3;
1229 const int kSleepTimeMS = 500;
1230
1231 // use device_try_lock() in case we dumpsys during a deadlock
1232 int retry = kNumRetries;
1233 while (retry > 0 && device_try_lock(adev) != 0) {
1234 sleep(kSleepTimeMS);
1235 retry--;
1236 }
1237
1238 if (retry > 0) {
1239 if (list_empty(&adev->output_stream_list)) {
1240 dprintf(fd, " No output streams.\n");
1241 } else {
1242 struct listnode* node;
1243 list_for_each(node, &adev->output_stream_list) {
1244 struct audio_stream* stream =
1245 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1246 out_dump(stream, fd);
1247 }
1248 }
1249
1250 if (list_empty(&adev->input_stream_list)) {
1251 dprintf(fd, "\n No input streams.\n");
1252 } else {
1253 struct listnode* node;
1254 list_for_each(node, &adev->input_stream_list) {
1255 struct audio_stream* stream =
1256 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1257 in_dump(stream, fd);
1258 }
1259 }
1260
1261 device_unlock(adev);
1262 } else {
1263 // Couldn't lock
1264 dprintf(fd, " Could not obtain device lock.\n");
1265 }
1266
1267 return 0;
1268 }
1269
adev_close(hw_device_t * device)1270 static int adev_close(hw_device_t *device)
1271 {
1272 free(device);
1273
1274 return 0;
1275 }
1276
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)1277 static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
1278 {
1279 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1280 return -EINVAL;
1281
1282 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
1283 if (!adev)
1284 return -ENOMEM;
1285
1286 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
1287
1288 list_init(&adev->output_stream_list);
1289 list_init(&adev->input_stream_list);
1290
1291 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
1292 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1293 adev->hw_device.common.module = (struct hw_module_t *)module;
1294 adev->hw_device.common.close = adev_close;
1295
1296 adev->hw_device.init_check = adev_init_check;
1297 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1298 adev->hw_device.set_master_volume = adev_set_master_volume;
1299 adev->hw_device.set_mode = adev_set_mode;
1300 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1301 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1302 adev->hw_device.set_parameters = adev_set_parameters;
1303 adev->hw_device.get_parameters = adev_get_parameters;
1304 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1305 adev->hw_device.open_output_stream = adev_open_output_stream;
1306 adev->hw_device.close_output_stream = adev_close_output_stream;
1307 adev->hw_device.open_input_stream = adev_open_input_stream;
1308 adev->hw_device.close_input_stream = adev_close_input_stream;
1309 adev->hw_device.dump = adev_dump;
1310
1311 *device = &adev->hw_device.common;
1312
1313 return 0;
1314 }
1315
1316 static struct hw_module_methods_t hal_module_methods = {
1317 .open = adev_open,
1318 };
1319
1320 struct audio_module HAL_MODULE_INFO_SYM = {
1321 .common = {
1322 .tag = HARDWARE_MODULE_TAG,
1323 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1324 .hal_api_version = HARDWARE_HAL_API_VERSION,
1325 .id = AUDIO_HARDWARE_MODULE_ID,
1326 .name = "USB audio HW HAL",
1327 .author = "The Android Open Source Project",
1328 .methods = &hal_module_methods,
1329 },
1330 };
1331