#include "mychardev.h"
int init_module(void)
{
printk(KERN_INFO "[+] Module Init\n");
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_INFO "[-] Registration chrdev failed with : %d\n", Major);
return Major;
}
printk(KERN_INFO "[+] Registration chrdev success : %d\n", Major);
printk(KERN_INFO "[+] Create device command : mknod /dev/%s c %d 0\n", DEVICE_NAME, Major);
return SUCCESS;
}
void cleanup_module(void)
{
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_INFO "[+] Module Exit\n");
}
// Ex : cat /dev/mychardev
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
printk("[*] Device Open\n");
if(Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "- %d times read\n", ++counter);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
static int device_release(struct inode *inode, struct file *file)
{
printk("[*] Device Release\n");
Device_Open--;
module_put(THIS_MODULE);
return 0;
}
static ssize_t device_read(struct file *flip, char *buffer, size_t length, loff_t *offset)
{
int bytes_read = 0;
printk("[*] Device read\n");
if(*msg_Ptr == 0)
return 0;
while(length && *msg_Ptr)
{
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
// Ex : echo "test" > /dev/mychardev
static ssize_t device_write(struct file *flip, const char *buffer, size_t length, loff_t *offset)
{
printk("[*] Device Write");
printk("[-] Not supported operation\n");
return -EINVAL;
}