1/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#include <syslog.h>
7
8#include "cras_bt_io.h"
9#include "cras_bt_device.h"
10#include "cras_utf8.h"
11#include "cras_iodev.h"
12#include "cras_iodev_list.h"
13#include "sfh.h"
14#include "utlist.h"
15
16#define DEFAULT_BT_DEVICE_NAME "BLUETOOTH"
17
18/* Extends cras_ionode to hold bluetooth profile information
19 * so that iodevs of different profile(A2DP or HFP/HSP) can be
20 * associated with the same bt_io.
21 * Members:
22 *    base - The base class cras_ionode.
23 *    profile_dev - Pointer to the profile specific iodev.
24 *    profile - The bluetooth profile profile_dev runs on.
25 */
26struct bt_node {
27	struct cras_ionode base;
28	struct cras_iodev *profile_dev;
29	unsigned int profile;
30};
31
32/* The structure represents a virtual input or output device of a
33 * bluetooth audio device, speaker or headset for example. A node
34 * will be added to this virtual iodev for each profile supported
35 * by the bluetooth audio device.
36 * Member:
37 *    base - The base class cras_iodev
38 *    next_node_id - The index will give to the next node
39 */
40struct bt_io {
41	struct cras_iodev base;
42	unsigned int next_node_id;
43	struct cras_bt_device *device;
44};
45
46/* Returns the active profile specific iodev. */
47static struct cras_iodev *active_profile_dev(const struct cras_iodev *iodev)
48{
49	struct bt_node *active = (struct bt_node *)iodev->active_node;
50
51	return active->profile_dev;
52}
53
54/* Adds a profile specific iodev to btio. */
55static struct cras_ionode *add_profile_dev(struct cras_iodev *bt_iodev,
56					   struct cras_iodev *dev,
57					   enum cras_bt_device_profile profile)
58{
59	struct bt_node *n;
60	struct bt_io *btio = (struct bt_io *)bt_iodev;
61
62	n = (struct bt_node *)calloc(1, sizeof(*n));
63	if (!n)
64		return NULL;
65
66	n->base.dev = bt_iodev;
67	n->base.idx = btio->next_node_id++;
68	n->base.type = CRAS_NODE_TYPE_BLUETOOTH;
69	n->base.volume = 100;
70	n->base.stable_id = dev->info.stable_id;
71	n->base.stable_id_new = dev->info.stable_id_new;
72	n->base.max_software_gain = 0;
73	gettimeofday(&n->base.plugged_time, NULL);
74
75	strcpy(n->base.name, dev->info.name);
76	n->profile_dev = dev;
77	n->profile = profile;
78
79	cras_iodev_add_node(bt_iodev, &n->base);
80	return &n->base;
81}
82
83/* Forces bt device to switch to use the given profile. Note that if
84 * it has already been open for streaming, the new active profile will
85 * take effect after the related btio(s) are reopened.
86 */
87static void bt_switch_to_profile(struct cras_bt_device *device,
88				 enum cras_bt_device_profile profile)
89{
90	switch (profile) {
91	case CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY:
92	case CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY:
93		cras_bt_device_set_active_profile(device,
94				CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY |
95				CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY);
96		break;
97	case CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE:
98		cras_bt_device_set_active_profile(device,
99				CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
100		break;
101	default:
102		syslog(LOG_ERR, "Unexpect profile %u", profile);
103		break;
104	}
105}
106
107/* Checks if bt device is active for the given profile.
108 */
109static int device_using_profile(struct cras_bt_device *device,
110				unsigned int profile)
111{
112	return cras_bt_device_get_active_profile(device) & profile;
113}
114
115/* Checks if the condition is met to switch to a different profile
116 * when trying to set the format to btio before open it. Base on two
117 * rules:
118 * (1) Prefer to use A2DP for output since the audio quality is better.
119 * (2) Must use HFP/HSP for input since A2DP doesn't support audio input.
120 *
121 * If the profile switch happens, return non-zero error code, otherwise
122 * return zero.
123 */
124static int update_supported_formats(struct cras_iodev *iodev)
125{
126	struct bt_io *btio = (struct bt_io *)iodev;
127	struct cras_iodev *dev = active_profile_dev(iodev);
128	int rc, length, i;
129
130	/* Force to use HFP if opening input dev. */
131	if (device_using_profile(btio->device,
132				 CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE) &&
133	    iodev->direction == CRAS_STREAM_INPUT) {
134		bt_switch_to_profile(btio->device,
135				     CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY);
136		cras_bt_device_switch_profile_enable_dev(btio->device, iodev);
137		return -EAGAIN;
138	}
139
140	if (dev->format == NULL) {
141		dev->format = (struct cras_audio_format *)
142				malloc(sizeof(*dev->format));
143		*dev->format = *iodev->format;
144	}
145
146	if (dev->update_supported_formats) {
147		rc = dev->update_supported_formats(dev);
148		if (rc)
149			return rc;
150	}
151
152	/* Fill in the supported rates and channel counts. */
153	for (length = 0; dev->supported_rates[length]; length++);
154	free(iodev->supported_rates);
155	iodev->supported_rates = (size_t *)malloc(
156			(length + 1) * sizeof(*iodev->supported_rates));
157	for (i = 0; i < length + 1; i++)
158		iodev->supported_rates[i] = dev->supported_rates[i];
159
160	for (length = 0; dev->supported_channel_counts[length]; length++);
161	iodev->supported_channel_counts = (size_t *)malloc(
162		(length + 1) * sizeof(*iodev->supported_channel_counts));
163	for (i = 0; i < length + 1; i++)
164		iodev->supported_channel_counts[i] =
165			dev->supported_channel_counts[i];
166
167	for (length = 0; dev->supported_formats[length]; length++);
168	iodev->supported_formats = (snd_pcm_format_t *)malloc(
169		(length + 1) * sizeof(*iodev->supported_formats));
170	for (i = 0; i < length + 1; i++)
171		iodev->supported_formats[i] =
172			dev->supported_formats[i];
173	return 0;
174}
175
176static int open_dev(struct cras_iodev *iodev)
177{
178	int rc;
179	struct cras_iodev *dev = active_profile_dev(iodev);
180	if (!dev)
181		return -EINVAL;
182
183	/* Fill back the format iodev is using. */
184	*dev->format = *iodev->format;
185
186	rc = dev->open_dev(dev);
187	if (rc) {
188		/* Free format here to assure the update_supported_format
189		 * callback will be called before any future open_dev call. */
190		cras_iodev_free_format(iodev);
191		return rc;
192	}
193
194	iodev->buffer_size = dev->buffer_size;
195	iodev->min_buffer_level = dev->min_buffer_level;
196	return 0;
197}
198
199static int close_dev(struct cras_iodev *iodev)
200{
201	struct bt_io *btio = (struct bt_io *)iodev;
202	int rc;
203	struct cras_iodev *dev = active_profile_dev(iodev);
204
205	/* Force back to A2DP if closing HFP. */
206	if (device_using_profile(btio->device,
207			CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY |
208			CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY) &&
209	    iodev->direction == CRAS_STREAM_INPUT &&
210	    cras_bt_device_has_a2dp(btio->device)) {
211		cras_bt_device_set_active_profile(btio->device,
212				CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
213		cras_bt_device_switch_profile(btio->device, iodev);
214	}
215
216	rc = dev->close_dev(dev);
217	if (rc < 0)
218		return rc;
219	cras_iodev_free_format(iodev);
220	return 0;
221}
222
223static void set_bt_volume(struct cras_iodev *iodev)
224{
225	struct cras_iodev *dev = active_profile_dev(iodev);
226
227	if (dev->active_node)
228		dev->active_node->volume = iodev->active_node->volume;
229
230	/* The parent bt_iodev could set software_volume_needed flag for cases
231	 * that software volume provides better experience across profiles
232	 * (HFP and A2DP). Otherwise, use the profile specific implementation
233	 * to adjust volume. */
234	if (dev->set_volume && !iodev->software_volume_needed)
235		dev->set_volume(dev);
236}
237
238static int frames_queued(const struct cras_iodev *iodev,
239			 struct timespec *tstamp)
240{
241	struct cras_iodev *dev = active_profile_dev(iodev);
242	if (!dev)
243		return -EINVAL;
244	return dev->frames_queued(dev, tstamp);
245}
246
247static int delay_frames(const struct cras_iodev *iodev)
248{
249	struct cras_iodev *dev = active_profile_dev(iodev);
250	if (!dev)
251		return -EINVAL;
252	return dev->delay_frames(dev);
253}
254
255static int get_buffer(struct cras_iodev *iodev,
256		      struct cras_audio_area **area,
257		      unsigned *frames)
258{
259	struct cras_iodev *dev = active_profile_dev(iodev);
260	if (!dev)
261		return -EINVAL;
262	return dev->get_buffer(dev, area, frames);
263}
264
265static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
266{
267	struct cras_iodev *dev = active_profile_dev(iodev);
268	if (!dev)
269		return -EINVAL;
270	return dev->put_buffer(dev, nwritten);
271}
272
273static int flush_buffer(struct cras_iodev *iodev)
274{
275	struct cras_iodev *dev = active_profile_dev(iodev);
276	if (!dev)
277		return -EINVAL;
278	return dev->flush_buffer(dev);
279}
280
281/* If the first private iodev doesn't match the active profile stored on
282 * device, select to the correct private iodev.
283 */
284static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
285			       unsigned dev_enabled)
286{
287	struct bt_io *btio = (struct bt_io *)iodev;
288	struct cras_ionode *node;
289	struct bt_node *active = (struct bt_node *)iodev->active_node;
290
291	if (device_using_profile(btio->device, active->profile))
292		return;
293
294	/* Switch to the correct dev using active_profile. */
295	DL_FOREACH(iodev->nodes, node) {
296		struct bt_node *n = (struct bt_node *)node;
297		if (n == active)
298			continue;
299
300		if (device_using_profile(btio->device, n->profile)) {
301			active->profile = n->profile;
302			active->profile_dev = n->profile_dev;
303
304			/* Set volume for the new profile. */
305			set_bt_volume(iodev);
306		}
307	}
308}
309
310struct cras_iodev *cras_bt_io_create(struct cras_bt_device *device,
311				     struct cras_iodev *dev,
312				     enum cras_bt_device_profile profile)
313{
314	int err;
315	struct bt_io *btio;
316	struct cras_iodev *iodev;
317	struct cras_ionode *node;
318	struct bt_node *active;
319
320	if (!dev)
321		return NULL;
322
323	btio = (struct bt_io *)calloc(1, sizeof(*btio));
324	if (!btio)
325		goto error;
326	btio->device = device;
327
328	iodev = &btio->base;
329	iodev->direction = dev->direction;
330	strcpy(iodev->info.name, dev->info.name);
331	iodev->info.stable_id = dev->info.stable_id;
332	iodev->info.stable_id_new = dev->info.stable_id_new;
333
334	iodev->open_dev = open_dev;
335	iodev->frames_queued = frames_queued;
336	iodev->delay_frames = delay_frames;
337	iodev->get_buffer = get_buffer;
338	iodev->put_buffer = put_buffer;
339	iodev->flush_buffer = flush_buffer;
340	iodev->close_dev = close_dev;
341	iodev->update_supported_formats = update_supported_formats;
342	iodev->update_active_node = update_active_node;
343	iodev->software_volume_needed = 1;
344	iodev->set_volume = set_bt_volume;
345	iodev->no_stream = cras_iodev_default_no_stream_playback;
346
347	/* Create the dummy node set to plugged so it's the only node exposed
348	 * to UI, and point it to the first profile dev. */
349	active = (struct bt_node *)calloc(1, sizeof(*active));
350	if (!active)
351		return NULL;
352	active->base.dev = iodev;
353	active->base.idx = btio->next_node_id++;
354	active->base.type = CRAS_NODE_TYPE_BLUETOOTH;
355	active->base.volume = 100;
356	active->base.plugged = 1;
357	active->base.stable_id = SuperFastHash(
358		cras_bt_device_object_path(device),
359		strlen(cras_bt_device_object_path(device)),
360		strlen(cras_bt_device_object_path(device)));
361	active->base.stable_id_new = active->base.stable_id;
362	active->profile = profile;
363	active->profile_dev = dev;
364	gettimeofday(&active->base.plugged_time, NULL);
365	strcpy(active->base.name, dev->info.name);
366	/* The node name exposed to UI should be a valid UTF8 string. */
367	if (!is_utf8_string(active->base.name))
368		strcpy(active->base.name, DEFAULT_BT_DEVICE_NAME);
369	cras_iodev_add_node(iodev, &active->base);
370
371	node = add_profile_dev(&btio->base, dev, profile);
372	if (node == NULL)
373		goto error;
374
375	/* Default active profile to a2dp whenever it's allowed. */
376	if (!cras_bt_device_get_active_profile(device) ||
377	    (profile == CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE &&
378	     cras_bt_device_can_switch_to_a2dp(device)))
379		bt_switch_to_profile(device, profile);
380
381	if (iodev->direction == CRAS_STREAM_OUTPUT)
382		err = cras_iodev_list_add_output(iodev);
383	else
384		err = cras_iodev_list_add_input(iodev);
385	if (err)
386		goto error;
387
388	cras_iodev_set_active_node(iodev, &active->base);
389	return &btio->base;
390
391error:
392	if (btio)
393		free(btio);
394	return NULL;
395}
396
397void cras_bt_io_destroy(struct cras_iodev *bt_iodev)
398{
399	int rc;
400	struct bt_io *btio = (struct bt_io *)bt_iodev;
401	struct cras_ionode *node;
402	struct bt_node *n;
403
404	if (bt_iodev->direction == CRAS_STREAM_OUTPUT)
405		rc = cras_iodev_list_rm_output(bt_iodev);
406	else
407		rc = cras_iodev_list_rm_input(bt_iodev);
408	if (rc == -EBUSY)
409		return;
410
411	DL_FOREACH(bt_iodev->nodes, node) {
412		n = (struct bt_node *)node;
413		cras_iodev_rm_node(bt_iodev, node);
414		free(n);
415	}
416	free(btio);
417}
418
419struct cras_ionode *cras_bt_io_get_profile(struct cras_iodev *bt_iodev,
420					   enum cras_bt_device_profile profile)
421{
422	struct cras_ionode *node;
423	DL_FOREACH(bt_iodev->nodes, node) {
424		struct bt_node *n = (struct bt_node *)node;
425		if (n->profile & profile)
426			return node;
427	}
428	return NULL;
429}
430
431int cras_bt_io_append(struct cras_iodev *bt_iodev,
432		      struct cras_iodev *dev,
433		      enum cras_bt_device_profile profile)
434{
435	struct cras_ionode *node;
436	struct bt_io *btio = (struct bt_io *)bt_iodev;
437
438	if (cras_bt_io_get_profile(bt_iodev, profile))
439		return -EEXIST;
440
441	node = add_profile_dev(bt_iodev, dev, profile);
442	if (!node)
443		return -ENOMEM;
444
445	if (profile == CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE &&
446	    cras_bt_device_can_switch_to_a2dp(btio->device)) {
447		bt_switch_to_profile(btio->device,
448				     CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
449		cras_bt_device_switch_profile(btio->device, bt_iodev);
450		syslog(LOG_ERR, "Switch to A2DP on append");
451	}
452	return 0;
453}
454
455int cras_bt_io_on_profile(struct cras_iodev *bt_iodev,
456			  enum cras_bt_device_profile profile)
457{
458	struct bt_node *btnode = (struct bt_node *)bt_iodev->active_node;
459	return !!(profile & btnode->profile);
460}
461
462int cras_bt_io_update_buffer_size(struct cras_iodev *bt_iodev)
463{
464	struct cras_iodev *dev = active_profile_dev(bt_iodev);
465	if (!dev)
466		return -EINVAL;
467
468	bt_iodev->buffer_size = dev->buffer_size;
469	return 0;
470}
471
472unsigned int cras_bt_io_try_remove(struct cras_iodev *bt_iodev,
473				   struct cras_iodev *dev)
474{
475	struct cras_ionode *node;
476	struct bt_node *active, *btnode;
477	unsigned int try_profile = 0;
478
479	active = (struct bt_node *)bt_iodev->active_node;
480
481	if (active->profile_dev == dev) {
482		DL_FOREACH(bt_iodev->nodes, node) {
483			btnode = (struct bt_node *)node;
484			/* Skip the active node and the node we're trying
485			 * to remove. */
486			if (btnode == active || btnode->profile_dev == dev)
487				continue;
488			try_profile = btnode->profile;
489			break;
490		}
491	} else {
492		try_profile = active->profile;
493	}
494	return try_profile;
495}
496
497int cras_bt_io_remove(struct cras_iodev *bt_iodev,
498		      struct cras_iodev *dev)
499{
500	struct cras_ionode *node;
501	struct bt_node *btnode;
502
503	DL_FOREACH(bt_iodev->nodes, node) {
504		btnode = (struct bt_node *)node;
505		if (btnode->profile_dev != dev)
506			continue;
507
508		/* If this is the active node, reset it. Otherwise delete
509		 * this node. */
510		if (node == bt_iodev->active_node) {
511			btnode->profile_dev = NULL;
512			btnode->profile = 0;
513		} else {
514			DL_DELETE(bt_iodev->nodes, node);
515			free(node);
516		}
517	}
518
519	/* The node of active profile could have been removed, update it.
520	 * Return err when fail to locate the active profile dev. */
521	update_active_node(bt_iodev, 0, 1);
522	btnode = (struct bt_node *)bt_iodev->active_node;
523	if ((btnode->profile == 0) || (btnode->profile_dev == NULL))
524		return -EINVAL;
525
526	return 0;
527}
528