1

I am going through LDD from Rubini to learn driver programming.Currently, I am going through 3rd chapter - writing character driver "scull". However, In the example code provided by the authors, I am not able to understand the following lines in scull_read() and scull_write() methods :

item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum; 
q_pos = rest % quantum; 

I have spent quite a time on it in vain( and still working on it) . Can someone please help me understand the functionality of the above code snippet??

Regards,

Roy

Peter L.
  • 1,041
  • 2
  • 22
  • 26
rstnsrrao
  • 43
  • 6
  • What do you not understand? The meaning of the operators? Operator precedence? Typecasting? – Peter Sep 23 '13 at 17:23

1 Answers1

4

Suppose you have set quantum area size to 4000 bytes in scull driver and qset array size to 10. In that case, value of itemsize would be 40000. f_pos is a position from where read/write should start, which is coming as a parameter to read/write function. suppose read request has come and f_pos is 50000.

Now, item = (long)*f_pos / itemsize; so item would be 50000/40000 = 1

rest = (long)*f_pos % itemsize; so rest would be 50000%40000 = 10000

s_pos = rest / quantum; so s_pos would be 10000/4000 = 2

q_pos = rest % quantum; so q_pos would be 10000%4000 = 2000

If you have read description of scull driver in chapter 3 carefully then each scull device is a linked list of pointers (of scull_qset) and in our case each scull_qset points to array of pointers which points to quantum area of 4000 bytes as we have set quantum area size 4000 bytes and array size in our case is 10. So, our each scull_qset is an array of 10 pointers and each pointer points to 4000 bytes. So, one scull_qset has capacity of 40000 bytes.

In our read request, f_pos is 50000, so obviously this position would not be in first scull_qset which is proven by calculation of item. As item is 1, it will point to second scull_qset(value of item would be 0 for first scull_qset, for more information see scull_follow function definition).

Value of rest will help to find out at which position in second scull_qset read should start. As each quantum area is of 4000 bytes, s_pos gives out of 10 pointers of second scull_qset which pointer should be used and qset tells that in a particular quantum area pointed by pointer found in s_pos, at which particular location read should start.

pratik
  • 434
  • 2
  • 8