#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/uio.h>
#include <vm/uma.h>
struct ttyoutq_block {
struct ttyoutq_block *tob_next;
char tob_data[TTYOUTQ_DATASIZE];
};
static uma_zone_t ttyoutq_zone;
#define TTYOUTQ_INSERT_TAIL(to, tob) do { \
if (to->to_end == 0) { \
tob->tob_next = to->to_firstblock; \
to->to_firstblock = tob; \
} else { \
tob->tob_next = to->to_lastblock->tob_next; \
to->to_lastblock->tob_next = tob; \
} \
to->to_nblocks++; \
} while (0)
#define TTYOUTQ_REMOVE_HEAD(to) do { \
to->to_firstblock = to->to_firstblock->tob_next; \
to->to_nblocks--; \
} while (0)
#define TTYOUTQ_RECYCLE(to, tob) do { \
if (to->to_quota <= to->to_nblocks) \
uma_zfree(ttyoutq_zone, tob); \
else \
TTYOUTQ_INSERT_TAIL(to, tob); \
} while (0)
void
ttyoutq_flush(struct ttyoutq *to)
{
to->to_begin = 0;
to->to_end = 0;
}
int
ttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size)
{
struct ttyoutq_block *tob;
to->to_quota = howmany(size, TTYOUTQ_DATASIZE);
while (to->to_quota > to->to_nblocks) {
tty_unlock(tp);
tob = uma_zalloc(ttyoutq_zone, M_WAITOK);
tty_lock(tp);
if (tty_gone(tp)) {
uma_zfree(ttyoutq_zone, tob);
return (ENXIO);
}
TTYOUTQ_INSERT_TAIL(to, tob);
}
return (0);
}
void
ttyoutq_free(struct ttyoutq *to)
{
struct ttyoutq_block *tob;
ttyoutq_flush(to);
to->to_quota = 0;
while ((tob = to->to_firstblock) != NULL) {
TTYOUTQ_REMOVE_HEAD(to);
uma_zfree(ttyoutq_zone, tob);
}
MPASS(to->to_nblocks == 0);
}
size_t
ttyoutq_read(struct ttyoutq *to, void *buf, size_t len)
{
char *cbuf = buf;
while (len > 0) {
struct ttyoutq_block *tob;
size_t cbegin, cend, clen;
if (to->to_begin == to->to_end)
break;
tob = to->to_firstblock;
if (tob == NULL)
break;
cbegin = to->to_begin;
cend = MIN(MIN(to->to_end, to->to_begin + len),
TTYOUTQ_DATASIZE);
clen = cend - cbegin;
memcpy(cbuf, tob->tob_data + cbegin, clen);
cbuf += clen;
len -= clen;
if (cend == to->to_end) {
to->to_begin = 0;
to->to_end = 0;
} else if (cend == TTYOUTQ_DATASIZE) {
TTYOUTQ_REMOVE_HEAD(to);
to->to_begin = 0;
to->to_end -= TTYOUTQ_DATASIZE;
TTYOUTQ_RECYCLE(to, tob);
} else {
to->to_begin += clen;
}
}
return (cbuf - (char *)buf);
}
int
ttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio)
{
while (uio->uio_resid > 0) {
int error;
struct ttyoutq_block *tob;
size_t cbegin, cend, clen;
if (to->to_begin == to->to_end)
return (0);
tob = to->to_firstblock;
if (tob == NULL)
return (0);
cbegin = to->to_begin;
cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid),
TTYOUTQ_DATASIZE);
clen = cend - cbegin;
if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) {
TTYOUTQ_REMOVE_HEAD(to);
to->to_begin = 0;
if (to->to_end <= TTYOUTQ_DATASIZE)
to->to_end = 0;
else
to->to_end -= TTYOUTQ_DATASIZE;
tty_unlock(tp);
error = uiomove(tob->tob_data + cbegin, clen, uio);
tty_lock(tp);
TTYOUTQ_RECYCLE(to, tob);
} else {
char ob[TTYOUTQ_DATASIZE - 1];
memcpy(ob, tob->tob_data + cbegin, clen);
to->to_begin += clen;
MPASS(to->to_begin < TTYOUTQ_DATASIZE);
tty_unlock(tp);
error = uiomove(ob, clen, uio);
tty_lock(tp);
}
if (error != 0)
return (error);
}
return (0);
}
size_t
ttyoutq_write(struct ttyoutq *to, const void *buf, size_t nbytes)
{
const char *cbuf = buf;
struct ttyoutq_block *tob;
unsigned int boff;
size_t l;
while (nbytes > 0) {
boff = to->to_end % TTYOUTQ_DATASIZE;
if (to->to_end == 0) {
MPASS(to->to_begin == 0);
tob = to->to_firstblock;
if (tob == NULL) {
break;
}
to->to_lastblock = tob;
} else if (boff == 0) {
tob = to->to_lastblock->tob_next;
if (tob == NULL) {
break;
}
to->to_lastblock = tob;
} else {
tob = to->to_lastblock;
}
l = MIN(nbytes, TTYOUTQ_DATASIZE - boff);
MPASS(l > 0);
memcpy(tob->tob_data + boff, cbuf, l);
cbuf += l;
nbytes -= l;
to->to_end += l;
}
return (cbuf - (const char *)buf);
}
int
ttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes)
{
size_t ret __unused;
if (ttyoutq_bytesleft(to) < nbytes)
return (-1);
ret = ttyoutq_write(to, buf, nbytes);
MPASS(ret == nbytes);
return (0);
}
static void
ttyoutq_startup(void *dummy)
{
ttyoutq_zone = uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block),
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
}
SYSINIT(ttyoutq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyoutq_startup, NULL);