EDA365电子论坛网
标题:
《Linux总线、设备与驱动》ldd3中demo分析
[打印本页]
作者:
pulbieup
时间:
2021-8-2 13:54
标题:
《Linux总线、设备与驱动》ldd3中demo分析
. Y% F2 Y" t+ B" \
一、古老方式(不支持热插拔)
) K0 F( q0 A3 u
/ t% M* ^+ W* G- T
1.开机前硬件设备已经插入总线;
0 F( N1 B8 W- G8 U3 E; {, Y
% O- a! w5 ~# a/ `+ {# m6 E
2.操作系统加载总线驱动,开始扫描设备、并为其申请struct device结构,最后挂入总线驱动devices链表;
% g: k: `; K8 v5 _3 V3 w5 h
# \; S! E3 I* b6 G& c& l: a9 `
3.操作系统加载设备驱动,注册struct device_driver结构,然后去总线驱动的devices链表中遍历没有绑定driver的设备(即struct device中struct device_driver为空的设备),如果匹配、就device_bind_driver。
# e5 r/ q9 M% m
+ e- k. o& c' A, j g
- T; i \" h$ k
二、现在方式(支持热插拔)
6 ]+ r) L! }1 O. ~
3 X6 [( H9 D/ q. i' s
1.操作系统加载总线驱动;
6 E& E- r: z( f( G% Q
% U. T3 H! F4 A- T
2.当有硬件插入时;总线驱动扫描到设备、并为其申请struct device,然后去总线驱动的drivers链表去遍历设备驱动;
: ]0 n+ ~! E" X7 o$ i }/ J* H
1 {2 C! f+ o6 Z1 ?
3.反之,当有设备驱动加载时;总线为其申请struct device_driver,然后去总线驱动的devices链表遍历设备。
0 w- |# w; c) M$ v2 Z! j2 b
- S9 y4 M9 P5 p. k u/ O
总结:由以上可以看出;总线驱动是核心,联系这设备和设备驱动。
m! d. u6 u' C- O5 Q
# M) q( L, }% ~; u
三、深入讲解
, A- \2 M$ i# ]; {1 e
% T% `6 D/ B% x6 @
1.数据结构kernel/include/linux/device.h
1 V3 z( W' }2 D# G( x# ?- o2 g# G
+ Y g( t3 h9 v
总线:struct bus_type;
' w$ e5 g3 ?- X0 ^# t
" D; i; w% d @$ A) ~! `
设备:struct device;
7 P3 p( k" `$ i p0 L
0 D, O5 Q1 @5 `3 `, j! G. V9 v
驱动:struct device_driver;
3 M$ x# \9 n% k
' p% F5 p( w/ |
2.关系
Q5 @, L! ]! A# n0 c
# O0 s: d+ y' S( h* p
总线驱动总会主动去扫描并为其上的设备分配struct device并添加到devices链表中;
9 _) v- c' a( ]4 t, u3 W6 f
2 J1 d$ m: u/ V' m
反之,总线驱动是被动的接受设备驱动的struct device_driver并添加到drivers链表中。
& C8 g4 b2 @" [
V3 E' U, b2 e" F. r
2 D) I7 }; y, h6 D9 n
四、实例-ldd3里边的例子
+ m6 t/ r; g- h8 E; C3 _0 S
; E6 d8 W% i% _- n! g# w( a! B9 e
1.总线驱动的注册与注销
+ e, \9 K2 i/ `3 n- D% B: ?
1 l [( E+ K0 T: m, s3 X2 b9 Q
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
//当一个总线上的新设备或者新驱动被添加时,会一次或者多次调用这个函数;用来匹配总线上的设备和驱动。
/*
static int ldd_match(struct device *dev, struct device_driver *driver){
//return !(strncmp(dev->bus_id, driver->name, strlen(driver->name));
return !strncmp(dev->init_name, driver->name, strlen(driver->name));
//change by tankai
//本测试demo,只是判断设备名和驱动名是否一样;实际可能会比较复杂,如USB是通过VID和PID
}
*/
.uevent = ldd_hotplug,
};
bus_register(&ldd_bus_type);
bus_unregister(&ldd_bus_type);
struct device ldd_bus = {
.bus_id = "ldd0",
.release = ldd_bus_release
};
device_register(&ldd_bus);
device_unregister(&ldd_bus);
7 w/ F6 y# L# F7 t8 z. T4 \7 `
! E9 s3 U& J& c
) B$ N2 N) }1 x {
2.总线驱动提供的设备驱动接口
+ C; S( d6 l) p0 _ U. r
/ w1 `$ r" Y- j( c
int register_ldd_driver(struct ldd_driver *driver){
int ret;
driver->driver.bus = &ldd_bus_type;
if (driver->probe)
driver->driver.probe = lddbus_drv_probe;
if (driver->remove)
driver->driver.remove = lddbus_drv_remove;
if (driver->shutdown)
driver->driver.shutdown = lddbus_drv_shutdown;
if (driver->suspend)
driver->driver.suspend = lddbus_drv_suspend;
if (driver->resume)
driver->driver.resume = lddbus_drv_resume;
ret = driver_register(&driver->driver);
//该函数很重要,会去bus上寻找匹配设备(间接调用总线的match接口)、如果匹配成功会调用驱动的探测函数
if (ret)
return ret;
driver->version_attr.attr.name = "version";
//driver->version_attr.attr.owner = driver->module;
driver->version_attr.attr.mode = S_IRUGO;
driver->version_attr.show = show_version;
driver->version_attr.store = NULL;
return driver_create_file(&driver->driver, &driver->version_attr);
}
% ~' j( o" j+ h5 ^* k4 w% z, A7 K
% Q% L7 K# c- ?& C# W% I1 @
$ q( b0 { {1 M+ K$ }' G7 R
3.总线在扫描到设备后的注册接口
/ Y% }# x4 d4 h# ~- B
8 L( s4 Y2 `& q, P3 x H
int register_ldd_device(struct ldd_device *ldddev){
ldddev->dev.bus = &ldd_bus_type;
ldddev->dev.parent = &ldd_bus;
ldddev->dev.release = ldd_dev_release;
//strncpy(ldddev->dev.bus_id, ldddev->name, BUS_ID_SIZE);
ldddev->dev.init_name = ldddev->name; //change by
tank@tcl.com
return device_register(&ldddev->dev);
//该函数很重要,会去bus上寻找匹配驱动(间接调用总线的match接口)、如果匹配成功会调用驱动的探测函数
}
, G# ~: I# G( f0 P
- r+ k& r" v8 Q% D2 k6 ?! x& ]
+ k" ]7 h+ D$ U5 p
五、以下贴上ldd3总线、设备、驱动demo
, Z; p9 C4 D J9 v9 k; p
* O& h: ?6 p! ~$ w. c; ]
7 z; J9 n1 _/ l$ M
注意:
, S$ I. ^/ t) [. {7 E: [+ L, V
* [8 J* e4 F' n% P% A2 i) F1 q0 j
因为是模拟事件发生,因此、驱动程序module_init时有设备的注册过程,实际驱动中不需要这部分、是总线轮询或中断导致设备的注册(注意总线注册的设备不会进入设备文件系统下创建设备节点、它只是加入总线的设备链表并在匹配设备驱动时使用;设备文件系统下设备节点的创建,是在设备驱动的探测probe函数中完成)。
) T2 O/ _1 |4 c8 K5 o4 G: A4 @
% O8 C! o& I( H! s* _
1.总线
+ ?; d6 j A: ~- e, { }
* f: y* a4 {' z8 P- q# s
testbus.c
! A* n# T: y4 ?6 x$ D6 B
# K8 ?; ~* L& R4 O6 h
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include "lddbus.h"
MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
static char *Version = "$Revision: 1.9 $";
/*
* Respond to hotplug events.
*/
static int ldd_hotplug(struct device *dev, char **envp, int num_envp,
char *buffer, int buffer_size)
{
envp[0] = buffer;
if (snprintf(buffer, buffer_size, "LDDBUS_VERSION=%s",
Version) >= buffer_size)
return -ENOMEM;
envp[1] = NULL;
return 0;
}
/*
* Match LDD devices to drivers. Just do a simple name test.
*/
static int ldd_match(struct device *dev, struct device_driver *driver)
{
//return !(strncmp(dev->bus_id, driver->name, strlen(driver->name));
return !strncmp(dev->init_name, driver->name, strlen(driver->name)); //change by
tank@tcl.com
}
/*
* The LDD bus device.
*/
static void ldd_bus_release(struct device *dev)
{
printk(KERN_DEBUG "lddbus release\n");
}
/*
* And the bus type.
*/
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
//.uevent = ldd_hotplug,
.uevent = ldd_uevent,
};
struct device ldd_bus = {
.init_name = "ldd0",
.release = ldd_bus_release
};
/*
* Export a simple attribute.
*/
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", Version);
}
static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);
/*
* LDD devices.
*/
/*
* For now, no references to LDDbus devices go out which are not
* tracked via the module reference count, so we use a no-op
* release function.
*/
static void ldd_dev_release(struct device *dev)
{
printk(KERN_ALERT"lddbus dev release \n");
}
int register_ldd_device(struct ldd_device *ldddev)
{
ldddev->dev.bus = &ldd_bus_type;
ldddev->dev.parent = &ldd_bus;
ldddev->dev.release = ldd_dev_release;
//strncpy(ldddev->dev.bus_id, ldddev->name, BUS_ID_SIZE);
ldddev->dev.init_name = ldddev->name; //change by
tank@tcl.com
return device_register(&ldddev->dev);
}
EXPORT_SYMBOL(register_ldd_device);
void unregister_ldd_device(struct ldd_device *ldddev)
{
device_unregister(&ldddev->dev);
}
EXPORT_SYMBOL(unregister_ldd_device);
/*
* Crude driver interface.
*/
static int lddbus_drv_probe(struct device *_dev)
{
struct ldd_driver *drv = to_ldd_driver(_dev->driver);
struct ldd_device *dev = to_ldd_device(_dev);
return drv->probe(dev);
}
static int lddbus_drv_remove(struct device *_dev)
{
struct ldd_driver *drv = to_ldd_driver(_dev->driver);
struct ldd_device *dev = to_ldd_device(_dev);
return drv->remove(dev);
}
static void lddbus_drv_shutdown(struct device *_dev)
{
struct ldd_driver *drv = to_ldd_driver(_dev->driver);
struct ldd_device *dev = to_ldd_device(_dev);
drv->shutdown(dev);
}
static int lddbus_drv_suspend(struct device *_dev, pm_message_t state)
{
struct ldd_driver *drv = to_ldd_driver(_dev->driver);
struct ldd_device *dev = to_ldd_device(_dev);
return drv->suspend(dev, state);
}
static int lddbus_drv_resume(struct device *_dev)
{
struct ldd_driver *drv = to_ldd_driver(_dev->driver);
struct ldd_device *dev = to_ldd_device(_dev);
return drv->resume(dev);
}
/*static*/ int lddbus_kill(struct ldd_device *dev)
{
printk("lddbus_kill: %s\n",dev->dev.init_name);
return 0;
}
EXPORT_SYMBOL(lddbus_kill);
static ssize_t show_version(struct device_driver *driver, char *buf)
{
struct ldd_driver *ldriver = to_ldd_driver(driver);
sprintf(buf, "%s\n", ldriver->version);
return strlen(buf);
}
int register_ldd_driver(struct ldd_driver *driver)
{
int ret;
driver->driver.bus = &ldd_bus_type;
if (driver->probe)
driver->driver.probe = lddbus_drv_probe;
if (driver->remove)
driver->driver.remove = lddbus_drv_remove;
if (driver->shutdown)
driver->driver.shutdown = lddbus_drv_shutdown;
if (driver->suspend)
driver->driver.suspend = lddbus_drv_suspend;
if (driver->resume)
driver->driver.resume = lddbus_drv_resume;
ret = driver_register(&driver->driver);
if (ret)
return ret;
driver->version_attr.attr.name = "version";
//driver->version_attr.attr.owner = driver->module;
driver->version_attr.attr.mode = S_IRUGO;
driver->version_attr.show = show_version;
driver->version_attr.store = NULL;
return driver_create_file(&driver->driver, &driver->version_attr);
}
void unregister_ldd_driver(struct ldd_driver *driver)
{
driver_unregister(&driver->driver);
}
EXPORT_SYMBOL(register_ldd_driver);
EXPORT_SYMBOL(unregister_ldd_driver);
static int __init ldd_bus_init(void)
{
int ret;
ret = bus_register(&ldd_bus_type);
if (ret)
return ret;
if (bus_create_file(&ldd_bus_type, &bus_attr_version))
printk(KERN_NOTICE "Unable to create version attribute\n");
ret = device_register(&ldd_bus);
if (ret)
printk(KERN_NOTICE "Unable to register ldd0\n");
return ret;
}
static void ldd_bus_exit(void)
{
device_unregister(&ldd_bus);
bus_unregister(&ldd_bus_type);
}
module_init(ldd_bus_init);
module_exit(ldd_bus_exit);
' T4 Q" S$ S, u5 C) X
# K- P1 X* e, C" w' }+ [& n' a" z8 f, z
6 x ?8 ]/ ?! G7 q& `1 a
lddbus.h
6 p, f4 g0 J* l1 z5 z
8 m4 f; M5 ^1 A
/*
* Definitions for the virtual LDD bus.
*
* $Id: lddbus.h,v 1.4 2004/08/20 18:49:44 corbet Exp $
*/
//extern struct device ldd_bus;
extern struct bus_type ldd_bus_type;
/*
* The LDD driver type.
*/
/*
* A device type for things "plugged" into the LDD bus.
*/
struct ldd_device {
char *name;
//struct ldd_driver *driver;
struct device dev;
};
#define to_ldd_device(x) container_of((x), struct ldd_device, dev)
struct ldd_driver {
char *version;
struct module *module;
int (*probe)(struct ldd_device *);
int (*remove)(struct ldd_device *);
void (*shutdown)(struct ldd_device *);
int (*suspend)(struct ldd_device *, pm_message_t state);
int (*resume)(struct ldd_device *);
struct device_driver driver;
struct driver_attribute version_attr;
};
#define to_ldd_driver(drv) container_of(drv, struct ldd_driver, driver)
extern int lddbus_kill(struct ldd_device *dev);
extern int register_ldd_device(struct ldd_device *);
extern void unregister_ldd_device(struct ldd_device *);
extern int register_ldd_driver(struct ldd_driver *);
extern void unregister_ldd_driver(struct ldd_driver *);
9 e6 z# y# [+ M& t
* f5 x+ d6 Y1 e5 l. Z5 W5 g4 m4 L
; c2 ?' S% }5 e3 H! A
2.设备与设备驱动
/ j, b+ f+ F3 C% p& d3 u( M
& `& }2 M" \8 `& f! Y
testmini.c
8 g9 i7 d7 P& c+ y+ z7 R: Q V+ V
4 \# H. s; _6 T/ E# [* [3 o5 ]
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
//#include <asm/arch/map.h>
#include <asm/io.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/semaphore.h>
#include <linux/module.h>
#include <linux/fs.h>
#include "lddbus.h"
#include <linux/sched.h>
//指向系统所拥有的资源信息,此信息为公用信息
//可以被多用户共同使用
static struct resource *mini_mem;
static struct resource *mini_irq;
static void __iomem *mini_base;
static unsigned char wq_flag = 0 ; //wait queue 队列的唤醒标志
//设备的数据结构,独立于platform的数据结构,此数据结构
//为驱动开发人员所要重点考虑的
//数据为用户公用的
struct Mini_Dev
{
struct miscdevice mdev;
wait_queue_head_t wq;
struct semaphore sem;
};
struct Mini_Dev *p_mdev;
static ssize_t s3c2440mini_read(struct file * file, char __user * userbuf,
size_t count, loff_t * off)
{
printk ("MINI TEST ..............READ\n");
return 0;
}
static ssize_t s3c2440mini_write(struct file *file, const char __user *data,
size_t len, loff_t *ppos)
{
printk ("MINI TEST ..............write\n");
return 0;
}
#define IOCTL_MINI_WAITIRQ _IOR('M',1,int)
#define IOCTL_MINI_SENDDATA _IOR('M',2,int)
static int s3c2440mini_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
int i;
switch(cmd)
{
case IOCTL_MINI_WAITIRQ:
wait_event_interruptible(p_mdev->wq, (wq_flag)&0x01);
wq_flag = 0;
break;
case IOCTL_MINI_SENDDATA:
for ( i = 0 ; i < 0x1000000; i ++)
{
writeb(0xff,mini_base);
}
break;
}
return 0;
}
static int s3c2440mini_open(struct inode *inode, struct file *file)
{
return 0;
}
static int s3c2440mini_release(struct inode *inode, struct file *file)
{
printk ("MINI TEST ..............release\n");
return 0;
}
//设备所具有的file 操作结构是驱动的工作重点
//同时也是设备功能实现的地方
static struct file_operations mini_ops = {
.owner = THIS_MODULE,
.write = s3c2440mini_write,
.read = s3c2440mini_read,
.unlocked_ioctl = s3c2440mini_ioctl,
.release = s3c2440mini_release,
.open = s3c2440mini_open,
};
//kernel interface
//platform 驱动数据结构,提供了探测、移除、挂起、回复和关闭的
//的系统接口,使系统设备更加的规范
//.driver 挂接着设备的数据结构和资源信息,这些信息已经提前被
//注册到系统里,只有在.name相同的情况下调用platform_driver_register才能
//注册成功
static struct ldd_device mini_device = {
.name = "mini",
};
static int mini_probe (struct ldd_device * dev)
{
printk("mini_probe %s\n",dev->name);
lddbus_kill(dev);
return 0;
}
static struct ldd_driver mini_driver = {
.version = "$Revision: 1.21 $",
.module = THIS_MODULE,
.probe = mini_probe,
.driver = {
.name = "mini",
},
};
static int __init mini_init(void)
{
register_ldd_device(&mini_device);
return register_ldd_driver(&mini_driver);
}
static void __exit mini_exit(void)
{
unregister_ldd_device(&mini_device);
return unregister_ldd_driver(&mini_driver);
}
module_init(mini_init);
module_exit(mini_exit);
MODULE_AUTHOR("ljf");
MODULE_LICENSE("Dual BSD/GPL");
" f( _9 U& h- g- G& F% W3 Z! v
- B* R/ ^ z% @" X$ N; \6 j
* h# Q+ ]+ s) @& R1 B0 D
Makefile
1 f% `+ o* N7 t3 W O- {% r
3 C% ?. \- f( l O2 C
obj-m := testlddbus.o testmini.o
PWD := $(shell pwd)
KERNELDIR := /usr/src/linux-headers-3.0.0-26-generic/
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
# cp -rf mini.ko ../module/
# cp -rf lddbus.ko ../module/
clean:
rm *.mod.c *.o *.ko *.bak modules.* Module.*
, E8 d6 x8 k5 E3 _4 f# i; S' R
% C8 ~5 E4 r& y7 A5 d0 N7 X
! u: R9 X1 Q1 t- t& P
作者:
Heaven_1
时间:
2021-8-2 20:38
驱动数据结构,提供了探测、移除、挂起、回复和关闭的
作者:
regngfpcb
时间:
2021-8-4 09:40
操作系统加载总线驱动,开始扫描设备、并为其申请struct device结构,最后挂入总线驱动devices链表
作者:
yin123
时间:
2021-8-4 09:41
数据结构kernel/include/linux/device.h
作者:
domenica
时间:
2021-8-4 09:41
支持热插拔
欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/)
Powered by Discuz! X3.2