Android layered learning notes (5)

Source: Internet
Author: User

In the Android system, some simple descriptions of its layering are provided in the above sections, followed by Kernel Driver-related content. This is a free world, of course complicated. It is precisely because of freedom that we can build a different operating system world. Android is one of them. Whether Android is a traditional Linux operating system or not, it implements some useful things.

I don't want to elaborate on the kernel here. After all, my skills are limited. Let's just talk about some simple drivers. As a matter of fact, the driver I have come into contact with is quite simple, with no extra pin control. The following uses a simple char driver to describe the underlying Android content, which is actually the content of the Linux DRIVER:

 

For driver development, we must look at"
Linux
Device Driver 3
2.6
"(
Lddp
) This book. Currently
2.6
The version is available in the Chinese version.



After reading this book, let's look at the following example. You will find it very simple. If you do not read this book, you can write a simple Driver Based on the following example. Note that the following example is a character driver.



Write
Linux
The driver has a framework. Generally, you can complete the following functions.

Xxx_read (struct file * filp, char _ User
* Buff, size_t count, loff_t * OFFP)

Xxx_write (struct file * filp, const char
_ User * buff, size_t count, loff_t * OFFP)

Xxx_open (struct inode * inode, struct File
* Filp)

Xxx_release (struct inode * inode, struct
File * filp)

Xxx_ioctl (struct inode * inode, struct File
* File,


Unsigned
Int cmd, unsigned long Arg)

Static int _ init xxx_init (void)

Static void _ exit xxx_exit (void)

 

Module_init (xxx_init );

Module_exit (xxx_exit );

 



In practice, the character driver
Write/read
Functions are rarely used. It can also be left empty.
IOCTL
Function.



The driver corresponds to the application interface function. You can see the name of the driver function.


Xxx_read
<---> Read

Xxx_write
<---> Write

Xxx_open
<---> Open

Xxx_release
<---> Close

Xxx_ioctl
<---> IOCTL



That is, when the application calls
Read, write, open, IOCTL
The function is to call the corresponding function of its driver.



In
Insmod
Will call
Xxx_init



In
Rmmod
Will call
Xxx_exit

 

For
Xxx_init
Function, there are many things to implement, such
Io
Port settings, or initial values of other variables, driver registration, and device node creation. Correspondingly,
Xxx_exit
Complete.
Xxx_init
Complete the inverse process. The details are as follows:

Each driver has a primary device number and secondary device number, which can be fixed but cannot conflict with each other. The best method is system allocation, which is also
Lddp
Recommended by the author.

Static struct
File_operations xxx_fops =

{


. Owner

=
This_module,


. Open

=
Xxx_open,


. Release

=
Xxx_release,


. IOCTL

=
Xxx_ioctl,


. Read

=
Xxx_read,


. Write

=
Xxx_write,

};

Struct cdev xxx_cdev;

Struct class * xxx_class;

Int
Xxx_init (void)

{

Int res, err;


Dev_t Dev = mkdev (xxx_major, 0 );


If (xxx_major)


{


Res = register_chrdev_region (Dev, 1,
Device_name );//
Fixed allocation


}


Else


{


Res = alloc_chrdev_region (& Dev, 0,
1, device_name );//
Dynamic Allocation


Xxx_major = major (Dev );


}

 


If (RES <0)


{


Printk ("xxx_device: Unable to get
Major % d/N ", xxx_major );


Return res;

 


}

 


If (xxx_major = 0)


{


Xxx_major = res;


}


Cdev_init (& xxx_cdev, & xxx_fops );


Xxx_cdev.owner = this_module;


Xxx_cdev.ops = & xxx_fops;


Err = cdev_add (& xxx_cdev, Dev, 1 );
//
Register
Cdev


If (ERR)


{


Printk ("error % d adding xxx % d ",
Err, 0 );


}


Xxx_class = class_create (this_module,
"Xxx_class ");


Device_create (xxx_class, null, Dev,
Device_name, "xxx % d", 0 );


Other_init ();

}

 

Int xxx_exit (void)

{


Xxx_release (null, null );

Cdev_del (& xxx_cdev );

Device_destroy (xxx_class, mkdev (xxx_major, 0 ));

Class_destroy (xxx_class );

Unregister_chrdev_region (mkdev (xxx_major, 0), 1 );

}

For
Xxx_open,
Some information is prompted, indicating that the driver is loaded.
Similarly
Xxx_release
You can also use a simple prompt to indicate that the driver is disabled.

The focus is on
Xxx_ioctl
Function. All the operations are here.

Static int
Epi_ioctl (struct inode * inode, struct file * file,


Unsigned int
CMD, unsigned long Arg)

{


Char val = 1;


Switch (CMD)


{


Case io_1__test_set:


Copy_from_user (& Val, (char *) Arg, 1 );


 
Printk ("% d", Val );


Break;

Case io_1__test_get:


Copy_to_user (char *) Arg, & Val, 1 );


Break;


Default:


Break;


}


Return 0;

}



The above implementation is from
ARG
And put the data
ARG
. Used
Copy_from_usr
And
Copy_to_usr
Two functions. Why are there two functions: the driver is in the working kernel space, and the application is in the user space. If you use
Memcpy
These functions may result in data in the kernel space. If you copy the data to the user space, you may get an error message.

 



After writing the driver, you can compile it.
Makefile
To facilitate compilation.

OBJ-M + = xxx. o

#
Kernel code tree location

Kdir: =/home/ALSA/kernel/linux_2.6.28

#
Set Compiler

Cross_compile = arm-None-Linux-gnueabi-

Cc: =$ (cross_compile) GCC

Ld: = $ (cross_compile) LD

Pwd = $ (shell PWD)

ALL: build install

Build: clean


$ (Make)
-C $ (kdir) M = $ (PWD) Modules

Clean:


Rm
-RF *. O *. Ko

Save and run
Make
Is generated in the current directory.
Xxx. Ko
File.

 

Finally, load the driver:

Loading: insmod XXX. Ko

Uninstall: rmmod xxx

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.