---
title: "利用udev、sys动态创建linux设备结点"
author: "cjc"
date: "2009-05-31T13:48:00Z"
tags: []
slug: "udev-sys-linux"
draft: false
lastmod: "2009-05-31T13:48:00Z"
id: 402545
url: "https://cjc.iteye.com/blog/402545"
---

&lt;script&gt;function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}&lt;/script&gt;

**作者：**刘洪涛，[华清远见嵌入式培训中心](http://www.farsight.com.cn/)讲师。

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

下面通过一个实例，说明udev、sys动态创建设备结点的方法。注意代码中红色的部分是为了实现动态创建设备结点添加的。  
#include &lt;linux/module.h&gt;  
#include &lt;linux/kernel.h&gt;  
#include &lt;linux/init.h&gt;  
#include &lt;linux/fs.h&gt;  
#include &lt;linux/cdev.h&gt;  
#include &lt;asm/uaccess.h&gt;  
#include &lt;linux/device.h&gt;  
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 opened\\n");  
return 0;  
}  
static int hello\_release (struct inode \*inode, struct file \*file)  
{  
printk (KERN\_INFO "Hmmm... device closed\\n");  
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 bytes\\n", 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 bytes\\n", count);  
if (count&gt;127) return -ENOMEM;  
if (count&lt;0) return -EINVAL;  
if (copy\_from\_user (data, buf, count)) {  
ret = -EFAULT;  
}  
else {  
data\[127]='\\0';  
printk (KERN\_INFO"Received: %s\\n", 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&lt;0) {  
printk (KERN\_WARNING "hello: can't get major number %d\\n", hello\_major);  
return result;  
}  
char\_reg\_setup\_cdev ();  
printk (KERN\_INFO "char device registered\\n");  
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。在不同版本的内核中，有些系统函数的参数可能不太一样。