Skip to content

Instantly share code, notes, and snippets.

@sepfy
Last active October 4, 2020 05:02
Show Gist options
  • Save sepfy/0d4228070a85ee6a0abf3c8847e99d3d to your computer and use it in GitHub Desktop.
Save sepfy/0d4228070a85ee6a0abf3c8847e99d3d to your computer and use it in GitHub Desktop.
struct miscdev_data {
int val;
char data[64];
};
#define IOCTL_MISCDEV_SET 0x00
#define IOCTL_MISCDEV_GET 0x01
static long mydev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct miscdev_data data;
memset(&data, 0, sizeof(data));
switch (cmd) {
case IOCTL_MISCDEV_SET:
printk("[%s][%d]\n", __func__, __LINE__);
copy_from_user(&data, (int __user *)arg, sizeof(data));
printk("Set data: miscdev_data.val = %d, miscdev_data.data = %s\n", data.val, data.data);
break;
case IOCTL_MISCDEV_GET:
printk("[%s][%d]\n", __func__, __LINE__);
sprintf(data.data, "Kernel says hi");
data.val = 3;
copy_to_user((int __user *)arg, &data, sizeof(data));
break;
default:
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment