Linux驅動修煉之道-SPI驅動框架源碼分析

http://www.cnblogs.com/fly-fish/archive/2011/10/26/2225613.html

http://blog.csdn.net/woshixingaaa/article/details/6574215

(上)

Linux驅動修煉之道-SPI驅動框架源碼分析(上)

分類: linux驅動編程 5915人閱讀 評論(11) 收藏 舉報

努力成爲linux kernel hacker的人李萬鵬原創作品,爲夢而戰。轉載請標明出處

http://blog.csdn.net/woshixingaaa/archive/2011/06/29/6574215.aspx

                               Linux驅動修煉之道-SPI驅動框架源碼分析(中)

                               Linux驅動修煉之道-SPI驅動框架源碼分析(下)

                                                 

SPI協議是一種同步的串行數據連接標準,由摩托羅拉公司命名,可工作於全雙工模式。相關通訊設備可工作於m/s模式。主設備發起數據幀,允許多個從設備的存在。每個從設備

有獨立的片選信號,SPI一般來說是四線串行總線結構。
接口:
SCLK——Serial Clock(output from master)時鐘(主設備發出)
MOSI/SIMO——Master Output, Slave Input(output from master)數據信號線mosi(主設備發出)
MISO/SOMI——Master Input,Slave Outpu(output from slave)數據信號線(從設備)
SS——Slave Select(active low;output from master)片選信號

下面來看一下Linux中的SPI驅動。在Linux設備驅動框架的設計中,有一個重要的主機,外設驅動框架分離的思想,如下圖。

外設a,b,c的驅動與主機控制器A,B,C的驅動不相關,主機控制器驅動不關心外設,而外設驅動也不關心主機,外設只是訪問核心層的通用的API進行數據的傳輸,主機和外設之間可以進行任意的組合。如果我們不進行如圖的主機和外設分離,外設a,b,c和主機A,B,C進行組合的時候,需要9種不同的驅動。設想一共有個主機控制器,n個外設,分離的結構是需要m+n個驅動,不分離則需要m*n個驅動。

下面介紹spi子系統的數據結構:
在Linux中,使用spi_master結構來描述一個SPI主機控制器的驅動。

  1. struct spi_master {  
  2. struct device    dev;/*總線編號,從0開始*/  
  3. s16  bus_num;/*支持的片選的數量,從設備的片選號不能大於這個數量*/  
  4. u16  num_chipselect;  
  5. u16  dma_alignment;/*改變spi_device的特性如:傳輸模式,字長,時鐘頻率*/  
  6. int  (*setup)(struct spi_device *spi);/*添加消息到隊列的方法,這個函數不可睡眠,他的任務是安排發生的傳送並且調用註冊的回調函數complete()*/  
  7. int  (*transfer)(struct spi_device *spi,struct spi_message *mesg);  
  8. void (*cleanup)(struct spi_device *spi);  
  9. };  

分配,註冊和註銷的SPI主機的API由SPI核心提供:

  1. struct spi_master *spi_alloc_master(struct device *host, unsigned size);  
  2. int spi_register_master(struct spi_master *master);  
  3. void spi_unregister_master(struct spi_master *master);    
在Linux中用spi_driver來描述一個SPI外設驅動。
  1. struct spi_driver {  
  2. int   (*probe)(struct spi_device *spi);  
  3. int   (*remove)(struct spi_device *spi);  
  4. void  (*shutdown)(struct spi_device *spi);  
  5. int   (*suspend)(struct spi_device *spi, pm_message_t mesg);  
  6. int   (*resume)(struct spi_device *spi);  
  7. struct device_driver  driver;  
  8. };   

可以看出,spi_driver結構體和platform_driver結構體有極大的相似性,都有probe(),remove(),suspend(),resume()這樣的接口。

Linux用spi_device來描述一個SPI外設設備。

  1. struct spi_device {  
  2. struct device        dev;  
  3. struct spi_master   *master;       //對應的控制器指針u32      
  4. max_speed_hz;  //spi通信的時鐘u8         
  5. chip_select;   //片選,用於區分同一總線上的不同設備  
  6. u8  mode;  
  7. #define    SPI_CPHA    0x01            /* clock phase */  
  8. #define    SPI_CPOL    0x02            /* clock polarity */  
  9. #define SPI_MODE_0  (0|0)           /* (original MicroWire) */#define   SPI_MODE_1  (0|SPI_CPHA)  
  10. #define SPI_MODE_2  (SPI_CPOL|0)  
  11. #define SPI_MODE_3  (SPI_CPOL|SPI_CPHA)#define  SPI_CS_HIGH 0x04            /* chipselect active high? */  
  12. #define    SPI_LSB_FIRST   0x08            /* per-word bits-on-wire */  
  13. #define  SPI_3WIRE   0x10            /* SI/SO signals shared */  
  14. #define   SPI_LOOP    0x20            /* loopback mode */  
  15. u8      bits_per_word;    //每個字長的比特數  
  16. int      irq;              //使用的中斷  
  17. void     *controller_state;  
  18. void     *controller_data;  
  19. char     modalias[32];    //名字  
  20. };    
如下圖,看這三個結構的關係,這裏spi_device與spi_master是同一個父設備,這是在spi_new_device函數中設定的,一般這個設備是一個物理設備。

這裏的spi_master_class,spi_bus_type又是什麼呢,看下邊兩個結構體:

  1. struct bus_type spi_bus_type = {     
  2.    .name       = "spi",  
  3.    .dev_attrs  = spi_dev_attrs,  
  4.    .match    = spi_match_device,  
  5.    .uevent   = spi_uevent,   
  6.    .suspend  = spi_suspend,  
  7.    .resume   = spi_resume,  
  8. };   
  9. static struct class spi_master_class = {     
  10.     .name             = "spi_master",   
  11.     .owner           = THIS_MODULE,  
  12.     .dev_release    = spi_master_release,  
  13. };    
spi_bus_type對應spi中的spi bus總線,spidev的類定義如下:

  1. static struct class *spidev_class;   
創建這個類的主要目的是使mdev/udev能在/dev下創建設備節點/dev/spiB.C。B代表總線,C代表片外設備的片選號。

下邊來看兩個板級的結構,其中spi_board_info用來初始化spi_device,s3c2410_spi_info用來初始化spi_master。這兩個板級的結構需要在移植的時候在arch/arm/mach-s3c2440/mach-smdk2440.c中初始化。

  1. struct spi_board_info {  
  2. char     modalias[32];   //設備與驅動匹配的唯一標識  
  3. const void    *platform_data;  
  4. void     *controller_data;  
  5. int        irq;  
  6. u32     max_speed_hz;  
  7. u16        bus_num;       //設備所歸屬的總線編號  
  8. u16      chip_select;  
  9. u8      mode;  
  10. };  
  11. struct s3c2410_spi_info {  
  12. int     pin_cs;         //芯片選擇管腳  
  13. unsigned int    num_cs;         //總線上的設備數  
  14. int        bus_num;        //總線號  
  15. void (*gpio_setup)(struct s3c2410_spi_info *spi, int enable);     //spi管腳配置函數  
  16. void (*set_cs)(struct s3c2410_spi_info *spi, int cs, int pol);  
  17. };    
