『Linuxバス、設備と駆動』ldd 3におけるdemo分析
13660 ワード
説明:本文はAndroid 2に基づく.3とLinux 2.6;その他のバージョンは参照用のみです.
Android2.3及びLinux 2.6.29カーネルシミュレータバージョンのコンパイルとデバッグ
一、古い方式(ホットスワップをサポートしない)
1.電源を入れる前にハードウェア設備がバスに挿入された;
2.オペレーティングシステムはバス駆動をロードし、デバイスのスキャンを開始し、struct device構造を申請し、最後にバス駆動devicesチェーンテーブルに掛ける.
3.オペレーティングシステムは設備駆動をロードし、struct deviceを登録する.driver構造は、その後、バス駆動のdevicesチェーンテーブルにdriverがバインドされていないデバイス(すなわちstruct deviceのstruct device_driverが空のデバイス)を遍歴し、一致すればdevice_bind_driver.
二、現在方式(ホットスワップ対応)
1.オペレーティングシステムのロードバス駆動;
2.ハードウェア挿入がある場合;バス駆動はデバイスにスキャンされ、struct deviceを申請し、バス駆動のdriversチェーンテーブルに行ってデバイス駆動を遍歴する.
3.逆に、デバイス駆動ロードがある場合;バスはstruct deviceを申請します.driver、それからバス駆動のdevicesチェーンテーブルに行ってデバイスを巡ります.まとめ:以上から分かる;バスドライバはコアであり、このデバイスとデバイスドライバに連絡します.
三、深く説明する
1.データ構造kernel/include/linux/device.h
バス:struct bus_type;
設備:struct device;
駆動:struct device_driver;
2.関係
バスドライバは、常にアクティブにスキャンし、そのデバイスにstructデバイスを割り当て、devicesチェーンテーブルに追加します.
逆に、バス駆動は受動的にデバイス駆動を受けるstruct deviceである.driverをdriversチェーンテーブルに追加します.
四、例-ldd 3の中の例
1.バスドライバの登録とログアウト
2.バス駆動が提供する設備駆動インタフェース
3.バスがデバイスにスキャンした後の登録インタフェース
注意:
シミュレーションイベント発生なので、ドライバmodule_Initにはデバイスの登録プロセスがあり、実際のドライバではこの部分は必要ありません.は、バスポーリングまたは中断によるデバイスの登録です(バスに登録されたデバイスは、デバイスファイルシステムの下にデバイスノードを作成しないことに注意してください.これは、バスのデバイスチェーンテーブルに参加し、デバイス駆動に一致するときにのみ使用されます.デバイスファイルシステムの下でデバイスノードの作成は、デバイス駆動を設定するプローブprobe関数で完了します).
1.バス
testbus.c
lddbus.h
testmini.c
Makefile
Android2.3及びLinux 2.6.29カーネルシミュレータバージョンのコンパイルとデバッグ
一、古い方式(ホットスワップをサポートしない)
1.電源を入れる前にハードウェア設備がバスに挿入された;
2.オペレーティングシステムはバス駆動をロードし、デバイスのスキャンを開始し、struct device構造を申請し、最後にバス駆動devicesチェーンテーブルに掛ける.
3.オペレーティングシステムは設備駆動をロードし、struct deviceを登録する.driver構造は、その後、バス駆動のdevicesチェーンテーブルにdriverがバインドされていないデバイス(すなわちstruct deviceのstruct device_driverが空のデバイス)を遍歴し、一致すればdevice_bind_driver.
二、現在方式(ホットスワップ対応)
1.オペレーティングシステムのロードバス駆動;
2.ハードウェア挿入がある場合;バス駆動はデバイスにスキャンされ、struct deviceを申請し、バス駆動のdriversチェーンテーブルに行ってデバイス駆動を遍歴する.
3.逆に、デバイス駆動ロードがある場合;バスはstruct deviceを申請します.driver、それからバス駆動のdevicesチェーンテーブルに行ってデバイスを巡ります.まとめ:以上から分かる;バスドライバはコアであり、このデバイスとデバイスドライバに連絡します.
三、深く説明する
1.データ構造kernel/include/linux/device.h
バス:struct bus_type;
設備:struct device;
駆動:struct device_driver;
2.関係
バスドライバは、常にアクティブにスキャンし、そのデバイスにstructデバイスを割り当て、devicesチェーンテーブルに追加します.
逆に、バス駆動は受動的にデバイス駆動を受けるstruct deviceである.driverをdriversチェーンテーブルに追加します.
四、例-ldd 3の中の例
1.バスドライバの登録とログアウト
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);
2.バス駆動が提供する設備駆動インタフェース
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);
}
3.バスがデバイスにスキャンした後の登録インタフェース
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 [email protected]
return device_register(&ldddev->dev);
// , bus ( match )、
}
五、以下ldd 3バス、設備、駆動demoを貼り付ける注意:
シミュレーションイベント発生なので、ドライバmodule_Initにはデバイスの登録プロセスがあり、実際のドライバではこの部分は必要ありません.は、バスポーリングまたは中断によるデバイスの登録です(バスに登録されたデバイスは、デバイスファイルシステムの下にデバイスノードを作成しないことに注意してください.これは、バスのデバイスチェーンテーブルに参加し、デバイス駆動に一致するときにのみ使用されます.デバイスファイルシステムの下でデバイスノードの作成は、デバイス駆動を設定するプローブprobe関数で完了します).
1.バス
testbus.c
#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 [email protected]
}
/*
* The LDD bus device.
*/
static void ldd_bus_release(struct device *dev)
{
printk(KERN_DEBUG "lddbus release
");
}
/*
* 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
", 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
");
}
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 [email protected]
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
",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
", 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
");
ret = device_register(&ldd_bus);
if (ret)
printk(KERN_NOTICE "Unable to register ldd0
");
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);
lddbus.h
/*
* 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 *);
2.デバイスとデバイス駆動testmini.c
#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
");
return 0;
}
static ssize_t s3c2440mini_write(struct file *file, const char __user *data,
size_t len, loff_t *ppos)
{
printk ("MINI TEST ..............write
");
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
");
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
",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");
Makefile
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.*