Linux device driver base

  

device driver is the interface between the operating system
kernel and machine hardware, the device driver shields the hardware details of the application, so that in the application view, the hardware device Just a device file, an application can operate on a hardware device just like a normal file. The device driver is part of the kernel. Its main functions are: initializing and releasing the device; transferring data from the kernel to the hardware and reading data from the hardware; reading the data transmitted by the application to the device file, and returning the application The requested data and errors in the detection and processing equipment.


Linux divides devices into two basic categories: one is a character device and the other is a block device. The main difference between a character device and a block device is that the actual hardware I/O typically occurs immediately after a read/write request is made to the character device. Character devices perform sequential read and write operations in a single byte, usually without buffering; block devices are stored and read and written with fixed-size blocks, such as hard disks, floppy disks, etc., and use a system memory as a buffer. Area. In order to improve efficiency, the system provides a caching mechanism for reading and writing of block devices. Due to problems involving buffer management, scheduling, and synchronization, it is much more complicated to implement than character devices. The LCD is accessed and managed as a character device. Linux treats the display driver as a character device and sends the data to be displayed to the LCD driver byte by byte.


Linux device management is closely integrated with the file system. Various devices are stored in the /dev directory as files, called device files. Applications can open, close, and read and write these device files to complete the operation of the device, just like working with normal data files. In order to manage these devices, the system has numbered the devices, and each device number is divided into a primary device number and a secondary device number. The primary device number is used to distinguish between different types of devices, while the secondary device number is used to distinguish multiple devices of the same type. For common devices, Linux has a custom number, such as the hard disk's major device number is 3. Linux provides a uniform operational function interface for all device files by using the data structure struct file_operations. This data structure includes a number of pointers to the operation functions, such as open (), close (), read () and write (), but because of the variety of peripherals, the operation is different. The members of the Struct file_operations structure are a series of interface functions, such as read/write functions for read/write and ioctl for control. Opening a file is to call the open operation in this file file_operations. Different types of files have different file_operations member functions, such as ordinary disk data files, interface functions to complete disk data block read and write operations; and for various device files, the final call to the I /O function in the respective driver for specific devices Operation. In this way, the application does not have to consider whether the device is operating or a normal file, and can be treated as a file, with a very clear and unified I/O interface. So file_operations is a file-level I/O interface.

Second Started to write

Used the method of adding comments in the code, and uploaded several files at the same time, like friends can download as a template. Each file is separated by ==

A total of 3 files, 1 driver header file, 1 driver file, and a driver test program file

are test.h. , test.c and ledtest.c

Simply talk about what the driver does, how to do it

1 System load driver

2 Open the device (file) in the application< Br>

3 Application to device operation

4 Application shutdown device (file)

5 System shutdown device

How does the application operate on the device?

Remember how to write files in C language? It is very similar here. For general character devices (also block devices, network devices, etc.) there are mainly 3 functions (there are many, you can see) llseek read: write: ioctl: Here only ioctl: control function, of course, can also be used to read and write The function operates on the IO port, but ioctl: seems to be more suitable.

The specific implementation can look at the ledtest.c file.

There are mainly several functions in test.c that are responsible for initialization and clearing, opening and closing. And ioctl writes some data to the serial port register.

Initialization and cleanup, there is a main sentence in the function that opens and closes, and comments have been made separately. Just remember it.

The register operation is not alone, you need to look at the 44B0 data sheet. Ok, let's look at the rest of the code.

======================================================== ==test.h========================================================= =========

/********************************** ******Copyright (c)**************************************** **********

** FREE

**

**--------------File Info-------------------------------

** File Name: config.h

** Last modified Date: 2006-9-9

** Last Version: 1.0

** Descriptions: User Configurable File

**

**---------------------------------------------- ------

** Created By: ZLG CHENMINGJI

** Created date: 2006-9-9

** Version: 1.0

** Descriptions: First version

**

**-------------------------- -----------------------

** Modified by:MAMAJINCO

** Modified date:2006-9- 9

** Version:1.0

** Descriptions: I am grateful to ZLG for my template. My high-quality programming consciousness originated here

**

************************************************ *****/

//Prevent duplicates from containing this file Set macro

#ifndef __CONFIG_H

#define __CONFIG_H

//Include the necessary header files

#i nclude

# i nclude //module must contain the header file

# i nclude /* printk () function, used to output information to the kernel * /

# i nclude /* very important which contains Structure such as file_opration This structure is used for file layer interface */

#i nclude /* error code*/

#i nclude

#i nclude

#i nclude

#i nclude

/***************************** ***/

/* Application Configuration*/

/************************** ******/

//The following changes are needed as needed

//Define the main device number device name

#define LED_MAJOR_NR 231 //231~239 240~ 255 can be

#define DEVICE_NAME "led" /* name for messaging */

#define SET_LED_OFF 0

#define SET_LED_ON 1

# Endif

/************************ End Of File

********* ************************************************/< Br>

=============END========================================= ======== ============

============test.c================= Br>

================================================ *****Copyright (c)**************************

Copyright © Windows knowledge All Rights Reserved