boardinfo是用來管理spi_board_info的結構,spi_board_info通過spi_register_board_info(struct spi_board_info const *info, unsigned n)交由boardinfo來管理,並掛到board_list鏈表上,list_add_tail(&bi->list,&board_list);

  1. struct boardinfo {   
  2.  /*用於掛到鏈表頭board_list上*/  
  3. struct list_head  list;  
  4. /*管理的spi_board_info的數量*/  
  5. unsigned  n_board_info;  
  6. /*存放結構體spi_board_info*/  
  7. struct spi_board_info    board_info[0];  
  8. };   
s3c24xx_spi是S3C2440的SPI控制器在Linux內核中的具體描述,該結構包含spi_bitbang內嵌結構,控制器時鐘頻率和佔用的中斷資源等重要成員,其中spi_bitbang具體負責SPI數據的傳輸。

  1. struct s3c24xx_spi {  
  2. /* bitbang has to be first */  
  3. struct spi_bitbang  bitbang;  
  4. struct completion   done;  
  5. void __iomem      *regs;  
  6. int            irq;  
  7. int             len;  
  8. int             count;  
  9. void         (*set_cs)(struct s3c2410_spi_info *spi,  int cs, int pol);  
  10. /* data buffers */const unsigned char *tx;  
  11. unsigned char       *rx;  
  12. struct clk      *clk;  
  13. struct resource        *ioarea;  
  14. struct spi_master   *master;  
  15. struct spi_device   *curdev;  
  16. struct device       *dev;  
  17. struct s3c2410_spi_info *pdata;  
  18. };  
爲了解決多個不同的SPI設備共享SPI控制器而帶來的訪問衝突,spi_bitbang使用內核提供的工作隊列(workqueue)。workqueue是Linux內核中定義的一種回調處理方式。採用這種方式需要傳輸數據時,不直接完成數據的傳輸,而是將要傳輸的工作分裝成相應的消息(spi_message),發送給對應的workqueue,由與workqueue關聯的內核守護線程(daemon)負責具體的執行。由於workqueue會將收到的消息按時間先後順序排列,這樣就是對設備的訪問嚴格串行化,解決了衝突。
  1. struct spi_bitbang {  
  2. struct workqueue_struct *workqueue;      //工作隊列頭  
  3. struct work_struct  work;            //每一次傳輸都傳遞下來一個spi_message,都向工作隊列頭添加一個  
  4. workspinlock_t     lock;  
  5. struct list_head   queue;           //掛接spi_message,如果上一次的spi_message還沒有處理完,接下來的spi_message就掛接在queue上等待處理  
  6. u8     busy;            //忙碌標誌  
  7. u8     use_dma;  
  8. u8     flags;  
  9. struct spi_master *master;/*一下3個函數都是在函數s3c24xx_spi_probe()中被初始化*/  
  10. int     (*setup_transfer)(struct spi_device *spi,struct spi_transfer *t);   //設置傳輸模式  
  11. void    (*chipselect)(struct spi_device *spi, int is_on);                    //片選  
  12. #define   BITBANG_CS_ACTIVE   1   /* normally nCS, active low */  
  13. #define   BITBANG_CS_INACTIVE 0/*傳輸函數,由s3c24xx_spi_txrx來實現*/  
  14. int    (*txrx_bufs)(struct spi_device *spi, struct spi_transfer *t);  
  15. u32    (*txrx_word[4])(struct spi_device *spi,unsigned nsecs,u32 word, u8 bits);  
  16. };  

下面來看看spi_message:

  1. struct spi_message {  
  2. struct list_head    transfers;   //此次消息的傳輸隊列,一個消息可以包含多個傳輸段  
  3. struct spi_device *spi;        //傳輸的目的設備  
  4. unsigned      is_dma_mapped:1;  //如果爲真,此次調用提供dma和cpu虛擬地址  
  5. void          (*complete)(void *context);  //異步調用完成後的回調函數  
  6. void         *context;                    //回調函數的參數  
  7. unsigned      actual_length;               //此次傳輸的實際長度  
  8. int         status;                      //執行的結果,成功被置0,否則是一個負的錯誤碼  
  9. struct list_head   queue;  
  10. void          *state;  
  11. };    
在有消息需要傳遞的時候,會將spi_transfer通過自己的transfer_list字段掛到spi_message的transfers鏈表頭上。spi_message用來原子的執行spi_transfer表示的一串數組傳輸請求。這個傳輸隊列是原子的,這意味着在這個消息完成之前不會有其他消息佔用總線。消息的執行總是按照FIFO的順序。

下面看一看spi_transfer:

  1. struct spi_transfer {  
  2. const void *tx_buf;  //要寫入設備的數據(必須是dma_safe),或者爲NULL  
  3. void       *rx_buf;  //要讀取的數據緩衝(必須是dma_safe),或者爲NULL  
  4. unsigned   len;      //tx和rx的大小(字節數),這裏不是指它的和,而是各自的長度,他們總是相等的  
  5. dma_addr_t    tx_dma;   //如果spi_message.is_dma_mapped是真,這個是tx的dma地址  
  6. dma_addr_t rx_dma;   //如果spi_message.is_dma_mapped是真,這個是rx的dma地址  
  7. unsigned   cs_change:1;    //影響此次傳輸之後的片選,指示本次tranfer結束之後是否要重新片選並調用setup改變設置,這個標誌可以較少系統開銷u8      
  8. bits_per_word;  //每個字長的比特數,如果是0,使用默認值  
  9. u16        delay_usecs;    //此次傳輸結束和片選改變之間的延時,之後就會啓動另一個傳輸或者結束整個消息  
  10. u32       speed_hz;       //通信時鐘。如果是0,使用默認值  
  11. struct list_head transfer_list; //用來連接的雙向鏈表節點

Linux驅動修煉之道-SPI驅動框架源碼分析(中)

分類: linux驅動編程 2150人閱讀 評論(13) 收藏 舉報

努力成爲linux kernel hacker的人李萬鵬原創作品,爲夢而戰。轉載請標明出處

http://blog.csdn.net/woshixingaaa/archive/2011/06/29/6574220.aspx

 

這篇來分析spi子系統的建立過程。
嵌入式微處理器訪問SPI設備有兩種方式:使用GPIO模擬SPI接口的工作時序或者使用SPI控制器。使用GPIO模擬SPI接口的工作時序是非常容易實現的,但是會導致大量的時間耗費在模擬SPI接口的時序上,訪問效率比較低,容易成爲系統瓶頸。這裏主要分析使用SPI控制器的情況。

這個是由sys文件系統導出的spi子系統在內核中的視圖了。
首先了解一下Linux內核中的幾個文件:spi.c也就是spi子系統的核心了,spi_s3c24xx.c是s3c24xx系列芯片的SPI controller驅動,它向更上層的SPI核心層(spi.c)提供接口用來控制芯片的SPI controller,是一個被其他驅動使用的驅動。而spidev.c是在覈心層基礎之上將SPI controller模擬成一個字符型的驅動,向文件系統提供標準的文件系統接口,用來操作對應的SPI controller。
下面我們來看看spi子系統是怎麼註冊進內核的:

  1. static int __init spi_init(void)  
  2. {  
  3.     int status;  
  4.     buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);  
  5.     if (!buf) {  
  6.         status = -ENOMEM;  
  7.         goto err0;  
  8.     }  
  9.     status = bus_register(&spi_bus_type);  
  10.     if (status < 0)  
  11.         goto err1;  
  12.     status = class_register(&spi_master_class);  
  13.     if (status < 0)  
  14.         goto err2;  
  15.     return 0;  
  16. err2:  
  17.     bus_unregister(&spi_bus_type);  
  18. err1:  
  19.     kfree(buf);  
  20.     buf = NULL;  
  21. err0:  
  22.     return status;  
  23. }  
  24. postcore_initcall(spi_init);  
