总线_设备_驱动注册流程详解

注册流程图

• 设备一般都需要先注册,才能注册驱动
– 现在越来越多的热拔插设备,反过来了。先注册驱动,设备来了再注册

设备

• 本节使用的命令
– 查看总线的命令#ls /sys/bus/
– 查看设备号的命令#cat /proc/devices
• 设备都有主设备号和次设备号,否则255个设备号不够用
– 查看杂项设备号的命令#cat /proc/misc

二、总线设备注册

关于注册设备的一点说明

• 早先的Linux会使用单独的文件注册设备,现在大多是使用引入的虚拟平台,使用虚拟平台来注册设备会容易很多
• 如果大家在网上看到大段的注册设备的代码,可简单了解一下,知道有这么个东西就成,不用去深究。不要去学习“屠龙技”,现在基本都是直接在平台文件中注册设备

注册设备
• 注册设备使用结构体platform_device,该结构体在头文件“vim include/linux/platform_device.h”中。头文件中也有注册设备和卸载设备的函数,了解即可

操作过程

– 注册设备。将设备结构体放到平台文件中,会自动注册设备,不用去调用注册设备的函数。

vim arch/arm/mach-exynos/mach-itop4412.c

添加设备注册

#ifdef CONFIG_HELLO_CTL
struct platform_device s3c_device_hello_ctl = {
.name = "hello_ctl",
.id = -,
};
#endif #ifdef CONFIG_LEDS_CTL
struct platform_device s3c_device_leds_ctl = {
.name = "leds",
.id = -,
};
#endif

和这个

#ifdef CONFIG_HELLO_CTL
&s3c_device_hello_ctl,
#endif #ifdef CONFIG_LEDS_CTL
&s3c_device_leds_ctl,
#endif

– 在Kconfig文件中添加编译HELLO设备的宏定义(前面教程中已经添加)
– 配置menuconfig中的HELLO宏定义,生成新的.config文件
– 生成新的zImage
• 注册完之后在虚拟平台总线下可以查到注册的设备
– ls /sys/devices/platform/

中间遇到的问题:编译make zImage的时候提示

arch/arm/mach-exynos/mach-itop4412.c:: error: 's3c_device_hello_ctl' undeclared here (not in a function)

这个的没有写正确

#ifdef CONFIG_HELLO_CTL
struct platform_device s3c_device_hello_ctl = {

三、总线驱动注册

头文件

• 驱动注册使用结构体platform_driver,该结构体在头文件“vim include/linux/platform_device.h”中
• 驱动注册platform_driver_register,驱动卸载函数 platform_driver_unregister也在这个头文件中
– 这两个函数的参数都只有结构体platform_driver

struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};

注册结构体

• 驱动常见的几种状态,初始化,移除,休眠,复位
– 就像PC一样,有的驱动休眠之后无法使用,有的可以使用;有的系统唤醒之后,驱动需要重新启动才能正常工作,也有直接就可以使用等等
• probe函数:platform_match函数匹配之后,驱动调用的初始化函数
• remove函数: 移除驱动函数
• suspend函数: 悬挂(休眠)驱动函数

• resume函数: 休眠后恢复驱动
• device_driver数据结构的两个参数
– name和注册的设备name要一致
– owner一般赋值THIS_MODULE

#include <linux/module.h>
#include <linux/init.h> /* device register header file, include device and driver struct
* register and remove function */
#include <linux/platform_device.h> #define DRIVER_NAME "hello_ctl" MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET"); static int hello_probe(struct platform_driver *pdv)
{
printk(KERN_EMERG "\tinitialized\n");
return ;
} static int hello_remove(struct platform_driver *pdv)
{
return ;
} static void hello_shutdown(struct platform_driver *pdv)
{ } static int hello_suspend(struct platform_driver *pdv)
{
return ;
} static int hello_resume(struct platform_driver *pdv)
{
return ;
} struct platform_driver hello_driver = {
.probe = hello_probe,
.remove = hello_remove,
.shutdown = hello_shutdown,
.suspend = hello_suspend,
.resume = hello_resume,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
}; static int hello_init(void)
{
int DriverState; printk(KERN_EMERG "Hello world enter!\n");
DriverState = platform_driver_register(&hello_driver); printk(KERN_EMERG "\tDriverState is %d\n", DriverState);
return ;
} static void hello_exit(void)
{
printk(KERN_EMERG "Hello world exit!\n");
platform_driver_unregister(&hello_driver);
} module_init(hello_init);
module_exit(hello_exit);

register driver

实验

• 在mini_linux_module的基础上添加驱动注册部分
• 编译,在开发板上加载和卸载驱动

mount -t nfs -o nolock 192.168.2.147:/home/topeet/linux /mnt/nfs

挂载了nfs后,加载内核后

[root@iTOP-]# insmod probe_linux_module.ko
[ 8454.570719] Hello world enter!
[ 8454.572614] initialized
[ 8454.582781] DriverState is
[root@iTOP-4412]# rmmod probe_linux_module                                                                         
[ 8647.985422] Hello world exit!

