CHARACTER DRIVER:- I have been working on device driver(character driver) since almost last three weeks. FIrst of all in device driver,I need to know what is device driver and why we need device driver?Device driver is nothing what a piece of code(called module) as a part of kernel.The device driver can be classified into three basic categories.
1) Character driver 2) Block driver 3 ) Network or Pipe driver.
1)Character driver:- A character device is one that can be accessed as a stream of bytes i.e. character by character.
As we know that it is kernel which is directly interect with the hardware and if any application want to interect with the hardware then we need a driver in the kernel space. As we know that our applications are in the user space and hardware is place in the kernel space and no application can directly access the hardware in the kernel space.so we create a named pipe(called fifo) between user space and kernel space using mknod.
Device Driver Implementation:-
In device driver implementation, firstly I used two modules called module_init(initilization_func ) and module_exit(cleanup_func). The module_init is used to load the module into the kenel and module_exit is used to unload the module from the kernel.For module_init and module_exit we need two header files, which are and .After loading and unloading our module next step is to register our driver. The registration of driver is done in two ways i.e. dyanamic registration and static registration.The dyanamic registration is done by using alloc_chrdev_region.The alloc_chrdev_region(&dev,minorno,nod,dev_name) take four arguments,first argument is the pointer to dev ,second argument is the minor number ,third argument is the number of devices for which we have register our driver and fourth argument is our device name.we used dynamic registration so that the kernel provieded maximum available major no to our device.The static registration is done by using register_chrdev_region(dev,nod,dev_name);.it will return maximum available major no to our device. After the registration of our device we have to unregister our device using unregister_chrdev_region(dev,nod).for registe and unregister we used header file . After registeration and unregistration we need to prepare Scull memory for our device.For preparing scull memory we define struct Sculldev. Struct Sculldev contain scullqset which points to the next scullqset. The scullqset points to the qset and qset points to the quantums.After preparing the scull we have to initilize the scull by using cdev_init and cdev_add.
After preparing scull we perform file operations like open,close ,read and write.finally i have done my char driver with lseek operation.