這裏註冊了一個spi_bus_type,也就是一個spi總線,和一個spi_master的class。分別對應上圖中sys/bus/下的spi目錄和sys/class/下的spi_master目錄。

下面來分析SPI controller驅動的註冊與初始化過程,首先執行的是s3c24xx_spi_init。

  1. static int __init s3c24xx_spi_init(void)  
  2. {  
  3.         return platform_driver_probe(&s3c24xx_spi_driver, s3c24xx_spi_probe);  
  4. }  
platform_driver_probe中完成了s3c24xx_spi_driver這個平臺驅動的註冊,相應的平臺設備在devs.c中定義,在smdk2440_devices中添加&s3c_device_spi0,&s3c_device_spi1,這就生成了圖中所示的s3c24xx-spi.0與s3c24xx-spi.1,當然了這圖是在網上找的,不是我畫的,所以是6410的。這裏s3c24xx-spi.0表示s3c2440的spi controller的0號接口,s3c24xx-spi.1表示s3c2440的spi controller的1號接口。註冊了s3c24xx_spi_driver後,賦值了平臺驅動的probe函數爲s3c24xx_spi_probe。所以當match成功後,調用s3c24xx_spi_probe,這裏看其實現:
  1. static int __init s3c24xx_spi_probe(struct platform_device *pdev)  
  2. {  
  3.     struct s3c2410_spi_info *pdata;  
  4.     struct s3c24xx_spi *hw;  
  5.     struct spi_master *master;  
  6.     struct resource *res;  
  7.     int err = 0;  
  8.     /*分配struct spi_master+struct s3c24xx_spi大小的數據,把s3c24xx_spi設爲spi_master的私有數據*/  
  9.     master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));  
  10.     if (master == NULL) {  
  11.         dev_err(&pdev->dev, "No memory for spi_master\n");  
  12.         err = -ENOMEM;  
  13.         goto err_nomem;  
  14.     }  
  15.     /*從master中獲得s3c24xx_spi*/  
  16.     hw = spi_master_get_devdata(master);  
  17.     memset(hw, 0, sizeof(struct s3c24xx_spi));  
  18.   
  19.   
  20.     hw->master = spi_master_get(master);  
  21.     /*驅動移植的時候需要實現的重要結構,初始化爲&s3c2410_spi0_platdata*/  
  22.     hw->pdatapdata = pdata = pdev->dev.platform_data;  
  23.     hw->dev = &pdev->dev;  
  24.   
  25.   
  26.     if (pdata == NULL) {  
  27.         dev_err(&pdev->dev, "No platform data supplied\n");  
  28.         err = -ENOENT;  
  29.         goto err_no_pdata;  
  30.     }  
  31.     /*設置平臺的私有數據爲s3c24xx_spi*/  
  32.     platform_set_drvdata(pdev, hw);  
  33.     init_completion(&hw->done);  
  34.   
  35.   
  36.     /* setup the master state. */  
  37.     /*該總線上的設備數*/  
  38.     master->num_chipselect = hw->pdata->num_cs;  
  39.     /*總線號*/    
  40.     master->bus_num = pdata->bus_num;  
  41.   
  42.   
  43.     /* setup the state for the bitbang driver */  
  44.     /*spi_bitbang專門負責數據的傳輸*/  
  45.     hw->bitbang.master         = hw->master;  
  46.     hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;  
  47.     hw->bitbang.chipselect     = s3c24xx_spi_chipsel;  
  48.     hw->bitbang.txrx_bufs      = s3c24xx_spi_txrx;  
  49.     hw->bitbang.master->setup  = s3c24xx_spi_setup;  
  50.   
  51.   
  52.     dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);  
  53.        
  54.     。。。。。。。。。。。。。。。。。。。。。。。。  
  55.       
  56.     /*初始化設置寄存器,包括對SPIMOSI,SPIMISO,SPICLK引腳的設置*/  
  57.     s3c24xx_spi_initialsetup(hw);  
  58.   
  59.   
  60.     /* register our spi controller */  
  61.   
  62.   
  63.     err = spi_bitbang_start(&hw->bitbang);  
  64.         。。。。。。。。。。。。。。。。。。。。。  
  65. }  
spi controller的register在spi_bitbang_start函數中實現:
  1. int spi_bitbang_start(struct spi_bitbang *bitbang)  
  2. {  
  3.     int status;  
  4.   
  5.   
  6.     if (!bitbang->master || !bitbang->chipselect)  
  7.         return -EINVAL;  
  8.     /*動態創建一個work_struct結構,它的處理函數是bitbang_work*/  
  9.     INIT_WORK(&bitbang->work, bitbang_work);  
  10.     spin_lock_init(&bitbang->lock);  
  11.     INIT_LIST_HEAD(&bitbang->queue);  
  12.     /*spi的數據傳輸就是用這個方法*/  
  13.     if (!bitbang->master->transfer)  
  14.         bitbang->master->transfer = spi_bitbang_transfer;  
  15.     if (!bitbang->txrx_bufs) {  
  16.         bitbang->use_dma = 0;  
  17.         /*spi_s3c24xx.c中有spi_bitbang_bufs方法,在bitbang_work中被調用*/  
  18.         bitbang->txrx_bufs = spi_bitbang_bufs;  
  19.         if (!bitbang->master->setup) {  
  20.             if (!bitbang->setup_transfer)  
  21.                 bitbang->setup_transfer =  
  22.                      spi_bitbang_setup_transfer;  
  23.             /*在spi_s3c24xx.c中有setup的處理方法,在spi_new_device中被調用*/  
  24.             bitbang->master->setup = spi_bitbang_setup;  
  25.             bitbang->master->cleanup = spi_bitbang_cleanup;  
  26.         }  
  27.     } else if (!bitbang->master->setup)  
  28.         return -EINVAL;  
  29.   
  30.   
  31.     /* this task is the only thing to touch the SPI bits */  
  32.     bitbang->busy = 0;  
  33.     /調用create_singlethread_workqueue創建單個工作線程/  
  34.     bitbang->workqueue = create_singlethread_workqueue(  
  35.             dev_name(bitbang->master->dev.parent));  
  36.     if (bitbang->workqueue == NULL) {  
  37.         status = -EBUSY;  
  38.         goto err1;  
  39.     }  
  40.     status = spi_register_master(bitbang->master);  
  41.     if (status < 0)  
  42.         goto err2;  
  43.     return status;  
  44. err2:  
  45.     destroy_workqueue(bitbang->workqueue);  
  46. err1:  
  47.     return status;  
  48. }  

然後看這裏是怎樣註冊spi主機控制器驅動的:

  1. int spi_register_master(struct spi_master *master)  
  2. {  
  3.     。。。。。。。。。。。。。。。。  
  4.     /*將spi添加到內核,這也是sys/class/Spi_master下產生Spi0,Spi1的原因*/  
  5.     dev_set_name(&master->dev, "spi%u", master->bus_num);  
  6.     status = device_add(&master->dev);  
  7.     scan_boardinfo(master);  
  8. }  

