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