How to user semephores in device drivers?

Tony Denault (denault@irmanoa1.ifa.hawaii.edu)
Tue, 24 Sep 1996 18:24:29 -1000


Hi All,

I am writing device drivers for Linux and am puzzled about the behavior
for the queue() and semephore kernel functions. I am kernel version 2.0.0

I am tring to get a MUTEX (binary) semephore to work from within a
device driver. I am writing a device driver for an 8 axis
motor controller. Since independent processes may control an individual
axis and Only one process should read/write to the controller at a time
(so we don't confuse the controller). I want to embedded the semephore
control inside the driver.

I know atomic operation can be done using cli(), but communication with
board could take up to many miliseconds. I just need to guard access to
resource, queuing user processes, AND serving them in a First-In-First-Out
method. Only processes waiting for the devices should block.

1. First I tried using add_wait_queue() and remove_wait_queue, but
remove_wait_queue() seems to remove ALL the processes waiting on a single
queue.

2. After poking around some more, I tried using the down(&sem) amd up(&sem).
To test this, I wrote somethime like this:

static int Sem = MUTEX;

down( &Sem );
printk("sem - got it. %d\n", id);
/*.... do some processing */
up( &Sem );
printk("sem - up() %d\n", id);

if I have 2 processes, A & B, making repeated calls to this iocntl()
is seem one would continuously lock and release the semephore. So,
if the both made repeated iocntl() call, task B would be waiting on
the queue, A would release the sem by calling up(), exit & re-enter
the driver, and lock the semephore before B. I even tried putting a
schedule() call after up(). But same behavior.

Is there a way to get a MUTEX (binary) semephore which release queued
processes in a FIFO order. This would seem like a need kernel service
for device driver writers.

Any suggestions / Comments?

Tony

/-----------------------------------------------------------------------------\
| Tony Denault | email: denault@irtf.ifa.hawaii.edu |
| Institute for Astronomy | Phone: (808) 956-8101 |
| 2680 Woodlawn Drive,Honolulu, HI 96822 | Fax: (808) 988-3893 |
\-----------------------------------------------------------------------------/