배우고픈 공돌이
1. 부번호 처리 본문
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/fdtable.h>
#define SK_MAJOR 240
int result;
/*
minor1
*/
static int minor1_open(struct inode *inode, struct file *filp)
{
printk("minor1 open success!\n");
return 0;
}
static int minor1_release(struct inode *inode, struct file *filp)
{
printk("minor1 release success!\n");
return 0;
}
static ssize_t minor1_read(struct file *filp, char * buf,size_t count,loff_t * f_pos)
{
int pid = 0;
unsigned long start_code[2] = {0};
if(count == 4)
{
pid = current->pid;
copy_to_user(buf, &pid, count);
}
else
{
start_code[0] = current->mm->start_code;
start_code[1] = current->mm->end_code;
copy_to_user(buf, start_code, count);
}
return count;
}
/*
minor2
*/
static int minor2_open(struct inode *inode, struct file *filp)
{
printk("minor2 open success!\n");
return 0;
}
static int minor2_release(struct inode *inode, struct file *filp)
{
printk("minor2 release success!\n");
return 0;
}
static ssize_t minor2_write(struct file *filp, const char * buf, size_t count, loff_t * f_pos)
{
char data[30] = {0};
copy_from_user(data, buf, count);
printk("write >>> %s\n", data);
return count;
}
/*
minor3
*/
static int minor3_open(struct inode *inode, struct file *filp)
{
printk("minor3 open success!\n");
return 0;
}
static int minor3_release(struct inode *inode, struct file *filp)
{
printk("minor3 release success!\n");
return 0;
}
static long minor3_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int pid, next_fd, f_flags;
unsigned long start_address[2] = {0};
unsigned long i_ino;
switch(cmd)
{
case 'a':
pid = current->pid;
copy_to_user((int *)arg, &pid,4);
break;
case 'b':
start_address[0] = current->mm->start_code;
start_address[1] = current->mm->end_code;
copy_to_user((long *)arg, start_address, 16);
break;
case 'c':
next_fd = current->files->next_fd;
copy_to_user((long *)arg,&next_fd,sizeof(next_fd));
printk("netx fd is %d\n", next_fd);
break;
case 'd':
f_flags = filp->f_flags;
copy_to_user((int *)arg,&f_flags,sizeof(f_flags));
printk("f_flags is %d\n", f_flags);
break;
case 'e':
i_ino = filp->f_path.dentry->d_inode->i_ino;
copy_to_user((unsigned long *)arg, &i_ino,sizeof(i_ino));
printk("i_ino is %ld\n", i_ino);
break;
}
return 0;
}
struct file_operations minor1_fops =
{
.owner = THIS_MODULE,
.open = minor1_open,
.release = minor1_release,
.read = minor1_read,
};
struct file_operations minor2_fops =
{
.owner = THIS_MODULE,
.open = minor2_open,
.release = minor2_release,
.write = minor2_write,
};
struct file_operations minor3_fops =
{
.owner = THIS_MODULE,
.open = minor3_open,
.release = minor3_release,
.unlocked_ioctl = minor3_ioctl,
};
static int master_open(struct inode *inode, struct file *filp)
{
printk("master open success\n");
switch(MINOR(inode->i_rdev))
{
case 1:
filp->f_op = &minor1_fops;
break;
case 2:
filp->f_op = &minor2_fops;
break;
case 3:
filp->f_op = &minor3_fops;
break;
default:
return -ENXIO;
}
if(filp->f_op && filp->f_op->open)
return filp->f_op->open(inode,filp);
return 0;
}
struct file_operations master_fops =
{
.owner = THIS_MODULE,
.open = master_open,
};
static int __init skeleton_init(void)
{
int ret;
printk("skeleton init success\n");
ret = register_chrdev(SK_MAJOR,"skeleton",&master_fops);
if(ret <0)
return ret;
return 0;
}
static void __exit skeleton_exit(void)
{
printk("skeleton exit success\n");
unregister_chrdev(SK_MAJOR,"skeleton");
}
module_init(skeleton_init);
module_exit(skeleton_exit);
MODULE_LICENSE("GPL");
커널이 장치를 분류하는 기준은 major이다.
그렇다면 minor(부번호)처리는 드라이버에서 컨트롤해야한다는 뜻이된다.
위의 드라이버 소스를 분석하자면,
1. 커널에서 syscall_open 때, master_fops의 open을 부른다.
2. master_fops의 open에서 inode -> i_rdev의 minor번호에 따른 minor#_fops를 커널 filp->f_op에 등록한다.
(즉, minor번호에 따라 그 번호에 맞는 f_ops로 스위칭한다.)
minor1을 읽기 전용, 2를 쓰기 전용, 3은 ioctl로 읽기/쓰기가 가능하도록 코딩하였으며,
커널을 등록할 때,
insmod skeleton.ko
mknod /dev/sk1 c 240 1
mknod /dev/sk2 c 240 2
mknod /dev/sk3 c 240 3
'디바이스 드라이버' 카테고리의 다른 글
3. 인터럽트 (0) | 2017.11.15 |
---|---|
2. 타이머 (0) | 2017.11.15 |
misc 장치에 등록하기 (0) | 2017.09.20 |
디바이스 드라이버 - APP Open & Read (0) | 2017.09.19 |
2016] 모듈 제작에 대한 동영상 (0) | 2017.09.19 |