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 <stdlib.h>
18 
19 #include <cutils/log.h>
20 
21 #include <qemu.h>
22 #include <hardware/hardware.h>
23 #include <hardware/vibrator.h>
24 
sendit(unsigned int timeout_ms)25 static int sendit(unsigned int timeout_ms)
26 {
27     if (qemu_check()) {
28         if (qemu_control_command("vibrator:%u", timeout_ms) < 0) {
29             return -errno;
30         }
31         return 0;
32     }
33 
34     return -ENOSYS;
35 }
36 
qemu_vibra_on(vibrator_device_t * vibradev __unused,unsigned int timeout_ms)37 static int qemu_vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
38 {
39     return sendit(timeout_ms);
40 }
41 
qemu_vibra_off(vibrator_device_t * vibradev __unused)42 static int qemu_vibra_off(vibrator_device_t* vibradev __unused)
43 {
44     return sendit(0);
45 }
46 
qemu_vibra_close(hw_device_t * device)47 static int qemu_vibra_close(hw_device_t *device)
48 {
49     free(device);
50     return 0;
51 }
52 
qemu_vibra_open(const hw_module_t * module,const char * id __unused,hw_device_t ** device)53 static int qemu_vibra_open(const hw_module_t* module, const char* id __unused,
54                            hw_device_t** device) {
55     vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t));
56 
57     if (!vibradev) {
58         ALOGE("No memory available to create Goldfish vibrator device!");
59         return -ENOMEM;
60     }
61 
62     vibradev->common.tag = HARDWARE_DEVICE_TAG;
63     vibradev->common.module = (hw_module_t *) module;
64     vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
65     vibradev->common.close = qemu_vibra_close;
66 
67     vibradev->vibrator_on = qemu_vibra_on;
68     vibradev->vibrator_off = qemu_vibra_off;
69 
70     *device = (hw_device_t *) vibradev;
71 
72     return 0;
73 }
74 
75 /*===========================*/
76 /* Emulator vibrator module  */
77 /*===========================*/
78 
79 static struct hw_module_methods_t qemu_vibrator_module_methods = {
80     .open = qemu_vibra_open,
81 };
82 
83 struct hw_module_t HAL_MODULE_INFO_SYM = {
84     .tag = HARDWARE_MODULE_TAG,
85     .module_api_version = VIBRATOR_API_VERSION,
86     .hal_api_version = HARDWARE_HAL_API_VERSION,
87     .id = VIBRATOR_HARDWARE_MODULE_ID,
88     .name = "Goldfish vibrator module",
89     .author = "The Android Open Source Project",
90     .methods = &qemu_vibrator_module_methods,
91 };
92