這裏跟蹤scan_boardinfo函數:

  1. static void scan_boardinfo(struct spi_master *master)  
  2. {  
  3.     struct boardinfo    *bi;  
  4. mutex_lock(&board_lock);  
  5.     /*遍歷所有掛在board_list上的struct boardinfo*/  
  6.     list_for_each_entry(bi, &board_list, list) {  
  7.         struct spi_board_info   *chip = bi->board_info;  
  8.         unsigned    n;  
  9.         /*遍歷每個boardinfo管理的spi_board_info,如果設備的總線號與控制器的總線好相等,則創建新設備*/  
  10.         for (n = bi->n_board_info; n > 0; n--, chip++) {  
  11.             if (chip->bus_num != master->bus_num)  
  12.                 continue;  
  13.             (void) spi_new_device(master, chip);  
  14.         }  
  15.     }  
  16.     mutex_unlock(&board_lock);  
  17. }  
在移植的時候我們會在mach-smdk2440.c中的smdk2440_machine_init中添加spi_register_board_info

這個函數完成了將spi_board_info交由boardinfo管理,並把boardinfo掛載到board_list鏈表上。也就是說在系統初始化的時候將spi_device交由到掛在board_list上的boardinfo管理,在spi controller的driver註冊的時候不但註冊這個主機控制器的驅動,還要遍歷這個主機控制器的總線上的spi_device,將總線上的spi_device全部註冊進內核。當註冊進內核並且spi_driver已經註冊的時候,如果總線match成功,則會調用spi_driver的probe函數,這個將在後邊進行分析。

  1. int __init  
  2. spi_register_board_info(struct spi_board_info const *info, unsigned n)  
  3. {  
  4.     struct boardinfo    *bi;  
  5.   
  6.   
  7.     bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);  
  8.     if (!bi)  
  9.         return -ENOMEM;  
  10.     bi->nn_board_info = n;  
  11.     memcpy(bi->board_info, info, n * sizeof *info);  
  12.   
  13.   
  14.     mutex_lock(&board_lock);  
  15.     list_add_tail(&bi->list, &board_list);  
  16.     mutex_unlock(&board_lock);  
  17.     return 0;  
  18. }  

看一下創建新設備的函數:

  1. struct spi_device *spi_new_device(struct spi_master *master,  
  2.                   struct spi_board_info *chip)  
  3. {  
  4.     struct spi_device   *proxy;  
  5.     int         status;  
  6.     proxy = spi_alloc_device(master);  
  7.     if (!proxy)  
  8.         return NULL;  
  9.   
  10.   
  11.     WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));  
  12.     /*初始化spi_device的各個字段*/  
  13.     proxy->chipchip_select = chip->chip_select;  
  14.     proxy->max_speed_hz = chip->max_speed_hz;  
  15.     proxy->mode = chip->mode;  
  16.     proxy->irq = chip->irq;  
  17.     /*這裏獲得了spi_device的名字,這個modalias也是在我們移植時在mach-smdk2440.c中的s3c2410_spi0_board中設定的*/  
  18.     strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));  
  19.     proxy->dev.platform_data = (void *) chip->platform_data;  
  20.     proxy->controller_data = chip->controller_data;  
  21.     proxy->controller_state = NULL;  
  22.     /*主要完成將spi_device添加到內核*/  
  23.     status = spi_add_device(proxy);  
  24.     if (status < 0) {  
  25.         spi_dev_put(proxy);  
  26.         return NULL;  
  27.     }  
  28.   
  29.   
  30.     return proxy;  
  31. }  
下面來看分配spi_alloc_device的函數,主要完成了分配spi_device,並初始化spi->dev的一些字段。
  1. struct spi_device *spi_alloc_device(struct spi_master *master)  
  2. {  
  3.     struct spi_device   *spi;  
  4.     struct device       *dev = master->dev.parent;  
  5.     if (!spi_master_get(master))  
  6.         return NULL;  
  7.     spi = kzalloc(sizeof *spi, GFP_KERNEL);  
  8.     if (!spi) {  
  9.         dev_err(dev, "cannot alloc spi_device\n");  
  10.         spi_master_put(master);  
  11.         return NULL;  
  12.     }  
  13.     spi->master = master;  
  14.     spi->dev.parent = dev;  
  15.     /*設置總線是spi_bus_type,下面會講到spi_device與spi_driver是怎樣match上的*/  
  16.     spi->dev.bus = &spi_bus_type;  
  17.     spi->dev.release = spidev_release;  
  18.     device_initialize(&spi->dev);  
  19.     return spi;  
  20. }  
下面來看分配的這個spi_device是怎樣註冊進內核的:
  1. int spi_add_device(struct spi_device *spi)  
  2. {  
  3.     static DEFINE_MUTEX(spi_add_lock);  
  4.     struct device *dev = spi->master->dev.parent;  
  5.     int status;  
  6.     /*spi_device的片選號不能大於spi控制器的片選數*/  
  7.     if (spi->chip_select >= spi->master->num_chipselect) {  
  8.         dev_err(dev, "cs%d >= max %d\n",  
  9.             spi->chip_select,  
  10.             spi->master->num_chipselect);  
  11.         return -EINVAL;  
  12.     }  
  13.     /*這裏設置是spi_device在Linux設備驅動模型中的name,也就是圖中的spi0.0,而在/dev/下設備節點的名字是proxy->modalias中的名字*/  
  14.     dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),  
  15.             spi->chip_select);  
  16.     mutex_lock(&spi_add_lock);  
  17.     /*如果總線上掛的設備已經有這個名字,則設置狀態忙碌,並退出*/  
  18.     if (bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev))  
  19.             != NULL) {  
  20.         dev_err(dev, "chipselect %d already in use\n",  
  21.                 spi->chip_select);  
  22.         status = -EBUSY;  
  23.         goto done;  
  24.     }  
  25.     /對spi_device的時鐘等進行設置/  
  26.     status = spi->master->setup(spi);  
  27.     if (status < 0) {  
  28.         dev_err(dev, "can't %s %s, status %d\n",  
  29.                 "setup", dev_name(&spi->dev), status);  
  30.         goto done;  
  31.     }  
  32.     /*添加到內核*/  
  33.     status = device_add(&spi->dev);  
  34.     if (status < 0)  
  35.         dev_err(dev, "can't %s %s, status %d\n",  
  36.                 "add", dev_name(&spi->dev), status);  
  37.     else  
  38.         dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));  
  39.   
  40.   
  41. done:  
  42.     mutex_unlock(&spi_add_lock);  
  43.     return status;  
  44. }  
  45.   
  46.   
  47. static int s3c24xx_spi_setup(struct spi_device *spi)  
  48. {  
  49.     。。。。。。。。。。。。。。  
  50.     ret = s3c24xx_spi_setupxfer(spi, NULL);  
  51.     。。。。。。。。。。。。。。  
  52. }  
  53.   
  54.   
  55. static int s3c24xx_spi_setupxfer(struct spi_device *spi,  
  56.                  struct spi_transfer *t)  
  57. {  
  58.     struct s3c24xx_spi *hw = to_hw(spi);  
  59.     unsigned int bpw;  
  60.     unsigned int hz;  
  61.     unsigned int div;  
  62.     /*設置了每字長的位數,發送速度*/  
  63.     bpw = t ? t->bits_per_word : spi->bits_per_word;  
  64.     hz  = t ? t->speed_hz : spi->max_speed_hz;  
  65.   
  66.   
  67.     if (bpw != 8) {  
  68.         dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);  
  69.         return -EINVAL;  
  70.     }  
  71.     /*設置分頻值*/  
  72.     div = clk_get_rate(hw->clk) / hz;  
  73.   
  74.   
  75.     /* is clk = pclk / (2 * (pre+1)), or is it 
  76.      *    clk = (pclk * 2) / ( pre + 1) */  
  77.   
  78.   
  79.     div /= 2;  
  80.   
  81.   
  82.     if (div > 0)  
  83.         div -= 1;  
  84.   
  85.   
  86.     if (div > 255)  
  87.         div = 255;  
  88.   
  89.   
  90.     dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", div, hz);  
  91.     writeb(div, hw->regs + S3C2410_SPPRE);  
  92.   
  93.   
  94.     spin_lock(&hw->bitbang.lock);  
  95.     if (!hw->bitbang.busy) {  
  96.         hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);  
  97.         /* need to ndelay for 0.5 clocktick ? */  
  98.     }  
  99.     spin_unlock(&hw->bitbang.lock);  
  100.   
  101.   
  102.     return 0;  
  103. }  
