字符设备驱动笔记(一)
1.調用關系
?? ??? ???? 應用程序:open ?? ??? ?read ?? ??? ?write?? ?
應用層?? ?-----------------------------------------
?? ??? ??? ? ??? ??? ??? ??? ?c庫?? ??? ?(swi val)
=================================================?? ?
內核:?? ?system call interface?? ?
?? ??? ???? (在異常處理函數里面,根據發生中斷的原因,調用
?? ??? ????? 不同的處理函數)
?? ??? ?-----------------------------------------
?? ??? ???? sys_open sys_read sys_write
?? ??? ???? (VFS Virtual File System)
?? ??? ?-----------------------------------------
?? ??? ???? 驅動: led_open ?? ??? ?led_read ?? ?led_write?? ?
?? ??? ?-----------------------------------------
?? ??? ??? 硬件: led ?? ?flash ....?? ?
?? ??? ?
? ????
2.驅動程序框架:
1)寫出led_on led_read
2)通知內核
? 1>構造file_operations
? ?? ??? ?.open
? ?? ??? ?.write
? ?? ??? ?...
? 2>入口函數中:register_chrdev
? ?? ??? ??? ????????? unregister_chrdev
? 3>first_drv_init(自定義的入口函數)
? ?? ?first_drv_exit
? 4>module_init修飾入口函數
? ?? ?module_exit
?
3.?? ?
用戶空間
?? ??? ?APP:?? ?open("/dev/xxx")?? ??? ?read?? ??? ?write
?? ? ??? ?????????? 屬性:c(設備類型),111(主設備號)
?? ? ??? ?-----------------------------------------------------
? ?? ??? ??? ??? ??? ??? ?C庫
==============================================================
內核?? ??? ??? ??? ??? ?system call interface
?? ??? ?------------------------------------------------------
?? ??? ? ??? ??? ? VFS?? ??? ? ?? ??? ? major作為索引
?? ??? ??? ??? ?————————————————————————
?? ??? ? | 數組?? ???? |?? ?|?? ?|...?? ?|file_operations|?? ??? |
?? ??? ??? ??? ?————————————————————————
?? ??? ?------------------------------------------------------
?? ??? ??? ??? ? led_open?? ??? ??? ??? ?led_read?? ?led_write
?? ??? ?------------------------------------------------------
?
VFS根據打開的文件的屬性:設備類型、主設備號,這兩個屬性就能找到
register_chrdev函數注冊進內核的file_operations結構體
4.Makefile
5.字符設備驅動程序:
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h>static struct class *firstdrv_class; static struct class_device *firstdrv_class_dev;volatile unsigned long *gpfcon = NULL; volatile unsigned long *gpfdat = NULL;static int first_drv_open(struct inode *inode, struct file *file) {//printk("first_drv_open\n");/* 配置GPF4,5,6為輸出 */*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));return 0; }static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) {int val;//printk("first_drv_write\n"); copy_from_user(&val, buf, count); // copy_to_user();if (val == 1){// 點燈*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));}else{// 滅燈*gpfdat |= (1<<4) | (1<<5) | (1<<6);}return 0; }static struct file_operations first_drv_fops = {.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */.open = first_drv_open, .write = first_drv_write, };//驅動的入口函數 int major; static int first_drv_init(void) {//0系統自動分配主設備號major = register_chrdev(0, "first_drv", &first_drv_fops); // 注冊, 告訴內核 firstdrv_class = class_create(THIS_MODULE, "firstdrv");firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
//0x56000050為GPFCON的物理地址gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);gpfdat = gpfcon + 1;return 0; }static void first_drv_exit(void) {unregister_chrdev(major, "first_drv"); // 卸載 class_device_unregister(firstdrv_class_dev);class_destroy(firstdrv_class);iounmap(gpfcon); }module_init(first_drv_init); module_exit(first_drv_exit);//不添加,則不能夠識別 MODULE_LICENSE("GPL");
6.測試程序:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>/* firstdrvtest on* firstdrvtest off*/ int main(int argc, char **argv) {int fd;int val = 1;fd = open("/dev/xyz", O_RDWR);if (fd < 0){printf("can't open!\n");}if (argc != 2){printf("Usage :\n");printf("%s <on|off>\n", argv[0]);return 0;}if (strcmp(argv[1], "on") == 0){val = 1;}else{val = 0;}write(fd, &val, 4);return 0; }?
7.運行
? 1>cat /proc/devices
? //加載驅動
? 2>insmod first_drv.ko
? //創建設備節點
? 3>mknod /dev/xxx? c? 111? 0
? 4>運行
?
? 5>lsmod
? 6>rmmod first_drv
? 7>rm /dev/xxx
? 8.? 自動分配主設備號,自動創建設備
?? ?1)驅動:①自動分配主設備號
?? ??????????? ②手工指定
?? ?2)應用:
?? ???????? open("/dev/xxx")
?? ???????? 1>mknod /dev/xxx c 主設備號???? 次設備號
?? ???????? 2>自動創建設備? udev? mdev(根據系統信息,創建設備節點)?
?? ? class_create()
?? ? class_device_create()
?? ?3)查看系統信息
?? ???? cd /sys/class
?? ???? cd firstdrv
?? ???? cd xyz
?? ???? ls
?? ?4) ?
?? ???? /etc/init.d/rc.S
?? ???? echo /sbin/mdev > /proc/sys/kernel/hotplus
總結
以上是生活随笔為你收集整理的字符设备驱动笔记(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隐藏bat执行界面
- 下一篇: 打开u盘黑屏了怎么办 u盘插入后屏幕黑屏