Linux 2 6 38中ioctl的變化

2021-06-05 12:29:10 字數 4765 閱讀 4687

linux驅動程式剛接觸,雖然不是很清楚,但是總歸是慢慢學習的過程。我的環境是fedora14虛擬機器。核心版本是2.6.38.1,其中的實現過程存在很多的問題,主要是因為很多的核心函式發生了較大的差別.其中最大的可能是ioctl以及互資訊量的實現。這兩個的問題也使得我們在驅動設計過程中出現很多的疑惑和問題。

接上一部分,繼續總結:

主要包括幾個重要的結構體、併發控制、以及ioctl的實現。在驅動的設計過程主要涉及3個重要的結構體。struct file_operations,struct inode,struct file.

struct file_operations主要是涉及一些檔案操作的函式,其本質上就是乙個函式指標的集合,包含了檔案操作的各種函式宣告,可能與應用程式設計中的相應函式只在引數上存在一定的差別。但是在2.6.36版本以後,其中的內容發生了較大的變化,主要設計了ioctl的相關操作。

struct file_operations ;

long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

long (*compat_ioctl) (struct file *, unsigned int, unsigned long);

是最近新增進來的函式,為了實現原來的ioctl函式,同時引數以及返回值都發生了較大的變化,這也是為什麼在2.6.36版本以後的核心中使用ioctl函式會報錯的原因。unlocked_ioctl函式通常用來實現原來的ioctl函式,而compat_ioctl函式則用來實現一些相容版本的ioctl問題。返回值由原來的int變為long型,也是需要注意的。在ioctl中,第乙個引數是struct inode,2.6.36以後的版本將不能直接訪問到inode引數,只能間接的訪問,具體的訪問方法後面在總結。

在驅動實現過程中主要包括對各個需要實現函式的賦值,但是open函式不能賦值與否,都會預設開啟,如果不賦值,則預設該裝置一直開啟。其他的函式不賦值,即表示不實現該方法。常用的複製方法如下:

/*新增該模組的基本檔案操作支援*

/static const struct file_operations mem_fops =;

需要注意的是後面不再是分號,而是逗號。其中的mem_read、mem_write等是函式的具體實現過程。.owner表示該結構體屬於那個,當然就是this_module,表示這個模組。

struct inode表示的是乙個檔案的索引,該結構是每乙個具體的物理檔案(儲存在儲存器中的實體檔案)的索引,乙個檔案對應乙個唯一的struct inode,其中表明了檔案的大小,檔案的型別,檔案的時間等引數,結構體中每乙個引數都能表示某乙個檔案的特性,通過inode就能表示檔案的所有資訊。

struct inode 

;unsigned long        i_ino;

/*引用次數,當這個數為0時,release函式才能完成*/

atomic_t        i_count;

unsigned int        i_nlink;

/*裝置檔案的裝置號*/

dev_t            i_rdev;

unsigned int        i_blkbits;

u64            i_version;

/*檔案偏移量*/

loff_t            i_size;

#ifdef __need_i_size_ordered

seqcount_t        i_size_seqcount;

#endif

/*檔案的時間引數,包括三種時間*/

struct timespec        i_atime;

struct timespec        i_mtime;

struct timespec        i_ctime;

blkcnt_t        i_blocks;

unsigned short i_bytes;

struct rw_semaphore    i_alloc_sem;

const struct file_operations    *i_fop;

/* former -

>i_op-

>default_file_ops *

/struct file_lock    *i_flock;

/*檔案的備份位址空間*/

/*裝置位址空間*/

struct address_space    i_data;

#ifdef config_quota

struct dquot        *i_dquot[maxquotas]

;#endif

struct list_head    i_devices;

/*說明了三種不同的驅動型別*/

union 

;__u32            i_generation;

#ifdef config_fsnotify

__u32            i_fsnotify_mask;

/* all events this inode cares about *

/struct hlist_head    i_fsnotify_marks;

#endif

#ifdef config_ima

/* protected by i_lock *

/unsigned int        i_readcount;

/* struct files open ro *

/#endif

/*寫者使用次數*/

atomic_t        i_writecount;

#ifdef config_security

void            *i_security;

#endif

#ifdef config_fs_posix_acl

struct posix_acl    *i_acl;

struct posix_acl    *i_default_acl;

#endif

void            *i_private;

/* fs or device private pointer */}

; 驅動程式設計過程中通常

採用i_rdev判斷裝置檔案的裝置號。

struct file是指檔案物件,表示程序中開啟的檔案,乙個物理檔案只有乙個inode,但是可以被開啟很多次,因此可以存在很多struct file結構體。

struct file  f_u;

/*檔案的路徑*/

struct path        f_path;

#define f_dentry    f_path.dentry

#define f_vfsmnt    f_path.mnt

/*該檔案支援的操作集合*/

const struct file_operations    *f_op;

spinlock_t        f_lock;

/* f_ep_links, f_flags, no irq *

/#ifdef config_smp

int            f_sb_list_cpu;

#endif

/*檔案物件的使用次數*/

atomic_long_t        f_count;

unsigned int         f_flags;

fmode_t            f_mode;

loff_t            f_pos;

struct fown_struct    f_owner;

const struct cred    *f_cred;

struct file_ra_state    f_ra;

u64            f_version;

#ifdef config_security

void            *f_security;

#endif

/* needed for tty driver,

and maybe others *

//*通常用來指向具體的資料或者裝置檔案,實現操作*/

void            *private_data;

#ifdef config_epoll

/* used by fs/eventpoll.c to

link all the hooks to this file *

/struct list_head    f_ep_links;

#endif /

* #ifdef config_epoll *

/#ifdef config_debug_writecount

unsigned long f_mnt_write_state;

#endif};

訪問inode主要是通過這兩個機構體之間的管理型。

struct path ;

struct dentry  d_u;

struct list_head d_subdirs;

/* our children *

/struct list_head d_alias;

/* inode alias list */}

; 其中的可以通過struct file間接的訪問物理檔案的struct inode,具體的實現是filp->f_path.entry->d_inode,這個過程也就實現了將inode和file結構體之間的聯絡。

上面的幾個主要的結構體是驅動實現過程中最重要的幾個。具體的意義還要聯絡起來分析。

Linux 2 6 38的LCD驅動分析(一)

一 讓lcd顯示可愛的小企鵝 還是先說說環境吧,處理器為s3c6410,linux的版本是2.6.38 友善之臂提供的 下面先說說怎樣讓lcd上顯示出可愛的小企鵝。最直接的步驟如下 記住不要問為什麼哈 一步一步跟著走就行了 1.新增s3c6410處理器的lcd控制暫存器的初始值,具體做法為在檔案ar...

Linux 2 6 38的LCD驅動分析(三)

三 解剖s3cfb driver變數 s3cfb driver變數有什麼作用呢?在前面的2.2節提到了它的定義,從它的原型可以看出s3cfb driver是個platform driver型別的變數,前面的幾個小節提到了從platform driver的名字可以看出它應該是platform devi...

Linux系統中ioctl 用法

一 什麼是ioctl。ioctl是裝置驅動程式中對裝置的i o通道進行管理的函式。所謂對i o通道進行管理,就 是對裝置的一些特性進行控制,例如串列埠的傳輸波特率 馬達的轉速等等。它的呼叫個數 如下 int ioctl int fd,ind cmd,其中fd就是使用者程式開啟裝置時使用open函式返...