Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/serial/serial.c: adapt to the iovec-based api #14898

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions drivers/loop/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@
* Private Function Prototypes
****************************************************************************/

static ssize_t loop_readv(FAR struct file *filep,
FAR const struct uio *uio);
static ssize_t loop_writev(FAR struct file *filep,
FAR const struct uio *uio);
static ssize_t loop_readv(FAR struct file *filep, FAR struct uio *uio);
static ssize_t loop_writev(FAR struct file *filep, FAR struct uio *uio);
static int loop_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);

Expand Down Expand Up @@ -71,23 +69,29 @@ static const struct file_operations g_loop_fops =
****************************************************************************/

/****************************************************************************
* Name: loop_read
* Name: loop_readv
****************************************************************************/

static ssize_t loop_readv(FAR struct file *filep,
FAR const struct uio *uio)
static ssize_t loop_readv(FAR struct file *filep, FAR struct uio *uio)
{
return 0; /* Return EOF */
}

/****************************************************************************
* Name: loop_write
* Name: loop_writev
****************************************************************************/

static ssize_t loop_writev(FAR struct file *filep,
FAR const struct uio *uio)
static ssize_t loop_writev(FAR struct file *filep, FAR struct uio *uio)
{
return uio_total_len(uio); /* Say that everything was written */
/* Say that everything was written */

ssize_t ret = uio_resid(uio);
if (ret >= 0)
{
uio_advance(uio, ret);
}

return ret;
}

/****************************************************************************
Expand Down
24 changes: 13 additions & 11 deletions drivers/misc/dev_null.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@
* Private Function Prototypes
****************************************************************************/

static ssize_t devnull_readv(FAR struct file *filep,
FAR const struct uio *uio);
static ssize_t devnull_writev(FAR struct file *filep,
FAR const struct uio *uio);
static ssize_t devnull_readv(FAR struct file *filep, FAR struct uio *uio);
static ssize_t devnull_writev(FAR struct file *filep, FAR struct uio *uio);
static int devnull_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup);

Expand Down Expand Up @@ -71,11 +69,10 @@ static const struct file_operations g_devnull_fops =
****************************************************************************/

/****************************************************************************
* Name: devnull_read
* Name: devnull_readv
****************************************************************************/

static ssize_t devnull_readv(FAR struct file *filep,
FAR const struct uio *uio)
static ssize_t devnull_readv(FAR struct file *filep, FAR struct uio *uio)
{
UNUSED(filep);
UNUSED(uio);
Expand All @@ -84,15 +81,20 @@ static ssize_t devnull_readv(FAR struct file *filep,
}

/****************************************************************************
* Name: devnull_write
* Name: devnull_writev
****************************************************************************/

static ssize_t devnull_writev(FAR struct file *filep,
FAR const struct uio *uio)
static ssize_t devnull_writev(FAR struct file *filep, FAR struct uio *uio)
{
UNUSED(filep);

return uio_total_len(uio); /* Say that everything was written */
ssize_t ret = uio_resid(uio); /* Say that everything was written */
if (ret >= 0)
{
uio_advance(uio, ret);
}

return ret;
}

/****************************************************************************
Expand Down
29 changes: 17 additions & 12 deletions drivers/misc/dev_zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@
* Private Function Prototypes
****************************************************************************/

static ssize_t devzero_readv(FAR struct file *filep,
FAR const struct uio *uio);
static ssize_t devzero_writev(FAR struct file *filep,
FAR const struct uio *uio);
static ssize_t devzero_readv(FAR struct file *filep, FAR struct uio *uio);
static ssize_t devzero_writev(FAR struct file *filep, FAR struct uio *uio);
static int devzero_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup);

Expand Down Expand Up @@ -71,13 +69,12 @@ static const struct file_operations g_devzero_fops =
****************************************************************************/

/****************************************************************************
* Name: devzero_read
* Name: devzero_readv
****************************************************************************/

static ssize_t devzero_readv(FAR struct file *filep,
FAR const struct uio *uio)
static ssize_t devzero_readv(FAR struct file *filep, FAR struct uio *uio)
{
ssize_t total = uio_total_len(uio);
ssize_t total = uio_resid(uio);
FAR const struct iovec *iov = uio->uio_iov;
int iovcnt = uio->uio_iovcnt;
int i;
Expand All @@ -94,19 +91,27 @@ static ssize_t devzero_readv(FAR struct file *filep,
memset(iov[i].iov_base, 0, iov[i].iov_len);
}

uio_advance(uio, total);

return total;
}

/****************************************************************************
* Name: devzero_write
* Name: devzero_writev
****************************************************************************/

static ssize_t devzero_writev(FAR struct file *filep,
FAR const struct uio *uio)
static ssize_t devzero_writev(FAR struct file *filep, FAR struct uio *uio)
{
ssize_t total;
UNUSED(filep);

return uio_total_len(uio);
total = uio_resid(uio);
if (total >= 0)
{
uio_advance(uio, total);
}

return total;
}