4412 Linux设备总线的更多相关文章

  1. Linux 设备总线驱动模型

    尽管LDD3中说对多数程序员掌握设备驱动模型不是必要的,但对于嵌入式Linux的底层程序员而言,对设备驱动模型的学习非常重要.     Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一 ...

  2. Linux设备总线

    kobject和kset是Linux设备模型中最基本的元素,其中,kset是同种类型kobject对象的集合.每个在内核中注册的kobject对象都对于sysfs文件系统中的一个目录.下面是自己花的一 ...

  3. linux设备驱动归纳总结(九):1.platform总线的设备和驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-111745.html linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxx ...

  4. linux设备驱动归纳总结(八):4.总线热插拔【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-110774.html linux设备驱动归纳总结(八):4.总线热插拔 xxxxxxxxxxxxxxx ...

  5. linux设备驱动归纳总结(八):2.总线、设备和驱动的关系【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-110295.html linux设备驱动归纳总结(八):2.总线.设备和驱动的关系 xxxxxxxxx ...

  6. linux设备驱动归纳总结(八):1.总线、设备和驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-109733.html linux设备驱动归纳总结(八):1.总线.设备和驱动 xxxxxxxxxxxx ...

  7. Linux设备模型(总线、设备、驱动程序和类)

    Linux设备驱动程序学习(13) -Linux设备模型(总线.设备.驱动程序和类)[转] 文章的例子和实验使用<LDD3>所配的lddbus模块(稍作修改). 提示:在学习这部分内容是一 ...

  8. linux PMBus总线及设备驱动分析

    PMBus协议规范介绍 PMBus是一套对电源进行配置.控制和监控的通讯协议标准.其最新版本为1.3,该规范还在不断演进中,比如新标准中新增的zone PMBus.AVSBus等特性.在其官网上有详细 ...

  9. 芯灵思SinlinxA33开发板 Linux平台总线设备驱动

    1.什么是platform(平台)总线? 相对于USB.PCI.I2C.SPI等物理总线来说,platform总线是一种虚拟.抽象出来的总线,实际中并不存在这样的总线. 那为什么需要platform总 ...

随机推荐

  1. 前端基础知识-----HTML

    一.HTML基础概述 HTML:超文本标记语言(英语:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准语言.也就是一般我们在浏览器里看到的东西的书写格式,与 ...

  2. jmeter3.0+ant1.10+jenkins实现接口自动化并发送邮件

    有很多关于接口自动化的文章,此篇仅用于记录自己的学习用.使用jmeter3.0+ant1.10+jenkins2.实现接口自动化并发送邮件,本篇是用的编写build文件来实现发送邮件,也可以用jenk ...

  3. Drone 持续集成实践 - 基于 Gogs,以 Golang 为例

    Drone 官方示例 - Example Go project 用 Docker 部署 Go 服务器 Golang 官方示例 - outyet 一个生产环境的例子 用 rsync 复制文件的方式进行部 ...

  4. WEB服务端安全---文件上传漏洞

    1.简述 文件上传漏洞是指用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务端命令的能力.这种攻击方式是最直接和有效的,而且互联网中我们经常会用到文件上传功能,它本身是没有问题的,正常的业 ...

  5. linux + eclipse + cdt 报错undefined reference......好麻烦的,这位大牛给出的方法可行,特此MARK!!!!

    http://bbs.csdn.net/topics/390239632 kerosun kerosun 等级: 结帖率:96.92% 楼主 发表于: 2012-10-11 12:00:51   比如 ...

  6. CSAPP:局部性原理

    一个编写良好的计算机程序常常具有良好的局部性(locality).局部性通常有两种不同的形式:时间局部性(temporal locality)和空间局部性(spatial locality).在一个具 ...

  7. (转载)如何在 Github 上发现优秀的开源项目?

    转载自:传送门 之前发过一系列有关 GitHub 的文章,有同学问了,GitHub 我大概了解了,Git 也差不多会使用了,但是还是搞不清 GitHub 如何帮助我的工作,怎么提升我的工作效率? 问到 ...

  8. mac安装pip并升级pip版本

      最近想安装inchat,直接使用命令pip install install,结果提示 使用提示中的命令升级,结果提示找不到pip.很郁闷,明明有pip,结果一升级还给升没了.最后用的是这个方法完美 ...

  9. C# 身份证号码验证正则和验证函数

    做身份证验证的时候要求能够按照标准18位身份证验证,普通正则表达式不能满足需求,所以在网上找到了这个函数,很好用,虽然还是有漏洞,不过一般乱填的号码都能被屏蔽掉 身份证验证函数(标准18位验证) pr ...

  10. c++ 由无向图构造邻接表,实现深度优先遍历、广度优先遍历。

    /* 首先,根据用户输入的顶点总数和边数,构造无向图,然后以用户输入的顶点 为起始点,进行深度优先.广度优先搜索遍历,并输出遍历的结果. */ #include <stdlib.h> #i ...