blob: 2bf0d9b9c89d75839fc4076967edf3351f766f6f [file] [log] [blame]
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +01001/*
2 * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com>
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#define LOG_TAG "audio_hw_audience"
18#define LOG_NDEBUG 0
19
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23
24#include <cutils/log.h>
25#include <audience-routes.h>
26
27#include "audience.h"
28
29/*
30 * Writes an Integer to a file.
31 *
32 * @param path The absolute path string.
33 * @param value The Integer value to be written.
34 * @return 0 on success, -1 or errno on error.
35 */
36static int write_int(char const *path, const int value)
37{
38 int fd;
39 static int already_warned;
40
41 already_warned = 0;
42
43 ALOGV("write_int: path %s, value %d", path, value);
44 fd = open(path, O_RDWR);
45
46 if (fd >= 0) {
47 char buffer[20];
48 int bytes = sprintf(buffer, "%d\n", value);
49 int amt = write(fd, buffer, bytes);
50 close(fd);
51 return amt == -1 ? -errno : 0;
52 } else {
53 if (already_warned == 0) {
54 ALOGE("write_int failed to open %s\n", path);
55 already_warned = 1;
56 }
57 return -errno;
58 }
59}
60
61/*
62 * Writes the route value to sysfs.
63 *
64 * @param value The long Integer value to be written.
65 * @return 0 on success, -1 or errno on error.
66 */
67static int es_route_value_set(int value)
68{
69 return write_int(SYSFS_PATH_PRESET, value);
70}
71
72/*
73 * Writes the veq control to sysfs.
74 *
75 * @param value The Integer value to be written.
76 * @return 0 on success, -1 or errno on error.
77 */
78static int es_veq_control_set(int value)
79{
80 return write_int(SYSFS_PATH_VEQ, value);
81}
82
83/*
84 * Writes the extra volume to sysfs.
85 *
86 * @param value The Integer value to be written.
87 * @return 0 on success, -1 or errno on error.
88 */
89static int es_extra_volume_set(int value)
90{
91 return write_int(SYSFS_PATH_EXTRAVOLUME, value);
92}
93
94/*
95 * Convertes an out_device from the session to an earSmart compatible route.
96 *
97 * @param out_device The output device to be converted.
98 * @return Audience earSmart route, coded as long Integer.
99 */
100static long es_device_to_route(struct voice_session *session)
101{
102 long ret;
103 long nb_route;
104 long wb_route;
105
106 switch(session->out_device) {
107 case AUDIO_DEVICE_OUT_EARPIECE:
108 nb_route = Call_CT_NB;
109 wb_route = Call_CT_WB;
110 break;
111 case AUDIO_DEVICE_OUT_SPEAKER:
112 nb_route = Call_FT_NB;
113 wb_route = Call_FT_WB;
114 break;
115 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
116 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
117 nb_route = Call_HS_NB;
118 wb_route = Call_HS_WB;
119 break;
120 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
121 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
122 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
123 nb_route = Call_BT_NB;
124 wb_route = Call_BT_WB;
125 break;
126 default:
127 /* if output device isn't supported, use earpiece by default */
Christopher N. Hesse70524602017-04-02 13:19:36 +0200128 ALOGE("%s: unknown output device: %d, defaulting to earpiece", __func__,
129 session->out_device);
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +0100130 nb_route = Call_CT_NB;
131 wb_route = Call_CT_WB;
132 break;
133 }
134
135 /* TODO: Handle wb_amr=2 */
136 if (session->wb_amr_type >= 1) {
137 ret = wb_route;
138 } else {
139 ret = nb_route;
140 }
141
142 ALOGV("%s: converting out_device=%d to %s route: %ld", __func__, session->out_device,
143 ret == wb_route ? "Wide Band" : "Narrow Band", ret);
144 return ret;
145}
146
147/*
148 * Configures and enables the Audience earSmart IC.
149 *
150 * @param session Reference to the active voice call session.
151 * @return @return 0 on success, -1 or errno on error.
152 */
153int es_start_voice_session(struct voice_session *session)
154{
155 int ret;
156 long es_route = es_device_to_route(session);
157
158 /* TODO: Calculate these */
159 int extra_volume = 0;
160 int veq_control = 4;
161
162 /*
163 * The control flow for audience earSmart chip is as follows:
164 *
165 * route_value >> power_control(internal) >> extra_volume >> veq_control
166 */
167 ret = es_route_value_set(es_route);
168 if (ret != 0) {
169 ALOGE("%s: es_route_value_set(%ld) failed with code: %d", __func__, es_route, ret);
170 goto exit;
171 }
172 ret = es_extra_volume_set(extra_volume);
173 if (ret != 0) {
174 ALOGE("%s: es_extra_volume_set(%d) failed with code: %d", __func__, extra_volume, ret);
175 goto exit;
176 }
177 ret = es_veq_control_set(veq_control);
178 if (ret != 0) {
179 ALOGE("%s: es_veq_control_set(%d) failed with code: %d", __func__, veq_control, ret);
180 goto exit;
181 }
182
183exit:
184 return ret;
185}
186
187/*
188 * Disables the Audience earSmart IC.
189 */
190void es_stop_voice_session()
191{
192 /* This will cancel any pending workers, stop the stream and send the IC to sleep */
193 es_route_value_set(AUDIENCE_SLEEP);
194}