Device driver question: How to implement semaphore in driver?

Device driver question: How to implement semaphore in driver?

Post by Tony Denau » Wed, 19 Jun 1996 04:00:00



I am tring to develop/port a device driver for a motor controller in linux.

I need help implementing a semaphore type of operation within the device driver.
My driver  needs to communication with the board by passing characters strings
to the board & reading replys (if any). This operation needs to be
atomic on the process level. For example, if two process (A&B) are requesting
information, process A need to send , wait, read as a single operation
before process B is allowed to talk to the board.

So I need some sort of semephore facility in my device driver. Here is a
first cut at some code. If anyone has already done this, could you please
post/send me your code. I am just starting developing in linux and could
use some help! Thanks,

To use it, I would:

1. Initialize structure in the init() function:

   struct sem_t sem;

   sem->cnt = 1;            /* only one resource available */
   sem->wait_queue = NULL;

2. Whenever I need to 'talk' to my device:

   sem_ret( &sem );   /* block until device is free */
   . . .              /* my code */
   sem_ret( &sem )    /* free device for others */

3. Some source code is posted below.
   I don't think it is perfect. In sem_get(), if multiple task
   are waiting on a single queue, a call to sem_ret() will
   wake them all up and the next one to get the 'resource' will
   depend on the scheduler. (no guarantee FIFO). Other than this
   is should work, I'll try to test it soon. Please comment.

Tony

Here the code:

struct sem_t  {
   int cnt;                       /* num of resources available */
   struct wait_queue *wait_queue; /* wait queue when blocked */

Quote:};

/*************************************************************************
** Semaphore-type blocking function
**
**  sem_get( struct sem_t *sw) - will block until it is able to get
**                               a free resource.
**  sem_ret( struct sem_t *sw) - return the resoruce & waits up any task
**                               blocked in sem_get().
**************************************************************************/

void sem_get( struct sem_t * sw )
{
   int got_it;  /* flag (T or F) to indicate when we grabbed the resoure */

   /* loop until we are able to grab a resource */
   got_it = 0;
   do {

      cli();
      if( sw->cnt > 0 ) /* if resource available */
      {
         got_it = 1; /* set to true, to exit */
         sw->cnt--;  /* decrement to indicate we grabbed one */
      }
      sti();

      /* didn't get one, lets sleep until someone free a resource */
      if( !got_it )
         interruptible_sleep_on( &sw->wait_queue );

   } while ( !got_it );

Quote:}

void sem_ret( struct sem_t * sw )
{
   cli();
   sw->cnt++;  /* increment counter to 'return' resource */
   sti();

   /* wake up other waiting on resource */
   wake_up_interruptible( &sw->wait_queue );

Quote:}

--

Institute for Astronomy           Phone: (808) 956-8101
2680 Woodlawn Drive,Honolulu, HI    Fax: (808) 988-3893
 
 
 

1. Device driver question (generic device driver)

Hi, I post this for a colleague, who is unable to post here, so please

Herbert
Now his questions:

Hello,
I'm currently involved in writing a generic device driver, that
implements some common functions for other real drivers. My problem is:
How can I make sure that the generic driver is loaded first (before the
real driver modules)? Is there any order besides the order in the
/etc/system-file?
How does the OS loads it's own generic driver-modules?
Is it on demand ?
If yes, how is the "demanding" encoded, and where (Is there a defaults
file for this ? Is it hardcoded or can it be configured?) ?

Any help appreciated!
Thanks in advance for your replies!

Yours,

Christian

_______________________________________________________________________

"Now's the time to impress not suppress your intellect, hold a gun to
 your head, if your mind's dead, blow away the cobweb" TEST DEPT; "bang
 on it" (Metal Edit) CD LEGACY (1990-1993), freud cd 047, efa 75230-2

2. System msg's keep popping up. how do i turn off?

3. Device driver calling another device driver.

4. Solaris, DHCP, and the lost interface ?

5. implementing layered device driver

6. I need a box-drawing font (or a font editor(

7. Implementing Key Board device driver

8. IDE error on 2.4.17

9. Implementing select() in an asynchronous device driver. Impossible!

10. How to implement DEVICE DRIVER in Linux?

11. How to implement runtime loadable device drivers (written in C)

12. Looking for driver for a driver for devices.mca.8ee6.

13. My first device driver, problems, devfsadm: driver failed to attach: