Subject: device driver questions
To: None <current-users@netbsd.org>
From: Brook Milligan <brook@biology.nmsu.edu>
List: current-users
Date: 10/04/2001 09:13:03
I am writing a device driver for the first time and have a few
design-type questions.  The device in question communicates with the
host in two ways.

First, if the host initiates the exchange there is an alternation of
bytes to and from the device.  Each time the device has a byte to
send, it generates an interrupt.  To handle this, I am designing the
driver in the following way:

- set a flag indicating that a response is expected
- write a byte to the device
- enter an idle loop until a response is available (as indicated by a
  flag set in the interrupt handler)
- continue until the exchange is done or quit if the idle loop times
  out

The interrupt handler does the following:

- check if an interrupt is expected; if not, handle case 2 below somehow
- read the response byte from the device
- set a flag indicating that a response is ready; this flag is checked
  in the idle loop above
- exit

This seems rather straightforward but, especially since I am new to
device driver design and interrupt handling and kernel programming, I
am open to comments on whether this is a reasonable design and on how
to improve it.

Second, the device can initiate an exchange with the host.  In this
case, an interrupt will occur when none is expected, an exchange of
bytes must follow (again with expected interrupts for each response
from the device), and the host may be required to take some
significant action as a result of the exchange.  I am much less clear
on how to handle this situation.

Clearly, the interrupt handler will know the difference between an
expected interrupt (case 1 or in case 2 for bytes after the first) and
an unexpected interrupt (first byte in case 2).  The question is, how
should the interrupt handler deal with the exchange required in
response to an unexpected interrupt and how should it initiate any
subsequent actions by the host?  The interrupt handler can indicate in
some driver-specific flag that such an interrupt has occurred and
needs handling, but how will control be passed to some routine that
can deal with it?

Any pointers on how to deal with this are welcome.  Also welcome are
pointers to existing drivers that do something similar (but are not
too complex too understand).  Finally, any help in understanding how
interrupt processing works would probably get me going in the right
direction.

Thanks for any help you can offer.

Cheers,
Brook