Skip to content

Instantly share code, notes, and snippets.

@taeseunglee
Created June 22, 2017 21:22
Show Gist options
  • Save taeseunglee/aaacab9fbd70d073804223a9d696f6c2 to your computer and use it in GitHub Desktop.
Save taeseunglee/aaacab9fbd70d073804223a9d696f6c2 to your computer and use it in GitHub Desktop.
#include <linux/string.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include "./puzzle_dev.h"
#include "./puzzle_data.h"
#include <asm/uaccess.h>
int __init puzzle_init(void);
void __exit puzzle_exit(void);
int puzzle_open(struct inode *, struct file *);
int puzzle_release(struct inode *, struct file *);
long puzzle_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param);
struct __puzzle_data puzzle_data;
static int puzzle_enable;
/* variables about this module */
static struct file_operations puzzle_fops =
{
.open = puzzle_open,
.release = puzzle_release,
.unlocked_ioctl = puzzle_ioctl,
};
int
puzzle_open(struct inode *minode, struct file *mfile)
{
printk("puzzle_open\n");
return 0;
}
int
puzzle_release(struct inode *minode, struct file *mfile)
{
printk("puzzle_release\n");
return 0;
}
/* ioctl_num : The number of the ioctl */
/* ioctl_param : The parameter to it */
long
puzzle_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
{
/* Switch according to the ioctl called */
switch (ioctl_num)
{
case PUZZLE_PUT:
copy_from_user(&puzzle_data, (struct __puzzle_data*)ioctl_param, sizeof(struct __puzzle_data));
printk("PUZZLE_PUT: %d %d\n", puzzle_data.row, puzzle_data.col);
break;
case PUZZLE_DATA_ENABLE:
{
int data = *((int*) ioctl_param);
if (data) // enable
puzzle_enable = 1; // static global
else // disable
puzzle_enable = 0;
printk("PUZZLE_DATA_ENABLE: %d\n", puzzle_enable);
}
break;
case PUZZLE_GET:
{
copy_to_user((struct __puzzle_data*) ioctl_param,
&puzzle_data,
sizeof(struct __puzzle_data));
}
break;
case PUZZLE_GET_ENABLE:
{
*((int*) ioctl_param) = puzzle_enable;
}
break;
}
return 0;
}
int __init
puzzle_init(void)
{
int result;
result = register_chrdev(PUZZLE_MAJOR, PUZZLE_NAME, &puzzle_fops);
if(result <0)
{
printk( "puzzle_init error %d\n",result);
return result;
}
printk("init module\n");
return 0;
}
void __exit
puzzle_exit(void)
{
printk("puzzle_exit\n");
unregister_chrdev(PUZZLE_MAJOR, PUZZLE_NAME);
}
module_init( puzzle_init);
module_exit( puzzle_exit);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("Taeseung Lee");
#ifndef __PUZZLE_DEV__
#define __PUZZLE_DEV__
#define PUZZLE_MAJOR 242
#define PUZZLE_MINOR 0
#define PUZZLE_NAME "puzzle_dev"
/* ioctl macros */
#define PUZZLE_PUT _IOW(PUZZLE_MAJOR, 1, struct __puzzle_data*)
#define PUZZLE_DATA_ENABLE _IOW(PUZZLE_MAJOR, 2, int*)
#define PUZZLE_GET _IOR(PUZZLE_MAJOR, 3, struct __puzzle_data*)
#define PUZZLE_GET_ENABLE _IOR(PUZZLE_MAJOR, 4, int*)
#endif /* __PUZZLE_DEV__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment