static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t ret = buflen; /* We may receive console writes through this path from * interrupt handlers and from debug output in the IDLE task! * In these cases, we will need to do things a little * differently. */ if (up_interrupt_context() || getpid() == 0) { if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return ERROR; } } /* Only one user can be accessing dev->xmit.head at once */ uart_takesem(&dev->xmit.sem); /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { int ch = *buffer++; /* Put the character into the transmit buffer */ uart_putxmitchar(dev, ch); /* If this is the console, then we should replace LF with LF-CR */ if (dev->isconsole && ch == '\n') { uart_putxmitchar(dev, '\r'); } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); return ret; }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t nwritten = buflen; bool oktoblock; int ret; char ch; /* We may receive console writes through this path from interrupt handlers and * from debug output in the IDLE task! In these cases, we will need to do things * a little differently. */ if (up_interrupt_context() || getpid() == 0) { #ifdef CONFIG_SERIAL_REMOVABLE /* If the removable device is no longer connected, refuse to write to * the device. */ if (dev->disconnected) { return -ENOTCONN; } #endif /* up_putc() will be used to generate the output in a busy-wait loop. * up_putc() is only available for the console device. */ if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return -EPERM; } } /* Only one user can access dev->xmit.head at a time */ ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will * abort the transfer. After the transfer has started, we are committed * and signals will be ignored. */ return ret; } #ifdef CONFIG_SERIAL_REMOVABLE /* If the removable device is no longer connected, refuse to write to the * device. This check occurs after taking the xmit.sem because the * disconnection event might have occurred while we were waiting for * access to the transmit buffers. */ if (dev->disconnected) { uart_givesem(&dev->xmit.sem); return -ENOTCONN; } #endif /* Can the following loop block, waiting for space in the TX * buffer? */ oktoblock = ((filep->f_oflags & O_NONBLOCK) == 0); /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { ch = *buffer++; ret = OK; #ifdef CONFIG_SERIAL_TERMIOS /* Do output post-processing */ if (dev->tc_oflag & OPOST) { /* Mapping CR to NL? */ if ((ch == '\r') && (dev->tc_oflag & OCRNL)) { ch = '\n'; } /* Are we interested in newline processing? */ if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET))) { ret = uart_putxmitchar(dev, '\r', oktoblock); if (ret < 0) { break; } } /* Specifically not handled: * * OXTABS - primarily a full-screen terminal optimisation * ONOEOT - Unix interoperability hack * OLCUC - Not specified by Posix * ONOCR - low-speed interactive optimisation */ } #else /* !CONFIG_SERIAL_TERMIOS */ /* If this is the console, convert \n -> \r\n */ if (dev->isconsole && ch == '\n') { ret = uart_putxmitchar(dev, '\r', oktoblock); } #endif /* Put the character into the transmit buffer */ if (ret == OK) { ret = uart_putxmitchar(dev, ch, oktoblock); } /* uart_putxmitchar() might return an error under one of two * conditions: (1) The wait for buffer space might have been * interrupted by a signal (ret should be -EINTR), (2) if * CONFIG_SERIAL_REMOVABLE is defined, then uart_putxmitchar() * might also return if the serial device was disconnected * (with -ENOTCONN), or (3) if O_NONBLOCK is specified, then * then uart_putxmitchar() might return -EAGAIN if the output * TX buffer is full. */ if (ret < 0) { /* POSIX requires that we return -1 and errno set if no data was * transferred. Otherwise, we return the number of bytes in the * interrupted transfer. */ if (buflen < nwritten) { /* Some data was transferred. Return the number of bytes that * were successfully transferred. */ nwritten -= buflen; } else { /* No data was transferred. Return the negated errno value. * The VFS layer will set the errno value appropriately). */ nwritten = ret; } break; } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); return nwritten; }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t nread = buflen; int ret; /* We may receive console writes through this path from interrupt handlers and * from debug output in the IDLE task! In these cases, we will need to do things * a little differently. */ if (up_interrupt_context() || getpid() == 0) { /* up_putc() will be used to generate the output in a busy-wait loop. * up_putc() is only available for the console device. */ if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return -EPERM; } } /* Only one user can access dev->xmit.head at a time */ ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will * abort the transfer. After the transfer has started, we are committed * and signals will be ignored. */ return ret; } /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { int ch = *buffer++; /* If the ONLCR flag is set, we should translate \n to \r\n */ ret = OK; if ((ch == '\n') && (dev->termios_s.c_oflag && ONLCR)) { ret = uart_putxmitchar(dev, '\r'); } /* Put the character into the transmit buffer */ if (ret == OK) { ret = uart_putxmitchar(dev, ch); } /* Were we awakened by a signal? That should be the only condition that * uart_putxmitchar() should return an error. */ if (ret < 0) { /* POSIX requires that we return -1 and errno set if no data was * transferred. Otherwise, we return the number of bytes in the * interrupted transfer. */ if (buflen < nread) { /* Some data was transferred. Return the number of bytes that were * successfully transferred. */ nread -= buflen; } else { /* No data was transferred. Return -EINTR. The VFS layer will * set the errno value appropriately). */ nread = -EINTR; } break; } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); return nread; }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t nread = buflen; int ret; char ch; /* We may receive console writes through this path from interrupt handlers and * from debug output in the IDLE task! In these cases, we will need to do things * a little differently. */ if (up_interrupt_context() || getpid() == 0) { /* up_putc() will be used to generate the output in a busy-wait loop. * up_putc() is only available for the console device. */ if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return -EPERM; } } /* Only one user can access dev->xmit.head at a time */ ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will * abort the transfer. */ return ret; } /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { ch = *buffer++; /* Do output post-processing */ #ifdef CONFIG_SERIAL_TERMIOS if (dev->tc_oflag & OPOST) { /* Mapping CR to NL? */ if ((ch == '\r') && (dev->tc_oflag & OCRNL)) { ch = '\n'; } /* Are we interested in newline processing? */ if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET))) { ret = uart_putxmitchar(dev, '\r'); if (ret != OK) { break; } } /* Specifically not handled: * * OXTABS - primarily a full-screen terminal optimisation * ONOEOT - Unix interoperability hack * OLCUC - Not specified by Posix * ONOCR - low-speed interactive optimisation */ } #else /* !CONFIG_SERIAL_TERMIOS */ /* If this is the console, convert \n -> \r\n */ if (dev->isconsole && ch == '\n') { ret = uart_putxmitchar(dev, '\r'); } #endif /* Put the character into the transmit buffer */ ret = uart_putxmitchar(dev, ch); if (ret != OK) { break; } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); /* Were we interrupted by a signal? That should be the only condition that * uart_putxmitchar() should return an error. */ if (ret < 0) { /* POSIX requires that we return -1 and errno set if no data was * transferred. Otherwise, we return the number of bytes in the * interrupted transfer. */ if (buflen < nread) { /* Some data was transferred. Return the number of bytes that were * successfully transferred. */ nread -= buflen; } else { /* No data was transferred. Return -EINTR. The VFS layer will * set the errno value appropriately). */ nread = -EINTR; } } return nread; }