#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h> //file_operations
#include<linux/device.h> //class_create device_create
#include<linux/cdev.h> //Struct cdev
#include<linux/slab.h> //kmalloc
#include<asm/io.h> //ioremap
#include<asm/uaccess.h> //copy_from_user
#define LED_PA_ADDR 0xE03001C0
/*1.构建对象:抽象出一个led设备来*/
struct led_device{
unsigned int led_major;
struct cdev *led_cdev;
struct device *led_device;
struct class *led_class;
dev_t devno;
};
static struct led_device *fs_led_dev;
static volatile unsigned long *gpg3con =NULL;
static volatile unsigned long *gpg3dat =NULL;
static int led_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "led open!\n");
/*初始化硬件:将GPG3CON[0]配置为输出*/
*gpg3con &=~(0xffff<<0);
*gpg3con |= 0x1111;
return 0;
}
static int led_close(struct inode *inode, struct file *file)
{
printk(KERN_INFO "led close!\n");
/*初始化硬件:将GPG3CON[0]配置为输出*/
*gpg3con &=~(0xffff<<0);
return 0;
}
//write(fd,&buf,4)
//sys_write
//根据fd找到对应的file结构体
//执行file->f_op->write =led_write
static ssize_t led_write(struct file *file, const char __user *buf, size_t size, loff_t *opps)
{
int ret=0;
int val;
/*获取用户空间的数据
*如果拷贝成功,则返回0
*失败返回剩下的没有拷贝成功的字节数>0
*/
ret =copy_from_user(&val,buf,size);
*gpg3dat &= ~(0xffff);
*gpg3dat = (0x1<<val);
return ret?-EFAULT:size;
}
struct file_operations led_fops ={
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_close,
};
static int __init led_init(void)
{
int ret;
/*1.为本地的led设备分配空间
*param1:大小
*param2:标志GFP_KERNEL:如果分配不成功,则会导致休眠
*/
fs_led_dev=kmalloc(sizeof(struct led_device), GFP_KERNEL);
if(fs_led_dev==NULL){
printk(KERN_ERR "no memory for malloc!\n");
return -ENOMEM;
}
/*2.申请设备号*/
ret=alloc_chrdev_region(&fs_led_dev->devno,0,1,"led_module");
if(ret<0){
printk(KERN_ERR "register major failed!\n");
ret =-EINVAL;
goto output0;
}
/*3.创建字符设备struct cdev*/
fs_led_dev->led_cdev =cdev_alloc();
if(fs_led_dev->led_cdev==NULL){
printk(KERN_ERR "alloc memory failed\n");
ret =-ENOMEM;
goto output1;
}
cdev_init(fs_led_dev->led_cdev, &led_fops);
cdev_add(fs_led_dev->led_cdev,fs_led_dev->devno,1);
/*4.创建设备文件*/
fs_led_dev->led_class=class_create(THIS_MODULE, "led_class");
if(IS_ERR(fs_led_dev->led_class)){
printk(KERN_ERR "create class failed!\n");
ret =PTR_ERR(fs_led_dev->led_class);
goto output2;
}
fs_led_dev->led_device=device_create(fs_led_dev->led_class,NULL,fs_led_dev->devno,NULL,"led");
if(IS_ERR(fs_led_dev->led_device)){
printk(KERN_ERR "create device failed!\n");
ret =PTR_ERR(fs_led_dev->led_device);
goto output3;
}
/*5.将物理地址映射为虚拟地址*/
gpg3con =ioremap(LED_PA_ADDR,8);
gpg3dat =gpg3con+1;
return 0;
output3:
class_destroy(fs_led_dev->led_class);
output2:
cdev_del(fs_led_dev->led_cdev);
output1:
unregister_chrdev_region(fs_led_dev->devno,1);
output0:
kfree(fs_led_dev);
return ret;
}
static void __exit led_exit(void)
{
unregister_chrdev_region(fs_led_dev->devno,1);
cdev_del(fs_led_dev->led_cdev);
device_destroy(fs_led_dev->led_class,fs_led_dev->devno);
class_destroy(fs_led_dev->led_class);
iounmap(gpg3con);
kfree(fs_led_dev);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
led_test:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
/*./led_test on 点灯
*./led_test off 灭灯
*/
int main(int argc,char **argv)
{
int fd;
int i;
int buf=0;
/*1.打开设备/打开驱动/打开设备文件*/
fd =open("/dev/led",O_RDWR);
if(fd<0){
perror("open failed");
exit(1);
}
/*2.操作硬件*/
/* if(strcmp(argv[1],"on")==0){
buf =1;
}else{
buf =0;
}
*/
while(1)
{
for(i=0; i<4; i++)
{
if(write(fd, &i, 4) != 4)
{
perror("write failed!\n");
exit(1);
}
usleep(100);
}
}
/*
if(write(fd,&buf,4)!=4){
perror("write failed");
exit(1);
}*/
close(fd);
return 0;
}
Makefile:
ifeq ($(KERNELRELEASE),)
KERNELDIR ?=/home/farsight/Linux_source/linux-2.6.35.5
PWD :=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
arm-unknown-linux-gnueabi-gcc -o led_test led_test.c
cp led_drv.ko led_test /opt/rootfs/s5pc100_drv
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf hello_test *.o *~core *.ko *.mod.c .tmp_versions Module.symvers modules.order
else
obj-m :=led_drv.o
endif