1 /* Copyright (c) 2013 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 <stdbool.h>
7 #include <sys/param.h>
8 #include <sys/socket.h>
9 #include <sys/time.h>
10 #include <syslog.h>
11
12 #include "cras_audio_area.h"
13 #include "cras_hfp_ag_profile.h"
14 #include "cras_hfp_iodev.h"
15 #include "cras_hfp_info.h"
16 #include "cras_hfp_slc.h"
17 #include "cras_iodev.h"
18 #include "cras_system_state.h"
19 #include "cras_util.h"
20 #include "sfh.h"
21 #include "utlist.h"
22
23 /* Implementation of bluetooth hands-free profile iodev.
24 * Members:
25 * base - The cras_iodev structure base class.
26 * device - The assciated bt_device.
27 * slc - Handle to the HFP service level connection.
28 * info - hfp_info taking care of SCO data read/write.
29 * drain_complete - Flag to indicate if valid samples are drained
30 * in no stream state. Only used for output.
31 * filled_zeros - Number of zero data in frames have been filled
32 * to buffer of hfp_info in no stream state. Only used for output
33 */
34 struct hfp_io {
35 struct cras_iodev base;
36 struct cras_bt_device *device;
37 struct hfp_slc_handle *slc;
38 struct hfp_info *info;
39 bool drain_complete;
40 unsigned int filled_zeros;
41 };
42
update_supported_formats(struct cras_iodev * iodev)43 static int update_supported_formats(struct cras_iodev *iodev)
44 {
45 struct hfp_io *hfpio = (struct hfp_io *)iodev;
46
47 /* 16 bit, mono, 8kHz for narrowband and 16KHz for wideband */
48 iodev->format->format = SND_PCM_FORMAT_S16_LE;
49
50 free(iodev->supported_rates);
51 iodev->supported_rates = (size_t *)malloc(2 * sizeof(size_t));
52
53 iodev->supported_rates[0] =
54 (hfp_slc_get_selected_codec(hfpio->slc) == HFP_CODEC_ID_MSBC) ?
55 16000 :
56 8000;
57 iodev->supported_rates[1] = 0;
58
59 free(iodev->supported_channel_counts);
60 iodev->supported_channel_counts = (size_t *)malloc(2 * sizeof(size_t));
61 iodev->supported_channel_counts[0] = 1;
62 iodev->supported_channel_counts[1] = 0;
63
64 free(iodev->supported_formats);
65 iodev->supported_formats =
66 (snd_pcm_format_t *)malloc(2 * sizeof(snd_pcm_format_t));
67 iodev->supported_formats[0] = SND_PCM_FORMAT_S16_LE;
68 iodev->supported_formats[1] = 0;
69
70 return 0;
71 }
72
no_stream(struct cras_iodev * iodev,int enable)73 static int no_stream(struct cras_iodev *iodev, int enable)
74 {
75 struct hfp_io *hfpio = (struct hfp_io *)iodev;
76 struct timespec hw_tstamp;
77 unsigned int hw_level;
78 unsigned int level_target;
79
80 if (iodev->direction != CRAS_STREAM_OUTPUT)
81 return 0;
82
83 hw_level = iodev->frames_queued(iodev, &hw_tstamp);
84 if (enable) {
85 if (!hfpio->drain_complete && (hw_level <= hfpio->filled_zeros))
86 hfpio->drain_complete = 1;
87 hfpio->filled_zeros += hfp_fill_output_with_zeros(
88 hfpio->info, iodev->buffer_size);
89 return 0;
90 }
91
92 /* Leave no stream state.*/
93 level_target = iodev->min_cb_level;
94 if (hfpio->drain_complete) {
95 hfp_force_output_level(hfpio->info, level_target);
96 } else {
97 unsigned int valid_samples = 0;
98 if (hw_level > hfpio->filled_zeros)
99 valid_samples = hw_level - hfpio->filled_zeros;
100 level_target = MAX(level_target, valid_samples);
101
102 if (level_target > hw_level)
103 hfp_fill_output_with_zeros(hfpio->info,
104 level_target - hw_level);
105 else
106 hfp_force_output_level(hfpio->info, level_target);
107 }
108 hfpio->drain_complete = 0;
109 hfpio->filled_zeros = 0;
110
111 return 0;
112 }
113
frames_queued(const struct cras_iodev * iodev,struct timespec * tstamp)114 static int frames_queued(const struct cras_iodev *iodev,
115 struct timespec *tstamp)
116 {
117 struct hfp_io *hfpio = (struct hfp_io *)iodev;
118
119 if (!hfp_info_running(hfpio->info))
120 return -1;
121
122 /* Do not enable timestamp mechanism on HFP device because last time
123 * stamp might be a long time ago and it is not really useful. */
124 clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
125 return hfp_buf_queued(hfpio->info, iodev->direction);
126 }
127
configure_dev(struct cras_iodev * iodev)128 static int configure_dev(struct cras_iodev *iodev)
129 {
130 struct hfp_io *hfpio = (struct hfp_io *)iodev;
131 int sk, err, mtu;
132
133 /* Assert format is set before opening device. */
134 if (iodev->format == NULL)
135 return -EINVAL;
136 iodev->format->format = SND_PCM_FORMAT_S16_LE;
137 cras_iodev_init_audio_area(iodev, iodev->format->num_channels);
138
139 if (hfp_info_running(hfpio->info))
140 goto add_dev;
141
142 sk = cras_bt_device_sco_connect(hfpio->device,
143 hfp_slc_get_selected_codec(hfpio->slc));
144 if (sk < 0)
145 goto error;
146
147 mtu = cras_bt_device_sco_packet_size(
148 hfpio->device, sk, hfp_slc_get_selected_codec(hfpio->slc));
149
150 /* Start hfp_info */
151 err = hfp_info_start(sk, mtu, hfpio->info);
152 if (err)
153 goto error;
154
155 hfpio->drain_complete = 0;
156 hfpio->filled_zeros = 0;
157 add_dev:
158 hfp_info_add_iodev(hfpio->info, iodev->direction, iodev->format);
159 hfp_set_call_status(hfpio->slc, 1);
160
161 iodev->buffer_size = hfp_buf_size(hfpio->info, iodev->direction);
162
163 return 0;
164 error:
165 syslog(LOG_ERR, "Failed to open HFP iodev");
166 return -1;
167 }
168
close_dev(struct cras_iodev * iodev)169 static int close_dev(struct cras_iodev *iodev)
170 {
171 struct hfp_io *hfpio = (struct hfp_io *)iodev;
172
173 hfp_info_rm_iodev(hfpio->info, iodev->direction);
174 if (hfp_info_running(hfpio->info) && !hfp_info_has_iodev(hfpio->info)) {
175 hfp_info_stop(hfpio->info);
176 hfp_set_call_status(hfpio->slc, 0);
177 }
178
179 cras_iodev_free_format(iodev);
180 cras_iodev_free_audio_area(iodev);
181 return 0;
182 }
183
set_hfp_volume(struct cras_iodev * iodev)184 static void set_hfp_volume(struct cras_iodev *iodev)
185 {
186 size_t volume;
187 struct hfp_io *hfpio = (struct hfp_io *)iodev;
188
189 volume = cras_system_get_volume();
190 if (iodev->active_node)
191 volume = cras_iodev_adjust_node_volume(iodev->active_node,
192 volume);
193
194 hfp_event_speaker_gain(hfpio->slc, volume);
195 }
196
delay_frames(const struct cras_iodev * iodev)197 static int delay_frames(const struct cras_iodev *iodev)
198 {
199 struct timespec tstamp;
200
201 return frames_queued(iodev, &tstamp);
202 }
203
get_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)204 static int get_buffer(struct cras_iodev *iodev, struct cras_audio_area **area,
205 unsigned *frames)
206 {
207 struct hfp_io *hfpio = (struct hfp_io *)iodev;
208 uint8_t *dst = NULL;
209
210 if (!hfp_info_running(hfpio->info))
211 return -1;
212
213 hfp_buf_acquire(hfpio->info, iodev->direction, &dst, frames);
214
215 iodev->area->frames = *frames;
216 /* HFP is mono only. */
217 iodev->area->channels[0].step_bytes =
218 cras_get_format_bytes(iodev->format);
219 iodev->area->channels[0].buf = dst;
220
221 *area = iodev->area;
222 return 0;
223 }
224
put_buffer(struct cras_iodev * iodev,unsigned nwritten)225 static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
226 {
227 struct hfp_io *hfpio = (struct hfp_io *)iodev;
228
229 if (!hfp_info_running(hfpio->info))
230 return -1;
231
232 hfp_buf_release(hfpio->info, iodev->direction, nwritten);
233 return 0;
234 }
235
flush_buffer(struct cras_iodev * iodev)236 static int flush_buffer(struct cras_iodev *iodev)
237 {
238 struct hfp_io *hfpio = (struct hfp_io *)iodev;
239 unsigned nframes;
240
241 if (iodev->direction == CRAS_STREAM_INPUT) {
242 nframes = hfp_buf_queued(hfpio->info, iodev->direction);
243 hfp_buf_release(hfpio->info, iodev->direction, nframes);
244 }
245 return 0;
246 }
247
update_active_node(struct cras_iodev * iodev,unsigned node_idx,unsigned dev_enabled)248 static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
249 unsigned dev_enabled)
250 {
251 }
252
hfp_free_resources(struct hfp_io * hfpio)253 void hfp_free_resources(struct hfp_io *hfpio)
254 {
255 struct cras_ionode *node;
256 node = hfpio->base.active_node;
257 if (node) {
258 cras_iodev_rm_node(&hfpio->base, node);
259 free(node);
260 }
261 free(hfpio->base.supported_channel_counts);
262 free(hfpio->base.supported_rates);
263 free(hfpio->base.supported_formats);
264 cras_iodev_free_resources(&hfpio->base);
265 }
266
hfp_iodev_create(enum CRAS_STREAM_DIRECTION dir,struct cras_bt_device * device,struct hfp_slc_handle * slc,enum cras_bt_device_profile profile,struct hfp_info * info)267 struct cras_iodev *hfp_iodev_create(enum CRAS_STREAM_DIRECTION dir,
268 struct cras_bt_device *device,
269 struct hfp_slc_handle *slc,
270 enum cras_bt_device_profile profile,
271 struct hfp_info *info)
272 {
273 struct hfp_io *hfpio;
274 struct cras_iodev *iodev;
275 struct cras_ionode *node;
276 const char *name;
277
278 hfpio = (struct hfp_io *)calloc(1, sizeof(*hfpio));
279 if (!hfpio)
280 goto error;
281
282 iodev = &hfpio->base;
283 iodev->direction = dir;
284
285 hfpio->device = device;
286 hfpio->slc = slc;
287
288 /* Set iodev's name to device readable name or the address. */
289 name = cras_bt_device_name(device);
290 if (!name)
291 name = cras_bt_device_object_path(device);
292
293 snprintf(iodev->info.name, sizeof(iodev->info.name), "%s", name);
294 iodev->info.name[ARRAY_SIZE(iodev->info.name) - 1] = 0;
295 iodev->info.stable_id =
296 SuperFastHash(cras_bt_device_object_path(device),
297 strlen(cras_bt_device_object_path(device)),
298 strlen(cras_bt_device_object_path(device)));
299
300 iodev->configure_dev = configure_dev;
301 iodev->frames_queued = frames_queued;
302 iodev->delay_frames = delay_frames;
303 iodev->get_buffer = get_buffer;
304 iodev->put_buffer = put_buffer;
305 iodev->flush_buffer = flush_buffer;
306 iodev->no_stream = no_stream;
307 iodev->close_dev = close_dev;
308 iodev->update_supported_formats = update_supported_formats;
309 iodev->update_active_node = update_active_node;
310 iodev->set_volume = set_hfp_volume;
311
312 node = (struct cras_ionode *)calloc(1, sizeof(*node));
313 node->dev = iodev;
314 strcpy(node->name, iodev->info.name);
315
316 node->plugged = 1;
317 node->type = CRAS_NODE_TYPE_BLUETOOTH;
318 node->volume = 100;
319 gettimeofday(&node->plugged_time, NULL);
320
321 cras_bt_device_append_iodev(device, iodev, profile);
322 cras_iodev_add_node(iodev, node);
323 cras_iodev_set_active_node(iodev, node);
324
325 hfpio->info = info;
326
327 return iodev;
328
329 error:
330 if (hfpio) {
331 hfp_free_resources(hfpio);
332 free(hfpio);
333 }
334 return NULL;
335 }
336
hfp_iodev_destroy(struct cras_iodev * iodev)337 void hfp_iodev_destroy(struct cras_iodev *iodev)
338 {
339 struct hfp_io *hfpio = (struct hfp_io *)iodev;
340
341 cras_bt_device_rm_iodev(hfpio->device, iodev);
342 hfp_free_resources(hfpio);
343 free(hfpio);
344 }
345