<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 總線設備驅(qū)動模型總結

          總線設備驅(qū)動模型總結

          作者: 時間:2016-12-01 來源:網(wǎng)絡 收藏
          2、設備
          設備的實現(xiàn)主要是依靠struct device函數(shù)實現(xiàn)的,設備的實現(xiàn)主要是對結構體的填充。實現(xiàn)相應的函數(shù)即可。
          struct device {
          /*父設備,通常就是總線設備,這也是為什么需要將總線作為設備添加的原因*/
          struct device *parent;
          struct device_private *p;
          struct kobject kobj;
          /*init_name是新添加的,替代了原來的bus_id,但是init_name不能直接被讀寫操作*/
          const char *init_name; /* initial name of the device */
          struct device_type *type;
          struct semaphore sem; /* semaphore to synchronize calls to
          * its driver.
          */
          /*總線類型,主要是關聯(lián)總線類型,這是前面添加的總線類型,通過相同的總線類型關聯(lián)設備和驅(qū)動*/
          struct bus_type *bus; /* type of bus device is on */
          struct device_driver *driver; /* which driver has allocated this device */
          void *driver_data; /* data private to the driver */
          void *platform_data; /* Platform specific data, device
          core doesnt touch it */
          struct dev_pm_info power;
          #ifdef CONFIG_NUMA
          int numa_node; /* NUMA node this device is close to */
          #endif
          u64 *dma_mask; /* dma mask (if dmaable device) */
          u64 coherent_dma_mask; /* Like dma_mask, but for
          alloc_coherent mappings as
          not all hardware supports
          64 bit addresses for consistent
          allocations such descriptors. */
          struct device_dma_parameters *dma_parms;
          struct list_head dma_pools; /* dma pools (if dmable) */
          struct dma_coherent_mem *dma_mem; /* internal for coherent mem
          override */
          /* arch specific additions */
          struct dev_archdata archdata;
          dev_t devt; /* dev_t, creates the sysfs "dev" */
          spinlock_t devres_lock;
          struct list_head devres_head;
          struct klist_node knode_class;
          struct class *class;
          struct attribute_group **groups; /* optional groups */
          /*必須實現(xiàn)的release函數(shù)*/
          void (*release)(struct device *dev);
          };
          /*由于init_name 不能直接讀寫,只能通過*dev_name來讀寫設備名*/
          static inline const char *dev_name(const struct device *dev)
          {
          return kobject_name(&dev->kobj);
          }
          /*實現(xiàn)對設備名的設置*/
          int dev_set_name(struct device *dev, const char *name, ...)
          __attribute__((format(printf, 2, 3)));
          /*設備文件屬性結構體,必須注意的改變點*/
          struct device_attribute {
          /*屬性值*/
          struct attribute attr;
          /*設備屬性讀函數(shù),必須注意是三個參數(shù),不再是兩個參數(shù)*/
          ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);
          /*設備屬性寫操作,必須注意是四個參數(shù),不是三個參數(shù)*/
          ssize_t (*store)(struct device *dev, struct device_attribute *attr,const char *buf, size_t count);
          };
          /*設備屬性宏定義,主要用來實現(xiàn)設備文件屬性*/
          #define DEVICE_ATTR(_name, _mode, _show, _store)
          struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
          /*創(chuàng)建設備文件屬性函數(shù),必須檢查返回值*/
          int __must_check device_create_file(struct device *device,struct device_attribute *entry);
          /*刪除設備文件屬性函數(shù)*/
          void device_remove_file(struct device *dev,struct device_attribute *attr);
          /*設備注冊函數(shù),必須檢查返回值*/
          int __must_check device_register(struct device *dev);
          /*設備釋放函數(shù)*/
          void device_unregister(struct device *dev);
          需要注意的是linux-2.6.30內(nèi)核以前,沒有init_name元素,而是元素bus_id,這個主要是實現(xiàn)設備名的填充,但是linux-2.6.30內(nèi)核之后的在struct device中init_name代替了bus_id,但是需要注意的是init_name不能直接被讀寫,當需要讀寫設備名時只能采用特定的函數(shù)實現(xiàn):dev_name(),set_dev_name()。當直接讀寫init_name會導致內(nèi)核錯誤,出現(xiàn)Unable to handle kernel NULL pointer dereference at virtual address 00000000的錯誤。
          最后注意device_attribute中的show,store函數(shù)不同于其他類型(總線、驅(qū)動)的函數(shù),device_attribute中的show和store函數(shù)中的參數(shù)數(shù)量多了一個。
          3、驅(qū)動
          驅(qū)動管理一定的設備,其中的關系主要是內(nèi)核的內(nèi)部機制實現(xiàn)的,但是實現(xiàn)的具體邏輯需要在bus_type中的match函數(shù)中具體設計。通常是一定的設備名和驅(qū)動名匹配,當然也可以有其他的邏輯,具體的只需要設計好bus_type中的match函數(shù)。
          驅(qū)動是由驅(qū)動結構體實現(xiàn)的。具體如下所示:
          /*驅(qū)動結構體*/
          struct device_driver {
          /*驅(qū)動名,通常用來匹配設備*/
          const char *name;
          /*關聯(lián)的總線類型,總線、設備、驅(qū)動關聯(lián)的總線類型*/
          struct bus_type *bus;
          struct module *owner;
          const char *mod_name; /* used for built-in modules */
          /*驅(qū)動中最應該實現(xiàn)的操作函數(shù)主要包括probe和remove函數(shù)*/
          /*當匹配完成以后的,入口函數(shù)*/
          int (*probe) (struct device *dev);
          /*驅(qū)動卸載時操作的相關函數(shù),退出函數(shù)*/
          int (*remove) (struct device *dev);
          void (*shutdown) (struct device *dev);
          int (*suspend) (struct device *dev, pm_message_t state);
          int (*resume) (struct device *dev);
          struct attribute_group **groups;
          struct dev_pm_ops *pm;
          struct driver_private *p;
          };
          /*驅(qū)動注冊函數(shù),返回值必須檢測*/
          int __must_check driver_register(struct device_driver *drv);
          /*驅(qū)動釋放函數(shù)*/
          void driver_unregister(struct device_driver *drv);
          /*驅(qū)動屬性結構體*/
          struct driver_attribute {
          /*屬性值*/
          struct attribute attr;
          /*屬性讀操作函數(shù)*/
          ssize_t (*show)(struct device_driver *driver, char *buf);
          /*屬性寫操作函數(shù)*/
          ssize_t (*store)(struct device_driver *driver, const char *buf,
          size_t count);
          };
          /*驅(qū)動屬性定義宏命令*/
          #define DRIVER_ATTR(_name, _mode, _show, _store)
          struct driver_attribute driver_attr_##_name =
          __ATTR(_name, _mode, _show, _store)
          /*驅(qū)動屬性文件創(chuàng)建函數(shù),返回值必須檢測*/
          int __must_check driver_create_file(struct device_driver *driver,struct driver_attribute *attr);
          /*驅(qū)動屬性文件移除函數(shù)*/
          void driver_remove_file(struct device_driver *driver,struct driver_attribute *attr);
          驅(qū)動結構體的定義不需要完成所有元素的賦值,只需要完成主要的幾個變量的賦值即可,其中主要的元素包含name,bus,以及probe和remove函數(shù)的實現(xiàn)。
          其中的probe函數(shù)是當總線中的match完成匹配操作以后,進入驅(qū)動的入口函數(shù),因此必須實現(xiàn)。remove我認為就是對應的退出函數(shù),因此也有必要實現(xiàn)。
          驅(qū)動的注冊,釋放也有相關的函數(shù)來操作,主要是driver_register()和driver_unregister()。
          總結:
          1、在總線驅(qū)動模型中我認為最主要的是搞清楚三個不同的結構體,分別是總線、驅(qū)動、設備。了解三個元素對應的屬性結構體以及相應的屬性操作函數(shù)的差異性。
          2、不同驅(qū)動設計的關鍵主要是完成不同結構體的填充過程,但是并不需要對結構體中所有的對象進行賦值,只需要完成重要的幾個元素的值。
          3、總線是一種類型,同時也是一種設備,在總線的相關處理中需要首先添加總線類型,然后添加總線設備,這是需要注意的。由于總線類型關聯(lián)驅(qū)動和設備,因此需要導出總線類型變量。由于總線設備是設備的父設備,因此也需要將總線設備變量導出。同樣在驅(qū)動和設備中也要導出相關的結構體變量,便于總線中的match函數(shù)實現(xiàn)驅(qū)動和設備的匹配操作。
          4、XXX_attr結構體基本相同,都是一個屬性結構體和函數(shù)show()、stroe()。但是不同的XXX可能會導致show、stroe函數(shù)的參數(shù)發(fā)生變化。這需要對照源碼。
          5、struct device中的init_name是一個特殊的量,不能直接讀寫操作,只能采用函數(shù)device_name()和set_device_name來設置設備名。
          6、xxx_register()之類的函數(shù),需要對返回值進行檢查。因為很有可能不成功
          上一頁 1 2 下一頁

          評論


          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();