Home » Linux » How can I pause for 100+ milliseconds in a linux driver module?

How can I pause for 100+ milliseconds in a linux driver module?

Posted by: admin January 30, 2018 Leave a comment

Questions:

I’m writing a kernel driver for a device that produces regular amounts of data for reading periodically. The user space program is ideally suited to making this a blocking driver.

What methods are available for pausing anywhere from 4 to 100ms in a driver (i.e. doing the “block“)? In user space I’d do something akin to:

tv.tv_sec  = microsecond_delay / 1000000ul;
tv.tv_usec = microsecond_delay % 1000000ul;
(void)select(0, NULL, NULL, NULL, & tv);

or

gettimeofday(tv,NULL);

and compare the structures.

[Edit – my own answer]

I will be using the following code in my driver:

#include <linux/jiffies.h>
...
schedule_timeout(file->private_data->my_driver_struct.read_pause_jiffies);

Voila! I shall now test …

Answers:
#include <linux/delay.h>

...
msleep(100);
...

Questions:
Answers:

Using schedule_timeout does NOT sleep for a specified time but for a minimum specified time. If you really want to block for a specified time, you will have to use locks. Sleeping will only guarantee you a minimum time – this may not matter to you depending on much granularity you need.
But a better driver would sleep until the reader asked for more data in any case.