/*
 * char_module.c 
 * 
 * Author: Konstantinos Chasapis 
 * 	e-mail: konstantinos.chasapis@informatik.uni-hamburg.de
 * 
 * This file is part of the corse matirial „Kernel Programming“ 
 * Praktikum offered from the Scientific Computing group of the   
 * University of Hamburg, Germany. 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For any commnet or complaint contact the author of the file. 
 *
 */

/* contains definitions and symbols need by loadable modules */
#include <linux/module.h>

/* contains macros for initilization and cleanup functions */
#include <linux/init.h>

#include <linux/device.h>

/* KERN_ALERT */
#include <linux/kernel.h>

/* contains definitions of the error codes */
#include <linux/errno.h>

/* contains definitions for the file strcuture and operations  */
#include <linux/fs.h>

/* contains definitions for the char devices */
#include <linux/cdev.h>

/* macro to declare that is module bears a free license. 
 * using this license you can call the functions implemented
 * inside the linux kernel.
 */
MODULE_LICENSE("GPL");

/* the modules author */
MODULE_AUTHOR("Someone you might know");

/* descripton of the module purpose */
MODULE_DESCRIPTION("Char device module example.");

/* the current version of the module */
MODULE_VERSION("1.0");

/* char example open function */
int 
char_open (struct inode *ino, struct file * file)
{
  printk(KERN_ALERT "Char module open function.\n");
  return 0;
}

/* char example release function */
int 
char_release (struct inode *ino, struct file * file)
{
  printk(KERN_ALERT "Char module release function.\n");
  return 0;
}


/* the structure of the char module */
struct char_example { 
  struct cdev char_cdev;	
} char_exp;


/* file operation struct */
static struct file_operations char_fops = {
	.owner = THIS_MODULE, 
	.open = char_open,
	.release = char_release,
};

/* allocate device number */
static dev_t char_dev_number;


#define DEVICE_NAME "charmod"

/* class */
struct class *char_mod_class;

/* the initilization function of the module */
static int __init
char_mod_init(void)
{
  int err;
  /* printk is a subset of printf implemented in the linux kernel.
   * KERN_ALERT is the priority of the message.
   * the output is the function will be printed in the system log files.
   */
  printk(KERN_ALERT "Char module init: started.\n");

  /* request dynamic allocation of a device number */
  if ( alloc_chrdev_region(&char_dev_number, 0, 1, DEVICE_NAME)  < 0 ) {
	err = -1;
	goto fail;
  }

  /* connect the cdev with the file operation strcut */
  cdev_init( &char_exp.char_cdev, &char_fops);

  /* create the class */
  char_mod_class = class_create(THIS_MODULE, DEVICE_NAME);

  /* create the /dev entries */
  device_create( char_mod_class, NULL, char_dev_number, NULL, "charmod");

  printk(KERN_ALERT "Char module init: done.\n");
  return 0; /* success */
  
fail:
  printk(KERN_ALERT "Char module init: failed\n");
  return err; /*  */
}

/* the shutdown function of the module */
static void __exit 
char_mod_exit(void)
{
  printk(KERN_ALERT "Char module exit: started.\n");

  unregister_chrdev_region(char_dev_number, 1);
 
  device_destroy(char_mod_class, char_dev_number);
  
  cdev_del(&char_exp.char_cdev);
 
  class_destroy(char_mod_class);
 
  printk(KERN_ALERT "Char module exit: done.\n");
}

/* declares which function will be invoked when the module is loaded */
module_init(char_mod_init);

/* declares which function will be invoked when the module is removed */
module_exit(char_mod_exit);

