linux读写文件config,Linux内核:读写配置文件、/proc

这篇博客介绍了Linux平台设备驱动的注册和卸载过程。通过`test_drv_probe`函数注册UIO设备,分配内存并进行初始化,然后使用`uio_register_device`将设备注册到系统中。在`test_drv_remove`函数中,设备被注销并释放分配的内存。此外,还涉及到sysctl表格的注册和注销,用于设置调试标志。整个过程涉及内核模块的生命周期管理和设备管理。
摘要由CSDN通过智能技术生成

#include #include "rwproc.h"

//

struct platform_device * test_uio_device;

u8 test_uio_driver_flag = 0;

static struct ctl_table_header * test_sysctl_header = NULL;

//

u32 debug_flag = 1;

static ctl_table test_sysctl_table[] =

{

{

.ctl_name = 1,

.procname = "debug_flag",

.data = &debug_flag,

.maxlen = sizeof(debug_flag),

.mode = 0644,

.proc_handler = &proc_dointvec

},

{ .ctl_name = 0 }

};

static ctl_table test_ipv4_table[] =

{

{

.ctl_name = 5002,

.procname = "test_server",

.mode = 0555,

.child = test_sysctl_table

},

{ .ctl_name = 0 }

};

static ctl_table test_net_table[] =

{

{

.ctl_name = NET_IPV4,

.procname = "ipv4",

.mode = 0555,

.child = test_ipv4_table

},

{ .ctl_name = 0 }

};

static ctl_table test_root_table[] =

{

{

.ctl_name = CTL_NET,

.procname = "net",

.mode = 0555,

.child = test_net_table

},

{ .ctl_name = 0 }

};

struct uio_info test_uio_info =

{

.name = "test_update_uio",

.version = "1.0",

.irq = UIO_IRQ_NONE,

};

int test_drv_probe(struct device * dev)

{

test_uio_info.mem[0].addr = (unsigned long)kmalloc(4096, GFP_KERNEL);

if((void *)test_uio_info.mem[0].addr == NULL)

{

printk(KERN_DEBUG"test_drv_probe fail 1.");

return 501;

}

test_uio_info.mem[0].memtype = UIO_MEM_LOGICAL;

test_uio_info.mem[0].size = 4096;

if( uio_register_device(dev, &test_uio_info) )

{

printk(KERN_DEBUG"test_drv_probe fail 2.");

return 502;

}

return 0;

}

int test_drv_remove(struct device * dev)

{

uio_unregister_device(&test_uio_info);

return 0;

}

struct device_driver test_uio_driver =

{

.name = "test_update_uio",

.bus = &platform_bus_type,

.probe = test_drv_probe,

.remove = test_drv_remove,

};

int config_init(void)

{

int ret;

//

test_uio_device = platform_device_register_simple("test_update_uio", -1, NULL, 0);

//

ret = driver_register(&test_uio_driver);

if(ret)

{

printk(KERN_DEBUG"config_init fail 1.");

return 503;

}

test_uio_driver_flag = 1;

//

test_sysctl_header = register_sysctl_table(test_root_table);

if( test_sysctl_header == NULL )

{

printk(KERN_DEBUG"config_init fail 2.");

return 504;

}

return 0;

}

int config_exit(void)

{

//

if(test_uio_driver_flag)

{

driver_unregister(&test_uio_driver);

}

//

platform_device_unregister(test_uio_device);

//

if(test_sysctl_header)

{

unregister_sysctl_table(test_sysctl_header);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值