下面來看這個spi_driver是怎樣註冊的,又是與spi_device怎樣match上的。
在spidev.c中:

  1. static int __init spidev_init(void)  
  2. {  
  3.     int status;  
  4.     BUILD_BUG_ON(N_SPI_MINORS > 256);  
  5.     status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);  
  6.     if (status < 0)  
  7.         return status;  
  8.     spidev_class = class_create(THIS_MODULE, "spidev");  
  9.     if (IS_ERR(spidev_class)) {  
  10.         unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);  
  11.         return PTR_ERR(spidev_class);  
  12.     }  
  13.     status = spi_register_driver(&spidev_spi);  
  14.     if (status < 0) {  
  15.         class_destroy(spidev_class);  
  16.         unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);  
  17.     }  
  18.     return status;  
  19. }  
註冊了名爲”spi”的字符驅動,然後註冊了spidev_spi驅動,這個就是圖中sys/Bus/Spi/Drivers/下的spidev。
  1. static struct spi_driver spidev_spi = {  
  2.     .driver = {  
  3.         .name =     "spidev",  
  4.         .owner =    THIS_MODULE,  
  5.     },  
  6.     .probe =    spidev_probe,  
  7.     .remove =   __devexit_p(spidev_remove),  
  8. };  
  1. static struct spi_driver spidev_spi = {  
  2.     .driver = {  
  3.         .name =     "spidev",  
  4.         .owner =    THIS_MODULE,  
  5.     },  
  6.     .probe =    spidev_probe,  
  7.     .remove =   __devexit_p(spidev_remove),  
  8. };  
這裏來看__driver_attach這個函數,其中分別調用了driver_match_device,driver_probe_device函數。如果匹配成果調用probe函數,否則返回。
  1. static int __driver_attach(struct device *dev, void *data)    
  2. {    
  3.     struct device_driver *drv = data;    
  4.     if (!driver_match_device(drv, dev))    
  5.         return 0;    
  6.     
  7.     if (dev->parent) /* Needed for USB */    
  8.         down(&dev->parent->sem);    
  9.     down(&dev->sem);    
  10.     if (!dev->driver)    
  11.         driver_probe_device(drv, dev);    
  12.     up(&dev->sem);    
  13.     if (dev->parent)    
  14.         up(&dev->parent->sem);    
  15.     
  16.     return 0;    
  17. }    
匹配的時候調用的bus的match函數。
  1. struct bus_type spi_bus_type = {  
  2.        .name             = "spi",  
  3.        .dev_attrs       = spi_dev_attrs,  
  4.        .match           = spi_match_device,  
  5.        .uevent           = spi_uevent,  
  6.        .suspend  = spi_suspend,  
  7.        .resume          = spi_resume,  
  8. };  
  9. static int spi_match_device(struct device *dev, struct device_driver *drv)  
  10. {  
  11.     const struct spi_device *spi = to_spi_device(dev);  
  12.   
  13.   
  14.     return strcmp(spi->modalias, drv->name) == 0;  
  15. }  
可以看到這裏根據驅動和設備的名字進行匹配,匹配成功後調用驅動的probe函數。
  1. static int spi_drv_probe(struct device *dev)  
  2. {  
  3.     const struct spi_driver     *sdrv = to_spi_driver(dev->driver);  
  4.   
  5.   
  6.     return sdrv->probe(to_spi_device(dev));  
  7. }  
可以看大調用了具體的probe函數,這裏實現了把spidev添加到device_list,這樣這個虛擬的字符驅動就註冊並初始化完畢。
  1. static int spidev_remove(struct spi_device *spi)  
  2. {  
  3.     struct spidev_data  *spidev = spi_get_drvdata(spi);  
  4.   
  5.   
  6.     /* make sure ops on existing fds can abort cleanly */  
  7.     spin_lock_irq(&spidev->spi_lock);  
  8.     spidev->spi = NULL;  
  9.     spi_set_drvdata(spi, NULL);  
  10.     spin_unlock_irq(&spidev->spi_lock);  
  11.   
  12.   
  13.     /* prevent new opens */  
  14.     mutex_lock(&device_list_lock);  
  15.     list_del(&spidev->device_entry);  
  16.     device_destroy(spidev_class, spidev->devt);  
  17.     clear_bit(MINOR(spidev->devt), minors);  
  18.     if (spidev->users == 0)  
  19.         kfree(spidev);  
  20.     mutex_unlock(&device_list_lock);  
  21.   
  22.   
  23.     return 0;  
  24. }  
在spidev的註冊函數中註冊了文件操作集合file_operations,爲用戶空間提供了操作SPI controller的接口。
  1. static struct file_operations spidev_fops = {  
  2.     .owner =    THIS_MODULE,  
  3.     /* REVISIT switch to aio primitives, so that userspace 
  4.      * gets more complete API coverage.  It'll simplify things 
  5.      * too, except for the locking. 
  6.      */  
  7.     .write =    spidev_write,  
  8.     .read =     spidev_read,  
  9.     .unlocked_ioctl = spidev_ioctl,  
  10.     .open =     spidev_open,  
  11.     .release =  spidev_release,  
  12. };  

到此爲止spi子系統與spi_master,spi_device,spi_driver這個Linux設備驅動模型已經建立完了。


Linux驅動修煉之道-SPI驅動框架源碼分析(下)

分類: linux驅動編程 1978人閱讀 評論(0) 收藏 舉報

努力成爲linux kernel hacker的人李萬鵬原創作品,爲夢而戰。轉載請標明出處 

http://blog.csdn.net/woshixingaaa/article/details/6574224

這篇文檔主要介紹spi數據傳輸過程。

