Android Driver Development Hello Instance: driver section modified: KERNEL/ARCH/ARM/CONFIGS/MSM8909-1GB_W100_HD720P-PERF_ Defconfigmodified: kernel/arch/arm/configs/msm8909-1gb_w100_hd720p_defconfigmodified: kernel/drivers /input/misc/kconfigmodified: kernel/drivers/input/misc/makefile KERNEL/DRIVERS/INPUT/MISC/HELLO.C&NBSP;FFBM section: Used to test whether the drive is working modified: vendor/qcom/proprietary/common/ Build/fastmmi/mmi.mkmodified: vendor/qcom/proprietary/fastmmi/res/config/mmi-w200_w.cfgmodified: Vendor/qcom/proprietary/fastmmi/res/layout/android.mkmodified: Vendor/qcom/proprietary/fastmmi/res/values /strings.xml vendor/qcom/proprietary/fastmmi/module/hello/ vendor/qcom/proprietary/fastmmi/module/hello/android.mk & nbsp Vendor/qcom/proprietary/fastmmi/module/hello/hello.cpp Vendor/qcom/proprietary/fastmmi/res/layout/layout_hello.xml selinux security, used to allow MMI access to/dev/hello nodes modified: device/qcom/sepolicy/common/mmi.temodified: external/ Sepolicy/device.temodified: external/sepolicy/file_contexts 1) kernel/drivers/input/misc/ hello.c #include <linux/types.h> #include <linux/module.h> #include <linux/fs.h> #include < linux/init.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/slab.h> #include <asm/uaccess.h> module_author ("zhaopuqing"); Module_license ("Dual BSD/GPL"); Module_version ("Hello v1.0"); Module_alias ("Hello"); #define Device_sum 1 static int hello_open (struct inode *inode, struct file *filp); static int hello_release (struct inode *, struct file *filp), static ssize_t hello_read (struct file*, char*, size_t, loff_t* ); static ssize_t hello_write (struct file*, const char*, size_t, loff_t*); /*the major device number*/static int hello_major = 0;static int hello_minor = 0; static struct class* hello_class = N Ull; /*init the file_operations structure*/struct file_operations hello_fops = { . Owner = THIS_MODULE,& nbsp . Open = hello_open, . Release = hello_release, . Read = hello_read, . Write = h ello_write,}; struct cdev *cdev; static int global_var = 0; /*global var*/ /*module init*/static int __init hello_init (void) { int ret = 0; struct Devic e* temp = null; dev_t devno = 0; PRINTK ("hello:hello_init.\n"); ret = Alloc_c Hrdev_region (&devno, Hello_minor, device_sum, "Hello"); if (Ret < 0) { Print K (Kern_alert "hello:failed to alloc Char dev region.\n");goto fail;} hello_major = Major (Devno); Hello_minor = minor (Devno); Cdev = Cdev_alloc (); Cdev->owner = This_module; Cdev->ops = &hello_fops; if (ret = Cdev_add (Cdev, Devno, 1))) {PRINTK (kern_notice "hello:error%d adding hello.\n", ret);return 0;}else{PRINTK (kern_alert "Hello:hello register success.\n"); } Hello_class = Class_create (this_module, "Hello"); if (Is_err (Hello_class)) {ret = Ptr_err (Hello_class);PRINTK (kern_alert "Failed to create Hello class.\n");goto Destroy_cdev;temp = Device_create (Hello_class, NULL, Devno, "%s", "Hello"); if (Is_err (temp)) {ret = ptr_err (temp);PRINTK (kern_alert "Failed to create Hello device.");goto Destroy_class;} return ret; Destroy_class:Class_destroy (hello_class);Destroy_cdev:Cdev_del (Cdev);Failreturn ret;} static void __exit hello_exit (void) {dev_t Devno = MKDEV (hello_major, 0); PRINTK (Kern_alert "Goodbye, cruel world\n"); /*remove Cdev from kernel*/Cdev_del (Cdev); /*unregister the device device driver*/unregister_chrdev_region (DEVNO, 1); /*free the dev structure*/if (cdev)Kfree (Cdev); Cdev = NULL;} /*open device*/static int Hello_open (struct inode *inode, struct file *filp) { INT ret = 0; PRINTK (kern_alert "Kernel:open success.\n"); return ret;} /*release device*/static int hello_release (struct inode *inode, struct file *filp) { PRINTK (Kern_alert "Kernel:release success.\n"); return 0;} /*read device*/static int hello_read (struct file *filp, char *buf, size_t len, loff_t *off) { PRINTK (KER N_alert "kernel:reading...\n"); if (Copy_to_user (buf, &global_var, sizeof (int))) { return-efault; } return sizeof (int);} /*write device*/static ssize_t hello_write (struct file *filp, const char *BUF, size_t len, loff_t *off) {   ; PRINTK (Kern_alert "kernel:writing...\n"); if (Copy_from_user (&global_var, buf, sizeof (int))) { return-efault; }   return sizeof (int);} /*module Register*/module_init (Hello_init); Module_exit (hello_exit); ******************************** 2) kernel/drivers/input/misc/kconfigconfig sensors_hello TriState "Hello" depends on spi=y help &NBS P;if say yes here's get supoort for Hello ' s hello test. **************** 3) kernel/drivers/input/misc/makefileobj-$ (Config_sensors_hello) + hello.o ******************************************************4) kernel/arch/arm/ configs/msm8909-1gb_w100_hd720p-perf_defconfigconfig_sensors_hello=y ************************************ 5) kernel/arch/arm/configs/msm8909-1gb_w100_hd720p_defconfigconfig_sensors_hello=y &NBSP;**************************************************6) vendor/qcom/pRoprietary/fastmmi/module/hello/hello.cpp#include "mmi_module.h" #define TAG "Hello" #define DEVICE_NAME "/dev/ Hello "#define hello_test_wait" Hello test,please waitting ... "#define hello_test_success" Hello TEST success! " #define HELLO_TEST_FAILED "HELLO TEST failed!" static Const mmi_module_t* G_module;static unordered_map < string, string > paras; static void cb_functi On (const char *STR, int size) { if (g_module! = NULL) { G_module->cb_print (paras[k Ey_module_name].c_str (), Subcmd_mmi, str, size, PRINT); }} int hello_test () { Alogi ("%s:% S start!\n ", TAG, __function__); int fd = -1; int val = 0; FD = open (DE Vice_name, O_RDWR); if (fd <= 0) { Alogi ("%s:%s open device [%s] failed!\n", TAG, __function__, device_name);return FAILED; } Alogi ("%s:%s start to read device info!\n", TAG, __function__); Read (FD, &val, sizeof (val)); Alogi ("%s:%s Read value=%d\n", TAG, __function__, Val); Alogi (" \ n "); val = 5; Alogi ("%s:%s start write value%d T o%s\n ", TAG, __function__, Val, device_name); Write (FD, &val, sizeof (val)); Alogi (" * \ n "); Alogi ("%s:%s read device value again!\n ", TAG, __ FUNCTION__); Read (FD, &val, sizeof (val)); Alogi ("%s:%s Read value=%d\n", TAG, __function__ , Val); Close (FD); return SUCCESS;} static int32_t module_init (const mmi_module_t * module, Unordered_map < string, string > ¶ms) { & nbsp Alogi ("%s:%s start!\n", TAG, __function__); if (module = = NULL) { Aloge ("%s:%s NULL point received!\n", __function__);return FAILED;} return SUCCESS;} Static int32_t module_deinit (const mmi_module_t * module) {Alogi ("%s:%s start!\n", TAG, __function__); if (module = = NULL) {Aloge ("%s:%s NULL point received!\n", TAG, __function__);return FAILED;} return SUCCESS;} Static int32_t Module_run (const mmi_module_t * module, const char *cmd, Unordered_map < string, string > ¶ms ) {int ret = FAILED; if (!module | |!cmd) {Aloge ("%s:%s NULL point received!\n", TAG, __function__);return FAILED;} g_module = module; paras = params; Alogi ("%s Start.command:%s\n", __function__, CMD); Cb_function (hello_test_wait, strlen (hello_test_wait)); if (!strcmp (cmd, subcmd_mmi)) {ret = Hello_test ();if (ret! = SUCCESS) {Aloge ("%s:%s Open Hello module failed!\n", TAG, __function__);cb_function (hello_test_failed, strlen (hello_test_failed));}else{Alogi ("%s:%s Open Hello module success!\n", TAG, __function__);cb_function (hello_test_success, strlen (hello_test_success)); }}else if (!strcmp (cmd, SUBCMD_PCBA)) {}else{Aloge ("%s:%s Invalid command:%s received!\n", TAG, __function__, cmd);ret = FAILED;} return ret;} Static int32_t module_stop (const mmi_module_t *module) {Alogi ("%s:%s start.\n", __function__); if (module = = NULL) {Aloge ("%s:%s NULL point received!\n", TAG, __function__);return FAILED; } Pthread_kill (module->run_pid, SIGUSR1); return SUCCESS;} static struct mmi_module_methods_t module_methods = { . Module_init = module_init, . module _deinit = module_deinit, . Module_run = module_run, . Module_stop = Module_stop,}; mmi_ module_t mmi_module_info_sym = { . version_major = 1, . Version_minor = 0, . Name = "H Ello ", . Author =" Hello test ", . Methods = &module_methods, . Module_handle = NUL l, supported_cmd_list = null, . supported_cmd_list_size = 0, . Cb_print = NULL,  ; . Run_pid = -1,}; *****************************************************7) vendor/qcom/proprietary /fastmmi/module/hello/android.mklocal_path: = $ (call My-dir) include $ (clear_vars) Local_module_owner: = QtiLOCAL_ Proprietary_module: = truelocal_src_files: = Hello.cpp locAl_module: = Mmi_hellolocal_clang: = falselocal_module_tags: = optionallocal_cflags: =-walllocal_c_includes: = External /libcxx/include \$ (qc_prop_root)/fastmmi/libmmiLocal_shared_libraries: = libcutils libutils libmmi libhardware libc++ local_c_includes + = $ (TARGET_OUT_INTERMEDIATES)/ Kernel_obj/usr/includeifeq ($ (Target_compile_with_msm_kernel), true) Local_additional_dependencies: = $ (TARGET_OUT_ Intermediates)/kernel_obj/usrendif include $ (build_shared_library) ********************************************** 8) Vendor/qcom/proprietary/fastmmi/res/layout/layout_hello.xml<?xml version= "1.0" encoding= "Utf-8"? ><layout> <include layout= "Header.xml"/> <buttonname= "instruction"text= "Hello_notice"w_rel= "h_rel= "Ten"x_rel= "0"y_rel= "color= "0X007D7DFF"/><textviewname= "Display"text= ""w_rel= "h_rel= "x_rel= "4"y_rel= "/> " <include layout= "Footer.xml"/></layout> ************************************ 9) vendor/qcom/proprietary/fastmmi/res/values/strings.xml<string name= "Hello" >Hello</ String><string name= "Hell_notice" >hello test, just for test!</string> ************************* Ten) vendor/qcom/proprietary/fastmmi/res/layout/android.mk include $ (clear_ VARS) Local_src_files: = layout_hello.xmllocal_cflags: =-walllocal_module: = Layout_hello.xmllocal_module_class: = Etclocal_module_tags: = Optionallocal_module_path: = $ (TARGET_OUT_ETC)/mmi/layoutinclude $ (BUILD_PREBUILT) *** One) vendor/qcom/proprietary/fastmmi/res/config/mmi-w200_ w.cfg[hello]lib_name=mmi_hello.soenable=1automation=0display_name=hellolayout=layout_hello.xml ********** vendor/qcom/proprietary/common/build/fastmmi/mmi.mkmmi + = MMI_hello mmi_res + = layout_hello.xml ************************************************13) external/ Sepolicy/device.te#hello Testtype Hello_device, dev_type; ************************************************14 ) external/sepolicy/file_contexts#hello Test/dev/hello u:object_r:hello_device:s0 ******** device/qcom/sepolicy/common/mmi.teallow MMI HELLO_DEVICE:CHR _file {Open Read write}; above for all test code: Print kernel message: adb shell cat/proc/kmsg
Hello instance of Android driver development