linux中将一个挂载在总线上的驱动的驱动模型分为三部分:device、driver和bus;
device部分:用来保存设备信息对象,在内核中一个klist_device链表中进行管理;
driver部分:用来保存驱动信息对象,在内核中一个klist_driver链表中进行管理;
bus部分:负责完成device和driver到的匹配,通过总线驱动中的match函数来实现;
当device和driver匹配成功后执行driver端的probe函数,在probe函数中完成驱动的注册、设备节点的创建、以及后续的硬件控制工作。

为了让没有挂载在总线上的设备也能够按照总线驱动模型进行驱动的编写,我们引入了paltform总线,引入platform统一我们的设备驱动模型。
platform是一段内核抽象出来的总线驱动代码,但是现实中并没有和platform总线驱动对应的真实总线,它的作用就是管理没有挂载在总线上的设备,让这些设备有也可以按照总线驱动模型编写驱动。
将一个platform总线驱动模型分为三部分:设备端、驱动端、总线端。由总线负责完成驱动和设备信息的匹配,当匹配成功之后会执行驱动端的probe函数。在probe函数中实现驱动的注册、设备节点的创建以及后续的硬件控制工作。


- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- #include <linux/mod_devicetable.h>
- #include <linux/of_gpio.h>
- #include <linux/gpio.h>
- #include <linux/io.h>
- #include <linux/device.h>
- #include <linux/uaccess.h>
- #include <linux/wait.h>
- #include <linux/of.h>
- #include <linux/of_irq.h>
- #include <linux/interrupt.h>
- #include <linux/slab.h>
- #include <linux/wait.h>
-
- // 定义一个等待队列头
- wait_queue_head_t wq_head;
- unsigned int condition = 0;
- unsigned int major;
- struct class *cls;
- struct device *dev;
- char kbuf[128] = {0};
- struct resource *res;
- unsigned int irqno;
- struct gpio_desc *gpiono;
- unsigned int number = 0;
-
- // 封装操作方法
- int mycdev_open(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
- return 0;
- }
-
- ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
- {
- int ret;
- // 判断IO方式
- if (file->f_flags & O_NONBLOCK) // 非阻塞
- {
- return -EINVAL;
- }
- else // 阻塞
- {
- wait_event_interruptible(wq_head, condition); // 先检查condition再将进程休眠
- }
- //将数据拷贝到用户空间
- ret = copy_to_user(ubuf, (void*)&number, size);
- if (ret)
- {
- printk("copy_to_ user err\n");
- return -EIO;
- }
-
- condition = 0; // 让下一次硬件数据没有就绪
-
- return 0;
- }
- ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
- {
- printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
- return 0;
- }
-
- int mycdev_close(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
- return 0;
- }
-
- struct file_operations fops = {
- .open = mycdev_open,
- .read = mycdev_read,
- .write = mycdev_write,
- .release = mycdev_close,
- };
-
- // 定义中断处理函数
- irqreturn_t key_handler(int irq, void *dev)
- {
- number = gpiod_get_value(gpiono); //获取led管脚值
- number = !number; //灯状态取反
- gpiod_set_value(gpiono, number); //重写管脚状态值
-
- condition = 1; // 表示硬件数据就绪
- wake_up_interruptible(&wq_head);
-
- return IRQ_HANDLED;
- }
-
- // 封装probe函数
- int pdri_probe(struct platform_device *pdev)
- {
- // 1初始化等待队列
- init_waitqueue_head(&wq_head);
-
- // 2字符设备驱动注册
- major = register_chrdev(0, "myled0", &fops);
- if (major < 0)
- {
- printk("字符设备驱动注册失败\n");
- return major;
- goto ERR1;
- }
- printk("字符设备驱动注册成功:major=%d\n", major);
-
- // 3向上提交目录
- cls = class_create(THIS_MODULE, "MYLED");
- if (IS_ERR(cls))
- {
- printk("向上提交目录失败\n");
- return -PTR_ERR(cls);
- goto ERR2;
- }
- printk("向上提交目录成功\n");
-
- // 4向上提交设备节点信息
- dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myled0");
- if (IS_ERR(dev))
- {
- printk("向上提交设备节点信息失败\n");
- return -PTR_ERR(dev);
- goto ERR3;
- }
-
- printk("向上提交设备节点信息成功\n");
-
- // 5基于设备数节点信息获取gpio_desc对象指针
- gpiono = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
- if (IS_ERR(gpiono))
- {
- printk("解析GPIO管脚信息失败\n");
- return -ENXIO;
- goto ERR4;
- }
- printk("解析GPIO管脚信息成功\n");
-
- // 获取中断类型的资源,中断号
- irqno = platform_get_irq(pdev, 0);
- if (irqno < 0)
- {
- printk("获取中断类型资源失败\n");
- return -ENXIO;
- goto ERR5;
- }
- printk("key1_irq资源:%d\n", irqno);
-
- // 注册按键1中断
- int ret = request_irq(irqno, key_handler, IRQF_TRIGGER_FALLING, "key1_int", NULL);
- if (ret < 0)
- {
- printk("注册按键1中断失败\n");
- return ret;
- goto ERR5;
- }
- printk("注册按键1中断成功\n");
-
- printk("%s-%s-%d\n", __FILE__, __func__, __LINE__);
- ERR5:
- gpiod_put(gpiono);
- ERR4:
- device_destroy(cls, MKDEV(major, 0));
- ERR3:
- class_destroy(cls);
- ERR2:
- unregister_chrdev(major,"myled");
- ERR1:
- return ret;
- return 0;
- }
- // 封装remove函数
- int pdri_remove(struct platform_device *pdev)
- {
- // 注销中断
- free_irq(irqno, NULL);
-
- //释放GPIO管脚信息
- gpiod_put(gpiono);
-
- // 1销毁设备节点信息
- device_destroy(cls, MKDEV(major, 0));
-
- // 2销毁目录信息
- class_destroy(cls);
-
- // 3字符设备驱动注销
- unregister_chrdev(major, "myled");
-
- printk("%s-%s-%d\n", __FILE__, __func__, __LINE__);
- return 0;
- }
- // 构建设备树匹配表
- struct of_device_id oftable[] = {
- {.compatible = "myplatform"},
- {}, // 防止数组越界
- };
-
- // 定义驱动信息对象并初始化
- struct platform_driver pdri = {
- .probe = pdri_probe,
- .remove = pdri_remove,
- .driver = {
- .name = "ccc",
- .of_match_table = oftable,
- },
- };
-
- module_platform_driver(pdri); // 一键注册宏
- MODULE_LICENSE("GPL");
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
-
- int main(int argc,char const *argv[])
- {
- char buf[128]={0};
- int fd = open("/dev/myled0",O_RDWR);
-
- if(fd < 0)
- {
- printf("设备文件打开失败\n");
- exit(-1);
- }
- while(1)
- {
- //读取number的值
- read(fd,buf,sizeof(buf));
- printf("number = %s\n",buf);
- }
-
- close(fd);
-
- return 0;
- }

