(完整word版)簡單字符設備驅動程序的設計(word文檔良心出品)_第1頁
(完整word版)簡單字符設備驅動程序的設計(word文檔良心出品)_第2頁
(完整word版)簡單字符設備驅動程序的設計(word文檔良心出品)_第3頁
(完整word版)簡單字符設備驅動程序的設計(word文檔良心出品)_第4頁
(完整word版)簡單字符設備驅動程序的設計(word文檔良心出品)_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、7實驗五:簡單字符設備驅動程序的設計實驗學時: 4實驗類型:(設計)、實驗目的1. 理解設備驅動程序的處理過程;2. 掌握 Linux 設備驅動程序開發(fā)的基本過程和設計方法;3. 學會編寫簡單的字符設備驅動程序。、實驗條件Linux 操作系統(tǒng) gcc三、實驗原理及相關知識設備驅動程序是 I/O 進程與設備控制器之間的通信程序。 驅動程序的功能: 接收由設備獨立性軟件發(fā)來的命令和參數(shù),并將命令中的抽象要求轉換為 具體的要求。 檢查用戶 I/O 請求的合法性,了解 I/O 設備的狀態(tài),傳遞有關參數(shù),設置設 備的工作方式。 發(fā)出 I/O 命令。 及時響應由控制器或通道發(fā)來的中斷請求,并根據(jù)其中斷類型

2、調用相應的 中斷處理程序進行處理。 對于設置有通道的計算機系統(tǒng), 驅動程序還應能夠根據(jù)用戶的 I/O 請求,自 動地構建通道程序。將抽象要求轉換為具體要求 檢查 I/O 設備請求的合法性 讀出和檢查設備的狀態(tài) 傳送必要的參數(shù) 工作方式的設置 啟動 I/O 設備設備驅動程序的處理過程:Linux 系統(tǒng)中,設備驅動程序是操作系統(tǒng)內核的重要組成部分,它與硬件設備 之間建立了標準的抽象接口。通過這個接口,用戶可以像處理普通文件一樣,對硬 件設備進行打開(open)、關閉(close)讀寫(read/write)等操作。通常設備驅動程序接口是由結構 file_operations 結構體向系統(tǒng)說明的,

3、它定義在 include/linux/fs.h 中。 file_operations 的數(shù)據(jù)結構如下: struct file_operations struct module *owner;loff_t (*llseek) (struct file *, loff_t, int);ssize_t (*read) (struct file *, char_user *, size_t, loff_t *);ssize_t (*write) (struct file *, const char _user *, size_t, loff_t *);ssize_t (*aio_read) (str

4、uct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t);unsigned int (*poll) (struct file *, struct poll_table_struct *);int (*ioctl) (struct inode *, struct file

5、*, unsigned int, unsigned long); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *);int (*open) (struct inode *, struct file *);int (*flush) (struct file *, fl_ow

6、ner_t id);int (*release) (struct inode *, struct file *);int (*fsync) (struct file *, struct dentry *, int datasync);int (*aio_fsync) (struct kiocb *, int datasync);int (*fasync) (int, struct file *, int); open 入口點:open函數(shù)負責打開設備、準備I/O。任何時候對設備文件進行打開操作,都會 調用設備的 open 入口點。所以, open 函數(shù)必須對將要進行的 I/O 操作做好必要的

7、準備工作,如清除緩沖區(qū)等。如果設備是獨占的。則 open 函數(shù)必須將設備標記成 忙狀態(tài)。 close 入口點close函數(shù)負責關閉設備的操作,當最后一次使用設備完成后,調用close函數(shù), 關閉設備文件。獨占設備必須標記為可再次使用。close()函數(shù)作用是關閉打開的文件。read入口點read函數(shù)負責從設備上讀數(shù)據(jù)和命令,有緩沖區(qū)的I/O設備操作一般是從緩沖區(qū)里讀數(shù)據(jù)。 write 入口點write 函數(shù)負責往設備上寫數(shù)據(jù)。對于有緩沖區(qū)的 I/O 設備操作,一般是把數(shù)據(jù) 寫入緩沖區(qū)里。對字符設備文件進行寫操作將調用 write 函數(shù)。 ioctl 入口點ioctl 函數(shù)執(zhí)行讀、寫之外的操作,

