3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/socket.h>
38 #include <netinet/in.h>
40 #include <alsa/asoundlib.h>
41 #include <alsa/pcm_external.h>
47 /* #define ENABLE_DEBUG */
49 #define UINT_SECS_MAX (UINT_MAX / 1000000 - 1)
51 #define MIN_PERIOD_TIME 1
53 #define BUFFER_SIZE 2048
56 #define DBG(fmt, arg...) printf("DEBUG: %s: " fmt "\n" , __FUNCTION__ , ## arg)
58 #define DBG(fmt, arg...)
66 #define SCO_TXBUFS 0x03
70 #define SCO_RXBUFS 0x04
74 # define MIN(x, y) ((x) < (y) ? (x) : (y))
78 # define MAX(x, y) ((x) > (y) ? (x) : (y))
81 #define MAX_BITPOOL 64
84 /* adapted from glibc sys/time.h timersub() macro */
85 #define priv_timespecsub(a, b, result) \
87 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
88 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
89 if ((result)->tv_nsec < 0) { \
91 (result)->tv_nsec += 1000000000; \
95 struct bluetooth_a2dp {
96 sbc_capabilities_t sbc_capabilities;
97 sbc_t sbc; /* Codec data */
98 int sbc_initialized; /* Keep track if the encoder is initialized */
99 unsigned int codesize; /* SBC codesize */
100 int samples; /* Number of encoded samples */
101 uint8_t buffer[BUFFER_SIZE]; /* Codec transfer buffer */
102 unsigned int count; /* Codec transfer buffer counter */
104 int nsamples; /* Cumulative number of codec samples */
105 uint16_t seq_num; /* Cumulative packet sequence */
106 int frame_count; /* Current frames in buffer*/
109 struct bluetooth_alsa_config {
110 char device[18]; /* Address of the remote Device */
112 uint8_t transport; /* Requested transport */
116 uint8_t channel_mode; /* A2DP only */
117 int has_channel_mode;
118 uint8_t allocation_method; /* A2DP only */
119 int has_allocation_method;
120 uint8_t subbands; /* A2DP only */
122 uint8_t block_length; /* A2DP only */
123 int has_block_length;
124 uint8_t bitpool; /* A2DP only */
129 struct bluetooth_data {
131 struct bluetooth_alsa_config alsa_config; /* ALSA resource file parameters */
132 volatile snd_pcm_sframes_t hw_ptr;
133 int transport; /* chosen transport SCO or AD2P */
134 unsigned int link_mtu; /* MTU for selected transport channel */
135 volatile struct pollfd stream; /* Audio stream filedescriptor */
136 struct pollfd server; /* Audio daemon filedescriptor */
137 uint8_t buffer[BUFFER_SIZE]; /* Encoded transfer buffer */
138 unsigned int count; /* Transfer buffer counter */
139 struct bluetooth_a2dp a2dp; /* A2DP data */
141 pthread_t hw_thread; /* Makes virtual hw pointer move */
142 int pipefd[2]; /* Inter thread communication */
144 sig_atomic_t reset; /* Request XRUN handling */
147 static int audioservice_send(int sk, const bt_audio_msg_header_t *msg);
148 static int audioservice_expect(int sk, bt_audio_msg_header_t *outmsg,
151 static int bluetooth_start(snd_pcm_ioplug_t *io)
153 DBG("bluetooth_start %p", io);
158 static int bluetooth_stop(snd_pcm_ioplug_t *io)
160 DBG("bluetooth_stop %p", io);
165 static void *playback_hw_thread(void *param)
167 struct bluetooth_data *data = param;
168 unsigned int prev_periods;
170 struct timespec start;
171 struct pollfd fds[2];
174 data->server.events = POLLIN;
175 /* note: only errors for data->stream.events */
177 fds[0] = data->server;
178 fds[1] = data->stream;
181 period_time = 1000000.0 * data->io.period_size / data->io.rate;
182 if (period_time > (int) (MIN_PERIOD_TIME * 1000))
183 poll_timeout = (int) (period_time / 1000.0f);
185 poll_timeout = MIN_PERIOD_TIME;
187 clock_gettime(CLOCK_MONOTONIC, &start);
190 unsigned int dtime, periods;
191 struct timespec cur, delta;
198 DBG("Handle XRUN in hw-thread.");
200 clock_gettime(CLOCK_MONOTONIC, &start);
204 clock_gettime(CLOCK_MONOTONIC, &cur);
206 priv_timespecsub(&cur, &start, &delta);
208 dtime = delta.tv_sec * 1000000 + delta.tv_nsec / 1000;
209 periods = 1.0 * dtime / period_time;
211 if (periods > prev_periods) {
213 int frags = periods - prev_periods, n;
215 data->hw_ptr += frags * data->io.period_size;
216 data->hw_ptr %= data->io.buffer_size;
218 for (n = 0; n < frags; n++) {
219 /* Notify user that hardware pointer
221 if (write(data->pipefd[1], &c, 1) < 0)
222 pthread_testcancel();
225 /* Reset point of reference to avoid too big values
226 * that wont fit an unsigned int */
227 if ((unsigned int) delta.tv_sec < UINT_SECS_MAX)
228 prev_periods = periods;
231 clock_gettime(CLOCK_MONOTONIC, &start);
236 /* sleep up to one period interval */
237 ret = poll(fds, 2, poll_timeout);
240 if (errno != EINTR) {
241 SNDERR("poll error: %s (%d)", strerror(errno),
245 } else if (ret > 0) {
246 ret = (fds[0].revents) ? 0 : 1;
247 SNDERR("poll fd %d revents %d", ret, fds[ret].revents);
248 if (fds[ret].revents & (POLLERR | POLLHUP | POLLNVAL))
252 /* Offer opportunity to be canceled by main thread */
253 pthread_testcancel();
260 static int bluetooth_playback_start(snd_pcm_ioplug_t *io)
262 struct bluetooth_data *data = io->private_data;
272 err = pthread_create(&data->hw_thread, 0, playback_hw_thread, data);
277 static int bluetooth_playback_stop(snd_pcm_ioplug_t *io)
279 struct bluetooth_data *data = io->private_data;
288 static snd_pcm_sframes_t bluetooth_pointer(snd_pcm_ioplug_t *io)
290 struct bluetooth_data *data = io->private_data;
295 static void bluetooth_exit(struct bluetooth_data *data)
297 struct bluetooth_a2dp *a2dp = &data->a2dp;
299 if (data->server.fd >= 0)
300 bt_audio_service_close(data->server.fd);
302 if (data->stream.fd >= 0)
303 close(data->stream.fd);
305 if (data->hw_thread) {
306 pthread_cancel(data->hw_thread);
307 pthread_join(data->hw_thread, 0);
310 if (a2dp->sbc_initialized)
311 sbc_finish(&a2dp->sbc);
313 if (data->pipefd[0] > 0)
314 close(data->pipefd[0]);
316 if (data->pipefd[1] > 0)
317 close(data->pipefd[1]);
322 static int bluetooth_close(snd_pcm_ioplug_t *io)
324 struct bluetooth_data *data = io->private_data;
328 bluetooth_exit(data);
333 static int bluetooth_prepare(snd_pcm_ioplug_t *io)
335 struct bluetooth_data *data = io->private_data;
337 char buf[BT_SUGGESTED_BUFFER_SIZE];
338 struct bt_start_stream_req *req = (void *) buf;
339 struct bt_start_stream_rsp *rsp = (void *) buf;
340 struct bt_new_stream_ind *ind = (void *) buf;
341 uint32_t period_count = io->buffer_size / io->period_size;
343 struct timeval t = { 0, period_count };
345 DBG("Preparing with io->period_size=%lu io->buffer_size=%lu",
346 io->period_size, io->buffer_size);
350 /* As we're gonna receive messages on the server socket, we have to stop the
351 hw thread that is polling on it, if any */
352 if (data->hw_thread) {
353 pthread_cancel(data->hw_thread);
354 pthread_join(data->hw_thread, 0);
358 if (io->stream == SND_PCM_STREAM_PLAYBACK)
359 /* If not null for playback, xmms doesn't display time
363 /* ALSA library is really picky on the fact hw_ptr is not null.
364 * If it is, capture won't start */
365 data->hw_ptr = io->period_size;
368 memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
369 req->h.type = BT_REQUEST;
370 req->h.name = BT_START_STREAM;
371 req->h.length = sizeof(*req);
373 err = audioservice_send(data->server.fd, &req->h);
377 rsp->h.length = sizeof(*rsp);
378 err = audioservice_expect(data->server.fd, &rsp->h,
383 ind->h.length = sizeof(*ind);
384 err = audioservice_expect(data->server.fd, &ind->h,
389 if (data->stream.fd >= 0)
390 close(data->stream.fd);
392 data->stream.fd = bt_audio_service_get_data_fd(data->server.fd);
393 if (data->stream.fd < 0) {
397 if (data->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
398 opt_name = (io->stream == SND_PCM_STREAM_PLAYBACK) ?
399 SO_SNDTIMEO : SO_RCVTIMEO;
401 if (setsockopt(data->stream.fd, SOL_SOCKET, opt_name, &t,
405 opt_name = (io->stream == SND_PCM_STREAM_PLAYBACK) ?
406 SCO_TXBUFS : SCO_RXBUFS;
408 if (setsockopt(data->stream.fd, SOL_SCO, opt_name, &period_count,
409 sizeof(period_count)) == 0)
412 opt_name = (io->stream == SND_PCM_STREAM_PLAYBACK) ?
413 SO_SNDBUF : SO_RCVBUF;
415 if (setsockopt(data->stream.fd, SOL_SCO, opt_name, &period_count,
416 sizeof(period_count)) == 0)
419 /* FIXME : handle error codes */
422 /* wake up any client polling at us */
423 err = write(data->pipefd[1], &c, 1);
430 static int bluetooth_hsp_hw_params(snd_pcm_ioplug_t *io,
431 snd_pcm_hw_params_t *params)
433 struct bluetooth_data *data = io->private_data;
434 char buf[BT_SUGGESTED_BUFFER_SIZE];
435 struct bt_open_req *open_req = (void *) buf;
436 struct bt_open_rsp *open_rsp = (void *) buf;
437 struct bt_set_configuration_req *req = (void *) buf;
438 struct bt_set_configuration_rsp *rsp = (void *) buf;
441 DBG("Preparing with io->period_size=%lu io->buffer_size=%lu",
442 io->period_size, io->buffer_size);
444 memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
445 open_req->h.type = BT_REQUEST;
446 open_req->h.name = BT_OPEN;
447 open_req->h.length = sizeof(*open_req);
449 strncpy(open_req->destination, data->alsa_config.device, 18);
450 open_req->seid = BT_A2DP_SEID_RANGE + 1;
451 open_req->lock = (io->stream == SND_PCM_STREAM_PLAYBACK ?
452 BT_WRITE_LOCK : BT_READ_LOCK);
454 err = audioservice_send(data->server.fd, &open_req->h);
458 open_rsp->h.length = sizeof(*open_rsp);
459 err = audioservice_expect(data->server.fd, &open_rsp->h,
464 memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
465 req->h.type = BT_REQUEST;
466 req->h.name = BT_SET_CONFIGURATION;
467 req->h.length = sizeof(*req);
469 req->codec.transport = BT_CAPABILITIES_TRANSPORT_SCO;
470 req->codec.seid = BT_A2DP_SEID_RANGE + 1;
471 req->codec.length = sizeof(pcm_capabilities_t);
473 req->h.length += req->codec.length - sizeof(req->codec);
474 err = audioservice_send(data->server.fd, &req->h);
478 rsp->h.length = sizeof(*rsp);
479 err = audioservice_expect(data->server.fd, &rsp->h,
480 BT_SET_CONFIGURATION);
484 data->transport = BT_CAPABILITIES_TRANSPORT_SCO;
485 data->link_mtu = rsp->link_mtu;
490 static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
493 case BT_SBC_SAMPLING_FREQ_16000:
494 case BT_SBC_SAMPLING_FREQ_32000:
496 case BT_SBC_SAMPLING_FREQ_44100:
498 case BT_A2DP_CHANNEL_MODE_MONO:
499 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
501 case BT_A2DP_CHANNEL_MODE_STEREO:
502 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
505 DBG("Invalid channel mode %u", mode);
508 case BT_SBC_SAMPLING_FREQ_48000:
510 case BT_A2DP_CHANNEL_MODE_MONO:
511 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
513 case BT_A2DP_CHANNEL_MODE_STEREO:
514 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
517 DBG("Invalid channel mode %u", mode);
521 DBG("Invalid sampling freq %u", freq);
526 static int bluetooth_a2dp_init(struct bluetooth_data *data,
527 snd_pcm_hw_params_t *params)
529 struct bluetooth_alsa_config *cfg = &data->alsa_config;
530 sbc_capabilities_t *cap = &data->a2dp.sbc_capabilities;
531 unsigned int max_bitpool, min_bitpool, rate, channels;
534 snd_pcm_hw_params_get_rate(params, &rate, &dir);
535 snd_pcm_hw_params_get_channels(params, &channels);
539 cap->frequency = BT_SBC_SAMPLING_FREQ_48000;
542 cap->frequency = BT_SBC_SAMPLING_FREQ_44100;
545 cap->frequency = BT_SBC_SAMPLING_FREQ_32000;
548 cap->frequency = BT_SBC_SAMPLING_FREQ_16000;
551 DBG("Rate %d not supported", rate);
555 if (cfg->has_channel_mode)
556 cap->channel_mode = cfg->channel_mode;
557 else if (channels == 2) {
558 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
559 cap->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
560 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
561 cap->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
562 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
563 cap->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
565 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
566 cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
569 if (!cap->channel_mode) {
570 DBG("No supported channel modes");
574 if (cfg->has_block_length)
575 cap->block_length = cfg->block_length;
576 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
577 cap->block_length = BT_A2DP_BLOCK_LENGTH_16;
578 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
579 cap->block_length = BT_A2DP_BLOCK_LENGTH_12;
580 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
581 cap->block_length = BT_A2DP_BLOCK_LENGTH_8;
582 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
583 cap->block_length = BT_A2DP_BLOCK_LENGTH_4;
585 DBG("No supported block lengths");
589 if (cfg->has_subbands)
590 cap->subbands = cfg->subbands;
591 if (cap->subbands & BT_A2DP_SUBBANDS_8)
592 cap->subbands = BT_A2DP_SUBBANDS_8;
593 else if (cap->subbands & BT_A2DP_SUBBANDS_4)
594 cap->subbands = BT_A2DP_SUBBANDS_4;
596 DBG("No supported subbands");
600 if (cfg->has_allocation_method)
601 cap->allocation_method = cfg->allocation_method;
602 if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
603 cap->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
604 else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
605 cap->allocation_method = BT_A2DP_ALLOCATION_SNR;
607 if (cfg->has_bitpool)
608 min_bitpool = max_bitpool = cfg->bitpool;
610 min_bitpool = MAX(MIN_BITPOOL, cap->min_bitpool);
611 max_bitpool = MIN(default_bitpool(cap->frequency,
616 cap->min_bitpool = min_bitpool;
617 cap->max_bitpool = max_bitpool;
622 static void bluetooth_a2dp_setup(struct bluetooth_a2dp *a2dp)
624 sbc_capabilities_t active_capabilities = a2dp->sbc_capabilities;
626 if (a2dp->sbc_initialized)
627 sbc_reinit(&a2dp->sbc, 0);
629 sbc_init(&a2dp->sbc, 0);
630 a2dp->sbc_initialized = 1;
632 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_16000)
633 a2dp->sbc.frequency = SBC_FREQ_16000;
635 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_32000)
636 a2dp->sbc.frequency = SBC_FREQ_32000;
638 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_44100)
639 a2dp->sbc.frequency = SBC_FREQ_44100;
641 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_48000)
642 a2dp->sbc.frequency = SBC_FREQ_48000;
644 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
645 a2dp->sbc.mode = SBC_MODE_MONO;
647 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
648 a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
650 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
651 a2dp->sbc.mode = SBC_MODE_STEREO;
653 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
654 a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
656 a2dp->sbc.allocation = active_capabilities.allocation_method
657 == BT_A2DP_ALLOCATION_SNR ? SBC_AM_SNR
660 switch (active_capabilities.subbands) {
661 case BT_A2DP_SUBBANDS_4:
662 a2dp->sbc.subbands = SBC_SB_4;
664 case BT_A2DP_SUBBANDS_8:
665 a2dp->sbc.subbands = SBC_SB_8;
669 switch (active_capabilities.block_length) {
670 case BT_A2DP_BLOCK_LENGTH_4:
671 a2dp->sbc.blocks = SBC_BLK_4;
673 case BT_A2DP_BLOCK_LENGTH_8:
674 a2dp->sbc.blocks = SBC_BLK_8;
676 case BT_A2DP_BLOCK_LENGTH_12:
677 a2dp->sbc.blocks = SBC_BLK_12;
679 case BT_A2DP_BLOCK_LENGTH_16:
680 a2dp->sbc.blocks = SBC_BLK_16;
684 a2dp->sbc.bitpool = active_capabilities.max_bitpool;
685 a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
686 a2dp->count = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
689 static int bluetooth_a2dp_hw_params(snd_pcm_ioplug_t *io,
690 snd_pcm_hw_params_t *params)
692 struct bluetooth_data *data = io->private_data;
693 struct bluetooth_a2dp *a2dp = &data->a2dp;
694 char buf[BT_SUGGESTED_BUFFER_SIZE];
695 struct bt_open_req *open_req = (void *) buf;
696 struct bt_open_rsp *open_rsp = (void *) buf;
697 struct bt_set_configuration_req *req = (void *) buf;
698 struct bt_set_configuration_rsp *rsp = (void *) buf;
701 DBG("Preparing with io->period_size=%lu io->buffer_size=%lu",
702 io->period_size, io->buffer_size);
704 memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
705 open_req->h.type = BT_REQUEST;
706 open_req->h.name = BT_OPEN;
707 open_req->h.length = sizeof(*open_req);
709 strncpy(open_req->destination, data->alsa_config.device, 18);
710 open_req->seid = a2dp->sbc_capabilities.capability.seid;
711 open_req->lock = (io->stream == SND_PCM_STREAM_PLAYBACK ?
712 BT_WRITE_LOCK : BT_READ_LOCK);
714 err = audioservice_send(data->server.fd, &open_req->h);
718 open_rsp->h.length = sizeof(*open_rsp);
719 err = audioservice_expect(data->server.fd, &open_rsp->h,
724 err = bluetooth_a2dp_init(data, params);
728 memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
729 req->h.type = BT_REQUEST;
730 req->h.name = BT_SET_CONFIGURATION;
731 req->h.length = sizeof(*req);
733 memcpy(&req->codec, &a2dp->sbc_capabilities,
734 sizeof(a2dp->sbc_capabilities));
736 req->codec.transport = BT_CAPABILITIES_TRANSPORT_A2DP;
737 req->codec.length = sizeof(a2dp->sbc_capabilities);
738 req->h.length += req->codec.length - sizeof(req->codec);
740 err = audioservice_send(data->server.fd, &req->h);
744 rsp->h.length = sizeof(*rsp);
745 err = audioservice_expect(data->server.fd, &rsp->h,
746 BT_SET_CONFIGURATION);
750 data->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
751 data->link_mtu = rsp->link_mtu;
753 /* Setup SBC encoder now we agree on parameters */
754 bluetooth_a2dp_setup(a2dp);
756 DBG("\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
757 a2dp->sbc.allocation, a2dp->sbc.subbands, a2dp->sbc.blocks,
763 static int bluetooth_poll_descriptors(snd_pcm_ioplug_t *io,
764 struct pollfd *pfd, unsigned int space)
766 struct bluetooth_data *data = io->private_data;
773 pfd[0].fd = data->stream.fd;
774 pfd[0].events = POLLIN;
780 static int bluetooth_poll_revents(snd_pcm_ioplug_t *io ATTRIBUTE_UNUSED,
781 struct pollfd *pfds, unsigned int nfds,
782 unsigned short *revents)
784 assert(pfds && nfds == 1 && revents);
786 *revents = pfds[0].revents;
791 static int bluetooth_playback_poll_descriptors_count(snd_pcm_ioplug_t *io)
796 static int bluetooth_playback_poll_descriptors(snd_pcm_ioplug_t *io,
797 struct pollfd *pfd, unsigned int space)
799 struct bluetooth_data *data = io->private_data;
803 assert(data->pipefd[0] >= 0);
808 pfd[0].fd = data->pipefd[0];
809 pfd[0].events = POLLIN;
811 pfd[1].fd = data->stream.fd;
812 pfd[1].events = POLLERR | POLLHUP | POLLNVAL;
818 static int bluetooth_playback_poll_revents(snd_pcm_ioplug_t *io,
819 struct pollfd *pfds, unsigned int nfds,
820 unsigned short *revents)
829 assert(pfds[0].fd >= 0);
830 assert(pfds[1].fd >= 0);
832 if (io->state != SND_PCM_STATE_PREPARED)
833 if (read(pfds[0].fd, buf, 1) < 0)
834 SYSERR("read error: %s (%d)", strerror(errno), errno);
836 if (pfds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
837 io->state = SND_PCM_STATE_DISCONNECTED;
839 *revents = (pfds[0].revents & POLLIN) ? POLLOUT : 0;
845 static snd_pcm_sframes_t bluetooth_hsp_read(snd_pcm_ioplug_t *io,
846 const snd_pcm_channel_area_t *areas,
847 snd_pcm_uframes_t offset,
848 snd_pcm_uframes_t size)
850 struct bluetooth_data *data = io->private_data;
851 snd_pcm_uframes_t frames_to_write, ret;
853 unsigned int frame_size = 0;
856 DBG("areas->step=%u areas->first=%u offset=%lu size=%lu io->nonblock=%u",
857 areas->step, areas->first, offset, size, io->nonblock);
859 frame_size = areas->step / 8;
864 nrecv = recv(data->stream.fd, data->buffer, data->link_mtu,
865 io->nonblock ? MSG_DONTWAIT : 0);
868 ret = (errno == EPIPE) ? -EIO : -errno;
872 if ((unsigned int) nrecv != data->link_mtu) {
874 SNDERR(strerror(-ret));
878 /* Increment hardware transmition pointer */
879 data->hw_ptr = (data->hw_ptr + data->link_mtu / frame_size) %
883 buff = (unsigned char *) areas->addr +
884 (areas->first + areas->step * offset) / 8;
886 if ((data->count + size * frame_size) <= data->link_mtu)
887 frames_to_write = size;
889 frames_to_write = (data->link_mtu - data->count) / frame_size;
891 memcpy(buff, data->buffer + data->count, frame_size * frames_to_write);
892 data->count += (frame_size * frames_to_write);
893 data->count %= data->link_mtu;
895 /* Return written frames count */
896 ret = frames_to_write;
899 DBG("returning %lu", ret);
903 static snd_pcm_sframes_t bluetooth_hsp_write(snd_pcm_ioplug_t *io,
904 const snd_pcm_channel_area_t *areas,
905 snd_pcm_uframes_t offset,
906 snd_pcm_uframes_t size)
908 struct bluetooth_data *data = io->private_data;
909 snd_pcm_sframes_t ret = 0;
910 snd_pcm_uframes_t frames_to_read;
912 int rsend, frame_size;
914 DBG("areas->step=%u areas->first=%u offset=%lu, size=%lu io->nonblock=%u",
915 areas->step, areas->first, offset, size, io->nonblock);
917 if (io->hw_ptr > io->appl_ptr) {
918 ret = bluetooth_playback_stop(io);
924 frame_size = areas->step / 8;
925 if ((data->count + size * frame_size) <= data->link_mtu)
926 frames_to_read = size;
928 frames_to_read = (data->link_mtu - data->count) / frame_size;
930 DBG("count=%d frames_to_read=%lu", data->count, frames_to_read);
932 /* Ready for more data */
933 buff = (uint8_t *) areas->addr +
934 (areas->first + areas->step * offset) / 8;
935 memcpy(data->buffer + data->count, buff, frame_size * frames_to_read);
937 /* Remember we have some frames in the pipe now */
938 data->count += frames_to_read * frame_size;
939 if (data->count != data->link_mtu) {
940 ret = frames_to_read;
944 rsend = send(data->stream.fd, data->buffer, data->link_mtu,
945 io->nonblock ? MSG_DONTWAIT : 0);
947 /* Reset count pointer */
950 ret = frames_to_read;
951 } else if (rsend < 0)
952 ret = (errno == EPIPE) ? -EIO : -errno;
957 DBG("returning %ld", ret);
961 static snd_pcm_sframes_t bluetooth_a2dp_read(snd_pcm_ioplug_t *io,
962 const snd_pcm_channel_area_t *areas,
963 snd_pcm_uframes_t offset, snd_pcm_uframes_t size)
965 snd_pcm_uframes_t ret = 0;
969 static int avdtp_write(struct bluetooth_data *data)
972 struct rtp_header *header;
973 struct rtp_payload *payload;
974 struct bluetooth_a2dp *a2dp = &data->a2dp;
976 header = (void *) a2dp->buffer;
977 payload = (void *) (a2dp->buffer + sizeof(*header));
979 memset(a2dp->buffer, 0, sizeof(*header) + sizeof(*payload));
981 payload->frame_count = a2dp->frame_count;
984 header->sequence_number = htons(a2dp->seq_num);
985 header->timestamp = htonl(a2dp->nsamples);
986 header->ssrc = htonl(1);
988 ret = send(data->stream.fd, a2dp->buffer, a2dp->count, MSG_DONTWAIT);
990 DBG("send returned %d errno %s.", ret, strerror(errno));
994 /* Reset buffer of data to send */
995 a2dp->count = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
996 a2dp->frame_count = 0;
1003 static snd_pcm_sframes_t bluetooth_a2dp_write(snd_pcm_ioplug_t *io,
1004 const snd_pcm_channel_area_t *areas,
1005 snd_pcm_uframes_t offset, snd_pcm_uframes_t size)
1007 struct bluetooth_data *data = io->private_data;
1008 struct bluetooth_a2dp *a2dp = &data->a2dp;
1009 snd_pcm_sframes_t ret = 0;
1010 unsigned int bytes_left;
1011 int frame_size, encoded;
1015 DBG("areas->step=%u areas->first=%u offset=%lu size=%lu",
1016 areas->step, areas->first, offset, size);
1017 DBG("hw_ptr=%lu appl_ptr=%lu diff=%lu", io->hw_ptr, io->appl_ptr,
1018 io->appl_ptr - io->hw_ptr);
1020 /* Calutate starting pointers */
1021 frame_size = areas->step / 8;
1022 bytes_left = size * frame_size;
1023 buff = (uint8_t *) areas->addr +
1024 (areas->first + areas->step * (offset)) / 8;
1026 /* Check for underrun */
1027 if (io->hw_ptr > io->appl_ptr) {
1028 ret = bluetooth_playback_stop(io);
1035 /* Check if we should autostart */
1036 if (io->state == SND_PCM_STATE_PREPARED) {
1037 snd_pcm_sw_params_t *swparams;
1038 snd_pcm_uframes_t threshold;
1040 snd_pcm_sw_params_malloc(&swparams);
1041 if (!snd_pcm_sw_params_current(io->pcm, swparams) &&
1042 !snd_pcm_sw_params_get_start_threshold(swparams,
1044 if (io->appl_ptr >= threshold) {
1045 ret = snd_pcm_start(io->pcm);
1051 snd_pcm_sw_params_free(swparams);
1054 /* Check if we have any left over data from the last write */
1055 if (data->count > 0) {
1056 unsigned int additional_bytes_needed =
1057 a2dp->codesize - data->count;
1058 if (additional_bytes_needed > bytes_left)
1061 memcpy(data->buffer + data->count, buff,
1062 additional_bytes_needed);
1064 /* Enough data to encode (sbc wants 1k blocks) */
1065 encoded = sbc_encode(&a2dp->sbc, data->buffer, a2dp->codesize,
1066 a2dp->buffer + a2dp->count,
1067 sizeof(a2dp->buffer) - a2dp->count,
1070 DBG("Encoding error %d", encoded);
1074 /* Increment a2dp buffers */
1075 a2dp->count += written;
1076 a2dp->frame_count++;
1077 a2dp->samples += encoded / frame_size;
1078 a2dp->nsamples += encoded / frame_size;
1080 /* No space left for another frame then send */
1081 if (a2dp->count + written >= data->link_mtu) {
1083 DBG("sending packet %d, count %d, link_mtu %u",
1084 a2dp->seq_num, a2dp->count,
1088 /* Increment up buff pointer to take into account
1089 * the data processed */
1090 buff += additional_bytes_needed;
1091 bytes_left -= additional_bytes_needed;
1093 /* Since data has been process mark it as zero */
1098 /* Process this buffer in full chunks */
1099 while (bytes_left >= a2dp->codesize) {
1100 /* Enough data to encode (sbc wants 1k blocks) */
1101 encoded = sbc_encode(&a2dp->sbc, buff, a2dp->codesize,
1102 a2dp->buffer + a2dp->count,
1103 sizeof(a2dp->buffer) - a2dp->count,
1106 DBG("Encoding error %d", encoded);
1110 /* Increment up buff pointer to take into account
1111 * the data processed */
1112 buff += a2dp->codesize;
1113 bytes_left -= a2dp->codesize;
1115 /* Increment a2dp buffers */
1116 a2dp->count += written;
1117 a2dp->frame_count++;
1118 a2dp->samples += encoded / frame_size;
1119 a2dp->nsamples += encoded / frame_size;
1121 /* No space left for another frame then send */
1122 if (a2dp->count + written >= data->link_mtu) {
1124 DBG("sending packet %d, count %d, link_mtu %u",
1125 a2dp->seq_num, a2dp->count,
1131 /* Copy the extra to our temp buffer for the next write */
1132 if (bytes_left > 0) {
1133 memcpy(data->buffer + data->count, buff, bytes_left);
1134 data->count += bytes_left;
1139 DBG("returning %ld", size - bytes_left / frame_size);
1141 return size - bytes_left / frame_size;
1144 static int bluetooth_playback_delay(snd_pcm_ioplug_t *io,
1145 snd_pcm_sframes_t *delayp)
1149 /* This updates io->hw_ptr value using pointer() function */
1150 snd_pcm_hwsync(io->pcm);
1152 *delayp = io->appl_ptr - io->hw_ptr;
1153 if ((io->state == SND_PCM_STATE_RUNNING) && (*delayp < 0)) {
1154 io->callback->stop(io);
1155 io->state = SND_PCM_STATE_XRUN;
1159 /* This should never fail, ALSA API is really not
1160 prepared to handle a non zero return value */
1164 static snd_pcm_ioplug_callback_t bluetooth_hsp_playback = {
1165 .start = bluetooth_playback_start,
1166 .stop = bluetooth_playback_stop,
1167 .pointer = bluetooth_pointer,
1168 .close = bluetooth_close,
1169 .hw_params = bluetooth_hsp_hw_params,
1170 .prepare = bluetooth_prepare,
1171 .transfer = bluetooth_hsp_write,
1172 .poll_descriptors_count = bluetooth_playback_poll_descriptors_count,
1173 .poll_descriptors = bluetooth_playback_poll_descriptors,
1174 .poll_revents = bluetooth_playback_poll_revents,
1175 .delay = bluetooth_playback_delay,
1178 static snd_pcm_ioplug_callback_t bluetooth_hsp_capture = {
1179 .start = bluetooth_start,
1180 .stop = bluetooth_stop,
1181 .pointer = bluetooth_pointer,
1182 .close = bluetooth_close,
1183 .hw_params = bluetooth_hsp_hw_params,
1184 .prepare = bluetooth_prepare,
1185 .transfer = bluetooth_hsp_read,
1186 .poll_descriptors = bluetooth_poll_descriptors,
1187 .poll_revents = bluetooth_poll_revents,
1190 static snd_pcm_ioplug_callback_t bluetooth_a2dp_playback = {
1191 .start = bluetooth_playback_start,
1192 .stop = bluetooth_playback_stop,
1193 .pointer = bluetooth_pointer,
1194 .close = bluetooth_close,
1195 .hw_params = bluetooth_a2dp_hw_params,
1196 .prepare = bluetooth_prepare,
1197 .transfer = bluetooth_a2dp_write,
1198 .poll_descriptors_count = bluetooth_playback_poll_descriptors_count,
1199 .poll_descriptors = bluetooth_playback_poll_descriptors,
1200 .poll_revents = bluetooth_playback_poll_revents,
1201 .delay = bluetooth_playback_delay,
1204 static snd_pcm_ioplug_callback_t bluetooth_a2dp_capture = {
1205 .start = bluetooth_start,
1206 .stop = bluetooth_stop,
1207 .pointer = bluetooth_pointer,
1208 .close = bluetooth_close,
1209 .hw_params = bluetooth_a2dp_hw_params,
1210 .prepare = bluetooth_prepare,
1211 .transfer = bluetooth_a2dp_read,
1212 .poll_descriptors = bluetooth_poll_descriptors,
1213 .poll_revents = bluetooth_poll_revents,
1216 #define ARRAY_NELEMS(a) (sizeof((a)) / sizeof((a)[0]))
1218 static int bluetooth_hsp_hw_constraint(snd_pcm_ioplug_t *io)
1220 struct bluetooth_data *data = io->private_data;
1221 snd_pcm_access_t access_list[] = {
1222 SND_PCM_ACCESS_RW_INTERLEAVED,
1223 /* Mmap access is really useless fo this driver, but we
1224 * support it because some pieces of software out there
1225 * insist on using it */
1226 SND_PCM_ACCESS_MMAP_INTERLEAVED
1228 unsigned int format_list[] = {
1234 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
1235 ARRAY_NELEMS(access_list), access_list);
1239 /* supported formats */
1240 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
1241 ARRAY_NELEMS(format_list), format_list);
1245 /* supported channels */
1246 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
1251 /* supported rate */
1252 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE,
1257 /* supported block size */
1258 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
1259 data->link_mtu, data->link_mtu);
1263 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS,
1271 static int bluetooth_a2dp_hw_constraint(snd_pcm_ioplug_t *io)
1273 struct bluetooth_data *data = io->private_data;
1274 struct bluetooth_a2dp *a2dp = &data->a2dp;
1275 struct bluetooth_alsa_config *cfg = &data->alsa_config;
1276 snd_pcm_access_t access_list[] = {
1277 SND_PCM_ACCESS_RW_INTERLEAVED,
1278 /* Mmap access is really useless fo this driver, but we
1279 * support it because some pieces of software out there
1280 * insist on using it */
1281 SND_PCM_ACCESS_MMAP_INTERLEAVED
1283 unsigned int format_list[] = {
1286 unsigned int rate_list[4];
1287 unsigned int rate_count;
1288 int err, min_channels, max_channels;
1289 unsigned int period_list[] = {
1291 4096, /* e.g. 23.2msec/period (stereo 16bit at 44.1kHz) */
1296 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
1297 ARRAY_NELEMS(access_list), access_list);
1301 /* supported formats */
1302 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
1303 ARRAY_NELEMS(format_list), format_list);
1307 /* supported channels */
1308 if (cfg->has_channel_mode)
1309 a2dp->sbc_capabilities.channel_mode = cfg->channel_mode;
1311 if (a2dp->sbc_capabilities.channel_mode &
1312 BT_A2DP_CHANNEL_MODE_MONO)
1317 if (a2dp->sbc_capabilities.channel_mode &
1318 (~BT_A2DP_CHANNEL_MODE_MONO))
1323 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
1324 min_channels, max_channels);
1328 /* supported buffer sizes
1329 * (can be used as 3*8192, 6*4096, 12*2048, ...) */
1330 err = snd_pcm_ioplug_set_param_minmax(io,
1331 SND_PCM_IOPLUG_HW_BUFFER_BYTES,
1336 /* supported block sizes: */
1337 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
1338 ARRAY_NELEMS(period_list), period_list);
1342 /* supported rates */
1344 if (cfg->has_rate) {
1345 rate_list[rate_count] = cfg->rate;
1348 if (a2dp->sbc_capabilities.frequency &
1349 BT_SBC_SAMPLING_FREQ_16000) {
1350 rate_list[rate_count] = 16000;
1354 if (a2dp->sbc_capabilities.frequency &
1355 BT_SBC_SAMPLING_FREQ_32000) {
1356 rate_list[rate_count] = 32000;
1360 if (a2dp->sbc_capabilities.frequency &
1361 BT_SBC_SAMPLING_FREQ_44100) {
1362 rate_list[rate_count] = 44100;
1366 if (a2dp->sbc_capabilities.frequency &
1367 BT_SBC_SAMPLING_FREQ_48000) {
1368 rate_list[rate_count] = 48000;
1373 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_RATE,
1374 rate_count, rate_list);
1381 static int bluetooth_parse_config(snd_config_t *conf,
1382 struct bluetooth_alsa_config *bt_config)
1384 snd_config_iterator_t i, next;
1386 memset(bt_config, 0, sizeof(struct bluetooth_alsa_config));
1389 bt_config->autoconnect = 1;
1391 snd_config_for_each(i, next, conf) {
1392 snd_config_t *n = snd_config_iterator_entry(i);
1393 const char *id, *value;
1395 if (snd_config_get_id(n, &id) < 0)
1398 if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0)
1401 if (strcmp(id, "autoconnect") == 0) {
1404 b = snd_config_get_bool(n);
1406 SNDERR("Invalid type for %s", id);
1410 bt_config->autoconnect = b;
1414 if (strcmp(id, "device") == 0 || strcmp(id, "bdaddr") == 0) {
1415 if (snd_config_get_string(n, &value) < 0) {
1416 SNDERR("Invalid type for %s", id);
1420 bt_config->has_device = 1;
1421 strncpy(bt_config->device, value, 18);
1425 if (strcmp(id, "profile") == 0) {
1426 if (snd_config_get_string(n, &value) < 0) {
1427 SNDERR("Invalid type for %s", id);
1431 if (strcmp(value, "auto") == 0) {
1432 bt_config->transport = BT_CAPABILITIES_TRANSPORT_ANY;
1433 bt_config->has_transport = 1;
1434 } else if (strcmp(value, "voice") == 0 ||
1435 strcmp(value, "hfp") == 0) {
1436 bt_config->transport = BT_CAPABILITIES_TRANSPORT_SCO;
1437 bt_config->has_transport = 1;
1438 } else if (strcmp(value, "hifi") == 0 ||
1439 strcmp(value, "a2dp") == 0) {
1440 bt_config->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
1441 bt_config->has_transport = 1;
1446 if (strcmp(id, "rate") == 0) {
1447 if (snd_config_get_string(n, &value) < 0) {
1448 SNDERR("Invalid type for %s", id);
1452 bt_config->rate = atoi(value);
1453 bt_config->has_rate = 1;
1457 if (strcmp(id, "mode") == 0) {
1458 if (snd_config_get_string(n, &value) < 0) {
1459 SNDERR("Invalid type for %s", id);
1463 if (strcmp(value, "mono") == 0) {
1464 bt_config->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
1465 bt_config->has_channel_mode = 1;
1466 } else if (strcmp(value, "dual") == 0) {
1467 bt_config->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
1468 bt_config->has_channel_mode = 1;
1469 } else if (strcmp(value, "stereo") == 0) {
1470 bt_config->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
1471 bt_config->has_channel_mode = 1;
1472 } else if (strcmp(value, "joint") == 0) {
1473 bt_config->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
1474 bt_config->has_channel_mode = 1;
1479 if (strcmp(id, "allocation") == 0) {
1480 if (snd_config_get_string(n, &value) < 0) {
1481 SNDERR("Invalid type for %s", id);
1485 if (strcmp(value, "loudness") == 0) {
1486 bt_config->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
1487 bt_config->has_allocation_method = 1;
1488 } else if (strcmp(value, "snr") == 0) {
1489 bt_config->allocation_method = BT_A2DP_ALLOCATION_SNR;
1490 bt_config->has_allocation_method = 1;
1495 if (strcmp(id, "subbands") == 0) {
1496 if (snd_config_get_string(n, &value) < 0) {
1497 SNDERR("Invalid type for %s", id);
1501 bt_config->subbands = atoi(value);
1502 bt_config->has_subbands = 1;
1506 if (strcmp(id, "blocks") == 0) {
1507 if (snd_config_get_string(n, &value) < 0) {
1508 SNDERR("Invalid type for %s", id);
1512 bt_config->block_length = atoi(value);
1513 bt_config->has_block_length = 1;
1517 if (strcmp(id, "bitpool") == 0) {
1518 if (snd_config_get_string(n, &value) < 0) {
1519 SNDERR("Invalid type for %s", id);
1523 bt_config->bitpool = atoi(value);
1524 bt_config->has_bitpool = 1;
1528 SNDERR("Unknown field %s", id);
1535 static int audioservice_send(int sk, const bt_audio_msg_header_t *msg)
1540 length = msg->length ? msg->length : BT_SUGGESTED_BUFFER_SIZE;
1542 DBG("sending %s:%s", bt_audio_strtype(msg->type),
1543 bt_audio_strname(msg->name));
1544 if (send(sk, msg, length, 0) > 0)
1548 SNDERR("Error sending data to audio service: %s(%d)",
1549 strerror(errno), errno);
1555 static int audioservice_recv(int sk, bt_audio_msg_header_t *inmsg)
1559 const char *type, *name;
1562 length = inmsg->length ? inmsg->length : BT_SUGGESTED_BUFFER_SIZE;
1564 DBG("trying to receive msg from audio service...");
1566 ret = recv(sk, inmsg, length, 0);
1569 SNDERR("Error receiving IPC data from bluetoothd: %s (%d)",
1570 strerror(errno), errno);
1571 } else if ((size_t) ret < sizeof(bt_audio_msg_header_t)) {
1572 SNDERR("Too short (%d bytes) IPC packet from bluetoothd", ret);
1575 type = bt_audio_strtype(inmsg->type);
1576 name = bt_audio_strname(inmsg->name);
1578 DBG("Received %s - %s", type, name);
1582 SNDERR("Bogus message type %d - name %d"
1583 " received from audio service",
1584 inmsg->type, inmsg->name);
1592 static int audioservice_expect(int sk, bt_audio_msg_header_t *rsp,
1595 bt_audio_error_t *error;
1596 int err = audioservice_recv(sk, rsp);
1601 if (rsp->name != expected_name) {
1603 SNDERR("Bogus message %s received while %s was expected",
1604 bt_audio_strname(rsp->name),
1605 bt_audio_strname(expected_name));
1608 if (rsp->type == BT_ERROR) {
1609 error = (void *) rsp;
1610 SNDERR("%s failed : %s(%d)",
1611 bt_audio_strname(rsp->name),
1612 strerror(error->posix_errno),
1613 error->posix_errno);
1614 return -error->posix_errno;
1620 static int bluetooth_parse_capabilities(struct bluetooth_data *data,
1621 struct bt_get_capabilities_rsp *rsp)
1623 int bytes_left = rsp->h.length - sizeof(*rsp);
1624 codec_capabilities_t *codec = (void *) rsp->data;
1626 data->transport = codec->transport;
1628 if (codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP)
1631 while (bytes_left > 0) {
1632 if ((codec->type == BT_A2DP_SBC_SINK) &&
1633 !(codec->lock & BT_WRITE_LOCK))
1636 bytes_left -= codec->length;
1637 codec = (void *) codec + codec->length;
1640 if (bytes_left <= 0 ||
1641 codec->length != sizeof(data->a2dp.sbc_capabilities))
1644 memcpy(&data->a2dp.sbc_capabilities, codec, codec->length);
1649 static int bluetooth_init(struct bluetooth_data *data,
1650 snd_pcm_stream_t stream, snd_config_t *conf)
1653 struct bluetooth_alsa_config *alsa_conf = &data->alsa_config;
1654 char buf[BT_SUGGESTED_BUFFER_SIZE];
1655 struct bt_get_capabilities_req *req = (void *) buf;
1656 struct bt_get_capabilities_rsp *rsp = (void *) buf;
1658 memset(data, 0, sizeof(struct bluetooth_data));
1660 err = bluetooth_parse_config(conf, alsa_conf);
1664 data->server.fd = -1;
1665 data->stream.fd = -1;
1667 sk = bt_audio_service_open();
1673 data->server.fd = sk;
1674 data->server.events = POLLIN;
1676 data->pipefd[0] = -1;
1677 data->pipefd[1] = -1;
1679 if (pipe(data->pipefd) < 0) {
1683 if (fcntl(data->pipefd[0], F_SETFL, O_NONBLOCK) < 0) {
1687 if (fcntl(data->pipefd[1], F_SETFL, O_NONBLOCK) < 0) {
1692 memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
1693 req->h.type = BT_REQUEST;
1694 req->h.name = BT_GET_CAPABILITIES;
1695 req->h.length = sizeof(*req);
1697 if (alsa_conf->autoconnect)
1698 req->flags |= BT_FLAG_AUTOCONNECT;
1699 strncpy(req->destination, alsa_conf->device, 18);
1700 if (alsa_conf->has_transport)
1701 req->transport = alsa_conf->transport;
1703 req->transport = BT_CAPABILITIES_TRANSPORT_ANY;
1705 err = audioservice_send(data->server.fd, &req->h);
1710 err = audioservice_expect(data->server.fd, &rsp->h,
1711 BT_GET_CAPABILITIES);
1715 bluetooth_parse_capabilities(data, rsp);
1721 bt_audio_service_close(sk);
1725 SND_PCM_PLUGIN_DEFINE_FUNC(bluetooth);
1727 SND_PCM_PLUGIN_DEFINE_FUNC(bluetooth)
1729 struct bluetooth_data *data;
1732 DBG("Bluetooth PCM plugin (%s)",
1733 stream == SND_PCM_STREAM_PLAYBACK ? "Playback" : "Capture");
1735 data = malloc(sizeof(struct bluetooth_data));
1741 err = bluetooth_init(data, stream, conf);
1745 data->io.version = SND_PCM_IOPLUG_VERSION;
1746 data->io.name = "Bluetooth Audio Device";
1747 data->io.mmap_rw = 0; /* No direct mmap communication */
1748 data->io.private_data = data;
1750 if (data->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
1751 data->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
1752 &bluetooth_a2dp_playback :
1753 &bluetooth_a2dp_capture;
1755 data->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
1756 &bluetooth_hsp_playback :
1757 &bluetooth_hsp_capture;
1759 err = snd_pcm_ioplug_create(&data->io, name, stream, mode);
1763 if (data->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
1764 err = bluetooth_a2dp_hw_constraint(&data->io);
1766 err = bluetooth_hsp_hw_constraint(&data->io);
1769 snd_pcm_ioplug_delete(&data->io);
1773 *pcmp = data->io.pcm;
1779 bluetooth_exit(data);
1784 SND_PCM_PLUGIN_SYMBOL(bluetooth);