內(nèi)核操作 Linux2.6內(nèi)核驅(qū)動移植參考
更新時間: 2007-05-11 15:16:39來源: 粵嵌教育瀏覽量:1369
隨著Linux2.6的發(fā)布,由于2.6內(nèi)核做了教的改動,各個設(shè)備的驅(qū)動程序在不同程度上要進(jìn)行改寫。為了方便各位Linux愛好者我把自己整理的這分文檔share出來。該文當(dāng)列舉了2.6內(nèi)核同以前版本的絕大多數(shù)變化,可惜的是由于時間和精力有限沒有詳細(xì)列出各個函數(shù)的用法。
特別聲明:該文檔中的內(nèi)容來自http://lwn.net,該網(wǎng)也上也有各個函數(shù)的較為詳細(xì)的說明可供各位參考。
1、使用新的入口
必須包含 <linux/init.h>
module_init(your_init_func);
module_exit(your_exit_func);
老版本:int init_module(void);
void cleanup_module(voi);
2.4中兩種都可以用,對如后面的入口函數(shù)不必要顯示包含任何頭文件。
2、GPL
MODULE_LICENSE("Dual BSD/GPL");
老版本:MODULE_LICENSE("GPL");
3、模塊參數(shù)
必須顯式包含<linux/moduleparam.h>
module_param(name, type, perm);
module_param_named(name, value, type, perm);
參數(shù)定義
module_param_string(name, string, len, perm);
module_param_array(name, type, num, perm);
老版本:MODULE_PARM(variable,type);
MODULE_PARM_DESC(variable,type);
4、模塊別名
MODULE_ALIAS("alias-name");
這是新增的,在老版本中需在/etc/modules.conf配置,現(xiàn)在在代碼中就可以實現(xiàn)。
5、模塊計數(shù)
int try_module_get(&module);
module_put();
老版本:MOD_INC_USE_COUNT 和 MOD_DEC_USE_COUNT
6、符號導(dǎo)出
只有顯示的導(dǎo)出符號才能被其他模塊使用,默認(rèn)不導(dǎo)出所有的符號,不必使用EXPORT_NO_SYMBOLS
老板本:默認(rèn)導(dǎo)出所有的符號,除非使用EXPORT_NO_SYMBOLS
7、內(nèi)核版本檢查
需要在多個文件中包含<linux/module.h>時,不必定義__NO_VERSION__
老版本:在多個文件中包含<linux/module.h>時,除在主文件外的其他文件中必須定義__NO_VERSION__,防止版本重復(fù)定義。
8、設(shè)備號
kdev_t被廢除不可用,新的dev_t拓展到了32位,12位主設(shè)備號,20位次設(shè)備號。
unsigned int iminor(struct inode *inode);
unsigned int imajor(struct inode *inode);
老版本:8位主設(shè)備號,8位次設(shè)備號
int MAJOR(kdev_t dev);
int MINOR(kdev_t dev);
9、內(nèi)存分配頭文件變更
所有的內(nèi)存分配函數(shù)包含在頭文件<linux/slab.h>,而原來的<linux/malloc.h>不存在
老版本:內(nèi)存分配函數(shù)包含在頭文件<linux/malloc.h>
10、結(jié)構(gòu)體的初試化
gcc開始采用ANSI C的struct結(jié)構(gòu)體的初始化形式:
static struct some_structure = {
.field1 = value,
.field2 = value,
..
};
老版本:非標(biāo)準(zhǔn)的初試化形式
static struct some_structure = {
field1: value,
field2: value,
..
};
11、用戶模式幫助器
int call_usermodehelper(char *path, char **argv, char **envp,int wait);
新增wait參數(shù)
12、request_module()
request_module("foo-device-%d", number);
老版本:
char module_name[32];
printf(module_name, "foo-device-%d", number);
request_module(module_name);
13、dev_t引發(fā)的字符設(shè)備的變化
1)取主次設(shè)備號為
unsigned iminor(struct inode *inode);
unsigned imajor(struct inode *inode);
2)老的register_chrdev()用法沒變,保持向后兼容,但不能訪問設(shè)備號大于256的設(shè)備。
3)新的接口為
a)注冊字符設(shè)備范圍
int register_chrdev_region(dev_t from, unsigned count, char *name);
b)動態(tài)申請主設(shè)備號
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, char
*name);
看了這兩個函數(shù)郁悶吧^_^!怎么和file_operations結(jié)構(gòu)聯(lián)系起來啊?別急!
c)包含 <linux/cdev.h>,利用struct cdev和file_operations連接
struct cdev *cdev_alloc(void);
void cdev_init(struct cdev *cdev, struct file_operations *fops);
int cdev_add(struct cdev *cdev, dev_t dev, unsigned count);
(分別為,申請cdev結(jié)構(gòu),和fops連接,將設(shè)備加入到系統(tǒng)中!好復(fù)雜啊!)
d)void cdev_del(struct cdev *cdev);
只有在cdev_add執(zhí)行成功才可運(yùn)行。
e)輔助函數(shù)
kobject_put(&cdev->kobj);
struct kobject *cdev_get(struct cdev *cdev);
void cdev_put(struct cdev *cdev);
這一部分變化和新增的/sys/dev有一定的關(guān)聯(lián)。
14、新增對/proc的訪問操作
<linux/seq_file.h>
以前的/proc中只能得到string, seq_file操作能得到如long等多種數(shù)據(jù)。
相關(guān)函數(shù):
static struct seq_operations 必須實現(xiàn)這個類似file_operations得數(shù)據(jù)中得各個成
員函數(shù)。
seq_printf();
int seq_putc(struct seq_file *m, char c);
int seq_puts(struct seq_file *m, const char *s);
int seq_escape(struct seq_file *m, const char *s, const char *esc);
int seq_path(struct seq_file *m, struct vfsmount *mnt,
struct dentry *dentry, char *esc);
seq_open(file, &ct_seq_ops);
等等
15、底層內(nèi)存分配
1)<linux/malloc.h>頭文件改為<linux/slab.h>
2)分配標(biāo)志GFP_BUFFER被取消,取而代之的是GFP_NOIO 和 GFP_NOFS
3)新增__GFP_REPEAT,__GFP_NOFAIL,__GFP_NORETRY分配標(biāo)志
4)頁面分配函數(shù)alloc_pages(),get_free_page()被包含在<linux/gfp.h>中
5)對NUMA系統(tǒng)新增了幾個函數(shù):
a) struct page *alloc_pages_node(int node_id,
unsigned int gfp_mask,
unsigned int order);
b) void free_hot_page(struct page *page);
c) void free_cold_page(struct page *page);
6) 新增Memory pools
<linux/mempool.h>
mempool_t *mempool_create(int min_nr,
mempool_alloc_t *alloc_fn,
mempool_free_t *free_fn,
void *pool_data);
void *mempool_alloc(mempool_t *pool, int gfp_mask);
void mempool_free(void *element, mempool_t *pool);
int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask);
16、 per-CPU變量
get_cpu_var();
put_cpu_var();
void *alloc_percpu(type);
void free_percpu(const void *);
per_cpu_ptr(void *ptr, int cpu)
get_cpu_ptr(ptr)
put_cpu_ptr(ptr)
老版本使用
DEFINE_PER_CPU(type, name);
EXPORT_PER_CPU_SYMBOL(name);
EXPORT_PER_CPU_SYMBOL_GPL(name);
DECLARE_PER_CPU(type, name);
DEFINE_PER_CPU(int, mypcint);
2.6內(nèi)核采用了可剝奪得調(diào)度方式這些宏都不安全
17、內(nèi)核時間變化
1)現(xiàn)在的各個平臺的HZ為
Alpha: 1024/1200; ARM: 100/128/200/1000; CRIS: 100; i386: 1000; IA-64:
1024; M68K: 100; M68K-nommu: 50-1000; MIPS: 100/128/1000; MIPS64: 100;
PA-RISC: 100/1000; PowerPC32: 100; PowerPC64: 1000; S/390: 100; SPARC32:
100; SPARC64: 100; SuperH: 100/1000; UML: 100; v850: 24-100; x86-64: 1000.
2)由于HZ的變化,原來的jiffies計數(shù)器很快就溢出了,引入了新的計數(shù)器jiffies_64
3)#include <linux/jiffies.h>
u64 my_time = get_jiffies_64();
4)新的時間結(jié)構(gòu)增加了納秒成員變量
struct timespec current_kernel_time(void);
5)他的timer函數(shù)沒變,新增
void add_timer_on(struct timer_list *timer, int cpu);
6)新增納秒級延時函數(shù)
ndelay();
7)POSIX clocks 參考kernel/posix-timers.c
18、工作隊列(workqueue)
1)任務(wù)隊列(task queue )接口函數(shù)都被取消,新增了workqueue接口函數(shù)
struct workqueue_struct *create_workqueue(const char *name);
DECLARE_WORK(name, void (*function)(void *), void *data);
INIT_WORK(struct work_struct *work,
void (*function)(void *), void *data);
PREPARE_WORK(struct work_struct *work,
void (*function)(void *), void *data);
2)申明struct work_struct結(jié)構(gòu)
int queue_work(struct workqueue_struct *queue,
struct work_struct *work);
int queue_delayed_work(struct workqueue_struct *queue,
struct work_struct *work,
unsigned long delay);
int cancel_delayed_work(struct work_struct *work);
void flush_workqueue(struct workqueue_struct *queue);
void destroy_workqueue(struct workqueue_struct *queue);
int schedule_work(struct work_struct *work);
int schedule_delayed_work(struct work_struct *work, unsigned long
delay);
推薦閱讀
- ·Linux字符設(shè)備驅(qū)動框架解析:file_operations的核心作用與實現(xiàn)
- ·廣東朝歌數(shù)碼科技股份有限公司專場招聘會
- ·深化產(chǎn)教融合,共筑技能人才培養(yǎng)新生態(tài) —— 廣州華立學(xué)院到訪粵嵌從化校區(qū)為深化產(chǎn)教
- ·校企合作新突破 | 粵嵌科技與三亞學(xué)院共探產(chǎn)教融合新路徑
- ·粵嵌科技入選國家級職業(yè)數(shù)字展館聯(lián)合建設(shè)單位,賦能計算機(jī)程序設(shè)計員高技能人才培養(yǎng)
- ·嵌入式實時操作系統(tǒng)的性能優(yōu)化與實現(xiàn)路徑
- ·校企攜手賦能教育!粵嵌科技助力海南科技職業(yè)大學(xué)探索 AGI 時代教學(xué)新范式
- ·嵌入式系統(tǒng)中的低功耗設(shè)計策略與實現(xiàn)路徑
- ·深圳市軒宇軟件開發(fā)有限公司專場招聘會
- ·嵌入式系統(tǒng)中的代碼空間優(yōu)化:策略與實踐