求高手修改小弟简单的字符驱动
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/io.h>
#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 inode *inode, 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 enter\n");
return 0;
}
static void test_exit(void)
{
unregister_chrdev_region(TEST_MAJOR, 1);
cdev_del(&test_dev);
printk(KERN_INFO"hello word exit\n");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
此代码直接加载到Linux3.0.1内核(OK6410光盘里的版本),报错如下::39: warning: initialization from incompatible pointer type
/bin/sh: scripts/basic/fixdep: cannot execute binary file
make[2]: *** [/nfs/zw_test/test_chrdev/./dev_test.o] 错误 126
make[1]: *** [_module_/nfs/zw_test/test_chrdev] 错误 2
高亮部分修改成int和.ioctl后,同样编译,报错如下::39: error: unknown field 'ioctl' specified in initializer
/nfs/zw_test/test_chrdev/./dev_test.c:39: warning: initialization from incompatible pointer type
make[2]: *** [/nfs/zw_test/test_chrdev/./dev_test.o] 错误 1
make[1]: *** [_module_/nfs/zw_test/test_chrdev] 错误 2
希望能有人帮帮小弟真正的解决问题,难得能够自己准备上手调试驱动
[解决办法]
我用你的原始代码在2.6.32下编译没问题
[root@localhost sdn]# make
make[1]: Entering directory `/mnt/pub/kbuild-FULL_REL'
CC [M] /mnt/extdrv/sdn/csdn.o
/mnt/extdrv/sdn/csdn.c:39: warning: initialization from incompatible pointer type
Building modules, stage 2.
MODPOST 1 modules
CC /mnt/extdrv/sdn/csdn.mod.o
LD [M] /mnt/extdrv/sdn/csdn.ko
make[1]: Leaving directory `/mnt/pub/kbuild-FULL_REL'
[root@localhost sdn]# ls -l
total 28
-rw------- 1 root root 1226 Sep 13 09:30 csdn.c
-rw------- 1 root root 0 Sep 13 09:29 csdn.c~
-rw-r--r-- 1 root root 2763 Sep 13 09:30 csdn.ko
-rw-r--r-- 1 root root 456 Sep 13 09:30 csdn.mod.c
-rw-r--r-- 1 root root 1404 Sep 13 09:30 csdn.mod.o
-rw-r--r-- 1 root root 1984 Sep 13 09:30 csdn.o
-rw-r--r-- 1 509 xuqian 216 Sep 13 09:30 Makefile
-rw-r--r-- 1 509 xuqian 315 Feb 27 2012 Makefile~
-rw-r--r-- 1 root root 0 Sep 13 09:30 Module.symvers
[解决办法]
在linux2.6.28下编译也没问题
root@ubuntu# make modules
scripts/kconfig/conf -s arch/arm/Kconfig
CHK include/linux/version.h
make[1]: `include/asm-arm/mach-types.h' is up to date.
CHK include/linux/utsrelease.h
SYMLINK include/asm -> include/asm-arm
CALL scripts/checksyscalls.sh
<stdin>:1097:2: warning: #warning syscall fadvise64 not implemented
<stdin>:1265:2: warning: #warning syscall migrate_pages not implemented
<stdin>:1321:2: warning: #warning syscall pselect6 not implemented
<stdin>:1325:2: warning: #warning syscall ppoll not implemented
<stdin>:1365:2: warning: #warning syscall epoll_pwait not implemented
CC [M] drivers/char/test.o
drivers/char/test.c:38: warning: initialization from incompatible pointer type
CC [M] drivers/media/video/gspca/gspca.o
LD [M] drivers/media/video/gspca/gspca_main.o
CC [M] drivers/scsi/scsi_wait_scan.o
Building modules, stage 2.
MODPOST 3 modules
CC drivers/char/test.mod.o
LD [M] drivers/char/test.ko
出现告警是由于struct file_operations中的unlocked_ioctl函数和你定义的test_ioctl函数参数不一样导致的,把static long test_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)改成static long test_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)就没告警了