Some simple device driver functions

Source: Internet
Author: User

Author: Cao Dongyang,Hua Qing vision embedded college lecturer.

Open () function
Function: open a device file.
Prototype: # include
# Include
# Include
Int open (const char * pathname, int flag );

Note: use the property specified by flags to open the device file that represents the specified character on pathname. Generally, the specified location on the pathname is the device file in the "/dev/" directory.

Variable: pathname specifies the device file character address

Flags specifies properties close to the device file:
_ O_rdonly open a file in read-only mode
_ O_wronly open the file in write-only mode
_ O_rdwr open a file in readable and writable Mode
_ O_nocty if the file to be opened is a terminal device, the terminal will not be treated as a process control terminal.
_ O_nonblock open the file in an unblocking way, that is, whether there is any data read or wait, it will immediately return to the Process
_ O_ndelay
_ O_sync open the file in synchronous mode. Before the content written on the device is recorded as hardware, the call process is in the blocking status.

Returned value: if the file is successfully opened, the file descriptor is returned. If the file fails, the value-1 is returned. If all permissions to be verified have passed the check

0 is returned, indicating that the operation is successful. If one permission is disabled,-1 is returned. When you get the-1 value, you can refer to errno to determine the value returned by the actual device driver.

Error code:
-The enxio file is a device file, but there is no corresponding Device
-Enodev does not have the device driver or hardware related to the device file.
-Insufficient enomem core memory

Close () function

Function: Disable device files.

Prototype: # include
Int close (int fd );

Note: To open a device file, disable the file descriptor FD corresponding to the file returned by the open () function.

Variable: file descriptor returned by FD open () function running result

Return Value: 0 is returned for successful shutdown, and-1 is returned for failure.

Read () function

Function: reads data from device files.

Prototype: # include
Ssize_t read (int fd, void * Buf, size_t count );

Note: The read () function transfers the device file pointed to by the parameter FD to the memory indicated by the Buf pointer in count bytes. In this case, the Count value should be less than ssize_max. When the open () function is not specified as o_nonblock or o_ndelay, the corresponding Count value can be read. When the device driver of the device file does not reflect o_nonblock or o_ndelay, the corresponding options are not specified and may be blocked. In principle, this is the wrong device driver. When creating a program, you must compare the above conditions. Therefore, you must confirm the returned results. In addition, the file read/write location will move with the size of the read byte.

Variable: FD descriptor returned by the open () function running result

Buf stores the location where data is read. The bucket should be larger than the Count byte
The data size read from the count device file. The value must be smaller than ssize_max. The return value is 0. terminate the operation immediately.

Returned value: the number of bytes read after the device file normally reads data. It is not an error even if the value is smaller than the corresponding number of bytes. There is almost no actual number of bytes available or the signal is interrupted. If it fails,-1 is returned. If the value-1 is returned, see errno. You can confirm the return value in the actual device driver.

Error code:

-Eintr: The call is interrupted by the signal.
-Eagain: When a file is opened using an unblockable (o_nonblock), read calls unreadable data.
-An input/output error occurs when the EIO device file reads data.
-The ebadf parameter FD is not a valid file descriptor, or the file is disabled.
-Einval FD is connected to an object that is not properly read.
-The efault parameter Buf is an invalid pointer and points to a space that cannot exist.

Write () function

Function: write data to a device file.

Prototype: # include
Ssize_t write (int fd, const void * Buf, size_t count );

Note: The write () function writes the Count bytes in the memory referred to by the parameter Buf to the file referred to by the parameter FD. In this case, the Count value should be less than ssize_max. When the open () function is not specified as o_nonblock or o_ndelay, the corresponding Count value can be read. When the device driver of the device file does not reflect o_nonblock or o_ndelay, the corresponding options are not specified and may be blocked. In principle, this is the wrong device driver. When creating a program, you must compare the above conditions. Therefore, you must confirm the returned results. The position of the file pointer will move the corresponding number of bytes.

Variable: FD descriptor returned by the open () function running result

