/*-
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Device would write only in READ_WAITING state and then update the state to
* WRITE_WAITING.
*/
int
pollpal_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
int flag)
{
pal_t *pl = fp->f_data;
int error;
error = 0;
/* To ensure that only single process can write in device at a time. */
mutex_enter(&pl->lock);
cv_broadcast(&pl->sc_cv);
switch (pl->sc_state) {
case READ_WAITING:
pl->sc_state = WRITE_WAITING;
selnotify(&pl->psel, POLLOUT | POLLWRNORM, 0);
while (pl->sc_state == WRITE_WAITING) {
if (pl->buf) {
error = cv_wait_sig(&pl->sc_cv, &pl->lock);
if (error)
goto ret;
}
pl->buf_len = uio->uio_iov->iov_len;
pl->buf = kmem_alloc(pl->buf_len, KM_SLEEP);
uiomove(pl->buf, pl->buf_len, uio);
printf("Use cat to know the result.\n");
break;
}
break;
case WRITE_WAITING:
printf("State: WRITE_WAITING\n");
break;
}
ret:
mutex_exit(&pl->lock);
return error;
}
/*
* Device would read only in WRITE_WAITING state and then update the state to
* READ_WAITING.
*/
int
pollpal_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
int flags)
{
pal_t *pl = fp->f_data;
int error;
error = 0;
/* To ensure that only single process can read device at a time. */
mutex_enter(&pl->lock);
cv_broadcast(&pl->sc_cv);
switch (pl->sc_state) {
case READ_WAITING:
printf("State: READ_WAITING\n");
printf("You need to write something to the module first!\n");
goto ret;
case WRITE_WAITING:
pl->sc_state = READ_WAITING;
selnotify(&pl->psel, POLLIN | POLLRDNORM, 0);
while (pl->sc_state == READ_WAITING) {
if (!pl->buf) {
error = cv_wait_sig(&pl->sc_cv, &pl->lock);
if (error)
goto ret;
}
printf("The string you entered was: ");
uiomove(pl->buf, pl->buf_len, uio);
printf("\n");
if (check_pal(pl->buf, pl->buf_len))
printf("String '%s' was a palindrome.\n",
pl->buf);
else
printf("String '%s' was not a palindrome.\n",
pl->buf);
int
pollpal_poll(struct file *fp, int events)
{
pal_t *pl = fp->f_data;
int revents;
revents = 0;
mutex_enter(&pl->lock);
switch (pl->sc_state) {
case READ_WAITING:
if (events & (POLLOUT | POLLWRNORM)) {
/* When device is in READ_WAITING state it can write */
revents |= POLLOUT | POLLWRNORM;
} else {
/* Record the request if it wasn't satisfied. */
selrecord(curlwp, &pl->psel);
}
break;
case WRITE_WAITING:
if (events & (POLLIN | POLLRDNORM)) {
/* When device is in WRITE_WAITING state it can read. */
revents |= POLLIN | POLLRDNORM;
} else {
/* Record the request if it wasn't satisfied. */
selrecord(curlwp, &pl->psel);
}
break;
}
mutex_exit(&pl->lock);
return revents;
}
MODULE(MODULE_CLASS_MISC, pollpal, NULL);
static int
pollpal_modcmd(modcmd_t cmd, void *arg __unused)
{
int cmajor = 351, bmajor = -1;
switch (cmd) {
case MODULE_CMD_INIT:
if (devsw_attach("pollpal", NULL, &bmajor, &pollpal_cdevsw,
&cmajor))
return ENXIO;
return 0;
case MODULE_CMD_FINI:
if (pollpal_nopen != 0)
return EBUSY;
devsw_detach(NULL, &pollpal_cdevsw);
return 0;
default:
return ENOTTY;
}
}