可以利用udev、sys动态创建linux设备结点

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

下面通过一个实例,说明udev、sys动态创建设备结点的方法。注意代码中红色的部分是为了实现动态创建设备结点添加的。

 可以利用udev、sys动态创建linux设备结点_设计制作_电源/新能源

#include

#include

#include

#include

#include

#include

#include

MODULE_LICENSE ("GPL");

int hello_major = 252;

int hello_     mi   nor = 0;

int number_of_devices = 1;

char data[50]="foobar not equal to barfoo";

struct cdev cdev;

dev_t dev = 0;

sta     ti   c int hello_open (struct inode *inode, struct file *file)

{

printk (KERN_INFO "Hey! device opened\n");

return 0;

}

sta  TI c 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 "wro     te   %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 "Wri  TI ng %d bytes\n", count);

if (count>127) return -ENOMEM;

if (count<0) return -EINVAL;

if (copy_f     rom   _user (data, buf, count)) {

ret = -EFAULT;

}

else {

data[127]='\0';

printk (KERN_INFO"Received: %s\n", data);

ret = count;

}

return ret;

}

struct file_opera  TI ons hello_fops = {

.owner = THIS_MODULE,

.open = hello_open,

.release = hello_release,

.read = hello_read,

.write = hello_write

};

struct class *my_class;

sta  TI c 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, "fa     rs   ight_class");//add by lht

if(IS_ERR(my_class)) {

printk("Err: f     ai   led 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。在不同版本的内核中,有些系统函数的参数可能不太一样。



90
94
0
68

相关资讯

  1. 1、如何用ai设计柿子树插画用ai设计柿子树插画的教程4015
  2. 2、电脑怎么远程操作朋友的电脑?用QQ远程操作的解决方法1099
  3. 3、豆瓣APP如何看别人日记?关注好友日常生活2199
  4. 4、Win8电脑要怎么关闭uac服务?win8关闭uac方法教程620
  5. 5、Win10专业版如何解除校园网限速?344
  6. 6、影音转霸如何去水印去字幕?影音转霸去水印去字幕的方法994
  7. 7、Win10系统如何查看网络适配器的型号?3743
  8. 8、Foxmail如何添加新联系人?Foxmail添加新联系人的方法步骤1737
  9. 9、Win10运行DNF全屏后黑屏怎么解决?3699
  10. 10、Win11如何固定开始菜单?Win11固定开始菜单的方法2658
全部评论(0)
我也有话说
0
收藏
点赞
顶部