【Linux驱动开发】005 新字符设备驱动实验

一、 新字符设备驱动原理 

1、新字符设备 -- 分配和释放设备号

使用 register_chrdev 函数注册字符设备(旧字符设备)的时候只需要给定一个主设备号即可,但是会导致:

  • 需要我们事先确定好哪些主设备号没有使用;
  • 会将一个主设备号下的所有次设备号都使用掉。

新字符设备的解决方法:需要多少设备号就申请多少。

两种情况:

1、没有指定设备号: 

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name) 

2、给定主设备号和次设备号:

int register_chrdev_region(dev_t from, unsigned count, const char *name) 

以上两种申请方式对应的释放函数:

void unregister_chrdev_region(dev_t from, unsigned count) 

2、新字符设备 -- 注册方法

旧字符设备使用 register_chrdev + 主设备号 + file_operations结构体 直接注册。在新字符设备中使用 cdev 结构体表示字符设备,cdev_init 函数对其进行初始化,然后使用 cdev_add 函数向 Linux 系统添加这个字符设备。

1 struct cdev { 
2     struct kobject   kobj; 
3     struct module    *owner; 
4     const struct file_operations *ops; // 字符设备文件操作函数集合
5     struct list_head  list; 
6     dev_t        dev; // 设备号
7     unsigned int    count; 
8 }; 


// 编写字符设备驱动之前需要定义一个 cdev 结构体变量,这个变量就表示一个字符设备
struct cdev test_cdev; 
// 参数 cdev 就是要初始化的 cdev 结构体变量,参数 fops 就是字符设备文件操作函数集合。
void cdev_init(struct cdev *cdev, const struct file_operations *fops) 
// 参数 p 指向要添加的字符设备(cdev 结构体变量),参数 dev 就是设备所使用的设备号,参数 count 是要添加的设备数量。
int cdev_add(struct cdev *p, dev_t dev, unsigned count) 

如果在加上示例代码上一小节中分配设备号的程序,那么就它们一起实现的就是函数 register_chrdev 的功能。 

// 新的注册字符设备代码段
1  struct cdev testcdev; 
2   
3  /* 设备操作函数 */ 
4  static struct file_operations test_fops = { 
5     .owner = THIS_MODULE, 
6     /* 其他具体的初始项 */ 
7  }; 
8   
9  testcdev.owner = THIS_MODULE; 
10 cdev_init(&testcdev, &test_fops);      /* 初始化 cdev 结构体变量  */ 
11 cdev_add(&testcdev, devid, 1);         /* 添加字符设备         */ 

卸载驱动的时候一定要使用 cdev_del 函数从 Linux 内核中删除相应的字符设备。

void cdev_del(struct cdev *p) // 函数原型

cdev_del(&testcdev);  //  删除 cdev 

3、总结

其实旧设备驱动的 register_chrdev(主设备号 + file_operations结构体),就等于新设备驱动的设备号分配函数 初始化函数(cdev + file_operations结构体) + add函数 。


二、udev(mdev) - class - device自动创建设备文件

在《003 驱动模块加载、卸载、测试》(旧字符设备驱动)中,当我们使用 modprobe 加载驱动程序后还需要使用 “mknod” 手动创建设备节点。本节就来讲解一下如何实现自动创建设备节点。

1、udev(mdev)

udev 是一个用户程序,udev 可以检测系统中硬件设备状态,可以根据系统中硬件设备状态来创建或者删除设备文件(模块加载成功后创建设备文件,模块卸载成功后删除设备文件)。mdev是udev 的简化版本,嵌入式Linux中使用mdev来实现设备结点文件的自动创建与删除。

  1. class_create(在sys/class里面去创建一个目录,并赋予一些属性方便后期操作)和device_create(在dev目录下创建一个字符设备) 会在虚拟文件系统生成信息;
  2. 然后 mdev 或者 udev 根据信息创建设备节点。

class_create和device_create与mdev和udev关系_aningxiaoxixi的博客-CSDN博客

class_create和device_create与mdev和udev关系_device_create mdev_艾特号的博客-CSDN博客linux驱动学习加强版-4(class_create和device_create)

2、class、device创建与删除

自动创建设备节点的工作是在驱动程序的入口函数中完成的,一般在 cdev_add 函数后面添
加自动创建设备节点相关代码
(前面已经完成了字符设备驱动的注册)。

首先要创建一个 class 类,还需要使用 device_create 函数在这个类下创建一个设备。

class与device的关系:

一个class可以看成一个容器(一个子系统),包含了很多的class_device,这些class_device是由class这个大容器来管理的,而每个class_device都对应着一个具体的设备。

