EmbLogic's Blog

module code to show how to initialize a cdev data structure with cdev_init() and tell the kernel about it with cdev_add().

RCS file: header.h,v
Working file: header.h
head: 1.16
branch:
locks: strict
root: 1.16
access list:
symbolic names:
keyword substitution: kv
total revisions: 16;    selected revisions: 16
description:
Base header file that includes the definitions of all the macros,structures and contains the necessary header files required by the module code.
—————————-
revision 1.16    locked by: root;
date: 2014/02/28 17:13:48;  author: root;  state: Exp;  lines: +2 -0
Header file:<linux/types.h> is added to support dev_t type.
—————————-
revision 1.15
date: 2014/02/28 16:58:15;  author: root;  state: Exp;  lines: +6 -0
Two macros are defined called MAJOR_NO and MINOR_NO.
—————————-
revision 1.14
date: 2014/02/28 15:44:56;  author: root;  state: Exp;  lines: +2 -2
The macro DEVICE_NAME is changed to DRIVER_NAME and the header file:<string.h> is removed.
—————————-
revision 1.13
date: 2014/02/28 15:41:31;  author: root;  state: Exp;  lines: +1 -1
Header file:<linux/cdev.h> is added to support a cdev structure.The cdev structure is used to register the driver with the kernel.
—————————-
revision 1.12
date: 2014/02/28 15:33:59;  author: root;  state: Exp;  lines: +1 -0
Header file:<string.h> is added to support memset() function.
—————————-
revision 1.11
date: 2014/02/28 15:18:15;  author: root;  state: Exp;  lines: +16 -0
Four macros are defined namely:DEVICE_SIZE , DATA_SIZE , QUANTUM and QSET.
These macros are used to initialize the fields inside the Scull_Dev structure.
—————————-
revision 1.10
date: 2014/02/28 15:10:35;  author: root;  state: Exp;  lines: +16 -0
Two structures are defined of type Sculldev and Scullqset.Sculldev is a structure that is used to hold device information.Scullqset is a structure that holds a pointer to the qset array and another pointer to a structure of Scullqset type.
—————————-
revision 1.9
date: 2014/02/27 21:07:42;  author: root;  state: Exp;  lines: +1 -0
Header file:<linux/slab.h> is added to support kmalloc() and kfree() functions.
—————————-
revision 1.8
date: 2014/02/27 20:02:53;  author: root;  state: Exp;  lines: +1 -0
Header file:<linux/moduleparam.h> is added to support the module_param() macro.With the help of this macro parameters can be passed to a module while loading it.
—————————-
revision 1.7
date: 2014/02/27 19:28:38;  author: root;  state: Exp;  lines: +4 -0
A macro called DEVICE_NAME is defined using #define.
—————————-
revision 1.6
date: 2014/02/27 19:20:01;  author: root;  state: Exp;  lines: +1 -0
Header file:<linux/fs.h> is added to the base header file to support various functions like alloc_chrdev_region(),unregister_chrdev_region(),etc.
—————————-
revision 1.5
date: 2014/02/27 18:44:36;  author: root;  state: Exp;  lines: +4 -0
A macro called DEBUG is defined using #define.
—————————-
revision 1.4
date: 2014/02/27 18:41:13;  author: root;  state: Exp;  lines: +2 -0
A special macro called MODULE_LICENSE is used to tell the kernel that the module bears a free license without such a decleration the kernel complains.
—————————-
revision 1.3
date: 2014/02/27 18:38:11;  author: root;  state: Exp;  lines: +1 -1
Header file:<linux/module.h> is added to the base header file to provide support for a great many number of macros like:MODULE_LICENSE , MODULE_AUTHOR,etc.
—————————-
revision 1.2
date: 2014/02/27 18:36:07;  author: root;  state: Exp;  lines: +2 -0
Header file:<linux/init.h> is added to the base header file to provide support for module_init() and module_exit() macros.
—————————-
revision 1.1
date: 2014/02/27 18:35:18;  author: root;  state: Exp;
Initial revision
=============================================================================
RCS file: initial.c,v
Working file: initial.c
head: 1.12
branch:
locks: strict
root: 1.12
access list:
symbolic names:
keyword substitution: kv
total revisions: 12;    selected revisions: 12
description:
File containing the module’s initialization function that is called at module insertion time.
—————————-
revision 1.12    locked by: root;
date: 2014/02/28 17:59:59;  author: root;  state: Exp;  lines: +1 -0
Header file:”file_opr.h” is added to include the file_operations structure.The file_operations structure is used to access the driver’s functions.
—————————-
revision 1.11
date: 2014/02/28 17:49:26;  author: root;  state: Exp;  lines: +15 -4
After a device number range has been obtained,the device needs to be activated by adding it to the character device database.This requires initializing an instance of cdev_init() followed by a call to cdev_add().cdev_add() tells te kernel about the cdev data structure.
—————————-
revision 1.10
date: 2014/02/28 17:16:31;  author: root;  state: Exp;  lines: +16 -0
Both the major and minor numbers are initialized to 0 inside the initialize_parameters() function.Macros like MAJOR() and MINOR() are used to extract the major and minor number from dev type variable.Another macro called MKDEV is used to create the device number.
Checking……
—————————-
revision 1.9
date: 2014/02/28 16:50:22;  author: root;  state: Exp;  lines: +25 -0
From the initialization function,another function is called to initialize the parameters such as device size, quantum_size ,qset_size and data_size.

