查看: 7202|回复: 5
打印 上一主题 下一主题

内核操作 Linux2.6内核驱动移植参考

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-9 15:28:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
随着Linux2.6的发布,由于2.6内核做了教的改动,各个设备的驱动程序在不同程度上要进行改写。为了方便各位Linux爱好者我把自己整理的这分文档share出来。该文当列举了2.6内核同以前版本的绝大多数变化,可惜的是由于时间和精力有限没有详细列出各个函数的用法。

特别声明:该文档中的内容来自http://lwn.net,该网也上也有各个函数的较为详细的说明可供各位参考。

1、 使用新的入口

必须包含 <linux/init.h>

module_init(your_init_func);

module_exit(your_exit_func);

老版本:int init_module(void);

void cleanup_module(voi);

2.4中两种都可以用,对如后面的入口函数不必要显示包含任何头文件。

2、 GPL

MODULE_LICENSE("Dual BSD/GPL");

老版本:MODULE_LICENSE("GPL");

3、 模块参数

必须显式包含<linux/moduleparam.h>

module_param(name, type, perm);

module_param_named(name, value, type, perm);

参数定义

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配置,现在在代码中就可以实现。

5、 模块计数

int try_module_get(&module);

module_put();

老版本:MOD_INC_USE_COUNT 和 MOD_DEC_USE_COUNT

6、 符号导出

只有显示的导出符号才能被其他模块使用,默认不导出所有的符号,不必使用EXPORT_NO_SYMBOLS

老板本:默认导出所有的符号,除非使用EXPORT_NO_SYMBOLS

7、 内核版本检查

需要在多个文件中包含<linux/module.h>时,不必定义__NO_VERSION__

老版本:在多个文件中包含<linux/module.h>时,除在主文件外的其他文件中必须定义__NO_VERSION__,防止版本重复定义。

8、 设备号

kdev_t被废除不可用,新的dev_t拓展到了32位,12位主设备号,20位次设备号。

unsigned int iminor(struct inode *inode);

unsigned int imajor(struct inode *inode);

老版本:8位主设备号,8位次设备号

int MAJOR(kdev_t dev);

int MINOR(kdev_t dev);

9、 内存分配头文件变更

所有的内存分配函数包含在头文件<linux/slab.h>,而原来的<linux/malloc.h>不存在

老版本:内存分配函数包含在头文件<linux/malloc.h>

10、 结构体的初试化

gcc开始采用ANSI C的struct结构体的初始化形式:

static struct some_structure = {

.field1 = value,

.field2 = value,

..

};

老版本:非标准的初试化形式

static struct some_structure = {

field1: value,

field2: value,

..

};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
2#
 楼主| 发表于 2007-9-9 15:28:42 | 只看该作者
台州网址导航
11、 用户模式帮助器

int call_usermodehelper(char *path, char **argv, char **envp,int wait);

新增wait参数

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引发的字符设备的变化

1、取主次设备号为

unsigned iminor(struct inode *inode);

unsigned imajor(struct inode *inode);

2、老的register_chrdev()用法没变,保持向后兼容,但不能访问设备号大于256的设备。

3、新的接口为

a)注册字符设备范围

int register_chrdev_region(dev_t from, unsigned count, char *name);

b)动态申请主设备号

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, char

*name);

看了这两个函数郁闷吧^_^!怎么和file_operations结构联系起来啊?别急!

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结构,和fops连接,将设备加入到系统中!好复杂啊!)

d)void cdev_del(struct cdev *cdev);

只有在cdev_add执行成功才可运行。

e)辅助函数

kobject_put(&cdev->kobj);

struct kobject *cdev_get(struct cdev *cdev);

void cdev_put(struct cdev *cdev);

这一部分变化和新增的/sys/dev有一定的关联。

14、 新增对/proc的访问操作

<linux/seq_file.h>

以前的/proc中只能得到string, seq_file操作能得到如long等多种数据。

相关函数:

static struct seq_operations 必须实现这个类似file_operations得数据中得各个成

员函数。

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、 底层内存分配

1、<linux/malloc.h>头文件改为<linux/slab.h>

2、分配标志GFP_BUFFER被取消,取而代之的是GFP_NOIO 和 GFP_NOFS

3、新增__GFP_REPEAT,__GFP_NOFAIL,__GFP_NORETRY分配标志

4、页面分配函数alloc_pages(),get_free_page()被包含在<linux/gfp.h>中

5、对NUMA系统新增了几个函数:

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);
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
3#
 楼主| 发表于 2007-9-9 15:28:55 | 只看该作者
台州网址导航
pe, name);

DEFINE_PER_CPU(int, mypcint);

2.6内核采用了可剥夺得调度方式这些宏都不安全。

