device driver: using poll to wait for an event

device driver: using poll to wait for an event

Post by Alex Raimond » Sat, 23 Nov 2002 23:34:01



Hi

I'm new to writing device drivers ;-)

I've got a device connected to my cpu which can arise an interrupt if it
wants
attention.
I succeded in getting an interrupt....

My problem is: How can I tell any user application that the device needs
attention.
First off all I thought of implementing the poll function.
I found some resources on the web telling me about this function, but I
can't get behind it (although it must be simple).

Some code:

unsigned int scull_p_poll(struct file *filp, poll_table *wait)
{
  unsigned int mask = 0;
 ...

  poll_wait(filp, &dev->inq, wait);
  poll_wait(filp, &dev->outq, wait);

  if ( isReadable() ) mask |= POLLIN | POLLRDNORM; /* readable */
  if ( isWritable() ) mask |= POLLOUT | POLLWRNORM; /* writable */
  return mask;

Quote:}

inq and outq are defined elsewhere.

Am'I right when mask is return as 0 (neither readable nor writeable) the
process blocks until the inq or outq is waked up?

But can I use this poll in my case? I don't have blocking read or write.
(The device can allways be read from or written to but its only interesting
after the device arised an interrupt.

The other thing I thought of was to implement an ioctl command, which puts
my process to sleep until an irq occours. Is this allowed and a good
behaviour of a device driver?

Thanks for any hint!

Alex

 
 
 

device driver: using poll to wait for an event

Post by Sundial Service » Sun, 24 Nov 2002 01:02:42



> I'm new to writing device drivers ;-)

More or less, everyone [still] is.  ;-)

Quote:> I've got a device connected to my cpu which can arise an interrupt if it
> wants attention.
> I succeded in getting an interrupt....

Yippeee!!!  ;-)  No, seriously, that in itself is an accomplishment.

Quote:> My problem is: How can I tell any user application that the device needs
> attention.
> First off all I thought of implementing the poll function.
> I found some resources on the web telling me about this function, but I
> can't get behind it (although it must be simple).

The "poll" functions are really not intended (IMHO) to "tell the user app"
that a device needs attention.  Normally, Unix signals are used for this
purpose.  The process issues a "poll" call to determine what has happened.

Quote:> Some code:

You don't say whether this is your original code, or code that you found.

Quote:

> unsigned int scull_p_poll(struct file *filp, poll_table *wait)
> {
>   unsigned int mask = 0;
>  ...

>   poll_wait(filp, &dev->inq, wait);
>   poll_wait(filp, &dev->outq, wait);

>   if ( isReadable() ) mask |= POLLIN | POLLRDNORM; /* readable */
>   if ( isWritable() ) mask |= POLLOUT | POLLWRNORM; /* writable */
>   return mask;
> }

> inq and outq are defined elsewhere.

Start by looking at the source:  include/linux/poll.h, where "poll_wait" is
defined, and fs/select.c, which defines "__pollwait."  The gist of the wait
routine is to place an entry on a wait-queue ... where it is blocked until
somebody decides to "run" that queue.  You're calling a low-level routine
here.

Quote:> Am'I right when mask is return as 0 (neither readable nor writeable) the
> process blocks until the inq or outq is waked up?

You are issuing two wait-calls in a row.  So, I believe, you will wait for
both queues.  The code to determine the return-value is distinct from this.

Quote:> But can I use this poll in my case? I don't have blocking read or write.
> (The device can allways be read from or written to but its only
> interesting after the device arised an interrupt.
> The other thing I thought of was to implement an ioctl command, which puts
> my process to sleep until an irq occours. Is this allowed and a good
> behaviour of a device driver?

What you want to do, Alex, is to provide an "ioctl" call that allows the
client to define a signal that it wishes to receive when the device
requires attention.  The process will then wait upon that signal.  Each
time it arrives, the process will presumably use "poll" to determine what
has happened.  [It ought to be able to determine, without waiting, that
"nothing has happened," because the delivery and acceptance of signals is
asynchronous and timing-holes must be allowed for.]

If you think it's convenient to provide an all-in-one ioctl call that puts
the process to sleep you could of course do that.

 
 
 

device driver: using poll to wait for an event

Post by Arne Driesche » Wed, 27 Nov 2002 17:37:56



>>>---snip---<<<
> But can I use this poll in my case? I don't have blocking read or write.
> (The device can allways be read from or written to but its only interesting
> after the device arised an interrupt.

poll is intended to determine if read or write would block. So, useres would
expect to use it that way. If you don't have blocking I/O don't use poll.

Quote:> The other thing I thought of was to implement an ioctl command, which puts
> my process to sleep until an irq occours. Is this allowed and a good
> behaviour of a device driver?

That is frequently done in simple device drivers and I cannot see any harm in it.
However, the result still is that your program will block. Putting a waitq
in an ioctl call is the same amount of work as putting it in your read
system call. What is wrong with a blocking read/write? The result would be
more what a user might expect and it is even faster because you don't need
2 system calls.
If the blocking is the problem, try to ask google about asynchronous IO.

-Arne

Quote:> Thanks for any hint!
> Alex

 
 
 

1. device driver: using poll to wait for an event

Hi

I'm new to writing device drivers ;-)

I've got a device connected to my cpu which can arise an interrupt if it
wants
attention.
I succeded in getting an interrupt....

My problem is: How can I tell any user application that the device needs
attention.
First off all I thought of implementing the poll function.
I found some resources on the web telling me about this function, but I
can't get behind it (although it must be simple).

Some code:

unsigned int scull_p_poll(struct file *filp, poll_table *wait)
{
  unsigned int mask = 0;
 ...

  poll_wait(filp, &dev->inq, wait);
  poll_wait(filp, &dev->outq, wait);

  if ( isReadable() ) mask |= POLLIN | POLLRDNORM; /* readable */
  if ( isWritable() ) mask |= POLLOUT | POLLWRNORM; /* writable */
  return mask;

inq and outq are defined elsewhere.

Am'I right when mask is return as 0 (neither readable nor writeable) the
process blocks until the inq or outq is waked up?

But can I use this poll in my case? I don't have blocking read or write.
(The device can allways be read from or written to but its only interesting
after the device arised an interrupt.

The other thing I thought of was to implement an ioctl command, which puts
my process to sleep until an irq occours. Is this allowed and a good
behaviour of a device driver?

Thanks for any hint!

Alex

2. SUID

3. fork,exec and wait and wait and wait and wait

4. Solaris cpio -H bar compatability option for old bar written tapes - Help TIA!!

5. Poll()/select() device driver - new version available

6. XEmacs: (xim-xlib/warning) Can't set locale.

7. 8/25/92 CLB EVENT NOTICE - ZEN AND THE ART OF DEVICE DRIVER WRITING -

8. newbie question on bind ?

9. Polling device driver

10. My Freebsd driver hangs in poll function after some time (problem with poll)

11. polling device drivers?

12. How many times does the poll method in device driver get called for each application select?

13. ? How do I find what device driver a device is using?