OSDN Git Service

android/hal-audio: Do not allocate memory if fd < 0
[android-x86/external-bluetooth-bluez.git] / android / hal-audio.c
1 /*
2  * Copyright (C) 2013 Intel Corporation
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
18 #include <errno.h>
19 #include <pthread.h>
20 #include <poll.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <arpa/inet.h>
28 #include <fcntl.h>
29
30 #include <hardware/audio.h>
31 #include <hardware/hardware.h>
32
33 #include <sbc/sbc.h>
34
35 #include "audio-msg.h"
36 #include "hal-log.h"
37 #include "hal-msg.h"
38 #include "../profiles/audio/a2dp-codecs.h"
39
40 #define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
41
42 static const uint8_t a2dp_src_uuid[] = {
43                 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
44                 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
45
46 static int listen_sk = -1;
47 static int audio_sk = -1;
48
49 static pthread_t ipc_th = 0;
50 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 #if __BYTE_ORDER == __LITTLE_ENDIAN
53
54 struct rtp_header {
55         unsigned cc:4;
56         unsigned x:1;
57         unsigned p:1;
58         unsigned v:2;
59
60         unsigned pt:7;
61         unsigned m:1;
62
63         uint16_t sequence_number;
64         uint32_t timestamp;
65         uint32_t ssrc;
66         uint32_t csrc[0];
67 } __attribute__ ((packed));
68
69 struct rtp_payload {
70         unsigned frame_count:4;
71         unsigned rfa0:1;
72         unsigned is_last_fragment:1;
73         unsigned is_first_fragment:1;
74         unsigned is_fragmented:1;
75 } __attribute__ ((packed));
76
77 #elif __BYTE_ORDER == __BIG_ENDIAN
78
79 struct rtp_header {
80         unsigned v:2;
81         unsigned p:1;
82         unsigned x:1;
83         unsigned cc:4;
84
85         unsigned m:1;
86         unsigned pt:7;
87
88         uint16_t sequence_number;
89         uint32_t timestamp;
90         uint32_t ssrc;
91         uint32_t csrc[0];
92 } __attribute__ ((packed));
93
94 struct rtp_payload {
95         unsigned is_fragmented:1;
96         unsigned is_first_fragment:1;
97         unsigned is_last_fragment:1;
98         unsigned rfa0:1;
99         unsigned frame_count:4;
100 } __attribute__ ((packed));
101
102 #else
103 #error "Unknown byte order"
104 #endif
105
106 struct media_packet {
107         struct rtp_header hdr;
108         struct rtp_payload payload;
109         uint8_t data[0];
110 };
111
112 struct audio_input_config {
113         uint32_t rate;
114         uint32_t channels;
115         audio_format_t format;
116 };
117
118 struct sbc_data {
119         a2dp_sbc_t sbc;
120
121         sbc_t enc;
122
123         size_t in_frame_len;
124         size_t in_buf_size;
125
126         size_t out_buf_size;
127         uint8_t *out_buf;
128
129         unsigned frame_duration;
130         unsigned frames_per_packet;
131
132         struct timespec start;
133         unsigned frames_sent;
134
135         uint16_t seq;
136 };
137
138 static inline void timespec_diff(struct timespec *a, struct timespec *b,
139                                         struct timespec *res)
140 {
141         res->tv_sec = a->tv_sec - b->tv_sec;
142         res->tv_nsec = a->tv_nsec - b->tv_nsec;
143
144         if (res->tv_nsec < 0) {
145                 res->tv_sec--;
146                 res->tv_nsec += 1000000000; /* 1sec */
147         }
148 }
149
150 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
151 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
152                                 void **codec_data);
153 static int sbc_cleanup(void *codec_data);
154 static int sbc_get_config(void *codec_data, struct audio_input_config *config);
155 static size_t sbc_get_buffer_size(void *codec_data);
156 static size_t sbc_get_mediapacket_duration(void *codec_data);
157 static void sbc_resume(void *codec_data);
158 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
159                                         size_t bytes, int fd);
160
161 struct audio_codec {
162         uint8_t type;
163
164         int (*get_presets) (struct audio_preset *preset, size_t *len);
165
166         int (*init) (struct audio_preset *preset, uint16_t mtu,
167                                 void **codec_data);
168         int (*cleanup) (void *codec_data);
169         int (*get_config) (void *codec_data,
170                                         struct audio_input_config *config);
171         size_t (*get_buffer_size) (void *codec_data);
172         size_t (*get_mediapacket_duration) (void *codec_data);
173         void (*resume) (void *codec_data);
174         ssize_t (*write_data) (void *codec_data, const void *buffer,
175                                 size_t bytes, int fd);
176 };
177
178 static const struct audio_codec audio_codecs[] = {
179         {
180                 .type = A2DP_CODEC_SBC,
181
182                 .get_presets = sbc_get_presets,
183
184                 .init = sbc_codec_init,
185                 .cleanup = sbc_cleanup,
186                 .get_config = sbc_get_config,
187                 .get_buffer_size = sbc_get_buffer_size,
188                 .get_mediapacket_duration = sbc_get_mediapacket_duration,
189                 .resume = sbc_resume,
190                 .write_data = sbc_write_data,
191         }
192 };
193
194 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
195
196 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
197
198 struct audio_endpoint {
199         uint8_t id;
200         const struct audio_codec *codec;
201         void *codec_data;
202         int fd;
203 };
204
205 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
206
207 enum a2dp_state_t {
208         AUDIO_A2DP_STATE_NONE,
209         AUDIO_A2DP_STATE_STANDBY,
210         AUDIO_A2DP_STATE_SUSPENDED,
211         AUDIO_A2DP_STATE_STARTED
212 };
213
214 struct a2dp_stream_out {
215         struct audio_stream_out stream;
216
217         struct audio_endpoint *ep;
218         enum a2dp_state_t audio_state;
219         struct audio_input_config cfg;
220 };
221
222 struct a2dp_audio_dev {
223         struct audio_hw_device dev;
224         struct a2dp_stream_out *out;
225 };
226
227 static const a2dp_sbc_t sbc_presets[] = {
228         {
229                 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
230                 .channel_mode = SBC_CHANNEL_MODE_MONO |
231                                 SBC_CHANNEL_MODE_DUAL_CHANNEL |
232                                 SBC_CHANNEL_MODE_STEREO |
233                                 SBC_CHANNEL_MODE_JOINT_STEREO,
234                 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
235                 .allocation_method = SBC_ALLOCATION_SNR |
236                                         SBC_ALLOCATION_LOUDNESS,
237                 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
238                                 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
239                 .min_bitpool = MIN_BITPOOL,
240                 .max_bitpool = MAX_BITPOOL
241         },
242         {
243                 .frequency = SBC_SAMPLING_FREQ_44100,
244                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
245                 .subbands = SBC_SUBBANDS_8,
246                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
247                 .block_length = SBC_BLOCK_LENGTH_16,
248                 .min_bitpool = MIN_BITPOOL,
249                 .max_bitpool = MAX_BITPOOL
250         },
251         {
252                 .frequency = SBC_SAMPLING_FREQ_48000,
253                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
254                 .subbands = SBC_SUBBANDS_8,
255                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
256                 .block_length = SBC_BLOCK_LENGTH_16,
257                 .min_bitpool = MIN_BITPOOL,
258                 .max_bitpool = MAX_BITPOOL
259         },
260 };
261
262 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
263 {
264         int i;
265         int count;
266         size_t new_len = 0;
267         uint8_t *ptr = (uint8_t *) preset;
268         size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
269
270         count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
271
272         for (i = 0; i < count; i++) {
273                 preset = (struct audio_preset *) ptr;
274
275                 if (new_len + preset_size > *len)
276                         break;
277
278                 preset->len = sizeof(a2dp_sbc_t);
279                 memcpy(preset->data, &sbc_presets[i], preset->len);
280
281                 new_len += preset_size;
282                 ptr += preset_size;
283         }
284
285         *len = new_len;
286
287         return i;
288 }
289
290 static void sbc_init_encoder(struct sbc_data *sbc_data)
291 {
292         a2dp_sbc_t *in = &sbc_data->sbc;
293         sbc_t *out = &sbc_data->enc;
294
295         sbc_init_a2dp(out, 0L, in, sizeof(*in));
296
297         out->endian = SBC_LE;
298         out->bitpool = in->max_bitpool;
299 }
300
301 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
302                                 void **codec_data)
303 {
304         struct sbc_data *sbc_data;
305         size_t hdr_len = sizeof(struct media_packet);
306         size_t in_frame_len;
307         size_t out_frame_len;
308         size_t num_frames;
309
310         if (preset->len != sizeof(a2dp_sbc_t)) {
311                 error("SBC: preset size mismatch");
312                 return AUDIO_STATUS_FAILED;
313         }
314
315         sbc_data = calloc(sizeof(struct sbc_data), 1);
316         if (!sbc_data)
317                 return AUDIO_STATUS_FAILED;
318
319         memcpy(&sbc_data->sbc, preset->data, preset->len);
320
321         sbc_init_encoder(sbc_data);
322
323         in_frame_len = sbc_get_codesize(&sbc_data->enc);
324         out_frame_len = sbc_get_frame_length(&sbc_data->enc);
325         num_frames = (mtu - hdr_len) / out_frame_len;
326
327         sbc_data->in_frame_len = in_frame_len;
328         sbc_data->in_buf_size = num_frames * in_frame_len;
329
330         sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
331         sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
332
333         sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
334         sbc_data->frames_per_packet = num_frames;
335
336         *codec_data = sbc_data;
337
338         return AUDIO_STATUS_SUCCESS;
339 }
340
341 static int sbc_cleanup(void *codec_data)
342 {
343         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
344
345         sbc_finish(&sbc_data->enc);
346         free(sbc_data->out_buf);
347         free(codec_data);
348
349         return AUDIO_STATUS_SUCCESS;
350 }
351
352 static int sbc_get_config(void *codec_data, struct audio_input_config *config)
353 {
354         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
355
356         switch (sbc_data->sbc.frequency) {
357         case SBC_SAMPLING_FREQ_16000:
358                 config->rate = 16000;
359                 break;
360         case SBC_SAMPLING_FREQ_32000:
361                 config->rate = 32000;
362                 break;
363         case SBC_SAMPLING_FREQ_44100:
364                 config->rate = 44100;
365                 break;
366         case SBC_SAMPLING_FREQ_48000:
367                 config->rate = 48000;
368                 break;
369         default:
370                 return AUDIO_STATUS_FAILED;
371         }
372         config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
373                                 AUDIO_CHANNEL_OUT_MONO :
374                                 AUDIO_CHANNEL_OUT_STEREO;
375         config->format = AUDIO_FORMAT_PCM_16_BIT;
376
377         return AUDIO_STATUS_SUCCESS;
378 }
379
380 static size_t sbc_get_buffer_size(void *codec_data)
381 {
382         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
383
384         return sbc_data->in_buf_size;
385 }
386
387 static size_t sbc_get_mediapacket_duration(void *codec_data)
388 {
389         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
390
391         return sbc_data->frame_duration * sbc_data->frames_per_packet;
392 }
393
394 static void sbc_resume(void *codec_data)
395 {
396         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
397
398         DBG("");
399
400         clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
401
402         sbc_data->frames_sent = 0;
403 }
404
405 static int write_media_packet(int fd, struct sbc_data *sbc_data,
406                                 struct media_packet *mp, size_t data_len)
407 {
408         struct timespec cur;
409         struct timespec diff;
410         unsigned expected_frames;
411         int ret;
412
413         while (true) {
414                 ret = write(fd, mp, sizeof(*mp) + data_len);
415                 if (ret >= 0)
416                         break;
417
418                 if (errno != EINTR)
419                         return -errno;
420         }
421
422         sbc_data->frames_sent += mp->payload.frame_count;
423
424         clock_gettime(CLOCK_MONOTONIC, &cur);
425         timespec_diff(&cur, &sbc_data->start, &diff);
426         expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
427                                                 sbc_data->frame_duration;
428
429         /* AudioFlinger does not seem to provide any *working*
430          * API to provide data in some interval and will just
431          * send another buffer as soon as we process current
432          * one. To prevent overflowing L2CAP socket, we need to
433          * introduce some artificial delay here base on how many
434          * audio frames were sent so far, i.e. if we're not
435          * lagging behind audio stream, we can sleep for
436          * duration of single media packet.
437          */
438         if (sbc_data->frames_sent >= expected_frames)
439                 usleep(sbc_data->frame_duration *
440                                 mp->payload.frame_count);
441
442         return ret;
443 }
444
445 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
446                                 size_t bytes, int fd)
447 {
448         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
449         size_t consumed = 0;
450         size_t encoded = 0;
451         struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
452         size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
453         int ret;
454
455         mp->hdr.v = 2;
456         mp->hdr.pt = 1;
457         mp->hdr.sequence_number = htons(sbc_data->seq++);
458         mp->hdr.ssrc = htonl(1);
459         mp->payload.frame_count = 0;
460
461         while (bytes - consumed >= sbc_data->in_frame_len) {
462                 ssize_t written = 0;
463
464                 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
465                                         sbc_data->in_frame_len,
466                                         mp->data + encoded, free_space,
467                                         &written);
468
469                 if (ret < 0) {
470                         error("SBC: failed to encode block");
471                         break;
472                 }
473
474                 mp->payload.frame_count++;
475
476                 consumed += ret;
477                 encoded += written;
478                 free_space -= written;
479
480                 /* write data if we either filled media packed or encoded all
481                  * input data
482                  */
483                 if (mp->payload.frame_count == sbc_data->frames_per_packet ||
484                                 bytes == consumed) {
485                         ret = write_media_packet(fd, sbc_data, mp, encoded);
486                         if (ret < 0)
487                                 return ret;
488
489                         encoded = 0;
490                         free_space = sbc_data->out_buf_size - sizeof(*mp);
491                         mp->payload.frame_count = 0;
492                 }
493         }
494
495         if (consumed != bytes) {
496                 /* we should encode all input data
497                  * if we did not, something went wrong but we can't really
498                  * handle this so this is just sanity check
499                  */
500                 error("SBC: failed to encode complete input buffer");
501         }
502
503         /* we always assume that all data was processed and sent */
504         return bytes;
505 }
506
507 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
508                         void *param, size_t *rsp_len, void *rsp, int *fd)
509 {
510         ssize_t ret;
511         struct msghdr msg;
512         struct iovec iv[2];
513         struct hal_hdr cmd;
514         char cmsgbuf[CMSG_SPACE(sizeof(int))];
515         struct hal_status s;
516         size_t s_len = sizeof(s);
517
518         pthread_mutex_lock(&sk_mutex);
519
520         if (audio_sk < 0) {
521                 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
522                 goto failed;
523         }
524
525         if (!rsp || !rsp_len) {
526                 memset(&s, 0, s_len);
527                 rsp_len = &s_len;
528                 rsp = &s;
529         }
530
531         memset(&msg, 0, sizeof(msg));
532         memset(&cmd, 0, sizeof(cmd));
533
534         cmd.service_id = service_id;
535         cmd.opcode = opcode;
536         cmd.len = len;
537
538         iv[0].iov_base = &cmd;
539         iv[0].iov_len = sizeof(cmd);
540
541         iv[1].iov_base = param;
542         iv[1].iov_len = len;
543
544         msg.msg_iov = iv;
545         msg.msg_iovlen = 2;
546
547         ret = sendmsg(audio_sk, &msg, 0);
548         if (ret < 0) {
549                 error("audio: Sending command failed:%s", strerror(errno));
550                 goto failed;
551         }
552
553         /* socket was shutdown */
554         if (ret == 0) {
555                 error("audio: Command socket closed");
556                 goto failed;
557         }
558
559         memset(&msg, 0, sizeof(msg));
560         memset(&cmd, 0, sizeof(cmd));
561
562         iv[0].iov_base = &cmd;
563         iv[0].iov_len = sizeof(cmd);
564
565         iv[1].iov_base = rsp;
566         iv[1].iov_len = *rsp_len;
567
568         msg.msg_iov = iv;
569         msg.msg_iovlen = 2;
570
571         if (fd) {
572                 memset(cmsgbuf, 0, sizeof(cmsgbuf));
573                 msg.msg_control = cmsgbuf;
574                 msg.msg_controllen = sizeof(cmsgbuf);
575         }
576
577         ret = recvmsg(audio_sk, &msg, 0);
578         if (ret < 0) {
579                 error("audio: Receiving command response failed:%s",
580                                                         strerror(errno));
581                 goto failed;
582         }
583
584         if (ret < (ssize_t) sizeof(cmd)) {
585                 error("audio: Too small response received(%zd bytes)", ret);
586                 goto failed;
587         }
588
589         if (cmd.service_id != service_id) {
590                 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
591                                                                 service_id);
592                 goto failed;
593         }
594
595         if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
596                 error("audio: Malformed response received(%zd bytes)", ret);
597                 goto failed;
598         }
599
600         if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
601                 error("audio: Invalid opcode received (%u vs %u)",
602                                                 cmd.opcode, opcode);
603                 goto failed;
604         }
605
606         if (cmd.opcode == AUDIO_OP_STATUS) {
607                 struct hal_status *s = rsp;
608
609                 if (sizeof(*s) != cmd.len) {
610                         error("audio: Invalid status length");
611                         goto failed;
612                 }
613
614                 if (s->code == AUDIO_STATUS_SUCCESS) {
615                         error("audio: Invalid success status response");
616                         goto failed;
617                 }
618
619                 pthread_mutex_unlock(&sk_mutex);
620
621                 return s->code;
622         }
623
624         pthread_mutex_unlock(&sk_mutex);
625
626         /* Receive auxiliary data in msg */
627         if (fd) {
628                 struct cmsghdr *cmsg;
629
630                 *fd = -1;
631
632                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
633                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
634                         if (cmsg->cmsg_level == SOL_SOCKET
635                                         && cmsg->cmsg_type == SCM_RIGHTS) {
636                                 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
637                                 break;
638                         }
639                 }
640
641                 if (*fd < 0)
642                         goto failed;
643         }
644
645         if (rsp_len)
646                 *rsp_len = cmd.len;
647
648         return AUDIO_STATUS_SUCCESS;
649
650 failed:
651         /* Some serious issue happen on IPC - recover */
652         shutdown(audio_sk, SHUT_RDWR);
653         pthread_mutex_unlock(&sk_mutex);
654
655         return AUDIO_STATUS_FAILED;
656 }
657
658 static int ipc_open_cmd(const struct audio_codec *codec)
659 {
660         uint8_t buf[BLUEZ_AUDIO_MTU];
661         struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
662         struct audio_rsp_open rsp;
663         size_t cmd_len = sizeof(buf) - sizeof(*cmd);
664         size_t rsp_len = sizeof(rsp);
665         int result;
666
667         DBG("");
668
669         memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
670
671         cmd->codec = codec->type;
672         cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
673
674         cmd_len += sizeof(*cmd);
675
676         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
677                                 &rsp_len, &rsp, NULL);
678
679         if (result != AUDIO_STATUS_SUCCESS)
680                 return 0;
681
682         return rsp.id;
683 }
684
685 static int ipc_close_cmd(uint8_t endpoint_id)
686 {
687         struct audio_cmd_close cmd;
688         int result;
689
690         DBG("");
691
692         cmd.id = endpoint_id;
693
694         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
695                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
696
697         return result;
698 }
699
700 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
701                                         struct audio_preset **caps)
702 {
703         char buf[BLUEZ_AUDIO_MTU];
704         struct audio_cmd_open_stream cmd;
705         struct audio_rsp_open_stream *rsp =
706                                         (struct audio_rsp_open_stream *) &buf;
707         size_t rsp_len = sizeof(buf);
708         int result;
709
710         DBG("");
711
712         if (!caps)
713                 return AUDIO_STATUS_FAILED;
714
715         cmd.id = endpoint_id;
716
717         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
718                                 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
719         if (result == AUDIO_STATUS_SUCCESS) {
720                 size_t buf_len = sizeof(struct audio_preset) +
721                                         rsp->preset[0].len;
722                 *mtu = rsp->mtu;
723                 *caps = malloc(buf_len);
724                 memcpy(*caps, &rsp->preset, buf_len);
725         } else {
726                 *caps = NULL;
727         }
728
729         return result;
730 }
731
732 static int ipc_close_stream_cmd(uint8_t endpoint_id)
733 {
734         struct audio_cmd_close_stream cmd;
735         int result;
736
737         DBG("");
738
739         cmd.id = endpoint_id;
740
741         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
742                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
743
744         return result;
745 }
746
747 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
748 {
749         struct audio_cmd_resume_stream cmd;
750         int result;
751
752         DBG("");
753
754         cmd.id = endpoint_id;
755
756         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
757                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
758
759         return result;
760 }
761
762 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
763 {
764         struct audio_cmd_suspend_stream cmd;
765         int result;
766
767         DBG("");
768
769         cmd.id = endpoint_id;
770
771         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
772                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
773
774         return result;
775 }
776
777 static int register_endpoints(void)
778 {
779         struct audio_endpoint *ep = &audio_endpoints[0];
780         size_t i;
781
782         for (i = 0; i < NUM_CODECS; i++, ep++) {
783                 const struct audio_codec *codec = &audio_codecs[i];
784
785                 ep->id = ipc_open_cmd(codec);
786
787                 if (!ep->id)
788                         return AUDIO_STATUS_FAILED;
789
790                 ep->codec = codec;
791                 ep->codec_data = NULL;
792                 ep->fd = -1;
793         }
794
795         return AUDIO_STATUS_SUCCESS;
796 }
797
798 static void unregister_endpoints(void)
799 {
800         size_t i;
801
802         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
803                 struct audio_endpoint *ep = &audio_endpoints[i];
804
805                 if (ep->id) {
806                         ipc_close_cmd(ep->id);
807                         memset(ep, 0, sizeof(*ep));
808                 }
809         }
810 }
811
812 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
813                                                                 size_t bytes)
814 {
815         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
816
817         /* We can auto-start only from standby */
818         if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
819                 DBG("stream in standby, auto-start");
820
821                 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
822                         return -1;
823
824                 out->ep->codec->resume(out->ep->codec_data);
825
826                 out->audio_state = AUDIO_A2DP_STATE_STARTED;
827         }
828
829         if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
830                 error("audio: stream not started");
831                 return -1;
832         }
833
834         if (out->ep->fd < 0) {
835                 error("audio: no transport socket");
836                 return -1;
837         }
838
839         return out->ep->codec->write_data(out->ep->codec_data, buffer,
840                                                 bytes, out->ep->fd);
841 }
842
843 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
844 {
845         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
846
847         DBG("");
848
849         return out->cfg.rate;
850 }
851
852 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
853 {
854         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
855
856         DBG("");
857
858         if (rate != out->cfg.rate) {
859                 warn("audio: cannot set sample rate to %d", rate);
860                 return -1;
861         }
862
863         return 0;
864 }
865
866 static size_t out_get_buffer_size(const struct audio_stream *stream)
867 {
868         DBG("");
869
870         /* We should return proper buffer size calculated by codec (so each
871          * input buffer is encoded into single media packed) but this does not
872          * work well with AudioFlinger and causes problems. For this reason we
873          * use magic value here and out_write code takes care of splitting
874          * input buffer into multiple media packets.
875          */
876         return 20 * 512;
877 }
878
879 static uint32_t out_get_channels(const struct audio_stream *stream)
880 {
881         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
882
883         DBG("");
884
885         return out->cfg.channels;
886 }
887
888 static audio_format_t out_get_format(const struct audio_stream *stream)
889 {
890         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
891
892         DBG("");
893
894         return out->cfg.format;
895 }
896
897 static int out_set_format(struct audio_stream *stream, audio_format_t format)
898 {
899         DBG("");
900         return -ENOSYS;
901 }
902
903 static int out_standby(struct audio_stream *stream)
904 {
905         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
906
907         DBG("");
908
909         if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
910                 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
911                         return -1;
912                 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
913         }
914
915         return 0;
916 }
917
918 static int out_dump(const struct audio_stream *stream, int fd)
919 {
920         DBG("");
921         return -ENOSYS;
922 }
923
924 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
925 {
926         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
927         char *kvpair;
928         char *str;
929         char *saveptr;
930         bool enter_suspend = false;
931         bool exit_suspend = false;
932
933         DBG("%s", kvpairs);
934
935         str = strdup(kvpairs);
936         kvpair = strtok_r(str, ";", &saveptr);
937
938         for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
939                 char *keyval;
940
941                 keyval = strchr(kvpair, '=');
942                 if (!keyval)
943                         continue;
944
945                 *keyval = '\0';
946                 keyval++;
947
948                 if (!strcmp(kvpair, "closing")) {
949                         if (!strcmp(keyval, "true"))
950                                 out->audio_state = AUDIO_A2DP_STATE_NONE;
951                 } else if (!strcmp(kvpair, "A2dpSuspended")) {
952                         if (!strcmp(keyval, "true"))
953                                 enter_suspend = true;
954                         else
955                                 exit_suspend = true;
956                 }
957         }
958
959         free(str);
960
961         if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
962                 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
963                         return -1;
964                 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
965         }
966
967         if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
968                 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
969
970         return 0;
971 }
972
973 static char *out_get_parameters(const struct audio_stream *stream,
974                                                         const char *keys)
975 {
976         DBG("");
977         return strdup("");
978 }
979
980 static uint32_t out_get_latency(const struct audio_stream_out *stream)
981 {
982         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
983         struct audio_endpoint *ep = out->ep;
984         size_t pkt_duration;
985
986         DBG("");
987
988         pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
989
990         return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
991 }
992
993 static int out_set_volume(struct audio_stream_out *stream, float left,
994                                                                 float right)
995 {
996         DBG("");
997         /* volume controlled in audioflinger mixer (digital) */
998         return -ENOSYS;
999 }
1000
1001 static int out_get_render_position(const struct audio_stream_out *stream,
1002                                                         uint32_t *dsp_frames)
1003 {
1004         DBG("");
1005         return -ENOSYS;
1006 }
1007
1008 static int out_add_audio_effect(const struct audio_stream *stream,
1009                                                         effect_handle_t effect)
1010 {
1011         DBG("");
1012         return -ENOSYS;
1013 }
1014
1015 static int out_remove_audio_effect(const struct audio_stream *stream,
1016                                                         effect_handle_t effect)
1017 {
1018         DBG("");
1019         return -ENOSYS;
1020 }
1021
1022 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1023 {
1024         DBG("");
1025         return -ENOSYS;
1026 }
1027
1028 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1029 {
1030         DBG("");
1031         return -ENOSYS;
1032 }
1033
1034 static size_t in_get_buffer_size(const struct audio_stream *stream)
1035 {
1036         DBG("");
1037         return -ENOSYS;
1038 }
1039
1040 static uint32_t in_get_channels(const struct audio_stream *stream)
1041 {
1042         DBG("");
1043         return -ENOSYS;
1044 }
1045
1046 static audio_format_t in_get_format(const struct audio_stream *stream)
1047 {
1048         DBG("");
1049         return -ENOSYS;
1050 }
1051
1052 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1053 {
1054         DBG("");
1055         return -ENOSYS;
1056 }
1057
1058 static int in_standby(struct audio_stream *stream)
1059 {
1060         DBG("");
1061         return -ENOSYS;
1062 }
1063
1064 static int in_dump(const struct audio_stream *stream, int fd)
1065 {
1066         DBG("");
1067         return -ENOSYS;
1068 }
1069
1070 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1071 {
1072         DBG("");
1073         return -ENOSYS;
1074 }
1075
1076 static char *in_get_parameters(const struct audio_stream *stream,
1077                                                         const char *keys)
1078 {
1079         DBG("");
1080         return strdup("");
1081 }
1082
1083 static int in_set_gain(struct audio_stream_in *stream, float gain)
1084 {
1085         DBG("");
1086         return -ENOSYS;
1087 }
1088
1089 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1090                                                                 size_t bytes)
1091 {
1092         DBG("");
1093         return -ENOSYS;
1094 }
1095
1096 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1097 {
1098         DBG("");
1099         return -ENOSYS;
1100 }
1101
1102 static int in_add_audio_effect(const struct audio_stream *stream,
1103                                                         effect_handle_t effect)
1104 {
1105         DBG("");
1106         return -ENOSYS;
1107 }
1108
1109 static int in_remove_audio_effect(const struct audio_stream *stream,
1110                                                         effect_handle_t effect)
1111 {
1112         DBG("");
1113         return -ENOSYS;
1114 }
1115
1116 static int set_blocking(int fd)
1117 {
1118         int flags;
1119
1120         flags = fcntl(fd, F_GETFL, 0);
1121         if (flags < 0) {
1122                 error("fcntl(F_GETFL): %s (%d)", strerror(errno), errno);
1123                 return -errno;
1124         }
1125
1126         if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
1127                 error("fcntl(F_SETFL): %s (%d)", strerror(errno), errno);
1128                 return -errno;
1129         }
1130
1131         return 0;
1132 }
1133
1134 static int audio_open_output_stream(struct audio_hw_device *dev,
1135                                         audio_io_handle_t handle,
1136                                         audio_devices_t devices,
1137                                         audio_output_flags_t flags,
1138                                         struct audio_config *config,
1139                                         struct audio_stream_out **stream_out)
1140
1141 {
1142         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1143         struct a2dp_stream_out *out;
1144         struct audio_preset *preset;
1145         const struct audio_codec *codec;
1146         uint16_t mtu;
1147         int fd;
1148
1149         out = calloc(1, sizeof(struct a2dp_stream_out));
1150         if (!out)
1151                 return -ENOMEM;
1152
1153         DBG("");
1154
1155         out->stream.common.get_sample_rate = out_get_sample_rate;
1156         out->stream.common.set_sample_rate = out_set_sample_rate;
1157         out->stream.common.get_buffer_size = out_get_buffer_size;
1158         out->stream.common.get_channels = out_get_channels;
1159         out->stream.common.get_format = out_get_format;
1160         out->stream.common.set_format = out_set_format;
1161         out->stream.common.standby = out_standby;
1162         out->stream.common.dump = out_dump;
1163         out->stream.common.set_parameters = out_set_parameters;
1164         out->stream.common.get_parameters = out_get_parameters;
1165         out->stream.common.add_audio_effect = out_add_audio_effect;
1166         out->stream.common.remove_audio_effect = out_remove_audio_effect;
1167         out->stream.get_latency = out_get_latency;
1168         out->stream.set_volume = out_set_volume;
1169         out->stream.write = out_write;
1170         out->stream.get_render_position = out_get_render_position;
1171
1172         /* TODO: for now we always use endpoint 0 */
1173         out->ep = &audio_endpoints[0];
1174
1175         if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1176                         AUDIO_STATUS_SUCCESS)
1177                 goto fail;
1178
1179         if (!preset || fd < 0)
1180                 goto fail;
1181
1182         if (set_blocking(fd) < 0)
1183                 goto fail;
1184
1185         out->ep->fd = fd;
1186         codec = out->ep->codec;
1187
1188         codec->init(preset, mtu, &out->ep->codec_data);
1189         codec->get_config(out->ep->codec_data, &out->cfg);
1190
1191         DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1192                         out->cfg.channels, out->cfg.format);
1193
1194         free(preset);
1195
1196         *stream_out = &out->stream;
1197         a2dp_dev->out = out;
1198
1199         out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1200
1201         return 0;
1202
1203 fail:
1204         error("audio: cannot open output stream");
1205         free(out);
1206         *stream_out = NULL;
1207         return -EIO;
1208 }
1209
1210 static void audio_close_output_stream(struct audio_hw_device *dev,
1211                                         struct audio_stream_out *stream)
1212 {
1213         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1214         struct audio_endpoint *ep = a2dp_dev->out->ep;
1215
1216         DBG("");
1217
1218         ipc_close_stream_cmd(ep->id);
1219
1220         if (ep->fd >= 0) {
1221                 close(ep->fd);
1222                 ep->fd = -1;
1223         }
1224
1225         ep->codec->cleanup(ep->codec_data);
1226         ep->codec_data = NULL;
1227
1228         free(stream);
1229         a2dp_dev->out = NULL;
1230 }
1231
1232 static int audio_set_parameters(struct audio_hw_device *dev,
1233                                                         const char *kvpairs)
1234 {
1235         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1236         struct a2dp_stream_out *out = a2dp_dev->out;
1237
1238         DBG("");
1239
1240         if (!out)
1241                 return 0;
1242
1243         return out->stream.common.set_parameters((struct audio_stream *) out,
1244                                                         kvpairs);
1245 }
1246
1247 static char *audio_get_parameters(const struct audio_hw_device *dev,
1248                                                         const char *keys)
1249 {
1250         DBG("");
1251         return strdup("");
1252 }
1253
1254 static int audio_init_check(const struct audio_hw_device *dev)
1255 {
1256         DBG("");
1257         return 0;
1258 }
1259
1260 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1261 {
1262         DBG("");
1263         return -ENOSYS;
1264 }
1265
1266 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1267 {
1268         DBG("");
1269         return -ENOSYS;
1270 }
1271
1272 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1273 {
1274         DBG("");
1275         return -ENOSYS;
1276 }
1277
1278 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1279 {
1280         DBG("");
1281         return -ENOSYS;
1282 }
1283
1284 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1285 {
1286         DBG("");
1287         return -ENOSYS;
1288 }
1289
1290 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1291                                         const struct audio_config *config)
1292 {
1293         DBG("");
1294         return -ENOSYS;
1295 }
1296
1297 static int audio_open_input_stream(struct audio_hw_device *dev,
1298                                         audio_io_handle_t handle,
1299                                         audio_devices_t devices,
1300                                         struct audio_config *config,
1301                                         struct audio_stream_in **stream_in)
1302 {
1303         struct audio_stream_in *in;
1304
1305         DBG("");
1306
1307         in = calloc(1, sizeof(struct audio_stream_in));
1308         if (!in)
1309                 return -ENOMEM;
1310
1311         in->common.get_sample_rate = in_get_sample_rate;
1312         in->common.set_sample_rate = in_set_sample_rate;
1313         in->common.get_buffer_size = in_get_buffer_size;
1314         in->common.get_channels = in_get_channels;
1315         in->common.get_format = in_get_format;
1316         in->common.set_format = in_set_format;
1317         in->common.standby = in_standby;
1318         in->common.dump = in_dump;
1319         in->common.set_parameters = in_set_parameters;
1320         in->common.get_parameters = in_get_parameters;
1321         in->common.add_audio_effect = in_add_audio_effect;
1322         in->common.remove_audio_effect = in_remove_audio_effect;
1323         in->set_gain = in_set_gain;
1324         in->read = in_read;
1325         in->get_input_frames_lost = in_get_input_frames_lost;
1326
1327         *stream_in = in;
1328
1329         return 0;
1330 }
1331
1332 static void audio_close_input_stream(struct audio_hw_device *dev,
1333                                         struct audio_stream_in *stream_in)
1334 {
1335         DBG("");
1336         free(stream_in);
1337 }
1338
1339 static int audio_dump(const audio_hw_device_t *device, int fd)
1340 {
1341         DBG("");
1342         return -ENOSYS;
1343 }
1344
1345 static int audio_close(hw_device_t *device)
1346 {
1347         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1348
1349         DBG("");
1350
1351         unregister_endpoints();
1352
1353         shutdown(listen_sk, SHUT_RDWR);
1354         shutdown(audio_sk, SHUT_RDWR);
1355
1356         pthread_join(ipc_th, NULL);
1357
1358         close(listen_sk);
1359         listen_sk = -1;
1360
1361         free(a2dp_dev);
1362         return 0;
1363 }
1364
1365 static void *ipc_handler(void *data)
1366 {
1367         bool done = false;
1368         struct pollfd pfd;
1369         int sk;
1370
1371         DBG("");
1372
1373         while (!done) {
1374                 DBG("Waiting for connection ...");
1375
1376                 sk = accept(listen_sk, NULL, NULL);
1377                 if (sk < 0) {
1378                         int err = errno;
1379
1380                         if (err == EINTR)
1381                                 continue;
1382
1383                         if (err != ECONNABORTED && err != EINVAL)
1384                                 error("audio: Failed to accept socket: %d (%s)",
1385                                                         err, strerror(err));
1386
1387                         break;
1388                 }
1389
1390                 pthread_mutex_lock(&sk_mutex);
1391                 audio_sk = sk;
1392                 pthread_mutex_unlock(&sk_mutex);
1393
1394                 DBG("Audio IPC: Connected");
1395
1396                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1397                         error("audio: Failed to register endpoints");
1398
1399                         unregister_endpoints();
1400
1401                         pthread_mutex_lock(&sk_mutex);
1402                         shutdown(audio_sk, SHUT_RDWR);
1403                         close(audio_sk);
1404                         audio_sk = -1;
1405                         pthread_mutex_unlock(&sk_mutex);
1406
1407                         continue;
1408                 }
1409
1410                 memset(&pfd, 0, sizeof(pfd));
1411                 pfd.fd = audio_sk;
1412                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1413
1414                 /* Check if socket is still alive. Empty while loop.*/
1415                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1416
1417                 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1418                         info("Audio HAL: Socket closed");
1419
1420                         pthread_mutex_lock(&sk_mutex);
1421                         close(audio_sk);
1422                         audio_sk = -1;
1423                         pthread_mutex_unlock(&sk_mutex);
1424                 }
1425         }
1426
1427         /* audio_sk is closed at this point, just cleanup endpoints states */
1428         memset(audio_endpoints, 0, sizeof(audio_endpoints));
1429
1430         info("Closing Audio IPC thread");
1431         return NULL;
1432 }
1433
1434 static int audio_ipc_init(void)
1435 {
1436         struct sockaddr_un addr;
1437         int err;
1438         int sk;
1439
1440         DBG("");
1441
1442         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1443         if (sk < 0) {
1444                 err = errno;
1445                 error("audio: Failed to create socket: %d (%s)", err,
1446                                                                 strerror(err));
1447                 return err;
1448         }
1449
1450         memset(&addr, 0, sizeof(addr));
1451         addr.sun_family = AF_UNIX;
1452
1453         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1454                                         sizeof(BLUEZ_AUDIO_SK_PATH));
1455
1456         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1457                 err = errno;
1458                 error("audio: Failed to bind socket: %d (%s)", err,
1459                                                                 strerror(err));
1460                 goto failed;
1461         }
1462
1463         if (listen(sk, 1) < 0) {
1464                 err = errno;
1465                 error("audio: Failed to listen on the socket: %d (%s)", err,
1466                                                                 strerror(err));
1467                 goto failed;
1468         }
1469
1470         listen_sk = sk;
1471
1472         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1473         if (err) {
1474                 err = -err;
1475                 ipc_th = 0;
1476                 error("audio: Failed to start Audio IPC thread: %d (%s)",
1477                                                         err, strerror(err));
1478                 goto failed;
1479         }
1480
1481         return 0;
1482
1483 failed:
1484         close(sk);
1485         return err;
1486 }
1487
1488 static int audio_open(const hw_module_t *module, const char *name,
1489                                                         hw_device_t **device)
1490 {
1491         struct a2dp_audio_dev *a2dp_dev;
1492         int err;
1493
1494         DBG("");
1495
1496         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1497                 error("audio: interface %s not matching [%s]", name,
1498                                                 AUDIO_HARDWARE_INTERFACE);
1499                 return -EINVAL;
1500         }
1501
1502         err = audio_ipc_init();
1503         if (err)
1504                 return -err;
1505
1506         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1507         if (!a2dp_dev)
1508                 return -ENOMEM;
1509
1510         a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1511         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1512         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1513         a2dp_dev->dev.common.close = audio_close;
1514
1515         a2dp_dev->dev.init_check = audio_init_check;
1516         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1517         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1518         a2dp_dev->dev.set_mode = audio_set_mode;
1519         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1520         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1521         a2dp_dev->dev.set_parameters = audio_set_parameters;
1522         a2dp_dev->dev.get_parameters = audio_get_parameters;
1523         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1524         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1525         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1526         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1527         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1528         a2dp_dev->dev.dump = audio_dump;
1529
1530         /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1531          * This results from the structure of following structs:a2dp_audio_dev,
1532          * audio_hw_device. We will rely on this later in the code.*/
1533         *device = &a2dp_dev->dev.common;
1534
1535         return 0;
1536 }
1537
1538 static struct hw_module_methods_t hal_module_methods = {
1539         .open = audio_open,
1540 };
1541
1542 struct audio_module HAL_MODULE_INFO_SYM = {
1543         .common = {
1544         .tag = HARDWARE_MODULE_TAG,
1545         .version_major = 1,
1546         .version_minor = 0,
1547         .id = AUDIO_HARDWARE_MODULE_ID,
1548         .name = "A2DP Bluez HW HAL",
1549         .author = "Intel Corporation",
1550         .methods = &hal_module_methods,
1551         },
1552 };