最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

Linux 驅動程式的 I/O

Driver crifan 5212浏览 0评论

Linux 驅動程式的 I/O, #1 #2: 基本概念[ZT]

由本篇日記開始,我們將進行「Linux Device Driver 入門:I/O 處理」的議題討論。這裡所提的 I/O 處理定義是:user process 與 physical device 的 I/O 存取。

在讀「Linux Device Driver 入門:I/O 處理」專欄前,您必須熟悉 Linux 驅動程式的架構,因此「Linux Device Driver 入門:架構層」的專欄是 Jollen’s Linux Device Driver 系列專欄的先備知識;此外,接下來的專欄使用的語法也必須對架構層有基本認識後才能看得懂。

Linux 驅動程式 I/O 機制

Linux device driver 處理 I/O 的「基本款」是:

  • fops->ioctl

  • fops->read

  • fops->write

另外「典藏款」則是 mmap,未來在「Linux Device Driver 進階」專欄裡再來討論這個主題。

fops->ioctl

ioctl 代表 input/output control 的意思,故名思義,ioctl system call 是用來控制 I/O 讀寫用的,並且是支援 user application 存取裝置的重要 system call。因此,在Linux驅動程式設計上,我們會實作ioctl system call以提供user application讀寫(input/output)裝置的功能。

依此觀念,回到架構篇所舉的 debug card 範例。當 user application 需要將數字顯示到 debug card 時,範例 debug card 0.1.0 便需要實作 ioctl system call,然後在 fops->ioctl 裡呼叫 outb() 將 user application 所指定的數字輸出至 I/O port 80H。

User application 使用 GNU LIBC 的 ioctl() 函數呼叫device driver所提供的命令來「控制」裝置,因此驅動程式必須實作 fops->ioctl 以提供「命令」給使用者。

fops->read & fops->write

read/write 是 Linux 驅動程式最重要的 2 個 driver function,也是驅動程式最核心的觀念所在。對驅動程式而言,read/write 的目的是在實作並支援 user application 的 read() 與 write() 函數;user application 是否能正常由硬體讀寫資料,完全掌握在驅動程式的 read/write method。

User application 呼叫 read()/write() 函數後,就會執行 fops->readfops->write。read/write method 負責讀取使用者資料與進行裝置的I/O 存取。依照觸發資料傳輸的方式來區分,我們可以將 I/O 裝置分成以下 2 種(from hardware view):

  • Polling:I/O裝置不具備中斷。
  • Interrupt:I/O裝置以中斷觸發方式進行I/O。

根據I/O處理原理的不同(from software view),可以將 read/write method 的實作策略分成多種排列組合來討論。為了簡化討論內容,未來的日記將鎖定「Interrupt 式的 I/O」來做探討。

Linux 驅動程式的 I/O, #2: I/O 存取相關函數

I/O 存取相關函數

要提到「I/O 處理」當然要整理 Linux 提供的相關函數,以下分 3 大類來整理:

1. I/O port
2. I/O memory
3. PCI configuration space
4. ioremap

I/O Port

以下是 Linux 提供最原始的 I/O port 存取函數:

˙ unsigned inb(unsigned port);
˙ unsigned inw(unsigned port);
˙ unsigned inl(unsigned port);
˙ void outb(unsigned char byte, unsigned port);
˙ void outw(unsigned short word, unsigned port);
˙ void outl(unsigned long word, unsigned port);

I/O Memory

以下是 Linux 提供最原始的 I/O memory 存取函數:

˙ unsigned readb(unsigned port);
˙ unsigned readw(unsigned port);
˙ unsigned readl(unsigned port);
˙ void writeb(unsigned char byte, unsigned port);
˙ void writew(unsigned short word, unsigned port);
˙ void writel(unsigned long word, unsigned port);

對於 I/O memory 的操作,Linux 也提供 memory copy 系列函數如下:

˙ memset_io(address, value, count);
˙ memcpy_fromio(dest, source, num);
˙ memcpy_toio(dest, source, num);

以上在「Linux 驅動程式觀念解析, #6: 依流程來實作 — Physical Device Driver」介紹過一次,並且也搭配了一個簡單範例做說明,您可參考該文。

PCI Configuration Space

Linux 也提供讀寫 PCI configuration space(PCI BIOS)的函數:

˙ int pci_read_config_byte(struct pci_dev *dev, int where, u8 *val);
˙ int pci_read_config_word(struct pci_dev *dev, int where, u16 *val);
˙ int pci_read_config_dword(struct pci_dev *dev, int where, u32 *val);
˙ int pci_write_config_byte(struct pci_dev *dev, int where, u8 val);
˙ int pci_write_config_word(struct pci_dev *dev, int where, u16 val);
˙ int pci_write_config_dword(struct pci_dev *dev, int where, u32 val);

