码迷,mamicode.com
首页 > 其他好文 > 详细

总线、设备和驱动

时间:2018-03-30 01:18:23      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:tar   固定   lap   auth   amp   void   gif   exit   总线   

http://blog.chinaunix.net/uid-25014876-id-109733.html

技术分享图片
 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 
 4 #include <linux/device.h>
 5 
 6 #define VER_SIZE 100
 7 char Version[VER_SIZE] = "xiaobai V1.0";
 8 
 9 
10 struct bus_type bus_type_wsz = {   // 1、定义bus_type结构体
11     .name = "wszbus"
12 };
13 
14 
15 static ssize_t show_bus_version(struct bus_type *bus, char *buf)
16 {
17     return snprintf(buf, VER_SIZE, "%s\n", Version);
18 }
19 static ssize_t store_bus_version(struct bus_type *bus, const char *buf, size_t count)
20 {
21     return snprintf(Version, VER_SIZE, "%s", buf);
22 }
23 
24 
25 /*
26 3、创建并初始化bus_attribute结构,使用宏BUS_ATTR
27 
28 该宏会定义一个名叫bus_attr_name(红色部分是固定的(bus_attr_))的bus_attibute的结构,
29 并且成员name设置为name,文件权限mode设置为_mode,两个函数调用分别人show和store。
30 <linux/device.h>
31 */
32 static BUS_ATTR(version, S_IRUGO|S_IWUGO, show_bus_version, store_bus_version); 
33 
34 static int __init bus_wsz_init(void)
35 {
36     int ret;
37     ret = bus_register(&bus_type_wsz);  // 2、注册总线
38     if(ret)
39     {
40         printk("bus register failed!\n");
41         return ret;    
42     }
43 
44 /*
45     4、将bus_attibute添加到指定的总线上
46     
47     一旦调用该函数,会就在指定bus总线的目录下新建一个名叫_name的文件,
48     权限为_mode,当访问和修改该文件是会分别调用show和store函数调用?
49 */
50     ret = bus_create_file(&bus_type_wsz, &bus_attr_version);
51     if(ret)
52     {
53         printk("bus creat file failed!\n");
54         bus_unregister(&bus_type_wsz);
55         return ret;    
56     }
57     
58     printk("wsz bus init\n");
59     return 0;
60 
61 }
62 
63 static void __exit bus_wsz_exit(void)
64 {
65     bus_unregister(&bus_type_wsz);
66     bus_remove_file(&bus_type_wsz, &bus_attr_version); //不调用这个也可以的,删除总线时会删除
67     printk("bus_wsz bye!\n");
68 }
69 
70 
71 
72 module_init(bus_wsz_init);
73 module_exit(bus_wsz_exit);
74 
75 MODULE_LICENSE("GPL");
76 MODULE_AUTHOR("WSZ");
bus

 

总线、设备和驱动

标签:tar   固定   lap   auth   amp   void   gif   exit   总线   

原文地址:https://www.cnblogs.com/wszdezh/p/8673302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!