Three important data structures driven by linux character devices

  
 

The three important data structures of the linux character device driver are

struct file

struct inode

strude file_operations

where struct file represents a The open file, struct inode, is used to record the physical information of the file. So for the time being, you can ignore it.

strude file_operations is a collection of function pointers. The following is a description of the GPIO driver and application examples:

The first part is the driver:

//------------------- OPEN ------------------------ssize_t SIMPLE_GPIO_open (struct Inode * inode , struct file * file){ #ifdef OURS_GPIO_DEBUG printk ("SIMPLE_GPIO_open [ --kernel--]\ "); #endif return 0;}

//------ ------------- RELEASE/CLOSE ---------------ssize_t SIMPLE_GPIO_release (struct inode * inode , struct file * file){ #ifdef OURS_GPIO_DEBUG printk ( "SIMPLE_GPIO_release [ --kernel--]\ "); #endif

return 0;}

//--------------- ----------------------------------struct file_operations gpio_fops ={

.open= SIMPLE_GPIO_open, .read= SIMPLE_GPIO_read, .write= SIMPLE_GPIO_write, .ioctl= SIMPLE_GPIO_ioctl, .release= SIMPLE_GPIO_release, };

Defined as struct file_operations in gpio_fops , for example .open = SIMPLE_GPIO_open, which defines the open function of the application to actually call the SIMPLE_GPIO_open function.

Look at the ssize_t defined in the driver SIMPLE_GPIO_open (struct inode * inode, struct file * file)

where the parameter is another The two data structures are ignored here. (Do not write parameters or anything)

Review the call in the application

#define DEVICE_NAME "/dev/gpio"

int main(void){ int fd ; int ret; char *i;

printf("\ start gpio_led_driver test\ \ ");

fd = open(DEVICE_NAME, O_RDWR); printf("fd = %d\ ",fd);

if (fd == -1) { printf("open device %s error\ ",DEVICE_NAME); }


The open function argument of the application call is open(DEVICE_NAME, O_RDWR);

where #define DEVICE_NAME "/dev/gpio" , O_RDWR means to open and write

In the driver, the xxxopen function can be written according to the book, the parameters can be completely ignored (the function body can write the content, I don't know what to do)

The open function parameter of the application According to the parameters written in the linux application book,

Copyright © Windows knowledge All Rights Reserved