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/vibrator.h> 18 #include <hardware/hardware.h> 19 20 #include <cutils/log.h> 21 22 #include <malloc.h> 23 #include <stdio.h> 24 #include <unistd.h> 25 #include <fcntl.h> 26 #include <errno.h> 27 #include <math.h> 28 29 static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable"; 30 31 static int vibra_exists() { 32 int fd; 33 34 fd = TEMP_FAILURE_RETRY(open(THE_DEVICE, O_RDWR)); 35 if(fd < 0) { 36 ALOGE("Vibrator file does not exist : %d", fd); 37 return 0; 38 } 39 40 close(fd); 41 return 1; 42 } 43 44 static int sendit(unsigned int timeout_ms) 45 { 46 int to_write, written, ret, fd; 47 48 char value[20]; /* large enough for millions of years */ 49 50 fd = TEMP_FAILURE_RETRY(open(THE_DEVICE, O_RDWR)); 51 if(fd < 0) { 52 return -errno; 53 } 54 55 to_write = snprintf(value, sizeof(value), "%u\n", timeout_ms); 56 written = TEMP_FAILURE_RETRY(write(fd, value, to_write)); 57 58 if (written == -1) { 59 ret = -errno; 60 } else if (written != to_write) { 61 /* even though EAGAIN is an errno value that could be set 62 by write() in some cases, none of them apply here. So, this return 63 value can be clearly identified when debugging and suggests the 64 caller that it may try to call vibraror_on() again */ 65 ret = -EAGAIN; 66 } else { 67 ret = 0; 68 } 69 70 errno = 0; 71 close(fd); 72 73 return ret; 74 } 75 76 static int vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms) 77 { 78 /* constant on, up to maximum allowed time */ 79 return sendit(timeout_ms); 80 } 81 82 static int vibra_off(vibrator_device_t* vibradev __unused) 83 { 84 return sendit(0); 85 } 86 87 static int vibra_close(hw_device_t *device) 88 { 89 free(device); 90 return 0; 91 } 92 93 static int vibra_open(const hw_module_t* module, const char* id __unused, 94 hw_device_t** device __unused) { 95 if (!vibra_exists()) { 96 ALOGE("Vibrator device does not exist. Cannot start vibrator"); 97 return -ENODEV; 98 } 99 100 vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t)); 101 102 if (!vibradev) { 103 ALOGE("Can not allocate memory for the vibrator device"); 104 return -ENOMEM; 105 } 106 107 vibradev->common.tag = HARDWARE_DEVICE_TAG; 108 vibradev->common.module = (hw_module_t *) module; 109 vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0); 110 vibradev->common.close = vibra_close; 111 112 vibradev->vibrator_on = vibra_on; 113 vibradev->vibrator_off = vibra_off; 114 115 *device = (hw_device_t *) vibradev; 116 117 return 0; 118 } 119 120 /*===========================================================================*/ 121 /* Default vibrator HW module interface definition */ 122 /*===========================================================================*/ 123 124 static struct hw_module_methods_t vibrator_module_methods = { 125 .open = vibra_open, 126 }; 127 128 struct hw_module_t HAL_MODULE_INFO_SYM = { 129 .tag = HARDWARE_MODULE_TAG, 130 .module_api_version = VIBRATOR_API_VERSION, 131 .hal_api_version = HARDWARE_HAL_API_VERSION, 132 .id = VIBRATOR_HARDWARE_MODULE_ID, 133 .name = "Default vibrator HAL", 134 .author = "The Android Open Source Project", 135 .methods = &vibrator_module_methods, 136 }; 137