编写字符驱动时出了问题: linux驱动测试程序打不开驱动设备文件 。求指导!谢谢了! 5

#defineDEVICE_NAME"mydev"intmajor=0;dev_tdev_nr=0;staticintdev_open(structinode*pinod... #define DEVICE_NAME "mydev"

int major = 0;

dev_t dev_nr = 0;
static int dev_open(struct inode *pinode, struct file *pfile)

{

printk(KERN_EMERG "open() is called\n");

return 0;

}

static struct file_operations dev_fops=

{

.owner = THIS_MODULE,

.open = dev_open,

};

struct cdev my_cdev;

static int __init chardev_init(void)

{

int ret;

if (major > 0){

ret = register_chrdev_region(MKDEV(major,0), 1, DEVICE_NAME);

}

else if(major < 0){

printk(KERN_EMERG "major < 0,err!!! \n");

}

else{

ret = alloc_chrdev_region(&dev_nr, 0, 1, DEVICE_NAME);

if (ret<0){

printk(KERN_EMERG DEVICE_NAME"\t get major erro\n");

return ret;

}

major = MAJOR(dev_nr);

printk(KERN_EMERG DEVICE_NAME" major:%d\n", major);

}

cdev_init(&my_cdev, &dev_fops);

my_cdev.owner =THIS_MODULE;

ret = cdev_add(&my_cdev, dev_nr, 1);

if(ret < 0){

unregister_chrdev_region(dev_nr, 1);

}

printk(KERN_EMERG DEVICE_NAME "\t module init now!!\n");

return (ret);

}

static void __exit chardev_exit(void)

{

cdev_del(&my_cdev);

unregister_chrdev_region(dev_nr, 1);

printk(KERN_EMERG DEVICE_NAME "\t module exit now !!!\n");

}

module_init(chardev_init);

module_exit(chardev_exit);

MODULE_LICENSE("GPL");

测试代码:
#define DEV_NAME "mydev"

int main(int argc, char *args[])

{

int fd = 0;

fd = open(DEV_NAME, O_RDWR);

if (fd < 0){

printf("can not open device\n");

}

printf("fd = %d\n", fd);

return 0;

}

[root@pysnetwork 2.6]# insmod drv.ko
Message from syslogd@localhost at Mar 15 12:02:28 ...

kernel:mydev major:248
Message from syslogd@localhost at Mar 15 12:02:28 ...

kernel:mydev module init now!!
[root@pysnetwork 2.6]# cat /proc/devices | grep mydev

248 mydev
[root@pysnetwork 2.6]# mknod /dev/mydev c 248 0
[root@pysnetwork 2.6]# ./app

can not open device
fd = -1
1均未贴出头文件
2第一段代码是驱动代码
展开
 我来答
cattleZhouYuli
2015-03-15 · TA获得超过194个赞
知道小有建树答主
回答量:369
采纳率:0%
帮助的人:180万
展开全部
应用层默认open函数定义:int open( const char * pathname,int flags);

驱程序般open函数定义:int(*open)(struct inode *inode, struct file *filp)

LED点亮应用层主函数
[code=C/C++][/code]void main(void)
{
int testdev;
int i;
char buf[10];
testdev = open ("/dev/test",O_RDWR); //应该用应用层默认Open函数吧
if(testdev == -1)
{
printf("Cann't open file...../n");
exit(0);
}
printf("buf = 0x%x/n",buf);
read(testdev,buf,10); //功opentestdev值0能够file文件描述符呢
write(testdev,buf,1);
led_ctl.port='G';
led_ctl.bit=5;
led_ctl.value=0;
ioctl(testdev,GPIO_IO_SET_GPG,&led_ctl);
printf("%s",buf);
pause();
close(testdev);
}

我疑问:
1.驱程序open函数:int(*open)(struct inode *inode, struct file *filp)
哪使用啊
2.驱open函数般用做啊
3.文代码功opentestdev值0能够file文件描述符呢
才始习Linux设备驱发借本书代码没注释面驱意思懂希望各位神指点指点万谢
面都我编写字符设备驱代码请问static int read_test()函数始static void release_test()函数参数内部代码意义
万谢

采纳前加100谢谢

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/module.h>

unsigned int test_major = 0;

static int read_test(struct inode *node,struct file *fle,char *buf,int count)
{
int left;
if (access_ok(VERIFY_WRITE, buf, count))
{
for (left = count; left > 0; left--)
{
_put_user('a',buf);
buf ++;
}
}
return count;
}

static int write_test(struct inode *node, struct file *fle, char *buf, int count)
{
return count;
}

static int open_test(struct inode *inode,struct file *file)
{
try_module_get(THIS_MODULE);
return 0;
}

static void release_test(struct inode *inode, struct file *file)
{
module_put(THIS_MODULE);
}

struct file_operations test_fops =
{
.owner = THIS_MODULE,
.read = read_test,
.write = write_test,
.open = open_test,
.release = release_test,
};

int init_module()
{
int result;
result = register_chrdev(0,"test,&test_fops");
if (result < 0)
{
printk(KERN_INFO "dddddddddddddddd");
return result;
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式