Switch instance in sysfs of Android Kernel

Source: Internet
Author: User

Switch instance in sysfs of Android Kernel

Switch instance in sysfs of Android Kernel

Finally, you can do what you want this weekend. I am the second-stream kernel driver developer to parse the switch class in sysfs. First, let's look at the source. On a common embedded Linux platform, we usually fight for each other and do not need to follow any rules at all. In Android, Google defines some subtle rules that you can not comply with at all. But it is always good to follow the rules.

First, let's take a look at what is in the/sys/class/switch class.

Examples/CvKOswLS/tNXmw + bev6o6pc9wpgo8cd4gpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140720/20140720085527140.png" alt = "\">

Here, four bl backlights are included; h2w is a headset; hdmi is hdmi; mic is a microphone that basically belongs to an external device. Of course, it may not be like bl. Each includes name/power/state/subsystem/uevent. The state is generally the state value provided to the user space in the driver.

The simplest Hello World example in LDD3 is used as a switch example:

After less than 20 lines of code are added, the simplest switch has been implemented. The effect is as follows:

You can directly call switch_set_state in the driver to set different status values. Correspondingly, the application layer can read this to obtain the status.

This article does not elaborate on the switch implementation details, and provides a complete example from a practical point of view. Of course, if you need to know the implementation details, you can refer to switch_class and android switch module. This can be used if the "driver" wants to inform the user of the Space status.

By the way, make KERNELDIR = ~ /4.2.2 _ r1/kernel/is followed by the Android kernel source code path. Of course, the kernel in the path is compiled. You can compile without any changes.


Complete simple example code is provided:

/* * $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $ */#include 
 
  #include 
  
   #include 
   
    MODULE_LICENSE("Dual BSD/GPL");struct switch_dev sdev;static int hello_switch(){    int state, ret;    sdev.name = "hello";    ret = switch_dev_register(&sdev);    if (ret < 0)        return ret;    state = 0;    switch_set_state(&sdev,state);    return 0;}static int hello_init(void){printk(KERN_ALERT "Hello, world\n");    if(hello_switch())    {        printk(KERN_ERR "failed to create hello switch\n");        return -ENODEV;    }return 0;}static void hello_exit(void){printk(KERN_ALERT "Goodbye, cruel world\n");}module_init(hello_init);module_exit(hello_exit);
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.