OSDN Git Service

android/hal-audio: Add support to unregister audio endpoints
[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
28 #include <hardware/audio.h>
29 #include <hardware/hardware.h>
30
31 #include "audio-msg.h"
32 #include "hal-log.h"
33 #include "hal-msg.h"
34 #include "../profiles/audio/a2dp-codecs.h"
35
36 static const uint8_t a2dp_src_uuid[] = {
37                 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
38                 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
39
40 static int listen_sk = -1;
41 static int audio_sk = -1;
42 static bool close_thread = false;
43
44 static pthread_t ipc_th = 0;
45 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48 struct audio_input_config {
49         uint32_t rate;
50         uint32_t channels;
51         audio_format_t format;
52 };
53
54 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
55
56 struct audio_codec {
57         uint8_t type;
58
59         int (*get_presets) (struct audio_preset *preset, size_t *len);
60
61         int (*init) (struct audio_preset *preset, void **codec_data);
62         int (*cleanup) (void *codec_data);
63         int (*get_config) (void *codec_data,
64                                         struct audio_input_config *config);
65         ssize_t (*write_data) (void *codec_data, const void *buffer,
66                                 size_t bytes);
67 };
68
69 static const struct audio_codec audio_codecs[] = {
70         {
71                 .type = A2DP_CODEC_SBC,
72
73                 .get_presets = sbc_get_presets,
74         }
75 };
76
77 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
78
79 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
80
81 struct audio_endpoint {
82         uint8_t id;
83         const struct audio_codec *codec;
84         void *codec_data;
85         int fd;
86 };
87
88 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
89
90 struct a2dp_audio_dev {
91         struct audio_hw_device dev;
92         struct audio_stream_out *out;
93 };
94
95 static const a2dp_sbc_t sbc_presets[] = {
96         {
97                 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
98                 .channel_mode = SBC_CHANNEL_MODE_MONO |
99                                 SBC_CHANNEL_MODE_DUAL_CHANNEL |
100                                 SBC_CHANNEL_MODE_STEREO |
101                                 SBC_CHANNEL_MODE_JOINT_STEREO,
102                 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
103                 .allocation_method = SBC_ALLOCATION_SNR |
104                                         SBC_ALLOCATION_LOUDNESS,
105                 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
106                                 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
107                 .min_bitpool = MIN_BITPOOL,
108                 .max_bitpool = MAX_BITPOOL
109         },
110         {
111                 .frequency = SBC_SAMPLING_FREQ_44100,
112                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
113                 .subbands = SBC_SUBBANDS_8,
114                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
115                 .block_length = SBC_BLOCK_LENGTH_16,
116                 .min_bitpool = MIN_BITPOOL,
117                 .max_bitpool = MAX_BITPOOL
118         },
119         {
120                 .frequency = SBC_SAMPLING_FREQ_48000,
121                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
122                 .subbands = SBC_SUBBANDS_8,
123                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
124                 .block_length = SBC_BLOCK_LENGTH_16,
125                 .min_bitpool = MIN_BITPOOL,
126                 .max_bitpool = MAX_BITPOOL
127         },
128 };
129
130 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
131 {
132         int i;
133         int count;
134         size_t new_len = 0;
135         uint8_t *ptr = (uint8_t *) preset;
136         size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
137
138         DBG("");
139
140         count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
141
142         for (i = 0; i < count; i++) {
143                 preset = (struct audio_preset *) ptr;
144
145                 if (new_len + preset_size > *len)
146                         break;
147
148                 preset->len = sizeof(a2dp_sbc_t);
149                 memcpy(preset->data, &sbc_presets[i], preset->len);
150
151                 new_len += preset_size;
152                 ptr += preset_size;
153         }
154
155         *len = new_len;
156
157         return i;
158 }
159
160 static void audio_ipc_cleanup(void)
161 {
162         if (audio_sk >= 0) {
163                 shutdown(audio_sk, SHUT_RDWR);
164                 audio_sk = -1;
165         }
166 }
167
168 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
169                         void *param, size_t *rsp_len, void *rsp, int *fd)
170 {
171         ssize_t ret;
172         struct msghdr msg;
173         struct iovec iv[2];
174         struct hal_hdr cmd;
175         char cmsgbuf[CMSG_SPACE(sizeof(int))];
176         struct hal_status s;
177         size_t s_len = sizeof(s);
178
179         if (audio_sk < 0) {
180                 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
181                 goto failed;
182         }
183
184         if (!rsp || !rsp_len) {
185                 memset(&s, 0, s_len);
186                 rsp_len = &s_len;
187                 rsp = &s;
188         }
189
190         memset(&msg, 0, sizeof(msg));
191         memset(&cmd, 0, sizeof(cmd));
192
193         cmd.service_id = service_id;
194         cmd.opcode = opcode;
195         cmd.len = len;
196
197         iv[0].iov_base = &cmd;
198         iv[0].iov_len = sizeof(cmd);
199
200         iv[1].iov_base = param;
201         iv[1].iov_len = len;
202
203         msg.msg_iov = iv;
204         msg.msg_iovlen = 2;
205
206         pthread_mutex_lock(&sk_mutex);
207
208         ret = sendmsg(audio_sk, &msg, 0);
209         if (ret < 0) {
210                 error("audio: Sending command failed:%s", strerror(errno));
211                 pthread_mutex_unlock(&sk_mutex);
212                 goto failed;
213         }
214
215         /* socket was shutdown */
216         if (ret == 0) {
217                 error("audio: Command socket closed");
218                 goto failed;
219         }
220
221         memset(&msg, 0, sizeof(msg));
222         memset(&cmd, 0, sizeof(cmd));
223
224         iv[0].iov_base = &cmd;
225         iv[0].iov_len = sizeof(cmd);
226
227         iv[1].iov_base = rsp;
228         iv[1].iov_len = *rsp_len;
229
230         msg.msg_iov = iv;
231         msg.msg_iovlen = 2;
232
233         if (fd) {
234                 memset(cmsgbuf, 0, sizeof(cmsgbuf));
235                 msg.msg_control = cmsgbuf;
236                 msg.msg_controllen = sizeof(cmsgbuf);
237         }
238
239         ret = recvmsg(audio_sk, &msg, 0);
240         if (ret < 0) {
241                 error("audio: Receiving command response failed:%s",
242                                                         strerror(errno));
243                 pthread_mutex_unlock(&sk_mutex);
244                 goto failed;
245         }
246
247         pthread_mutex_unlock(&sk_mutex);
248
249         if (ret < (ssize_t) sizeof(cmd)) {
250                 error("audio: Too small response received(%zd bytes)", ret);
251                 goto failed;
252         }
253
254         if (cmd.service_id != service_id) {
255                 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
256                                                                 service_id);
257                 goto failed;
258         }
259
260         if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
261                 error("audio: Malformed response received(%zd bytes)", ret);
262                 goto failed;
263         }
264
265         if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
266                 error("audio: Invalid opcode received (%u vs %u)",
267                                                 cmd.opcode, opcode);
268                 goto failed;
269         }
270
271         if (cmd.opcode == AUDIO_OP_STATUS) {
272                 struct hal_status *s = rsp;
273
274                 if (sizeof(*s) != cmd.len) {
275                         error("audio: Invalid status length");
276                         goto failed;
277                 }
278
279                 if (s->code == AUDIO_STATUS_SUCCESS) {
280                         error("audio: Invalid success status response");
281                         goto failed;
282                 }
283
284                 return s->code;
285         }
286
287         /* Receive auxiliary data in msg */
288         if (fd) {
289                 struct cmsghdr *cmsg;
290
291                 *fd = -1;
292
293                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
294                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
295                         if (cmsg->cmsg_level == SOL_SOCKET
296                                         && cmsg->cmsg_type == SCM_RIGHTS) {
297                                 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
298                                 break;
299                         }
300                 }
301         }
302
303         if (rsp_len)
304                 *rsp_len = cmd.len;
305
306         return AUDIO_STATUS_SUCCESS;
307
308 failed:
309         /* Some serious issue happen on IPC - recover */
310         shutdown(audio_sk, SHUT_RDWR);
311         audio_sk = -1;
312         return AUDIO_STATUS_FAILED;
313 }
314
315 static int ipc_open_cmd(const struct audio_codec *codec)
316 {
317         uint8_t buf[BLUEZ_AUDIO_MTU];
318         struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
319         struct audio_rsp_open rsp;
320         size_t cmd_len = sizeof(buf) - sizeof(*cmd);
321         size_t rsp_len = sizeof(rsp);
322         int result;
323
324         DBG("");
325
326         memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
327
328         cmd->codec = codec->type;
329         cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
330
331         cmd_len += sizeof(*cmd);
332
333         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
334                                 &rsp_len, &rsp, NULL);
335
336         if (result != AUDIO_STATUS_SUCCESS)
337                 return 0;
338
339         return rsp.id;
340 }
341
342 static int ipc_close_cmd(uint8_t endpoint_id)
343 {
344         struct audio_cmd_close cmd;
345         int result;
346
347         DBG("");
348
349         cmd.id = endpoint_id;
350
351         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
352                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
353
354         return result;
355 }
356
357 static int register_endpoints(void)
358 {
359         struct audio_endpoint *ep = &audio_endpoints[0];
360         size_t i;
361
362         for (i = 0; i < NUM_CODECS; i++, ep++) {
363                 const struct audio_codec *codec = &audio_codecs[i];
364
365                 ep->id = ipc_open_cmd(codec);
366
367                 if (!ep->id)
368                         return AUDIO_STATUS_FAILED;
369
370                 ep->codec = codec;
371                 ep->codec_data = NULL;
372                 ep->fd = -1;
373         }
374
375         return AUDIO_STATUS_SUCCESS;
376 }
377
378 static void unregister_endpoints(void)
379 {
380         size_t i;
381
382         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
383                 struct audio_endpoint *ep = &audio_endpoints[i];
384
385                 if (ep->id) {
386                         ipc_close_cmd(ep->id);
387                         memset(ep, 0, sizeof(*ep));
388                 }
389         }
390 }
391
392 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
393                                                                 size_t bytes)
394 {
395         DBG("");
396         return -ENOSYS;
397 }
398
399 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
400 {
401         DBG("");
402         return -ENOSYS;
403 }
404
405 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
406 {
407         DBG("");
408         return -ENOSYS;
409 }
410
411 static size_t out_get_buffer_size(const struct audio_stream *stream)
412 {
413         DBG("");
414         return -ENOSYS;
415 }
416
417 static uint32_t out_get_channels(const struct audio_stream *stream)
418 {
419         DBG("");
420         return -ENOSYS;
421 }
422
423 static audio_format_t out_get_format(const struct audio_stream *stream)
424 {
425         DBG("");
426         return -ENOSYS;
427 }
428
429 static int out_set_format(struct audio_stream *stream, audio_format_t format)
430 {
431         DBG("");
432         return -ENOSYS;
433 }
434
435 static int out_standby(struct audio_stream *stream)
436 {
437         DBG("");
438         return -ENOSYS;
439 }
440
441 static int out_dump(const struct audio_stream *stream, int fd)
442 {
443         DBG("");
444         return -ENOSYS;
445 }
446
447 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
448 {
449         DBG("");
450         return -ENOSYS;
451 }
452
453 static char *out_get_parameters(const struct audio_stream *stream,
454                                                         const char *keys)
455 {
456         DBG("");
457         return strdup("");
458 }
459
460 static uint32_t out_get_latency(const struct audio_stream_out *stream)
461 {
462         DBG("");
463         return -ENOSYS;
464 }
465
466 static int out_set_volume(struct audio_stream_out *stream, float left,
467                                                                 float right)
468 {
469         DBG("");
470         /* volume controlled in audioflinger mixer (digital) */
471         return -ENOSYS;
472 }
473
474 static int out_get_render_position(const struct audio_stream_out *stream,
475                                                         uint32_t *dsp_frames)
476 {
477         DBG("");
478         return -ENOSYS;
479 }
480
481 static int out_add_audio_effect(const struct audio_stream *stream,
482                                                         effect_handle_t effect)
483 {
484         DBG("");
485         return -ENOSYS;
486 }
487
488 static int out_remove_audio_effect(const struct audio_stream *stream,
489                                                         effect_handle_t effect)
490 {
491         DBG("");
492         return -ENOSYS;
493 }
494
495 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
496 {
497         DBG("");
498         return -ENOSYS;
499 }
500
501 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
502 {
503         DBG("");
504         return -ENOSYS;
505 }
506
507 static size_t in_get_buffer_size(const struct audio_stream *stream)
508 {
509         DBG("");
510         return -ENOSYS;
511 }
512
513 static uint32_t in_get_channels(const struct audio_stream *stream)
514 {
515         DBG("");
516         return -ENOSYS;
517 }
518
519 static audio_format_t in_get_format(const struct audio_stream *stream)
520 {
521         DBG("");
522         return -ENOSYS;
523 }
524
525 static int in_set_format(struct audio_stream *stream, audio_format_t format)
526 {
527         DBG("");
528         return -ENOSYS;
529 }
530
531 static int in_standby(struct audio_stream *stream)
532 {
533         DBG("");
534         return -ENOSYS;
535 }
536
537 static int in_dump(const struct audio_stream *stream, int fd)
538 {
539         DBG("");
540         return -ENOSYS;
541 }
542
543 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
544 {
545         DBG("");
546         return -ENOSYS;
547 }
548
549 static char *in_get_parameters(const struct audio_stream *stream,
550                                                         const char *keys)
551 {
552         DBG("");
553         return strdup("");
554 }
555
556 static int in_set_gain(struct audio_stream_in *stream, float gain)
557 {
558         DBG("");
559         return -ENOSYS;
560 }
561
562 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
563                                                                 size_t bytes)
564 {
565         DBG("");
566         return -ENOSYS;
567 }
568
569 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
570 {
571         DBG("");
572         return -ENOSYS;
573 }
574
575 static int in_add_audio_effect(const struct audio_stream *stream,
576                                                         effect_handle_t effect)
577 {
578         DBG("");
579         return -ENOSYS;
580 }
581
582 static int in_remove_audio_effect(const struct audio_stream *stream,
583                                                         effect_handle_t effect)
584 {
585         DBG("");
586         return -ENOSYS;
587 }
588
589 static int audio_open_output_stream(struct audio_hw_device *dev,
590                                         audio_io_handle_t handle,
591                                         audio_devices_t devices,
592                                         audio_output_flags_t flags,
593                                         struct audio_config *config,
594                                         struct audio_stream_out **stream_out)
595
596 {
597         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
598         struct audio_stream_out *out;
599
600         out = calloc(1, sizeof(struct audio_stream_out));
601         if (!out)
602                 return -ENOMEM;
603
604         DBG("");
605
606         out->common.get_sample_rate = out_get_sample_rate;
607         out->common.set_sample_rate = out_set_sample_rate;
608         out->common.get_buffer_size = out_get_buffer_size;
609         out->common.get_channels = out_get_channels;
610         out->common.get_format = out_get_format;
611         out->common.set_format = out_set_format;
612         out->common.standby = out_standby;
613         out->common.dump = out_dump;
614         out->common.set_parameters = out_set_parameters;
615         out->common.get_parameters = out_get_parameters;
616         out->common.add_audio_effect = out_add_audio_effect;
617         out->common.remove_audio_effect = out_remove_audio_effect;
618         out->get_latency = out_get_latency;
619         out->set_volume = out_set_volume;
620         out->write = out_write;
621         out->get_render_position = out_get_render_position;
622
623         *stream_out = out;
624         a2dp_dev->out = out;
625
626         return 0;
627 }
628
629 static void audio_close_output_stream(struct audio_hw_device *dev,
630                                         struct audio_stream_out *stream)
631 {
632         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
633
634         DBG("");
635
636         free(stream);
637         a2dp_dev->out = NULL;
638 }
639
640 static int audio_set_parameters(struct audio_hw_device *dev,
641                                                         const char *kvpairs)
642 {
643         DBG("");
644         return -ENOSYS;
645 }
646
647 static char *audio_get_parameters(const struct audio_hw_device *dev,
648                                                         const char *keys)
649 {
650         DBG("");
651         return strdup("");
652 }
653
654 static int audio_init_check(const struct audio_hw_device *dev)
655 {
656         DBG("");
657         return -ENOSYS;
658 }
659
660 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
661 {
662         DBG("");
663         return -ENOSYS;
664 }
665
666 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
667 {
668         DBG("");
669         return -ENOSYS;
670 }
671
672 static int audio_set_mode(struct audio_hw_device *dev, int mode)
673 {
674         DBG("");
675         return -ENOSYS;
676 }
677
678 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
679 {
680         DBG("");
681         return -ENOSYS;
682 }
683
684 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
685 {
686         DBG("");
687         return -ENOSYS;
688 }
689
690 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
691                                         const struct audio_config *config)
692 {
693         DBG("");
694         return -ENOSYS;
695 }
696
697 static int audio_open_input_stream(struct audio_hw_device *dev,
698                                         audio_io_handle_t handle,
699                                         audio_devices_t devices,
700                                         struct audio_config *config,
701                                         struct audio_stream_in **stream_in)
702 {
703         struct audio_stream_in *in;
704
705         DBG("");
706
707         in = calloc(1, sizeof(struct audio_stream_in));
708         if (!in)
709                 return -ENOMEM;
710
711         in->common.get_sample_rate = in_get_sample_rate;
712         in->common.set_sample_rate = in_set_sample_rate;
713         in->common.get_buffer_size = in_get_buffer_size;
714         in->common.get_channels = in_get_channels;
715         in->common.get_format = in_get_format;
716         in->common.set_format = in_set_format;
717         in->common.standby = in_standby;
718         in->common.dump = in_dump;
719         in->common.set_parameters = in_set_parameters;
720         in->common.get_parameters = in_get_parameters;
721         in->common.add_audio_effect = in_add_audio_effect;
722         in->common.remove_audio_effect = in_remove_audio_effect;
723         in->set_gain = in_set_gain;
724         in->read = in_read;
725         in->get_input_frames_lost = in_get_input_frames_lost;
726
727         *stream_in = in;
728
729         return 0;
730 }
731
732 static void audio_close_input_stream(struct audio_hw_device *dev,
733                                         struct audio_stream_in *stream_in)
734 {
735         DBG("");
736         free(stream_in);
737 }
738
739 static int audio_dump(const audio_hw_device_t *device, int fd)
740 {
741         DBG("");
742         return -ENOSYS;
743 }
744
745 static int audio_close(hw_device_t *device)
746 {
747         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
748
749         DBG("");
750
751         pthread_mutex_lock(&close_mutex);
752         audio_ipc_cleanup();
753         close_thread = true;
754         pthread_mutex_unlock(&close_mutex);
755
756         pthread_join(ipc_th, NULL);
757
758         close(listen_sk);
759         listen_sk = -1;
760
761         free(a2dp_dev);
762         return 0;
763 }
764
765 static void *ipc_handler(void *data)
766 {
767         bool done = false;
768         struct pollfd pfd;
769
770         DBG("");
771
772         while (!done) {
773                 DBG("Waiting for connection ...");
774                 audio_sk = accept(listen_sk, NULL, NULL);
775                 if (audio_sk < 0) {
776                         int err = errno;
777                         error("audio: Failed to accept socket: %d (%s)", err,
778                                                                 strerror(err));
779                         continue;
780                 }
781
782                 DBG("Audio IPC: Connected");
783
784                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
785                         error("audio: Failed to register endpoints");
786
787                         unregister_endpoints();
788
789                         shutdown(audio_sk, SHUT_RDWR);
790                         continue;
791                 }
792
793                 memset(&pfd, 0, sizeof(pfd));
794                 pfd.fd = audio_sk;
795                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
796
797                 /* Check if socket is still alive. Empty while loop.*/
798                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
799
800                 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
801                         info("Audio HAL: Socket closed");
802                         audio_sk = -1;
803                 }
804
805                 /*Check if audio_dev is closed */
806                 pthread_mutex_lock(&close_mutex);
807                 done = close_thread;
808                 close_thread = false;
809                 pthread_mutex_unlock(&close_mutex);
810         }
811
812         unregister_endpoints();
813
814         info("Closing Audio IPC thread");
815         return NULL;
816 }
817
818 static int audio_ipc_init(void)
819 {
820         struct sockaddr_un addr;
821         int err;
822         int sk;
823
824         DBG("");
825
826         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
827         if (sk < 0) {
828                 err = errno;
829                 error("audio: Failed to create socket: %d (%s)", err,
830                                                                 strerror(err));
831                 return err;
832         }
833
834         memset(&addr, 0, sizeof(addr));
835         addr.sun_family = AF_UNIX;
836
837         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
838                                         sizeof(BLUEZ_AUDIO_SK_PATH));
839
840         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
841                 err = errno;
842                 error("audio: Failed to bind socket: %d (%s)", err,
843                                                                 strerror(err));
844                 goto failed;
845         }
846
847         if (listen(sk, 1) < 0) {
848                 err = errno;
849                 error("audio: Failed to listen on the socket: %d (%s)", err,
850                                                                 strerror(err));
851                 goto failed;
852         }
853
854         listen_sk = sk;
855
856         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
857         if (err) {
858                 err = -err;
859                 ipc_th = 0;
860                 error("audio: Failed to start Audio IPC thread: %d (%s)",
861                                                         err, strerror(err));
862                 goto failed;
863         }
864
865         return 0;
866
867 failed:
868         close(sk);
869         return err;
870 }
871
872 static int audio_open(const hw_module_t *module, const char *name,
873                                                         hw_device_t **device)
874 {
875         struct a2dp_audio_dev *a2dp_dev;
876         int err;
877
878         DBG("");
879
880         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
881                 error("audio: interface %s not matching [%s]", name,
882                                                 AUDIO_HARDWARE_INTERFACE);
883                 return -EINVAL;
884         }
885
886         err = audio_ipc_init();
887         if (err)
888                 return -err;
889
890         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
891         if (!a2dp_dev)
892                 return -ENOMEM;
893
894         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
895         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
896         a2dp_dev->dev.common.close = audio_close;
897
898         a2dp_dev->dev.init_check = audio_init_check;
899         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
900         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
901         a2dp_dev->dev.set_mode = audio_set_mode;
902         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
903         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
904         a2dp_dev->dev.set_parameters = audio_set_parameters;
905         a2dp_dev->dev.get_parameters = audio_get_parameters;
906         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
907         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
908         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
909         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
910         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
911         a2dp_dev->dev.dump = audio_dump;
912
913         /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
914          * This results from the structure of following structs:a2dp_audio_dev,
915          * audio_hw_device. We will rely on this later in the code.*/
916         *device = &a2dp_dev->dev.common;
917
918         return 0;
919 }
920
921 static struct hw_module_methods_t hal_module_methods = {
922         .open = audio_open,
923 };
924
925 struct audio_module HAL_MODULE_INFO_SYM = {
926         .common = {
927         .tag = HARDWARE_MODULE_TAG,
928         .version_major = 1,
929         .version_minor = 0,
930         .id = AUDIO_HARDWARE_MODULE_ID,
931         .name = "A2DP Bluez HW HAL",
932         .author = "Intel Corporation",
933         .methods = &hal_module_methods,
934         },
935 };