Sixth. First Linux driver: Count the number of words

Source: Internet
Author: User
Tags dmesg

In this chapter, as the first chapter, the first thing to do is to understand what the Linux driver is, first of all, to understand how the Linux driver works, Linux system will each drive is mapped to a file, save in/dev, and then use the C function ioctl to drive the device file to send print commands.

First, write the Linux driver

1. Build Linux driver skeleton

Linux kernel requires load and unload drivers when using drivers

Load driver: Set up device file, allocate memory address space, etc. module_init function processing driver initialization

Uninstall drive: Delete device files, free memory address space, etc. module_exit function processing exit

A C program file containing the two macros of these two functions can also be seen as a Linux-powered skeleton

2. Registering and unregistering device files

Any Linux driver requires a device file, or the application will not be able to interact with the driver.

Set up the device file: Completed in the first step of the function to process the Linux initialization work. Misc_register function

Delete device files: Completed in the first step of the function to process the Linux exit. Misc_deregister function

3. Specify driver-related information

The driver is self-describing, the driver's author name, the open source protocol used, the alias, the driver description, and so on. This information needs to be specified in the driver source code.

Module_author, Module_license, Module_alls, Module_descripion, and other macros can specify driver-related information

4. Specifying a callback function

A driver does not have to specify all the callback functions, and the callback function is registered by the relevant mechanism.

5. Writing business logic

Specific business logic is related to the functionality of the drive, and the business logic may consist of multiple functions, multiple files, or even multiple Linux driver modules

6. Writing the makefile file

The compilation rules for the Linux kernel source code are defined by the makefile file. So writing a new Linux driver must have a makefile file

7. Compiling Linux Drivers

Can be compiled directly into the kernel or can be compiled separately as a module

8. Install and uninstall Linux drivers

If the Linux driver is compiled into the kernel, as long as Linux uses the kernel, the driver will be automatically reproduced

If the Linux driver is in a separate module, you need to load the Linux driver module with the Insmod or Modprode command, and the Rmmod command uninstalls the Linux driver module.

Second, the first Linux driver, take Word_count as an example

(i) The basis for writing code

#mkdir-P/root/drivers/ch06/word_count build directory to store Linux drivers

#cd/root/drivers/ch06/word_count

#echo ' > word_count.c build Drive source code files

#echo ' obj-m: = WORD_COUNT.O ' > Makefile write a Makefile file make command to compile word_count.c or WORD_COUNT.S files in the Linux drive source directory into Word_ COUNT.O file

Obj-m means that the Linux driver is compiled as a module (. ko file) and WORD_COUNT.O is connected to the Word_count.ko file and then loaded with Insmod or Modprode commands Word_count.ko

Obj-y means the Linux driver is compiled into the Linux kernel, WORD_COUNT.O will be connected to the BUILT-IN.O file and will eventually be connected into the kernel

The Linux system divides the memory into the user space and the kernel space, the space program cannot directly access, the PRINTK function runs in the kernel space, the printf function runs in the user space, therefore the Linux driver which belongs to the kernel program does not have the direct access to the printf function.

#make-C/usr/src/linux-headers-3.0.0-15-generic m=/root/driver/ch06/word_count compiling Linux driver source code

# insmod Word_count.ko Load Drive

# Lsmod | grep Word_count to see if Word_count installed successfully

# rmmod word_count Uninstall Linux driver

#dmesg | grep Word_count | Tail-n 2 Viewing log information with Linux driver output

(ii) Designation of driver-related information

Module Module_author ("Lining");

Module Description: Module_descrption ("Statistics of Word count.");

Module alias: Module_alias ("word Count MODULE.");

Open Source Agreement: Module_license ("GPL");

#define DEVICE_NAME "WordCount"//define Device file name

Describes the callback function pointer corresponding to the event triggered by the device file

Owner: Device event callback which driver module is applied to, This_module represents the current driver module

static struct File_operations Dev_fops={.owner = this_module};

Information describing the device file

Minor: Secondary device number Misc_dynamic_minor,: Dynamically generated secondary device number name: Device filename

fops:file_operations struct variable pointer

static struct Miscdevice Misc={.minor = Misc_dynamic_minor,. Name=device_name,.fops = &dev_fops};

Initializing Linux drivers

static int word_count_init (void)

Uninstalling Linux Drivers

static void Word_count_exit (void)

Because kernel-space programs do not have direct access to data in user space, you need to use Copy_to_user and Copy_ respectively in Word_count_read (read data from device files) and Word_count_write (write data to device files) functions The From_user function copies data from the kernel space to the user space or from the user space to the kernel space

(iii) loading and unloading drive

Check if the Word_count drive is fully operational

#dmesg | Tail-n 1

#modinfo Word_count.ko

Detecting dependencies for Linux driver modules

#depmod/root/drivers/ch06/word_count/word_count.ko

Calling commands to load Linux drivers

#modprode Word_count

Note: Both the Insmod and Modprode commands are load-driven, which can check the dependency of the driver module

Three, many ways to test the Linux driver

(i) Testing Linux drivers with Ubuntu Linux

Need to write a program specifically for testing

(ii) test Linux drivers with native C programs on the Android emulator

(iii) using the Android NDK test

(iv) Use of Development Board testing

(v) Compiling the driver into the Linux kernel for testing

Iv. developing Linux drivers in eclipse

(i) Development of Linux drivers in Eclipse

The first step: Build C Engineering

Step Two: Establish C source code file link New>source folder folder name Input src, import word_count.c file

Step three: Set include path click menu Items properties,c/c++general > Paths and Symbols, select the GNU C entry for include, add two paths:/root/kernel/goldfish /include and/root/kernel/goldfish/arch/arm/include

Fourth step: Compiling Linux drivers

(ii) Testing Linux drivers in Eclipse

First step: Import the Test_word_count.c file

Step two: Set the Include path

Step three: Set Target make target > Create, enter Word_count_eclipse_test in the Target Name text box, click OK

Fourth step: Build Project Make Target > Build, select the file created in the third step, then click Build

Fifth step: Running the test program run as > Local C + + application

http://www.cnblogs.com/JOKERLAU/

Sixth. First Linux driver: Count the number of words

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