led_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h> //包含copy_from_user函数
#include <linux/device.h> //包含class类相关的处理函数
#include <linux/fs.h> //包含file_operations结构体
#include <asm/io.h> //包含iomap函数iounmap函数
#define DRIVER_NAME "led_drv"
int major;
static struct class *led_drv_class;
static struct class_device *led_drv_class_dev[4];
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev); //获取设备的次设备号
switch(minor)
{
case 0:
{
*gpfcon &= ~(0x3<<(2*4));
*gpfcon |= (0x1<<(2*4));
*gpfcon &= ~(0x3<<(2*5));
*gpfcon |= (0x1<<(2*5));
*gpfcon &= ~(0x3<<(2*6));
*gpfcon |= (0x1<<(2*6));
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); //输出低电平
break;
}
case 1:
{
*gpfcon &= ~(0x3<<(2*4));
*gpfcon |= (0x1<<(2*4));
*gpfdat &= ~(0x1<<4);
break;
}
case 2:
{
*gpfcon &= ~(0x3<<(2*5));
*gpfcon |= (0x1<<(2*5));
*gpfdat &= ~(0x1<<5);
break;
}
case 3:
{
*gpfcon &= ~(0x3<<(2*6));
*gpfcon |= (0x1<<(2*6));
*gpfdat &= ~(0x1<<6);
break;
}
}
return 0;
}
static int led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int minor = MINOR(file->f_dentry->d_inode->i_rdev); //获取设备的次设备号
char val, ret;
ret = copy_from_user(&val, buf, 1);
if(ret!=0)
{
printk("led_drv_write error 1 \n");
}
switch(minor)
{
case 0:
{
if(val==1)
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); //输出低电平, 灯亮
}
else
{
*gpfdat |= ((1<<4) | (1<<5) | (1<<6)); //输出高电平, 灯灭
}
break;
}
case 1:
{
if(val==1)
{
*gpfdat &= ~(1<<4);
}
else
{
*gpfdat |= (1<<4);
}
break;
}
case 2:
{
if(val==1)
{
*gpfdat &= ~(1<<5);
}
else
{
*gpfdat |= (1<<5);
}
break;
}
case 3:
{
if(val==1)
{
*gpfdat &= ~(1<<6);
}
else
{
*gpfdat |= (1<<6);
}
break;
}
}
return 0;
}
static struct file_operations led_drv_fopt = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int led_drv_init(void)
{
int i;
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
major = register_chrdev(0, DRIVER_NAME, &led_drv_fopt); //向内核中注册驱动
if(major<0)
{
printk(KERN_EMERG "can not obtain major number.\n");
return major;
}
//建立设备类别
led_drv_class = class_create(THIS_MODULE, DRIVER_NAME);
//分配次设备号,主设备号为major从设备号为0
led_drv_class_dev[0] = class_device_create(led_drv_class, NULL, MKDEV(major, 0), NULL, "leds");
//分配次设备号,主设备号为major从设备号为i
for(i=1; i<4; i++)
{
led_drv_class_dev[i] = class_device_create(led_drv_class, NULL, MKDEV(major, i), NULL, "led%d", i);
}
return 0;
}
static void led_drv_exit(void)
{
int i;
unregister_chrdev(major, DRIVER_NAME);
for(i=0; i<4; i++)
{
class_device_unregister(led_drv_class_dev[i]);
}
class_destroy(led_drv_class);
iounmap(gpfcon);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
Makefile文件:
obj-m += led_drv.o
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c
led_app文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int val;
char *filename;
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd<0)
{
printf("error, can't open %s\n", filename);
return 0;
}
if(strcmp("on", argv[2])==0)
{
val = 1;
write(fd, &val, 1);
}
else if(strcmp("off", argv[2])==0)
{
val = 0;
write(fd, &val, 1);
}
return 0;
}
编译生成led_drv.ko和led_app, 运行 ./led_app /dev/leds on
./led_app /dev/leds off
./led_app /dev/led1 on
./led_app /dev/led1 off
./led_app /dev/led2 on
./led_app /dev/led2 off
./led_app /dev/led3 on
./led_app /dev/led3 off
可以看到不同的效果