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
| #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/kdev_t.h> #include <linux/cdev.h> #include <linux/slab.h> #include <linux/uaccess.h> #include <linux/mutex.h> #include <linux/device.h> #include <linux/version.h>
#define DEVICE_NAME "chrdev_test" #define CLASS_NAME "chrdev_class" #define BUF_SIZE 1024 #define CMD_CLEAR _IO('k', 1)
struct my_device_data { dev_t dev_num; struct cdev cdev; struct class *class; struct device *device; char *buffer; struct mutex lock; };
static struct my_device_data my_dev;
static int chrdev_open(struct inode *inode, struct file *file) { printk(KERN_INFO "This is chrdev_open\n"); return 0; }
static ssize_t chrdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset) { printk(KERN_INFO "This is chrdev_read\n"); int ret; size_t avaliable;
mutex_lock(&my_dev.lock); if (*offset >= BUF_SIZE) { mutex_unlock(&my_dev.lock); printk(KERN_WARNING "chrdev_read: buf is out of size"); return 0; }
if (count > BUF_SIZE - *offset) { count = BUF_SIZE - *offset; }
ret = copy_to_user(buf, my_dev.buffer + *offset, count); if (ret) { mutex_unlock(&my_dev.lock); return -EFAULT; }
*offset += count; mutex_unlock(&my_dev.lock); printk(KERN_INFO "chrdev_read: read %d bytes\n", count); return count; }
static ssize_t chrdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { printk(KERN_INFO "This is chrdev_write\n"); int ret; mutex_lock(&my_dev.lock);
if (*offset >= BUF_SIZE) { mutex_unlock(&my_dev.lock); return -ENOSPC; }
if (count > BUF_SIZE - *offset) { count = BUF_SIZE - *offset; }
ret = copy_from_user(my_dev.buffer + *offset, buf, count); if (ret) { mutex_unlock(&my_dev.lock); return -EFAULT; }
*offset += count; mutex_unlock(&my_dev.lock); return count; }
static int chrdev_release(struct inode *inode, struct file *file) { printk(KERN_INFO "This is chrdev_release"); return 0; }
static long chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch(cmd) { case CMD_CLEAR: mutex_lock(&my_dev.lock); memset(my_dev.buffer, 0, BUF_SIZE); mutex_unlock(&my_dev.lock); printk(KERN_INFO "chrdev_ioctl: buffer cleared via IOCTL\n"); break; default: return -EINVAL; } return 0; }
static loff_t chrdev_llseek(struct file *file, loff_t offset, int whence) { printk(KERN_INFO "This is chrdev_llseek"); loff_t new_pos;
switch (whence) { case SEEK_SET: new_pos = offset; break; case SEEK_CUR: new_pos = file->f_pos + offset; break; case SEEK_END: new_pos = BUF_SIZE + offset; break; default: return -EINVAL; }
if (new_pos < 0) { return -EINVAL; }
file->f_pos = new_pos;
return new_pos; }
static struct file_operations fops = { .owner = THIS_MODULE, .open = chrdev_open, .release = chrdev_release, .read = chrdev_read, .write = chrdev_write, .unlocked_ioctl = chrdev_ioctl, .llseek = chrdev_llseek, };
static int __init chrdev_fops_init(void) { int ret; int major, minor; mutex_init(&my_dev.lock);
my_dev.buffer = kzalloc(BUF_SIZE, GFP_KERNEL); if (!my_dev.buffer) { printk(KERN_ERR "kzalloc failed\n"); return -ENOMEM; }
ret = alloc_chrdev_region(&my_dev.dev_num, 0, 1, DEVICE_NAME); if (ret < 0) { printk(KERN_ERR "alloc_chrdev_region failed\n"); goto failed_mem; }
printk(KERN_INFO "alloc_chrdev_region ok\n"); major = MAJOR(my_dev.dev_num); minor = MINOR(my_dev.dev_num); printk(KERN_INFO "major is %d\n", major); printk(KERN_INFO "minor is %d\n", minor);
cdev_init(&my_dev.cdev, &fops); my_dev.cdev.owner = THIS_MODULE;
ret = cdev_add(&my_dev.cdev, my_dev.dev_num, 1); if (ret < 0) { printk(KERN_ERR "cdev_add failed\n"); goto failed_region; } printk(KERN_INFO "cdev_add is ok\n"); my_dev.class = class_create("class_test"); if (IS_ERR(my_dev.class)) { printk(KERN_ERR "class_create failed! Error code: %d\n", ret); ret = PTR_ERR(my_dev.class); goto failed_cdev; }
my_dev.device = device_create(my_dev.class, NULL, my_dev.dev_num, NULL, DEVICE_NAME); if (IS_ERR(my_dev.device)) { ret = PTR_ERR(my_dev.device); printk(KERN_ERR "device_create failed! Error code: %d\n", ret); goto failed_class; }
printk(KERN_INFO "chrdev is ready"); return 0;
failed_class: class_destroy(my_dev.class); failed_cdev: cdev_del(&my_dev.cdev); failed_region: unregister_chrdev_region(my_dev.dev_num, 1); failed_mem: kfree(my_dev.buffer); return ret; }
static void __exit chrdev_fops_exit(void) { device_destroy(my_dev.class, my_dev.dev_num); class_destroy(my_dev.class); cdev_del(&my_dev.cdev); unregister_chrdev_region(my_dev.dev_num, 1); kfree(my_dev.buffer); printk("module exit\n"); }
module_init(chrdev_fops_init); module_exit(chrdev_fops_exit); MODULE_LICENSE("GPL");
|