當應用層要向設備傳輸數據的時候,會通過ioctl向設備驅動發送傳輸數據的命令。如圖,向SPI從設備發送讀寫命令,實際的讀寫操作還是調用了主機控制器驅動的數據傳輸函數。transfer函數用於spi的IO傳輸。但是,transfer函數一般不會執行真正的傳輸操作,而是把要傳輸的內容放到一個隊列裏,然後調用一種類似底半部的機制進行真正的傳輸。這是因爲,spi總線一般會連多個spi設備,而spi設備間的訪問可能會併發。如果直接在transfer函數中實現傳輸,那麼會產生競態,spi設備互相間會干擾。所以,真正的spi傳輸與具體的spi控制器的實現有關,spi的框架代碼中沒有涉及。像spi設備的片選,根據具體設備進行時鐘調整等等都在實現傳輸的代碼中被調用。spi的傳輸命令都是通過結構spi_message定義,設備程序調用transfer函數將spi_message交給spi總線驅動,總線驅動再將message傳到底半部排隊,實現串行化傳輸。



在spidev.c中實現了file_operations:

  1. <span style="font-size:18px;">static struct file_operations spidev_fops = {  
  2.     .owner =    THIS_MODULE,  
  3.     .write =    spidev_write,  
  4.     .read =     spidev_read,  
  5.     .unlocked_ioctl = spidev_ioctl,  
  6.     .open =     spidev_open,  
  7.     .release =  spidev_release,  
  8. };</span>  
這裏看spidev_ioctl的實現:

  1. <span style="font-size:18px;">static long  
  2. spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)  
  3. {  
  4.     int         err = 0;  
  5.     int         retval = 0;  
  6.     struct spidev_data  *spidev;  
  7.     struct spi_device   *spi;  
  8.     u32         tmp;  
  9.     unsigned        n_ioc;  
  10.     struct spi_ioc_transfer *ioc;  
  11.   
  12.     /*查看這個命令的幻數字段是否爲'k'*/  
  13.     if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)  
  14.         return -ENOTTY;  
  15.   
  16.     /*如果方向是用戶空間從內核讀,即內核向用戶空間寫,則檢查用戶空間的地址是否有效*/  
  17.     if (_IOC_DIR(cmd) & _IOC_READ)  
  18.         err = !access_ok(VERIFY_WRITE,  
  19.                 (void __user *)arg, _IOC_SIZE(cmd));  
  20.     /*如果方向是用戶空間向內核寫,即內核讀用戶空間,則檢查用戶空間的地址是否有效*/  
  21.     if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)  
  22.         err = !access_ok(VERIFY_READ,  
  23.                 (void __user *)arg, _IOC_SIZE(cmd));  
  24.     if (err)  
  25.         return -EFAULT;  
  26.   
  27.     /* guard against device removal before, or while, 
  28.      * we issue this ioctl. 
  29.      */  
  30.     spidev = filp->private_data;  
  31.     spin_lock_irq(&spidev->spi_lock);  
  32.     spi = spi_dev_get(spidev->spi);  
  33.     spin_unlock_irq(&spidev->spi_lock);  
  34.   
  35.     if (spi == NULL)  
  36.         return -ESHUTDOWN;  
  37.   
  38.     mutex_lock(&spidev->buf_lock);  
  39.   
  40.     switch (cmd) {  
  41.     /* read requests */  
  42.     case SPI_IOC_RD_MODE:  
  43.         /*因爲已經進行了地址是否有效的檢查,所以這裏使用__put_user,__get_user,__copy_from_user可以節省幾個時鐘週期呢*/  
  44.         retval = __put_user(spi->mode & SPI_MODE_MASK,  
  45.                     (__u8 __user *)arg);  
  46.         break;  
  47.     case SPI_IOC_RD_LSB_FIRST:  
  48.         retval = __put_user((spi->mode & SPI_LSB_FIRST) ?  1 : 0,  
  49.                     (__u8 __user *)arg);  
  50.         break;  
  51.     case SPI_IOC_RD_BITS_PER_WORD:  
  52.         retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);  
  53.         break;  
  54.     case SPI_IOC_RD_MAX_SPEED_HZ:  
  55.         retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);  
  56.         break;  
  57.   
  58.     /*設置SPI模式*/  
  59.     case SPI_IOC_WR_MODE:  
  60.         retval = __get_user(tmp, (u8 __user *)arg);  
  61.         if (retval == 0) {  
  62.             /*先將之前的模式保存起來,一旦設置失敗進行回覆*/  
  63.             u8  save = spi->mode;  
  64.   
  65.             if (tmp & ~SPI_MODE_MASK) {  
  66.                 retval = -EINVAL;  
  67.                 break;  
  68.             }  
  69.   
  70.             tmp |= spi->mode & ~SPI_MODE_MASK;  
  71.             spi->mode = (u8)tmp;  
  72.             retval = spi_setup(spi);  
  73.             if (retval < 0)  
  74.                 spi->mode = save;  
  75.             else  
  76.                 dev_dbg(&spi->dev, "spi mode %02x\n", tmp);  
  77.         }  
  78.         break;  
  79.     case SPI_IOC_WR_LSB_FIRST:  
  80.         retval = __get_user(tmp, (__u8 __user *)arg);  
  81.         if (retval == 0) {  
  82.             u8  save = spi->mode;  
  83.   
  84.             if (tmp)  
  85.                 spi->mode |= SPI_LSB_FIRST;  
  86.             else  
  87.                 spi->mode &= ~SPI_LSB_FIRST;  
  88.             retval = spi_setup(spi);  
  89.             if (retval < 0)  
  90.                 spi->mode = save;  
  91.             else  
  92.                 dev_dbg(&spi->dev, "%csb first\n",  
  93.                         tmp ? 'l' : 'm');  
  94.         }  
  95.         break;  
  96.     case SPI_IOC_WR_BITS_PER_WORD:  
  97.         retval = __get_user(tmp, (__u8 __user *)arg);  
  98.         if (retval == 0) {  
  99.             u8  save = spi->bits_per_word;  
  100.   
  101.             spi->bits_per_word = tmp;  
  102.             retval = spi_setup(spi);  
  103.             if (retval < 0)  
  104.                 spi->bits_per_word = save;  
  105.             else  
  106.                 dev_dbg(&spi->dev, "%d bits per word\n", tmp);  
  107.         }  
  108.         break;  
  109.     case SPI_IOC_WR_MAX_SPEED_HZ:  
  110.         retval = __get_user(tmp, (__u32 __user *)arg);  
  111.         if (retval == 0) {  
  112.             u32 save = spi->max_speed_hz;  
  113.   
  114.             spi->max_speed_hz = tmp;  
  115.             retval = spi_setup(spi);  
  116.             if (retval < 0)  
  117.                 spi->max_speed_hz = save;  
  118.             else  
  119.                 dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);  
  120.         }  
  121.         break;  
  122.   
  123.     default:  
  124.         /* segmented and/or full-duplex I/O request */  
  125.         if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))  
  126.                 || _IOC_DIR(cmd) != _IOC_WRITE) {  
  127.             retval = -ENOTTY;  
  128.             break;  
  129.         }  
  130.         /*得到用戶空間數據的大小*/  
  131.         tmp = _IOC_SIZE(cmd);  
  132.         /*如果這些數據不能分成spi_ioc_transfer的整數倍,則不能進行傳輸,spi_io_transfer是對spi_transfer的映射*/  
  133.         if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {  
  134.             retval = -EINVAL;  
  135.             break;  
  136.         }  
  137.         /*計算出能分多少個spi_ioc_transfer*/  
  138.         n_ioc = tmp / sizeof(struct spi_ioc_transfer);  
  139.         if (n_ioc == 0)  
  140.             break;  
  141.   
  142.         /*在內核中分配裝載這些數據的內存空間*/  
  143.         ioc = kmalloc(tmp, GFP_KERNEL);  
  144.         if (!ioc) {  
  145.             retval = -ENOMEM;  
  146.             break;  
  147.         }  
  148.         /*把用戶空間的數據拷貝過來*/  
  149.         if (__copy_from_user(ioc, (void __user *)arg, tmp)) {  
  150.             kfree(ioc);  
  151.             retval = -EFAULT;  
  152.             break;  
  153.         }  
  154.   
  155.         /*進行數據傳輸*/  
  156.         <span style="color:#ff0000;">retval = spidev_message(spidev, ioc, n_ioc);</span>  
  157.         kfree(ioc);  
  158.         break;  
  159.     }  
  160.   
  161.     mutex_unlock(&spidev->buf_lock);  
  162.     spi_dev_put(spi);  
  163.     return retval;  
  164. }  
  165. </span>  

