#include <linux/module.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
static char *str = NULL;
/***
static void *my_seq_start(struct seq_file *m, loff_t *pos)
{
if (0 == *pos)
{
++*pos;
return (void *)1;
}
return NULL;
}
static void *my_seq_next(struct seq_file *m, void *v, loff_t *pos)
{
return NULL;
}
static void my_seq_stop(struct seq_file *m, void *v)
{
}
static int my_seq_show(struct seq_file *m, void *v)
{
seq_printf(m, "current kernel time is %llu\n", (unsigned long long) get_jiffies_64());
seq_printf(m, "str is %s\n", str);
return 0;
}
static struct seq_operations my_seq_fops =
{
.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show,
};
static int proc_seq_open(struct inode *inode, struct file *file)
{
return seq_open(file, &my_seq_fops);
}
***/
static int read_proc(struct seq_file *s, void *v)
{
printk("open read use\n");
seq_printf(s,"input show=%s\n", str); /****此处加入打印函数可以在相应的proc文件下看到输出值,便shell中命令查看,而且只能放在seqopen函数下,其他读写函数会出各种问题***/
return 0 ;
}
static int proc_seq_open(struct inode *inode, struct file *file)
{
return(single_open(file, read_proc, NULL));/***seq——file结构可以自己写,但是麻烦,可以直接用它相应的库函数***/
}
static ssize_t proc_seq_write(struct file *file, const char __user *buffer, size_t count, loff_t *f_pos)
{
int i=0;
char *tmp = kzalloc((count+1), GFP_KERNEL);
if (!tmp)
return -ENOMEM;
if (copy_from_user(tmp, buffer, count)) {
kfree(tmp);
return -EFAULT;
}
for (i = 0; i < strlen(tmp)-1; i++) {
if ((tmp[i] - 48) == i) {
printk("input tmp[0] %c\n", tmp[i]);
}
else {
printk("input error\n");
}
}
kfree(str);
str = tmp;
return count;
}
static struct file_operations proc_seq_fops =
{
.owner = THIS_MODULE,
.open = proc_seq_open,
.read = seq_read,
.write = proc_seq_write,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init my_init(void)
{
struct proc_dir_entry *file;
file = proc_create_data("gpio", 0, NULL, &proc_seq_fops, NULL);
if (NULL == file)
{
printk("Count not create /proc/gpio file!\n");
return -ENOMEM;
}
return 0;
}
static void __exit my_exit(void)
{
remove_proc_entry("gpio", NULL);
kfree(str);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include "led.h"
static int _major;
struct class *_gpiodev_class = NULL;
static long _led_ioctl(struct file *file, unsigned int cmd, unsigned long in_or_out)
{
ulong ret;
copy_from_user(&ret, in_or_out, sizeof(ret));
if (file < 0) {
printk("Application passed ioctl file pointer failed\n");
}
else {
printk("entry kernel sucess\n");
printk("in_or_out=%d\n", ret);
}
switch (cmd) {
case GPIO_IOC_OUT:
printk("enty ken222\n");
break;
default:
printk("common is fallt\n");
break ;
}
return 0 ;
}
static struct file_operations _gpio_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = _led_ioctl,
};
static int __init _gpio_test_module_init(void)
{
printk("start insmod \n");
_major = register_chrdev(0, "led_gpio", &_gpio_fops);
if (_major < 0) {
return _major;
}
_gpiodev_class = class_create(THIS_MODULE, "led_gpio");
if (IS_ERR(_gpiodev_class)) {
printk("Error creating gpio class\n");
return -1;
}
device_create(_gpiodev_class, NULL, MKDEV(_major, 0), NULL, "led_gpio");
return 0;
}
static void __exit _gpio_test_module_exit(void)
{
if (_gpiodev_class != NULL) {
device_destroy(_gpiodev_class, MKDEV(_major, 0));
class_destroy(_gpiodev_class);
}
_gpiodev_class = NULL;
if (_major >= 0) {
unregister_chrdev(_major, "led_gpio");
}
}
MODULE_LICENSE("GPL");
module_init(_gpio_test_module_init);
module_exit(_gpio_test_module_exit);
#ifndef _LED_H_
#define _LED_H_
/*Number 2 failed to introduce kernel, do not know why ?*/
#define GPIO_IOC_MAGIC 'G'
#define GPIO "GPIO"
#define GPIO_IOC_OUT _IOWR(GPIO_IOC_MAGIC, 1,unsigned long)
#define MIDDLE_ON 10
#define MIDDLE_OFF 20
#define UPPER_ON 30
#define UPPER_OFF 40
/*Application be used*/
#define LED_MIDDLE_ON 0
#define LED_MIDDLE_OFF 1
#define LED_UPPER_ON 2
#define LED_UPPER_OFF 3
#define LED_ALL_OFF 4
#define LED_ALL_ON 5
#endif
MAKE := make
TARGET := gpio
TARGET1 := proc_test
obj-m := $(TARGET).o
obj-m := $(TARGET1).o
GPIO_PATH = /home/share/tenda/bcm4708/develop/cbb/x86test/my_test
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
GPIO_CFLAGS += -O2 -Wall -fno-strict-aliasing
GPIO_CFLAGS += -I$(GPIO_PATH)
override EXTRA_CFLAGS := $(GPIO_CFLAGS)
ll:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules
cp $(TARGET).ko ./my_test/led_gpio.ko
chmod 777 ./my_test/led_gpio.ko
clean:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) clean
rm -rf ./my_test/led_gpio.ko
字符驱动与proc文件创建
最新推荐文章于 2021-11-17 11:47:05 发布