Development of device drivers in SCOUNIX

Source: Internet
Author: User
In SCOUNIX, the development of Device Drivers-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. Related Concepts
---- 1 device number and Device Driver
---- The device number is a number that indicates the device. These devices have special files on the system. The device number consists of the master device number and sub-device number. The high byte is the master device number and the bottom byte is the sub-device number. the main device ID indicates the type of the device. It indicates the core driver to process the request from the device. The next device ID is explained by the corresponding device driver, it indicates a specific physical device.
---- The main device Number of the X. 8 serial card is the same, and the secondary device number is different.

---- 2 device Node

---- Unix processes a device as a special file, called a device file, that is, a device node. The device node provides interfaces for physical devices and their corresponding drivers. 0. The command format for adding a device node is

---- Mknod device name [B/c] device Number of the master device number

---- It will generate a directory item and corresponding I node for the device file.

---- 3 Device Driver

---- A device driver is a software interface between a physical device and an operating system. It consists of a group of subprograms. It is responsible for communicating with physical devices and providing unified interfaces for the core of the operating system. User-level programs access devices through device files, but the actual device reads and writes are completed by the device driver.

---- Device Drivers fall into two categories: character device drivers and block device drivers.

Ii. Design and Development of device drivers for UNIX systems
---- 1 The driver provides core routines:
---- Xxxinit (), xxxxopen (), xxxxclose (), xxxxread (), xxxxwrite (), xxxxioctl (), xxxxhalt (); and so on. Here, xxxx is the prefix of the device driver, which is the same as the device name. In this example, It is mytt. This group of routines is the device driver entry routine. The following describes each routine:

---- Xxxxinit: initializes the device, which is automatically called by the core at startup.

---- Xxxxhalt: Core call to disable the device when the system is disabled

---- Xxxxopen: obtain access to a device. When a user process calls the file system to call open, the core calls this routine.

---- Xxxxclose: cancels access to a device. When a user process calls the file system routine close, the core calls this routine.

---- Xxxxread: reads data from a device. When a user process calls the file system routine read, the core calls this routine to transmit data from the system address space to the user address space.

---- Xxxxwrite: write data to the device. When a user process calls the file system routine write, the core calls this routine to transmit data from the user address space to the system address space, and send it to the physical device.

---- Xxxxintr: Process device interruptions. When a device sends an interruption request to the system, the core calls this routine to process the interruption.

---- Xxxxstart: starts access to a driver, which is also automatically called by the core during system startup. The difference with the xxinit routine is that the xxinit routine is used by the core at the beginning of the system, and the call of the xxstart routine is called after the system interruption system has been initialized.

---- Xxxxioct1: Controls character devices. When a user's process calls the file system to use ioct1, the core calls this routine.

---- These routines are optional.

---- 2 parameter passing of the driver:

---- The system core allocates and maintains a core data structure USER for each process, which contains all the information required for running the process. The variable u is the USER data structure of the current active process. It contains only the core private information, and the driver will frequently use many of these elements, which are commonly used:

---- U. u_base: the address of the user data zone, which contains the read/write data to be transmitted. When a user calls the file read/write system function, the address parameter exists in this unit.

---- U. u_offset: Start address of the transmitted data file

---- U. u_count: contains the number of transmitted bytes.

---- U. u_error: value of the global variable errno returned by the file system call

---- Data in the user data zone is transmitted through core routines, passc (), cpass (), copyin (), copyout (), passc (), cpass () the u structure can be automatically maintained. copyin () and copyout () must be manually maintained.

---- 3 Driver debugging:

---- 1) print the variables to be observed through the printf statement.

---- 2) write your own test program debugging, a simple program for reading and writing terminal Devices

---- 3) When a core error occurs, you can use crash

# Unload memory image
# Ldsysdump filename
# Crash-d filename

---- 4 install the device driver:

---- Install the device driver by following the steps below:

---- 1) obtain the available master device number. The command is:

/Etc/conf/cf. d/configure-j NEXTMAJOR
Back to-master device number, such as 120.

---- 2) create a device file node and run the following command:
Mknod/dev/mytt c 120 0
"/Dev/mytt" refers to the device file I have made, "c"
It refers to the character device. "120" indicates the master device number, and "0" indicates the secondary device number.

---- 3) configure the system with the command:

Cd/etc/conf/cf. d
./Configure-m 120-c-14-T2-v
4-a myttinit myttopen myttclose \
Myttread myttwrite myttioct1

Option:
L4 interrupt priority
T2 device interruption mode (0-3)
V4 requires Phase Break


---- 4) check whether the device mytt has been added to the system. The command is:

./Configure-j mytt

---- If 120 is returned, mytt is added to the system and the master device is 120. If the returned message is "no sueh device", there is no device named mytt in the system. The above command can also check whether a device with the same name exists in the system before configuring the system. If yes, delete the device and then add it to the new device. To delete a specified device, you must know its primary device number. Take the master device number command as shown in the preceding steps:

./Configure-j mytt
./Configure-m 120-c-d
Then, the device mytt is deleted from the core.


---- 5) Compile the driver source code and upload the generated. o file to the/etc/conf/pack. d directory.

---- Compile the driver source code command line:

Cc-c-D_INKERNEL mytt. c

Mkdir/etc/conf/pack. d/mytt

Cp mytt. o/etc/conf/pack. d/mytt/Driver. o


---- 6) duplicate key core

Cd/etc/conf/cf. d

./Link_unix


---- To answer two Y s, a new core unix is generated under the root directory, and the old core file is saved as unix. after the o1d driver has been installed, You need to restart the unix system to make the device driver effective. Then, the system automatically runs myttinit () and displays information on the console, indicating that the device driver has been installed.
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.