设备模型中的class所提供的功能也一样了,例如一些相似的device(学生),需要向用户空间提供相似的接口(课程),如果每个设备的驱动都实现一遍的话,就会导致内核有大量的冗余代码,这就是极大的浪费。

设备类创建: 

// 参数 owner 一般为 THIS_MODULE,参数 name 是类名字
struct class *class_create (struct module *owner, const char *name) 

设备创建: 

struct device *device_create(struct class    *class,   //设备要创建哪个类下面
                             struct device   *parent,  // 父设备
                             dev_t           devt,     // 设备号
                             void            *drvdata, // 可能会使用的数据,一般NULL
                             const char      *fmt, ...)// 设备名

卸载驱动程序的时候(驱动出口函数)需要删除掉设备类与设备:

void class_destroy(struct class *cls); // 删除设备类
void device_destroy(struct class *class, dev_t devt); // 删除设备

3、示例

1  struct class *class;      /* 类       */        
2  struct device *device;    /* 设备     */ 
3  dev_t devid;              /* 设备号   */          
4   
5  /* 驱动入口函数 */  
6  static int __init led_init(void) 
7  { 
8     /* 创建类   */ 
9     class = class_create(THIS_MODULE, "xxx"); 
10     /* 创建设备 */ 
11    device = device_create(class, NULL, devid, NULL, "xxx"); 
12    return 0; 
13 } 
14  
15 /* 驱动出口函数 */ 
16 static void __exit led_exit(void) 
17 { 
18    /* 删除设备 */ 
19    device_destroy(newchrled.class, newchrled.devid); 
20    /* 删除类   */ 
21    class_destroy(newchrled.class); 
22 } 
23  
24 module_init(led_init); 
25 module_exit(led_exit); 

 4、总结

旧字符设备驱动是手动创建设备结点,而新字符设备驱动通过使用mdev,创建class、device自动生成设备结点。


三、设置文件私有数据

1、设置文件私有数据

前面好多变量,东一个西一个的,不如直接将设备驱动的所有属性信息其放到一个结构体中。

/* 申请或者指定的设备号 */
dev_t devid;              /* 设备号     */ 
int major;                /* 主设备号   */ 
int minor;                /* 次设备号   */ 

/* 包含操作函数集合 */
/* cdev中的设备号,是通过初始化函数将上面的设备号放进去的*/
struct cdev cdev;         /* 字符设备   */ 

/* 自动创建设备结点文件 */
struct class *class;      /* 设备类     */ 
struct device *device;    /* 设备       */ 

编写驱动 open 函数的时候将设备结构体作为私有数据添加到设备文件中,在 write、read、close 等函数中直接读取 private_data 即可得到设备结构体。(open的时候有标志:譬如只读、只写、阻塞等等,write、read、close 等函数在访问设备结构体的时候,必须根据open的属性访问)

/* 设备结构体 */ 
1  struct test_dev{ 
2     dev_t devid;              /* 设备号       */ 
3     struct cdev cdev;         /* cdev        */ 
4     struct class *class;      /* 类          */ 
5     struct device *device;    /* 设备        */ 
6     int major;                /* 主设备号    */ 
7     int minor;                /* 次设备号    */ 
8  }; 
9   
10 struct test_dev testdev; 
11   
12 /* open 函数 */ 
13 static int test_open(struct inode *inode, struct file *filp) 
14 { 
15    filp->private_data = &testdev; /* 设置私有数据 */ 
16    return 0; 
17 } 

2、总结

旧字符设备驱动没有设置文件私有数据,新字符设备将驱动属性信息封装成结构体,并在open函数中设置文件私有数据。


四、LED灯驱动程序

