1 /* 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 17 #include <hardware/hardware.h> 18 #include <hardware/vibrator.h> 19 20 #include <errno.h> 21 #include <fcntl.h> 22 #include <malloc.h> 23 #include <math.h> 24 #include <stdbool.h> 25 #include <stdio.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #include <log/log.h> 30 31 #define TIMEOUT_STR_LEN 20 32 33 static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable"; 34 35 static bool device_exists(const char *file) { 36 int fd; 37 38 fd = TEMP_FAILURE_RETRY(open(file, O_RDWR)); 39 if(fd < 0) { 40 return false; 41 } 42 43 close(fd); 44 return true; 45 } 46 47 static bool vibra_exists() { 48 return device_exists(THE_DEVICE); 49 } 50 51 static int write_value(const char *file, const char *value) 52 { 53 int to_write, written, ret, fd; 54 55 fd = TEMP_FAILURE_RETRY(open(file, O_WRONLY)); 56 if (fd < 0) { 57 return -errno; 58 } 59 60 to_write = strlen(value) + 1; 61 written = TEMP_FAILURE_RETRY(write(fd, value, to_write)); 62 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 68 caller that it may try to call vibrator_on() again */ 69 ret = -EAGAIN; 70 } else { 71 ret = 0; 72 } 73 74 errno = 0; 75 close(fd); 76 77 return ret; 78 } 79 80 static 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 88 static 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 94 static int vibra_off(vibrator_device_t* vibradev __unused) 95 { 96 return sendit(0); 97 } 98 99 static const char LED_DEVICE[] = "/sys/class/leds/vibrator"; 100 101 static 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 109 static bool vibra_led_exists() 110 { 111 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); 116 } 117 118 static 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 135 static int vibra_led_off(vibrator_device_t* vibradev __unused) 136 { 137 return write_led_file("activate", "0"); 138 } 139 140 static int vibra_close(hw_device_t *device) 141 { 142 free(device); 143 return 0; 144 } 145 146 static int vibra_open(const hw_module_t* module, const char* id __unused, 147 hw_device_t** device __unused) { 148 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 { 157 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 173 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 } 180 181 *device = (hw_device_t *) vibradev; 182 183 return 0; 184 } 185 186 /*===========================================================================*/ 187 /* Default vibrator HW module interface definition */ 188 /*===========================================================================*/ 189 190 static struct hw_module_methods_t vibrator_module_methods = { 191 .open = vibra_open, 192 }; 193 194 struct 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 }; 203