有些朋友可能看過開頭是 pcibios_* 的函數版本,「不過這是舊的函數,請勿再使用」。

ioremap()

這個 API 就重要到不行了,任何時候,Linux device driver 都「不能直接存取 physical address」。所以,「使用以上的 I/O 相關函數時,只能傳 virtual address,不能傳入 physical address」,ioremap() 就是用來將 physical address 對應到 virtual address 的 API。

小結

對於 I/O 函數的使用,應該在「深諳」Linux驅動程式架構與Linux作業系統原理的情況下使用,「單純的 kernel module + IO APIs」並不叫做 Linux 驅動程式,再更進一步的「kernel module + read/write/ioctl + IO APIs」也只是小聰明(編註),還是稱不上 Linux「驅動程式」。建構在作業系統裡的驅動程式,90% 都是在實作良好的機制與行為,因此「OS 原理與機制的研究」,才是正確的思考方向。與大家分享自己的心得,希望對您的學習有幫助。

編註:這是 Linux device driver 的「開始」但不是全部,也只是冰山一角。但是許多教育訓練機構的課程卻是以此為做為規劃方向,並不是很妥當。

Linux 驅動程式的 I/O, #3: kernel-space 與 user-space 的「I/O」

重要觀念

任何作業系統底下的「驅動程式」,都需要分二個層面來討論所謂的「I/O 處理」:

1. 實體層:驅動程式 v.s. 硬體。

2. 虛擬層:驅動程式 v.s. user process

在前一篇日記「Linux 驅動程式的 I/O, #2: I/O 存取相關函數」中所提到的 I/O 函數是處理「實體層」的 I/O;本日記所要介紹的 copy_to_user()copy_from_user() 則是在處理「虛擬層」的 I/O。另外,在繼續往下讀之前,您必須了解以下的觀念都是「等價」的:

1. 驅動程式與 user process 的 I/O;等於

2. 驅動程式與 user process 間的 data communication;等於

3. kernel-space 與 user-space 間的 data communication。

此外,還要了解:

1. user-space 無法「直接」存取 kernel-space 的記憶體。

2. 「Linux device driver」與「user-space」間的 I/O 會與 fops->readfops->writefops->ioctl 共三個 system call 有關。

copy_to_user() 與 copy_from_user()

了解以上的觀念後,再來「直接殺進重點」就很容易懂了:從 user-space 讀取資料至 kernel-space,或是將 kernel-space 的資料寫至 user-space,「必須」透過 kernel 提供的 2 個 API 來進行。這二個 API 如下:

˙ long copy_to_user(void *to, const void *from, long n);
˙ long copy_from_user(void *to, const void *from, long n);

參數說明,以 copy_to_user() 來說:

˙ to:資料的目的位址,此參數為一個指向 user-space 記憶體的指標。
˙ from:資料的來源位址,此參數為一個指向 kernel-space 記憶體的指標。
˙ 口訣:copy data to user-space from kernel-space

copy_from_user() 來說:

˙ to:資料的目的位址,此參數為一個指向 kernel-space 記憶體的指標。
˙ from:資料的來源位址,此參數為一個指向 user-space 記憶體的指標。
˙ 口訣:copy data from user-space to kernel-space

由 user-space 讀取資料,或是寫入資料給 user-space 的 3 個 driver method 為:read、write與ioctl。

另外,指向 user-space 的指標是 kernel 回呼 driver method 時所傳遞進來的,可由 read、write 與 ioctl driver function 的函數原型宣告來觀察(紅色部份):

˙ int card_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
˙ ssize_t write(struct file *filp, const char *buff, size_t count, loff_t *offp);
˙ ssize_t read(struct file *filp, char *buff, size_t count, loff_t *offp);

fops->ioctl 的參數 arg、fops->write 與 fops->read 的參數 buff 是指向 user-space 資料的指標。撰寫程式時,要注意資料型別上的不同。

下一篇日記再寫一個範例來配合著研究,大家應該會更清楚。

Linux 驅動程式的 I/O, #4: fops->ioctl 實作

延續前一篇文章的介紹,在了解 copy_to_user() copy_from_user() 二個 API 後,接著 Jollen 將由 Linux device driver 的架構層來討論 user-space 與 kernel-space 的 I/O 機制。 同時,也延續在「架構層」系列教學專欄的 debug card 範例。

基本觀念

