驱动.c文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include "text.h"
#define CNAME "myled"
int major;
char kbuf[128]={0};
struct class *cls;
struct device *dev;
int rets;
int ret;
image_t image;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
switch(cmd)
{
case UACCESS_BUF:
rets=copy_from_user(kbuf,(char*)arg,sizeof(char [128]));
if(rets)
{
printk("copy form user led on\n");
return -EIO;
}
printk("copy form user kbuf=%s\n",kbuf);
case UACCESS_IMAGE:
ret=copy_from_user(&image,(image_t*)arg,sizeof(image_t));
if(ret)
{
printk("copy form user led on\n");
return -EIO;
}
printk("width=%d high=%d\n",image.width,image.high);
image.width+=10;
image.high+=10;
ret=copy_to_user((image_t*)arg,&image,sizeof(image_t));
if(ret)
{
printk("copy form user led on\n");
return -EIO;
}
}
return 0;
}
int mycdev_release(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops={
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_release,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0,CNAME,&fops);
if(major<0)
{
printk("error\n");
return major;
}
cls=class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls))
{
printk("error\n");
return PTR_ERR(cls);
}
dev=device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
if(IS_ERR(dev))
{
printk("error\n");
return PTR_ERR(dev);
}
printk("major=%d\n",major);
return 0;
}
static void __exit mycdev_exit(void)
{
device_destroy(cls,major);
class_destroy(cls);
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
Makefile文件
arch?=arm
file?=demo
ifeq ($(arch),x86)
LIANXI=/lib/modules/$(shell uname -r)/build
endif
ifeq ($(arch),arm)
LIANXI=/home/ubuntu/linux-5.10.61
endif
PWD:=$(shell pwd)
all:
make -C $(LIANXI) M=$(PWD) modules
clean:
make -C $(LIANXI) M=$(PWD) clean
obj-m:=$(file).o
应用层.c文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "text.h"
char buf[128]={"i am huyue"};
int main(int argc, char const *argv[])
{
int fd=-1;
fd=open("/dev/myled",O_RDWR);
if(-1==fd)
{
perror("open is error");
exit(1);
}
image_t image={20,1034};
ioctl(fd,UACCESS_BUF,buf);
ioctl(fd,UACCESS_IMAGE,&image);
printf("%d %d\n",image.width,image.high);
close(fd);
return 0;
}
.h文件
#ifndef __LED_H__
#define __LED_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;
}led_t;
typedef enum
{
LED1,
LED2,
LED3
}led_te;
typedef struct {
int width;
int high;
}image_t;
#define PHY_GPIOE_MODER 0x50006000
#define PHY_GPIOF_MODER 0x50007000
#define PHY_RCC 0x50000A28
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
#define UACCESS_BUF _IOW('a',1,char [128])
#define UACCESS_IMAGE _IOWR('a',0,image_t)
#endif