匯流排裝置驅動模型----驅動篇
阿新 • • 發佈:2019-01-31
源自 http://blog.chinaunix.net/uid-27664726-id-3334923.html
如果你瞭解了前面匯流排、裝置模型,分析匯流排裝置驅動模型的driver相對來說會輕鬆很多。開始也是看看其資料結構。
點選(此處)摺疊或開啟
-
struct device_driver {
-
const char *name; //驅動的名字
-
struct bus_type *bus; //驅動呈現屬於的匯流排型別
-
struct module *owner;
-
const
-
bool suppress_bind_attrs; /* disables bind/unbind
via sysfs */
-
const struct of_device_id *of_match_table;
- int (*probe) (struct device *dev); //驅動掛載的時候呼叫
- int (*remove) (struct device *dev); //解除安裝的時候呼叫
-
void (*shutdown)
-
int (*suspend) (struct
device *dev, pm_message_t state);
-
int (*resume) (struct
device *dev);
-
const struct attribute_group **groups;
-
const struct dev_pm_ops *pm;
-
struct driver_private *p;
- };
點選(此處
-
struct driver_private {
-
struct kobject kobj; //在sysfs中代表目錄本身
-
struct klist klist_devices;//驅動連結串列
-
struct klist_node knode_bus; //掛載在匯流排的驅動連結串列的節點
-
struct module_kobject *mkobj;//driver與相關的module之間的聯絡
-
struct device_driver *driver;
-
};
- #define to_driver(obj) container_of(obj, struct driver_private, kobj)
點選(此處)摺疊或開啟
-
struct driver_attribute {
-
struct attribute attr;
-
ssize_t (*show)(struct
device_driver *driver, char *buf);
-
ssize_t (*store)(struct
device_driver *driver, const char *buf,
-
size_t count);
-
};
-
#define DRIVER_ATTR(_name, _mode, _show, _store) \
-
struct driver_attribute driver_attr_##_name = \
- __ATTR(_name, _mode, _show, _store)
點選(此處)摺疊或開啟
-
int driver_register(struct device_driver *drv)
-
{
-
int ret;
-
struct device_driver *other;
-
BUG_ON(!drv->bus->p);
-
if ((drv->bus->probe && drv->probe) ||
-
(drv->bus->remove && drv->remove) ||
-
(drv->bus->shutdown && drv->shutdown))
-
printk(KERN_WARNING "Driver '%s' needs updating - please use "
-
"bus_type methods\n", drv->name);
-
other = driver_find(drv->name, drv->bus);
-
if (other) {
-
put_driver(other);
-
printk(KERN_ERR "Error: Driver '%s' is already registered, "
-
"aborting...\n", drv->name);
-
return -EBUSY;
-
}
-
ret = bus_add_driver(drv);
-
if (ret)
-
return ret;
-
ret = driver_add_groups(drv, drv->groups);
-
if (ret)
-
bus_remove_driver(drv);
-
return ret;
- }
點選(此處)摺疊或開啟
-
int bus_add_driver(struct device_driver *drv)
-
{
-
struct bus_type *bus;
-
struct driver_private *priv;
-
int error = 0;
-
bus = bus_get(drv->bus);//增加對bus的引用
-
if (!bus)
-
return -EINVAL;
-
pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
-
priv = kzalloc(sizeof(*priv), GFP_KERNEL);//分配初始化一個drv->p,也就是上面的driver_private結構
-
if (!priv) {
-
error = -ENOMEM;
-
goto out_put_bus;
-
}
-
klist_init(&priv->klist_devices, NULL, NULL);
-
priv->driver = drv;
-
drv->p = priv;
-
priv->kobj.kset = bus->p->drivers_kset;
-
error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
-
"%s", drv->name);//將drv加入sysfs
-
if (error)
-
goto out_unregister;
-
if (drv->bus->p->drivers_autoprobe) {
-
error = driver_attach(drv);//如果匯流排可以自動的probe,就會呼叫匹配函式
-
if (error)
-
goto out_unregister;
-
}
-
klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);//將drv掛入到匯流排的連結串列中
-
module_add_driver(drv->owner, drv);//建立driver相關的模組
-
error = driver_create_file(drv, &driver_attr_uevent);//在drv目錄下建立event屬性檔案
-
if (error) {
-
printk(KERN_ERR "%s: uevent attr (%s) failed\n",
-
__func__, drv->name);
-
}
-
error = driver_add_attrs(bus, drv);//新增屬性
-
if (error) {
-
/* How the hell do we get out
of this pickle? Give up */
-
printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
-
__func__, drv->name);
-
}
-
if (!drv->suppress_bind_attrs) {
-
error = add_bind_files(drv);
-
if (error) {
-
/* Ditto */
-
printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
-
__func__, drv->name);
-
}
-
}
-
kobject_uevent(&priv->kobj, KOBJ_ADD);//向用戶空間釋出kobj_add訊息
-
return 0;
-
out_unregister:
-
kobject_put(&priv->kobj);
-
kfree(drv->p);
-
drv->p = NULL;
-
out_put_bus:
-
bus_put(bus);
-
return error;
- }
點選(此處)摺疊或開啟
-
int driver_attach(struct device_driver *drv)
-
{
-
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
- }
點選(此處)摺疊或開啟
-
static int __driver_attach(struct device *dev, void *data)
-
{
-
struct device_driver *drv = data;
-
/*
-
* Lock device and try to bind to it. We
drop the error
-
* here and always return 0, because we need to keep
trying
-
* to bind to devices and some
drivers will return an error
-
* simply if it didn't support the device.
-
*
-
* driver_probe_device() will spit a warning if there
-
* is an error.
-
*/
-
if (!driver_match_device(drv, dev))
-
return 0;
-
if (dev->parent) /* Needed for USB */
-
device_lock(dev->parent);
-
device_lock(dev);
-
if (!dev->driver)
-
driver_probe_device(drv, dev);
-
device_unlock(dev);
-
if (dev->parent)
-
device_unlock(dev->parent);
- return 0;
點選(此處)摺疊或開啟
-
static inline int driver_match_device(struct device_driver *drv,
-
struct device *dev)
-
{
-
return drv->bus->match ? drv->bus->match(dev, drv) : 1;
- }
點選(此處)摺疊或開啟
-
extern struct bus_type my_bus_type;
-
static int my_probe(struct device *dev)
-
{
-
printk("Driver found device which my driver can handle!\n");
-
return 0;
-
}
-
static int my_remove(struct device *dev)
-
{
-
printk("Driver found device unpluged!\n");
-
return 0;
-
}
-
struct device_driver my_driver = {
-
.name = "my_dev",
-
.bus = &my_bus_type,
-
.probe = my_probe,
-
.remove = my_remove,
-
};
-
/*
-
* Export a simple attribute.
-
*/
-
static ssize_t mydriver_show(struct device_driver *driver, char *buf)
-
{
-
return sprintf(buf, "%s\n", "This
is my driver!");
-
}
-
static DRIVER_ATTR(drv, S_IRUGO, mydriver_show, NULL);
-
static int __init my_driver_init(void)
-
{
-
int ret = 0;
-
/*註冊驅動*/
-
driver_register(&my_driver);
-
/*建立屬性檔案*/
-
driver_create_file(&my_driver, &driver_attr_drv);
-
return ret;
-
}
-
static void my_driver_exit(void)
-
{
-
driver_unregister(&my_driver);
-
}
-
module_init(my_driver_init);
- module_exit(my_driver_exit);