驱动移植问题
我在nb的虚拟机(CentOS 32bits)中编写了一个字符驱动,使用insmod加载成功并且能正常使用。将代码移植到rehat 6.4 64bits中后编译可以通过,但是使用insmod加载驱动时,系统就死机,并且没有任何打印输出信息,本人刚接触linux driver,不清楚什么原因造成的。求各位大神指点迷津。以下是代码(代码是网上拿过来使用的):
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <linux/version.h>
#include <asm/uaccess.h>
#include <asm/io.h>
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("<0>""0\r\n");
break;
case _IO_IOCTL_READ:
printk("<0>""start read data \r\n");
break;
case _IO_IOCTL_WRITE:
printk("<0>""start write data \r\n");
break;
case _IO_IOCTL_RDWR:
printk("<0>""3\r\n");
break;
case SET_KCS_PORT:
break;
default:
printk("<0>""NULL\r\n");
break;
}
return 1;
}
//
//
//
static int open_dev(struct inode *inode,struct file *file)
{
int old =0;
#ifdef DEBUG
printk("<0>""device_open(%p)\r\n",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)\r\n",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("<0>""%02x ",buf[i]);
i++;
}
printk("<0>""\r\n");
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 ("<0>" "module init..........\n");
//KERN_WARNING level is 4
int result = 0;
//#define MKDEV(major,minor) (((major) << MINORBITS) | (minor))
devno = MKDEV(KCS_MAJOR, KCS_MINOR);
if (KCS_MAJOR)
result = register_chrdev_region(devno, 2,DEVICE_NAME);
//get a or more device number for the driver
else
{
result = alloc_chrdev_region(&devno, 0, 2,DEVICE_NAME);
KCS_MAJOR = MAJOR(devno);
}
printk("<1>""MAJOR IS %d\r\n",KCS_MAJOR);
my_class = class_create(THIS_MODULE,"hello_char_class");
//class name is hello_char_class,below /dev creat node
if(IS_ERR(my_class))
{
printk("<1>""Err: failed in creating class\r\n");
return -1;
}
device_create(my_class,NULL,devno,NULL,DEVICE_NAME);
//DEV name memdev ,creat a name(DEVICE_NAME) below /dev
if (result<0)
{
device_destroy(my_class, devno);
class_destroy(my_class);
unregister_chrdev_region (devno,NUMBER_OF_DEVICES);
printk ("<1>" "hello: can't get major number %d\r\n",KCS_MAJOR);
return result;
}
cdev_init(&cdev, &tdd_fops);
cdev.owner = THIS_MODULE;
cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
printk("<0>""Module load Success\r\n");
return 1;
}
//
void _cleanup_module(void)
{
cdev_del (&cdev);
device_destroy(my_class, devno);
//delete device node under /dev//
class_destroy(my_class);
//delete class created by us
unregister_chrdev_region (devno,NUMBER_OF_DEVICES);
printk("<0>""driver unload success\r\n");
}
module_init(_init_module);
module_exit(_cleanup_module);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR("bentu");
积分不多了,40积分给能帮忙解决问题的人