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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
| #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/slab.h> #include <linux/uaccess.h>
#define GLOBALMEM_SIZE (0x1000) #define MEM_CLEAR (0x01)
#define GLOBALMEM_MAJOR (0)
#define DEV_NAME "globalmem" #define DEVICE_NUM (1)
static int globalmem_major = GLOBALMEM_MAJOR; module_param(globalmem_major, int, S_IRUGO);
struct globalmem_dev { struct cdev cdev; unsigned char mem[GLOBALMEM_MAJOR]; struct mutex mutex; wait_queue_head_t r_wait; wait_queue_head_t w_wait; unsigned int current_len; };
struct globalmem_dev *globalmem_devp;
static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; DECLARE_WAITQUEUE(wait, current);
mutex_lock(&dev->mutex); add_wait_queue(&dev->r_wait, &wait);
while(dev->current_len == 0) { if(filp->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; } printk(KERN_INFO "wait for read!\n"); __set_current_state(TASK_INTERRUPTIBLE); mutex_unlock(&dev->mutex); schedule(); if(signal_pending(current)) { ret = -ERESTARTSYS; goto out2; } mutex_lock(&dev->mutex); }
if(p >= GLOBALMEM_SIZE) { ret = 0; goto out; } if(count > dev->current_len) { count = dev->current_len; }
if(copy_to_user(buf, dev->mem , count)) { ret = -EFAULT; } else { memcpy(dev->mem, dev->mem + count, dev->current_len - count); dev->current_len -= count; *ppos += count; ret = count; printk(KERN_INFO "read %u byte(s) from %lu\n", count, p); wake_up_interruptible(&dev->w_wait); printk(KERN_INFO "wakeup write!\n"); }
out: mutex_unlock(&dev->mutex); out2: remove_wait_queue(&dev->r_wait, &wait); set_current_state(TASK_RUNNING); return ret; }
static ssize_t globalmem_write(struct file *filp, const char __user * buf, size_t size, loff_t*ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; DECLARE_WAITQUEUE(wait, current); mutex_lock(&dev->mutex); add_wait_queue(&dev->w_wait, &wait);
while(dev->current_len >= GLOBALMEM_SIZE) { if(filp->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; } printk(KERN_INFO "wait for write!\n"); __set_current_state(TASK_INTERRUPTIBLE); mutex_unlock(&dev->mutex); schedule(); if(signal_pending(current)) { ret = -ERESTARTSYS; goto out2; } mutex_lock(&dev->mutex); }
if(p >= GLOBALMEM_SIZE) { ret = 0; goto out; } if(count > GLOBALMEM_SIZE - dev->current_len) { count = GLOBALMEM_SIZE - dev->current_len; }
if(copy_from_user(dev->mem + dev->current_len, buf, count)) { ret = -EFAULT; } else { dev->current_len += count; *ppos += count; ret = count; printk(KERN_INFO "written %u byte(s) current_len %lu\n", count, dev->current_len); printk(KERN_INFO "wakeup read"); wake_up_interruptible(&dev->r_wait); }
out: mutex_unlock(&dev->mutex); out2: remove_wait_queue(&dev->w_wait, &wait); set_current_state(TASK_RUNNING);
return ret;
}
static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig) { loff_t ret = 0; switch(orig) { case 0: { if(offset < 0) { ret= -EINVAL; break; } if((unsigned int)offset > GLOBALMEM_SIZE) { ret = -EINVAL; break; } filp->f_pos = (unsigned int)offset; ret = filp->f_pos; }break; case 1: { if((filp->f_pos + offset) > GLOBALMEM_SIZE) { ret = -EINVAL; break; } if((filp->f_pos + offset) < 0) { ret = -EINVAL; break; } filp->f_pos += offset; ret = filp->f_pos; }break; default: { ret = -EINVAL; }break;
} return ret;
} static long globalmem_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct globalmem_dev *dev = filp->private_data;
switch(cmd) { case MEM_CLEAR: { mutex_lock(&dev->mutex); memset(dev->mem, 0, GLOBALMEM_SIZE); mutex_unlock(&dev->mutex); printk(KERN_INFO "globalmem is set to zero\n"); }break; default: return -EINVAL; }
return 0;
}
static int globalmem_open(struct inode *inode, struct file *filp) { struct globalmem_dev *dev = container_of(inode->i_cdev, struct globalmem_dev, cdev); filp->private_data = dev; return 0; } static int globalmem_release(struct inode *inode, struct file *filp) { return 0; }
static const struct file_operations globalmem_fops = { .owner = THIS_MODULE, .llseek = globalmem_llseek, .read =globalmem_read, .write = globalmem_write, .unlocked_ioctl = globalmem_ioctl, .open = globalmem_open, .release = globalmem_release, };
static void globalmem_setup_cdev(struct globalmem_dev *dev, int index) { int err, devno = MKDEV(globalmem_major, index);
cdev_init(&dev->cdev, &globalmem_fops); dev->cdev.owner = THIS_MODULE; err = cdev_add(&dev->cdev, devno, 1); if(err) { printk(KERN_NOTICE "Error %d adding globalmem %d\n", err, index); } }
static int __init globalmem_init(void) { int ret; int i = 0; dev_t devno = MKDEV(globalmem_major, 0);
if(globalmem_major) { ret = register_chrdev_region(devno, DEVICE_NUM, DEV_NAME); } else { ret = alloc_chrdev_region(&devno, 0, DEVICE_NUM, DEV_NAME); globalmem_major = MAJOR(devno); } if(ret < 0) { return ret; } globalmem_devp = kzalloc(sizeof(struct globalmem_dev) * DEVICE_NUM, GFP_KERNEL); if(globalmem_devp == NULL) { ret = -ENOMEM; goto fail_malloc; } for(i = 0; i < DEVICE_NUM; i++) { mutex_init(&(globalmem_devp + i)->mutex); globalmem_setup_cdev(globalmem_devp + i, i); init_waitqueue_head(&(globalmem_devp + i)->r_wait); init_waitqueue_head(&(globalmem_devp + i)->w_wait); } return 0;
fail_malloc: unregister_chrdev_region(devno, DEVICE_NUM); return ret; } module_init(globalmem_init);
static void __exit globalmem_exit(void) { int i = 0; for(i= 0; i < DEVICE_NUM; i++) { cdev_del(&(globalmem_devp + i)->cdev); } unregister_chrdev_region(MKDEV(globalmem_major, 0), DEVICE_NUM); kfree(globalmem_devp); } module_exit(globalmem_exit);
MODULE_AUTHOR("kcmetercec <kcmeter.cec@gmail.com>");
MODULE_LICENSE("GPL v2"); MODULE_DESCRIPTION("A simple example char device "); MODULE_ALIAS("a simplest module"); MODULE_VERSION("ver1.0");
|