说明:本文基于Android2.3和Linux2.6;其他版本仅供参考。
Android2.3及Linux2.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 device并添加到devices链表中;
反之,总线驱动是被动的接受设备驱动的struct device_driver并添加到drivers链表中。
四、实例-ldd3里边的例子
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接口)、如果匹配成功会调用驱动的探测函数 }五、以下贴上ldd3总线、设备、驱动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\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 [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\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);
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\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");
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.*