3.9. 快速參考

2018-02-24 15:49 更新

3.9.?快速參考

本章介紹了下面符號(hào)和頭文件. struct file_operations 和 struct file 中的成員的列表這里不重復(fù)了.

#include <linux/types.h>
dev_t 

dev_t 是用來在內(nèi)核里代表設(shè)備號(hào)的類型.

int MAJOR(dev_t dev);
int MINOR(dev_t dev);

從設(shè)備編號(hào)中抽取主次編號(hào)的宏.

dev_t MKDEV(unsigned int major, unsigned int minor);

從主次編號(hào)來建立 dev_t 數(shù)據(jù)項(xiàng)的宏定義.

#include <linux/fs.h>

"文件系統(tǒng)"頭文件是編寫設(shè)備驅(qū)動(dòng)需要的頭文件. 許多重要的函數(shù)和數(shù)據(jù)結(jié)構(gòu)在此定義.

int register_chrdev_region(dev_t first, unsigned int count, char *name)
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name)
void unregister_chrdev_region(dev_t first, unsigned int count);

允許驅(qū)動(dòng)分配和釋放設(shè)備編號(hào)的范圍的函數(shù). register_chrdev_region 應(yīng)當(dāng)用在事先知道需要的主編號(hào)時(shí); 對(duì)于動(dòng)態(tài)分配, 使用 alloc_chrdev_region 代替.

int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);

老的( 2.6 之前) 字符設(shè)備注冊(cè)函數(shù). 它在 2.6 內(nèi)核中被模擬, 但是不應(yīng)當(dāng)給新代碼使用. 如果主編號(hào)不是 0, 可以不變地用它; 否則一個(gè)動(dòng)態(tài)編號(hào)被分配給這個(gè)設(shè)備.

int unregister_chrdev(unsigned int major, const char *name);
恢復(fù)一個(gè)由 register_chrdev 所作的注冊(cè)的函數(shù). major 和 name 字符串必須包含之前用來注冊(cè)設(shè)備時(shí)同樣的值.

struct file_operations;
struct file;
struct inode;

大部分設(shè)備驅(qū)動(dòng)使用的 3 個(gè)重要數(shù)據(jù)結(jié)構(gòu). file_operations 結(jié)構(gòu)持有一個(gè)字符驅(qū)動(dòng)的方法; struct file 代表一個(gè)打開的文件, struct inode 代表磁盤上的一個(gè)文件.

#include <linux/cdev.h>
struct cdev *cdev_alloc(void);
void cdev_init(struct cdev *dev, struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
void cdev_del(struct cdev *dev);

cdev 結(jié)構(gòu)管理的函數(shù), 它代表內(nèi)核中的字符設(shè)備.

#include <linux/kernel.h>
container_of(pointer, type, field);

一個(gè)傳統(tǒng)宏定義, 可用來獲取一個(gè)結(jié)構(gòu)指針, 從它里面包含的某個(gè)其他結(jié)構(gòu)的指針.

#include <asm/uaccess.h>

這個(gè)包含文件聲明內(nèi)核代碼使用的函數(shù)來移動(dòng)數(shù)據(jù)到和從用戶空間.

unsigned long copy_from_user (void *to, const void *from, unsigned long count);
unsigned long copy_to_user (void *to, const void *from, unsigned long count);

在用戶空間和內(nèi)核空間拷貝數(shù)據(jù).

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)