blob: afd7fa27f4531b545485ffad03ada504d986564f [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 */
128 nb_route = Call_CT_NB;
129 wb_route = Call_CT_WB;
130 break;
131 }
132
133 /* TODO: Handle wb_amr=2 */
134 if (session->wb_amr_type >= 1) {
135 ret = wb_route;
136 } else {
137 ret = nb_route;
138 }
139
140 ALOGV("%s: converting out_device=%d to %s route: %ld", __func__, session->out_device,
141 ret == wb_route ? "Wide Band" : "Narrow Band", ret);
142 return ret;
143}
144
145/*
146 * Configures and enables the Audience earSmart IC.
147 *
148 * @param session Reference to the active voice call session.
149 * @return @return 0 on success, -1 or errno on error.
150 */
151int es_start_voice_session(struct voice_session *session)
152{
153 int ret;
154 long es_route = es_device_to_route(session);
155
156 /* TODO: Calculate these */
157 int extra_volume = 0;
158 int veq_control = 4;
159
160 /*
161 * The control flow for audience earSmart chip is as follows:
162 *
163 * route_value >> power_control(internal) >> extra_volume >> veq_control
164 */
165 ret = es_route_value_set(es_route);
166 if (ret != 0) {
167 ALOGE("%s: es_route_value_set(%ld) failed with code: %d", __func__, es_route, ret);
168 goto exit;
169 }
170 ret = es_extra_volume_set(extra_volume);
171 if (ret != 0) {
172 ALOGE("%s: es_extra_volume_set(%d) failed with code: %d", __func__, extra_volume, ret);
173 goto exit;
174 }
175 ret = es_veq_control_set(veq_control);
176 if (ret != 0) {
177 ALOGE("%s: es_veq_control_set(%d) failed with code: %d", __func__, veq_control, ret);
178 goto exit;
179 }
180
181exit:
182 return ret;
183}
184
185/*
186 * Disables the Audience earSmart IC.
187 */
188void es_stop_voice_session()
189{
190 /* This will cancel any pending workers, stop the stream and send the IC to sleep */
191 es_route_value_set(AUDIENCE_SLEEP);
192}