Status:The parameters are initialized successfully.
Now before the kernel calls the device’s operations,a cdev structure must be initialized with cdev_init() and the kernel is being told about the initialization with cdev_add().Implementing this task….
—————————-
revision 1.8
date: 2014/02/28 15:46:09;  author: root;  state: Exp;  lines: +1 -1
Inside the alloc_chrdev_region(),the fourth argument is changed from DEVICE_NAME to DRIVER_NAME.
—————————-
revision 1.7
date: 2014/02/28 15:32:31;  author: root;  state: Exp;  lines: +1 -0
memset() is used to fill the memory allocated using kmalloc() function with ”.
—————————-
revision 1.6
date: 2014/02/28 15:26:38;  author: root;  state: Exp;  lines: +12 -1
kmalloc() function is used to allocate memory.kmalloc() function takes two arguments:
1.size ,and
2.set of flags.
On success,kmalloc returns a pointer to the allocated memory otherwise it returns NULL.
In the flag field,GFP_KERNEL is used.
—————————-
revision 1.5
date: 2014/02/27 20:12:29;  author: root;  state: Exp;  lines: +7 -1
module_param() macro is used.With the help of this macro parameters can be passed to the module while loading it,when using “insmod”.
module_param() macro takes three arguments:
1.The name of the variable,
2.The type of the variable,
3.Permissions.
—————————-
revision 1.4
date: 2014/02/27 19:58:09;  author: root;  state: Exp;  lines: +1 -1
alloc_chrdev_region() function is used to allocate dynamically a major number.On success,the first parameter of this function stores the first device number.
—————————-
revision 1.3
date: 2014/02/27 19:31:39;  author: root;  state: Exp;  lines: +11 -0
Header file: “decleration.h” is added to include all the variable declerations.
—————————-
revision 1.2
date: 2014/02/27 18:50:37;  author: root;  state: Exp;  lines: +7 -0
module_init() macro is used to define which function is called during module’s insertion time.
__init token hints to the kernel that the function is used only at the initialization time.
The function specified by the module_init() macro is declared static because it is not meant to be visible outside the file.
—————————-
revision 1.1
date: 2014/02/27 18:49:06;  author: root;  state: Exp;
Initial revision
=============================================================================
RCS file: cleanup.c,v
Working file: cleanup.c
head: 1.6
branch:
locks: strict
root: 1.6
access list:
symbolic names:
keyword substitution: kv
total revisions: 6;    selected revisions: 6
description:
File that contains the cleanup function which returns all the resources back to the system.
—————————-
revision 1.6    locked by: root;
date: 2014/02/28 15:35:01;  author: root;  state: Exp;  lines: +1 -0
kfree() function is used for freeing the memory.
—————————-
revision 1.5
date: 2014/02/27 20:15:39;  author: root;  state: Exp;  lines: +3 -1
A variable of type int representing the number of devices is included here using extern.
—————————-
revision 1.4
date: 2014/02/27 19:36:03;  author: root;  state: Exp;  lines: +1 -0
A variable dev of type dev_t storing the device number is included here using extern.
—————————-
revision 1.3
date: 2014/02/27 19:33:49;  author: root;  state: Exp;  lines: +3 -1
Called unregister_chrdev_region() which is having 2 arguments: device number and the total number of device numbers to unregister.
—————————-
revision 1.2
date: 2014/02/27 18:58:02;  author: root;  state: Exp;  lines: +14 -0
module_exit() macro is used to define which function is to be called at module removal time.\
The function specified by the module_exit() macro is declared static because it is not meant to be visible outside the file.
—————————-
revision 1.1
date: 2014/02/27 18:55:42;  author: root;  state: Exp;
Initial revision
=============================================================================
RCS file: decleration.h,v
Working file: decleration.h
head: 1.6
branch:
locks: strict
root: 1.6
access list:
symbolic names:
keyword substitution: kv
total revisions: 6;    selected revisions: 6
description:
All the variable declerations are done inside this header file.
—————————-
revision 1.6    locked by: root;
date: 2014/02/28 17:05:32;  author: root;  state: Exp;  lines: +1 -0
A variable of type dev_t is declared that is used to store the updated device number obtained from mkdev.
—————————-
revision 1.5
date: 2014/02/28 16:59:40;  author: root;  state: Exp;  lines: +3 -0
Two variables are declared inside this file that will hold the information about major and minor numbers.
—————————-
revision 1.4
date: 2014/02/28 16:39:07;  author: root;  state: Exp;  lines: +4 -0
Variables quantum_size,qset_size,data_size and device_size are declared that will get initialized by the macros defined in header.h.
—————————-
revision 1.3
date: 2014/02/28 15:30:46;  author: root;  state: Exp;  lines: +1 -0
A variable of type Sculldev is declared in this file.
—————————-
revision 1.2
date: 2014/02/27 20:06:26;  author: root;  state: Exp;  lines: +1 -0
A variable of type int is declared representing the number of devices.
—————————-
revision 1.1
date: 2014/02/27 19:30:54;  author: root;  state: Exp;
Initial revision
=============================================================================

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>