EmbLogic's Blog

CHARACTER DEVICE DRIVER

CHARACTER DEVICE DRIVER

 

Device driver is a software implemented part of the kernel to provide a interface or interaction between hardware and file system , Device driver runs in kernel space as a part of kernel , which can be load and unload on demand , so these type of device driver are known as dynamically loaded modules.

Driver can be insert in kernel as a module using insmod and can be remove using rmmod these are two basic commands. Though in this article i am going to discuss about char driver that i have implemented.

Implementation :

In my character driver first of all i registered my char driver using

int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);

Unregistered using the follwing function –

void unregister_chrdev_region(dev_t, unsigned);

Till here my driver got least available major number allocated by kernel you can see by doing

cat /proc/devices after inserting the lkm. Now my next task was to add a number devices to my driver and provide them minor number i done this using macro called MKDEV(MAJOR, MINOR). So till here i have allocated major and minor number and registered my char driver with my kernel .

My next task was to create an application in user space and performing read write operation by that application at a specific node that was created by me in file system using node creating command .

mknod node c mjorno minorno

this will create a character driver node in user space our application will be performing read and write operation on this node .

Mapping System calls :

scull_open :

this was the main task to map the read and write operation of application to our driver so when the application gives a system call open() , then our mapped open starts and return a file descriptor to the application .

int scull_open(struct inode *inodep, struct file *filep) ;

 

scull_read :

After opening the device we need to perform read peration on that device so we need to map

our read and write also by using this callback function.

ssize_t scull_read(struct file *filep, char __user *ubuff, size_t size, loff _t *loff) ;

 

scull_write :

After opening the device we need to perform write operation on that device so we need to map

our read and write also by using this callback function.

ssize_t scull_write(struct file *filep, char __user *ubuff, size_t size, loff _t *loff) ;

 

scull_release :

When the application give the call to close the file descriptor then our mapped callback function starts.

int scull_release(struct inode *inodep, struct file *filep) ;

 

Other operations :

I implemented some more operations like ioctl operation used for debugging perpose :

long scull_ioctl(struct file *, unsigned int, unsigned long) ;

And lseek opeartion were also implemented

loff_t scull_llseek(struct file *, loff_t, int);

Device for operations :

Since we were not having any device so for that I have created a memory area on RAM that

was tread as a device memory , I named it SCULL .

Thank YOU

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>