2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/serial/pty.c
|
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-07-25 10:35:03 +02:00
|
|
|
#include <ctype.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#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-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>
|
|
|
|
|
2023-07-25 10:35:03 +02:00
|
|
|
#include <nuttx/ascii.h>
|
2016-07-15 14:48:10 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.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-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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
/* Maximum number of threads than can be waiting for POLL events */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEV_PTY_NPOLLWAITERS
|
|
|
|
# define CONFIG_DEV_PTY_NPOLLWAITERS 2
|
|
|
|
#endif
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
struct pty_poll_s
|
|
|
|
{
|
|
|
|
FAR void *src;
|
|
|
|
FAR void *sink;
|
|
|
|
};
|
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/* 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 */
|
2023-07-25 10:35:03 +02:00
|
|
|
uint8_t pd_escape; /* Number of the character to be escaped */
|
2022-02-04 19:35:04 +01:00
|
|
|
tcflag_t pd_iflag; /* Terminal input modes */
|
2023-03-20 03:54:12 +01:00
|
|
|
tcflag_t pd_lflag; /* Terminal local modes */
|
2022-02-04 19:35:04 +01:00
|
|
|
tcflag_t pd_oflag; /* Terminal output modes */
|
2018-08-26 15:27:19 +02:00
|
|
|
struct pty_poll_s pd_poll[CONFIG_DEV_PTY_NPOLLWAITERS];
|
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
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
bool pp_susv1; /* SUSv1 or BSD style */
|
2016-07-15 17:39:33 +02:00
|
|
|
bool pp_locked; /* Slave is locked */
|
|
|
|
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 */
|
2016-07-15 17:39:33 +02:00
|
|
|
sem_t pp_slavesem; /* Slave lock semaphore */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t pp_lock; /* Mutual exclusion */
|
2016-07-15 04:20:48 +02:00
|
|
|
};
|
|
|
|
|
2016-07-26 18:09:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pty_destroy(FAR struct pty_devpair_s *devpair);
|
2022-02-06 15:46:54 +01:00
|
|
|
static int pty_pipe(FAR struct pty_devpair_s *devpair);
|
2016-07-26 18:09:35 +02:00
|
|
|
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,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2016-07-26 18:09:35 +02:00
|
|
|
static ssize_t pty_write(FAR struct file *filep, FAR const char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2016-07-26 18:09:35 +02:00
|
|
|
static int pty_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
|
|
|
static int pty_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-04-12 12:16:13 +02:00
|
|
|
bool setup);
|
2016-07-26 18:09:35 +02:00
|
|
|
#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
|
|
|
{
|
|
|
|
pty_open, /* open */
|
|
|
|
pty_close, /* close */
|
|
|
|
pty_read, /* read */
|
|
|
|
pty_write, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
pty_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
pty_poll /* poll */
|
2016-07-15 04:20:48 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, pty_unlink /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_destroy
|
|
|
|
****************************************************************************/
|
|
|
|
|
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];
|
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
if (devpair->pp_susv1)
|
|
|
|
{
|
|
|
|
/* Free this minor number so that it can be reused */
|
2016-07-15 14:48:10 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
ptmx_minor_free(devpair->pp_minor);
|
2022-02-07 09:03:58 +01:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
/* Un-register the slave device */
|
2022-02-07 09:03:58 +01:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
snprintf(devname, 16, "/dev/pts/%d", devpair->pp_minor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Un-register the master device (/dev/ptyN may have already been
|
|
|
|
* unlinked).
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
snprintf(devname, 16, "/dev/pty%d", (int)devpair->pp_minor);
|
|
|
|
unregister_driver(devname);
|
2016-07-15 15:22:36 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
/* Un-register the slave device */
|
|
|
|
|
|
|
|
snprintf(devname, 16, "/dev/ttyp%d", devpair->pp_minor);
|
|
|
|
}
|
|
|
|
|
|
|
|
unregister_driver(devname);
|
2016-07-15 15:22:36 +02:00
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/* And free the device structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&devpair->pp_lock);
|
2022-02-06 17:36:23 +01:00
|
|
|
nxsem_destroy(&devpair->pp_slavesem);
|
2016-07-15 14:48:10 +02:00
|
|
|
kmm_free(devpair);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
2022-02-06 15:46:54 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_pipe
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pty_pipe(FAR struct pty_devpair_s *devpair)
|
|
|
|
{
|
|
|
|
FAR struct file *pipe_a[2];
|
|
|
|
FAR struct file *pipe_b[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create two pipes:
|
|
|
|
*
|
|
|
|
* pipe_a: Master source, slave sink (TX, slave-to-master)
|
|
|
|
* pipe_b: Master sink, slave source (RX, master-to-slave)
|
|
|
|
*/
|
|
|
|
|
|
|
|
pipe_a[0] = &devpair->pp_master.pd_src;
|
|
|
|
pipe_a[1] = &devpair->pp_slave.pd_sink;
|
|
|
|
|
|
|
|
ret = file_pipe(pipe_a, CONFIG_PSEUDOTERM_TXBUFSIZE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_b[0] = &devpair->pp_slave.pd_src;
|
|
|
|
pipe_b[1] = &devpair->pp_master.pd_sink;
|
|
|
|
|
|
|
|
ret = file_pipe(pipe_b, CONFIG_PSEUDOTERM_RXBUFSIZE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
file_close(pipe_a[0]);
|
|
|
|
file_close(pipe_a[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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;
|
2022-02-06 15:46:54 +01:00
|
|
|
int ret = OK;
|
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;
|
|
|
|
|
2022-02-06 17:29:04 +01:00
|
|
|
/* Get exclusive access to the device structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devpair->pp_lock);
|
2022-02-06 17:29:04 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2022-02-06 17:29:04 +01:00
|
|
|
/* Slave... Check if the slave driver is locked. */
|
2016-07-15 17:39:33 +02:00
|
|
|
|
|
|
|
while (devpair->pp_locked)
|
|
|
|
{
|
2022-02-06 17:29:04 +01:00
|
|
|
/* Release the exclusive access before wait */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devpair->pp_lock);
|
2022-02-06 17:29:04 +01:00
|
|
|
|
2020-05-04 08:49:38 +02:00
|
|
|
/* Wait until unlocked.
|
|
|
|
* We will also most certainly suspend here.
|
|
|
|
*/
|
2016-07-15 17:39:33 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = nxsem_wait(&devpair->pp_slavesem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 17:39:33 +02:00
|
|
|
|
2022-02-06 17:29:04 +01:00
|
|
|
/* Restore the semaphore count */
|
|
|
|
|
|
|
|
DEBUGVERIFY(nxsem_post(&devpair->pp_slavesem));
|
|
|
|
|
2016-07-15 17:39:33 +02:00
|
|
|
/* Get exclusive access to the device structure. This might also
|
|
|
|
* cause suspension.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devpair->pp_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 17:39:33 +02:00
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/* If one side of the driver has been unlinked, then refuse further
|
|
|
|
* opens.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
{
|
2022-02-06 15:46:54 +01:00
|
|
|
/* First open? */
|
|
|
|
|
|
|
|
if (devpair->pp_nopen == 0)
|
|
|
|
{
|
|
|
|
/* Yes, create the internal pipe */
|
|
|
|
|
|
|
|
ret = pty_pipe(devpair);
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-06 15:46:54 +01:00
|
|
|
/* Increment the count of open references on the driver */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-06 15:46:54 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
devpair->pp_nopen++;
|
|
|
|
DEBUGASSERT(devpair->pp_nopen > 0);
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devpair->pp_lock);
|
2016-07-15 04:20:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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;
|
2020-04-01 18:13:52 +02:00
|
|
|
int ret;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2022-02-06 18:31:01 +01:00
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
2016-07-15 04:20:48 +02:00
|
|
|
DEBUGASSERT(dev != NULL && dev->pd_devpair != NULL);
|
2022-02-06 18:31:01 +01:00
|
|
|
devpair = dev->pd_devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devpair->pp_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
/* Check if the decremented inode reference count would go to zero */
|
2016-07-15 15:33:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
if (inode->i_crefs == 1)
|
2016-07-15 15:33:48 +02:00
|
|
|
{
|
2022-02-06 18:31:01 +01:00
|
|
|
/* Did the (single) master just close its reference? */
|
2016-07-15 15:33:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
if (dev->pd_master && devpair->pp_susv1)
|
|
|
|
{
|
|
|
|
/* Yes, then we are essentially unlinked and when all of the
|
|
|
|
* slaves close there references, then the PTY should be
|
|
|
|
* destroyed.
|
|
|
|
*/
|
2016-07-15 15:33:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
devpair->pp_unlinked = true;
|
|
|
|
}
|
2022-02-06 15:46:54 +01:00
|
|
|
|
|
|
|
/* Close the contained file structures */
|
|
|
|
|
|
|
|
file_close(&dev->pd_src);
|
|
|
|
file_close(&dev->pd_sink);
|
|
|
|
}
|
|
|
|
|
2016-07-15 15:33:48 +02:00
|
|
|
/* 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--;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devpair->pp_lock);
|
2016-07-15 04:20:48 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2022-02-19 14:33:04 +01:00
|
|
|
ssize_t i;
|
|
|
|
ssize_t j;
|
2016-07-16 18:43:44 +02:00
|
|
|
char ch;
|
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
|
|
|
/* 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))
|
|
|
|
{
|
2022-02-19 14:33:04 +01:00
|
|
|
while ((ntotal = file_read(&dev->pd_src, buffer, len)) > 0)
|
2016-07-16 18:43:44 +02:00
|
|
|
{
|
2022-02-19 14:33:04 +01:00
|
|
|
for (i = j = 0; i < ntotal; i++)
|
2016-07-18 16:00:56 +02:00
|
|
|
{
|
2022-02-19 14:33:04 +01:00
|
|
|
/* Perform input processing */
|
2016-07-19 14:45:02 +02:00
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
ch = buffer[i];
|
2016-07-19 14:45:02 +02:00
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
/* \n -> \r or \r -> \n translation? */
|
2016-07-18 16:00:56 +02:00
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
if (ch == '\n' && (dev->pd_iflag & INLCR) != 0)
|
2016-07-18 16:00:56 +02:00
|
|
|
{
|
2022-02-19 14:33:04 +01:00
|
|
|
ch = '\r';
|
2016-07-18 16:00:56 +02:00
|
|
|
}
|
2022-02-19 14:33:04 +01:00
|
|
|
else if (ch == '\r' && (dev->pd_iflag & ICRNL) != 0)
|
2016-07-18 16:00:56 +02:00
|
|
|
{
|
2022-02-19 14:33:04 +01:00
|
|
|
ch = '\n';
|
2016-07-18 16:00:56 +02:00
|
|
|
}
|
2016-07-19 14:45:02 +02:00
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
/* Discarding \r ? Print character if (1) character is not \r
|
|
|
|
* or if (2) we were not asked to ignore \r.
|
2016-07-19 14:45:02 +02:00
|
|
|
*/
|
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
if (ch != '\r' || (dev->pd_iflag & IGNCR) == 0)
|
|
|
|
{
|
|
|
|
buffer[j++] = ch;
|
|
|
|
}
|
2016-07-16 18:43:44 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
/* Is the buffer not empty after process? */
|
2016-07-16 18:43:44 +02:00
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
if (j != 0)
|
2016-07-16 18:43:44 +02:00
|
|
|
{
|
2022-02-19 14:33:04 +01:00
|
|
|
/* Yes, we are done. */
|
2016-07-16 18:43:44 +02:00
|
|
|
|
2022-02-19 14:33:04 +01:00
|
|
|
ntotal = j;
|
|
|
|
break;
|
2016-07-16 18:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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
|
2021-01-04 04:33:16 +01:00
|
|
|
* contained source pipe? file_fcntl()? 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);
|
|
|
|
}
|
|
|
|
|
2023-05-17 03:29:07 +02:00
|
|
|
if ((dev->pd_lflag & ECHO) && (ntotal > 0))
|
2023-03-20 03:54:12 +01:00
|
|
|
{
|
2023-07-25 10:35:03 +02:00
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
for (i = j = 0; i < ntotal; i++)
|
|
|
|
{
|
|
|
|
ch = buffer[i];
|
|
|
|
|
|
|
|
/* Check for the beginning of a VT100 escape sequence, 3 byte */
|
|
|
|
|
|
|
|
if (ch == ASCII_ESC)
|
|
|
|
{
|
|
|
|
/* Mark that we should skip 2 more bytes */
|
|
|
|
|
|
|
|
dev->pd_escape = 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (dev->pd_escape == 2 && ch != ASCII_LBRACKET)
|
|
|
|
{
|
|
|
|
/* It's not an <esc>[x 3 byte sequence, show it */
|
|
|
|
|
|
|
|
dev->pd_escape = 0;
|
|
|
|
}
|
|
|
|
else if (dev->pd_escape > 0)
|
|
|
|
{
|
|
|
|
/* Skipping character count down */
|
|
|
|
|
|
|
|
if (--dev->pd_escape > 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Echo if the character in batch */
|
|
|
|
|
|
|
|
if (ch == '\n' || (n != 0 && j + n != i))
|
|
|
|
{
|
|
|
|
if (n != 0)
|
|
|
|
{
|
|
|
|
pty_write(filep, buffer + j, n);
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
pty_write(filep, "\r\n", 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Record the character can be echo */
|
|
|
|
|
|
|
|
if (!iscntrl(ch & 0xff) && n++ == 0)
|
|
|
|
{
|
|
|
|
j = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n != 0)
|
|
|
|
{
|
|
|
|
pty_write(filep, buffer + j, n);
|
|
|
|
}
|
2023-03-20 03:54:12 +01:00
|
|
|
}
|
|
|
|
|
2016-07-16 18:43:44 +02:00
|
|
|
return ntotal;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
static ssize_t pty_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct pty_dev_s *dev;
|
2016-07-16 18:43:44 +02:00
|
|
|
ssize_t ntotal;
|
|
|
|
ssize_t nwritten;
|
|
|
|
size_t i;
|
|
|
|
char ch;
|
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
|
|
|
/* 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
|
2021-01-04 04:33:16 +01:00
|
|
|
* contained sink pipe? file_fcntl()? Or FIONSPACE? See the
|
2016-07-26 20:13:30 +02:00
|
|
|
* TODO comment at the top of this file.
|
2020-06-01 15:24:10 +02:00
|
|
|
*
|
|
|
|
* NOTE: The newline is not included in total number of bytes
|
|
|
|
* written. Otherwise, we would return more than the
|
|
|
|
* requested number of bytes.
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-01-04 04:33:16 +01:00
|
|
|
* contained sink pipe? file_fcntl()? 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
|
|
|
|
{
|
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
|
2021-01-04 04:33:16 +01:00
|
|
|
* contained sink pipe? file_fcntl()? 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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devpair->pp_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 17:39:33 +02:00
|
|
|
|
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* */
|
|
|
|
{
|
|
|
|
FAR int *ptyno = (FAR int *)((uintptr_t)arg);
|
|
|
|
if (ptyno == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-07 09:03:58 +01:00
|
|
|
*ptyno = devpair->pp_minor;
|
2016-07-15 16:33:47 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCSPTLCK: /* Lock/unlock Pty: int */
|
2016-07-15 17:39:33 +02:00
|
|
|
{
|
|
|
|
if (arg == 0)
|
|
|
|
{
|
2022-02-06 17:29:04 +01:00
|
|
|
if (devpair->pp_locked)
|
|
|
|
{
|
|
|
|
/* Release any waiting threads */
|
2016-07-15 17:39:33 +02:00
|
|
|
|
2022-02-06 17:29:04 +01:00
|
|
|
ret = nxsem_post(&devpair->pp_slavesem);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
devpair->pp_locked = false;
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 17:39:33 +02:00
|
|
|
}
|
2022-02-06 17:29:04 +01:00
|
|
|
else if (!devpair->pp_locked)
|
2016-07-15 17:39:33 +02:00
|
|
|
{
|
|
|
|
/* Locking */
|
|
|
|
|
2022-02-06 17:29:04 +01:00
|
|
|
ret = nxsem_wait(&devpair->pp_slavesem);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
devpair->pp_locked = true;
|
|
|
|
}
|
2016-07-15 17:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
2022-02-06 17:29:04 +01:00
|
|
|
*ptr = devpair->pp_locked;
|
2016-07-15 17:39:33 +02:00
|
|
|
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
|
|
|
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;
|
2023-05-09 08:58:02 +02:00
|
|
|
dev->pd_lflag = termiosp->c_lflag;
|
2016-07-18 19:27:06 +02:00
|
|
|
ret = OK;
|
2016-07-16 18:43:44 +02:00
|
|
|
}
|
|
|
|
break;
|
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;
|
|
|
|
|
2021-11-05 07:46:39 +01:00
|
|
|
case FIONBIO:
|
|
|
|
{
|
2021-11-09 08:24:41 +01:00
|
|
|
ret = file_ioctl(&dev->pd_src, cmd, arg);
|
2021-11-25 06:32:52 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2022-08-30 07:46:46 +02:00
|
|
|
ret = file_ioctl(&dev->pd_sink, cmd, arg);
|
2021-11-25 06:32:52 +01:00
|
|
|
}
|
2021-11-05 07:46:39 +01:00
|
|
|
}
|
|
|
|
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
|
2021-11-05 07:46:39 +01:00
|
|
|
ret = -ENOTTY;
|
2016-07-25 22:06:32 +02:00
|
|
|
#endif
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devpair->pp_lock);
|
2016-07-15 04:20:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_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;
|
2018-08-26 15:27:19 +02:00
|
|
|
FAR struct pty_devpair_s *devpair;
|
|
|
|
FAR struct pty_poll_s *pollp = NULL;
|
2020-04-01 18:13:52 +02:00
|
|
|
int ret;
|
2018-08-26 15:27:19 +02:00
|
|
|
int i;
|
2016-07-15 18:07:33 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
dev = inode->i_private;
|
2018-08-26 15:27:19 +02:00
|
|
|
devpair = dev->pd_devpair;
|
2016-07-15 18:07:33 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devpair->pp_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
for (i = 0; i < CONFIG_DEV_PTY_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
if (dev->pd_poll[i].src == NULL && dev->pd_poll[i].sink == NULL)
|
|
|
|
{
|
|
|
|
pollp = &dev->pd_poll[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_DEV_PTY_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pollp = (FAR struct pty_poll_s *)fds->priv;
|
|
|
|
}
|
2016-07-15 18:07:33 +02:00
|
|
|
|
2020-05-04 08:49:38 +02:00
|
|
|
/* POLLIN: Data may be read without blocking. */
|
2016-07-15 18:07:33 +02:00
|
|
|
|
|
|
|
if ((fds->events & POLLIN) != 0)
|
|
|
|
{
|
2018-08-26 15:27:19 +02:00
|
|
|
fds->priv = pollp->src;
|
2016-07-16 15:03:38 +02:00
|
|
|
ret = file_poll(&dev->pd_src, fds, setup);
|
2018-08-26 15:27:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
pollp->src = fds->priv;
|
2016-07-15 18:07:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
/* POLLOUT: Normal data may be written without blocking. */
|
2016-07-15 18:07:33 +02:00
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
if ((fds->events & POLLOUT) != 0)
|
|
|
|
{
|
|
|
|
fds->priv = pollp->sink;
|
|
|
|
ret = file_poll(&dev->pd_sink, fds, setup);
|
|
|
|
if (ret < 0)
|
2016-07-15 18:07:33 +02:00
|
|
|
{
|
2018-08-26 15:27:19 +02:00
|
|
|
if (pollp->src)
|
|
|
|
{
|
|
|
|
fds->priv = pollp->src;
|
|
|
|
file_poll(&dev->pd_src, fds, false);
|
|
|
|
pollp->src = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout;
|
2016-07-15 18:07:33 +02:00
|
|
|
}
|
2020-04-01 18:13:52 +02:00
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
pollp->sink = fds->priv;
|
2016-07-15 18:07:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
fds->priv = pollp;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devpair->pp_lock);
|
2016-07-15 18:07:33 +02:00
|
|
|
return ret;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2020-04-01 18:13:52 +02:00
|
|
|
int ret;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
DEBUGASSERT(inode != NULL && inode->i_private != NULL);
|
2022-02-06 18:31:01 +01: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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devpair->pp_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devpair->pp_lock);
|
2016-07-15 04:20:48 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
2020-04-01 18:13:52 +02:00
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-02-06 18:31:01 +01:00
|
|
|
* Name: pty_register2
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
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.
|
2022-02-06 18:31:01 +01:00
|
|
|
* susv1 - select SUSv1 or BSD behaviour
|
2016-07-15 14:48:10 +02:00
|
|
|
*
|
2016-07-15 15:18:12 +02:00
|
|
|
* Returned Value:
|
2020-05-04 08:49:38 +02:00
|
|
|
* 0 is returned on success; otherwise, the negative error code return
|
|
|
|
* appropriately.
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
int pty_register2(int minor, bool susv1)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
FAR struct pty_devpair_s *devpair;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize semaphores & mutex */
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&devpair->pp_slavesem, 0, 0);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&devpair->pp_lock);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2023-02-27 10:14:22 +01:00
|
|
|
/* Map CR -> NL from terminal input (master)
|
|
|
|
* For some usage like adb shell:
|
2023-03-20 03:54:12 +01:00
|
|
|
* adb shell write \r -> nsh read \n and echo input
|
2023-02-27 10:14:22 +01:00
|
|
|
* nsh write \n -> adb shell read \r\n
|
|
|
|
*/
|
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
devpair->pp_susv1 = susv1;
|
2016-07-15 15:33:48 +02:00
|
|
|
devpair->pp_minor = minor;
|
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;
|
2023-02-27 10:14:22 +01:00
|
|
|
devpair->pp_master.pd_oflag = OPOST | OCRNL;
|
2016-07-15 17:39:33 +02:00
|
|
|
devpair->pp_slave.pd_devpair = devpair;
|
2021-06-11 15:28:55 +02:00
|
|
|
devpair->pp_slave.pd_oflag = OPOST | ONLCR;
|
2023-03-20 03:54:12 +01:00
|
|
|
devpair->pp_slave.pd_lflag = ECHO;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-07 09:03:58 +01:00
|
|
|
/* Register the master device
|
2016-07-15 14:48:10 +02:00
|
|
|
*
|
2022-02-07 09:03:58 +01:00
|
|
|
* BSD style (deprecated): /dev/ptyN
|
|
|
|
* 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
|
|
|
|
2022-02-07 09:03:58 +01:00
|
|
|
snprintf(devname, 16, "/dev/pty%d", minor);
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-07 09:03:58 +01:00
|
|
|
ret = register_driver(devname, &g_pty_fops, 0666, &devpair->pp_master);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-02-06 15:46:54 +01:00
|
|
|
goto errout_with_devpair;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 09:03:58 +01:00
|
|
|
/* Register the slave device
|
2016-07-15 14:48:10 +02:00
|
|
|
*
|
2022-02-07 09:03:58 +01:00
|
|
|
* BSD style (deprecated): /dev/ttypN
|
|
|
|
* SUSv1 style: /dev/pts/N
|
2016-07-15 14:48:10 +02:00
|
|
|
*
|
|
|
|
* Where N is the minor number
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
if (susv1)
|
|
|
|
{
|
|
|
|
snprintf(devname, 16, "/dev/pts/%d", minor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(devname, 16, "/dev/ttyp%d", minor);
|
|
|
|
}
|
2016-07-15 14:48:10 +02:00
|
|
|
|
2022-02-07 09:03:58 +01:00
|
|
|
ret = register_driver(devname, &g_pty_fops, 0666, &devpair->pp_slave);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-02-07 09:03:58 +01:00
|
|
|
goto errout_with_master;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
2022-02-07 09:03:58 +01:00
|
|
|
errout_with_master:
|
|
|
|
snprintf(devname, 16, "/dev/pty%d", minor);
|
2020-01-02 17:49:34 +01:00
|
|
|
unregister_driver(devname);
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
errout_with_devpair:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&devpair->pp_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
nxsem_destroy(&devpair->pp_slavesem);
|
|
|
|
kmm_free(devpair);
|
|
|
|
return ret;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
2022-02-06 18:31:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create and register PTY master and slave devices. The master device
|
|
|
|
* will be registered at /dev/ptyN and slave at /dev/ttypN where N is
|
|
|
|
* the provided minor number.
|
|
|
|
*
|
|
|
|
* The slave side of the interface is always locked initially. The
|
|
|
|
* master must call unlockpt() before the slave device can be opened.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* minor - The number that qualifies the naming of the created devices.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pty_register(int minor)
|
|
|
|
{
|
|
|
|
return pty_register2(minor, false);
|
|
|
|
}
|