好记性不如铅笔头

C && C++, kernel, 编程

驱动学习笔记:混杂设备

CONTENTS

混杂设备。

这里备份下代码:

Makefile:

obj-m := hellomisc.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
	make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
	rm *.o *.ko *.mod.c

.PHONY:clean

hellomisc.c:

#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>

MODULE_LICENSE("Dual BSD/GPL");


#define STR_MAX_SIZE 255
#define INIT_STR_VALUE "Hello Misc From Kernel\n"
static char string_var[STR_MAX_SIZE];
static int currReadNum = 0;
static int currWriteNum = 0;

int myopen(struct inode *inode, struct file *file)
{
	printk(KERN_ALERT "File Open\n");

	currReadNum = 0;
	currWriteNum = 0;
    return 0;
}
int myclose(struct inode *node, struct file *filp)
{
	printk(KERN_ALERT "File Close\n");
	return 0;
}

ssize_t myread(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	printk(KERN_ALERT "File Read:\n");
	
	if (currReadNum >= STR_MAX_SIZE)
	{
		return 0;
	}
	if (count > STR_MAX_SIZE - currReadNum)
	{
		count = STR_MAX_SIZE - currReadNum;
	}
	copy_to_user( buf, string_var + currReadNum, count);
	
	currReadNum += count; 
	return count;
}

ssize_t mywrite(struct file *file, const char __user *buf, size_t count, loff_t *l)
{
	printk(KERN_ALERT "File Write:\n");
	
	if (currWriteNum >= STR_MAX_SIZE)
	{
		return 0;
	}
	
	if (count > STR_MAX_SIZE - currWriteNum)
	{
		count = STR_MAX_SIZE - currWriteNum;
	}
	copy_from_user(string_var + currWriteNum, buf, count);

	return count;
}


struct file_operations myfops = {
    .owner = THIS_MODULE,
    .open = myopen,
	.release = myclose,
    .write = mywrite,
    .read = myread,
};
struct miscdevice my_misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = "hellomisc",
    .fops = &myfops,
};

static int hello_init(void)
{
	printk(KERN_ALERT "Hello, world\n");
	
	memset(string_var, STR_MAX_SIZE, 1);
	sprintf(string_var, "%s", INIT_STR_VALUE);
	
	misc_register(&my_misc);
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT "Goodbye, world\n");
    misc_deregister(&my_misc);
}

module_init(hello_init);
module_exit(hello_exit);

使用方式及日志:

root@cstriker1407-ubuntu:~/hellomisc# ls
hellomisc.c  Makefile
root@cstriker1407-ubuntu:~/hellomisc# make
make -C /lib/modules/3.11.0-15-generic/build SUBDIRS=/root/hellomisc modules
make[1]: 正在进入目录 `/usr/src/linux-headers-3.11.0-15-generic'
  CC [M]  /root/hellomisc/hellomisc.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/hellomisc/hellomisc.mod.o
  LD [M]  /root/hellomisc/hellomisc.ko
make[1]:正在离开目录 `/usr/src/linux-headers-3.11.0-15-generic'
root@cstriker1407-ubuntu:~/hellomisc# insmod hellomisc.ko
root@cstriker1407-ubuntu:~/hellomisc# cd /dev
root@cstriker1407-ubuntu:/dev# ls
.....  hellomisc   ......
root@cstriker1407-ubuntu:/dev# cat hellomisc 
Hello Misc From Kernel
root@cstriker1407-ubuntu:/dev# echo "THIS IS A STRING FROM CONSOLE " > hellomisc 
root@cstriker1407-ubuntu:/dev# cat hellomisc 
THIS IS A STRING FROM CONSOLE 
root@cstriker1407-ubuntu:/dev# rmmod hellomisc 

 

发表评论

13 − 6 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据