Results 1 to 4 of 4
Hi in the below code can anybody explain why this calculation is required heilighted in red colour,please dont hesitate to explain i will be happy and thankful to your answer ...
- 06-02-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 9
got struck with device driver calculations ..in orelly book
Hi in the below code can anybody explain why this calculation is required heilighted in red colour,please dont hesitate to explain i will be happy and thankful to your answer .when i am getting cursor position as argument why this is required again ,i got struck at this position.
ssize_t scull_read(struct file *filp, char *buf, size_t count,
loff_t *f_pos)
{
Scull_Dev *dev = filp->private_data; /* the first listitem */
Scull_Dev *dptr;
int quantum = dev->quantum;
int qset = dev->qset;
int itemsize = quantum * qset; /* how many bytes in the listitem */
int item, s_pos, q_pos, rest;
ssize_t ret = 0;
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
if (*f_pos >= dev->size)
goto out;
if (*f_pos + count > dev->size)
count = dev->size - *f_pos;
/* find listitem, qset index, and offset in the quantum */
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum; q_pos = rest % quantum;
/* follow the list up to the right position (defined elsewhere) */
dptr = scull_follow(dev, item);
if (!dptr->data)
goto out; /* don't fill holes */
if (!dptr->data[s_pos])
goto out;
/* read only up to the end of this quantum */
if (count > quantum - q_pos)
count = quantum - q_pos;
if (copy_to_user(buf, dptr->data[s_pos]+q_pos, count)) {
ret = -EFAULT;
goto out;
}
*f_pos += count;
ret = count;
out:
up(&dev->sem);
return ret;
}
- 06-02-2008 #2Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
scull is a linked list of nodes. Each node has an list of small quantums. Number of quantum is defined in the program.
So item is just used to calculate the node number, Then quantum number is calculated for the node(item) , and then the position inside the quantum as q_pos.
The user applicaton will open the device and give the position, but you have actually reach the position inside the device to perform the activity(read/write) requested.
- 06-02-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 9
please confirm me wether i understood or not
Initially i will have pointer to first quantum and first quantum size and current array size right.and then i will get f_pos of unknown quantum,based on these things i need to find the node and current position of cursor of that particular node right for read or write is this correct,please correct me if i am wrong
- 06-03-2008 #4Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
The user can start writing anywhere within the file. ok so you can get any position of file to work with. So in your driver you have to file that position as you are implementing the device.
You are havinf pointer void **data. Which is a pointer to list of pointers (each pointer points to a quantum).


Reply With Quote
