当前位置: 技术问答>linux和unix
驱动移植问题
来源: 互联网 发布时间:2017-04-16
本文导语: 我在nb的虚拟机(CentOS 32bits)中编写了一个字符驱动,使用insmod加载成功并且能正常使用。将代码移植到rehat 6.4 64bits中后编译可以通过,但是使用insmod加载驱动时,系统就死机,并且没有任何打印输出信息,本人刚...
我在nb的虚拟机(CentOS 32bits)中编写了一个字符驱动,使用insmod加载成功并且能正常使用。将代码移植到rehat 6.4 64bits中后编译可以通过,但是使用insmod加载驱动时,系统就死机,并且没有任何打印输出信息,本人刚接触linux driver,不清楚什么原因造成的。求各位大神指点迷津。以下是代码(代码是网上拿过来使用的):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct class *my_class;
struct cdev cdev;
dev_t devno;
int KCS_MAJOR = 0;
int KCS_MINOR = 1;
int NUMBER_OF_DEVICES = 2;
#define DEVICE_NAME "TEST_driver"
#define STATUS_SUCCESS 0
#define _IO_IOCTL_NONE 0
#define _IO_IOCTL_READ 1
#define _IO_IOCTL_WRITE 3
#define _IO_IOCTL_RDWR 5
#define SET_PORT 7
static int Device_Open=0;
int test_major=1;
static int kcs_ioctl(struct inode *indode,struct file *file
,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
case _IO_IOCTL_NONE:
printk("""0rn");
break;
case _IO_IOCTL_READ:
printk("""start read data rn");
break;
case _IO_IOCTL_WRITE:
printk("""start write data rn");
break;
case _IO_IOCTL_RDWR:
printk("""3rn");
break;
case SET_KCS_PORT:
break;
default:
printk("""NULLrn");
break;
}
return 1;
}
//
//
//
static int open_dev(struct inode *inode,struct file *file)
{
int old =0;
#ifdef DEBUG
printk("""device_open(%p)rn",file);
#endif
old=Device_Open;
Device_Open++;
if(Device_Open==old)
{
return -EBUSY;
}
return 0;
}
//
static int release_dev(struct inode *inode,struct file *file)
{
#ifdef DEBUG
printk("device_release(%p,%p)rn",inode,file);
#endif
Device_Open--;
return 0;
}
//
static ssize_t read_data(struct file *file,char *buf,size_t count,loff_t *f_pos)
{
int left;
int i=0;
for(left = count ;left > 0;left--)
{
buf[i]='A';
i++;
}
return i;
}
//
static ssize_t write_data(struct file *file,const char *buf,size_t count,loff_t *f_pos)
{
int left;
int i=0;
for(left = count ;left > 0;left--)
{
printk("""%02x ",buf[i]);
i++;
}
printk("""rn");
return i;
}
//
static struct file_operations tdd_fops =
{
.owner = THIS_MODULE,
.ioctl = kcs_ioctl,
.read = read_kcs,
.write = write_kcs,
.open = open_dev,
.release = release_dev,
};
//
int _init_module(void)
{
printk ("" "module init..........n");
//KERN_WARNING level is 4
int result = 0;
//#define MKDEV(major,minor) (((major)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct class *my_class;
struct cdev cdev;
dev_t devno;
int KCS_MAJOR = 0;
int KCS_MINOR = 1;
int NUMBER_OF_DEVICES = 2;
#define DEVICE_NAME "TEST_driver"
#define STATUS_SUCCESS 0
#define _IO_IOCTL_NONE 0
#define _IO_IOCTL_READ 1
#define _IO_IOCTL_WRITE 3
#define _IO_IOCTL_RDWR 5
#define SET_PORT 7
static int Device_Open=0;
int test_major=1;
static int kcs_ioctl(struct inode *indode,struct file *file
,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
case _IO_IOCTL_NONE:
printk("""0rn");
break;
case _IO_IOCTL_READ:
printk("""start read data rn");
break;
case _IO_IOCTL_WRITE:
printk("""start write data rn");
break;
case _IO_IOCTL_RDWR:
printk("""3rn");
break;
case SET_KCS_PORT:
break;
default:
printk("""NULLrn");
break;
}
return 1;
}
//
//
//
static int open_dev(struct inode *inode,struct file *file)
{
int old =0;
#ifdef DEBUG
printk("""device_open(%p)rn",file);
#endif
old=Device_Open;
Device_Open++;
if(Device_Open==old)
{
return -EBUSY;
}
return 0;
}
//
static int release_dev(struct inode *inode,struct file *file)
{
#ifdef DEBUG
printk("device_release(%p,%p)rn",inode,file);
#endif
Device_Open--;
return 0;
}
//
static ssize_t read_data(struct file *file,char *buf,size_t count,loff_t *f_pos)
{
int left;
int i=0;
for(left = count ;left > 0;left--)
{
buf[i]='A';
i++;
}
return i;
}
//
static ssize_t write_data(struct file *file,const char *buf,size_t count,loff_t *f_pos)
{
int left;
int i=0;
for(left = count ;left > 0;left--)
{
printk("""%02x ",buf[i]);
i++;
}
printk("""rn");
return i;
}
//
static struct file_operations tdd_fops =
{
.owner = THIS_MODULE,
.ioctl = kcs_ioctl,
.read = read_kcs,
.write = write_kcs,
.open = open_dev,
.release = release_dev,
};
//
int _init_module(void)
{
printk ("" "module init..........n");
//KERN_WARNING level is 4
int result = 0;
//#define MKDEV(major,minor) (((major)
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!
©2012-2021,