当前位置:  技术问答>linux和unix

一个关于在驱动程序中向文件中写数据的小问题

    来源: 互联网  发布时间:2015-11-11

    本文导语:  我在修改无线驱动程序的时候,希望在驱动程序中向一个文件中写入一些数据,大致代码如下: struct file *ch_fd; char buf[4]; int len; ch_fd = filp_open("/tmp/channel", O_CREAT|O_RDWR, 0777); if (IS_ERR(ch_fd)) {     printk(RT_DEBUG_TRAC...

我在修改无线驱动程序的时候,希望在驱动程序中向一个文件中写入一些数据,大致代码如下:
struct file *ch_fd;
char buf[4];
int len;

ch_fd = filp_open("/tmp/channel", O_CREAT|O_RDWR, 0777);
if (IS_ERR(ch_fd))
{
    printk(RT_DEBUG_TRACE, "--> Error %ld opening %sn", -PTR_ERR(ch_fd),"/tmp/channel");
}
else
{
    if (ch_fd->f_op && ch_fd->f_op->write)
    {
        memset(buf, 0x00, 4);
        sprintf(buf, "%d",pAd->PortCfg.Channel);
        retval = ch_fd->f_op->write(ch_fd, buf, len, &ch_fd->f_pos);
    }
    retval=filp_close(ch_fd,NULL);
}

可运行结果是文件生成了,权限为:-rwxr-xr-x,可是文件中并没有写入数据,write()函数指针返回的值retval=-14。这段代码有什么问题,为什么不能往文件中写数据。望高手告知。谢谢

|
好像需要修改边界地址值吧。
A file object contains a field f_op. This field points to a file_operations structure that is used to store the file operations table. This structure contains pointers to functions such as read() and write(). By invoking this read (or write) and passing as its first parameter a pointer to the file object, one can perform a read on the given file. The other three parameters for a read (or write) are the buffer in which to place the bytes read, the number of bytes to be read, and the position in the file to begin the read. Only one problem exists. The buffer is in kernel space. 

The read operation is going to perform a check on this buffer to make sure it is in user space. Obviously this check will fail. This check will consist of comparing the address of the buffer with the value in the addr_limit field of the task_struct structure of the current process. A user space process will typically have an addr_limit value of 0xc0000000. We want to use the function set_fs() to change this addr_limit value to 0xffffffff. This will allow the check on the address of the buffer, which is in kernel space to pass. 

可以参看代码:
http://www.cise.ufl.edu/~mfoster/research/kernel/LJArticle/ReadWriteFile.c


    
 
 

您可能感兴趣的文章:

  • 我的网卡在redhat7.3下不能自动驱动,但我有for linux的驱动程序,请问如何才能驱动我的网卡,我是菜菜,请详细说明,谢谢!
  • 关于Linux 2.6平台下,自定义的USB HID设备是系统提供驱动程序还是需要自己完成驱动程序?
  • 哪位大哥大姐懂驱动驱动程序开发哇?
  • 有两个USB键盘怎么用自己写的USB键盘驱动程序只替换其中一个驱动?
  • -------------- 请问大家,做好一个PCI设备驱动了,如何做个应用程序来使用这个驱动呢??
  • 请问, linux 驱动中, IO 口变化了,驱动如何通知应用程序?
  • 各位老大,你们是从哪些方面改进网卡驱动程序的效率,欧现在写了一个lan91c嵌入式网卡的驱动,需要帮助
  • USB的ADSL驱动程序!!(可以驱动电信的ADSL)
  • 各位高人,关于IDE的驱动程序,我看蒙了,谁能告诉我到底linux源文件中到底那些是跟IDE硬盘驱动有关的?
  • 有触摸屏驱动程序源代码,怎么写一个命令行的程序测试驱动程序?
  • 驱动程序(模块)怎样与应用层交互呀?也就是应用程序怎样控制驱动程序?让其做什么它就做什么。
  • 谁会在DOS下面安装网卡驱动程序?
  • 两个驱动程序能互相访问么?
  • 驱动程序是否需要修改呢?帮顶有分
  • Norflash是否一定要驱动程序
  • 请问UNIX系统下装上WINDOWS的打印机驱动程序,可否打印?
  • 我太菜!!关于驱动程序的问题,急盼拍砖
  • 驱动程序开发和嵌入式开发有什么联系吗?
  • 移植PCI驱动程序,提示找不到iobuf.h iis7站长之家
  • 用户程序的ioctl是怎么和驱动程序中的ioctl实现联系
  • 请教:修改Linux内核时,需要调用驱动程序的函数,头文件也包含了,但是编译时候说头文件找不到!
  • 怎样才能得到Redhat8.0支持的所有打印机的驱动程序列表文件,我程序里要用。先谢过
  • misc类型的驱动程序怎么添加设备文件?也是用mknod么?
  • 在编译字符驱动程序时,为何总是找不到头文件!急......
  • 请问大家 在linux 2.6.16内核代码中,哪些文件是 关于 主板驱动程序的?
  • 请问gz格式的文件如何安装,我在Redhat 7.0下。这是我的via97声卡的驱动程序。
  • 驱动程序头文件的问题
  • LILO怎么启动系统的?/etc/lilo.conf文件该怎么修改?我看LINUX中的问题好象除了LILO外,就是驱动程序了。
  • 弱弱的问一个,简单字符设备驱动程序,打开文件错误问题。
  • 加载混杂字符设备驱动在/dev/下没有出现设备文件,请帮忙分析我程序!
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 谁有LINUX设备驱动程序第三版的程序代码,发我一份,非常的感谢!
  • 请问如何设置驱动程序和应用程序的启动顺序和优先级呢?
  • 请问shell 开发能开发什么样的程序?硬件的驱动程序是否能够开发呢?
  • linux驱动程序是怎样通过内核和应用程序通信,还有是怎样和硬件通信,跪求求解,谢谢高手留言。
  • 驱动程序找不到Class.forName("oracle.jdbc.driver.OracleDriver");,在程序中还要设置什么?
  • 菜鸟请教,应用程序如何判断驱动程序已经挂载成功
  • 在哪里可以下载OREILLY的linux设备驱动程序一书的程序源代码?在线送分!
  • linux初学者一简单问题!在线送分!linux设备驱动程序一书的第一个程序.
  • Linux的驱动程序构架怎么看不懂?请进来指点
  • 谁知道哪里有usb驱动程序的详细介绍,我想学习。
  • 驱动程序返回值的问题
  • 有没有这样的驱动程序?
  • 如何调试驱动程序
  • android内核中怎样hook一个驱动程序??
  • 有好多关于《linux设备驱动程序第三版》的问题,哪里问合适呢?
  • 有关于驱动程序的问题。一定给分的。
  • linux和windows的驱动程序是一样的吗??
  • 请问哪里有声卡的驱动程序下载?
  • 移植PCI驱动程序,提示找不到iobuf.h
  • 请教!关于linux字符驱动程序


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3