blob: 8042b5701d3705338dee2dae67ab7a2d245e48eb [file] [log] [blame]
Vincent Becker022224f2012-08-10 14:40:49 +02001/*
2 * Copyright (C) 2013 The Android Open Source Project
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
Vincent Becker022224f2012-08-10 14:40:49 +020017#include <hardware/hardware.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070018#include <hardware/vibrator.h>
Vincent Becker022224f2012-08-10 14:40:49 +020019
Mark Salyzynd88dfe82017-04-11 08:56:09 -070020#include <errno.h>
21#include <fcntl.h>
Elliott Hughes07c08552015-01-29 21:19:10 -080022#include <malloc.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070023#include <math.h>
Rob Herring61701df2015-11-16 12:54:30 -060024#include <stdbool.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070025#include <stdio.h>
Rob Herring61701df2015-11-16 12:54:30 -060026#include <string.h>
Vincent Becker022224f2012-08-10 14:40:49 +020027#include <unistd.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070028
29#include <log/log.h>
Vincent Becker022224f2012-08-10 14:40:49 +020030
Rob Herring61701df2015-11-16 12:54:30 -060031#define TIMEOUT_STR_LEN 20
32
Vincent Becker022224f2012-08-10 14:40:49 +020033static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable";
34
David Lin4e8f5612017-03-08 17:27:59 -080035static bool device_exists(const char *file) {
Vincent Becker022224f2012-08-10 14:40:49 +020036 int fd;
37
David Lin4e8f5612017-03-08 17:27:59 -080038 fd = TEMP_FAILURE_RETRY(open(file, O_RDWR));
Vincent Becker022224f2012-08-10 14:40:49 +020039 if(fd < 0) {
David Lin4e8f5612017-03-08 17:27:59 -080040 return false;
Vincent Becker022224f2012-08-10 14:40:49 +020041 }
42
43 close(fd);
David Lin4e8f5612017-03-08 17:27:59 -080044 return true;
45}
46
47static bool vibra_exists() {
48 return device_exists(THE_DEVICE);
Vincent Becker022224f2012-08-10 14:40:49 +020049}
50
Rob Herring61701df2015-11-16 12:54:30 -060051static int write_value(const char *file, const char *value)
Vincent Becker022224f2012-08-10 14:40:49 +020052{
53 int to_write, written, ret, fd;
54
Rob Herring61701df2015-11-16 12:54:30 -060055 fd = TEMP_FAILURE_RETRY(open(file, O_WRONLY));
56 if (fd < 0) {
Vincent Becker022224f2012-08-10 14:40:49 +020057 return -errno;
58 }
59
Rob Herring61701df2015-11-16 12:54:30 -060060 to_write = strlen(value) + 1;
Vincent Becker022224f2012-08-10 14:40:49 +020061 written = TEMP_FAILURE_RETRY(write(fd, value, to_write));
Vincent Becker022224f2012-08-10 14:40:49 +020062 if (written == -1) {
63 ret = -errno;
64 } else if (written != to_write) {
65 /* even though EAGAIN is an errno value that could be set
66 by write() in some cases, none of them apply here. So, this return
67 value can be clearly identified when debugging and suggests the
Rob Herring61701df2015-11-16 12:54:30 -060068 caller that it may try to call vibrator_on() again */
Vincent Becker022224f2012-08-10 14:40:49 +020069 ret = -EAGAIN;
70 } else {
71 ret = 0;
72 }
73
74 errno = 0;
75 close(fd);
76
77 return ret;
78}
79
Rob Herring61701df2015-11-16 12:54:30 -060080static int sendit(unsigned int timeout_ms)
81{
82 char value[TIMEOUT_STR_LEN]; /* large enough for millions of years */
83
84 snprintf(value, sizeof(value), "%u", timeout_ms);
85 return write_value(THE_DEVICE, value);
86}
87
Vincent Becker022224f2012-08-10 14:40:49 +020088static int vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
89{
90 /* constant on, up to maximum allowed time */
91 return sendit(timeout_ms);
92}
93
94static int vibra_off(vibrator_device_t* vibradev __unused)
95{
96 return sendit(0);
97}
98
Rob Herring61701df2015-11-16 12:54:30 -060099static const char LED_DEVICE[] = "/sys/class/leds/vibrator";
100
101static int write_led_file(const char *file, const char *value)
102{
103 char file_str[50];
104
105 snprintf(file_str, sizeof(file_str), "%s/%s", LED_DEVICE, file);
106 return write_value(file_str, value);
107}
108
David Lin4e8f5612017-03-08 17:27:59 -0800109static bool vibra_led_exists()
Rob Herring61701df2015-11-16 12:54:30 -0600110{
David Lin4e8f5612017-03-08 17:27:59 -0800111 char file_str[50];
112
113 snprintf(file_str, sizeof(file_str), "%s/%s", LED_DEVICE, "activate");
114 return device_exists(file_str);
Rob Herring61701df2015-11-16 12:54:30 -0600115}
116
117static int vibra_led_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
118{
119 int ret;
120 char value[TIMEOUT_STR_LEN]; /* large enough for millions of years */
121
122 ret = write_led_file("state", "1");
123 if (ret)
124 return ret;
125
126 snprintf(value, sizeof(value), "%u\n", timeout_ms);
127 ret = write_led_file("duration", value);
128 if (ret)
129 return ret;
130
131 return write_led_file("activate", "1");
132}
133
134static int vibra_led_off(vibrator_device_t* vibradev __unused)
135{
136 return write_led_file("activate", "0");
137}
138
Vincent Becker022224f2012-08-10 14:40:49 +0200139static int vibra_close(hw_device_t *device)
140{
141 free(device);
142 return 0;
143}
144
145static int vibra_open(const hw_module_t* module, const char* id __unused,
146 hw_device_t** device __unused) {
Rob Herring61701df2015-11-16 12:54:30 -0600147 bool use_led;
148
149 if (vibra_exists()) {
150 ALOGD("Vibrator using timed_output");
151 use_led = false;
152 } else if (vibra_led_exists()) {
153 ALOGD("Vibrator using LED trigger");
154 use_led = true;
155 } else {
Vincent Becker022224f2012-08-10 14:40:49 +0200156 ALOGE("Vibrator device does not exist. Cannot start vibrator");
157 return -ENODEV;
158 }
159
160 vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t));
161
162 if (!vibradev) {
163 ALOGE("Can not allocate memory for the vibrator device");
164 return -ENOMEM;
165 }
166
167 vibradev->common.tag = HARDWARE_DEVICE_TAG;
168 vibradev->common.module = (hw_module_t *) module;
169 vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
170 vibradev->common.close = vibra_close;
171
Rob Herring61701df2015-11-16 12:54:30 -0600172 if (use_led) {
173 vibradev->vibrator_on = vibra_led_on;
174 vibradev->vibrator_off = vibra_led_off;
175 } else {
176 vibradev->vibrator_on = vibra_on;
177 vibradev->vibrator_off = vibra_off;
178 }
Vincent Becker022224f2012-08-10 14:40:49 +0200179
180 *device = (hw_device_t *) vibradev;
181
182 return 0;
183}
184
185/*===========================================================================*/
186/* Default vibrator HW module interface definition */
187/*===========================================================================*/
188
189static struct hw_module_methods_t vibrator_module_methods = {
190 .open = vibra_open,
191};
192
193struct hw_module_t HAL_MODULE_INFO_SYM = {
194 .tag = HARDWARE_MODULE_TAG,
195 .module_api_version = VIBRATOR_API_VERSION,
196 .hal_api_version = HARDWARE_HAL_API_VERSION,
197 .id = VIBRATOR_HARDWARE_MODULE_ID,
198 .name = "Default vibrator HAL",
199 .author = "The Android Open Source Project",
200 .methods = &vibrator_module_methods,
201};