如何编写Linux设备驱动程序(代码)

相信一些人编过www.top-e.org/jiaoshi/html/这个网站的设备驱动,

但编译之后,错误和警告多多。主要原因是因为linux版本不同。现,在Linux2.6.13编译,然后下到友善之臂s3c2440a上运行。

 

按照这网站上的要求,编译之后,错误和警告多,主要显示:

 

warning: `verify_area' is deprecated (declared at include/asm/uaccess.h:83)

 

error: `MOD_INC_USE_COUNT' undeclared (first use in this function)

 

error: `__put_user' undeclared (first use in this function)

 

warning: initialization from incompatible pointer type

 

 

网站源程序:

 

 

#define __NO_VERSION__

#include <linux/modules.h>

#include <linux/version.h>

char kernel_version [] = UTS_RELEASE;

#include <linux/types.h>

#include <linux/fs.h>

#include <linux/mm.h>

#include <linux/errno.h>

#include <asm/segment.h>

unsigned   int   test_major   =   0;

static int read_test(struct inode *node,struct file *file, char *buf,int count)

{

int   left;

if   (verify_area(VERIFY_WRITE,buf,count)   ==   -EFAULT   )

return   -EFAULT;

for(left   =   count   ;   left >   0   ;   left--)

{

__put_user(1,buf,1);

buf++;

}

return   count;

}

static int write_test(struct inode *inode,struct file *file,const char *buf,int count)

{

return   count;

}

static int open_test(struct inode *inode,struct file   *file   )

{

MOD_INC_USE_COUNT;

return   0;

}

static void release_test(struct inode *inode,struct file *file   )

{

MOD_DEC_USE_COUNT;

}

 

 

struct file_operations test_fops = {

 

NULL,

read_test,

write_test,

NULL, /* test_readdir */

NULL,

NULL, /* test_ioctl */

NULL, /* test_mmap */

open_test,

release_test, NULL, /* test_fsync */

NULL, /* test_fasync */

/* nothing more, fill with NULLs */

};  

int   init_module(void)

{

int   result;

result   =   register_chrdev(0,   "test",   &test_fops);

if   (result   <   0)   {

printk(KERN_INFO   "test:   can't   get   major   number   ");

return   result;

}

if   (test_major   ==   0)   test_major   =   result;   /* dynamic   */

return   0;

}

void   cleanup_module(void)

{

unregister_chrdev(test_major,   "test");

}

/* 描述驱动程序的一些信息,不是必须的 */

MODULE_AUTHOR("http://www.100ask.net");             // 驱动程序的作者

MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");   // 一些描述信息

MODULE_LICENSE("GPL");                              // 遵循的协议

 

出现错误的原因主要是结构体file_operations的不同,linux2.6.13file_operations为:

 

struct file_operations {

       struct module *owner;

       loff_t (*llseek) (struct file *, loff_t, int);

       ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

       ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);

       ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

       ssize_t (*aio_write) (struct kiocb *, const char __user *, 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);

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

       long (*compat_ioctl) (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 (*aio_fsync) (struct kiocb *, 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 (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);

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

       int (*check_flags)(int);

       int (*dir_notify)(struct file *filp, unsigned long arg);

       int (*flock) (struct file *, int, struct file_lock *);

};

 

“__put_user(1,buf,1)”的错误,在linux2.6.13版本上应改为:

“put_user(1,buf)”

put_user(x,ptr)定义如下: x:用户内存空间的整型变量 ptr:用户空间的地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值