8、主要實現(xiàn)對設備的控制。 四、實驗步驟(1)(2)(3)(4)(5)(6)file_operations 結構體設計 模塊初始化、模塊卸載函數(shù)實現(xiàn) 讀寫函數(shù)的實現(xiàn) 測試程序編寫 驅動程序編譯和加載 驅動程序測試實驗報告要求:驅動程序工作理說明 驅動程序中使用的數(shù)據(jù)結構及符號說明。 流程圖。源程序并附上注釋。程序運行結果和分析。(1)(2)(3)(4)(5)五、思考題及其它塊設備驅動程序的設計。附件1)驅動程序 string.c#include <linux/module.h>#include <linux/init.h>#include <linux/fs.h>

9、;#include <linux/types.h>#include <linux/kernel.h>#include <asm/uaccess.h> MODULE_LICENSE("GPL");#define MAJOR_NUM 254/主設備號static char str20="Hello,Wrold!" /"string" 設備的數(shù)組static ssize_t string_read(struct file *filp, char *buf,size_t len,loff_t *loff)

10、/將 str 中的內容從內核空間復制到用戶空間if (copy_to_user(buf, &str, sizeof(str)/讀函數(shù)return -1;return sizeof(str);static ssize_t string_write(struct file *filp,const char *buf,size_t len,loff_t *off) /寫函數(shù)/將用戶空間的數(shù)據(jù)復制到內核空間的str 數(shù)組中if(copy_from_user(&str,buf,sizeof(str)return -1;return sizeof(str);/ 初始化字符設備驅動的 fil

11、e_operations 結構體struct file_operations string_fops=read: string_read, / 將標準的讀取函數(shù)指向對應于設備的具體函數(shù) write: string_write, / 將標準的寫函數(shù)指向對應于設備的具體函數(shù) ;static int _init string_init(void)int ret;/注冊設備驅動ret = register_chrdev(MAJOR_NUM, "string", &string_fops); if (ret) printk("string register fail

12、ure"); else printk("string register success"); return ret;/注冊失敗/注冊成功static void _exit string_exit(void)int ret;/銷毀設備驅動ret = unregister_chrdev(MAJOR_NUM, "string"); if (ret) printk("string unregister failure");elseprintk("string unregister success"); modul

13、e_init(string_init); module_exit(string_exit);(2)測試程序test.c:#in clude <sys/t yp es.h>#in clude <sys/stat.h>#i nclude <stdio.h>#include <fcntl.h> / 包括文件操作函數(shù),如open()、close()、read()等main ()int fd;char str20;/打開 ”dev/string ”fd = open ("/dev/stri ng", O_RDWR, S_IRUSR |

14、S_IWUSR); /打開設備文件if (fd != -1 )/打開設備文件成功read(fd, &str, sizeof(str); /初次讀 str prin tf("The stri ng is :%sn", str);printfC'P lease input the stri ng to the senten ce:n"); scan f("%s", &str);/ 寫 str/再次讀strwrite(fd, &str, sizeof(str); read(fd, &str, sizeof(st

15、r);prin tf("The string is :%sn", str);關閉 ”dev/string ” close(fd);/打開設備文件失敗 else prin tf("Device open failuren");(3)調試說明編譯驅動程序string.c (如圖1):n|X丈件迥終耶(:DroullocaIhuHl root# cd osL ruu tlocaIhus t os# Ischa r,c" sr i ng c t esI,r t cs T.c'I TOOlloca I huti I os # gcc n I r

16、i ng.c -o s t r i .u SI r i ng. c : f)T 2fi : asriV u accesi Ji .b 用右'那個丈件或 H 采 sIring”c:14: wy rn ing: 'h l ruct file' dec Iared s I r i ng. c : 14 r wiirning: i I a sco p亡 i s only Thisp robab I > nu I l you 'A-an lsir i ng.c:2S: sIring.c;32: s I r i ng.c:34 r s I r i ng.c:34 r

17、s I r i ng * c:34 J s I r i ng.c:: s I ring + c; s I r i ng + c:35: SI ring + c;32:inside pa ranrler Ii sIde f in iIi un c r dec I a ruii un . wh i ch i sinside pa runr ter Ii sI variable h I ring_fopy ' has ini I i al i ler hul incorrpl el e type unknown field read' Hpecified in initialiser

18、 wiirnin 昔:ex cess e I enrn t s in s rue t wiirnin 莒:(nf y r i n i i zi 1 i za t iunor unknown field write' si卩ec i f i ed i n wiirning: excess elements in s I ruct warning: (near i n i i a 1 i za t iunorwy rn ing: h I met file' dec Iaredin iIi ai zer s t r i n_ops ') in i1i ai zer initi

19、alizer s t r i ng_ fops ')ruollocaIhuH t e/ -GroollocalhuH t us#Htora百亡 s i ze of sting_fops' isn't knownos# Kc-c -D_KERbh:L_ - nMJUL E-| Zu s r/h r c/I i nux-2.4.20-S/i nc 1 nd -c (itrin.c圖1編譯驅動程序至此設備驅動模塊加載完成;為了驗證驅動程序的正確性,下面進行測試。首先編譯測試文件test.c,使用命令 :gcc test.c -o test.o (如圖5)模塊加載(如圖2)os

20、 # insntid £ r j n窩.心 os#Vroc llot;a hos Iroc tlot;a hoi I圖2編譯驅動程序模塊加載就會在 /proc/devices中看到該設備:如果加載成功,使用命令:cat /proc/devices查看(如圖 3)Vi沖四族軸® 生石墮垮輅_D 轉釘© 刑期® pT mPTspx叭pTJ;ruQLii'kxLjJtiuat /ua-in)»135I郭137.i»I:知140141142143Ifi21802S4Iramli sk2fd3i dertgml12untiurru J(

21、4untiiim? J22idcl3Sunrm ITU J39untianmJock dev 3 CCS:rawu sbstring圖3查看設備可以看到多了“ 254 string” 一行,說明設備驅動模塊加載成功。接著創(chuàng)建設備結點,用戶進程就可以通過這個結點訪問到字符數(shù)組的虛擬設備了:創(chuàng)建設備結點可以使用如下命令:mknod /dev/string c 254 0 (如圖4)yV文卄rp 褊鼎CL?和巴 輝ffi 轉到世?ruo l:loca I has I os # nknud /dev/ s t r i ng c 2&A 0 roo l:loca I Ims L os # I圖4

22、創(chuàng)建設備結點13皇件逍詢巴rootlQcyhost os#rootlu£ihost Obi#root® xd h"t osl#繹辭 轉刮:fFilftcH)irknod /tlcv? s I r inf; c 254 0 gee tost.c -0 Iti t.0圖5編譯測試程序編譯完成后生成一個使用命令:./test.o財I迥黑軸旦 1S桿坐I roooca I hos roo【1 oca I hasroo tl oca I ho stos # os# od#test.o的文件,運行該文件: (如圖6)燼爲CD轉刮匹L;,祈助(11rrkntjd /dev /s

23、 r i ni c 254 fl yC PS . D -0 DS .0./t ca t . 0 k'The s r i ng i s ; He I It), Wo I d!PI ease input he kr i a he s cncncct圖6運行測試程序可以看到初始化時 str為"Hello,Wrold! ”所以讀取的字符串為 Hello,Wrold!。此時按照提示 輸入新數(shù)值:“ First,test! ”情況如下:(如圖7)roti誕kfciillTiObC 加!i終話(! 轉別r© 帝肋IK)nknod /dev/yring c 254 0I Ci I.

24、c =0 1 cs I .o./ I es I .o宜杵墮)精軻血 在削巴 roo lloc ii I has I os # roo Lloca I L Oi # roollocaIhusI os#The sir i 牝 is : JIc I Io, Wu J! 卩©占:ic inpuE Lhe string ic ibc geniencc: Fi rsI,IenHThe string i s :F i rs t.cs l EIrootlocaIhusl os# |圖7運行測試程序可以看到用戶輸入字符串到設備后,程序可以讀取到該字符串,說明驅動程序編寫成功。 當不需要該設備驅動程序時

25、可以卸載該驅動,使用命令為:rmmod string (如圖8)丈申巴 編耕® SrfilV) 終堿口 始那應 /肋也】 rec tloca ho!< t root# rd fisroe tl >ca ho!i t O!i # i nsrrt J ftrin 呂.flreel tloca 1 ho!i t os # ./lcst*oThe siring i s : He I I a , W-fl J d!J ease i npu ( (he Ktrin 呂 iCi I he Mnlcncc: qwedThe Siring is : qv/odraa tloca 1 hos t os # rnmiiJ siringrco iloca hos t o叮# I圖8卸載驅動此時驅動程序已被卸載,若再

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論