1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h>
#include <linux/fs.h> #include <linux/cdev.h> #include <linux/uaccess.h>
#include "./timestamp_autogenerated.h" #include "./version_autogenerated.h"
#define CHRDEV_NAME "sdev" #define CLASS_NAME "sclass" #define DEVICE_NAME "sdevice" #define BUFSIZE 32
#define CMD_TEST0 _IO('S', 0) #define CMD_TEST1 _IOW('S', 1, int) #define CMD_TEST2 _IOR('S', 2, int) #define CMD_TEST3 _IOW('S', 3, int)
struct __CMD_TEST{ int a; int b; int c; };
typedef struct __CHAR_DEVICE { dev_t dev_num; struct cdev s_cdev; struct class *class; struct device *device; char buf[BUFSIZE]; } _CHAR_DEVICE;
_CHAR_DEVICE g_sdev;
static int sdev_open(struct inode *inode, struct file *file) { printk("This is sdev_open!\n"); file->private_data = &g_sdev; return 0; }
static ssize_t sdev_read(struct file *file, char __user *buf, size_t size, loff_t *off) { int i = 0; loff_t offset = *off; size_t count = size; _CHAR_DEVICE *pDev=(_CHAR_DEVICE *)file->private_data;
if (offset > BUFSIZE) { return 0; }
if (count > BUFSIZE - offset) { count = BUFSIZE - offset; }
if (copy_to_user(buf, pDev->buf + offset, count)) { printk("copy_to_user error!\n"); return -1; }
for (i = 0; i < BUFSIZE; i++) { printk("buf[%d] %c\n", i, pDev->buf[i]); } printk("read offset is %llu, count is %d\n", offset, count); *off = *off + count;
return count; }
static ssize_t sdev_write(struct file *file, const char __user *buf, size_t size, loff_t *off) { int i = 0; loff_t offset = *off; size_t count = size; _CHAR_DEVICE *pDev=(_CHAR_DEVICE *)file->private_data;
if (offset > BUFSIZE) { return 0; }
if (count > BUFSIZE - offset) { count = BUFSIZE - offset; }
if (copy_from_user(pDev->buf + offset, buf, count)) { printk("copy_to_user error \n"); return -1; }
for (i = 0; i < BUFSIZE; i++) { printk("buf[%d] %c\n", i, pDev->buf[i]); } printk("write offset is %llu, count is %d\n", offset, count); *off = *off + count;
return count; }
static int sdev_release(struct inode *inode, struct file *file) { printk("This is sdev_release!\n"); return 0; }
static loff_t sdev_llseek(struct file *file, loff_t offset, int whence) { loff_t new_offset = 0; switch (whence) { case SEEK_SET: if (offset < 0 || offset > BUFSIZE) { return -EINVAL; } new_offset = offset; break; case SEEK_CUR: if ((file->f_pos + offset < 0) || (file->f_pos + offset > BUFSIZE)) { return -EINVAL; } new_offset = file->f_pos + offset; break; case SEEK_END: if (file->f_pos + offset < 0) { return -EINVAL; } new_offset = BUFSIZE + offset; break; default: break; } file->f_pos = new_offset;
return new_offset; }
static long sdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { int val = 0; switch(cmd) { case CMD_TEST0: printk("this is CMD_TEST0\n"); break; case CMD_TEST1: printk("this is CMD_TEST1\n"); printk("arg is %ld\n",arg); break; case CMD_TEST2: val = 1; printk("this is CMD_TEST2\n"); if(copy_to_user((int *)arg, &val, sizeof(val)) != 0) { printk("copy_to_user error \n"); } break; case CMD_TEST3: { struct __CMD_TEST cmd_test3 = {0}; if (copy_from_user(&cmd_test3, (int *)arg, sizeof(cmd_test3)) != 0) { printk("copy_from_user error\n"); } printk("cmd_test3.a = %d\n", cmd_test3.a); printk("cmd_test3.b = %d\n", cmd_test3.b); printk("cmd_test3.c = %d\n", cmd_test3.c); break; } default: break; }
return 0; }
static struct file_operations cdev_ops = { .owner = THIS_MODULE, .open = sdev_open, .read = sdev_read, .write = sdev_write, .release = sdev_release, .llseek = sdev_llseek, .unlocked_ioctl = sdev_ioctl, };
static int __init chrdev_data_private_demo_init(void) { int ret; int major, minor; printk("*** [%s:%d]Build Time: %s %s, git version:%s ***\n", __FUNCTION__, __LINE__, KERNEL_KO_DATE, KERNEL_KO_TIME, KERNEL_KO_VERSION); printk("chrdev_data_private_demo module init!\n");
ret = alloc_chrdev_region(&g_sdev.dev_num, 0, 1, CHRDEV_NAME); if (ret < 0) { printk("alloc_chrdev_region is error!\n"); } printk("alloc_register_region is ok!\n"); major = MAJOR(g_sdev.dev_num); minor = MINOR(g_sdev.dev_num); printk("major is %d, minor is %d !\n", major, minor);
cdev_init(&g_sdev.s_cdev, &cdev_ops); g_sdev.s_cdev.owner = THIS_MODULE; ret = cdev_add(&g_sdev.s_cdev, g_sdev.dev_num, 1); if (ret < 0) { printk("cdev_add is error !\n"); } printk("cdev_add is ok !\n"); g_sdev.class = class_create(THIS_MODULE, CLASS_NAME); g_sdev.device = device_create(g_sdev.class, NULL, g_sdev.dev_num, NULL, DEVICE_NAME);
return 0; }
static void __exit chrdev_data_private_demo_exit(void) { cdev_del(&g_sdev.s_cdev); unregister_chrdev_region(g_sdev.dev_num, 1); device_destroy(g_sdev.class, g_sdev.dev_num); class_destroy(g_sdev.class); printk("chrdev_data_private_demo exit!\n"); }
module_init(chrdev_data_private_demo_init); module_exit(chrdev_data_private_demo_exit);
MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("sumu"); MODULE_DESCRIPTION("Description"); MODULE_ALIAS("module's other name");
|