CHPOLL(9E) Driver Entry Points CHPOLL(9E)


NAME


chpoll - poll entry point for a non-STREAMS character driver

SYNOPSIS


#include <sys/types.h>
#include <sys/poll.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>


int prefixchpoll(dev_t dev, short events, int anyyet,
short *reventsp, struct pollhead **phpp);


INTERFACE LEVEL


This entry point is optional. Architecture independent level 1 (DDI/DKI).

PARAMETERS


dev
The device number for the device to be polled.


events
The events that may occur. Valid events are:

POLLIN
Data other than high priority data may be read
without blocking.


POLLOUT
Normal data may be written without blocking.


POLLPRI
High priority data may be received without
blocking.


POLLHUP
A device hangup has occurred.


POLLERR
An error has occurred on the device.


POLLRDNORM
Normal data (priority band = 0) may be read
without blocking.


POLLRDBAND
Data from a non-zero priority band may be read
without blocking


POLLWRNORM
The same as POLLOUT.


POLLWRBAND
Priority data (priority band > 0) may be
written.


POLLET
The desired event is to be edge-triggered;
calls to pollwakeup(9F) should not be
suppressed, even if the event is pending at the
time of call to the chpoll() function.


anyyet
A flag that is non-zero if any other file descriptors in the
pollfd array have events pending. The poll(2) system call
takes a pointer to an array of pollfd structures as one of
its arguments. See the poll(2) reference page for more
details.


reventsp
A pointer to a bitmask of the returned events satisfied.


phpp
A pointer to a pointer to a pollhead structure.


DESCRIPTION


The chpoll() entry point routine is used by non-STREAMS character device
drivers that wish to support polling. The driver must implement the
polling discipline itself. The following rules must be followed when
implementing the polling discipline:

1. Implement the following algorithm when the chpoll() entry
point is called:

if (specified_events_are_satisfied_now) {
*reventsp = satisfied_events & events;
} else {
*reventsp = 0;
}
if ((*reventsp == 0 && !anyyet) || (events & POLLET))
*phpp = &my_local_pollhead_structure;
return (0);

Note: Prior to the integration of epoll(5), which included
edge-triggering via the POLLET flag, standard chpoll
mechanisms would only provide a pollhead in phpp if there were
no matching events. Edge-triggered polling requires that
pollwakeup() always be called for a resource, so if POLLET is
set in the events of interest, the chpoll method must yield a
pollhead and prepare to issue pollwakeup() calls on it.

Drivers which are not wired up to make pollwakeup() calls on a
pollhead when their status changes should emit one from their
chpoll routine. This will exclude the resource from caching
by pollers, since it cannot alert them to new events without
pollwakeup() notification.


2. Allocate an instance of the pollhead structure. This instance
may be tied to the per-minor data structure defined by the
driver. The pollhead structure should be treated as a "black
box" by the driver. Initialize the pollhead structure by
filling it with zeroes. The size of this structure is
guaranteed to remain the same across releases.

3. Call the pollwakeup() function with events listed above
whenever pollable events which the driver should monitor
occur. This function can be called with multiple events at one
time. The pollwakup() can be called regardless of whether or
not the chpoll() entry is called; it should be called every
time the driver detects the pollable event. The driver must
not hold any mutex across the call to pollwakeup(9F) that is
acquired in its chpoll() entry point, or a deadlock may
result. Note that if POLLET is set in the specified events,
the driver must call pollwakeup(9F) on subsequent events, even
if events are pending at the time of the call to chpoll().


4. In the close(9E) entry point, the driver should call
pollwakeup() on the pollhead structure that corresponds to the
closing software state, specifying POLLERR for the events.
Further, upon return from pollwakeup(), the driver's close(9E)
entry point should call the pollhead_clean(9F) function,
specifying the pollhead that corresponds to the structure that
will be deallocated:


static int
mydriver_close(dev_t dev, int flag, int otyp, cred_t *cp)
{
minor_t minor = getminor(dev);
mydriver_state_t *state;

state = ddi_get_soft_state(mydriver_softstate, minor);

pollwakeup(&state->mydriver_pollhd, POLLERR);
pollhead_clean(&state->mydriver_pollhd);
...

This step is necessary to inform other kernel subsystems that
the memory associated with the pollhead is about to be
deallocated by the close(9E) entry point.


RETURN VALUES


chpoll() should return 0 for success, or the appropriate error number.

SEE ALSO


poll(2), epoll(5), nochpoll(9F), pollwakeup(9F)


Writing Device Drivers


January 18, 2017 CHPOLL(9E)