blob: 950bc599c024f0a785e3ce00d177a08eaf5fbb62 [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 int fd;
112 char file_str[50];
113
114 snprintf(file_str, sizeof(file_str), "%s/%s", LED_DEVICE, "activate");
115 return device_exists(file_str);
Rob Herring61701df2015-11-16 12:54:30 -0600116}
117
118static int vibra_led_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
119{
120 int ret;
121 char value[TIMEOUT_STR_LEN]; /* large enough for millions of years */
122
123 ret = write_led_file("state", "1");
124 if (ret)
125 return ret;
126
127 snprintf(value, sizeof(value), "%u\n", timeout_ms);
128 ret = write_led_file("duration", value);
129 if (ret)
130 return ret;
131
132 return write_led_file("activate", "1");
133}
134
135static int vibra_led_off(vibrator_device_t* vibradev __unused)
136{
137 return write_led_file("activate", "0");
138}
139
Vincent Becker022224f2012-08-10 14:40:49 +0200140static int vibra_close(hw_device_t *device)
141{
142 free(device);
143 return 0;
144}
145
146static int vibra_open(const hw_module_t* module, const char* id __unused,
147 hw_device_t** device __unused) {
Rob Herring61701df2015-11-16 12:54:30 -0600148 bool use_led;
149
150 if (vibra_exists()) {
151 ALOGD("Vibrator using timed_output");
152 use_led = false;
153 } else if (vibra_led_exists()) {
154 ALOGD("Vibrator using LED trigger");
155 use_led = true;
156 } else {
Vincent Becker022224f2012-08-10 14:40:49 +0200157 ALOGE("Vibrator device does not exist. Cannot start vibrator");
158 return -ENODEV;
159 }
160
161 vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t));
162
163 if (!vibradev) {
164 ALOGE("Can not allocate memory for the vibrator device");
165 return -ENOMEM;
166 }
167
168 vibradev->common.tag = HARDWARE_DEVICE_TAG;
169 vibradev->common.module = (hw_module_t *) module;
170 vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
171 vibradev->common.close = vibra_close;
172
Rob Herring61701df2015-11-16 12:54:30 -0600173 if (use_led) {
174 vibradev->vibrator_on = vibra_led_on;
175 vibradev->vibrator_off = vibra_led_off;
176 } else {
177 vibradev->vibrator_on = vibra_on;
178 vibradev->vibrator_off = vibra_off;
179 }
Vincent Becker022224f2012-08-10 14:40:49 +0200180
181 *device = (hw_device_t *) vibradev;
182
183 return 0;
184}
185
186/*===========================================================================*/
187/* Default vibrator HW module interface definition */
188/*===========================================================================*/
189
190static struct hw_module_methods_t vibrator_module_methods = {
191 .open = vibra_open,
192};
193
194struct hw_module_t HAL_MODULE_INFO_SYM = {
195 .tag = HARDWARE_MODULE_TAG,
196 .module_api_version = VIBRATOR_API_VERSION,
197 .hal_api_version = HARDWARE_HAL_API_VERSION,
198 .id = VIBRATOR_HARDWARE_MODULE_ID,
199 .name = "Default vibrator HAL",
200 .author = "The Android Open Source Project",
201 .methods = &vibrator_module_methods,
202};