17、 内核时间变化

1、现在的各个平台的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计数器很快就溢出了,引入了新的计数器jiffies_64

3、#include <linux/jiffies.h>

u64 my_time = get_jiffies_64();

4、新的时间结构增加了纳秒成员变量

struct timespec current_kernel_time(void);

5、他的timer函数没变,新增

void add_timer_on(struct timer_list *timer, int cpu);

6、新增纳秒级延时函数

ndelay();

7、POSIX clocks 参考kernel/posix-timers.c

18、 工作队列(workqueue)

1、任务队列(task queue )接口函数都被取消,新增了workqueue接口函数

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结构

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);

19、 新增创建VFS的"libfs"

libfs给创建一个新的文件系统提供了大量的API.

主要是对struct file_system_type的实现。

参考源代码:

drivers/hotplug/pci_hotplug_core.c

drivers/usb/core/inode.c

drivers/oprofile/oprofilefs.c

fs/ramfs/inode.c

fs/nfsd/nfsctl.c (simple_fill_super() example)
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
4#
 楼主| 发表于 2007-9-9 15:29:08 | 只看该作者
台州网址导航
sl);

void write_sequnlock_irq(seqlock_t *sl);

void write_seqlock_bh(seqlock_t *sl);

void write_sequnlock_bh(seqlock_t *sl);

unsigned int read_seqbegin(seqlock_t *sl);

int read_seqretry(seqlock_t *sl, unsigned int iv);

unsigned int read_seqbegin_irqsave(seqlock_t *sl, long flags);

int read_seqretry_irqrestore(seqlock_t *sl, unsigned int iv, long

flags);
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
5#
 楼主| 发表于 2007-9-9 15:29:20 | 只看该作者
台州网址导航
22、 内核可剥夺

<linux/preempt.h>

preempt_disable();

preempt_enable_no_resched();

preempt_enable_noresched();

preempt_check_resched();

23、 眠和唤醒

1、原来的函数可用,新增下列函数:

prepare_to_wait_exclusive();

prepare_to_wait();

2、等待队列的变化

typedef int (*wait_queue_func_t)(wait_queue_t *wait,

unsigned mode, int sync);

void init_waitqueue_func_entry(wait_queue_t *queue,

wait_queue_func_t func);

24、 新增完成事件(completion events)

<linux/completion.h>

init_completion(&my_comp);

void wait_for_completion(struct completion *comp);

void complete(struct completion *comp);

void complete_all(struct completion *comp);

25、 RCU(Read-copy-update)

rcu_read_lock();

void call_rcu(struct rcu_head *head, void (*func)(void *arg),

void *arg);

26、 中断处理

1、中断处理有返回值了。

IRQ_RETVAL(handled);

2、cli(), sti(), save_flags(), 和 restore_flags()不再有效,应该使用local_save

_flags() 或local_irq_disable()。

3、synchronize_irq()函数有改动

4、新增int can_request_irq(unsigned int irq, unsigned long flags);

5、 request_irq() 和free_irq() 从 <linux/sched.h>改到了 <linux/interrupt.h>

27、 异步I/O(AIO)

<linux/aio.h>

ssize_t (*aio_read) (struct kiocb *iocb, char __user *buffer,

size_t count, loff_t pos);

ssize_t (*aio_write) (struct kiocb *iocb, const char __user *buffer,

size_t count, loff_t pos);

int (*aio_fsync) (struct kiocb *, int datasync);

新增到了file_operation结构中。

is_sync_kiocb(struct kiocb *iocb);

int aio_complete(struct kiocb *iocb, long res, long res2);

28、 网络驱动

1、struct net_device *alloc_netdev(int sizeof_priv, const char *name,

void (*setup)(struct net_device *));

struct net_device *alloc_etherdev(int sizeof_priv);

2、新增NAPI(New API)

void netif_rx_schedule(struct net_device *dev);

void netif_rx_complete(struct net_device *dev);

int netif_rx_ni(struct sk_buff *skb);

(老版本为netif_rx())

29、 USB驱动

老版本struct usb_driver取消了,新的结构体为

struct usb_class_driver {

char *name;

struct file_operations *fops;

mode_t mode;

int minor_base;

};

int usb_submit_urb(struct urb *urb, int mem_flags);

int (*probe) (struct usb_interface *intf,

const struct usb_device_id *id);
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
6#
 楼主| 发表于 2007-9-9 15:29:38 | 只看该作者
台州网址导航
主要用于设备管理。

1、 sysfs

2、 Kobjects
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州维博网络(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:企业网站建设、网站程序开发、手机APP客户端、平面设计、主机域名、虚拟空间、网站推广、网站优化、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2024 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表