EmbLogic's Blog

device driver

Gud day sir,
Device driver,the name,the project,which we all were happy to start with because of easy in looking the functionality but same with compexity internally.on the very first day of the project,got a brief introduction on the topic. the second day was the main because on that day the internal structure of the device driver came into picture and unfortunately i was late and didnot attend the lecture .but thanks to Hemant ,who briefed me all so well that i felt it was taught by sir himself .
REGISTRATION AND UNREGISTRATION
In the beginning of programming ,we did the registration of our driver by ALLOC_CHRDEV_REGION function and got the MAJOR NO. for our driver.MAJOR NO. is a no. specifically allocated to a specific driver dynamically(if using ALLOC_CHRDEV REGION) and if we want to allocate some major no. that is free in the kernel then we can use the function REGISTER_CHRDEV_REGION.the no. got after registration is stored in dev function which is in struct inode which is the internal structure created by kernel regarding driver.registration is done in ENTRY FUNCTION which is the first to open.
then after registration we allocate memory for the user defined structure(sculldev) i.e the predefined copy for the device or application that we will open by this driver,having the same memory as that of opened application for read and write operation by kmalloc(defined in slab.h).
then we initialized the cdev structure which is defined in cdev.h by initialized all its arguments.the cdev structure is the structure which is used to get interface of our driver with the kernel.after intialization we add the cdev in kernel by cdev_add function.
we get a minor no. when we open a file or application through our driver (least possible value starting from 0).
this makes our driver inserted into kernel.
now comes the unregistration.this is done in EXIT function everything we did in entry function will be reversed here.deallocation of sculldev by kfree function,cdev_del and unregister_chrdev_region.

OPEN FUNCTION
the prototype of this function is defined in struct file_op in fs.h.we did 3 functions in this
1. container_of()–we take three arguments in this .first one is the pointer to a field (2nd argument) of type (3rd argument)container field within the structure of type container type and returns a pointer to the contained structure.
2. Storage of the pointer in the private_data in struct file.
3. Trimming if opened in write operations.
Release function
this is just the reverse of open .as such we did nothing to release but we are supposed to do opposite what we did in open function.
Write function
then comes the write function. That is to write in the sculldev ,user defined copy of the application,from the application openned. Writing all done by the function called copy_from_user.
But before this we did the allocation part that is making of scullqsets ,having the qsets having the pointers to the quantoms. The structure is just like the linked list . The sculls have the pointer points to the next scull and so on.
copy_from_user() having three arguments destination,source and the bytes to write in.source is the user buffer and the destination is the quantums.
Read function
whatever we write in quantums is time to write in the user buffer. The used function is copy_to_user. The function is same as copy_from_user.

lseek function

the function to implement is to change the file_position ,means to write and read in the quantums from the desired position by the application user. it is done by llseek function having three arguments,storing the value in struct file’s f_pos which is further used to change the position to write.

sir,i have done till the write lseek part but there is a problem in writing above 64 bytes and offset value for seeking is above 64 bytes. the writing is not done after 204 bytes including offset but after that writing error occurred instead of having left quantums available. still working on it . sorry if i am not up to date .i will recover soon.

thanks

prateek

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>