Android Drive Development Reading note Seven

Source: Internet
Author: User

Seventh Chapter

(i) Create a device file

1. Initialize the CDec with the Cdev_init function

Describing the device file requires a CDEV structure with the following code:

struct cdev{

struct Kobject kobj;

struct module *owener;

const struct File_operations *ops;

struct List_head list;

dev_t Dev;

unsigned int count;


Most of the member variables do not need to be initialized by ourselves, call the Cdev-init function.

2. Specify the device number

Specify the master/slave device number separately, so you need to MKDEV the macro

int Dev_number=mkdev (Major,minor);

3. Use the Cdev_add function to add a character device to the character device data in the kernel

Calling the Cdev_add function requires specifying the device file pointer (p), the device number (dev), the number of device files (count), and also calls an important function kobj_map.

4. Create a struct class using the Class_create macro

The Struct class contains some variables related to device files and some callback function pointer variables. Code:

Struct class *leds_class=null;

Leds_class=class_create (This_module, "dev_name");

5. Create a device file using the Device_create function

Decive_create (Leds_class,null,dev_number,null,device_name)

(ii) Uninstalling the LED driver's device files

Uninstall the LED-driven device file and call the Decive_destory, Class_destory, unregister_chrdev_region methods in turn. The Led_destory_device function is used to unload the LED-driven device file, and the Leds_exit function is the LED-driven unload function that completes the operation of uninstalling the LED driver file by calling the Led_destory_device function.

Setting the status of LEDs, etc.

Set the register and initialize the LED driver by setting the register value to set the status of the LED pin and control its light off. Then to control the LEDs, you can pass strings and I/O commands. Use the string to File_operations.write function, use the I/O command to File_operations.ioctl, from the user space like the kernel to write data to the function Copy_from_user.

Module functions for LED drivers

You can specify access permissions for a parameter file by using the Module_param macro

State int leds_state=1;

State int led_init (void)


INT ret;

Ret=led_create_device ();

LEDS_INIT_GPM (~led_state);

PRINTK (device_name "\tinitialized\n");

Return ret;


Module_param (Led_state,int, S_irugo | IWUSR);

This chapter then describes the use of the NDK test led driver, using the Java test led driver and the LED driver porting.

Android Drive Development Reading note Seven

Related Article

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: 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.