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 char file_str[50]; 112 113 snprintf(file_str, sizeof(file_str), "%s/%s", LED_DEVICE, "activate"); 114 return device_exists(file_str); 115 } 116 117 static 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 134 static int vibra_led_off(vibrator_device_t* vibradev __unused) 135 { 136 return write_led_file("activate", "0"); 137 } 138 139 static int vibra_close(hw_device_t *device) 140 { 141 free(device); 142 return 0; 143 } 144 145 static int vibra_open(const hw_module_t* module, const char* id __unused, 146 hw_device_t** device __unused) { 147 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 { 156 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 172 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 } 179 180 *device = (hw_device_t *) vibradev; 181 182 return 0; 183 } 184 185 /*===========================================================================*/ 186 /* Default vibrator HW module interface definition */ 187 /*===========================================================================*/ 188 189 static struct hw_module_methods_t vibrator_module_methods = { 190 .open = vibra_open, 191 }; 192 193 struct 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 }; 202