Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 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 | |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 17 | #include <hardware/hardware.h> |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame^] | 18 | #include <hardware/vibrator.h> |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 19 | |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame^] | 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
Elliott Hughes | 07c0855 | 2015-01-29 21:19:10 -0800 | [diff] [blame] | 22 | #include <malloc.h> |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame^] | 23 | #include <math.h> |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 24 | #include <stdbool.h> |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame^] | 25 | #include <stdio.h> |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 26 | #include <string.h> |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame^] | 28 | |
| 29 | #include <log/log.h> |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 30 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 31 | #define TIMEOUT_STR_LEN 20 |
| 32 | |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 33 | static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable"; |
| 34 | |
David Lin | 4e8f561 | 2017-03-08 17:27:59 -0800 | [diff] [blame] | 35 | static bool device_exists(const char *file) { |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 36 | int fd; |
| 37 | |
David Lin | 4e8f561 | 2017-03-08 17:27:59 -0800 | [diff] [blame] | 38 | fd = TEMP_FAILURE_RETRY(open(file, O_RDWR)); |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 39 | if(fd < 0) { |
David Lin | 4e8f561 | 2017-03-08 17:27:59 -0800 | [diff] [blame] | 40 | return false; |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | close(fd); |
David Lin | 4e8f561 | 2017-03-08 17:27:59 -0800 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
| 47 | static bool vibra_exists() { |
| 48 | return device_exists(THE_DEVICE); |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 51 | static int write_value(const char *file, const char *value) |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 52 | { |
| 53 | int to_write, written, ret, fd; |
| 54 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 55 | fd = TEMP_FAILURE_RETRY(open(file, O_WRONLY)); |
| 56 | if (fd < 0) { |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 57 | return -errno; |
| 58 | } |
| 59 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 60 | to_write = strlen(value) + 1; |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 61 | written = TEMP_FAILURE_RETRY(write(fd, value, to_write)); |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 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 |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 68 | caller that it may try to call vibrator_on() again */ |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 69 | ret = -EAGAIN; |
| 70 | } else { |
| 71 | ret = 0; |
| 72 | } |
| 73 | |
| 74 | errno = 0; |
| 75 | close(fd); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 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 | |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 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 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 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 | |
David Lin | 4e8f561 | 2017-03-08 17:27:59 -0800 | [diff] [blame] | 109 | static bool vibra_led_exists() |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 110 | { |
David Lin | 4e8f561 | 2017-03-08 17:27:59 -0800 | [diff] [blame] | 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); |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 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 | |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 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) { |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 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 { |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 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 | |
Rob Herring | 61701df | 2015-11-16 12:54:30 -0600 | [diff] [blame] | 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 | } |
Vincent Becker | 022224f | 2012-08-10 14:40:49 +0200 | [diff] [blame] | 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 | }; |