/****************************************************************************
Expand Down
105 changes: 83 additions & 22 deletions drivers/serial/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch,
static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
FAR const char *buffer,
size_t buflen);
static inline ssize_t uart_irqwritev(FAR uart_dev_t *dev,
FAR struct uio *uio);
static int uart_tcdrain(FAR uart_dev_t *dev,
bool cancelable, clock_t timeout);

Expand All @@ -110,11 +112,8 @@ static int uart_tcsendbreak(FAR uart_dev_t *dev,

static int uart_open(FAR struct file *filep);
static int uart_close(FAR struct file *filep);
static ssize_t uart_read(FAR struct file *filep,
FAR char *buffer, size_t buflen);
static ssize_t uart_write(FAR struct file *filep,
FAR const char *buffer,
size_t buflen);
static ssize_t uart_readv(FAR struct file *filep, FAR struct uio *uio);
static ssize_t uart_writev(FAR struct file *filep, FAR struct uio *uio);
static int uart_ioctl(FAR struct file *filep,
int cmd, unsigned long arg);
static int uart_poll(FAR struct file *filep,
Expand All @@ -141,15 +140,15 @@ static const struct file_operations g_serialops =
{
uart_open, /* open */
uart_close, /* close */
uart_read, /* read */
uart_write, /* write */
NULL, /* read */
NULL, /* write */
NULL, /* seek */
uart_ioctl, /* ioctl */
NULL, /* mmap */
NULL, /* truncate */
uart_poll, /* poll */
NULL, /* readv */
NULL /* writev */
uart_readv, /* readv */
uart_writev /* writev */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, uart_unlink /* unlink */
#endif
Expand Down Expand Up @@ -441,6 +440,50 @@ static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
return buflen;
}

/****************************************************************************
* Name: uart_irqwritev
****************************************************************************/

static inline ssize_t uart_irqwritev(FAR uart_dev_t *dev,
FAR struct uio *uio)
{
ssize_t error = 0;
ssize_t total = 0;
int iovcnt = uio->uio_iovcnt;
int i;

for (i = 0; i < iovcnt; i++)
{
const struct iovec *iov = &uio->uio_iov[i];
if (iov->iov_len == 0)
{
continue;
}

ssize_t written = uart_irqwrite(dev, iov->iov_base, iov->iov_len);
if (written < 0)
{
error = written;
break;
}

if (SSIZE_MAX - total < written)
{
error = -EOVERFLOW;
break;
}

total += written;
}

if (error != 0 && total == 0)
{
return error;
}

return total;
}

/****************************************************************************
* Name: uart_tcdrain
*
Expand Down Expand Up @@ -847,11 +890,10 @@ static int uart_close(FAR struct file *filep)
}

/****************************************************************************
* Name: uart_read
* Name: uart_readv
****************************************************************************/

static ssize_t uart_read(FAR struct file *filep,
FAR char *buffer, size_t buflen)
static ssize_t uart_readv(FAR struct file *filep, FAR struct uio *uio)
{
FAR struct inode *inode = filep->f_inode;
FAR uart_dev_t *dev = inode->i_private;
Expand All @@ -862,11 +904,18 @@ static ssize_t uart_read(FAR struct file *filep,
#endif
irqstate_t flags;
ssize_t recvd = 0;
ssize_t buflen;
bool echoed = false;
int16_t tail;
char ch;
int ret;

buflen = uio_resid(uio);
if (buflen < 0)
{
return buflen;
}

/* Only one user can access rxbuf->tail at a time */

ret = nxmutex_lock(&dev->recv.lock);
Expand All @@ -885,7 +934,7 @@ static ssize_t uart_read(FAR struct file *filep,
* data from the end of the buffer.
*/

while ((size_t)recvd < buflen)
while (recvd < buflen)
{
#ifdef CONFIG_SERIAL_REMOVABLE
/* If the removable device is no longer connected, refuse to read any
Expand Down Expand Up @@ -961,7 +1010,8 @@ static ssize_t uart_read(FAR struct file *filep,
{
if (recvd > 0)
{
*buffer-- = '\0';
static const char zero = '\0';
uio_copyfrom(uio, recvd, &zero, 1);
recvd--;
if (dev->tc_lflag & ECHO)
{
Expand Down Expand Up @@ -990,7 +1040,7 @@ static ssize_t uart_read(FAR struct file *filep,

/* Store the received character */

*buffer++ = ch;
uio_copyfrom(uio, recvd, &ch, 1);
recvd++;

if (dev->tc_lflag & ECHO)
Expand Down Expand Up @@ -1325,19 +1375,24 @@ static ssize_t uart_read(FAR struct file *filep,
#endif

nxmutex_unlock(&dev->recv.lock);
if (recvd >= 0)
{
uio_advance(uio, recvd);
}

return recvd;
}

/****************************************************************************
* Name: uart_write
* Name: uart_writev
****************************************************************************/

static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
static ssize_t uart_writev(FAR struct file *filep, FAR struct uio *uio)
{
FAR struct inode *inode = filep->f_inode;
FAR uart_dev_t *dev = inode->i_private;
ssize_t nwritten = buflen;
ssize_t nwritten;
ssize_t buflen;
bool oktoblock;
int ret;
char ch;
Expand All @@ -1363,12 +1418,18 @@ static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer,
#endif

flags = enter_critical_section();
ret = uart_irqwrite(dev, buffer, buflen);
ret = uart_irqwritev(dev, uio);
leave_critical_section(flags);

return ret;
}

buflen = nwritten = uio_resid(uio);
if (nwritten < 0)
{
return nwritten;
}

/* Only one user can access dev->xmit.head at a time */

ret = nxmutex_lock(&dev->xmit.lock);
Expand Down Expand Up @@ -1408,9 +1469,9 @@ static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer,
*/

uart_disabletxint(dev);
for (; buflen; buflen--)
for (; buflen; uio_advance(uio, 1), buflen--)
{
ch = *buffer++;
uio_copyto(uio, 0, &ch, 1);
ret = OK;

/* Do output post-processing */
Expand Down
Loading
Loading