下面跟蹤spidev_message看看:

  1. <span style="font-size:18px;">static int spidev_message(struct spidev_data *spidev,  
  2.         struct spi_ioc_transfer *u_xfers, unsigned n_xfers)  
  3. {  
  4.     struct spi_message  msg;  
  5.     struct spi_transfer *k_xfers;  
  6.     struct spi_transfer *k_tmp;  
  7.     struct spi_ioc_transfer *u_tmp;  
  8.     unsigned        n, total;  
  9.     u8          *buf;  
  10.     int         status = -EFAULT;  
  11.     /*初始化spi_message的tranfers鏈表頭*/  
  12.     spi_message_init(&msg);  
  13.     /*分配n個spi_transfer的內存空間,一個spi_message由多個數據段spi_message組成*/  
  14.     k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);  
  15.     if (k_xfers == NULL)  
  16.         return -ENOMEM;  
  17.   
  18.     buf = spidev->buffer;  
  19.     total = 0;  
  20.     /*這個for循環的主要任務是將所有的spi_transfer組裝成一個spi_message*/  
  21.     for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;  
  22.             n;  
  23.             n--, k_tmp++, u_tmp++) {  
  24.         /*u_tmp是從用戶空間傳下來的spi_ioc_message的大小,spi_ioc_message是對spi_message的映射*/  
  25.         k_tmp->len = u_tmp->len;  
  26.         /*統計要傳輸數據的總量*/  
  27.         total += k_tmp->len;  
  28.         if (total > bufsiz) {  
  29.             status = -EMSGSIZE;  
  30.             goto done;  
  31.         }  
  32.         /*spi_transfer是一個讀寫的buffer對,如果是要接收則把buffer給接收的rx_buf*/  
  33.         if (u_tmp->rx_buf) {  
  34.             k_tmp->rx_buf = buf;  
  35.             if (!access_ok(VERIFY_WRITE, (u8 __user *)  
  36.                         (uintptr_t) u_tmp->rx_buf,  
  37.                         u_tmp->len))  
  38.                 goto done;  
  39.         }  
  40.         /*如果要傳輸,這個buffer給tx_buf使用,從用戶空間拷過來要傳輸的數據*/  
  41.         if (u_tmp->tx_buf) {  
  42.             k_tmp->tx_buf = buf;  
  43.             if (copy_from_user(buf, (const u8 __user *)  
  44.                         (uintptr_t) u_tmp->tx_buf,  
  45.                     u_tmp->len))  
  46.                 goto done;  
  47.         }  
  48.         /*指向下一段內存*/  
  49.         buf += k_tmp->len;  
  50.         /*最後一個transfer傳輸完畢是否會影響片選*/  
  51.         k_tmp->cs_change = !!u_tmp->cs_change;  
  52.         /*每字長的字節數*/  
  53.         k_tmp->bits_per_word = u_tmp->bits_per_word;  
  54.         /*一段數據傳輸完需要一定的時間等待*/  
  55.         k_tmp->delay_usecs = u_tmp->delay_usecs;  
  56.         /*初始化傳輸速度*/  
  57.         k_tmp->speed_hz = u_tmp->speed_hz;  
  58.         /*將spi_transfer通過它的transfer_list字段掛到spi_message的transfer隊列上*/  
  59.         spi_message_add_tail(k_tmp, &msg);  
  60.     }  
  61.     /*調用底層的傳輸函數*/  
  62.     <span style="color:#ff0000;">status = spidev_sync(spidev, &msg);</span>  
  63.     if (status < 0)  
  64.         goto done;  
  65.   
  66.     /* copy any rx data out of bounce buffer */  
  67.     buf = spidev->buffer;  
  68.     /*把傳輸數據拷貝到用戶空間打印出來,可以查看是否傳輸成功*/  
  69.     for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {  
  70.         if (u_tmp->rx_buf) {  
  71.             if (__copy_to_user((u8 __user *)  
  72.                     (uintptr_t) u_tmp->rx_buf, buf,  
  73.                     u_tmp->len)) {  
  74.                 status = -EFAULT;  
  75.                 goto done;  
  76.             }  
  77.         }  
  78.         buf += u_tmp->len;  
  79.     }  
  80.     status = total;  
  81.   
  82. done:  
  83.     kfree(k_xfers);  
  84.     return status;  
  85. }  
  86. </span>  

看spidev_sync的實現:

  1. <span style="font-size:18px;">static ssize_t  
  2. spidev_sync(struct spidev_data *spidev, struct spi_message *message)  
  3. {  
  4.     /*聲明並初始化一個完成量*/  
  5.     DECLARE_COMPLETION_ONSTACK(done);  
  6.     int status;  
  7.     /*指定spi_message使用的喚醒完成量函數*/  
  8.     message->complete = spidev_complete;  
  9.     message->context = &done;  
  10.   
  11.     spin_lock_irq(&spidev->spi_lock);  
  12.     if (spidev->spi == NULL)  
  13.         status = -ESHUTDOWN;  
  14.     else  
  15.         /*調用spi核心中的函數進行數據傳輸*/  
  16.     <span style="color:#ff0000;"> status = spi_async(spidev->spi, message);</span>  
  17.     spin_unlock_irq(&spidev->spi_lock);  
  18.   
  19.     if (status == 0) {  
  20.         /*等待完成量被喚醒*/  
  21.         wait_for_completion(&done);  
  22.         status = message->status;  
  23.         if (status == 0)  
  24.             status = message->actual_length;  
  25.     }  
  26.     return status;  
  27. }  
  28. </span>  
spi_async在spi.h中定義的:
  1. <span style="font-size:18px;">static inline int  
  2. spi_async(struct spi_device *spi, struct spi_message *message)  
  3. {  
  4.     message->spi = spi;  
  5.     return spi->master->transfer(spi, message);  
  6. }  
  7. </span>  
這裏的master->transfer是在spi_bitbang_start中進行賦值的:

bitbang->master->transfer= spi_bitbang_transfer;

