当前位置: 技术问答>linux和unix
设备驱动编译无措,加载insmod不上(无错误提示)
来源: 互联网 发布时间:2017-03-16
本文导语: #include #include #include #include #include #include #include #include #define TEST_MAJOR 250 #define TEST_NAME "test" struct cdev test_dev; static ssize_t test_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) { return 0...
#include
#include
#include
#include
#include
#include
#include
#include
#define TEST_MAJOR 250
#define TEST_NAME "test"
struct cdev test_dev;
static ssize_t test_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}
static ssize_t test_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}
static long test_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
printk("it's my first char!!!!!n");
return 0;
}
struct file_operations test_fops=
{
.owner = THIS_MODULE,
.read = test_read,
.write = test_write,
.unlocked_ioctl = test_ioctl,
};
static int test_init(void)
{
int ret;
cdev_init(&test_dev, &test_fops);
register_chrdev_region(TEST_MAJOR, 1, TEST_NAME);
ret = cdev_add(&test_dev, TEST_MAJOR, 1);
printk(KERN_INFO"hello word entern");
return 0;
}
static void test_exit(void)
{
unregister_chrdev_region(TEST_MAJOR, 1);
cdev_del(&test_dev);
printk(KERN_INFO"hello word exitn");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
附应用层代码:
#include
#include
#include
#include
#include
#include
int main()
{
int test_fd;
test_fd = open("/dev/test", O_RDONLY);
if(test_fd != 0)
{
printf("open the dev is err!n");
return 0;
}
ioctl(test_fd, 5, 5);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#define TEST_MAJOR 250
#define TEST_NAME "test"
struct cdev test_dev;
static ssize_t test_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}
static ssize_t test_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}
static long test_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
printk("it's my first char!!!!!n");
return 0;
}
struct file_operations test_fops=
{
.owner = THIS_MODULE,
.read = test_read,
.write = test_write,
.unlocked_ioctl = test_ioctl,
};
static int test_init(void)
{
int ret;
cdev_init(&test_dev, &test_fops);
register_chrdev_region(TEST_MAJOR, 1, TEST_NAME);
ret = cdev_add(&test_dev, TEST_MAJOR, 1);
printk(KERN_INFO"hello word entern");
return 0;
}
static void test_exit(void)
{
unregister_chrdev_region(TEST_MAJOR, 1);
cdev_del(&test_dev);
printk(KERN_INFO"hello word exitn");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
附应用层代码:
#include
#include
#include
#include
#include
#include
int main()
{
int test_fd;
test_fd = open("/dev/test", O_RDONLY);
if(test_fd != 0)
{
printf("open the dev is err!n");
return 0;
}
ioctl(test_fd, 5, 5);
return 0;
}
|
register_chrdev_region(TEST_MAJOR, 1, TEST_NAME);
这句有问题,注册的设备号错误
register_chrdev_region的第一个参数dev_t应当是使用MKDEV宏生成的,而不是直接传主设备号
所以应该是
dev_t dev = MKDEV(TEST_MAJOR, 0)
register_chrdev_region(dev, 1, TEST_NAME);
MKDEV宏定义参见内核
#define MKDEV(ma,mi) (((ma)
这句有问题,注册的设备号错误
register_chrdev_region的第一个参数dev_t应当是使用MKDEV宏生成的,而不是直接传主设备号
所以应该是
dev_t dev = MKDEV(TEST_MAJOR, 0)
register_chrdev_region(dev, 1, TEST_NAME);
MKDEV宏定义参见内核
#define MKDEV(ma,mi) (((ma)