misc driver (miscellaneous devices)는 주번호 10번을 고정으로 minor number 는 동적으로 할당하면서 등록된다
주번호를 사용할 때는 충돌되는 위험이 있지만 misc driver는 이러한 위험이 없다
misc_register(struct miscdevice *misc) - misc 등록 함수
misc_deregister(struct miscdevice *misc) - misc 해제 함수
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
static int open(struct inode *inode, struct file *file)
{
pr_info("open() is called.\n");
return 0;
}
static int close(struct inode *inode, struct file *file)
{
pr_info("close() is called.\n");
return 0;
}
static long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
pr_info("ioctl() is called. cmd = %d, arg = %ld\n", cmd, arg);
return 0;
}
static const struct file_operations my_dev_fops = {
.owner = THIS_MODULE,
.open = open,
.release = close,
.unlocked_ioctl = ioctl,
};
/*
.minor = minor number 동적 할당,
.name = misc driver name,
.fops = misc file operation,
*/
static struct miscdevice miscdevice = {
.minor = MISC_DYNAMIC_MINOR,
.name = "mydev",
.fops = &my_dev_fops,
};
static int __init hello_init(void)
{
int ret;
pr_info("Hello init\n");
/* misc driver 등록 */
ret = misc_register(&miscdevice);
if (ret != 0) {
pr_err("fail register the misc device mydev");
return ret;
}
pr_info("mydev got minor %i\n",miscdevice.minor);
return 0;
}
static void __exit hello_exit(void)
{
pr_info("Hello exit\n");
misc_deregister(&miscdevice);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
'misc_drvier'를 적재 하면 minor number를 동적으로 할당한 것을 확인 할 수 있다.
root@raspberrypi:/home/board# insmod misc_driver.ko
root@raspberrypi:/home/board# dmesg
[ 83.523561] Hello init
[ 83.523901] mydev got minor 60
'/sys/class/misc'에서 생성된 파일 list
root@raspberrypi:/home/board# ls /sys/class/misc/
autofs cachefiles cpu_dma_latency fuse hw_random loop-control mydev rfkill vcsm-cma watchdog
'/dev/mydev'를 보면 minor number가 60으로 동적 할당 되어있다.
root@raspberrypi:/home/board# ls -l /dev/mydev
crw------- 1 root root 10, 60 May 21 07:46 /dev/mydev
'device driver' 카테고리의 다른 글
charter device driver - 6. led_class_platform 만들기 (0) | 2023.05.26 |
---|---|
charter device driver - 5. platform_driver로 led control 해보자 (0) | 2023.05.22 |
charter device driver - 4. platform driver module 만들기 (0) | 2023.05.21 |
charter device driver - 2. char device drier 만들기 (0) | 2023.05.21 |
charter device driver - 1. 간단한 module 만들기 (0) | 2023.05.21 |
댓글