mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
char kbuf[128]="";
struct device_node *dnode;
unsigned int gpiono1,gpiono2,gpiono3;
int minor=0;
#if 0
unsigned int major=0;
#else
unsigned int major=500;
#endif
dev_t devno;
struct cdev *cdev;
struct class *cls;
struct device *dev;
const int count=3;
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
int mycdev_open(struct inode *inode,struct file *file){
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
int mycdev_close(struct inode *inode,struct file *file){
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof){
unsigned long ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which,ret;
ret=copy_from_user(&which,(void *)arg,sizeof(int));
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(cmd)
{
case LED_ON:
switch(which)
{
case 1:
gpio_set_value(gpiono1,1);
break;
case 2:
gpio_set_value(gpiono2,1);
break;
case 3:
gpio_set_value(gpiono3,1);
break;
}
break;
case LED_OFF:
switch(which)
{
case 1:
gpio_set_value(gpiono1,0);
break;
case 2:
gpio_set_value(gpiono2,0);
break;
case 3:
gpio_set_value(gpiono3,0);
break;
}
break;
}
return 0;
}
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
.unlocked_ioctl=mycdev_ioctl,
};
static int __init mycdev_init(void)
{
int ret=0,i;
cdev = cdev_alloc();
if(cdev == NULL){
ret = ENOMEM;
goto OUT1;
}
printk("字符设备对象实例化成功\n");
cdev_init(cdev,&fops);
if(major==0)
{
ret=alloc_chrdev_region(&devno,minor,3,"mycdev");
if(ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major=MAJOR(devno);
minor=MINOR(devno);
}
else
{
devno=MKDEV(major,0);
ret=register_chrdev_region(devno,3,"mycdev");
if(ret)
{
printk("静态设备号失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
ret=cdev_add(cdev,devno,3);
if(ret)
{
printk("注册字符设备驱动失败\n");
goto OUT3;
}
printk("注册字符设备驱动成功\n");
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
for(i=0;i<count;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mychrdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备信息失败\n");
ret=PTR_ERR(dev);
goto OUT5;
}
}
printk("自动创建设备节点成功\n");
dnode = of_find_node_by_name(NULL,"myleds");
if(dnode == NULL){
printk("设备树节点解析失败\n");
ret = ENOMEM;
goto OUT5;
}
gpiono1 = of_get_named_gpio(dnode,"led1",0);
if(gpiono1 < 0){
printk("of_get_named_gpio led1 failed\n");
ret = ENOENT;
goto OUT5;
}
gpiono2 = of_get_named_gpio(dnode,"led2",0);
if(gpiono2 < 0){
printk("of_get_named_gpio led2 failed\n");
ret = ENOENT;
goto OUT5;
}
gpiono3 = of_get_named_gpio(dnode,"led3",0);
if(gpiono3 < 0){
printk("of_get_named_gpio led3 failed\n");
ret = ENOENT;
goto OUT5;
}
ret = gpio_request(gpiono1,NULL);
if(ret != 0){
printk("gpio_request gpiono1 failed\n");
ret = EPERM;
goto OUT5;
}
ret = gpio_request(gpiono2,NULL);
if(ret != 0){
printk("gpio_request gpiono2 failed\n");
ret = EPERM;
goto OUT5;
}
ret = gpio_request(gpiono3,NULL);
if(ret != 0){
printk("gpio_request gpiono3 failed\n");
ret = EPERM;
goto OUT5;
}
printk("已申请到GPIO使用权\n");
gpio_direction_output(gpiono1,0);
gpio_direction_output(gpiono2,0);
gpio_direction_output(gpiono3,0);
return 0;
OUT5:
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(devno,3);
OUT2:
kfree(cdev);
OUT1:
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);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major,minor),count);
kfree(cdev);
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
main.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
int main() {
char buf[128]={0};
int fd=open("/dev/c",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
int a,b;
while(1)
{
printf("请输入要实现的功能:1(开灯) 0(关灯)>");
scanf("%d",&a);
if(a==1)
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d",&b);
ioctl(fd,LED_ON,&b);
}
else
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d",&b);
ioctl(fd,LED_OFF,&b);
}
}
close(fd);
return 0;
}