无聊,照着书上边抄边写,贴上来共享一下,非常不规范,仅供娱乐
/********************************************************
#include <linux/init.h>#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void){
printk(KERN_EMERG "Hello world\n");
return 0;
}
static void hello_exit(void){
printk(KERN_EMERG "Goodby,cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
**********************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
//#include "scull.h"
#define SCULL_MAJOR 0
#define SCULL_MINOR 0
#define SCULL_QUANTUM 4000
#define SCULL_QSET 1000
extern int scull_open(struct inode *inode, struct file *filp);
extern int scull_release(struct inode *inode, struct file *filp);
extern ssize_t scull_read(struct file *filp,char __user *buf, size_t count,loff_t *f_pos);
extern ssize_t scull_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_pos);
static int scull_major = SCULL_MAJOR;
static int scull_minor = SCULL_MINOR;
static int scull_quantum = SCULL_QUANTUM;
static int scull_qset = SCULL_QSET;
static int scull_nr_devs = 1;
static dev_t dev;
struct scull_dev{
struct scull_qset *data;
int quantum;
int qset;
unsigned long size;
unsigned int access_key;
struct semaphore sem;
struct cdev cdev;
}scull_dev_all;
struct file_operations scull_fops={
.owner = THIS_MODULE,
// .llseek = scull_llseek,
.open = scull_open,
.read = scull_read,
.write = scull_write,
// .ioctl = scull_ioctl,
.release = scull_release,
};
struct scull_qset{
void **data;
struct scull_qset *next;
};
int scull_trim(struct scull_dev *dev){
struct scull_qset *next, *dptr;
int qset = dev->qset;
int i;
for(dptr = dev->data;dptr;dptr = next){
if(dptr->data){
for(i = 0;i<qset;i++)
kfree(dptr->data[i]);
kfree(dptr->data);
dptr->data = NULL;
}
next = dptr->next;
kfree(dptr);
}
dev->size = 0;
dev->quantum = scull_quantum;
dev->qset = scull_qset;
dev->data = NULL;
return 0;
}
struct scull_qset *scull_follow(struct scull_dev *dev, int item){
struct scull_qset *dptr = dev->data;
if (!dptr) {
dptr = dev->data = kmalloc(sizeof(struct scull_qset),GFP_KERNEL);
if (!dptr) {
return NULL;
}
memset(dptr, 0, sizeof(struct scull_qset));
}
while (item--) {
if (!dptr->next) {
dptr->next = kmalloc(sizeof(struct scull_qset),GFP_KERNEL);
if (!dptr->next) {
return NULL;
}
memset(dptr->next, 0, sizeof(struct scull_qset));
}
dptr = dptr->next;
}
return dptr;
}
int scull_open(struct inode *inode, struct file *filp){
struct scull_dev *scull_dev;
scull_dev = container_of(inode->i_cdev,struct scull_dev, cdev);
scull_dev->quantum = scull_quantum;
scull_dev->qset = scull_qset;
scull_dev->size = 0;
filp->private_data = scull_dev;
if((filp->f_flags & O_ACCMODE) == O_WRONLY){
scull_trim(scull_dev);
}
return 0;
}
int scull_release(struct inode *inode, struct file *filp){
return 0;
}
ssize_t scull_read(struct file *filp,char __user *buf, size_t count,loff_t *f_pos){
struct scull_dev *scull_dev = filp->private_data;
struct scull_qset *dptr;
int quantum = scull_dev->quantum;
int qset = scull_dev->qset;
int itemsize = quantum * qset;
int item,s_pos,q_pos,rest;
ssize_t retval = 0;
if(down_interruptible(&scull_dev->sem))
return -ERESTARTSYS;
if(*f_pos >= scull_dev->size)
goto out;
if(*f_pos + count > scull_dev->size)
count = scull_dev->size - *f_pos;
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum;
q_pos = rest % quantum;
dptr = scull_follow(scull_dev,item);
if(dptr == NULL || !dptr->data || !dptr->data[s_pos])
goto out;
if(count > quantum - q_pos)
count = quantum - q_pos;
if(copy_to_user(buf, dptr->data[s_pos]+q_pos, count)){
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
out:
up(&scull_dev->sem);
return retval;
}
ssize_t scull_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_pos){
struct scull_dev *scull_dev = filp->private_data;
struct scull_qset *dptr;
int quantum = scull_dev->quantum;
int qset = scull_dev->qset;
int itemsize = quantum * qset;
int item,s_pos,q_pos,rest;
ssize_t retval = -ENOMEM;
if(down_interruptible(&scull_dev->sem))
return -ERESTARTSYS;
if(*f_pos >= scull_dev->size)
goto out;
if(*f_pos + count > scull_dev->size)
count = scull_dev->size - *f_pos;
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum;
q_pos = rest % quantum;
dptr = scull_follow(scull_dev,item);
if(dptr == NULL)
goto out;
if(!dptr->data){
dptr->data = kmalloc(qset * sizeof(char *),GFP_KERNEL);
if(!dptr->data)
goto out;
memset(dptr->data, 0, qset * sizeof(char *));
}
if(!dptr->data[s_pos]){
dptr->data[s_pos] = kmalloc(quantum,GFP_KERNEL);
if(!dptr->data[s_pos])
goto out;
}
if(count > quantum - q_pos)
count = quantum - q_pos;
if(copy_from_user(dptr->data[s_pos] + q_pos, buf, count)){
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
if(scull_dev->size < *f_pos)
scull_dev->size = *f_pos;
out:
up(&scull_dev->sem);
return retval;
}
static int scull_init(void){
if(scull_major){
dev = MKDEV(scull_major,scull_minor);
register_chrdev_region(dev,scull_nr_devs,"scull");
}else{
alloc_chrdev_region(&dev,scull_minor,scull_nr_devs,"scull");
scull_major = MAJOR(dev);
}
init_MUTEX(&scull_dev_all.sem);
cdev_init(&scull_dev_all.cdev,&scull_fops);
cdev_add(&scull_dev_all.cdev,dev,0);
}
static void scull_exit(void){
unregister_chrdev_region(dev,scull_nr_devs);
cdev_del(&scull_dev_all.cdev);
}
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("XUELIANG.FAN");
module_init(scull_init);
module_exit(scull_exit);