歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux字符設備驅動的三個重要數據結構

linux字符設備驅動的三個重要數據結構

日期:2017/3/2 9:46:44   编辑:關於Linux

linux字符設備驅動的三個重要數據結構分別是

struct file

struct inode

strude file_operations

其中 struct file 代表一個打開的文件,struct inode 用來記錄文件的物理上的信息。所以暫時可以不去理會

strude file_operations 是一個函數指針的集合,下面結合GPIO驅動和應用程序實例說明:

首先是驅動程序的部分

// ------------------- OPEN ------------------------
ssize_t SIMPLE_GPIO_open (struct inode * inode ,struct file * file)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_open [ --kernel--]\n");
#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--]\n");
#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,

};

gpio_fops 定義為struct file_operations ,例如.open= SIMPLE_GPIO_open,就是定義了應用程序的open函數實際調用SIMPLE_GPIO_open函數

驅動程序中定義ssize_t SIMPLE_GPIO_open (struct inode * inode ,struct file * file)

其中參數就是另外的兩種數據結構,在此我們不理會。(不用寫參數什麼的)

再來看應用程序中的調用

#define DEVICE_NAME "/dev/gpio"

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

printf("\nstart gpio_led_driver test\n\n");

fd = open(DEVICE_NAME, O_RDWR);

printf("fd = %d\n",fd);

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

應用程序調用的open函數參數為open(DEVICE_NAME, O_RDWR);

其中#define DEVICE_NAME "/dev/gpio" ,O_RDWR表示以讀寫方式打開

總的來說,驅動程序中的xxxopen函數按照書上寫即可,參數可以完全不管(函數體可以寫點內容,具體些什麼我也不清楚)

應用程序的open函數參數之言按照linux應用程序書上寫的參數即可

Copyright © Linux教程網 All Rights Reserved