Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28679
  • 博文数量: 12
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-17 19:57
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(11)

我的朋友

分类: LINUX

2009-06-03 11:08:41

通过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>   // 1
 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; 2
 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  3

         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"); 4
         // 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);

阅读(429) | 评论(1) | 转发(0) |
0

上一篇:xpE学习

下一篇:linux的磁盘shell

给主人留下些什么吧!~~

chinaunix网友2009-06-08 11:13:18

好服月租型IT服务台,与你共成长! 月租型ITSM软件,注册即可免费体验! 详情请登录官方网站:http://www.servicezon.com