Buf stores the location where data is written. The bucket referred to by this address must be larger than the Count byte.
The size of data to be written to the Count device file. The value must be smaller than ssize_max. If the return value is 0, the request is immediately interrupted.

Returned value: the number of bytes written to the device file after the data is normally written. It is not an error even if the value is smaller than the required number of bytes. There may be no actual number of bytes written, or some signal is interrupted. If the device fails,-1 is returned. If the value-1 is returned, see errno. You can check whether the returned value is in the actual device driver.

Error Code

-The ebadf parameter FD is not a valid file descriptor, or the file is not writable.
-Einval FD: connect to the object not suitable for writing
-The efault parameter Buf is an invalid pointer and points to a space that cannot exist.
-Although eagain opened the file with an unblocking (o_nonblock), it is not in the status that can be processed directly after the read call.
-Before eintr finishes writing data, this call is interrupted by signals.
-No data space exists on the device where enospc contains FD files.
-An error occurred while writing data to an EIO device file.

Lseek () function

Function: Move the read/write location of a file.

Prototype: # include
# Include
Off_t lseek (int fd, off_t offset, int whence );

Note: The lseek () function is used to control the read/write location of the file. Move the device file specified by file descriptor FD to the file pointer to the offset value specified by whence. The location of the file pointer varies with the processing method of the device driver connected to the device file. For example, device files that manage memory can use the memory location. However, most character device drivers do not use this function.

Variable: the offset value returned by the open () function running result is in bytes, specifying the position of the pointer to the file to be moved. This value is interpreted as the actual moving position along with whence. Whence specifies the condition used to explain the offset.
-The seek_set parameter offset is the new read/write location.
-Seek_cur: Increase the offset displacement at the current read/write position.
-Seek_end: points the read/write position to the end of the file and increases the offset displacement. Most devices have vague definitions at the end of a file. This value is usually not used.

Return Value: when the call is successful, the current read/write location is returned, that is, the number of bytes from the beginning of the file. If an error occurs, return (off_t)-1, and errno stores the error code. After obtaining the normal Moving position of the file pointer, the actual moving position is returned. If it fails,-1 is returned. When the value-1 is obtained, the value returned by the actual device driver can be determined by referring to the global variable error value.

Error Code

-The value specified by einval whence is not suitable.

IOCTL () function

Function: Controls device files.

Prototype: # include
Int IOCTL (int fd, int request ,...);

Note: The IOCTL () function implements the read () and write () functions on the device file corresponding to the file descriptor FD for hard-to-complete input/output processing. Except FD, each variable in this function has no standard value. Knowledge specifies the criteria defined by several macro values. Different device files have different values. Variable of IOCTL () function. Although it can be expressed in syntax, it can contain up to three. The third factor indicates char * argp;

Variable: The request descriptor returned by the open () function running result defines the command that the device driver connecting to the device file should call. The macro determines whether a command is input or output. When the value specified by argp is used as the storage address, the transmission factor is displayed in bytes... the third variable is called argp. It is an ignored variable. It is related to the request. It is the auxiliary information returned for processing the request command. When the call is successful, 0 is returned and-1 is returned. When the value-1 is obtained, the value returned by the actual device driver can be determined by referring to the global variable error value.

Error Code

-The efault parameter argp is invalid and points to the memory space that cannot exist.
-Enotty FD is irrelevant to the character Device File
-The device driver that connects the device file with einval cannot process the request or argp.

Recommended reading:

1. Hua Qing VISION enterprise college free lecture: http://www.farsight.com.cn/lecture/index.aspx

2. Tsinghua VISION enterprise college download center: http://download.farsight.com.cn/download/

3. Hua Qing vision embedded school free classroom: http://www.embedu.org/Lecture/

4. Hua Qing vision 3g College free discount: http://www.3g-edu.org/free/

5. Hua Qing vision data request:

· The Android multimedia programming from beginner to proficient (Chapter 1-6) e-book, request address: http://www.3g-edu.org/news/materials.htm

· "Embedded Linux application development standard tutorial" (2nd) and "Linux device driver development details" ebook, request address: http://www.embedu.org/news/materials.htm

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.