看spi_bitbang_transfer的實現:

  1. <span style="font-size:18px;">int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)  
  2. {  
  3.     struct spi_bitbang  *bitbang;  
  4.     unsigned long       flags;  
  5.     int         status = 0;  
  6.   
  7.     m->actual_length = 0;  
  8.     m->status = -EINPROGRESS;  
  9.     /*在spi_alloc_master函數中調用spi_master_set_devdata把struct s3c24xx_spi結構存放起來,而struct spi_bitbang正是struct s3c24xx_spi結構所包含的第一個結構*/  
  10.     bitbang = spi_master_get_devdata(spi->master);  
  11.   
  12.     spin_lock_irqsave(&bitbang->lock, flags);  
  13.     if (!spi->max_speed_hz)  
  14.         status = -ENETDOWN;  
  15.     else {  
  16.         /*把message加入到bitbang的等待隊列中*/  
  17.         list_add_tail(&m->queue, &bitbang->queue);  
  18.         /*把bitbang-work加入bitbang->workqueue中,調度運行*/  
  19.         queue_work(bitbang->workqueue, &bitbang->work);  
  20.     }  
  21.     spin_unlock_irqrestore(&bitbang->lock, flags);  
  22.   
  23.     return status;  
  24. }  
  25. EXPORT_SYMBOL_GPL(spi_bitbang_transfer);  
  26. </span>  

分析工作隊列的處理函數:


  1. <span style="font-size:18px;">static void bitbang_work(struct work_struct *work)  
  2. {  
  3.     struct spi_bitbang  *bitbang =  
  4.         container_of(work, struct spi_bitbang, work);  
  5.     unsigned long       flags;  
  6.   
  7.     spin_lock_irqsave(&bitbang->lock, flags);  
  8.     /*設置成忙狀態*/  
  9.     bitbang->busy = 1;  
  10.     /*對bitqueue中的每一個spi_message進行處理*/  
  11.     while (!list_empty(&bitbang->queue)) {  
  12.         struct spi_message  *m;  
  13.         struct spi_device   *spi;  
  14.         unsigned        nsecs;  
  15.         struct spi_transfer *t = NULL;  
  16.         unsigned        tmp;  
  17.         unsigned        cs_change;  
  18.         int         status;  
  19.         int         (*setup_transfer)(struct spi_device *,  
  20.                         struct spi_transfer *);  
  21.   
  22.         m = container_of(bitbang->queue.next, struct spi_message,  
  23.                 queue);  
  24.         /*從隊列中驅動這個spi_message*/  
  25.         list_del_init(&m->queue);  
  26.         spin_unlock_irqrestore(&bitbang->lock, flags);  
  27.   
  28.         nsecs = 100;  
  29.   
  30.         spi = m->spi;  
  31.         tmp = 0;  
  32.         cs_change = 1;  
  33.         status = 0;  
  34.         setup_transfer = NULL;  
  35.         /*對spi_message的transfers上的每個spi_transfer進行處理*/  
  36.         list_for_each_entry (t, &m->transfers, transfer_list) {  
  37.         。。。。。。。。。。。。。。。。。  
  38.             if (t->len) {                  
  39.                 if (!m->is_dma_mapped)  
  40.                     t->rx_dma = t->tx_dma = 0;  
  41.                 /*調用bitbang->txrx_bufs進行數據的傳輸,bitbang->txrx_bufs = s3c24xx_spi_txrx;這個在s3c24xx_spi_probe中進行賦值的*/  
  42.                 <span style="color:#ff0000;">status = bitbang->txrx_bufs(spi, t);</span>  
  43.             }  
  44.         。。。。。。。。。。。。。。。。  
  45.   
  46.         m->status = status;  
  47.         /*傳輸完成,喚醒剛纔的那個完成變量*/  
  48.         m->complete(m->context);  
  49.   
  50.         /* restore speed and wordsize */  
  51.         if (setup_transfer)  
  52.             setup_transfer(spi, NULL);  
  53.         if (!(status == 0 && cs_change)) {  
  54.             ndelay(nsecs);  
  55.             bitbang->chipselect(spi, BITBANG_CS_INACTIVE);  
  56.             ndelay(nsecs);  
  57.         }  
  58.   
  59.         spin_lock_irqsave(&bitbang->lock, flags);  
  60.     }  
  61.     bitbang->busy = 0;  
  62.     spin_unlock_irqrestore(&bitbang->lock, flags);  
  63. }  
  64. </span>  
這個工作隊列的處理函數中調用了spi controller driver中的傳輸函數:
  1. <span style="font-size:18px;">static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)  
  2. {  
  3.     struct s3c24xx_spi *hw = to_hw(spi);  
  4.   
  5.     dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",  
  6.         t->tx_buf, t->rx_buf, t->len);  
  7.   
  8.     hw->tx = t->tx_buf;  //發送指針  
  9.     hw->rx = t->rx_buf;  //接收指針  
  10.     hw->len = t->len;    //需要發送/接收的數目  
  11.     hw->count = 0;       //存放實際spi傳輸的數據數目  
  12.     /*初始化了完成量*/  
  13.     init_completion(&hw->done);  
  14.       
  15.     /* 
  16.      *只需發送第一個字節(如果發送爲空,則發送0xff),中斷中就會自動發送完其他字節(並接受數據) 
  17.      *直到所有數據發送完畢且所有數據接收完畢才返回 
  18.      */  
  19.     writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);  
  20.     /*等待完成量被喚醒*/  
  21.     wait_for_completion(&hw->done);  
  22.     return hw->count;  
  23. }  
  24. static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)  
  25. {  
  26.         return hw->tx ? hw->tx[count] : 0xff;  
  27.         //如果還有數據沒接收完且要發送的數據經已發送完畢,發送空數據0xFF  
  28. }  
  29. </span>  
下面來分析中斷函數:
  1. <span style="font-size:18px;">static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)  
  2. {  
  3.     struct s3c24xx_spi *hw = dev;  
  4.     /*讀取spi的狀態寄存器*/  
  5.     unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);  
  6.     unsigned int count = hw->count;  
  7.     /*檢測衝突*/  
  8.     if (spsta & S3C2410_SPSTA_DCOL) {               
  9.         dev_dbg(hw->dev, "data-collision\n");   
  10.         /*喚醒完成量*/  
  11.         complete(&hw->done);  
  12.         goto irq_done;  
  13.     }  
  14.     /*設備忙*/  
  15.     if (!(spsta & S3C2410_SPSTA_READY)) {  
  16.         dev_dbg(hw->dev, "spi not ready for tx?\n");  
  17.         /*喚醒完成量*/  
  18.         complete(&hw->done);  
  19.         goto irq_done;  
  20.     }  
  21.   
  22.     hw->count++;  
  23.     /*接收數據*/  
  24.     if (hw->rx)  
  25.         hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);  
  26.   
  27.     count++;  
  28.     /*如果count小於需要發送或接收數據的數目,發送其他數據*/  
  29.     if (count < hw->len)  
  30.         writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);  
  31.     else  
  32.         /*喚醒完成量,通知s3c24xx_spi_txrx函數*/  
  33.         complete(&hw->done);  
  34.   
  35.  irq_done:  
  36.     return IRQ_HANDLED;  
  37. }  
  38. </span>  
至此spi數據傳輸過程完成,如果不想爲自己的SPI設備寫驅動,那麼可以用Linux自帶的spidev.c提供的驅動程序,只要在登記時,把設備名設置成spidev就可以了。spidev.c會在device目錄下自動爲每一個匹配的SPI設備創建設備節點,節點名"spi%d"。之後,用戶程序可以通過字符型設備的通用接口控制SPI設備。需要注意的是,spidev創建的設備在設備模型中屬於虛擬設備,他的class是spidev_class,他的父設備是在boardinfo中定義的spi設備。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章