【问题标题】:Reading configuration files in linux device driver读取linux设备驱动中的配置文件
【发布时间】:2013-12-13 11:56:36
【问题描述】:

如何读取linux设备驱动中的配置文件?专家说在内核空间读写文件是一种不好的做法。对于固件下载,我们有 request_firmware 内核 API。是否有用于读取和解析驱动程序配置文件的 linux 内核 API?例如:读取特定驱动程序的波特率和固件文件路径。

【问题讨论】:

  • 设备的配置方式有很多种,如自动配置、ioctls、模块参数等。你到底想配置什么?
  • 我正在开发一个与 UART 交互的驱动程序。需要将波特率和固件文件位置设置为驱动模块的可配置参数。我想探索是否有用于此目的的内核接口。

标签: linux-kernel config linux-device-driver


【解决方案1】:

大多数时候不鼓励从内核空间进行文件 i/o,但是如果您仍然想要从内核空间读取文件的方式,内核提供了一个很好的接口来从内核打开和读取文件。这是一个示例模块。

 /*
  * read_file.c - A Module to read a file from Kernel Space
  */
 #include <linux/module.h>
 #include <linux/fs.h>

 #define PATH "/home/knare/test.c"
 int mod_init(void)
 {
       struct file *fp;
       char buf[512];
       int offset = 0;
       int ret, i;


       /*open the file in read mode*/
       fp = filp_open(PATH, O_RDONLY, 0);
       if (IS_ERR(fp)) {
            printk("Cannot open the file %ld\n", PTR_ERR(fp));
            return -1;
       }

       printk("Opened the file successfully\n");
       /*Read the data to the end of the file*/
       while (1) {
            ret = kernel_read(fp, offset, buf, 512);
            if (ret > 0) {
                    for (i = 0; i < ret; i++)
                            printk("%c", buf[i]);
                    offset += ret;
            } else
                    break;
        }

       filp_close(fp, NULL);
       return 0;
  }

  void mod_exit(void)
  {

  }

  module_init(mod_init); 
  module_exit(mod_exit);

 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Knare Technologies (www.knare.org)");
 MODULE_DESCRIPTION("Module to read a file from kernel space");    

我在 linux-3.2 内核上测试了这个模块。我使用了 printk() 函数 打印数据,但这不是您的实际情况,这只是作为示例。

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多