Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360897
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-08-15 09:52:41

1、首先定义一个bus,完成insmod后可以在sys/bus下看见
  1. #include<linux/module.h>
  2. #include<linux/kernel.h>
  3. #include<linux/init.h>

  4. #include<linux/string.h>
  5. #include<linux/device.h>

  6. MODULE_AUTHOR("AW");
  7. MODULE_LICENSE("GPL");

  8. static char *Version="imlink 2.0";
  9. static int my_match(struct device *dev,struct device_driver *driver)
  10. {
  11.     int result=0;
  12.     printk("<1>Now prepare to compare!!!!");
  13.     printk("<1>the device init_name is %s\n",dev->init_name);
  14.     printk("<1>the new ini_name is %s\n",(dev_name(dev)));
  15.     printk("<1>the driver name is %s\n",driver->name);
  16.     result=strncmp(dev_name(dev),driver->name,strlen(driver->name));
  17.     
  18.     if(result==0)
  19.         return 1;
  20.     else
  21.         return 0;
  22. }

  23. static void my_bus_release(struct device *dev)
  24. {
  25.     printk("<1>my bus released!");
  26. }


  27. struct device my_bus=
  28. {
  29.     .init_name="my_bus0",
  30.     .release=my_bus_release,
  31. };


  32. struct bus_type my_bus_type=
  33. {
  34.     .name="my_bus2",
  35.     .match=my_match,
  36. };

  37. EXPORT_SYMBOL(my_bus);
  38. EXPORT_SYMBOL(my_bus_type);

  39. static ssize_t show_bus_version(struct bus_type *bus,char *buf)
  40. {
  41.     return snprintf(buf,PAGE_SIZE,"%s\n",Version);
  42. }

  43. static BUS_ATTR(version,S_IRUGO,show_bus_version,NULL);

  44. static int my_bus_init(void)
  45. {
  46.     int ret=bus_register(&my_bus_type);
  47.     if(ret)
  48.     {
  49.         return ret;
  50.     }

  51.     if(bus_create_file(&my_bus_type,&bus_attr_version))
  52.     {
  53.         printk("<1>Fail to create version attribute!\n");
  54.     }

  55.     ret=device_register(&my_bus);
  56.     
  57.     if(ret)
  58.     {
  59.         printk("<1>Fail to register device :my_bus\n");
  60.     }
  61.     return ret;
  62. }

  63. static void my_bus_exit(void)
  64. {
  65.     device_unregister(&my_bus);
  66.     bus_unregister(&my_bus_type);
  67. }

  68. module_init(my_bus_init);
  69. module_exit(my_bus_exit);


2、然后注册1个设备

  1. #include<linux/kernel.h>
  2. #include<linux/module.h>
  3. #include<linux/init.h>

  4. #include<linux/string.h>
  5. #include<linux/device.h>

  6. MODULE_AUTHOR("AW");
  7. MODULE_LICENSE("GPL");

  8. extern struct device my_bus;
  9. extern struct bus_type my_bus_type;

  10. static void my_dev_release(struct device *dev)
  11. {

  12. }

  13. struct device my_dev=
  14. {
  15.     .init_name="my_dev1",
  16.     .bus=&my_bus_type,
  17.     .parent=&my_bus,
  18.     .release=my_dev_release,
  19. };

  20. static ssize_t my_dev_show(struct device *dev,struct device_attribute *attr,char *buf)
  21. {
  22.     return sprintf(buf,"%s\n","this is my device");
  23. }

  24. static DEVICE_ATTR(dev,S_IRUGO,my_dev_show,NULL);

  25. static int my_device_init(void)
  26. {
  27.     int ret=0;
  28.     device_register(&my_dev);
  29.     device_create_file(&my_dev,&dev_attr_dev);
  30.     return ret;
  31. }

  32. static void my_device_exit(void)
  33. {
  34.     device_unregister(&my_dev);
  35. }

  36. module_init(my_device_init);
  37. module_exit(my_device_exit);

3、然后注册1个驱动

  1. #include <linux/device.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>

  6. MODULE_AUTHOR("AW");
  7. MODULE_LICENSE("GPL");

  8. extern struct bus_type my_bus_type;

  9. static int my_probe(struct device *dev)
  10. {
  11.     printk("Driver found device which my driver can handle!\n");
  12.     return 0;
  13. }

  14. static int my_remove(struct device *dev)
  15. {
  16.     printk("Driver found device unpluged!\n");
  17.     return 0;
  18. }

  19. struct device_driver my_driver = {
  20.     .name = "my_dev1",
  21.     .bus = &my_bus_type,
  22.     .probe = my_probe,
  23.         .remove    = my_remove,
  24. };

  25. static ssize_t mydriver_show(struct device_driver *driver, char *buf)
  26. {
  27.     return sprintf(buf, "%s\n", "This is my driver!");
  28. }

  29. static DRIVER_ATTR(drv, S_IRUGO, mydriver_show, NULL);

  30. static int my_driver_init(void)
  31. {

  32.     int ret=0;
  33.     driver_register(&my_driver);
  34.         
  35.     //driver_create_file(&my_driver, &driver_attr_drv);
  36.     
  37.     return ret;    

  38. }

  39. static void my_driver_exit(void)
  40. {
  41.     driver_unregister(&my_driver);
  42. }

  43. module_init(my_driver_init);
  44. module_exit(my_driver_exit);

其中2,3的顺序可以反,没有影响



阅读(746) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~