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

[11-20 15:53:59]   来源:http://www.88dzw.com  arm嵌入式   阅读:8969

文章摘要:作者:刘洪涛,www.88dzw.com嵌入式培训中心讲师。在Linux2.6内核中,devfs被认为是过时的方法,并最终被抛弃,udev取代了它。Devfs的一个很重要的特点就是可以动态创建设备结点。那我们现在如何通过udev和sys文件系统动态创建设备结点呢?下面通过一个实例,说明udev、sys动态创建设备结点的方法。注意代码中红色的部分是为了实现动态创建设备结点添加的。#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/

利用udev、sys动态创建linux设备结点,标签:arm嵌入式系统,arm系统,http://www.88dzw.com

  作者:刘洪涛,www.88dzw.com嵌入式培训中心讲师。

  在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 = {

  .         wner = THIS_MODULE,

  .         pen = 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");

[1] [2]  下一页


Tag:arm嵌入式arm嵌入式系统,arm系统arm嵌入式

《利用udev、sys动态创建linux设备结点》相关文章

分类导航
最新更新
热门排行