1. 程式人生 > >Linux裝置驅動模型之platform(平臺)匯流排詳解

Linux裝置驅動模型之platform(平臺)匯流排詳解

  1 int driver_attach(struct device_driver *drv)
  2 {
  3     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);   //  這個函式的功能就是:   依次去匹配bus匯流排下的各個裝置
  4 }
  5 

6 7 int bus_for_each_dev(struct bus_type *bus, struct device *start, 8 void *data, int (*fn)(struct device *, void
*)) 9 { 10 struct klist_iter i; // 定義一個klist_iter 結構體變數 包含: struct klist 和 struct klist_node 11 struct device *dev; 12 int error = 0; 13 14 if (!bus) 15 return -EINVAL; 16 17 klist_iter_init_node(&bus->p->klist_devices, &i, //
這個函式的功能就是將 klist_devices 和 knode_bus填充到 i 變數中 18 (start ? &start->p->knode_bus : NULL)); 19 while ((dev = next_device(&i)) && !error) //  依次返回出總線上的各個裝置結構體device 20 error = fn(dev, data); // 對於每一個裝置和裝置驅動都呼叫fn這個函式 直道成功 或者全部都匹配不上
21 klist_iter_exit(&i); 22 return error; 23 } 24 25

26 27 static int __driver_attach(struct device *dev, void *data) 28 { 29 struct device_driver *drv = data; // 定義一個device_driver 指標 30 31 /* 32 * Lock device and try to bind to it. We drop the error 33 * here and always return 0, because we need to keep trying 34 * to bind to devices and some drivers will return an error 35 * simply if it didn't support the device. 36 * 37 * driver_probe_device() will spit a warning if there 38 * is an error. 39 */ 40 41 if (!driver_match_device(drv, dev)) // 通過這個函式進行匹配 呼叫匯流排下的match 函式 42 return 0; 43 44 if (dev->parent) /* Needed for USB */ 45 device_lock(dev->parent); 46 device_lock(dev); 47 if (!dev->driver) 48 driver_probe_device(drv, dev); // 呼叫probe函式 49 device_unlock(dev); 50 if (dev->parent) 51 device_unlock(dev->parent); 52 53 return 0; 54 } 55 56

57 58 int driver_probe_device(struct device_driver *drv, struct device *dev) 59 { 60 int ret = 0; 61 62 if (!device_is_registered(dev)) // 判斷這個裝置是否已經註冊了 63 return -ENODEV; 64 65 pr_debug("bus: '%s': %s: matched device %s with driver %s\n", 66 drv->bus->name, __func__, dev_name(dev), drv->name); 67 68 pm_runtime_get_noresume(dev); 69 pm_runtime_barrier(dev); 70 ret = really_probe(dev, drv); // 在這個函式中就會呼叫裝置驅動或者是匯流排下的 probe 函式 71 pm_runtime_put_sync(dev); 72 73 return ret; 74 } 75

76 77 static int really_probe(struct device *dev, struct device_driver *drv) 78 { 79 int ret = 0; 80 81 atomic_inc(&probe_count); 82 pr_debug("bus: '%s': %s: probing driver %s with device %s\n", 83 drv->bus->name, __func__, drv->name, dev_name(dev)); 84 WARN_ON(!list_empty(&dev->devres_head)); 85 86 dev->driver = drv; // 使用 dev->driver 指標去指向 drv 這就使得這兩者建立了一種關係 87 if (driver_sysfs_add(dev)) { 88 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n", 89 __func__, dev_name(dev)); 90 goto probe_failed; 91 } 92 93 if (dev->bus->probe) { // 如果匯流排下的probe函式存在 則呼叫優先呼叫這個函式 94 ret = dev->bus->probe(dev); 95 if (ret) 96 goto probe_failed; 97 } else if (drv->probe) { // 否則呼叫裝置驅動中的probe函式 98 ret = drv->probe(dev); // 所以由此可知: 匯流排中的probe函式具有更高的優先順序 99 if (ret) 100 goto probe_failed; 101 } 102 103 driver_bound(dev); 104 ret = 1; 105 pr_debug("bus: '%s': %s: bound device %s to driver %s\n", 106 drv->bus->name, __func__, dev_name(dev), drv->name); 107 goto done; 108 109 probe_failed: 110 devres_release_all(dev); 111 driver_sysfs_remove(dev); 112 dev->driver = NULL; 113 114 if (ret != -ENODEV && ret != -ENXIO) { 115 /* driver matched but the probe failed */ 116 printk(KERN_WARNING 117 "%s: probe of %s failed with error %d\n", 118 drv->name, dev_name(dev), ret); 119 } 120 /* 121 * Ignore errors returned by ->probe so that the next driver can try 122 * its luck. 123 */ 124 ret = 0; 125 done: 126 atomic_dec(&probe_count); 127 wake_up(&probe_waitqueue); 128 return ret; 129 }

相關推薦

Linux裝置驅動模型platform(平臺)匯流排

1 int driver_attach(struct device_driver *drv) 2 { 3 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); // 這個函式的功能就是: 依次去匹配b

Linux裝置驅動模型3——platform平臺匯流排工作原理

以下內容源於朱有鵬嵌入式課程的學習,如有侵權,請告知刪除。 四、platform平臺匯流排工作原理1 1、何為平臺匯流排? (1)屬於匯流排中的一種,相對於usb、pci、i2c等物理匯流排來說,platform匯流排是虛擬的、抽象出來的。 (2)CPU與外部通訊的2種方

Linux裝置驅動模型platform匯流排深入淺出(加入裝置樹)

在Linux2.6以後的裝置驅動模型中,需關心匯流排,裝置和驅動這三種實體,匯流排將裝置和驅動繫結。在系統每註冊一個裝置的時候,會尋找與之匹配的驅動;相反,在系統每註冊一個驅動的時候,會尋找與之匹配的裝置,而匹配由匯流排完成。 對於依附在USB、PCI、I2C

linux裝置驅動模型匯流排裝置驅動三者的關係

匯流排、裝置、驅動,也就是bus、device、driver,在核心裡都有對應的結構體,在include/linux/device.h 裡定義。Device.h (linux-3.4.2\inclu

linux裝置驅動模型Kobject、kobj_type、kset

一、sysfs檔案系統簡介: 1、sysfs概述 sysfs檔案系統是核心物件(kobject)、屬性(kobj_type)、及它們相互關係的一種表現。 sysfs非常重要的特徵:使用者可以從sysfs中讀出核心資料,也可以將使用者資料寫入核心。 2、核心結構與sysfs對應關係:

LINUX裝置驅動模型class

  轉自 https://blog.csdn.net/qq_20678703/article/details/52754661 1、LINUX裝置驅動模型中的bus、device、driver,。其中bus:實際的匯流排,device:實際的裝置和介面,而driver:對應存在的驅動。 2、但

Linux Kernel裝置驅動模型匯流排查詢裝置

裝置驅動模型之匯流排查詢裝置: /**  * bus_find_device - device iterator for locating a particular device.  * @bus: bus type  * @start: Device to begin w

Linux裝置驅動工程師路——platform型別按鍵驅動

Linux裝置驅動工程師之路——platform按鍵驅動  Y-Kee 轉載請註明來自於衡陽師範學院08電2  Y-Kee http://blog.csdn.net/ayangke,QQ:843308498 一 、重要知識點: 1.platform裝置模型    

Linux裝置驅動工程師路——裝置模型(上)底層模型

Linux裝置驅動工程師之路——裝置模型(上)底層模型 K-Style 一、重要知識點          1.Sysfs檔案系統        Sysfs檔案系統是一種類似於proc檔案系統的特殊檔案系統,它存在於記憶體當中,當系統啟動時由核心掛載於記憶體當中。用於將

閱讀Linux裝置驅動模型原始碼 device結構體成員

【前言】         我們學習 Linux 裝置驅動,很多人在一開始往往急於想找到快速入門的方法,希望能有一個提綱挈領的使用說明來幫助我們快速理解 Linux 裝置驅動的設計思路和框架,從而擺脫掉 Linux 核心這頭龐然怪獸。我自己最初也是這樣。然而事與願違,如果學習

linux裝置驅動模型 - regmap

1. regmap介紹 regmap主要是為了方便操作暫存器而設計的,它將所有模組的暫存器(包括soc上模組的暫存器和外圍裝置的暫存器等) 抽象出來,用一套統一介面來操作暫存器 比如,如果要操作i2c裝置的暫存器,那麼就要呼叫i2c_transfer介面,要操作spi裝置的暫存

linux裝置驅動模型 - device/bus/driver

在linux驅動模型中,為了便於管理各種裝置,我們把不同裝置分別掛在他們對應的總線上,裝置對應的驅動程式也在總線上找,這樣就提出了deivce-bus-driver的模型,硬體上有許多裝置匯流排,那麼我們就在裝置模型上抽象出bus概念,相應的device就代表裝置,driver表示驅動,

linux裝置驅動模型 - sys/kobject

1. sysfs 1.1 sysfs檔案系統註冊 在系統啟動時會註冊sysfs檔案系統 (fs/sysfs/mount.c) int __init sysfs_init(void) { int err; sysfs_root = kernfs_creat

linux裝置驅動模型 - 驅動框架

linux驅動模型框架如圖所示: 1. kernfs 驅動最終是提供給使用者層使用,那麼其中的介面就是通過kernfs檔案系統來註冊,kernfs是一個通用的核心虛擬檔案系統 2. sysfs/kobject sysfs是裝置驅動檔案系統,裝置之間的各種關係會在在/

《5.linux驅動開發-第5部分-5.5.linux裝置驅動模型

《5.linux驅動開發-第5部分-5.5.linux裝置驅動模型》 第一部分、章節目錄 5.5.1.linux裝置驅動模型簡介 5.5.2.裝置驅動模型的底層架構 5.5.3.匯流排式裝置驅動組織方式 5.5.4.platform平臺匯流排簡介1 5.5.5.platform平臺匯流排

裝置驅動模型:kobject,kset,ktype(六)

本篇部落格介紹kset與kobject/kset之間的關係,好了,廢話不多說,直接上ktype的結構體: struct kobj_type { void (*release)(struct kobject *kobj); const struct sysf

裝置驅動模型:kobject,kset,ktype(五)

在《裝置驅動模型之:kobject,kset,ktype(四)》這篇博文裡面已經詳細介紹了kset的操作以及kset與kobject的關係,下面則是對於這篇博文的一些實際操作: #include <linux/module.h> #include <linux/init

裝置驅動模型:kobject,kset,ktype(四)

之前的部落格介紹的關於kobject的一些操作和函式的用法,此篇部落格介紹關於kset函式的一些操作,順便把有關於kset的kobject函式的一些用法補齊,這樣才會有知識共享的效果; 關於kset的操作的函式以及操作的作用有以下: 1. extern void kset_init(s

裝置驅動模型:kobject,kset,ktype(三)

這篇部落格裡面對於kobject作用做了一個剖析,這篇部落格是對於kobject一個實際運用,程式碼如下: <kobject.c> #include <linux/init.h> #include <linux/module.h> #include

裝置驅動模型:kobject,kset,ktype(二)

之前https://mp.csdn.net/mdeditor/84722837#這個部落格裡面介紹了關於kobject,set,ktype三個結構體之間的關係以及作用,可以做為參考,下面介紹一下這三個結構體相關的函式的使用以及作用; static void kobject_init_in