Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Note processing of Advanced character device drivers for Linux Kernel device drivers

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

/ * Advanced character device driver * /

(1) ioctl

In addition to reading and writing to devices, most drivers require the ability to perform various types of hardware control through device drivers. For example, eject medium, change baud rate and so on. These operations are supported by the ioctl method, which implements a system call with the same name.

In user space, the prototype of the ioctl system call is:

Int ioctl (int fd, unsigned long cmd,...); fd: open device file descriptor cmd: command third argument: can be an integer or pointer or not, depending on the command. Adopt "." Is only used to prevent the compiler from reporting errors.

There are some differences between the ioctl method prototype of the driver and the user space version:

Int (* ioctl) (struct inode * inode)

Struct file * filp

Unsigned int cmd

Unsigned long arg)

Inode/filp: the fd corresponding to the user space

Cmd: corresponding to the cmd from user space

Arg: corresponds to the passed cmd parameter

Most ioctl implementations include a switch statement that selects the corresponding operation based on the cmd parameter. The command numbers in user space and kernel space should be the same.

(2) Select the command number of ioctl

Before you write the code for ioctl, choose the number that corresponds to the different commands. You can't simply choose a number from 0 or 1, because linux requires that the command number should be unique system-wide. The linux kernel uses a conventional method to select the ioctl number for the driver. You can refer to include/asm/ioctl.h and Documentation/ioctl-number.txt.

An ioctl number is 32 bits, and linux divides it into four parts. The macros needed to build an ioctl number are defined in:

Type 8-bit magic number. It's all about choosing a number for your driver. Refer to the ioctl-number.txtnumber 8-digit ordinal. Direction 2. The transmission direction of data is defined. For example, _ IOC_NONE (no data transfer), _ IOC_READ | _ IOC_WRITE (bidirectional data transfer). Note that this direction is for the user, so IOC_READ means reading data from the device, and the driver should write data to user space. Size 14. The size of user data involved.

You can use macros in to construct an ioctl number.

_ IO (type,nr) _ IOR (type,nr,datatype) _ IOW (type,nr,datatype)

Return value

For a system call, a positive return value is first protected, while a negative value is considered an error and is used to set the user space error variable. If an undefined ioctl number is passed in when calling the ioctl method, the error values returned by the system are-ENVAL and-ENOTTY

(3) blocking and non-blocking operation

For operations such as read and write, the default operation is blocking, which is characterized by:

* if a process calls read but has no data to read, the process must block. The process wakes up when the data arrives and returns the data to the caller, even if the number of data is less than that specified by the count parameter.

* if a process calls write but there is no space in the buffer, the process must be blocked and must sleep on a different wait queue than the read process. When some data is written to the hardware device, thus freeing up part of the output buffer, the process is awakened and the write call is successful.

Sometimes we want to change this feature to non-blocking so that the read/write method returns immediately regardless of whether the device has data to read or write.

If you want to set a file to be nonblocking, you should set the O_NONBLOCK flag of filp- > f_flags. Applications must be very careful when calling the stdio function when dealing with non-blocking files, because it is easy to mistake a non-blocking return for EOF, so the errno must always be checked.

(4) Asynchronous notification

a. The role of asynchronous notification

Most of the time, the combination of blocking and non-blocking operations and the select method can effectively query the device, but sometimes it is not efficient to use this technique. In the face of some random or rare situations (such as typing CTRL+C through the keyboard), asynchronous notification (asynchronous notification) is required.

b. How does the user space program start asynchronous notification

To start the asynchronous notification mechanism for files, the user program must perform two steps:

01. Specify a process as the owner of the device file. When the process executes the F_SETOWN command using the fcntl system call, the process ID number of the host process is saved in filp- > f_owner. This step is necessary to let the kernel know who to notify. 02. In order to actually start the asynchronous notification mechanism, the user program must also set the FASYNC flag on the device, which is done through the fchtl command F_SETFL. After performing these two steps, the device file can request a SIGIO signal when the new data arrives. This signal is sent to the process stored in file- > f_owner (if it is negative, it is the process group). Not all devices support asynchronous notification, and applications usually assume that only sockets and terminals have asynchronous notification capabilities.

(5) how to implement asynchronous notification in driver

a. Correspondence of user space operations in the kernel

01. When F_SETOWN is set, assign 02. 0 to file- > f_owner. When F_SETFL is executed to start FASYNC, the driver's fasync method is called. This method is called whenever the FASYNC flag in filp- > f_flags (which is cleared by default when the file is opened) changes. 03. When the data arrives, the kernel sends a SIGIO signal to all processes registered as asynchronous notifications

b. Add a pointer to the fasync_struct in the device structure

The structure is defined in:

Struct fasync_struct {int magic;int fa_fd;struct fasync_struct * fa_next;struct file * fa_file;}

c. Drive the two functions to be called

These two functions are declared in.

Defined in / fs/fcntl.c.

The prototype is as follows:

01. Int fasync_helper (int fd, struct file * filp, int mode, struct fasync_struct * fa); 02. Void kill_fasync (struct fasync_struct * * fa, int sig, int band)

When the FASYNC flag of an open file is modified, fasync_helper is called to add or remove files from the list of related processes, and kill_fasync notifies all relevant processes when the data arrives.

d. Examples

01. Define fasync_struct dynamic data structures in device types

Struct my_pipe {struct fasync_struct * async_queue; / * Asynchronous read structure * /.}

02. The fasync function in the driver calls fasync_helper

Int my_fasync (fasync_file fd, struct file * filp, int mode) {my_pipe * dev = filp- > private_data; return fasync_helper (fd, filp, mode, & dev- > async_queue);}

03. Call kill_fasync when the asynchronous notification condition is met

What is notified asynchronously is a read process, so send the kill_fasync using write.

Call kill_fasync to send a signal SIGIO to all processes in the asynchronous queue async_queue registered on the device.

Ssize_t my_write (struct file * filp, const char * buf, size_t count, loff_t * f_pos) {.if (dev- > async_queue) kill_fasync (& dev- > async_queue, SIGIO, POLL_IN);.}

04. The fasync method must be called when the file is closed

The fasync method must be called when the file is closed to remove the file from the list of active asynchronous read processes.

Call in release: scull_p_fasync (- 1, filp, 0)

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report