Platform MTK6573
Android Vibrator System Architecture
I. Driver Layer
Modify and add Linux kernel files for Android
/kernel/drivers/staging/android/timed_output.h/kernel/drivers/staging/android/timed_output.c
Timed_output.h defines the timed_output_dev struct.
- struct timed_output_dev {
- const char *name;
-
- /* enable the output and set the timer */
- void (*enable)(struct timed_output_dev *sdev, int timeout);
-
- /* returns the current number of milliseconds remaining on the timer */
- int (*get_time)(struct timed_output_dev *sdev);
-
- /* private data */
- struct device *dev;
- int index;
- int state;
- };
Timed_output.c implements this struct. The timed_output_dev_register function is used for registration and timed_output_dev_unregister is used for cancellation.
- int timed_output_dev_register(struct timed_output_dev *tdev)
- {
- int ret;
-
- if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
- return -EINVAL;
-
- ret = create_timed_output_class();
- if (ret < 0)
- return ret;
-
- tdev->index = atomic_inc_return(&device_count);
- tdev->dev = device_create(timed_output_class, NULL,
- MKDEV(0, tdev->index), NULL, tdev->name);
- if (IS_ERR(tdev->dev))
- return PTR_ERR(tdev->dev);
-
- ret = device_create_file(tdev->dev, &dev_attr_enable);
- if (ret < 0)
- goto err_create_file;
-
- dev_set_drvdata(tdev->dev, tdev);
- tdev->state = 0;
- return 0;
-
- err_create_file:
- device_destroy(timed_output_class, MKDEV(0, tdev->index));
- printk(KERN_ERR "timed_output: Failed to register driver %s\n",
- tdev->name);
-
- return ret;
- }
- EXPORT_SYMBOL_GPL(timed_output_dev_register);
-
- void timed_output_dev_unregister(struct timed_output_dev *tdev)
- {
- device_remove_file(tdev->dev, &dev_attr_enable);
- device_destroy(timed_output_class, MKDEV(0, tdev->index));
- dev_set_drvdata(tdev->dev, NULL);
- }
- EXPORT_SYMBOL_GPL(timed_output_dev_unregister);
Driver implementation porting
Take MTK 6573 as an Example
./Mediatek/platform/mt6573/kernel/drivers/vibrator. c
Operation Device
First open the mobile phone debugging, connect to the USB, execute the adb shell, and enter/sys/devices/timed_output/vibrator/
Execute echo "10000" enable to find that the mobile phone is shaking
- # echo "10000" enable
- echo "10000" enable
- 10000 enable
Run cat enable to view the remaining number of current vibration time:
- # cat enable
- cat enable
- 0
Ii. Hardware Abstraction Layer
Android encapsulates calls to the underlying driver and becomes the hardware abstraction layer.
/Hardware/libhardware_legacy/vibrator. c
- int vibrator_on(int timeout_ms)
- {
- /* constant on, up to maximum allowed time */
- return sendit(timeout_ms);
- }
-
- int vibrator_off()
- {
- return sendit(0);
- }
Iii. JNI framework layer
The Android JNI framework layer facilitates Java to call the C/C ++ method.
./Frameworks/base/services/jni/com_android_server_VibratorService.cpp
- namespace android
- {
-
- static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)
- {
- // LOGI("vibratorOn\n");
- vibrator_on(timeout_ms);
- }
- static void vibratorOff(JNIEnv *env, jobject clazz)
- {
- // LOGI("vibratorOff\n");
- vibrator_off();
- }
- static JNINativeMethod method_table[] = {
- { "vibratorOn", "(J)V", (void*)vibratorOn },
- { "vibratorOff", "()V", (void*)vibratorOff }
- };
- int register_android_server_VibratorService(JNIEnv *env)
- {
- return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
- method_table, NELEM(method_table));
- }
- };
Iv. Java Application Layer
This layer includes Java application calls and Android system service Java Layer
./Frameworks/base/services/java/com/android/server/VibratorService. java