#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“KERNEL_CODE”);
#define DEBUG
#define NAME “char_dev”
#define NODE 5
#define SCULL_MAJOR 0
#define SCULL_MINOR 0
#define QUANTUM 8
#define SIZE 0
#define QSET 8
#define DEV_SIZE 1024
#define DEVICE_NAME “char_dev”
#define CLASS_NAME “scull”
static int scull_minor = SCULL_MINOR;
static int scull_major = SCULL_MAJOR;
static struct class* scull_class = NULL;
static struct device* scull_device = NULL;
int size = SIZE;
int qset = QSET;
int quantum = QUANTUM;
struct Scull_qset{
struct Scull_qset *next;
void **data1;
};
dev_t dev;
struct Scull_dev{
struct Scull_qset *data;
int quantum;
int qset;
int device_size;
int data_size;
struct cdev cdev;
};
struct Scull_dev *scull_dev;
const struct file_operations fops=
{};
struct inode *finode;
struct file *ffile;
/***********************************************************************************/
/* sysfs entry for 1st operation *//*we can pass the arguments we need*/
static ssize_t sys_entry1(struct device* dev, struct device_attribute* attr, const char* buf, size_t count)
{
#ifdef DEBUG
printk(KERN_INFO ” string from user is%s\n”,buf);
#endif
return count;
}
/* Declare the sysfs entries. The macros create instances of dev_attr_fifo and dev_attr_reset */
static DEVICE_ATTR(entry, S_IWUSR, NULL, sys_entry1);
/***********************************************************************************/
static int __init hello_world(void){
int retval;
retval = alloc_chrdev_region(&dev,scull_minor,NODE,DEVICE_NAME);
scull_minor = MINOR(dev);
scull_major = MAJOR(dev);
scull_dev = kmalloc(NODE*sizeof(struct Scull_dev),GFP_KERNEL);
memset(scull_dev,”,sizeof(struct Scull_dev));
/* use a “virtual” device class. */
scull_class = class_create(THIS_MODULE, CLASS_NAME);
printk(KERN_DEBUG”register device class ‘%s’\n”, CLASS_NAME);
if (!scull_class) {
printk(KERN_DEBUG”register device class ‘%s’\n”, CLASS_NAME);
goto OUT;
}
/* With a class, the easiest way to instantiate a device is to call device_create() */
scull_device = device_create(scull_class, NULL, MKDEV(scull_major, 0), NULL, CLASS_NAME “_” DEVICE_NAME);
if (!scull_device) {
printk(KERN_DEBUG”register device class ‘%s’\n”, DEVICE_NAME);
goto OUT;
}
/* dev_attr_entry come from the DEVICE_ATTR(…) earlier */
retval = device_create_file(scull_device, &dev_attr_entry);
if (retval cdev,&fops);
scull_dev->quantum = quantum;
scull_dev->qset = qset;
scull_dev->device_size= size;
scull_dev->cdev.owner = THIS_MODULE;
scull_dev->cdev.ops = &fops;
retval = cdev_add(&scull_dev->cdev,dev, NODE);
if(retvalcdev);
kfree(scull_dev);
device_remove_file(scull_device, &dev_attr_entry);
device_destroy(scull_class, MKDEV(scull_major, 0));
class_unregister(scull_class);
class_destroy(scull_class);
#ifdef DEBUG
printk(KERN_INFO”cha_device unregistration from fun %s\n”,__func__);
printk(KERN_INFO”BYE BYE \n”);
#endif
}
module_init(hello_world);
module_exit(hello_exit);