<script>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();}</script>
作者:刘洪涛,华清远见嵌入式培训中心讲师。
在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 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>127) return -ENOMEM;
if (count<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<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。在不同版本的内核中,有些系统函数的参数可能不太一样。
分享到:
相关推荐
可以利用udev、sys动态创建linux设备结点.docx
那我们现在如何通过udev和sys文件系统动态创建设备结点呢? 下面通过一个实例,说明udev、sys动态创建设备结点的方法。注意代码中红色的部分是为了实现动态创建设备结点添加的。 #include <linux> #include...
利用Udev在linux设备装载时实现易于识别的设备文件名.pdf
udev 高效、动态地管理 Linux 设备文件.
介绍了linux下如何利用udev来监控系统设备, 比如磁盘故障、磁盘挂载卸载等情况、网络异常等等
在Linux早期,设备文件仅仅是是一些带有适当的属性集的普通文件,它由mknod命令创建,文件存放在/dev目录下。后来,采用了devfs,一个基于内核的动态设备文件系统,他首次出现在2.3.46 内核中。Mandrake,Gentoo等...
Linux 用户常常会很难鉴别同一类型的设备名,比如 eth0, eth1, sda, sdb 等等。通过观察这些设备的内核设备名称,用户通常能知道这些是什么类型的设备,但是不知道哪一个设备是他们想要的。
作为守护进程, udev 接收的事件主要由 linux 内核生成,这些事件是外部设备产生的物理事件。总之, udev 探测外设和热插拔,将设备控制权传递给内核,例如加载内核模块或设备固件。 作为 devfsd 和 hotplug 的替代...
在Linux2.6內核中,devfs被認為是過時的方法,並最終被拋棄,udev取代了它。Devfs的一個很重要的特點就是可以動態創建設備結點。那我們現在如何通過udev和sys檔系統動態創建設備結點呢?
适用与海思平台下udev使用u盘的自动挂载,自动卸载功能
在上篇《利用udev、sys动态创建设备结点》的记录中,设备驱动中主要依靠下面两个功能完成的: 1、在/sys/class下创建farsight_class类 my_class =class_create(THIS_MODULE, "farsight_class"); 2、在...
1. 在/etc/udev/rules目录下新建11-add-usb.rules和11-add-remove.rules,负责设备监测。 root@am335x-evm:/etc/udev/rules.d# vi 11-add-usb.rules ACTION!="add",GOTO="farsight" KERNEL=="sd[a-z][0-9]",RUN+=...
udev源代码,用于管理linux下设备文件
本文以通俗的方法阐述 udev 及相关术语的概念、udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例。本文会使那些需要高效地、方便地管理 Linux 设备的...
udev是linux kernel的设备管理器,在内核版本中kernel_3.10开始的版本中,使用udev已经代替了以前devfs、hotplug等功能,意味着它要处理添加/删除硬件时,所有的用户空间行为。
本文档详细介绍了linux下udev的配置规则
通过udev库得到usb信息,能够正确分别USB信息,并且进行USB交互。平台是linux 平台
Red Hat Enterprise Linux 6使用udev配置Oracle ASM总结文档,使用udev固定设备名,解决系统重启后设备名变化等问题。