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