




下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)現(xiàn)一個(gè)簡(jiǎn)單的linux字符設(shè)備驅(qū)動(dòng) 步驟1: 編寫(xiě)驅(qū)動(dòng)程序 view plaincopy to clipboardprint?1. #include <linux/module.h> 2. #include <linux/init.h> 3. #include <linux/kernel.h> 4. #include <linux/cdev.h> 5. #include <linux
2、/fs.h> 6. #include <linux/kdev_t.h> 7. #include <asm/uaccess.h> 8. #include <linux/device.h> 9. #define DEVICE_NAME "cdev_zhangwei" 10. int number_of_devices = 1; &
3、#160; 11. struct cdev mydev; 12. dev_t dev = 0; 13. char data128 = "/0" / the data of my device 14. struct class *myclass; 15. static int mydev_
4、open(struct inode *inode, struct file *file) 16. 17. pr_info("mydev driver open!/n"); 18. return 0; 19. 20. static int mydev_release(st
5、ruct inode *inode, struct file *file) 21. 22. pr_info("mydev driver released!/n"); 23. return 0; 24. 25. ssize_t mydev_write(struct fil
6、e *file, const char _user *buf, size_t count, loff_t *f_pos) 26. 27. ssize_t ret = 0; 28. pr_info("mydev_write!/n"); 29.
7、0; pr_info("writing %d bytes/n", count); 30. if (count > 127) 31. return -ENOMEM; 32. if (count <
8、160;0) 33. return -EINVAL; 34. if (copy_from_user(data, buf, count) 35. ret = -EFAULT; 36.
9、60; 37. else 38. data127 = ''/0'' 39. pr_info("kernel received: %s/n", d
10、ata); 40. ret = count; 41. 42. return ret; 43. 44. static ssize_t mydev_read(struct file* filp, char*
11、buf, size_t len,loff_t* off) 45. 46. if( copy_to_user(buf,data,len) ) 47. 48. return -EFAULT; 49. &
12、#160; 50. 51. return len; 52. 53. struct file_operations mydev_fops = 54. .owner = THIS_MODULE, 55.
13、 .open = mydev_open, 56. .read = mydev_read, 57. .write = mydev_write, 58. .release = mydev_release 59.
14、;60. ; 61. static int _init mydev_init(void) 62. 63. int result, error; 64. result = register_chrdev(0, DEVICE_NAME, &mydev_fops); 65.
15、 pr_info("udev_cdev: get major number: %d/n", result); 66. dev = MKDEV(result, 0); 67. myclass = class_create(THIS_MODULE, "m
16、ydev_class"); 68. device_create(myclass, NULL, dev, NULL, DEVICE_NAME); 69. return 0; 70. 71. static void _exit mydev_exit(void) 72. 73.
17、160; cdev_del(&mydev); 74. unregister_chrdev_region(dev, number_of_devices); 75. device_destroy(myclass, dev); 76. class_destroy(myclass); 77.
18、0; pr_info("Goodbye cdev!/n"); 78. 79. module_init(mydev_init); 80. module_exit(mydev_exit); 81. MODULE_LICENSE("GPL"); 82. MODULE_DESCRIPTION("Simple cdev udev driver test");
19、60; 步驟2: 編譯,形成ko文件,然后利用insmod命令插入內(nèi)核。 (最好利用makefile進(jìn)行編譯,網(wǎng)上有文章專門有介紹) 步驟3:創(chuàng)建設(shè)備節(jié)點(diǎn): mknod /dev/your_name c 主設(shè)備號(hào) 次設(shè)備號(hào) 次設(shè)備號(hào)這里填0, 主設(shè)備號(hào)可以利用 cat /proc/devices 查看 步驟4:編寫(xiě)用戶程序(測(cè)試咱們的驅(qū)動(dòng)是否可行), 如以下代
20、碼,這個(gè)嘛,簡(jiǎn)單的用gcc命令編譯就好了 view plaincopy to clipboardprint?1. #include <stdio.h> 2. #include <sys/types.h> 3. #include <unistd.h> 4. #include <stdlib.h> 5. #include <fcntl.h> 6. int main (void)
21、; 7. 8. int fd,len; 9. pid_t pid; 10. char buff = "This is from userspace zhangwei fight it!" 11.
22、60; char buff_read100 12. fd = open ("/dev/cdev_zhangwei", O_RDWR); 13. if (fd < 0) 14. perror(
23、"open failed"); 15. exit(0); 16. 17. pid = fork(); 18. if(pid>0) 19. 20. len = write (fd, buff, sizeof(buff); 21. p
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 解除委托代理協(xié)議書(shū)
- 預(yù)存話費(fèi)合同協(xié)議書(shū)
- 退役定向就業(yè)協(xié)議書(shū)
- 信用卡書(shū)面和解協(xié)議書(shū)
- 酒樓廢品回收協(xié)議書(shū)
- 菏澤學(xué)院戰(zhàn)略協(xié)議書(shū)
- 餐廳聯(lián)營(yíng)經(jīng)營(yíng)協(xié)議書(shū)
- 非全日制競(jìng)業(yè)協(xié)議書(shū)
- 集體公寓轉(zhuǎn)讓協(xié)議書(shū)
- 鄰居之間接電協(xié)議書(shū)
- JGJ106-2014 建筑基樁檢測(cè)技術(shù)規(guī)范
- 酒店各部門衛(wèi)生區(qū)域劃分
- 工程測(cè)量控制點(diǎn)交樁記錄表
- GA 1810-2022城鎮(zhèn)燃?xì)庀到y(tǒng)反恐怖防范要求
- 重慶地區(qū)現(xiàn)代方言中的古語(yǔ)詞
- 3第三章申論寫(xiě)作 寫(xiě)作課件
- 廣西建設(shè)工程質(zhì)量檢測(cè)和建筑材料試驗(yàn)收費(fèi)項(xiàng)目及標(biāo)準(zhǔn)指導(dǎo)性意見(jiàn)(新)2023.10.11
- 國(guó)開(kāi)電大 可編程控制器應(yīng)用實(shí)訓(xùn) 形考任務(wù)5實(shí)訓(xùn)報(bào)告
- PEP英語(yǔ)四年級(jí)下冊(cè)U5 My clothes Read and write(教學(xué)課件)
- DB37-T 2671-2019 教育機(jī)構(gòu)能源消耗定額標(biāo)準(zhǔn)-(高清版)
- 信息系統(tǒng)項(xiàng)目管理師論文8篇
評(píng)論
0/150
提交評(píng)論