Lines Matching +full:fifo +full:- +full:size
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
64 struct fifo {
68 int num; /* number of characters in the fifo */
69 int size; /* size of the fifo */
74 struct fifo rxfifo;
101 tcgetattr(tf->rfd, &orig);
105 tcsetattr(tf->rfd, TCSANOW, &new);
119 len = read(tf->rfd, &rb, 1);
129 return (write(tf->wfd, &wb, 1));
135 return (sc->rxfifo.num < sc->rxfifo.size);
141 struct fifo *fifo;
145 fifo = &sc->rxfifo;
146 if (fifo->num > 0) {
149 c = fifo->buf[fifo->rindex];
150 fifo->rindex = (fifo->rindex + 1) % fifo->size;
151 fifo->num--;
153 if (sc->tty.opened) {
154 error = mevent_enable(sc->mev);
160 return (-1);
166 return (sc->rxfifo.num);
172 struct fifo *fifo;
175 fifo = &sc->rxfifo;
177 if (fifo->num < fifo->size) {
178 fifo->buf[fifo->windex] = ch;
179 fifo->windex = (fifo->windex + 1) % fifo->size;
180 fifo->num++;
182 if (sc->tty.opened) {
184 * Disable mevent callback if the FIFO is full.
186 error = mevent_disable(sc->mev);
192 return (-1);
202 if (ttyread(&sc->tty, &ch) == 0 && sc->tty.is_socket)
206 len = ttyread(&sc->tty, &ch);
209 if (len == 0 && sc->tty.is_socket)
224 } else if (sc->tty.opened) {
225 /* write returning -1 means disconnected. */
226 if (ttywrite(&sc->tty, ch) == -1 && sc->tty.is_socket)
236 uart_rxfifo_reset(struct uart_softc *sc, int size)
239 struct fifo *fifo;
243 fifo = &sc->rxfifo;
244 bzero(fifo, sizeof(struct fifo));
245 fifo->size = size;
247 if (sc->tty.opened) {
252 nread = read(sc->tty.rfd, flushbuf, sizeof(flushbuf));
261 error = mevent_enable(sc->mev);
278 SNAPSHOT_VAR_OR_LEAVE(sc->rxfifo.rindex, meta, ret, done);
279 SNAPSHOT_VAR_OR_LEAVE(sc->rxfifo.windex, meta, ret, done);
280 SNAPSHOT_VAR_OR_LEAVE(sc->rxfifo.num, meta, ret, done);
281 SNAPSHOT_VAR_OR_LEAVE(sc->rxfifo.size, meta, ret, done);
282 SNAPSHOT_BUF_OR_LEAVE(sc->rxfifo.buf, sizeof(sc->rxfifo.buf),
299 struct uart_softc *sc = socket_softc->softc;
303 if (conn_fd == -1)
309 pthread_mutex_lock(&sc->mtx);
311 if (sc->tty.opened) {
313 pthread_mutex_unlock(&sc->mtx);
316 sc->tty.rfd = sc->tty.wfd = conn_fd;
317 sc->tty.opened = true;
318 sc->mev = mevent_add(sc->tty.rfd, EVF_READ, socket_softc->drain,
319 socket_softc->arg);
322 pthread_mutex_unlock(&sc->mtx);
326 if (conn_fd != -1)
331 * When a connection-oriented protocol disconnects, this handler is used to
340 mevent_delete_close(sc->mev);
341 sc->mev = NULL;
342 sc->tty.opened = false;
343 sc->tty.rfd = sc->tty.wfd = -1;
355 return (-1);
357 sc->tty.rfd = STDIN_FILENO;
358 sc->tty.wfd = STDOUT_FILENO;
359 sc->tty.opened = true;
361 if (fcntl(sc->tty.rfd, F_SETFL, O_NONBLOCK) != 0)
362 return (-1);
363 if (fcntl(sc->tty.wfd, F_SETFL, O_NONBLOCK) != 0)
364 return (-1);
368 if (caph_rights_limit(sc->tty.rfd, &rights) == -1)
370 if (caph_ioctls_limit(sc->tty.rfd, cmds, nitems(cmds)) == -1)
390 return (-1);
394 return (-1);
397 sc->tty.rfd = sc->tty.wfd = fd;
398 sc->tty.opened = true;
402 if (caph_rights_limit(fd, &rights) == -1)
404 if (caph_ioctls_limit(fd, cmds, nitems(cmds)) == -1)
425 int bind_fd = -1;
454 if (bind(bind_fd, src_addr->ai_addr, src_addr->ai_addrlen) == -1) {
464 if (fcntl(bind_fd, F_SETFL, O_NONBLOCK) == -1)
467 if (listen(bind_fd, 1) == -1) {
480 sc->tty.is_socket = true;
482 socket_softc->softc = sc;
483 socket_softc->drain = drain;
484 socket_softc->arg = arg;
489 if (caph_rights_limit(bind_fd, &rights) == -1)
491 if (caph_ioctls_limit(bind_fd, cmds, nitems(cmds)) == -1)
493 if (caph_fcntls_limit(bind_fd, CAP_FCNTL_SETFL) == -1)
497 if ((sc->mev = mevent_add(bind_fd, EVF_READ, uart_tcp_listener,
504 if (bind_fd != -1)
510 return (-1);
520 pthread_mutex_init(&sc->mtx, NULL);
539 * A connection-oriented protocol should wait for a connection,
542 if (retval == 0 && !sc->tty.is_socket) {
543 ttyopen(&sc->tty);
544 sc->mev = mevent_add(sc->tty.rfd, EVF_READ, drain, arg);
545 assert(sc->mev != NULL);
554 pthread_mutex_lock(&sc->mtx);
560 pthread_mutex_unlock(&sc->mtx);