字元型驅動編寫技術文檔,字元編寫技術文檔

來源:互聯網
上載者:User

字元型驅動編寫技術文檔,字元編寫技術文檔
一、概述

    一個字元型驅動的demo程式,主要功能:申請一塊記憶體,把它當作裝置,進行讀寫操作。它涵蓋了編寫驅動的主要函數和流程,適合入門驅動人員參考。該程式已經驗證通過。

二、源碼

有主要3個程式:

Makefile用來編譯,my_driver.c驅動程式,test.c是測試程式。

.

├── Makefile

├── my_driver.c

└── test

    └── test.c

 

三、測試

依據上述檔案格式排列,將Makefile和my_driver存放到同一個檔案夾,

輸入命令:

make

這樣就已經把驅動載入到了Linux核心中,通過/proc/device查看裝置:

輸入命令:

cat /proc/device


載入裝置檔案結點,輸入命令


進入帶有test.c檔案的檔案夾,利用gcc編譯這個檔案:

gcc test.c


四、程式

my_driver.c#include <linux/module.h>#include <linux/syscalls.h>#include <linux/kernel.h>#include <linux/slab.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/cdev.h>#define GLOBAL_MAJOR 233  //major device number#define MAX_SIZE 10static int my_major = GLOBAL_MAJOR;struct my_dev{struct cdev cdev;unsigned char mem[MAX_SIZE]; // my memory device};struct my_dev *my_devp;int my_open(struct inode *inode,struct file *filp){filp->private_data = my_devp;printk(KERN_INFO"my driver open\n");return 0;}int my_release(struct inode *inode,struct file *filp){printk(KERN_INFO"my driver close\n");return 0;}static ssize_t my_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 my_dev *dev = filp->private_data;if(p >= MAX_SIZE)return count?-ENXIO:0;if(count > MAX_SIZE-p)count = MAX_SIZE-p;if(copy_to_user(buf,(void*)dev->mem+p,count)){ret = -EFAULT;}else{*ppos+=count;ret=count;}return ret;}static ssize_t my_write(struct file *filp,const char __user *buf,size_t size,loff_t *ppos){unsigned long p = *ppos;unsigned int count = size;int ret;struct my_dev *dev = filp->private_data;if(p >= MAX_SIZE)return count?-ENXIO:0;if(count > MAX_SIZE-p)count = MAX_SIZE-p;memset(dev->mem,0,MAX_SIZE);if(copy_from_user(dev->mem+p,buf,count)){ret=-EFAULT;}return ret;}static loff_t my_llseek(struct file *filp,loff_t offset,int orig){loff_t ret=0;switch(orig){case 0:if(offset < 0){ret = -EINVAL;break;}if((unsigned int)offset > MAX_SIZE){ret = -EINVAL;break;}filp->f_pos = (unsigned int)offset;ret = filp->f_pos;break;case 1:if((filp->f_pos+offset) < 0){ret = -EINVAL;break;}if((filp->f_pos+offset) > MAX_SIZE){ret = -EINVAL;break;}filp->f_pos += offset;ret = filp->f_pos;break;default:ret = -EINVAL;break;}return ret;}static void hello(void){memcpy(&my_devp->mem,"hello",5);}static void clear(void){memset(&my_devp->mem,0,MAX_SIZE);}static int my_ioctl(struct file *filp,unsigned int cmd,unsigned long arg){struct my_dev *dev = filp->private_data;memset(dev->mem,0,MAX_SIZE);switch(cmd){case 0:hello();break;case 1:clear();break;default:break;}return 0;}static const struct file_operations my_fops={.owner = THIS_MODULE,.llseek = my_llseek,.read = my_read,.write = my_write,.unlocked_ioctl = my_ioctl,.open = my_open,.release = my_release,};static void my_setup_cdev(struct my_dev *dev,int index){int err;dev_t devno = MKDEV(my_major,index);//設定裝置號cdev_init(&dev->cdev,&my_fops); //初始化dev->cdev.owner=THIS_MODULE; dev->cdev.ops=&my_fops;err=cdev_add(&dev->cdev,devno,1);if(err)printk(KERN_NOTICE "Error %d adding %d\n",err,index);}static int __init test_init(void){int ret;dev_t devno=MKDEV(my_major,0);printk(KERN_INFO "module my_test init\n");if(my_major)ret=register_chrdev_region(devno,1,"mytest");else{ret=alloc_chrdev_region(&devno,0,1,"mytest");my_major=MAJOR(devno);}if(ret<0)return ret;my_devp=kmalloc(sizeof(struct my_dev),GFP_KERNEL);if(!my_devp){ret=-ENOMEM;goto fail_malloc;}memset(my_devp,0,sizeof(struct my_dev));my_setup_cdev(my_devp,0);return 0;fail_malloc:unregister_chrdev_region(devno,1);return ret;}static void __exit test_exit(void){cdev_del(&my_devp->cdev);kfree(my_devp);unregister_chrdev_region(MKDEV(my_major,0),1);printk(KERN_INFO "module my_test exit\n");}module_param(my_major,int,S_IRUGO);module_init(test_init);module_exit(test_exit);MODULE_LICENSE("GPL");Makefile:KONAME = my_driver.oIFTEST=$(shell lsmod | grep my_driver | wc -l)KERNELDIR := /lib/modules/$(shell uname -r)/buildPWD :=$(shell pwd)obj-m:=$(KONAME) module:make -C $(KERNELDIR) M=$(PWD) test_install :ifeq ($(IFTEST),1)rmmod my_driverinsmod my_driver.koelseinsmod my_driver.koendifinstall: test_install.PHONY:install clean:rm *.o *.mod.c  modules.order Module.symvers .*.cmd *.korm -rf .tmp_versionstest.c#include <stdio.h>#include <string.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <fcntl.h>#define HELLO 0#define CLEAR 1void show(char *buf){int i;for(i = 0;i < 6;i++){printf("%c",buf[i]);}printf("\n");}int main(){int dev_fd;char *buf = (char*)malloc(10);memset(buf,0,10);        dev_fd=open("/dev/my_test",O_RDWR|O_NONBLOCK);        if(dev_fd==-1){                  perror("cann't open file /dev/aes_test:");                 exit(-1);        }        write(dev_fd,"hahaha",6);lseek(dev_fd,0,SEEK_SET);read(dev_fd,buf,6);printf("the read buf is: \n");show(buf);lseek(dev_fd,0,SEEK_SET);        ioctl(dev_fd,HELLO,0);read(dev_fd,buf,6);printf("after HELLO the read buf is: \n");show(buf);        ioctl(dev_fd,CLEAR,0);lseek(dev_fd,0,SEEK_SET);read(dev_fd,buf,6);printf("after CLEAR the read buf is: \n");show(buf);return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.