当前位置:首页 > 嵌入式 > 嵌入式软件
[导读]如何通过udev和sys文件系统动态创建设备结点

在Linux2.6内核中,devfs被认为是过时的方法,并最终被抛弃,udev取代了它。Devfs的一个很重要的特点就是可以动态创建设备结点。那我们现在如何通过udev和sys文件系统动态创建设备结点呢?

        下面通过一个实例,说明udev、sys动态创建设备结点的方法。注意代码中红色的部分是为了实现动态创建设备结点添加的。 
#include <linux/module.h> 
#include <linux/kernel.h>
#include <linux/init.h> 
#include <linux/fs.h> 
#include <linux/cdev.h> 
#include <asm/uaccess.h> 
#include <linux/device.h>
MODULE_LICENSE ("GPL");
int hello_major = 252;
int hello_minor = 0;
int number_of_devices = 1;
char data[50]="foobar not equal to barfoo";
struct cdev cdev;
dev_t dev = 0;
static int hello_open (struct inode *inode, struct file *file)
{
printk (KERN_INFO "Hey! device openedn");
return 0;
}
static int hello_release (struct inode *inode, struct file *file)
{
printk (KERN_INFO "Hmmm... device closedn");
return 0;
}
ssize_t hello_read (struct file *filp, char *buff, size_t count, loff_t *offp)
{
ssize_t result = 0;
if (copy_to_user (buff, data, sizeof(data)-1)) 
result = -EFAULT;
else 
printk (KERN_INFO "wrote %d bytesn", count);
return result;
}
ssize_t hello_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
ssize_t ret = 0;
printk (KERN_INFO "Writing %d bytesn", count);
if (count>127) return -ENOMEM;
if (count<0) return -EINVAL;
if (copy_from_user (data, buf, count)) {
ret = -EFAULT;
}
else {
data[127]='';
printk (KERN_INFO"Received: %sn", data);
ret = count;
}
return ret;
}
struct file_operations hello_fops = {
.owner = THIS_MODULE,
.open = hello_open,
.release = hello_release,
.read = hello_read,
.write = hello_write
};
struct class *my_class;
static void char_reg_setup_cdev (void)
{
int error, devno = MKDEV (hello_major, hello_minor);
cdev_init (&cdev, &hello_fops);
cdev.owner = THIS_MODULE;
cdev.ops = &hello_fops;
error = cdev_add (&cdev, devno , 1);
if (error)
printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error);
/* creating your own class */
my_class =class_create(THIS_MODULE, "farsight_class");//add by lht
if(IS_ERR(my_class)) {
printk("Err: failed in creating class.n");
return ;
}
/* register your own device in sysfs, and this will cause udevd to create corresponding device node */
class_device_create(my_class,NULL, devno, NULL,"farsight_dev");
// device_create(my_class,NULL, devno,"farsight_dev");
}
static int __init hello_2_init (void)
{
int result;
dev = MKDEV (hello_major, hello_minor);
result = register_chrdev_region (dev, number_of_devices, "test");
if (result<0) {
printk (KERN_WARNING "hello: can't get major number %dn", hello_major);
return result;
}
char_reg_setup_cdev ();
printk (KERN_INFO "char device registeredn");
return 0;
}
static void __exit hello_2_exit (void)
{
dev_t devno = MKDEV (hello_major, hello_minor);
cdev_del (&cdev);
unregister_chrdev_region (devno, number_of_devices);
class_device_destroy(my_class, devno);
class_destroy(my_class);
}
module_init (hello_2_init);
module_exit (hello_2_exit);v

        在编译了驱动后,可以查看/dev/farsight_dev设备结点,和 /sys/class/farsight_class/farsight_dev/ 本代码的测试环境是Ubantu7.04,内核版本是2.6.20-15-generi。在不同版本的内核中,有些系统函数的参数可能不太一样。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

武汉2025年9月9日 /美通社/ -- 7月24日,2025慧聪跨业品牌巡展——湖北•武汉站在武汉中南花园酒店隆重举办!本次巡展由慧聪安防网、慧聪物联网、慧聪音响灯光网、慧聪LED屏网、慧聪教育网联合主办,吸引了安防、...

关键字: AI 希捷 BSP 平板

上海2025年9月9日 /美通社/ -- 9月8日,移远通信宣布,其自研蓝牙协议栈DynaBlue率先通过蓝牙技术联盟(SIG)BQB 6.1标准认证。作为移远深耕短距离通信...

关键字: 蓝牙协议栈 移远通信 COM BSP

上海2025年9月9日 /美通社/ -- 为全面落实党中央、国务院和上海市委、市政府关于加快发展人力资源服务业的决策部署,更好发挥人力资源服务业赋能百业作用,8月29日,以"AI智领 HR智链 静候你来&quo...

关键字: 智能体 AI BSP 人工智能

北京2025年9月8日 /美通社/ -- 近日,易生支付与一汽出行达成合作,为其自主研发的"旗驭车管"车辆运营管理平台提供全流程支付通道及技术支持。此次合作不仅提升了平台对百余家企业客户的运营管理效率...

关键字: 一汽 智能化 BSP SAAS

深圳2025年9月8日 /美通社/ -- 晶泰科技(2228.HK)今日宣布,由其助力智擎生技制药(PharmaEngine, Inc.)发现的新一代PRMT5抑制剂PEP0...

关键字: 泰科 AI MT BSP

上海2025年9月5日 /美通社/ -- 由上海市经济和信息化委员会、上海市发展和改革委员会、上海市商务委员会、上海市教育委员会、上海市科学技术委员会指导,东浩兰生(集团)有限公司主办,东浩兰生会展集团上海工业商务展览有...

关键字: 电子 BSP 芯片 自动驾驶

推进卓越制造,扩大产能并优化布局 苏州2025年9月5日 /美通社/ -- 耐世特汽车系统与苏州工业园区管委会正式签署备忘录,以设立耐世特亚太总部苏州智能制造项目。...

关键字: 智能制造 BSP 汽车系统 线控

慕尼黑和北京2025年9月4日 /美通社/ -- 宝马集团宣布,新世代首款量产车型BMW iX3将于9月5日全球首发,9月8日震撼亮相慕尼黑车展。中国专属版车型也将在年内与大家见面,2026年在国内投产。 宝马集团董事...

关键字: 宝马 慕尼黑 BSP 数字化

北京2025年9月4日 /美通社/ -- 在全球新一轮科技革命与产业变革的澎湃浪潮中,人工智能作为引领创新的核心驱动力,正以前所未有的深度与广度重塑各行业发展格局。体育领域深度融入科技变革浪潮,驶入数字化、智能化转型快车...

关键字: 人工智能 智能体 AI BSP

上海2025年9月2日 /美通社/ -- 近日,由 ABB、Moxa(摩莎科技)等八家企业在上海联合发起并成功举办"2025 Ethernet-APL 技术应用发展大会"。会议以"破界•融合...

关键字: ETHERNET 智能未来 BSP 工业通信
关闭