需要由 user-space 讀取資料,或是寫入資料給 user-space 的主要 3 個 driver method 為:read、write 與 ioctl。指向 user-space 資料空間(buffer)的指標是 kernel 回呼 driver method 時所傳遞進來的,我們由 read、write 與 ioctl 的函數原型宣告來說明如何撰寫程式:

˙int card_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
˙ssize_t write(struct file *filp, const char *buff, size_t count, loff_t *offp);
˙ssize_t read(struct file *filp, char *buff, size_t count, loff_t *offp);

fops->ioctl 的參數 argfops->writefops->read 的參數 buff 是指向 user-space 資料的指標。撰寫程式時,要注意資料型別上的不同。

實作 fops->ioctl

ioctl 代表 input/output control 的意思,故名思義,ioctl system call 是用來控制 I/O 讀寫用的,並且是支援user application存取裝置的重要 system call。因此,在 Linux 驅動程式設計上,我們會實作 ioctl system call 以提供 user application 讀寫(input/output)裝置的功能。

依此觀念,當 user application 需要將數字顯示到 debug card 時。範例 debug card 0.1.0 便需要實作 ioctl system call,然後在 fops->ioctl 裡呼叫 outb() 將 user application 所指定的數字輸出至 I/O port 80H。

User application 使用 GNU LIBC 的 ioctl() 函數呼叫 device driver 所提供的命令來「控制」裝置,因此驅動程式必須實作 fops->ioctl以提供「命令」給使用者。

fops->ioctl 函數原型如下:

int ioctl(struct inode *, struct file *, unsigned int, unsigned long);

Linux 驅動程式以一個唯一且不重覆的數字來代表 ioctl 的命令,設計 Linux 驅動程式時,我們必須使用 kernel 所提供的巨集來宣告命令。根據命令的「方向」,kernel 提供以下 4 個巨集供我們宣告 ioctl 命令:,

  • _IO(type,nr):表示此 ioctl 命令不指定資料向方
  • _IOR(type,nr,dataitem):此 ioctl 命令由裝置 (driver) 讀取資料
  • _IOW(type,nr,dataitem):此 ioctl 命令將資料寫入裝置
  • _IOWR(type,nr,dataitem):此 ioctl 命令同時讀寫資料

若 user application 呼叫到驅動程式未提供的命令,則回傳 -ENOTTY 錯誤代碼。

debug card 0.1.0 範例裡,我們宣告了一個 IOCTL_WRITE 命令,當 user application 呼叫此命令後,驅動程式會將 user application 所指定的數字顯示在 debug card 上。由於我們的資料方向為「寫入裝置」,因此使用的宣告巨集為 _IOW

Debug card 0.1.0 實作 fops->ioctl 的完整程式片斷如下:

#include &lt;linux/ioctl.h&gt;<br /><br />#define DEV_MAJOR 121<br />#define DEV_NAME &quot;debug&quot;<br />#define   DEV_IOCTLID 0xD0<br />#define IOCTL_WRITE _IOW(DEV_IOCTLID, 10, int)<br /><br />unsigned long IOPort = 0x80;<br /><br />void write_card(unsigned int);<br /><br />void write_card(unsigned int num)<br />{<br /> outb((unsigned char)num, IOPort);<br />}<br /><br />int card_ioctl(struct inode *inode, struct file *filp,<br />   unsigned int cmd, unsigned long arg)<br />{<br /> switch (cmd) {<br />  case IOCTL_WRITE:<br />   write_card((unsigned int)arg);<br />   break;<br />  default:<br />   return -1;<br /> }<br />     return 0;<br />}

struct file_operation 的定義並未列出,不過請別忘了在 fops 裡加上 ioctl system call 的欄位。

User-space

以 debug card 0.1.0 驅動程式為例,user-space 的測試程式寫法如下:

int main(int argc, char *argv[])<br />{<br />    int devfd;<br />    int num = 0;<br /><br />    if (argc &gt; 1) num = atoi(argv[1]);<br />    if (num &lt; 0) num = 0xff;<br /><br /> <strong>devfd = open(&quot;/dev/debug&quot;, O_RDONLY);</strong><br />    if (devfd == -1) {<br /> printf(&quot;Can't open /dev/debugn&quot;);<br /> return -1;<br />    }<br /><br />    printf(&quot;Write 0x%02x...n&quot;, num);<br /> <strong>ioctl(devfd, IOCTL_WRITE, num);</strong><br />    printf(&quot;Done. Wait 5 seconds...n&quot;);<br />    sleep(5);<br />    close(devfd);<br /><br />    return 0;<br />}

转载请注明:在路上 » Linux 驅動程式的 I/O

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. good
    匿名网友16年前 (2008-04-02)回复
85 queries in 0.142 seconds, using 22.21MB memory