1   #include <linux/types.h> 
2   #include <linux/kernel.h> 
3   #include <linux/delay.h> 
4   #include <linux/ide.h> 
5   #include <linux/init.h> 
6   #include <linux/module.h> 
7   #include <linux/errno.h> 
8   #include <linux/gpio.h> 
9   #include <linux/cdev.h> 
10  #include <linux/device.h> 
11  #include <asm/mach/map.h> 
12  #include <asm/uaccess.h> 
13  #include <asm/io.h> 
14   
25  #define NEWCHRLED_CNT        1                /* 设备号个数   */ 
26  #define NEWCHRLED_NAME       "newchrled"      /* 名字     */ 
27  #define LEDOFF                 0              /* 关灯     */ 
28  #define LEDON                  1              /* 开灯     */ 
29    
30  /* 寄存器物理地址 */ 
31  #define CCM_CCGR1_BASE               (0X020C406C)     
32  #define SW_MUX_GPIO1_IO03_BASE       (0X020E0068) 
33  #define SW_PAD_GPIO1_IO03_BASE       (0X020E02F4) 
34  #define GPIO1_DR_BASE                (0X0209C000) 
35  #define GPIO1_GDIR_BASE              (0X0209C004) 
36   
37  /* 映射后的寄存器虚拟地址指针 */ 
38  static void __iomem *IMX6U_CCM_CCGR1; 
39  static void __iomem *SW_MUX_GPIO1_IO03; 
40  static void __iomem *SW_PAD_GPIO1_IO03; 
41  static void __iomem *GPIO1_DR; 
42  static void __iomem *GPIO1_GDIR; 
43   
44  /* newchrled 设备结构体 */ 
45  struct newchrled_dev{ 
46      dev_t devid;                /* 设备号    */ 
47      struct cdev cdev;           /* cdev    */ 
48      struct class *class;        /* 类       */ 
49      struct device *device;     /* 设备     */ 
50      int major;                  /* 主设备号 */ 
51      int minor;                  /* 次设备号 */ 
52  }; 
53   
54  struct newchrled_dev newchrled; /* led 设备 */ 
55   
56  /* 
57   * @description  : LED 打开/关闭 
58   * @param - sta   : LEDON(0) 打开 LED,LEDOFF(1) 关闭 LED 
59   * @return         : 无 
60   */ 
61  void led_switch(u8 sta) 
62  { 
63      u32 val = 0; 
64      if(sta == LEDON) { 
65          val = readl(GPIO1_DR); 
66          val &= ~(1 << 3);    
67          writel(val, GPIO1_DR); 
68      }else if(sta == LEDOFF) { 
69          val = readl(GPIO1_DR); 
70          val|= (1 << 3);  
71          writel(val, GPIO1_DR); 
72      }    
73  } 
74   
75  /* 
76   * @description   : 打开设备 
77   * @param – inode : 传递给驱动的 inode 
78   * @param - filp  : 设备文件,file 结构体有个叫做 private_data 的成员变量 
79   *                    一般在 open 的时候将 private_data 指向设备结构体。 
80   * @return         : 0 成功;其他 失败 
81   */ 
82  static int led_open(struct inode *inode, struct file *filp) 
83  { 
84      filp->private_data = &newchrled; /* 设置私有数据 */ 
85      return 0; 
86  } 
87   
88  /* 
89   * @description   : 从设备读取数据  
90   * @param - filp  : 要打开的设备文件(文件描述符) 
91   * @param - buf   : 返回给用户空间的数据缓冲区 
92   * @param - cnt   : 要读取的数据长度 
93   * @param – offt  : 相对于文件首地址的偏移 
94   * @return         : 读取的字节数,如果为负值,表示读取失败 
95   */ 
96  static ssize_t led_read(struct file *filp, char __user *buf,  
size_t cnt, loff_t *offt) 
97  { 
98      return 0; 
99  } 
100  
101 /* 
102  * @description   : 向设备写数据  
103  * @param – filp  : 设备文件,表示打开的文件描述符 
104  * @param - buf   : 要写给设备写入的数据 
105  * @param - cnt   : 要写入的数据长度 
106  * @param – offt  : 相对于文件首地址的偏移 
107  * @return         : 写入的字节数,如果为负值,表示写入失败 
108  */ 
109 static ssize_t led_write(struct file *filp, const char __user *buf,  
size_t cnt, loff_t *offt) 
110 { 
111     int retvalue; 
112     unsigned char databuf[1]; 
113     unsigned char ledstat; 
114  
115     retvalue = copy_from_user(databuf, buf, cnt); 
116     if(retvalue < 0) { 
117         printk("kernel write failed!\r\n"); 
118         return -EFAULT; 
119     } 
120  
121     ledstat = databuf[0];        /* 获取状态值   */ 
122  
123     if(ledstat == LEDON) {   
124         led_switch(LEDON);        /* 打开 LED 灯   */ 
125     } else if(ledstat == LEDOFF) { 
126         led_switch(LEDOFF);    /* 关闭 LED 灯   */ 
127     } 
128     return 0; 
129 } 
130  
131 /* 
132  * @description   : 关闭/释放设备 
133  * @param – filp  : 要关闭的设备文件(文件描述符) 
134  * @return         : 0 成功;其他 失败 
135  */ 
136 static int led_release(struct inode *inode, struct file *filp) 
137 { 
138     return 0; 
139 } 
140  
141 /* 设备操作函数 */ 
142 static struct file_operations newchrled_fops = { 
143     .owner = THIS_MODULE, 
144     .open = led_open, 
145     .read = led_read, 
146     .write = led_write, 
147     .release =  led_release, 
148 }; 
149  
150 /* 
151  * @description   : 驱动入口函数 
152  * @param         : 无 
153  * @return        : 无 
154  */ 
155 static int __init led_init(void) 
156 { 
157     u32 val = 0; 
158  
159     /* 初始化 LED */ 
160     /* 1、寄存器地址映射 */ 
161     IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4); 
162     SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4); 
163     SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4); 
164     GPIO1_DR = ioremap(GPIO1_DR_BASE, 4); 
165     GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4); 
166  
167     /* 2、使能 GPIO1 时钟 */ 
168     val = readl(IMX6U_CCM_CCGR1); 
169     val &= ~(3 << 26);  /* 清楚以前的设置 */ 
170     val |= (3 << 26);   /* 设置新值 */ 
171     writel(val, IMX6U_CCM_CCGR1); 
172  
173     /* 3、设置 GPIO1_IO03 的复用功能,将其复用为 
174      *    GPIO1_IO03,最后设置 IO 属性。 
175      */ 
176     writel(5, SW_MUX_GPIO1_IO03); 
177      
178     /* 寄存器 SW_PAD_GPIO1_IO03 设置 IO 属性 */ 
179     writel(0x10B0, SW_PAD_GPIO1_IO03); 
180  
181     /* 4、设置 GPIO1_IO03 为输出功能 */ 
182     val = readl(GPIO1_GDIR); 
183     val &= ~(1 << 3);   /* 清除以前的设置 */ 
184     val |= (1 << 3);    /* 设置为输出 */ 
185     writel(val, GPIO1_GDIR); 
186  
187     /* 5、默认关闭 LED */ 
188     val = readl(GPIO1_DR); 
189     val |= (1 << 3);     
190     writel(val, GPIO1_DR); 
191  
192     /* 注册字符设备驱动 */ 
193     /* 1、创建设备号 */ 
194     if (newchrled.major) {       /*  定义了设备号 */ 
195         newchrled.devid = MKDEV(newchrled.major, 0); 
196         register_chrdev_region(newchrled.devid, NEWCHRLED_CNT,  
NEWCHRLED_NAME); 
197     } else {                      /* 没有定义设备号 */ 
198         alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_CNT,  
NEWCHRLED_NAME);      /* 申请设备号 */ 
199         newchrled.major = MAJOR(newchrled.devid); /* 获取主设备号 */ 
200         newchrled.minor = MINOR(newchrled.devid); /* 获取次设备号 */ 
201     } 
202     printk("newcheled major=%d,minor=%d\r\n",newchrled.major,  
newchrled.minor);  
203      
204     /* 2、初始化 cdev */ 
205     newchrled.cdev.owner = THIS_MODULE; 
206     cdev_init(&newchrled.cdev, &newchrled_fops); 
207      
208     /* 3、添加一个 cdev */ 
209     cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_CNT); 
210  
211     /* 4、创建类 */ 
212     newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME); 
213     if (IS_ERR(newchrled.class)) { 
214         return PTR_ERR(newchrled.class); 
215     } 
216  
217     /* 5、创建设备 */ 
218     newchrled.device = device_create(newchrled.class, NULL,  
newchrled.devid, NULL, NEWCHRLED_NAME); 
219     if (IS_ERR(newchrled.device)) { 
220         return PTR_ERR(newchrled.device); 
221     } 
222      
223     return 0; 
224 } 
225  
226 /* 
227  * @description   : 驱动出口函数 
228  * @param         : 无 
229  * @return        : 无 
230  */ 
231 static void __exit led_exit(void) 
232 { 
233     /* 取消映射 */ 
234     iounmap(IMX6U_CCM_CCGR1); 
235     iounmap(SW_MUX_GPIO1_IO03); 
236     iounmap(SW_PAD_GPIO1_IO03); 
237     iounmap(GPIO1_DR); 
238     iounmap(GPIO1_GDIR); 
239  
240     /* 注销字符设备 */ 
241     cdev_del(&newchrled.cdev);/*  删除 cdev */ 
242     unregister_chrdev_region(newchrled.devid, NEWCHRLED_CNT); 
243  
244     device_destroy(newchrled.class, newchrled.devid); 
245     class_destroy(newchrled.class); 
246 } 
247  
248 module_init(led_init); 
249 module_exit(led_exit); 
250 MODULE_LICENSE("GPL"); 
251 MODULE_AUTHOR("zuozhongkai"); // 来自正点原子

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kashine

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值