blob: 2f077675adab414da6875c22b6cb25e1611468e2 [file] [log] [blame]
Rohit Sekharc5949c32023-08-23 15:19:29 +05301/*
2 * Copyright (c) 2021, The Linux Foundation. All rights reserved.
3 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * * Neither the name of The Linux Foundation nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Changes from Qualcomm Innovation Center are provided under the following license:
31 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
32 * SPDX-License-Identifier: BSD-3-Clause-Clear
33 */
34
35#define LOG_TAG "vendor.qti.vibrator.offload"
36
37#include <stdio.h>
38#include <stdint.h>
39#include <string.h>
40#include <thread>
41#include <linux/input.h>
42#include <log/log.h>
43#include <fcntl.h>
44#include <cutils/log.h>
45#include <cutils/uevent.h>
46#include <cutils/properties.h>
47#include <sys/poll.h>
48#include <sys/ioctl.h>
49
50#include "include/Vibrator.h"
51#include "VibratorPatterns.h"
52
53namespace aidl {
54namespace android {
55namespace hardware {
56namespace vibrator {
57
58#define UEVENT_MSG_LEN 1024
59#define SLATE_EVENT "SLATE_EVENT="
60#define SLATE_EVENT_STRING_LEN 12 //length of SLATE_EVENT
61/*
62 * TODO Need to work on solution to get this from kernel header
63 * without effecting other kernel versions where this change
64 * goes in.
65 */
66#define SLATE_AFTER_POWER_UP 4
67
68PatternOffload::PatternOffload()
69{
70 char prop_str[PROPERTY_VALUE_MAX];
71 mEnabled = 0;
72
73 if (property_get("ro.vendor.qc_aon_presence", prop_str, NULL))
74 mEnabled = atoi(prop_str);
75
76 if (mEnabled != 1)
77 return;
78
79 std::thread t(&PatternOffload::SSREventListener, this);
80 t.detach();
81}
82
83void PatternOffload::SSREventListener(void)
84{
85 int device_fd, n, ssr_event = 0;
86 char msg[UEVENT_MSG_LEN + 2];
87 char *msg_ptr = msg;
88
89 /* Offload during the bootup */
90 SendPatterns();
91
92 device_fd = uevent_open_socket(64*1024, true);
93 if(device_fd < 0)
94 {
95 ALOGE("open socket failed: %d", device_fd);
96 return;
97 }
98
99 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
100 if (n <= 0 || n > UEVENT_MSG_LEN) {
101 ALOGE("Message length %d is not correct\n", n);
102 continue;
103 }
104 msg[n] = '\0';
105 msg[n+1] = '\0';
106 if (strstr(msg, "slate_com_dev")) {
107 while(*msg_ptr) {
108 if(!strncmp(msg_ptr, SLATE_EVENT, SLATE_EVENT_STRING_LEN)) {
109 msg_ptr += SLATE_EVENT_STRING_LEN;
110 ssr_event = (atoi(msg_ptr));
111 switch(ssr_event) {
112 case SLATE_AFTER_POWER_UP:
113 ALOGD("SLATE is powered up");
114 SendPatterns();
115 break;
116 }
117 }
118 while(*msg_ptr++);
119 }
120 }
121 }
122}
123
124/** Offload patterns
125 * The sequence of steps in offloading patterns.
126 * 1. Open the Glink channel to offload the patterns
127 * 2. Send the configuration/meta data to co-proc
128 * 3. Wait for the response from the co-proc
129 * 4. Send the pattern data to co-proc
130 * 5. Wait for the response
131 * 6. Exit
132 */
133void PatternOffload::SendPatterns()
134{
135 uint8_t *data;
136 uint32_t len;
137 int32_t rc;
138
139 rc = initChannel();
140 if (rc < 0)
141 return;
142
143 rc = get_pattern_config(&data, &len);
144 if (rc < 0 || !data)
145 return;
146
147 /* Send config data */
148 rc = sendData(data, len);
149 if (rc < 0)
150 return;
151
152 rc = get_pattern_data(&data, &len);
153 if (rc < 0)
154 return;
155
156 /* Send pattern data */
157 rc = sendData(data, len);
158 if (rc < 0)
159 ALOGE("pattern offloaded failed\n");
160 else
161 ALOGI("Patterns offloaded successfully\n");
162
163 free_pattern_mem(data);
164}
165
166int PatternOffload::sendData(uint8_t *data, int len)
167{
168 int rc, status = 0;
169
170 if (!data || !len)
171 return -EINVAL;
172
173 rc = GlinkCh.GlinkWrite(data, len);
174 if (rc < 0)
175 return rc;
176
177 rc = GlinkCh.GlinkPoll();
178 if (rc < 0)
179 return rc;
180
181 rc = GlinkCh.GlinkRead((uint8_t *)&status, 4);
182 if (rc < 0)
183 return rc;
184
185 if (status != OFFLOAD_SUCCESS)
186 return -EIO;
187
188 return 0;
189}
190
191
192int PatternOffload::initChannel()
193{
194 std::string chname = "/dev/glinkpkt_slate_haptics_offload";
195 int rc;
196
197 rc = GlinkCh.GlinkOpen(chname);
198 if (rc < 0)
199 {
200 ALOGE("Failed to open Glink channel name %s\n", chname.c_str());
201 return rc;
202 }
203
204 return 0;
205}
206
207#define GLINK_MAX_CONN_RETRIES 60
208int OffloadGlinkConnection::GlinkOpen(std::string& dev)
209{
210 int tries = GLINK_MAX_CONN_RETRIES;
211 dev_name = dev;
212
213 do {
214 fd = ::open(dev_name.c_str(), O_RDWR);
215 tries--;
216 if (fd < 0)
217 {
218 ALOGE("%s: %s: open error(%s)", __func__, dev.c_str(), strerror(errno));
219 sleep(1);
220 }
221 } while(-ETIMEDOUT == errno && tries > 0 );
222
223 return fd;
224}
225
226int OffloadGlinkConnection::GlinkClose()
227{
228 if (fd >= 0)
229 {
230 ::close(fd);
231 fd = -1;
232 }
233 return 0;
234}
235
236int OffloadGlinkConnection::GlinkPoll()
237{
238 ssize_t rc = 0;
239 struct pollfd poll_fd;
240
241 // wait for Rx data available in fd, for 2 seconds timeout
242 poll_fd.fd = fd;
243 poll_fd.events = POLLIN;
244
245 rc = ::poll(&poll_fd, 1, 2000);
246
247 if(rc > 0)
248 {
249 if (poll_fd.revents & POLLIN)
250 return 0;
251 } else if (rc == 0) {
252 ALOGE("Glink poll timeout");
253 } else {
254 ALOGE("Glink poll error: %s\n", strerror(errno));
255 }
256
257 return -1;
258}
259
260int OffloadGlinkConnection::GlinkRead(uint8_t *data, size_t size)
261{
262 int rc = 0;
263 size_t bytes_read = 0;
264
265 if (fd < 0)
266 return -1;
267
268 if (0 != GlinkPoll())
269 return -1;
270
271 while (bytes_read < size)
272 {
273 rc = ::read(fd, data+bytes_read, size-bytes_read);
274 if (rc < 0) {
275 if (errno != EAGAIN) {
276 ALOGE("%s: Read error: %s, rc %d", __func__,
277 strerror(errno), rc);
278 goto read_error;
279 }
280 } else if (rc == 0) {
281 ALOGE("%s: Zero length packet received or hardware connection went off",
282 __func__);
283 }
284 bytes_read += rc;
285 }
286 return 0;
287
288read_error:
289 return -1;
290}
291
292int OffloadGlinkConnection::GlinkWrite(uint8_t *buf, size_t buflen)
293{
294 size_t bytes_written_out = 0;
295 int rc = 0;
296
297 if (fd < 0)
298 return -1;
299
300 if (!buflen) {
301 ALOGE("%s: Invalid buffer len", __func__);
302 return 0;
303 }
304
305 while (bytes_written_out < buflen) {
306 rc = ::write (fd, buf+bytes_written_out, buflen-bytes_written_out);
307 if (rc < 0) {
308 ALOGE("%s: Write returned failure %d", __func__, rc);
309 return errno;
310 }
311 bytes_written_out += rc;
312 }
313 rc = 0;
314
315 return rc;
316}
317
318} // namespace vibrator
319} // namespace hardware
320} // namespace android
321} // namespace aidl