'\" te
.\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 1989 AT&T
.\" Copyright 2017 Joyent, Inc
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH CHPOLL 9E "Jan 18, 2017"
.SH NAME
chpoll \- poll entry point for a non-STREAMS character driver
.SH SYNOPSIS
.LP
.nf
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>



\fBint prefix\fR\fBchpoll\fR(\fBdev_t\fR \fIdev\fR, \fBshort\fR \fIevents\fR, \fBint\fR \fIanyyet\fR,
     \fBshort *\fR\fIreventsp\fR, \fBstruct pollhead **\fR\fIphpp\fR);
.fi

.SH INTERFACE LEVEL
.LP
This entry point is optional. Architecture independent level 1 (DDI/DKI).
.SH PARAMETERS
.ne 2
.na
\fB\fIdev\fR\fR
.ad
.RS 12n
The device number for the device to be polled.
.RE

.sp
.ne 2
.na
\fB\fIevents\fR\fR
.ad
.RS 12n
The events that may occur. Valid events are:
.sp
.ne 2
.na
\fB\fBPOLLIN\fR\fR
.ad
.RS 14n
Data other than high priority data may be read without blocking.
.RE

.sp
.ne 2
.na
\fB\fBPOLLOUT\fR\fR
.ad
.RS 14n
Normal data may be written without blocking.
.RE

.sp
.ne 2
.na
\fB\fBPOLLPRI\fR\fR
.ad
.RS 14n
High priority data may be received without blocking.
.RE

.sp
.ne 2
.na
\fB\fBPOLLHUP\fR\fR
.ad
.RS 14n
A device hangup has occurred.
.RE

.sp
.ne 2
.na
\fB\fBPOLLERR\fR\fR
.ad
.RS 14n
An error has occurred on the device.
.RE

.sp
.ne 2
.na
\fB\fBPOLLRDNORM\fR\fR
.ad
.RS 14n
Normal data (priority band = 0) may be read without blocking.
.RE

.sp
.ne 2
.na
\fB\fBPOLLRDBAND\fR\fR
.ad
.RS 14n
Data from a non-zero priority band may be read without blocking
.RE

.sp
.ne 2
.na
\fB\fBPOLLWRNORM\fR\fR
.ad
.RS 14n
The same as \fBPOLLOUT\fR.
.RE

.sp
.ne 2
.na
\fB\fBPOLLWRBAND\fR\fR
.ad
.RS 14n
Priority data (priority band > 0) may be written.
.RE

.sp
.ne 2
.na
\fB\fBPOLLET\fR\fR
.ad
.RS 14n
The desired event is to be edge-triggered; calls to \fBpollwakeup\fR(9F)
should not be suppressed, even if the event is pending at the time of
call to the \fBchpoll()\fR function.
.RE

.RE

.sp
.ne 2
.na
\fB\fIanyyet\fR\fR
.ad
.RS 12n
A flag that is non-zero if any other file descriptors in the \fBpollfd\fR array
have events pending. The \fBpoll\fR(2) system call takes a pointer to an array
of \fBpollfd\fR structures as one of its arguments. See the \fBpoll\fR(2)
reference page for more details.
.RE

.sp
.ne 2
.na
\fB\fIreventsp\fR\fR
.ad
.RS 12n
A pointer to a bitmask of the returned events satisfied.
.RE

.sp
.ne 2
.na
\fB\fIphpp\fR\fR
.ad
.RS 12n
A pointer to a pointer to a \fBpollhead\fR structure.
.RE

.SH DESCRIPTION
.LP
The \fBchpoll()\fR 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:
.RS +4
.TP
1.
Implement the following algorithm when the \fBchpoll()\fR entry point is
called:
.sp
.in +2
.nf
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);
.fi
.in -2

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

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

.RE
.RS +4
.TP
2.
Allocate an instance of the \fBpollhead\fR structure. This instance may be
tied to the per-minor data structure defined by the driver. The \fBpollhead\fR
structure should be treated as a "black box" by the driver. Initialize the
\fBpollhead\fR structure by filling it with zeroes. The size of this structure
is guaranteed to remain the same across releases.
.RE
.RS +4
.TP
3.
Call the \fBpollwakeup()\fR function with \fBevents\fR listed above whenever
pollable \fBevents\fR which the driver should monitor occur. This function can
be called with multiple events at one time. The \fBpollwakup()\fR can be called
regardless of whether or not the \fBchpoll()\fR 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 \fBpollwakeup\fR(9F) that is acquired in its
\fBchpoll()\fR entry point, or a deadlock may result.  Note that if
\fBPOLLET\fR is set in the specified events, the driver must call
\fBpollwakeup\fR(9F) on subsequent events, even if events are pending at
the time of the call to \fBchpoll()\fR.

.RE
.RS +4
.TP
4.
In the \fBclose\fR(9E) entry point, the driver should call \fBpollwakeup()\fR
on the \fBpollhead\fR structure that corresponds to the closing software
state, specifying \fBPOLLERR\fR for the events.  Further, upon return from
\fBpollwakeup()\fR, the driver's \fBclose\fR(9E) entry point should call
the \fBpollhead_clean\fR(9F) function, specifying the \fBpollhead\fR that
corresponds to the structure that will be deallocated:

.sp
.in +2
.nf
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);
      ...
.fi
.in -2

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

.RE
.SH RETURN VALUES
.LP
\fBchpoll()\fR should return \fB0\fR for success, or the appropriate error
number.
.SH SEE ALSO
.LP
\fBpoll\fR(2), \fBepoll\fR(5), \fBnochpoll\fR(9F), \fBpollwakeup\fR(9F)
.sp
.LP
\fIWriting Device Drivers\fR