我写了一个简单的字符设备驱动程序,并且想要为craneboard(ARM架构)进行交叉编译。 我的文件名是gDev.c 我将该文件复制到了鹤壁板上的kernel/drivers/char目录。 我修改了同一个目录中的Kconfig文件并添加了以下几行。
config TEST_GCHARD tristate "My Character driver" default m
我将下面一行添加到同一目录中的Makefile中。
obj-$(CONfig_TEST_GCHARD) += gDev.o
我在arch/arm/configs目录的am3517_crane_defconfig中添加了以下行。
CONfig_TEST_GCHARD=m
我的问题是,当我在am3517_crane_defconfig中将其设置为m时,该文件不包含在编译中。 但是,如果我将它更改为y ,则会进行编译。 但是,我需要它成为一个模块,我必须启动后, insmod 。 请指导我是否缺less任何步骤。 谢谢。
Windows驱动程序加载程序?
如何正确使用WDK 8.1 devfund_IOAttack_ERT_Basictestingfuzzing windows驱动程序
Windows XP Embedded中的CUDA
如何通过Windows上的内核驱动程序(例如Ndisfilter)发送原始的802.11数据包(例如mgmt,控制)?
DriverEntry调用者
是否有替代libusb-win32 64位窗口?
ARM Linux内核驱动程序中的关键时序
在Windows 10 Professional中安装自定义inf问题。 Windows使用OEM驱动程序覆盖它
找不到适合jdbc的驱动程序:postgresql://192.168.1.8:5432 / NexentaSearch
如何从IRP_MJ_CREATEcallback获得OpenResult?
这是因为你必须在一个单独的过程中建立模块:
make modules
另外,你可以安装它们:
make modules_install
如果这不是针对你自己的系统,而是针对另一个系统,比如嵌入式系统,你应该将它们“安装”在一个特定的目录中,然后你将使用INSTALL_MOD_PATH复制目标:
make INSTALL_MOD_PATH=/tmp/modules_for_target modules_install
这里是在megharajchard.c司机
#include<linux/module.h> #include<linux/kernel.h> #include<linux/fs.h> /*this is the file structure,file open read close */ #include<linux/cdev.h> /* this is for character device,makes cdev avilable*/ #include<linux/semaphore.h> /* this is for the semaphore*/ #include<linux/uaccess.h> /*this is for copy_user vice vers*/ int chardev_init(void); void chardev_exit(void); static int device_open(struct inode *,struct file *); static int device_close(struct inode *,struct file *); static ssize_t device_read(struct file *,char *,size_t,loff_t *); static ssize_t device_write(struct file *,const char *,loff_t *); static loff_t device_lseek(struct file *file,loff_t offset,int orig); /*new code*/ #define BUFFER_SIZE 1024 static char device_buffer[BUFFER_SIZE]; struct semaphore sem; struct cdev *mcdev; /*this is the name of my char driver that i will be registering*/ int major_number; /* will store the major number extracted by dev_t*/ int ret; /*used to return values*/ dev_t dev_num; /*will hold the major number that the kernel gives*/ #define DEVICENAME "megharajchard" /*inode reffers to the actual file on disk*/ static int device_open(struct inode *inode,struct file *filp) { if(down_interruptible(&sem) != 0) { printk(KERN_ALERT "megharajchard : the device has been opened by some other device,unable to open lockn"); return -1; } //buff_rptr = buff_wptr = device_buffer; printk(KERN_INFO "megharajchard : device opened succesfullyn"); return 0; } static ssize_t device_read(struct file *fp,char *buff,size_t length,loff_t *ppos) { int maxbytes; /*maximum bytes that can be read from ppos to BUFFER_SIZE*/ int bytes_to_read; /* gives the number of bytes to read*/ int bytes_read;/*number of bytes actually read*/ maxbytes = BUFFER_SIZE - *ppos; if(maxbytes > length) bytes_to_read = length; else bytes_to_read = maxbytes; if(bytes_to_read == 0) printk(KERN_INFO "megharajchard : Reached the end of the devicen"); bytes_read = bytes_to_read - copy_to_user(buff,device_buffer + *ppos,bytes_to_read); printk(KERN_INFO "megharajchard : device has been read %dn",bytes_read); *ppos += bytes_read; printk(KERN_INFO "megharajchard : device has been readn"); return bytes_read; } static ssize_t device_write(struct file *fp,const char *buff,loff_t *ppos) { int maxbytes; /*maximum bytes that can be read from ppos to BUFFER_SIZE*/ int bytes_to_write; /* gives the number of bytes to write*/ int bytes_writen;/*number of bytes actually writen*/ maxbytes = BUFFER_SIZE - *ppos; if(maxbytes > length) bytes_to_write = length; else bytes_to_write = maxbytes; bytes_writen = bytes_to_write - copy_from_user(device_buffer + *ppos,buff,bytes_to_write); printk(KERN_INFO "megharajchard : device has been written %dn",bytes_writen); *ppos += bytes_writen; printk(KERN_INFO "megharajchard : device has been writtenn"); return bytes_writen; } static loff_t device_lseek(struct file *file,int orig) { loff_t new_pos = 0; printk(KERN_INFO "megharajchard : lseek function in workn"); switch(orig) { case 0 : /*seek set*/ new_pos = offset; break; case 1 : /*seek cur*/ new_pos = file->f_pos + offset; break; case 2 : /*seek end*/ new_pos = BUFFER_SIZE - offset; break; } if(new_pos > BUFFER_SIZE) new_pos = BUFFER_SIZE; if(new_pos < 0) new_pos = 0; file->f_pos = new_pos; return new_pos; } static int device_close(struct inode *inode,struct file *filp) { up(&sem); printk(KERN_INFO "megharajchard : device has been closedn"); return ret; } struct file_operations fops = { /* these are the file operations provided by our driver */ .owner = THIS_MODULE,/*prevents unloading when operations are in use*/ .open = device_open,/*to open the device*/ .write = device_write,/*to write to the device*/ .read = device_read,/*to read the device*/ .release = device_close,/*to close the device*/ .llseek = device_lseek }; int chardev_init(void) { /* we will get the major number dynamically this is recommended please read ldd3*/ ret = alloc_chrdev_region(&dev_num,1,DEVICENAME); if(ret < 0) { printk(KERN_ALERT " megharajchard : Failed to allocate major numbern"); return ret; } else printk(KERN_INFO " megharajchard : mjor number allocated succesfuln"); major_number = MAJOR(dev_num); printk(KERN_INFO "megharajchard : major number of our device is %dn",major_number); printk(KERN_INFO "megharajchard : to use mknod /dev/%sc %d 0n",DEVICENAME,major_number); mcdev = cdev_alloc(); /*create,allocate and initialize our cdev structure*/ mcdev->ops = &fops; /*fops stand for our file operations*/ mcdev->owner = THIS_MODULE; /*we have created and initialized our cdev structure Now we need to add it to the kernel*/ ret = cdev_add(mcdev,dev_num,1); if(ret < 0) { printk(KERN_ALERT "megharajchard : device adding to the kerknel Failedn"); return ret; } else printk(KERN_INFO "megharajchard : device additin to the kernel succesfuln"); sema_init(&sem,1); /* initial value to one*/ return 0; } void chardev_exit(void) { cdev_del(mcdev); /*removing the structure that we added prevIoUsly*/ printk(KERN_INFO " megharajchard : removed the mcdev from kerneln"); unregister_chrdev_region(dev_num,1); printk(KERN_INFO "megharajchard : unregistered the device numbersn"); printk(KERN_ALERT " megharajchard : character driver is exitingn"); } MODULE_AUTHOR("AG MEGHaraJ(agmeghara[email protected])"); MODULE_DESCRIPTION("A BASIC CHAR DRIVER"); //MODULE_LICENCE("GPL"); module_init(chardev_init); module_exit(chardev_exit);
使文件相同。
obj-m := megharajchard.o KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) all: $(MAKE) -C $(KERNELDIR) M=$(PWD) clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
加载脚本
#!/bin/sh sudo insmod megharajchard.ko sudo mknod /dev/megharajchard c 251 0 sudo chmod 777 /dev/megharajchard
卸载脚本
#!/bin/sh sudo rmmod megharajchard sudo rm /dev/megharajchard
AC应用程序来测试驱动程序的功能。
#include<stdio.h> #include<fcntl.h> #include<string.h> #include<malloc.h> #define DEVICE "/dev/megharajchard" //#define DEVICE "megharaj.txt" int debug = 1,fd = 0; int write_device() { int write_length = 0; ssize_t ret; char *data = (char *)malloc(1024 * sizeof(char)); printf("please enter the data to write into devicen"); scanf(" %[^n]",data); /* a space added after"so that it reads white space,%[^n] is addeed so that it takes input until new line*/ write_length = strlen(data); if(debug)printf("the length of dat written = %dn",write_length); ret = write(fd,data,write_length); if(ret == -1) printf("writting Failedn"); else printf("writting successn"); if(debug)fflush(stdout);/*not to miss any log*/ free(data); return 0; } int read_device() { int read_length = 0; ssize_t ret; char *data = (char *)malloc(1024 * sizeof(char)); printf("enter the length of the buffer to readn"); scanf("%d",&read_length); if(debug)printf("the read length selected is %dn",read_length); memset(data,sizeof(data)); data[0] = '0'; ret = read(fd,read_length); printf("DEVICE_READ : %sn",data); if(ret == -1) printf("reading Failedn"); else printf("reading successn"); if(debug)fflush(stdout);/*not to miss any log*/ free(data); return 0; } int lseek_device() { int lseek_offset = 0,seek_value = 0; int counter = 0; /* to check if function called multiple times or looP*/ counter++; printf("counter value = %dn",counter); printf("enter the seek offsetn"); scanf("%d",&lseek_offset); if(debug) printf("seek_offset selected is %dn",lseek_offset); printf("1 for SEEK_SET,2 for SEEK_CUR and 3 for SEEK_ENDn"); scanf("%d",&seek_value); printf("seek value = %dn",seek_value); switch(seek_value) { case 1: lseek(fd,lseek_offset,SEEK_SET); return 0; break; case 2: lseek(fd,SEEK_CUR); return 0; break; case 3: lseek(fd,SEEK_END); return 0; break; default : printf("unkNown option selected,please enter right onen"); break; } /*if(seek_value == 1) { printf("seek value = %dn",seek_value); lseek(fd,SEEK_SET); return 0; } if(seek_value == 2) { lseek(fd,SEEK_CUR); return 0; } if(seek_value == 3) { lseek(fd,SEEK_END); return 0; }*/ if(debug)fflush(stdout);/*not to miss any log*/ return 0; } int lseek_write() { lseek_device(); write_device(); return 0; } int lseek_read() { lseek_device(); read_device(); return 0; } int main() { int value = 0; if(access(DEVICE,F_OK) == -1) { printf("module %s not loadedn",DEVICE); return 0; } else printf("module %s loaded,will be usedn",DEVICE); while(1) { printf("ttplease enter 1 to writen 2 to readn 3 to lseek and write 4 to lseek and readn"); scanf("%d",&value); switch(value) { case 1 :printf("write option selectedn"); fd = open(DEVICE,O_RDWR); write_device(); close(fd); /*closing the device*/ break; case 2 :printf("read option selectedn"); /* dont kNow why but i am suppoesed to open it for writing and close it,i cant keep open and read. its not working,need to sort out why is that so */ fd = open(DEVICE,O_RDWR); read_device(); close(fd); /*closing the device*/ break; case 3 :printf("lseek option selectedn"); fd = open(DEVICE,O_RDWR); lseek_write(); close(fd); /*closing the device*/ break; case 4 :printf("lseek option selectedn"); fd = open(DEVICE,O_RDWR); lseek_read(); close(fd); /*closing the device*/ break; default : printf("unkNown option selected,please enter right onen"); break; } } return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。