1、按照字符设备驱动分步注册的方式实现控制3个灯
//mycdev.c
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/uaccess.h>
#include<linux/slab.h>
#include<linux/io.h>
#include<linux/device.h>
#include"myled.h"
//定义字符设备驱动结构体对象指针
struct cdev* cdev;
char kbuf[128]={0};
dev_t devno;//设备号
unsigned int minor=0;//次设备号
int count=3;//设备数量
struct class* cls;//指向设备目录的指针
struct device* dev;//指向设备节点的指针
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int*vir_rcc;
#if 1
unsigned int major=0;//进行动态申请设备号
#else
unsigned int major=500;//进行静态指定设备号
#endif
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char __user *ubuf,size_t size,loff_t *off)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//先判断size的大小是否大于内核kbuf的大小,如果比kbuf大,把size的数据修改为kbuf的大小
if(size>sizeof(kbuf))
size=sizeof(kbuf);
//将kbuf的数据拷贝给用户空间
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file,const char __user *ubuf,size_t size,loff_t *off)
{
int ret;// 定义变量接收函数返回值
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//先判断size的大小是否大于内核kbuf的大小,如果比kbuf大,把size的数据修改为kbuf的大小
if(size>sizeof(kbuf))
size=sizeof(kbuf);
//从空间拷贝数据
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user failed\n");
return -EIO;
}
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int whitch,ret;
switch(cmd)
{
case LED_ON:
//判断哪个灯开
//通过copy_from_user来接收第三个参数
ret=copy_from_user(&whitch,(void *)arg,sizeof(int));
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(whitch)
{
case 1:
vir_led1->ODR |= (1<<10);
break;
case 2:
vir_led2->ODR |= (1<<10);
break;
case 0:
vir_led3->ODR |= (1<<8);
break;
}
break;
case LED_OFF:
ret=copy_from_user(&whitch,(void *)arg,sizeof(int));
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(whitch)
{
case 1:
vir_led1->ODR &= (~(1<<10));
break;
case 2:
vir_led2->ODR &= (~(1<<10));
break;
case 0:
vir_led3->ODR &= (~(1<<8));
break;
}
break;
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//定义操作方法结构体
struct file_operations fops =
{
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.unlocked_ioctl=mycdev_ioctl,
.release=mycdev_close,
};
//进行寄存器地址映射和初始化函数
int all_led_init(void)
{
vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
if(vir_led1==NULL)
{
printk("led1地址映射失败\n");
return -ENOMEM;
}
vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
if(vir_led2==NULL)
{
printk("led2地址映射失败\n");
return -ENOMEM;
}
vir_led3=ioremap(PHY_LED3_ADDR,sizeof(gpio_t));
if(vir_led3==NULL)
{
printk("led3地址映射失败\n");
return -ENOMEM;
}
vir_rcc=ioremap(PHY_RCC_ADDR,4);
if(vir_rcc==NULL)
{
printk("rcc地址映射失败\n");
return -ENOMEM;
}
printk("寄存器地址映射成功\n");
//寄存器初始化
//led1
vir_led1->MODER &= (~(3<<20));
vir_led1->MODER |= (1<<20);//led1输出
vir_led1->ODR &= (~(1<<10));//led1输出低电平
//led2
vir_led2->MODER &= (~(3<<20));
vir_led2->MODER |= (1<<20);
vir_led2->ODR &= (~(1<<10));
//led3
vir_led3->MODER &= (~(3<<16));
vir_led3->MODER |= (1<<16);
vir_led3->ODR &= (~(1<<8));
//rcc
(*vir_rcc) |= (3<<4);
printk("寄存器配置成功\n");
return 0;
}
static int __init mycdev_init(void)
{
int ret,i;
//给字符设备驱动对象申请空间
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("给字符设备驱动对象申请空间失败\n");
ret=-ENOMEM;
goto ERR1;
}
printk("给字符设备驱动对象申请空间成功\n");
//对象的初始化
cdev_init(cdev,&fops);
//申请设备号
if(major==0)//动态申请
{
ret=alloc_chrdev_region(&devno,minor,count,"mycdev");
if(ret)
{
printk("动态指定设备号失败\n");
goto ERR2;
}
//获取主设备号的次设备号
major=MAJOR(devno);//获取主设备号
minor=MINOR(devno);//获取次设备号
}
else if(major>0)
{
ret=register_chrdev_region(MKDEV(major,minor),count,"mycdev");
if(ret)
{
printk("静态指定设备号失败\n");
goto ERR2;
}
}
printk("设备号申请成功\n");
//将字符设备驱动注册进内核
ret=cdev_add(cdev,MKDEV(major,minor),count);
if(ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
all_led_init();
//自动创建设备节点
//向上提交目录
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=PTR_ERR(cls);
goto ERR4;
}
printk("向上提交目录成功\n");
//向上提交设备节点
for(i=0;i<count;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret=PTR_ERR(dev);
goto ERR5;
}
}
printk("向上提交节点信息成功\n");
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
//销毁设备节点
int i;
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录
class_destroy(cls);
//取消物理地址的映射
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_led3);
iounmap(vir_rcc);
//删除驱动对象
cdev_del(cdev);
//销毁设备号
unregister_chrdev_region(MKDEV(major,minor),count);
//释放动态申请的字符设备驱动的对象空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
//myled.h
#ifndef __MYLED_H__
#define __MYLED_H__
typedef struct
{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
}gpio_t;
#define PHY_LED1_ADDR 0x50006000
#define PHY_LED2_ADDR 0x50007000
#define PHY_LED3_ADDR 0x50006000
#define PHY_RCC_ADDR 0x50000A28
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
//test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "myled.h"
int main(int argc, const char *argv[])
{
while(1)
{
int whitch=1;
char buf[128]={0};
//打开设备节点
int fd0=open("/dev/mycdev0",O_RDWR);
if(fd0<0)
{
printf("设备节点0打开失败\n");
exit(-1);
}
printf("设备节点0打开成功\n");
//在终端输入数据
whitch=whitch%3;
ioctl(fd0,LED_ON,&whitch);
sleep(1);
ioctl(fd0,LED_OFF,&whitch);
sleep(1);
whitch++;
int fd1=open("/dev/mycdev1",O_RDWR);
if(fd1<0)
{
printf("设备节点1打开失败\n");
exit(-1);
}
printf("设备节点1打开成功\n");
//在终端输入数据
whitch=whitch%3;
ioctl(fd1,LED_ON,&whitch);
sleep(1);
ioctl(fd1,LED_OFF,&whitch);
sleep(1);
whitch++;
int fd2=open("/dev/mycdev2",O_RDWR);
if(fd2<0)
{
printf("设备节点2打开失败\n");
exit(-1);
}
printf("设备节点2打开成功\n");
//在终端输入数据
whitch=whitch%3;
ioctl(fd2,LED_ON,&whitch);
sleep(1);
ioctl(fd2,LED_OFF,&whitch);
sleep(1);
close(fd0);
close(fd1);
close(fd2);
}
return 0;
}