Linux字元裝置驅動程式的一個簡單示例
阿新 • • 發佈:2019-01-24
一.原始碼: // memdev.c #define MEMDEV_MAJOR 254 /*預設的mem的主裝置號*/ #define MEMDEV_NR_DEVS 2 /*裝置數*/ #define MEMDEV_SIZE 4096 /*mem裝置描述結構體*/ struct mem_dev { char *data; unsigned long size; }; #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/mm.h> #include <linux/sched.h> #include <linux/init.h> #include <linux/cdev.h> #include <asm/io.h> #include <asm/system.h> #include <asm/uaccess.h> #include "memdev.h" static mem_major = MEMDEV_MAJOR; module_param(mem_major, int, S_IRUGO); struct mem_dev *mem_devp; /*裝置結構體指標*/ struct cdev cdev; /*檔案開啟函式*/ int mem_open(struct inode *inode, struct file *filp) { struct mem_dev *dev; /*獲取次裝置號*/ int num = MINOR(inode->i_rdev); if (num >= MEMDEV_NR_DEVS) return -ENODEV; dev = &mem_devp[num]; /*將裝置描述結構指標賦值給檔案私有資料指標*/ filp->private_data = dev; //方便以後對該指標的使用 return 0; } /*檔案釋放函式*/ int mem_release(struct inode *inode, struct file *filp) { return 0; } /*讀函式*/ static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct mem_dev *dev = filp->private_data; /*獲得裝置結構體指標*/ /*判斷讀位置是否有效*/ if (p >= MEMDEV_SIZE) //超出讀取範圍,返回0表示讀取不到資料 return 0; if (count > MEMDEV_SIZE - p) count = MEMDEV_SIZE - p; /*讀資料到使用者空間*/ if (copy_to_user(buf, (void*)(dev->data + p), count)) { ret = - EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "read %d bytes(s) from %d\n", count, p); } return ret; } /*寫函式*/ static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct mem_dev *dev = filp->private_data; /*獲得裝置結構體指標*/ /*分析和獲取有效的寫長度*/ if (p >= MEMDEV_SIZE) return 0; if (count > MEMDEV_SIZE - p) count = MEMDEV_SIZE - p; /*從使用者空間寫入資料*/ if (copy_from_user(dev->data + p, buf, count)) ret = - EFAULT; else { *ppos += count; ret = count; printk(KERN_INFO "written %d bytes(s) from %d\n", count, p); } return ret; } /* seek檔案定位函式 */ static loff_t mem_llseek(struct file *filp, loff_t offset, int whence) { loff_t newpos; switch(whence) { case 0: /* SEEK_SET */ newpos = offset; break; case 1: /* SEEK_CUR */ newpos = filp->f_pos + offset; break; case 2: /* SEEK_END */ newpos = MEMDEV_SIZE -1 + offset; break; default: /* can't happen */ return -EINVAL; } if ((newpos<0) || (newpos>MEMDEV_SIZE)) return -EINVAL; filp->f_pos = newpos; return newpos; } /*檔案操作結構體*/ static const struct file_operations mem_fops = { .owner = THIS_MODULE, .llseek = mem_llseek, .read = mem_read, .write = mem_write, .open = mem_open, .release = mem_release, }; /*裝置驅動模組載入函式*/ static int memdev_init(void) { int result; int i; dev_t devno = MKDEV(mem_major, 0); /* 靜態申請裝置號*/ if (mem_major) result = register_chrdev_region(devno, 2, "memdev"); else /* 動態分配裝置號 */ { result = alloc_chrdev_region(&devno, 0, 2, "memdev"); mem_major = MAJOR(devno); } if (result < 0) return result; /*初始化cdev結構*/ cdev_init(&cdev, &mem_fops);//使cdev與mem_fops聯絡起來 cdev.owner = THIS_MODULE;//owner成員表示誰擁有這個驅動程式,使“核心引用模組計數”加1;THIS_MODULE表示現在這個模組被核心使用,這是核心定義的一個巨集 cdev.ops = &mem_fops; /* 註冊字元裝置 */ cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS); /* 為裝置描述結構分配記憶體*/ mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);//目前為止我們始終用GFP_KERNEL if (!mem_devp) /*申請失敗*/ { result = - ENOMEM; goto fail_malloc; } memset(mem_devp, 0, sizeof(struct mem_dev)); /*為裝置分配記憶體*/ for (i=0; i < MEMDEV_NR_DEVS; i++) { mem_devp[i].size = MEMDEV_SIZE; mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL);//分配出來的地址存在此 memset(mem_devp[i].data, 0, MEMDEV_SIZE); } return 0; fail_malloc: unregister_chrdev_region(devno, 1); return result; } /*模組解除安裝函式*/ static void memdev_exit(void) { cdev_del(&cdev); /*登出裝置*/ kfree(mem_devp); /*釋放裝置結構體記憶體*/ unregister_chrdev_region(MKDEV(mem_major, 0), 2); /*釋放裝置號*/ } MODULE_LICENSE("GPL"); module_init(memdev_init); module_exit(memdev_exit); 二.編譯原始碼 1.把這兩個驅動原始檔複製進核心linux-2.6.32.2/drivers/char目錄下 2.修改該目錄下的Kconfig檔案新增config MEMDEV_DRIVER tristate "memdev driver" 3.修改該目錄下的Makefile檔案,依葫蘆畫瓢,新增 obj-$(CONFIG_HELLO_DRIVER) += memdev.o 至此,檔案以新增進核心。 4.到Linux-2.6.32.2原始碼根目錄下執行 make menuconfig 在字元裝置中找到選單項“memdev driver“,就是我們剛才新增的驅動模組,選為M 5.在Linux-2.6.32.2原始碼根目錄下執行 make modules 就能生成核心模組檔案memdev.ko 至此,我們已經完成驅動模組的編譯。三.把驅動下載到開發版並安裝 1.把memdev.ko 下載到開發板,並移到/lib/modules/2.6.29.4-FriendlyARM目錄下,然後在開發板中執行 #modprobe memdev (注意用modprobe命令不需要.ko字尾,rmmod也是如此,這個經常會忘記) 當然你也可以用insmod命令:insmod memdev.ko 2.建立裝置檔案節的 #mknod /dev/memdev0 c 254 0四.測試測試程式碼如下:app_mem.c#include <stdio.h> int main() { FILE *fp0 = NULL; char Buf[4096]; /*初始化Buf*/ strcpy(Buf,"Mem is char dev!"); printf("BUF: %s\n",Buf); /*開啟裝置檔案*/ fp0 = fopen("/dev/memdev0","r+"); if (fp0 == NULL) { printf("Open Memdev0 Error!\n"); return -1; } /*寫入裝置*/ fwrite(Buf, sizeof(Buf), 1, fp0); /*重新定位檔案位置(思考沒有該指令,會有何後果)*/ fseek(fp0,0,SEEK_SET); /*清除Buf*/ strcpy(Buf,"Buf is NULL!"); printf("BUF: %s\n",Buf); /*讀出裝置*/ fread(Buf, sizeof(Buf), 1, fp0); /*檢測結果*/ printf("BUF: %s\n",Buf); return 0; }