正在研究linux設備驅動程序,現(xiàn)在把平時的學習心得以筆記的形式發(fā)到博客上,方便跟同行們交流與討論!因為是初學者,對linux的認識還不夠深入,所以在博文中會有很多錯誤,我乃拋磚引玉,請大俠們指教!!
說明:博文的內(nèi)容主要參考好朋友Tekkaman Ninja同學博客http://blog.chinaunix.net/u1/34474/index.html上的文章。
linux驅動程序學習-字符設備驅動程序(第三章)
一、主設備號和次設備號
對字符設備的訪問是通過文件系統(tǒng)內(nèi)的設備名稱進行的。那些名稱稱為特殊文件、設備文件或者簡單稱之為文件系統(tǒng)樹的節(jié)點,它們通常位于/dev目錄下。
主設備號:標識設備對應的驅動程序 次設備號:標識確定設備文件所指的設備
同一個主設備號下有不同的從設備號,對應同一類驅動程序下的不同具體設備,如:同屬于字符設備的有控制臺和串口終端等。
注意理解:主設備號、次設備號、設備文件之間的關系!!
二、設備編號的內(nèi)部表達
內(nèi)核用dev_t類型(<linux/types.h>)來保存設備編號,dev_t是一個32位的數(shù),12位表示主設備號,20為表示次設備號。在實際使用中,是通過<linux/kdev_t.h>中定義的宏來轉換格式。
如果:想獲得主設備號或者次設備號,應使用:MAJOR(dev_t dev)--獲得主設備號 MINOR(dev_t dev)--獲得次設備號
如果:已知了主設備號與次設備號,想把他轉換成dev_t類型,則使用MKDEV(int major,int minor);
三、分配和釋放設備編號
在建立一個字符設備之前,驅動程序首先要做的事情是獲得一個或者多個設備編號。
有2種情況:一種是在已經(jīng)知道設備編號的情況下,調(diào)用函數(shù)分配;一種是先前不知道驅動所需的設備編號,調(diào)用函數(shù)去分配
第一種情況:調(diào)用函數(shù) int register_chrdev_region(dev_t first,
unsigned
int
count,
char
*name); //指定設備編號
第二種情況:調(diào)用函數(shù) int alloc_chrdev_region(dev_t *dev,
unsigned
int firstminor,
unsigned
int
count,
char
*name); //動態(tài)生成設備編號
釋放設備編號:void unregister_chrdev_region(dev_t first,
unsigned
int
count); //釋放設備編號
四、一些重要的數(shù)據(jù)結構:
設備編號的注冊是驅動程序代碼必須完成的許多工作中第一件事情而已,后面還有很多事情等著我們?nèi)プ瞿兀。〈蟛糠只镜尿寗映绦虿僮魃婕暗饺齻重要的內(nèi)核數(shù)據(jù)結構,分別是file_operations、file、inode。下面詳細闡述:
struct file_operations fops 設備驅動程序接口
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
};
上面結構體內(nèi)的每個字段大部分是函數(shù)指針,這些函數(shù)指針指向驅動程序實現(xiàn)具體操作的函數(shù)。我們可以看到上面的指針所指向函數(shù)的參數(shù)里面有一種結構體很常見:struct file 還有struct inode。
下面來分析struct file:file結構與用戶空間中的FILE沒有任何關聯(lián),struct file是一個內(nèi)核結構,他不會出現(xiàn)在用戶程序中。file結構代表一個打開的文件,是由內(nèi)核在open時創(chuàng)建的,并傳遞給在該文件上進行操作的所有函數(shù),直到最后的close函數(shù),在文件的所有實例都被關閉之后,內(nèi)核會釋放掉這個數(shù)據(jù)結構。在這個數(shù)據(jù)結構中有一個重要的字段:struct file_operations *f_op,內(nèi)核在執(zhí)行open操作時,對這個指針賦值,以后需要處理這些操作時就讀取這個指針。filp->f_op中的值決不會為了方便引用而保存起來,也就是說,我們可以在任何時候修改文件的關聯(lián)操作,在返回給調(diào)用者之后,新的操作方法立即生效。例如:對應于主設備號1的open代碼根據(jù)要打開的次設備號替換filp->f_op中的操作。注意:也就是說,struct file與struct file_operations這2個結構體是通過這樣的方式進行相關聯(lián)的。
inode結構:內(nèi)核用inode結構在內(nèi)部表示文件,因此它和file結構不同,后者表示打開的文件描述符。對于單個文件,可能會有許多個表示打開的文件描述符的file結構。但他們都指向單個inode結構。對于編寫驅動程序,只有2個字段比較常用:dev_t i_rdev; struct cdev *i_cdev;
struct cdev表示字符設備的內(nèi)核的內(nèi)部結構。當inode指向一個字符設備文件時,該字段包含了指向struct cdev結構的指針。
內(nèi)核內(nèi)部使用struct cdev結構來表示字符設備。在內(nèi)核調(diào)用設備的操作之前,必須分配并注冊一個或者多個上述結構。
注冊一個獨立的cdev設備的基本過程如下:
1、為struct cdev 分配空間(如果已經(jīng)將struct cdev 嵌入到自己的設備的特定結構體中,并分配了空間,這步略過!)
struct cdev *my_cdev = cdev_alloc();
2、初始化struct cdev
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
3、初始化cdev.owner
cdev.owner = THIS_MODULE;
4、cdev設置完成,通知內(nèi)核struct cdev的信息(在執(zhí)行這步之前必須確定你對struct cdev的以上設置已經(jīng)完成!)
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
從系統(tǒng)中移除一個字符設備:void cdev_del(struct cdev *p)
/*
* Set up the char_dev structure for this device.
*/
static
void scull_setup_cdev(struct scull_dev *dev,
int index)
{
int err, devno = MKDEV(scull_major, scull_minor + index);
cdev_init(&dev->cdev,
&scull_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops =
&scull_fops;
//這句可以省略,在cdev_init中已經(jīng)做過
err = cdev_add (&dev->cdev, devno, 1);
/* Fail gracefully if need be 這步值得注意*/
if
(err)
printk(KERN_NOTICE "Error %d adding scull%d", err, index);
}
scull模型的結構體:
/*
* Representation of scull quantum sets.
*/
struct scull_qset {
void
**data;
struct scull_qset *next;
};
struct scull_dev {
struct scull_qset *data;
/* Pointer to first quantum set */
int quantum;
/* the current quantum size */
int qset;
/* the current array size */
unsigned
long size;
/* amount of data stored here */
unsigned
int access_key;
/* used by sculluid and scullpriv */
struct semaphore sem;
/* mutual exclusion semaphore */
struct cdev cdev; /* Char device structure */