2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/serial/pty.c
|
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2016-2017 Gregory Nutt. All rights reserved.
|
2016-07-15 04:20:48 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
|
|
|
* COPYRIGHT OWNER 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-26 21:39:20 +02:00
|
|
|
/* TODO: O_NONBLOCK is not yet supported. Currently, the source and sink
|
2016-07-27 16:03:09 +02:00
|
|
|
* pipes are opened in blocking mode on both the slave and master so only
|
|
|
|
* blocking behavior is supported. This driver must be able to support
|
|
|
|
* multiple slave as well as master clients that may have the PTY device
|
|
|
|
* opened in blocking and non-blocking modes simultaneously.
|
2016-07-26 20:13:30 +02:00
|
|
|
*
|
|
|
|
* There are two different possible implementations under consideration:
|
|
|
|
*
|
2016-07-26 21:39:20 +02:00
|
|
|
* 1. Keep the pipes in blocking mode, but use a test based on FIONREAD (for
|
2016-07-26 20:13:30 +02:00
|
|
|
* the source pipe) or FIONSPACE (for the sink pipe) to determine if the
|
|
|
|
* read or write would block. There is existing logic like this in
|
|
|
|
* pty_read() to handle the case of a single byte reads which must never
|
|
|
|
* block in any case: Essentially, this logic uses FIONREAD to determine
|
|
|
|
* if there is anything to read before calling file_read(). Similar
|
|
|
|
* logic could be replicated for all read cases.
|
|
|
|
*
|
|
|
|
* Analogous logic could be added for all writes using FIONSPACE to
|
|
|
|
* assure that there is sufficient free space in the sink pipe to write
|
|
|
|
* without blocking. The write length could be adjusted, in necceary,
|
|
|
|
* to assure that there is no blocking.
|
|
|
|
*
|
|
|
|
* Locking, perhaps via sched_lock(), would be required to assure the
|
|
|
|
* test via FIONREAD or FIONWRITE is atomic with respect to the
|
|
|
|
* file_read() or file_write() operation.
|
|
|
|
*
|
|
|
|
* 2. An alternative that appeals to me is to modify the contained source
|
|
|
|
* or sink pipe file structures before each file_read() or file_write()
|
|
|
|
* operation to assure that the O_NONBLOCK is set correctly when the
|
2016-07-27 16:03:09 +02:00
|
|
|
* pipe read or write operation is performed. This might be done with
|
|
|
|
* file_vfcntl() (there is no file_fcntl(), yet) or directly into the
|
|
|
|
* source/sink file structure oflags mode settings.
|
2016-07-26 20:13:30 +02:00
|
|
|
*
|
|
|
|
* This would require (1) the ability to lock each pipe individually,
|
2016-07-27 16:03:09 +02:00
|
|
|
* setting the blocking mode for the source or sink pipe to match the
|
|
|
|
* mode in the open flags of the PTY device file structure, and (2)
|
|
|
|
* logic to restore the default pipe mode after the file_read/write()
|
|
|
|
* operation and before the pipe is unlocked.
|
2016-07-26 21:39:20 +02:00
|
|
|
*
|
|
|
|
* There are existing locks to support (1) destruction of the driver
|
2016-07-27 16:03:09 +02:00
|
|
|
* (pp_exclsem) and (2) slave PTY locking (pp_slavesem), as well as (3)
|
|
|
|
* locks within the pipe implementation. Care must be taken with any new
|
|
|
|
* source/sink pipe locking to assure that deadlocks are not possible.
|
2016-07-26 20:13:30 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2016-07-15 16:33:47 +02:00
|
|
|
#include <sys/ioctl.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
2016-07-19 14:45:02 +02:00
|
|
|
#include <sched.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <semaphore.h>
|
2016-07-16 18:43:44 +02:00
|
|
|
#include <termios.h>
|
2016-07-15 14:48:10 +02:00
|
|
|
#include <stdio.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2016-07-20 21:15:37 +02:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2016-07-15 15:18:12 +02:00
|
|
|
#include <nuttx/serial/pty.h>
|
|
|
|
|
|
|
|
#include "pty.h"
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-18 16:00:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Should never be set... only for comparison to serial.c */
|
|
|
|
|
|
|
|
#undef CONFIG_PSEUDOTERM_FULLBLOCKS
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This device structure describes on memory of the PTY device pair */
|
|
|
|
|
|
|
|
struct pty_devpair_s;
|
|
|
|
struct pty_dev_s
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
FAR struct pty_devpair_s *pd_devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
struct file pd_src; /* Provides data to read() method (pipe output) */
|
|
|
|
struct file pd_sink; /* Accepts data from write() method (pipe input) */
|
2016-07-15 15:33:48 +02:00
|
|
|
bool pd_master; /* True: this is the master */
|
2016-07-16 18:43:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
/* Terminal control flags */
|
|
|
|
|
|
|
|
tcflag_t pd_iflag; /* Terminal nput modes */
|
|
|
|
tcflag_t pd_oflag; /* Terminal output modes */
|
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the pipe pair */
|
|
|
|
|
|
|
|
struct pty_devpair_s
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
struct pty_dev_s pp_master; /* Maseter device */
|
2016-07-15 17:39:33 +02:00
|
|
|
struct pty_dev_s pp_slave; /* Slave device */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
bool pp_locked; /* Slave is locked */
|
2016-07-15 04:20:48 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2016-07-15 17:39:33 +02:00
|
|
|
bool pp_unlinked; /* File has been unlinked */
|
2016-07-15 04:20:48 +02:00
|
|
|
uint8_t pp_minor; /* Minor device number */
|
|
|
|
uint16_t pp_nopen; /* Open file count */
|
|
|
|
#endif
|
2016-07-15 17:39:33 +02:00
|
|
|
sem_t pp_slavesem; /* Slave lock semaphore */
|
|
|
|
sem_t pp_exclsem; /* Mutual exclusion */
|
2016-07-15 04:20:48 +02:00
|
|
|
};
|
|
|
|
|
2016-07-26 18:09:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pty_semtake(FAR struct pty_devpair_s *devpair);
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static void pty_destroy(FAR struct pty_devpair_s *devpair);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int pty_open(FAR struct file *filep);
|
|
|
|
static int pty_close(FAR struct file *filep);
|
|
|
|
static ssize_t pty_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t pty_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int pty_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
static int pty_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pty_unlink(FAR struct inode *inode);
|
|
|
|
#endif
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-15 22:09:02 +02:00
|
|
|
static const struct file_operations g_pty_fops =
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
pty_open, /* open */
|
|
|
|
pty_close, /* close */
|
|
|
|
#else
|
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
|
|
|
#endif
|
|
|
|
pty_read, /* read */
|
|
|
|
pty_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
pty_ioctl /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, pty_poll /* poll */
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, pty_unlink /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
static void pty_semtake(FAR struct pty_devpair_s *devpair)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&devpair->pp_exclsem);
|
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_semgive
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define pty_semgive(c) nxsem_post(&(c)->pp_exclsem)
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_destroy
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2016-07-15 14:48:10 +02:00
|
|
|
static void pty_destroy(FAR struct pty_devpair_s *devpair)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
char devname[16];
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
/* Un-register the slave device */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
#ifdef CONFIG_PSEUDOTERM_BSD
|
|
|
|
snprintf(devname, 16, "/dev/ttyp%d", devpair->pp_minor);
|
|
|
|
#else
|
|
|
|
snprintf(devname, 16, "/dev/pts/%d", devpair->pp_minor);
|
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
(void)unregister_driver(devname);
|
|
|
|
|
2016-07-18 16:00:56 +02:00
|
|
|
/* Un-register the master device (/dev/ptyN may have already been
|
|
|
|
* unlinked).
|
|
|
|
*/
|
2016-07-15 14:48:10 +02:00
|
|
|
|
|
|
|
snprintf(devname, 16, "/dev/pty%d", (int)devpair->pp_minor);
|
2016-07-15 04:20:48 +02:00
|
|
|
(void)unregister_driver(devname);
|
|
|
|
|
|
|
|
/* Close the contained file structures */
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
(void)file_close_detached(&devpair->pp_master.pd_src);
|
|
|
|
(void)file_close_detached(&devpair->pp_master.pd_sink);
|
|
|
|
(void)file_close_detached(&devpair->pp_slave.pd_src);
|
|
|
|
(void)file_close_detached(&devpair->pp_slave.pd_sink);
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 15:22:36 +02:00
|
|
|
#ifdef CONFIG_PSEUDOTERM_SUSV1
|
|
|
|
/* Free this minor number so that it can be reused */
|
|
|
|
|
|
|
|
ptmx_minor_free(devpair->pp_minor);
|
|
|
|
#endif
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/* And free the device structure */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&devpair->pp_exclsem);
|
2016-07-15 14:48:10 +02:00
|
|
|
kmm_free(devpair);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pty_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-15 14:48:10 +02:00
|
|
|
FAR struct pty_devpair_s *devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
|
|
|
DEBUGASSERT(dev != NULL && dev->pd_devpair != NULL);
|
|
|
|
devpair = dev->pd_devpair;
|
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
/* Wait if this is an attempt to open the slave device and the slave
|
|
|
|
* device is locked.
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
if (!dev->pd_master)
|
|
|
|
{
|
|
|
|
/* Slave... Check if the slave driver is locked. We need to lock the
|
|
|
|
* scheduler while we are running to prevent asyncrhonous modification
|
|
|
|
* of pp_locked by pty_ioctl().
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
while (devpair->pp_locked)
|
|
|
|
{
|
|
|
|
/* Wait until unlocked. We will also most certainly suspend here. */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
(void)nxsem_wait(&devpair->pp_slavesem);
|
2016-07-15 17:39:33 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the device structure. This might also
|
|
|
|
* cause suspension.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pty_semtake(devpair);
|
|
|
|
|
|
|
|
/* Check again in case something happened asynchronously while we
|
|
|
|
* were suspended.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (devpair->pp_locked)
|
|
|
|
{
|
|
|
|
/* This cannot suspend because we have the scheduler locked.
|
|
|
|
* So pp_locked cannot change asyncrhonously between this test
|
|
|
|
* and the redundant test at the top of the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pty_semgive(devpair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Master ... Get exclusive access to the device structure */
|
|
|
|
|
|
|
|
pty_semtake(devpair);
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 22:29:32 +02:00
|
|
|
#ifndef CONFIG_PSEUDOTERM_SUSV1
|
2016-07-15 04:20:48 +02:00
|
|
|
/* If one side of the driver has been unlinked, then refuse further
|
|
|
|
* opens.
|
2016-07-15 22:29:32 +02:00
|
|
|
*
|
|
|
|
* NOTE: We ignore this case in the SUSv1 case. In the SUSv1 case, the
|
|
|
|
* master side is always unlinked.
|
2016-07-15 04:20:48 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
if (devpair->pp_unlinked)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = -EIDRM;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
else
|
2016-07-15 22:29:32 +02:00
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
/* Increment the count of open references on the driver */
|
|
|
|
|
|
|
|
devpair->pp_nopen++;
|
|
|
|
DEBUGASSERT(devpair->pp_nopen > 0);
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
pty_semgive(devpair);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pty_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-15 14:48:10 +02:00
|
|
|
FAR struct pty_devpair_s *devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
|
|
|
DEBUGASSERT(dev != NULL && dev->pd_devpair != NULL);
|
|
|
|
devpair = dev->pd_devpair;
|
|
|
|
|
|
|
|
/* Get exclusive access */
|
|
|
|
|
|
|
|
pty_semtake(devpair);
|
|
|
|
|
2016-07-15 15:33:48 +02:00
|
|
|
#ifdef CONFIG_PSEUDOTERM_SUSV1
|
|
|
|
/* Did the (single) master just close its reference? */
|
|
|
|
|
|
|
|
if (dev->pd_master)
|
|
|
|
{
|
|
|
|
/* Yes, then we are essentially unlinked and when all of the
|
|
|
|
* slaves close there references, then the PTY should be
|
|
|
|
* destroyed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
devpair->pp_unlinked = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Is this the last open reference? If so, was the driver previously
|
|
|
|
* unlinked?
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(devpair->pp_nopen > 0);
|
|
|
|
if (devpair->pp_nopen <= 1 && devpair->pp_unlinked)
|
|
|
|
{
|
2016-07-15 15:33:48 +02:00
|
|
|
/* Yes.. Free the device pair now (without freeing the semaphore) */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
pty_destroy(devpair);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise just decrement the open count */
|
|
|
|
|
|
|
|
devpair->pp_nopen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
pty_semgive(devpair);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t pty_read(FAR struct file *filep, FAR char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-16 18:43:44 +02:00
|
|
|
ssize_t ntotal;
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
ssize_t nread;
|
|
|
|
size_t i;
|
|
|
|
char ch;
|
2016-07-18 16:00:56 +02:00
|
|
|
int ret;
|
2016-07-16 18:43:44 +02:00
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
|
|
|
DEBUGASSERT(dev != NULL);
|
|
|
|
|
2016-07-16 18:43:44 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
/* Do input processing if any is enabled
|
|
|
|
*
|
|
|
|
* Specifically not handled:
|
|
|
|
*
|
2016-07-18 16:00:56 +02:00
|
|
|
* All of the local modes; echo, line editing, etc.
|
|
|
|
* Anything to do with break or parity errors.
|
|
|
|
* ISTRIP - We should be 8-bit clean.
|
|
|
|
* IUCLC - Not Posix
|
|
|
|
* IXON/OXOFF - No xon/xoff flow control.
|
2016-07-16 18:43:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->pd_iflag & (INLCR | IGNCR | ICRNL))
|
|
|
|
{
|
2016-07-18 16:00:56 +02:00
|
|
|
/* We will transfer one byte at a time, making the appropriate
|
2016-07-16 18:43:44 +02:00
|
|
|
* translations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ntotal = 0;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2016-07-18 16:00:56 +02:00
|
|
|
#ifndef CONFIG_PSEUDOTERM_FULLBLOCKS
|
|
|
|
/* This logic should return if the pipe becomes empty after some
|
|
|
|
* bytes were read from the pipe. If we have already read some
|
|
|
|
* data, we use the FIONREAD ioctl to test if there are more bytes
|
|
|
|
* in the pipe.
|
2016-07-19 18:40:23 +02:00
|
|
|
*
|
|
|
|
* REVISIT: An alternative design might be to (1) configure the
|
|
|
|
* source file as non-blocking, then (2) wait using poll() for the
|
|
|
|
* first byte to be received. (3) Subsequent bytes would
|
|
|
|
* use file_read() without polling and would (4) terminate when no
|
|
|
|
* data is returned.
|
2016-07-18 16:00:56 +02:00
|
|
|
*/
|
2016-07-16 18:43:44 +02:00
|
|
|
|
2016-07-18 16:00:56 +02:00
|
|
|
if (ntotal > 0)
|
|
|
|
{
|
|
|
|
int nsrc;
|
|
|
|
|
2016-07-19 14:45:02 +02:00
|
|
|
/* There are inherent race conditions in this test. We lock
|
|
|
|
* the scheduler before the test and after the file_read()
|
|
|
|
* below to eliminate one race: (a) We detect that there is
|
|
|
|
* data in the source file, (b) we are suspended and another
|
|
|
|
* thread reads the data, emptying the fifo, then (c) we
|
|
|
|
* resume and call file_read(), blocking indefinitely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
2016-07-18 16:00:56 +02:00
|
|
|
/* Check how many bytes are waiting in the pipe */
|
|
|
|
|
|
|
|
ret = file_ioctl(&dev->pd_src, FIONREAD,
|
|
|
|
(unsigned long)((uintptr_t)&nsrc));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-07-19 14:45:02 +02:00
|
|
|
sched_unlock();
|
2016-07-18 16:00:56 +02:00
|
|
|
ntotal = ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Break out of the loop and return ntotal if the pipe is
|
2016-07-19 14:45:02 +02:00
|
|
|
* empty. This is another race: There fifo was empty when we
|
2016-07-18 18:55:37 +02:00
|
|
|
* called file_ioctl() above, but it might not be empty right
|
2016-07-19 14:45:02 +02:00
|
|
|
* now. Losing that race should not lead to any bad behaviors,
|
|
|
|
* however, we the caller will get those bytes on the next
|
|
|
|
* read.
|
2016-07-18 16:00:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (nsrc < 1)
|
|
|
|
{
|
2016-07-19 14:45:02 +02:00
|
|
|
sched_unlock();
|
2016-07-18 16:00:56 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-07-19 14:45:02 +02:00
|
|
|
|
|
|
|
/* Read one byte from the source the byte. This should not
|
|
|
|
* block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nread = file_read(&dev->pd_src, &ch, 1);
|
|
|
|
sched_unlock();
|
2016-07-18 16:00:56 +02:00
|
|
|
}
|
2016-07-19 14:45:02 +02:00
|
|
|
else
|
2016-07-19 17:53:52 +02:00
|
|
|
#else
|
|
|
|
/* If we wanted to return full blocks of data, then file_read()
|
|
|
|
* may need to be called repeatedly. That is because the pipe
|
|
|
|
* read() method will return early if the fifo becomes empty
|
|
|
|
* after any data has been read.
|
|
|
|
*/
|
|
|
|
|
|
|
|
# error Missing logic
|
2016-07-18 16:00:56 +02:00
|
|
|
#endif
|
2016-07-19 14:45:02 +02:00
|
|
|
{
|
2016-07-19 17:53:52 +02:00
|
|
|
/* Read one byte from the source the byte. This call will
|
|
|
|
* block if the source pipe is empty.
|
2016-07-26 18:09:35 +02:00
|
|
|
*
|
|
|
|
* REVISIT: Should not block if the oflags include O_NONBLOCK.
|
|
|
|
* How would we ripple the O_NONBLOCK characteristic to the
|
2016-07-27 16:03:09 +02:00
|
|
|
* contained soruce pipe? file_vfcntl()? Or FIONREAD? See the
|
2016-07-26 20:13:30 +02:00
|
|
|
* TODO comment at the top of this file.
|
2016-07-19 14:45:02 +02:00
|
|
|
*/
|
2016-07-18 16:00:56 +02:00
|
|
|
|
2016-07-19 14:45:02 +02:00
|
|
|
nread = file_read(&dev->pd_src, &ch, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if file_read was successful */
|
2016-07-18 16:00:56 +02:00
|
|
|
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
ntotal = nread;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform input processing */
|
2016-07-16 18:43:44 +02:00
|
|
|
/* \n -> \r or \r -> \n translation? */
|
|
|
|
|
|
|
|
if (ch == '\n' && (dev->pd_iflag & INLCR) != 0)
|
|
|
|
{
|
|
|
|
ch = '\r';
|
|
|
|
}
|
|
|
|
else if (ch == '\r' && (dev->pd_iflag & ICRNL) != 0)
|
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Discarding \r ? Print character if (1) character is not \r or
|
|
|
|
* if (2) we were not asked to ignore \r.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ch != '\r' || (dev->pd_iflag & IGNCR) == 0)
|
|
|
|
{
|
|
|
|
|
2016-07-18 16:00:56 +02:00
|
|
|
/* Transfer the (possibly translated) character and update the
|
|
|
|
* count of bytes transferred.
|
|
|
|
*/
|
2016-07-16 18:43:44 +02:00
|
|
|
|
2016-07-18 16:00:56 +02:00
|
|
|
*buffer++ = ch;
|
2016-07-16 18:43:44 +02:00
|
|
|
ntotal++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-07-18 18:55:37 +02:00
|
|
|
/* NOTE: the source pipe will block if no data is available in
|
2016-07-18 16:00:56 +02:00
|
|
|
* the pipe. Otherwise, it will return data from the pipe. If
|
|
|
|
* there are fewer than 'len' bytes in the, it will return with
|
|
|
|
* ntotal < len.
|
2016-07-26 18:09:35 +02:00
|
|
|
*
|
|
|
|
* REVISIT: Should not block if the oflags include O_NONBLOCK.
|
|
|
|
* How would we ripple the O_NONBLOCK characteristic to the
|
2016-07-27 16:03:09 +02:00
|
|
|
* contained source pipe? file_vfcntl()? Or FIONREAD? See the
|
2016-07-26 20:13:30 +02:00
|
|
|
* TODO comment at the top of this file.
|
2016-07-18 16:00:56 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-16 18:43:44 +02:00
|
|
|
ntotal = file_read(&dev->pd_src, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntotal;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t pty_write(FAR struct file *filep, FAR const char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-16 18:43:44 +02:00
|
|
|
ssize_t ntotal;
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
ssize_t nwritten;
|
|
|
|
size_t i;
|
|
|
|
char ch;
|
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
|
|
|
DEBUGASSERT(dev != NULL);
|
|
|
|
|
2016-07-16 18:43:44 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
/* Do output post-processing */
|
|
|
|
|
|
|
|
if ((dev->pd_oflag & OPOST) != 0)
|
|
|
|
{
|
|
|
|
/* We will transfer one byte at a time, making the appropriae
|
|
|
|
* translations. Specifically not handled:
|
|
|
|
*
|
|
|
|
* OXTABS - primarily a full-screen terminal optimisation
|
|
|
|
* ONOEOT - Unix interoperability hack
|
|
|
|
* OLCUC - Not specified by POSIX
|
|
|
|
* ONOCR - low-speed interactive optimisation
|
|
|
|
*/
|
|
|
|
|
|
|
|
ntotal = 0;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
ch = *buffer++;
|
|
|
|
|
|
|
|
/* Mapping CR to NL? */
|
|
|
|
|
|
|
|
if (ch == '\r' && (dev->pd_oflag & OCRNL) != 0)
|
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we interested in newline processing? */
|
|
|
|
|
|
|
|
if ((ch == '\n') && (dev->pd_oflag & (ONLCR | ONLRET)) != 0)
|
|
|
|
{
|
|
|
|
char cr = '\r';
|
|
|
|
|
2016-07-26 18:09:35 +02:00
|
|
|
/* Transfer the carriage return. This will block if the
|
|
|
|
* sink pipe is full.
|
|
|
|
*
|
|
|
|
* REVISIT: Should not block if the oflags include O_NONBLOCK.
|
|
|
|
* How would we ripple the O_NONBLOCK characteristic to the
|
2016-07-27 16:03:09 +02:00
|
|
|
* contained sink pipe? file_vfcntl()? Or FIONSPACE? See the
|
2016-07-26 20:13:30 +02:00
|
|
|
* TODO comment at the top of this file.
|
2016-07-26 18:09:35 +02:00
|
|
|
*/
|
2016-07-16 18:43:44 +02:00
|
|
|
|
|
|
|
nwritten = file_write(&dev->pd_sink, &cr, 1);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
ntotal = nwritten;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the count of bytes transferred */
|
|
|
|
|
|
|
|
ntotal++;
|
|
|
|
}
|
|
|
|
|
2016-07-26 18:09:35 +02:00
|
|
|
/* Transfer the (possibly translated) character.. This will block
|
|
|
|
* if the sink pipe is full
|
|
|
|
*
|
|
|
|
* REVISIT: Should not block if the oflags include O_NONBLOCK.
|
|
|
|
* How would we ripple the O_NONBLOCK characteristic to the
|
2016-07-27 16:03:09 +02:00
|
|
|
* contained sink pipe? file_vfcntl()? Or FIONSPACe? See the
|
2016-07-26 20:13:30 +02:00
|
|
|
* TODO comment at the top of this file.
|
2016-07-26 18:09:35 +02:00
|
|
|
*/
|
2016-07-16 18:43:44 +02:00
|
|
|
|
|
|
|
nwritten = file_write(&dev->pd_sink, &ch, 1);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
ntotal = nwritten;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the count of bytes transferred */
|
|
|
|
|
|
|
|
ntotal++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-07-26 18:09:35 +02:00
|
|
|
/* Write the 'len' bytes to the sink pipe. This will block until all
|
|
|
|
* 'len' bytes have been written to the pipe.
|
|
|
|
*
|
|
|
|
* REVISIT: Should not block if the oflags include O_NONBLOCK.
|
|
|
|
* How would we ripple the O_NONBLOCK characteristic to the
|
2016-07-27 16:03:09 +02:00
|
|
|
* contained sink pipe? file_vfcntl()? Or FIONSPACE? See the
|
2016-07-26 20:13:30 +02:00
|
|
|
* TODO comment at the top of this file.
|
2016-07-26 18:09:35 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-16 18:43:44 +02:00
|
|
|
ntotal = file_write(&dev->pd_sink, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntotal;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard ioctl method. This is where ALL of the PWM work is done.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pty_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-15 16:33:47 +02:00
|
|
|
FAR struct pty_devpair_s *devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2016-07-15 16:33:47 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
|
|
|
DEBUGASSERT(dev != NULL && dev->pd_devpair != NULL);
|
|
|
|
devpair = dev->pd_devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
|
|
|
pty_semtake(devpair);
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/* Handle IOCTL commands */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* PTY IOCTL commands would be handled here */
|
2016-07-15 16:33:47 +02:00
|
|
|
|
|
|
|
case TIOCGPTN: /* Get Pty Number (of pty-mux device): FAR int* */
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
ret = -ENOSYS;
|
|
|
|
#else
|
|
|
|
FAR int *ptyno = (FAR int *)((uintptr_t)arg);
|
|
|
|
if (ptyno == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ptyno = (int)devpair->pp_minor;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCSPTLCK: /* Lock/unlock Pty: int */
|
2016-07-15 17:39:33 +02:00
|
|
|
{
|
|
|
|
if (arg == 0)
|
|
|
|
{
|
|
|
|
int sval;
|
|
|
|
|
|
|
|
/* Unlocking */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
devpair->pp_locked = false;
|
|
|
|
|
|
|
|
/* Release any waiting threads */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-10-03 20:51:15 +02:00
|
|
|
DEBUGVERIFY(nxsem_getvalue(&devpair->pp_slavesem, &sval));
|
2016-07-15 17:39:33 +02:00
|
|
|
if (sval < 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&devpair->pp_slavesem);
|
2016-07-15 17:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (sval < 0);
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Locking */
|
|
|
|
|
|
|
|
devpair->pp_locked = true;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 16:33:47 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCGPTLCK: /* Get Pty lock state: FAR int* */
|
2016-07-15 17:39:33 +02:00
|
|
|
{
|
|
|
|
FAR int *ptr = (FAR int *)((uintptr_t)arg);
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ptr = (int)devpair->pp_locked;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 16:33:47 +02:00
|
|
|
break;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-16 18:43:44 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
case TCGETS:
|
|
|
|
{
|
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)arg;
|
|
|
|
|
|
|
|
if (!termiosp)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And update with flags from this layer */
|
|
|
|
|
|
|
|
termiosp->c_iflag = dev->pd_iflag;
|
|
|
|
termiosp->c_oflag = dev->pd_oflag;
|
|
|
|
termiosp->c_lflag = 0;
|
2016-07-18 19:27:06 +02:00
|
|
|
ret = OK;
|
2016-07-16 18:43:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCSETS:
|
|
|
|
{
|
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)arg;
|
|
|
|
|
|
|
|
if (!termiosp)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the flags we keep at this layer */
|
|
|
|
|
|
|
|
dev->pd_iflag = termiosp->c_iflag;
|
|
|
|
dev->pd_oflag = termiosp->c_oflag;
|
2016-07-18 19:27:06 +02:00
|
|
|
ret = OK;
|
2016-07-16 18:43:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2016-07-25 22:06:32 +02:00
|
|
|
|
|
|
|
/* Get the number of bytes that are immediately available for reading
|
|
|
|
* from the source pipe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case FIONREAD:
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&dev->pd_src, cmd, arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get the number of bytes waiting in the sink pipe (FIONWRITE) or the
|
|
|
|
* number of unused bytes in the sink pipe (FIONSPACE).
|
|
|
|
*/
|
|
|
|
|
|
|
|
case FIONWRITE:
|
|
|
|
case FIONSPACE:
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&dev->pd_sink, cmd, arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/* Any unrecognized IOCTL commands will be passed to the contained
|
|
|
|
* pipe driver.
|
2016-07-25 22:06:32 +02:00
|
|
|
*
|
|
|
|
* REVISIT: We know for a fact that the pipe driver only supports
|
|
|
|
* FIONREAD, FIONWRITE, FIONSPACE and PIPEIOC_POLICY. The first two
|
|
|
|
* are handled above and PIPEIOC_POLICY should not be managed by
|
|
|
|
* applications -- it can break the PTY!
|
2016-07-15 04:20:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2016-07-25 22:06:32 +02:00
|
|
|
#if 0
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = file_ioctl(&dev->pd_src, cmd, arg);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret >= 0 || ret == -ENOTTY)
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = file_ioctl(&dev->pd_sink, cmd, arg);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
2016-07-25 22:06:32 +02:00
|
|
|
#else
|
|
|
|
ret = ENOTTY;
|
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
pty_semgive(devpair);
|
2016-07-15 04:20:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
static int pty_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2016-07-15 22:29:32 +02:00
|
|
|
bool setup)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
2016-07-15 18:07:33 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
/* REVISIT: If both POLLIN and POLLOUT are set, might the following logic
|
|
|
|
* fail? Could we not get POLLIN on the sink file and POLLOUT on the source
|
|
|
|
* file?
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* POLLIN: Data other than high-priority data may be read without blocking. */
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) != 0)
|
|
|
|
{
|
2016-07-16 15:03:38 +02:00
|
|
|
ret = file_poll(&dev->pd_src, fds, setup);
|
2016-07-15 18:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= OK || ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
/* POLLOUT: Normal data may be written without blocking. */
|
|
|
|
|
|
|
|
if ((fds->events & POLLOUT) != 0)
|
|
|
|
{
|
2016-07-16 15:03:38 +02:00
|
|
|
ret = file_poll(&dev->pd_sink, fds, setup);
|
2016-07-15 18:07:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pty_unlink(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-15 14:48:10 +02:00
|
|
|
FAR struct pty_devpair_s *devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(inode != NULL && inode->i_private != NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
dev = inode->i_private;
|
|
|
|
devpair = dev->pd_devpair;
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(dev->pd_devpair != NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/* Get exclusive access */
|
|
|
|
|
|
|
|
pty_semtake(devpair);
|
|
|
|
|
|
|
|
/* Indicate that the driver has been unlinked */
|
|
|
|
|
|
|
|
devpair->pp_unlinked = true;
|
|
|
|
|
|
|
|
/* If there are no further open references to the driver, then commit
|
|
|
|
* Hara-Kiri now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (devpair->pp_nopen == 0)
|
|
|
|
{
|
|
|
|
pty_destroy(devpair);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
pty_semgive(devpair);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_register
|
|
|
|
*
|
2016-07-15 17:39:33 +02:00
|
|
|
* Description:
|
|
|
|
* Create and register PTY master and slave devices. The slave side of
|
|
|
|
* the interface is always locked initially. The master must call
|
|
|
|
* unlockpt() before the slave device can be opened.
|
|
|
|
*
|
2016-07-15 04:20:48 +02:00
|
|
|
* Input Parameters:
|
2016-07-15 15:18:12 +02:00
|
|
|
* minor - The number that qualifies the naming of the created devices.
|
2016-07-15 14:48:10 +02:00
|
|
|
*
|
2016-07-15 15:18:12 +02:00
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pty_register(int minor)
|
|
|
|
{
|
|
|
|
FAR struct pty_devpair_s *devpair;
|
|
|
|
int pipe_a[2];
|
|
|
|
int pipe_b[2];
|
|
|
|
char devname[16];
|
2016-07-15 14:48:10 +02:00
|
|
|
int ret;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/* Allocate a device instance */
|
|
|
|
|
|
|
|
devpair = kmm_zalloc(sizeof(struct pty_devpair_s));
|
|
|
|
if (devpair == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
/* Initialize semaphores */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&devpair->pp_slavesem, 0, 0);
|
|
|
|
nxsem_init(&devpair->pp_exclsem, 0, 1);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
|
|
|
/* The pp_slavesem semaphore is used for signaling and, hence, should not
|
|
|
|
* have priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_setprotocol(&devpair->pp_slavesem, SEM_PRIO_NONE);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2016-07-15 15:33:48 +02:00
|
|
|
devpair->pp_minor = minor;
|
2016-07-15 04:20:48 +02:00
|
|
|
#endif
|
2016-07-15 17:39:33 +02:00
|
|
|
devpair->pp_locked = true;
|
2016-07-15 14:48:10 +02:00
|
|
|
devpair->pp_master.pd_devpair = devpair;
|
2016-07-15 15:33:48 +02:00
|
|
|
devpair->pp_master.pd_master = true;
|
2016-07-15 17:39:33 +02:00
|
|
|
devpair->pp_slave.pd_devpair = devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-19 22:26:41 +02:00
|
|
|
/* Create two pipes:
|
|
|
|
*
|
|
|
|
* pipe_a: Master source, slave sink (TX, slave-to-master)
|
|
|
|
* pipe_b: Master sink, slave source (RX, master-to-slave)
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-19 22:26:41 +02:00
|
|
|
ret = pipe2(pipe_a, CONFIG_PSEUDOTERM_TXBUFSIZE);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_devpair;
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:26:41 +02:00
|
|
|
ret = pipe2(pipe_b, CONFIG_PSEUDOTERM_RXBUFSIZE);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_pipea;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach the pipe file descriptors (closing them in the process)
|
|
|
|
*
|
|
|
|
* fd[0] is for reading;
|
|
|
|
* fd[1] is for writing.
|
|
|
|
*/
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = file_detach(pipe_a[0], &devpair->pp_master.pd_src);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_pipeb;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_a[0] = -1;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = file_detach(pipe_a[1], &devpair->pp_slave.pd_sink);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_pipeb;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_a[1] = -1;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = file_detach(pipe_b[0], &devpair->pp_slave.pd_src);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_pipeb;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_b[0] = -1;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
ret = file_detach(pipe_b[1], &devpair->pp_master.pd_sink);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_pipeb;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_b[1] = -1;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
/* Register the slave device
|
|
|
|
*
|
|
|
|
* BSD style (deprecated): /dev/ttypN
|
|
|
|
* SUSv1 style: /dev/pts/N
|
|
|
|
*
|
|
|
|
* Where N is the minor number
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
#ifdef CONFIG_PSEUDOTERM_BSD
|
|
|
|
snprintf(devname, 16, "/dev/ttyp%d", minor);
|
|
|
|
#else
|
2016-07-15 04:20:48 +02:00
|
|
|
snprintf(devname, 16, "/dev/pts/%d", minor);
|
2016-07-15 14:48:10 +02:00
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 22:09:02 +02:00
|
|
|
ret = register_driver(devname, &g_pty_fops, 0666, &devpair->pp_slave);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_pipeb;
|
|
|
|
}
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
/* Register the master device
|
|
|
|
*
|
|
|
|
* BSD style (deprecated): /dev/ptyN
|
2016-07-15 19:15:33 +02:00
|
|
|
* SUSv1 style: Master: /dev/ptmx (multiplexor, see ptmx.c)
|
2016-07-15 14:48:10 +02:00
|
|
|
*
|
|
|
|
* Where N is the minor number
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
snprintf(devname, 16, "/dev/pty%d", minor);
|
|
|
|
|
2016-07-15 22:09:02 +02:00
|
|
|
ret = register_driver(devname, &g_pty_fops, 0666, &devpair->pp_master);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
goto errout_with_slave;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
errout_with_slave:
|
|
|
|
#ifdef CONFIG_PSEUDOTERM_BSD
|
|
|
|
snprintf(devname, 16, "/dev/ttyp%d", minor);
|
|
|
|
#else
|
|
|
|
snprintf(devname, 16, "/dev/pts/%d", minor);
|
|
|
|
#endif
|
|
|
|
(void)unregister_driver(devname);
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
errout_with_pipeb:
|
|
|
|
if (pipe_b[0] >= 0)
|
|
|
|
{
|
|
|
|
close(pipe_b[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
(void)file_close_detached(&devpair->pp_master.pd_src);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe_b[1] >= 0)
|
|
|
|
{
|
|
|
|
close(pipe_b[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
(void)file_close_detached(&devpair->pp_slave.pd_sink);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_pipea:
|
|
|
|
if (pipe_a[0] >= 0)
|
|
|
|
{
|
|
|
|
close(pipe_a[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
(void)file_close_detached(&devpair->pp_slave.pd_src);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe_a[1] >= 0)
|
|
|
|
{
|
|
|
|
close(pipe_a[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-15 14:48:10 +02:00
|
|
|
(void)file_close_detached(&devpair->pp_master.pd_sink);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_devpair:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&devpair->pp_exclsem);
|
|
|
|
nxsem_destroy(&devpair->pp_slavesem);
|
2016-07-15 04:20:48 +02:00
|
|
|
kmm_free(devpair);
|
|
|
|
return ret;
|
|
|
|
}
|