From 022224fbf883bb1600a69e4537b9a80eed35fbb8 Mon Sep 17 00:00:00 2001 From: Vincent Becker Date: Fri, 10 Aug 2012 14:40:49 +0200 Subject: Vibra: Adapt AOSP (default) vibrator HAL into a hardware module. Android's implementation of vibrator needs to be done inside a hardware module, so that it can make a vendor implementation possible. Hw module's name becomes vibrator.default.so. This change is related to other changes in: - frameworks/base - hardware/libhardware_legacy - device/generic/goldfish - platform/build Change-Id: I844279f5535289f079d412fdc44c5cb3c9c1130c Author: Vincent Becker Signed-off-by: Vincent Becker Signed-off-by: Shuo Gao Signed-off-by: Bruce Beare Signed-off-by: Jack Ren Signed-off-by: David Wagner Author-tracking-BZ: 49760 94611 --- modules/vibrator/vibrator.c | 135 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 modules/vibrator/vibrator.c (limited to 'modules/vibrator/vibrator.c') diff --git a/modules/vibrator/vibrator.c b/modules/vibrator/vibrator.c new file mode 100644 index 0000000..ce4c03c --- /dev/null +++ b/modules/vibrator/vibrator.c @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include + +#include +#include +#include +#include +#include + +static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable"; + +static int vibra_exists() { + int fd; + + fd = TEMP_FAILURE_RETRY(open(THE_DEVICE, O_RDWR)); + if(fd < 0) { + ALOGE("Vibrator file does not exist : %d", fd); + return 0; + } + + close(fd); + return 1; +} + +static int sendit(unsigned int timeout_ms) +{ + int to_write, written, ret, fd; + + char value[20]; /* large enough for millions of years */ + + fd = TEMP_FAILURE_RETRY(open(THE_DEVICE, O_RDWR)); + if(fd < 0) { + return -errno; + } + + to_write = snprintf(value, sizeof(value), "%u\n", timeout_ms); + written = TEMP_FAILURE_RETRY(write(fd, value, to_write)); + + if (written == -1) { + ret = -errno; + } else if (written != to_write) { + /* even though EAGAIN is an errno value that could be set + by write() in some cases, none of them apply here. So, this return + value can be clearly identified when debugging and suggests the + caller that it may try to call vibraror_on() again */ + ret = -EAGAIN; + } else { + ret = 0; + } + + errno = 0; + close(fd); + + return ret; +} + +static int vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms) +{ + /* constant on, up to maximum allowed time */ + return sendit(timeout_ms); +} + +static int vibra_off(vibrator_device_t* vibradev __unused) +{ + return sendit(0); +} + +static int vibra_close(hw_device_t *device) +{ + free(device); + return 0; +} + +static int vibra_open(const hw_module_t* module, const char* id __unused, + hw_device_t** device __unused) { + if (!vibra_exists()) { + ALOGE("Vibrator device does not exist. Cannot start vibrator"); + return -ENODEV; + } + + vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t)); + + if (!vibradev) { + ALOGE("Can not allocate memory for the vibrator device"); + return -ENOMEM; + } + + vibradev->common.tag = HARDWARE_DEVICE_TAG; + vibradev->common.module = (hw_module_t *) module; + vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0); + vibradev->common.close = vibra_close; + + vibradev->vibrator_on = vibra_on; + vibradev->vibrator_off = vibra_off; + + *device = (hw_device_t *) vibradev; + + return 0; +} + +/*===========================================================================*/ +/* Default vibrator HW module interface definition */ +/*===========================================================================*/ + +static struct hw_module_methods_t vibrator_module_methods = { + .open = vibra_open, +}; + +struct hw_module_t HAL_MODULE_INFO_SYM = { + .tag = HARDWARE_MODULE_TAG, + .module_api_version = VIBRATOR_API_VERSION, + .hal_api_version = HARDWARE_HAL_API_VERSION, + .id = VIBRATOR_HARDWARE_MODULE_ID, + .name = "Default vibrator HAL", + .author = "The Android Open Source Project", + .methods = &vibrator_module_methods, +}; -- cgit v1.1