驱动代码:
#include <linux/init.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#ifndef MODULE
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif
#define MAJOR_NUM 253
MODULE_LICENSE("GPL");
int dev_test_open(struct inode *, struct file *);
ssize_t dev_test_read(struct file *, char *, size_t, loff_t*);
ssize_t dev_test_write(struct file *, const char *, size_t, loff_t*);
int dev_test_release(struct inode *, struct file *);
struct file_operations hello_fops =
{
open:dev_test_open,
read:dev_test_read,
write:dev_test_write,
release:dev_test_release,
};
char *pBuff;
static int __init dev_test_init(void)
{
printk("<1>hello devtest\r\n");
if(register_chrdev(MAJOR_NUM, "dev_test",&hello_fops))
printk("<1>registerfailed\r\n");
return0;
}
static void __exit dev_test_exit(void)
{
printk("<1>bye dev_test\r\n");
if(unregister_chrdev(MAJOR_NUM, "dev_test"))
printk("<1>unregiterfailed\r\n");
}
int dev_test_open(struct inode *pInode, struct file *pFile)
{
pBuff =(char*)kmalloc(128, GFP_KERNEL);
if (pBuff ==NULL)
printk("<1>kmalloc memoryfailed\r\n");
return0;
}
int dev_test_release(struct inode *pInode, struct file*pFile)
{
kfree(pBuff);
return0;
}
ssize_t dev_test_read(struct file *pFile, char *buff, size_t len,loff_t *pOff)
{
if(copy_to_user(buff, pBuff, len))
{
printk("<1>copy_from_userfailed\r\n");
return-EFAULT;
}
returnlen;
}
ssize_t dev_test_write(struct file *pFile, const char *buff, size_tlen, loff_t *pOff)
{
int i =0;
if(copy_from_user(pBuff, buff, len))
{
printk("<1>copy_to_userfailed\r\n");
return-EFAULT;
}
while(pBuff[i] != '\0')
{
if (pBuff[i]> 0x40 && pBuff[i]< 0x60)
pBuff[i] +=0x20;
else if(pBuff[i] > 0x60 &&pBuff[i] < 0x80)
pBuff[i] -=0x20;
i++;
}
returnlen;
}
module_init(dev_test_init);
module_exit(dev_test_exit);
=========================================================
把该代码保存为/usr/local/src/linux-2.x.x/driver/char/dev_test.c
(假设内核代码的目录为/usr/local/src/linux-2.x.x)
=========================================================
下面就是编译模块,按动态加载和静态加载进内核分为两种编译模式.
(1) 动态加载到内核模式,这个比较简单, 但是uClinux不支持这种方式, 所以以linux为例.
A. 用 gcc-D__KERNEL__ -DMODULE -DLINUX -I /usr/local/src/linux-2.x.x/include-c -o dev_test.o dev_test.c
B.在/usr/local/src/linux-2.x.x/driver/char/上用shell 输入 insmoddev_test.o
C.在shell上使用 mknod /dev/dev_test c 253 0 创建设备文件
到这里就搞定了, 用户就可以在用户层使用open read write 等函数对"/dev/dev_test"这个文件进行操作,内核就会调用相应的函数处理用户层的请求.
当用户不需要使用这个驱动模块时,可以用rmmod dev_test进行卸载.
--------------------------------------------------------------------------------------------------------
(2) 静态编入内核,通过 这种方法编译的,只要内核一启动,模块就会自动加载到内核
A.在/usr/local/src/linux-2.x.x/driver/char/下的Makefile文件中加入以下代码
ifeq ($(CONFIG_DEV_TEST),y)
obj-y += dev_test.o
endif
B.在/usr/local/src/linux-2.x.x/driver/char/下的Makefile文件中加入以下代码
bool 'support for dev_test' CONFIG_DEV_TEST y
如果没有该文件,就在/usr/local/src/linux-2.x.x/arch/xxxnommu/目录下的config.in文件中的字符驱动部分加入.
C.在shell上使用mknod /dev/dev_test c 253 0 创建设备文件
(如果是uClinux, 则使用mknod /xxx/romdist/dev/dev_test c 253 0,/xxx/romdist为嵌入式文件系统的目录)
D.编译固件和文件系统镜像后烧到目标版上就可以了.
===========================================================
下面是用户层的应用程序代码, 它使用open, read, write等函数来调用内核实现的驱动代码.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
char buff[128];
fd = open("/dev/dev_test", O_RDWR);
while(1)
{
printf("enter the string:");
scanf("%s", buff);
write(fd, buff, 128);
memset(buff, 0, 128);
read(fd, buff, 128);
printf("%s\r\n", buff);
}
return 0;
}